Skip to content

Commit afa9bb4

Browse files
author
Gregory Roberts
committed
feature[frontend]: improve printing of materials and material library
1 parent a25cb6c commit afa9bb4

File tree

6 files changed

+271
-1
lines changed

6 files changed

+271
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- `fill` and `fill_structures` argument in `td.Simulation.plot_structures()` and `td.Simulation.plot()` respectively to disable fill and plot outlines of structures only.
1212
- New subpixel averaging option `ContourPathAveraging` applied to dielectric material boundaries.
1313
- A property `interior_angle` in `PolySlab` that stores angles formed inside polygon by two adjacent edges.
14+
- Methods for pretty printing the `material_library` and well as different materials and their variants.
15+
16+
### Changed
17+
- Updated represenetation for `AbstractMedium` instances that have a specified name field to use that name instead of the full string representation of the medium to reduce clutter when printing models that have these mediums.
1418

1519
### Fixed
1620
- Compatibility with `xarray>=2025.03`.

tests/test_material_library/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Tests material library functions and pretty printing"""
2+
3+
import tidy3d as td
4+
5+
6+
def test_material_library_summary():
7+
"""Test to make sure we can print the material library without error."""
8+
print(td.material_library)
9+
10+
11+
def test_material_summary():
12+
"""Test the string method for each material in the material library."""
13+
14+
for _, material in td.material_library.items():
15+
print(material)
16+
17+
18+
def test_variant_summary():
19+
"""Test the string method for each variant in the material library."""
20+
21+
for _, material in td.material_library.items():
22+
# graphene in the material library is run differently than the other materials and
23+
# doesn't have the variant structure so we exclude any materials that are in this
24+
# format
25+
if hasattr(material, "variants"):
26+
for variant in material.variants:
27+
print(variant)
28+
29+
30+
def test_material_library_medium_repr():
31+
"""Test the new repr method does not error for material library variants."""
32+
33+
for material_key, material in td.material_library.items():
34+
if hasattr(material, "variants"):
35+
for variant_key in material.variants:
36+
mat = td.material_library[material_key][variant_key]
37+
print(mat)
38+
print(repr(mat))
39+
40+
41+
def test_medium_repr():
42+
"""Test the new repr method does not error for regular media with and without names."""
43+
44+
test_media = [
45+
td.Medium(permittivity=1.5**2),
46+
td.Medium(permittivity=1.5**2, name="material"),
47+
td.material_library["SiO2"]["Horiba"].updated_copy(name=None),
48+
]

tidy3d/components/medium.py

+6
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,12 @@ def derivative_eps_complex_volume(
13781378

13791379
return vjp_value.sum("f")
13801380

1381+
def __repr__(self):
1382+
if self.name:
1383+
return self.name
1384+
else:
1385+
return super().__repr__()
1386+
13811387

13821388
class AbstractCustomMedium(AbstractMedium, ABC):
13831389
"""A spatially varying medium."""

0 commit comments

Comments
 (0)