|
| 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" |
0 commit comments