Skip to content

Commit

Permalink
Added new interfaces for inputs (#733)
Browse files Browse the repository at this point in the history
* Removed 2> log for test files.

* Re-organize input and output. Added patchVelocity

* Added dF back for patchVelocity.

* Enabled CD and CL based on AOA.

* Enabled auto make mode.

* Print info for DAInput and calcJac.

* Changed designVar to solverInput and re-organized all partial calls.
  • Loading branch information
friedenhe authored Jan 1, 2025
1 parent ad9434c commit d5680e8
Show file tree
Hide file tree
Showing 40 changed files with 970 additions and 845 deletions.
1 change: 0 additions & 1 deletion .github/workflows/code_cov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ jobs:
docker exec -i regtest /bin/bash -c "rm -rf ${{env.DOCKER_WORKING_DIR}} && cp -r ${{env.DOCKER_MOUNT_DIR}} ${{env.DOCKER_WORKING_DIR}}"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/src && . ./.add_coverage_flag.sh"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && . ${{env.DOCKER_OF_ADR_BASHRC}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake 2> log && pip install ."
docker exec -i -e DF_CHECK_COVERAGE=1 regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/tests && ./Allrun"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/tests && coverage combine && coverage xml && echo dafoamuser | sudo -S cp -r coverage.xml ${{env.DOCKER_MOUNT_DIR}}/"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}} && lcov --capture --directory . --output-file coverage.info && echo dafoamuser | sudo -S cp -r coverage.info ${{env.DOCKER_MOUNT_DIR}}/"
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ jobs:
docker run -i -d -u dafoamuser --name regtest -v $GITHUB_WORKSPACE:${{env.DOCKER_MOUNT_DIR}} dafoam/opt-packages:${{env.DOCKER_TAG}} /bin/bash
docker exec -i regtest /bin/bash -c "rm -rf ${{env.DOCKER_WORKING_DIR}} && cp -r ${{env.DOCKER_MOUNT_DIR}} ${{env.DOCKER_WORKING_DIR}}"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake"
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && . ${{env.DOCKER_OF_ADR_BASHRC}} && cd ${{env.DOCKER_WORKING_DIR}} && ./Allmake 2> log && pip install ."
docker exec -i regtest /bin/bash -c ". ${{env.DOCKER_ENV_FILE}} && cd ${{env.DOCKER_WORKING_DIR}}/tests && ./Allrun"
45 changes: 36 additions & 9 deletions Allmake
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
#!/usr/bin/env bash

# the script will exit if there is any error
set -e

if [ -z "$WM_PROJECT" ]; then
echo "OpenFOAM environment not found, forgot to source the OpenFOAM bashrc?"
exit 1
fi

wmakeLnInclude src/adjoint
cd src/adjoint && ./Allmake && cd -
cd src/pyUnitTests && ./Allmake && cd -
cd src/pyDASolvers && ./Allmake && cd -
cd src/utilities/coloring && ./Allmake && cd -
echo "******* DAFoam $WM_CODI_AD_LIB_POSTFIX compilation finished! *********"
function makeDAFoam()
{
# the script will exit if there is any error
set -e
wmakeLnInclude src/adjoint
cd src/adjoint && ./Allmake && cd -
cd src/pyUnitTests && ./Allmake && cd -
cd src/pyDASolvers && ./Allmake && cd -
cd src/utilities/coloring && ./Allmake && cd -
# disable the -e flag
set +e
}

# compile original mode
echo "***************** Compiling original mode **************"
. $DAFOAM_ROOT_PATH/loadDAFoam.sh
makeDAFoam
# compile ADR mode
echo "***************** Compiling ADR mode **************"
. $DAFOAM_ROOT_PATH/loadDAFoam.sh
. $DAFOAM_ROOT_PATH/OpenFOAM/OpenFOAM-v1812-ADR/etc/bashrc
makeDAFoam
# if COMPILE_DAFOAM_ADF is set, compile ADF mode
if [ -z "$COMPILE_DAFOAM_ADF" ]; then
echo "COMPILE_DAFOAM_ADF is not set. skip the ADF mode"
else
echo "***************** Compiling ADF mode **************"
. $DAFOAM_ROOT_PATH/loadDAFoam.sh
. $DAFOAM_ROOT_PATH/OpenFOAM/OpenFOAM-v1812-ADF/etc/bashrc
makeDAFoam
fi

# reset the OpenFOAM environment to the original mode
. $DAFOAM_ROOT_PATH/loadDAFoam.sh

pip install .
763 changes: 180 additions & 583 deletions dafoam/mphys/mphys_dafoam.py

Large diffs are not rendered by default.

87 changes: 43 additions & 44 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,27 @@ def __init__(self):
## },
self.function = {}

