Skip to content

Fix MATLAB interface samples #1911

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion interfaces/matlab_experimental/Base/Interface.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
end

function c = get.concentrations(s)
surfID = s.tr_id;
surfID = s.tpID;
nsp = s.nSpecies;
xx = zeros(1, nsp);
pt = libpointer('doublePtr', xx);
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 @@ -58,14 +58,9 @@ function delete(s)

%% Sim1D Utility Methods

function display(s, fname)
function display(s)
Copy link
Member

Choose a reason for hiding this comment

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

For the sake of consistency with other APIs, the name of the method should be show, see https://cantera.org/stable/python/onedim.html#cantera.Sim1D.show.

% 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
2 changes: 1 addition & 1 deletion interfaces/matlab_experimental/Reactor/ReactorSurface.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
name = '(none)';
end

s.surfID = ctFunc('reactor_new', 'ReactorSurface', surf.solnID, name);
s@Reactor(surf, 'ReactorSurface', name)
ctFunc('reactorsurface_install', s.id, reactor.id);
end

Expand Down
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
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
4 changes: 2 additions & 2 deletions samples/matlab_experimental/equil.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function equil(g)

nsp = gas.nSpecies;

% find methane, nitrogen, and oxygen indices
%% find methane, nitrogen, and oxygen indices
ich4 = gas.speciesIndex('CH4');
io2 = gas.speciesIndex('O2');
in2 = gas.speciesIndex('N2');
Expand All @@ -41,7 +41,7 @@ function equil(g)
xeq(:, i) = gas.X;
end

% make plots
%% make plots
clf;
subplot(1, 2, 1);
plot(phi, tad);
Expand Down
2 changes: 1 addition & 1 deletion samples/matlab_experimental/flame.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
f = Sim1D({left flow right});

% set default initial profiles.
rho0 = gas.D;
rho0 = gas.massDensity;

