Skip to content

Commit 6e53f24

Browse files
committed
test: reorganize test, now all 3D interactive plotting routines are in test_008
1 parent 3f9a1b9 commit 6e53f24

File tree

2 files changed

+176
-34
lines changed

2 files changed

+176
-34
lines changed

tests/test_007_mpi_lossy_cavity.py

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,19 @@ class TestMPILossyCavity:
8686
-6.04105997e+01 ,-3.06532160e+01 ,-1.17749936e+01 ,-3.12574866e+00,
8787
-7.35339521e-01 ,-1.13085658e-01 , 7.18247535e-01 , 8.73829036e-02])
8888

89-
gridLogs = {'use_mesh_refinement': False, 'Nx': 60, 'Ny': 60, 'Nz': 140, 'dx': 0.00866666634877522,
90-
'dy': 0.00866666634877522, 'dz': 0.005714285799435207,
91-
'xmin': -0.25999999046325684, 'xmax': 0.25999999046325684,
92-
'ymin': -0.25999999046325684, 'ymax': 0.25999999046325684,
93-
'zmin': -0.25, 'zmax': 0.550000011920929,
89+
gridLogs = {'use_mesh_refinement': False, 'Nx': 60, 'Ny': 60, 'Nz': 140, 'dx': 0.00866666634877522,
90+
'dy': 0.00866666634877522, 'dz': 0.005714285799435207,
91+
'xmin': -0.25999999046325684, 'xmax': 0.25999999046325684,
92+
'ymin': -0.25999999046325684, 'ymax': 0.25999999046325684,
93+
'zmin': -0.25, 'zmax': 0.550000011920929,
9494
'stl_solids': {'cavity': 'tests/stl/007_vacuum_cavity.stl', 'shell': 'tests/stl/007_lossymetal_shell.stl'},
95-
'stl_materials': {'cavity': 'vacuum', 'shell': [30, 1.0, 30]},
95+
'stl_materials': {'cavity': 'vacuum', 'shell': [30, 1.0, 30]},
9696
'gridInitializationTime': 0}
97-
97+
9898
solverLogs = {'use_gpu': False, 'use_mpi': False, 'background': 'pec',
99-
'bc_low': ['pec', 'pec', 'pec'], 'bc_high': ['pec', 'pec', 'pec'],
99+
'bc_low': ['pec', 'pec', 'pec'], 'bc_high': ['pec', 'pec', 'pec'],
100100
'dt': 6.970326728398966e-12, 'solverInitializationTime': 0}
101-
101+
102102
wakeSolverLogs = {'ti': 2.8516132094735135e-09, 'q': 1e-09, 'sigmaz': 0.1, 'beta': 1.0,
103103
'xsource': 0.0, 'ysource': 0.0, 'xtest': 0.0, 'ytest': 0.0, 'chargedist': None,
104104
'skip_cells': 10, 'results_folder': 'tests/007_results/', 'wakelength': 10.0, 'simulationTime': 0}
@@ -252,29 +252,6 @@ def test_mpi_plot1D(self):
252252
xscale='linear', yscale='linear',
253253
off_screen=True, title=self.img_folder+'Ez1d', n=3000)
254254

