Skip to content

Commit 59d54ff

Browse files
committed
Modeler validation and sim_dict performance improvements
1 parent 427dec9 commit 59d54ff

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

.github/workflows/tidy3d-python-client-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ jobs:
262262
BRANCH_NAME="${STEPS_EXTRACT_BRANCH_NAME_OUTPUTS_BRANCH_NAME}"
263263
echo $BRANCH_NAME
264264
# Allow only Jira keys from known projects, even if the branch has an author prefix
265-
ALLOWED_JIRA_PROJECTS=("FXC" "SCEM")
265+
ALLOWED_JIRA_PROJECTS=("FXC" "SCEM" "SCRF")
266266
JIRA_PROJECT_PATTERN=$(IFS='|'; echo "${ALLOWED_JIRA_PROJECTS[*]}")
267267
JIRA_PATTERN="(${JIRA_PROJECT_PATTERN})-[0-9]+"
268268

tests/test_plugins/test_array_factor.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,15 @@ def make_antenna_sim():
363363
remove_dc_component=False, # Include DC component for more accuracy at low frequencies
364364
)
365365

366-
sim_unit = list(modeler.sim_dict.values())[0]
367-
368-
return sim_unit
366+
return modeler
369367

370368

371369
def test_rectangular_array_calculator_array_make_antenna_array():
372370
"""Test automatic antenna array creation."""
373371
freq0 = 10e9
374372
wavelength0 = td.C_0 / 10e9
375-
sim_unit = make_antenna_sim()
373+
modeler = make_antenna_sim()
374+
sim_unit = list(modeler.sim_dict.values())[0]
376375
array_calculator = mw.RectangularAntennaArrayCalculator(
377376
array_size=(1, 2, 3),
378377
spacings=(0.5 * wavelength0, 0.6 * wavelength0, 0.4 * wavelength0),
@@ -437,8 +436,9 @@ def test_rectangular_array_calculator_array_make_antenna_array():
437436
assert len(sim_array.sources) == 6
438437

439438
# check that override_structures are duplicated
440-
assert len(sim_unit.grid_spec.override_structures) == 2
441-
assert len(sim_array.grid_spec.override_structures) == 7
439+
# assert len(sim_unit.grid_spec.override_structures) == 2
440+
# assert len(sim_array.grid_spec.override_structures) == 7
441+
assert sim_unit.grid.boundaries == modeler.base_sim.grid.boundaries
442442

443443
# check that phase shifts are applied correctly
444444
phases_expected = array_calculator._antenna_phases
@@ -674,7 +674,8 @@ def test_rectangular_array_calculator_simulation_data_from_array_factor():
674674
phase_shifts=(np.pi / 3, np.pi / 4, np.pi / 5),
675675
)
676676

677-
sim_unit = make_antenna_sim()
677+
modeler = make_antenna_sim()
678+
sim_unit = list(modeler.sim_dict.values())[0]
678679

679680
monitor = sim_unit.monitors[0]
680681
monitor_directivity = sim_unit.monitors[2]

tidy3d/plugins/smatrix/component_modelers/terminal.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,17 @@ def _warn_refactor_2_10(cls, values):
223223

224224
@property
225225
def _sim_with_sources(self) -> Simulation:
226-
"""Instance of :class:`.Simulation` with all sources and absorbers added for each port, for troubleshooting."""
226+
"""Instance of :class:`.Simulation` with all sources and absorbers added for each port, for plotting."""
227227

228228
sources = [port.to_source(self._source_time) for port in self.ports]
229229
absorbers = [
230230
port.to_absorber()
231231
for port in self.ports
232232
if isinstance(port, WavePort) and port.absorber
233233
]
234-
return self.simulation.updated_copy(sources=sources, internal_absorbers=absorbers)
234+
return self.simulation.updated_copy(
235+
sources=sources, internal_absorbers=absorbers, validate=False
236+
)
235237

236238
@equal_aspect
237239
@add_ax_if_none
@@ -382,18 +384,17 @@ def matrix_indices_run_sim(self) -> tuple[NetworkIndex, ...]:
382384
def sim_dict(self) -> SimulationMap:
383385
"""Generate all the :class:`.Simulation` objects for the port parameter calculation."""
384386

