-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmarker.py
225 lines (177 loc) · 6.43 KB
/
benchmarker.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
# Benchmark computational performance of higher-order mass-lumped FEM
import firedrake as fd
from firedrake import Constant, dx, dot, grad, COMM_WORLD
from firedrake.petsc import PETSc
import finat
from mpi4py import MPI
import numpy as np
import sys
from helpers import RickerWavelet, delta_expr, gauss_lobatto_legendre_cube_rule
import os
__all__ = ["solver_CG"]
PETSc.Log().begin()
if COMM_WORLD.rank == 0:
if not os.path.exists("data"):
os.makedirs("data")
elif not os.path.isdir("data"):
raise RuntimeError("Cannot create output directory, file of given name exists")
COMM_WORLD.barrier()
def _get_time(event, comm=COMM_WORLD):
return (
comm.allreduce(PETSc.Log.Event(event).getPerfInfo()["time"], op=MPI.SUM)
/ comm.size
)
def _build_space(mesh, el, space, deg):
if el == "tria":
V = fd.FunctionSpace(mesh, space, deg)
elif el == "quad":
if space == "spectral":
element = fd.FiniteElement(
"CG", mesh.ufl_cell(), degree=deg, variant="spectral"
)
V = fd.FunctionSpace(mesh, element)
elif space == "S":
V = fd.FunctionSpace(mesh, "S", deg)
else:
raise ValueError("Space not supported yet")
return V
def _build_quad_rule(el, V, space):
if el == "tria" and space == "KMV":
quad_rule_mass_matrix = finat.quadrature.make_quadrature(
V.finat_element.cell,
V.ufl_element().degree(),
space,
)
quad_rule_general = None
elif el == "quad" and space == "spectral":
quad_rule_mass_matrix = gauss_lobatto_legendre_cube_rule(
V.mesh().geometric_dimension(), V.ufl_element().degree()
)
quad_rule_general = quad_rule_mass_matrix
elif el == "quad" and space == "S":
quad_rule_mass_matrix = None
quad_rule_general = None
elif el == "tria" and space == "CG":
quad_rule_mass_matrix = None
quad_rule_general = None
else:
raise ValueError("Unsupported element/space combination")
return (quad_rule_mass_matrix, quad_rule_general)
def _select_params(space):
if space == "KMV":
params = {"ksp_type": "preonly", "pc_type": "jacobi"}
else:
params = {"ksp_type": "cg", "pc_type": "jacobi"}
return params
def solver_CG(mesh, el, space, deg, T, dt=0.001, warm_up=False):
"""Solve the scalar wave equation on a unit square/cube using a
CG FEM formulation with several different element types.
Parameters
----------
mesh: Firedrake.mesh
A utility mesh from the Firedrake package
el: string
The type of element either "tria" or "quad".
`tria` in 3d implies tetrahedra and
`quad` in 3d implies hexahedral elements.
space: string
The space of the FEM. Available options are:
"CG": Continuous Galerkin Finite Elements,
"KMV": Kong-Mulder-Veldhuzien higher-order mass lumped elements
"S" (for Serendipity) (NB: quad/hexs only)
"spectral": spectral elements using GLL quad
points (NB: quads/hexs only)
deg: int
The spatial polynomial degree.
T: float
The simulation duration in simulation seconds.
dt: float, optional
Simulation timestep
warm_up: boolean, optional
Warm up symbolics by running one timestep and shutting down.
Returns
-------
u_n: Firedrake.Function
The solution at time `T`
"""
sd = mesh.geometric_dimension()
V = _build_space(mesh, el, space, deg)
quad_rule1, quad_rule2 = _build_quad_rule(el, V, space)
params = _select_params(space)
# DEBUG
# outfile = fd.File(os.getcwd() + "/results/simple_shots.pvd")
# END DEBUG
tot_dof = COMM_WORLD.allreduce(V.dof_dset.total_size, op=MPI.SUM)
# if COMM_WORLD.rank == 0:
# print("------------------------------------------")
# print("The problem has " + str(tot_dof) + " degrees of freedom.")
# print("------------------------------------------")
nt = int(T / dt) # number of timesteps
u = fd.TrialFunction(V)
v = fd.TestFunction(V)
u_np1 = fd.Function(V) # n+1
u_n = fd.Function(V) # n
u_nm1 = fd.Function(V) # n-1
# constant speed
c = Constant(1.5)
m = (
(1.0 / (c * c))
* (u - 2.0 * u_n + u_nm1)
/ Constant(dt * dt)
* v
* dx(rule=quad_rule1)
) # mass-like matrix
a = dot(grad(u_n), grad(v)) * dx(rule=quad_rule2) # stiffness matrix
# injection of source into mesh
ricker = Constant(0.0)
source = Constant([0.5] * sd)
coords = fd.SpatialCoordinate(mesh)
F = m + a - delta_expr(source, *coords) * ricker * v * dx(rule=quad_rule2)
a, r = fd.lhs(F), fd.rhs(F)
A, R = fd.assemble(a), fd.assemble(r)
solver = fd.LinearSolver(A, solver_parameters=params, options_prefix="")
# timestepping loop
results = []
t = 0.0
for step in range(nt):
with PETSc.Log.Stage("{el}{deg}".format(el=el, deg=deg)):
ricker.assign(RickerWavelet(t, freq=6))
R = fd.assemble(r, tensor=R)
solver.solve(u_np1, R)
snes = _get_time("SNESSolve")
ksp = _get_time("KSPSolve")
pcsetup = _get_time("PCSetUp")
pcapply = _get_time("PCApply")
jac = _get_time("SNESJacobianEval")
residual = _get_time("SNESFunctionEval")
sparsity = _get_time("CreateSparsity")
results.append(
[tot_dof, snes, ksp, pcsetup, pcapply, jac, residual, sparsity]
)
if warm_up:
# Warm up symbolics/disk cache
solver.solve(u_np1, R)
sys.exit("Warming up...")
u_nm1.assign(u_n)
u_n.assign(u_np1)
t = step * float(dt)
# if step % 10 == 0:
# outfile.write(u_n)
# print("Time is " + str(t), flush=True)
results = np.asarray(results)
if mesh.comm.rank == 0:
with open(
"data/scalar_wave.{el}.{deg}.{space}.csv".format(
el=el, deg=deg, space=space
),
"w",
) as f:
np.savetxt(
f,
results,
fmt=["%d"] + ["%e"] * 7,
delimiter=",",
header="tot_dof,SNESSolve,KSPSolve,PCSetUp,PCApply,SNESJacobianEval,SNESFunctionEval,CreateSparsity",
comments="",
)
return u_n