Skip to content

Commit 8d442e6

Browse files
committed
Remove inplace functions
1 parent 4011f23 commit 8d442e6

File tree

5 files changed

+3
-130
lines changed

5 files changed

+3
-130
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "AIBECS"
22
uuid = "ace601d6-714c-11e9-04e5-89b7fad23838"
33
authors = ["Benoit Pasquier <[email protected]>"]
4-
version = "0.12.0"
4+
version = "0.13.0"
55

66
[deps]
77
Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"

src/multiTracer.jl

+1-86
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,6 @@ function AIBECSFunction(T::Function, Gs::Tuple, nb::Int)
3737
AIBECSFunction((T,), Gs, nb, nt, Tidx)
3838
end
3939
function AIBECSFunction(Ts::Tuple, Gs::Tuple, nb::Int, nt::Int=length(Ts), Tidx::AbstractVector=1:nt)
40-
if all(isinplace(G, nt + 2) for G in Gs) # +2 to account for dx and p
41-
iipAIBECSFunction(Ts, Gs, nb, nt, Tidx)
42-
else
43-
oopAIBECSFunction(Ts, Gs, nb, nt, Tidx)
44-
end
45-
end
46-
47-
function iipAIBECSFunction(Ts, Gs, nb, nt=length(Ts), Tidx=1:nt)
48-
tracers(u) = state_to_tracers(u, nb, nt)
49-
tracer(u, i) = state_to_tracer(u, nb, nt, i)
50-
function G(du, u, p)
51-
for j in 1:nt
52-
Gs[j](tracer(du, j), tracers(u)..., p)
53-
end
54-
du
55-
end
56-
function f(du, u, p, t=0)
57-
G(du, u, p)
58-
for jT in eachindex(Ts)
59-
op = Ts[jT](p)
60-
for j in findall(Tidx .== jT)
61-
mul!(tracer(du, j), op, tracer(u, j), -1, 1)
62-
end
63-
end
64-
du
65-
end
66-
# Jacobian
67-
∇ₓG(u, p) = inplace_local_jacobian(Gs, u, p, nt, nb)
68-
function T(p)
69-
uniqueTs = [Tⱼ(p) for Tⱼ in Ts]
70-
blockdiag([uniqueTs[Tidx[j]] for j in 1:nt]...)
71-
end
72-
jac(u, p, t=0) = ∇ₓG(u, p) - T(p)
73-
return ODEFunction{true}(f, jac=jac)
74-
end
75-
function oopAIBECSFunction(Ts, Gs, nb, nt=length(Ts), Tidx=1:nt)
7640
tracers(u) = state_to_tracers(u, nb, nt)
7741
tracer(u, i) = state_to_tracer(u, nb, nt, i)
7842
G(u, p) = reduce(vcat, Gⱼ(tracers(u)..., p) for Gⱼ in Gs)
@@ -99,54 +63,16 @@ end
9963
function AIBECSFunction(Ts, Gs, nb::Int, ::Type{P}) where {P <: APar}
10064
AIBECSFunction(AIBECSFunction(Ts, Gs, nb), P)
10165
end
102-
function AIBECSFunction(fun::ODEFunction{false}, ::Type{P}) where {P <: APar}
66+
function AIBECSFunction(fun::ODEFunction, ::Type{P}) where {P <: APar}
10367
jac(u, p::P, t=0) = fun.jac(u, p, t)
10468
jac(u, λ::Vector, t=0) = fun.jac(u, λ2p(P, λ), t)
10569
f(u, p::P, t=0) = fun.f(u, p, t)
10670
f(u, λ::Vector, t=0) = fun.f(u, λ2p(P, λ), t)
10771
return ODEFunction{false}(f, jac=jac)
10872
end
109-
function AIBECSFunction(fun::ODEFunction{true}, ::Type{P}) where {P <: APar}
110-
jac(u, p::P, t=0) = fun.jac(u, p, t)
111-
jac(u, λ::Vector, t=0) = fun.jac(u, λ2p(P, λ), t)
112-
f(du, u, p::P, t=0) = fun.f(du, u, p, t)
113-
f(du, u, λ::Vector, t=0) = fun.f(du, u, λ2p(P, λ), t)
114-
return ODEFunction{true}(f, jac=jac)
115-
end
11673

