-
Notifications
You must be signed in to change notification settings - Fork 98
/
ThetaMethod.jl
191 lines (159 loc) · 3.94 KB
/
ThetaMethod.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
"""
struct ThetaMethod <: ODESolver end
θ-method ODE solver.
```
residual(tx, ux, vx) = 0,
tx = t_n + θ * dt
ux = u_n + θ * dt * x
vx = x,
u_(n+1) = u_n + dt * x.
```
"""
struct ThetaMethod <: ODESolver
sysslvr::NonlinearSolver
dt::Real
θ::Real
function ThetaMethod(sysslvr, dt, θ)
θ01 = clamp(θ, 0, 1)
if θ01 != θ
msg = """
The parameter θ of the θ-method must lie between zero and one.
Setting θ to $(θ01).
"""
@warn msg
end
if iszero(θ01)
ForwardEuler(sysslvr, dt)
else
new(sysslvr, dt, θ01)
end
end
end
MidPoint(sysslvr, dt) = ThetaMethod(sysslvr, dt, 0.5)
BackwardEuler(sysslvr, dt) = ThetaMethod(sysslvr, dt, 1)
##################
# Nonlinear case #
##################
function allocate_odecache(
odeslvr::ThetaMethod, odeop::ODEOperator,
t0::Real, us0::NTuple{1,AbstractVector}
)
u0 = us0[1]
us0N = (u0, u0)
odeopcache = allocate_odeopcache(odeop, t0, us0N)
uθ = copy(u0)
sysslvrcache = nothing
odeslvrcache = (uθ, sysslvrcache)
(odeslvrcache, odeopcache)
end
function ode_march!(
stateF::NTuple{1,AbstractVector},
odeslvr::ThetaMethod, odeop::ODEOperator,
t0::Real, state0::NTuple{1,AbstractVector},
odecache
)
# Unpack inputs
u0 = state0[1]
odeslvrcache, odeopcache = odecache
uθ, sysslvrcache = odeslvrcache
# Unpack solver
sysslvr = odeslvr.sysslvr
dt, θ = odeslvr.dt, odeslvr.θ
# Define scheme
x = stateF[1]
dtθ = θ * dt
tx = t0 + dtθ
function usx(x)
copy!(uθ, u0)
axpy!(dtθ, x, uθ)
(uθ, x)
end
ws = (dtθ, 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 = _udate_theta!(stateF, state0, dt, x)
# Pack outputs
odeslvrcache = (uθ, sysslvrcache)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
###############
# Linear case #
###############
function allocate_odecache(
odeslvr::ThetaMethod, odeop::ODEOperator{<:AbstractLinearODE},
t0::Real, us0::NTuple{1,AbstractVector}
)
u0 = us0[1]
us0N = (u0, u0)
odeopcache = allocate_odeopcache(odeop, t0, us0N)
constant_stiffness = is_form_constant(odeop, 0)
constant_mass = is_form_constant(odeop, 1)
reuse = (constant_stiffness && 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::ThetaMethod, odeop::ODEOperator{<:AbstractLinearODE},
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, odeslvr.θ
# Define scheme
# Set x to zero to split jacobian and residual
x = stateF[1]
fill!(x, zero(eltype(x)))
dtθ = θ * dt
tx = t0 + dtθ
usx = (u0, x)
ws = (dtθ, 1)
# Update ODE operator cache
update_odeopcache!(odeopcache, odeop, tx)
# Solve the discrete ODE operator
stageop = LinearStageOperator(
odeop, odeopcache,
tx, usx, ws,
J, r, reuse, sysslvrcache
)
sysslvrcache = solve!(x, sysslvr, stageop, sysslvrcache)
# Update state
tF = t0 + dt
stateF = _udate_theta!(stateF, state0, dt, x)
# Pack outputs
odeslvrcache = (reuse, J, r, sysslvrcache)
odecache = (odeslvrcache, odeopcache)
(tF, stateF, odecache)
end
#########
# Utils #
#########
function _udate_theta!(
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)
stateF = (uF,)
end