Skip to content

Commit ca449fa

Browse files
committed
fix(mass_balance): avoid div-by-zero, don't check mass balance with frequency 0
1 parent b8e269f commit ca449fa

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/utilities/bmi/mass_balance.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ auto NgenMassBalance::initialize(const ModelPtr& model, const Properties& proper
174174
} else {
175175
frequency = 1; //default, check every timestep
176176
}
177+
if ( frequency == 0 ) {
178+
check = false; // can't check at frequency 0, disable mass balance checking
179+
}
177180
} else{
178181
//no mass balance requested, or not supported, so don't check it
179182
check = false;

test/utils/bmi/mass_balance_Test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,16 @@ TEST_F(Bmi_Mass_Balance_Test, frequency) {
369369
ASSERT_THROW(protocols.run(models::bmi::protocols::Protocol::MASS_BALANCE, make_context(2, 2, time, model_name)), ProtocolError);
370370
}
371371

372+
TEST_F(Bmi_Mass_Balance_Test, frequency_zero) {
373+
auto properties = MassBalanceMock(true, 1e-5, 0).as_json_property();
374+
auto context = make_context(0, 2, time, model_name);
375+
auto protocols = NgenBmiProtocols(model, properties);
376+
double mass_error = 10; // Force a mass balance error above tolerance
377+
model->SetValue(OUTPUT_MASS_NAME, &mass_error);
378+
auto result = protocols.run(models::bmi::protocols::Protocol::MASS_BALANCE, make_context(0, 2, time, model_name));
379+
EXPECT_TRUE( result.has_value() ); // should pass, frequency 0 means never check
380+
}
381+
372382
TEST_F(Bmi_Mass_Balance_Test, frequency_end) {
373383
auto properties = MassBalanceMock(true, 1e-5, -1).as_json_property();
374384
auto context = make_context(0, 2, time, model_name);

0 commit comments

Comments
 (0)