Skip to content

Commit 5ada9f7

Browse files
committed
Added the writeMinorIterations option to wirte primal solutions during minor iterations for debugging.
1 parent d94d83f commit 5ada9f7

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

dafoam/pyDAFoam.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,13 @@ class DAOPTION(object):
599599
## The max number of correctBoundaryConditions calls in the updateOFField function.
600600
maxCorrectBCCalls = 10
601601

602+
## Whether to write the primal solutions for minor iterations (i.e., line search).
603+
## The default is False. If set it to True, it will write flow fields (and the deformed geometry)
604+
## for each primal solution. This will significantly increases the IO runtime, so it should never
605+
## be True for production runs. However, it is useful for debugging purpose (e.g., to find out
606+
## the poor quality mesh during line search)
607+
writeMinorIterations = False
608+
602609
def __init__(self):
603610
"""
604611
Nothing needs to be done for initializing DAOPTION
@@ -679,8 +686,8 @@ def __init__(self, comm=None, options=None):
679686
self._initSolver()
680687

681688
# initialize the number of primal and adjoint calls
682-
self.nSolvePrimals = 0
683-
self.nSolveAdjoints = 0
689+
self.nSolvePrimals = 1
690+
self.nSolveAdjoints = 1
684691

685692
# flags for primal and adjoint failure
686693
self.primalFail = 0
@@ -1099,7 +1106,7 @@ def writeDesignVariable(self, fileName, xDV):
10991106
"""
11001107
# Write the design variable history to files
11011108
if self.comm.rank == 0:
1102-
if self.nSolveAdjoints == 0:
1109+
if self.nSolveAdjoints == 1:
11031110
f = open(fileName, "w")
11041111
else:
11051112
f = open(fileName, "a")
@@ -1144,7 +1151,7 @@ def writeTotalDeriv(self, fileName, sens, evalFuncs):
11441151
"""
11451152
# Write the sens history to files
11461153
if self.comm.rank == 0:
1147-
if self.nSolveAdjoints == 1:
1154+
if self.nSolveAdjoints == 2:
11481155
f = open(fileName, "w")
11491156
else:
11501157
f = open(fileName, "a")
@@ -1855,6 +1862,9 @@ def solvePrimal(self):
18551862
else:
18561863
self.primalFail = self.solver.solvePrimal(self.xvVec, self.wVec)
18571864

1865+
if self.getOption("writeMinorIterations"):
1866+
self.renameSolution(self.nSolvePrimals)
1867+
18581868
self.nSolvePrimals += 1
18591869

18601870
return
@@ -1889,7 +1899,8 @@ def solveAdjoint(self):
18891899
if self.getOption("useAD")["mode"] == "forward":
18901900
raise Error("solveAdjoint only supports useAD->mode=reverse|fd")
18911901

1892-
solutionTime = self.renameSolution(self.nSolveAdjoints)
1902+
if not self.getOption("writeMinorIterations"):
1903+
solutionTime = self.renameSolution(self.nSolveAdjoints)
18931904

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

@@ -1912,7 +1923,7 @@ def solveAdjoint(self):
19121923

19131924
# calculate dRdWTPC
19141925
adjPCLag = self.getOption("adjPCLag")
1915-
if self.nSolveAdjoints == 0 or self.nSolveAdjoints % adjPCLag == 0:
1926+
if self.nSolveAdjoints == 1 or (self.nSolveAdjoints - 1) % adjPCLag == 0:
19161927
self.dRdWTPC = PETSc.Mat().create(PETSc.COMM_WORLD)
19171928
self.solver.calcdRdWT(self.xvVec, self.wVec, 1, self.dRdWTPC)
19181929

@@ -2334,7 +2345,7 @@ def solveAdjoint(self):
23342345
self.nSolveAdjoints += 1
23352346

23362347
# we destroy dRdWTPC only when we need to recompute it next time
2337-
if self.nSolveAdjoints % adjPCLag == 0:
2348+
if (self.nSolveAdjoints - 1) % adjPCLag == 0:
23382349
self.dRdWTPC.destroy()
23392350

23402351
return
@@ -2714,7 +2725,7 @@ def renameSolution(self, solIndex):
27142725
Info("Solution time %g less than 1e-6, not moved." % float(solutionTime))
27152726
return solutionTime
27162727

2717-
distTime = "%.8f" % ((solIndex + 1) / 1e8)
2728+
distTime = "%.8f" % (solIndex / 1e8)
27182729

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

0 commit comments

Comments
 (0)