Skip to content
Open
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
15 changes: 15 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,25 @@ jobs:
echo "LD_PRELOAD=$LIB_STDCXX:$LIB_OPENBLAS:$LIB_LAPACK" >> $GITHUB_ENV
- name: Set up MATLAB
uses: matlab-actions/setup-matlab@718d4320188c73c86eb94ce76b553cbf89778487 # v2.5.0
- name: Set MATLAB search paths for tests and build MLTBX toolbox # v2.2.1
uses: matlab-actions/run-command@67f012f1ee4bc1627a7a95a0ccdeec745c9f6f36
with:
command: |
toolboxPath = fullfile(getenv('CANTERA_ROOT'));
libPath = fullfile(toolboxPath, 'build', 'lib');
includePath = fullfile(toolboxPath, 'include');
addpath(fullfile(toolboxPath, 'interfaces', 'matlab_experimental', 'Utility'));
ctPaths(libPath, includePath, toolboxPath);
- name: Run tests
uses: matlab-actions/run-tests@f9fc3d8ca29fadef6227fa52884b144b9011fa2f # v2.1.1
with:
select-by-folder: /home/runner/work/cantera/cantera/test/matlab_experimental
- name: Run MATLAB test_examples
uses: matlab-actions/run-command@67f012f1ee4bc1627a7a95a0ccdeec745c9f6f36 # v2.2.1
with:
command: |
addpath('/home/runner/work/cantera/cantera/samples/matlab_experimental');
test_examples;

ubuntu-multiple-pythons:
name: ${{ matrix.os }} with Python ${{ matrix.python-version }}, Numpy ${{ matrix.numpy || 'latest' }}, Cython ${{ matrix.cython || 'latest' }}
Expand Down
6 changes: 6 additions & 0 deletions interfaces/matlab_experimental/Base/ThermoPhase.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
% Scalar double mean molecular weight. Units: kg/kmol.
meanMolecularWeight

massDensity % Mass basis density. Units: kg/m^3.

molarDensity % Molar basis density. Units: kmol/m^3.

molecularWeights % Molecular weights of the species. Units: kg/kmol.
Expand Down Expand Up @@ -903,6 +905,10 @@ function display(tp)
mmw = ctFunc('thermo_meanMolecularWeight', tp.tpID);
end

function density = get.massDensity(tp)
density = ctFunc('thermo_density', tp.tpID);
end

function density = get.molarDensity(tp)
density = ctFunc('thermo_molarDensity', tp.tpID);
end
Expand Down
9 changes: 2 additions & 7 deletions interfaces/matlab_experimental/OneDim/Sim1D.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,9 @@ function delete(s)

%% Sim1D Utility Methods

function display(s, fname)
function display(s)
% Show all domains.

if nargin == 1
fname = '-';
end

ctFunc('sim1D_show', s.stID, fname);
ctFunc('sim1D_show', s.stID);
end

function restore(s, fname, id)
Expand Down
41 changes: 24 additions & 17 deletions interfaces/matlab_experimental/Utility/ctLoad.m
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
function ctLoad()
% ctLoad
% Load the Cantera C Library into Memory

paths = ctPaths();

if any(cellfun(@isempty, {paths.libPath, paths.includePath, paths.toolboxPath}))
error('ctLoad:MissingPath', ...
'Library, header, and toolbox paths must be configured with ctPaths(libPath,includePath, toolboxPath).');
end

if ispc
ctName = '/bin/cantera_shared.dll';
libName = 'cantera_shared.dll';
elseif ismac
ctName = '/Lib/libcantera_shared.dylib';
libName = 'libcantera_shared.dylib';
elseif isunix
ctName = '/lib/libcantera_shared.so';
libName = 'libcantera_shared.so';
else
error('Operating System Not Supported!');
return;
error('ctLoad:UnsupportedPlatform', 'Operating system not supported.');
end

