DAResidualSimpleFoam.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAResidualSimpleFoam.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAResidualSimpleFoam, 0);
16 addToRunTimeSelectionTable(DAResidual, DAResidualSimpleFoam, dictionary);
17 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18 
20  const word modelType,
21  const fvMesh& mesh,
22  const DAOption& daOption,
23  const DAModel& daModel,
24  const DAIndex& daIndex)
25  : DAResidual(modelType, mesh, daOption, daModel, daIndex),
26  // initialize and register state variables and their residuals, we use macros defined in macroFunctions.H
27  setResidualClassMemberVector(U, dimensionSet(0, 1, -2, 0, 0, 0, 0)),
28  setResidualClassMemberScalar(p, dimensionSet(0, 0, -1, 0, 0, 0, 0)),
30  alphaPorosity_(const_cast<volScalarField&>(
31  mesh_.thisDb().lookupObject<volScalarField>("alphaPorosity"))),
32  fvSource_(const_cast<volVectorField&>(
33  mesh_.thisDb().lookupObject<volVectorField>("fvSource"))),
34  fvOptions_(fv::options::New(mesh)),
35  daTurb_(const_cast<DATurbulenceModel&>(daModel.getDATurbulenceModel())),
36  // create simpleControl
37  simple_(const_cast<fvMesh&>(mesh)),
38  MRF_(const_cast<IOMRFZoneListDF&>(
39  mesh_.thisDb().lookupObject<IOMRFZoneListDF>("MRFProperties")))
40 {
41  // initialize fvSource
42  const dictionary& allOptions = daOption.getAllOptions();
43  if (allOptions.subDict("fvSource").toc().size() != 0)
44  {
45  hasFvSource_ = 1;
46  }
47 
48  // this is just a dummy call because we need to run the constrain once
49  // to initialize fvOptions, before we can use it. Otherwise, we may get
50  // a seg fault when we call fvOptions_.correct(U_) in updateIntermediateVars
51  fvVectorMatrix UEqn(
52  fvm::div(phi_, U_)
53  - fvOptions_(U_));
54  fvOptions_.constrain(UEqn);
55 }
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
60 {
61  /*
62  Description:
63  Clear all members to avoid memory leak because we will initalize
64  multiple objects of DAResidual. Here we need to delete all members
65  in the parent and child classes
66  */
67  URes_.clear();
68  pRes_.clear();
69  phiRes_.clear();
70 }
71 
72 void DAResidualSimpleFoam::calcResiduals(const dictionary& options)
73 {
74  /*
75  Description:
76  This is the function to compute residuals.
77 
78  Input:
79  options.isPC: 1 means computing residuals for preconditioner matrix.
80  This essentially use the first order scheme for div(phi,U)
81 
82  p_, U_, phi_, etc: State variables in OpenFOAM
83 
84  Output:
85  URes_, pRes_, phiRes_: residual field variables
86  */
87 
88  // ******** U Residuals **********
89  // copied and modified from UEqn.H
90 
91  word divUScheme = "div(phi,U)";
92 
93  label isPC = options.getLabel("isPC");
94 
95  if (isPC)
96  {
97  divUScheme = "div(pc)";
98  }
99 
100  if (hasFvSource_)
101  {
102  DAFvSource& daFvSource(const_cast<DAFvSource&>(
103  mesh_.thisDb().lookupObject<DAFvSource>("DAFvSource")));
104  daFvSource.calcFvSource(fvSource_);
105  }
106 
107  tmp<fvVectorMatrix> tUEqn(
108  fvm::div(phi_, U_, divUScheme)
109  + fvm::Sp(alphaPorosity_, U_)
110  + MRF_.DDt(U_)
112  - fvSource_
113  - fvOptions_(U_));
114  fvVectorMatrix& UEqn = tUEqn.ref();
115 
116  UEqn.relax();
117 
118  fvOptions_.constrain(UEqn);
119 
120  URes_ = (UEqn & U_) + fvc::grad(p_);
121  normalizeResiduals(URes);
122 
123  // ******** p Residuals **********
124  // copied and modified from pEqn.H
125  // NOTE manually set pRefCell and pRefValue
126  label pRefCell = 0;
127  scalar pRefValue = 0.0;
128 
129  volScalarField rAU(1.0 / UEqn.A());
130  //volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U_, p_));
131  //***************** NOTE *******************
132  // constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
133  // function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
134  // Basically, we should not constrain any variable because it will create discontinuity.
135  // Instead, we use the old implementation used in OpenFOAM-3.0+ and before
136  volVectorField HbyA("HbyA", U_);
137  HbyA = rAU * UEqn.H();
138 
139  surfaceScalarField phiHbyA("phiHbyA", fvc::flux(HbyA));
140 
142 
143  adjustPhi(phiHbyA, U_, p_);
144 
145  tmp<volScalarField> rAtU(rAU);
146 
147  if (simple_.consistent())
148  {
149  rAtU = 1.0 / (1.0 / rAU - UEqn.H1());
150  phiHbyA += fvc::interpolate(rAtU() - rAU) * fvc::snGrad(p_) * mesh_.magSf();
151  HbyA -= (rAU - rAtU()) * fvc::grad(p_);
152  }
153 
154  tUEqn.clear();
155 
156  // Update the pressure BCs to ensure flux consistency
158 
159  fvScalarMatrix pEqn(
160  fvm::laplacian(rAtU(), p_)
161  == fvc::div(phiHbyA));
162  pEqn.setReference(pRefCell, pRefValue);
163 
164  pRes_ = pEqn & p_;
165  normalizeResiduals(pRes);
166 
167  // ******** phi Residuals **********
168  // copied and modified from pEqn.H
169  phiRes_ = phiHbyA - pEqn.flux() - phi_;
170  // need to normalize phiRes
171  normalizePhiResiduals(phiRes);
172 }
173 
175 {
176  /*
177  Description:
178  Update the intermediate variables that depend on the state variables
179  */
180 
182  fvOptions_.correct(U_);
183 }
184 
186 {
187  /*
188  Description:
189  Update the boundary condition for all the states in the selected solver
190  */
191 
192  U_.correctBoundaryConditions();
193  p_.correctBoundaryConditions();
194 }
195 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
196 
197 } // End namespace Foam
198 
199 // ************************************************************************* //
Foam::DAFvSource
Definition: DAFvSource.H:34
Foam::MRFZoneListDF::DDt
tmp< volVectorField > DDt(const volVectorField &U) const
Definition: MRFZoneListDF.C:120
Foam::DAResidualSimpleFoam::calcResiduals
virtual void calcResiduals(const dictionary &options)
compute residual
Definition: DAResidualSimpleFoam.C:72
Foam::DAResidualSimpleFoam::alphaPorosity_
volScalarField & alphaPorosity_
alpha porosity term
Definition: DAResidualSimpleFoam.H:46
tUEqn
tmp< fvVectorMatrix > tUEqn(fvm::ddt(U)+fvm::div(phi, U)+MRF.DDt(U)+turbulence->divDevReff(U)==fvOptions(U))
allOptions
const dictionary & allOptions
Definition: createRefsRhoSimpleC.H:15
adjustPhi
adjustPhi(phiHbyA, U, p)
U
U
Definition: pEqnPimpleDyM.H:60
DAResidualSimpleFoam.H
Foam::DAResidualSimpleFoam::updateIntermediateVariables
virtual void updateIntermediateVariables()
update any intermediate variables that are dependent on state variables and are used in calcResiduals
Definition: DAResidualSimpleFoam.C:174
Foam::DAResidualSimpleFoam::p_
volScalarField & p_
Definition: DAResidualSimpleFoam.H:38
Foam::DAResidual::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAResidual.H:47
Foam::DATurbulenceModel::divDevReff
tmp< fvVectorMatrix > divDevReff(volVectorField &U)
divDev terms
Definition: DATurbulenceModel.C:347
pRefValue
scalar & pRefValue
Definition: createRefsPimple.H:12
Foam::DAOption
Definition: DAOption.H:29
Foam::DAResidualSimpleFoam::correctBoundaryConditions
virtual void correctBoundaryConditions()
update the boundary condition for all the states in the selected solver
Definition: DAResidualSimpleFoam.C:185
UEqn
fvVectorMatrix & UEqn
Definition: UEqnPimpleDyM.H:13
Foam::DAResidualSimpleFoam::hasFvSource_
label hasFvSource_
whether to have fvSource term
Definition: DAResidualSimpleFoam.H:64
daOption
DAOption daOption(mesh, pyOptions_)
constrainPressure
constrainPressure(p, U, phiHbyA, rAtU(), MRF)
Foam::DAFvSource::calcFvSource
virtual void calcFvSource(volVectorField &fvSource)
compute the FvSource term
Definition: DAFvSource.C:90
pRefCell
label & pRefCell
Definition: createRefsPimple.H:11
Foam::DAResidualSimpleFoam::URes_
volVectorField URes_
Definition: DAResidualSimpleFoam.H:36
Foam::DAResidualSimpleFoam::fvSource_
volVectorField & fvSource_
fvSource term
Definition: DAResidualSimpleFoam.H:49
normalizeResiduals
#define normalizeResiduals(resName)
Definition: DAMacroFunctions.H:28
Foam::DAResidualSimpleFoam::simple_
simpleControl simple_
simpleControl object which will be initialized in this class
Definition: DAResidualSimpleFoam.H:58
HbyA
HbyA
Definition: pEqnRhoSimpleC.H:12
p
volScalarField & p
Definition: createRefsPimple.H:6
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
setResidualClassMemberPhi
#define setResidualClassMemberPhi(stateName)
Definition: DAMacroFunctions.H:83
phi
surfaceScalarField & phi
Definition: createRefsPimple.H:8
phiHbyA
phiHbyA
Definition: pEqnRhoSimpleC.H:27
Foam::DAIndex
Definition: DAIndex.H:32
setResidualClassMemberVector
#define setResidualClassMemberVector(stateName, stateUnit)
Definition: DAMacroFunctions.H:68
Foam::DAResidualSimpleFoam::DAResidualSimpleFoam
DAResidualSimpleFoam(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAResidualSimpleFoam.C:19
Foam::DAResidualSimpleFoam::MRF_
IOMRFZoneListDF & MRF_
Multiple Reference Frame.
Definition: DAResidualSimpleFoam.H:61
Foam::DAModel
Definition: DAModel.H:59
Foam
Definition: multiFreqScalarFvPatchField.C:144
rAtU
tmp< volScalarField > rAtU(rAU)
Foam::IOMRFZoneListDF
Definition: IOMRFZoneListDF.H:49
Foam::MRFZoneListDF::makeRelative
void makeRelative(volVectorField &U) const
Definition: MRFZoneListDF.C:148
Foam::DAResidual
Definition: DAResidual.H:35
Foam::DATurbulenceModel
Definition: DATurbulenceModel.H:61
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFvSource, 0)
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFvSource, DAFvSourceActuatorDisk, dictionary)
rAU
volScalarField rAU(1.0/UEqn.A())
Foam::MRFZoneListDF::correctBoundaryVelocity
void correctBoundaryVelocity(volVectorField &U) const
Definition: MRFZoneListDF.C:207
daModel
DAModel daModel(mesh, daOption)
Foam::DAResidualSimpleFoam::clear
virtual void clear()
clear the members
Definition: DAResidualSimpleFoam.C:59
Foam::DAResidualSimpleFoam::daTurb_
DATurbulenceModel & daTurb_
DATurbulenceModel object.
Definition: DAResidualSimpleFoam.H:55
daIndex
DAIndex daIndex(mesh, daOption, daModel)
Foam::DAResidualSimpleFoam::phiRes_
surfaceScalarField phiRes_
Definition: DAResidualSimpleFoam.H:42
setResidualClassMemberScalar
#define setResidualClassMemberScalar(stateName, stateUnit)
Definition: DAMacroFunctions.H:53
Foam::DAResidualSimpleFoam::U_
volVectorField & U_
Definition: DAResidualSimpleFoam.H:35
Foam::DAResidualSimpleFoam::fvOptions_
fv::options & fvOptions_
fvOptions term
Definition: DAResidualSimpleFoam.H:52
normalizePhiResiduals
#define normalizePhiResiduals(resName)
Definition: DAMacroFunctions.H:37
Foam::DAResidualSimpleFoam::phi_
surfaceScalarField & phi_
Definition: DAResidualSimpleFoam.H:41
Foam::DAResidualSimpleFoam::pRes_
volScalarField pRes_
Definition: DAResidualSimpleFoam.H:39