Skip to content

Commit

Permalink
Merge pull request #34 from open-atmos/parse_condensed_phase_photolysis
Browse files Browse the repository at this point in the history
parsing condensed phase photolysis
  • Loading branch information
K20shores authored Jan 23, 2024
2 parents 997be71 + e3da194 commit 7aa9b49
Show file tree
Hide file tree
Showing 15 changed files with 580 additions and 10 deletions.
4 changes: 0 additions & 4 deletions examples/full_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,6 @@
{
"species name": "H2O2_aq",
"coefficient": 1
},
{
"species name": "H2O_aq",
"coefficient": 1
}
],
"products": [
Expand Down
13 changes: 13 additions & 0 deletions include/open_atmos/mechanism_configuration/validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ namespace open_atmos
const std::string Photolysis_key = "PHOTOLYSIS";
const std::string scaling_factor = "scaling factor";

// Condensed Phae Photolysis
const std::string CondensedPhasePhotolysis_key = "CONDENSED_PHASE_PHOTOLYSIS";
// also
// scaling factor
// aerosol phase
// aerosol-phase water

// Emissions
const std::string Emission_key = "EMISSION";
// also scaling factor
Expand Down Expand Up @@ -185,6 +192,12 @@ namespace open_atmos
const std::vector<std::string> optional_keys{ keys.name, keys.scaling_factor };
} photolysis;

struct CondensedPhasePhotolysis
{
const std::vector<std::string> required_keys{ keys.reactants, keys.products, keys.type, keys.aerosol_phase, keys.aerosol_phase_water };
const std::vector<std::string> optional_keys{ keys.name, keys.scaling_factor };
} condensed_phase_photolysis;

struct Emission
{
const std::vector<std::string> required_keys{ keys.products, keys.type, keys.gas_phase };
Expand Down
29 changes: 24 additions & 5 deletions include/open_atmos/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ namespace open_atmos
std::unordered_map<std::string, std::string> unknown_properties;
};

struct CondensedPhasePhotolysis
{
/// @brief Scaling factor to apply to user-provided rate constants
double scaling_factor_{ 1.0 };
/// @brief A list of reactants
std::vector<ReactionComponent> reactants;
/// @brief A list of products
std::vector<ReactionComponent> products;
/// @brief An identifier, optional, uniqueness not enforced
std::string name;
/// @brief An identifier indicating which aerosol phase this reaction takes place in
std::string aerosol_phase;
/// @brief An identifier indicating the species label of aqueous phase water
std::string aerosol_phase_water;
/// @brief Unknown properties, prefixed with two underscores (__)
std::unordered_map<std::string, std::string> unknown_properties;
};

struct Emission
{
/// @brief Scaling factor to apply to user-provided rate constants
Expand Down Expand Up @@ -226,14 +244,15 @@ namespace open_atmos
struct Reactions
{
std::vector<types::Arrhenius> arrhenius;
std::vector<types::CondensedPhaseArrhenius> condensed_phase_arrhenius;
std::vector<types::Troe> troe;
std::vector<types::Branched> branched;
std::vector<types::Tunneling> tunneling;
std::vector<types::Surface> surface;
std::vector<types::Photolysis> photolysis;
std::vector<types::CondensedPhaseArrhenius> condensed_phase_arrhenius;
std::vector<types::CondensedPhasePhotolysis> condensed_phase_photolysis;
std::vector<types::Emission> emission;
std::vector<types::FirstOrderLoss> first_order_loss;
std::vector<types::Photolysis> photolysis;
std::vector<types::Surface> surface;
std::vector<types::Troe> troe;
std::vector<types::Tunneling> tunneling;
};

struct Mechanism
Expand Down
118 changes: 118 additions & 0 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,114 @@ namespace open_atmos
return { status, photolysis };
}

