7
7
import pytest
8
8
9
9
import tidy3d as td
10
+ from tidy3d import IndexSimulationData
10
11
from tidy3d .exceptions import SetupError , Tidy3dKeyError
11
- from tidy3d .plugins .smatrix import (
12
- ComponentModeler ,
13
- Port ,
14
- )
12
+ from tidy3d .plugins .smatrix import ComponentModeler , ComponentModelerData , Port
15
13
from tidy3d .web .api .container import Batch
16
14
17
15
from ...utils import run_emulated
@@ -193,12 +191,20 @@ def make_component_modeler(**kwargs):
193
191
return ComponentModeler (simulation = sim , ports = ports , freqs = sim .monitors [0 ].freqs , ** kwargs )
194
192
195
193
196
- def run_component_modeler (monkeypatch , modeler : ComponentModeler ):
194
+ def run_component_modeler (monkeypatch , modeler : ComponentModeler ) -> ComponentModelerData :
197
195
sim_dict = modeler .sim_dict
198
196
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
197
+ port_data = IndexSimulationData (
198
+ index = list (batch_data .keys ()),
199
+ data = list (batch_data .values ()),
200
+ )
201
+ modeler_data = ComponentModelerData (modeler = modeler , data = port_data )
202
+ return modeler_data
203
+
204
+
205
+ def get_port_data_array (monkeypatch , modeler : ComponentModeler ):
206
+ modeler_data = run_component_modeler (monkeypatch = monkeypatch , modeler = modeler )
207
+ return modeler_data .smatrix .data
202
208
203
209
204
210
def test_validate_no_sources ():
@@ -244,7 +250,9 @@ def test_ports_too_close_boundary():
244
250
def test_validate_batch_supplied (tmp_path ):
245
251
sim = make_coupler ()
246
252
_ = ComponentModeler (
247
- simulation = sim , ports = [], freqs = sim .monitors [0 ].freqs , path_dir = str (tmp_path )
253
+ simulation = sim ,
254
+ ports = [],
255
+ freqs = sim .monitors [0 ].freqs ,
248
256
)
249
257
250
258
@@ -266,13 +274,13 @@ def test_make_component_modeler():
266
274
267
275
def test_run (monkeypatch ):
268
276
modeler = make_component_modeler ()
269
- monkeypatch .setattr (ComponentModeler , "run" , lambda self , path_dir = None : None )
270
- modeler .run ()
277
+ _ = run_component_modeler (monkeypatch , modeler = modeler )
271
278
272
279
273
280
def test_run_component_modeler (monkeypatch ):
274
281
modeler = make_component_modeler ()
275
- s_matrix = run_component_modeler (monkeypatch , modeler )
282
+ modeler_data = run_component_modeler (monkeypatch , modeler = modeler )
283
+ s_matrix = modeler_data .smatrix
276
284
277
285
for port_in in modeler .ports :
278
286
for mode_index_in in range (port_in .mode_spec .num_modes ):
@@ -295,7 +303,8 @@ def test_component_modeler_run_only(monkeypatch):
295
303
ONLY_SOURCE = (port_run_only , mode_index_run_only ) = ("right_bot" , 0 )
296
304
run_only = [ONLY_SOURCE ]
297
305
modeler = make_component_modeler (run_only = run_only )
298
- s_matrix = run_component_modeler (monkeypatch , modeler )
306
+ modeler_data = run_component_modeler (monkeypatch , modeler = modeler )
307
+ s_matrix = modeler_data .smatrix
299
308
300
309
coords_in_run_only = {"port_in" : port_run_only , "mode_index_in" : mode_index_run_only }
301
310
@@ -340,7 +349,8 @@ def test_run_component_modeler_mappings(monkeypatch):
340
349
((("left_bot" , 0 ), ("right_top" , 0 )), (("left_top" , 0 ), ("right_bot" , 0 )), + 1 ),
341
350
)
342
351
modeler = make_component_modeler (element_mappings = element_mappings )
343
- s_matrix = run_component_modeler (monkeypatch , modeler )
352
+ modeler_data = run_component_modeler (monkeypatch , modeler = modeler )
353
+ s_matrix = modeler_data .smatrix
344
354
_test_mappings (element_mappings , s_matrix )
345
355
346
356
@@ -366,11 +376,12 @@ def test_mapping_exclusion(monkeypatch):
366
376
element_mappings .append (mapping )
367
377
368
378
modeler = make_component_modeler (element_mappings = element_mappings )
379
+ modeler_data = run_component_modeler (monkeypatch , modeler = modeler )
380
+ s_matrix = modeler_data .smatrix
369
381
370
382
run_sim_indices = modeler .matrix_indices_run_sim
371
383
assert EXCLUDE_INDEX not in run_sim_indices , "mapping didnt exclude row properly"
372
384
373
- s_matrix = run_component_modeler (monkeypatch , modeler )
374
385
_test_mappings (element_mappings , s_matrix )
375
386
376
387
@@ -403,47 +414,45 @@ def test_mapping_with_run_only():
403
414
_ = make_component_modeler (element_mappings = element_mappings , run_only = run_only )
404
415
405
416
406
- def test_batch_filename (tmp_path ):
407
- modeler = make_component_modeler ()
408
- path = modeler ._batch_path
409
- assert path
410
-
411
-
412
- def test_import_smatrix_smatrix ():
413
- from tidy3d .plugins .smatrix .smatrix import ComponentModeler , Port # noqa: F401
414
-
415
-
416
- def test_to_from_file_empty_batch (tmp_path ):
417
- modeler = make_component_modeler ()
418
-
419
- fname = str (tmp_path ) + "/modeler.json"
420
-
421
- modeler .to_file (fname )
422
- modeler2 = modeler .from_file (fname )
423
-
424
- assert modeler2 .batch_cached is None
425
-
426
-
427
- def test_to_from_file_batch (tmp_path , monkeypatch ):
428
- modeler = make_component_modeler ()
429
- _ = run_component_modeler (monkeypatch , modeler )
430
-
431
- batch = td .web .Batch (simulations = {})
432
-
433
- modeler ._cached_properties ["batch" ] = batch
434
-
435
- fname = str (tmp_path ) + "/modeler.json"
436
-
437
- modeler .to_file (fname )
438
- modeler2 = modeler .from_file (fname )
439
-
440
- assert modeler2 .batch_cached == modeler2 .batch == batch
441
-
442
-
443
- def test_non_default_path_dir (monkeypatch ):
444
- modeler = make_component_modeler (path_dir = "not_default" )
445
- monkeypatch .setattr (ComponentModeler , "_construct_smatrix" , lambda self : None )
446
- modeler .run ()
447
- modeler .run (path_dir = "not_default" )
448
- with pytest .raises (ValueError ):
449
- modeler .run (path_dir = "a_new_path" )
417
+ # def test_batch_filename(tmp_path):
418
+ # modeler = make_component_modeler()
419
+ # path = modeler._batch_path
420
+ # assert path
421
+ # def test_import_smatrix_smatrix():
422
+ # from tidy3d.plugins.smatrix.smatrix import ComponentModeler, Port
423
+
424
+ # def test_to_from_file_empty_batch(tmp_path):
425
+ # modeler = make_component_modeler()
426
+ #
427
+ # fname = str(tmp_path) + "/modeler.json"
428
+ #
429
+ # modeler.to_file(fname)
430
+ # modeler2 = modeler.from_file(fname)
431
+ #
432
+ # assert modeler2.batch_cached is None
433
+ #
434
+ #
435
+ # def test_to_from_file_batch(tmp_path, monkeypatch):
436
+ # modeler = make_component_modeler()
437
+ # _ = run_component_modeler(monkeypatch, modeler)
438
+ #
439
+ # batch = td.web.Batch(simulations={})
440
+ #
441
+ # modeler._cached_properties["batch"] = batch
442
+ #
443
+ # fname = str(tmp_path) + "/modeler.json"
444
+ #
445
+ # modeler.to_file(fname)
446
+ # modeler2 = modeler.from_file(fname)
447
+ #
448
+ # # BREAK this test because it introduces mutability which shouldn't exist
449
+ # assert modeler2.batch_cached == modeler2.batch == batch
450
+ #
451
+ #
452
+ # def test_non_default_path_dir(monkeypatch):
453
+ # modeler = make_component_modeler(path_dir="not_default")
454
+ # monkeypatch.setattr(ComponentModeler, "_construct_smatrix", lambda self: None)
455
+ # modeler.run()
456
+ # modeler.run(path_dir="not_default")
457
+ # with pytest.raises(ValueError):
458
+ # modeler.run(path_dir="a_new_path")
0 commit comments