11774
export AIBECSFunction
11875

119-
"""
120-
F, ∇ₓF = F_and_∇ₓF(Ts, Gs, nb)
121-
122-
Returns the state function `F` and its Jacobian, `∇ₓF`.
123-
124-
F, ∇ₓF = F_and_∇ₓF(T, Gs, nb)
125-
126-
Returns the state function `F` and its Jacobian, `∇ₓF` (with all tracers transported by single `T`).
127-
128-
This function is deprecated. Use
129-
130-
F = AIBECSFunction(Ts, Gs, nb)
131-
132-
instead.
133-
You can then call `F(x,p)` for the tendencies, and `F(Val{:jac},x,p)` for the Jacobian.
134-
"""
135-
function F_and_∇ₓF(fun::ODEFunction)
136-
Base.depwarn("""Deprecation:
137-
F, ∇ₓF = F_and_∇ₓF(args...)
138-
is deprecated. Use
139-
F = AIBECSFunction(args...)
140-
instead! (And then you can still call `F(x,p)`.)
141-
""", :F_and_∇ₓF, force=true)
142-
fun.f, fun.jac
143-
end
144-
F_and_∇ₓF(args...) = F_and_∇ₓF(AIBECSFunction(args...))
145-
export F_and_∇ₓF
146-
147-
148-
149-
15076
"""
15177
localderivative(G, x, p)
15278
localderivative(Gᵢ, xs, i, p)
@@ -166,9 +92,6 @@ end
16692
function localderivative(Gᵢ, xs, j, p) # for multiple tracers
16793
return ForwardDiff.derivative-> Gᵢ(perturb_tracer(xs, j, λ)..., p), 0.0)
16894
end
169-
function localderivative(Gᵢ!, dx, xs, j, p) # if Gᵢ are in-place
170-
return ForwardDiff.derivative((dx, λ) -> Gᵢ!(dx, perturb_tracer(xs, j, λ)..., p), dx, 0.0)
171-
end
17295
perturb_tracer(xs, j, λ) = (xs[1:j - 1]..., xs[j] .+ λ, xs[j + 1:end]...)
17396

17497

@@ -202,19 +125,11 @@ export split_state_function_and_Jacobian
202125
function local_jacobian(Gs, x, p, nt, nb)
203126
return reduce(vcat, local_jacobian_row(Gⱼ, x, p, nt, nb) for Gⱼ in Gs)
204127
end
205-
function inplace_local_jacobian(Gs, x, p, nt, nb)
206-
return reduce(vcat, inplace_local_jacobian_row(Gⱼ!, x, p, nt, nb) for Gⱼ! in Gs)
207-
end
208128

209129
function local_jacobian_row(Gᵢ, x, p, nt, nb)
210130
tracers(x) = state_to_tracers(x, nb, nt)
211131
return reduce(hcat, sparse(Diagonal(localderivative(Gᵢ, tracers(x), j, p))) for j in 1:nt)
212132
end
213-
function inplace_local_jacobian_row(Gᵢ!, x, p, nt, nb)
214-
tracers(x) = state_to_tracers(x, nb, nt)
215-
dx = Vector{Float64}(undef, nb)
216-
return reduce(hcat, sparse(Diagonal(localderivative(Gᵢ!, dx, tracers(x), j, p))) for j in 1:nt)
217-
end
218133

219134
#= ============================================
220135
Generate 𝑓 and derivatives from user input

test/bgc_functions.jl

-35
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,10 @@ function sms_POP(DIP, DOP, POP, p)
4949
end
5050
sms_all = (sms_DIP, sms_DOP, sms_POP) # bundles all the source-sink functions in a tuple
5151

52-
#===========================================
53-
In-place sources minus sinks
54-
===========================================#
55-
56-
function G_DIP!(dx, DIP, DOP, POP, p)
57-
@unpack τgeo, xgeo, τDIP, k, τDOP = p
58-
@. dx = (xgeo - DIP) / τgeo - (DIP 0) / τDIP * DIP^2 / (DIP + k) * (ztop == 0) + DOP / τDOP
59-
end
60-
function G_DOP!(dx, DIP, DOP, POP, p)
61-
@unpack τgeo, xgeo, τDIP, k, τDOP, τPOP, σ = p
62-
@. dx = σ * (DIP 0) / τDIP * DIP^2 / (DIP + k) * (ztop == 0) - DOP / τDOP + POP / τPOP
63-
end
64-
function G_POP!(dx, DIP, DOP, POP, p)
65-
@unpack τgeo, xgeo, τDIP, k, τDOP, τPOP, σ = p
66-
@. dx = (1 - σ) * (DIP 0) / τDIP * DIP^2 / (DIP + k) * (ztop == 0) - POP / τPOP
67-
end
68-
Gs = (G_DIP!, G_DOP!, G_POP!)
69-
70-
7152
#===========================================
7253
AIBECS F and ∇ₓF
7354
===========================================#
7455
fun = AIBECSFunction(T_all, sms_all, nb)
75-
fun! = AIBECSFunction((T_D, T_POP), Gs, nb, 3, [1, 1, 2])
76-
F, ∇ₓF = F_and_∇ₓF(T_all, sms_all, nb) # deprecated
77-
F!, ∇ₓF! = F_and_∇ₓF((T_D, T_POP), Gs, nb, 3, [1, 1, 2]) # deprecated
7856

7957
#===========================================
8058
AIBECS split operators for CNLF
@@ -126,19 +104,6 @@ Tests
126104
end
127105
end
128106

129-
130-
@testset "In-place functions" begin
131-
nt = length(T_all)
132-
n = nt * nb
133-
@unpack xgeo = p
134-
x = xgeo * ones(n) .* exp.(cos.(collect(1:n)) / 10)
135-
dx = similar(x)
136-
@testset "in-place F ≈ out-of-place F" begin
137-
@test fun(x, p) fun!(dx, x, p) rtol = 1e-10
138-
@test fun.jac(x, p) fun!.jac(x, p) rtol = 1e-10
139-
end
140-
end
141-
142107
@testset "Linear/nonlinear split functions" begin
143108
nt = length(T_all)
144109
n = nt * nb

test/parameters.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function prior(::Type{T}, s::Symbol) where {T<:AbstractParameters}
2727
σ = 10.0 # Assumes that a sensible unit is chosen (i.e., that within 10.0 * U)
2828
return Normal(μ, σ)
2929
else
30-
return LocationScale(lb, ub-lb, LogitNormal()) # <- The LogitNormal works well for Optim?
30+
return lb + (ub-lb) * LogitNormal() # <- The LogitNormal works well for Optim?
3131
end
3232
else
3333
return nothing

test/solvers.jl

-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ algs = [CTKAlg]
1616
@test s isa SteadyStateSolution
1717
@test oopprob isa SteadyStateProblem
1818
@test norm(s.u) / norm(fun.f(s.u, testp, 0)) > ustrip(upreferred(1e5u"Myr"))
19-
20-
iipprob = SteadyStateProblem(fun!, nx * x, testp)
21-
s = solve(iipprob, alg())
22-
@test s isa SteadyStateSolution
23-
@test iipprob isa SteadyStateProblem
24-
ds = copy(s.u)
25-
@test norm(s.u) / norm(fun!.f(ds, s.u, testp, 0)) > ustrip(upreferred(1e5u"Myr"))
2619
end
2720
end
2821
end

0 commit comments

Comments
 (0)