Skip to content

Commit ae4e2e3

Browse files
Gregory Robertsgroberts-flex
authored andcommitted
feature[frontend]: improve printing of materials and material library
1 parent 9091008 commit ae4e2e3

File tree

7 files changed

+566
-2
lines changed

7 files changed

+566
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Classmethod `from_frequency_range` in `GaussianPulse` for generating a pulse whose amplitude in the frequency_range [fmin, fmax] is maximized, which is particularly useful for running broadband simulations.
1818
- Differentiable function `td.plugins.autograd.interpolate_spline` for 1D linear, quadratic, and cubic spline interpolation, supporting differentiation with respect to the interpolated values (`y_points`) and optional endpoint derivative constraints.
1919
- `SteadyEnergyBandMonitor` in the Charge solver.
20+
- Pretty printing enabled with `rich.print` for the material library, materials, and their variants. In notebooks, this can be accessed using `rich.print` or `display`, or by evaluating the material library, a material, or a variant in a cell.
2021

2122
### Changed
2223
- Performance enhancement for adjoint gradient calculations by optimizing field interpolation.
2324
- Auto grid in EME simulations with multiple `freqs` provided uses the largest instead of raising an error.
25+
- Named mediums now display by name for brevity; materials/variants print concise summaries including references.
2426

2527
### Fixed
2628
- Fixed `reverse` property of `td.Scene.plot_structures_property()` to also reverse the colorbar.

tests/test_material_library/__init__.py