/// @brief Parses a photolysis reaction
/// @param object A json object that should have information containing arrhenius parameters
/// @param existing_species A list of species configured in a mechanism
/// @param existing_phases A list of phases configured in a mechanism
/// @return A pair indicating parsing success and a struct of Photolysis parameters
std::pair<ConfigParseStatus, types::CondensedPhasePhotolysis>
ParseCondensedPhasePhotolysis(const json& object, const std::vector<types::Species> existing_species, const std::vector<types::Phase> existing_phases)
{
ConfigParseStatus status = ConfigParseStatus::Success;
types::CondensedPhasePhotolysis condensed_phase_photolysis;

status = ValidateSchema(object, validation::condensed_phase_photolysis.required_keys, validation::photolysis.optional_keys);
if (status == ConfigParseStatus::Success)
{
std::vector<types::ReactionComponent> products{};
for (const auto& reactant : object[validation::keys.products])
{
auto product_parse = ParseReactionComponent(reactant);
status = product_parse.first;
if (status != ConfigParseStatus::Success)
{
break;
}
products.push_back(product_parse.second);
}

std::vector<types::ReactionComponent> reactants{};
for (const auto& reactant : object[validation::keys.reactants])
{
auto reactant_parse = ParseReactionComponent(reactant);
status = reactant_parse.first;
if (status != ConfigParseStatus::Success)
{
break;
}
reactants.push_back(reactant_parse.second);
}

if (object.contains(validation::keys.scaling_factor))
{
condensed_phase_photolysis.scaling_factor_ = object[validation::keys.scaling_factor].get<double>();
}

if (object.contains(validation::keys.name))
{
condensed_phase_photolysis.name = object[validation::keys.name].get<std::string>();
}

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

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

std::string aerosol_phase = object[validation::keys.aerosol_phase].get<std::string>();
std::string aerosol_phase_water = object[validation::keys.aerosol_phase_water].get<std::string>();

std::vector<std::string> requested_species;
for (const auto& spec : products)
{
requested_species.push_back(spec.species_name);
}
for (const auto& spec : reactants)
{
requested_species.push_back(spec.species_name);
}
requested_species.push_back(aerosol_phase_water);

if (status == ConfigParseStatus::Success && RequiresUnknownSpecies(requested_species, existing_species))
{
status = ConfigParseStatus::ReactionRequiresUnknownSpecies;
}

if (status == ConfigParseStatus::Success && reactants.size() > 1)
{
status = ConfigParseStatus::TooManyReactionComponents;
}

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
{
status = ConfigParseStatus::UnknownPhase;
}

condensed_phase_photolysis.aerosol_phase = aerosol_phase;
condensed_phase_photolysis.aerosol_phase_water = aerosol_phase_water;
condensed_phase_photolysis.products = products;
condensed_phase_photolysis.reactants = reactants;
condensed_phase_photolysis.unknown_properties = unknown_properties;
}

return { status, condensed_phase_photolysis };
}

