Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions include/base/FoamSolver.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#pragma once

#include "fvMesh.H"
#include "scalar.H"
#include "solver.H"
#include "functionObject.H"

#include <Time.H>
#include <TimeState.H>
#include <filesystem>

namespace fs = std::filesystem;

namespace Foam
{
Expand All @@ -19,13 +15,20 @@ namespace functionObjects
class mooseDeltaT : public functionObject
{
private:
const scalar & dt_;
const scalar & _dt;
scalar _old_desired_dt;
const scalar _delta_t_factor;
bool _enabled;

public:
TypeName("mooseDeltaT")

mooseDeltaT(const word & name, const Time & runTime, const scalar & dt)
: functionObject(name, runTime), dt_(dt)
: functionObject(name, runTime),
_dt(dt),
_old_desired_dt(time_.deltaTValue()),
_delta_t_factor(Foam::solver::deltaTFactor),
_enabled(true)
{
}

Expand All @@ -35,7 +38,23 @@ class mooseDeltaT : public functionObject

bool execute() { return true; }
bool write() { return true; }
scalar maxDeltaT() const { return dt_; }
void setOldDesiredDt(scalar desired_dt) { _old_desired_dt = desired_dt; }
void enable() { _enabled = true; }
void disable() { _enabled = false; }
scalar maxDeltaT() const
{
// If we don't want MOOSE's timestep to be considered, we return the maximum value.
if (!_enabled)
return Foam::VGREAT;

// If MOOSE altered the previous time step change the deltaTfactor to undo the MOOSE induced
// cutback
if (time_.deltaTValue() != _old_desired_dt)
Foam::solver::deltaTFactor = _delta_t_factor * _old_desired_dt / time_.deltaTValue();
else
Foam::solver::deltaTFactor = _delta_t_factor;
return _dt;
}
};
}
}
Expand Down Expand Up @@ -67,7 +86,7 @@ class FoamSolver
bool isDeltaTAdjustable() const;
// creates function object that tells OpenFOAM what MOOSE's
// time step is.
void appendDeltaTFunctionObject(const Foam::scalar & dt);
Foam::functionObjects::mooseDeltaT & appendDeltaTFunctionObject(const Foam::scalar & dt);
// get the current deltaT.
Foam::scalar getTimeDelta() const { return runTime().deltaTValue(); }

Expand Down
40 changes: 0 additions & 40 deletions include/solvers/timesteppers/FoamTimeStepper.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@

#include <InputParameters.h>
#include <TimeStepper.h>
#include <functional>
#include <optional>

/*
Time stepper that allows OpenFOAM to control the time step enabling features such as CFL
daptive time steps. The intention is to allows the current time step in OpenFOAM
adaptive time steps. The intention is to allow the current time step in OpenFOAM
to be exposed to MOOSE
*/
Comment thread
k-collie marked this conversation as resolved.

class FoamControlledTimeStepper : public TimeStepper
class FoamTimeStepper : public TimeStepper
{
public:
FoamControlledTimeStepper(InputParameters const & params);
FoamTimeStepper(InputParameters const & params);
static InputParameters validParams();

// Get initial time step from OpenFOAM input file
virtual Real computeInitialDT() { return computeDT(); };
virtual Real computeInitialDT() override { return computeDT(); };

/* Read time step from OpenFOAM
- Make sure the time step duration is computed in the current step
*/
virtual Real computeDT();
virtual Real computeDT() override;

// Consider how to communicate starting time for this stepper
// e.g. after a restart this would need to be executed after the
// OpenFOAM restart.
virtual void init();
virtual void init() override;

private:
// These two variables are needed depending on how the time-stepper is initialised
Expand All @@ -38,5 +40,7 @@ class FoamControlledTimeStepper : public TimeStepper
// Variables to determine whether an adjustable time step is used in OF and
// what it is.
bool _dt_adjustable = false;
Real _foam_initial_dt = 0.;
Real _foam_dt = 0.;
Real _desired_dt;
std::optional<std::reference_wrapper<Foam::functionObjects::mooseDeltaT>> _moose_dt;
};
8 changes: 4 additions & 4 deletions src/base/FoamSolver.C
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <algorithm>
#include <cassert>
#include <iterator>

namespace Foam
{
Expand Down Expand Up @@ -167,10 +166,11 @@ FoamSolver::isDeltaTAdjustable() const
return _solver->runTime.controlDict().lookupOrDefault("adjustTimeStep", false);
}

