Added error handling for out of bounds indices

This commit is contained in:
ssun30 2023-09-26 15:40:55 -04:00 committed by Ray Speth
parent cc54703c5b
commit bb7c01a836
3 changed files with 23 additions and 14 deletions

View File

@ -614,7 +614,7 @@ classdef ThermoPhase < handle
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;

View File

@ -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);
}
@ -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);
}
@ -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);
}

View File

@ -217,9 +217,6 @@ classdef ctTestThermo < matlab.unittest.TestCase
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'}};
@ -236,14 +233,11 @@ classdef ctTestThermo < matlab.unittest.TestCase
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');
@ -257,8 +251,8 @@ classdef ctTestThermo < matlab.unittest.TestCase
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)