/// @brief Parses a emission reaction
/// @param object A json object that should have information containing arrhenius parameters
/// @param existing_species A list of species configured in a mechanism
Expand Down Expand Up @@ -1308,6 +1416,16 @@ namespace open_atmos
}
reactions.photolysis.push_back(photolysis_parse.second);
}
else if (type == validation::keys.CondensedPhasePhotolysis_key)
{
auto condensed_phase_photolysis_parse = ParseCondensedPhasePhotolysis(object, existing_species, existing_phases);
status = condensed_phase_photolysis_parse.first;
if (status != ConfigParseStatus::Success)
{
break;
}
reactions.condensed_phase_photolysis.push_back(condensed_phase_photolysis_parse.second);
}
else if (type == validation::keys.Emission_key)
{
auto emission_parse = ParseEmission(object, existing_species, existing_phases);
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 @@ -19,6 +19,7 @@ TEST(JsonParser, ParsesFullConfiguration)
EXPECT_EQ(mechanism.reactions.tunneling.size(), 1);
EXPECT_EQ(mechanism.reactions.surface.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis.size(), 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis.size(), 1);
EXPECT_EQ(mechanism.reactions.emission.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss.size(), 1);
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include(test_util)
create_standard_test(NAME parse_arrhenius SOURCES test_parse_arrhenius.cpp)
create_standard_test(NAME parse_branched SOURCES test_parse_branched.cpp)
create_standard_test(NAME parse_condensed_phase_arrhenius SOURCES test_parse_condensed_phase_arrhenius.cpp)
create_standard_test(NAME parse_condensed_phase_photolysis SOURCES test_parse_condensed_phase_photolysis.cpp)
create_standard_test(NAME parse_emission SOURCES test_parse_emission.cpp)
create_standard_test(NAME parse_first_order_loss SOURCES test_parse_first_order_loss.cpp)
create_standard_test(NAME parse_phases SOURCES test_parse_phases.cpp)
Expand Down
79 changes: 79 additions & 0 deletions test/unit/test_parse_condensed_phase_photolysis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <gtest/gtest.h>

#include <open_atmos/mechanism_configuration/parser.hpp>

using namespace open_atmos::mechanism_configuration;

TEST(JsonParser, CanParseValidCondensedPhasePhotolysisReaction)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/condensed_phase_photolysis/valid.json"));
EXPECT_EQ(status, ConfigParseStatus::Success);

EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis.size(), 2);

EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].aerosol_phase, "aqueous aerosol");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].aerosol_phase_water, "H2O_aq");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].name, "my condensed phase photolysis");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].scaling_factor_, 12.3);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].reactants[0].species_name, "B");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].reactants[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].products.size(), 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].products[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].products[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[0].unknown_properties["__comment"], "\"hi\"");

EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].aerosol_phase, "aqueous aerosol");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].aerosol_phase_water, "H2O_aq");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].scaling_factor_, 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].reactants[0].species_name, "B");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].reactants[0].coefficient, 1.2);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].products.size(), 1);
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].products[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.condensed_phase_photolysis[1].products[0].coefficient, 0.2);
}

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

TEST(JsonParser, CondensedPhasePhotolysisDetectsBadReactionComponent)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/condensed_phase_photolysis/bad_reaction_component.json"));
EXPECT_EQ(status, ConfigParseStatus::InvalidKey);
}

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

TEST(JsonParser, CondensedPhasePhotolysisDoesNotAcceptMoreThanOneReactant)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/condensed_phase_photolysis/more_than_one_reactant.json"));
EXPECT_EQ(status, ConfigParseStatus::TooManyReactionComponents);
}

TEST(JsonParser, CondensedPhasePhotolysisDetectsWhenRequestedSpeciesAreNotInAerosolPhase)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/condensed_phase_photolysis/species_not_in_aerosol_phase.json"));
EXPECT_EQ(status, ConfigParseStatus::RequestedAerosolSpeciesNotIncludedInAerosolPhase);
}

TEST(JsonParser, CondensedPhaseArrheniusDetectsUnknownAerosolPhaseWater)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/condensed_phase_photolysis/missing_aerosol_phase_water.json"));
EXPECT_EQ(status, ConfigParseStatus::ReactionRequiresUnknownSpecies);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"version": "1.0.0",
"name": "Bad reaction component",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
},
{
"name": "H2O_aq"
}
],
"phases": [
{
"name": "aqueous aerosol",
"species": [
"A",
"B",
"C",
"H2O_aq"
]
}
],
"reactions": [
{
"type": "CONDENSED_PHASE_PHOTOLYSIS",
"aerosol phase": "aqueous aerosol",
"aerosol-phase water": "H2O_aq",
"reactants": [
{
"species name": "A",
"Coefficient": 1.2
}
],
"products": [
{
"species name": "B",
"coefficient": 0.2
}
]
}
]
}
Loading

0 comments on commit 7aa9b49

Please sign in to comment.