Skip to content

Commit

Permalink
Fixed setter errors with unit conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
ssun30 committed Oct 4, 2023
1 parent 0f11f9c commit c6284bf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 58 deletions.
6 changes: 3 additions & 3 deletions interfaces/matlab_experimental/Base/Kinetics.m
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

reactionEquations % All reaction equations within the Kinetics object.

reactionPhaseIndex % The index of the phase where the reactions occur.
reactionPhaseIndex % The index of the phase where the reactions occur.
end

methods
Expand Down Expand Up @@ -342,7 +342,7 @@ function delete(kin)
for k = krange

for i = irange

if iscell(k)
kk = kin.kineticsSpeciesIndex(k{:});
else
Expand All @@ -363,7 +363,7 @@ function delete(kin)
n = nonzeros(temp);
elseif nnz(temp) == 0
n = 0;
else
else
n = temp;
end
end
Expand Down
38 changes: 33 additions & 5 deletions interfaces/matlab_experimental/Base/ThermoPhase.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
% String. Can be 'mole'/'molar'/'Molar'/'Mole' or 'mass'/'Mass'.
basis

phaseName % Name of the phase.
phaseName % Name of the phase.

electricPotential % Electric potential. Units: V.

Expand Down Expand Up @@ -176,8 +176,8 @@

refPressure % Reference pressure for standard-state. Units: Pa.

% Generate a report describing the thermodynamic state of this phase.
% To print the report to the terminal, simply call the phase object.
% Generate a report describing the thermodynamic state of this phase.
% To print the report to the terminal, simply call the phase object.
% The following two statements are equivalent ::
%
% >> phase
Expand Down Expand Up @@ -997,7 +997,7 @@ function display(tp)
aa = char(ones(1, buflen));
ptr = libpointer('cstring', aa);
[iok, bb] = calllib(ctLib, 'thermo_report', tp.tpID, buflen, ptr, 1);

if iok < 0
error(ctGetErr);
end
Expand Down Expand Up @@ -1400,6 +1400,9 @@ function display(tp)
function set.HP(tp, input)
h = input{1};
p = input{2};
if strcmp(tp.basis, 'molar')
h = h/tp.meanMolecularWeight;
end
ctFunc('thermo_set_HP', tp.tpID, [h, p]);
end

Expand Down Expand Up @@ -1438,6 +1441,10 @@ function display(tp)
function set.SH(tp, input)
s = input{1};
h = input{2};
if strcmp(tp.basis, 'molar')
s = s/tp.meanMolecularWeight;
h = h/tp.meanMolecularWeight;
end
ctFunc('thermo_set_SH', tp.tpID, [s, h]);
end

Expand All @@ -1454,6 +1461,9 @@ function display(tp)
function set.SP(tp, input)
s = input{1};
p = input{2};
if strcmp(tp.basis, 'molar')
s = s/tp.meanMolecularWeight;
end
ctFunc('thermo_set_SP', tp.tpID, [s, p]);
end

Expand All @@ -1470,6 +1480,9 @@ function display(tp)
function set.ST(tp, input)
s = input{1};
t = input{2};
if strcmp(tp.basis, 'molar')
s = s/tp.meanMolecularWeight;
end
ctFunc('thermo_set_ST', tp.tpID, [s, t]);
end

Expand All @@ -1486,6 +1499,9 @@ function display(tp)
function set.SV(tp, input)
s = input{1};
v = input{2};
if strcmp(tp.basis, 'molar')
s = s/tp.meanMolecularWeight;
end
ctFunc('thermo_set_SV', tp.tpID, [s, v]);
end

Expand All @@ -1496,7 +1512,7 @@ function display(tp)

function set.SVY(tp, input)
tp.Y = input{3};
tp.SP = input(1:2);
tp.SV = input(1:2);
end

function set.TD(tp, input)
Expand All @@ -1518,6 +1534,9 @@ function display(tp)
function set.TH(tp, input)
t = input{1};
h = input{2};
if strcmp(tp.basis, 'molar')
h = h/tp.meanMolecularWeight;
end
ctFunc('thermo_set_TH', tp.tpID, [t, h]);
end

