forked from uw-comphys/opencmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_integration_schemes.py
executable file
·341 lines (263 loc) · 13.2 KB
/
time_integration_schemes.py
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
"""
Copyright 2021 the authors (see AUTHORS file for full list)
This file is part of OpenCMP.
OpenCMP is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or
(at your option) any later version.
OpenCMP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with OpenCMP. If not, see <https://www.gnu.org/licenses/>.
"""
import ngsolve as ngs
from models import Model
from typing import List
from ngsolve.comp import ProxyFunction
from ngsolve import dx
from typing import Tuple
def explicit_euler(model: Model, gfu_0: List[ngs.GridFunction], U: List[ProxyFunction], V: List[ProxyFunction],
dt: List[ngs.Parameter]) -> Tuple[ngs.BilinearForm, ngs.LinearForm]:
"""
Explicit Euler time integration scheme.
Function to constructs the final bilinear and linear forms for the time integration scheme by adding the
necessary time-dependent terms to the model's stationary terms.
The returned bilinear and linear forms have NOT been assembled.
Args:
model: The model to solve.
gfu_0: List of the solutions of previous time steps ordered from most recent to oldest.
U: List of trial functions for the model.
V: List of test (weighting) functions.
dt: List of timestep sizes ordered from most recent to oldest.
Returns:
a: The final bilinear form (as a ngs.BilinearForm but not assembled).
L: The final linear form (as a ngs.LinearForm but not assembled).
"""
# Separate out the various components of each gridfunction solution to a previous timestep.
# gfu_lst = [[component 0, component 1...] at t^n, [component 0, component 1...] at t^n-1, ...]
gfu_lst = []
for i in range(len(gfu_0)):
if (len(gfu_0[i].components) > 0):
gfu_lst.append([gfu_0[i].components[j] for j in range(len(gfu_0[i].components))])
else:
gfu_lst.append([gfu_0[i]])
# Construct the bilinear form
a = ngs.BilinearForm(model.fes)
# Construct the linear form
L = ngs.LinearForm(model.fes)
L += model.construct_linear(V, gfu_lst[0], dt[0])
L += -1.0 * model.construct_bilinear(gfu_lst[0], V, dt[0], True)
a_dt, L_dt = model.time_derivative_terms(gfu_lst, 'explicit euler')
# When adding the time discretization term, multiply by the phase field if using the diffuse interface method.
if model.DIM:
a_dt *= model.DIM_solver.phi_gfu
L_dt *= model.DIM_solver.phi_gfu
a += a_dt * dx
L += L_dt * dx
return a, L
def implicit_euler(model: Model, gfu_0: List[ngs.GridFunction], U: List[ProxyFunction], V: List[ProxyFunction],
dt: List[ngs.Parameter]) -> Tuple[ngs.BilinearForm, ngs.LinearForm]:
"""
Implicit Euler time integration scheme.
This function constructs the final bilinear and linear forms for the time integration scheme by adding the
necessary time-dependent terms to the model's stationary terms.
The returned bilinear and linear forms have NOT been assembled.
Args:
model: The model to solve.
gfu_0: List of the solutions of previous time steps ordered from most recent to oldest.
U: List of trial functions for the model.
V: List of test (weighting) functions.
dt: List of timestep sizes ordered from most recent to oldest.
Returns:
a: The final bilinear form (as a ngs.BilinearForm but not assembled).
L: The final linear form (as a ngs.LinearForm but not assembled).
"""
# Separate out the various components of each gridfunction solution to a previous timestep.
# gfu_lst = [[component 0, component 1...] at t^n, [component 0, component 1...] at t^n-1, ...]
gfu_lst = []
for i in range(len(gfu_0)):
if (len(gfu_0[i].components) > 0):
gfu_lst.append([gfu_0[i].components[j] for j in range(len(gfu_0[i].components))])
else:
gfu_lst.append([gfu_0[i]])
# Construct the bilinear form
a = ngs.BilinearForm(model.fes)
a += model.construct_bilinear(U, V, dt[0])
# Construct the linear form
L = ngs.LinearForm(model.fes)
L += model.construct_linear(V, gfu_lst[0], dt[0])
a_dt, L_dt = model.time_derivative_terms(gfu_lst, 'implicit euler')
# When adding the time discretization term, multiply by the phase field if using the diffuse interface method.
if model.DIM:
a_dt *= model.DIM_solver.phi_gfu
L_dt *= model.DIM_solver.phi_gfu
a += a_dt * dx
L += L_dt * dx
return a, L
def crank_nicolson(model: Model, gfu_0: List[ngs.GridFunction], U: List[ProxyFunction], V: List[ProxyFunction],
dt: List[ngs.Parameter]) -> Tuple[ngs.BilinearForm, ngs.LinearForm]:
"""
Crank Nicolson (trapezoidal rule) time integration scheme.
This function constructs the final bilinear and linear forms for the time integration scheme by adding the
necessary time-dependent terms to the model's stationary terms.
The returned bilinear and linear forms have NOT been assembled.
Args:
model: The model to solve.
gfu_0: List of the solutions of previous time steps ordered from most recent to oldest.
U: List of trial functions for the model.
V: List of test (weighting) functions.
dt: List of timestep sizes ordered from most recent to oldest.
Returns:
a: The final bilinear form (as a ngs.BilinearForm but not assembled).
L: The final linear form (as a ngs.LinearForm but not assembled).
"""
# Separate out the various components of each gridfunction solution to a previous timestep.
# gfu_lst = [[component 0, component 1...] at t^n, [component 0, component 1...] at t^n-1, ...]
gfu_lst = []
for i in range(len(gfu_0)):
if (len(gfu_0[i].components) > 0):
gfu_lst.append([gfu_0[i].components[j] for j in range(len(gfu_0[i].components))])
else:
gfu_lst.append([gfu_0[i]])
# Construct the bilinear form
a = ngs.BilinearForm(model.fes)
a += 0.5 * model.construct_bilinear(U, V, dt[0])
# Construct the linear form
L = ngs.LinearForm(model.fes)
L += model.construct_linear(V, gfu_lst[0], dt[0])
L += -0.5 * model.construct_bilinear(gfu_lst[0], V, dt[0], True)
a_dt, L_dt = model.time_derivative_terms(gfu_lst, 'crank nicolson')
# When adding the time discretization term, multiply by the phase field if using the diffuse interface method.
if model.DIM:
a_dt *= model.DIM_solver.phi_gfu
L_dt *= model.DIM_solver.phi_gfu
a += a_dt * dx
L += L_dt * dx
return a, L
def CNLF(model: Model, gfu_0: List[ngs.GridFunction], U: List[ProxyFunction], V: List[ProxyFunction],
dt: List[ngs.Parameter]) -> Tuple[ngs.BilinearForm, ngs.LinearForm]:
"""
Crank Nicolson Leap Frog IMEX time integration scheme.
This function constructs the final bilinear and linear forms for the time integration scheme by adding the
necessary time-dependent terms to the model's stationary terms.
The returned bilinear and linear forms have NOT been assembled.
Args:
model: The model to solve.
gfu_0: List of the solutions of previous time steps ordered from most recent to oldest.
U: List of trial functions for the model.
V: List of test (weighting) functions.
dt: List of timestep sizes ordered from most recent to oldest.
Returns:
a: The final bilinear form (as a ngs.BilinearForm but not assembled).
L: The final linear form (as a ngs.LinearForm but not assembled).
"""
# Separate out the various components of each gridfunction solution to a previous timestep.
# gfu_lst = [[component 0, component 1...] at t^n, [component 0, component 1...] at t^n-1, ...]
gfu_lst = []
for i in range(len(gfu_0)):
if (len(gfu_0[i].components) > 0):
gfu_lst.append([gfu_0[i].components[j] for j in range(len(gfu_0[i].components))])
else:
gfu_lst.append([gfu_0[i]])
# Construct the bilinear form
a = ngs.BilinearForm(model.fes)
a += model.construct_bilinear(U, V, dt[0])
# Construct the linear form
L = ngs.LinearForm(model.fes)
L += 2.0 * model.construct_linear(V, gfu_lst[0], dt[0])
L += -1.0 * model.construct_bilinear(gfu_lst[1], V, dt[0], True)
a_dt, L_dt = model.time_derivative_terms(gfu_lst, 'CNLF')
# When adding the time discretization term, multiply by the phase field if using the diffuse interface method.
if model.DIM:
a_dt *= model.DIM_solver.phi_gfu
L_dt *= model.DIM_solver.phi_gfu
a += a_dt * dx
L += L_dt * dx
return a, L
def SBDF(model: Model, gfu_0: List[ngs.GridFunction], U: List[ProxyFunction], V: List[ProxyFunction],
dt: List[ngs.Parameter]) -> Tuple[ngs.BilinearForm, ngs.LinearForm]:
"""
Third order semi-implicit backwards differencing time integration scheme.
This function constructs the final bilinear and linear forms for the time integration scheme by adding the
necessary time-dependent terms to the model's stationary terms.
The returned bilinear and linear forms have NOT been assembled.
Args:
model: The model to solve.
gfu_0: List of the solutions of previous time steps ordered from most recent to oldest.
U: List of trial functions for the model.
V: List of test (weighting) functions.
dt: List of timestep sizes ordered from most recent to oldest.
Returns:
a: The final bilinear form (as a ngs.BilinearForm but not assembled).
L: The final linear form (as a ngs.LinearForm but not assembled).
"""
# Separate out the various components of each gridfunction solution to a previous timestep.
# gfu_lst = [[component 0, component 1...] at t^n, [component 0, component 1...] at t^n-1, ...]
gfu_lst = []
for i in range(len(gfu_0)):
if (len(gfu_0[i].components) > 0):
gfu_lst.append([gfu_0[i].components[j] for j in range(len(gfu_0[i].components))])
else:
gfu_lst.append([gfu_0[i]])
# Construct the bilinear form
a = ngs.BilinearForm(model.fes)
a += model.construct_bilinear(U, V, dt[0])
# Construct the linear form
L = ngs.LinearForm(model.fes)
L += 3.0 * model.construct_linear(V, gfu_lst[0], dt[0])
L += -3.0 * model.construct_linear(V, gfu_lst[1], dt[0])
L += model.construct_linear(V, gfu_lst[2], dt[0])
a_dt, L_dt = model.time_derivative_terms(gfu_lst, 'SBDF')
# When adding the time discretization term, multiply by the phase field if using the diffuse interface method.
if model.DIM:
a_dt *= model.DIM_solver.phi_gfu
L_dt *= model.DIM_solver.phi_gfu
a += a_dt * dx
L += L_dt * dx
return a, L
def adaptive_IMEX_pred(model: Model, gfu_0: List[ngs.GridFunction], U: List[ProxyFunction], V: List[ProxyFunction],
dt: List[ngs.Parameter]) -> Tuple[ngs.BilinearForm, ngs.LinearForm]:
"""
Predictor for the adaptive time-stepping IMEX time integration scheme.
This function constructs the final bilinear and linear forms for the time integration scheme by adding the
necessary time-dependent terms to the model's stationary terms.
The returned bilinear and linear forms have NOT been assembled.
Args:
model: The model to solve.
gfu_0: List of the solutions of previous time steps ordered from most recent to oldest.
U: List of trial functions for the model.
V: List of test (weighting) functions.
dt: List of timestep sizes ordered from most recent to oldest.
Returns:
a: The final bilinear form (as a ngs.BilinearForm but not assembled).
L: The final linear form (as a ngs.LinearForm but not assembled).
"""
# Separate out the various components of each gridfunction solution to a previous timestep.
# gfu_lst = [[component 0, component 1...] at t^n, [component 0, component 1...] at t^n-1, ...]
gfu_lst = []
for i in range(len(gfu_0)):
if (len(gfu_0[i].components) > 0):
gfu_lst.append([gfu_0[i].components[j] for j in range(len(gfu_0[i].components))])
else:
gfu_lst.append([gfu_0[i]])
# Operators specific to this time integration scheme.
w = dt[0] / dt[1]
E = model.construct_gfu().components[0]
E_expr = (1.0 + w) * gfu_lst[0][0] - w * gfu_lst[1][0]
E.Set(E_expr)
# Construct the bilinear form
a = ngs.BilinearForm(model.fes)
a += model.construct_bilinear(U, V, dt[0])
# Construct the linear form
L = ngs.LinearForm(model.fes)
L += model.construct_linear(V, [gfu_lst[0][0], 0.0], dt[0]) # TODO: Why doesn't using E work? Numerical error?
a_dt, L_dt = model.time_derivative_terms(gfu_lst, 'crank nicolson')
# When adding the time discretization term, multiply by the phase field if using the diffuse interface method.
if model.DIM:
a_dt *= model.DIM_solver.phi_gfu
L_dt *= model.DIM_solver.phi_gfu
a += a_dt * dx
L += L_dt * dx
return a, L