From 4710b5b5d7d85d0d674bed830571843bbd3aab3d Mon Sep 17 00:00:00 2001 From: saisuraj27 Date: Sun, 17 Dec 2023 23:46:38 +0530 Subject: [PATCH] Fixed pre-commit errors and updated black version. --- .pre-commit-config.yaml | 2 +- README.rst | 2 +- docs/source/input_hash_cmds.rst | 4 +- gprMax/cmds_geometry/fractal_box.py | 77 +++++++++++++++++++++-------- gprMax/config.py | 4 +- gprMax/contexts.py | 17 ++++--- gprMax/geometry_outputs.py | 4 +- gprMax/model_build_run.py | 16 +++--- gprMax/scene.py | 16 +++--- gprMax/utilities/utilities.py | 2 +- 10 files changed, 92 insertions(+), 52 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 335a04ba..cc2e21ea 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,7 +9,7 @@ repos: - id: check-added-large-files - id: check-toml - repo: https://github.com/psf/black-pre-commit-mirror - rev: 23.11.0 + rev: 23.12.0 hooks: - id: black args: ["--line-length", "120"] # Adjust the max line length value as needed. diff --git a/README.rst b/README.rst index 4a96670f..9f3355e4 100644 --- a/README.rst +++ b/README.rst @@ -210,4 +210,4 @@ Periodically you should update conda and the required Python packages. With the Thanks To Our Contributors ✨🔗 ========================== .. image:: https://contrib.rocks/image?repo=gprMax/gprMax - :target: https://github.com/gprMax/gprMax/graphs/contributors \ No newline at end of file + :target: https://github.com/gprMax/gprMax/graphs/contributors diff --git a/docs/source/input_hash_cmds.rst b/docs/source/input_hash_cmds.rst index 3a901427..5fc551ef 100644 --- a/docs/source/input_hash_cmds.rst +++ b/docs/source/input_hash_cmds.rst @@ -767,11 +767,11 @@ For example, to specify the normalised first derivative of a Gaussian waveform w Allows you to specify an ASCII file that contains amplitude values that specify custom waveform(s) that can be used with sources in the model. -The first row of each column must begin with a identifier string that will be used as the name of each waveform. Subsequent rows should contain amplitude values for the custom waveform you want to use. You can import multiple different waveforms (as columns of amplitude data) in a single file. +The first row of each column must begin with a identifier string that will be used as the name of each waveform. Subsequent rows should contain amplitude values for the custom waveform you want to use. You can import multiple different waveforms (as columns of amplitude data) in a single file. Ideally, there should be the same number of amplitude values as number of iterations in your model. If there are less amplitude values than the number of iterations in the model, the end of the sequence of amplitude values will be padded with zero values up to the number of iterations. If extra amplitude values are specified than needed then they are ignored. -Optionally, in the first column of the file you may specify your own time vector of values (which must use the identifier ``time``) to use with the amplitude values of the waveform. +Optionally, in the first column of the file you may specify your own time vector of values (which must use the identifier ``time``) to use with the amplitude values of the waveform. The amplitude values will be interpolated using either the aforementioned user specified time vector, or if none was supplied, a vector of time values corresponding to the simulation time step and number of iterations will be used. Key parameters used for the interpolation can be specified in the command. diff --git a/gprMax/cmds_geometry/fractal_box.py b/gprMax/cmds_geometry/fractal_box.py index b60960a0..2a4c5100 100644 --- a/gprMax/cmds_geometry/fractal_box.py +++ b/gprMax/cmds_geometry/fractal_box.py @@ -21,11 +21,11 @@ import logging import numpy as np import gprMax.config as config - +from gprMax.cmds_geometry.cmds_geometry import UserObjectGeometry, rotate_2point_object from gprMax.fractals import FractalVolume from gprMax.materials import ListMaterial + from ..cython.geometry_primitives import build_voxels_from_array, build_voxels_from_array_mask -from gprMax.cmds_geometry.cmds_geometry import UserObjectGeometry, rotate_2point_object logger = logging.getLogger(__name__) @@ -172,7 +172,7 @@ class FractalBox(UserObjectGeometry): f"is {dielectricsmoothing}." ) grid.fractalvolumes.append(self.volume) - + def build(self, grid, uip): if self.do_pre_build: self.pre_build(grid, uip) @@ -218,7 +218,8 @@ class FractalBox(UserObjectGeometry): # otherwise a mixing model if self.volume.nbins == 1: self.volume.fractalvolume = np.ones( - (self.volume.nx, self.volume.ny, self.volume.nz), dtype=config.sim_config.dtypes["float_or_double"] + (self.volume.nx, self.volume.ny, self.volume.nz), + dtype=config.sim_config.dtypes["float_or_double"], ) materialnumID = next(x.numID for x in grid.materials if x.ID == self.volume.operatingonID) self.volume.fractalvolume *= materialnumID @@ -253,11 +254,17 @@ class FractalBox(UserObjectGeometry): for j in range(surface.ys, surface.yf): for k in range(surface.zs, surface.zf): if i < surface.fractalsurface[j - surface.ys, k - surface.zs]: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 1 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 1 elif surface.filldepth > 0 and i < surface.filldepth: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 2 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 2 else: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 0 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 0 elif surface.ID == "grass": g = surface.grass[0] # Build the blades of the grass @@ -269,7 +276,10 @@ class FractalBox(UserObjectGeometry): for i in range(self.volume.xs, surface.fractalrange[1]): if ( i < surface.fractalsurface[j - surface.ys, k - surface.zs] - and self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] != 1 + and self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] + != 1 ): y, z = g.calculate_blade_geometry(blade, height) # Add y, z coordinates to existing location @@ -304,7 +314,10 @@ class FractalBox(UserObjectGeometry): surface.fractalsurface[j - surface.ys, k - surface.zs] - self.volume.originalxf ) - and self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] == 1 + and self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] + == 1 ): y, z = g.calculate_root_geometry(root, depth) # Add y, z coordinates to existing location @@ -342,11 +355,17 @@ class FractalBox(UserObjectGeometry): for j in range(surface.fractalrange[0], surface.fractalrange[1]): for k in range(surface.zs, surface.zf): if j < surface.fractalsurface[i - surface.xs, k - surface.zs]: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 1 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 1 elif surface.filldepth > 0 and j < surface.filldepth: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 2 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 2 else: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 0 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 0 elif surface.ID == "grass": g = surface.grass[0] # Build the blades of the grass @@ -358,7 +377,10 @@ class FractalBox(UserObjectGeometry): for j in range(self.volume.ys, surface.fractalrange[1]): if ( j < surface.fractalsurface[i - surface.xs, k - surface.zs] - and self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] != 1 + and self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] + != 1 ): x, z = g.calculate_blade_geometry(blade, height) # Add x, z coordinates to existing location @@ -393,7 +415,10 @@ class FractalBox(UserObjectGeometry): surface.fractalsurface[i - surface.xs, k - surface.zs] - self.volume.originalyf ) - and self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] == 1 + and self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] + == 1 ): x, z = g.calculate_root_geometry(root, depth) # Add x, z coordinates to existing location @@ -431,11 +456,17 @@ class FractalBox(UserObjectGeometry): for j in range(surface.ys, surface.yf): for k in range(surface.fractalrange[0], surface.fractalrange[1]): if k < surface.fractalsurface[i - surface.xs, j - surface.ys]: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 1 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 1 elif surface.filldepth > 0 and k < surface.filldepth: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 2 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 2 else: - self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] = 0 + self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] = 0 elif surface.ID == "grass": g = surface.grass[0] # Build the blades of the grass @@ -447,7 +478,10 @@ class FractalBox(UserObjectGeometry): for k in range(self.volume.zs, surface.fractalrange[1]): if ( k < surface.fractalsurface[i - surface.xs, j - surface.ys] - and self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] != 1 + and self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] + != 1 ): x, y = g.calculate_blade_geometry(blade, height) # Add x, y coordinates to existing location @@ -482,7 +516,10 @@ class FractalBox(UserObjectGeometry): surface.fractalsurface[i - surface.xs, j - surface.ys] - self.volume.originalzf ) - and self.volume.mask[i - self.volume.xs, j - self.volume.ys, k - self.volume.zs] == 1 + and self.volume.mask[ + i - self.volume.xs, j - self.volume.ys, k - self.volume.zs + ] + == 1 ): x, y = g.calculate_root_geometry(root, depth) # Add x, y coordinates to existing location @@ -553,4 +590,4 @@ class FractalBox(UserObjectGeometry): grid.rigidE, grid.rigidH, grid.ID, - ) \ No newline at end of file + ) diff --git a/gprMax/config.py b/gprMax/config.py index 7891c898..1cdaefae 100644 --- a/gprMax/config.py +++ b/gprMax/config.py @@ -90,9 +90,7 @@ class ModelConfig: except: deviceID = 0 - self.device = {"dev": sim_config.set_model_device(deviceID), - "deviceID": deviceID, - "snapsgpu2cpu": False} + self.device = {"dev": sim_config.set_model_device(deviceID), "deviceID": deviceID, "snapsgpu2cpu": False} # Total memory usage for all grids in the model. Starts with 50MB overhead. self.mem_overhead = 65e6 diff --git a/gprMax/contexts.py b/gprMax/contexts.py index f78877a2..4e389259 100644 --- a/gprMax/contexts.py +++ b/gprMax/contexts.py @@ -23,6 +23,7 @@ import sys import humanize from colorama import Fore, Style, init + init() import gprMax.config as config @@ -87,10 +88,10 @@ class Context: del solver, model if not config.sim_config.args.geometry_fixed: - # Manual garbage collection required to stop memory leak on GPUs + # Manual garbage collection required to stop memory leak on GPUs # when using pycuda del G - + gc.collect() self.tsimend = timer() @@ -141,9 +142,11 @@ class MPIContext(Context): model_config = config.ModelConfig() # Set GPU deviceID according to worker rank if config.sim_config.general["solver"] == "cuda": - model_config.device = {"dev": config.sim_config.devices["devs"][self.rank - 1], - "deviceID": self.rank - 1, - "snapsgpu2cpu": False} + model_config.device = { + "dev": config.sim_config.devices["devs"][self.rank - 1], + "deviceID": self.rank - 1, + "snapsgpu2cpu": False, + } config.model_configs = model_config G = create_G() @@ -156,7 +159,7 @@ class MPIContext(Context): del solver, model # Manual garbage collection required to stop memory leak on GPUs when - # using pycuda + # using pycuda del G gc.collect() @@ -178,7 +181,7 @@ class MPIContext(Context): s = f"\n--- Input file: {config.sim_config.input_file_path}" logger.basic(Fore.GREEN + f"{s} {'-' * (get_terminal_width() - 1 - len(s))}\n" + Style.RESET_ALL) - + sys.stdout.flush() # Contruct MPIExecutor diff --git a/gprMax/geometry_outputs.py b/gprMax/geometry_outputs.py index bce4fc0e..d8973c9c 100644 --- a/gprMax/geometry_outputs.py +++ b/gprMax/geometry_outputs.py @@ -96,9 +96,7 @@ class GeometryView: def set_filename(self): """Constructs filename from user-supplied name and model run number.""" parts = config.get_model_config().output_file_path.parts - self.filename = Path(*parts[:-1], - self.filenamebase + - config.get_model_config().appendmodelnumber) + self.filename = Path(*parts[:-1], self.filenamebase + config.get_model_config().appendmodelnumber) class GeometryViewLines(GeometryView): diff --git a/gprMax/model_build_run.py b/gprMax/model_build_run.py index 4d4270dc..6f24dc94 100644 --- a/gprMax/model_build_run.py +++ b/gprMax/model_build_run.py @@ -58,7 +58,7 @@ class ModelBuildRun: # Set number of OpenMP threads to physical threads at this point to be # used with threaded model building methods, e.g. fractals. Can be - # changed by the user via #num_threads command in input file or via API + # changed by the user via #num_threads command in input file or via API # later for use with CPU solver. config.get_model_config().ompthreads = set_omp_threads(config.get_model_config().ompthreads) @@ -309,14 +309,18 @@ class ModelBuildRun: if config.sim_config.general["solver"] == "opencl": solvername = "OpenCL" platformname = " on " + " ".join(config.get_model_config().device["dev"].platform.name.split()) - devicename = (f'Device {config.get_model_config().device["deviceID"]}: ' - f'{" ".join(config.get_model_config().device["dev"].name.split())}') + devicename = ( + f'Device {config.get_model_config().device["deviceID"]}: ' + f'{" ".join(config.get_model_config().device["dev"].name.split())}' + ) else: solvername = "CUDA" platformname = "" - devicename = (f'Device {config.get_model_config().device["deviceID"]}: ' - f'{" ".join(config.get_model_config().device["dev"].name().split())}') - + devicename = ( + f'Device {config.get_model_config().device["deviceID"]}: ' + f'{" ".join(config.get_model_config().device["dev"].name().split())}' + ) + logger.basic( f"\nModel {config.model_num + 1}/{config.sim_config.model_end} " f"solving on {config.sim_config.hostinfo['hostname']} " diff --git a/gprMax/scene.py b/gprMax/scene.py index a4b86fa5..06fdd899 100644 --- a/gprMax/scene.py +++ b/gprMax/scene.py @@ -18,11 +18,11 @@ import logging -from gprMax.cmds_geometry.cmds_geometry import UserObjectGeometry -from gprMax.cmds_geometry.fractal_box import FractalBox from gprMax.cmds_geometry.add_grass import AddGrass from gprMax.cmds_geometry.add_surface_roughness import AddSurfaceRoughness from gprMax.cmds_geometry.add_surface_water import AddSurfaceWater +from gprMax.cmds_geometry.cmds_geometry import UserObjectGeometry +from gprMax.cmds_geometry.fractal_box import FractalBox from gprMax.cmds_multiuse import UserObjectMulti from gprMax.cmds_singleuse import Discretisation, Domain, TimeWindow, UserObjectSingle from gprMax.materials import create_built_in_materials @@ -57,10 +57,10 @@ class Scene: else: logger.exception("This object is unknown to gprMax") raise ValueError - + def build_obj(self, obj, grid): """Builds objects. - + Args: obj: user object grid: FDTDGrid class describing a grid in a model. @@ -71,9 +71,10 @@ class Scene: except ValueError: logger.exception("Error creating user input object") raise - + def process_subgrid_cmds(self): """Process all commands in any sub-grids.""" + def func(obj): if isinstance(obj, SubGridUserBase): return True @@ -99,13 +100,12 @@ class Scene: self.build_obj(obj, grid) return self - + def process_geocmds(self, commands, grid): # Check for fractal boxes and modifications and pre-process them first proc_cmds = [] for obj in commands: - if isinstance(obj, (FractalBox, AddGrass, - AddSurfaceRoughness, AddSurfaceWater)): + if isinstance(obj, (FractalBox, AddGrass, AddSurfaceRoughness, AddSurfaceWater)): self.build_obj(obj, grid) if isinstance(obj, (FractalBox)): proc_cmds.append(obj) diff --git a/gprMax/utilities/utilities.py b/gprMax/utilities/utilities.py index 978bf5c1..6be0614f 100644 --- a/gprMax/utilities/utilities.py +++ b/gprMax/utilities/utilities.py @@ -183,4 +183,4 @@ def fft_power(waveform, dt): def timer(): """Time in fractional seconds.""" - return timer_fn() \ No newline at end of file + return timer_fn()