-
-
Notifications
You must be signed in to change notification settings - Fork 196
/
dae_solve.jl
191 lines (165 loc) · 6.23 KB
/
dae_solve.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""
NNDAE(chain,
OptimizationOptimisers.Adam(0.1),
init_params = nothing;
autodiff = false,
kwargs...)
Algorithm for solving differential algebraic equationsusing a neural network. This is a specialization
of the physics-informed neural network which is used as a solver for a standard `DAEProblem`.
!!! warn
Note that NNDAE only supports DAEs which are written in the out-of-place form, i.e.
`du = f(du,u,p,t)`, and not `f(out,du,u,p,t)`. If not declared out-of-place, then the NNDAE
will exit with an error.
## Positional Arguments
* `chain`: A neural network architecture, defined as either a `Flux.Chain` or a `Lux.AbstractExplicitLayer`.
* `opt`: The optimizer to train the neural network.
* `init_params`: The initial parameter of the neural network. By default, this is `nothing`
which thus uses the random initialization provided by the neural network library.
## Keyword Arguments
* `autodiff`: The switch between automatic(not supported yet) and numerical differentiation for
the PDE operators. The reverse mode of the loss function is always
automatic differentiation (via Zygote), this is only for the derivative
in the loss function (the derivative with respect to time).
* `strategy`: The training strategy used to choose the points for the evaluations.
By default, `GridTraining` is used with `dt` if given.
"""
struct NNDAE{C, O, P, K, S <: Union{Nothing, AbstractTrainingStrategy}
} <: SciMLBase.AbstractDAEAlgorithm
chain::C
opt::O
init_params::P
autodiff::Bool
strategy::S
kwargs::K
end
function NNDAE(chain, opt, init_params = nothing; strategy = nothing, autodiff = false,
kwargs...)
!(chain isa Lux.AbstractExplicitLayer) &&
(chain = adapt(FromFluxAdaptor(false, false), chain))
NNDAE(chain, opt, init_params, autodiff, strategy, kwargs)
end
function dfdx(phi::ODEPhi, t::AbstractVector, θ, autodiff::Bool,
differential_vars::AbstractVector)
if autodiff
autodiff && throw(ArgumentError("autodiff not supported for DAE problem."))
else
dphi = (phi(t .+ sqrt(eps(eltype(t))), θ) - phi(t, θ)) ./ sqrt(eps(eltype(t)))
batch_size = size(t)[1]
reduce(vcat,
[dv ? dphi[[i], :] : zeros(1, batch_size)
for (i, dv) in enumerate(differential_vars)])
end
end
function inner_loss(phi::ODEPhi{C, T, U}, f, autodiff::Bool, t::AbstractVector, θ,
p, differential_vars::AbstractVector) where {C, T, U}
out = Array(phi(t, θ))
dphi = Array(dfdx(phi, t, θ, autodiff, differential_vars))
arrt = Array(t)
loss = reduce(hcat, [f(dphi[:, i], out[:, i], p, arrt[i]) for i in 1:size(out, 2)])
sum(abs2, loss) / length(t)
end
function generate_loss(strategy::GridTraining, phi, f, autodiff::Bool, tspan, p,
differential_vars::AbstractVector)
ts = tspan[1]:(strategy.dx):tspan[2]
autodiff && throw(ArgumentError("autodiff not supported for GridTraining."))
function loss(θ, _)
sum(abs2, inner_loss(phi, f, autodiff, ts, θ, p, differential_vars))
end
return loss
end
function SciMLBase.__solve(prob::SciMLBase.AbstractDAEProblem,
alg::NNDAE,
args...;
dt = nothing,
# timeseries_errors = true,
save_everystep = true,
# adaptive = false,
abstol = 1.0f-6,
reltol = 1.0f-3,
verbose = false,
saveat = nothing,
maxiters = nothing,
tstops = nothing)
u0 = prob.u0
du0 = prob.du0
tspan = prob.tspan
f = prob.f
p = prob.p
t0 = tspan[1]
#hidden layer
chain = alg.chain
opt = alg.opt
autodiff = alg.autodiff
#train points generation
init_params = alg.init_params
# A logical array which declares which variables are the differential (non-algebraic) vars
differential_vars = prob.differential_vars
if chain isa Lux.AbstractExplicitLayer || chain isa Flux.Chain
phi, init_params = generate_phi_θ(chain, t0, u0, init_params)
init_params = ComponentArrays.ComponentArray(;
depvar = ComponentArrays.ComponentArray(init_params))
else
error("Only Lux.AbstractExplicitLayer and Flux.Chain neural networks are supported")
end
if isinplace(prob)
throw(error("The NNODE solver only supports out-of-place DAE definitions, i.e. du=f(u,p,t)."))
end
try
phi(t0, init_params)
catch err
if isa(err, DimensionMismatch)
throw(DimensionMismatch("Dimensions of the initial u0 and chain should match"))
else
throw(err)
end
end
strategy = if alg.strategy === nothing
if dt !== nothing
GridTraining(dt)
else
error("dt is not defined")
end
end
inner_f = generate_loss(strategy, phi, f, autodiff, tspan, p, differential_vars)
# Creates OptimizationFunction Object from total_loss
total_loss(θ, _) = inner_f(θ, phi)
# Optimization Algo for Training Strategies
opt_algo = Optimization.AutoZygote()
# Creates OptimizationFunction Object from total_loss
optf = OptimizationFunction(total_loss, opt_algo)
iteration = 0
callback = function (p, l)
iteration += 1
verbose && println("Current loss is: $l, Iteration: $iteration")
l < abstol
end
optprob = OptimizationProblem(optf, init_params)
res = solve(optprob, opt; callback, maxiters, alg.kwargs...)
#solutions at timepoints
if saveat isa Number
ts = tspan[1]:saveat:tspan[2]
elseif saveat isa AbstractArray
ts = saveat
elseif dt !== nothing
ts = tspan[1]:dt:tspan[2]
elseif save_everystep
ts = range(tspan[1], tspan[2], length = 100)
else
ts = [tspan[1], tspan[2]]
end
if u0 isa Number
u = [first(phi(t, res.u)) for t in ts]
else
u = [phi(t, res.u) for t in ts]
end
sol = SciMLBase.build_solution(prob, alg, ts, u;
k = res, dense = true,
calculate_error = false,
retcode = ReturnCode.Success,
original = res,
resid = res.objective)
SciMLBase.has_analytic(prob.f) &&
SciMLBase.calculate_solution_errors!(sol; timeseries_errors = true,
dense_errors = false)
sol
end