Ejemplo n.º 1
0
def test_make_a_to_b():

    A = bgt.new("Ce", library="BioChem", value=[1, 1, 1])
    B = bgt.new("Ce", library="BioChem", value=[1, 1, 1])

    Re = bgt.new("Re", library="BioChem", value={"R": 1, "T": 1})
    a_to_b = bgt.new()
    a_to_b.add(A, Re, B)

    connect(A, (Re, 0))
    connect(B, (Re, 1))

    with pytest.raises(InvalidPortException):
        connect(A, Re)

    assert Re.control_vars == ['r']
    assert list(a_to_b.state_vars.keys()) == ['x_0', 'x_1']
    assert list(a_to_b.control_vars.keys()) == ['u_0']
    assert not a_to_b.ports
    
    state_basis, _, control_basis = a_to_b.basis_vectors
    (x_A, dx_A), = (k for k, (v, _) in state_basis.items() if v is A)
    (x_B, dx_B), = (k for k, (v, _) in state_basis.items() if v is B)
    (r,) = (k for k, (v, _) in control_basis.items() if v is Re)

    solutions = {dx_A + r*x_A - r*x_B, dx_B - r*x_A + r*x_B}
    relations = set(a_to_b.constitutive_relations)
    assert not solutions ^ relations
def rename(module, names, quiet=False):
    """ Renames components within module according to dict names
    """

    if not quiet:
        print("Renaming components within", module.name)

    ## Find list of all names in this module
    nameList = []
    for comp in module.components:
        if not comp.metamodel in ["0", "1"]:
            #print(comp.metamodel,comp.name)
            nameList.append(comp.name)

    for key, name in names.items():
        if not name in nameList:
            if not quiet:
                print("\tRenaming", key, "to", name)
            (module / key).name = name
        else:
            print("\t" + name, "exists in", module.name + ": connecting")

            ## Find what current components called key and name are attached to
            for bond in module.bonds:
                if bond.head.component.name is name:
                    nameJun = bond.tail.component
                if bond.head.component.name is key:
                    keyJun = bond.tail.component

            keyComp = module / key
            bgt.connect(keyJun, nameJun)
            bgt.disconnect(keyJun, keyComp)
            module.remove(keyComp)
Ejemplo n.º 3
0
def test_equal():

    c_1 = bgt.new("C", value=1)
    c_2 = bgt.new("C", value=1)

    assert c_1 == c_2
    assert c_1 is c_1
    assert c_1 is not c_2
Ejemplo n.º 4
0
    def test_remove_components_fail_states(self):
        zero = bgt.new("0")
        r = bgt.new("R", value=1)
        c = bgt.new("C", value=1)

        bg = bgt.new()
        bg.add([zero, r])

        with pytest.raises(InvalidComponentException):
            bg.remove(c)
Ejemplo n.º 5
0
def test_set_compound_param():

    j = bgt.new("0")
    c = bgt.new("C")
    se = bgt.new("Se")

    bg = bgt.new()
    bg.add([c, se, j])

    assert len(bg.params) == 2

    bg.params[0] = 1
Ejemplo n.º 6
0
def test_build_and_drive(rlc):
    se = bgt.new("Se")
    assert len(se.control_vars) == 1
    rlc.add(se)

    for comp in rlc.components:
        if comp.metamodel == "0":
            bgt.connect(se, comp)
            break

    assert len(rlc.bonds) == 4
    assert len(rlc.control_vars) == 1
Ejemplo n.º 7
0
def test_se():

    Se = bgt.new('Se', value=1)
    c = bgt.new('C', value=1)
    vc = bgt.new()
    vc.add([Se, c])
    assert Se.constitutive_relations == [sympy.sympify("e_0 - 1")]
    bgt.connect(Se, c)


    assert vc.constitutive_relations == [sympy.sympify("dx_0"),
                                         sympy.sympify("x_0 - 1")]
Ejemplo n.º 8
0
    def test_swap_failstate(self):
        zero = bgt.new("0")
        r = bgt.new("R", value=1)
        c = bgt.new("C", value=1)
        l = bgt.new("I")

        bg = bgt.new()
        bg.add([zero, r, c])
        connect(r, zero)
        connect(c, zero)

        with pytest.raises(InvalidPortException):
            swap(zero, l)
