Skip to content

Commit

Permalink
Changed how the primalResTol is calculated (#587)
Browse files Browse the repository at this point in the history
* Changed how primalResTol is computed.

* Fixed tests.
  • Loading branch information
friedenhe authored Feb 15, 2024
1 parent 561f127 commit 376843f
Show file tree
Hide file tree
Showing 50 changed files with 178 additions and 203 deletions.
4 changes: 2 additions & 2 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,8 @@ def __init__(self):
}

## number of minimal primal iterations. The primal has to run this many iterations, even the primal residual
## has reduced below the tolerance. The default is a negative value (always satisfied).
self.primalMinIters = -1
## has reduced below the tolerance. The default is 1: the primal has to run for at least one iteration
self.primalMinIters = 1

## tensorflow related functions
self.tensorflow = {
Expand Down
7 changes: 2 additions & 5 deletions src/adjoint/DAModel/DATurbulenceModel/DASpalartAllmaras.C
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,8 @@ void DASpalartAllmaras::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverNuTilda = solve(nuTildaEqn);
if (printToScreen)
{
Info << "nuTilda Initial residual: " << solverNuTilda.initialResidual() << endl
<< " Final residual: " << solverNuTilda.finalResidual() << endl;
}

DAUtility::primalResidualControl(solverNuTilda, printToScreen, "nuTilda");

DAUtility::boundVar(allOptions_, nuTilda_, printToScreen);
nuTilda_.correctBoundaryConditions();
Expand Down
6 changes: 1 addition & 5 deletions src/adjoint/DAModel/DATurbulenceModel/DASpalartAllmarasFv3.C
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,7 @@ void DASpalartAllmarasFv3::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverNuTilda = solve(nuTildaEqn);
if (printToScreen)
{
Info << "nuTilda Initial residual: " << solverNuTilda.initialResidual() << endl
<< " Final residual: " << solverNuTilda.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverNuTilda, printToScreen, "nuTilda");

DAUtility::boundVar(allOptions_, nuTilda_, printToScreen);
nuTilda_.correctBoundaryConditions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,7 @@ void DASpalartAllmarasFv3FieldInversion::calcResiduals(const dictionary& options
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverNuTilda = solve(nuTildaEqn);
if (printToScreen)
{
Info << "nuTilda Initial residual: " << solverNuTilda.initialResidual() << endl
<< " Final residual: " << solverNuTilda.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverNuTilda, printToScreen, "nuTilda");

DAUtility::boundVar(allOptions_, nuTilda_, printToScreen);
nuTilda_.correctBoundaryConditions();
Expand Down
12 changes: 2 additions & 10 deletions src/adjoint/DAModel/DATurbulenceModel/DAkEpsilon.C
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,7 @@ void DAkEpsilon::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverEpsilon = solve(epsEqn);
if (printToScreen)
{
Info << "epsilon Initial residual: " << solverEpsilon.initialResidual() << endl
<< " Final residual: " << solverEpsilon.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverEpsilon, printToScreen, "epsilon");

DAUtility::boundVar(allOptions_, epsilon_, printToScreen);
}
Expand Down Expand Up @@ -610,11 +606,7 @@ void DAkEpsilon::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverK = solve(kEqn);
if (printToScreen)
{
Info << "k Initial residual: " << solverK.initialResidual() << endl
<< " Final residual: " << solverK.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverK, printToScreen, "k");

DAUtility::boundVar(allOptions_, k_, printToScreen);

Expand Down
12 changes: 2 additions & 10 deletions src/adjoint/DAModel/DATurbulenceModel/DAkOmega.C
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,7 @@ void DAkOmega::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverOmega = solve(omegaEqn);
if (printToScreen)
{
Info << "omega Initial residual: " << solverOmega.initialResidual() << endl
<< " Final residual: " << solverOmega.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverOmega, printToScreen, "omega");

DAUtility::boundVar(allOptions_, omega_, printToScreen);
}
Expand Down Expand Up @@ -614,11 +610,7 @@ void DAkOmega::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverK = solve(kEqn);
if (printToScreen)
{
Info << "k Initial residual: " << solverK.initialResidual() << endl
<< " Final residual: " << solverK.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverK, printToScreen, "k");

