Skip to content

Commit 4a25807

Browse files
Fix modeler.to_file when batch is empty
1 parent 0d9b8a9 commit 4a25807

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2828
### Fixed
2929
- Make gauge selection for non-converged modes more robust.
3030
- Fixed extremely long runtime generated by `RunTimeSpec` in the presence of `LossyMetalMedium`.
31+
- Fixed `ComponentModeler.to_file` when its batch is empty.
3132

3233
## [2.8.0rc2] - 2025-01-28
3334

tests/test_plugins/smatrix/test_component_modeler.py

+11
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ def test_import_smatrix_smatrix():
381381
from tidy3d.plugins.smatrix.smatrix import ComponentModeler, Port # noqa: F401
382382

383383

384+
def test_to_from_file_empty_batch(tmp_path):
385+
modeler = make_component_modeler()
386+
387+
fname = str(tmp_path) + "/modeler.json"
388+
389+
modeler.to_file(fname)
390+
modeler2 = modeler.from_file(fname)
391+
392+
assert modeler2.batch_cached is None
393+
394+
384395
def test_to_from_file_batch(tmp_path, monkeypatch):
385396
modeler = make_component_modeler()
386397
_ = run_component_modeler(monkeypatch, modeler)

tidy3d/plugins/smatrix/component_modelers/base.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,15 @@ def to_file(self, fname: str) -> None:
137137
"""
138138

139139
batch_cached = self._cached_properties.get("batch")
140-
jobs_cached = batch_cached._cached_properties.get("jobs")
141-
if jobs_cached is not None:
142-
jobs = {}
143-
for key, job in jobs_cached.items():
144-
task_id = job._cached_properties.get("task_id")
145-
jobs[key] = job.updated_copy(task_id_cached=task_id)
146-
batch_cached = batch_cached.updated_copy(jobs_cached=jobs)
147-
self = self.updated_copy(batch_cached=batch_cached)
140+
if batch_cached is not None:
141+
jobs_cached = batch_cached._cached_properties.get("jobs")
142+
if jobs_cached is not None:
143+
jobs = {}
144+
for key, job in jobs_cached.items():
145+
task_id = job._cached_properties.get("task_id")
146+
jobs[key] = job.updated_copy(task_id_cached=task_id)
147+
batch_cached = batch_cached.updated_copy(jobs_cached=jobs)
148+
self = self.updated_copy(batch_cached=batch_cached)
148149
super(AbstractComponentModeler, self).to_file(fname=fname) # noqa: UP008
149150

150151
@cached_property

0 commit comments

Comments
 (0)