From 6440b408ac0955f3b5a5e94ed7a9d68ba2df62e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Jos=C3=A9=20Carracedo=20Carballal?= Date: Mon, 6 Mar 2023 20:36:50 +0100 Subject: [PATCH 1/3] Implement mesh_estimate_cfl_timestep --- python/openEMS/automesh.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/python/openEMS/automesh.py b/python/openEMS/automesh.py index 93472fa..734201a 100644 --- a/python/openEMS/automesh.py +++ b/python/openEMS/automesh.py @@ -75,3 +75,26 @@ def mesh_hint_from_box(box, dirs, **kw): hint[ny].append(start[ny]) return hint +def mesh_estimate_cfl_timestep(box): + """ mesh_estimate_cfl_timestep(box) + + Estimate the maximum CFL time step of the given box needed to ensure numerical stability, + assuming propagation in pure vacuum. + + :returns: the maximum CFL time step, in seconds + """ + minDiff = [None, None, None] + for ny in range(3): + lines = box.GetQtyLines(ny) + for i in range(1, lines): + delta = box.GetLine(ny, i) - box.GetLine(ny, i - 1) + if minDiff[ny] is None or delta < minDiff[ny]: + minDiff[ny] = delta + if None in minDiff: + sys.stderr.write('FDTD::automesh: Warning, mesh is ill-defined (no lines in certain directions?)\n') + return 0 + + delta_t = box.GetDeltaUnit() / (C0 * np.sqrt(np.sum(np.array(minDiff) ** -2))) + + return delta_t + From 3eb44399592ceca143c8b0b0a873faef3601cdb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Jos=C3=A9=20Carracedo=20Carballal?= Date: Mon, 6 Mar 2023 21:45:17 +0100 Subject: [PATCH 2/3] Improve readability of mesh_estimate_cfl_timestep --- python/openEMS/automesh.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/python/openEMS/automesh.py b/python/openEMS/automesh.py index 734201a..c3da361 100644 --- a/python/openEMS/automesh.py +++ b/python/openEMS/automesh.py @@ -75,26 +75,19 @@ def mesh_hint_from_box(box, dirs, **kw): hint[ny].append(start[ny]) return hint -def mesh_estimate_cfl_timestep(box): - """ mesh_estimate_cfl_timestep(box) +def mesh_estimate_cfl_timestep(mesh): + """ mesh_estimate_cfl_timestep(mesh) - Estimate the maximum CFL time step of the given box needed to ensure numerical stability, + Estimate the maximum CFL time step of the given mesh needed to ensure numerical stability, assuming propagation in pure vacuum. :returns: the maximum CFL time step, in seconds """ - minDiff = [None, None, None] + invMinDiff = [None, None, None] for ny in range(3): - lines = box.GetQtyLines(ny) - for i in range(1, lines): - delta = box.GetLine(ny, i) - box.GetLine(ny, i - 1) - if minDiff[ny] is None or delta < minDiff[ny]: - minDiff[ny] = delta - if None in minDiff: - sys.stderr.write('FDTD::automesh: Warning, mesh is ill-defined (no lines in certain directions?)\n') - return 0 + invMinDiff[ny] = np.min(np.diff(mesh.GetLines(ny))) ** -2 - delta_t = box.GetDeltaUnit() / (C0 * np.sqrt(np.sum(np.array(minDiff) ** -2))) + delta_t = mesh.GetDeltaUnit() / (C0 * np.sqrt(np.sum(invMinDiff))) return delta_t From cf34998b015caa433bdf238914349c99474b120e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gonzalo=20Jos=C3=A9=20Carracedo=20Carballal?= Date: Mon, 6 Mar 2023 21:46:43 +0100 Subject: [PATCH 3/3] Add missing import --- python/openEMS/automesh.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/openEMS/automesh.py b/python/openEMS/automesh.py index c3da361..51bf0cb 100644 --- a/python/openEMS/automesh.py +++ b/python/openEMS/automesh.py @@ -10,6 +10,7 @@ import numpy as np from CSXCAD import CSPrimitives from CSXCAD.Utilities import CheckNyDir, GetMultiDirs +from openEMS.physical_constants import C0 def mesh_hint_from_primitive(primitive, dirs, **kw): if primitive.GetType() is CSPrimitives.POINT: