Skip to content

Commit

Permalink
correcting first order loss format
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Jan 19, 2024
1 parent c238872 commit dc129fe
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/full_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
{
"type": "FIRST_ORDER_LOSS",
"gas phase": "gas",
"products": [
"reactants": [
{
"species name": "C",
"coefficient": 1
Expand Down
3 changes: 2 additions & 1 deletion include/open_atmos/mechanism_configuration/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace open_atmos
DuplicatePhasesDetected,
PhaseRequiresUnknownSpecies,
ReactionRequiresUnknownSpecies,
UnknownPhase
UnknownPhase,
TooManyReactionComponents
};
std::string configParseStatusToString(const ConfigParseStatus &status);

Expand Down
2 changes: 1 addition & 1 deletion include/open_atmos/mechanism_configuration/validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ namespace open_atmos

struct FirstOrderLoss
{
const std::vector<std::string> required_keys{ keys.products, keys.type, keys.gas_phase };
const std::vector<std::string> required_keys{ keys.reactants, keys.type, keys.gas_phase };
const std::vector<std::string> optional_keys{ keys.name, keys.scaling_factor };
} first_order_loss;

Expand Down
4 changes: 2 additions & 2 deletions include/open_atmos/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ namespace open_atmos
{
/// @brief Scaling factor to apply to user-provided rate constants
double scaling_factor_{ 1.0 };
/// @brief A list of products
std::vector<ReactionComponent> products;
/// @brief A list of reactants
std::vector<ReactionComponent> reactants;
/// @brief An identifier, optional, uniqueness not enforced
std::string name;
/// @brief An identifier indicating which gas phase this reaction takes place in
Expand Down
20 changes: 13 additions & 7 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace open_atmos
case ConfigParseStatus::PhaseRequiresUnknownSpecies: return "PhaseRequiresUnknownSpecies";
case ConfigParseStatus::ReactionRequiresUnknownSpecies: return "ReactionRequiresUnknownSpecies";
case ConfigParseStatus::UnknownPhase: return "UnknownPhase";
case ConfigParseStatus::TooManyReactionComponents: return "TooManyReactionComponents";
default: return "Unknown";
}
}
Expand Down Expand Up @@ -952,16 +953,16 @@ namespace open_atmos
status = ValidateSchema(object, validation::first_order_loss.required_keys, validation::first_order_loss.optional_keys);
if (status == ConfigParseStatus::Success)
{
std::vector<types::ReactionComponent> products{};
for (const auto& reactant : object[validation::keys.products])
std::vector<types::ReactionComponent> reactants{};
for (const auto& reactant : object[validation::keys.reactants])
{
auto product_parse = ParseReactionComponent(reactant);
status = product_parse.first;
auto reactant_parse = ParseReactionComponent(reactant);
status = reactant_parse.first;
if (status != ConfigParseStatus::Success)
{
break;
}
products.push_back(product_parse.second);
reactants.push_back(reactant_parse.second);
}

if (object.contains(validation::keys.scaling_factor))
Expand All @@ -984,7 +985,7 @@ namespace open_atmos
}

std::vector<std::string> requested_species;
for (const auto& spec : products)
for (const auto& spec : reactants)
{
requested_species.push_back(spec.species_name);
}
Expand All @@ -1002,8 +1003,13 @@ namespace open_atmos
status = ConfigParseStatus::UnknownPhase;
}

if (status == ConfigParseStatus::Success && reactants.size() > 1)
{
status = ConfigParseStatus::TooManyReactionComponents;
}

first_order_loss.gas_phase = gas_phase;
first_order_loss.products = products;
first_order_loss.reactants = reactants;
first_order_loss.unknown_properties = unknown_properties;
}

Expand Down
1 change: 1 addition & 0 deletions test/integration/test_json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ TEST(JsonParser, ParsesFullConfiguration)
EXPECT_EQ(mechanism.reactions.surface.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis.size(), 1);
EXPECT_EQ(mechanism.reactions.emission.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss.size(), 1);
}

TEST(JsonParser, ParserReportsBadFiles)
Expand Down
19 changes: 13 additions & 6 deletions test/unit/test_parse_first_order_loss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ TEST(JsonParser, CanParseValidFirstOrderLossReaction)
EXPECT_EQ(mechanism.reactions.first_order_loss[0].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.first_order_loss[0].name, "my first order loss");
EXPECT_EQ(mechanism.reactions.first_order_loss[0].scaling_factor_, 12.3);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].products.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].products[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.first_order_loss[0].products[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].reactants[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.first_order_loss[0].reactants[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].unknown_properties["__comment"], "\"Strawberries are the superior fruit\"");

EXPECT_EQ(mechanism.reactions.first_order_loss[1].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.first_order_loss[1].scaling_factor_, 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[1].products.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[1].products[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.first_order_loss[1].products[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[1].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[1].reactants[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.first_order_loss[1].reactants[0].coefficient, 1);
}

TEST(JsonParser, FirstOrderLossDetectsUnknownSpecies)
Expand All @@ -47,4 +47,11 @@ TEST(JsonParser, FirstOrderLossDetectsUnknownPhase)
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/first_order_loss/missing_phase.json"));
EXPECT_EQ(status, ConfigParseStatus::UnknownPhase);
}

TEST(JsonParser, FirstOrderLossDetectsMoreThanOneSpecies)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/first_order_loss/too_many_reactants.json"));
EXPECT_EQ(status, ConfigParseStatus::TooManyReactionComponents);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"type": "FIRST_ORDER_LOSS",
"gas phase": "gas",
"products": [
"reactants": [
{
"Species name": "C"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
"type": "FIRST_ORDER_LOSS",
"gas phase": "gas",
"products": [
"reactants": [
{
"species name": "C"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "1.0.0",
"name": "Too many reactants",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
}
],
"phases": [
{
"name": "gas",
"species": [
"A",
"B",
"C"
]
}
],
"reactions": [
{
"type": "FIRST_ORDER_LOSS",
"gas phase": "gas",
"reactants": [
{
"species name": "C",
"coefficient": 1
},
{
"species name": "B",
"coefficient": 1
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{
"type": "FIRST_ORDER_LOSS",
"gas phase": "gas",
"products": [
"reactants": [
{
"species name": "C"
}
Expand Down
12 changes: 2 additions & 10 deletions test/unit/unit_configs/reactions/first_order_loss/valid.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@
"B",
"C"
]
},
{
"name": "surface reacting phase",
"species": [
"A",
"B",
"C"
]
}
],
"reactions": [
{
"type": "FIRST_ORDER_LOSS",
"gas phase": "gas",
"products": [
"reactants": [
{
"species name": "C",
"coefficient": 1
Expand All @@ -47,7 +39,7 @@
{
"type": "FIRST_ORDER_LOSS",
"gas phase": "gas",
"products": [
"reactants": [
{
"species name": "C",
"coefficient": 1
Expand Down

0 comments on commit dc129fe

Please sign in to comment.