Skip to content
New issue

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

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

Already on GitHub? # to your account

Third body species are not properly used #613

Closed
K20shores opened this issue Aug 7, 2024 · 0 comments · Fixed by #614
Closed

Third body species are not properly used #613

K20shores opened this issue Aug 7, 2024 · 0 comments · Fixed by #614
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@K20shores
Copy link
Collaborator

K20shores commented Aug 7, 2024

In #551 we added support for species to be labeled as third-body, meaning a species that is non reacting, whose concentration doesn't change, but can still be present in a list of products or reactants.

However, if you set any of these M species in our examples as a third body by setting them to something like

        {
            "name": "M",
            "type": "CHEM_SPEC",
            "tracer type": "THIRD_BODY"
        },

{
"name": "M",
"type": "CHEM_SPEC",
"__description": "third-body species"
}

{
"name": "M",
"type": "CHEM_SPEC",
"tracer type": "CONSTANT"
},

{
"name": "M",
"type": "CHEM_SPEC"
},

{
"name" : "M",
"type" : "CHEM_SPEC",
"tracer type" : "CONSTANT"
},

we end up with test failures

94% tests passed, 4 tests failed out of 69

Total Test time (real) =  17.88 sec

The following tests FAILED:
         49 - chapman_integration (Failed)
         66 - carbon_bond_5_example (Subprocess aborted)
         67 - chapman_example (Subprocess aborted)
         69 - ts1_example (Subprocess aborted)
Errors while running CTest

Each of them fails with the same message

:: ctest -R chapman_integration --verbose
49: [ RUN      ] ChapmanIntegration.CanBuildChapmanSystemUsingConfig
49: unknown file: Failure
49: C++ exception with description "M: Reactant does not exist" thrown in the test body.

This is the same failure that we see in music box when trying to set a third body species.

The reason this happens is that products and reactants are read in as names from their reaction lists and turned into Species objects, which look like our regular species.

std::vector<Species> ParseReactants(const json& object)
{
const std::string QTY = "qty";
std::vector<Species> reactants;
for (auto& [key, value] : object.items())
{
std::size_t qty = 1;
ValidateSchema(value, {}, { "qty" });
if (value.contains(QTY))
qty = value[QTY];
for (std::size_t i = 0; i < qty; ++i)
reactants.push_back(Species(key));
}
return reactants;
}
std::vector<std::pair<Species, double>> ParseProducts(const json& object)
{
const std::string YIELD = "yield";
constexpr double DEFAULT_YIELD = 1.0;
std::vector<std::pair<Species, double>> products;
for (auto& [key, value] : object.items())
{
ValidateSchema(value, {}, { "yield" });
if (value.contains(YIELD))
{
products.push_back(std::make_pair(Species(key), value[YIELD]));
}
else
{
products.push_back(std::make_pair(Species(key), DEFAULT_YIELD));
}
}
return products;
}

The problem is that a species being a third body is only known when reading from the species list.

void ParseChemicalSpecies(const json& object)
{
// required keys
const std::string NAME = "name";
const std::string TYPE = "type";
const std::string TRACER_TYPE = "tracer type";
const std::string ABS_TOLERANCE = "absolute tolerance";
const std::string DIFFUSION_COEFF = "diffusion coefficient [m2 s-1]";
const std::string MOL_WEIGHT = "molecular weight [kg mol-1]";
const std::string THIRD_BODY = "THIRD_BODY";
ValidateSchema(object, { NAME, TYPE }, { TRACER_TYPE, ABS_TOLERANCE, DIFFUSION_COEFF, MOL_WEIGHT });
std::string name = object[NAME].get<std::string>();
Species species{ name };
// Load remaining keys as properties
for (auto& [key, value] : object.items())
{
if (key != NAME && key != TYPE)
{
if (value.is_string())
{
if (key == TRACER_TYPE && value == THIRD_BODY)
{
species.SetThirdBody();
}

When we indicate that a species is a third body, we set a value called parameterize_ which holds a lambda function to return the air density, which is the value that needs to be used for the concentration of a third body species when calculating reaction rates.

But, we don't know that M, or whatever species is marked as third body, should be a third body species when parsing the reactants and products.

The error occurs when we try to build a solver which has read in a configuration with a third body species present. Specifically it fails during construction of the process set. In the snippet below, reactant will eventually be M, but because it was read from the reactant or product list, it will never have been set as a parameterized species. And, because variable_map contains only the non-parameterized species, we will throw an error on line 148.

{
MICM_PROFILE_FUNCTION();
for (auto& process : processes)
{
std::size_t number_of_reactants = 0;
std::size_t number_of_products = 0;
for (auto& reactant : process.reactants_)
{
if (reactant.IsParameterized())
continue; // Skip reactants that are parameterizations
if (variable_map.count(reactant.name_) < 1)
throw std::system_error(make_error_code(MicmProcessSetErrc::ReactantDoesNotExist), reactant.name_);
reactant_ids_.push_back(variable_map.at(reactant.name_));
++number_of_reactants;
}
for (auto& product : process.products_)
{
if (product.first.IsParameterized())
continue; // Skip products that are parameterizations
if (variable_map.count(product.first.name_) < 1)
throw std::system_error(make_error_code(MicmProcessSetErrc::ProductDoesNotExist), product.first.name_);
product_ids_.push_back(variable_map.at(product.first.name_));
yields_.push_back(product.second);
++number_of_products;
}
number_of_reactants_.push_back(number_of_reactants);
number_of_products_.push_back(number_of_products);
}
};

Acceptance criteria

  • All configurations used in our tests with M have M set as a third body species
  • Configurations can build and run

Ideas

  • Remove the idea of the third body species entirely, or
  • In the configuration reader, use a map of strings to species and lookup the species to set as a reactant or product when constructing the products or reactants
@K20shores K20shores added bug Something isn't working good first issue Good for newcomers labels Aug 7, 2024
@K20shores K20shores self-assigned this Aug 7, 2024
@K20shores K20shores linked a pull request Aug 7, 2024 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant