LCOV - code coverage report
Current view: top level - include/utils - ReactionNetworkUtils.h (source / functions) Hit Total Coverage
Test: idaholab/moose chemical_reactions: #32971 (54bef8) with base c6cf66 Lines: 5 6 83.3 %
Date: 2026-05-29 20:35:47 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #pragma once
      11             : 
      12             : #include "MooseTypes.h"
      13             : #include <string>
      14             : #include <optional>
      15             : 
      16             : namespace ReactionNetworkUtils
      17             : {
      18             : // Helper structs for parsing reaction network
      19             : struct Term
      20             : {
      21             :   double coefficient;
      22             :   std::string species;
      23             :   // State is between parenthesis
      24             :   std::optional<std::string> state;
      25             :   std::optional<std::string> charge;
      26             : 
      27             :   // We don't compare the coefficient on purpose, to lump terms with different coefficients
      28             :   // together
      29             :   // NOTE: if you have a need to differentiate terms by terms by coefficients, you'll have
      30             :   // to create a custom comparator
      31         112 :   friend bool operator<(const Term & a, const Term & b) noexcept
      32             :   {
      33         112 :     if (a.species != b.species)
      34          84 :       return a.species < b.species;
      35          28 :     if (a.state != b.state)
      36           0 :       return a.state < b.state;
      37          28 :     return a.charge < b.charge;
      38             :   }
      39             : };
      40             : 
      41             : using TermList = std::vector<Term>;
      42             : using Metadata = std::map<std::string, std::string>;
      43             : 
      44             : struct Reaction
      45             : {
      46             :   TermList reactants;
      47             :   TermList products;
      48             :   Metadata metadata;
      49             : 
      50             :   /// Get all species involved in the reaction
      51             :   std::vector<VariableName> getSpecies() const;
      52             :   /// Get all unique species involved in the reaction
      53             :   /// Note: a species will only appear once even if both a reactant and a product
      54             :   std::vector<VariableName> getUniqueSpecies() const;
      55             : 
      56             :   /// Get the stoeichiometric coefficients for each species in the reaction
      57             :   /// The sign used matches the sign in the reaction equation.
      58             :   std::vector<Real> getStoichiometricCoefficients() const;
      59             :   /// Get the stoeichiometric coefficients for each unique species in the reaction
      60             :   /// Note: if a species is both a reactant and a product,
      61             :   /// the coefficient will be the difference between its product and reactor coefficients
      62             :   /// Note: this means all coefficients for reactants that are not products are negative,
      63             :   /// unlike in getStoichiometricCoefficients()
      64             :   std::map<VariableName, Real> getUniqueStoichiometricCoefficients() const;
      65             : 
      66             :   /// Get all reactant species involved in the reaction (on LHS)
      67             :   std::vector<VariableName> getReactantSpecies() const;
      68             :   /// Get all unique reactant species involved in the reaction (on LHS)
      69             :   std::vector<VariableName> getUniqueReactantSpecies() const;
      70             : 
      71             :   /// Get all product species involved in the reaction (on RHS)
      72             :   std::vector<std::string> getProductSpecies() const;
      73             :   /// Get all unique product species involved in the reaction (on RHS)
      74             :   std::vector<std::string> getUniqueProductSpecies() const;
      75             : 
      76             :   /// Whether the reaction has the metadata with the given type
      77             :   template <typename T>
      78             :   bool hasMetaData(const std::string & key) const;
      79             : 
      80             :   /// Whether the reaction has the metadata
      81             :   bool hasMetaData(const std::string & key) const;
      82             : 
      83             :   /// Get the metadata from the reaction
      84             :   const std::string & getMetaData(const std::string & key) const;
      85             : };
      86             : 
      87             : /// @brief  Parses the reaction network from a string form to a vector a Reaction
      88             : /// @param reaction_network_string
      89             : /// @return
      90             : std::vector<Reaction> parseReactionNetwork(const std::string & reaction_network_string,
      91             :                                            bool output_to_cout);
      92             : }
      93             : 
      94             : namespace Moose
      95             : {
      96             : std::string stringify(const ReactionNetworkUtils::Reaction & t);
      97             : }

Generated by: LCOV version 1.14