Expand Down Expand Up @@ -1572,6 +1591,9 @@ function display(tp)
function set.UP(tp, input)
u = input{1};
p = input{2};
if strcmp(tp.basis, 'molar')
u = u/tp.meanMolecularWeight;
end
ctFunc('thermo_set_UP', tp.tpID, [u, p]);
end

Expand All @@ -1588,6 +1610,9 @@ function display(tp)
function set.UV(tp, input)
u = input{1};
v = input{2};
if strcmp(tp.basis, 'molar')
u = u/tp.meanMolecularWeight;
end
ctFunc('thermo_set_UV', tp.tpID, [u, v]);
end

Expand All @@ -1604,6 +1629,9 @@ function display(tp)
function set.VH(tp, input)
v = input{1};
h = input{2};
if strcmp(tp.basis, 'molar')
h = h/tp.meanMolecularWeight;
end
ctFunc('thermo_set_VH', tp.tpID, [v, h]);
end

Expand Down
2 changes: 1 addition & 1 deletion test/matlab_experimental/ctTestKinetics.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function testIsReversible(self)
for i = 1:self.phase.nReactions
self.verifyTrue(self.phase.isReversible(i));
end

diamond = Solution('diamond.yaml', 'diamond_100', 'none');
self.verifyFalse(diamond.isReversible(20));
clear diamond
Expand Down
82 changes: 33 additions & 49 deletions test/matlab_experimental/ctTestThermo.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function deleteSolution(self)

% Generic function to set invalid values to attribute and verify errors
function setInvalidValue(self, attr, val, errMessage)
try
try
self.phase.(attr) = val;
self.verifyFail;
catch ME
Expand All @@ -56,19 +56,19 @@ function setInvalidValue(self, attr, val, errMessage)
try
if nargin == 3
val = self.phase.(attr);
else
else
val = self.phase.(attr)(args{:});
end
catch ME
self.verifySubstring(ME.message, errMessage);
end
end
end

% Check state
function checkState(self, T, D, Y)
self.verifyEqual(self.phase.T, T, 'RelTol', self.rtol);
self.verifyEqual(self.phase.D, D, 'RelTol', self.rtol);
self.verifyEqual(self.phase.Y, Y, 'AbsTol', self.atol);
self.verifyEqual(self.phase.D, D, 'RelTol', self.rtol);
self.verifyEqual(self.phase.Y, Y, 'AbsTol', self.atol);
end

% Check multi properties
Expand All @@ -93,23 +93,23 @@ function checkGetters(self)

self.checkMultiProperties('HP');
self.checkMultiProperties('HPX');
self.checkMultiProperties('HPY');
self.checkMultiProperties('HPY');

self.checkMultiProperties('UV');
self.checkMultiProperties('UVX');
self.checkMultiProperties('UVY');

self.checkMultiProperties('SP');
self.checkMultiProperties('SPX');
self.checkMultiProperties('SPY');

self.checkMultiProperties('SV');
self.checkMultiProperties('SVX');
self.checkMultiProperties('SVY');
self.checkMultiProperties('SVY');

self.checkMultiProperties('DP');
self.checkMultiProperties('DPX');
self.checkMultiProperties('DPY');
self.checkMultiProperties('DPY');
end

% Check setter
Expand All @@ -133,47 +133,47 @@ function checkSetters(self, T1, D1, Y1)

self.phase.TDY = {T0, D0, Y0};
self.phase.UVY = {U1, V1, Y1};
self.checkState(T1, D1, Y1);
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.HPY = {H1, P1, Y1};
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.SPY = {S1, P1, Y1};
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.TPX = {T1, P1, X1};
self.checkState(T1, D1, Y1);
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.UVX = {U1, V1, X1};
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.HPX = {H1, P1, X1};
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.SPX = {S1, P1, X1};
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.SVX = {S1, V1, X1};
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.SVY = {S1, V1, Y1};
self.checkState(T1, D1, Y1);
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.DPX = {D1, P1, X1};
self.checkState(T1, D1, Y1);

