|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Run Python tests for DARhoPimpleFoam |
| 4 | +""" |
| 5 | + |
| 6 | +from mpi4py import MPI |
| 7 | +from dafoam import PYDAFOAM, optFuncs |
| 8 | +import sys |
| 9 | +import os |
| 10 | +from pygeo import * |
| 11 | +from pyspline import * |
| 12 | +from idwarp import * |
| 13 | +import numpy as np |
| 14 | +from testFuncs import * |
| 15 | + |
| 16 | +calcFDSens = 0 |
| 17 | +if len(sys.argv) != 1: |
| 18 | + if sys.argv[1] == "calcFDSens": |
| 19 | + calcFDSens = 1 |
| 20 | + |
| 21 | +gcomm = MPI.COMM_WORLD |
| 22 | + |
| 23 | +os.chdir("./reg_test_files-main/NACA0012UnsteadyComp") |
| 24 | + |
| 25 | +if gcomm.rank == 0: |
| 26 | + os.system("rm -rf processor*") |
| 27 | + |
| 28 | +twist0 = 0 |
| 29 | +U0 = 100 |
| 30 | +T0 = 300.0 |
| 31 | +p0 = 101325.0 |
| 32 | +rho0 = p0 / 287.0 / T0 |
| 33 | +A0 = 0.1 |
| 34 | +nuTilda0 = 1e-4 |
| 35 | + |
| 36 | +# test incompressible solvers |
| 37 | +daOptions = { |
| 38 | + "designSurfaces": ["wing"], |
| 39 | + "solverName": "DARhoPimpleFoam", |
| 40 | + "primalBC": {"useWallFunction": True}, |
| 41 | + "unsteadyAdjoint": { |
| 42 | + "mode": "timeAccurate", |
| 43 | + "PCMatPrecomputeInterval": 20, |
| 44 | + "PCMatUpdateInterval": 1, |
| 45 | + "objFuncTimeOperator": "average", |
| 46 | + }, |
| 47 | + "printIntervalUnsteady": 1, |
| 48 | + "objFunc": { |
| 49 | + "CD": { |
| 50 | + "part1": { |
| 51 | + "type": "force", |
| 52 | + "source": "patchToFace", |
| 53 | + "patches": ["wing"], |
| 54 | + "directionMode": "fixedDirection", |
| 55 | + "direction": [1.0, 0.0, 0.0], |
| 56 | + "scale": 1.0 / (0.5 * U0 * U0 * A0 * rho0), |
| 57 | + "addToAdjoint": True, |
| 58 | + } |
| 59 | + }, |
| 60 | + "CL": { |
| 61 | + "part1": { |
| 62 | + "type": "force", |
| 63 | + "source": "patchToFace", |
| 64 | + "patches": ["wing"], |
| 65 | + "directionMode": "fixedDirection", |
| 66 | + "direction": [0.0, 1.0, 0.0], |
| 67 | + "scale": 1.0 / (0.5 * U0 * U0 * A0 * rho0), |
| 68 | + "addToAdjoint": True, |
| 69 | + } |
| 70 | + }, |
| 71 | + }, |
| 72 | + "adjStateOrdering": "cell", |
| 73 | + "adjEqnOption": { |
| 74 | + "gmresRelTol": 1.0e-8, |
| 75 | + "pcFillLevel": 1, |
| 76 | + "jacMatReOrdering": "natural", |
| 77 | + "useNonZeroInitGuess": True, |
| 78 | + }, |
| 79 | + "normalizeStates": { |
| 80 | + "U": U0, |
| 81 | + "p": p0, |
| 82 | + "T": T0, |
| 83 | + "nuTilda": 1e-3, |
| 84 | + "phi": 1.0, |
| 85 | + }, |
| 86 | + "designVar": { |
| 87 | + "twist": {"designVarType": "FFD"}, |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | +# mesh warping parameters, users need to manually specify the symmetry plane |
| 92 | +meshOptions = { |
| 93 | + "gridFile": os.getcwd(), |
| 94 | + "fileType": "OpenFOAM", |
| 95 | + # point and normal for the symmetry plane |
| 96 | + "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]]], |
| 97 | +} |
| 98 | + |
| 99 | +# ============================================================================= |
| 100 | +# Design variable setup |
| 101 | +# ============================================================================= |
| 102 | +DVGeo = DVGeometry("./FFD/FFD.xyz") |
| 103 | +DVGeo.addRefAxis("bodyAxis", xFraction=0.25, alignIndex="k") |
| 104 | + |
| 105 | + |
| 106 | +def twist(val, geo): |
| 107 | + for i in range(2): |
| 108 | + geo.rot_z["bodyAxis"].coef[i] = val[0] |
| 109 | + |
| 110 | + |
| 111 | +DVGeo.addGlobalDV("twist", [twist0], twist, lower=-100.0, upper=100.0, scale=1.0) |
| 112 | + |
| 113 | + |
| 114 | +# ============================================================================= |
| 115 | +# DAFoam initialization |
| 116 | +# ============================================================================= |
| 117 | +DASolver = PYDAFOAM(options=daOptions, comm=gcomm) |
| 118 | +DASolver.setDVGeo(DVGeo) |
| 119 | +mesh = USMesh(options=meshOptions, comm=gcomm) |
| 120 | +DASolver.printFamilyList() |
| 121 | +DASolver.setMesh(mesh) |
| 122 | +evalFuncs = [] |
| 123 | +DASolver.setEvalFuncs(evalFuncs) |
| 124 | + |
| 125 | +# ============================================================================= |
| 126 | +# Constraint setup |
| 127 | +# ============================================================================= |
| 128 | +DVCon = DVConstraints() |
| 129 | +DVCon.setDVGeo(DVGeo) |
| 130 | +DVCon.setSurface(DASolver.getTriangulatedMeshSurface(groupName=DASolver.designSurfacesGroup)) |
| 131 | + |
| 132 | +# ============================================================================= |
| 133 | +# Initialize optFuncs for optimization |
| 134 | +# ============================================================================= |
| 135 | +optFuncs.DASolver = DASolver |
| 136 | +optFuncs.DVGeo = DVGeo |
| 137 | +optFuncs.DVCon = DVCon |
| 138 | +optFuncs.evalFuncs = evalFuncs |
| 139 | +optFuncs.gcomm = gcomm |
| 140 | + |
| 141 | +# Run |
| 142 | +if calcFDSens == 1: |
| 143 | + optFuncs.calcFDSens() |
| 144 | +else: |
| 145 | + DASolver.runColoring() |
| 146 | + xDV = DVGeo.getValues() |
| 147 | + funcs = {} |
| 148 | + funcs, fail = optFuncs.calcObjFuncValues(xDV) |
| 149 | + funcsSens = {} |
| 150 | + funcsSens, fail = optFuncs.calcObjFuncSens(xDV, funcs) |
| 151 | + |
| 152 | + if gcomm.rank == 0: |
| 153 | + reg_write_dict(funcs, 1e-8, 1e-10) |
| 154 | + reg_write_dict(funcsSens, 1e-4, 1e-6) |
0 commit comments