Revision 561f127d4ba2eea2a762f35b937ba43257c66f3a authored by Ping He on 12 February 2024, 23:57:20 UTC, committed by GitHub on 12 February 2024, 23:57:20 UTC
1 parent 69e5bc8
runTests_DASolidDisplacementFoam.py
#!/usr/bin/env python
"""
Run Python tests for DASolidDisplacementFoam
"""
from mpi4py import MPI
from dafoam import PYDAFOAM, optFuncs
import sys
import os
from pygeo import *
from pyspline import *
from idwarp import *
from pyoptsparse import Optimization, OPT
import numpy as np
from testFuncs import *
calcFDSens = 0
if len(sys.argv) != 1:
if sys.argv[1] == "calcFDSens":
calcFDSens = 1
gcomm = MPI.COMM_WORLD
os.chdir("./input/PlateHole")
if gcomm.rank == 0:
os.system("rm -rf processor*")
# test incompressible solvers
aeroOptions = {
"debug": True,
"solverName": "DASolidDisplacementFoam",
"useAD": {"mode": "reverse"},
"designSurfaces": ["hole"],
"primalMinResTol": 1e-10,
"primalMinResTolDiff": 1e10,
"hasIterativeBC": True,
"maxCorrectBCCalls": 20,
"objFunc": {
"VMS": {
"part1": {
"type": "vonMisesStressKS",
"source": "boxToCell",
"min": [-10.0, -10.0, -10.0],
"max": [10.0, 10.0, 10.0],
"scale": 1.0,
"coeffKS": 2.0e-3,
"addToAdjoint": True,
}
},
"M": {
"part1": {
"type": "mass",
"source": "boxToCell",
"min": [-10.0, -10.0, -10.0],
"max": [10.0, 10.0, 10.0],
"scale": 1.0,
"addToAdjoint": True,
}
},
},
"normalizeStates": {"D": 1.0e-7},
"adjPartDerivFDStep": {"State": 1e-5, "FFD": 1e-3},
"adjEqnOption": {"gmresRelTol": 1.0e-12, "gmresAbsTol": 1.0e-12, "pcFillLevel": 1, "jacMatReOrdering": "rcm"},
# Design variable setup
"designVar": {
"shapey": {"designVarType": "FFD"},
"shapex": {"designVarType": "FFD"}
},
}
# mesh warping parameters, users need to manually specify the symmetry plane
meshOptions = {
"gridFile": os.getcwd(),
"fileType": "OpenFOAM",
# point and normal for the symmetry plane
"symmetryPlanes": [[[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]], [[0.0, 0.0, 0.1], [0.0, 0.0, 1.0]]],
}
# DVGeo
FFDFile = "./FFD/plateFFD.xyz"
DVGeo = DVGeometry(FFDFile)
# select points
pts = DVGeo.getLocalIndex(0)
indexList = pts[2:5, 2, 0].flatten()
PS = geo_utils.PointSelect("list", indexList)
DVGeo.addLocalDV("shapey", lower=-1.0, upper=1.0, axis="y", scale=1.0, pointSelect=PS)
indexList = pts[4, 2:5, 0].flatten()
PS = geo_utils.PointSelect("list", indexList)
DVGeo.addLocalDV("shapex", lower=-1.0, upper=1.0, axis="x", scale=1.0, pointSelect=PS)
# DAFoam
DASolver = PYDAFOAM(options=aeroOptions, comm=gcomm)
DASolver.setDVGeo(DVGeo)
mesh = USMesh(options=meshOptions, comm=gcomm)
DASolver.printFamilyList()
DASolver.setMesh(mesh)
# set evalFuncs
evalFuncs = []
DASolver.setEvalFuncs(evalFuncs)
# DVCon
DVCon = DVConstraints()
DVCon.setDVGeo(DVGeo)
[p0, v1, v2] = DASolver.getTriangulatedMeshSurface(groupName=DASolver.designSurfacesGroup)
surf = [p0, v1, v2]
DVCon.setSurface(surf)
# optFuncs
optFuncs.DASolver = DASolver
optFuncs.DVGeo = DVGeo
optFuncs.DVCon = DVCon
optFuncs.evalFuncs = evalFuncs
optFuncs.gcomm = gcomm
# Run
if calcFDSens == 1:
optFuncs.calcFDSens(objFun=optFuncs.calcObjFuncValues, fileName="sensFD.txt")
else:
DASolver.runColoring()
xDV = DVGeo.getValues()
funcs = {}
funcs, fail = optFuncs.calcObjFuncValues(xDV)
funcsSens = {}
funcsSens, fail = optFuncs.calcObjFuncSens(xDV, funcs)
if gcomm.rank == 0:
reg_write_dict(funcs, 1e-8, 1e-10)
reg_write_dict(funcsSens, 1e-4, 1e-6)
Computing file changes ...