Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

164 prevent non standard keys in json #317

Merged
merged 9 commits into from
Oct 25, 2023
Merged
283 changes: 223 additions & 60 deletions include/micm/configure/solver_config.hpp

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions test/unit/configure/process/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ include(test_util)
################################################################################
# Tests

create_standard_test(NAME arrhenius_config SOURCES test_arrhenius_config.cpp)
create_standard_test(NAME branched_config SOURCES test_branched_config.cpp)
create_standard_test(NAME emission_config SOURCES test_emission_config.cpp)
create_standard_test(NAME first_order_loss_config SOURCES test_first_order_loss_config.cpp)
create_standard_test(NAME photolysis_config SOURCES test_photolysis_config.cpp)
create_standard_test(NAME surface_config SOURCES test_surface_config.cpp)
create_standard_test(NAME ternary_chemical_activation_config SOURCES test_ternary_chemical_activation_config.cpp)
create_standard_test(NAME troe_config SOURCES test_troe_config.cpp)
create_standard_test(NAME tunneling_config SOURCES test_tunneling_config.cpp)
114 changes: 114 additions & 0 deletions test/unit/configure/process/test_arrhenius_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <gtest/gtest.h>

#include <micm/configure/solver_config.hpp>

TEST(ArrheniusConfig, DetectsInvalidConfig)
{
micm::SolverConfig solver_config;

// Read and parse the configure files
micm::ConfigParseStatus status =
solver_config.ReadAndParse("./unit_configs/process/arrhenius/missing_reactants");
EXPECT_EQ(micm::ConfigParseStatus::RequiredKeyNotFound, status);
status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/missing_products");
EXPECT_EQ(micm::ConfigParseStatus::RequiredKeyNotFound, status);
status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/mutually_exclusive");
EXPECT_EQ(micm::ConfigParseStatus::MutuallyExclusiveOption, status);
}

TEST(ArrheniusConfig, ParseConfig)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/valid");
EXPECT_EQ(micm::ConfigParseStatus::Success, status);

micm::SolverParameters solver_params = solver_config.GetSolverParams();

auto& process_vector = solver_params.processes_;

// first reaction
{
EXPECT_EQ(process_vector[0].reactants_.size(), 3);
EXPECT_EQ(process_vector[0].reactants_[0].name_, "foo");
EXPECT_EQ(process_vector[0].reactants_[1].name_, "quz");
EXPECT_EQ(process_vector[0].reactants_[2].name_, "quz");
EXPECT_EQ(process_vector[0].products_.size(), 2);
EXPECT_EQ(process_vector[0].products_[0].first.name_, "bar");
EXPECT_EQ(process_vector[0].products_[0].second, 1.0);
EXPECT_EQ(process_vector[0].products_[1].first.name_, "baz");
EXPECT_EQ(process_vector[0].products_[1].second, 3.2);
micm::ArrheniusRateConstant* ternary_rate_constant =
dynamic_cast<micm::ArrheniusRateConstant*>(process_vector[0].rate_constant_.get());
auto& params = ternary_rate_constant->parameters_;
EXPECT_EQ(params.A_, 1.0);
EXPECT_EQ(params.B_, 0.0);
EXPECT_EQ(params.C_, 0.0);
EXPECT_EQ(params.D_, 300);
EXPECT_EQ(params.E_, 0.0);
}

// second reaction
{
EXPECT_EQ(process_vector[1].reactants_.size(), 2);
EXPECT_EQ(process_vector[1].reactants_[0].name_, "bar");
EXPECT_EQ(process_vector[1].reactants_[1].name_, "baz");
EXPECT_EQ(process_vector[1].products_.size(), 2);
EXPECT_EQ(process_vector[1].products_[0].first.name_, "bar");
EXPECT_EQ(process_vector[1].products_[0].second, 0.5);
EXPECT_EQ(process_vector[1].products_[1].first.name_, "foo");
EXPECT_EQ(process_vector[1].products_[1].second, 1.0);
micm::ArrheniusRateConstant* ternary_rate_constant =
dynamic_cast<micm::ArrheniusRateConstant*>(process_vector[1].rate_constant_.get());
auto& params = ternary_rate_constant->parameters_;
EXPECT_EQ(params.A_, 32.1);
EXPECT_EQ(params.B_, -2.3);
EXPECT_EQ(params.C_, 102.3);
EXPECT_EQ(params.D_, 63.4);
EXPECT_EQ(params.E_, -1.3);
}

