2626from dolfinx import mesh , fem , default_scalar_type
2727import ufl
2828from mpi4py import MPI
29- from petsc4py import PETSc
3029
3130from elastodynamicsx .pde import material , BodyForce , boundarycondition , PDE
3231from elastodynamicsx .solvers import FrequencyDomainSolver
3332from elastodynamicsx .plot import plotter , live_plotter
34- from elastodynamicsx .utils import make_facet_tags , make_cell_tags , ParallelEvaluator
33+ from elastodynamicsx .utils import make_facet_tags , ParallelEvaluator
3534
3635from analyticalsolutions import u_2D_PSV_xw , int_Fraunhofer_2D
3736
4443# +
4544degElement = 1
4645length , height = 10 , 10
47- Nx , Ny = 100 // degElement , 100 // degElement
46+ Nx , Ny = 100 // degElement , 100 // degElement
4847
4948# create the mesh
5049extent = [[0. , 0. ], [length , height ]]
5554
5655tag_left , tag_top , tag_right , tag_bottom = 1 , 2 , 3 , 4
5756all_tags = (tag_left , tag_top , tag_right , tag_bottom )
58- boundaries = [(tag_left , lambda x : np .isclose (x [0 ], 0 )),\
59- (tag_right , lambda x : np .isclose (x [0 ], length )),\
60- (tag_bottom , lambda x : np .isclose (x [1 ], 0 )),\
57+ boundaries = [(tag_left , lambda x : np .isclose (x [0 ], 0 )),
58+ (tag_right , lambda x : np .isclose (x [0 ], length )),
59+ (tag_bottom , lambda x : np .isclose (x [1 ], 0 )),
6160 (tag_top , lambda x : np .isclose (x [1 ], height ))]
6261
6362# define some tags
8483
8584# +
8685Z_N , Z_T = mat .Z_N , mat .Z_T # P and S mechanical impedances
87- bc = boundarycondition ((V , facet_tags , all_tags ), 'Dashpot' , Z_N , Z_T )
86+ bc = boundarycondition ((V , facet_tags , all_tags ), 'Dashpot' , Z_N , Z_T )
8887
8988bcs = [bc ]
9089# -
9695# +
9796F0 = fem .Constant (domain , default_scalar_type ([1 , 0 ])) # amplitude
9897R0 = 0.1 # radius
99- X0 = np .array ([length / 2 , height / 2 , 0 ]) # center
98+ X0 = np .array ([length / 2 , height / 2 , 0 ]) # center
10099
101- x = ufl .SpatialCoordinate (domain )
102- gaussianBF = F0 * ufl .exp (- ((x [0 ]- X0 [0 ])** 2 + (x [1 ]- X0 [1 ])** 2 )/ 2 / R0 ** 2 ) / (2 * np .pi * R0 ** 2 )
100+ x = ufl .SpatialCoordinate (domain )
101+ gaussianBF = F0 * ufl .exp (- ((x [0 ] - X0 [0 ])** 2 + (x [1 ] - X0 [1 ])** 2 ) / 2 / R0 ** 2 ) / (2 * np .pi * R0 ** 2 )
103102
104103bf = BodyForce (V , gaussianBF )
105104# -
156155from scipy .spatial .transform import Rotation as R
157156theta = np .radians (35 )
158157pts = np .linspace (0 , length / 2 , endpoint = False )[1 :]
159- points_out = X0 [:,np .newaxis ] + R .from_rotvec ([0 , 0 , theta ]).as_matrix () @ np .array ([pts ,
160- np .zeros_like (pts ),
161- np .zeros_like (pts )])
158+ points_out = X0 [:, np .newaxis ] + \
159+ R .from_rotvec ([0 , 0 , theta ]).as_matrix () @ np .array ([pts ,
160+ np .zeros_like (pts ),
161+ np .zeros_like (pts )])
162162
163163# Declare a convenience ParallelEvaluator
164164paraEval = ParallelEvaluator (domain , points_out )
167167u_at_pts_local = np .zeros ((paraEval .nb_points_local ,
168168 V .num_sub_spaces ,
169169 omegas .size ),
170- dtype = default_scalar_type ) # <- output stored here
170+ dtype = default_scalar_type ) # <- output stored here
171+
171172
172173# Callback function: post process solution
173174def cbck_storeAtPoints (i , out ):
174175 if paraEval .nb_points_local > 0 :
175- u_at_pts_local [:,:,i ] = u_res .eval (paraEval .points_local , paraEval .cells_local )
176+ u_at_pts_local [:, :, i ] = u_res .eval (paraEval .points_local , paraEval .cells_local )
177+
176178
177179# Live plotting
178- enable_plot = True
179- if domain .comm .rank == 0 and enable_plot :
180- p = live_plotter (u_res , clim = 0.25 * np .linalg .norm (mu .value * F0 .value ) * np .array ([0 , 1 ]))
180+ if domain .comm .rank == 0 :
181+ p = live_plotter (u_res ,
182+ show_edges = False ,
183+ clim = 0.25 * np .linalg .norm (mu .value * F0 .value ) * np .array ([0 , 1 ]))
181184 if paraEval .nb_points_local > 0 :
182185 p .add_points (paraEval .points_local ) # add points to live_plotter
186+ if p .off_screen :
187+ p .window_size = [640 , 480 ]
188+ p .open_movie ('freq_2D-PSV_FullSpace.mp4' , framerate = 1 )
183189else :
184190 p = None
185191
@@ -196,25 +202,25 @@ def cbck_storeAtPoints(i, out):
196202u_at_pts = paraEval .gather (u_at_pts_local , root = 0 )
197203
198204if domain .comm .rank == 0 :
199- ### -> Exact solution, At few points
205+ # -> Exact solution, At few points
200206 x = points_out .T
201207
202208 # account for the size of the source in the analytical formula
203209 fn_kdomain_finite_size = int_Fraunhofer_2D ['gaussian' ](R0 )
204- u_at_pts_anal = u_2D_PSV_xw (x - X0 [np .newaxis ,:], omegas , F0 .value , rho .value ,
210+ u_at_pts_anal = u_2D_PSV_xw (x - X0 [np .newaxis , :], omegas , F0 .value , rho .value ,
205211 lambda_ .value , mu .value , fn_kdomain_finite_size )
206212
207213 #
208214 fn = np .real
209215
210216 icomp = 0
211- fig , ax = plt .subplots (len (omegas ),1 )
217+ fig , ax = plt .subplots (len (omegas ), 1 )
212218 fig .suptitle (r'u at few points, $\theta$=' + str (int (round (np .degrees (theta ), 0 ))) + r'$^{\circ}$' )
213- r = np .linalg .norm (x - X0 [np .newaxis ,:], axis = 1 )
219+ r = np .linalg .norm (x - X0 [np .newaxis , :], axis = 1 )
214220 for i in range (len (omegas )):
215221 ax [i ].text (0.15 , 0.95 , r'$\omega$=' + str (round (omegas [i ], 2 )),
216222 ha = 'left' , va = 'top' , transform = ax [i ].transAxes )
217- ax [i ].plot (r , fn (u_at_pts [:, icomp , i ]), ls = '-' , label = 'FEM' )
223+ ax [i ].plot (r , fn (u_at_pts [:, icomp , i ]), ls = '-' , label = 'FEM' )
218224 ax [i ].plot (r , fn (u_at_pts_anal [:, icomp , i ]), ls = '--' , label = 'analytical' )
219225 ax [0 ].legend ()
220226 ax [- 1 ].set_xlabel ('Distance to source' )
0 commit comments