Skip to content

Commit

Permalink
Merge pull request #255 from NCAR/config_parser
Browse files Browse the repository at this point in the history
Config parser
  • Loading branch information
dwfncar committed Sep 27, 2023
2 parents 6e7e991 + 5154fb5 commit 07680a9
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 2 deletions.
34 changes: 32 additions & 2 deletions include/micm/configure/solver_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace micm
InvalidKey,
UnknownKey,
InvalidSpecies,
CAMPFilesSectionNotFound,
InvalidCAMPFileCount,
CAMPDataSectionNotFound,
InvalidMechanism,
ObjectTypeNotFound,
Expand All @@ -51,6 +53,8 @@ namespace micm
case ConfigParseStatus::InvalidKey: return "InvalidKey";
case ConfigParseStatus::UnknownKey: return "UnknownKey";
case ConfigParseStatus::InvalidSpecies: return "InvalidSpecies";
case ConfigParseStatus::CAMPFilesSectionNotFound: return "CAMPFilesSectionNotFound";
case ConfigParseStatus::InvalidCAMPFileCount: return "InvalidCAMPFileCount";
case ConfigParseStatus::CAMPDataSectionNotFound: return "CAMPDataSectionNotFound";
case ConfigParseStatus::InvalidMechanism: return "InvalidMechanism";
case ConfigParseStatus::ObjectTypeNotFound: return "ObjectTypeNotFound";
Expand Down Expand Up @@ -103,13 +107,15 @@ namespace micm

// Constants
// Configure files
static const inline std::string CAMP_CONFIG = "config.json";
static const inline std::string SPECIES_CONFIG = "species.json";
static const inline std::string MECHANISM_CONFIG = "mechanism.json";
static const inline std::string REACTIONS_CONFIG = "reactions.json";
static const inline std::string TOLERANCE_CONFIG = "tolerance.json";

// Common JSON
static const inline std::string CAMP_DATA = "camp-data";
static const inline std::string CAMP_FILES = "camp-files";
static const inline std::string TYPE = "type";

// Functions
Expand All @@ -122,9 +128,33 @@ namespace micm
std::filesystem::path species_config(config_dir / SPECIES_CONFIG);
std::filesystem::path mechanism_config(config_dir / MECHANISM_CONFIG);
std::filesystem::path reactions_config(config_dir / REACTIONS_CONFIG);
// Note tolerance_config is defined here but not used
std::filesystem::path tolerance_config(config_dir / TOLERANCE_CONFIG);

// Current reaction configs should be either mechanism_config or reactions config
// Look for CAMP config file
std::filesystem::path camp_config(config_dir / CAMP_CONFIG);
if (std::filesystem::exists(camp_config))
{
json camp_data = json::parse(std::ifstream(camp_config));
if (!camp_data.contains(CAMP_FILES))
return ConfigParseStatus::CAMPFilesSectionNotFound;

std::vector<std::string> camp_files;
for (const auto& element : camp_data[CAMP_FILES])
{
camp_files.push_back(element.get<std::string>());
}
if (camp_files.size() != 2) {
std::string err_msg = "CAMP file list should contain two files [species.json, mechanism.json]";
std::cerr << err_msg << std::endl;
return ConfigParseStatus::InvalidCAMPFileCount;
}
// As a temporary implementation, assume camp files are ordered
species_config = config_dir / camp_files[0];
mechanism_config = config_dir / camp_files[1];
}

// Current reaction configs should be either mechanism_config or reactions_config
std::filesystem::path cur_reactions_config;

// Check if species config exists
Expand Down Expand Up @@ -784,4 +814,4 @@ namespace micm
}
};

} // namespace micm
} // namespace micm
22 changes: 22 additions & 0 deletions test/unit/configure/test_solver_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ TEST(SolverConfig, DetectsInvalidConfigFile)
EXPECT_EQ(micm::ConfigParseStatus::InvalidSpeciesFilePath, status);
}

TEST(SolverConfig, ReadAndParseCAMPFiles)
{
micm::SolverConfig solverConfig{};

// Read and parse the CAMP configure file
micm::ConfigParseStatus status = solverConfig.ReadAndParse("./unit_configs/CAMP/camp_valid");
EXPECT_EQ(micm::ConfigParseStatus::Success, status);
}

