def test_load_model_from_modular(): Vs = load(file_path / "modular.bg", model='source', as_name="Source 1") Vs2 = load(file_path / "modular.bg", model='source', as_name="Source 2") assert Vs is not Vs2 assert len(Vs.components) == 3 assert len(Vs2.components) == 3
def test_save_idempotent(): model_1 = load(file_path / "modular.bg") new_file = str(file_path / "modular_2.bg") with TempFile(new_file): save(model_1, new_file) model_2 = load(new_file) assert model_1 is not model_2 assert model_1.name == model_2.name assert_components_are_equal(model_1, model_2)
def test_load_modular(): model_1 = load(file_path / "modular.bg") assert model_1.uri == "system:" assert model_1.name == "system" tree = set() def uri_tree(bg): tree.add(bg.uri) try: for c in bg.components: uri_tree(c) except AttributeError: pass return uri_tree(model_1) assert tree == { "system:", "system:/Vs", "system:/Z", "system:/kvl", "system:/Vs/Sf", "system:/Vs/pout", "system:/Vs/kvl", "system:/Z/R1", "system:/Z/pin", "system:/Z/kvl", "system:/Z/L1", "system:/Z/C1" }
def test_modularity(): model_1 = load(file_path / "modular.bg") Vs = model_1 / "Vs" Z = model_1 / "Z" Vs_cr = Vs.constitutive_relations assert Vs_cr == [sp.sympify('f_0 + u_0')] assert (set(model_1.constitutive_relations) == { sp.sympify("dx_0 - x_1"), sp.sympify("dx_1 - u_0 +x_0 + x_1") }) or (set(model_1.constitutive_relations) == {sp.sympify("dx_1 - x_0"), sp.sympify("dx_0 - u_0 +x_0 + x_1")})
def test_load_modular(): model_1 = load(file_path / "modular.bg") assert model_1.name == "system" assert {c.name for c in model_1.components} == {"Vs", "Z", "kvl"} for c in model_1.components: assert c assert c.parent is model_1 Vs = model_1 / "Vs" Z = model_1 / "Z" kvl = model_1 / "kvl" assert Vs in model_1.components assert len(model_1.bonds) == 2 assert len(Vs.ports) == 1 assert list(Vs.ports)[0].name == 'A' assert Vs.state_vars == {} assert len(Vs.control_vars) == 1
def test_load_rlc_parallel(): path = str(file_path / 'rlc_parallel.bg') model = load(path) one, = (comp for comp in model.components if comp.metamodel == "1") assert one rel = model.constitutive_relations _, v = model.state_vars['x_0'] if str(v) != 'p_0': eq1 = sp.sympify("dx_0 - x_1") eq2 = sp.sympify("dx_1 - u_0 + x_0 + x_1") else: eq1 = sp.sympify("dx_1 - x_0") eq2 = sp.sympify("dx_0 - u_0 + x_0 + x_1") for r in rel: assert r in (eq1, eq2)
def test_load_rlc(): path = str(file_path / 'rlc.bg') model = load(path) uris = ["RLC:/C1", "RLC:/R1", "RLC:/L1", "RLC:/kcl", "RLC:/Sf"] c, r, l, kcl, sf = (comp for uri in uris for comp in model.components if comp.uri == uri) assert len(model.control_vars) == 1 assert 'u_0' in model.control_vars assert c.params['C']['value'] == 10 assert r.params['r']['value'] == 100 assert l.params['L']['value'] == 10 r_0, = r.ports c_0, = c.ports l_0, = l.ports sf_0, = sf.ports kcl_0, kcl_1, kcl_2, kcl_3 = kcl.ports assert set(model.bonds) == {(r_0, kcl_0), (c_0, kcl_1), (l_0, kcl_2), (sf_0, kcl_3)} eqns = { sp.sympify("dx_0 - u_0 + x_0 / 1000 + x_1 / 10"), sp.sympify("dx_1 - x_0 / 10") } eqns_2 = { sp.sympify("dx_1 - u_0 + x_1 / 1000 + x_0 / 10"), sp.sympify("dx_0 - x_1 / 10") } assert (set(model.constitutive_relations) == eqns) or \ (set(model.constitutive_relations) == eqns_2)