// third reaction
{
EXPECT_EQ(process_vector[2].reactants_.size(), 2);
EXPECT_EQ(process_vector[2].reactants_[0].name_, "bar");
EXPECT_EQ(process_vector[2].reactants_[1].name_, "baz");
EXPECT_EQ(process_vector[2].products_.size(), 2);
EXPECT_EQ(process_vector[2].products_[0].first.name_, "bar");
EXPECT_EQ(process_vector[2].products_[0].second, 0.5);
EXPECT_EQ(process_vector[2].products_[1].first.name_, "foo");
EXPECT_EQ(process_vector[2].products_[1].second, 1.0);
micm::ArrheniusRateConstant* ternary_rate_constant =
dynamic_cast<micm::ArrheniusRateConstant*>(process_vector[2].rate_constant_.get());
auto& params = ternary_rate_constant->parameters_;
EXPECT_EQ(params.A_, 32.1);
EXPECT_EQ(params.B_, -2.3);
EXPECT_EQ(params.C_, -1 * 2e23 / BOLTZMANN_CONSTANT);
EXPECT_EQ(params.D_, 63.4);
EXPECT_EQ(params.E_, -1.3);
}
}

TEST(ArrheniusConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(ArrheniusConfig, DetectsNonstandardProductCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/nonstandard_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(ArrheniusConfig, DetectsNonstandardReactantCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/nonstandard_reactant_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
27 changes: 27 additions & 0 deletions test/unit/configure/process/test_branched_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,30 @@ TEST(BranchedConfig, ParseConfig)
EXPECT_EQ(params.branch_, micm::BranchedRateConstantParameters::Branch::Nitrate);
}
}

TEST(BranchedConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/branched/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(BranchedConfig, DetectsNonstandardProductCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/branched/nonstandard_alkoxy_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);

status = solver_config.ReadAndParse("./unit_configs/process/branched/nonstandard_nitrate_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(BranchedConfig, DetectsNonstandardReactantCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/branched/nonstandard_reactant_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
8 changes: 8 additions & 0 deletions test/unit/configure/process/test_emission_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ TEST(EmissionConfig, ParseConfig)
EXPECT_EQ(emission_rate_constant->SizeCustomParameters(), 1);
EXPECT_EQ(emission_rate_constant->CustomParameters()[0], "EMIS.bar");
}
}

TEST(EmissionConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/emission/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
8 changes: 8 additions & 0 deletions test/unit/configure/process/test_first_order_loss_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ TEST(FirstOrderLossConfig, ParseConfig)
EXPECT_EQ(first_order_loss_rate_constant->SizeCustomParameters(), 1);
EXPECT_EQ(first_order_loss_rate_constant->CustomParameters()[0], "LOSS.bar");
}
}

TEST(FirstOrderLossConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/first_order_loss/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
24 changes: 24 additions & 0 deletions test/unit/configure/process/test_photolysis_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,28 @@ TEST(PhotolysisConfig, ParseConfig)
EXPECT_EQ(photo_rate_constant->SizeCustomParameters(), 1);
EXPECT_EQ(photo_rate_constant->CustomParameters()[0], "PHOTO.jbar");
}
}

