Skip to content

Commit 35e4cd4

Browse files
authored
Added an option to use constrainHbyA (#486)
* Added an option to enable constrainHbyA. * Added the missing header file. * Used pointer for HbyA. * Fixed a minor issue. * Updated the integration test due to an issue for SLSQP v2 scripts.
1 parent a00afb1 commit 35e4cd4

File tree

12 files changed

+159
-84
lines changed

12 files changed

+159
-84
lines changed

dafoam/pyDAFoam.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
"""
1313

14-
__version__ = "3.0.7"
14+
__version__ = "3.0.8"
1515

1616
import subprocess
1717
import os
@@ -491,6 +491,13 @@ def __init__(self):
491491
## multiple times to make sure the AD seeds are propagated properly for the iterative BCs.
492492
self.hasIterativeBC = False
493493

494+
## whether to use the constrainHbyA in the pEqn. The DASolvers are similar to OpenFOAM's native
495+
## solvers except that we directly compute the HbyA term without any constraints. In other words,
496+
## we comment out the constrainHbyA line in the pEqn. However, some cases may diverge without
497+
## the constrainHbyA, e.g., the MRF cases with the SST model. Here we have an option to add the
498+
## constrainHbyA back to the primal and adjoint solvers.
499+
self.useConstrainHbyA = False
500+
494501
# *********************************************************************************************
495502
# ************************************ Advance Options ****************************************
496503
# *********************************************************************************************

src/adjoint/DAResidual/DAResidual.H

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "DAField.H"
2323
#include "DAFvSource.H"
2424
#include "IOMRFZoneListDF.H"
25+
#include "constrainHbyA.H"
2526

2627
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
2728

src/adjoint/DAResidual/DAResidualRhoSimpleCFoam.C

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,23 @@ void DAResidualRhoSimpleCFoam::calcResiduals(const dictionary& options)
142142
// copied and modified from pEqn.H
143143
volScalarField rAU(1.0 / UEqn.A());
144144
volScalarField rAtU(1.0 / (1.0 / rAU - UEqn.H1()));
145-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
146145
//***************** NOTE *******************
147-
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
148-
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
149-
// Basically, we should not constrain any variable because it will create discontinuity.
150-
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
151-
volVectorField HbyA("HbyA", U_);
152-
HbyA = rAU * UEqn.H();
146+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
147+
// because constraining variables will create discontinuity. Here we have a option to use the old
148+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
149+
autoPtr<volVectorField> HbyAPtr = nullptr;
150+
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
151+
if (useConstrainHbyA)
152+
{
153+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U_, p_)));
154+
}
155+
else
156+
{
157+
HbyAPtr.reset(new volVectorField("HbyA", U_));
158+
HbyAPtr() = rAU * UEqn.H();
159+
}
160+
volVectorField& HbyA = HbyAPtr();
161+
153162
tUEqn.clear();
154163

155164
surfaceScalarField phiHbyA("phiHbyA", fvc::interpolate(rho_) * fvc::flux(HbyA));
@@ -191,7 +200,6 @@ void DAResidualRhoSimpleCFoam::calcResiduals(const dictionary& options)
191200
// copied and modified from pEqn.H
192201
phiRes_ = phiHbyA + pEqn.flux() - phi_;
193202
normalizePhiResiduals(phiRes);
194-
195203
}
196204

197205
void DAResidualRhoSimpleCFoam::updateIntermediateVariables()

src/adjoint/DAResidual/DAResidualRhoSimpleFoam.C

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,23 @@ void DAResidualRhoSimpleFoam::calcResiduals(const dictionary& options)
177177
// copied and modified from pEqn.H
178178
volScalarField rAU(1.0 / UEqn.A());
179179
surfaceScalarField rhorAUf("rhorAUf", fvc::interpolate(rho_ * rAU));
180-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
181180
//***************** NOTE *******************
182-
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
183-
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
184-
// Basically, we should not constrain any variable because it will create discontinuity.
185-
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
186-
volVectorField HbyA("HbyA", U_);
187-
HbyA = rAU * UEqn.H();
181+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
182+
// because constraining variables will create discontinuity. Here we have a option to use the old
183+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
184+
autoPtr<volVectorField> HbyAPtr = nullptr;
185+
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
186+
if (useConstrainHbyA)
187+
{
188+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U_, p_)));
189+
}
190+
else
191+
{
192+
HbyAPtr.reset(new volVectorField("HbyA", U_));
193+
HbyAPtr() = rAU * UEqn.H();
194+
}
195+
volVectorField& HbyA = HbyAPtr();
196+
188197
tUEqn.clear();
189198

190199
surfaceScalarField phiHbyA("phiHbyA", fvc::interpolate(rho_) * fvc::flux(HbyA));

src/adjoint/DAResidual/DAResidualSimpleFoam.C

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,22 @@ void DAResidualSimpleFoam::calcResiduals(const dictionary& options)
127127
scalar pRefValue = 0.0;
128128

129129
volScalarField rAU(1.0 / UEqn.A());
130-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U_, p_));
131130
//***************** 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();
131+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
132+
// because constraining variables will create discontinuity. Here we have a option to use the old
133+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
134+
autoPtr<volVectorField> HbyAPtr = nullptr;
135+
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
136+
if (useConstrainHbyA)
137+
{
138+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U_, p_)));
139+
}
140+
else
141+
{
142+
HbyAPtr.reset(new volVectorField("HbyA", U_));
143+
HbyAPtr() = rAU * UEqn.H();
144+
}
145+
volVectorField& HbyA = HbyAPtr();
138146

139147
surfaceScalarField phiHbyA("phiHbyA", fvc::flux(HbyA));
140148

src/adjoint/DAResidual/DAResidualSimpleTFoam.C

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,22 @@ void DAResidualSimpleTFoam::calcResiduals(const dictionary& options)
146146
scalar pRefValue = 0.0;
147147

148148
volScalarField rAU(1.0 / UEqn.A());
149-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U_, p_));
150149
//***************** NOTE *******************
151-
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
152-
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
153-
// Basically, we should not constrain any variable because it will create discontinuity.
154-
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
155-
volVectorField HbyA("HbyA", U_);
156-
HbyA = rAU * UEqn.H();
150+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
151+
// because constraining variables will create discontinuity. Here we have a option to use the old
152+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
153+
autoPtr<volVectorField> HbyAPtr = nullptr;
154+
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
155+
if (useConstrainHbyA)
156+
{
157+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U_, p_)));
158+
}
159+
else
160+
{
161+
HbyAPtr.reset(new volVectorField("HbyA", U_));
162+
HbyAPtr() = rAU * UEqn.H();
163+
}
164+
volVectorField& HbyA = HbyAPtr();
157165

158166
surfaceScalarField phiHbyA("phiHbyA", fvc::flux(HbyA));
159167

src/adjoint/DASolver/DARhoSimpleCFoam/pEqnRhoSimpleC.H

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ rho = thermo.rho();
22

33
volScalarField rAU(1.0 / UEqn.A());
44
volScalarField rAtU(1.0 / (1.0 / rAU - UEqn.H1()));
5-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
65
//***************** NOTE *******************
7-
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
8-
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
9-
// Basically, we should not constrain any variable because it will create discontinuity.
10-
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
11-
volVectorField HbyA("HbyA", U);
12-
HbyA = rAU * UEqn.H();
6+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
7+
// because constraining variables will create discontinuity. Here we have a option to use the old
8+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
9+
autoPtr<volVectorField> HbyAPtr = nullptr;
10+
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
11+
if (useConstrainHbyA)
12+
{
13+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U, p)));
14+
}
15+
else
16+
{
17+
HbyAPtr.reset(new volVectorField("HbyA", U));
18+
HbyAPtr() = rAU * UEqn.H();
19+
}
20+
volVectorField& HbyA = HbyAPtr();
21+
1322
tUEqn.clear();
1423

1524
bool closedVolume = false;

src/adjoint/DASolver/DARhoSimpleFoam/pEqnRhoSimple.H

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
volScalarField rAU(1.0 / UEqn.A());
22
surfaceScalarField rhorAUf("rhorAUf", fvc::interpolate(rho* rAU));
3-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
43
//***************** NOTE *******************
5-
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
6-
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
7-
// Basically, we should not constrain any variable because it will create discontinuity.
8-
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
9-
volVectorField HbyA("HbyA", U);
10-
HbyA = rAU * UEqn.H();
4+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
5+
// because constraining variables will create discontinuity. Here we have a option to use the old
6+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
7+
autoPtr<volVectorField> HbyAPtr = nullptr;
8+
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
9+
if (useConstrainHbyA)
10+
{
11+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U, p)));
12+
}
13+
else
14+
{
15+
HbyAPtr.reset(new volVectorField("HbyA", U));
16+
HbyAPtr() = rAU * UEqn.H();
17+
}
18+
volVectorField& HbyA = HbyAPtr();
19+
1120
tUEqn.clear();
1221

1322
bool closedVolume = false;

src/adjoint/DASolver/DASimpleFoam/pEqnSimple.H

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
{
22
volScalarField rAU(1.0 / UEqn.A());
3-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
43
//***************** NOTE *******************
5-
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
6-
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
7-
// Basically, we should not constrain any variable because it will create discontinuity.
8-
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
9-
volVectorField HbyA("HbyA", U);
10-
HbyA = rAU * UEqn.H();
4+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
5+
// because constraining variables will create discontinuity. Here we have a option to use the old
6+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
7+
autoPtr<volVectorField> HbyAPtr = nullptr;
8+
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
9+
if (useConstrainHbyA)
10+
{
11+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U, p)));
12+
}
13+
else
14+
{
15+
HbyAPtr.reset(new volVectorField("HbyA", U));
16+
HbyAPtr() = rAU * UEqn.H();
17+
}
18+
volVectorField& HbyA = HbyAPtr();
1119
surfaceScalarField phiHbyA("phiHbyA", fvc::flux(HbyA));
1220

1321
MRF.makeRelative(phiHbyA);

src/adjoint/DASolver/DASimpleTFoam/pEqnSimpleT.H

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
{
22
volScalarField rAU(1.0 / UEqn.A());
3-
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
43
//***************** NOTE *******************
5-
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
6-
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
7-
// Basically, we should not constrain any variable because it will create discontinuity.
8-
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
9-
volVectorField HbyA("HbyA", U);
10-
HbyA = rAU * UEqn.H();
4+
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives
5+
// because constraining variables will create discontinuity. Here we have a option to use the old
6+
// implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
7+
autoPtr<volVectorField> HbyAPtr = nullptr;
8+
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
9+
if (useConstrainHbyA)
10+
{
11+
HbyAPtr.reset(new volVectorField(constrainHbyA(rAU * UEqn.H(), U, p)));
12+
}
13+
else
14+
{
15+
HbyAPtr.reset(new volVectorField("HbyA", U));
16+
HbyAPtr() = rAU * UEqn.H();
17+
}
18+
volVectorField& HbyA = HbyAPtr();
1119
surfaceScalarField phiHbyA("phiHbyA", fvc::flux(HbyA));
1220

1321
MRF.makeRelative(phiHbyA);

0 commit comments

Comments
 (0)