@@ -2216,15 +2216,55 @@ def _modal_plane_frames(self) -> list[Structure]:
22162216 @cached_property
22172217 def _finalized (self ) -> Simulation :
22182218 """Return the finalized version of the simulation setup. That is, including automatic frames around mode sources and internal absorbers, and 2d strutures converted into volumetric analogues."""
2219+ if (
2220+ len (self ._modal_plane_frames ) == 0
2221+ and not self ._contains_converted_volumetric_structures
2222+ ):
2223+ return self
2224+ return self .updated_copy (
2225+ grid_spec = GridSpec .from_grid (self .grid ),
2226+ structures = self ._finalized_volumetric_structures ,
2227+ )
22192228
2229+ @cached_property
2230+ def _finalized_volumetric_structures (self ) -> list [Structure ]:
2231+ """Volumetric structures in the simulation, including automatic frames around mode sources and internal absorbers, and 2d strutures converted into volumetric analogues."""
22202232 modal_frames = self ._modal_plane_frames
2233+ if not self ._contains_converted_volumetric_structures :
2234+ return list (self .structures ) + modal_frames
2235+ return list (self .volumetric_structures ) + modal_frames
22212236
2222- if len (modal_frames ) == 0 and not self ._contains_converted_volumetric_structures :
2223- return self
2237+ @cached_property
2238+ def _finalized_optical_medium_map (self ) -> dict [MediumType , pydantic .NonNegativeInt ]:
2239+ """Returns dict mapping medium to index in material in finalized simulation.
22242240
2225- structures = list (self .volumetric_structures ) + modal_frames
2241+ Returns
2242+ -------
2243+ Dict[:class:`.AbstractMedium`, int]
2244+ Mapping between distinct mediums to index in finalized simulation.
2245+ """
2246+ medium_set = {
2247+ structure ._optical_medium for structure in self ._finalized_volumetric_structures
2248+ }
2249+ medium_set .add (Structure ._get_optical_medium (self .medium ))
2250+ return {medium : index for index , medium in enumerate (medium_set )}
22262251
2227- return self .updated_copy (grid_spec = GridSpec .from_grid (self .grid ), structures = structures )
2252+ @cached_property
2253+ def _finalized_full_optical_medium_map (self ) -> dict [MediumType , pydantic .NonNegativeInt ]:
2254+ """Returns dict mapping medium to index in material in finalized simulation, including
2255+ parent medium.
2256+ """
2257+ full_medium_map = dict (self ._finalized_optical_medium_map )
2258+ next_index = len (full_medium_map )
2259+ for medium in self ._finalized_optical_medium_map .keys ():
2260+ if (
2261+ isinstance (medium , AbstractCustomMedium )
2262+ and medium .derived_from is not None
2263+ and medium .derived_from not in full_medium_map
2264+ ):
2265+ full_medium_map [medium .derived_from ] = next_index
2266+ next_index += 1
2267+ return full_medium_map
22282268
22292269 def _validate_finalized (self ) -> None :
22302270 """Validate that after adding pec frames simulation setup is still valid."""
0 commit comments