Skip to content

Commit d0ebc42

Browse files
committed
WIP
1 parent a34e5f6 commit d0ebc42

File tree

2 files changed

+78
-62
lines changed

2 files changed

+78
-62
lines changed

arkane/encorr/isodesmic.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ def __eq__(self, other):
291291
f"GenericConstraint object has no __eq__ defined for other object of "
292292
f"type {type(other)}"
293293
)
294+
def __repr__(self):
295+
return self.constraint_str
294296

295297

296298
def bond_centric_constraints(
@@ -307,8 +309,14 @@ def bond_centric_constraints(
307309

308310

309311
def _buerger_rc2(bond: Bond) -> BondConstraint:
310-
atom1 = AtomConstraint(label=bond.atom1.symbol)
311-
atom2 = AtomConstraint(label=bond.atom2.symbol)
312+
atom1 = bond.atom1
313+
atom2 = bond.atom2
314+
315+
if atom1.symbol > atom2.symbol:
316+
atom1, atom2 = atom2, atom1
317+
318+
atom1 = AtomConstraint(label=atom1.symbol)
319+
atom2 = AtomConstraint(label=atom2.symbol)
312320

313321
return BondConstraint(atom1=atom1, atom2=atom2, bond_order=int(bond.order))
314322

@@ -317,16 +325,25 @@ def _buerger_rc3(bond: Bond) -> BondConstraint:
317325
atom1 = bond.atom1
318326
atom2 = bond.atom2
319327

328+
if atom1.symbol > atom2.symbol:
329+
atom1, atom2 = atom2, atom1
330+
320331
atom1 = AtomConstraint(label=f"{atom1.symbol}{atom1.connectivity1}")
321332
atom2 = AtomConstraint(label=f"{atom2.symbol}{atom2.connectivity1}")
322333

323334
return BondConstraint(atom1=atom1, atom2=atom2, bond_order=int(bond.order))
324335

325336

326337
def _buerger_rc4(bond: Bond) -> BondConstraint:
338+
atom1 = bond.atom1
339+
atom2 = bond.atom2
340+
341+
if atom1.symbol > atom2.symbol:
342+
atom1, atom2 = atom2, atom1
343+
327344
atoms = []
328345

329-
for atom in [bond.atom1, bond.atom2]:
346+
for atom in [atom1, atom2]:
330347
connections = []
331348
for a, b in atom.bonds.items():
332349
ac = AtomConstraint(label=f"{a.symbol}{a.connectivity1}")
@@ -412,6 +429,7 @@ def _get_all_constraints(
412429
if self.conserve_ring_size:
413430
features += self._get_ring_constraints(species)
414431

432+
features.sort(key=lambda x: x.__repr__())
415433
return features
416434

417435
def _enumerate_constraints(self, full_constraints_list):

test/arkane/encorr/isodesmicTest.py

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
This script contains unit tests of the :mod:`arkane.isodesmic` module.
3232
"""
3333

34-
3534
import numpy as np
3635

3736
from rmgpy.molecule import Molecule
@@ -137,12 +136,11 @@ def test_initializing_constraint_map(self):
137136
"""
138137
Test that the constraint map is properly initialized when a SpeciesConstraints object is initialized
139138
"""
140-
caffeine_consts = SpeciesConstraints(self.caffeine, [self.butane, self.benzene])
141-
assert set(caffeine_consts.constraint_map.keys()) == {
142-
"H",
143-
"C",
144-
"O",
145-
"N",
139+
consts = SpeciesConstraints(self.caffeine, [self.butane, self.benzene])
140+
caffeine_features = consts._get_all_constraints(self.caffeine)
141+
caffeine_constraint_list = [feat.__repr__() for feat in caffeine_features]
142+
143+
assert set(caffeine_constraint_list) == {
146144
"C=O",
147145
"C-N",
148146
"C-H",
@@ -154,55 +152,69 @@ def test_initializing_constraint_map(self):
154152
}
155153

156154
no_rings = SpeciesConstraints(self.caffeine, [self.butane, self.benzene], conserve_ring_size=False)
157-
assert set(no_rings.constraint_map.keys()) == {"H", "C", "O", "N", "C=O", "C-N", "C-H", "C=C", "C=N", "C-C"}
158-
159-
atoms_only = SpeciesConstraints(self.caffeine, [self.butane], conserve_ring_size=False, conserve_bonds=False)
160-
assert set(atoms_only.constraint_map.keys()) == {"H", "C", "O", "N"}
155+
caffeine_features = no_rings._get_all_constraints(self.caffeine)
156+
caffeine_constraint_list = [feat.__repr__() for feat in caffeine_features]
157+
assert set(caffeine_constraint_list) == {"C=O", "C-N", "C-H", "C=C", "C=N", "C-C"}
161158

162159
def test_enumerating_constraints(self):
163160
"""
164161
Test that a SpeciesConstraints object can properly enumerate the constraints of a given ErrorCancelingSpecies
165162
"""
166163
spcs_consts = SpeciesConstraints(self.benzene, [])
167-
assert set(spcs_consts.constraint_map.keys()) == {"C", "H", "C=C", "C-C", "C-H", "6_ring"}
168-
169-
# Now that we have confirmed that the correct keys are present, overwrite the constraint map to set the order
170-
spcs_consts.constraint_map = {
171-
"H": 0,
172-
"C": 1,
173-
"C=C": 2,
174-
"C-C": 3,
175-
"C-H": 4,
176-
"6_ring": 5,
177-
}
164+
benzene_features = spcs_consts._get_all_constraints(self.benzene)
165+
benzene_constraint_list = [feat.__repr__() for feat in benzene_features]
166+
assert set(benzene_constraint_list) == {"C=C", "C-C", "C-H", "6_ring"}
167+
168+
target_constraints, _ = spcs_consts._enumerate_constraints([benzene_features])
169+
benzene_constraints = target_constraints
178170

179171
assert np.array_equal(
180-
spcs_consts._enumerate_constraints(self.propene),
181-
np.array([6, 3, 1, 1, 6, 0]),
172+
benzene_constraints,
173+
np.array([1, 3, 6, 3]),
182174
)
175+
176+
spcs_consts.all_reference_species = [self.propene]
177+
propene_features = spcs_consts._get_all_constraints(self.propene)
178+
_, reference_constraints = spcs_consts._enumerate_constraints([benzene_features, propene_features])
179+
propene_constraints = reference_constraints[0]
183180
assert np.array_equal(
184-
spcs_consts._enumerate_constraints(self.butane),
185-
np.array([10, 4, 0, 3, 10, 0]),
181+
propene_constraints,
182+
np.array([0, 1, 6, 1]),
186183
)
184+
185+
spcs_consts.all_reference_species = [self.butane]
186+
butane_features = spcs_consts._get_all_constraints(self.butane)
187+
_, reference_constraints = spcs_consts._enumerate_constraints([benzene_features, butane_features])
188+
butane_constraints = reference_constraints[0]
187189
assert np.array_equal(
188-
spcs_consts._enumerate_constraints(self.benzene),
189-
np.array([6, 6, 3, 3, 6, 1]),
190+
butane_constraints,
191+
np.array([0, 3, 10, 0]),
190192
)
191193

192-
# Caffeine and ethyne should return None since they have features not found in benzene
193-
assert spcs_consts._enumerate_constraints(self.caffeine) is None
194-
assert spcs_consts._enumerate_constraints(self.ethyne) is None
194+
# Caffeine and ethyne should return empty list since they have features not found in benzene
195+
spcs_consts.all_reference_species = [self.caffeine]
196+
caffeine_features = spcs_consts._get_all_constraints(self.caffeine)
197+
_, reference_constraints = spcs_consts._enumerate_constraints([benzene_features, caffeine_features])
198+
assert len(reference_constraints) == 0
199+
200+
spcs_consts.all_reference_species = [self.ethyne]
201+
ethyne_features = spcs_consts._get_all_constraints(self.ethyne)
202+
_, reference_constraints = spcs_consts._enumerate_constraints([benzene_features, ethyne_features])
203+
assert len(reference_constraints) == 0
195204

196205
def test_calculating_constraints(self):
197206
"""
198207
Test that a SpeciesConstraints object can properly return the target constraint vector and the constraint matrix
199208
"""
200209
spcs_consts = SpeciesConstraints(self.caffeine, [self.propene, self.butane, self.benzene, self.ethyne])
201-
assert set(spcs_consts.constraint_map.keys()) == {
202-
"H",
203-
"C",
204-
"O",
205-
"N",
210+
caffeine_features = spcs_consts._get_all_constraints(self.caffeine)
211+
propene_features = spcs_consts._get_all_constraints(self.propene)
212+
butane_features = spcs_consts._get_all_constraints(self.butane)
213+
benzene_features = spcs_consts._get_all_constraints(self.benzene)
214+
ethyne_features = spcs_consts._get_all_constraints(self.ethyne)
215+
216+
caffeine_feature_list = [feat.__repr__() for feat in caffeine_features]
217+
assert set(caffeine_feature_list) == {
206218
"C=O",
207219
"C-N",
208220
"C-H",
@@ -213,36 +225,20 @@ def test_calculating_constraints(self):
213225
"6_ring",
214226
}
215227

216-
# Now that we have confirmed that the correct keys are present, overwrite the constraint map to set the order
217-
spcs_consts.constraint_map = {
218-
"H": 0,
219-
"C": 1,
220-
"O": 2,
221-
"N": 3,
222-
"C=O": 4,
223-
"C-N": 5,
224-
"C-H": 6,
225-
"C=C": 7,
226-
"C=N": 8,
227-
"C-C": 9,
228-
"5_ring": 10,
229-
"6_ring": 11,
230-
}
231-
232228
target_consts, consts_matrix = spcs_consts.calculate_constraints()
233229

234230
# First, test that ethyne is not included in the reference set
235231
assert spcs_consts.reference_species == [self.propene, self.butane, self.benzene]
236232

237233
# Then, test the output of the calculation
238-
assert np.array_equal(target_consts, np.array([10, 8, 2, 4, 2, 10, 10, 1, 1, 1, 1, 1]))
234+
assert np.array_equal(target_consts, np.array([ 1, 1, 1, 10, 10, 1, 1, 2, 0, 8, 10, 4, 2]))
239235
assert np.array_equal(
240236
consts_matrix,
241237
np.array(
242238
[
243-
[6, 3, 0, 0, 0, 0, 6, 1, 0, 1, 0, 0],
244-
[10, 4, 0, 0, 0, 0, 10, 0, 0, 3, 0, 0],
245-
[6, 6, 0, 0, 0, 0, 6, 3, 0, 3, 0, 1],
239+
[ 0, 0, 1, 6, 0, 1, 0, 0, 0, 3, 6, 0, 0],
240+
[ 0, 0, 3, 10, 0, 0, 0, 0, 0, 4, 10, 0, 0],
241+
[ 0, 1, 3, 6, 0, 3, 0, 0, 0, 6, 6, 0, 0],
246242
]
247243
),
248244
)
@@ -278,6 +274,8 @@ def test_creating_error_canceling_schemes(self):
278274
scheme = ErrorCancelingScheme(
279275
self.propene,
280276
[self.butane, self.benzene, self.caffeine, self.ethyne],
277+
"rc2",
278+
True,
281279
True,
282280
True,
283281
)
@@ -328,7 +326,7 @@ def test_multiple_error_canceling_reactions(self):
328326
)
329327

330328
reaction_list = scheme.multiple_error_canceling_reaction_search(n_reactions_max=20)
331-
assert len(reaction_list) == 20
329+
assert len(reaction_list) == 6
332330
reaction_string = reaction_list.__repr__()
333331
# Consider both permutations of the products in the reaction string
334332
rxn_str1 = "<ErrorCancelingReaction 1*C=CC + 1*CCCC <=> 1*CCC + 1*C=CCC >"
@@ -361,9 +359,9 @@ def test_calculate_target_enthalpy(self):
361359
)
362360

363361
target_thermo, rxn_list = scheme.calculate_target_enthalpy(n_reactions_max=3, milp_software=["lpsolve"])
364-
assert target_thermo.value_si == 115000.0
362+
assert target_thermo.value_si == 110000.0
365363
assert isinstance(rxn_list[0], ErrorCancelingReaction)
366364

367365
if self.pyo is not None:
368366
target_thermo, _ = scheme.calculate_target_enthalpy(n_reactions_max=3, milp_software=["pyomo"])
369-
assert target_thermo.value_si == 115000.0
367+
assert target_thermo.value_si == 110000.0

0 commit comments

Comments
 (0)