Skip to content

Commit 5936037

Browse files
committed
contains all the changes
1 parent ad00ab7 commit 5936037

36 files changed

+2507
-1739
lines changed

include/micm/CPU.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ namespace micm
2222
using StandardState = State<DenseMatrixStandard, SparseMatrixStandard>;
2323

2424
using RosenbrockVectorType = typename RosenbrockSolverParameters::
25-
template SolverType<ProcessSet<DenseMatrixVector, SparseMatrixVector>, LinearSolver<SparseMatrixVector, LuDecomposition>>;
25+
template SolverType<ProcessSet<DenseMatrixVector, SparseMatrixVector>, LinearSolver<SparseMatrixVector, LuDecomposition>, ConstraintSet<DenseMatrixVector, SparseMatrixVector>>;
2626
using Rosenbrock = Solver<RosenbrockVectorType, State<DenseMatrixVector, SparseMatrixVector>>;
2727

2828
using RosenbrockStandardType = typename RosenbrockSolverParameters::
29-
template SolverType<ProcessSet<DenseMatrixStandard, SparseMatrixStandard>, LinearSolver<SparseMatrixStandard, LuDecomposition>>;
29+
template SolverType<ProcessSet<DenseMatrixStandard, SparseMatrixStandard>, LinearSolver<SparseMatrixStandard, LuDecomposition>, ConstraintSet<DenseMatrixStandard, SparseMatrixStandard>>;
3030
using RosenbrockStandard = Solver<RosenbrockStandardType, State<DenseMatrixStandard, SparseMatrixStandard>>;
3131

3232
using BackwardEulerVectorType = typename BackwardEulerSolverParameters::
33-
template SolverType<ProcessSet<DenseMatrixVector, SparseMatrixVector>, LinearSolver<SparseMatrixVector, LuDecomposition>>;
33+
template SolverType<ProcessSet<DenseMatrixVector, SparseMatrixVector>, LinearSolver<SparseMatrixVector, LuDecomposition>, ConstraintSet<DenseMatrixVector, SparseMatrixVector>>;
3434
using BackwardEuler = Solver<BackwardEulerVectorType, State<DenseMatrixVector, SparseMatrixVector>>;
3535

3636
using BackwardEulerStandardType = typename BackwardEulerSolverParameters::
37-
template SolverType<ProcessSet<DenseMatrixStandard, SparseMatrixStandard>, LinearSolver<SparseMatrixStandard, LuDecomposition>>;
37+
template SolverType<ProcessSet<DenseMatrixStandard, SparseMatrixStandard>, LinearSolver<SparseMatrixStandard, LuDecomposition>, ConstraintSet<DenseMatrixStandard, SparseMatrixStandard>>;
3838
using BackwardEulerStandard = Solver<BackwardEulerStandardType, State<DenseMatrixStandard, SparseMatrixStandard>>;
3939

4040
using RosenbrockThreeStageBuilder = CpuSolverBuilder<RosenbrockSolverParameters, DenseMatrixVector, SparseMatrixVector>;

include/micm/Constraint.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
#pragma once
44

5+
#include <micm/constraint/types/equilibrium_constraint.hpp>
6+
#include <micm/constraint/types/linear_constraint.hpp>
57
#include <micm/constraint/constraint.hpp>
6-
#include <micm/constraint/constraint_set.hpp>
7-
#include <micm/constraint/equilibrium_constraint.hpp>
8+
#include <micm/constraint/constraint_set.hpp>

include/micm/GPU.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <micm/cuda/util/cuda_param.hpp>
2121
#include <micm/cuda/util/cuda_sparse_matrix.hpp>
2222
#include <micm/cuda/util/cuda_util.cuh>
23+
#include <micm/constraint/constraint_set.hpp>
2324
#include <micm/solver/solver_builder.hpp>
2425

2526
namespace micm
@@ -30,7 +31,7 @@ namespace micm
3031
using GpuState = CudaState<CudaDenseMatrixVector, CudaSparseMatrixVector, CudaLuDecompositionMozartInPlace>;
3132

3233
using CudaRosenbrockVectorType = typename CudaRosenbrockSolverParameters::
33-
template SolverType<CudaProcessSet<CudaDenseMatrixVector, CudaSparseMatrixVector>, CudaLinearSolverInPlace<CudaSparseMatrixVector>>;
34+
template SolverType<CudaProcessSet<CudaDenseMatrixVector, CudaSparseMatrixVector>, CudaLinearSolverInPlace<CudaSparseMatrixVector>, ConstraintSet<CudaDenseMatrixVector, CudaSparseMatrixVector>>;
3435
using CudaRosenbrock = Solver<CudaRosenbrockVectorType, GpuState>;
3536

3637
using GpuRosenbrockThreeStageBuilder = CudaSolverBuilderInPlace<CudaRosenbrockSolverParameters>;

include/micm/constraint/constraint.hpp

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
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

include/micm/constraint/constraint_error.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum class MicmConstraintErrc
1414
EmptyReactants = MICM_CONSTRAINT_ERROR_CODE_EMPTY_REACTANTS,
1515
EmptyProducts = MICM_CONSTRAINT_ERROR_CODE_EMPTY_PRODUCTS,
1616
InvalidStoichiometry = MICM_CONSTRAINT_ERROR_CODE_INVALID_STOICHIOMETRY,
17+
DuplicateAlgebraicSpecies = MICM_CONSTRAINT_ERROR_CODE_DUPLICATE_ALGEBRAIC_SPECIES,
1718
};
1819

1920
namespace std
@@ -41,6 +42,7 @@ class MicmConstraintErrorCategory : public std::error_category
4142
case MicmConstraintErrc::EmptyReactants: return "Equilibrium constraint requires at least one reactant";
4243
case MicmConstraintErrc::EmptyProducts: return "Equilibrium constraint requires at least one product";
4344
case MicmConstraintErrc::InvalidStoichiometry: return "Stoichiometric coefficients must be positive";
45+
case MicmConstraintErrc::DuplicateAlgebraicSpecies: return "Multiple constraints map to the same algebraic species";
4446
default: return "Unknown error";
4547
}
4648
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (C) 2023-2026 University Corporation for Atmospheric Research
2+
// SPDX-License-Identifier: Apache-2.0
3+
#pragma once
4+
5+
#include <vector>
6+
#include <cstddef>
7+
8+
namespace micm
9+
{
10+
/// @brief Information for each constraint (built during ConstraintSet construction)
11+
struct ConstraintInfo
12+
{
13+
std::size_t index_; // Index in constraints_ vector
14+
std::size_t row_index_; // Row in the forcing/Jacobian
15+
std::size_t number_of_dependencies_; // Number of species this constraint depends on
16+
std::size_t dependency_offset_; // Starting offset in dependency_ids_
17+
std::size_t jacobian_flat_offset_; // Starting offset in jacobian_flat_ids_
18+
std::vector<std::size_t> state_indices_; // Dependency indices in state_variables_
19+
};
20+
}

0 commit comments

Comments
 (0)