8
8
9
9
import tidy3d as td
10
10
from tidy3d .exceptions import SetupError , Tidy3dKeyError
11
- from tidy3d .plugins .smatrix import (
12
- ComponentModeler ,
13
- Port ,
14
- )
11
+ from tidy3d .plugins .smatrix import ComponentModeler , ComponentModelerData , Port , PortSimulationData
15
12
from tidy3d .web .api .container import Batch
16
13
17
14
from ...utils import run_emulated
@@ -193,12 +190,20 @@ def make_component_modeler(**kwargs):
193
190
return ComponentModeler (simulation = sim , ports = ports , freqs = sim .monitors [0 ].freqs , ** kwargs )
194
191
195
192
196
- def run_component_modeler (monkeypatch , modeler : ComponentModeler ):
193
+ def run_component_modeler (monkeypatch , modeler : ComponentModeler ) -> ComponentModelerData :
197
194
sim_dict = modeler .sim_dict
198
195
batch_data = {task_name : run_emulated (sim ) for task_name , sim in sim_dict .items ()}
199
- monkeypatch .setattr (ComponentModeler , "batch_data" , property (lambda self : batch_data ))
200
- s_matrix = modeler ._construct_smatrix ()
201
- return s_matrix
196
+ port_data = PortSimulationData (
197
+ ports = [port for port in batch_data .keys ()],
198
+ data = [sim_data for sim_data in batch_data .values ()],
199
+ )
200
+ modeler_data = ComponentModelerData (modeler = modeler , data = port_data )
201
+ return modeler_data
202
+
203
+
204
+ def get_port_data_array (monkeypatch , modeler : ComponentModeler ) -> TerminalPortDataArray :
205
+ modeler_data = run_component_modeler (monkeypatch = monkeypatch , modeler = modeler )
206
+ return modeler_data .smatrix .data
202
207
203
208
204
209
def test_validate_no_sources ():
@@ -244,7 +249,9 @@ def test_ports_too_close_boundary():
244
249
def test_validate_batch_supplied (tmp_path ):
245
250
sim = make_coupler ()
246
251
_ = ComponentModeler (
247
- simulation = sim , ports = [], freqs = sim .monitors [0 ].freqs , path_dir = str (tmp_path )
252
+ simulation = sim ,
253
+ ports = [],
254
+ freqs = sim .monitors [0 ].freqs ,
248
255
)
249
256
250
257
@@ -266,13 +273,12 @@ def test_make_component_modeler():
266
273
267
274
def test_run (monkeypatch ):
268
275
modeler = make_component_modeler ()
269
- monkeypatch .setattr (ComponentModeler , "run" , lambda self , path_dir = None : None )
270
- modeler .run ()
276
+ _ = run_component_modeler (monkeypatch , modeler = modeler )
271
277
272
278
273
279
def test_run_component_modeler (monkeypatch ):
274
280
modeler = make_component_modeler ()
275
- s_matrix = run_component_modeler (monkeypatch , modeler )
281
+ s_matrix = get_port_data_array (monkeypatch , modeler )
276
282
277
283
for port_in in modeler .ports :
278
284
for mode_index_in in range (port_in .mode_spec .num_modes ):
@@ -295,7 +301,7 @@ def test_component_modeler_run_only(monkeypatch):
295
301
ONLY_SOURCE = (port_run_only , mode_index_run_only ) = ("right_bot" , 0 )
296
302
run_only = [ONLY_SOURCE ]
297
303
modeler = make_component_modeler (run_only = run_only )
298
- s_matrix = run_component_modeler (monkeypatch , modeler )
304
+ s_matrix = get_port_data_array (monkeypatch , modeler )
299
305
300
306
coords_in_run_only = {"port_in" : port_run_only , "mode_index_in" : mode_index_run_only }
301
307
@@ -340,7 +346,7 @@ def test_run_component_modeler_mappings(monkeypatch):
340
346
((("left_bot" , 0 ), ("right_top" , 0 )), (("left_top" , 0 ), ("right_bot" , 0 )), + 1 ),
341
347
)
342
348
modeler = make_component_modeler (element_mappings = element_mappings )
343
- s_matrix = run_component_modeler (monkeypatch , modeler )
349
+ s_matrix = get_port_data_array (monkeypatch , modeler )
344
350
_test_mappings (element_mappings , s_matrix )
345
351
346
352
@@ -370,52 +376,49 @@ def test_mapping_exclusion(monkeypatch):
370
376
run_sim_indices = modeler .matrix_indices_run_sim
371
377
assert EXCLUDE_INDEX not in run_sim_indices , "mapping didnt exclude row properly"
372
378
373
- s_matrix = run_component_modeler (monkeypatch , modeler )
379
+ s_matrix = get_port_data_array (monkeypatch , modeler )
374
380
_test_mappings (element_mappings , s_matrix )
375
381
376
382
377
- def test_batch_filename (tmp_path ):
378
- modeler = make_component_modeler ()
379
- path = modeler ._batch_path
380
- assert path
381
-
382
-
383
- def test_import_smatrix_smatrix ():
384
- from tidy3d .plugins .smatrix .smatrix import ComponentModeler , Port # noqa: F401
385
-
386
-
387
- def test_to_from_file_empty_batch (tmp_path ):
388
- modeler = make_component_modeler ()
389
-
390
- fname = str (tmp_path ) + "/modeler.json"
391
-
392
- modeler .to_file (fname )
393
- modeler2 = modeler .from_file (fname )
394
-
395
- assert modeler2 .batch_cached is None
396
-
397
-
398
- def test_to_from_file_batch (tmp_path , monkeypatch ):
399
- modeler = make_component_modeler ()
400
- _ = run_component_modeler (monkeypatch , modeler )
401
-
402
- batch = td .web .Batch (simulations = {})
403
-
404
- modeler ._cached_properties ["batch" ] = batch
405
-
406
- fname = str (tmp_path ) + "/modeler.json"
407
-
408
- modeler .to_file (fname )
409
- modeler2 = modeler .from_file (fname )
410
-
411
- # BREAK this test because it introduces mutability which shouldn't exist
412
- assert modeler2 .batch_cached == modeler2 .batch == batch
413
-
414
-
415
- def test_non_default_path_dir (monkeypatch ):
416
- modeler = make_component_modeler (path_dir = "not_default" )
417
- monkeypatch .setattr (ComponentModeler , "_construct_smatrix" , lambda self : None )
418
- modeler .run ()
419
- modeler .run (path_dir = "not_default" )
420
- with pytest .raises (ValueError ):
421
- modeler .run (path_dir = "a_new_path" )
383
+ # def test_batch_filename(tmp_path):
384
+ # modeler = make_component_modeler()
385
+ # path = modeler._batch_path
386
+ # assert path
387
+ # def test_import_smatrix_smatrix():
388
+ # from tidy3d.plugins.smatrix.smatrix import ComponentModeler, Port
389
+
390
+ # def test_to_from_file_empty_batch(tmp_path):
391
+ # modeler = make_component_modeler()
392
+ #
393
+ # fname = str(tmp_path) + "/modeler.json"
394
+ #
395
+ # modeler.to_file(fname)
396
+ # modeler2 = modeler.from_file(fname)
397
+ #
398
+ # assert modeler2.batch_cached is None
399
+ #
400
+ #
401
+ # def test_to_from_file_batch(tmp_path, monkeypatch):
402
+ # modeler = make_component_modeler()
403
+ # _ = run_component_modeler(monkeypatch, modeler)
404
+ #
405
+ # batch = td.web.Batch(simulations={})
406
+ #
407
+ # modeler._cached_properties["batch"] = batch
408
+ #
409
+ # fname = str(tmp_path) + "/modeler.json"
410
+ #
411
+ # modeler.to_file(fname)
412
+ # modeler2 = modeler.from_file(fname)
413
+ #
414
+ # # BREAK this test because it introduces mutability which shouldn't exist
415
+ # assert modeler2.batch_cached == modeler2.batch == batch
416
+ #
417
+ #
418
+ # def test_non_default_path_dir(monkeypatch):
419
+ # modeler = make_component_modeler(path_dir="not_default")
420
+ # monkeypatch.setattr(ComponentModeler, "_construct_smatrix", lambda self: None)
421
+ # modeler.run()
422
+ # modeler.run(path_dir="not_default")
423
+ # with pytest.raises(ValueError):
424
+ # modeler.run(path_dir="a_new_path")
0 commit comments