387+
# Check base simulation for grid size at ports
388+
TerminalComponentModeler._check_grid_size_at_ports(self.base_sim, self._lumped_ports)
389+
TerminalComponentModeler._check_grid_size_at_wave_ports(self.base_sim, self._wave_ports)
390+
385391
sim_dict = {}
386392
# Now, create simulations with wave port sources and mode solver monitors for computing port modes
387393
for network_index in self.matrix_indices_run_sim:
388394
task_name, sim_with_src = self._add_source_to_sim(network_index)
389395
# update simulation
390396
sim_dict[task_name] = sim_with_src
391397

392-
# Check final simulations for grid size at ports
393-
for _, sim in sim_dict.items():
394-
TerminalComponentModeler._check_grid_size_at_ports(sim, self._lumped_ports)
395-
TerminalComponentModeler._check_grid_size_at_wave_ports(sim, self._wave_ports)
396-
397398
return SimulationMap(keys=tuple(sim_dict.keys()), values=tuple(sim_dict.values()))
398399

399400
@cached_property
@@ -414,7 +415,10 @@ def _base_sim_no_radiation_monitors(self) -> Simulation:
414415

415416
# Make an initial simulation with new grid_spec to determine where LumpedPorts are snapped
416417
sim_wo_source = self.simulation.updated_copy(
417-
grid_spec=grid_spec, lumped_elements=lumped_resistors
418+
grid_spec=grid_spec,
419+
lumped_elements=lumped_resistors,
420+
validate=False,
421+
deep=False,
418422
)
419423
snap_centers = {}
420424
for port in self._lumped_ports:
@@ -480,7 +484,11 @@ def _base_sim_no_radiation_monitors(self) -> Simulation:
480484
)
481485

482486
# update base simulation with updated set of shared components
483-
sim_wo_source = sim_wo_source.copy(update=update_dict)
487+
sim_wo_source = sim_wo_source.updated_copy(
488+
**update_dict,
489+
validate=False,
490+
deep=False,
491+
)
484492

485493
# extrude port structures
486494
sim_wo_source = self._extrude_port_structures(sim=sim_wo_source)
@@ -527,7 +535,10 @@ def base_sim(self) -> Simulation:
527535
"""The base simulation with all components added, including radiation monitors."""
528536
base_sim_tmp = self._base_sim_no_radiation_monitors
529537
mnts_with_radiation = list(base_sim_tmp.monitors) + list(self._finalized_radiation_monitors)
530-
return base_sim_tmp.updated_copy(monitors=mnts_with_radiation)
538+
grid_spec = GridSpec.from_grid(base_sim_tmp.grid)
539+
grid_spec.attrs["from_grid_spec"] = base_sim_tmp.grid_spec
540+
# We skipped validations up to now, here we finally validate the base sim
541+
return base_sim_tmp.updated_copy(monitors=mnts_with_radiation, grid_spec=grid_spec)
531542

532543
def _generate_radiation_monitor(
533544
self, simulation: Simulation, auto_spec: DirectivityMonitorSpec
@@ -712,7 +723,10 @@ def _add_source_to_sim(self, source_index: NetworkIndex) -> tuple[str, Simulatio
712723
)
713724
task_name = self.get_task_name(port=port, mode_index=mode_index)
714725

715-
return (task_name, self.base_sim.updated_copy(sources=[port_source]))
726+
return (
727+
task_name,
728+
self.base_sim.updated_copy(sources=[port_source], validate=False, deep=False),
729+
)
716730

717731
@cached_property
718732
def _source_time(self):
@@ -983,6 +997,8 @@ def _extrude_port_structures(self, sim: Simulation) -> Simulation:
983997
sim = sim.updated_copy(
984998
grid_spec=GridSpec.from_grid(sim.grid),
985999
structures=[*sim.structures, *all_new_structures],
1000+
validate=False,
1001+
deep=False,
9861002
)
9871003

9881004
return sim

0 commit comments

Comments
 (0)