Skip to content

Commit 64409a1

Browse files
committed
address copilot review
1 parent 16b52fe commit 64409a1

File tree

6 files changed

+14
-3
lines changed

6 files changed

+14
-3
lines changed

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
}

include/micm/constraint/constraint_set.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ namespace micm
8585

8686
if (!algebraic_variable_ids_.insert(info.row_index_).second)
8787
{
88-
throw std::runtime_error(
88+
throw std::system_error(
89+
make_error_code(MicmConstraintErrc::DuplicateAlgebraicSpecies),
8990
"Multiple constraints map to the same algebraic species row '" + algebraic_species + "'");
9091
}
9192

include/micm/constraint/types/equilibrium_constraint.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ namespace micm
183183

184184
state.ForEachRow([stoich](const double& conc, double& product)
185185
{
186-
product *= std::pow(conc, stoich);
186+
product *= std::pow(std::max(0.0, conc), stoich);
187187
}, state.GetConstColumnView(species_idx), product_product);
188188
}
189189

include/micm/constraint/types/linear_constraint.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <micm/constraint/constraint_error.hpp>
6+
#include <micm/constraint/constraint_info.hpp>
67
#include <micm/system/stoich_species.hpp>
78

89
#include <cstddef>
@@ -99,6 +100,12 @@ namespace micm
99100
// Create a variable for accumulating the linear sum
100101
auto linear_sum = force.GetRowVariable();
101102

103+
// Initialize linear_sum to 0.0 before accumulation
104+
state.ForEachRow([](double& sum)
105+
{
106+
sum = 0.0;
107+
}, linear_sum);
108+
102109
for (std::size_t i = 0; i < coeffs.size(); ++i)
103110
{
104111
const double coeff = coeffs[i];

include/micm/util/error.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,4 @@
7171
#define MICM_CONSTRAINT_ERROR_CODE_EMPTY_REACTANTS 3
7272
#define MICM_CONSTRAINT_ERROR_CODE_EMPTY_PRODUCTS 4
7373
#define MICM_CONSTRAINT_ERROR_CODE_INVALID_STOICHIOMETRY 5
74+
#define MICM_CONSTRAINT_ERROR_CODE_DUPLICATE_ALGEBRAIC_SPECIES 6

test/unit/constraint/test_constraint_set_policy.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void testAddForcingTerms()
174174
// State with 2 grid cells
175175
DenseMatrixPolicy state(2, num_species);
176176
state[0] = { 0.2, 0.4, 0.6 }; // Away from equilibrium
177-
state[1] = { 0.1, 0.3, 0.7 }; // At equilibrium
177+
state[1] = { 0.1, 0.3, 0.7 }; // Away from equilibrium
178178

179179
// Forcing vector (same size as state; constraint replaces AB row at index 2)
180180
DenseMatrixPolicy forcing(2, num_species, 0.0);

0 commit comments

Comments
 (0)