Skip to content

Commit

Permalink
Added error handling for out of bounds indices
Browse files Browse the repository at this point in the history
  • Loading branch information
ssun30 committed Oct 3, 2023
1 parent a43191b commit 05d6aac
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion interfaces/matlab_experimental/Base/ThermoPhase.m
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ function display(tp)
end

if ~ischar(element)
error('Element name must be of format character.');
error('Wrong type for element name: must be character.');
end

n = tp.nSpecies;
Expand Down
21 changes: 18 additions & 3 deletions src/clib/ct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ extern "C" {
size_t thermo_elementIndex(int n, const char* nm)
{
try {
return ThermoCabinet::item(n).elementIndex(nm);
size_t k = ThermoCabinet::item(n).elementIndex(nm);
if (k == npos) {
throw CanteraError("thermo_elementIndex",
"No such element {}.", nm);
}
return k;
} catch (...) {
return handleAllExceptions(npos, npos);
}
Expand All @@ -312,7 +317,12 @@ extern "C" {
size_t thermo_speciesIndex(int n, const char* nm)
{
try {
return ThermoCabinet::item(n).speciesIndex(nm);
size_t k = ThermoCabinet::item(n).speciesIndex(nm);
if (k == npos) {
throw CanteraError("thermo_speciesIndex",
"No such species {}.", nm);
}
return k;
} catch (...) {
return handleAllExceptions(npos, npos);
}
Expand Down Expand Up @@ -1138,7 +1148,12 @@ extern "C" {
size_t kin_phaseIndex(int n, const char* ph)
{
try {
return KineticsCabinet::item(n).phaseIndex(ph);
size_t k = KineticsCabinet::item(n).phaseIndex(ph);
if (k == npos) {
throw CanteraError("kin_phaseIndex",
"No such phase {}.", ph);
}
return k;
} catch (...) {
return handleAllExceptions(npos, npos);
}
Expand Down
14 changes: 4 additions & 10 deletions test/matlab_experimental/ctTestThermo.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ function testElements(self)
end

function testNAtoms(self)
self.assumeFail(['Fails because error messages for nAtoms', ...
' are incorrect']);

data = {{1, 'O', 'O'}, {2, 'O', 'O2'}, {1, 'H', 'OH'},...
{2, 'H', 'H2O'}, {2, 'O', 'H2O2'}, {1, 'Ar', 'AR'},...
{0, 'O', 'H'}, {0, 'H', 'AR'}, {0, 'Ar', 'HO2'}};
Expand All @@ -236,14 +233,11 @@ function testNAtoms(self)
self.verifyEqual(n2, n);
end

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

function testElementalMassFraction(self)
self.assumeFail(['Fails because error messages for', ...
' elementalMassFraction are incorrect']);

self.phase.Y = 'H2O:0.5, O2:0.5';
Zo = self.phase.elementalMassFraction('O');
Zh = self.phase.elementalMassFraction('H');
Expand All @@ -257,8 +251,8 @@ function testElementalMassFraction(self)
self.verifyEqual(Zh, exp2, 'AbsTol', self.atol);
self.verifyEqual(Zar, exp3, 'AbsTol', self.atol);

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

function testWeights(self)
Expand Down

0 comments on commit 05d6aac

Please sign in to comment.