class CompoundT(MoleculeT): ## @brief constructor for class CompoundT # @param molec_set A MolecSet Object def __init__(self, molec_set): self._C = MolecSet(molec_set.to_seq()) ## @brief return the molecules in the compound # @return a Set containing the molecules in the compound def get_molec_set(self): return self._C ## @brief count the number of atoms of a element in the compound # @param e the element to check for in the compound # @return integer indicating the number of atoms of element e in the compound def num_atoms(self, e): temp_seq = self._C.to_seq() count = 0 for molecule in temp_seq: count += molecule.num_atoms(e) return count ## @brief return an ElmSet of the elements in the molecules that are in the compound # @return ElmSet containing the elements def constit_elems(self): molecs = self._C.to_seq() elems = [] for molecule in molecs: elems.append(molecule.get_elm()) return ElmSet(elems) ## @brief check if two compounds are equals # @param d the object to compare against # @return boolean indicating if they are equal def equals(self, d): return self.get_molec_set().equals(d.get_molec_set())
def test_equals(self): O = MoleculeT(1, ElementT.O) H2 = MoleculeT(2, ElementT.H) H2O = CompoundT(MolecSet([H2, O])) o = MoleculeT(1, ElementT.O) h2 = MoleculeT(2, ElementT.H) h2o = CompoundT(MolecSet([h2, o])) assert H2O.equals(H2O) assert not H2O.equals(h2o)
def test_rhs_coeff(self): H2 = MoleculeT(2, ElementT.H) O2 = MoleculeT(2, ElementT.O) O = MoleculeT(1, ElementT.O) H2O = CompoundT(MolecSet([H2, O])) H2 = CompoundT(MolecSet([H2])) O2 = CompoundT(MolecSet([O2])) L = [H2, O2] R = [H2O] reaction = ReactionT(L, R) cR = reaction.get_rhs_coeff() assert cR[0] == 2
def test_get_lhs(self): H2 = MoleculeT(2, ElementT.H) O2 = MoleculeT(2, ElementT.O) O = MoleculeT(1, ElementT.O) H2O = CompoundT(MolecSet([H2, O])) H2 = CompoundT(MolecSet([H2])) O2 = CompoundT(MolecSet([O2])) L = [H2, O2] R = [H2O] reaction = ReactionT(L, R) l = reaction.get_lhs() l = l[0].get_molec_set().s l = l[0] assert l.get_elm() == ElementT.H assert l.get_num() == 2
def test_num_atoms(self): oxy = MoleculeT(1, ElementT.O) h2 = MoleculeT(2, ElementT.H) h2o = CompoundT(MolecSet([h2, oxy])) h_num = h2o.num_atoms(ElementT.H) o_num = h2o.num_atoms(ElementT.O) assert h_num == 2 assert o_num == 1
def test_get_molec_set(self): oxy = MoleculeT(1, ElementT.O) h2 = MoleculeT(2, ElementT.H) h2o = CompoundT(MolecSet([h2, oxy])) s = h2o.get_molec_set().to_seq() assert s[0].get_elm() == ElementT.H assert s[0].get_num() == 2 assert s[1].get_elm() == ElementT.O assert s[1].get_num() == 1
def test_get_rhs(self): H2 = MoleculeT(2, ElementT.H) O2 = MoleculeT(2, ElementT.O) O = MoleculeT(1, ElementT.O) H2O = CompoundT(MolecSet([H2, O])) H2 = CompoundT(MolecSet([H2])) O2 = CompoundT(MolecSet([O2])) L = [H2, O2] R = [H2O] reaction = ReactionT(L, R) r = reaction.get_rhs() r = r[0].get_molec_set().s r1 = r[0] r2 = r[1] assert r1.get_elm() == ElementT.H assert r1.get_num() == 2 assert r2.get_elm() == ElementT.O assert r2.get_num() == 1
def setup_method(self, method): self.s1 = Set([3, -6, 4, 0, 12, 9]) self.e1 = ElmSet([ElementT.H, ElementT.O]) self.m1 = MoleculeT(2, ElementT.H) self.m2 = MoleculeT(7, ElementT.O) self.m3 = MoleculeT(2, ElementT.H) self.m4 = MoleculeT(2, ElementT.O) self.m5 = MoleculeT(1, ElementT.O) self.c1 = CompoundT(MolecSet([self.m1, self.m2])) self.c2 = CompoundT(MolecSet([self.m3])) self.c3 = CompoundT(MolecSet([self.m4])) self.c4 = CompoundT(MolecSet([self.m3, self.m5])) # 2 H2 + O2 --> 2 H2O self.r1 = ReactionT([self.c2, self.c3], [self.c4]) # 2 N --> N2 self.m8 = MoleculeT(1, ElementT.N) self.m9 = MoleculeT(2, ElementT.N) self.c8 = CompoundT(MolecSet([self.m8])) self.c9 = CompoundT(MolecSet([self.m9])) self.r2 = ReactionT([self.c8], [self.c9])
def test_constit_elems(self): O = MoleculeT(1, ElementT.O) H2 = MoleculeT(2, ElementT.H) H2O = CompoundT(MolecSet([H2, O])) s = H2O.constit_elems() assert s.s == [ElementT.H, ElementT.O]
R = Set([12, -6, 0, 1, 5]) print(S.equals(R)) print(S == R) print(str(R.to_seq())) for i in R.s: print(i) # ElmSet Examples E = ElmSet([ElementT.H, ElementT.O]) E.add(ElementT.C) print(E == ElmSet([ElementT.H, ElementT.C, ElementT.O])) # MoleculeT Examples M1 = MoleculeT(2, ElementT.H) M2 = MoleculeT(7, ElementT.O) print(M1.num_atoms(ElementT.C)) print(M1.constit_elems() == ElmSet([ElementT.H])) print(M1.equals(M2)) print(M1 == M2) # CompoundT Examples C1 = CompoundT(MolecSet([M1, M2])) print(C1.num_atoms(ElementT.H)) e = C1.constit_elems() print(e.equals(ElmSet([ElementT.H, ElementT.O]))) print(C1.equals(CompoundT(MolecSet([M1]))))
# ElmSet Examples E = ElmSet([ElementT.H, ElementT.O]) E.add(ElementT.C) print(E == ElmSet([ElementT.H, ElementT.C, ElementT.O])) # MoleculeT Examples M1 = MoleculeT(2, ElementT.H) M2 = MoleculeT(7, ElementT.O) print(M1.num_atoms(ElementT.C)) print(M1.constit_elems() == ElmSet([ElementT.H])) print(M1.constit_elems().to_seq()) print(M1.equals(M2)) print(M1 == M2) # CompoundT Examples C1 = CompoundT(MolecSet([M1, M2])) print(C1.num_atoms(ElementT.H)) e = C1.constit_elems() print(e.to_seq()) print(e.equals(ElmSet([ElementT.H, ElementT.O]))) print(C1.equals(CompoundT(MolecSet([M1])))) # ReactionT Examples # H2 + O2 --> H2O M3 = MoleculeT(2, ElementT.H) M4 = MoleculeT(2, ElementT.O) C2 = CompoundT(MolecSet([M3])) C3 = CompoundT(MolecSet([M4])) M4 = MoleculeT(1, ElementT.O) C4 = CompoundT(MolecSet([M3, M4]))
def test_CompoundT_get_molec_set(self): assert self.c1.get_molec_set() == MolecSet([self.m1, self.m2])
def test_equals_cmp(): assert iron_oxide.equals(CompoundT(MolecSet([iron2, oxygen3]))) assert not iron_oxide.equals( CompoundT(MolecSet([MoleculeT(2, ElementT.H), oxygen3])))
def test_get_molec_set(): assert iron_oxide.get_molec_set() == MolecSet([iron2, oxygen3]) assert iron_oxide.get_molec_set() != MolecSet([ElementT.H, ElementT.Tc])
from MoleculeT import * from CompoundT import * from ReactionT import * # Global varibale declarations test_set = Set([1, 2, 3, 4, 5]) oxygen3 = MoleculeT(3, ElementT.O) iron1 = MoleculeT(1, ElementT.Fe) carbon1 = MoleculeT(1, ElementT.C) iron2 = MoleculeT(2, ElementT.Fe) oxygen2 = MoleculeT(2, ElementT.O) oxygen1 = MoleculeT(1, ElementT.O) hydrogen2 = MoleculeT(2, ElementT.H) iron_oxide = CompoundT(MolecSet([iron2, oxygen3])) carbon = CompoundT(MolecSet([carbon1])) iron = CompoundT(MolecSet([iron1])) carbon_oxide = CompoundT(MolecSet([carbon1, oxygen2])) hydrogendioxide = CompoundT(MolecSet([hydrogen2, oxygen1])) reaction1 = ReactionT([iron_oxide, carbon], [iron, carbon_oxide]) reaction2 = ReactionT([hydrogen2, oxygen2], [hydrogendioxide]) # testing Set def test_add(): test_set.add(6) assert test_set == Set([1, 2, 3, 4, 5, 6])
def __init__(self, molec_set): self._C = MolecSet(molec_set.to_seq())