## Design variable information. Different type of design variables require different keys
## For alpha, we need to prescribe a list of far field patch names from which the angle of
## Solver input information. Different type of design variables require different keys
## For patchVelocity, we need to set a list of far field patch names from which the angle of
## attack is computed, this is usually a far field patch. Also, we need to prescribe
## flow and normal axies, and alpha = atan( U_normal / U_flow ) at patches
## Example
## designVar = {
## "shapey" : {"designVarType": "FFD"},
## "twist": {"designVarType": "FFD"},
## "alpha" = {
## "designVarType": "AOA",
## solverInput = {
## "aero_vol_coords" : {"type": "volCoord"},
## "patchV" = {
## "type": "patchVelocity",
## "patches": ["farField"],
## "flowAxis": "x",
## "normalAxis": "y"
## },
## "ux0" = {
## "designVarType": "BC",
## "type": "patchVariable",
## "patches": ["inlet"],
## "variable": "U",
## "comp": 0
## },
## }
self.designVar = {}
self.solverInput = {}

## List of patch names for the design surface. These patch names need to be of wall type
## and shows up in the constant/polyMesh/boundary file
Expand Down Expand Up @@ -755,17 +754,6 @@ def __init__(self):
# }
}

## An internal dict for DAInput options, it will be used to transfer options between mphys_dafoam and OF layers
## Users should not use it in runScript.py
self._inputOptions = {}

## An internal dict for DAInput options, it will be used to transfer options between mphys_dafoam and OF layers
## Users should not use it in runScript.py
self._outputOptions = {
"functionName": "None", # used in DAOutputFunction
"isPC": 0, # used in DAOutputResidual
}


class PYDAFOAM(object):
"""
Expand Down Expand Up @@ -834,6 +822,9 @@ def __init__(self, comm=None, options=None):
# initialize the pySolvers
self.solverInitialized = 0
self._initSolver()

# set the primal boundary condition after initializing the solver
self.setPrimalBoundaryConditions()

# initialize the number of primal and adjoint calls
self.nSolvePrimals = 1
Expand Down Expand Up @@ -977,15 +968,37 @@ def __call__(self):
# add point set and update the mesh based on the DV values

# now call the internal design var function to update DASolver parameters
self.runInternalDVFunc()
# self.runInternalDVFunc()

# update the primal boundary condition right before calling solvePrimal
self.setPrimalBoundaryConditions()
# self.setPrimalBoundaryConditions()

Info("Running Primal Solver %03d" % self.nSolvePrimals)

self.deletePrevPrimalSolTime()

# self.primalFail: if the primal solution fails, assigns 1, otherwise 0
self.primalFail = 0
if self.getOption("useAD")["mode"] == "forward":
self.primalFail = self.solverAD.solvePrimal()
else:
self.primalFail = self.solver.solvePrimal()

# solve the primal to get new state variables
self.solvePrimal()
if self.getOption("writeMinorIterations"):
self.renameSolution(self.nSolvePrimals)
self.writeDeformedFFDs(self.nSolvePrimals)

self.nSolvePrimals += 1

return

def setRunStatus(self, status):
"""
Set the DAGlobalVar.runStatus value
"""

self.solver.setRunStatus(status)
self.solverAD.setRunStatus(status)

def _getDefOptions(self):
"""
Expand Down Expand Up @@ -1048,7 +1061,7 @@ def _initializeAdjTotalDeriv(self):
An empty dict that contains total derivative of objective function with respect design variables
"""

designVarDict = self.getOption("designVar")
designVarDict = self.getOption("solverInput")
functionDict = self.getOption("function")

adjTotalDeriv = {}
Expand Down Expand Up @@ -1934,21 +1947,7 @@ def solvePrimal(self):
self.primalFail: if the primal solution fails, assigns 1, otherwise 0
"""

Info("Running Primal Solver %03d" % self.nSolvePrimals)

self.deletePrevPrimalSolTime()

self.primalFail = 0
if self.getOption("useAD")["mode"] == "forward":
self.primalFail = self.solverAD.solvePrimal()
else:
self.primalFail = self.solver.solvePrimal()

if self.getOption("writeMinorIterations"):
self.renameSolution(self.nSolvePrimals)
self.writeDeformedFFDs(self.nSolvePrimals)

self.nSolvePrimals += 1


return

Expand Down Expand Up @@ -2353,7 +2352,7 @@ def solveAdjointUnsteady(self):
self.solverAD.createMLRKSPMatrixFree(PCMat, ksp)

functionDict = self.getOption("function")
designVarDict = self.getOption("designVar")
designVarDict = self.getOption("solverInput")

# init the dFdW vec
wSize = self.solver.getNLocalAdjointStates()
Expand Down Expand Up @@ -2569,7 +2568,7 @@ def solveAdjoint(self):
# ************ Now compute the total derivatives **********************
Info("Computing total derivatives....")

designVarDict = self.getOption("designVar")
designVarDict = self.getOption("solverInput")
for designVarName in designVarDict:
Info("Computing total derivatives for %s" % designVarName)
###################### BC: boundary condition as design variable ###################
Expand Down Expand Up @@ -4115,7 +4114,7 @@ def getNLocalAdjointStates(self):
Get number of local adjoint states
"""
return self.solver.getNLocalAdjointStates()

def getNLocalPoints(self):
"""
Get number of local points
Expand Down Expand Up @@ -4179,7 +4178,7 @@ def getResiduals(self):
residuals[iRel] = resVec[i]

return residuals

def setVolCoords(self, volCoords):
"""
Set the volCoords to the OpenFOAM's mesh coordinate
Expand Down
Loading

0 comments on commit d5680e8

Please sign in to comment.