Ejemplo n.º 9
0
def test_disconnect_component():
    zero = bgt.new("0")
    r = bgt.new("R")
    c = bgt.new("C")
    one = bgt.new("1")

    bg = bgt.new()
    bg.add([zero, r, c, one])

    connect(c, (one, one.non_inverting))

    with pytest.raises(TypeError):
        disconnect(c)
Ejemplo n.º 10
0
def model():
    """ 
    Model of chloroplast electron transport chain
    """

    ETC = bgt.new(name='ETC')  # Create system
    PII = PII_abg.model()
    Cyt = Cyt_abg.model()
    PI = PI_abg.model()
    Fer = Fer_abg.model()
    bgt.add(ETC, PII, Cyt, PI, Fer)

    return ETC
Ejemplo n.º 11
0
def test_build_junction_dict():
    c = bgt.new("C")
    kvl = bgt.new("0")
    bg = bgt.new()
    bg.add([c, kvl])
    connect(kvl, c)
    cp, kp = list(c.ports) + list(kvl.ports)
    index_map = {cp: 0, kp: 1}
    M = adjacency_to_dict(index_map, bg.bonds, offset=1)
    assert M[(0, 1)] == 1
    assert M[(0, 3)] == -1
    assert M[(1, 2)] == 1
    assert M[(1, 4)] == 1
Ejemplo n.º 12
0
def test_cv_relations():
    c = bgt.new("C", value=1)
    se = bgt.new("Se")
    r = bgt.new("R", value=1)
    kcl = bgt.new("1")
    bg = bgt.new()
    bg.add([c, se, kcl, r])

    connect(c, (kcl, kcl.non_inverting))
    connect(r, (kcl, kcl.non_inverting))
    connect(se, (kcl, kcl.non_inverting))

    assert bg.constitutive_relations == [sympy.sympify("dx_0 + u_0 + x_0")]
Ejemplo n.º 13
0
    def test_connect_components(self):
        c = bgt.new("C")
        se = bgt.new("Se")

        bg = bgt.new()
        bg.add([c, se])

        connect(c, se)
        bond = list(bg.bonds)[0]
        assert isinstance(bond.tail.component, Component)
        assert isinstance(bond.head.component, Component)
        assert bond.head.component in (c, se)
        assert bond.tail.component in (c, se)
    def test_swap_component_1(self):
        ###
        # We must be able to swap a 1-port for a 1-port
        #
        #

        zero = bgt.new("0")
        r = bgt.new("R", value=1)
        c = bgt.new("C", value=1)

        bg = bgt.new()
        bg.add([zero, r, c])

        connect(r, zero)
        connect(c, zero)
        assert c in bg.components
        r_p, = r.ports
        c_p, = c.ports

        z0, z1, = zero.ports

        assert bg.bonds == [
            (r_p, z0),
            (c_p, z1)
        ]

        assert len(bg.state_vars) == 1
        Sf = bgt.new('Sf')
        swap(c, Sf)
        sf_port, = Sf.ports
        assert len(bg.state_vars) == 0
        assert len(bg.control_vars) == 1

        assert bg.bonds == [
            (r_p, z0),
            (sf_port, z1)
        ]

        assert c not in bg.components
        assert Sf in bg.components

        swap(Sf, c)

        assert bg.bonds == [
            (r_p, z0),
            (c_p, z1)
        ]

        assert c in bg.components
        assert Sf not in bg.components
Ejemplo n.º 15
0
def test_parallel_crv_relations():
    c = bgt.new("C", value=1)
    se = bgt.new("Se")
    r = bgt.new("R", value=1)
    kcl = bgt.new("0")
    bg = bgt.new()
    bg.add([c, se, kcl, r])

    connect(c, kcl)
    connect(se, kcl)
    connect(r, kcl)

    assert bg.constitutive_relations == [sympy.sympify("dx_0 - du_0"),
                                         sympy.sympify("x_0 - u_0")]
