Skip to content

Commit

Permalink
parsing photolysis
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Jan 18, 2024
1 parent 9c6ac54 commit c94a116
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/open_atmos/mechanism_configuration/validation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ namespace open_atmos
const std::string gas_phase_products = "gas-phase products";
const std::string aerosol_phase = "aerosol phase";

// Photolysis
const std::string Photolysis_key = "PHOTOLYSIS";
const std::string scaling_factor = "scaling factor";

} keys;

struct Configuration
Expand Down Expand Up @@ -149,6 +153,12 @@ namespace open_atmos
const std::vector<std::string> optional_keys{ keys.name, keys.reaction_probability };
} surface;

struct Photolysis
{
const std::vector<std::string> required_keys{ keys.reactants, keys.products, keys.type, keys.gas_phase };
const std::vector<std::string> optional_keys{ keys.name, keys.scaling_factor };
} photolysis;

struct Mechanism
{
const std::vector<std::string> required_keys{};
Expand Down
17 changes: 17 additions & 0 deletions include/open_atmos/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,30 @@ namespace open_atmos
std::unordered_map<std::string, std::string> unknown_properties;
};

struct Photolysis
{
/// @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 gas phase this reaction takes place in
std::string gas_phase;
/// @brief Unknown properties, prefixed with two underscores (__)
std::unordered_map<std::string, std::string> unknown_properties;
};

struct Reactions
{
std::vector<types::Arrhenius> 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;
};

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

std::pair<ConfigParseStatus, types::Photolysis> ParsePhotolysis(const json& object, const std::vector<types::Species> existing_species, const std::vector<types::Phase> existing_phases)
{
ConfigParseStatus status = ConfigParseStatus::Success;
types::Photolysis photolysis;

status = ValidateSchema(object, validation::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))
{
photolysis.scaling_factor_ = object[validation::keys.scaling_factor].get<double>();
}

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

auto comments = GetComments(object, validation::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::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);
}

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

std::string gas_phase = object[validation::keys.gas_phase].get<std::string>();
auto it =
std::find_if(existing_phases.begin(), existing_phases.end(), [&gas_phase](const auto& phase) { return phase.name == gas_phase; });
if (status == ConfigParseStatus::Success && it == existing_phases.end())
{
status = ConfigParseStatus::UnknownPhase;
}

photolysis.gas_phase = gas_phase;
photolysis.products = products;
photolysis.reactants = reactants;
photolysis.unknown_properties = unknown_properties;
}

return { status, photolysis };
}

std::pair<ConfigParseStatus, types::Reactions>
ParseReactions(const json& objects, const std::vector<types::Species> existing_species, const std::vector<types::Phase> existing_phases)
{
Expand Down Expand Up @@ -821,6 +904,16 @@ namespace open_atmos
}
reactions.surface.push_back(surface_parse.second);
}
else if (type == validation::keys.Photolysis_key)
{
auto photolysis_parse = ParsePhotolysis(object, existing_species, existing_phases);
status = photolysis_parse.first;
if (status != ConfigParseStatus::Success)
{
break;
}
reactions.photolysis.push_back(photolysis_parse.second);
}
}

return { status, reactions };
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 @@ -16,6 +16,7 @@ TEST(JsonParser, ParsesFullConfiguration)
EXPECT_EQ(mechanism.reactions.branched.size(), 1);
EXPECT_EQ(mechanism.reactions.tunneling.size(), 1);
EXPECT_EQ(mechanism.reactions.surface.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis.size(), 1);
}

TEST(JsonParser, ParserReportsBadFiles)
Expand Down
1 change: 1 addition & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ create_standard_test(NAME parse_troe SOURCES test_parse_troe.cpp)
create_standard_test(NAME parse_branched SOURCES test_parse_branched.cpp)
create_standard_test(NAME parse_tunneling SOURCES test_parse_tunneling.cpp)
create_standard_test(NAME parse_surface SOURCES test_parse_surface.cpp)
create_standard_test(NAME parse_photolysis SOURCES test_parse_photolysis.cpp)

################################################################################
# Copy test data
Expand Down
56 changes: 56 additions & 0 deletions test/unit/test_parse_photolysis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <gtest/gtest.h>

#include <open_atmos/mechanism_configuration/parser.hpp>

using namespace open_atmos::mechanism_configuration;

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

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

EXPECT_EQ(mechanism.reactions.photolysis[0].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.photolysis[0].name, "my photolysis");
EXPECT_EQ(mechanism.reactions.photolysis[0].scaling_factor_, 12.3);
EXPECT_EQ(mechanism.reactions.photolysis[0].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis[0].reactants[0].species_name, "B");
EXPECT_EQ(mechanism.reactions.photolysis[0].reactants[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.photolysis[0].products.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis[0].products[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.photolysis[0].products[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.photolysis[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis[0].unknown_properties["__comment"], "\"hi\"");

EXPECT_EQ(mechanism.reactions.photolysis[1].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.photolysis[1].scaling_factor_, 1);
EXPECT_EQ(mechanism.reactions.photolysis[1].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis[1].reactants[0].species_name, "B");
EXPECT_EQ(mechanism.reactions.photolysis[1].reactants[0].coefficient, 1.2);
EXPECT_EQ(mechanism.reactions.photolysis[1].products.size(), 1);
EXPECT_EQ(mechanism.reactions.photolysis[1].products[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.photolysis[1].products[0].coefficient, 0.2);
}

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

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

TEST(JsonParser, PhotolysisDetectsUnknownPhase)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/photolysis/missing_phase.json"));
EXPECT_EQ(status, ConfigParseStatus::UnknownPhase);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"version": "1.0.0",
"name": "Bad reaction component",
"species": [
{
"name": "A"
},
{
"name": "B"
}
],
"phases": [
{
"name": "gas",
"species": [
"A",
"B"
]
}
],
"reactions": [
{
"type": "PHOTOLYSIS",
"gas phase": "gas",
"reactants": [
{
"species name": "A",
"Coefficient": 1.2
}
],
"products": [
{
"species name": "B",
"coefficient": 0.2
}
]
}
]
}
34 changes: 34 additions & 0 deletions test/unit/unit_configs/reactions/photolysis/missing_phase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": "1.0.0",
"name": "Missing phase",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
}
],
"phases": [ ],
"reactions": [
{
"type": "PHOTOLYSIS",
"gas phase": "gas",
"reactants": [
{
"species name": "B",
"coefficient": 1.2
}
],
"products": [
{
"species name": "C",
"coefficient": 0.2
}
]
}
]
}
39 changes: 39 additions & 0 deletions test/unit/unit_configs/reactions/photolysis/unknown_species.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"version": "1.0.0",
"name": "Unknown species",
"species": [
{
"name": "A"
},
{
"name": "B"
}
],
"phases": [
{
"name": "gas",
"species": [
"A",
"B"
]
}
],
"reactions": [
{
"type": "PHOTOLYSIS",
"gas phase": "gas",
"reactants": [
{
"species name": "B",
"coefficient": 1.2
}
],
"products": [
{
"species name": "C",
"coefficient": 0.2
}
]
}
]
}
Loading

0 comments on commit c94a116

Please sign in to comment.