Skip to content

Commit

Permalink
Added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Jan 30, 2025
1 parent 3f15a19 commit d775e23
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 3 deletions.
143 changes: 143 additions & 0 deletions tests/not_working_runRegTests_DARhoPimpleFoam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env python
"""
Run Python tests for optimization integration
"""

from mpi4py import MPI
import os
import numpy as np
from testFuncs import *

import openmdao.api as om
from openmdao.api import Group
from mphys.multipoint import Multipoint
from dafoam.mphys.mphys_dafoam import DAFoamBuilderUnsteady
from mphys.scenario_aerodynamic import ScenarioAerodynamic
from pygeo.mphys import OM_DVGEOCOMP

gcomm = MPI.COMM_WORLD

os.chdir("./reg_test_files-main/Ramp")
if gcomm.rank == 0:
os.system("rm -rf 0 processor* *.bin")
os.system("cp -r 0_compressible 0")
replace_text_in_file("system/fvSchemes", "meshWave;", "meshWaveFrozen;")

# aero setup
U0 = 10.0

daOptions = {
"designSurfaces": ["bot"],
"solverName": "DARhoPimpleFoam",
"useAD": {"mode": "reverse", "seedIndex": 0, "dvName": "shape"},
"primalBC": {
# "U0": {"variable": "U", "patches": ["inlet"], "value": [U0, 0.0, 0.0]},
"useWallFunction": True,
},
"unsteadyAdjoint": {
"mode": "timeAccurate",
"PCMatPrecomputeInterval": 5,
"PCMatUpdateInterval": 100, # TODO. the PCUpdate is not working. the calcPCMatWithFvMatrix function is problematic for rhoPimple
"readZeroFields": True,
},
"function": {
"UBulk": {
"type": "variableVolSum",
"source": "allCells",
"varName": "U",
"varType": "vector",
"component": 0,
"isSquare": 1,
"divByTotalVol": 1,
"scale": 1.0,
"timeOp": "average",
},
"CD": {
"type": "force",
"source": "patchToFace",
"patches": ["bot"],
"directionMode": "fixedDirection",
"direction": [1.0, 0.0, 0.0],
"scale": 1.0,
"timeOp": "average",
},
},
"adjStateOrdering": "cell",
"adjEqnOption": {"gmresRelTol": 1.0e-8, "pcFillLevel": 1, "jacMatReOrdering": "natural"},
"normalizeStates": {"U": U0, "p": 101325.0, "phi": 1.0, "nuTilda": 1e-3, "T": 300.0},
"inputInfo": {
"aero_vol_coords": {"type": "volCoord", "components": ["solver", "function"]},
},
}

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.05], [0.0, 0.0, 1.0]]],
}


class Top(Group):
def setup(self):

self.add_subsystem("dvs", om.IndepVarComp(), promotes=["*"])

# add the geometry component, we dont need a builder because we do it here.
self.add_subsystem("geometry", OM_DVGEOCOMP(file="FFD/FFD.xyz", type="ffd"), promotes=["*"])

self.add_subsystem(
"cruise",
DAFoamBuilderUnsteady(solver_options=daOptions, mesh_options=meshOptions),
promotes=["*"],
)

self.connect("x_aero0", "x_aero")

def configure(self):

# create geometric DV setup
points = self.cruise.get_surface_mesh()

# add pointset
self.geometry.nom_add_discipline_coords("aero", points)

# add the dv_geo object to the builder solver. This will be used to write deformed FFDs
self.cruise.solver.add_dvgeo(self.geometry.DVGeo)

# geometry setup
pts = self.geometry.DVGeo.getLocalIndex(0)
dir_y = np.array([0.0, 1.0, 0.0])
shapes = []
shapes.append({pts[2, 0, 0]: dir_y, pts[2, 0, 1]: dir_y})
self.geometry.nom_addShapeFunctionDV(dvName="shape", shapes=shapes)

# add the design variables to the dvs component's output
self.dvs.add_output("shape", val=np.zeros(1))
self.dvs.add_output("x_aero_in", val=points, distributed=True)

# define the design variables to the top level
self.add_design_var("shape", lower=-10.0, upper=10.0, scaler=1.0)

# add constraints and the objective
self.add_objective("UBulk", scaler=1.0)
self.add_constraint("CD", scaler=1.0, equals=1.0)


# NOTE: the patchV deriv is accurate with FD but not with forward AD. The forward AD changed the primal value
# and the reason is still unknown..

funcDict = {}
derivDict = {}

dvNames = ["shape"]
dvIndices = [[0]]
funcNames = ["cruise.solver.UBulk", "cruise.solver.CD"]

# run the adjoint and forward ref
run_tests(om, Top, gcomm, daOptions, funcNames, dvNames, dvIndices, funcDict, derivDict)

