Skip to content

Delegate PEC check from MultiPhysicsMedium to its Optical Medium #2431

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

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
17 changes: 17 additions & 0 deletions tests/test_components/test_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ def test_plot_eps():
plt.close()


def test_plot_eps_multiphysics():
s = td.Scene(
structures=[
td.Structure(
geometry=td.Box(size=(1, 1, 1), center=(-1, 0.5, 0.5)),
medium=td.MultiPhysicsMedium(
optical=td.Medium(permittivity=3.9),
charge=td.ChargeInsulatorMedium(permittivity=3.9),
name="SiO2",
),
)
]
)
assert s.structures[0].medium.name == "SiO2"
s.plot_eps(x=0)


def test_plot_eps_bounds():
_ = SCENE_FULL.plot_eps(x=0, hlim=[-0.45, 0.45])
plt.close()
Expand Down
47 changes: 47 additions & 0 deletions tidy3d/components/material/multi_physics.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,53 @@ class MultiPhysicsMedium(Tidy3dBaseModel):
None, title="Charge properties", description="Specifies properties for Charge simulations."
)

def __getattr__(self, name: str):
"""
Delegate attribute lookup to inner media or fail fast.

Parameters
----------
name : str
The attribute that could not be found on the ``MultiPhysicsMedium`` itself.

Returns
-------
Any
* The attribute value obtained from a delegated sub-medium when
``name`` is listed in ``DELEGATED_ATTRIBUTES``.
* ``None`` when ``name`` is explicitly ignored (e.g. ``"__deepcopy__"``).

Raises
------
ValueError
If ``name`` is neither ignored nor in the delegation map, signalling that
the caller may have intended to access ``optical``, ``heat``, or
``charge`` directly.

Notes
-----
Only the attributes enumerated in the local ``DELEGATED_ATTRIBUTES`` dict are
forwarded.
Extend that mapping as additional cross-medium shim behaviour becomes
necessary.
"""
IGNORED_ATTRIBUTES = ["__deepcopy__"]
if name in IGNORED_ATTRIBUTES:
return None

DELEGATED_ATTRIBUTES = {
"is_pec": self.optical,
"_eps_plot": self.optical,
"viz_spec": self.optical,
}

if name in DELEGATED_ATTRIBUTES:
return getattr(DELEGATED_ATTRIBUTES[name], name)
else:
raise ValueError(
f"MultiPhysicsMedium has no attribute called {name}. Did you mean to access the attribute of one of the optical, heat or charge media?"
)

@property
def heat_spec(self):
if self.heat is not None:
Expand Down