From 4e32102cabc028aefe25d6a7c3842c8a04066130 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:09:28 -0700 Subject: [PATCH 01/12] use superminimal from RMS --- pyrms/testing/superminimal.rms | 498 +++++++++++++++++---------------- 1 file changed, 263 insertions(+), 235 deletions(-) diff --git a/pyrms/testing/superminimal.rms b/pyrms/testing/superminimal.rms index 290ee61..eb52a32 100644 --- a/pyrms/testing/superminimal.rms +++ b/pyrms/testing/superminimal.rms @@ -1,260 +1,288 @@ Phases: - - name: gas - Species: - - name: Ar - type: Species - smiles: '[Ar]' - thermo: - polys: - - Tmax: 1000.0 - Tmin: 200.0 - coefs: [0.0, 0.0, 2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 4.37967] - type: NASApolynomial - - Tmax: 6000.0 - Tmin: 1000.0 - coefs: [0.0, 0.0, 2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 4.37967] - type: NASApolynomial - type: NASA - - name: He - type: Species - smiles: '[He]' - thermo: - polys: - - Tmax: 1000.0 - Tmin: 200.0 - coefs: [0.0, 0.0, 2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 0.928724] - type: NASApolynomial - - Tmax: 6000.0 - Tmin: 1000.0 - coefs: [0.0, 0.0, 2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 0.928724] - type: NASApolynomial - type: NASA - - name: Ne - type: Species - smiles: '[Ne]' - thermo: - polys: - - Tmax: 1000.0 - Tmin: 200.0 - coefs: [0.0, 0.0, 2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 3.35532] - type: NASApolynomial - - Tmax: 6000.0 - Tmin: 1000.0 - coefs: [0.0, 0.0, 2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 3.35532] - type: NASApolynomial - type: NASA - - name: N2 - type: Species - smiles: N#N - thermo: - polys: - - Tmax: 1000.0 - Tmin: 200.0 - coefs: [0.0, 0.0, 3.53101, -0.000123661, -5.02999e-07, 2.43531e-09, -1.40881e-12, - -1046.98, 2.96747] - type: NASApolynomial - - Tmax: 6000.0 - Tmin: 1000.0 - coefs: [0.0, 0.0, 2.95258, 0.0013969, -4.92632e-07, 7.8601e-11, -4.60755e-15, - -923.949, 5.87189] - type: NASApolynomial - type: NASA - - name: H2 - type: Species - smiles: '[H][H]' - thermo: - polys: - - Tmax: 1959.07 - Tmin: 100.0 - coefs: [0.0, 0.0, 3.43536401, 0.000212711277, -2.78627139e-07, 3.40268811e-10, - -7.76036069e-14, -1031.35983, -3.90841691] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 1959.07 - coefs: [0.0, 0.0, 2.78817696, 0.000587626487, 1.59017184e-07, -5.52752608e-11, - 4.34320884e-15, -596.150834, 0.112666705] - type: NASApolynomial - type: NASA - - name: O2 - type: Species - smiles: '[O][O]' - thermo: - polys: - - Tmax: 1074.56 - Tmin: 100.0 - coefs: [0.0, 0.0, 3.53732181, -0.00121570933, 5.31617835e-06, -4.89443434e-09, - 1.45845047e-12, -1038.58846, 4.68368405] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 1074.56 - coefs: [0.0, 0.0, 3.15382424, 0.00167803804, -7.69971033e-07, 1.51274717e-10, - -1.08781803e-14, -1040.81878, 6.16753891] - type: NASApolynomial - type: NASA - - name: H - type: Species - smiles: '[H]' - thermo: - polys: - - Tmax: 4484.82 - Tmin: 100.0 - coefs: [0.0, 0.0, 2.5, -1.08860897e-13, 1.40734568e-16, -5.92258814e-20, 7.66587112e-24, - 25474.2178, -0.444972896] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 4484.82 - coefs: [0.0, 0.0, 2.50003916, -3.40586045e-08, 1.10996924e-11, -1.60664106e-15, - 8.71471667e-20, 25474.1818, -0.445221594] - type: NASApolynomial - type: NASA - - name: '[O]O' - type: Species - smiles: '[O]O' - thermo: - polys: - - Tmax: 932.15 - Tmin: 100.0 - coefs: [0.0, 0.0, 4.04594466, -0.00173464517, 1.03766421e-05, -1.02202388e-08, - 3.34907969e-12, -986.754236, 4.63581369] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 932.15 - coefs: [0.0, 0.0, 3.21023922, 0.00367941878, -1.27701506e-06, 2.18045101e-10, - -1.46337803e-14, -910.368761, 8.18291517] - type: NASApolynomial - type: NASA - - name: OH(D) - type: Species - smiles: '[OH]' - thermo: - polys: - - Tmax: 1145.74 - Tmin: 100.0 - coefs: [0.0, 0.0, 3.51456708, 2.9288145e-05, -5.32198348e-07, 1.01953157e-09, - -3.85960991e-13, 3414.25424, 2.10435232] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 1145.74 - coefs: [0.0, 0.0, 3.07194683, 0.000604004352, -1.39719923e-08, -2.13460568e-11, - 2.48077407e-15, 3579.38361, 4.57796014] - type: NASApolynomial - type: NASA - - name: O(T) - type: Species - smiles: '[O]' - thermo: - polys: - - Tmax: 4484.82 - Tmin: 100.0 - coefs: [0.0, 0.0, 2.5, -1.08860897e-13, 1.40734568e-16, -5.92258814e-20, 7.66587112e-24, - 29230.2441, 4.09104286] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 4484.82 - coefs: [0.0, 0.0, 2.50003916, -3.40586045e-08, 1.10996924e-11, -1.60664106e-15, - 8.71471667e-20, 29230.2081, 4.09079416] - type: NASApolynomial - type: NASA - - name: OO - type: Species - smiles: OO - thermo: - polys: - - Tmax: 908.88 - Tmin: 100.0 - coefs: [0.0, 0.0, 3.73134853, 0.0033508285, 9.3498791e-06, -1.52093238e-08, - 6.41555638e-12, -17721.1704, 5.45914195] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 908.88 - coefs: [0.0, 0.0, 5.41581398, 0.00261003418, -4.39864183e-07, 4.91020782e-11, - -3.35132535e-15, -18302.9631, -4.02263225] - type: NASApolynomial - type: NASA - - name: H2O - type: Species - smiles: O - thermo: - polys: - - Tmax: 1130.24 - Tmin: 100.0 - coefs: [0.0, 0.0, 4.05763582, -0.000787935728, 2.90877451e-06, -1.47518811e-09, - 2.12842752e-13, -30281.5866, -0.311364019] - type: NASApolynomial - - Tmax: 5000.0 - Tmin: 1130.24 - coefs: [0.0, 0.0, 2.84325058, 0.00275108526, -7.81031384e-07, 1.07243617e-10, - -5.79392056e-15, -29958.6128, 5.91041922] - type: NASApolynomial - type: NASA - +- Species: + - name: Ar + radicalelectrons: 0 + smiles: '[Ar]' + thermo: + polys: + - Tmax: 1000.0 + Tmin: 200.0 + coefs: [2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 4.37967] + type: NASApolynomial + - Tmax: 6000.0 + Tmin: 1000.0 + coefs: [2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 4.37967] + type: NASApolynomial + type: NASA + type: Species + - name: He + radicalelectrons: 0 + smiles: '[He]' + thermo: + polys: + - Tmax: 1000.0 + Tmin: 200.0 + coefs: [2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 0.928724] + type: NASApolynomial + - Tmax: 6000.0 + Tmin: 1000.0 + coefs: [2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 0.928724] + type: NASApolynomial + type: NASA + type: Species + - name: Ne + radicalelectrons: 0 + smiles: '[Ne]' + thermo: + polys: + - Tmax: 1000.0 + Tmin: 200.0 + coefs: [2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 3.35532] + type: NASApolynomial + - Tmax: 6000.0 + Tmin: 1000.0 + coefs: [2.5, 0.0, 0.0, 0.0, 0.0, -745.375, 3.35532] + type: NASApolynomial + type: NASA + type: Species + - name: N2 + radicalelectrons: 0 + smiles: N#N + thermo: + polys: + - Tmax: 1000.0 + Tmin: 200.0 + coefs: [3.53101, -0.000123661, -5.02999e-07, 2.43531e-09, -1.40881e-12, -1046.98, + 2.96747] + type: NASApolynomial + - Tmax: 6000.0 + Tmin: 1000.0 + coefs: [2.95258, 0.0013969, -4.92632e-07, 7.8601e-11, -4.60755e-15, -923.949, + 5.87189] + type: NASApolynomial + type: NASA + type: Species + - name: H2 + radicalelectrons: 0 + smiles: '[H][H]' + thermo: + polys: + - Tmax: 1959.07 + Tmin: 100.0 + coefs: [3.43536393, 0.000212711953, -2.78628671e-07, 3.40270013e-10, -7.76039045e-14, + -1031.35983, -3.90841661] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 1959.07 + coefs: [2.78818509, 0.000587615921, 1.5902213e-07, -5.52762536e-11, 4.3432812e-15, + -596.155632, 0.112618494] + type: NASApolynomial + type: NASA + type: Species + - name: O2 + radicalelectrons: 0 + smiles: '[O][O]' + thermo: + polys: + - Tmax: 1074.55 + Tmin: 100.0 + coefs: [3.53732243, -0.00121571647, 5.31620254e-06, -4.89446434e-09, 1.45846258e-12, + -1038.58849, 4.68368183] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 1074.55 + coefs: [3.15382081, 0.00167804371, -7.69974236e-07, 1.51275462e-10, -1.08782414e-14, + -1040.81728, 6.16755832] + type: NASApolynomial + type: NASA + type: Species + - name: HO2 + radicalelectrons: 1 + smiles: '[O]O' + thermo: + polys: + - Tmax: 932.15 + Tmin: 100.0 + coefs: [4.04594488, -0.00173464779, 1.03766518e-05, -1.02202522e-08, 3.34908581e-12, + -986.754245, 4.63581294] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 932.15 + coefs: [3.21023857, 0.00367941991, -1.27701572e-06, 2.18045259e-10, -1.46337935e-14, + -910.368497, 8.1829188] + type: NASApolynomial + type: NASA + type: Species + - name: OH + radicalelectrons: 1 + smiles: '[OH]' + thermo: + polys: + - Tmax: 1145.76 + Tmin: 100.0 + coefs: [3.51456841, 2.92732126e-05, -5.32149895e-07, 1.01947438e-09, -3.85939087e-13, + 3414.25418, 2.10434749] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 1145.76 + coefs: [3.0719371, 0.000604020067, -1.39807196e-08, -2.13440522e-11, 2.4806113e-15, + 3579.38798, 4.57801549] + type: NASApolynomial + type: NASA + type: Species + - name: H + radicalelectrons: 1 + smiles: '[H]' + thermo: + polys: + - Tmax: 4879.8 + Tmin: 100.0 + coefs: [2.5, -3.01680531e-12, 3.74582141e-15, -1.50856878e-18, 1.86626471e-22, + 25474.2178, -0.444972899] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 4879.8 + coefs: [4.28461071, -0.00145494649, 4.44804306e-07, -6.04359642e-11, 3.07921551e-15, + 23723.0923, -11.8931307] + type: NASApolynomial + type: NASA + type: Species + - name: H2O + radicalelectrons: 0 + smiles: O + thermo: + polys: + - Tmax: 1130.24 + Tmin: 100.0 + coefs: [4.05763557, -0.000787932878, 2.90876518e-06, -1.47517698e-09, 2.12838441e-13, + -30281.5866, -0.311363105] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 1130.24 + coefs: [2.84325231, 0.00275108244, -7.81029811e-07, 1.07243254e-10, -5.79389106e-15, + -29958.6136, 5.91040933] + type: NASApolynomial + type: NASA + type: Species + - name: H2O2 + radicalelectrons: 0 + smiles: OO + thermo: + polys: + - Tmax: 908.87 + Tmin: 100.0 + coefs: [3.73136061, 0.00335067714, 9.35045149e-06, -1.52101308e-08, 6.41593098e-12, + -17721.1709, 5.4590992] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 908.87 + coefs: [5.41578065, 0.00261009268, -4.39898683e-07, 4.91103613e-11, -3.35202076e-15, + -18302.9497, -4.02244574] + type: NASApolynomial + type: NASA + type: Species + - name: O(T) + radicalelectrons: 2 + smiles: '[O]' + thermo: + polys: + - Tmax: 4879.8 + Tmin: 100.0 + coefs: [2.5, -3.01680531e-12, 3.74582141e-15, -1.50856878e-18, 1.86626471e-22, + 29230.2441, 4.09104286] + type: NASApolynomial + - Tmax: 5000.0 + Tmin: 4879.8 + coefs: [4.28461071, -0.00145494649, 4.44804306e-07, -6.04359642e-11, 3.07921551e-15, + 27479.1187, -7.35711496] + type: NASApolynomial + type: NASA + type: Species + name: gas Reactions: -- products: [H2] +- kinetics: {A: 54500.00000000001, Ea: 6276.0, n: 0.0, type: Arrhenius} + products: [H2] + radicalchange: -2 reactants: [H, H] - kinetics: {A: 54500.00000000001, Ea: 6276.0, n: 0.0, type: Arrhenius} type: ElementaryReaction -- products: ['[O]O', H] - reactants: [O2, H2] - kinetics: {A: 290000000.00000006, Ea: 236981.76, n: 0.0, type: Arrhenius} +- kinetics: {A: 290000000.00000006, Ea: 236981.76, n: 0.0, type: Arrhenius} + products: [HO2, H] + radicalchange: 2 + reactants: [H2, O2] type: ElementaryReaction -- products: ['[O]O'] +- kinetics: {A: 87900.00000000001, Ea: 1882.8, n: 1.0, type: Arrhenius} + products: [HO2] + radicalchange: 0 reactants: [O2, H] - kinetics: {A: 87900.00000000001, Ea: 1882.8, n: 1.0, type: Arrhenius} type: ElementaryReaction -- products: [H2, '[O]O'] - reactants: [H, OO] - kinetics: {A: 1.2350200000000002, Ea: 25463.824, n: 1.634, type: Arrhenius} +- kinetics: {A: 1.2350200000000002, Ea: 25463.824, n: 1.634, type: Arrhenius} + products: [H2, HO2] + radicalchange: 0 + reactants: [H, H2O2] type: ElementaryReaction -- products: [OO] - reactants: ['[O]O', H] - kinetics: {A: 5005182.000000001, Ea: 7096.063999999999, n: 0.282, type: Arrhenius} +- kinetics: {A: 5005182.000000001, Ea: 7096.063999999999, n: 0.282, type: Arrhenius} + products: [H2O2] + radicalchange: -2 + reactants: [HO2, H] type: ElementaryReaction -- products: [O2, OO] - reactants: ['[O]O', '[O]O'] - kinetics: {A: 17500.000000000004, Ea: -13702.6, n: 0.0, type: Arrhenius} +- kinetics: {A: 17500.000000000004, Ea: -13702.6, n: 0.0, type: Arrhenius} + products: [O2, H2O2] + radicalchange: -2 + reactants: [HO2, HO2] type: ElementaryReaction -- products: [OO] - reactants: [OH(D), OH(D)] - kinetics: {A: 7850000.000000001, Ea: 0.0, n: 0.0, type: Arrhenius} +- kinetics: {A: 7850000.000000001, Ea: 0.0, n: 0.0, type: Arrhenius} + products: [H2O2] + radicalchange: -2 + reactants: [OH, OH] type: ElementaryReaction -- products: [H, H2O] - reactants: [H2, OH(D)] - kinetics: {A: 1820.0000000000002, Ea: 83972.88, n: 1.21, type: Arrhenius} +- kinetics: {A: 1820.0000000000002, Ea: 83972.88, n: 1.21, type: Arrhenius} + products: [H, H2O] + radicalchange: 0 + reactants: [H2, OH] type: ElementaryReaction -- products: [H2O] - reactants: [H, OH(D)] - kinetics: {A: 162000000.00000003, Ea: 627.6, n: 0.0, type: Arrhenius} +- kinetics: {A: 162000000.00000003, Ea: 627.6, n: 0.0, type: Arrhenius} + products: [H2O] + radicalchange: -2 + reactants: [OH, H] type: ElementaryReaction -- products: ['[O]O', OH(D)] +- kinetics: {A: 9300000.000000002, Ea: 310118.08, n: 0.0, type: Arrhenius} + products: [HO2, OH] + radicalchange: 2 reactants: [O2, H2O] - kinetics: {A: 9300000.000000002, Ea: 310118.08, n: 0.0, type: Arrhenius} type: ElementaryReaction -- products: ['[O]O', H2O] - reactants: [OH(D), OO] - kinetics: {A: 0.4994995000000001, Ea: 26463.8, n: 1.927, type: Arrhenius} +- kinetics: {A: 0.4994995000000001, Ea: 26463.8, n: 1.927, type: Arrhenius} + products: [HO2, H2O] + radicalchange: 0 + reactants: [OH, H2O2] type: ElementaryReaction -- products: ['[O]O'] - reactants: [OH(D), O(T)] - kinetics: {A: 43.58386000000001, Ea: 5167.240000000001, n: 1.88, type: Arrhenius} +- kinetics: {A: 43.58386000000001, Ea: 5167.240000000001, n: 1.88, type: Arrhenius} + products: [HO2] + radicalchange: -2 + reactants: [OH, O(T)] type: ElementaryReaction -- products: [OH(D)] +- kinetics: {A: 10000000.000000002, Ea: 0.0, n: 0.0, type: Arrhenius} + products: [OH] + radicalchange: -2 reactants: [H, O(T)] - kinetics: {A: 10000000.000000002, Ea: 0.0, n: 0.0, type: Arrhenius} type: ElementaryReaction -- products: [O2, OH(D)] - reactants: ['[O]O', O(T)] - kinetics: {A: 3680.3920000000007, Ea: 24681.416, n: 0.678, type: Arrhenius} +- kinetics: {A: 3680.3920000000007, Ea: 24681.416, n: 0.678, type: Arrhenius} + products: [O2, OH] + radicalchange: -2 + reactants: [HO2, O(T)] type: ElementaryReaction -- products: [H, OH(D)] +- kinetics: {A: 340.00000000000006, Ea: 96022.8, n: 1.5, type: Arrhenius} + products: [OH, H] + radicalchange: 0 reactants: [H2, O(T)] - kinetics: {A: 340.00000000000006, Ea: 96022.8, n: 1.5, type: Arrhenius} type: ElementaryReaction -- products: ['[O]O', OH(D)] - reactants: [O(T), OO] - kinetics: {A: 17400000.000000004, Ea: 19874.0, n: 0.0, type: Arrhenius} +- kinetics: {A: 17400000.000000004, Ea: 19874.0, n: 0.0, type: Arrhenius} + products: [HO2, OH] + radicalchange: 0 + reactants: [H2O2, O(T)] type: ElementaryReaction -- products: [OH(D), OH(D)] - reactants: [O(T), H2O] - kinetics: {A: 5260.000000000001, Ea: 74600.71999999999, n: 1.2, type: Arrhenius} +- kinetics: {A: 5260.000000000001, Ea: 74600.71999999999, n: 1.2, type: Arrhenius} + products: [OH, OH] + radicalchange: 0 + reactants: [H2O, O(T)] type: ElementaryReaction Units: {} From c5248090ef3d1e23de45df11aaaba1aa9af065dc Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:09:40 -0700 Subject: [PATCH 02/12] add environment file --- environment.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 environment.yml diff --git a/environment.yml b/environment.yml new file mode 100644 index 0000000..e6f9396 --- /dev/null +++ b/environment.yml @@ -0,0 +1,15 @@ +name: pyrms_env +channels: + - defaults + - mjohnson541 + - conda-forge +dependencies: + - juliaup + - nose + - pyjuliacall + - rmgmolecule >= 0.3 + - rdkit + - pydot + - matplotlib + - numpy + - jupyter \ No newline at end of file From b4729df0f335e42356cc7a435da0cefa98c122eb Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:10:26 -0700 Subject: [PATCH 03/12] automate julia install (based on diffeqpy) --- pyrms/__init__.py | 55 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/pyrms/__init__.py b/pyrms/__init__.py index e47cc7d..efbd187 100644 --- a/pyrms/__init__.py +++ b/pyrms/__init__.py @@ -1,16 +1,45 @@ -import os -import subprocess +import shutil +from subprocess import run +# juliacall must be loaded after `_ensure_julia_installed()` is run, +# so this import is in `load_julia_packages()` +# from juliacall import Main -def install(): +def _find_julia(): + return shutil.which("julia") + +def _ensure_julia_installed(): + if not _find_julia(): + print("No Julia version found. Installing Julia.") + run("juliaup update") + if not _find_julia(): + raise RuntimeError( + "Julia installed with jill but `julia` binary cannot be found in the path" + ) + +# TODO: upstream this function or an alternative into juliacall +def load_julia_packages(*names): """ - Install Julia packages required for diffeqpy. + Load Julia packages and return references to them, automatically installing julia and + the packages as necessary. """ - import julia - import diffeqpy - julia.install() - diffeqpy.install() - from julia.api import Julia - jl = Julia(compiled_modules=False) - from julia import Pkg - Pkg.add("ReactionMechanismSimulator") - from julia import ReactionMechanismSimulator + # This is terrifying to many people. However, it seems SciML takes pragmatic approach. + _ensure_julia_installed() + + script = """import Pkg + Pkg.activate(\"pyrms\", shared=true) + try + import {0} + catch e + e isa ArgumentError || throw(e) + Pkg.add([{1}]) + import {0} + end + {0}""".format(", ".join(names), ", ".join(f'"{name}"' for name in names)) + + # Unfortunately, `seval` doesn't support multi-line strings + # https://github.com/JuliaPy/PythonCall.jl/issues/433 + script = script.replace("\n", ";") + + # Must be loaded after `_ensure_julia_installed()` + from juliacall import Main + return Main.seval(script) From abcbad34ecc61e713f98bea0d13d3ee0e506755c Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:11:47 -0700 Subject: [PATCH 04/12] adapt to juliacall based on diffeqpy should no longer need threadedsensitivities adaption because conversion isn't automatic in juliacall can't directly replace getfluxdiagram so create pygetfluxdiagram --- pyrms/rms.py | 58 ++++------------------------------------------------ 1 file changed, 4 insertions(+), 54 deletions(-) diff --git a/pyrms/rms.py b/pyrms/rms.py index f87927c..5691df8 100644 --- a/pyrms/rms.py +++ b/pyrms/rms.py @@ -1,59 +1,9 @@ -import os import sys from IPython.display import Image import IPython.display +from . import load_julia_packages +rms, _, = load_julia_packages("ReactionMechanismSimulator", "PythonCall") +from juliacall import Main -from julia import Main -from julia import ReactionMechanismSimulator +sys.modules[__name__] = rms -from julia.ReactionMechanismSimulator import makefluxdiagrams - -def pygetfluxdiagram(bsol,t,centralspecieslist=[],superimpose=False, - maximumnodecount=50, maximumedgecount=50, concentrationtol=1e-6, speciesratetolerance=1e-6, - maximumnodepenwidth=10.0,maximumedgepenwidth=10.0,radius=1,centralreactioncount=-1,outputdirectory="fluxdiagrams", - colorscheme="viridis"): - - fd = makefluxdiagrams(bsol,[t], centralspecieslist=centralspecieslist,superimpose=superimpose, - maximumnodecount=maximumnodecount, maximumedgecount=maximumedgecount, concentrationtol=concentrationtol, - speciesratetolerance=speciesratetolerance,maximumnodepenwidth=maximumnodepenwidth, - maximumedgepenwidth=maximumedgepenwidth,radius=radius,centralreactioncount=centralreactioncount, - outputdirectory=outputdirectory,colorscheme=colorscheme) - - IPython.display.display(IPython.display.Image(os.path.join(fd.outputdirectory,"flux_diagram_1.png"))) - -ReactionMechanismSimulator.getfluxdiagram = pygetfluxdiagram - -# These functions force the returned object to be a jlwrap object, -# to avoid the solution object gets converted into list of lists -_threadedsensitivities_inds = Main.pyfunctionret(ReactionMechanismSimulator.threadedsensitivities, Main.Any, Main.PyAny, Main.PyAny) -_threadedsensitivities = Main.pyfunctionret(ReactionMechanismSimulator.threadedsensitivities, Main.Any, Main.PyAny) -# Allow us the get the solution object in the julia wrapped object without -# it being converted into list of lists -get = Main.pyfunctionret(Main.get, Main.Any, Main.PyAny, Main.PyAny, Main.PyAny) -def pythreadedsensitivities(react, inds=None, - odesolver=Main.nothing, senssolver=Main.nothing, - odekwargs={"abstol": 1e-20, "reltol": 1e-6}, - senskwargs={"abstol": 1e-6, "reltol": 1e-3}): - if inds is not None: - # the sol_dict returned here is a jlwrap object and is not easy to access the contents - sol_dict = _threadedsensitivities_inds(react, inds, - odesolver=odesolver, senssolver=senssolver, - odekwargs=odekwargs, senskwargs=senskwargs) - # pysol_dict is the python dictionary, but the solution object got turned into list of lists - pysol_dict = Main.PyObject(sol_dict) - # this remakes the dictionary with the values as julia wrapped object ODEsolution, - # but at the same time allows the user to access it normally like a dictionary - return {key: get(sol_dict, key, Main.Any) for key in pysol_dict.keys()} - else: - sol = _threadedsensitivities(react, - odesolver=odesolver, senssolver=senssolver, - odekwargs=odekwargs, senskwargs=senskwargs) - return sol - -ReactionMechanismSimulator.threadedsensitivities = pythreadedsensitivities - -for item in dir(ReactionMechanismSimulator): - try: - locals()[item] = getattr(ReactionMechanismSimulator,item) - except AttributeError: - pass From 8a30127992f421ae06fd1fa28ba9bfcec1ccf6c3 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:12:16 -0700 Subject: [PATCH 05/12] rename and adapt notebook example --- ...yrms example.ipynb => pyrms_example.ipynb} | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) rename Ipython/{pyrms example.ipynb => pyrms_example.ipynb} (83%) diff --git a/Ipython/pyrms example.ipynb b/Ipython/pyrms_example.ipynb similarity index 83% rename from Ipython/pyrms example.ipynb rename to Ipython/pyrms_example.ipynb index c47e5f1..169f9cb 100644 --- a/Ipython/pyrms example.ipynb +++ b/Ipython/pyrms_example.ipynb @@ -6,8 +6,8 @@ "metadata": {}, "outputs": [], "source": [ - "from diffeqpy import de\n", "from pyrms import rms\n", + "from juliacall import Main as jl\n", "from matplotlib import pyplot as plt\n", "%matplotlib inline" ] @@ -55,7 +55,7 @@ "metadata": {}, "outputs": [], "source": [ - "domain,y0 = rms.ConstantTPDomain(phase=ig,initialconds=initialconds)" + "domain,y0,p = rms.ConstantTPDomain(phase=ig,initialconds=jl.convert(jl.Dict,initialconds))" ] }, { @@ -64,7 +64,7 @@ "metadata": {}, "outputs": [], "source": [ - "react = rms.Reactor(domain,y0,(0.0,10.001))" + "react = rms.Reactor(domain,y0,(0.0,10.001),p=p)" ] }, { @@ -73,7 +73,7 @@ "metadata": {}, "outputs": [], "source": [ - "sol = de.solve(react.ode,de.CVODE_BDF(),abstol=1e-20,reltol=1e-8)" + "sol = rms.solve(react.ode,rms.CVODE_BDF(),abstol=1e-20,reltol=1e-8)" ] }, { @@ -100,7 +100,7 @@ "metadata": {}, "outputs": [], "source": [ - "rms.plotrops(sim,\"OH(D)\",1.0,N=10)" + "rms.plotrops(sim,\"OH\",1.0,N=10)" ] }, { @@ -122,23 +122,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.15" + "pygments_lexer": "ipython3", + "version": "3.9.19" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } From 080381339ad4a4b9f87db6c9a080054c1e796875 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:12:26 -0700 Subject: [PATCH 06/12] adapt test --- pyrms/rmsTest.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pyrms/rmsTest.py b/pyrms/rmsTest.py index 23ee408..b0b7b91 100644 --- a/pyrms/rmsTest.py +++ b/pyrms/rmsTest.py @@ -1,8 +1,7 @@ import unittest -import math from pyrms import rms -from diffeqpy import de +from juliacall import Main as jl class Testrms(unittest.TestCase): def test_simulate(self): @@ -11,8 +10,8 @@ def test_simulate(self): rxns = phaseDict["gas"]["Reactions"] ig = rms.IdealGas(spcs,rxns,name="gas") initialconds = {"T":1000.0,"P":10.0e5,"H2":2.0,"O2":1.0} - domain,y0 = rms.ConstantTPDomain(phase=ig,initialconds=initialconds) - react = rms.Reactor(domain,y0,(0.0,10.001)) - sol = de.solve(react.ode,de.CVODE_BDF(),abstol=1e-20,reltol=1e-8) + domain,y0,p = rms.ConstantTPDomain(phase=ig,initialconds=jl.convert(jl.Dict,initialconds)) + react = rms.Reactor(domain,y0,(0.0,10.001),p=p) + sol = rms.solve(react.ode,rms.CVODE_BDF(),abstol=1e-20,reltol=1e-8) sim = rms.Simulation(sol,domain) self.assertAlmostEqual(rms.molefractions(sim,"H2",3.0),0.34445669,places=3) From b332b89b04f83c7b10fc9c9187083097cff3bc10 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:12:41 -0700 Subject: [PATCH 07/12] adapt README --- README.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 343b90a..cd1faa5 100644 --- a/README.md +++ b/README.md @@ -9,16 +9,15 @@ In theory this wraps all functionality within RMS with two caveats: 1) In jupyter notebooks julia objects don't display the same way in the python kernel as they would in the julia kernel. For example flux diagram generation had to be hard coded into this wrapper to display properly. If this happens please make an issue. -2) There are ways to define julia functions that makes them impossible to call from python using the pyjulia. In most cases this is easy to fix. If you find a case where this happens please make an issue. +2) There are ways to define julia functions that makes them impossible to call from python. In most cases this is easy to fix. If you find a case where this happens please make an issue. ## Installation Instructions from Anaconda: -Note: We recommend installing pyrms in its own conda environment when convenient because getting pyjulia to work with conda python efficiently currently requires relinking - the python executable to the python-jl executable, this usually isn't problematic, but it can be an issue in more complicated conda environments. +We recommend installing pyrms in its own conda environment -To Install Binaries with Conda Run: +To Install Binaries with Conda Run: ``` conda install -c rmg pyrms -python -c "import pyrms; pyrms.install()" -ln -sfn $(which python-jl) $(which python) ``` + +Note the install process occurs automatically on your first import. \ No newline at end of file From a564d384fe7db33e04d7e33a21ac2ef86f1f0879 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:12:50 -0700 Subject: [PATCH 08/12] increment to 2.0.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 92681a2..0f94e56 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="pyrms", # Replace with your own username - version="1.0.2", + version="2.0.0", author="mjohnson541", author_email="mjohnson541@gmail.com", description="", From 3e8e1618199dbb920a97150c1731392cc99b2a1f Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:16:09 -0700 Subject: [PATCH 09/12] add conda build filles --- .conda/build.sh | 2 ++ .conda/meta.yaml | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 .conda/build.sh create mode 100644 .conda/meta.yaml diff --git a/.conda/build.sh b/.conda/build.sh new file mode 100644 index 0000000..1d85f53 --- /dev/null +++ b/.conda/build.sh @@ -0,0 +1,2 @@ +juliaup update +python -c "from pyrms import rms" \ No newline at end of file diff --git a/.conda/meta.yaml b/.conda/meta.yaml new file mode 100644 index 0000000..584ddb3 --- /dev/null +++ b/.conda/meta.yaml @@ -0,0 +1,42 @@ +#for conda build +package: + name: pyrms + version: {{ environ.get('GIT_DESCRIBE_TAG', '') }} + +source: + path: ../ + +build: + number: {{ environ.get('GIT_DESCRIBE_NUMBER', 0) }} + script_env: + - JULIAUP_DEPOT_PATH=$PREFIX/share/juliaup + - JULIA_DEPOT_PATH=$PREFIX/share/julia + - JULIA_CONDAPKG_ENV=$PREFIX + +requirements: + host: + - pyjuliacall + - rmgmolecule >=0.3 + - rdkit >=2015.09.2 + - setuptools + - juliaup + - numpy + run: + - pydot >=2.0 + - yaml + - pyjuliacall + - rmgmolecule >=0.3 + - rdkit >=2015.09.2 + - juliaup + - jupyter + - matplotlib + - nose +test: + imports: + - pyrms + commands: + - nosetests pyrms/rmsTest.py # [unix] +about: + home: http://github.com/ReactionMechanismGenerator/pyrms + license: MIT + summary: "A program for simulating and analyzing kinetic models of chemical reaction mechanisms." From 041881fd2bb1dbfca449fb9775f0c0351a39a2bb Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sun, 7 Jul 2024 19:19:53 -0700 Subject: [PATCH 10/12] add CI workflow --- .github/workflows/CI.yml | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 .github/workflows/CI.yml diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml new file mode 100644 index 0000000..bcc2126 --- /dev/null +++ b/.github/workflows/CI.yml @@ -0,0 +1,48 @@ +name: CI + +on: + push: + branches: + - main + tags: '*' + pull_request: + workflow_dispatch: + +jobs: + test: + runs-on: ${{ matrix.os }} + defaults: + run: + shell: bash -l {0} + strategy: + matrix: + os: + - ubuntu-latest + architecture: [x64] + python-version: ['3.9'] + fail-fast: false + name: Test ${{ matrix.os }} ${{ matrix.architecture }} + Python ${{ matrix.python-version }} + steps: + - uses: actions/checkout@v1 + - name: Setup python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + architecture: ${{ matrix.architecture }} + - name: Setup Conda Environment + uses: conda-incubator/setup-miniconda@v3 + with: + activate-environment: rms_env + environment-file: environment.yml + miniforge-variant: Mambaforge + miniforge-version: "latest" + python-version: ${{ matrix.python-version }} + use-mamba: true + - name: Conda Info + run: | + mamba info + mamba list + - name: Run test + run: | + nosetests pyrms/rmsTest.py \ No newline at end of file From d99d71078b637a695d62e22aeee02a0f905f2ca7 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sat, 3 Aug 2024 16:45:30 -0700 Subject: [PATCH 11/12] require pydot >= 2.0 --- environment.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment.yml b/environment.yml index e6f9396..3c5f82c 100644 --- a/environment.yml +++ b/environment.yml @@ -9,7 +9,7 @@ dependencies: - pyjuliacall - rmgmolecule >= 0.3 - rdkit - - pydot + - pydot >= 2.0 - matplotlib - numpy - jupyter \ No newline at end of file From 7864ec5bfde6307f4e6c54c24c9624eea6f21862 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Sat, 3 Aug 2024 17:03:08 -0700 Subject: [PATCH 12/12] test notebook in CI workflow --- .github/workflows/CI.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index bcc2126..5bbf8bd 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -39,10 +39,15 @@ jobs: miniforge-version: "latest" python-version: ${{ matrix.python-version }} use-mamba: true + - name: Install packages + run: | + python -m pip install pytest nbmake + python -m pip install -e . - name: Conda Info run: | mamba info mamba list - name: Run test run: | - nosetests pyrms/rmsTest.py \ No newline at end of file + nosetests pyrms/rmsTest.py + pytest --nbmake Ipython/pyrms_example.ipynb \ No newline at end of file