22// SPDX-License-Identifier: Apache-2.0
33#pragma once
44
5- #include < micm/constraint/equilibrium_constraint.hpp>
6- #include < micm/constraint/linear_constraint.hpp>
5+ #include < micm/constraint/types/equilibrium_constraint.hpp>
6+ #include < micm/constraint/types/linear_constraint.hpp>
7+ #include < micm/constraint/constraint_info.hpp>
78
89#include < cstddef>
910#include < string>
@@ -42,9 +43,16 @@ namespace micm
4243 return std::visit ([](const auto & c) { return c.name_ ; }, constraint_);
4344 }
4445
46+ // / @brief Returns the species whose state row should be replaced by this algebraic constraint
47+ // / @return Algebraic species name
48+ const std::string& AlgebraicSpecies () const
49+ {
50+ return std::visit ([](const auto & c) -> const std::string& { return c.AlgebraicSpecies (); }, constraint_);
51+ }
52+
4553 // / @brief Get species dependencies
4654 // / @return Vector of species names this constraint depends on
47- const std::vector<std::string>& GetSpeciesDependencies () const
55+ const std::vector<std::string>& SpeciesDependencies () const
4856 {
4957 return std::visit ([](const auto & c) -> const std::vector<std::string>& { return c.species_dependencies_ ; }, constraint_);
5058 }
@@ -56,29 +64,41 @@ namespace micm
5664 return std::visit ([](const auto & c) { return c.species_dependencies_ .size (); }, constraint_);
5765 }
5866
59- // / @brief Evaluate the constraint residual G(y)
60- // / @param concentrations Pointer to species concentrations (row of state matrix)
61- // / @param indices Pointer to indices mapping species_dependencies_ to positions in concentrations
62- // / @return Residual value (should be 0 when constraint is satisfied)
63- double Residual (const double * concentrations, const std::size_t * indices) const
67+ // / @brief Get a function object to compute the constraint residual
68+ // / This returns a reusable function that can be invoked multiple times
69+ // / @param info Constraint information including species indices and row index
70+ // / @param state_variable_indices Map from species names to state variable indices
71+ // / @return Function object that takes (state_variables, forcing) and computes the residual
72+ template <typename DenseMatrixPolicy>
73+ auto ResidualFunction (const ConstraintInfo& info, const auto & state_variable_indices) const
6474 {
65- return std::visit ([&](const auto & c) { return c.Residual (concentrations, indices); }, constraint_);
75+ return std::visit (
76+ [&info, &state_variable_indices](const auto & c) {
77+ return c.template ResidualFunction <DenseMatrixPolicy>(info, state_variable_indices);
78+ },
79+ constraint_);
6680 }
6781
68- // / @brief Compute partial derivatives dG/d[species] for each dependent species
69- // / @param concentrations Pointer to species concentrations (row of state matrix)
70- // / @param indices Pointer to indices mapping species_dependencies_ to positions in concentrations
71- // / @param jacobian Output buffer for partial derivatives dG/d[species] (same order as species_dependencies_)
72- void Jacobian (const double * concentrations, const std::size_t * indices, double * jacobian) const
82+ // / @brief Get a function object to compute the constraint Jacobian
83+ // / This returns a reusable function that can be invoked multiple times
84+ // / @param info Constraint information including species indices and Jacobian flat IDs
85+ // / @param state_variable_indices Map from species names to state variable indices
86+ // / @param jacobian_flat_ids Iterator to the jacobian flat IDs for this constraint
87+ // / @param jacobian Sparse matrix to store Jacobian values
88+ // / @return Function object that takes (state_variables, jacobian) and computes partials
89+ template <typename DenseMatrixPolicy, typename SparseMatrixPolicy>
90+ auto JacobianFunction (
91+ const ConstraintInfo& info,
92+ const auto & state_variable_indices,
93+ auto jacobian_flat_ids,
94+ SparseMatrixPolicy& jacobian) const
7395 {
74- std::visit ([&](const auto & c) { c.Jacobian (concentrations, indices, jacobian); }, constraint_);
75- }
76-
77- // / @brief Returns the species whose state row should be replaced by this algebraic constraint
78- // / @return Algebraic species name
79- const std::string& GetAlgebraicSpecies () const
80- {
81- return std::visit ([](const auto & c) -> const std::string& { return c.AlgebraicSpecies (); }, constraint_);
96+ return std::visit (
97+ [&info, &state_variable_indices, jacobian_flat_ids, &jacobian](const auto & c) {
98+ return c.template JacobianFunction <DenseMatrixPolicy, SparseMatrixPolicy>(
99+ info, state_variable_indices, jacobian_flat_ids, jacobian);
100+ },
101+ constraint_);
82102 }
83103 };
84104
0 commit comments