-
Notifications
You must be signed in to change notification settings - Fork 98
/
ForwardEuler.jl
164 lines (134 loc) · 3.32 KB
/
ForwardEuler.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
"""
struct ForwardEuler <: ODESolver end
Forward Euler ODE solver.
```
residual(tx, ux, vx) = 0,
tx = t_n
ux = u_n
vx = x,
u_(n+1) = u_n + dt * x.
```
"""
struct ForwardEuler <: ODESolver
sysslvr::NonlinearSolver
dt::Real
end
##################
# Nonlinear case #
##################
function allocate_odecache(
odeslvr::ForwardEuler, odeop::ODEOperator,
t0::Real, us0::NTuple{1,AbstractVector}
)
u0 = us0[1]
us0N = (u0, u0)
odeopcache = allocate_odeopcache(odeop, t0, us0N)
sysslvrcache = nothing
odeslvrcache = (sysslvrcache,)
(odeslvrcache, odeopcache)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::ForwardEuler, odeop::ODEOperator,
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
# Unpack inputs
u0 = state0[1]
odeslvrcache, odeopcache = odecache
sysslvrcache, = odeslvrcache
# Unpack solver
sysslvr = odeslvr.sysslvr
dt = odeslvr.dt
# Define scheme
x = stateF[1]
tx = t0
usx(x) = (u0, x)
ws = (0, 1)
# Update ODE operator cache
update_odeopcache!(odeopcache, odeop, tx)
# Create and solve stage operator
stageop = NonlinearStageOperator(
odeop, odeopcache,
tx, usx, ws
)
sysslvrcache = solve!(x, sysslvr, stageop, sysslvrcache)
# Update state
tF = t0 + dt
stateF = _update_euler!(stateF, state0, dt, x)
# Pack outputs
odeslvrcache = (sysslvrcache,)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
###############
# Linear case #
###############
function allocate_odecache(
odeslvr::ForwardEuler, odeop::ODEOperator{<:AbstractQuasilinearODE},
t0::Real, us0::NTuple{1,AbstractVector}
)
u0 = us0[1]
us0N = (u0, u0)
odeopcache = allocate_odeopcache(odeop, t0, us0N)
is_semilinear = (ODEOperatorType(odeop) <: AbstractSemilinearODE)
constant_mass = is_form_constant(odeop, 1)
reuse = (is_semilinear && constant_mass)
J = allocate_jacobian(odeop, t0, us0N, odeopcache)
r = allocate_residual(odeop, t0, us0N, odeopcache)
sysslvrcache = nothing
odeslvrcache = (reuse, J, r, sysslvrcache)
(odeslvrcache, odeopcache)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::ForwardEuler, odeop::ODEOperator{<:AbstractQuasilinearODE},
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
# Unpack inputs
u0 = state0[1]
odeslvrcache, odeopcache = odecache
reuse, J, r, sysslvrcache = odeslvrcache
# Unpack solver
sysslvr = odeslvr.sysslvr
dt = odeslvr.dt
# Define scheme
# Set x to zero to split jacobian and residual
x = stateF[1]
fill!(x, zero(eltype(x)))
tx = t0
usx = (u0, x)
ws = (0, 1)
# Update ODE operator cache
update_odeopcache!(odeopcache, odeop, tx)
# Create and solve stage operator
stageop = LinearStageOperator(
odeop, odeopcache,
tx, usx, ws,
J, r, reuse, sysslvrcache
)
sysslvrcache = solve!(x, sysslvr, stageop, sysslvrcache)
# Update state
tF = t0 + dt
stateF = _update_euler!(stateF, state0, dt, x)
# Pack outputs
odeslvrcache = (reuse, J, r, sysslvrcache)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
#########
# Utils #
#########
function _update_euler!(
stateF::NTuple{1,AbstractVector}, state0::NTuple{1,AbstractVector},
dt::Real, x::AbstractVector
)
# uF = u0 + dt * x
# We always have x === uF
u0 = state0[1]
uF = stateF[1]
rmul!(uF, dt)
axpy!(1, u0, uF)
(uF,)
end