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
375
376
377
378
379
380
381
382
383
384
385
386
|
# Copyright 2017-2022 The WarpX Community
#
# This file is part of WarpX. It defines the wrapper functions that directly
# call the underlying compiled routines through pybind11.
#
# NOTE: We will reduce the libwarpx.py level of abstraction eventually!
# Please add new functionality directly to pybind11-bound modules
# and call them via sim.extension.libwarpx_so. ... and sim.extension.warpx.
# ... from user code.
#
# Authors: Axel Huebl, Andrew Myers, David Grote, Remi Lehe, Weiqun Zhang
#
# License: BSD-3-Clause-LBNL
import atexit
import os
import sys
import numpy as np
from .Geometry import geometry
class LibWarpX():
"""This class manages the WarpX classes, part of the Python module from the compiled C++ code.
It will only load the library when it is referenced, and this can only be done after
the geometry is defined so that the version of the library that is needed can be determined.
Once loaded, all the settings of function call interfaces are set up.
"""
def __init__(self):
# Track whether amrex and warpx have been initialized
self.initialized = False
atexit.register(self.finalize)
# set once libwarpx_so is loaded
self.__version__ = None
def __getattr__(self, attribute):
if attribute == 'libwarpx_so':
# If the 'libwarpx_so' is referenced, load it.
# Once loaded, it gets added to the dictionary so this code won't be called again.
self.load_library()
return self.__dict__[attribute]
else:
# For any other attribute, call the built-in routine - this should always
# return an AttributeException.
return self.__getattribute__(attribute)
def _get_package_root(self):
'''
Get the path to the installation location (where libwarpx.so would be installed).
'''
cur = os.path.abspath(__file__)
while True:
name = os.path.basename(cur)
if name == 'pywarpx':
return cur
elif not name:
return ''
cur = os.path.dirname(cur)
def load_library(self):
if 'libwarpx_so' in self.__dict__:
raise RuntimeError(
"Invalid attempt to load the pybind11 bindings library multiple times. "
"Note that multiple AMReX/WarpX geometries cannot be loaded yet into the same Python process. "
"Please write separate scripts for each geometry."
)
# --- Use geometry to determine whether to import the 1D, 2D, 3D or RZ version.
# --- The geometry must be setup before the lib warpx shared object can be loaded.
try:
_prob_lo = geometry.prob_lo
_dims = geometry.dims
except AttributeError:
raise Exception('The shared object could not be loaded. The geometry must be setup before the WarpX pybind11 module can be accessesd. The geometry determines which version of the shared object to load.')
if _dims == 'RZ':
self.geometry_dim = 'rz'
elif (_dims == '1' or _dims == '2' or _dims == '3'):
self.geometry_dim = '%dd'%len(_prob_lo)
else:
raise Exception('Undefined geometry %d'%_dims)
try:
if self.geometry_dim == "1d":
import amrex.space1d as amr
self.amr = amr
from . import warpx_pybind_1d as cxx_1d
self.libwarpx_so = cxx_1d
self.dim = 1
elif self.geometry_dim == "2d":
import amrex.space2d as amr
self.amr = amr
from . import warpx_pybind_2d as cxx_2d
self.libwarpx_so = cxx_2d
self.dim = 2
elif self.geometry_dim == "rz":
import amrex.space2d as amr
self.amr = amr
from . import warpx_pybind_rz as cxx_rz
self.libwarpx_so = cxx_rz
self.dim = 2
elif self.geometry_dim == "3d":
import amrex.space3d as amr
self.amr = amr
from . import warpx_pybind_3d as cxx_3d
self.libwarpx_so = cxx_3d
self.dim = 3
except ImportError:
raise Exception(f"Dimensionality '{self.geometry_dim}' was not compiled in this Python install. Please recompile with -DWarpX_DIMS={_dims}")
self.__version__ = self.libwarpx_so.__version__
def getNProcs(self):
'''
Get the number of processors
'''
return self.libwarpx_so.getNProcs()
def getMyProc(self):
'''
Get the number of the processor
'''
return self.libwarpx_so.getMyProc()
def get_nattr(self):
'''
Get the number of extra particle attributes.
'''
# --- The -3 is because the comps include the velocites
return self.libwarpx_so.warpx_nComps() - 3
def get_nattr_species(self, species_name):
'''
Get the number of real attributes for the given species.
Parameters
----------
species_name: str
Name of the species
'''
warpx = self.libwarpx_so.get_instance()
mpc = warpx.multi_particle_container()
pc = mpc.get_particle_container_from_name(species_name)
return pc.num_real_comps()
def amrex_init(self, argv, mpi_comm=None):
if mpi_comm is None: # or MPI is None:
self.libwarpx_so.amrex_init(argv)
else:
raise Exception('mpi_comm argument not yet supported')
def initialize(self, argv=None, mpi_comm=None):
'''
Initialize WarpX and AMReX. Must be called before doing anything else.
'''
if argv is None:
argv = sys.argv
self.amrex_init(argv, mpi_comm)
self.warpx = self.libwarpx_so.get_instance()
self.warpx.initialize_data()
self.libwarpx_so.execute_python_callback("afterinit")
self.libwarpx_so.execute_python_callback("particleloader")
#self.libwarpx_so.warpx_init()
self.initialized = True
def finalize(self, finalize_mpi=1):
'''
Call finalize for WarpX and AMReX. Registered to run at program exit.
'''
# TODO: simplify, part of pyAMReX already
if self.initialized:
del self.warpx
# The call to warpx_finalize causes a crash - don't know why
#self.libwarpx_so.warpx_finalize()
self.libwarpx_so.amrex_finalize()
from pywarpx import callbacks
callbacks.clear_all()
def getistep(self, level=0):
'''
Get the current time step number for the specified level
Parameter
---------
level : int
The refinement level to reference
'''
return self.warpx.getistep(level)
def gett_new(self, level=0):
'''
Get the next time for the specified level.
Parameters
----------
level : int
The refinement level to reference
'''
return self.warpx.gett_new(level)
def evolve(self, num_steps=-1):
'''
Evolve the simulation for num_steps steps. If num_steps=-1,
the simulation will be run until the end as specified in the
inputs file.
Parameters
----------
num_steps: int
The number of steps to take
'''
self.warpx.evolve(num_steps)
def getProbLo(self, direction, level=0):
'''
Get the values of the lower domain boundary.
Parameters
----------
direction : int
Direction of interest
'''
assert 0 <= direction < self.dim, 'Inappropriate direction specified'
return self.warpx.Geom(level).ProbLo(direction)
def getProbHi(self, direction, level=0):
'''
Get the values of the upper domain boundary.
Parameters
----------
direction : int
Direction of interest
'''
assert 0 <= direction < self.dim, 'Inappropriate direction specified'
return self.warpx.Geom(level).ProbHi(direction)
def getCellSize(self, direction, level=0):
'''
Get the cell size in the given direction and on the given level.
Parameters
----------
direction : int
Direction of interest
level : int
The refinement level to reference
'''
assert 0 <= direction < 3, 'Inappropriate direction specified'
assert 0 <= level and level <= self.libwarpx_so.warpx_finestLevel(), 'Inappropriate level specified'
return self.libwarpx_so.warpx_getCellSize(direction, level)
#def get_sigma(self, direction):
# '''
#
# Return the 'sigma' PML coefficients for the electric field
# in a given direction.
#
# '''
#
# size = ctypes.c_int(0)
# data = self.libwarpx_so.warpx_getPMLSigma(direction, ctypes.byref(size))
# arr = np.ctypeslib.as_array(data, (size.value,))
# arr.setflags(write=1)
# return arr
#
#
#def get_sigma_star(self, direction):
# '''
#
# Return the 'sigma*' PML coefficients for the magnetic field
# in the given direction.
#
# '''
#
# size = ctypes.c_int(0)
# data = self.libwarpx_so.warpx_getPMLSigmaStar(direction, ctypes.byref(size))
# arr = np.ctypeslib.as_array(data, (size.value,))
# arr.setflags(write=1)
# return arr
#
#
#def compute_pml_factors(self, lev, dt):
# '''
#
# This recomputes the PML coefficients for a given level, using the
# time step dt. This needs to be called after modifying the coefficients
# from Python.
#
# '''
#
# self.libwarpx_so.warpx_ComputePMLFactors(lev, dt)
def depositChargeDensity(self, species_name, level, clear_rho=True, sync_rho=True):
'''
Deposit the specified species' charge density in rho_fp in order to
access that data via pywarpx.fields.RhoFPWrapper().
Parameters
----------
species_name : str
The species name that will be deposited.
level : int
Which AMR level to retrieve scraped particle data from.
clear_rho : bool
If True, zero out rho_fp before deposition.
sync_rho : bool
If True, perform MPI exchange and properly set boundary cells for rho_fp.
'''
rho_fp = self.warpx.multifab(f'rho_fp[level={level}]')
if rho_fp is None:
raise RuntimeError("Multifab `rho_fp` is not allocated.")
# ablastr::warn_manager::WMRecordWarning(
# "WarpXWrappers", "rho_fp is not allocated",
# ablastr::warn_manager::WarnPriority::low
# );
# return
if clear_rho:
rho_fp.set_val(0.0)
# deposit the charge density from the desired species
mypc = self.warpx.multi_particle_container()
myspc = mypc.get_particle_container_from_name(species_name)
myspc.deposit_charge(rho_fp, level)
if self.geometry_dim == 'rz':
self.warpx.apply_inverse_volume_scaling_to_charge_density(rho_fp, level)
if sync_rho:
self.warpx.sync_rho()
def set_potential_EB(self, potential):
"""
Set the expression string for the embedded boundary potential
Parameters
----------
potential : str
The expression string
"""
self.warpx.set_potential_on_eb(potential)
libwarpx = LibWarpX()
|