# write the test results
if gcomm.rank == 0:
reg_write_dict(funcDict, 1e-10, 1e-12)
reg_write_dict(derivDict, 1e-8, 1e-12)
15 changes: 14 additions & 1 deletion tests/refs/DAFoam_Test_DASimpleFoamRef.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ Dictionary Key: CD
@value 0.0170618714819692 1e-10 1e-12
Dictionary Key: CL
@value 0.2984067151382958 1e-10 1e-12
Dictionary Key: nonOrtho
@value 23.8934356542776314 1e-10 1e-12
Dictionary Key: skewness
@value 1.4671221512193253 1e-10 1e-12
Dictionary Key: CL
Dictionary Key: patchV-Adjoint
@value 0.0602938548210897 1e-08 1e-12
Expand All @@ -23,4 +27,13 @@ Dictionary Key: patchV-FD
Dictionary Key: shape-Adjoint
@value 62.9167283705385358 1e-08 1e-12
Dictionary Key: shape-FD
@value 62.9468890552234370 1e-08 1e-12
@value 62.9468890552234370 1e-08 1e-12
Dictionary Key: nonOrtho
Dictionary Key: shape-Adjoint
@value -0.0178410856249778 1e-08 1e-12
Dictionary Key: shape-FD
@value -0.0243671058851760 1e-08 1e-12
Dictionary Key: skewness
@value -0.0001091622840756 1e-08 1e-12
Dictionary Key: shape-FD
@value -0.0003604027958772 1e-08 1e-12
3 changes: 2 additions & 1 deletion tests/runRegTests_DAPimpleFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

os.chdir("./reg_test_files-main/Ramp")
if gcomm.rank == 0:
os.system("rm -rf processor* *.bin")
os.system("rm -rf 0 processor* *.bin")
os.system("cp -r 0_incompressible 0")
replace_text_in_file("system/fvSchemes", "meshWave;", "meshWaveFrozen;")

# aero setup
Expand Down
24 changes: 23 additions & 1 deletion tests/runRegTests_DASimpleFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@
"patchVelocityInputName": "patchV",
"scale": 1.0 / (0.5 * U0 * U0 * A0),
},
"skewness": {
"type": "meshQualityKS",
"source": "allCells",
"coeffKS": 20.0,
"metric": "faceSkewness",
"scale": 1.0,
},
"nonOrtho": {
"type": "meshQualityKS",
"source": "allCells",
"coeffKS": 1.0,
"metric": "nonOrthoAngle",
"scale": 1.0,
},
},
"adjEqnOption": {"gmresRelTol": 1.0e-12, "pcFillLevel": 1, "jacMatReOrdering": "rcm"},
"normalizeStates": {"U": U0, "p": U0 * U0 / 2.0, "phi": 1.0, "nuTilda": 1e-3},
Expand Down Expand Up @@ -154,7 +168,7 @@ def configure(self):
# verify the total derivatives against the finite-difference
prob.run_model()
results = prob.check_totals(
of=["LoD.val", "cruise.aero_post.CL"],
of=["LoD.val", "cruise.aero_post.CL", "cruise.aero_post.skewness", "cruise.aero_post.nonOrtho"],
wrt=["patchV", "shape"],
compact_print=True,
step=1e-3,
Expand All @@ -166,6 +180,8 @@ def configure(self):
funcDict = {}
funcDict["CD"] = prob.get_val("cruise.aero_post.CD")
funcDict["CL"] = prob.get_val("cruise.aero_post.CL")
funcDict["skewness"] = prob.get_val("cruise.aero_post.skewness")
funcDict["nonOrtho"] = prob.get_val("cruise.aero_post.nonOrtho")
derivDict = {}
derivDict["LoD"] = {}
derivDict["LoD"]["shape-Adjoint"] = results[("LoD.val", "shape")]["J_fwd"][0]
Expand All @@ -177,5 +193,11 @@ def configure(self):
derivDict["CL"]["shape-FD"] = results[("cruise.aero_post.CL", "shape")]["J_fd"][0]
derivDict["CL"]["patchV-Adjoint"] = results[("cruise.aero_post.CL", "patchV")]["J_fwd"][0]
derivDict["CL"]["patchV-FD"] = results[("cruise.aero_post.CL", "patchV")]["J_fd"][0]
derivDict["skewness"] = {}
derivDict["skewness"]["shape-Adjoint"] = results[("cruise.aero_post.skewness", "shape")]["J_fwd"][0]
derivDict["skewness"]["shape-FD"] = results[("cruise.aero_post.skewness", "shape")]["J_fd"][0]
derivDict["nonOrtho"] = {}
derivDict["nonOrtho"]["shape-Adjoint"] = results[("cruise.aero_post.nonOrtho", "shape")]["J_fwd"][0]
derivDict["nonOrtho"]["shape-FD"] = results[("cruise.aero_post.nonOrtho", "shape")]["J_fd"][0]
reg_write_dict(funcDict, 1e-10, 1e-12)
reg_write_dict(derivDict, 1e-8, 1e-12)

0 comments on commit d775e23

Please sign in to comment.