TEST(SolverConfig, DetectsInvalidCAMPConfigFile)
{
micm::SolverConfig solverConfig{};

// Read and parse the CAMP configure file
micm::ConfigParseStatus status;
status = solverConfig.ReadAndParse("./unit_configs/CAMP/camp_invalid");
EXPECT_EQ(micm::ConfigParseStatus::InvalidCAMPFileCount, status);

status = solverConfig.ReadAndParse("./unit_configs/CAMP/camp_no_files_key");
EXPECT_EQ(micm::ConfigParseStatus::CAMPFilesSectionNotFound, status);
}

TEST(SolverConfig, ReadAndParseSystemObject)
{
micm::SolverConfig solverConfig;
Expand Down
1 change: 1 addition & 0 deletions test/unit/unit_configs/CAMP/camp_invalid/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-files": ["camp_species.json", "camp_mechanism.json", "other.json"]}
1 change: 1 addition & 0 deletions test/unit/unit_configs/CAMP/camp_no_files_key/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"wrong-key": ["camp_species.json", "camp_mechanism.json"]}
134 changes: 134 additions & 0 deletions test/unit/unit_configs/CAMP/camp_valid/camp_mechanism.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
{
"camp-data" : [
{
"name" : "Chapman",
"type" : "MECHANISM",
"reactions" : [
{
"type" : "PHOTOLYSIS",
"reactants" : {
"O2" : { }
},
"products" : {
"O" : { "yield" : 2.0 }
},
"MUSICA name" : "O2_1"
},
{
"type" : "PHOTOLYSIS",
"reactants" : {
"O3" : { }
},
"products" : {
"O1D" : { },
"O2" : { }
},
"MUSICA name" : "O3_1"
},
{
"type" : "PHOTOLYSIS",
"reactants" : {
"O3" : { }
},
"products" : {
"O" : { },
"O2" : { }
},
"MUSICA name" : "O3_2"
},
{
"type" : "ARRHENIUS",
"reactants" : {
"O1D" : { },
"N2" : { }
},
"products" : {
"O" : { },
"N2" : { }
},
"A" : 2.15e-11,
"C" : 110.0
},
{
"type" : "ARRHENIUS",
"reactants" : {
"O1D" : { },
"O2" : { }
},
"products" : {
"O" : { },
"O2" : { }
},
"A" : 3.3e-11,
"C" : 55.0
},
{
"type" : "ARRHENIUS",
"reactants" : {
"O" : { },
"O3" : { }
},
"products" : {
"O2" : { "yield" : 2.0 }
},
"A" : 8.0e-12,
"C" : -2060.00
},
{
"type" : "ARRHENIUS",
"reactants" : {
"O" : { },
"O2" : { },
"M" : { }
},
"products" : {
"O3" : { },
"M" : { }
},
"A" : 6.0e-34,
"B" : 2.4
},
{
"type" : "EMISSION",
"species" : "O1D",
"MUSICA name" : "O1D"
},
{
"type" : "EMISSION",
"species" : "O",
"MUSICA name" : "O"
},
{
"type" : "EMISSION",
"species" : "O3",
"MUSICA name" : "O3"
},
{
"type" : "FIRST_ORDER_LOSS",
"species" : "N2",
"MUSICA name" : "N2"
},
{
"type" : "FIRST_ORDER_LOSS",
"species" : "O2",
"MUSICA name" : "O2"
},
{
"type" : "FIRST_ORDER_LOSS",
"species" : "CO2",
"MUSICA name" : "CO2"
},
{
"type" : "FIRST_ORDER_LOSS",
"species" : "Ar",
"MUSICA name" : "Ar"
},
{
"type" : "FIRST_ORDER_LOSS",
"species" : "H2O",
"MUSICA name" : "H2O"
}
]
}
]
}
53 changes: 53 additions & 0 deletions test/unit/unit_configs/CAMP/camp_valid/camp_species.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"camp-data" : [
{
"type" : "RELATIVE_TOLERANCE",
"value" : 1.0e-4
},
{
"name" : "M",
"type" : "CHEM_SPEC",
"tracer type" : "CONSTANT"
},
{
"name" : "Ar",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
},
{
"name" : "CO2",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
},
{
"name" : "H2O",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
},
{
"name" : "N2",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
},
{
"name" : "O1D",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
},
{
"name" : "O",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
},
{
"name" : "O2",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
},
{
"name" : "O3",
"type" : "CHEM_SPEC",
"absolute tolerance" : 1.0e-12
}
]
}
1 change: 1 addition & 0 deletions test/unit/unit_configs/CAMP/camp_valid/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-files": ["camp_species.json", "camp_mechanism.json"]}

0 comments on commit 07680a9

Please sign in to comment.