-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcavity_with_sc_bounday.py
374 lines (295 loc) · 10.7 KB
/
cavity_with_sc_bounday.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Solve Maxwell eigenvalue problem for a cavity with superconducting boundary conditions.
import os
import numpy as np
from mpi4py import MPI
from petsc4py import PETSc
import dolfinx
import argparse
assert dolfinx.has_petsc
if PETSc.IntType == np.int64 and MPI.COMM_WORLD.size > 1:
print(
"This solver fails with PETSc and 64-bit integers becaude of memory errors in MUMPS."
)
# Note: when PETSc.IntType == np.int32, superlu_dist is used
# rather than MUMPS and does not trigger memory failures.
exit(0)
real_type = PETSc.RealType
scalar_type = PETSc.ScalarType
import ufl
from basix.ufl import element
from dolfinx import fem, io, plot
from dolfinx.io import gmshio
from dolfinx.fem.petsc import assemble_matrix
from dolfinx.mesh import (
CellType,
create_rectangle,
exterior_facet_indices,
locate_entities,
)
from slepc4py import SLEPc
try:
import pyvista
have_pyvista = True
except ModuleNotFoundError:
print("pyvista and pyvistaqt are required to visualise the solution")
have_pyvista = False
def solve(
mesh,
inv_lambda_sqr: fem.Function,
fem_degree=2,
gui=False,
eps_target=15.0,
num_eigs=13,
tol=1e-10,
output_path="results",
):
print(f"Assembling matrices...")
N1curl = element("N1curl", mesh.basix_cell(), fem_degree, dtype=real_type)
V = fem.functionspace(mesh, N1curl)
b_func = ufl.TrialFunction(V)
w_func = ufl.TestFunction(V)
a_weak = (
ufl.inner(ufl.curl(b_func), ufl.curl(w_func))
+ inv_lambda_sqr * ufl.inner(b_func, w_func)
) * ufl.dx
b_weak = (ufl.inner(b_func, w_func)) * ufl.dx
a = fem.form(a_weak)
b = fem.form(b_weak)
# Direchlet boundary conditions
bc_facets = exterior_facet_indices(mesh.topology)
bc_dofs = fem.locate_dofs_topological(V, mesh.topology.dim - 1, bc_facets)
u_bc = fem.Function(V)
with u_bc.x.petsc_vec.localForm() as loc:
loc.set(0)
bc = fem.dirichletbc(u_bc, bc_dofs)
# Assemble matrices for eigenvalue problem
# Ax = lambda B x
A = assemble_matrix(a, bcs=[bc])
A.assemble()
B = assemble_matrix(b, bcs=[bc], diagonal=0.0)
B.assemble()
# Create SLEPc Eigenvalue solver
# TODO: Check parameters for SLEPc of Palace:
# Configuring SLEPc eigenvalue solver:
# Scaling γ = 3.036e+04, δ = 5.892e-05
# Configuring divergence-free projection
# Using random starting vector
# Shift-and-invert σ = 5.000e+00 GHz (3.144e-01)
print(f"Solving eigenvalues...")
eps = SLEPc.EPS().create(mesh.comm)
eps.setOperators(A, B)
eps.setType(SLEPc.EPS.Type.KRYLOVSCHUR)
eps.setProblemType(SLEPc.EPS.ProblemType.GHEP)
eps.setWhichEigenpairs(eps.Which.TARGET_MAGNITUDE)
eps.setTarget(eps_target)
# Set shift-and-invert transformation
st = eps.getST()
st.setType(SLEPc.ST.Type.SINVERT)
st.setShift(eps_target)
def monitor(eps, its, nconv, eig, err):
print(f"Iteration: {its}, Error: {err[nconv]}")
eps.setMonitor(monitor)
eps.setDimensions(num_eigs, PETSc.DECIDE, PETSc.DECIDE)
eps.setFromOptions()
eps.setTolerances(tol=tol)
eps.solve()
eps.view()
eps.errorView()
its = eps.getIterationNumber()
eps_type = eps.getType()
n_ev, n_cv, mpd = eps.getDimensions()
tol, max_it = eps.getTolerances()
n_conv = eps.getConverged()
print("##################################")
print(f"Number of iterations: {its}")
print(f"Solution method: {eps_type}")
print(f"Number of requested eigenvalues: {n_ev}")
print(f"Stopping condition: tol={tol}, maxit={max_it}")
print(f"Number of converged eigenpairs: {n_conv}")
print("##################################\n")
vals = [(i, eps.getEigenvalue(i)) for i in range(eps.getConverged())]
vals.sort(key=lambda x: x[1].real)
eig_list = []
field_B = fem.Function(V)
if os.path.exists(f"{output_path}/lambda_{lambda_l}"):
print(
f"Warning:Folder {output_path}/lambda_{lambda_l} already exists. Removing it..."
)
for root, dirs, files in os.walk(
f"{output_path}/lambda_{lambda_l}", topdown=False
):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
else:
os.makedirs(f"{output_path}/lambda_{lambda_l}", exist_ok=True)
for i, eig in vals:
# Save eigenvector in field_B
eps.getEigenpair(i, field_B.x.petsc_vec)
error = eps.computeError(i, SLEPc.EPS.ErrorType.RELATIVE)
if error >= tol: # and np.isclose(eig.imag, 0, atol=tol):
print(
f"Warning: The error {error} of eigenvalue {eig.real} is greater than tolerance {tol}. Neglecting it."
)
continue
eig_list.append(eig)
# TODO: Comparing with analytical modes when λ_L is zero.
eig_str = f"#{i} Eigenvalue: {eig.real}\t L√E/π: {np.sqrt(eig.real)/np.pi}"
print(eig_str)
with open(f"{output_path}/lambda_{lambda_l}/eigs.txt", "a") as f:
f.write(eig_str + "\n")
field_B.x.scatter_forward()
gdim = mesh.geometry.dim
V_dg = fem.functionspace(mesh, ("DG", fem_degree, (gdim,)))
B_dg = fem.Function(V_dg)
B_dg.interpolate(field_B)
# Save solutions
with io.VTXWriter(
mesh.comm, f"{output_path}/lambda_{lambda_l}/sols/B_{i}.bp", B_dg
) as f:
f.write(0.0)
# Visualize solutions with Pyvista
if have_pyvista:
V_cells, V_types, V_x = plot.vtk_mesh(V_dg)
V_grid = pyvista.UnstructuredGrid(V_cells, V_types, V_x)
B_vectors = B_dg.x.array.reshape(V_x.shape[0], mesh.topology.dim).real
B_values = np.linalg.norm(B_vectors, axis=1)
# V_grid.point_data["b"] = B_vectors[:, 0]
V_grid.point_data["b"] = B_values
# B_max = np.max(B_values)
V_grid.set_active_scalars("b")
warped = V_grid.warp_by_scalar("b", factor=0.3)
plotter = pyvista.Plotter(off_screen=not gui)
plotter.add_mesh(warped, show_edges=False, cmap="twilight")
# plotter.add_mesh(V_grid.copy(), show_edges=False, cmap="twilight")
plotter.add_text(
f"L√E/π: {np.sqrt(eig.real)/np.pi}",
position="upper_left",
)
# plotter.view_xy()
# plotter.link_views()
plotter.save_graphic(
f"{output_path}/lambda_{lambda_l}/eig_{np.sqrt(eig.real)/np.pi:.3f}_i{i}.svg"
)
if not pyvista.OFF_SCREEN:
plotter.show()
eps.destroy()
def load_gmsh_mesh(mesh_path):
# Using Gmsh.
Cavity = 1
SC_boundary = 2
mesh, cell_markers, facet_markers = gmshio.read_from_msh(
mesh_path, MPI.COMM_WORLD, gdim=2
)
mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim)
D = fem.functionspace(mesh, ("DG", 0))
num_dofs = D.dofmap.index_map.size_local + D.dofmap.index_map.num_ghosts
print(f"Number of DOFs: {num_dofs}")
inv_lambda_sqr = fem.Function(D)
ils = 1 / lambda_l**2
material_tags = np.unique(cell_markers.values)
print(f"Material tags: {material_tags}")
for tag in material_tags:
cells = cell_markers.find(tag)
# Set values for inv lambda^2
if tag == SC_boundary:
inv_lambda_sqr.x.array[cells] = np.full_like(cells, ils, dtype=scalar_type)
elif tag == Cavity:
inv_lambda_sqr.x.array[cells] = np.full_like(cells, 0, dtype=scalar_type)
return mesh, inv_lambda_sqr
def generate_mesh():
# Using FEniCS buid-in mesh generator.
lx = 1.0
ly = 1.0
sc = 0.5
cell_size = 0.01
# Mesh
nx = int((lx + 2 * sc) / cell_size)
ny = int((ly + 2 * sc) / cell_size)
print(f"Number of cells: {nx} x {ny}")
mesh = create_rectangle(
MPI.COMM_WORLD,
np.array([[-sc, -sc], [lx + sc, ly + sc]]),
np.array([nx, ny]),
CellType.quadrilateral,
)
mesh.topology.create_connectivity(mesh.topology.dim - 1, mesh.topology.dim)
print(f"msh.topology.dim: {mesh.topology.dim}")
# 1/lambda^2
def at_sc_boundary(pos):
return pos[0] <= 0 or pos[0] >= lx or pos[1] <= 0 or pos[1] >= ly
def sc(x):
return np.array([at_sc_boundary(pos) for pos in x.T], dtype=bool)
def cavity(pos_list):
return np.logical_not(sc(pos_list))
D = fem.functionspace(mesh, ("DG", 0))
num_dofs = D.dofmap.index_map.size_local + D.dofmap.index_map.num_ghosts
print(f"Number of DOFs: {num_dofs}")
inv_lambda_sqr = fem.Function(D)
cells_scb = locate_entities(mesh, mesh.topology.dim, sc)
cells_cavity = locate_entities(mesh, mesh.topology.dim, cavity)
ils = 1 / lambda_l**2
inv_lambda_sqr.x.array[cells_scb] = np.full_like(cells_scb, ils, dtype=scalar_type)
inv_lambda_sqr.x.array[cells_cavity] = np.full_like(
cells_cavity, 0, dtype=scalar_type
)
return mesh, inv_lambda_sqr
def parse_arguments():
parser = argparse.ArgumentParser(
description="Solve Maxwell eigenvalue problem for a cavity with superconducting boundary conditions."
)
parser.add_argument(
"-l", "--lambda_l", type=float, default=0.01, help="Lambda parameter"
)
parser.add_argument(
"-msh",
"--mesh_path",
type=str,
help="GMSH format mesh file path. If not set, use built-in mesh generator.",
)
parser.add_argument(
"-o",
"--output_path",
type=str,
default="results",
help="Output path for results",
)
parser.add_argument(
"-n",
"--num_eigs",
type=int,
default=13,
help="Number of eigenvalues to compute",
)
parser.add_argument("--gui", action="store_true", help="Enable GUI")
parser.add_argument(
"--eps_target", type=float, default=15.0, help="Target eigenvalue for SLEPc"
)
parser.add_argument(
"--tol", type=float, default=1e-10, help="Tolerance for eigenvalue solver"
)
return parser.parse_args()
if __name__ == "__main__":
args = parse_arguments()
lambda_l = args.lambda_l
gui = args.gui
eps_target = args.eps_target
num_eigs = args.num_eigs
tol = args.tol
output_path = args.output_path
mesh_path = args.mesh_path
if mesh_path:
mesh, inv_lambda_sqr = load_gmsh_mesh(mesh_path)
else:
mesh, inv_lambda_sqr = generate_mesh()
solve(
mesh,
inv_lambda_sqr,
fem_degree=3,
gui=gui,
eps_target=eps_target,
num_eigs=num_eigs,
tol=tol,
output_path=output_path,
)