self.phase.TDY = {T0, D0, Y0};
self.phase.DPY = {D1, P1, Y1};
self.checkState(T1, D1, Y1);
self.checkState(T1, D1, Y1);
end
end

Expand All @@ -186,7 +186,7 @@ function testBaseAttributes(self)
self.verifyGreaterThanOrEqual(self.phase.tpID, 0);
self.verifyMatches(self.phase.basis, 'molar');
self.phase.basis = 'mass';
self.verifyMatches(self.phase.basis, 'mass');
self.verifyMatches(self.phase.basis, 'mass');

self.verifyInstanceOf(self.phase.phaseName, 'char');
self.phase.phaseName = 'spam';
Expand Down Expand Up @@ -233,8 +233,8 @@ function testNAtoms(self)
self.verifyEqual(n2, n);
end

self.getInvalidValue('nAtoms', {'C', 'H2'}, 'outside valid range');
self.getInvalidValue('nAtoms', {'H', 'CH4'}, 'outside valid range');
self.getInvalidValue('nAtoms', {'C', 'H2'}, 'No such species');
self.getInvalidValue('nAtoms', {'H', 'CH4'}, 'No such element');
end

function testElementalMassFraction(self)
Expand All @@ -251,7 +251,7 @@ function testElementalMassFraction(self)
self.verifyEqual(Zh, exp2, 'AbsTol', self.atol);
self.verifyEqual(Zar, exp3, 'AbsTol', self.atol);

self.getInvalidValue('elementalMassFraction', {'C'}, 'outside valid range');
self.getInvalidValue('elementalMassFraction', {'C'}, 'No such element');
self.getInvalidValue('elementalMassFraction', {5}, 'Wrong type');
end

Expand Down Expand Up @@ -283,7 +283,7 @@ function testCharges(self)

flag = sum(ismember(chargePhase.speciesNames, species));
self.verifyGreaterThan(flag, 0);

idx = chargePhase.speciesIndex(species);
self.verifyEqual(charges(idx), charge);
end
Expand Down Expand Up @@ -322,16 +322,6 @@ function testSingleGetters(self)
(GasConstant * self.phase.T);
self.verifyEqual(val, exp, 'RelTol', self.rtol);

self.phase.basis = 'mass';
val = self.phase.V;
exp = 1/exp;
self.verifyEqual(val, exp, 'RelTol', self.rtol);

self.phase.basis = 'molar';
val = self.phase.V;
exp = exp * self.phase.meanMolecularWeight;
self.verifyEqual(val, exp, 'RelTol', self.rtol);

val = self.phase.molarDensity;
exp = self.phase.D/self.phase.meanMolecularWeight;
self.verifyEqual(val, exp, 'RelTol', self.rtol);
Expand Down Expand Up @@ -411,40 +401,34 @@ function testSetCompositionStringBad(self)
end

function testSetStateMole(self)
self.assumeFail(['Fails because multi-property setters could not', ...
' set correct values']);

self.checkSetters(750, 0.07, [0.2, 0.1, 0.0, 0.3, 0.1, ...
0.0, 0.0, 0.2, 0.1, 0.0]);
end

function testSetStateMass(self)
self.assumeFail(['Fails because multi-property setters could not', ...
' set correct values']);

self.phase.basis = 'mass';
self.checkSetters(500, 1.5, [0.1, 0.0, 0.0, 0.1, 0.4, ...
0.2, 0.0, 0.0, 0.2, 0.0]);
0.2, 0.0, 0.0, 0.2, 0.0]);
end

function testSetterErrors(self)
self.setInvalidValue('TD', {400}, 'not exceed');
self.setInvalidValue('TD', {400}, 'not exceed');
end

function testInvalidProperty(self)

function a = getInvalidProperty()
a = self.phase.foobar;
end

function setInvalidProperty(val)
self.phase.foobar = val;
end
end

self.verifyError(@() getInvalidProperty,...
'MATLAB:noSuchMethodOrField');
self.verifyError(@() setInvalidProperty(300),...
'MATLAB:noPublicFieldForClass');
'MATLAB:noPublicFieldForClass');
end

end
Expand Down

0 comments on commit c6284bf

Please sign in to comment.