From 3bd4b61ef91af620cbe9095282411e40c1168b91 Mon Sep 17 00:00:00 2001 From: Ping He Date: Sun, 7 Apr 2024 11:35:40 -0500 Subject: [PATCH] Fixed a potential issue for calcMeanStates. (#620) --- src/adjoint/DASolver/DASolver.C | 14 +++++++++++++- src/adjoint/DASolver/DASolver.H | 2 ++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/adjoint/DASolver/DASolver.C b/src/adjoint/DASolver/DASolver.C index 44533e81..b4322bc8 100644 --- a/src/adjoint/DASolver/DASolver.C +++ b/src/adjoint/DASolver/DASolver.C @@ -9686,9 +9686,11 @@ void DASolver::assignMeanStatesToStates() /* Description: Assigned the calculated meanStates to the primal states and update intermediate vars + NOTE: if meanStatesCalculated_ == 0, we will not assignMeanStatesToStates at the end of the primal. + meanStatesCalculated_ is assigned to 1 if timeIndex >= startTimeIndex in DASolver::calcMeanStates */ - if (!useMeanStates_) + if (!useMeanStates_ || !meanStatesCalculated_) { return; } @@ -9729,6 +9731,9 @@ void DASolver::assignMeanStatesToStates() // update state BC and intermedate vars this->updateStateBoundaryConditions(); + + // after the meanStates is assigned to states, reset meanStatesCalculated_ for the next primal solution. + meanStatesCalculated_ = 0; } void DASolver::calcMeanStates() @@ -9809,6 +9814,13 @@ void DASolver::calcMeanStates() } } } + + // if we have caluclate mean states, i.e., timeIndex >= startTimeIndex, set meanStatesCalculated_ = 1 + // this is to avoid setting a large startTime but the flow somehow converge before the startTime is + // triggered. In this case, the meanStates is never calculated and will return the wrong results + // if meanStatesCalculated_ == 0, we will not assignMeanStatesToStates at the end of the primal + // check DASolver::assignMeanStatesToStates + meanStatesCalculated_ = 1; } } diff --git a/src/adjoint/DASolver/DASolver.H b/src/adjoint/DASolver/DASolver.H index 492a98cd..d85a2950 100644 --- a/src/adjoint/DASolver/DASolver.H +++ b/src/adjoint/DASolver/DASolver.H @@ -209,6 +209,8 @@ protected: label useMeanStates_ = 0; /// if the meanStates is used, what is the step-averaging start scalar meanStateStart_ = 0.5; + /// whether we have calculated the meanStates, if not (flow converge before meanStateStart_), we will not use meanStates + label meanStatesCalculated_ = 0; /// step-averaged scalar states PtrList meanVolScalarStates_;