boutproject / BOUT-dev

BOUT++: Plasma fluid finite-difference simulation code in curvilinear coordinate systems
http://boutproject.github.io/
GNU Lesser General Public License v3.0
179 stars 93 forks source link

Modifying the field at each output step (as opposed to each timestep) #2907

Closed davedavemckay closed 2 months ago

davedavemckay commented 2 months ago

Hi,

I'm attempting to apply a correction to a BOUT++ simulation using a modified version of the Hasegawa-Wakatani example. At the moment, during the simulation a call is made to a database to retrieve a correction from a ML model, which is added to n and vort. However, the ML model is trained on data from output (BOUT.dmp.*.nc files), not on each timestep, so there is a mismatch.

Is there a way to modify the field on output step rather than timesteps, similarly to implementing an output monitor by extending the Monitor class?

An alternative I'm considering may be to count the steps within the HW PhysicsModel and only retrieve the correction when the timestep is aligned with the output step (a quick _"if (ts % ts_per_output == 0) {addcorrection;}") - this might be easier to implement, but less general.

I'd be grateful for any thoughts.

davedavemckay commented 2 months ago

My colleague came up with a solution using the outpotMonitor. Adding (to hw.cxx):

  int outputMonitor(BoutReal simtime, int iter, int nout) {
    output << "iteration  =     " << iter << std::endl;
    if (iter >= 0) {
      output << "setting correction to true" << std::endl;
      addCorrection = true;
    }
    return 0;
  }

then the code to add the correction is wrapped in if (addCorrection){ ... }