From dc129fe75af306c7efdbf12e84edb8424beac857 Mon Sep 17 00:00:00 2001 From: Kyle Shores Date: Fri, 19 Jan 2024 11:05:57 -0600 Subject: [PATCH] correcting first order loss format --- examples/full_configuration.json | 2 +- .../mechanism_configuration/parser.hpp | 3 +- .../mechanism_configuration/validation.hpp | 2 +- include/open_atmos/types.hpp | 4 +- src/parser.cpp | 20 +++++---- test/integration/test_json_parser.cpp | 1 + test/unit/test_parse_first_order_loss.cpp | 19 ++++++--- .../bad_reaction_component.json | 2 +- .../first_order_loss/missing_phase.json | 2 +- .../first_order_loss/too_many_reactants.json | 41 +++++++++++++++++++ .../first_order_loss/unknown_species.json | 2 +- .../reactions/first_order_loss/valid.json | 12 +----- 12 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 test/unit/unit_configs/reactions/first_order_loss/too_many_reactants.json diff --git a/examples/full_configuration.json b/examples/full_configuration.json index 6547b4f..ffe9789 100644 --- a/examples/full_configuration.json +++ b/examples/full_configuration.json @@ -205,7 +205,7 @@ { "type": "FIRST_ORDER_LOSS", "gas phase": "gas", - "products": [ + "reactants": [ { "species name": "C", "coefficient": 1 diff --git a/include/open_atmos/mechanism_configuration/parser.hpp b/include/open_atmos/mechanism_configuration/parser.hpp index 5c217dc..53fd893 100644 --- a/include/open_atmos/mechanism_configuration/parser.hpp +++ b/include/open_atmos/mechanism_configuration/parser.hpp @@ -33,7 +33,8 @@ namespace open_atmos DuplicatePhasesDetected, PhaseRequiresUnknownSpecies, ReactionRequiresUnknownSpecies, - UnknownPhase + UnknownPhase, + TooManyReactionComponents }; std::string configParseStatusToString(const ConfigParseStatus &status); diff --git a/include/open_atmos/mechanism_configuration/validation.hpp b/include/open_atmos/mechanism_configuration/validation.hpp index 0b0f7d2..4a76662 100644 --- a/include/open_atmos/mechanism_configuration/validation.hpp +++ b/include/open_atmos/mechanism_configuration/validation.hpp @@ -175,7 +175,7 @@ namespace open_atmos struct FirstOrderLoss { - const std::vector required_keys{ keys.products, keys.type, keys.gas_phase }; + const std::vector required_keys{ keys.reactants, keys.type, keys.gas_phase }; const std::vector optional_keys{ keys.name, keys.scaling_factor }; } first_order_loss; diff --git a/include/open_atmos/types.hpp b/include/open_atmos/types.hpp index d059e52..78bebae 100644 --- a/include/open_atmos/types.hpp +++ b/include/open_atmos/types.hpp @@ -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 products; + /// @brief A list of reactants + std::vector reactants; /// @brief An identifier, optional, uniqueness not enforced std::string name; /// @brief An identifier indicating which gas phase this reaction takes place in diff --git a/src/parser.cpp b/src/parser.cpp index b99a001..b9e4108 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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"; } } @@ -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 products{}; - for (const auto& reactant : object[validation::keys.products]) + std::vector 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)) @@ -984,7 +985,7 @@ namespace open_atmos } std::vector requested_species; - for (const auto& spec : products) + for (const auto& spec : reactants) { requested_species.push_back(spec.species_name); } @@ -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; } diff --git a/test/integration/test_json_parser.cpp b/test/integration/test_json_parser.cpp index 40cb8b9..26ef446 100644 --- a/test/integration/test_json_parser.cpp +++ b/test/integration/test_json_parser.cpp @@ -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) diff --git a/test/unit/test_parse_first_order_loss.cpp b/test/unit/test_parse_first_order_loss.cpp index fcf1c57..30de586 100644 --- a/test/unit/test_parse_first_order_loss.cpp +++ b/test/unit/test_parse_first_order_loss.cpp @@ -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) @@ -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); } \ No newline at end of file diff --git a/test/unit/unit_configs/reactions/first_order_loss/bad_reaction_component.json b/test/unit/unit_configs/reactions/first_order_loss/bad_reaction_component.json index 67fb7cb..53c1934 100644 --- a/test/unit/unit_configs/reactions/first_order_loss/bad_reaction_component.json +++ b/test/unit/unit_configs/reactions/first_order_loss/bad_reaction_component.json @@ -22,7 +22,7 @@ { "type": "FIRST_ORDER_LOSS", "gas phase": "gas", - "products": [ + "reactants": [ { "Species name": "C" } diff --git a/test/unit/unit_configs/reactions/first_order_loss/missing_phase.json b/test/unit/unit_configs/reactions/first_order_loss/missing_phase.json index 2c0ee45..3ea1954 100644 --- a/test/unit/unit_configs/reactions/first_order_loss/missing_phase.json +++ b/test/unit/unit_configs/reactions/first_order_loss/missing_phase.json @@ -17,7 +17,7 @@ { "type": "FIRST_ORDER_LOSS", "gas phase": "gas", - "products": [ + "reactants": [ { "species name": "C" } diff --git a/test/unit/unit_configs/reactions/first_order_loss/too_many_reactants.json b/test/unit/unit_configs/reactions/first_order_loss/too_many_reactants.json new file mode 100644 index 0000000..a3ff66f --- /dev/null +++ b/test/unit/unit_configs/reactions/first_order_loss/too_many_reactants.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/test/unit/unit_configs/reactions/first_order_loss/unknown_species.json b/test/unit/unit_configs/reactions/first_order_loss/unknown_species.json index 6116201..8fe2187 100644 --- a/test/unit/unit_configs/reactions/first_order_loss/unknown_species.json +++ b/test/unit/unit_configs/reactions/first_order_loss/unknown_species.json @@ -22,7 +22,7 @@ { "type": "FIRST_ORDER_LOSS", "gas phase": "gas", - "products": [ + "reactants": [ { "species name": "C" } diff --git a/test/unit/unit_configs/reactions/first_order_loss/valid.json b/test/unit/unit_configs/reactions/first_order_loss/valid.json index 7a610e5..e4e2b92 100644 --- a/test/unit/unit_configs/reactions/first_order_loss/valid.json +++ b/test/unit/unit_configs/reactions/first_order_loss/valid.json @@ -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 @@ -47,7 +39,7 @@ { "type": "FIRST_ORDER_LOSS", "gas phase": "gas", - "products": [ + "reactants": [ { "species name": "C", "coefficient": 1