Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug for adjoint solution's fail flag. #643

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading