def testReadAndWriteDescription(self): m = Cml.Molecule() m.property["Description"] = "H20 is the base of all life." m.property["Description-Attribution"] = "Wikipedia, CC-BY-SA" m.write("tests/testWrite.cml") m = Cml.Molecule() m.parse("tests/testWrite.cml") self.assertEqual(m.property["Description"], "H20 is the base of all life.") self.assertEqual(m.property["Description-Attribution"], "Wikipedia, CC-BY-SA") os.remove("tests/testWrite.cml")
def testWriteAndParseReactions(self): r = Cml.Reactions() r1 = Cml.Reaction(["O", "O"], ["O2"]) r.reactions.append(r1) r2 = Cml.Reaction(["H+", "H+"], ["H2"]) r.reactions.append(r2) r.write("tests/writtenReactions.cml") r = Cml.Reactions() r.parse("tests/writtenReactions.cml") self.assertEqual(r.reactions[0].reactants, ["O","O"]) self.assertEqual(r.reactions[0].products, ["O2"]) self.assertEqual(r.reactions[1].reactants, ['H+', 'H+']) self.assertEqual(r.reactions[1].products, ["H2"]) os.remove("tests/writtenReactions.cml")
def testWriteAndParsePropertys(self): m = Cml.Molecule() m.atoms["a1"] = Cml.Atom("a1", "H", 0, 0, 0) m.property["Name"] = "Hydrogen" m.property["Weight"] = 1.0 m.property["Radius"] = 25 m.write("tests/testHydrogen.cml") m = Cml.Molecule() m.parse("tests/testHydrogen.cml") self.assertEqual(m.property["Name"], "Hydrogen") self.assertEqual(m.property["Weight"], 1.0) self.assertEqual(m.property["Radius"], 25) self.assertEqual(m.is_atom, True) os.remove("tests/testHydrogen.cml")
def testWriteAndParseStatePropertys(self): m = Cml.Molecule() gas = Cml.State("Gas", -426, 64) aq = Cml.State("Aqueous", ions=["Na+", "OH-"]) m.states["Gas"] = gas m.states["Aqueous"] = aq m.write("tests/testSodiumhydroxide.cml") m = Cml.Molecule() m.parse("tests/testSodiumhydroxide.cml") self.assertEqual(m.states["Gas"].enthalpy, -426) self.assertEqual(m.states["Gas"].entropy, 64) self.assertEqual(m.states["Aqueous"].ions, ["Na+", "OH-"]) self.assertEqual(m.states["Aqueous"].ions_str, "Na+,OH-") os.remove("tests/testSodiumhydroxide.cml")
def testCachedMolecule(self): m = Cml.Molecule() m.parse("tests/testPropane.cml") m.write("tests/testWrite.cml") c = CachedCml.getMoleculeCml("tests/testWrite.cml") self.assertEqual(len(m.atoms), 12) self.assertEqual(len(m.bonds), 11) self.assertEqual(m.atoms["a1"].elementType, "H") self.assertAlmostEqual(m.atoms["a1"].x, -4.719821) self.assertAlmostEqual(m.atoms["a1"].y, 1.866564) self.assertAlmostEqual(m.atoms["a1"].z, -1.096199) self.assertAlmostEqual(m.atoms["a2"].x, -4.299694) self.assertAlmostEqual(m.atoms["a2"].y, 2.06041) self.assertAlmostEqual(m.atoms["a2"].z, -2.091249) self.assertEqual(m.atoms["a2"].elementType, "C") self.assertEqual(m.bonds[0].atomA.id, "a1") self.assertEqual(m.bonds[0].atomB.id, "a2") os.remove("tests/testWrite.cml") c = CachedCml.getMoleculeCml("tests/testWrite.cml") self.assertEqual(len(m.atoms), 12) self.assertEqual(len(m.bonds), 11) self.assertEqual(m.atoms["a1"].elementType, "H") self.assertAlmostEqual(m.atoms["a1"].x, -4.719821) self.assertAlmostEqual(m.atoms["a1"].y, 1.866564) self.assertAlmostEqual(m.atoms["a1"].z, -1.096199) self.assertAlmostEqual(m.atoms["a2"].x, -4.299694) self.assertAlmostEqual(m.atoms["a2"].y, 2.06041) self.assertAlmostEqual(m.atoms["a2"].z, -2.091249) self.assertEqual(m.atoms["a2"].elementType, "C") self.assertEqual(m.bonds[0].atomA.id, "a1") self.assertEqual(m.bonds[0].atomB.id, "a2")
def testParseReactions(self): r = Cml.Reactions() r.parse("tests/reactions.cml") self.assertEqual(r.reactions[0].reactants, ["H2","O"]) self.assertEqual(r.reactions[0].products, ["H2O(s)"]) self.assertEqual(r.reactions[1].reactants, ['SO3', 'H2O']) self.assertEqual(r.reactions[1].products, ["H2SO4(aq)"])
def testParsePropane(self): m = Cml.Molecule() m.parse("tests/testPropane.cml") self.assertEqual(len(m.atoms), 12) self.assertEqual(len(m.bonds), 11) self.assertEqual(m.atoms["a1"].elementType, "H") self.assertEqual(m.atoms["a1"].formalCharge, 0) self.assertEqual(m.atoms["a1"].x, -4.719821) self.assertEqual(m.atoms["a1"].y, 1.866564) self.assertEqual(m.atoms["a1"].z, -1.096199) self.assertEqual(m.atoms["a1"].pos, (-4.719821, 1.866564)) self.assertEqual(m.atoms["a2"].x, -4.299694) self.assertEqual(m.atoms["a2"].y, 2.06041) self.assertEqual(m.atoms["a2"].z, -2.091249) self.assertEqual(m.atoms["a2"].pos, (-4.299694, 2.06041)) self.assertEqual(m.atoms["a2"].elementType, "C") self.assertEqual(m.atoms["a2"].formalCharge, 0) self.assertEqual(m.bonds[0].atomA.id, "a1") self.assertEqual(m.bonds[0].atomB.id, "a2") self.assertAlmostEqual(m.bonds[0].atomA.x, -4.719821) self.assertAlmostEqual(m.bonds[0].atomA.y, 1.866564) self.assertAlmostEqual(m.bonds[0].atomA.z, -1.096199) self.assertAlmostEqual(m.bonds[0].atomB.x, -4.299694) self.assertAlmostEqual(m.bonds[0].atomB.y, 2.06041) self.assertAlmostEqual(m.bonds[0].atomB.z, -2.091249)
def testParseLevel01(self): m = Cml.Level() m.parse("data/levels/01-Water.cml") expected = ['H+(g)', 'O(g)', 'O(g)', 'H+(g)', 'P(g)', 'F(g)', 'Al(s)'] self.assertEqual(m.molecules, expected) self.assertEqual(m.victory_condition, ["H2O"]) self.assertEqual(m.objective, "Create a water molecule") self.assertEqual(m.hint, "H + H + O => H2O")
def testParseStatePropertys(self): m = Cml.Molecule() m.parse("tests/testProperty.cml") self.assertEqual(m.states["Gas"].enthalpy, -426) self.assertEqual(m.states["Gas"].entropy, 64) self.assertEqual(m.states["Gas"].short, "g") self.assertEqual(m.get_state("g").entropy, 64) self.assertEqual(m.get_state("l"), None) self.assertEqual(m.states["Aqueous"].ions, ["Na+", "OH-"])
def getMoleculeCml(filename): global __cml_cache if filename in __cml_cache: return __cml_cache[filename] m = Cml.Molecule() m.parse(filename) __cml_cache[filename] = m return m
def react(self, molecule): ions = molecule.to_aqueous() if ions != None and len(ions) > 0: print(molecule.formula, "-(Water)>", ions) cml = Cml.Reaction([molecule.formula],ions) reaction = Reaction.Reaction(cml,[molecule.state_formula]) return reaction elif Config.current.DEBUG: print("Water beaker didnt react with:", molecule.formula)
def testParseLevel(self): m = Cml.Level() m.parse("tests/testlevel.cml") expected = ['H+(g)', 'O(g)', 'O(g)', 'H+(g)', 'P(g)', 'F(g)', 'Al(s)'] self.assertEqual(m.molecules, expected) self.assertEqual(m.victory_condition, ["H2O"]) self.assertEqual(m.objective, "Create a water molecule") self.assertEqual(m.hint, "H + H + O => H2O") self.assertEqual(m.effects[0].title, "Fire") self.assertEqual(m.effects[0].value, 800) self.assertEqual(m.effects[0].x2, 12) self.assertEqual(m.effects[0].y2, 10)
def testParseMoleculeWithoutBonds(self): m = Cml.Molecule() m.parse("data/molecule/NaCl.cml") self.assertEqual(len(m.atoms), 2) self.assertEqual(len(m.bonds), 1) self.assertEqual(m.atoms["a1"].elementType, "Na") self.assertEqual(m.atoms["a1"].formalCharge, 1) self.assertEqual(m.atoms["a1"].x, 1.0) self.assertEqual(m.atoms["a1"].y, 0.0) self.assertEqual(m.atoms["a2"].x, 0.0) self.assertEqual(m.atoms["a2"].y, 0.0) self.assertEqual(m.atoms["a2"].elementType, "Cl") self.assertEqual(m.atoms["a2"].formalCharge, -1)
def testCreateMoleculeWithCharge(self): m = Cml.Molecule() a1 = Cml.Atom("a1", "C",-1, 0, 0) a2 = Cml.Atom("a2", "O", 1, 1, 0) m.atoms["a1"] = a1 m.atoms["a2"] = a2 b = Cml.Bond(a1, a2, 2) m.bonds.append(b) m.write("tests/testOxygen.cml") m = Cml.Molecule() m.parse("tests/testOxygen.cml") self.assertAlmostEqual(m.atoms["a1"].x, 0.0) self.assertAlmostEqual(m.atoms["a1"].y, 0.0) self.assertEqual(m.atoms["a1"].elementType, "C") self.assertEqual(m.atoms["a1"].formalCharge,-1) self.assertAlmostEqual(m.atoms["a2"].x, 1.0) self.assertAlmostEqual(m.atoms["a2"].y, 0.0) self.assertEqual(m.atoms["a2"].elementType, "O") self.assertEqual(m.atoms["a2"].formalCharge,1) self.assertEqual(m.bonds[0].atomA.id, "a1") self.assertEqual(m.bonds[0].atomB.id, "a2") self.assertEqual(m.bonds[0].bonds, 2) os.remove("tests/testOxygen.cml")
def testParseAmmonia(self): m = Cml.Molecule() m.parse("tests/testAmmonia.cml") self.assertEqual(len(m.atoms), 4) self.assertEqual(len(m.bonds), 3) self.assertEqual(m.atoms["a1"].elementType, "N") self.assertEqual(m.atoms["a1"].x, 1.0) self.assertEqual(m.atoms["a1"].y, 0.0) self.assertEqual(m.atoms["a2"].x, 2.02) self.assertEqual(m.atoms["a2"].y, 0.0) self.assertEqual(m.atoms["a2"].elementType, "H") self.assertEqual(m.bonds[0].atomA.id, "a1") self.assertEqual(m.bonds[0].atomB.id, "a2") self.assertEqual(m.is_atom, False)
def testSortedAtoms(self): m = Cml.Molecule() m.parse("tests/testPropane.cml") self.assertEqual(m.atoms_sorted[0].id, "a1") self.assertEqual(m.atoms_sorted[1].id, "a2") self.assertEqual(m.atoms_sorted[2].id, "a3") self.assertEqual(m.atoms_sorted[3].id, "a4") self.assertEqual(m.atoms_sorted[4].id, "a5") self.assertEqual(m.atoms_sorted[5].id, "a6") self.assertEqual(m.atoms_sorted[6].id, "a7") self.assertEqual(m.atoms_sorted[7].id, "a8") self.assertEqual(m.atoms_sorted[8].id, "a9") self.assertEqual(m.atoms_sorted[9].id, "a10") self.assertEqual(m.atoms_sorted[10].id, "a11") self.assertEqual(m.atoms_sorted[11].id, "a12") self.assertEqual(m.getDigits("asdas23434"), 23434)
def openFile(self, filename): self.filename = filename self.current_pos = self.folder_list.index(filename) molecule = Cml.Molecule() self.molecule = molecule molecule.parse(filename) formula = filename.split("/")[-1].split(".cml")[0] state_formula = formula+"(g)" cml2img.convert_cml2png(state_formula, "preview.png") pixBuffPreview = Pixbuf.new_from_file("preview.png") imgPreview = self.widget("imgPreview") imgPreview.set_from_pixbuf(pixBuffPreview) self.txtMoleculeName = self.widget("txtName") self.txtAtomWeight = self.widget("txtWeight") self.txtAtomRadius = self.widget("txtRadius") self.txtMoleculeName.set_text(str(molecule.property.get("Name", ""))) self.widget("txtAttribution").set_text( molecule.property.get("DescriptionAttribution", "")) self.widget("textbufferDescription").set_text( molecule.property.get("Description", "")) if self.molecule.is_atom: self.setAtomSettings() else: self.txtAtomWeight.set_text("") self.txtAtomWeight.set_sensitive(False) self.txtAtomRadius.set_text("") self.txtAtomRadius.set_sensitive(False) self.modelStates.clear() for state in molecule.states.values(): stateList = [state.name, state.enthalpy, state.entropy, state.ions_str] stateList = [str(x) if x is not None else "" for x in stateList] self.modelStates.append(stateList) self.widget("cmbLicense").set_active(-1) index = 0 for license in self.widget("liststoreLicenses"): if license[0] == molecule.property.get("DescriptionLicense", "N/A"): self.widget("cmbLicense").set_active(index) break index += 1
def testNormalizePos(self): m = Cml.Molecule() m.parse("tests/testPropane.cml") self.assertEqual(m.max_pos(), (-0.605318, 3.557669, -1.096199)) self.assertEqual(m.min_pos(), (-4.928943, 1.137126, -4.097433)) m.normalize_pos() self.assertEqual(m.max_pos(), (4.323625, 2.4205430000000003, 3.0012339999999997)) self.assertEqual(m.min_pos(), (0.0, 0.0, 0.0)) self.assertEqual(len(m.atoms), 12) self.assertEqual(len(m.bonds), 11) self.assertEqual(m.atoms["a1"].elementType, "H") self.assertAlmostEqual(m.atoms["a1"].x, 0.209122) self.assertAlmostEqual(m.atoms["a1"].y, 0.729438) self.assertAlmostEqual(m.atoms["a1"].z, 3.001234) self.assertAlmostEqual(m.atoms["a2"].x, 0.629249) self.assertAlmostEqual(m.atoms["a2"].y, 0.923284) self.assertAlmostEqual(m.atoms["a2"].z, 2.006184) self.assertEqual(m.atoms["a2"].elementType, "C") self.assertEqual(m.bonds[0].atomA.id, "a1") self.assertEqual(m.bonds[0].atomB.id, "a2")
def on_btnSave_clicked(self, widget): self.molecule.states = dict() for col in self.modelStates: name = col[0] enthalpy = col[1] if col[1] != "" else None entropy = col[2] if col[2] != "" else None ions = col[3].split(',') if col[3] != "" else None state = Cml.State(name, enthalpy, entropy, ions) self.molecule.states[name] = state self.molecule.property["Name"] = self.txtMoleculeName.get_text() textbuffer = self.widget("textbufferDescription") start_iter = textbuffer.get_start_iter() end_iter = textbuffer.get_end_iter() description = textbuffer.get_text(start_iter, end_iter, True) self.molecule.property["Description"] = description self.molecule.property["DescriptionAttribution"] = self.widget("txtAttribution").get_text() self.molecule.property["DescriptionLicense"] = get_active_text(self.widget("cmbLicense")) if self.molecule.is_atom: self.molecule.property["Weight"] = float(self.txtAtomWeight.get_text()) self.molecule.property["Radius"] = float(self.txtAtomRadius.get_text()) self.molecule.write(self.filename)
def __init__(self, formula_with_state, space, batch, pos=None, render_only=False): self.space = space self.batch = batch self.creation_time = time.time() formula, state = Reaction.split_state(formula_with_state) self.formula = formula self.cml = CachedCml.getMolecule(formula) self.cml.normalize_pos() self.current_state = self.cml.get_state(state) if self.current_state is None and render_only: self.current_state = Cml.State("Gas") elif self.current_state is None: raise Exception("Did not find state for:" + formula_with_state + " existing states are:" + str(self.cml.states.keys())) if pos is None: pos = (random.randint(10, 600), random.randint(200, 500)) self.pos = pos self.create_atoms()
def testParseLevelWithMiningEffect(self): m = Cml.Level() m.parse("data/levels/12-Iron-1.cml") self.assertEqual(m.effects[0].title, "Mining") self.assertEqual(m.effects[0].molecules, ['Fe2O3(s)', 'Fe3O4(s)'])
def setupRealReactor(self): cml = Cml.Reactions() cml.parse("data/reactions.cml") reactor = Reactor(cml.reactions) return reactor
def setupSimpleReactor(self): r1 = Cml.Reaction(["O", "H+"], ["OH-(aq)"]) r2 = Cml.Reaction(["O", "O"], ["O2(g)"]) return Reactor([r1, r2])
def testAllMolecules(self): for filename in glob.glob("data/molecule/*"): m = Cml.Molecule() m.parse(filename) self.assertNotEqual(len(m.states), 0, msg="%s dont have any state info!" % filename)
def __init__(self): self.moelcules = dict() cml = Cml.Reactions() cml.parse("data/reactions.cml") self.reactor = Reactor(cml.reactions)
def setupSimpleReactor(): r1 = Cml.Reaction(["H2SO4(aq)", "NaCl(s)", "NaCl(s)"], ["HCl(g)", "HCl(g)", "Na2SO4(s)"]) r2 = Cml.Reaction(["OH-", "H+"], ["H2O(l)"]) return Reactor([r1, r2])
def get_current_level(self): path = self.levels[self.current_level] cml = Cml.Level() cml.parse(path) return Level(cml, self.window)