1
1
from __future__ import annotations
2
2
3
+ import os
4
+
3
5
from tidy3d .plugins .smatrix .component_modelers .modal import ComponentModeler
4
6
from tidy3d .plugins .smatrix .component_modelers .terminal import TerminalComponentModeler
5
7
from tidy3d .plugins .smatrix .component_modelers .types import (
6
8
ComponentModelerType ,
7
9
)
8
- from tidy3d .plugins .smatrix .data .modal import ComponentModelerData , PortSimulationMap
10
+ from tidy3d .plugins .smatrix .data .modal import ComponentModelerData , PortSimulationData
9
11
from tidy3d .plugins .smatrix .data .terminal import TerminalComponentModelerData
10
12
from tidy3d .plugins .smatrix .data .types import ComponentModelerDataType
11
13
from tidy3d .web import Batch , BatchData
@@ -19,47 +21,96 @@ def create_batch(
19
21
file_name : str = "batch.hdf5" ,
20
22
** kwargs ,
21
23
) -> Batch :
22
- batch = Batch (simulations = modeler .sim_dict , ** kwargs )
23
- batch .to_file (file_name )
24
+ """Creates a simulation Batch from a component modeler and saves it to a file.
25
+
26
+ Args:
27
+ modeler: The component modeler that defines the set of simulations.
28
+ path_dir: Directory where the batch file will be saved.
29
+ file_name: Name for the HDF5 file where the batch is stored.
30
+ **kwargs: Additional keyword arguments passed to the `Batch` constructor.
24
31
32
+ Returns:
33
+ The configured `Batch` object ready for execution.
34
+ """
35
+ filepath = os .path .join (path_dir , file_name )
36
+ batch = Batch (simulations = modeler .sim_dict , ** kwargs )
37
+ batch .to_file (filepath )
25
38
return batch
26
39
27
40
28
41
def compose_terminal_modeler_data (
29
42
modeler : TerminalComponentModeler ,
30
- batch_data : BatchData = None ,
43
+ batch_data : BatchData ,
31
44
) -> TerminalComponentModelerData :
32
- port_to_sim_data_map = {
33
- port_i : batch_data [modeler .get_task_name (port = port_i )] for port_i in modeler .ports
34
- }
35
- port_simulation_data = PortSimulationMap (data = port_to_sim_data_map )
45
+ """Assembles `TerminalComponentModelerData` from simulation results.
46
+
47
+ This function maps the simulation data from a completed batch run back to the
48
+ ports of the terminal component modeler.
49
+
50
+ Args:
51
+ modeler: The `TerminalComponentModeler` used to generate the simulations.
52
+ batch_data: The results obtained from running the simulation `Batch`.
53
+
54
+ Returns:
55
+ A `TerminalComponentModelerData` object containing the results mapped to
56
+ their respective ports.
57
+ """
58
+ ports = [modeler .get_task_name (port = port_i ) for port_i in modeler .ports ]
59
+ data = [batch_data [modeler .get_task_name (port = port_i )] for port_i in modeler .ports ]
60
+ port_simulation_data = PortSimulationData (ports = ports , data = data )
36
61
return TerminalComponentModelerData (modeler = modeler , data = port_simulation_data )
37
62
38
63
39
64
def compose_component_modeler_data (
40
65
modeler : ComponentModeler ,
41
- batch_data : BatchData = None ,
66
+ batch_data : BatchData ,
42
67
) -> ComponentModelerData :
43
- port_to_sim_data_map = {
44
- port_i : batch_data [modeler .get_task_name (port = port_i )] for port_i in modeler .ports
45
- }
46
- port_simulation_data = PortSimulationMap (data = port_to_sim_data_map )
68
+ """Assembles `ComponentModelerData` from simulation results.
69
+
70
+ This function maps the simulation data from a completed batch run back to the
71
+ ports of the component modeler.
72
+
73
+ Args:
74
+ modeler: The `ComponentModeler` used to generate the simulations.
75
+ batch_data: The results obtained from running the simulation `Batch`.
76
+
77
+ Returns:
78
+ A `ComponentModelerData` object containing the results mapped to
79
+ their respective ports.
80
+ """
81
+ ports = [modeler .get_task_name (port = port_i ) for port_i in modeler .ports ]
82
+ data = [batch_data [modeler .get_task_name (port = port_i )] for port_i in modeler .ports ]
83
+ port_simulation_data = PortSimulationData (ports = ports , data = data )
47
84
return ComponentModelerData (modeler = modeler , data = port_simulation_data )
48
85
49
86
50
87
def compose_modeler_data (
51
88
modeler : ComponentModelerType ,
52
- batch_data : BatchData = None ,
89
+ batch_data : BatchData ,
53
90
) -> ComponentModelerDataType :
54
- """
55
- This method internally determines which functions to run to compose the corresponding ComponentModelerDataType.
91
+ """Selects the correct composer based on the modeler type and creates the data object.
92
+
93
+ This method acts as a dispatcher, inspecting the type of `modeler` to determine
94
+ which composer function (`compose_component_modeler_data` or
95
+ `compose_terminal_modeler_data`) to invoke.
96
+
97
+ Args:
98
+ modeler: The component modeler, which can be either a `ComponentModeler` or
99
+ a `TerminalComponentModeler`.
100
+ batch_data: The results obtained from running the simulation `Batch`.
101
+
102
+ Returns:
103
+ The appropriate `ComponentModelerDataType` object containing the simulation results.
104
+
105
+ Raises:
106
+ TypeError: If the provided `modeler` is not a recognized type.
56
107
"""
57
108
if isinstance (modeler , ComponentModeler ):
58
109
modeler_data = compose_component_modeler_data (modeler = modeler , batch_data = batch_data )
59
110
elif isinstance (modeler , TerminalComponentModeler ):
60
111
modeler_data = compose_terminal_modeler_data (modeler = modeler , batch_data = batch_data )
61
112
else :
62
- raise Exception ( "sasas " )
113
+ raise TypeError ( f"Unsupported modeler type: { type ( modeler ). __name__ } " )
63
114
64
115
return modeler_data
65
116
@@ -68,8 +119,20 @@ def run(
68
119
modeler : ComponentModelerType ,
69
120
path_dir : str = DEFAULT_DATA_DIR ,
70
121
) -> ComponentModelerDataType :
71
- """
72
- This method internally determines which functions to run to compose the corresponding ComponentModelerDataType.
122
+ """Executes the full simulation workflow for a given component modeler.
123
+
124
+ This function orchestrates the end-to-end process:
125
+ 1. Creates a `Batch` of simulations from the `modeler`.
126
+ 2. Submits the `Batch` for execution and waits for results.
127
+ 3. Composes the results into a structured `ComponentModelerDataType` object.
128
+
129
+ Args:
130
+ modeler: The component modeler defining the simulations to be run.
131
+ path_dir: The directory where the batch file will be saved.
132
+
133
+ Returns:
134
+ A `ComponentModelerDataType` object containing the processed simulation data,
135
+ ready for S-parameter extraction and analysis.
73
136
"""
74
137
batch = create_batch (modeler = modeler , path_dir = path_dir )
75
138
batch_data = batch .run ()
0 commit comments