Skip to content

Commit

Permalink
parsing wet deposition
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Jan 22, 2024
1 parent 027a7ed commit bd362e6
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 42 deletions.
1 change: 0 additions & 1 deletion examples/full_configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@
},
{
"type": "WET_DEPOSITION",
"gas phase": "gas",
"aerosol phase": "cloud",
"name": "rxn cloud",
"scaling factor": 12.3
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 @@ -114,7 +114,6 @@ namespace open_atmos
// also
// scaling factor
// aerosol phase
// gas phase

} keys;

Expand Down Expand Up @@ -206,7 +205,7 @@ namespace open_atmos

struct WetDeposition
{
const std::vector<std::string> required_keys{ keys.gas_phase, keys.aerosol_phase, keys.type };
const std::vector<std::string> required_keys{ keys.aerosol_phase, keys.type };
const std::vector<std::string> optional_keys{ keys.name, keys.scaling_factor };
} wet_deposition;

Expand Down
10 changes: 4 additions & 6 deletions include/open_atmos/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ namespace open_atmos
struct Photolysis
{
/// @brief Scaling factor to apply to user-provided rate constants
double scaling_factor_{ 1.0 };
double scaling_factor{ 1.0 };
/// @brief A list of reactants
std::vector<ReactionComponent> reactants;
/// @brief A list of products
Expand All @@ -198,7 +198,7 @@ namespace open_atmos
struct Emission
{
/// @brief Scaling factor to apply to user-provided rate constants
double scaling_factor_{ 1.0 };
double scaling_factor{ 1.0 };
/// @brief A list of products
std::vector<ReactionComponent> products;
/// @brief An identifier, optional, uniqueness not enforced
Expand All @@ -212,7 +212,7 @@ namespace open_atmos
struct FirstOrderLoss
{
/// @brief Scaling factor to apply to user-provided rate constants
double scaling_factor_{ 1.0 };
double scaling_factor{ 1.0 };
/// @brief A list of reactants
std::vector<ReactionComponent> reactants;
/// @brief An identifier, optional, uniqueness not enforced
Expand All @@ -226,11 +226,9 @@ namespace open_atmos
struct WetDeposition
{
/// @brief Scaling factor to apply to user-provided rate constants
double scaling_factor_{ 1.0 };
double scaling_factor{ 1.0 };
/// @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 An identifier indicating which aerosol phase this reaction takes place in
std::string aerosol_phase;
/// @brief Unknown properties, prefixed with two underscores (__)
Expand Down
55 changes: 52 additions & 3 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ namespace open_atmos

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

if (object.contains(validation::keys.name))
Expand Down Expand Up @@ -1105,7 +1105,7 @@ namespace open_atmos

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

if (object.contains(validation::keys.name))
Expand Down Expand Up @@ -1176,7 +1176,7 @@ namespace open_atmos

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

if (object.contains(validation::keys.name))
Expand Down Expand Up @@ -1224,6 +1224,55 @@ namespace open_atmos
return { status, first_order_loss };
}

/// @brief Parses a wet deposition 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 First Order Loss parameters
std::pair<ConfigParseStatus, types::WetDeposition>
ParseWetDeposition(const json& object, const std::vector<types::Species> existing_species, const std::vector<types::Phase> existing_phases)
{
ConfigParseStatus status = ConfigParseStatus::Success;
types::WetDeposition wet_deposition;

status = ValidateSchema(object, validation::wet_deposition.required_keys, validation::wet_deposition.optional_keys);
if (status == ConfigParseStatus::Success)
{
if (object.contains(validation::keys.scaling_factor))
{
wet_deposition.scaling_factor = object[validation::keys.scaling_factor].get<double>();
}

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

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

// check if aerosol phase exists
auto it = std::find_if(existing_phases.begin(), existing_phases.end(), [&aerosol_phase](const auto& phase) { return phase.name == aerosol_phase; });
if (status == ConfigParseStatus::Success && it == existing_phases.end())
{
status = ConfigParseStatus::UnknownPhase;
}

wet_deposition.aerosol_phase = aerosol_phase;
wet_deposition.unknown_properties = unknown_properties;
}

return { status, wet_deposition };
}

/// @brief Parses all reactions
/// @param objects A json object that should contain only valid reactions
/// @param existing_species A list of spcecies configured for a mechanism
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_parse_emission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ TEST(JsonParser, CanParseValidEmissionReaction)

EXPECT_EQ(mechanism.reactions.emission[0].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.emission[0].name, "my emission");
EXPECT_EQ(mechanism.reactions.emission[0].scaling_factor_, 12.3);
EXPECT_EQ(mechanism.reactions.emission[0].scaling_factor, 12.3);
EXPECT_EQ(mechanism.reactions.emission[0].products.size(), 1);
EXPECT_EQ(mechanism.reactions.emission[0].products[0].species_name, "B");
EXPECT_EQ(mechanism.reactions.emission[0].products[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.emission[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.reactions.emission[0].unknown_properties["__comment"], "\"Dr. Pepper outranks any other soda\"");

EXPECT_EQ(mechanism.reactions.emission[1].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.emission[1].scaling_factor_, 1);
EXPECT_EQ(mechanism.reactions.emission[1].scaling_factor, 1);
EXPECT_EQ(mechanism.reactions.emission[1].products.size(), 1);
EXPECT_EQ(mechanism.reactions.emission[1].products[0].species_name, "B");
EXPECT_EQ(mechanism.reactions.emission[1].products[0].coefficient, 1);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_parse_first_order_loss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ TEST(JsonParser, CanParseValidFirstOrderLossReaction)

EXPECT_EQ(mechanism.reactions.first_order_loss[0].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.first_order_loss[0].name, "my first order loss");
EXPECT_EQ(mechanism.reactions.first_order_loss[0].scaling_factor_, 12.3);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].scaling_factor, 12.3);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].reactants[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.first_order_loss[0].reactants[0].coefficient, 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[0].unknown_properties["__comment"], "\"Strawberries are the superior fruit\"");

EXPECT_EQ(mechanism.reactions.first_order_loss[1].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.first_order_loss[1].scaling_factor_, 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[1].scaling_factor, 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[1].reactants.size(), 1);
EXPECT_EQ(mechanism.reactions.first_order_loss[1].reactants[0].species_name, "C");
EXPECT_EQ(mechanism.reactions.first_order_loss[1].reactants[0].coefficient, 1);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/test_parse_photolysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TEST(JsonParser, CanParseValidPhotolysisReaction)

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].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);
Expand All @@ -25,7 +25,7 @@ TEST(JsonParser, CanParseValidPhotolysisReaction)
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].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);
Expand Down
32 changes: 8 additions & 24 deletions test/unit/test_parse_wet_deposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,17 @@ TEST(JsonParser, CanParseValidWetDepositionReaction)
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/wet_deposition/valid.json"));
EXPECT_EQ(status, ConfigParseStatus::Success);