255-
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
256-
def test_mpi_plot3D(self):
257-
# Plot Abs Electric field on domain
258-
# disabled when mpi = True
259-
global solver
260-
solver.plot3D('E', component='Abs',
261-
cmap='rainbow', clim=[0, 500],
262-
add_stl=['cavity', 'shell'], stl_opacity=0.1,
263-
clip_interactive=True, clip_normal='-y')
264-
265-
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
266-
def test_mpi_plot3DonSTL(self):
267-
# Plot Abs Electric field on STL solid `cavity`
268-
# disabled when mpi = True
269-
global solver
270-
solver.plot3DonSTL('E', component='Abs',
271-
cmap='rainbow', clim=[0, 500],
272-
stl_with_field='cavity', field_opacity=1.0,
273-
stl_transparent='shell', stl_opacity=0.1, stl_colors='white',
274-
clip_plane=True, clip_normal='-y', clip_origin=[0,0,0],
275-
off_screen=False, zoom=1.2, title=self.img_folder+'Ez3d')
276-
277-
278255
def test_mpi_wakefield(self):
279256
# Reset fields
280257
global solver
@@ -343,7 +320,7 @@ def test_long_impedance(self):
343320
assert np.allclose(np.real(wake.Z)[::20], np.real(self.Z), rtol=0.1), "Real Impedance samples failed"
344321
assert np.allclose(np.imag(wake.Z)[::20], np.imag(self.Z), rtol=0.1), "Imag Impedance samples failed"
345322
assert np.cumsum(np.abs(wake.Z))[-1] == pytest.approx(250910.51090497518, 0.1), "Abs Impedance cumsum failed"
346-
323+
347324
def test_log_file(self):
348325
# Helper function to compare nested dicts with float tolerance
349326
def assert_dict_allclose(d1, d2, rtol=1e-6, atol=1e-12, path=""):
@@ -379,7 +356,7 @@ def assert_dict_allclose(d1, d2, rtol=1e-6, atol=1e-12, path=""):
379356

380357
global solver
381358
# Exclude timing info from comparison as they can vary between runs
382-
solver.logger.grid["gridInitializationTime"] = 0
359+
solver.logger.grid["gridInitializationTime"] = 0
383360
solver.logger.solver["solverInitializationTime"] = 0
384361
solver.logger.wakeSolver["simulationTime"] = 0
385362
self.solverLogs['use_mpi'] = use_mpi

