-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathisotopesTest.py
More file actions
1170 lines (1065 loc) · 34.5 KB
/
isotopesTest.py
File metadata and controls
1170 lines (1065 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
###############################################################################
# #
# RMG - Reaction Mechanism Generator #
# #
# Copyright (c) 2002-2023 Prof. William H. Green ([email protected]), #
# Prof. Richard H. West ([email protected]) and the RMG Team ([email protected]) #
# #
# Permission is hereby granted, free of charge, to any person obtaining a #
# copy of this software and associated documentation files (the 'Software'), #
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# #
###############################################################################
import os
import numpy as np
from rmgpy import settings
from rmgpy.data.kinetics.family import TemplateReaction
from rmgpy.data.rmg import RMGDatabase
from rmgpy.kinetics.arrhenius import Arrhenius
from rmgpy.molecule import Molecule
from rmgpy.reaction import Reaction
from rmgpy.species import Species
from rmgpy.tools.isotopes import (
correct_entropy,
apply_kinetic_isotope_effect_simple,
generate_isotope_reactions,
get_reduced_mass,
get_labeled_reactants,
is_enriched,
generate_isotopomers,
cluster,
remove_isotope,
redo_isotope,
ensure_reaction_direction,
compare_isotopomers,
)
database = None
def setUpModule():
"""A function that is run ONCE before all unit tests in this module."""
global database
database = RMGDatabase()
database.load(
path=os.path.join(settings["test_data.directory"], "testing_database"),
kinetics_families=["H_Abstraction", "intra_H_migration"],
testing=True,
depository=False,
solvation=False,
surface=False,
)
database.load_forbidden_structures()
# Prepare the database by loading training reactions and averaging the rate rules
for family in database.kinetics.families.values():
if not family.auto_generated:
family.add_rules_from_training(thermo_database=database.thermo)
family.fill_rules_by_averaging_up(verbose=True)
def tearDownModule():
"""A function that is run ONCE after all unit tests in this module."""
from rmgpy.data import rmg
rmg.database = None
class IsotopesTest:
@classmethod
def setup_class(cls):
global database
cls.database = database
cls.family = database.kinetics.families["H_Abstraction"]
def test_cluster_with_species(self):
"""
Test that isotope partitioning algorithm work with Reaction Objects.
"""
eth = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethi = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
meth = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
"""
)
spc_list = [eth, ethi]
clusters = cluster(spc_list)
assert len(clusters) == 1
assert len(clusters[0]) == 2
spc_list = [meth, ethi]
clusters = cluster(spc_list)
assert len(clusters) == 2
assert len(clusters[0]) == 1
def test_cluster_with_reactions(self):
"""
Test that isotope partitioning algorithm works with Reaction objects
"""
eth = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethi = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
meth = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
"""
)
rxn0 = Reaction(reactants=[ethi, ethi], products=[ethi, eth])
rxn1 = Reaction(reactants=[eth, ethi], products=[eth, eth])
rxn2 = Reaction(reactants=[ethi, meth], products=[meth, ethi])
rxn3 = Reaction(reactants=[eth, meth], products=[eth, meth])
rxn4 = Reaction(reactants=[meth], products=[meth])
rxn5 = Reaction(reactants=[ethi], products=[eth])
same_cluster_list = [rxn0, rxn1]
clusters = cluster(same_cluster_list)
assert len(clusters) == 1
assert len(clusters[0]) == 2
same_cluster_list = [rxn2, rxn3]
clusters = cluster(same_cluster_list)
assert len(clusters) == 1
assert len(clusters[0]) == 2
multi_cluster_list = [rxn0, rxn1, rxn2, rxn3, rxn4, rxn5]
clusters = cluster(multi_cluster_list)
assert len(clusters) == 4
assert len(clusters[0]) == 1
def test_remove_isotope_for_reactions(self):
"""
Test that remove isotope algorithm works with Reaction objects.
"""
eth = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethi = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
unlabeled_rxn = Reaction(reactants=[eth], products=[eth])
labeled_rxn = Reaction(reactants=[ethi], products=[ethi])
stripped = remove_isotope(labeled_rxn)
assert unlabeled_rxn.is_isomorphic(stripped)
def test_remove_isotope_for_species(self):
"""
Test that remove isotope algorithm works with Species.
"""
eth = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethi = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
stripped = remove_isotope(ethi)
assert eth.is_isomorphic(stripped)
def test_inplace_remove_isotope_for_reactions(self):
"""
Test that removeIsotope and redoIsotope works with reactions
"""
eth = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethi = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
unlabeled_rxn = Reaction(reactants=[eth], products=[eth])
labeled_rxn = Reaction(reactants=[ethi], products=[ethi])
stored_labeled_rxn = labeled_rxn.copy()
modified_atoms = remove_isotope(labeled_rxn, inplace=True)
assert unlabeled_rxn.is_isomorphic(labeled_rxn)
redo_isotope(modified_atoms)
assert stored_labeled_rxn.is_isomorphic(labeled_rxn)
def test_ensure_reaction_direction(self):
"""
Tests that the direction of the reaction is constant for every isotopomer
"""
# get reactions
methyl = Species().from_smiles("[CH3]")
methyli = Species().from_adjacency_list(
"""
multiplicity 2
1 C u1 p0 c0 i13 {2,S} {3,S} {4,S}
2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
"""
)
methane = Species().from_smiles("C")
methanei = Species().from_adjacency_list(
"""
1 C u0 p0 c0 i13 {2,S} {3,S} {4,S} {5,S}
2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
"""
)
dipropyl = Species().from_smiles("[CH2]C[CH2]")
dipropyli = Species().from_adjacency_list(
"""
multiplicity 3
1 C u1 p0 c0 {2,S} {3,S} {4,S}
2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 C u0 p0 c0 {1,S} {5,S} {8,S} {9,S}
5 C u1 p0 c0 i13 {4,S} {6,S} {7,S}
6 H u0 p0 c0 {5,S}
7 H u0 p0 c0 {5,S}
8 H u0 p0 c0 {4,S}
9 H u0 p0 c0 {4,S}
"""
)
propyl = Species().from_smiles("CC[CH2]")
propyli = Species().from_adjacency_list(
"""
multiplicity 2
1 C u1 p0 c0 i13 {2,S} {3,S} {4,S}
2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 C u0 p0 c0 {1,S} {5,S} {6,S} {7,S}
5 C u0 p0 c0 {4,S} {8,S} {9,S} {10,S}
6 H u0 p0 c0 {4,S}
7 H u0 p0 c0 {4,S}
8 H u0 p0 c0 {5,S}
9 H u0 p0 c0 {5,S}
10 H u0 p0 c0 {5,S}
"""
)
propyl.label = "propyl"
propyli.label = "propyli"
dipropyl.label = "dipropyl"
dipropyli.label = "dipropyli"
methyl.label = "methyl"
methyli.label = "methyli"
methane.label = "methane"
methanei.label = "methanei"
# make reactions
rxn1 = TemplateReaction(
reactants=[dipropyl, methane],
products=[methyl, propyl],
kinetics=Arrhenius(A=(1.0, "cm^3/(mol*s)"), Ea=(2.0, "kJ/mol"), n=0.0),
family="H_Abstraction",
template=["a", "c"],
degeneracy=8,
pairs=[[methane, methyl], [dipropyl, propyl]],
)
rxn2 = TemplateReaction(
reactants=[methyli, propyl],
products=[methanei, dipropyl],
kinetics=Arrhenius(A=(1e-20, "cm^3/(mol*s)"), Ea=(2.0, "kJ/mol"), n=0.0),
family="H_Abstraction",
template=["b", "d"],
degeneracy=3,
pairs=[[methyli, methanei], [propyl, dipropyl]],
)
rxn3 = TemplateReaction(
reactants=[methane, dipropyli],
products=[methyl, propyli],
kinetics=Arrhenius(A=(0.5, "cm^3/(mol*s)"), Ea=(2.0, "kJ/mol"), n=0.0),
family="H_Abstraction",
template=["a", "c"],
degeneracy=4,
pairs=[[methane, methyl], [dipropyli, propyli]],
)
rxn4 = TemplateReaction(
reactants=[methyli, propyli],
products=[methanei, dipropyli],
kinetics=Arrhenius(A=(1e-20, "cm^3/(mol*s)"), Ea=(2.0, "kJ/mol"), n=0.0),
family="H_Abstraction",
template=["d", "b"],
degeneracy=3,
pairs=[[methyli, methanei], [propyli, dipropyli]],
)
rxns = [rxn1, rxn2, rxn3, rxn4]
# call method
ensure_reaction_direction(rxns)
for rxn in rxns:
# ensure there is a methane in reactants for each reaction
assert any(
[compare_isotopomers(methane, reactant) for reactant in rxn.reactants]
), "ensureReactionDirection didnt flip the proper reactants and products"
# ensure kinetics is correct
if any([dipropyli.is_isomorphic(reactant) for reactant in rxn.reactants]):
assert round(abs(rxn.kinetics.A.value - 0.5), 7) == 0, (
"The A value returned, {0}, is incorrect. "
"Check the reactions degeneracy and how A.value is obtained. "
"The reaction is:{1}".format(rxn.kinetics.A.value, rxn)
)
else:
assert round(abs(rxn.kinetics.A.value - 1.0), 7) == 0, (
"The A value returned, {0}, is incorrect. "
"Check the reactions degeneracy and how A.value is obtained. "
"The reaction is:{1}".format(rxn.kinetics.A.value, rxn)
)
def test_ensure_reaction_direction_with_multiple_ts(self):
"""Tests that ensure reaction direction can handle multiple transition states"""
family = self.database.kinetics.families["intra_H_migration"]
r = Molecule().from_smiles("[CH2]CCC")
p = Molecule().from_smiles("C[CH]CC")
rxn = TemplateReaction(reactants=[r], products=[p])
family.add_atom_labels_for_reaction(reaction=rxn)
rxn.template = family.get_reaction_template_labels(reaction=rxn)
rxn.degeneracy = family.calculate_degeneracy(rxn)
rxn.family = "intra_H_migration"
rxn.kinetics = Arrhenius(A=(1, "s^-1"))
ri = Molecule().from_adjacency_list(
"""
multiplicity 2
1 C u1 p0 c0 {2,S} {3,S} {4,S}
2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 C u0 p0 c0 i13 {1,S} {5,S} {7,S} {8,S}
5 C u0 p0 c0 i13 {4,S} {6,S} {9,S} {10,S}
6 C u0 p0 c0 {5,S} {11,S} {12,S} {13,S}
7 H u0 p0 c0 {4,S}
8 H u0 p0 c0 {4,S}
9 H u0 p0 c0 {5,S}
10 H u0 p0 c0 {5,S}
11 H u0 p0 c0 {6,S}
12 H u0 p0 c0 {6,S}
13 H u0 p0 c0 {6,S}
"""
)
pi = Molecule().from_adjacency_list(
"""
multiplicity 2
1 C u0 p0 c0 {2,S} {6,S} {7,S} {8,S}
2 C u1 p0 c0 i13 {1,S} {3,S} {4,S}
3 H u0 p0 c0 {2,S}
4 C u0 p0 c0 i13 {2,S} {5,S} {9,S} {10,S}
5 C u0 p0 c0 {4,S} {11,S} {12,S} {13,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {1,S}
8 H u0 p0 c0 {1,S}
9 H u0 p0 c0 {4,S}
10 H u0 p0 c0 {4,S}
11 H u0 p0 c0 {5,S}
12 H u0 p0 c0 {5,S}
13 H u0 p0 c0 {5,S}
"""
)
rxni = TemplateReaction(reactants=[pi], products=[ri], pairs=[[pi, ri]])
family.add_atom_labels_for_reaction(reaction=rxni)
rxni.template = family.get_reaction_template_labels(reaction=rxni)
rxn_cluster = [rxn, rxni]
ensure_reaction_direction(rxn_cluster)
assert rxn_cluster[0].degeneracy == 2
assert rxn_cluster[1].degeneracy == 2
assert "R2Hall" in rxn_cluster[0].template
assert "R2Hall" in rxn_cluster[1].template
assert round(abs(rxn_cluster[0].kinetics.get_rate_coefficient(298) - rxn_cluster[1].kinetics.get_rate_coefficient(298)), 7) == 0
def test_compare_isotopomers_works_on_species(self):
"""
Test that compareIsotomers works on species objects
"""
ethii = Species().from_adjacency_list(
"""
1 C u0 p0 c0 i13 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethi = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
assert compare_isotopomers(ethii, ethi)
def test_compare_isotopomers_does_not_alter_species(self):
"""
Test that compareIsotomers works on species objects
"""
ethii = Species().from_adjacency_list(
"""
1 C u0 p0 c0 i13 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethi = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 i13 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
compare_isotopomers(ethii, ethi)
# ensure species still have labels
for atom in ethii.molecule[0].atoms:
if atom.element.symbol == "C":
assert atom.element.isotope == 13, "compareIsotopomer removed the isotope of a species."
def test_compare_isotopomers_fails_on_species(self):
"""
Test that compareIsotomers fails on species objects
"""
ethane = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 C u0 p0 c0 {1,S} {6,S} {7,S} {8,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {2,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
"""
)
ethenei = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,D} {3,S} {4,S}
2 C u0 p0 c0 i13 {1,D} {6,S} {5,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {2,S}
6 H u0 p0 c0 {2,S}
"""
)
assert not compare_isotopomers(ethane, ethenei)
def test_compare_isotopomers_works_on_reactions(self):
"""
Test that compareIsotomers works on different reaction objects
"""
h = Species().from_adjacency_list(
"""
multiplicity 2
1 H u1 p0 c0
"""
)
h2 = Species().from_adjacency_list(
"""
1 H u0 p0 c0 {2,S}
2 H u0 p0 c0 {1,S}
"""
)
propanei = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2 C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3 C u0 p0 c0 i13 {2,S} {9,S} {10,S} {11,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
9 H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
"""
)
propane = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2 C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3 C u0 p0 c0 {2,S} {9,S} {10,S} {11,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
9 H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
"""
)
npropyli = Species().from_adjacency_list(
"""
multiplicity 2
1 C u0 p0 c0 {2,S} {6,S} {7,S} {8,S}
2 C u0 p0 c0 {1,S} {3,S} {9,S} {10,S}
3 C u1 p0 c0 i13 {2,S} {4,S} {5,S}
4 H u0 p0 c0 {3,S}
5 H u0 p0 c0 {3,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {1,S}
8 H u0 p0 c0 {1,S}
9 H u0 p0 c0 {2,S}
10 H u0 p0 c0 {2,S}
"""
)
npropyl = Species().from_adjacency_list(
"""
multiplicity 2
1 C u0 p0 c0 {2,S} {6,S} {7,S} {8,S}
2 C u0 p0 c0 {1,S} {3,S} {9,S} {10,S}
3 C u1 p0 c0 {2,S} {4,S} {5,S}
4 H u0 p0 c0 {3,S}
5 H u0 p0 c0 {3,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {1,S}
8 H u0 p0 c0 {1,S}
9 H u0 p0 c0 {2,S}
10 H u0 p0 c0 {2,S}
"""
)
reaction2 = TemplateReaction(reactants=[propanei, h], products=[npropyli, h2], family="H_Abstraction")
reaction3 = TemplateReaction(reactants=[propane, h], products=[h2, npropyl], family="H_Abstraction")
assert compare_isotopomers(reaction2, reaction3)
def test_compare_isotopomers_fails_on_reactions(self):
"""
Test that compareIsotomers fails on different reaction objects
"""
h = Species().from_adjacency_list(
"""
multiplicity 2
1 H u1 p0 c0
"""
)
h2 = Species().from_adjacency_list(
"""
1 H u0 p0 c0 {2,S}
2 H u0 p0 c0 {1,S}
"""
)
propanei = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2 C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3 C u0 p0 c0 i13 {2,S} {9,S} {10,S} {11,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
9 H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
"""
)
propane = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2 C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3 C u0 p0 c0 {2,S} {9,S} {10,S} {11,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
9 H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
"""
)
npropyli = Species().from_adjacency_list(
"""
multiplicity 2
1 C u0 p0 c0 {2,S} {6,S} {7,S} {8,S}
2 C u0 p0 c0 {1,S} {3,S} {9,S} {10,S}
3 C u1 p0 c0 i13 {2,S} {4,S} {5,S}
4 H u0 p0 c0 {3,S}
5 H u0 p0 c0 {3,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {1,S}
8 H u0 p0 c0 {1,S}
9 H u0 p0 c0 {2,S}
10 H u0 p0 c0 {2,S}
"""
)
reaction2 = TemplateReaction(reactants=[propanei, h], products=[npropyli, h2], family="H_Abstraction")
magic_reaction = TemplateReaction(reactants=[propane, h], products=[propanei, h], family="H_Abstraction")
assert not compare_isotopomers(reaction2, magic_reaction)
def test_correct_entropy(self):
"""
Tests that correctEntropy effectively makes the isotopomer have the same
thermo as isotopeless with the change in entropy corresponding to the
symmetry difference.
This example compares propane to a asymmetrically labeled propane.
"""
from rmgpy.thermo.nasa import NASA, NASAPolynomial
from copy import deepcopy
from rmgpy import constants
propanei = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2 C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3 C u0 p0 c0 i13 {2,S} {9,S} {10,S} {11,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
9 H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
"""
)
propane = Species().from_adjacency_list(
"""
1 C u0 p0 c0 {2,S} {4,S} {5,S} {6,S}
2 C u0 p0 c0 {1,S} {3,S} {7,S} {8,S}
3 C u0 p0 c0 {2,S} {9,S} {10,S} {11,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {2,S}
8 H u0 p0 c0 {2,S}
9 H u0 p0 c0 {3,S}
10 H u0 p0 c0 {3,S}
11 H u0 p0 c0 {3,S}
"""
)
# get thermo of propane
original_thermo = NASA(
polynomials=[
NASAPolynomial(
coeffs=[
1.10564,
0.0315339,
-1.48274e-05,
3.39621e-09,
-2.97953e-13,
-14351.9,
18.775,
],
Tmin=(100, "K"),
Tmax=(3370.6, "K"),
),
NASAPolynomial(
coeffs=[
-1.45473,
0.0241202,
-6.87667e-06,
9.03634e-10,
-4.48389e-14,
-6688.59,
43.0459,
],
Tmin=(3370.6, "K"),
Tmax=(5000, "K"),
),
],
Tmin=(100, "K"),
Tmax=(5000, "K"),
comment="""Thermo group additivity estimation: group(Cs-CsCsHH) + gauche(Cs(CsCsRR)) + other(R) + group(Cs-CsHHH) + gauche(Cs(Cs(CsRR)RRR)) + other(R) + group(Cs-CsHHH) + gauche(Cs(Cs(CsRR)RRR)) + other(R)""",
)
propane.thermo = deepcopy(original_thermo)
propanei.thermo = deepcopy(original_thermo)
correct_entropy(propanei, propane)
assert round(abs(propane.get_enthalpy(298) - propanei.get_enthalpy(298)), 7) == 0
assert round(abs(propanei.get_entropy(298) - propane.get_entropy(298) - constants.R * np.log(2)), 7) == 0
def test_generate_isotopomers(self):
"""
Test that the generation of isotopomers with N isotopes works.
"""
from rmgpy.thermo.nasa import NASAPolynomial, NASA
spc = Species().from_smiles("CC")
polynomial = NASAPolynomial(
coeffs=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
Tmin=(200, "K"),
Tmax=(1600, "K"),
E0=(1.0, "kJ/mol"),
comment="made up thermo",
)
spc.thermo = NASA(
polynomials=[polynomial],
Tmin=(200, "K"),
Tmax=(1600, "K"),
E0=(1.0, "kJ/mol"),
comment="made up thermo",
)
spcs = generate_isotopomers(spc, 0)
assert len(spcs) == 0
spcs = generate_isotopomers(spc)
assert len(spcs) == 1
spcs = generate_isotopomers(spc, 2)
assert len(spcs) == 2
spcs = generate_isotopomers(spc, 3)
assert len(spcs) == 2
def test_is_enriched(self):
"""
ensures the Enriched method functions
"""
npropyl = Species().from_adjacency_list(
"""
multiplicity 2
1 C u0 p0 c0 {2,S} {6,S} {7,S} {8,S}
2 C u0 p0 c0 {1,S} {3,S} {9,S} {10,S}
3 C u1 p0 c0 {2,S} {4,S} {5,S}
4 H u0 p0 c0 {3,S}
5 H u0 p0 c0 {3,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {1,S}
8 H u0 p0 c0 {1,S}
9 H u0 p0 c0 {2,S}
10 H u0 p0 c0 {2,S}
"""
)
npropyli = Species().from_adjacency_list(
"""
multiplicity 2
1 C u0 p0 c0 {2,S} {6,S} {7,S} {8,S}
2 C u0 p0 c0 i13 {1,S} {3,S} {9,S} {10,S}
3 C u1 p0 c0 {2,S} {4,S} {5,S}
4 H u0 p0 c0 {3,S}
5 H u0 p0 c0 {3,S}
6 H u0 p0 c0 {1,S}
7 H u0 p0 c0 {1,S}
8 H u0 p0 c0 {1,S}
9 H u0 p0 c0 {2,S}
10 H u0 p0 c0 {2,S}
"""
)
assert is_enriched(npropyli)
assert not is_enriched(npropyl)
enriched_reaction = TemplateReaction(reactants=[npropyl], products=[npropyli], family="H_Abstraction")
assert is_enriched(enriched_reaction)
bare_reaction = TemplateReaction(reactants=[npropyl], products=[npropyl], family="H_Abstraction")
assert not is_enriched(bare_reaction)
def test_get_labeled_reactants(self):
"""
tests to ensure that get_labeled_reactants returns labeled reactants
"""
reactant_pair = [Species().from_smiles("C"), Species().from_smiles("[H]")]
product_pair = [Species().from_smiles("[H][H]"), Species().from_smiles("[CH3]")]
rxn = TemplateReaction(reactants=reactant_pair, products=product_pair, family="H_Abstraction")
labeled_reactants = get_labeled_reactants(rxn, self.family)
r1_labels = labeled_reactants[0].get_all_labeled_atoms()
assert "*1" in list(r1_labels.keys())
assert "*2" in list(r1_labels.keys())
r2_labels = labeled_reactants[1].get_all_labeled_atoms()
assert "*3" in list(r2_labels.keys())
def test_get_reduced_mass(self):
"""
tests that get_reduced_mass returns the proper value for H_abstraction
"""
labels = ["*1", "*3"]
reactants = [
Molecule().from_adjacency_list(
"""
1 *1 C u0 p0 c0 {2,S} {3,S} {4,S} {5,S}
2 *2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
"""
),
Molecule().from_adjacency_list(
"""
multiplicity 2
1 *3 H u1 p0 c0
"""
),
]
reduced_mass = get_reduced_mass(reactants, labels, True)
assert round(abs(reduced_mass - 1 / (1 / 1.008 + 1 / (1.008 + 12.01)) / 1000), 6) == 0
def test_get_reduced_mass2(self):
"""
tests that get_reduced_mass returns proper value when isotopes are labeled
"""
labels = ["*1", "*3"]
reactants = [
Molecule().from_adjacency_list(
"""
1 *1 C u0 p0 c0 i13 {2,S} {3,S} {4,S} {5,S}
2 *2 H u0 p0 c0 {1,S}
3 H u0 p0 c0 {1,S}
4 H u0 p0 c0 {1,S}
5 H u0 p0 c0 {1,S}
"""
),
Molecule().from_adjacency_list(
"""
multiplicity 2
1 *3 H u1 p0 c0
"""
),
]
reduced_mass = get_reduced_mass(reactants, labels, True)
assert round(abs(reduced_mass - 1 / (1 / 1.008 + 1 / (1.008 + 13.01)) / 1000), 6) == 0
def test_get_kinetic_isotope_effect_simple(self):
reactant_pair = [Species().from_smiles("C"), Species().from_smiles("[H]")]
product_pair = [Species().from_smiles("[H][H]"), Species().from_smiles("[CH3]")]
rxn_unlabeled = TemplateReaction(
reactants=reactant_pair,
products=product_pair,
family="H_Abstraction",
kinetics=Arrhenius(A=(1e5, "cm^3/(mol*s)"), Ea=(0, "J/mol")),
)
rxn_labeled = TemplateReaction(
reactants=[
Species().from_adjacency_list(