Skip to content

Commit

Permalink
checking that species in condensed reaction are in the requested aero…
Browse files Browse the repository at this point in the history
…sol phase
  • Loading branch information
K20shores committed Jan 19, 2024
1 parent a31f4bd commit 2a35273
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 11 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 @@ -33,7 +33,8 @@ namespace open_atmos
DuplicatePhasesDetected,
PhaseRequiresUnknownSpecies,
ReactionRequiresUnknownSpecies,
UnknownPhase
UnknownPhase,
RequestedAerosolSpeciesNotIncludedInAerosolPhase
};
std::string configParseStatusToString(const ConfigParseStatus &status);

Expand Down
35 changes: 33 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace open_atmos
case ConfigParseStatus::PhaseRequiresUnknownSpecies: return "PhaseRequiresUnknownSpecies";
case ConfigParseStatus::ReactionRequiresUnknownSpecies: return "ReactionRequiresUnknownSpecies";
case ConfigParseStatus::UnknownPhase: return "UnknownPhase";
case ConfigParseStatus::RequestedAerosolSpeciesNotIncludedInAerosolPhase: return "RequestedAerosolSpeciesNotIncludedInAerosolPhase";
default: return "Unknown";
}
}
Expand Down Expand Up @@ -176,6 +177,21 @@ namespace open_atmos
return false;
}

bool RequiresUnknownSpecies(const std::vector<std::string> requested_species, const std::vector<std::string>& existing_species)
{
for (const auto& spec : requested_species)
{
auto it =
std::find_if(existing_species.begin(), existing_species.end(), [&spec](const std::string& existing) { return existing == spec; });

if (it == existing_species.end())
{
return true;
}
}
return false;
}

std::pair<ConfigParseStatus, std::vector<types::Species>> ParseSpecies(const json& objects)
{
ConfigParseStatus status = ConfigParseStatus::Success;
Expand Down Expand Up @@ -407,7 +423,7 @@ namespace open_atmos
return { status, arrhenius };
}

std::pair<ConfigParseStatus, types::CondensedPhaseArrhenius> ParseCondensedPhaseArrhenius(const json& object, const std::vector<types::Species> existing_species)
std::pair<ConfigParseStatus, types::CondensedPhaseArrhenius> ParseCondensedPhaseArrhenius(const json& object, const std::vector<types::Species>& existing_species, const std::vector<types::Phase>& existing_phases)
{
ConfigParseStatus status = ConfigParseStatus::Success;
types::CondensedPhaseArrhenius condensed_phase_arrhenius;
Expand Down Expand Up @@ -503,6 +519,21 @@ namespace open_atmos
status = ConfigParseStatus::ReactionRequiresUnknownSpecies;
}

auto phase_it = std::find_if(existing_phases.begin(), existing_phases.end(), [&aerosol_phase](const types::Phase& phase) {
return phase.name == aerosol_phase;
});

if (phase_it != existing_phases.end()) {
// check if all of the species for this reaction are actually in the aerosol phase
std::vector<std::string> aerosol_phase_species = {(*phase_it).species.begin(), (*phase_it).species.end()};
if (status == ConfigParseStatus::Success && RequiresUnknownSpecies(requested_species, aerosol_phase_species))
{
status = ConfigParseStatus::RequestedAerosolSpeciesNotIncludedInAerosolPhase;
}
}
else {
// TODO phase check
}

