Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matlab interface (new) test suite #1496

Merged
merged 19 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/main.yml
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,46 @@ concurrency:
cancel-in-progress: true

jobs:
matlab-test:
name: Run MATLAB Test Script
runs-on: ubuntu-latest
rwest marked this conversation as resolved.
Show resolved Hide resolved
env:
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
steps:
- uses: actions/checkout@v3
name: Checkout the repository
with:
submodules: recursive
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: x64
- name: Install Apt dependencies
run: |
sudo apt update
sudo apt install libboost-dev gfortran libopenmpi-dev libpython3-dev \
libblas-dev liblapack-dev libhdf5-dev
gcc --version
- name: Upgrade pip
run: python3 -m pip install -U pip setuptools wheel
- name: Install Python dependencies
run: |
python3 -m pip install ruamel.yaml scons==3.1.2
- name: Build Cantera
run: |
python3 `which scons` build env_vars=all -j2 debug=n --debug=time \
cc_flags=-D_GLIBCXX_ASSERTIONS
- name: Set up MATLAB
uses: matlab-actions/setup-matlab@v1
- name: Run tests
uses: matlab-actions/run-tests@v1
with:
select-by-folder: /home/runner/work/cantera/cantera/test/matlab_experimental

Comment on lines +59 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation for the run-tests action says this should be a path "relative to the project root folder", that is just test/matlab_experimental. Did you try that and have problems? I'm wondering if this is behind the warnings in the logs related to files that are outside this path and shouldn't be examined at all as part of this test suite, for example:

[Warning: "/home/runner/work/cantera/cantera/test/matlab/TestImport.m" was
  excluded.
  Caused by:
      The specified superclass 'TestCase' contains a parse error, cannot be found
      on MATLAB's search path, or is shadowed by another file with the same name.]

and

[Warning:
  "/home/runner/work/cantera/cantera/ext/HighFive/deps/catch2/projects/XCode/OCTest/OCTest/TestObj.m"
  was excluded.
  Caused by:
      File:
      /home/runner/work/cantera/cantera/ext/HighFive/deps/catch2/projects/XCode/OCTest/OCTest/TestObj.m
      Line: 1 Column: 1
      Invalid use of operator.]� 
  [> In matlab.unittest.internal/InvalidTestFactory/createSuiteImplicitly (line 49)


ubuntu-multiple-pythons:
name: ${{ matrix.os }} with Python ${{ matrix.python-version }}
runs-on: ${{ matrix.os }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include/cantera/base/system.h.gch
include/cantera/ext/
interfaces/matlab/ctpath.m
interfaces/matlab/Contents.m
interfaces/matlab_experimental/ctRoot.m
src/extensions/delegator.h
stage/
.sconsign.dblite
Expand Down
1 change: 0 additions & 1 deletion interfaces/matlab_experimental/Base/Solution.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
function delete(s)
% Delete :mat:class:`Solution` object.
ctFunc('soln_del', s.phaseID);
disp('Solution class object has been deleted');
end

end
Expand Down
162 changes: 162 additions & 0 deletions test/matlab_experimental/ctTestKinetics.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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 (Test)

function testCounts(self)
self.verifyEqual(self.phase.nReactions, 29);
self.verifyEqual(self.phase.nTotalSpecies, 10);
self.verifyEqual(self.phase.nPhases, 1);

self.assumeFail('Fails because Kinetics.reactionPhaseIndex is missing')
self.verifyEqual(self.phase.reactionPhaseIndex, 0);
end

function testIsReversible(self)
self.assumeFail('Fails because of incorrect reaction indexing');

for i = 1:self.phase.nReactions
self.verifyTrue(self.phase.isReversible(i));
end
% To do: test on a mechanism where not all reactions are
% reversible

end

function testMultipler(self)

f0 = self.phase.forwardRatesOfProgress;
r0 = self.phase.reverseRatesOfProgress;

self.phase.setMultiplier(1, 2.0);
self.phase.setMultiplier(7, 0.1);

f1 = self.phase.forwardRatesOfProgress;
r1 = self.phase.reverseRatesOfProgress;

self.verifyEqual(2 .* f0(1), f1(1), 'AbsTol', self.atol);
self.verifyEqual(0.1 .* f0(7), f1(7), 'AbsTol', self.atol);
self.verifyEqual(2 .* r0(1), r1(1), 'AbsTol', self.atol);
self.verifyEqual(0.1 .* 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);
self.verifyEqual(0.5 .* r0, r2, '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)
self.assumeFail(['Fails because StoichCoeffs methods do not', ...
' convert species names to species indices']);

nu_r = self.phase.reactantStoichCoeffs;
nu_p = self.phase.productStoichCoeffs;

function checkReactant(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
23 changes: 23 additions & 0 deletions test/matlab_experimental/ctTestPath.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
% ctTestPath.m
% Set up environment for testing the Cantera Matlab interface
% from within the Cantera source tree. Run this file from the
% root of the Cantera source tree, for example:
%
% cd ~/src/cantera
% run interfaces/matlab_experimental/testpath.m
ssun30 marked this conversation as resolved.
Show resolved Hide resolved

% get the list of directories on the Matlab path
dirs = split(path, pathsep);

% if 'cantera' is already in the path, remove it
for i = 1:length(dirs)
if contains(dirs{i}, 'CANTERA', 'IgnoreCase', true)
rmpath(dirs{i});
end
end

cantera_root = getenv('CANTERA_ROOT');

% Add the Cantera toolbox to the Matlab path
addpath(genpath([cantera_root, '/interfaces/matlab_experimental']));
addpath(genpath([cantera_root, '/test/matlab_experimental']));
27 changes: 27 additions & 0 deletions test/matlab_experimental/ctTestSetUp.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
clear all

% Copy library to test folder
ctTestPath;

% Load Cantera
rootDir = getenv('CANTERA_ROOT');

if ispc
ctName = '/build/lib/cantera_shared.dll';
elseif ismac
ctName = '/build/lib/libcantera_shared.dylib';
elseif isunix
ctName = '/build/lib/libcantera_shared.so';
end
% Load Cantera
if ~libisloaded('libcantera_shared')
[~, warnings] = loadlibrary([rootDir, ctName], ...
[rootDir, '/include/cantera/clib/ctmatlab.h'], ...
'includepath', [rootDir, '/include'], ...
'addheader', 'ct', 'addheader', 'ctfunc', ...
'addheader', 'ctmultiphase', 'addheader', ...
'ctonedim', 'addheader', 'ctreactor', ...
'addheader', 'ctrpath', 'addheader', 'ctsurf');
end

disp('Cantera is loaded for test');
4 changes: 4 additions & 0 deletions test/matlab_experimental/ctTestTearDown.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clear all
% Unload Cantera
unloadlibrary('libcantera_shared');
disp('Cantera has been unloaded');
Loading