def test_component_in_bond(self): # see issues 85 c = new('C') se = new('Se') r = new('R') one = new('1') bg = new() bg.add(c, se, r, one) bonds = [(c, one.non_inverting), (se, one), (one, r)] for bond in bonds: connect(*bond) comps = [{c, one}, {se, one}, {one, r}] all_comps = {c, se, r, one} for i, bond in enumerate(bg.bonds): for component in all_comps: if component in comps[i]: assert component in bond else: assert component not in bond
def test_compare(self): from BondGraphTools.base import Bond from BondGraphTools.actions import new c = new('C') one = new('1') b_1 = Bond(head=(c, 0), tail=(one, 0)) assert b_1 == ((one, 0), (c, 0))
def test_get_exposed_port(self): from BondGraphTools.actions import new, expose model = new() port = new('SS') model.add(port) expose(port, label='port_1') port = model.get_port() assert port.index == 0 assert port.name == "port_1"
def test_cmp(self): from BondGraphTools.base import Port from BondGraphTools.actions import new c = new("C") one = new("1") port_c = Port(c, 0) port_one = Port(one, 0) assert port_c is not port_one assert port_c != port_one
def test_in(self): from BondGraphTools.base import Port from BondGraphTools.actions import new c = new("C") one = new("1") port_c = Port(c, 0) assert c in port_c assert c is not port_c assert one is not port_c assert one not in port_c
def test_create(self): from BondGraphTools.base import Bond from BondGraphTools.actions import new c = new('C') one = new('1') b_1 = Bond(head=(c, 0), tail=(one, 0)) assert c in b_1 assert one in b_1 assert (c, 0) in b_1 assert (one, 0) in b_1 assert (one, 1) not in b_1
def test_connection_feedback(self): from BondGraphTools import new, connect, add from BondGraphTools.exceptions import InvalidPortException model = new() c1 = new('C') c2 = new('C') c3 = new('C') add(model, c1, c2, c3) connect(c1, (c2, 0)) with pytest.raises(InvalidPortException) as ex: connect((c2, 0), c3) assert "Port is already connected: " in str(ex)
def test_failstate(self): # see issue 110 from BondGraphTools import new, expose, connect from BondGraphTools.exceptions import InvalidPortException model = new() port = new('SS') model.add(port) expose(port, label='port_1') test_model = new() component = new("0") ## could be anything test_model.add(component, model) target_port = (model, 'port_') # typo in port name with pytest.raises(InvalidPortException): connect(component, target_port)
def test_one_port(self): c = new('C') se = new('Se') r = new('R') one = new('1') bg = new() bg.add(c, se, r, one) connect(c, one.non_inverting) connect(se, one) connect(one, r) p0, p1, p2 = tuple(one.ports) assert p0.weight == 1 assert p1.weight == 1 assert p2.weight == -1
def _build_reactions(self, system, species_anchors, normalised): if normalised: param_dict = {"R": 1, "T": 1} else: param_dict = {"R": R, "T": self._T} for reaction_name, (bck_sto, fwd_sto, _, _) in self._reactions.items(): reaction = new("Re", library=LIBRARY, name=reaction_name, value=param_dict) system.add(reaction) fwd_name = "".join( list(fwd_sto.keys()) ) bck_name = "".join( list(bck_sto.keys()) ) if len(bck_sto) == 1 and list(bck_sto.values())[0] == 1: species = list(bck_sto.keys()).pop() connect(species_anchors[species], reaction) else: reverse_complex = new("1", name=bck_name) system.add(reverse_complex) connect(reverse_complex.inverting, reaction) self._connect_complex( system, species_anchors, reverse_complex, bck_sto, is_reversed=True ) if len(fwd_sto) == 1 and list(fwd_sto.values())[0] == 1: species = list(fwd_sto.keys()).pop() connect(reaction, species_anchors[species]) else: forward_complex = new("1", name=fwd_name) system.add(forward_complex) connect(reaction, forward_complex.inverting) self._connect_complex( system, species_anchors, forward_complex, fwd_sto, is_reversed=False )
def _build_species(self, system, normalised): if normalised: param_dict = {"R": 1, "T": 1} else: param_dict = {"R": R, "T": self._T} species_anchors = {} for species, n_reactions in self._species.items(): # edit: create new component for chemostat if species in self._chemostats: this_species = new( "Se", name=species, value=self._chemostats[species] ) n_reactions = n_reactions - 1 else: this_species = new( "Ce", library=LIBRARY, name=species, value=param_dict ) system.add(this_species) if n_reactions == 1: species_anchors[species] = this_species else: anchor = new("0", name=species) system.add(anchor) connect(anchor, this_species) species_anchors[species] = anchor if species in self._flowstats: flowstat = new( "Sf", value=self._flowstats[species], name=species ) system.add(flowstat) connect(flowstat, species_anchors[species]) return species_anchors
def as_network_model(self, normalised: bool = False): """Produces a bond graph :obj:`BondGraph.BondGraph` model of the system Args: normalised: If true, sets pressure and temperature to 1 Returns: A new instance of :obj:`BondGraphTools.BondGraph` representing this reaction system. """ system = new(name=self.name) species_anchor = self._build_species(system, normalised) self._build_reactions(system, species_anchor, normalised) return system
def _connect_complex(system, species_anchors, junct, stoichiometry, is_reversed=False): for i, (species, qty) in enumerate(stoichiometry.items()): if qty == 1: if is_reversed: connect(species_anchors[species], junct.non_inverting) else: connect(junct.non_inverting, species_anchors[species]) else: tf = new("TF", value=qty) system.add(tf) if is_reversed: connect((tf, 1), junct.non_inverting) connect(species_anchors[species], (tf, 0)) else: connect(junct.non_inverting, (tf, 1)) connect((tf, 0), species_anchors[species])