From 0b66c78849b4219e760fcec7325927c4d5153ba6 Mon Sep 17 00:00:00 2001 From: jasminium Date: Thu, 12 May 2016 11:11:06 +0100 Subject: [PATCH] install gprMax as a module --- gprMax/__init__.py | 5 +++- gprMax/gprMax.py | 66 +++++++++++++++++++++++++++++++------------ gprMax/writer_hdf5.py | 20 ++++++------- setup.py | 37 +++++++++++++----------- 4 files changed, 81 insertions(+), 47 deletions(-) diff --git a/gprMax/__init__.py b/gprMax/__init__.py index 152d4e97..e4904dc4 100644 --- a/gprMax/__init__.py +++ b/gprMax/__init__.py @@ -1 +1,4 @@ -from gprMax._version import __version__ \ No newline at end of file +from ._version import __version__ +from .gprMax import api as run + +__name__ = 'gprMax' diff --git a/gprMax/gprMax.py b/gprMax/gprMax.py index e046767f..4a64158d 100644 --- a/gprMax/gprMax.py +++ b/gprMax/gprMax.py @@ -18,33 +18,38 @@ """gprMax.gprMax: provides entry point main().""" -import argparse, datetime, itertools, os, psutil, sys -from time import perf_counter -from enum import Enum +import argparse +import datetime +import itertools +import os +import psutil +import sys import numpy as np -import gprMax -from gprMax.constants import c, e0, m0, z0, floattype -from gprMax.exceptions import GeneralError -from gprMax.fields_update import update_electric, update_magnetic, update_electric_dispersive_multipole_A, update_electric_dispersive_multipole_B, update_electric_dispersive_1pole_A, update_electric_dispersive_1pole_B -from gprMax.grid import FDTDGrid, dispersion_check -from gprMax.input_cmds_geometry import process_geometrycmds -from gprMax.input_cmds_file import process_python_include_code, write_processed_file, check_cmd_names -from gprMax.input_cmds_multiuse import process_multicmds -from gprMax.input_cmds_singleuse import process_singlecmds -from gprMax.materials import Material -from gprMax.writer_hdf5 import prepare_hdf5, write_hdf5 -from gprMax.pml import build_pmls, update_electric_pml, update_magnetic_pml -from gprMax.utilities import update_progress, logo, human_size -from gprMax.yee_cell_build import build_electric_components, build_magnetic_components +from time import perf_counter +from enum import Enum +from ._version import __version__ +from .constants import c, e0, m0, z0 +from .exceptions import GeneralError +from .fields_update import update_electric, update_magnetic, update_electric_dispersive_multipole_A, update_electric_dispersive_multipole_B, update_electric_dispersive_1pole_A, update_electric_dispersive_1pole_B +from .grid import FDTDGrid, dispersion_check +from .input_cmds_geometry import process_geometrycmds +from .input_cmds_file import process_python_include_code, write_processed_file, check_cmd_names +from .input_cmds_multiuse import process_multicmds +from .input_cmds_singleuse import process_singlecmds +from .materials import Material +from .writer_hdf5 import prepare_hdf5, write_hdf5 +from .pml import build_pmls, update_electric_pml, update_magnetic_pml +from .utilities import update_progress, logo, human_size +from .yee_cell_build import build_electric_components, build_magnetic_components def main(): """This is the main function for gprMax.""" # Print gprMax logo, version, and licencing/copyright information - logo(gprMax.__version__ + ' (Bowmore)') + logo(__version__ + ' (Bowmore)') # Parse command line arguments parser = argparse.ArgumentParser(prog='gprMax', description='Electromagnetic modelling software based on the Finite-Difference Time-Domain (FDTD) method') @@ -57,6 +62,31 @@ def main(): parser.add_argument('--write-processed', action='store_true', default=False, help='write an input file after any Python code and include commands in the original input file have been processed') parser.add_argument('--opt-taguchi', action='store_true', default=False, help='optimise parameters using the Taguchi optimisation method') args = parser.parse_args() + + run_main(args) + + +def api(inputfile, n=1, mpi=False, benchmark=False, geometry_only=False, geometry_fixed=False, write_processed=False, opt_taguchi=False): + """If you have installed gprMax as a module this is the entry point""" + class ImportArguments: + pass + + args = ImportArguments() + + args.inputfile = inputfile + args.n = n + args.mpi = mpi + args.benchmark = benchmark + args.geometry_only = geometry_only + args.geometry_fixed = geometry_fixed + args.write_processed = write_processed + args.opt_taguchi = opt_taguchi + + run_main(args) + + +def run_main(args): + numbermodelruns = args.n inputdirectory = os.path.dirname(os.path.abspath(args.inputfile)) inputfile = os.path.abspath(os.path.join(inputdirectory, os.path.basename(args.inputfile))) diff --git a/gprMax/writer_hdf5.py b/gprMax/writer_hdf5.py index 8f85b65e..67fdb233 100644 --- a/gprMax/writer_hdf5.py +++ b/gprMax/writer_hdf5.py @@ -17,26 +17,25 @@ # along with gprMax. If not, see . import h5py -import numpy as np -import gprMax +from ._version import __version__ from gprMax.constants import floattype from gprMax.grid import Ix, Iy, Iz def prepare_hdf5(outputfile, G): """Prepares an output file in HDF5 format for writing. - + Args: outputfile (str): Name of the output file. G (class): Grid class instance - holds essential parameters describing the model. - + Returns: - f (file object): File object for the file to be written to. + f (file object): File object for the file to be written to. """ f = h5py.File(outputfile, 'w') - f.attrs['gprMax'] = gprMax.__version__ + f.attrs['gprMax'] = __version__ f.attrs['Title'] = G.title f.attrs['Iterations'] = G.iterations f.attrs['nx, ny, nz'] = (G.nx, G.ny, G.nz) @@ -47,7 +46,7 @@ def prepare_hdf5(outputfile, G): f.attrs['nrx'] = len(G.rxs) f.attrs['srcsteps'] = (G.srcstepx, G.srcstepy, G.srcstepz) f.attrs['rxsteps'] = (G.rxstepx, G.rxstepy, G.rxstepz) - + # Create group for sources (except transmission lines); add type and positional data attributes srclist = G.voltagesources + G.hertziandipoles + G.magneticdipoles @@ -55,7 +54,7 @@ def prepare_hdf5(outputfile, G): grp = f.create_group('/srcs/src' + str(srcindex + 1)) grp.attrs['Type'] = type(src).__name__ grp.attrs['Position'] = (src.xcoord * G.dx, src.ycoord * G.dy, src.zcoord * G.dz) - + # Create group for transmission lines; add positional data, line resistance and line discretisation attributes; initialise arrays for line voltages and currents if G.transmissionlines: for tlindex, tl in enumerate(G.transmissionlines): @@ -68,7 +67,7 @@ def prepare_hdf5(outputfile, G): grp['Iinc'] = tl.Iinc grp.create_dataset('Vtotal', (G.iterations, ), dtype=floattype) grp.create_dataset('Itotal', (G.iterations, ), dtype=floattype) - + # Create group and add positional data and initialise field component arrays for receivers for rxindex, rx in enumerate(G.rxs): grp = f.create_group('/rxs/rx' + str(rxindex + 1)) @@ -83,7 +82,7 @@ def prepare_hdf5(outputfile, G): def write_hdf5(f, timestep, Ex, Ey, Ez, Hx, Hy, Hz, G): """Writes field component values to an output file in HDF5 format. - + Args: f (file object): File object for the file to be written to. timestep (int): Current iteration number. @@ -116,4 +115,3 @@ def write_hdf5(f, timestep, Ex, Ey, Ez, Hx, Hy, Hz, G): for tlindex, tl in enumerate(G.transmissionlines): f['/tls/tl' + str(tlindex + 1) + '/Vtotal'][timestep] = tl.voltage[tl.antpos] f['/tls/tl' + str(tlindex + 1) + '/Itotal'][timestep] = tl.current[tl.antpos] - diff --git a/setup.py b/setup.py index 0b43c2b3..3bbada2e 100644 --- a/setup.py +++ b/setup.py @@ -27,28 +27,30 @@ try: except ImportError: raise ImportError('gprMax requires the NumPy package.') -import glob, os, shutil, sys +import glob +import os +import shutil +import sys +import re + +# Importing _version__.py before building can cause issues. +with open('gprMax/_version.py', 'r') as fd: + version = re.search(r'^__version__\s*=\s*[\'"]([^\'"]*)[\'"]', + fd.read(), re.MULTILINE).group(1) + +""" + Parse package name from init file. Importing __init__.py / gprMax will + break as gprMax depends on compiled .pyx files. +""" +with open('gprMax/__init__.py', 'r') as fd: + packagename = re.search(r'^__name__\s*=\s*[\'"]([^\'"]*)[\'"]', + fd.read(), re.MULTILINE).group(1) # Python version if sys.version_info[:2] < (3, 4): print('gprMax requires Python 3.4 or newer') sys.exit(-1) -import gprMax - -# Package name -packagename = gprMax.__name__ - -# Package version -version = gprMax.__version__ - -# Process 'install' command line argument -if 'install' in sys.argv: - print("'install' is not required for this package, running 'build_ext --inplace' instead.") - sys.argv.remove('install') - sys.argv.append('build_ext') - sys.argv.append('--inplace') - # Process 'build' command line argument if 'build' in sys.argv: print("'build' is not required for this package, running 'build_ext --inplace' instead.") @@ -118,7 +120,7 @@ elif sys.platform == 'darwin': compile_args = ['-O3', '-w', '-fstrict-aliasing', '-fno-common', '-fopenmp'] linker_args = ['-fopenmp'] extra_objects = [] - + # If user is cwarren do static linking as this is for binaries uploaded to GitHub # if os.getlogin() == 'cwarren': # linker_args = ['-static-libgcc'] @@ -176,4 +178,5 @@ setup(name=packagename, 'Topic :: Scientific/Engineering' ], ext_modules=extensions, + packages=['gprMax'], include_dirs=[np.get_include()])