-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlin_swe_2d.py
More file actions
executable file
·135 lines (105 loc) · 3.46 KB
/
lin_swe_2d.py
File metadata and controls
executable file
·135 lines (105 loc) · 3.46 KB
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
from firedrake import *
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.animation import FuncAnimation, FFMpegWriter
# Set up mesh
nx = 50
ny = nx
mesh = PeriodicSquareMesh(nx,ny,1.0, quadrilateral=True)
# Define function spaces
V = FunctionSpace(mesh, "RTCF", 2)
Q = FunctionSpace(mesh, "DQ", 1)
W = V*Q
# Set up trial and test functions
u, p = TrialFunctions(W)
v, q = TestFunctions(W)
# Solution function
w = Function(W)
# Initial condition
# Gaussian displacement and zero velocity
x,y = SpatialCoordinate(mesh)
u_val = Constant(0.0)
u_expr = as_vector([u_val,u_val])
p_expr = 10*exp(-100*((x-1/3)**2 + (y-1/3)**2))
u0 = Function(V).project(u_expr)
p0 = Function(Q).project(p_expr)
# Timestepping
T = 1.0
dt = T/100.0
dtc = Constant(dt)
theta = 0.5
thetac = Constant(theta)
# Constants
c = 1.0
c_const = Constant(c)
# Store displacement fields for plotting later
ps = [p0]
# Set up system
swe_eqn = (inner(v,u) - inner(v,u0) - (1 - theta)*dtc*p*div(v) - theta*dtc*p0*div(v)
+ p*q - p0*q + (1 - theta)*c_const**2*dtc*q*div(u) + theta*c_const**2*dtc*q*div(u0))*dx
a = lhs(swe_eqn)
L = rhs(swe_eqn)
swe_problem = LinearVariationalProblem(a, L, w, constant_jacobian=False)
solver_parameters = {'ksp_type': 'gmres',
'ksp_rtol': 1.0e-7,
'ksp_max_it': 1500,
'pc_type': 'fieldsplit',
'pc_fieldsplit': {'type': 'schur',
'schur_fact_type': 'full',
'schur_precondition': 'selfp'},
'fieldsplit_0': {'ksp_type': 'preonly',
'pc_type': 'bjacobi',
'sub_pc_type': 'ilu'},
'fieldsplit_1': {'ksp_type': 'preonly',
'pc_type': 'gamg',
'mg_levels': {'ksp_type': 'chebyshev',
'ksp_max_it': 5,
'pc_type': 'bjacobi',
'sub_pc_type': 'ilu'}}}
swe_solver_gmres = LinearVariationalSolver(swe_problem,
solver_parameters=solver_parameters)
# Solve problem
w.assign(0.0)
t = 0.0
step = 0
output_freq = 5
while t < T:
swe_solver_gmres.solve()
u,p = w.split()
u0.assign(u)
p0.assign(p)
step += 1
t += dt
if step % output_freq == 0:
ps.append(p.copy(deepcopy=True))
print('t = {:.4f}'.format(t))
## Animation ##
# Setup phase
fig = plt.figure()
z_min = 0.0
z_max = 0.0
for p in ps:
new_max = max(p.vector())
new_min = min(p.vector())
if new_max > z_max:
z_max = new_max
if new_min < z_min:
z_min = new_min
ax = fig.gca(zlim=(z_min, z_max), projection='3d')
ax.elev = 30.
ax.azim = -85.
# Setup plot with eta_0
surf = trisurf(ps[0], cmap=cm.coolwarm, linewidth=0, antialiased=False, axes=ax)
# Animation function
def animate(i, p, plot):
ax.clear()
plot = trisurf(p[i], cmap=cm.coolwarm, linewidth=0, antialiased=False, axes=ax)
ax.set_zlim(z_min, z_max)
ax.elev = 30.
ax.azim = -85.
return plot,
# Call the animator
interval = 1e4*output_freq*dt
anim = FuncAnimation(fig, animate, fargs = (ps, surf), frames=len(ps), interval=interval)
writer=FFMpegWriter(bitrate=5000, fps=6)
anim.save('lin_swe_2d.mp4', dpi=300, writer = writer)