Skip to content

Commit

Permalink
Added an option to use step-averaged states for steady-state solvers. (
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe authored Mar 28, 2024
1 parent fed0263 commit 1b56851
Show file tree
Hide file tree
Showing 12 changed files with 401 additions and 16 deletions.
13 changes: 13 additions & 0 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,19 @@ def __init__(self):
"defaultOutputValue": 0.0,
}

## whether to use averaged states
## This is useful for cases when steady-state solvers do not converge very well, e.g., flow
## separation. In these cases, the flow field and the objective function will oscillate and
## to get a better flow field and obj func value, we can use step-averaged (mean) states
## the start can be from 0 to 1. 0 means we use all time steps for averaging, while 1 means
## we use no time steps for averaging. 0.8 means we use the last 20% of the time step for averaging.
## We usually don't use 0 because the flow will need some spin up time, so using the spin-up
## flow field for meanStates will be slightly inaccurate.
self.useMeanStates = {
"active": False,
"start": 0.5
}

# *********************************************************************************************
# ************************************ Advance Options ****************************************
# *********************************************************************************************
Expand Down
8 changes: 8 additions & 0 deletions src/adjoint/DASolver/DARhoSimpleCFoam/DARhoSimpleCFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ label DARhoSimpleCFoam::solvePrimal(
// check if the parameters are set in the Python layer
daRegressionPtr_->validate();

// if useMeanStates is used, we need to zero meanStates before the primal run
this->zeroMeanStates();

label printInterval = daOptionPtr_->getOption<label>("printInterval");
label printToScreen = 0;
Expand Down Expand Up @@ -181,6 +183,9 @@ label DARhoSimpleCFoam::solvePrimal(
<< nl << endl;
}

// if useMeanStates is used, we need to calculate the meanStates
this->calcMeanStates();

runTime.write();
}

Expand All @@ -189,6 +194,9 @@ label DARhoSimpleCFoam::solvePrimal(
return 1;
}

// if useMeanStates is used, we need to assign meanStates to states right after the case converges
this->assignMeanStatesToStates();

this->calcPrimalResidualStatistics("print");

// primal converged, assign the OpenFoam fields to the state vec wVec
Expand Down
9 changes: 9 additions & 0 deletions src/adjoint/DASolver/DARhoSimpleFoam/DARhoSimpleFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ label DARhoSimpleFoam::solvePrimal(
// check if the parameters are set in the Python layer
daRegressionPtr_->validate();

// if useMeanStates is used, we need to zero meanStates before the primal run
this->zeroMeanStates();

label printInterval = daOptionPtr_->getOption<label>("printInterval");
label printToScreen = 0;
label regModelFail = 0;
Expand Down Expand Up @@ -197,6 +200,9 @@ label DARhoSimpleFoam::solvePrimal(
<< nl << endl;
}

// if useMeanStates is used, we need to calculate the meanStates
this->calcMeanStates();

runTime.write();
}

Expand All @@ -205,6 +211,9 @@ label DARhoSimpleFoam::solvePrimal(
return 1;
}

// if useMeanStates is used, we need to assign meanStates to states right after the case converges
this->assignMeanStatesToStates();

this->writeAssociatedFields();

this->calcPrimalResidualStatistics("print");
Expand Down
9 changes: 9 additions & 0 deletions src/adjoint/DASolver/DASimpleFoam/DASimpleFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ label DASimpleFoam::solvePrimal(
// check if the parameters are set in the Python layer
daRegressionPtr_->validate();

// if useMeanStates is used, we need to zero meanStates before the primal run
this->zeroMeanStates();

label printInterval = daOptionPtr_->getOption<label>("printInterval");
label printToScreen = 0;
label regModelFail = 0;
Expand Down Expand Up @@ -209,6 +212,9 @@ label DASimpleFoam::solvePrimal(
<< nl << endl;
}

// if useMeanStates is used, we need to calculate the meanStates
this->calcMeanStates();

runTime.write();
}

Expand All @@ -217,6 +223,9 @@ label DASimpleFoam::solvePrimal(
return 1;
}

// if useMeanStates is used, we need to assign meanStates to states right after the case converges
this->assignMeanStatesToStates();

this->writeAssociatedFields();

this->calcPrimalResidualStatistics("print");
Expand Down
9 changes: 9 additions & 0 deletions src/adjoint/DASolver/DASimpleTFoam/DASimpleTFoam.C
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ label DASimpleTFoam::solvePrimal(
// check if the parameters are set in the Python layer
daRegressionPtr_->validate();

// if useMeanStates is used, we need to zero meanStates before the primal run
this->zeroMeanStates();

label printInterval = daOptionPtr_->getOption<label>("printInterval");
label printToScreen = 0;
label regModelFail = 0;
Expand Down Expand Up @@ -189,6 +192,9 @@ label DASimpleTFoam::solvePrimal(
<< nl << endl;
}

// if useMeanStates is used, we need to calculate the meanStates
this->calcMeanStates();

runTime.write();
}

Expand All @@ -197,6 +203,9 @@ label DASimpleTFoam::solvePrimal(
return 1;
}

// if useMeanStates is used, we need to assign meanStates to states right after the case converges
this->assignMeanStatesToStates();

this->calcPrimalResidualStatistics("print");

// primal converged, assign the OpenFoam fields to the state vec wVec
Expand Down
Loading

0 comments on commit 1b56851

Please sign in to comment.