void
Foam::functionObjects::mooseDeltaT &
FoamSolver::appendDeltaTFunctionObject(const Foam::scalar & dt)
{
runTime().functionObjects().append(
new Foam::functionObjects::mooseDeltaT("Moose time step", runTime(), dt));
auto moose_dt = new Foam::functionObjects::mooseDeltaT("Moose time step", runTime(), dt);
runTime().functionObjects().append(moose_dt);
return *moose_dt;
}
} // namespace Hippo
82 changes: 0 additions & 82 deletions src/timesteppers/FoamControlledTimeStepper.C

This file was deleted.

82 changes: 63 additions & 19 deletions src/timesteppers/FoamTimeStepper.C
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "FoamProblem.h"
#include "FoamTimeStepper.h"
#include <pimpleSingleRegionControl.H>

#include <scalar.H>
#include <TimeStepper.h>
#include <Transient.h>
#include <solver.H>

registerMooseObject("hippoApp", FoamTimeStepper);

Expand All @@ -13,7 +16,8 @@ FoamTimeStepper::validParams()
return params;
}

FoamTimeStepper::FoamTimeStepper(InputParameters const & params) : TimeStepper(params)
FoamTimeStepper::FoamTimeStepper(InputParameters const & params)
: TimeStepper(params), _foam_dt{}, _desired_dt{}, _moose_dt()
{
auto problem = dynamic_cast<FoamProblem *>(&_app.feProblem());
if (!problem)
Expand All @@ -22,28 +26,32 @@ FoamTimeStepper::FoamTimeStepper(InputParameters const & params) : TimeStepper(p
}
}

Real
FoamTimeStepper::computeInitialDT()
{
auto dt = _executioner.parameters().get<double>("dt");
solver().setTimeDelta(_dt);
return dt;
}

Real
FoamTimeStepper::computeDT()
{
solver().setTimeDelta(_dt);
return _dt;
}

void
FoamTimeStepper::init()
{
TimeStepper::init();
solver().setCurrentTime(_time);
solver().setEndTime(_end_time);
solver().setTimeDelta(_dt);
if (!_dt_adjustable)
return _foam_dt;

// Not ideal, but for MOOSE to get an accurate deltaT
// preSolve must be called as this updates the BCs.
solver().preSolve();

// Tells the mooseDelta function object what the previous desired time
// step was so it can work out whether there was a MOOSE induced cutback.
_moose_dt->get().setOldDesiredDt(_desired_dt);

// Ensure MOOSE gets OpenFOAM's time step unaffected by the mooseDeltaT
// functionObject.
_moose_dt->get().disable();

// compute OpenFOAM's desired time step
_desired_dt = solver().computeDeltaT();

// reenable the function object
_moose_dt->get().enable();

return _desired_dt;
}

FoamProblem *
Expand All @@ -56,3 +64,39 @@ FoamTimeStepper::problem()
}
return problem;
}

void
FoamTimeStepper::init()
{
TimeStepper::init();

// Apply start time from input file if it is present
if (_executioner.isParamSetByUser("start_time"))
solver().setCurrentTime(_time);

// Apply end time from input file if it is present
if (_executioner.isParamSetByUser("end_time"))
solver().setEndTime(_end_time);

if (_executioner.isParamSetByUser("dt"))
{
_foam_dt = _executioner.getParam<Real>("dt");
_dt_adjustable = false;
solver().setTimeDelta(_foam_dt);
return;
}
// determine if OpenFOAM's time-step is adjustable in controlDict
_dt_adjustable = solver().isDeltaTAdjustable();

// The key idea is that runTime.functionObjects().maxDeltaT() in adjustDeltaT
// loops over the function objects and chooses the minimum, so by having
// a function Object that returns what MOOSE wants, OpenFOAM will use the
// MOOSE time step if it is smaller than what OpenFOAM wants. As a result,
// if MOOSE wants to add a synchronisation step OpenFOAM will also use it too.

// create function object and append it to the solver's function object list
_moose_dt = solver().appendDeltaTFunctionObject(_dt);
_desired_dt = solver().getTimeDelta();
if (!_dt_adjustable)
_foam_dt = solver().getTimeDelta();
}
2 changes: 1 addition & 1 deletion test/tests/actions/foam_bc/main.i
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
end_time = 1
[TimeSteppers]
[foam]
type = FoamControlledTimeStepper
type = FoamTimeStepper
[]
[]
[]
Expand Down
2 changes: 1 addition & 1 deletion test/tests/actions/foam_variable/main.i
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
end_time = 0.32
[TimeSteppers]
[foam]
type = FoamControlledTimeStepper
type = FoamTimeStepper
[]
[]
[]
Expand Down
Loading
Loading