EXPECT_EQ(mechanism.reactions.wet_deposition.size(), 1);
EXPECT_EQ(mechanism.reactions.wet_deposition.size(), 2);

EXPECT_EQ(mechanism.reactions.wet_deposition[0].name, "my arrhenius");
EXPECT_EQ(mechanism.reactions.wet_deposition[0].gas_phase, "gas");
EXPECT_EQ(mechanism.reactions.wet_deposition[0].name, "rxn cloud");
EXPECT_EQ(mechanism.reactions.wet_deposition[0].aerosol_phase, "cloud");
EXPECT_EQ(mechanism.reactions.wet_deposition[0].scaling_factor, 12.3);
EXPECT_EQ(mechanism.reactions.wet_deposition[0].unknown_properties.size(), 1);
EXPECT_EQ(mechanism.reactions.wet_deposition[0].unknown_properties["__solver_param"], "0.1");
}

TEST(JsonParser, WetDepositionDetectsUnknownSpecies)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/wet_deposition/unknown_species.json"));
EXPECT_EQ(status, ConfigParseStatus::ReactionRequiresUnknownSpecies);
}
EXPECT_EQ(mechanism.reactions.wet_deposition[0].unknown_properties["__comment"], "\"Tuxedo cats are the best\"");

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

TEST(JsonParser, WetDepositionDetectsBadReactionComponent)
{
JsonParser parser;
auto [status, mechanism] = parser.Parse(std::string("unit_configs/reactions/wet_deposition/bad_reaction_component.json"));
EXPECT_EQ(status, ConfigParseStatus::RequiredKeyNotFound);
EXPECT_EQ(mechanism.reactions.wet_deposition[1].name, "rxn cloud2");
EXPECT_EQ(mechanism.reactions.wet_deposition[1].aerosol_phase, "cloud");
EXPECT_EQ(mechanism.reactions.wet_deposition[1].scaling_factor, 1);
}

TEST(JsonParser, WetDepositionDetectsUnknownPhase)
Expand Down
24 changes: 24 additions & 0 deletions test/unit/unit_configs/reactions/wet_deposition/missing_phase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"version": "1.0.0",
"name": "Missing phase",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
}
],
"phases": [
],
"reactions": [
{
"type": "WET_DEPOSITION",
"aerosol phase": "cloud",
"name": "rxn cloud"
}
]
}
39 changes: 39 additions & 0 deletions test/unit/unit_configs/reactions/wet_deposition/valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"version": "1.0.0",
"name": "Valid wet deposition",
"species": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "C"
}
],
"phases": [
{
"name": "cloud",
"species": [
"A",
"B",
"C"
]
}
],
"reactions": [
{
"type": "WET_DEPOSITION",
"aerosol phase": "cloud",
"name": "rxn cloud",
"scaling factor": 12.3,
"__comment": "Tuxedo cats are the best"
},
{
"type": "WET_DEPOSITION",
"aerosol phase": "cloud",
"name": "rxn cloud2"
}
]
}

0 comments on commit bd362e6

Please sign in to comment.