-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters