Skip to content

Commit

Permalink
[Input] Improve behavior when kinetics / reactions are unspecified
Browse files Browse the repository at this point in the history
Including a 'reactions' field without a 'kinetics' field in the phase definition
is an error.

Including a 'kinetics' field without a 'reactions' field is allowed only if a
'reactions' section exists. To specify a kinetics model with no reactions, the
'reactions' field must be set to 'none'.
  • Loading branch information
speth committed Jan 2, 2020
1 parent 237936c commit f6f52c3
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 11 deletions.
40 changes: 29 additions & 11 deletions src/kinetics/KineticsFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ unique_ptr<Kinetics> newKinetics(vector<ThermoPhase*>& phases,
kin->addPhase(*phase);
}
kin->init();
if (kin->kineticsType() != "Kinetics") {
addReactions(*kin, phaseNode, rootNode);
}
addReactions(*kin, phaseNode, rootNode);
return kin;
}

Expand Down Expand Up @@ -103,12 +101,23 @@ void addReactions(Kinetics& kin, const AnyMap& phaseNode, const AnyMap& rootNode
vector<string> sections, rules;

if (phaseNode.hasKey("reactions")) {
if (kin.kineticsType() == "Kinetics") {
throw InputFileError("addReactions", phaseNode["reactions"],
"Phase entry includes a 'reactions' field but does not "
"specify a kinetics model.");
}
const auto& reactionsNode = phaseNode.at("reactions");
if (reactionsNode.is<string>()) {
// Specification of the rule for adding species from the default
// 'reactions' section
sections.push_back("reactions");
rules.push_back(reactionsNode.asString());
if (rootNode.hasKey("reactions")) {
// Specification of the rule for adding species from the default
// 'reactions' section, if it exists
sections.push_back("reactions");
rules.push_back(reactionsNode.asString());
} else {
throw InputFileError("addReactions", reactionsNode,
"Phase entry implies existence of 'reactions' section "
"which does not exist in the current input file.");
}
} else if (reactionsNode.is<vector<string>>()) {
// List of sections from which all species should be added
for (const auto& item : reactionsNode.as<vector<string>>()) {
Expand All @@ -123,10 +132,19 @@ void addReactions(Kinetics& kin, const AnyMap& phaseNode, const AnyMap& rootNode
rules.push_back(item.begin()->second.asString());
}
}
} else {
// Default behavior is to add all reactions from the 'reactions' section
sections.push_back("reactions");
rules.push_back("all");
} else if (kin.kineticsType() != "Kinetics") {
if (rootNode.hasKey("reactions")) {
// Default behavior is to add all reactions from the 'reactions'
// section, if a 'kinetics' model has been specified
sections.push_back("reactions");
rules.push_back("all");
} else {
throw InputFileError("addReactions", phaseNode,
"Phase entry implies existence of 'reactions' section which "
"does not exist in the current input file. Add the field "
"'reactions: none' to the phase entry to specify a kinetics "
"model with no reactions.");
}
}

// Add reactions from each section
Expand Down
24 changes: 24 additions & 0 deletions test/data/phase-reaction-spec1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
phases:
# should work fine
- name: nokinetics-noreactions
species: [{gri30.yaml/species: [O2, H2, H2O]}]
thermo: ideal-gas

# should be an error
- name: kinetics-no-reaction-section1
species: [{gri30.yaml/species: [O2, H2, H2O]}]
thermo: ideal-gas
kinetics: gas

# should be an error
- name: kinetics-no-reaction-section2
species: [{gri30.yaml/species: [O2, H2, H2O]}]
thermo: ideal-gas
kinetics: gas
reactions: all

# should be an error
- name: nokinetics-reactions
species: [{gri30.yaml/species: [O2, H2, H2O]}]
thermo: ideal-gas
reactions: all
21 changes: 21 additions & 0 deletions test/data/phase-reaction-spec2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
phases:
# should work fine (no reactions)
- name: nokinetics-noreactions
species: [{gri30.yaml/species: [O2, H2, H2O]}]
thermo: ideal-gas

# should also work fine (all reactions)
- name: kinetics-noreactions
species: [{gri30.yaml/species: [O2, H2, H2O]}]
thermo: ideal-gas
kinetics: gas

# should be an error
- name: nokinetics-reactions
species: [{gri30.yaml/species: [O2, H2, H2O]}]
thermo: ideal-gas
reactions: all

reactions:
- equation: 2 H2 + O2 => 2 H2O
rate-constant: {A: 1.4e11, b: 0.5, Ea: 2400 cal/mol}
52 changes: 52 additions & 0 deletions test/kinetics/kineticsFromYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,55 @@ TEST(Kinetics, ElectrochemFromYaml)
EXPECT_NEAR(ropf[0], 0.279762338, 1e-8);
EXPECT_NEAR(ropr[0], 0.045559670, 1e-8);
}

TEST(KineticsFromYaml, NoKineticsModelOrReactionsField1)
{
auto soln = newSolution("phase-reaction-spec1.yaml",
"nokinetics-noreactions");
EXPECT_EQ(soln->kinetics()->kineticsType(), "Kinetics");
EXPECT_EQ(soln->kinetics()->nReactions(), (size_t) 0);
}

TEST(KineticsFromYaml, NoKineticsModelOrReactionsField2)
{
auto soln = newSolution("phase-reaction-spec2.yaml",
"nokinetics-noreactions");
EXPECT_EQ(soln->kinetics()->kineticsType(), "Kinetics");
EXPECT_EQ(soln->kinetics()->nReactions(), (size_t) 0);
}

TEST(KineticsFromYaml, KineticsModelWithoutReactionsSection1)
{
EXPECT_THROW(newSolution("phase-reaction-spec1.yaml",
"kinetics-no-reaction-section1"),
InputFileError);
}

TEST(KineticsFromYaml, KineticsModelWithoutReactionsSection2)
{
EXPECT_THROW(newSolution("phase-reaction-spec1.yaml",
"kinetics-no-reaction-section2"),
InputFileError);
}

TEST(KineticsFromYaml, KineticsModelWithoutReactionsField)
{
auto soln = newSolution("phase-reaction-spec2.yaml",
"kinetics-noreactions");
EXPECT_EQ(soln->kinetics()->kineticsType(), "Gas");
EXPECT_EQ(soln->kinetics()->nReactions(), (size_t) 1);
}

TEST(KineticsFromYaml, ReactionsFieldWithoutKineticsModel1)
{
EXPECT_THROW(newSolution("phase-reaction-spec1.yaml",
"nokinetics-reactions"),
InputFileError);
}

TEST(KineticsFromYaml, ReactionsFieldWithoutKineticsModel2)
{
EXPECT_THROW(newSolution("phase-reaction-spec2.yaml",
"nokinetics-reactions"),
InputFileError);
}

0 comments on commit f6f52c3

Please sign in to comment.