Skip to content

Commit

Permalink
Fixed a bug for the renameSolution. (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe authored Jan 24, 2024
1 parent 205c9c3 commit 0f42fe5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
25 changes: 8 additions & 17 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2469,7 +2469,7 @@ def solveAdjoint(self):
raise Error("solveAdjoint only supports useAD->mode=reverse|fd")

if not self.getOption("writeMinorIterations"):
solutionTime, renamed = self.renameSolution(self.nSolveAdjoints)
self.renameSolution(self.nSolveAdjoints)

Info("Running adjoint Solver %03d" % self.nSolveAdjoints)

Expand Down Expand Up @@ -3233,35 +3233,26 @@ def renameSolution(self, solIndex):
The major interation index
"""

allSolutions = []
rootDir = os.getcwd()
if self.parallel:
checkPath = os.path.join(rootDir, "processor%d" % self.comm.rank)
else:
checkPath = rootDir

folderNames = os.listdir(checkPath)
for folderName in folderNames:
try:
float(folderName)
allSolutions.append(folderName)
except ValueError:
continue
allSolutions.sort(reverse=True)
# choose the latst solution to rename
solutionTime = allSolutions[0]
latestTime = self.solver.getLatestTime()

if float(solutionTime) < 1e-4:
Info("Solution time %g less than 1e-4, not renamed." % float(solutionTime))
if latestTime < 1e-4:
Info("Latest solution time %g less than 1e-4, not renamed." % latestTime)
renamed = False
return solutionTime, renamed
return latestTime, renamed

distTime = "%g" % (solIndex / 1e8)
targetTime = "%g" % latestTime

src = os.path.join(checkPath, solutionTime)
src = os.path.join(checkPath, targetTime)
dst = os.path.join(checkPath, distTime)

Info("Moving time %s to %s" % (solutionTime, distTime))
Info("Moving time %s to %s" % (targetTime, distTime))

if os.path.isdir(dst):
raise Error("%s already exists, moving failed!" % dst)
Expand Down
8 changes: 8 additions & 0 deletions src/adjoint/DASolver/DASolver.H
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,14 @@ public:
const double* dFdField,
const word fieldType,
const double timeName);

scalar getLatestTime()
{
// get the latest time solution from the case folder.
instantList timeDirs = runTimePtr_->findTimes(runTimePtr_->path(), runTimePtr_->constant());
scalar latestTime = timeDirs.last().value();
return latestTime;
}
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
13 changes: 13 additions & 0 deletions src/pyDASolvers/DASolvers.H
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,19 @@ public:
{
DASolverPtr_->writeSensMapField(name, dFdField, fieldType, timeName);
}

/// get the latest solution time
double getLatestTime()
{
double latestTime = 0.0;
#if !defined(CODI_AD_REVERSE) && !defined(CODI_AD_FORWARD)
latestTime = DASolverPtr_->getLatestTime();
#else
scalar tmp = DASolverPtr_->getLatestTime();
latestTime = tmp.getValue();
#endif
return latestTime;
};
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
4 changes: 4 additions & 0 deletions src/pyDASolvers/pyDASolvers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ cdef extern from "DASolvers.H" namespace "Foam":
int getUnsteadyObjFuncEndTimeIndex()
void writeSensMapSurface(char *, double *, double *, int, double)
void writeSensMapField(char *, double *, char *, double)
double getLatestTime()

# create python wrappers that call cpp functions
cdef class pyDASolvers:
Expand Down Expand Up @@ -690,3 +691,6 @@ cdef class pyDASolvers:
dFdField_data,
fieldType.encode(),
timeName)

def getLatestTime(self):
return self._thisptr.getLatestTime()

0 comments on commit 0f42fe5

Please sign in to comment.