From c0762cc112075b65ee640eee5105fdfc03badee5 Mon Sep 17 00:00:00 2001 From: Sai-Suraj-27 Date: Fri, 23 Jun 2023 19:35:54 +0530 Subject: [PATCH] Removed unnecessary if conditions at some places, and used itertools.product to make code more readable/cleaner. --- gprMax/cmds_geometry/add_grass.py | 13 ++--- gprMax/cmds_geometry/add_surface_roughness.py | 11 ++-- gprMax/cmds_geometry/fractal_box.py | 11 ++-- testing/benchmarking/bench_simple.py | 54 +++++++++---------- 4 files changed, 41 insertions(+), 48 deletions(-) diff --git a/gprMax/cmds_geometry/add_grass.py b/gprMax/cmds_geometry/add_grass.py index 43b80f25..cedfeab8 100644 --- a/gprMax/cmds_geometry/add_grass.py +++ b/gprMax/cmds_geometry/add_grass.py @@ -111,7 +111,7 @@ class AddGrass(UserObjectGeometry): if ys == yf or zs == zf: logger.exception(f'{self.__str__()} dimensions are not specified correctly') raise ValueError - if xs != volume.xs and xs != volume.xf: + if xs not in [volume.xs and volume.xf]: logger.exception(f'{self.__str__()} must specify external surfaces on a fractal box') raise ValueError fractalrange = (round_value(limits[0] / grid.dx), round_value(limits[1] / grid.dx)) @@ -130,10 +130,10 @@ class AddGrass(UserObjectGeometry): requestedsurface = 'xplus' elif ys == yf: - if xs == xf or zs == zf: + if zs == zf: logger.exception(f'{self.__str__()} dimensions are not specified correctly') raise ValueError - if ys != volume.ys and ys != volume.yf: + if ys not in [volume.ys and volume.yf]: logger.exception(f'{self.__str__()} must specify external surfaces on a fractal box') raise ValueError fractalrange = (round_value(limits[0] / grid.dy), round_value(limits[1] / grid.dy)) @@ -152,10 +152,7 @@ class AddGrass(UserObjectGeometry): requestedsurface = 'yplus' elif zs == zf: - if xs == xf or ys == yf: - logger.exception(f'{self.__str__()} dimensions are not specified correctly') - raise ValueError - if zs != volume.zs and zs != volume.zf: + if zs not in [volume.zs and volume.zf]: logger.exception(f'{self.__str__()} must specify external surfaces on a fractal box') raise ValueError fractalrange = (round_value(limits[0] / grid.dz), round_value(limits[1] / grid.dz)) @@ -227,7 +224,7 @@ class AddGrass(UserObjectGeometry): surface.grass.append(g) # Check to see if grass has been already defined as a material - if not any(x.ID == 'grass' for x in grid.materials): + if all(x.ID == 'grass' for x in grid.materials): create_grass(grid) # Check if time step for model is suitable for using grass diff --git a/gprMax/cmds_geometry/add_surface_roughness.py b/gprMax/cmds_geometry/add_surface_roughness.py index df4b02da..a822696e 100644 --- a/gprMax/cmds_geometry/add_surface_roughness.py +++ b/gprMax/cmds_geometry/add_surface_roughness.py @@ -118,7 +118,7 @@ class AddSurfaceRoughness(UserObjectGeometry): if ys == yf or zs == zf: logger.exception(f'{self.__str__()} dimensions are not specified correctly') raise ValueError - if xs != volume.xs and xs != volume.xf: + if xs not in [volume.xs, volume.xf]: logger.exception(f'{self.__str__()} can only be used on the external ' + 'surfaces of a fractal box') raise ValueError @@ -144,10 +144,10 @@ class AddSurfaceRoughness(UserObjectGeometry): requestedsurface = 'xplus' elif ys == yf: - if xs == xf or zs == zf: + if zs == zf: logger.exception(f'{self.__str__()} dimensions are not specified correctly') raise ValueError - if ys != volume.ys and ys != volume.yf: + if ys not in [volume.ys and volume.yf]: logger.exception(f'{self.__str__()} can only be used on the external ' + 'surfaces of a fractal box') raise ValueError @@ -173,10 +173,7 @@ class AddSurfaceRoughness(UserObjectGeometry): requestedsurface = 'yplus' elif zs == zf: - if xs == xf or ys == yf: - logger.exception(f'{self.__str__()} dimensions are not specified correctly') - raise ValueError - if zs != volume.zs and zs != volume.zf: + if zs not in [volume.zs and volume.zf]: logger.exception(f'{self.__str__()} can only be used on the external ' + 'surfaces of a fractal box') raise ValueError diff --git a/gprMax/cmds_geometry/fractal_box.py b/gprMax/cmds_geometry/fractal_box.py index 5e6cc598..1ae6a376 100644 --- a/gprMax/cmds_geometry/fractal_box.py +++ b/gprMax/cmds_geometry/fractal_box.py @@ -140,12 +140,11 @@ class FractalBox(UserObjectGeometry): logger.exception(f'{self.__str__()} must be used with more than ' + 'one material from the mixing model.') raise ValueError - if isinstance(mixingmodel, ListMaterial): - if nbins > len(mixingmodel.mat): - logger.exception(f'{self.__str__()} too many materials/bins ' + - 'requested compared to materials in ' + - 'mixing model.') - raise ValueError + if isinstance(mixingmodel, ListMaterial) and nbins > len(mixingmodel.mat): + logger.exception(f'{self.__str__()} too many materials/bins ' + + 'requested compared to materials in ' + + 'mixing model.') + raise ValueError # Create materials from mixing model as number of bins now known # from fractal_box command. mixingmodel.calculate_properties(nbins, grid) diff --git a/testing/benchmarking/bench_simple.py b/testing/benchmarking/bench_simple.py index 9688ca56..6c82c016 100644 --- a/testing/benchmarking/bench_simple.py +++ b/testing/benchmarking/bench_simple.py @@ -3,9 +3,11 @@ receiver at the centre. """ + from pathlib import Path import gprMax +import itertools # File path for output fn = Path(__file__) @@ -17,38 +19,36 @@ ompthreads = [1, 2, 4, 8, 16, 32, 64, 128] scenes = [] -for d in domains: - for threads in ompthreads: +# Discretisation +dl = 0.001 - # Discretisation - dl = 0.001 +for d, threads in itertools.product(domains, ompthreads): + # Domain + x = d + y = x + z = x - # Domain - x = d - y = x - z = x + scene = gprMax.Scene() - scene = gprMax.Scene() + title = gprMax.Title(name=fn.with_suffix('').name) + domain = gprMax.Domain(p1=(x, y, z)) + dxdydz = gprMax.Discretisation(p1=(dl, dl, dl)) + time_window = gprMax.TimeWindow(time=3e-9) + wv = gprMax.Waveform(wave_type='gaussiandotnorm', amp=1, freq=900e6, + id='MySource') + src = gprMax.HertzianDipole(p1=(x/2, y/2, z/2), polarisation='x', + waveform_id='MySource') - title = gprMax.Title(name=fn.with_suffix('').name) - domain = gprMax.Domain(p1=(x, y, z)) - dxdydz = gprMax.Discretisation(p1=(dl, dl, dl)) - time_window = gprMax.TimeWindow(time=3e-9) - wv = gprMax.Waveform(wave_type='gaussiandotnorm', amp=1, freq=900e6, - id='MySource') - src = gprMax.HertzianDipole(p1=(x/2, y/2, z/2), polarisation='x', - waveform_id='MySource') + omp = gprMax.OMPThreads(n=threads) - omp = gprMax.OMPThreads(n=threads) - - scene.add(title) - scene.add(domain) - scene.add(dxdydz) - scene.add(time_window) - scene.add(wv) - scene.add(src) - scene.add(omp) - scenes.append(scene) + scene.add(title) + scene.add(domain) + scene.add(dxdydz) + scene.add(time_window) + scene.add(wv) + scene.add(src) + scene.add(omp) + scenes.append(scene) # Run model gprMax.run(scenes=scenes, n=len(scenes), geometry_only=False, outputfile=fn, gpu=None) \ No newline at end of file