simple.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2007-2019 PCOpt/NTUA
9  Copyright (C) 2013-2019 FOSS GP
10  Copyright (C) 2019 OpenCFD Ltd.
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "simple.H"
31 #include "findRefCell.H"
32 #include "constrainHbyA.H"
33 #include "constrainPressure.H"
34 #include "adjustPhi.H"
35 #include "Time.H"
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
43  addToRunTimeSelectionTable(incompressiblePrimalSolver, simple, dictionary);
44 }
45 
46 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
47 
49 {
51  return getIncoVars();
52 }
53 
54 
56 {
57  if (incoVars_.useSolverNameForFields())
58  {
60  << "useSolverNameForFields is set to true for primalSolver "
61  << solverName() << nl << tab
62  << "Appending variable names with the solver name" << nl << tab
63  << "Please adjust the necessary entries in fvSchemes and fvSolution"
64  << nl << endl;
65  }
66 }
67 
68 
70 {
71  surfaceScalarField& phi = incoVars_.phiInst();
73 
74  scalar sumLocalContErr = mesh_.time().deltaTValue()*
75  mag(contErr)().weightedAverage(mesh_.V()).value();
76 
77  scalar globalContErr = mesh_.time().deltaTValue()*
78  contErr.weightedAverage(mesh_.V()).value();
79  cumulativeContErr_ += globalContErr;
80 
81  Info<< "time step continuity errors : sum local = " << sumLocalContErr
82  << ", global = " << globalContErr
83  << ", cumulative = " << cumulativeContErr_
84  << endl;
85 }
86 
87 
88 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
89 
90 Foam::simple::simple
91 (
92  fvMesh& mesh,
93  const word& managerType,
94  const dictionary& dict
95 )
96 :
97  incompressiblePrimalSolver(mesh, managerType, dict),
98  solverControl_(SIMPLEControl::New(mesh, managerType, *this)),
99  incoVars_(allocateVars()),
100  MRF_(mesh),
101  cumulativeContErr_(Zero),
102  objectives_(0)
103 {
104  fvOptions_.reset
105  (
106  new fv::optionList(mesh_, dict.subOrEmptyDict("fvOptions"))
107  );
108  addExtraSchemes();
109  setRefCell
110  (
111  incoVars_.pInst(),
112  solverControl_().dict(),
113  solverControl_().pRefCell(),
114  solverControl_().pRefValue()
115  );
116 }
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
122 {
124  {
125  return true;
126  }
127 
128  return false;
129 }
130 
132 {
133  const Time& time = mesh_.time();
134  Info<< "Time = " << time.timeName() << "\n" << endl;
135 
136  // Grab references
137  volScalarField& p = incoVars_.pInst();
138  volVectorField& U = incoVars_.UInst();
139  surfaceScalarField& phi = incoVars_.phiInst();
141  incoVars_.turbulence();
142  label& pRefCell = solverControl_().pRefCell();
143  scalar& pRefValue = solverControl_().pRefValue();
144 
145  // Momentum predictor
146  //~~~~~~~~~~~~~~~~~~~
147 
148  MRF_.correctBoundaryVelocity(U);
149 
151  (
152  fvm::div(phi, U)
153  + MRF_.DDt(U)
154  + turbulence->divDevReff(U)
155  ==
156  fvOptions_()(U)
157  );
158  fvVectorMatrix& UEqn = tUEqn.ref();
159 
160  addOptimisationTypeSource(UEqn);
161 
162  UEqn.relax();
163 
164  fvOptions_().constrain(UEqn);
165 
166  if (solverControl_().momentumPredictor())
167  {
168  Foam::solve(UEqn == -fvc::grad(p));
169 
170  fvOptions_().correct(U);
171  }
172 
173  // Pressure Eq
174  //~~~~~~~~~~~~
175  {
176  volScalarField rAU(1.0/UEqn.A());
179  MRF_.makeRelative(phiHbyA);
180  adjustPhi(phiHbyA, U, p);
181 
183 
184  if (solverControl_().consistent())
185  {
186  rAtU = 1.0/(1.0/rAU - UEqn.H1());
187  phiHbyA +=
188  fvc::interpolate(rAtU() - rAU)*fvc::snGrad(p)*mesh_.magSf();
189  HbyA -= (rAU - rAtU())*fvc::grad(p);
190  }
191 
192  tUEqn.clear();
193 
194  // Update the pressure BCs to ensure flux consistency
195  constrainPressure(p, U, phiHbyA, rAtU(), MRF_);
196 
197  // Non-orthogonal pressure corrector loop
198  while (solverControl_().correctNonOrthogonal())
199  {
200  fvScalarMatrix pEqn
201  (
203  );
204 
206 
207  pEqn.solve();
208 
209  if (solverControl_().finalNonOrthogonalIter())
210  {
211  phi = phiHbyA - pEqn.flux();
212  }
213  }
214 
215  continuityErrors();
216 
217  // Explicitly relax pressure for momentum corrector
218  p.relax();
219 
220  // Momentum corrector
221  U = HbyA - rAtU()*fvc::grad(p);
222  U.correctBoundaryConditions();
223  fvOptions_().correct(U);
224  }
225 
226  incoVars_.laminarTransport().correct();
227  turbulence->correct();
228 
229  solverControl_().write();
230 
231  // Print objective values to screen and compute mean value
232  Info<< endl;
233  for (objective* obj : objectives_)
234  {
235  Info<< obj->objectiveName() << " : " << obj->J() << endl;
236  obj->accumulateJMean(solverControl_());
237  obj->writeInstantaneousValue();
238  }
239 
240  // Average fields if necessary
241  incoVars_.computeMeanFields();
242 
243  // Print execution time
244  time.printExecutionTime(Info);
245 }
246 
247 
249 {
250  // Iterate
251  if (active_)
252  {
253  // Get the objectives for this solver
254  if (objectives_.empty())
255  {
256  objectives_ = getObjectiveFunctions();
257  }
258 
259  // Reset initial and mean fields before solving
260  restoreInitValues();
261  incoVars_.resetMeanFields();
262 
263  // Validate turbulence model fields
264  incoVars_.turbulence()->validate();
265 
266  while (solverControl_().loop())
267  {
268  solveIter();
269  }
270 
271  // Safety
272  objectives_.clear();
273  }
274 }
275 
276 
278 {
279  return solverControl_().loop();
280 }
281 
282 
284 {
285  incoVars_.restoreInitValues();
286 }
287 
288 
290 {
291  os.writeEntry("averageIter", solverControl_().averageIter());
292 
293  return true;
294 }
295 
296 
297 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
Foam::constrainPressure
void constrainPressure(volScalarField &p, const RhoType &rho, const volVectorField &U, const surfaceScalarField &phiHbyA, const RAUType &rhorAU, const MRFType &MRF)
Definition: constrainPressure.C:38
Foam::fvMatrix::H
tmp< GeometricField< Type, fvPatchField, volMesh > > H() const
Return the H operation source.
Definition: fvMatrix.C:817
Foam::constrainHbyA
tmp< volVectorField > constrainHbyA(const tmp< volVectorField > &tHbyA, const volVectorField &U, const volScalarField &p)
Definition: constrainHbyA.C:35
Foam::fvc::snGrad
tmp< GeometricField< Type, fvsPatchField, surfaceMesh > > snGrad(const GeometricField< Type, fvPatchField, volMesh > &vf, const word &name)
Definition: fvcSnGrad.C:47
sumLocalContErr
scalar sumLocalContErr
Definition: compressibleContinuityErrs.H:34
rAtU
volScalarField rAtU(1.0/(1.0/rAU - UEqn.H1()))
contErr
volScalarField::Internal contErr((fvc::ddt(rho)+fvc::div(rhoPhi) -(fvOptions(alpha1, mixture.thermo1().rho())&rho1) -(fvOptions(alpha2, mixture.thermo2().rho())&rho2))())
simple.H
Foam::adjustPhi
bool adjustPhi(surfaceScalarField &phi, const volVectorField &U, volScalarField &p)
Adjust the balance of fluxes to obey continuity.
Definition: adjustPhi.C:37
simple
const dictionary & simple
Definition: readFluidMultiRegionSIMPLEControls.H:1
p
volScalarField & p
Definition: createFieldRefs.H:8
momentumPredictor
const bool momentumPredictor
Definition: readFluidMultiRegionSIMPLEControls.H:6
Foam::fvc::grad
tmp< GeometricField< typename outerProduct< vector, Type >::type, fvPatchField, volMesh >> grad(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Definition: fvcGrad.C:54
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::simple::solverControl_
autoPtr< SIMPLEControl > solverControl_
Solver control.
Definition: simple.H:72
turbulence
Info<< "Reading field U\n"<< endl;volVectorField U(IOobject("U", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE), mesh);volScalarField rho(IOobject("rho", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::AUTO_WRITE), thermo.rho());volVectorField rhoU(IOobject("rhoU", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE), rho *U);volScalarField rhoE(IOobject("rhoE", runTime.timeName(), mesh, IOobject::NO_READ, IOobject::NO_WRITE), rho *(e+0.5 *magSqr(U)));surfaceScalarField pos(IOobject("pos", runTime.timeName(), mesh), mesh, dimensionedScalar("pos", dimless, 1.0));surfaceScalarField neg(IOobject("neg", runTime.timeName(), mesh), mesh, dimensionedScalar("neg", dimless, -1.0));surfaceScalarField phi("phi", fvc::flux(rhoU));Info<< "Creating turbulence model\n"<< endl;autoPtr< compressible::turbulenceModel > turbulence(compressible::turbulenceModel::New(rho, U, phi, thermo))
Definition: createFields.H:94
Foam::tmp
A class for managing temporary objects.
Definition: PtrList.H:59
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
findRefCell.H
Find the reference cell nearest (in index) to the given cell but which is not on a cyclic,...
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
Foam::solver::mesh_
fvMesh & mesh_
Reference to the mesh database.
Definition: solver.H:71
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:764
constrainPressure.H
Foam::fvc::div
tmp< GeometricField< Type, fvPatchField, volMesh > > div(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Definition: fvcDiv.C:49
HbyA
HbyA
Definition: pcEqn.H:74
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::fvMatrix::setReference
void setReference(const label celli, const Type &value, const bool forceReference=false)
Set reference level for solution.
Definition: fvMatrix.C:509
Foam::simple::addExtraSchemes
void addExtraSchemes()
Definition: simple.C:55
adjustPhi.H
For cases which do no have a pressure boundary adjust the balance of fluxes to obey continuity....
Foam::simple::continuityErrors
void continuityErrors()
Compute continuity errors.
Definition: simple.C:69
Foam::simple::solveIter
virtual void solveIter()
Execute one iteration of the solution algorithm.
Definition: simple.C:131
Foam::Time::printExecutionTime
Ostream & printExecutionTime(OSstream &os) const
Print the elapsed ExecutionTime (cpu-time), ClockTime.
Definition: TimeIO.C:614
tUEqn
tmp< fvVectorMatrix > tUEqn(fvm::ddt(rho, U)+fvm::div(phi, U)+MRF.DDt(rho, U)+turbulence->divDevRhoReff(U)==fvOptions(rho, U))
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
phiHbyA
phiHbyA
Definition: pcEqn.H:73
Foam::simple::writeData
virtual bool writeData(Ostream &os) const
Write average iteration.
Definition: simple.C:289
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::fvm::laplacian
tmp< fvMatrix< Type > > laplacian(const GeometricField< Type, fvPatchField, volMesh > &vf, const word &name)
Definition: fvmLaplacian.C:48
Foam::fvMatrix::H1
tmp< volScalarField > H1() const
Return H(1)
Definition: fvMatrix.C:879
Foam::fvMatrix::relax
void relax(const scalar alpha)
Relax matrix (for steady-state solution).
Definition: fvMatrix.C:574
Foam::incompressiblePrimalSolver::readDict
virtual bool readDict(const dictionary &dict)
Read dict if updated.
Definition: incompressiblePrimalSolver.C:109
Foam::setRefCell
bool setRefCell(const volScalarField &field, const volScalarField &fieldRef, const dictionary &dict, label &refCelli, scalar &refValue, const bool forceReference=false)
If the field fieldRef needs referencing find the reference cell nearest.
Definition: findRefCell.C:34
Foam::solve
SolverPerformance< Type > solve(faMatrix< Type > &, Istream &)
Solve returning the solution statistics given convergence tolerance.
phi
surfaceScalarField & phi
Definition: setRegionFluidFields.H:8
Foam::simple::restoreInitValues
virtual void restoreInitValues()
Restore initial field values if necessary.
Definition: simple.C:283
Foam::incompressiblePrimalSolver
Base class for primal incompressible solvers.
Definition: incompressiblePrimalSolver.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fvMatrix::A
tmp< volScalarField > A() const
Return the central coefficient.
Definition: fvMatrix.C:788
Foam::simple::loop
virtual bool loop()
Looper (advances iters, time step)
Definition: simple.C:277
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:84
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::fvc::interpolate
static tmp< GeometricField< Type, fvsPatchField, surfaceMesh > > interpolate(const GeometricField< Type, fvPatchField, volMesh > &tvf, const surfaceScalarField &faceFlux, Istream &schemeData)
Interpolate field onto faces using scheme given by Istream.
Foam::dictionary::subOrEmptyDict
dictionary subOrEmptyDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX, const bool mandatory=false) const
Definition: dictionary.C:603
Foam::SIMPLEControl::New
static autoPtr< SIMPLEControl > New(fvMesh &mesh, const word &managerType, const solver &solver)
Return a reference to the selected turbulence model.
Definition: SIMPLEControl.C:64
globalContErr
scalar globalContErr
Definition: compressibleContinuityErrs.H:37
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
U
U
Definition: pEqn.H:72
Foam::tab
constexpr char tab
Definition: Ostream.H:371
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::solver::vars_
autoPtr< variablesSet > vars_
Base variableSet pointer.
Definition: solver.H:93
Foam::DimensionedField::weightedAverage
dimensioned< Type > weightedAverage(const DimensionedField< scalar, GeoMesh > &weightField) const
Calculate and return weighted average.
Definition: DimensionedField.C:461
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::simple::allocateVars
incompressibleVars & allocateVars()
Protected Member Functions.
Definition: simple.C:48
Foam::GeometricField::relax
void relax(const scalar alpha)
Relax field (for steady-state solution).
Definition: GeometricField.C:941
Foam::fvMatrix
A special matrix type and solver, designed for finite volume solutions of scalar equations....
Definition: fvPatchField.H:76
Foam::fvMatrix::solve
SolverPerformance< Type > solve(const dictionary &)
Solve returning the solution statistics.
Definition: fvMatrixSolve.C:298
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:219
rAU
tmp< volScalarField > rAU
Definition: initCorrectPhi.H:1
Foam::simple::readDict
virtual bool readDict(const dictionary &dict)
Read dict if updated.
Definition: simple.C:121
UEqn
fvVectorMatrix & UEqn
Definition: UEqn.H:13
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::incompressiblePrimalSolver::getIncoVars
const incompressibleVars & getIncoVars() const
Access to the incompressible variables set.
Definition: incompressiblePrimalSolver.C:156
Foam::fvc::flux
tmp< surfaceScalarField > flux(const volVectorField &vvf)
Return the face-flux field obtained from the given volVectorField.
Definition: fvcFlux.C:34
Foam::GeometricField< scalar, fvsPatchField, surfaceMesh >
Foam::fvm::div
tmp< fvMatrix< Type > > div(const surfaceScalarField &flux, const GeometricField< Type, fvPatchField, volMesh > &vf, const word &name)
Definition: fvmDiv.C:48
Foam::objective
Abstract base class for objective functions. No point in making this runTime selectable since its chi...
Definition: objective.H:58
Foam::fv::optionList
List of finite volume options.
Definition: fvOptionList.H:67
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
constrainHbyA.H
Foam::fvMatrix::flux
tmp< GeometricField< Type, fvsPatchField, surfaceMesh > > flux() const
Return the face-flux field from the matrix.
Definition: fvMatrix.C:927
Foam::incompressibleVars
Base class for solution control classes.
Definition: incompressibleVars.H:54
pRefCell
const label pRefCell
Definition: setRegionFluidFields.H:34
Foam::simple::solve
virtual void solve()
Main control loop.
Definition: simple.C:248
pRefValue
const scalar pRefValue
Definition: setRegionFluidFields.H:35