@@ -37,24 +37,27 @@ NgenMassBalance::NgenMassBalance() : check(false) {}
3737NgenMassBalance::~NgenMassBalance () = default ;
3838
3939auto NgenMassBalance::run (const ModelPtr& model, const Context& ctx) const -> expected<void, ProtocolError> {
40- bool check_step = false ;
41- // if frequency was set to -1 (or any negative), only check at the end
42- if ( frequency < 0 ){
43- if (ctx.current_time_step == ctx.total_steps ){
44- check_step = true ;
45- }
46- }
47- else {
48- check_step = (ctx.current_time_step % frequency) == 0 ;
49- }
5040 if ( model == nullptr ) {
5141 return make_unexpected<ProtocolError>( ProtocolError (
5242 Error::UNITIALIZED_MODEL,
5343 " Cannot run mass balance protocol with null model."
5444 )
5545 );
46+ } else if ( !check || !supported ) {
47+ return {};
5648 }
57- if (model && supported && check && check_step) {
49+ bool check_step = false ;
50+ // if frequency was set to -1 (or any negative), only check at the end
51+ // use <= to avoid a potential divide by zero should frequency be 0
52+ // (though frequency 0 should have been caught during initialization and check disabled)
53+ if ( frequency > 0 ){
54+ check_step = (ctx.current_time_step % frequency) == 0 ;
55+ }
56+ else if (ctx.current_time_step == ctx.total_steps ){
57+ check_step = true ;
58+ }
59+
60+ if (check_step) {
5861 double mass_in, mass_out, mass_stored, mass_leaked, mass_balance;
5962 model->GetValue (INPUT_MASS_NAME, &mass_in);
6063 model->GetValue (OUTPUT_MASS_NAME, &mass_out);
@@ -174,6 +177,9 @@ auto NgenMassBalance::initialize(const ModelPtr& model, const Properties& proper
174177 } else {
175178 frequency = 1 ; // default, check every timestep
176179 }
180+ if ( frequency == 0 ) {
181+ check = false ; // can't check at frequency 0, disable mass balance checking
182+ }
177183 } else {
178184 // no mass balance requested, or not supported, so don't check it
179185 check = false ;
0 commit comments