diff options
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pywarpx/PGroup.py | 1 | ||||
-rw-r--r-- | Python/pywarpx/WarpX.py | 21 | ||||
-rw-r--r-- | Python/pywarpx/__init__.py | 10 | ||||
-rwxr-xr-x | Python/pywarpx/_libwarpx.py | 245 | ||||
-rw-r--r-- | Python/setup.py | 14 |
5 files changed, 123 insertions, 168 deletions
diff --git a/Python/pywarpx/PGroup.py b/Python/pywarpx/PGroup.py index 64e396d38..68b37740d 100644 --- a/Python/pywarpx/PGroup.py +++ b/Python/pywarpx/PGroup.py @@ -1,5 +1,4 @@ import numpy as np -from . import WarpX from . import _libwarpx class PGroup(object): diff --git a/Python/pywarpx/WarpX.py b/Python/pywarpx/WarpX.py index f32dd735c..9c4a4b380 100644 --- a/Python/pywarpx/WarpX.py +++ b/Python/pywarpx/WarpX.py @@ -1,5 +1,4 @@ from .Bucket import Bucket - from .Constants import constants from .Amr import amr from .Geometry import geometry @@ -10,9 +9,6 @@ from .Laser import laser from . import Particles from .Particles import particles, particles_list -import ctypes -from ._libwarpx import libwarpx -from ._libwarpx import amrex_init class WarpX(Bucket): """ @@ -50,22 +46,25 @@ class WarpX(Bucket): return argv def init(self): + from . import wx argv = ['warpx'] + self.create_argv_list() - amrex_init(argv) - libwarpx.warpx_init() + wx.initialize(argv) def evolve(self, nsteps=-1): - libwarpx.warpx_evolve(nsteps) + from . import wx + wx.evolve(nsteps) def finalize(self, finalize_mpi=1): - libwarpx.warpx_finalize() - libwarpx.amrex_finalize(finalize_mpi) + from . import wx + wx.finalize(finalize_mpi) def getProbLo(self, direction): - return libwarpx.warpx_getProbLo(direction) + from . import wx + return wx.libwarpx.warpx_getProbLo(direction) def getProbHi(self, direction): - return libwarpx.warpx_getProbHi(direction) + from . import wx + return wx.libwarpx.warpx_getProbHi(direction) def write_inputs(self, filename='inputs', **kw): argv = self.create_argv_list() diff --git a/Python/pywarpx/__init__.py b/Python/pywarpx/__init__.py index d3c92d5bc..1517bbbaf 100644 --- a/Python/pywarpx/__init__.py +++ b/Python/pywarpx/__init__.py @@ -1,4 +1,3 @@ - from .WarpX import warpx from .Constants import constants from .Amr import amr @@ -8,12 +7,3 @@ from .Langmuirwave import langmuirwave from .Interpolation import interpolation from .Particles import particles, electrons, positrons, protons, newspecies from .Laser import laser - -#from .timestepper import TimeStepper -from .PGroup import PGroup -from .PGroup import PGroups -#from .WarpXPIC import WarpXPIC - -from ._libwarpx import add_particles - -from .callbacks import * diff --git a/Python/pywarpx/_libwarpx.py b/Python/pywarpx/_libwarpx.py index 71e0a6729..9e3c9364b 100755 --- a/Python/pywarpx/_libwarpx.py +++ b/Python/pywarpx/_libwarpx.py @@ -2,14 +2,14 @@ import os import sys import ctypes -from ctypes.util import find_library +from ctypes.util import find_library as _find_library import numpy as np -from numpy.ctypeslib import ndpointer +from numpy.ctypeslib import ndpointer as _ndpointer # --- Is there a better way of handling constants? clight = 2.99792458e+8 # m/s -def get_package_root(): +def _get_package_root(): ''' Get the path to the installation location (where libwarpx.so would be installed). ''' @@ -23,39 +23,42 @@ def get_package_root(): cur = os.path.dirname(cur) -libwarpx = ctypes.CDLL(os.path.join(get_package_root(), "libwarpx.so")) -libc = ctypes.CDLL(find_library('c')) +try: + libwarpx = ctypes.CDLL(os.path.join(_get_package_root(), "libwarpx.so")) +except OSError: + raise Exception('libwarp.so was not installed. It can be installed by running "make" in the Python directory of WarpX') + +_libc = ctypes.CDLL(_find_library('c')) dim = libwarpx.warpx_SpaceDim() # our particle data type -p_struct = [(d, 'f8') for d in 'xyz'[:dim]] + [('id', 'i4'), ('cpu', 'i4')] -p_dtype = np.dtype(p_struct, align=True) +_p_struct = [(d, 'f8') for d in 'xyz'[:dim]] + [('id', 'i4'), ('cpu', 'i4')] +_p_dtype = np.dtype(_p_struct, align=True) -numpy_to_ctypes = {} -numpy_to_ctypes['f8'] = ctypes.c_double -numpy_to_ctypes['i4'] = ctypes.c_int +_numpy_to_ctypes = {} +_numpy_to_ctypes['f8'] = ctypes.c_double +_numpy_to_ctypes['i4'] = ctypes.c_int class Particle(ctypes.Structure): - _fields_ = [(v[0], numpy_to_ctypes[v[1]]) for v in p_struct] + _fields_ = [(v[0], _numpy_to_ctypes[v[1]]) for v in _p_struct] # some useful typenames -LP_particle_p = ctypes.POINTER(ctypes.POINTER(Particle)) -LP_c_int = ctypes.POINTER(ctypes.c_int) -LP_c_void_p = ctypes.POINTER(ctypes.c_void_p) -LP_c_double = ctypes.POINTER(ctypes.c_double) -LP_LP_c_double = ctypes.POINTER(LP_c_double) -LP_c_char = ctypes.POINTER(ctypes.c_char) -LP_LP_c_char = ctypes.POINTER(LP_c_char) - -# from where do I import these? this might only work for CPython... -PyBuf_READ = 0x100 -PyBUF_WRITE = 0x200 +_LP_particle_p = ctypes.POINTER(ctypes.POINTER(Particle)) +_LP_c_int = ctypes.POINTER(ctypes.c_int) +_LP_c_void_p = ctypes.POINTER(ctypes.c_void_p) +_LP_c_double = ctypes.POINTER(ctypes.c_double) +_LP_LP_c_double = ctypes.POINTER(_LP_c_double) +_LP_c_char = ctypes.POINTER(ctypes.c_char) +_LP_LP_c_char = ctypes.POINTER(_LP_c_char) # this is a function for converting a ctypes pointer to a numpy array -def array1d_from_pointer(pointer, dtype, size): +def _array1d_from_pointer(pointer, dtype, size): if sys.version_info.major >= 3: + # from where do I import these? this might only work for CPython... + #PyBuf_READ = 0x100 + PyBUF_WRITE = 0x200 buffer_from_memory = ctypes.pythonapi.PyMemoryView_FromMemory buffer_from_memory.argtypes = (ctypes.c_void_p, ctypes.c_int, ctypes.c_int) buffer_from_memory.restype = ctypes.py_object @@ -68,89 +71,42 @@ def array1d_from_pointer(pointer, dtype, size): # set the arg and return types of the wrapped functions -f = libwarpx.amrex_init -f.argtypes = (ctypes.c_int, LP_LP_c_char) - -f = libwarpx.warpx_getParticleStructs -f.restype = LP_particle_p - -f = libwarpx.warpx_getParticleArrays -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getEfield -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getEfieldLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getEfieldCP -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getEfieldCPLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getEfieldFP -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getEfieldFPLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getBfield -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getBfieldLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getBfieldCP -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getBfieldCPLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getBfieldFP -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getBfieldFPLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getCurrentDensity -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getCurrentDensityLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getCurrentDensityCP -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getCurrentDensityCPLoVects -f.restype = LP_c_int - -f = libwarpx.warpx_getCurrentDensityFP -f.restype = LP_LP_c_double - -f = libwarpx.warpx_getCurrentDensityFPLoVects -f.restype = LP_c_int - -#f = libwarpx.warpx_getPMLSigma -#f.restype = LP_c_double -# -#f = libwarpx.warpx_getPMLSigmaStar -#f.restype = LP_c_double -# -#f = libwarpx.warpx_ComputePMLFactors -#f.argtypes = (ctypes.c_int, ctypes.c_double) - -f = libwarpx.warpx_addNParticles -f.argtypes = (ctypes.c_int, ctypes.c_int, - ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), - ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), - ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), - ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), - ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), - ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), - ctypes.c_int, - ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), - ctypes.c_int) +libwarpx.amrex_init.argtypes = (ctypes.c_int, _LP_LP_c_char) +libwarpx.warpx_getParticleStructs.restype = _LP_particle_p +libwarpx.warpx_getParticleArrays.restype = _LP_LP_c_double +libwarpx.warpx_getEfield.restype = _LP_LP_c_double +libwarpx.warpx_getEfieldLoVects.restype = _LP_c_int +libwarpx.warpx_getEfieldCP.restype = _LP_LP_c_double +libwarpx.warpx_getEfieldCPLoVects.restype = _LP_c_int +libwarpx.warpx_getEfieldFP.restype = _LP_LP_c_double +libwarpx.warpx_getEfieldFPLoVects.restype = _LP_c_int +libwarpx.warpx_getBfield.restype = _LP_LP_c_double +libwarpx.warpx_getBfieldLoVects.restype = _LP_c_int +libwarpx.warpx_getBfieldCP.restype = _LP_LP_c_double +libwarpx.warpx_getBfieldCPLoVects.restype = _LP_c_int +libwarpx.warpx_getBfieldFP.restype = _LP_LP_c_double +libwarpx.warpx_getBfieldFPLoVects.restype = _LP_c_int +libwarpx.warpx_getCurrentDensity.restype = _LP_LP_c_double +libwarpx.warpx_getCurrentDensityLoVects.restype = _LP_c_int +libwarpx.warpx_getCurrentDensityCP.restype = _LP_LP_c_double +libwarpx.warpx_getCurrentDensityCPLoVects.restype = _LP_c_int +libwarpx.warpx_getCurrentDensityFP.restype = _LP_LP_c_double +libwarpx.warpx_getCurrentDensityFPLoVects.restype = _LP_c_int + +#libwarpx.warpx_getPMLSigma.restype = _LP_c_double +#libwarpx.warpx_getPMLSigmaStar.restype = _LP_c_double +#libwarpx.warpx_ComputePMLFactors.argtypes = (ctypes.c_int, ctypes.c_double) + +libwarpx.warpx_addNParticles.argtypes = (ctypes.c_int, ctypes.c_int, + _ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), + _ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), + _ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), + _ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), + _ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), + _ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), + ctypes.c_int, + _ndpointer(ctypes.c_double, flags="C_CONTIGUOUS"), + ctypes.c_int) libwarpx.warpx_getProbLo.restype = ctypes.c_double libwarpx.warpx_getProbHi.restype = ctypes.c_double @@ -188,7 +144,7 @@ def get_nattr(): def amrex_init(argv): # --- Construct the ctype list of strings to pass in argc = len(argv) - argvC = (LP_c_char * (argc+1))() + argvC = (_LP_c_char * (argc+1))() for i, arg in enumerate(argv): enc_arg = arg.encode('utf-8') argvC[i] = ctypes.create_string_buffer(enc_arg) @@ -208,7 +164,7 @@ def initialize(argv=None): libwarpx.warpx_init() -def finalize(): +def finalize(finalize_mpi=1): ''' Call finalize for WarpX and AMReX. Must be called at @@ -216,7 +172,7 @@ def finalize(): ''' libwarpx.warpx_finalize() - libwarpx.amrex_finalize() + libwarpx.amrex_finalize(finalize_mpi) def evolve(num_steps=-1): @@ -360,7 +316,7 @@ def get_particle_structs(species_number): ''' - particles_per_tile = LP_c_int() + particles_per_tile = _LP_c_int() num_tiles = ctypes.c_int(0) data = libwarpx.warpx_getParticleStructs(species_number, ctypes.byref(num_tiles), @@ -368,11 +324,11 @@ def get_particle_structs(species_number): particle_data = [] for i in range(num_tiles.value): - arr = array1d_from_pointer(data[i], p_dtype, particles_per_tile[i]) + arr = _array1d_from_pointer(data[i], _p_dtype, particles_per_tile[i]) particle_data.append(arr) - libc.free(particles_per_tile) - libc.free(data) + _libc.free(particles_per_tile) + _libc.free(data) return particle_data @@ -398,7 +354,7 @@ def get_particle_arrays(species_number, comp): ''' - particles_per_tile = LP_c_int() + particles_per_tile = _LP_c_int() num_tiles = ctypes.c_int(0) data = libwarpx.warpx_getParticleArrays(species_number, comp, ctypes.byref(num_tiles), @@ -410,8 +366,8 @@ def get_particle_arrays(species_number, comp): arr.setflags(write=1) particle_data.append(arr) - libc.free(particles_per_tile) - libc.free(data) + _libc.free(particles_per_tile) + _libc.free(data) return particle_data @@ -607,7 +563,7 @@ def get_mesh_electric_field(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getEfield(level, direction, @@ -625,8 +581,8 @@ def get_mesh_electric_field(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -656,7 +612,7 @@ def get_mesh_electric_field_cp(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getEfieldCP(level, direction, @@ -674,8 +630,8 @@ def get_mesh_electric_field_cp(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -705,7 +661,7 @@ def get_mesh_electric_field_fp(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getEfieldFP(level, direction, @@ -723,8 +679,8 @@ def get_mesh_electric_field_fp(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -755,7 +711,7 @@ def get_mesh_magnetic_field(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getBfield(level, direction, @@ -773,8 +729,8 @@ def get_mesh_magnetic_field(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -804,7 +760,7 @@ def get_mesh_magnetic_field_cp(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getBfieldCP(level, direction, @@ -822,8 +778,8 @@ def get_mesh_magnetic_field_cp(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -853,7 +809,7 @@ def get_mesh_magnetic_field_fp(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getBfieldFP(level, direction, @@ -871,8 +827,8 @@ def get_mesh_magnetic_field_fp(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -901,7 +857,7 @@ def get_mesh_current_density(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getCurrentDensity(level, direction, @@ -919,8 +875,8 @@ def get_mesh_current_density(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -950,7 +906,7 @@ def get_mesh_current_density_cp(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getCurrentDensityCP(level, direction, @@ -968,8 +924,8 @@ def get_mesh_current_density_cp(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -999,7 +955,7 @@ def get_mesh_current_density_fp(level, direction, include_ghosts=True): assert(level == 0) - shapes = LP_c_int() + shapes = _LP_c_int() size = ctypes.c_int(0) ngrow = ctypes.c_int(0) data = libwarpx.warpx_getCurrentDensityFP(level, direction, @@ -1017,8 +973,8 @@ def get_mesh_current_density_fp(level, direction, include_ghosts=True): else: grid_data.append(arr[[slice(ng, -ng) for _ in range(dim)]]) - libc.free(shapes) - libc.free(data) + _libc.free(shapes) + _libc.free(data) return grid_data @@ -1039,7 +995,7 @@ def _get_mesh_array_lovects(level, direction, include_ghosts=True, getarrayfunc= lovects += ngrow.value del lovects_ref - libc.free(data) + _libc.free(data) return lovects @@ -1242,4 +1198,3 @@ def get_mesh_current_density_fp_lovects(level, direction, include_ghosts=True): ''' return _get_mesh_array_lovects(level, direction, include_ghosts, libwarpx.warpx_getCurrentDensityFPLoVects) - diff --git a/Python/setup.py b/Python/setup.py index 71f82dbc7..8d4c5cd12 100644 --- a/Python/setup.py +++ b/Python/setup.py @@ -4,18 +4,30 @@ setup.py file for WarpX """ +import sys +import argparse + from distutils.core import setup +argparser = argparse.ArgumentParser(add_help=False) +argparser.add_argument('--with-libwarpx', action='store_true', help='Install libwarpx. This option is only used by the makefile.') +args, unknown = argparser.parse_known_args() +sys.argv = [sys.argv[0]] + unknown + try: from distutils.command.build_py import build_py_2to3 as build_py except ImportError: from distutils.command.build_py import build_py +if args.with_libwarpx: + package_data = {'pywarpx' : ['libwarpx.so']} +else: + package_data = {} setup (name = 'pywarpx', packages = ['pywarpx'], package_dir = {'pywarpx':'pywarpx'}, description = """Wrapper of WarpX""", - package_data = {'pywarpx' : ['libwarpx.so']}, + package_data = package_data, cmdclass={'build_py': build_py} ) |