From df93c001b249dfd9869cf0fb5acb9700f0de4ea6 Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Thu, 29 Jul 2021 00:08:02 +0000 Subject: [PATCH 1/2] lower tolerance of CW and eigenfrequency solver tests for single precision --- python/simulation.py | 5 +++-- python/tests/test_array_metadata.py | 2 +- python/tests/test_physical.py | 2 +- src/meep.hpp | 5 +++-- tests/dft-fields.cpp | 2 +- tests/near2far.cpp | 4 ++-- tests/physical.cpp | 2 +- 7 files changed, 12 insertions(+), 10 deletions(-) diff --git a/python/simulation.py b/python/simulation.py index e8ef07d83..3415f9fa1 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -3061,13 +3061,14 @@ def modal_volume_in_box(self, box=None, center=None, size=None): return self.fields.modal_volume_in_box(box) - def solve_cw(self, tol=1e-8, maxiters=10000, L=2): + def solve_cw(self, tol=1e-5 if mp.is_single_precision() else 1e-8, maxiters=10000, L=2): if self.fields is None: raise RuntimeError('Fields must be initialized before using solve_cw') self._evaluate_dft_objects() return self.fields.solve_cw(tol, maxiters, L) - def solve_eigfreq(self, tol=1e-7, maxiters=100, guessfreq=None, cwtol=None, cwmaxiters=10000, L=10): + def solve_eigfreq(self, tol=1e-5 if mp.is_single_precision() else 1e-7, maxiters=100, + guessfreq=None, cwtol=None, cwmaxiters=10000, L=10): if self.fields is None: raise RuntimeError('Fields must be initialized before using solve_cw') if cwtol is None: diff --git a/python/tests/test_array_metadata.py b/python/tests/test_array_metadata.py index 4b433e961..0e1e150e1 100644 --- a/python/tests/test_array_metadata.py +++ b/python/tests/test_array_metadata.py @@ -42,7 +42,7 @@ def test_array_metadata(self): boundary_layers=pml_layers) sim.init_sim() - sim.solve_cw(1e-6, 1000, 10) + sim.solve_cw(1e-5 if mp.is_single_precision() else 1e-6, 1000, 10) def electric_energy(r, ez, eps): return np.real(eps * np.conj(ez)*ez) diff --git a/python/tests/test_physical.py b/python/tests/test_physical.py index 25b873187..8a3c29120 100644 --- a/python/tests/test_physical.py +++ b/python/tests/test_physical.py @@ -41,7 +41,7 @@ def test_physical(self): sources=sources, force_complex_fields=True) sim.init_sim() - sim.solve_cw(tol=1e-6) + sim.solve_cw(tol=1e-5 if mp.is_single_precision() else 1e-6) p1 = mp.Vector3() p2 = mp.Vector3(dx) diff --git a/src/meep.hpp b/src/meep.hpp index 5d8485023..9d829fff3 100644 --- a/src/meep.hpp +++ b/src/meep.hpp @@ -1824,8 +1824,9 @@ class fields { // cw_fields.cpp: bool solve_cw(double tol, int maxiters, std::complex frequency, int L = 2, std::complex *eigfreq = NULL, double eigtol = 1e-8, int eigiters = 20); - bool solve_cw(double tol = 1e-8, int maxiters = 10000, int L = 2, - std::complex *eigfreq = NULL, double eigtol = 1e-8, int eigiters = 20); + bool solve_cw(double tol = sizeof(realnum) == sizeof(float) ? 1e-5 : 1e-8, int maxiters = 10000, + int L = 2, std::complex *eigfreq = NULL, double eigtol = 1e-8, + int eigiters = 20); // sources.cpp: double last_source_time(); diff --git a/tests/dft-fields.cpp b/tests/dft-fields.cpp index ee5424c3b..241866703 100644 --- a/tests/dft-fields.cpp +++ b/tests/dft-fields.cpp @@ -92,7 +92,7 @@ void Run(bool Pulse, double resolution, cdouble **field_array = 0, int *array_ra } else { f.add_point_source(Ez, continuous_src_time(fcen, df), x0); - f.solve_cw(1e-8, 10000, 10); + f.solve_cw(sizeof(realnum) == sizeof(float) ? 1e-5 : 1e-8, 10000, 10); h5file *file = f.open_h5file("cw-fields", h5file::WRITE, 0, false); f.output_hdf5(Ez, f.v, file); f.output_hdf5(Hx, f.v, file); diff --git a/tests/near2far.cpp b/tests/near2far.cpp index 0a232a38d..be1e89bca 100644 --- a/tests/near2far.cpp +++ b/tests/near2far.cpp @@ -44,7 +44,7 @@ int check_cyl(double sr, double sz, double a) { continuous_src_time src(w); vec x0 = veccyl(0.5 * sr, 0); f.add_point_source(c0, src, x0); - f.solve_cw(1e-6); + f.solve_cw(sizeof(realnum) == sizeof(float) ? 1e-5 : 1e-6); component c = Ep; const int N = 20; @@ -137,7 +137,7 @@ int check_2d_3d(ndim dim, const double xmax, double a, component c0, component c continuous_src_time src(w); f.add_point_source(c0, src, zero_vec(dim)); if (c1 != NO_COMPONENT) f.add_point_source(c1, src, zero_vec(dim), 0.7654321); - f.solve_cw(1e-6); + f.solve_cw(sizeof(realnum) == sizeof(float) ? 1e-5 : 1e-6); FOR_E_AND_H(c) { if (gv.has_field(c)) { diff --git a/tests/physical.cpp b/tests/physical.cpp index 6d324e16e..3955a894d 100644 --- a/tests/physical.cpp +++ b/tests/physical.cpp @@ -42,7 +42,7 @@ int radiating_2D(const double xmax) { // let the source reach steady state #if 1 - f.solve_cw(1e-6); + f.solve_cw(sizeof(realnum) == sizeof(float) ? 1e-5 : 1e-6); #else while (f.time() < 400) f.step(); From 30115777500679dbb989ec87f909c6b596247747 Mon Sep 17 00:00:00 2001 From: Ardavan Oskooi Date: Thu, 29 Jul 2021 19:58:13 +0000 Subject: [PATCH 2/2] remove change in default argument from Python wrappers --- python/simulation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/simulation.py b/python/simulation.py index 3415f9fa1..41092b6e3 100644 --- a/python/simulation.py +++ b/python/simulation.py @@ -3061,13 +3061,13 @@ def modal_volume_in_box(self, box=None, center=None, size=None): return self.fields.modal_volume_in_box(box) - def solve_cw(self, tol=1e-5 if mp.is_single_precision() else 1e-8, maxiters=10000, L=2): + def solve_cw(self, tol=1e-8, maxiters=10000, L=2): if self.fields is None: raise RuntimeError('Fields must be initialized before using solve_cw') self._evaluate_dft_objects() return self.fields.solve_cw(tol, maxiters, L) - def solve_eigfreq(self, tol=1e-5 if mp.is_single_precision() else 1e-7, maxiters=100, + def solve_eigfreq(self, tol=1e-7, maxiters=100, guessfreq=None, cwtol=None, cwmaxiters=10000, L=10): if self.fields is None: raise RuntimeError('Fields must be initialized before using solve_cw')