Skip to content

Commit

Permalink
parsing species
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Jan 12, 2024
1 parent a9521a0 commit c7ad061
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 23 deletions.
14 changes: 11 additions & 3 deletions examples/full_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,25 +28,29 @@
},
{
"name": "ethanol",
"phase": "gas",
"diffusion coefficient [m2 s-1]": 0.95E-05,
"N star": 2.55,
"molecular weight [kg mol-1]": 0.04607,
"__absolute tolerance": 1.0e-20
},
{
"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
},
Expand Down
4 changes: 0 additions & 4 deletions include/open_atmos/mechanism_configuration/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ namespace open_atmos
InvalidKey,
UnknownKey,
InvalidFilePath,
NoConfigFilesFound,
DataSectionNotFound,
InvalidSpecies,
ObjectTypeNotFound,
RequiredKeyNotFound,
ContainsNonStandardKey,
MutuallyExclusiveOption,
InvalidVersion,
DuplicateSpeciesDetected
Expand Down
29 changes: 15 additions & 14 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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>();
std::string phase = object[validation::keys.phase].get<std::string>();

Expand All @@ -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<std::string>();
string_properties[key] = val;
}
else {
else
{
double val = object[key].get<double>();
numerical_properties[key] = val;
}
Expand All @@ -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 };
Expand Down Expand Up @@ -274,4 +275,4 @@ namespace open_atmos
return { status, mechanism };
}
} // namespace mechanism_configuration
} // namespace open_atmos
} // namespace open_atmos
16 changes: 16 additions & 0 deletions test/unit/test_parse_species.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion test/unit/unit_configs/duplicate_species.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.0.0",
"name": "Full Configuration",
"name": "Duplicate Species",
"species": [
{
"name": "A",
Expand Down
13 changes: 13 additions & 0 deletions test/unit/unit_configs/invalid_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1.0.0",
"name": "Invalid key",
"species": [
{
"name": "A",
"phase": "gas",
"_absolute tolerance": 1.0e-30
}
],
"phases": [ ],
"reactions": [ ]
}
12 changes: 12 additions & 0 deletions test/unit/unit_configs/missing_required_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1.0.0",
"name": "Invalid key",
"species": [
{
"Name": "A",
"Phase": "gas"
}
],
"phases": [ ],
"reactions": [ ]
}
2 changes: 1 addition & 1 deletion test/unit/unit_configs/valid_species.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version": "1.0.0",
"name": "Full Configuration",
"name": "Valid species configuration",
"species": [
{
"name": "A",
Expand Down

0 comments on commit c7ad061

Please sign in to comment.