DAUtility::boundVar(allOptions_, k_, printToScreen);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,7 @@ void DAkOmegaFieldInversionOmega::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverOmega = solve(omegaEqn);
if (printToScreen)
{
Info << "omega Initial residual: " << solverOmega.initialResidual() << endl
<< " Final residual: " << solverOmega.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverOmega, printToScreen, "omega");

DAUtility::boundVar(allOptions_, omega_, printToScreen);
}
Expand Down Expand Up @@ -618,11 +614,7 @@ void DAkOmegaFieldInversionOmega::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverK = solve(kEqn);
if (printToScreen)
{
Info << "k Initial residual: " << solverK.initialResidual() << endl
<< " Final residual: " << solverK.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverK, printToScreen, "k");

DAUtility::boundVar(allOptions_, k_, printToScreen);

Expand Down
12 changes: 2 additions & 10 deletions src/adjoint/DAModel/DATurbulenceModel/DAkOmegaSST.C
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,7 @@ void DAkOmegaSST::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverOmega = solve(omegaEqn);
if (printToScreen)
{
Info << "omega Initial residual: " << solverOmega.initialResidual() << endl
<< " Final residual: " << solverOmega.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverOmega, printToScreen, "omega");

DAUtility::boundVar(allOptions_, omega_, printToScreen);
}
Expand Down Expand Up @@ -782,11 +778,7 @@ void DAkOmegaSST::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverK = solve(kEqn);
if (printToScreen)
{
Info << "k Initial residual: " << solverK.initialResidual() << endl
<< " Final residual: " << solverK.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverK, printToScreen, "k");

DAUtility::boundVar(allOptions_, k_, printToScreen);

Expand Down
12 changes: 2 additions & 10 deletions src/adjoint/DAModel/DATurbulenceModel/DAkOmegaSSTFIML.C
Original file line number Diff line number Diff line change
Expand Up @@ -1047,11 +1047,7 @@ void DAkOmegaSSTFIML::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverOmega = solve(omegaEqn);
if (printToScreen)
{
Info << "omega Initial residual: " << solverOmega.initialResidual() << endl
<< " Final residual: " << solverOmega.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverOmega, printToScreen, "omega");

DAUtility::boundVar(allOptions_, omega_, printToScreen);
}
Expand Down Expand Up @@ -1088,11 +1084,7 @@ void DAkOmegaSSTFIML::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverK = solve(kEqn);
if (printToScreen)
{
Info << "k Initial residual: " << solverK.initialResidual() << endl
<< " Final residual: " << solverK.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverK, printToScreen, "k");

DAUtility::boundVar(allOptions_, k_, printToScreen);

Expand Down
12 changes: 2 additions & 10 deletions src/adjoint/DAModel/DATurbulenceModel/DAkOmegaSSTFieldInversion.C
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,7 @@ void DAkOmegaSSTFieldInversion::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverOmega = solve(omegaEqn);
if (printToScreen)
{
Info << "omega Initial residual: " << solverOmega.initialResidual() << endl
<< " Final residual: " << solverOmega.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverOmega, printToScreen, "omega");

DAUtility::boundVar(allOptions_, omega_, printToScreen);
}
Expand Down Expand Up @@ -833,11 +829,7 @@ void DAkOmegaSSTFieldInversion::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverK = solve(kEqn);
if (printToScreen)
{
Info << "k Initial residual: " << solverK.initialResidual() << endl
<< " Final residual: " << solverK.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverK, printToScreen, "k");

DAUtility::boundVar(allOptions_, k_, printToScreen);

