Skip to content

Commit

Permalink
Added an option to enable constrainHbyA.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Sep 16, 2023
1 parent a00afb1 commit 3d9ab64
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 50 deletions.
9 changes: 8 additions & 1 deletion dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"""

__version__ = "3.0.7"
__version__ = "3.0.8"

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

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

# *********************************************************************************************
# ************************************ Advance Options ****************************************
# *********************************************************************************************
Expand Down
16 changes: 9 additions & 7 deletions src/adjoint/DAResidual/DAResidualRhoSimpleCFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,17 @@ void DAResidualRhoSimpleCFoam::calcResiduals(const dictionary& options)
// copied and modified from pEqn.H
volScalarField rAU(1.0 / UEqn.A());
volScalarField rAtU(1.0 / (1.0 / rAU - UEqn.H1()));
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U_, p_));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U_);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}

tUEqn.clear();

surfaceScalarField phiHbyA("phiHbyA", fvc::interpolate(rho_) * fvc::flux(HbyA));
Expand Down Expand Up @@ -191,7 +194,6 @@ void DAResidualRhoSimpleCFoam::calcResiduals(const dictionary& options)
// copied and modified from pEqn.H
phiRes_ = phiHbyA + pEqn.flux() - phi_;
normalizePhiResiduals(phiRes);

}

void DAResidualRhoSimpleCFoam::updateIntermediateVariables()
Expand Down
15 changes: 9 additions & 6 deletions src/adjoint/DAResidual/DAResidualRhoSimpleFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,17 @@ void DAResidualRhoSimpleFoam::calcResiduals(const dictionary& options)
// copied and modified from pEqn.H
volScalarField rAU(1.0 / UEqn.A());
surfaceScalarField rhorAUf("rhorAUf", fvc::interpolate(rho_ * rAU));
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U_, p_));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U_);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}

tUEqn.clear();

surfaceScalarField phiHbyA("phiHbyA", fvc::interpolate(rho_) * fvc::flux(HbyA));
Expand Down
15 changes: 9 additions & 6 deletions src/adjoint/DAResidual/DAResidualSimpleFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,17 @@ void DAResidualSimpleFoam::calcResiduals(const dictionary& options)
scalar pRefValue = 0.0;

volScalarField rAU(1.0 / UEqn.A());
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U_, p_));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U_, p_));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U_);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}


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

Expand Down
14 changes: 8 additions & 6 deletions src/adjoint/DAResidual/DAResidualSimpleTFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,16 @@ void DAResidualSimpleTFoam::calcResiduals(const dictionary& options)
scalar pRefValue = 0.0;

volScalarField rAU(1.0 / UEqn.A());
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U_, p_));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U_, p_));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U_);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOption_.getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}

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

Expand Down
15 changes: 9 additions & 6 deletions src/adjoint/DASolver/DARhoSimpleCFoam/pEqnRhoSimpleC.H
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ rho = thermo.rho();

volScalarField rAU(1.0 / UEqn.A());
volScalarField rAtU(1.0 / (1.0 / rAU - UEqn.H1()));
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U, p));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}

tUEqn.clear();

bool closedVolume = false;
Expand Down
15 changes: 9 additions & 6 deletions src/adjoint/DASolver/DARhoSimpleFoam/pEqnRhoSimple.H
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
volScalarField rAU(1.0 / UEqn.A());
surfaceScalarField rhorAUf("rhorAUf", fvc::interpolate(rho* rAU));
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U, p));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}

tUEqn.clear();

bool closedVolume = false;
Expand Down
14 changes: 8 additions & 6 deletions src/adjoint/DASolver/DASimpleFoam/pEqnSimple.H
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
volScalarField rAU(1.0 / UEqn.A());
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U, p));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}
surfaceScalarField phiHbyA("phiHbyA", fvc::flux(HbyA));

MRF.makeRelative(phiHbyA);
Expand Down
14 changes: 8 additions & 6 deletions src/adjoint/DASolver/DASimpleTFoam/pEqnSimpleT.H
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
volScalarField rAU(1.0 / UEqn.A());
//volVectorField HbyA(constrainHbyA(rAU*UEqn.H(), U, p));
volVectorField HbyA(constrainHbyA(rAU * UEqn.H(), U, p));
//***************** NOTE *******************
// constrainHbyA has been used since OpenFOAM-v1606; however, We do NOT use the constrainHbyA
// function in DAFoam because we found it significantly degrades the accuracy of shape derivatives.
// constrainHbyA has been used since OpenFOAM-v1606; however, it may degrade the accuracy of derivatives.
// Basically, we should not constrain any variable because it will create discontinuity.
// Instead, we use the old implementation used in OpenFOAM-3.0+ and before
volVectorField HbyA("HbyA", U);
HbyA = rAU * UEqn.H();
// Here we have a option to use the old implementation in OpenFOAM-3.0+ and before (no constraint for HbyA)
label useConstrainHbyA = daOptionPtr_->getOption<label>("useConstrainHbyA");
if (!useConstrainHbyA)
{
HbyA = rAU * UEqn.H();
}
surfaceScalarField phiHbyA("phiHbyA", fvc::flux(HbyA));

MRF.makeRelative(phiHbyA);
Expand Down

0 comments on commit 3d9ab64

Please sign in to comment.