Skip to content

Commit

Permalink
add '_hash_self' method for deterministic hash, fixes 'ComponentModel…
Browse files Browse the repository at this point in the history
…er._batch_file' inconsistency.
  • Loading branch information
tylerflex committed Nov 21, 2023
1 parent 1b5b46c commit 07e81cc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
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
9 changes: 9 additions & 0 deletions tidy3d/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from functools import wraps
from typing import List, Callable, Dict, Union, Tuple, Any
from math import ceil
import io
import base64
import hashlib

import rich
import pydantic.v1 as pydantic
Expand Down Expand Up @@ -99,6 +102,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
5 changes: 4 additions & 1 deletion tidy3d/plugins/smatrix/smatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from typing import List, Tuple, Optional, Dict
import os
import hashlib
import json

import pydantic.v1 as pd
import numpy as np
Expand Down Expand Up @@ -349,7 +351,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

0 comments on commit 07e81cc

Please sign in to comment.