-
Notifications
You must be signed in to change notification settings - Fork 97
/
AffineThetaMethod.jl
149 lines (122 loc) · 4.05 KB
/
AffineThetaMethod.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
function solve_step!(uf::AbstractVector,
solver::ThetaMethod,
op::AffineODEOperator,
u0::AbstractVector,
t0::Real,
cache) # -> (uF,tF)
dt = solver.dt
solver.θ == 0.0 ? dtθ = dt : dtθ = dt*solver.θ
tθ = t0+dtθ
if cache === nothing
ode_cache = allocate_cache(op)
vθ = similar(u0)
vθ .= 0.0
l_cache = nothing
A, b = _allocate_matrix_and_vector(op,u0,ode_cache)
else
ode_cache, vθ, A, b, l_cache = cache
end
ode_cache = update_cache!(ode_cache,op,tθ)
_matrix_and_vector!(A,b,op,tθ,dtθ,u0,ode_cache,vθ)
afop = AffineOperator(A,b)
newmatrix = true
l_cache = solve!(uf,solver.nls,afop,l_cache,newmatrix)
uf = uf + u0
if 0.0 < solver.θ < 1.0
uf = uf*(1.0/solver.θ)-u0*((1-solver.θ)/solver.θ)
end
cache = (ode_cache, vθ, A, b, l_cache)
tf = t0+dt
return (uf,tf,cache)
end
function solve_step!(uf::AbstractVector,
solver::ThetaMethod,
op::ConstantODEOperator,
u0::AbstractVector,
t0::Real,
cache) # -> (uF,tF)
dt = solver.dt
solver.θ == 0.0 ? dtθ = dt : dtθ = dt*solver.θ
tθ = t0+dtθ
if cache === nothing
ode_cache = allocate_cache(op)
vθ = similar(u0)
vθ .= 0.0
A, b = _allocate_matrix_and_vector(op,u0,ode_cache)
A = _matrix!(A,op,tθ,dtθ,u0,ode_cache,vθ)
b = _vector!(b,op,tθ,dtθ,vθ,ode_cache,vθ)
M = _allocate_matrix(op,u0,ode_cache)
M = _mass_matrix!(M,op,tθ,dtθ,u0,ode_cache,vθ)
_u0 = similar(u0,(axes(M)[2],)) # Needed for the distributed case
copy!(_u0,u0)
l_cache = nothing
newmatrix = true
else
ode_cache, _u0, vθ, A, b, M, l_cache = cache
newmatrix = false
copy!(_u0,u0)
end
ode_cache = update_cache!(ode_cache,op,tθ)
vθ = b + M*_u0
afop = AffineOperator(A,vθ)
l_cache = solve!(uf,solver.nls,afop,l_cache,newmatrix)
if 0.0 < solver.θ < 1.0
@. uf = uf * (1.0/solver.θ) - u0 * ((1-solver.θ)/solver.θ)
end
cache = (ode_cache, _u0, vθ, A, b, M, l_cache)
tf = t0+dt
return (uf,tf,cache)
end
"""
Affine operator that represents the θ-method affine operator at a
given time step, i.e., M(t)(u_n+θ-u_n)/dt + K(t)u_n+θ + b(t)
"""
function ThetaMethodAffineOperator(odeop::AffineODEOperator,tθ::Float64,dtθ::Float64,
u0::AbstractVector,ode_cache,vθ::AbstractVector)
# vθ .= 0.0
A, b = _allocate_matrix_and_vector(odeop,u0,ode_cache)
_matrix_and_vector!(A,b,odeop,tθ,dtθ,u0,ode_cache,vθ)
afop = AffineOperator(A,b)
end
function _matrix_and_vector!(A,b,odeop,tθ,dtθ,u0,ode_cache,vθ)
_matrix!(A,odeop,tθ,dtθ,u0,ode_cache,vθ)
_vector!(b,odeop,tθ,dtθ,u0,ode_cache,vθ)
end
function _matrix!(A,odeop,tθ,dtθ,u0,ode_cache,vθ)
z = zero(eltype(A))
fillstored!(A,z)
jacobians!(A,odeop,tθ,(vθ,vθ),(1.0,1/dtθ),ode_cache)
end
function _mass_matrix!(A,odeop,tθ,dtθ,u0,ode_cache,vθ)
z = zero(eltype(A))
fillstored!(A,z)
jacobian!(A,odeop,tθ,(vθ,vθ),2,(1/dtθ),ode_cache)
end
function _vector!(b,odeop,tθ,dtθ,u0,ode_cache,vθ)
residual!(b,odeop,tθ,(u0,vθ),ode_cache)
b .*= -1.0
end
function _allocate_matrix(odeop,u0,ode_cache)
A = allocate_jacobian(odeop,u0,ode_cache)
return A
end
function _allocate_matrix_and_vector(odeop,u0,ode_cache)
b = allocate_residual(odeop,u0,ode_cache)
A = allocate_jacobian(odeop,u0,ode_cache)
return A, b
end
"""
Affine operator that represents the θ-method affine operator at a
given time step, i.e., M(t)(u_n+θ-u_n)/dt + K(t)u_n+θ + b(t)
"""
function ThetaMethodConstantOperator(odeop::ConstantODEOperator,tθ::Float64,dtθ::Float64,
u0::AbstractVector,ode_cache,vθ::AbstractVector)
b = allocate_residual(odeop,u0,ode_cache)
A = allocate_jacobian(odeop,u0,ode_cache)
residual!(b,odeop,tθ,(u0,vθ),ode_cache)
@. b = -1.0 * b
z = zero(eltype(A))
fillstored!(A,z)
jacobians!(A,odeop,tθ,(vθ,vθ),(1.0,1/dtθ),ode_cache)
return A, b
end