From 9ba6978b0d159061d9d4db136464249757c57a88 Mon Sep 17 00:00:00 2001 From: Tobias Hienzsch Date: Tue, 3 Sep 2024 04:54:19 +0200 Subject: [PATCH] [python] Add geometry module --- data/models/Diffusor/Diffusor.py | 2 +- data/models/Modes2D/Modes2D.py | 2 +- src/python/pffdtd/absorption/air.py | 2 +- .../pffdtd/absorption/air_abs_filters.py | 2 +- src/python/pffdtd/analysis/response.py | 2 +- src/python/pffdtd/analysis/room_modes.py | 2 +- src/python/pffdtd/common/check_version.py | 9 --- src/python/pffdtd/common/timer.py | 71 ------------------- src/python/pffdtd/geometry/__init__.py | 0 src/python/pffdtd/{common => geometry}/box.py | 2 +- .../{common/myfuncs.py => geometry/math.py} | 0 .../tri_box_intersection.py | 6 +- .../tri_ray_intersection.py | 4 +- .../{common => geometry}/tris_precompute.py | 2 +- src/python/pffdtd/sim2d/setup.py | 2 +- src/python/pffdtd/sim3d/engine.py | 2 +- src/python/pffdtd/sim3d/process_outputs.py | 2 +- src/python/pffdtd/sim3d/room_geometry.py | 4 +- src/python/pffdtd/sim3d/rotate.py | 2 +- src/python/pffdtd/sim3d/sim_signals.py | 2 +- src/python/pffdtd/voxelizer/vox_grid.py | 2 +- src/python/pffdtd/voxelizer/vox_grid_base.py | 6 +- src/python/pffdtd/voxelizer/vox_scene.py | 10 +-- src/python/test/test_sim2d_modes.py | 2 +- src/python/test/test_sim3d_infinite_baffle.py | 4 +- .../test/test_sim3d_locate_sound_source.py | 2 +- 26 files changed, 33 insertions(+), 113 deletions(-) delete mode 100644 src/python/pffdtd/common/check_version.py delete mode 100644 src/python/pffdtd/common/timer.py create mode 100644 src/python/pffdtd/geometry/__init__.py rename src/python/pffdtd/{common => geometry}/box.py (98%) rename src/python/pffdtd/{common/myfuncs.py => geometry/math.py} (100%) rename src/python/pffdtd/{common => geometry}/tri_box_intersection.py (97%) rename src/python/pffdtd/{common => geometry}/tri_ray_intersection.py (98%) rename src/python/pffdtd/{common => geometry}/tris_precompute.py (98%) diff --git a/data/models/Diffusor/Diffusor.py b/data/models/Diffusor/Diffusor.py index 8f8a93e..ff8e7d8 100644 --- a/data/models/Diffusor/Diffusor.py +++ b/data/models/Diffusor/Diffusor.py @@ -1,6 +1,6 @@ import numpy as np -from pffdtd.common.myfuncs import to_ixy, point_on_circle +from pffdtd.geometry.math import to_ixy, point_on_circle from pffdtd.diffusor.design import diffusor_bandwidth from pffdtd.diffusor.qrd import quadratic_residue_diffuser from pffdtd.diffusor.prd import primitive_root_diffuser diff --git a/data/models/Modes2D/Modes2D.py b/data/models/Modes2D/Modes2D.py index 6efd8b3..f57ef71 100644 --- a/data/models/Modes2D/Modes2D.py +++ b/data/models/Modes2D/Modes2D.py @@ -1,4 +1,4 @@ -from pffdtd.common.myfuncs import to_ixy +from pffdtd.geometry.math import to_ixy from pffdtd.sim2d.setup import sim_setup_2d diff --git a/src/python/pffdtd/absorption/air.py b/src/python/pffdtd/absorption/air.py index b187c18..a435c92 100644 --- a/src/python/pffdtd/absorption/air.py +++ b/src/python/pffdtd/absorption/air.py @@ -7,7 +7,7 @@ from scipy.fft import rfft, irfft from tqdm import tqdm -from pffdtd.common.myfuncs import iceil, iround +from pffdtd.geometry.math import iceil, iround def air_absorption(frequencies,temperature_celsius,rel_humidity_pnct,pressure_atmospheric_kPa=101.325): """This is an implementation of formulae in the ISO9613-1 standard for air absorption diff --git a/src/python/pffdtd/absorption/air_abs_filters.py b/src/python/pffdtd/absorption/air_abs_filters.py index c910e7b..840f76d 100644 --- a/src/python/pffdtd/absorption/air_abs_filters.py +++ b/src/python/pffdtd/absorption/air_abs_filters.py @@ -5,7 +5,7 @@ import matplotlib.pyplot as plt import numpy as np -from pffdtd.common.myfuncs import iround +from pffdtd.geometry.math import iround from pffdtd.absorption.air import ( apply_modal_filter, apply_ola_filter, diff --git a/src/python/pffdtd/analysis/response.py b/src/python/pffdtd/analysis/response.py index d3ba72e..998a630 100644 --- a/src/python/pffdtd/analysis/response.py +++ b/src/python/pffdtd/analysis/response.py @@ -5,7 +5,7 @@ from matplotlib.ticker import ScalarFormatter from pffdtd.common.plot import plot_styles -from pffdtd.common.myfuncs import iceil +from pffdtd.geometry.math import iceil def fractional_octave_smoothing(magnitudes, fs, nfft, fraction=3): diff --git a/src/python/pffdtd/analysis/room_modes.py b/src/python/pffdtd/analysis/room_modes.py index d86a441..115d7b0 100644 --- a/src/python/pffdtd/analysis/room_modes.py +++ b/src/python/pffdtd/analysis/room_modes.py @@ -8,7 +8,7 @@ from scipy.signal import find_peaks from scipy.io import wavfile -from pffdtd.common.myfuncs import iceil +from pffdtd.geometry.math import iceil from pffdtd.common.plot import plot_styles diff --git a/src/python/pffdtd/common/check_version.py b/src/python/pffdtd/common/check_version.py deleted file mode 100644 index 597e1f3..0000000 --- a/src/python/pffdtd/common/check_version.py +++ /dev/null @@ -1,9 +0,0 @@ -# SPDX-License-Identifier: MIT - -import sys - -ATLEASTVERSION39 = sys.version_info >= (3, 9) -ATLEASTVERSION38 = sys.version_info >= (3, 8) -ATLEASTVERSION37 = sys.version_info >= (3, 7) -ATLEASTVERSION36 = sys.version_info >= (3, 6) -assert ATLEASTVERSION39 diff --git a/src/python/pffdtd/common/timer.py b/src/python/pffdtd/common/timer.py deleted file mode 100644 index ac10948..0000000 --- a/src/python/pffdtd/common/timer.py +++ /dev/null @@ -1,71 +0,0 @@ -############################################################################## -# This file is a part of PFFDTD. -# -# PFFTD is released under the MIT License. -# For details see the LICENSE file. -# -# Copyright 2021 Brian Hamilton. -# -# File name: timer.py -# -# Description: This is a timer. Was set up specifically for FDTD simulations, -# but mostly superseded by timerdict.py -# -############################################################################## - -import time - -class Timer: - def __init__(self,max_tic:int=2): - assert isinstance(max_tic,int) - tic = time.time() - #generic tic/toc (e.g., one for updates, one for drawing frames) - self.t_tic = [0.0]*max_tic - self.t_toc = [0.0]*max_tic - for i in range(0,max_tic): - self.t_tic[i] = tic - self.t_toc[i] = tic - - #calc/draw - self.t_tic_draw = tic - self.t_toc_draw = tic - self.t_tic_calc = tic - self.t_toc_calc = tic - - self.t_slf = tic - self.t_elapsed = 0. - self.t_elapsed_calc = 0. - self.t_elapsed_draw = 0. - - def tic(self,i:int=0): - assert isinstance(i,int) - self.t_tic[i] = time.time() - - def toc(self,i:int=0,print_elapsed:bool=True,prestr:str='')->float: - assert isinstance(i,int) - assert isinstance(print_elapsed,bool) - assert isinstance(prestr,str) - self.t_toc[i] = time.time() - delta = self.t_toc[i] - self.t_tic[i] - if print_elapsed: - print('%selapsed = %.4f s' % (prestr,delta)) - return delta - - - def tic_draw(self): - self.t_tic_draw = time.time() - - def toc_draw(self): - self.t_toc_draw = time.time() - delta = self.t_toc_draw - self.t_tic_draw - self.t_elapsed_draw += delta - self.t_elapsed += delta - - def tic_calc(self): - self.t_tic_calc = time.time() - - def toc_calc(self): - self.t_toc_calc = time.time() - delta = self.t_toc_calc - self.t_tic_calc - self.t_elapsed_calc += delta - self.t_elapsed += delta diff --git a/src/python/pffdtd/geometry/__init__.py b/src/python/pffdtd/geometry/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/python/pffdtd/common/box.py b/src/python/pffdtd/geometry/box.py similarity index 98% rename from src/python/pffdtd/common/box.py rename to src/python/pffdtd/geometry/box.py index 7785aa0..4d55660 100644 --- a/src/python/pffdtd/common/box.py +++ b/src/python/pffdtd/geometry/box.py @@ -3,7 +3,7 @@ import numpy as np from numpy import array as npa import numpy.random as npr -from pffdtd.common.myfuncs import mydefault,rotmatrix_ax_ang +from pffdtd.geometry.math import mydefault,rotmatrix_ax_ang class Box: """This is a class for a box. Used in a few places (vox_grid, tri_box). diff --git a/src/python/pffdtd/common/myfuncs.py b/src/python/pffdtd/geometry/math.py similarity index 100% rename from src/python/pffdtd/common/myfuncs.py rename to src/python/pffdtd/geometry/math.py diff --git a/src/python/pffdtd/common/tri_box_intersection.py b/src/python/pffdtd/geometry/tri_box_intersection.py similarity index 97% rename from src/python/pffdtd/common/tri_box_intersection.py rename to src/python/pffdtd/geometry/tri_box_intersection.py index a092a3b..15c0894 100644 --- a/src/python/pffdtd/common/tri_box_intersection.py +++ b/src/python/pffdtd/geometry/tri_box_intersection.py @@ -20,8 +20,8 @@ import numpy as np from numpy import array as npa -from pffdtd.common.myfuncs import dotv -from pffdtd.common.tris_precompute import tris_precompute +from pffdtd.geometry.math import dotv +from pffdtd.geometry.tris_precompute import tris_precompute #p is lower corner of box #dp is distance to upper corner of box @@ -121,7 +121,7 @@ def tri_box_intersection_vec(bbmin,bbmax,tris_pre): def main(): import numpy.random as npr - from pffdtd.common.box import Box + from pffdtd.geometry.box import Box import argparse parser = argparse.ArgumentParser() parser.add_argument('--nodraw', action='store_true',help='don''t draw') diff --git a/src/python/pffdtd/common/tri_ray_intersection.py b/src/python/pffdtd/geometry/tri_ray_intersection.py similarity index 98% rename from src/python/pffdtd/common/tri_ray_intersection.py rename to src/python/pffdtd/geometry/tri_ray_intersection.py index 3bf502b..3586f15 100644 --- a/src/python/pffdtd/common/tri_ray_intersection.py +++ b/src/python/pffdtd/geometry/tri_ray_intersection.py @@ -22,9 +22,9 @@ import numpy as np from numpy import array as npa -from pffdtd.common.myfuncs import dotv,normalise,vecnorm +from pffdtd.geometry.math import dotv,normalise,vecnorm from pffdtd.common.myasserts import assert_np_array_float -from pffdtd.common.tris_precompute import tris_precompute +from pffdtd.geometry.tris_precompute import tris_precompute #d_eps is a distance eps, cp is for coplanarity (non-dimensional) #pretty standard test: check if coplanar, then check point-on-plane inside edge functions diff --git a/src/python/pffdtd/common/tris_precompute.py b/src/python/pffdtd/geometry/tris_precompute.py similarity index 98% rename from src/python/pffdtd/common/tris_precompute.py rename to src/python/pffdtd/geometry/tris_precompute.py index db4f76e..0044214 100644 --- a/src/python/pffdtd/common/tris_precompute.py +++ b/src/python/pffdtd/geometry/tris_precompute.py @@ -15,7 +15,7 @@ import numpy as np from numpy import array as npa -from pffdtd.common.myfuncs import normalise,dot2,vecnorm +from pffdtd.geometry.math import normalise,dot2,vecnorm #N.B. careful with slicing tris_pre[idx]['var'] (bad) vs tris_pre['var'][idx] (good) def tris_precompute(pts=None,tris=None): diff --git a/src/python/pffdtd/sim2d/setup.py b/src/python/pffdtd/sim2d/setup.py index 964a37b..5899926 100644 --- a/src/python/pffdtd/sim2d/setup.py +++ b/src/python/pffdtd/sim2d/setup.py @@ -5,7 +5,7 @@ import numpy as np from scipy.signal import lfilter -from pffdtd.common.myfuncs import to_ixy +from pffdtd.geometry.math import to_ixy from pffdtd.sim3d.sim_constants import SimConstants diff --git a/src/python/pffdtd/sim3d/engine.py b/src/python/pffdtd/sim3d/engine.py index e157a6b..ef8070c 100644 --- a/src/python/pffdtd/sim3d/engine.py +++ b/src/python/pffdtd/sim3d/engine.py @@ -31,7 +31,7 @@ from tqdm import tqdm from pffdtd.common.timerdict import TimerDict -from pffdtd.common.myfuncs import ind2sub3d,rel_diff,get_default_nprocs +from pffdtd.geometry.math import ind2sub3d,rel_diff,get_default_nprocs MMb = 12 #max allowed number of branches diff --git a/src/python/pffdtd/sim3d/process_outputs.py b/src/python/pffdtd/sim3d/process_outputs.py index b85122e..8e46e45 100644 --- a/src/python/pffdtd/sim3d/process_outputs.py +++ b/src/python/pffdtd/sim3d/process_outputs.py @@ -26,7 +26,7 @@ from pffdtd.absorption.air import apply_visco_filter from pffdtd.absorption.air import apply_modal_filter from pffdtd.absorption.air import apply_ola_filter -from pffdtd.common.myfuncs import wavwrite,iceil,iround +from pffdtd.geometry.math import wavwrite,iceil,iround from pffdtd.common.plot import plot_styles #class to process sim_outs.h5 file diff --git a/src/python/pffdtd/sim3d/room_geometry.py b/src/python/pffdtd/sim3d/room_geometry.py index 5d7c32c..2c10196 100644 --- a/src/python/pffdtd/sim3d/room_geometry.py +++ b/src/python/pffdtd/sim3d/room_geometry.py @@ -2,8 +2,8 @@ import numpy as np import json as json from numpy import array as npa -from pffdtd.common.myfuncs import dotv,rotate_az_el_deg -from pffdtd.common.tris_precompute import tris_precompute +from pffdtd.geometry.math import dotv,rotate_az_el_deg +from pffdtd.geometry.tris_precompute import tris_precompute class RoomGeometry: """Class for room geometry, source/receiver positions, and materials (labels) diff --git a/src/python/pffdtd/sim3d/rotate.py b/src/python/pffdtd/sim3d/rotate.py index c588016..c3644f5 100644 --- a/src/python/pffdtd/sim3d/rotate.py +++ b/src/python/pffdtd/sim3d/rotate.py @@ -21,7 +21,7 @@ from pathlib import Path import time import h5py -from pffdtd.common.myfuncs import ind2sub3d +from pffdtd.geometry.math import ind2sub3d from pffdtd.common.timerdict import TimerDict import shutil diff --git a/src/python/pffdtd/sim3d/sim_signals.py b/src/python/pffdtd/sim3d/sim_signals.py index 8a33089..33a04aa 100644 --- a/src/python/pffdtd/sim3d/sim_signals.py +++ b/src/python/pffdtd/sim3d/sim_signals.py @@ -20,7 +20,7 @@ from scipy.signal import lfilter from pffdtd.common.timerdict import TimerDict -from pffdtd.common.myfuncs import iceil +from pffdtd.geometry.math import iceil class SimSignals: def __init__(self,save_folder): #will read h,xv,yv,zv from h5 data diff --git a/src/python/pffdtd/voxelizer/vox_grid.py b/src/python/pffdtd/voxelizer/vox_grid.py index f417987..04c8d1a 100644 --- a/src/python/pffdtd/voxelizer/vox_grid.py +++ b/src/python/pffdtd/voxelizer/vox_grid.py @@ -14,7 +14,7 @@ from numpy import array as npa from tqdm import tqdm -from pffdtd.common.myfuncs import get_default_nprocs,iceil +from pffdtd.geometry.math import get_default_nprocs,iceil from pffdtd.sim3d.room_geometry import RoomGeometry from pffdtd.voxelizer.cart_grid import CartGrid from pffdtd.voxelizer.vox_grid_base import VoxGridBase,VoxBase diff --git a/src/python/pffdtd/voxelizer/vox_grid_base.py b/src/python/pffdtd/voxelizer/vox_grid_base.py index a8c4371..5c11334 100644 --- a/src/python/pffdtd/voxelizer/vox_grid_base.py +++ b/src/python/pffdtd/voxelizer/vox_grid_base.py @@ -19,8 +19,8 @@ from tqdm import tqdm from pffdtd.common.timerdict import TimerDict -from pffdtd.common.tri_box_intersection import tri_box_intersection_vec -from pffdtd.common.myfuncs import clear_dat_folder, get_default_nprocs +from pffdtd.geometry.tri_box_intersection import tri_box_intersection_vec +from pffdtd.geometry.math import clear_dat_folder, get_default_nprocs #base class for a voxel class VoxBase: @@ -199,7 +199,7 @@ def print_stats(self): #draws non-empty boxes only def draw_boxes(self,tube_radius,backend='mayavi'): - from pffdtd.common.box import Box + from pffdtd.geometry.box import Box Nvox = self.Nvox self.print('drawing boxes..') boxtris = np.zeros((Nvox*12,3)) diff --git a/src/python/pffdtd/voxelizer/vox_scene.py b/src/python/pffdtd/voxelizer/vox_scene.py index 9c9a4ad..04d4811 100644 --- a/src/python/pffdtd/voxelizer/vox_scene.py +++ b/src/python/pffdtd/voxelizer/vox_scene.py @@ -37,13 +37,13 @@ from pffdtd.sim3d.room_geometry import RoomGeometry from pffdtd.common.timerdict import TimerDict -from pffdtd.common.tri_ray_intersection import tri_ray_intersection_vec +from pffdtd.geometry.tri_ray_intersection import tri_ray_intersection_vec from pffdtd.voxelizer.cart_grid import CartGrid from pffdtd.voxelizer.vox_grid import VoxGrid -from pffdtd.common.myfuncs import clear_dat_folder -from pffdtd.common.myfuncs import yes_or_no,ind2sub3d -from pffdtd.common.myfuncs import dotv -from pffdtd.common.myfuncs import get_default_nprocs +from pffdtd.geometry.math import clear_dat_folder +from pffdtd.geometry.math import yes_or_no,ind2sub3d +from pffdtd.geometry.math import dotv +from pffdtd.geometry.math import get_default_nprocs F_EPS = np.finfo(np.float64).eps R_EPS = 1e-6 #relative eps (to grid spacing) for near hits diff --git a/src/python/test/test_sim2d_modes.py b/src/python/test/test_sim2d_modes.py index 3319663..ef1e162 100644 --- a/src/python/test/test_sim2d_modes.py +++ b/src/python/test/test_sim2d_modes.py @@ -7,7 +7,7 @@ import numpy as np import pytest -from pffdtd.common.myfuncs import to_ixy +from pffdtd.geometry.math import to_ixy from pffdtd.cli import main as cli from pffdtd.sim2d.setup import sim_setup_2d diff --git a/src/python/test/test_sim3d_infinite_baffle.py b/src/python/test/test_sim3d_infinite_baffle.py index ccebba8..ba421a9 100644 --- a/src/python/test/test_sim3d_infinite_baffle.py +++ b/src/python/test/test_sim3d_infinite_baffle.py @@ -3,8 +3,8 @@ import numpy as np import pytest -from pffdtd.common.myfuncs import wavread -from pffdtd.common.myfuncs import point_on_circle +from pffdtd.geometry.math import wavread +from pffdtd.geometry.math import point_on_circle from pffdtd.materials.adm_funcs import write_freq_ind_mat_from_Yn, convert_Sabs_to_Yn from pffdtd.sim3d.sim_setup import sim_setup from pffdtd.sim3d.testing import run_engine, skip_if_native_engine_unavailable diff --git a/src/python/test/test_sim3d_locate_sound_source.py b/src/python/test/test_sim3d_locate_sound_source.py index 4f64162..c67f5c9 100644 --- a/src/python/test/test_sim3d_locate_sound_source.py +++ b/src/python/test/test_sim3d_locate_sound_source.py @@ -3,7 +3,7 @@ import numpy as np import pytest -from pffdtd.common.myfuncs import wavread +from pffdtd.geometry.math import wavread from pffdtd.materials.adm_funcs import write_freq_ind_mat_from_Yn, convert_Sabs_to_Yn from pffdtd.sim3d.room_builder import RoomBuilder from pffdtd.sim3d.sim_setup import sim_setup