Skip to content

Commit

Permalink
dropping to c++17, using optional for ion pair
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Jan 23, 2024
1 parent 4845bfb commit b7be482
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 101 deletions.
6 changes: 4 additions & 2 deletions examples/full_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@
"species": [
"H2O2_aq",
"H2O_aq",
"ethanol_aq"
"ethanol_aq",
"A",
"B",
"C"
]
},
{
Expand Down Expand Up @@ -120,7 +123,6 @@
},
{
"type": "AQUEOUS_EQUILIBRIUM",
"gas phase": "gas",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_aq",
"A": 1.14e-2,
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 @@ -35,7 +35,8 @@ namespace open_atmos
ReactionRequiresUnknownSpecies,
UnknownPhase,
RequestedAerosolSpeciesNotIncludedInAerosolPhase,
TooManyReactionComponents
TooManyReactionComponents,
InvalidIonPair
};
std::string configParseStatusToString(const ConfigParseStatus &status);

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 @@ -8,6 +8,8 @@
#include <unordered_map>
#include <array>
#include <vector>
#include <optional>


namespace open_atmos
{
Expand Down Expand Up @@ -240,7 +242,7 @@ namespace open_atmos
/// @brief A list of products
std::vector<ReactionComponent> products;
/// @brief Optional ion pairs
std::array<ReactionComponent, 2> ion_pair;
std::optional<std::array<ReactionComponent, 2>> ion_pair;
/// @brief Pre-exponential factor (s-1)
double A{ 1 };
/// @brief A constant
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ configure_file(version.hpp.in ${PROJECT_SOURCE_DIR}/include/open_atmos/mechanism
add_library(mechanism_configuration)
add_library(open_atmos::mechanism_configuration ALIAS mechanism_configuration)

target_compile_features(mechanism_configuration PUBLIC cxx_std_20)
target_compile_features(mechanism_configuration PUBLIC cxx_std_17)

target_sources(mechanism_configuration
PRIVATE
Expand Down
24 changes: 12 additions & 12 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace open_atmos
case ConfigParseStatus::UnknownPhase: return "UnknownPhase";
case ConfigParseStatus::RequestedAerosolSpeciesNotIncludedInAerosolPhase: return "RequestedAerosolSpeciesNotIncludedInAerosolPhase";
case ConfigParseStatus::TooManyReactionComponents: return "TooManyReactionComponents";
case ConfigParseStatus::InvalidIonPair: return "InvalidIonPair";
default: return "Unknown";
}
}
Expand Down Expand Up @@ -137,7 +138,7 @@ namespace open_atmos
// now, anything left must be standard comment starting with __
for (auto& key : remaining)
{
if (!key.starts_with("__"))
if (key.find("__") == std::string::npos)
{
std::cerr << "Non-standard key '" << key << "' found in object" << object << std::endl;

Expand Down Expand Up @@ -1325,22 +1326,21 @@ namespace open_atmos
status = ConfigParseStatus::UnknownPhase;
}

const auto& ion_pair_object = object[validation::keys.ion_pair];
status = ValidateSchema(ion_pair_object, validation::ion_pair.required_keys, validation::ion_pair.optional_keys);
if (status == ConfigParseStatus::Success) {
if (status == ConfigParseStatus::Success && object.contains(validation::keys.ion_pair)) {
const auto& ion_pair_object = object[validation::keys.ion_pair];
status = ValidateSchema(ion_pair_object, validation::ion_pair.required_keys, validation::ion_pair.optional_keys);
auto first_parse = ParseReactionComponent(ion_pair_object[validation::keys.first]);
if (first_parse.first != ConfigParseStatus::Success) {
status = first_parse.first;
}
else {
aqueous_equilibrium.ion_pair[0] = first_parse.second;
}
auto second_parse = ParseReactionComponent(ion_pair_object[validation::keys.second]);
if (second_parse.first != ConfigParseStatus::Success) {
status = second_parse.first;
}
else {
aqueous_equilibrium.ion_pair[1] = second_parse.second;
auto second_parse = ParseReactionComponent(ion_pair_object[validation::keys.second]);
if (second_parse.first != ConfigParseStatus::Success) {
status = second_parse.first;
}
else {
aqueous_equilibrium.ion_pair = std::array<types::ReactionComponent, 2>{first_parse.second, second_parse.second};
}
}
}

Expand Down
53 changes: 27 additions & 26 deletions test/unit/test_parse_aqueous_equilibrium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ TEST(JsonParser, CanParseValidAqueousEquilibriumReaction)
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].products[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].products[1].species_name, "C");
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].products[1].coefficient, 1);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].ion_pair[0].species_name, "A");
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].ion_pair[1].species_name, "B");
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].ion_pair.value()[0].species_name, "A");
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].ion_pair.value()[1].species_name, "B");
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[0].unknown_properties["__comment"], "\"GIF is pronounced with a hard g\"");

EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].aerosol_phase, "aerosol");
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].aerosol_phase_water, "H2O_aq");
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].A, 1);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].C, 0);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].ion_pair.has_value(), false);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].k_reverse, 0.32);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].reactants[0].species_name, "A");
Expand All @@ -46,30 +47,30 @@ TEST(JsonParser, CanParseValidAqueousEquilibriumReaction)
EXPECT_EQ(mechanism.reactions.aqueous_equilibrium[1].products[1].coefficient, 1);
}

// TEST(JsonParser, AqueousEquilibriumDetectsUnknownSpecies)
// {
// JsonParser parser;
// auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/unknown_species.json"));
// EXPECT_EQ(status, ConfigParseStatus::ReactionRequiresUnknownSpecies);
// }
TEST(JsonParser, AqueousEquilibriumDetectsUnknownSpecies)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/unknown_species.json"));
EXPECT_EQ(status, ConfigParseStatus::ReactionRequiresUnknownSpecies);
}

// TEST(JsonParser, AqueousEquilibriumDetectsMutuallyExclusiveOptions)
// {
// JsonParser parser;
// auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/mutually_exclusive.json"));
// EXPECT_EQ(status, ConfigParseStatus::MutuallyExclusiveOption);
// }
TEST(JsonParser, AqueousEquilibriumDetectsBadReactionComponent)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/bad_reaction_component.json"));
EXPECT_EQ(status, ConfigParseStatus::RequiredKeyNotFound);
}

// TEST(JsonParser, AqueousEquilibriumDetectsBadReactionComponent)
// {
// JsonParser parser;
// auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/bad_reaction_component.json"));
// EXPECT_EQ(status, ConfigParseStatus::RequiredKeyNotFound);
// }
TEST(JsonParser, AqueousEquilibriumDetectsUnknownPhase)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/missing_phase.json"));
EXPECT_EQ(status, ConfigParseStatus::UnknownPhase);
}

// TEST(JsonParser, AqueousEquilibriumDetectsUnknownPhase)
// {
// JsonParser parser;
// auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/missing_phase.json"));
// EXPECT_EQ(status, ConfigParseStatus::UnknownPhase);
// }
TEST(JsonParser, AqueousEquilibriumDetectsInvalidIonPair)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/aqueous_equilibrium/invalid_ion_pair.json"));
EXPECT_EQ(status, ConfigParseStatus::InvalidIonPair);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,49 @@
},
{
"name": "B"
},
{
"name": "C"
},
{
"name": "H2O_aq"
}
],
"phases": [
{
"name": "gas",
"name": "aerosol",
"species": [
"A",
"B"
"B",
"C",
"H2O_aq"
]
}
],
"reactions": [
{
"type": "ARRHENIUS",
"gas phase": "gas",
"type": "AQUEOUS_EQUILIBRIUM",
"aerosol phase": "aerosol",
"aerosol-phase water": "H2O_aq",
"k_reverse": 0.32,
"ion pair": {
"first": { "species name": "A"},
"second": { "species name": "B"}
},
"reactants": [
{
"Species name": "A"
"Species name": "A",
"coefficient": 2
}
],
"products": [
{
"species name": "B"
"Species name": "B",
"coefficient": 1
},
{
"Species name": "C",
"coefficient": 1
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"version": "1.0.0",
"name": "Valid aqueous equilibrium",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
},
{
"name": "H2O_aq"
}
],
"phases": [
{
"name": "aerosol",
"species": [
"A",
"B",
"C",
"H2O_aq"
]
}
],
"reactions": [
{
"type": "AQUEOUS_EQUILIBRIUM",
"aerosol phase": "aerosol",
"aerosol-phase water": "H2O_aq",
"k_reverse": 0.32,
"ion pair": {
"first": { "species name": "A"},
"second": { "species name": "B"}
},
"reactants": [
{
"species name": "A",
"coefficient": 2
}
],
"products": [
{
"species name": "B",
"coefficient": 1
},
{
"species name": "C",
"coefficient": 1
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,36 @@
},
{
"name": "C"
},
{
"name": "H2O_aq"
}
],
"phases": [ ],
"reactions": [
{
"type": "ARRHENIUS",
"gas phase": "gas",
"type": "AQUEOUS_EQUILIBRIUM",
"aerosol phase": "aerosol",
"aerosol-phase water": "H2O_aq",
"k_reverse": 0.32,
"ion pair": {
"first": { "species name": "A"},
"second": { "species name": "B"}
},
"reactants": [
{
"species name": "A"
"species name": "A",
"coefficient": 2
}
],
"products": [
{
"species name": "C"
"species name": "B",
"coefficient": 1
},
{
"species name": "C",
"coefficient": 1
}
]
}
Expand Down

This file was deleted.

Loading

0 comments on commit b7be482

Please sign in to comment.