|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Run Python tests for optimization integration |
| 4 | +""" |
| 5 | + |
| 6 | +import openmdao.api as om |
| 7 | +import os |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from mpi4py import MPI |
| 11 | +from dafoam import PYDAFOAM |
| 12 | +from testFuncs import * |
| 13 | +from mphys.multipoint import Multipoint |
| 14 | +from dafoam.mphys import DAFoamBuilder |
| 15 | +from funtofem.mphys import MeldThermalBuilder |
| 16 | +from pygeo import geo_utils |
| 17 | +from mphys.scenario_aerothermal import ScenarioAeroThermal |
| 18 | +from pygeo.mphys import OM_DVGEOCOMP |
| 19 | + |
| 20 | +# NOTE: we will test DASimpleFoam and DAHeatTransferFoam for incompressible, compressible, and solid solvers using the daCustom wallDistanceMethod |
| 21 | + |
| 22 | +# ******************** |
| 23 | +# incompressible tests |
| 24 | +# ******************** |
| 25 | +gcomm = MPI.COMM_WORLD |
| 26 | +os.chdir("./reg_test_files-main/ChannelConjugateHeatV4") |
| 27 | + |
| 28 | +if gcomm.rank == 0: |
| 29 | + os.system("rm -rf */processor*") |
| 30 | + |
| 31 | +# aero setup |
| 32 | +U0 = 10.0 |
| 33 | + |
| 34 | +daOptionsAero = { |
| 35 | + "designSurfaces": [ |
| 36 | + "hot_air_inner", |
| 37 | + "hot_air_outer", |
| 38 | + "hot_air_sides", |
| 39 | + "cold_air_outer", |
| 40 | + "cold_air_inner", |
| 41 | + "cold_air_sides", |
| 42 | + ], |
| 43 | + "solverName": "DASimpleFoam", |
| 44 | + "wallDistanceMethod": "daCustom", |
| 45 | + "primalMinResTol": 1.0e-12, |
| 46 | + "primalMinResTolDiff": 1.0e12, |
| 47 | + "discipline": "aero", |
| 48 | + "primalBC": { |
| 49 | + "UHot": {"variable": "U", "patches": ["hot_air_in"], "value": [U0, 0.0, 0.0]}, |
| 50 | + "UCold": {"variable": "U", "patches": ["cold_air_in"], "value": [-U0, 0.0, 0.0]}, |
| 51 | + "useWallFunction": False, |
| 52 | + }, |
| 53 | + "function": { |
| 54 | + "HFX": { |
| 55 | + "type": "wallHeatFlux", |
| 56 | + "byUnitArea": False, |
| 57 | + "source": "patchToFace", |
| 58 | + "patches": ["hot_air_inner"], |
| 59 | + "scale": 1, |
| 60 | + }, |
| 61 | + }, |
| 62 | + "adjStateOrdering": "cell", |
| 63 | + "adjEqnOption": { |
| 64 | + "gmresRelTol": 1.0e-3, |
| 65 | + "pcFillLevel": 1, |
| 66 | + "jacMatReOrdering": "natural", |
| 67 | + "useNonZeroInitGuess": True, |
| 68 | + "dynAdjustTol": True, |
| 69 | + }, |
| 70 | + "normalizeStates": { |
| 71 | + "U": U0, |
| 72 | + "p": U0 * U0 / 2.0, |
| 73 | + "nuTilda": 1e-3, |
| 74 | + "T": 300, |
| 75 | + "phi": 1.0, |
| 76 | + }, |
| 77 | + "inputInfo": { |
| 78 | + "aero_vol_coords": {"type": "volCoord", "components": ["solver", "function"]}, |
| 79 | + "T_convect": { |
| 80 | + "type": "thermalCouplingInput", |
| 81 | + "patches": ["hot_air_inner", "cold_air_outer"], |
| 82 | + "components": ["solver"], |
| 83 | + }, |
| 84 | + }, |
| 85 | + "outputInfo": { |
| 86 | + "q_convect": { |
| 87 | + "type": "thermalCouplingOutput", |
| 88 | + "patches": ["hot_air_inner", "cold_air_outer"], |
| 89 | + "components": ["thermalCoupling"], |
| 90 | + }, |
| 91 | + }, |
| 92 | +} |
| 93 | + |
| 94 | +daOptionsThermal = { |
| 95 | + "designSurfaces": ["channel_outer", "channel_inner", "channel_sides"], |
| 96 | + "solverName": "DAHeatTransferFoam", |
| 97 | + "wallDistanceMethod": "daCustom", |
| 98 | + "primalMinResTol": 1.0e-12, |
| 99 | + "primalMinResTolDiff": 1.0e12, |
| 100 | + "discipline": "thermal", |
| 101 | + "function": { |
| 102 | + "HF_INNER": { |
| 103 | + "type": "wallHeatFlux", |
| 104 | + "byUnitArea": False, |
| 105 | + "source": "patchToFace", |
| 106 | + "patches": ["channel_inner"], |
| 107 | + "scale": 1, |
| 108 | + }, |
| 109 | + }, |
| 110 | + "adjStateOrdering": "cell", |
| 111 | + "adjEqnOption": { |
| 112 | + "gmresRelTol": 1.0e-3, |
| 113 | + "pcFillLevel": 1, |
| 114 | + "jacMatReOrdering": "natural", |
| 115 | + "useNonZeroInitGuess": True, |
| 116 | + "dynAdjustTol": True, |
| 117 | + }, |
| 118 | + "normalizeStates": { |
| 119 | + "T": 300.0, |
| 120 | + }, |
| 121 | + "inputInfo": { |
| 122 | + "thermal_vol_coords": {"type": "volCoord", "components": ["solver", "function"]}, |
| 123 | + "q_conduct": { |
| 124 | + "type": "thermalCouplingInput", |
| 125 | + "patches": ["channel_outer", "channel_inner"], |
| 126 | + "components": ["solver"], |
| 127 | + }, |
| 128 | + }, |
| 129 | + "outputInfo": { |
| 130 | + "T_conduct": { |
| 131 | + "type": "thermalCouplingOutput", |
| 132 | + "patches": ["channel_outer", "channel_inner"], |
| 133 | + "components": ["thermalCoupling"], |
| 134 | + }, |
| 135 | + }, |
| 136 | +} |
| 137 | + |
| 138 | +# Mesh deformation setup |
| 139 | +meshOptions = { |
| 140 | + "gridFile": os.getcwd(), |
| 141 | + "fileType": "OpenFOAM", |
| 142 | + # point and normal for the symmetry plane |
| 143 | + "symmetryPlanes": [], |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +class Top(Multipoint): |
| 148 | + def setup(self): |
| 149 | + |
| 150 | + dafoam_builder_aero = DAFoamBuilder(daOptionsAero, meshOptions, scenario="aerothermal", run_directory="aero") |
| 151 | + dafoam_builder_aero.initialize(self.comm) |
| 152 | + |
| 153 | + dafoam_builder_thermal = DAFoamBuilder( |
| 154 | + daOptionsThermal, meshOptions, scenario="aerothermal", run_directory="thermal" |
| 155 | + ) |
| 156 | + dafoam_builder_thermal.initialize(self.comm) |
| 157 | + |
| 158 | + thermalxfer_builder = MeldThermalBuilder(dafoam_builder_aero, dafoam_builder_thermal, n=1, beta=0.5) |
| 159 | + thermalxfer_builder.initialize(self.comm) |
| 160 | + |
| 161 | + # add the design variable component to keep the top level design variables |
| 162 | + self.add_subsystem("dvs", om.IndepVarComp(), promotes=["*"]) |
| 163 | + |
| 164 | + # add the mesh component |
| 165 | + self.add_subsystem("mesh_aero", dafoam_builder_aero.get_mesh_coordinate_subsystem()) |
| 166 | + self.add_subsystem("mesh_thermal", dafoam_builder_thermal.get_mesh_coordinate_subsystem()) |
| 167 | + |
| 168 | + # add the geometry component (FFD). Note that the aero and thermal use the exact same FFD file |
| 169 | + self.add_subsystem("geometry_aero", OM_DVGEOCOMP(file="aero/FFD/channelFFD.xyz", type="ffd")) |
| 170 | + self.add_subsystem("geometry_thermal", OM_DVGEOCOMP(file="aero/FFD/channelFFD.xyz", type="ffd")) |
| 171 | + |
| 172 | + # add a scenario (flow condition) for optimization, we pass the builder |
| 173 | + # to the scenario to actually run the flow and adjoint |
| 174 | + self.mphys_add_scenario( |
| 175 | + "scenario", |
| 176 | + ScenarioAeroThermal( |
| 177 | + aero_builder=dafoam_builder_aero, |
| 178 | + thermal_builder=dafoam_builder_thermal, |
| 179 | + thermalxfer_builder=thermalxfer_builder, |
| 180 | + ), |
| 181 | + om.NonlinearBlockGS(maxiter=20, iprint=2, use_aitken=True, rtol=1e-8, atol=1e-14), |
| 182 | + om.LinearBlockGS(maxiter=20, iprint=2, use_aitken=True, rtol=1e-8, atol=1e-14), |
| 183 | + ) |
| 184 | + |
| 185 | + # need to manually connect the x_aero0 between the mesh and geometry components |
| 186 | + self.connect("mesh_aero.x_aero0", "geometry_aero.x_aero_in") |
| 187 | + self.connect("geometry_aero.x_aero0", "scenario.x_aero") |
| 188 | + |
| 189 | + self.connect("mesh_thermal.x_thermal0", "geometry_thermal.x_thermal_in") |
| 190 | + self.connect("geometry_thermal.x_thermal0", "scenario.x_thermal") |
| 191 | + |
| 192 | + def configure(self): |
| 193 | + |
| 194 | + super().configure() |
| 195 | + |
| 196 | + # get the surface coordinates from the mesh component |
| 197 | + points_aero = self.mesh_aero.mphys_get_surface_mesh() |
| 198 | + points_thermal = self.mesh_thermal.mphys_get_surface_mesh() |
| 199 | + |
| 200 | + # add pointset to the geometry component |
| 201 | + self.geometry_aero.nom_add_discipline_coords("aero", points_aero) |
| 202 | + self.geometry_thermal.nom_add_discipline_coords("thermal", points_thermal) |
| 203 | + |
| 204 | + # geometry setup |
| 205 | + pts = self.geometry_aero.DVGeo.getLocalIndex(0) |
| 206 | + dir_y = np.array([0.0, 1.0, 0.0]) |
| 207 | + shapes = [] |
| 208 | + shapes.append({pts[9, 0, 0]: dir_y, pts[9, 0, 1]: dir_y}) |
| 209 | + self.geometry_aero.nom_addShapeFunctionDV(dvName="shape", shapes=shapes) |
| 210 | + self.geometry_thermal.nom_addShapeFunctionDV(dvName="shape", shapes=shapes) |
| 211 | + |
| 212 | + # add the design variables to the dvs component's output |
| 213 | + self.dvs.add_output("shape", val=np.array([0])) |
| 214 | + # manually connect the dvs output to the geometry |
| 215 | + self.connect("shape", "geometry_aero.shape") |
| 216 | + self.connect("shape", "geometry_thermal.shape") |
| 217 | + |
| 218 | + # define the design variables to the top level |
| 219 | + self.add_design_var("shape", lower=-1, upper=1, scaler=1.0) |
| 220 | + |
| 221 | + # add objective and constraints to the top level |
| 222 | + self.add_objective("scenario.aero_post.HFX", scaler=1.0) |
| 223 | + |
| 224 | + |
| 225 | +prob = om.Problem() |
| 226 | +prob.model = Top() |
| 227 | + |
| 228 | +prob.setup(mode="rev") |
| 229 | +prob.run_model() |
| 230 | + |
| 231 | +HFX = prob.get_val("scenario.aero_post.HFX")[0] |
| 232 | +print("HFX:", HFX) |
| 233 | +if (abs(HFX - 180000.0) / (HFX + 1e-16)) > 1e-6: |
| 234 | + print("DAHeatTransferFoam test failed for DACustomCHT!") |
| 235 | + exit(1) |
| 236 | +else: |
| 237 | + print("DAHeatTransferFoam test passed for DACustomCHT!") |
| 238 | + |
| 239 | + |
| 240 | +# ******************** |
| 241 | +# compressible tests |
| 242 | +# ******************** |
| 243 | +os.chdir("../ConvergentChannel") |
| 244 | +if gcomm.rank == 0: |
| 245 | + os.system("rm -rf 0/* processor* *.bin") |
| 246 | + os.system("cp -r 0.compressible/* 0/") |
| 247 | + os.system("cp -r system.subsonic/* system/") |
| 248 | + os.system("cp -r constant/turbulenceProperties.sa constant/turbulenceProperties") |
| 249 | + |
| 250 | +# aero setup |
| 251 | +U0 = 100.0 |
| 252 | + |
| 253 | +daOptions = { |
| 254 | + "solverName": "DARhoSimpleFoam", |
| 255 | + "wallDistanceMethod": "daCustom", |
| 256 | + "primalMinResTol": 1.0e-12, |
| 257 | + "primalMinResTolDiff": 1e4, |
| 258 | + "printDAOptions": False, |
| 259 | + "primalBC": { |
| 260 | + "U0": {"variable": "U", "patches": ["inlet"], "value": [U0, 0.0, 0.0]}, |
| 261 | + "T0": {"variable": "T", "patches": ["inlet"], "value": [310.0]}, |
| 262 | + "p0": {"variable": "p", "patches": ["outlet"], "value": [101325.0]}, |
| 263 | + "useWallFunction": True, |
| 264 | + }, |
| 265 | + "function": { |
| 266 | + "HFX": { |
| 267 | + "type": "wallHeatFlux", |
| 268 | + "byUnitArea": False, |
| 269 | + "source": "patchToFace", |
| 270 | + "patches": ["walls"], |
| 271 | + "scale": 1.0, |
| 272 | + }, |
| 273 | + }, |
| 274 | +} |
| 275 | + |
| 276 | +DASolver = PYDAFOAM(options=daOptions, comm=gcomm) |
| 277 | +DASolver() |
| 278 | + |
| 279 | +funcs = {} |
| 280 | +DASolver.evalFunctions(funcs) |
| 281 | + |
| 282 | +diff = abs(8967.626339018574 - funcs["HFX"]) / 8967.626339018574 |
| 283 | +if diff > 1e-10: |
| 284 | + if gcomm.rank == 0: |
| 285 | + print("DAFunction comp test failed for DACustomCHT") |
| 286 | + exit(1) |
| 287 | +else: |
| 288 | + if gcomm.rank == 0: |
| 289 | + print("DAFunction comp test passed for DACustomCHT!") |
0 commit comments