Skip to content

Commit

Permalink
Fixed a bug for adjoint solution's fail flag. (#643)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe authored May 29, 2024
1 parent 8a9c93a commit e40e2f8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/pyDASolvers/DASolvers.H
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/pyDASolvers/pyDASolvers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e40e2f8

Please sign in to comment.