Skip to content

Commit b8e269f

Browse files
committed
fix(mass_balance): NaN in model vars should trigger mass balance error/warning
1 parent c1cbe8e commit b8e269f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/utilities/bmi/mass_balance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ auto NgenMassBalance::run(const ModelPtr& model, const Context& ctx) const -> ex
6262
model->GetValue(LEAKED_MASS_NAME, &mass_leaked);
6363
// TODO consider unit conversion if/when it becomes necessary
6464
mass_balance = mass_in - mass_out - mass_stored - mass_leaked;
65-
if ( std::abs(mass_balance) > tolerance ) {
65+
if ( std::abs(mass_balance) > tolerance || std::isnan(mass_balance)) {
6666
std::stringstream ss;
6767
ss << "mass_balance: "
6868
<< "at timestep " << std::to_string(ctx.current_time_step)

test/utils/bmi/mass_balance_Test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,17 @@ TEST_F(Bmi_Mass_Balance_Test, nan) {
409409
EXPECT_TRUE( result.has_value() ); // should pass
410410
}
411411

412+
TEST_F(Bmi_Mass_Balance_Test, model_nan) {
413+
auto properties = MassBalanceMock(true).as_json_property();
414+
auto context = make_context(0, 2, time, model_name);
415+
auto protocols = NgenBmiProtocols(model, properties);
416+
auto result = protocols.run(models::bmi::protocols::Protocol::MASS_BALANCE, context);
417+
EXPECT_TRUE( result.has_value() ); // should pass
418+
double mass_error = std::numeric_limits<double>::quiet_NaN();
419+
model->SetValue(OUTPUT_MASS_NAME, &mass_error); // Force a NaN into the mass balance computation
420+
time = "t1";
421+
// should cause an error since mass balance will be NaN using this value in its computation
422+
ASSERT_THROW(protocols.run(models::bmi::protocols::Protocol::MASS_BALANCE, make_context(1, 2, time, model_name)), ProtocolError);
423+
}
424+
412425
#endif // NGEN_BMI_CPP_LIB_TESTS_ACTIVE

0 commit comments

Comments
 (0)