From 6b7c27743e3e380417ec3a598c1ed76a2760c9c5 Mon Sep 17 00:00:00 2001 From: ssun30 Date: Mon, 12 Jun 2023 21:19:46 -0400 Subject: [PATCH] Added Kinetics Test Class --- .github/workflows/main.yml | 3 +- test/matlab_experimental/ctTestKinetics.m | 156 ++++++++++++++++++++++ test/matlab_experimental/ctTestTearDown.m | 4 +- test/matlab_experimental/ctTestThermo.m | 1 + 4 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 test/matlab_experimental/ctTestKinetics.m diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2a446dace20..04546fb13c0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,7 +27,6 @@ jobs: LD_PRELOAD: /usr/lib/x86_64-linux-gnu/libstdc++.so.6 CANTERA_ROOT: /home/runner/work/cantera/cantera CANTERA_DATA: /home/runner/work/cantera/cantera/data - CANTERA_TEST: /home/runner/work/cantera/cantera/test/matlab_experimental steps: - uses: actions/checkout@v3 name: Checkout the repository @@ -66,7 +65,7 @@ jobs: - name: Run tests uses: matlab-actions/run-tests@v1 with: - select-by-folder: $CANTERA_TEST + select-by-folder: /home/runner/work/cantera/cantera/test/matlab_experimental ubuntu-multiple-pythons: diff --git a/test/matlab_experimental/ctTestKinetics.m b/test/matlab_experimental/ctTestKinetics.m new file mode 100644 index 00000000000..d3c101667bf --- /dev/null +++ b/test/matlab_experimental/ctTestKinetics.m @@ -0,0 +1,156 @@ +classdef ctTestKinetics < matlab.unittest.TestCase + + properties + phase + rtol = 1e-6; + atol = 1e-8; + end + + methods (TestClassSetup) + + function testSetUp(self) + ctTestSetUp + end + + end + + methods (TestClassTeardown) + + function testTearDown(self) + ctCleanUp + ctTestTearDown + end + + end + + methods (TestMethodSetup) + + function createPhase(self) + src = 'h2o2.yaml'; + id = 'ohmech'; + transport = 'none'; + self.phase = Solution(src, id, transport); + self.phase.TPX = {800, 2 * OneAtm, ... + [0.1, 1e-4, 1e-5, 0.2, 2e-4, 0.3, 1e-6, 5e-5, 0.4, 0]}; + end + + end + + methods (TestMethodTeardown) + + function deleteSolution(self) + clear self.phase; + end + + end + + methods + + end + + methods (Test) + + function testCounts(self) + self.verifyEqual(self.phase.nReactions, 29); + self.verifyEqual(self.phase.nTotalSpecies, 10); + self.verifyEqual(self.phase.nPhases, 1); + self.verifyEqual(self.phase.reactionPhaseIndex, 0); + end + + function testIsReversible(self) + + for i = 1:self.phase.nReactions + self.verifyTrue(self.phase.isReversible(i)); + end + + end + + function testMultipler(self) + f0 = self.phase.forwardRatesOfProgress; + r0 = self.phase.reverseRatesOfProgress; + + self.phase.setMultiplier(2.0, 1); + self.phase.setMultiplier(0.1, 7); + + f1 = self.phase.forwardRatesOfProgress; + r1 = self.phase.reverseRatesOfProgress; + + self.verifyEqual(2 .* f0(1), f1(1), 'AbsTol', self.atol); + self.verifyEqual(2 .* f0(7), f1(7), 'AbsTol', self.atol); + self.verifyEqual(2 .* r0(1), r1(1), 'AbsTol', self.atol); + self.verifyEqual(2 .* r0(7), r1(7), 'AbsTol', self.atol); + + for i = 1:self.phase.nReactions + + if i ~= 1 || i ~= 7 + self.verifyEqual(f0(i), f1(i), 'AbsTol', self.atol); + self.verifyEqual(r0(i), r1(i), 'AbsTol', self.atol); + end + + end + + self.phase.setMultiplier(0.5); + f2 = self.phase.forwardRatesOfProgress; + r2 = self.phase.reverseRatesOfProgress; + tol = ones(1, self.phase.nReactions) .* self.atol; + self.verifyEqual(0.5 .* f0, f2, 'AbsTol', tol); + + end + + function testReactionEquations(self) + self.verifyEqual(self.phase.nReactions, ... + length(self.phase.reactionEquations)); + s = strsplit(self.phase.reactionEquation(19), '<=>'); + r = s{1}; + p = s{2}; + self.verifySubstring(r, 'H'); + self.verifySubstring(r, 'H2O2'); + self.verifySubstring(p, 'HO2'); + self.verifySubstring(p, 'H2'); + + end + + function testStoichCoeffs(self) + nu_r = self.phase.reactantStoichCoeffs; + nu_p = self.phase.productStoichCoeffs; + + function checkReactnat(s, i, val) + k = self.phase.kineticsSpeciesIndex(s); + self.verifyEqual(self.phase.reactantStoichCoeffs(s, i), ... + val); + self.verifyEqual(self.phase.reactantStoichCoeffs(k, i), ... + val); + self.verifyEqual(nu_r(k, i), val); + end + + function checkProduct(s, i, val) + k = self.phase.kineticsSpeciesIndex(s); + self.verifyEqual(self.phase.productStoichCoeffs(s, i), ... + val); + self.verifyEqual(self.phase.productStoichCoeffs(k, i), ... + val); + self.verifyEqual(nu_p(k, i), val); + end + + % H + H2O2 <=> HO2 + H2 + checkReactant('H', 19, 1) + checkReactant('H2O2', 19, 1) + checkReactant('HO2', 19, 0) + checkReactant('H2', 19, 0) + + checkProduct('H', 19, 0) + checkProduct('H2O2', 19, 0) + checkProduct('HO2', 19, 1) + checkProduct('H2', 19, 1) + + % 2 O + M <=> O2 + M + checkReactant('O', 1, 2) + checkReactant('O2', 1, 0) + checkProduct('O', 1, 0) + checkProduct('O2', 1, 1) + + end + + end + +end diff --git a/test/matlab_experimental/ctTestTearDown.m b/test/matlab_experimental/ctTestTearDown.m index 2ef1b32ca2c..c8109191871 100644 --- a/test/matlab_experimental/ctTestTearDown.m +++ b/test/matlab_experimental/ctTestTearDown.m @@ -1,10 +1,10 @@ clear all -rootDir = fullfile(pwd); +cantera_root = getenv('CANTERA_ROOT'); ctName = '/test/matlab_experimental/libcantera_shared.so'; % Unload Cantera and remove temporary library file unloadlibrary('libcantera_shared'); -delete([rootDir, ctName]); +delete([cantera_root, ctName]); disp('Cantera has been unloaded'); diff --git a/test/matlab_experimental/ctTestThermo.m b/test/matlab_experimental/ctTestThermo.m index b03f36ead3b..2ee1d2bb838 100644 --- a/test/matlab_experimental/ctTestThermo.m +++ b/test/matlab_experimental/ctTestThermo.m @@ -36,6 +36,7 @@ function createPhase(self) function deleteSolution(self) clear self.phase; end + end methods