root = ctRoot;
if ispc
root = [ctRoot, '/Library'];
end
fullLibPath = fullfile(paths.libPath, libName);
fullHeaderPath = fullfile(paths.includePath, 'cantera', 'clib', 'ctmatlab.h');

if ~libisloaded(ctLib)
[~, warnings] = loadlibrary([root, ctName], ...
[root, '/include/cantera/clib/ctmatlab.h'], ...
'includepath', [root, '/include'], ...
'addheader', 'ct', 'addheader', 'ctfunc', ...
'addheader', 'ctmultiphase', 'addheader', ...
'ctonedim', 'addheader', 'ctreactor', ...
'addheader', 'ctrpath', 'addheader', 'ctsurf');
[~, warnings] = loadlibrary(fullLibPath, fullHeaderPath, ...
'includepath', paths.includePath, ...
'addheader', 'ct', ...
'addheader', 'ctfunc', ...
'addheader', 'ctmultiphase', ...
'addheader', 'ctonedim', ...
'addheader', 'ctreactor', ...
'addheader', 'ctrpath', ...
'addheader', 'ctsurf');
end

disp(sprintf('Cantera %s is ready for use.', ctVersion))
fprintf('Cantera %s is ready for use.\n', ctVersion);

end
73 changes: 73 additions & 0 deletions interfaces/matlab_experimental/Utility/ctPaths.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function paths = ctPaths(varargin)
% ctPaths ::
% Configure or retrieve the library/header/toolbox paths for Cantera.
% The paths are stored as MATLAB preferences.
%
% >> ctPaths() % Get current config as struct
% >> ctPaths(libPath, includePath) % Set library/header/toolbox paths
% >> ctPaths('clear') % Clear saved paths
%
% :return:
% paths: struct with fields 'libPath', 'includePath', and
% 'toolboxPath'

if nargin == 1 && strcmp(varargin{1}, 'clear')
if ispref('Cantera', 'Paths')
paths = getpref('Cantera', 'Paths');
subDirs = strsplit(genpath(paths.toolboxPath), pathsep);
currentPaths = strsplit(path, pathsep);
subdirsToRemove = intersect(subDirs, currentPaths);
if ~isempty(subdirsToRemove)
rmpath(subdirsToRemove{:});
end
rmpref('Cantera', 'Paths');
end
paths = struct('libPath', '', 'includePath', '', 'toolboxPath', '');
return
elseif nargin == 3 && all(cellfun(@ischar, varargin))
paths = struct('libPath', varargin{1}, ...
'includePath', varargin{2}, ...
'toolboxPath', varargin{3});
setpref('Cantera', 'Paths', paths);
else
% Load from saved preferences if available
if ispref('Cantera', 'Paths')
paths = getpref('Cantera', 'Paths');
return
else
paths = struct('libPath', '', 'includePath', '', 'toolboxPath', '');
end
end

if any(cellfun(@isempty, {paths.libPath, paths.includePath, paths.toolboxPath}))
error('ctPaths:MissingPath', ...
'Library, header, and toolbox paths must be configured with ctPaths(libPath,includePath, toolboxPath).');
end

mapping = {
'interfaces/matlab_experimental', 'toolbox';
'samples/matlab_experimental', 'samples';
'test/matlab_experimental', 'test/matlab_toolbox';
'test/data', 'test/data';
'data', 'data'
};

% Check whether user is using Cantera source code or MLTBX based on folder structure
if isfolder(fullfile(paths.toolboxPath, 'interfaces'))
col = 1;
else
col = 2;
end

for i = 1:size(mapping, 1)
subdir = fullfile(paths.toolboxPath, mapping{i, col});
if isfolder(subdir)
addpath(genpath(subdir));
else
warning('ctPaths:MissingDirectory', ...
'Directory not found: %s', subdir);
end
end

savepath();
end
2 changes: 1 addition & 1 deletion samples/matlab_experimental/conhp.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
% It assumes that the ``gas`` object represents a reacting ideal gas mixture.

