Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Component Modeler Batch Path #1259

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added support for two-photon absorption via `TwoPhotonAbsorption` class. Added `KerrNonlinearity` that implements Kerr effect without third-harmonic generation.
- Can create `PoleResidue` from LO-TO form via `PoleResidue.from_lo_to`.

- Support for an anisotropic medium containing PEC components.
- `SimulationData.mnt_data_from_file()` method to load only a single monitor data object from a simulation data `.hdf5` file.
- `_hash_self` to base model, uses `hashlib` to hash a Tidy3D component the same way every session.

### Changed
- Indent for the json string of Tidy3D models has been changed to `None` when used internally; kept as `indent=4` for writing to `json` and `yaml` files.
Expand All @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improved error handling if file can not be downloaded from server.
- Fix for detection of file extensions for file names with dots.
- Restrict to `matplotlib` >= 3.5, avoiding bug in plotting `CustomMedium`.
- Fixes `ComponentModeler` batch file being different in different sessions by use of deterministic hash function for computing batch filename.

## [2.5.0rc2] - 2023-10-30

Expand Down
5 changes: 5 additions & 0 deletions tests/test_plugins/test_component_modeler.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,8 @@ def test_mapping_exclusion(monkeypatch, tmp_path):

s_matrix = run_component_modeler(monkeypatch, modeler)
_test_mappings(element_mappings, s_matrix)


def test_batch_filename(tmp_path):
modeler = make_component_modeler(path_dir=str(tmp_path))
path = modeler._batch_path
8 changes: 8 additions & 0 deletions tidy3d/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from functools import wraps
from typing import List, Callable, Dict, Union, Tuple, Any
from math import ceil
import io
import hashlib

import rich
import pydantic.v1 as pydantic
Expand Down Expand Up @@ -99,6 +101,12 @@ def __hash__(self) -> int:
except TypeError:
return hash(self.json())

def _hash_self(self) -> str:
"""Hash this component with ``hashlib`` in a way that is the same every session."""
bf = io.BytesIO()
self.to_hdf5(bf)
return hashlib.sha256(bf.getvalue()).hexdigest()

def __init__(self, **kwargs):
"""Init method, includes post-init validators."""
log.begin_capture()
Expand Down
3 changes: 2 additions & 1 deletion tidy3d/plugins/smatrix/smatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ def get_path_dir(self, path_dir: str) -> None:
@cached_property
def _batch_path(self) -> str:
"""Where we store the batch for this ComponentModeler instance after the run."""
return os.path.join(self.path_dir, "batch" + str(hash(self)) + ".json")
hash_str = self._hash_self()
return os.path.join(self.path_dir, "batch" + hash_str + ".json")

def _run_sims(self, path_dir: str = DEFAULT_DATA_DIR) -> BatchData:
"""Run :class:`Simulations` for each port and return the batch after saving."""
Expand Down