condensed_phase_arrhenius.aerosol_phase = aerosol_phase;
condensed_phase_arrhenius.aerosol_phase_water = aerosol_phase_water;
Expand Down Expand Up @@ -890,7 +921,7 @@ namespace open_atmos
}
else if (type == validation::keys.CondensedPhaseArrhenius_key)
{
auto condensed_phase_arrhenius_parse = ParseCondensedPhaseArrhenius(object, existing_species);
auto condensed_phase_arrhenius_parse = ParseCondensedPhaseArrhenius(object, existing_species, existing_phases);
status = condensed_phase_arrhenius_parse.first;
if (status != ConfigParseStatus::Success)
{
Expand Down
1 change: 1 addition & 0 deletions test/integration/test_json_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TEST(JsonParser, ParsesFullConfiguration)
EXPECT_EQ(mechanism.name, "Full Configuration");
EXPECT_EQ(mechanism.species.size(), 11);
EXPECT_EQ(mechanism.reactions.arrhenius.size(), 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_arrhenius.size(), 1);
EXPECT_EQ(mechanism.reactions.troe.size(), 1);
EXPECT_EQ(mechanism.reactions.branched.size(), 1);
EXPECT_EQ(mechanism.reactions.tunneling.size(), 1);
Expand Down
14 changes: 14 additions & 0 deletions test/unit/test_parse_condensed_phase_arrhenius.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,18 @@ TEST(JsonParser, CondensedPhaseArrheniusDetectsBadReactionComponent)
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/condensed_phase_arrhenius/bad_reaction_component.json"));
EXPECT_EQ(status, ConfigParseStatus::RequiredKeyNotFound);
}

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

TEST(JsonParser, CondensedPhaseArrheniusDetectsWhenRequestedSpeciesAreNotInAerosolPhase)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/condensed_phase_arrhenius/species_not_in_aerosol_phase.json"));
EXPECT_EQ(status, ConfigParseStatus::RequestedAerosolSpeciesNotIncludedInAerosolPhase);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@
},
{
"name": "B"
},
{
"name": "H2O_aq"
}
],
"phases": [
{
"name": "gas",
"species": [
"A",
"B"
"B",
"H2O_aq"
]
}
],
"reactions": [
{
"type": "ARRHENIUS",
"gas phase": "gas",
"type": "CONDENSED_PHASE_ARRHENIUS",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_a",
"reactants": [
{
"Species name": "A"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": "1.0.0",
"name": "Missing condensed phase arrhenius aerosol phase water",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
},
{
"name": "H2O_aq"
}
],
"phases": [
{
"name": "aqueous aerosol",
"species": [
"A",
"B",
"C",
"H2O_aq"
]
}
],
"reactions": [
{
"type": "CONDENSED_PHASE_ARRHENIUS",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_a",
"reactants": [
{
"species name": "A"
}
],
"products": [
{
"species name": "C"
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@
},
{
"name": "B"
},
{
"name": "H2O_aq"
}
],
"phases": [
{
"name": "gas",
"species": [
"A",
"B"
"B",
"H2O_aq"
]
}
],
"reactions": [
{
"type": "ARRHENIUS",
"gas phase": "gas",
"type": "CONDENSED_PHASE_ARRHENIUS",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_aq",
"reactants": [
{
"species name": "A"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"version": "1.0.0",
"name": "Condensed phase arrhenius using species not in its requested aerosol phase",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
},
{
"name": "H2O_aq"
}
],
"phases": [
{
"name": "aqueous aerosol",
"species": [
"A",
"C",
"H2O_aq"
]
}
],
"reactions": [
{
"type": "CONDENSED_PHASE_ARRHENIUS",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_aq",
"reactants": [
{
"species name": "A",
"coefficient": 1
}
],
"products": [
{
"species name": "B",
"coefficient": 1.2
},
{
"species name": "C",
"coefficient": 0.3
}
],
"A": 32.1,
"B": -2.3,
"C": 102.3,
"D": 63.4,
"E": -1.3,
"name": "my arrhenius",
"__solver_param": 0.1
},
{
"type": "CONDENSED_PHASE_ARRHENIUS",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_aq",
"reactants": [
{
"species name": "A",
"coefficient": 2
},
{
"species name": "B",
"coefficient": 0.1
}
],
"products": [
{
"species name": "C",
"coefficient": 0.5,
"__optional thing": "hello"
}
],
"A": 3.1,
"B": -0.3,
"C": 12.3,
"D": 6.4,
"E": -0.3,
"name": "my arrhenius2"
},
{
"type": "CONDENSED_PHASE_ARRHENIUS",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_aq",
"reactants": [
{
"species name": "A"
}
],
"products": [
{
"species name": "C"
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"species": [
"A",
"B",
"C"
"C",
"H2O_aq"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"species": [
"A",
"B",
"C"
"C",
"H2O_aq"
]
}
],
Expand Down

0 comments on commit 2a35273

Please sign in to comment.