def unify(model, common=[], metamodel="C", quiet=False):
    """ Unify C components (species) listed in common
    """
    ## Setup strings
    junStr, CeStr, bondStr, ReStr = setStr()

    if not quiet:
        print("Unifying components in:", model.name)

    ## Create the common components at this level
    for Name in common:
        junName = Name + '_jun'
        if not quiet:
            print("Creating:", "Ce:" + Name, "and", "0:" + junName, "in",
                  model.name)
        exec(CeStr.format(Name))
        exec(junStr.format(junName, '0'))
        exec(bondStr.format(junName, Name))

    ## Replace common components in subsystems by ports
    for subsystem in model.components:
        #print(subsystem.metamodel,subsystem.name)

        if subsystem.metamodel in ["BG"]:
            for comp in subsystem.components:
                if comp.metamodel in ["BG"]:
                    if not quiet:
                        print("unify:", comp.name)
                    #unify(comp,common=common,metamodel=metamodel)

                if ((comp.metamodel in [metamodel]) and (comp.name in common)):
                    #out = compPort(subsystem,comp)
                    #print("Replacing", subsystem.name,":",comp.name,
                    #      "with a port.")
                    ## Expose as port
                    bgt.expose(comp, comp.name)

                    ## Connect port to junction
                    port = (subsystem, comp.name)
                    junName = comp.name + '_jun'
                    for com in model.components:
                        if com.name in [junName]:
                            jun = com
                    bgt.connect(port, jun)

                    if not quiet:
                        print("Exposing:", "Ce:" + comp.name, "in",
                              subsystem.name, "and connecting to",
                              "0:" + jun.name)
Ejemplo n.º 17
0
def test_make_diode():

    d = bgt.new("Di", library="elec", value={"Is": 5 * 10**-6})
    # todo: fix this
    # assert d.metamodel == "Di"
    assert len(d.ports) == 1
    assert d.params["Is"]["value"] == 5 * 10**-6
Ejemplo n.º 18
0
def test_simulation():
    model = four_site_phos_model()
    set_4site_model_parameters(model)

    # Initial concentrations (unit nM)
    S_tot = 1e4
    E_tot = 2.8e3
    F_tot = 2.8e3

    species = [comp.name for comp, q in model.state_vars.values()]
    idx_E = species.index("E")
    idx_F = species.index("F")
    idx_S0 = species.index("S0")
    idx_S4 = species.index("S4")

    alpha_vals = [0.0, 0.5, 0.9]
    expected_S4 = [6077.03288712802, 2590.0823948000684, 6.480921602618817e-07]

    for alpha, S4_end in zip(alpha_vals, expected_S4):
        x0 = np.zeros(len(model.state_vars))
        x0[idx_E] = E_tot
        x0[idx_F] = F_tot
        x0[idx_S0] = alpha * S_tot
        x0[idx_S4] = (1 - alpha) * S_tot

        # Use different time steps for small and large time
        tspan = (0.0, 1000.0)
        t, x = bgt.simulate(model, tspan, x0, dt=1)
        assert x[-1][idx_S4] == pytest.approx(S4_end, abs=1e-6)
Ejemplo n.º 19
0
def test_make_a_to_b_inplicit():

    A = bgt.new("Ce", library="BioChem", value=[1, 1, 1])
    B = bgt.new("Ce", library="BioChem", value=[1, 1, 1])

    Re = bgt.new("Re", library="BioChem", value={"R": 1, "T": 1})
    a_to_b = bgt.new()
    a_to_b.add(A, Re, B)

    connect(A, Re)
    connect(B, Re)

    assert Re.control_vars == ['r']
    assert list(a_to_b.state_vars.keys()) == ['x_0', 'x_1']
    assert list(a_to_b.control_vars.keys()) == ['u_0']
    assert not a_to_b.ports
Ejemplo n.º 20
0
def test_build_relations():
    c = bgt.new("C")
    eqns = c._build_relations()

    test_eqn = {sympy.sympify("q_0 - C*e_0"), sympy.sympify("dq_0 - f_0")}

    assert set(eqns) == test_eqn
Ejemplo n.º 21
0
def test_disconnect_multiport():
    zero = bgt.new("0")
    r = bgt.new("R")
    c = bgt.new("C")
    one = bgt.new("1")
    bg = bgt.new()
    bg.add([zero, r, c, one])

    connect(zero, one.non_inverting)
    connect(r, zero)
    connect(c, one.inverting)

    assert len(bg.bonds) == 3
    disconnect(zero, one)
    assert len(bg.bonds) == 2
    assert ((zero, 0), (one, 0)) not in bg.bonds
Ejemplo n.º 22
0
def test_add():

    c = bgt.new("C")
    se = bgt.new("Se")

    bg = bgt.new()
    bg.add([c, se])

    assert len(bg.internal_ports) == 2
    assert len(bg.state_vars) == 1

    assert bg is not c
    assert bg is not se

    assert c in bg.components
    assert se in bg.components
Ejemplo n.º 23
0
def test_new():
    c = bgt.new("C")

    assert isinstance(c, Component)
    assert len(c.ports) == 1
    assert len(c.state_vars) == 1
    assert len(c.params) == 1
