From c7ad0610a5f541beca1fbdd640f209c6fe8c55da Mon Sep 17 00:00:00 2001 From: Kyle Shores Date: Fri, 12 Jan 2024 09:52:46 -0600 Subject: [PATCH] parsing species --- examples/full_configuration.json | 14 +++++++-- .../mechanism_configuration/parser.hpp | 4 --- src/parser.cpp | 29 ++++++++++--------- test/unit/test_parse_species.cpp | 16 ++++++++++ test/unit/unit_configs/duplicate_species.json | 2 +- test/unit/unit_configs/invalid_key.json | 13 +++++++++ .../unit_configs/missing_required_key.json | 12 ++++++++ test/unit/unit_configs/valid_species.json | 2 +- 8 files changed, 69 insertions(+), 23 deletions(-) create mode 100644 test/unit/unit_configs/invalid_key.json create mode 100644 test/unit/unit_configs/missing_required_key.json diff --git a/examples/full_configuration.json b/examples/full_configuration.json index 6635e4c..0beb0ec 100644 --- a/examples/full_configuration.json +++ b/examples/full_configuration.json @@ -4,16 +4,20 @@ "species": [ { "name": "A", - "__absolute tolerance": 1.0e-30 + "__absolute tolerance": 1.0e-30, + "phase": "gas" }, { - "name": "B" + "name": "B", + "phase": "gas" }, { - "name": "C" + "name": "C", + "phase": "gas" }, { "name": "H2O2", + "phase": "gas", "HLC(298K) [mol m-3 Pa-1]": 1.011596348, "HLC exponential factor [K]": 6340, "diffusion coefficient [m2 s-1]": 1.46E-05, @@ -24,6 +28,7 @@ }, { "name": "ethanol", + "phase": "gas", "diffusion coefficient [m2 s-1]": 0.95E-05, "N star": 2.55, "molecular weight [kg mol-1]": 0.04607, @@ -31,18 +36,21 @@ }, { "name": "ethanol_aq", + "phase": "gas", "molecular weight [kg mol-1]": 0.04607, "density [kg m-3]": 1000.0, "__absolute tolerance": 1.0e-20 }, { "name": "H2O2_aq", + "phase": "gas", "molecular weight [kg mol-1]": 0.0340147, "density [kg m-3]": 1000.0, "__absolute tolerance": 1.0e-10 }, { "name": "H2O_aq", + "phase": "gas", "density [kg m-3]": 1000.0, "molecular weight [kg mol-1]": 0.01801 }, diff --git a/include/open_atmos/mechanism_configuration/parser.hpp b/include/open_atmos/mechanism_configuration/parser.hpp index 8f6ab7b..3cd6aa1 100644 --- a/include/open_atmos/mechanism_configuration/parser.hpp +++ b/include/open_atmos/mechanism_configuration/parser.hpp @@ -25,12 +25,8 @@ namespace open_atmos InvalidKey, UnknownKey, InvalidFilePath, - NoConfigFilesFound, - DataSectionNotFound, - InvalidSpecies, ObjectTypeNotFound, RequiredKeyNotFound, - ContainsNonStandardKey, MutuallyExclusiveOption, InvalidVersion, DuplicateSpeciesDetected diff --git a/src/parser.cpp b/src/parser.cpp index fe390cd..a6a524f 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -21,11 +21,8 @@ namespace open_atmos case ConfigParseStatus::InvalidKey: return "InvalidKey"; case ConfigParseStatus::UnknownKey: return "UnknownKey"; case ConfigParseStatus::InvalidFilePath: return "InvalidFilePath"; - case ConfigParseStatus::NoConfigFilesFound: return "NoConfigFilesFound"; - case ConfigParseStatus::InvalidSpecies: return "InvalidSpecies"; case ConfigParseStatus::ObjectTypeNotFound: return "ObjectTypeNotFound"; case ConfigParseStatus::RequiredKeyNotFound: return "RequiredKeyNotFound"; - case ConfigParseStatus::ContainsNonStandardKey: return "ContainsNonStandardKey"; case ConfigParseStatus::MutuallyExclusiveOption: return "MutuallyExclusiveOption"; case ConfigParseStatus::DuplicateSpeciesDetected: return "DuplicateSpeciesDetected"; default: return "Unknown"; @@ -136,7 +133,7 @@ namespace open_atmos { std::cerr << "Non-standard key '" << key << "' found in object" << object << std::endl; - return ConfigParseStatus::ContainsNonStandardKey; + return ConfigParseStatus::InvalidKey; } } return ConfigParseStatus::Success; @@ -150,14 +147,12 @@ namespace open_atmos for (const auto& object : objects) { types::Species species; - auto status = ValidateSchema(object, validation::species.required_keys, validation::species.optional_keys); + status = ValidateSchema(object, validation::species.required_keys, validation::species.optional_keys); if (status != ConfigParseStatus::Success) { break; } - // std::cout << "object:\n" << object.dump(4) << std::endl; - std::string name = object[validation::keys.name].get(); std::string phase = object[validation::keys.phase].get(); @@ -167,11 +162,13 @@ namespace open_atmos { if (object.contains(key)) { - if (key == validation::keys.tracer_type) { + if (key == validation::keys.tracer_type) + { std::string val = object[key].get(); string_properties[key] = val; } - else { + else + { double val = object[key].get(); numerical_properties[key] = val; } @@ -197,14 +194,18 @@ namespace open_atmos } // check for duplicate species - for(size_t i = 0; i < all_species.size(); ++i) { - for(size_t j = i+1; j < all_species.size(); ++j) { - if (all_species[i].name == all_species[j].name) { + for (size_t i = 0; i < all_species.size(); ++i) + { + for (size_t j = i + 1; j < all_species.size(); ++j) + { + if (all_species[i].name == all_species[j].name) + { status = ConfigParseStatus::DuplicateSpeciesDetected; } break; } - if (status != ConfigParseStatus::Success) break; + if (status != ConfigParseStatus::Success) + break; } return { status, all_species }; @@ -274,4 +275,4 @@ namespace open_atmos return { status, mechanism }; } } // namespace mechanism_configuration -} // namespace open_atmos \ No newline at end of file +} // namespace open_atmos diff --git a/test/unit/test_parse_species.cpp b/test/unit/test_parse_species.cpp index c814b68..4ad0d30 100644 --- a/test/unit/test_parse_species.cpp +++ b/test/unit/test_parse_species.cpp @@ -46,4 +46,20 @@ TEST(JsonParser, DetectsDuplicateSpecies) auto [status, mechanism] = parser.Parse(std::string("unit_configs/duplicate_species.json")); EXPECT_EQ(status, ConfigParseStatus::DuplicateSpeciesDetected); +} + +TEST(JsonParser, DetectsMissingRequiredKeys) +{ + JsonParser parser; + auto [status, mechanism] = parser.Parse(std::string("unit_configs/missing_required_key.json")); + + EXPECT_EQ(status, ConfigParseStatus::RequiredKeyNotFound); +} + +TEST(JsonParser, DetectsInvalidKeys) +{ + JsonParser parser; + auto [status, mechanism] = parser.Parse(std::string("unit_configs/invalid_key.json")); + + EXPECT_EQ(status, ConfigParseStatus::InvalidKey); } \ No newline at end of file diff --git a/test/unit/unit_configs/duplicate_species.json b/test/unit/unit_configs/duplicate_species.json index f65630e..2a254d4 100644 --- a/test/unit/unit_configs/duplicate_species.json +++ b/test/unit/unit_configs/duplicate_species.json @@ -1,6 +1,6 @@ { "version": "1.0.0", - "name": "Full Configuration", + "name": "Duplicate Species", "species": [ { "name": "A", diff --git a/test/unit/unit_configs/invalid_key.json b/test/unit/unit_configs/invalid_key.json new file mode 100644 index 0000000..8ef7994 --- /dev/null +++ b/test/unit/unit_configs/invalid_key.json @@ -0,0 +1,13 @@ +{ + "version": "1.0.0", + "name": "Invalid key", + "species": [ + { + "name": "A", + "phase": "gas", + "_absolute tolerance": 1.0e-30 + } + ], + "phases": [ ], + "reactions": [ ] +} \ No newline at end of file diff --git a/test/unit/unit_configs/missing_required_key.json b/test/unit/unit_configs/missing_required_key.json new file mode 100644 index 0000000..477df85 --- /dev/null +++ b/test/unit/unit_configs/missing_required_key.json @@ -0,0 +1,12 @@ +{ + "version": "1.0.0", + "name": "Invalid key", + "species": [ + { + "Name": "A", + "Phase": "gas" + } + ], + "phases": [ ], + "reactions": [ ] +} \ No newline at end of file diff --git a/test/unit/unit_configs/valid_species.json b/test/unit/unit_configs/valid_species.json index a1bd5fe..74fe68c 100644 --- a/test/unit/unit_configs/valid_species.json +++ b/test/unit/unit_configs/valid_species.json @@ -1,6 +1,6 @@ { "version": "1.0.0", - "name": "Full Configuration", + "name": "Valid species configuration", "species": [ { "name": "A",