tests/test_008_3D_plotting.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import os
2+
import sys
3+
import numpy as np
4+
import pyvista as pv
5+
import matplotlib.pyplot as plt
6+
7+
sys.path.append('../wakis')
8+
9+
from tqdm import tqdm
10+
from scipy.constants import c
11+
12+
from wakis import SolverFIT3D
13+
from wakis import GridFIT3D
14+
from wakis.sources import Beam
15+
16+
import pytest
17+
18+
# Turn true when running local
19+
flag_plot_3D = False
20+
flag_offscreen = False
21+
22+
@pytest.mark.slow
23+
class Test3Dplotting:
24+
25+
img_folder = 'tests/008_img/'
26+
27+
def test_simulation(self):
28+
29+
# ---------- Domain setup ---------
30+
31+
# Geometry & Materials
32+
solid_1 = 'tests/stl/007_vacuum_cavity.stl'
33+
solid_2 = 'tests/stl/007_lossymetal_shell.stl'
34+
35+
stl_solids = {'cavity': solid_1,
36+
'shell': solid_2
37+
}
38+
39+
stl_materials = {'cavity': 'vacuum',
40+
'shell': [30, 1.0, 30] #[eps_r, mu_r, sigma[S/m]]
41+
}
42+
43+
stl_colors = {'cavity': 'vacuum',
44+
'shell': [1, 1, 1]}
45+
46+
# Extract domain bounds from geometry
47+
solids = pv.read(solid_1) + pv.read(solid_2)
48+
xmin, xmax, ymin, ymax, ZMIN, ZMAX = solids.bounds
49+
50+
# Number of mesh cells
51+
Nx = 60
52+
Ny = 60
53+
NZ = 140
54+
55+
grid = GridFIT3D(xmin, xmax, ymin, ymax, ZMIN, ZMAX,
56+
Nx, Ny, NZ,
57+
use_mpi=False, # Enables MPI subdivision of the domain
58+
stl_solids=stl_solids,
59+
stl_materials=stl_materials,
60+
stl_colors=stl_colors,
61+
stl_scale=1.0,
62+
stl_rotate=[0,0,0],
63+
stl_translate=[0,0,0],
64+
verbose=1)
65+
66+
# ------------ Beam source & Wake ----------------
67+
# Beam parameters
68+
sigmaz = 10e-2 #[m] -> 2 GHz
69+
q = 1e-9 #[C]
70+
beta = 1.0 # beam beta
71+
xs = 0. # x source position [m]
72+
ys = 0. # y source position [m]
73+
ti = 3*sigmaz/c # injection time [s]
74+
75+
beam = Beam(q=q, sigmaz=sigmaz, beta=beta,
76+
xsource=xs, ysource=ys, ti=ti)
77+
78+
# ----------- Solver & Simulation ----------
79+
# boundary conditions
80+
bc_low=['pec', 'pec', 'pec']
81+
bc_high=['pec', 'pec', 'pec']
82+
83+
# Solver setup
84+
global solver
85+
solver = SolverFIT3D(grid,
86+
bc_low=bc_low,
87+
bc_high=bc_high,
88+
use_stl=True,
89+
use_mpi=False, # Activate MPI
90+
bg='pec' # Background material
91+
)
92+
93+
# -------------- Output folder ---------------------
94+
if not os.path.exists(self.img_folder):
95+
os.mkdir(self.img_folder)
96+
97+
# -------------- Custom time loop -----------------
98+
Nt = 3000
99+
for n in tqdm(range(Nt)):
100+
beam.update(solver, n*solver.dt)
101+
solver.one_step()
102+
103+
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
104+
def test_grid_inspect(self):
105+
# Plot grid and imported solids
106+
global solver
107+
solver.grid.inspect(add_stl=['cavity', 'shell'],
108+
stl_opacity=0.1, off_screen=flag_offscreen,
109+
anti_aliasing='ssaa')
110+
111+
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
112+
def test_grid_plot_solids(self):
113+
# Plot only imported solids
114+
global solver
115+
solver.grid.plot_solids(bounding_box=True,
116+
show_grid=True,
117+
opacity=1,
118+
specular=0.5,
119+
smooth_shading=False,
120+
off_screen=flag_offscreen,)
121+
122+
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
123+
def test_grid_stl_mask(self):
124+
# Plot STL solid masks in the grid
125+
global solver
126+
solver.grid.plot_stl_mask(stl_solid='cavity',
127+
cmap='viridis',
128+
add_stl='all',
129+
stl_opacity=0.5,
130+
smooth_shading=False,
131+
anti_aliasing='ssaa',
132+
ymax=0.0,
133+
off_screen=flag_offscreen,)
134+
135+
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
136+
def test_solver_inspect(self):
137+
# Plot imported solids and beam source and integraiton path
138+
global solver
139+
pl = solver.inspect(window_size=(1200,800), off_screen=flag_offscreen,
140+
specular=0.,opacity=1, inactive_opacity=0.1,
141+
add_silhouette=True,)
142+
if flag_offscreen:
143+
pl.export_html(self.img_folder+'solver_inspect.html')
144+
pl.screenshot(self.img_folder+'solver_inspect.png')
145+
146+
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
147+
def test_plot3D(self):
148+
# Plot Abs Electric field on domain
149+
global solver
150+
solver.plot3D('E', component='Abs',
151+
cmap='rainbow', clim=[0, 500],
152+
add_stl=['cavity', 'shell'], stl_opacity=0.1,
153+
clip_interactive=True, clip_normal='-y',
154+
off_screen=flag_offscreen)
155+
156+
@pytest.mark.skipif(not flag_plot_3D, reason="Requires interactive plotting")
157+
def test_plot3DonSTL(self):
158+
# Plot Abs Electric field on STL solid `cavity`
159+
global solver
160+
solver.plot3DonSTL('E', component='Abs',
161+
cmap='rainbow', clim=[0, 500],
162+
stl_with_field='cavity', field_opacity=1.0,
163+
stl_transparent='shell', stl_opacity=0.1, stl_colors='white',
164+
clip_plane=True, clip_normal='-y', clip_origin=[0,0,0],
165+
off_screen=flag_offscreen, zoom=1.2, title=self.img_folder+'Ez3d')

0 commit comments

Comments
 (0)