Ejemplo n.º 24
0
    def test_connect(self):
        c = bgt.new("C")
        se = bgt.new("Se")

        bg = bgt.new()
        bg.add([c, se])

        k1, k2 = tuple(bg.components)

        connect(k1, k2)

        (c1, p1), (c2, p2) = list(bg.bonds)[0]
        assert isinstance(c1, Component)
        assert isinstance(c2, Component)
        assert c1 in (c, se)
        assert c2 in (c, se)
Ejemplo n.º 25
0
    def test_connect_ports(self):
        c = bgt.new("C")
        se = bgt.new("Se")

        bg = bgt.new()
        bg.add([c, se])

        k1 = bg.internal_ports[0]
        k2 = bg.internal_ports[1]

        connect(k1, k2)

        (c1, p1), (c2, p2) = list(bg.bonds)[0]
        assert isinstance(c1, Component)
        assert isinstance(c2, Component)
        assert c1 in (c, se)
        assert c2 in (c, se)
Ejemplo n.º 26
0
def test_many_port():

    j = bgt.new("0")
    c = bgt.new("C")
    se = bgt.new("Se")

    bg = bgt.new()
    bg.add([c, se, j])

    connect(c, j)
    connect(se, j)

    for (c1, p1), (c2, p2) in bg.bonds:
        assert c1 in (c, se)
        assert c2 is j

    assert len(j.ports) == 2
Ejemplo n.º 27
0
def test_connect_unadded():

    j = bgt.new("0")
    c = bgt.new("C")
    se = bgt.new("Se")

    bg = bgt.new()
    bg.add([c, j])

    connect(c, j)

    assert j in bg.components
    assert c in bg.components
    assert j is not se

    with pytest.raises(InvalidComponentException):
        connect(se, j)
Ejemplo n.º 28
0
def test_diode_model():

    d = bgt.new("Di", library="elec", value=[1, 1, 1])

    assert d.basis_vectors
    assert not d.control_vars

    assert d.constitutive_relations[0] == sp.sympify('f_0 - exp(e_0) +1')
Ejemplo n.º 29
0
def test_ab_to_c_model():

    A = bgt.new("Ce", library="BioChem", value=[1, 1, 1])
    B = bgt.new("Ce", library="BioChem", value=[1, 1, 1])
    C = bgt.new("Ce", library="BioChem", value=[1, 1, 1])
    Re = bgt.new("Re", library="BioChem", value={'r': 1, "R": 1, "T": 1})
    Y_AB = bgt.new('1')
    Y_C = bgt.new('1')

    bg = bgt.new()
    bg.add(A, B, Re, Y_AB, C, Y_C)
    connect(A, Y_AB)
    connect(B, Y_AB)
    connect(Y_AB, Re)
    connect(Re, Y_C)
    connect(Y_C, C)

    state_basis, _, _ = bg.basis_vectors
    (x_0, dx_0), = (k for k, (v, _) in state_basis.items() if v is A)
    (x_1, dx_1), = (k for k, (v, _) in state_basis.items() if v is B)
    (x_2, dx_2), = (k for k, (v, _) in state_basis.items() if v is C)

    eqns = {
        dx_0 + x_0 * x_1 - x_2, dx_1 + x_0 * x_1 - x_2, dx_2 - x_0 * x_1 + x_2
    }

    relations = set(bg.constitutive_relations)
    assert not relations ^ eqns
Ejemplo n.º 30
0
def test_ported_cr():
    model = bgt.new()
    Sf = bgt.new('Sf', name="Sf")
    R = bgt.new("R", value=2)
    zero = bgt.new("0")
    ss = bgt.new("SS")

    model.add(Sf, R, zero, ss)
    connect(Sf, zero)
    connect(R, zero)
    connect(ss, zero)

    bgt.expose(ss, 'A')
    assert len(model.control_vars) == 1

    ts, ps, cs = model._build_internal_basis_vectors()
    assert len(cs) == 1
    assert len(ps) == 7
    assert len(ts) == 0

    mapping, coords = inverse_coord_maps(ts, ps, cs)
    assert len(coords) == 15

    coords, mappings, lin_op, nl_op, conttr = model.system_model()
    assert nl_op.is_zero_matrix
    assert not conttr

    assert model.constitutive_relations == [
        sympy.sympify('e_0 - 2*f_0 - 2*u_0')
    ]