From adfd947ffac19dcc564e99313dd892e2873c90bc Mon Sep 17 00:00:00 2001 From: Ping He Date: Tue, 28 May 2024 22:54:27 -0500 Subject: [PATCH] Fixed a bug for adjoint solution's fail flag. --- dafoam/pyDAFoam.py | 7 ++++++- src/pyDASolvers/DASolvers.H | 4 ++-- src/pyDASolvers/pyDASolvers.pyx | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/dafoam/pyDAFoam.py b/dafoam/pyDAFoam.py index 52570295..6c2ba427 100755 --- a/dafoam/pyDAFoam.py +++ b/dafoam/pyDAFoam.py @@ -2379,6 +2379,7 @@ def solveAdjointUnsteady(self): self.adjTotalDeriv = self._initializeAdjTotalDeriv() # loop over all objFunc, calculate dFdW, and solve the adjoint + self.adjointFail = 0 for objFuncName in objFuncDict: if objFuncName in self.objFuncNames4Adj: # zero the vecs @@ -2438,7 +2439,7 @@ def solveAdjointUnsteady(self): self.solver.calcPCMatWithFvMatrix(PCMat) # now solve the adjoint eqn - self.adjointFail = self.solverAD.solveLinearEqn(ksp, dFdW, self.adjVectors[objFuncName]) + self.adjointFail += self.solverAD.solveLinearEqn(ksp, dFdW, self.adjVectors[objFuncName]) # loop over all the design vars and accumulate totals for designVarName in designVarDict: @@ -2470,6 +2471,10 @@ def solveAdjointUnsteady(self): self.solverAD.calcdRdWOldTPsiAD(1, self.adjVectors[objFuncName], dRdW0TPsi) self.solverAD.calcdRdWOldTPsiAD(2, self.adjVectors[objFuncName], dRdW00TPsiBuffer) + # if one adjoint solution fails, return immediate without solving for the rest of steps. + if self.adjointFail > 0: + break + self.nSolveAdjoints += 1 def solveAdjoint(self): diff --git a/src/pyDASolvers/DASolvers.H b/src/pyDASolvers/DASolvers.H index 689e6c9c..5275b3b5 100755 --- a/src/pyDASolvers/DASolvers.H +++ b/src/pyDASolvers/DASolvers.H @@ -225,12 +225,12 @@ public: } /// solve the linear equation - void solveLinearEqn( + label solveLinearEqn( const KSP ksp, const Vec rhsVec, Vec solVec) { - DASolverPtr_->solveLinearEqn(ksp, rhsVec, solVec); + return DASolverPtr_->solveLinearEqn(ksp, rhsVec, solVec); } /// convert the mpi vec to a seq vec diff --git a/src/pyDASolvers/pyDASolvers.pyx b/src/pyDASolvers/pyDASolvers.pyx index b030181f..e36ffee8 100755 --- a/src/pyDASolvers/pyDASolvers.pyx +++ b/src/pyDASolvers/pyDASolvers.pyx @@ -52,7 +52,7 @@ cdef extern from "DASolvers.H" namespace "Foam": void createMLRKSP(PetscMat, PetscMat, PetscKSP) void createMLRKSPMatrixFree(PetscMat, PetscKSP) void updateKSPPCMat(PetscMat, PetscKSP) - void solveLinearEqn(PetscKSP, PetscVec, PetscVec) + int solveLinearEqn(PetscKSP, PetscVec, PetscVec) void calcdRdBC(PetscVec, PetscVec, char *, PetscMat) void calcdFdBC(PetscVec, PetscVec, char *, char *, PetscVec) void calcdFdBCAD(PetscVec, PetscVec, char *, char *, PetscVec) @@ -227,7 +227,7 @@ cdef class pyDASolvers: self._thisptr.updateKSPPCMat(PCMat.mat, myKSP.ksp) def solveLinearEqn(self, KSP myKSP, Vec rhsVec, Vec solVec): - self._thisptr.solveLinearEqn(myKSP.ksp, rhsVec.vec, solVec.vec) + return self._thisptr.solveLinearEqn(myKSP.ksp, rhsVec.vec, solVec.vec) def calcdRdBC(self, Vec xvVec, Vec wVec, designVarName, Mat dRdBC): self._thisptr.calcdRdBC(xvVec.vec, wVec.vec, designVarName, dRdBC.mat)