% Set the state of the gas, based on the current solution vector.
gas.basis = 'mass';
gas.Y = y(2:end);
gas.TP = {y(1), gas.P};
nsp = gas.nSpecies;

% energy equation
wdot = gas.netProdRates;
H = gas.partialMolarEnthalpies';
gas.basis = 'mass';
tdot =- 1 / (gas.D * gas.cp) .* wdot * H;

% set up column vector for dydt
Expand Down
2 changes: 1 addition & 1 deletion samples/matlab_experimental/conuv.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
% It assumes that the ``gas`` object represents a reacting ideal gas mixture.

% Set the state of the gas, based on the current solution vector.
gas.basis = 'mass';
gas.Y = y(2:end);
gas.TD = {y(1), gas.D};
nsp = gas.nSpecies;

% energy equation
wdot = gas.netProdRates;
U = gas.partialMolarIntEnergies';
gas.basis = 'mass';
tdot =- 1 / (gas.D * gas.cv) .* wdot * U;

% set up column vector for dydt
Expand Down
61 changes: 61 additions & 0 deletions samples/matlab_experimental/crit_properites.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
%% Critical state properties
% Print the critical state properties for the fluids for which Cantera has
% built-in liquid/vapor equations of state.
%
% .. tags:: Matlab, thermodynamics, multiphase, non-ideal fluid

clear all;
close all;

tic
help crit_properites

%% Create a pure fluid object
fluids = struct('water', Water(), ...
'nitrogen', Nitrogen(), ...
'methane', Methane(), ...
'hydrogen', Hydrogen(), ...
'oxygen', Oxygen(), ...
'carbon_dioxide', CarbonDioxide(), ...
'heptane', Heptane(), ...
'HFC_134a', HFC134a());

names = fieldnames(fluids);

%% Plot critical properties and print tabulated values

figure;
hold on;
title('Critical Properties of Pure Fluids');
xlabel('Critical Temperature [K]');
ylabel('Critical Pressure [Pa]');
xlim([0 750]);
grid on;

% Print header
fprintf('Critical State Properties\n');
fprintf('%-16s %-7s %-10s %-7s\n', 'Fluid', 'Tc [K]', 'Pc [Pa]', 'Zc');
fprintf('%s %s %s %s\n', repmat('-',1,16), repmat('-',1,7), repmat('-',1,10), repmat('-',1,7));

%% Loop through fluids
for i = 1:length(names)
name = names{i};
f = fluids.(name);

Tc = f.critTemperature;
Pc = f.critPressure;
rhoc = f.critDensity;
mw = f.meanMolecularWeight;
R = GasConstant;

Zc = Pc * mw / (rhoc * R * Tc);

% Plot
plot(Tc, Pc, 'o');
text(Tc + 4, Pc + 2e5, strrep(name, '_', ' '), 'FontSize', 9);

% Print table row
fprintf('%-16s %7.2f %10.4g %7.4f\n', strrep(name, '_', ' '), Tc, Pc, Zc);
end

toc
2 changes: 1 addition & 1 deletion samples/matlab_experimental/diamond_cvd.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
r = surf_phase.netProdRates;
carbon_dot = r(iC);
mdot = mw * carbon_dot;
rate = mdot / dbulk.D;
rate = mdot / dbulk.massDensity;
xx = [xx; x(ih)];
rr = [rr; rate * 1.0e6 * 3600.0];
cov = [cov; surf_phase.coverages];
Expand Down
2 changes: 1 addition & 1 deletion samples/matlab_experimental/diff_flame.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
% ``help setRefineCriteria``.

f.energyEnabled = true;
fl.setRefineCriteria(2, 200.0, 0.1, 0.2);
fl.setRefineCriteria(2, 4, 0.2, 0.3, 0.04);
fl.solve(loglevel, refine_grid);

%% Show statistics of solution and elapsed time
Expand Down
Loading