TEST(PhotolysisConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/photolysis/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(PhotolysisConfig, DetectsNonstandardProductCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/photolysis/nonstandard_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(PhotolysisConfig, DetectsNonstandardReactantCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/photolysis/nonstandard_reactant_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
17 changes: 17 additions & 0 deletions test/unit/configure/process/test_surface_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,20 @@ TEST(SurfaceConfig, ParseConfig)
EXPECT_EQ(surface_rate_constant->mean_free_speed_factor_, 8.0 * GAS_CONSTANT / (M_PI * 0.321));
}
}


TEST(SurfaceConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/surface/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(SurfaceConfig, DetectsNonstandardProductCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/surface/nonstandard_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ TEST(TernaryChemicalActivationConfig, ParseConfig)
EXPECT_EQ(params.N_, 32.1);
}
}

TEST(TernaryChemicalActivationConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/ternary_chemical_activation/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(TernaryChemicalActivationConfig, DetectsNonstandardProductCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/ternary_chemical_activation/nonstandard_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(TernaryChemicalActivationConfig, DetectsNonstandardReactantCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/ternary_chemical_activation/nonstandard_reactant_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
98 changes: 98 additions & 0 deletions test/unit/configure/process/test_troe_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <gtest/gtest.h>

#include <micm/configure/solver_config.hpp>

TEST(TroeConfig, DetectsInvalidConfig)
{
micm::SolverConfig solver_config;

// Read and parse the configure files
micm::ConfigParseStatus status =
solver_config.ReadAndParse("./unit_configs/process/troe/missing_reactants");
EXPECT_EQ(micm::ConfigParseStatus::RequiredKeyNotFound, status);
status = solver_config.ReadAndParse("./unit_configs/process/troe/missing_products");
EXPECT_EQ(micm::ConfigParseStatus::RequiredKeyNotFound, status);
}

TEST(TroeConfig, ParseConfig)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/troe/valid");
EXPECT_EQ(micm::ConfigParseStatus::Success, status);

micm::SolverParameters solver_params = solver_config.GetSolverParams();

auto& process_vector = solver_params.processes_;

// first reaction
{
EXPECT_EQ(process_vector[0].reactants_.size(), 3);
EXPECT_EQ(process_vector[0].reactants_[0].name_, "foo");
EXPECT_EQ(process_vector[0].reactants_[1].name_, "quz");
EXPECT_EQ(process_vector[0].reactants_[2].name_, "quz");
EXPECT_EQ(process_vector[0].products_.size(), 2);
EXPECT_EQ(process_vector[0].products_[0].first.name_, "bar");
EXPECT_EQ(process_vector[0].products_[0].second, 1.0);
EXPECT_EQ(process_vector[0].products_[1].first.name_, "baz");
EXPECT_EQ(process_vector[0].products_[1].second, 3.2);
micm::TroeRateConstant* ternary_rate_constant =
dynamic_cast<micm::TroeRateConstant*>(process_vector[0].rate_constant_.get());
auto& params = ternary_rate_constant->parameters_;
EXPECT_EQ(params.k0_A_, 1.0);
EXPECT_EQ(params.k0_B_, 0.0);
EXPECT_EQ(params.k0_C_, 0.0);
EXPECT_EQ(params.kinf_A_, 1.0);
EXPECT_EQ(params.kinf_B_, 0.0);
EXPECT_EQ(params.kinf_C_, 0.0);
EXPECT_EQ(params.Fc_, 0.6);
EXPECT_EQ(params.N_, 1.0);
}

// second reaction
{
EXPECT_EQ(process_vector[1].reactants_.size(), 2);
EXPECT_EQ(process_vector[1].reactants_[0].name_, "bar");
EXPECT_EQ(process_vector[1].reactants_[1].name_, "baz");
EXPECT_EQ(process_vector[1].products_.size(), 2);
EXPECT_EQ(process_vector[1].products_[0].first.name_, "bar");
EXPECT_EQ(process_vector[1].products_[0].second, 0.5);
EXPECT_EQ(process_vector[1].products_[1].first.name_, "foo");
EXPECT_EQ(process_vector[1].products_[1].second, 1.0);
micm::TroeRateConstant* ternary_rate_constant =
dynamic_cast<micm::TroeRateConstant*>(process_vector[1].rate_constant_.get());
auto& params = ternary_rate_constant->parameters_;
EXPECT_EQ(params.k0_A_, 32.1);
EXPECT_EQ(params.k0_B_, -2.3);
EXPECT_EQ(params.k0_C_, 102.3);
EXPECT_EQ(params.kinf_A_, 63.4);
EXPECT_EQ(params.kinf_B_, -1.3);
EXPECT_EQ(params.kinf_C_, 908.5);
EXPECT_EQ(params.Fc_, 1.3);
EXPECT_EQ(params.N_, 32.1);
}
}

TEST(TroeConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/troe/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(TroeConfig, DetectsNonstandardProductCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/troe/nonstandard_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(TroeConfig, DetectsNonstandardReactantCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/troe/nonstandard_reactant_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
24 changes: 24 additions & 0 deletions test/unit/configure/process/test_tunneling_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,27 @@ TEST(TunnelingConfig, ParseConfig)
EXPECT_EQ(params.C_, 102.3);
}
}

TEST(TunnelingConfig, DetectsNonstandardKeys)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/tunneling/contains_nonstandard_key");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(TunnelingConfig, DetectsNonstandardProductCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/nonstandard_product_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}

TEST(TunnelingConfig, DetectsNonstandardReactantCoefficient)
{
micm::SolverConfig solver_config;

micm::ConfigParseStatus status = solver_config.ReadAndParse("./unit_configs/process/arrhenius/nonstandard_reactant_coef");
EXPECT_EQ(micm::ConfigParseStatus::ContainsNonStandardKey, status);
}
Loading