Skip to content

Commit

Permalink
checking duplicate species
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Jan 12, 2024
1 parent ad07e4f commit 29fb30e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 12 deletions.
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 @@ -32,7 +32,8 @@ namespace open_atmos
RequiredKeyNotFound,
ContainsNonStandardKey,
MutuallyExclusiveOption,
InvalidVersion
InvalidVersion,
DuplicateSpeciesDetected
};
std::string configParseStatusToString(const ConfigParseStatus &status);

Expand Down
3 changes: 1 addition & 2 deletions include/open_atmos/mechanism_configuration/validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ namespace open_atmos

struct Species
{
const std::vector<std::string> required_keys{ keys.name };
const std::vector<std::string> required_keys{ keys.name, keys.phase };
const std::vector<std::string> optional_keys{ keys.tracer_type,
keys.absolute_tolerance,
keys.diffusion_coefficient,
keys.molecular_weight,
keys.phase,
keys.henrys_law_constant_298,
keys.henrys_law_constant_exponential_factor,
keys.n_star,
Expand Down
4 changes: 3 additions & 1 deletion include/open_atmos/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ namespace open_atmos
struct Species
{
std::string name;
std::string phase;

std::map<std::string, double> optional_properties;
std::map<std::string, double> optional_numerical_properties;
std::map<std::string, std::string> optional_string_properties;

std::unordered_map<std::string, std::string> unknown_properties;
};
Expand Down
34 changes: 28 additions & 6 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace open_atmos
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 @@ -155,35 +156,56 @@ namespace open_atmos
break;
}

std::cout << "object:\n" << object.dump(4) << std::endl;
// std::cout << "object:\n" << object.dump(4) << std::endl;

std::string name = object[validation::keys.name].get<std::string>();
std::map<std::string, double> properties{};
std::string phase = object[validation::keys.phase].get<std::string>();

std::map<std::string, double> numerical_properties{};
std::map<std::string, std::string> string_properties{};
for (const auto& key : validation::species.optional_keys)
{
if (object.contains(key))
{
double val = object[key].get<double>();
properties[key] = val;
if (key == validation::keys.tracer_type) {
std::string val = object[key].get<std::string>();
string_properties[key] = val;
}
else {
double val = object[key].get<double>();
numerical_properties[key] = val;
}
}
}

auto comments = GetComments(object, validation::species.required_keys, validation::species.optional_keys);

std::unordered_map<std::string, std::string> unknown_properties;
for (const auto& key : validation::species.optional_keys)
for (const auto& key : comments)
{
std::string val = object[key].dump();
unknown_properties[key] = val;
}

species.name = name;
species.optional_properties = properties;
species.phase = phase;
species.optional_numerical_properties = numerical_properties;
species.optional_string_properties = string_properties;
species.unknown_properties = unknown_properties;

all_species.push_back(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) {
status = ConfigParseStatus::DuplicateSpeciesDetected;
}
break;
}
if (status != ConfigParseStatus::Success) break;
}

return { status, all_species };
}

Expand Down
2 changes: 2 additions & 0 deletions test/integration/test_json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ using namespace open_atmos::mechanism_configuration;
TEST(JsonParser, Returns)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("examples/full_configuration.json"));
EXPECT_EQ(status, ConfigParseStatus::Success);
}
39 changes: 37 additions & 2 deletions test/unit/test_parse_species.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,46 @@

using namespace open_atmos::mechanism_configuration;

TEST(JsonParser, CanParseSpecies)
TEST(JsonParser, CanParseValidSpecies)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/species.json"));
auto [status, mechanism] = parser.Parse(std::string("unit_configs/valid_species.json"));

EXPECT_EQ(status, ConfigParseStatus::Success);
EXPECT_EQ(mechanism.species.size(), 3);

EXPECT_EQ(mechanism.species[0].name, "A");
EXPECT_EQ(mechanism.species[0].phase, "gas");
EXPECT_EQ(mechanism.species[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.species[0].unknown_properties["__absolute tolerance"], "1e-30");

EXPECT_EQ(mechanism.species[1].name, "H2O2");
EXPECT_EQ(mechanism.species[1].phase, "gas");
EXPECT_EQ(mechanism.species[1].optional_numerical_properties.size(), 6);
EXPECT_EQ(mechanism.species[1].optional_numerical_properties["HLC(298K) [mol m-3 Pa-1]"], 1.011596348);
EXPECT_EQ(mechanism.species[1].optional_numerical_properties["HLC exponential factor [K]"], 6340);
EXPECT_EQ(mechanism.species[1].optional_numerical_properties["diffusion coefficient [m2 s-1]"], 1.46e-05);
EXPECT_EQ(mechanism.species[1].optional_numerical_properties["N star"], 1.74);
EXPECT_EQ(mechanism.species[1].optional_numerical_properties["molecular weight [kg mol-1]"], 0.0340147);
EXPECT_EQ(mechanism.species[1].optional_numerical_properties["density [kg m-3]"], 1000.0);
EXPECT_EQ(mechanism.species[1].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.species[1].unknown_properties["__absolute tolerance"], "1e-10");

EXPECT_EQ(mechanism.species[2].name, "aerosol stuff");
EXPECT_EQ(mechanism.species[2].phase, "AEROSOL");
EXPECT_EQ(mechanism.species[2].optional_numerical_properties.size(), 2);
EXPECT_EQ(mechanism.species[2].optional_numerical_properties["molecular weight [kg mol-1]"], 0.5);
EXPECT_EQ(mechanism.species[2].optional_numerical_properties["density [kg m-3]"], 1000.0);
EXPECT_EQ(mechanism.species[2].optional_string_properties.size(), 1);
EXPECT_EQ(mechanism.species[2].optional_string_properties["tracer type"], "CONSTANT");
EXPECT_EQ(mechanism.species[2].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.species[2].unknown_properties["__absolute tolerance"], "1e-20");
}

TEST(JsonParser, DetectsDuplicateSpecies)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/duplicate_species.json"));

EXPECT_EQ(status, ConfigParseStatus::DuplicateSpeciesDetected);
}
16 changes: 16 additions & 0 deletions test/unit/unit_configs/duplicate_species.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "1.0.0",
"name": "Full Configuration",
"species": [
{
"name": "A",
"phase": "gas"
},
{
"name": "A",
"phase": "gas"
}
],
"phases": [ ],
"reactions": [ ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"species": [
{
"name": "A",
"phase": "gas",
"__absolute tolerance": 1.0e-30
},
{
"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 Down

0 comments on commit 29fb30e

Please sign in to comment.