Expand Down
24 changes: 4 additions & 20 deletions src/adjoint/DAModel/DATurbulenceModel/DAkOmegaSSTLM.C
Original file line number Diff line number Diff line change
Expand Up @@ -1105,11 +1105,7 @@ void DAkOmegaSSTLM::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverOmega = solve(omegaEqn);
if (printToScreen)
{
Info << "omega Initial residual: " << solverOmega.initialResidual() << endl
<< " Final residual: " << solverOmega.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverOmega, printToScreen, "omega");

DAUtility::boundVar(allOptions_, omega_, printToScreen);
}
Expand Down Expand Up @@ -1146,11 +1142,7 @@ void DAkOmegaSSTLM::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverK = solve(kEqn);
if (printToScreen)
{
Info << "k Initial residual: " << solverK.initialResidual() << endl
<< " Final residual: " << solverK.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverK, printToScreen, "k");

DAUtility::boundVar(allOptions_, k_, printToScreen);

Expand Down Expand Up @@ -1207,11 +1199,7 @@ void DAkOmegaSSTLM::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverReThetat = solve(ReThetatEqn);
if (printToScreen)
{
Info << "ReThetat Initial residual: " << solverReThetat.initialResidual() << endl
<< " Final residual: " << solverReThetat.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverReThetat, printToScreen, "ReThetat");

DAUtility::boundVar(allOptions_, ReThetat_, printToScreen);
}
Expand Down Expand Up @@ -1259,11 +1247,7 @@ void DAkOmegaSSTLM::calcResiduals(const dictionary& options)
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverGammaInt = solve(gammaIntEqn);
if (printToScreen)
{
Info << "gammaInt Initial residual: " << solverGammaInt.initialResidual() << endl
<< " Final residual: " << solverGammaInt.finalResidual() << endl;
}
DAUtility::primalResidualControl(solverGammaInt, printToScreen, "gammaInt");

DAUtility::boundVar(allOptions_, gammaInt_, printToScreen);

Expand Down
5 changes: 3 additions & 2 deletions src/adjoint/DASolver/DAHeatTransferFoam/DAHeatTransferFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ label DAHeatTransferFoam::solvePrimal(
return 1;
}