Whitespace-only changes.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""Tests material library functions and pretty printing"""
2+
3+
import tidy3d as td
4+
from tidy3d.material_library.material_library import MaterialItemUniaxial
5+
6+
7+
def test_material_library_summary():
8+
"""Test to make sure we can print the material library without error."""
9+
print(td.material_library)
10+
11+
12+
def test_material_summary():
13+
"""Test the string method for each material in the material library."""
14+
15+
for _, material in td.material_library.items():
16+
print(material)
17+
18+
19+
def test_variant_summary():
20+
"""Test the string method for each variant in the material library."""
21+
22+
for _, material in td.material_library.items():
23+
# graphene in the material library is run differently than the other materials and
24+
# doesn't have the variant structure so we exclude any materials that are in this
25+
# format
26+
if hasattr(material, "variants"):
27+
for variant in material.variants:
28+
print(variant)
29+
30+
31+
def test_material_library_medium_repr():
32+
"""Test the new repr method does not error for material library variants."""
33+
34+
for material_key, material in td.material_library.items():
35+
if hasattr(material, "variants"):
36+
for variant_key in material.variants:
37+
mat = td.material_library[material_key]
38+
med = td.material_library[material_key][variant_key]
39+
print(med)
40+
print(repr(med))
41+
42+
if (type(med) is not td.MultiPhysicsMedium) and (
43+
type(mat) is not MaterialItemUniaxial
44+
):
45+
assert med.__repr__() == med.name, "Expected repr to return just the name"
46+
47+
48+
def test_medium_repr():
49+
"""Test the new repr method does not error for regular media with and without names and
50+
that names are returned correctly by repr when they exist."""
51+
52+
material_name = "material"
53+
test_media = [
54+
td.Medium(permittivity=1.5**2),
55+
td.Medium(permittivity=1.5**2, name=material_name),
56+
td.material_library["SiO2"]["Horiba"].updated_copy(name=None),
57+
]
58+
noname_medium_in_dict = {"medium": test_media[0]}
59+
60+
str_noname_medium = str(test_media[0])
61+
repr_noname_medium = test_media[0].__repr__()
62+
str_noname_medium_dict = str(noname_medium_in_dict)
63+
64+
assert (
65+
"type='Medium' permittivity=2.25 conductivity=0.0" in str_noname_medium
66+
), "Expected medium information in string"
67+
assert (
68+
"Medium(attrs={}, name=None, frequency_range=None" in repr_noname_medium
69+
), "Expcted medium information in repr"
70+
assert repr_noname_medium in str_noname_medium_dict, "Expected repr in dictionary string"
71+
72+
for medium in test_media:
73+
repr_str = medium.__repr__()
74+
75+
assert (
76+
test_media[1].__repr__() == material_name
77+
), "Expected repr to return just the material name."
78+
79+
80+
def test_variant_str():
81+
"""Test one of the materials for some expected output in variant printing."""
82+
83+
printed_SiO2 = str(td.material_library["SiO2"].variants["Horiba"])
84+
85+
assert "eps_inf: 1.0" in printed_SiO2, "Expected eps_inf in SiO2 printed string"
86+
assert "poles: 1" in printed_SiO2, "Expected 1 pole in SiO2 printed string"
87+
88+
printed_SiO2_Palik_lossless = str(td.material_library["SiO2"].variants["Palik_Lossless"])
89+
90+
assert (
91+
"eps_inf: 1.5385442336875639" in printed_SiO2_Palik_lossless
92+
), "Expected eps_inf in SiO2 printed string"
93+
assert "poles: 2" in printed_SiO2_Palik_lossless, "Expected 1 pole in SiO2 printed string"
94+
95+
printed_SiO2_Palik_lossy = str(td.material_library["SiO2"].variants["Palik_Lossy"])
96+
97+
assert (
98+
"eps_inf: 2.1560362571240765" in printed_SiO2_Palik_lossy
99+
), "Expected eps_inf in SiO2 printed string"
100+
assert "poles: 5" in printed_SiO2_Palik_lossy, "Expected 1 pole in SiO2 printed string"
101+
102+
103+
def test_material_str():
104+
"""Test one of the materials for some expected output in variant printing."""
105+
106+
printed_Ag = str(td.material_library["Ag"])
107+
108+
assert (
109+
"Default Variant: Rakic1998BB" in printed_Ag
110+
), "Expected default variant in printed string"
111+
assert "RakicLorentzDrude1998" in printed_Ag, "Expected variant in printed string"
112+
113+
printed_Au = str(td.material_library["Au"])
114+
115+
assert "Olmon2012evaporated" in printed_Au, "Expected default variant in printed string"
116+
assert "Olmon2012evaporated" in printed_Au, "Expected variant in printed string"
117+
118+
119+
def test_material_library_str():
120+
"""Test the material library string method for expected output."""
121+
122+
printed_library = str(td.material_library)
123+
124+
assert (
125+
"Key: Polycarbonate, Name: Polycarbonate, Default Variant: Sultanova2009, # Variants: 2"
126+
in printed_library
127+
), "Expected information in material library"
128+
assert (
129+
"- Key: WS2, Name: Tungsten Disulfide, Default Variant: Li2014, # Variants: 1"
130+
in printed_library
131+
), "Expected information in material library"
132+
assert "- Key: graphene" in printed_library, "Expected information in material library"

tidy3d/components/medium.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,13 @@ def derivative_eps_complex_volume(
14181418

14191419
return vjp_value.sum("f")
14201420

1421+
def __repr__(self):
1422+
"""If the medium has a name, use it as the representation. Otherwise, use the default representation."""
1423+
if self.name:
1424+
return self.name
1425+
else:
1426+
return super().__repr__()
1427+
14211428

14221429
class AbstractCustomMedium(AbstractMedium, ABC):
14231430
"""A spatially varying medium."""
@@ -7244,7 +7251,7 @@ def from_dispersive_medium(cls, medium: DispersiveMedium, thickness: float) -> M
72447251
The 2D equivalent of the given 3D medium.
72457252
"""
72467253
med = cls._weighted_avg([medium], [thickness])
7247-
return Medium2D(ss=med, tt=med, frequency_range=medium.frequency_range)
7254+
return Medium2D(ss=med, tt=med, frequency_range=medium.frequency_range, name=medium.name)
72487255

72497256
@classmethod
72507257
def from_anisotropic_medium(

tidy3d/components/viz.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
# Arrow length in inches
4141
ARROW_LENGTH = 0.3
4242

43+
FLEXCOMPUTE_COLORS = {
44+
"brand_green": 0x00643C,
45+
"brand_tan": 0xB8A18B,
46+
"brand_blue": 0x6DB5DD,
47+
"brand_purple": 0x8851AD,
48+
"brand_black": 0x000000,
49+
}
50+
4351
""" Decorators """
4452

4553

0 commit comments

Comments
 (0)