% find the adiabatic flame temperature and corresponding
% equilibrium composition
Expand Down
2 changes: 1 addition & 1 deletion samples/matlab_experimental/ignite.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
gas.TPX = {1001.0, OneAtm, 'H2:2,O2:1,N2:4'};
gas.basis = 'mass';
y0 = [gas.U
1.0 / gas.D
1.0 / gas.massDensity
gas.Y'];

time_interval = [0 0.001];
Expand Down
2 changes: 1 addition & 1 deletion samples/matlab_experimental/isentropic.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function isentropic(g)
%% Isentropic, adiabatic flow
%% Adiabatic, isentropic flow
%
% In this example, the area ratio vs. Mach number curve is computed for a
% hydrogen/nitrogen gas mixture.
Expand Down
35 changes: 22 additions & 13 deletions samples/matlab_experimental/periodic_cstr.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
tic
help periodic_cstr

% create the gas mixture
%% create the gas mixture
gas = Solution('h2o2.yaml', 'ohmech');

% pressure = 60 Torr, T = 770 K
Expand All @@ -39,26 +39,27 @@

gas.TPX = {300, p, 'H2:2, O2:1'};

% create an upstream reservoir that will supply the reactor. The
% temperature, pressure, and composition of the upstream reservoir are
%%
% Create an upstream reservoir that will supply the reactor.
% The temperature, pressure, and composition of the upstream reservoir are
% set to those of the 'gas' object at the time the reservoir is
% created.
upstream = Reservoir(gas);

%%
% Now set the gas to the initial temperature of the reactor, and create
% the reactor object.
gas.TP = {t, p};
cstr = IdealGasReactor(gas);

%%
% Set its volume to 10 cm^3. In this problem, the reactor volume is
% fixed, so the initial volume is the volume at all later times.
cstr.V = 10.0 * 1.0e-6;

%%
% We need to have heat loss to see the oscillations. Create a
% reservoir to represent the environment, and initialize its
% temperature to the reactor temperature.
env = Reservoir(gas);

%%
% Create a heat-conducting wall between the reactor and the
% environment. Set its area, and its overall heat transfer
% coefficient. Larger U causes the reactor to be closer to isothermal.
Expand All @@ -67,27 +68,27 @@
w = Wall(cstr, env);
w.area = 1.0;
w.heatTransferCoeff = 0.02;

%%
% Connect the upstream reservoir to the reactor with a mass flow
% controller (constant mdot). Set the mass flow rate to 1.25 sccm.
sccm = 1.25;
vdot = sccm * 1.0e-6/60.0 * ((OneAtm / gas.P) * (gas.T / 273.15)); % m^3/s
mdot = gas.D * vdot; % kg/s
mdot = gas.massDensity * vdot; % kg/s
mfc = MassFlowController(upstream, cstr);
mfc.massFlowRate = mdot;

%%
% now create a downstream reservoir to exhaust into.
downstream = Reservoir(gas);

%%
% connect the reactor to the downstream reservoir with a valve, and
% set the coefficient sufficiently large to keep the reactor pressure
% close to the downstream pressure of 60 Torr.
v = Valve(cstr, downstream);
v.valveCoeff = 1.0e-9;

%%
% create the network
network = ReactorNet({cstr});

%%
% now integrate in time
tme = 0.0;
dt = 0.1;
Expand All @@ -104,11 +105,19 @@
y(3, n) = cstr.massFraction('H2O');
end

for i = 2:length(tm)
if abs(y(3, i-1) - y(3, i)) > (y(3, i-1) * 5)
disp(tm(i))
end
end
%% plot
clf
figure(1)
plot(tm, y)
legend('H2', 'O2', 'H2O')
title('Mass Fractions')
ylabel('Mass Fractions')
xlabel('Time (s)')

toc
end
2 changes: 1 addition & 1 deletion samples/matlab_experimental/plug_flow_reactor.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@

T_calc(1) = gas_calc.T;
Y_calc(1, :) = gas_calc.Y;
rho_calc(1) = gas_calc.D;
rho_calc(1) = gas_calc.massDensity;

for i = 2:length(x_calc)

Expand Down
2 changes: 1 addition & 1 deletion samples/matlab_experimental/prandtl1.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function prandtl1(g)

disp(['CPU time = ' num2str(cputime - t0)]);

% plot results
%% plot results

clf;
%figure(1);
Expand Down
2 changes: 1 addition & 1 deletion samples/matlab_experimental/prandtl2.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function prandtl2(g)

disp(['CPU time = ' num2str(cputime - t0)]);

% plot results
%% plot results

clf;
subplot(2, 2, 1);
Expand Down
10 changes: 7 additions & 3 deletions samples/matlab_experimental/rankine.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,39 @@

help rankine

% Initialize parameters
%% Initialize parameters
eta_pump = 0.6;
eta_turbine = 0.8;
p_max = 8.0 * OneAtm;
t1 = 300.0;

% create an object representing water
%% create an object representing water
w = Water;

%%
% start with saturated liquid water at t1
basis = 'mass';
w.TQ = {t1, 0};
h1 = w.H;
p1 = w.P;

%%
% pump it to p2
pump_work = pump(w, p_max, eta_pump);
h2 = w.H;

%%
% heat to saturated vapor
w.PQ = {p_max, 1.0};
h3 = w.H;

heat_added = h3 - h2;

%%
% expand adiabatically back to the initial pressure
turbine_work = expand(w, p1, eta_turbine);

% compute the efficiency
%% compute the efficiency
efficiency = (turbine_work - pump_work) / heat_added;
disp(sprintf('efficiency = %.2f%%', efficiency*100));

Expand Down
11 changes: 7 additions & 4 deletions samples/matlab_experimental/reactor1.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function reactor1(g)
end

P = 101325.0;
% set the initial conditions
%% set the initial conditions
gas.TP = {1001.0, P};
nsp = gas.nSpecies;
xx = zeros(nsp, 1);
Expand All @@ -29,26 +29,29 @@ function reactor1(g)
xx(48) = 0.573;
gas.X = xx;

% create a reactor, and insert the gas
%% create a reactor, and insert the gas
r = IdealGasReactor(gas);

% create a reservoir to represent the environment
%% create a reservoir to represent the environment
a = Solution('air.yaml', 'air', 'none');
a.TP = {a.T, P};
env = Reservoir(a);

%%
% Define a wall between the reactor and the environment and
% make it flexible, so that the pressure in the reactor is held
% at the environment pressure.
w = Wall(r, env);

%%
% set expansion parameter. dV/dt = KA(P_1 - P_2)
w.expansionRateCoeff = 1.0e6;

%%
% set wall area
w.area = 1.0;

% create a reactor network and insert the reactor:
%% create a reactor network and insert the reactor:
network = ReactorNet({r});

nSteps = 100;
Expand Down
6 changes: 3 additions & 3 deletions samples/matlab_experimental/reactor2.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ function reactor2(g)
gas = Solution('gri30.yaml', 'gri30', 'none');
end

% set the initial conditions
%% set the initial conditions
gas.TPX = {1001.0, OneAtm, 'H2:2,O2:1,N2:4'};

% create a reactor, and insert the gas
%% create a reactor, and insert the gas
r = IdealGasReactor(gas);

% create a reactor network and insert the reactor
%% create a reactor network and insert the reactor
network = ReactorNet({r});

nSteps = 100;
Expand Down
Loading