primalMinRes_ = 1e10;
label printInterval = daOptionPtr_->getOption<label>("printIntervalUnsteady");
label printToScreen = 0;
// main loop
while (this->loop(runTime)) // using simple.loop() will have seg fault in parallel
{
DAUtility::primalMaxInitRes_ = -1e16;

printToScreen = this->isPrintTime(runTime, printInterval);

if (printToScreen)
Expand All @@ -102,7 +103,7 @@ label DAHeatTransferFoam::solvePrimal(
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverT = TEqn.solve();
this->primalResidualControl<scalar>(solverT, printToScreen, printInterval, "T");
DAUtility::primalResidualControl(solverT, printToScreen, "T");

if (this->validateStates())
{
Expand Down
5 changes: 3 additions & 2 deletions src/adjoint/DASolver/DALaplacianFoam/DALaplacianFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ label DALaplacianFoam::solvePrimal(
// right after mesh.movePoints() calls.
//mesh.moving(false);

primalMinRes_ = 1e10;
label printInterval = daOptionPtr_->getOption<label>("printIntervalUnsteady");
label printToScreen = 0;
label timeInstanceI = 0;

// main loop
while (this->loop(runTime)) // using simple.loop() will have seg fault in parallel
{
DAUtility::primalMaxInitRes_ = -1e16;

printToScreen = this->isPrintTime(runTime, printInterval);

if (printToScreen)
Expand All @@ -167,7 +168,7 @@ label DALaplacianFoam::solvePrimal(
// get the solver performance info such as initial
// and final residuals
SolverPerformance<scalar> solverT = TEqn.solve();
this->primalResidualControl<scalar>(solverT, printToScreen, printInterval, "T");
DAUtility::primalResidualControl(solverT, printToScreen, "T");

if (this->validateStates())
{
Expand Down
1 change: 0 additions & 1 deletion src/adjoint/DASolver/DAPimpleFoam/DAPimpleFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ label DAPimpleFoam::solvePrimal(
// right after mesh.movePoints() calls.
//mesh.moving(false);

primalMinRes_ = 1e10;
label printInterval = daOptionPtr_->getOption<label>("printIntervalUnsteady");
label printToScreen = 0;
label timeInstanceI = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/DASolver/DAPimpleFoam/UEqnPimple.H
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (pimple.momentumPredictor())
// and final residuals
SolverPerformance<vector> solverU = solve(UEqn == -fvc::grad(p));

this->primalResidualControl<vector>(solverU, pimplePrintToScreen, printInterval, "U");
DAUtility::primalResidualControl(solverU, pimplePrintToScreen, "U");
}

// bound U
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/DASolver/DAPimpleFoam/pEqnPimple.H
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ while (pimple.correctNonOrthogonal())
// and final residuals
SolverPerformance<scalar> solverP = pEqn.solve(mesh.solver(p.select(pimple.finalInnerIter())));

this->primalResidualControl<scalar>(solverP, pimplePrintToScreen, printInterval, "p");
DAUtility::primalResidualControl(solverP, pimplePrintToScreen, "p");

if (pimple.finalNonOrthogonalIter())
{
Expand Down
3 changes: 2 additions & 1 deletion src/adjoint/DASolver/DAPisoFoam/DAPisoFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,15 @@ label DAPisoFoam::solvePrimal(
// right after mesh.movePoints() calls.
//mesh.moving(false);

primalMinRes_ = 1e10;
label printInterval = daOptionPtr_->getOption<label>("printIntervalUnsteady");
label printToScreen = 0;
label timeInstanceI = 0;

// main loop
while (this->loop(runTime)) // using simple.loop() will have seg fault in parallel
{
DAUtility::primalMaxInitRes_ = -1e16;

printToScreen = this->isPrintTime(runTime, printInterval);

if (printToScreen)
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/DASolver/DAPisoFoam/UEqnPiso.H
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (piso.momentumPredictor())
// and final residuals
SolverPerformance<vector> solverU = solve(UEqn == -fvc::grad(p));

this->primalResidualControl<vector>(solverU, printToScreen, printInterval, "U");
DAUtility::primalResidualControl(solverU, printToScreen, "U");
}

// bound U
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/DASolver/DAPisoFoam/pEqnPiso.H
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ while (piso.correctNonOrthogonal())
// and final residuals
SolverPerformance<scalar> solverP = pEqn.solve(mesh.solver(p.select(piso.finalInnerIter())));

this->primalResidualControl<scalar>(solverP, printToScreen, printInterval, "p");
DAUtility::primalResidualControl(solverP, printToScreen, "p");

if (piso.finalNonOrthogonalIter())
{
Expand Down
3 changes: 2 additions & 1 deletion src/adjoint/DASolver/DARhoSimpleCFoam/DARhoSimpleCFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,13 @@ label DARhoSimpleCFoam::solvePrimal(
// check if the parameters are set in the Python layer
daRegressionPtr_->validate();

primalMinRes_ = 1e10;

label printInterval = daOptionPtr_->getOption<label>("printInterval");
label printToScreen = 0;
label regModelFail = 0;
while (this->loop(runTime)) // using simple.loop() will have seg fault in parallel
{
DAUtility::primalMaxInitRes_ = -1e16;

printToScreen = this->isPrintTime(runTime, printInterval);

Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/DASolver/DARhoSimpleCFoam/EEqnRhoSimpleC.H
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// and final residuals
SolverPerformance<scalar> solverE = EEqn.solve();

this->primalResidualControl<scalar>(solverE, printToScreen, printInterval, "he");
DAUtility::primalResidualControl(solverE, printToScreen, "he");

// bound he
DAUtility::boundVar(allOptions, he, printToScreen);
Expand Down
Loading

0 comments on commit 376843f

Please sign in to comment.