Skip to content

Commit f7dc586

Browse files
authored
Mphys multi dirs (#395)
* Multi_case opt now uses the mphys run_directory interface. * Turned off the report to avoid an error.
1 parent cbd04d1 commit f7dc586

File tree

3 files changed

+28
-53
lines changed

3 files changed

+28
-53
lines changed

dafoam/mphys/mphys_dafoam.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import numpy as np
99
from mpi4py import MPI
1010
from mphys import MaskedConverter, UnmaskedConverter, MaskedVariableDescription
11+
from mphys.utils.directory_utils import cd
1112

1213
petsc4py.init(sys.argv)
1314

@@ -23,6 +24,7 @@ def __init__(
2324
mesh_options=None, # IDWarp options
2425
scenario="aerodynamic", # scenario type to configure the groups
2526
prop_coupling=None,
27+
run_directory="", # the directory to run this case in, default is the current directory
2628
):
2729

2830
# options dictionary for DAFoam
@@ -44,6 +46,9 @@ def __init__(
4446
# flag for aerostructural coupling variables
4547
self.struct_coupling = False
4648

49+
# the directory to run this case in, default is the current directory
50+
self.run_directory = run_directory
51+
4752
# flag for aero-propulsive coupling variables
4853
self.prop_coupling = prop_coupling
4954

@@ -60,17 +65,20 @@ def __init__(
6065

6166
# api level method for all builders
6267
def initialize(self, comm):
68+
6369
self.comm = comm
64-
# initialize the PYDAFOAM class, defined in pyDAFoam.py
65-
self.DASolver = PYDAFOAM(options=self.options, comm=comm)
66-
# always set the mesh
67-
mesh = USMesh(options=self.mesh_options, comm=comm)
68-
self.DASolver.setMesh(mesh)
69-
# add the design surface family group
70-
self.DASolver.addFamilyGroup(
71-
self.DASolver.getOption("designSurfaceFamily"), self.DASolver.getOption("designSurfaces")
72-
)
73-
self.DASolver.printFamilyList()
70+
71+
with cd(self.run_directory):
72+
# initialize the PYDAFOAM class, defined in pyDAFoam.py
73+
self.DASolver = PYDAFOAM(options=self.options, comm=comm)
74+
# always set the mesh
75+
mesh = USMesh(options=self.mesh_options, comm=comm)
76+
self.DASolver.setMesh(mesh)
77+
# add the design surface family group
78+
self.DASolver.addFamilyGroup(
79+
self.DASolver.getOption("designSurfaceFamily"), self.DASolver.getOption("designSurfaces")
80+
)
81+
self.DASolver.printFamilyList()
7482

7583
def get_solver(self):
7684
# this method is only used by the RLT transfer scheme
@@ -756,7 +764,6 @@ def solve_linear(self, d_outputs, d_residuals, mode):
756764
# if writeMinorIterations=True, we rename the solution in pyDAFoam.py. So we don't recompute the PC
757765
if DASolver.getOption("writeMinorIterations"):
758766
if DASolver.dRdWTPC is None or DASolver.ksp is None:
759-
DASolver.cdRoot()
760767
DASolver.dRdWTPC = PETSc.Mat().create(self.comm)
761768
DASolver.solver.calcdRdWT(DASolver.xvVec, DASolver.wVec, 1, DASolver.dRdWTPC)
762769
DASolver.ksp = PETSc.KSP().create(self.comm)
@@ -782,7 +789,6 @@ def solve_linear(self, d_outputs, d_residuals, mode):
782789
adjPCLag = DASolver.getOption("adjPCLag")
783790
if DASolver.dRdWTPC is None or DASolver.ksp is None or (self.solution_counter - 1) % adjPCLag == 0:
784791
if renamed:
785-
DASolver.cdRoot()
786792
# calculate the PC mat
787793
if DASolver.dRdWTPC is not None:
788794
DASolver.dRdWTPC.destroy()
@@ -1116,7 +1122,7 @@ def compute_jacvec_product(self, inputs, d_inputs, d_outputs, mode):
11161122
dFdAOA.setSizes((PETSc.DECIDE, 1), bsize=1)
11171123
dFdAOA.setFromOptions()
11181124
DASolver.calcdFdAOAAnalytical(objFuncName, dFdAOA)
1119-
1125+
11201126
# The aoaBar variable will be length 1 on the root proc, but length 0 an all slave procs.
11211127
# The value on the root proc must be broadcast across all procs.
11221128
if self.comm.rank == 0:

dafoam/pyDAFoam.py

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
"""
1313

14-
__version__ = "3.0.3"
14+
__version__ = "3.0.4"
1515

1616
import subprocess
1717
import os
@@ -456,11 +456,6 @@ def __init__(self):
456456
# ************************************ Advance Options ****************************************
457457
# *********************************************************************************************
458458

459-
## The root directory for the DAFoam case. If rooDir = "None" (default), we will assign os.getcwd() to rootDir
460-
## If we want to have multiple cases running at the same time, e.g., coupled wing propeller case, we may
461-
## set different rootDirs for each case. NOTE: if we set rootDir, we need to set an absolute path!
462-
self.rootDir = "None"
463-
464459
## The run status which can be solvePrimal, solveAdjoint, or calcTotalDeriv. This parameter is
465460
## used internally, so users should never change this option in the Python layer.
466461
self.runStatus = "None"
@@ -664,13 +659,6 @@ def __init__(self, comm=None, options=None):
664659
# check if the combination of options is valid.
665660
self._checkOptions()
666661

667-
# the absolute path where the run script is located at
668-
if self.getOption("rootDir") == "None":
669-
self.rootDir = os.getcwd()
670-
else:
671-
self.rootDir = self.getOption("rootDir")
672-
self.cdRoot()
673-
674662
# Initialize families
675663
self.families = OrderedDict()
676664

@@ -807,8 +795,6 @@ def __call__(self):
807795
# update the mesh coordinates if DVGeo is set
808796
# add point set and update the mesh based on the DV values
809797

810-
self.cdRoot()
811-
812798
if self.DVGeo is not None:
813799

814800
# if the point set is not in DVGeo add it first
@@ -1659,9 +1645,7 @@ def writeFieldSensitivityMap(self, objFuncName, designVarName, solutionTime, fie
16591645
The Petsc vector that contains the sensitivity
16601646
"""
16611647

1662-
self.cdRoot()
1663-
1664-
workingDir = self.rootDir
1648+
workingDir = os.getcwd()
16651649
if self.parallel:
16661650
sensDir = "processor%d/%.8f/" % (self.rank, solutionTime)
16671651
else:
@@ -1736,9 +1720,7 @@ def writeSurfaceSensitivityMap(self, objFuncName, designVarName, solutionTime):
17361720
conn, faceSizes = self.getSurfaceConnectivity(self.allWallsGroup)
17371721
conn = np.array(conn).flatten()
17381722

1739-
self.cdRoot()
1740-
1741-
workingDir = self.rootDir
1723+
workingDir = os.getcwd()
17421724
if self.parallel:
17431725
meshDir = "processor%d/%.11f/polyMesh/" % (self.rank, solutionTime)
17441726
sensDir = "processor%d/%.11f/" % (self.rank, solutionTime)
@@ -1946,8 +1928,6 @@ def solveAdjoint(self):
19461928
viewerW(self.wVec)
19471929
"""
19481930

1949-
self.cdRoot()
1950-
19511931
if self.getOption("useAD")["mode"] == "forward":
19521932
raise Error("solveAdjoint only supports useAD->mode=reverse|fd")
19531933

@@ -2672,8 +2652,6 @@ def runColoring(self):
26722652
Info("| Running Coloring Solver |")
26732653
Info("+--------------------------------------------------------------------------+")
26742654

2675-
self.cdRoot()
2676-
26772655
solverName = self.getOption("solverName")
26782656
if solverName in self.solverRegistry["Incompressible"]:
26792657

@@ -2729,8 +2707,7 @@ def deletePrevPrimalSolTime(self):
27292707

27302708
solTime = self.solver.getPrevPrimalSolTime()
27312709

2732-
self.cdRoot()
2733-
rootDir = self.rootDir
2710+
rootDir = os.getcwd()
27342711
if self.parallel:
27352712
checkPath = os.path.join(rootDir, "processor%d/%g" % (self.comm.rank, solTime))
27362713
else:
@@ -2763,8 +2740,7 @@ def renameSolution(self, solIndex):
27632740
"""
27642741

27652742
allSolutions = []
2766-
self.cdRoot()
2767-
rootDir = self.rootDir
2743+
rootDir = os.getcwd()
27682744
if self.parallel:
27692745
checkPath = os.path.join(rootDir, "processor%d" % self.comm.rank)
27702746
else:
@@ -2952,8 +2928,7 @@ def _readMeshInfo(self):
29522928
Initialize mesh information and read mesh information
29532929
"""
29542930

2955-
self.cdRoot()
2956-
dirName = self.rootDir
2931+
dirName = os.getcwd()
29572932

29582933
self.fileNames, self.xv0, self.faces, self.boundaries, self.owners, self.neighbours = self._readOFGrid(dirName)
29592934
self.xv = copy.copy(self.xv0)
@@ -3769,12 +3744,6 @@ def vec2ArraySeq(self, vec):
37693744
array1[i] = vec[i]
37703745
return array1
37713746

3772-
def cdRoot(self):
3773-
"""
3774-
Go to the case root dir, as set in self.rootDir
3775-
"""
3776-
os.chdir(self.rootDir)
3777-
37783747
def _printCurrentOptions(self):
37793748
"""
37803749
Prints a nicely formatted dictionary of all the current solver
@@ -3806,8 +3775,8 @@ def _writeDecomposeParDict(self):
38063775
"""
38073776
if self.comm.rank == 0:
38083777
# Open the options file for writing
3809-
self.cdRoot()
3810-
workingDirectory = self.rootDir
3778+
3779+
workingDirectory = os.getcwd()
38113780
sysDir = "system"
38123781
varDir = os.path.join(workingDirectory, sysDir)
38133782
fileName = "decomposeParDict"

tests/runTests_MphysAeroField.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def alphaPorosity(val, DASolver):
157157
self.add_constraint("cruise.aero_post.CL", equals=0.3, scaler=1.0)
158158

159159

160-
prob = om.Problem()
160+
prob = om.Problem(reports=None)
161161
prob.model = Top()
162162

163163
prob.driver = om.pyOptSparseDriver()

0 commit comments

Comments
 (0)