#!/usr/bin/env python """ DAFoam run script for the Onera M4 wing at transonic speed """ # ============================================================================= # Imports # ============================================================================= import os import argparse from mpi4py import MPI from dafoam import PYDAFOAM, optFuncs from pygeo import * from pyspline import * from idwarp import * from pyoptsparse import Optimization, OPT import numpy as np # ============================================================================= # Input Parameters # ============================================================================= parser = argparse.ArgumentParser() # which optimizer to use. Options are: slsqp (default), snopt, or ipopt parser.add_argument("--opt", help="optimizer to use", type=str, default="ipopt") # which task to run. Options are: opt (default), run, testSensShape, or solveCL parser.add_argument("--task", help="type of run to do", type=str, default="opt") args = parser.parse_args() gcomm = MPI.COMM_WORLD # global parameters U0 = 285.0 p0 = 101325.0 nuTilda0 = 4.5e-5 T0 = 300.0 CL_target = 0.270 alpha0 = 3.0 A0 = 0.7575 rho0 = 1.0 # density for normalizing CD and CL # Set the parameters for optimization daOptions = { "designSurfaces": ["WING"], "useAD": {"mode": "reverse"}, "solverName": "DARhoSimpleCFoam", "primalMinResTol": 1.0e-8, "primalBC": { "U0": {"variable": "U", "patches": ["FARFIELD"], "value": [U0, 0.0, 0.0]}, "p0": {"variable": "p", "patches": ["FARFIELD"], "value": [p0]}, "T0": {"variable": "T", "patches": ["FARFIELD"], "value": [T0]}, "nuTilda0": {"variable": "nuTilda", "patches": ["FARFIELD"], "value": [nuTilda0]}, "useWallFunction": True, }, # variable bounds for compressible flow conditions "primalVarBounds": { "UMax": 1000.0, "UMin": -1000.0, "pMax": 500000.0, "pMin": 20000.0, "eMax": 500000.0, "eMin": 100000.0, "rhoMax": 5.0, "rhoMin": 0.2, }, "objFunc": { "CD": { "part1": { "type": "force", "source": "patchToFace", "patches": ["WING"], "directionMode": "parallelToFlow", "alphaName": "alpha", "scale": 1.0 / (0.5 * rho0 * U0 * U0 * A0), "addToAdjoint": True, } }, "CL": { "part1": { "type": "force", "source": "patchToFace", "patches": ["WING"], "directionMode": "normalToFlow", "alphaName": "alpha", "scale": 1.0 / (0.5 * rho0 * U0 * U0 * A0), "addToAdjoint": True, } }, }, "adjEqnOption": {"gmresRelTol": 1.0e-6, "pcFillLevel": 1, "jacMatReOrdering": "rcm"}, # transonic preconditioner to speed up the adjoint convergence "transonicPCOption": 1, "normalizeStates": {"U": U0, "p": p0, "nuTilda": nuTilda0 * 10.0, "phi": 1.0, "T": T0}, "adjPartDerivFDStep": {"State": 1e-6, "FFD": 1e-3}, "adjPCLag": 1, "designVar": {}, } # mesh warping parameters, users need to manually specify the symmetry plane meshOptions = { #########################deformation related "gridFile": os.getcwd(), "fileType": "OpenFOAM", "useRotations": False, # point and normal for the symmetry plane "symmetryPlanes": [[[0.0, 0.0, 0.0], [0.0, 1.0, 0.0]]], } # options for optimizers if args.opt == "snopt": optOptions = { "Major feasibility tolerance": 1.0e-7, "Major optimality tolerance": 1.0e-7, "Minor feasibility tolerance": 1.0e-7, "Verify level": -1, "Function precision": 1.0e-7, "Major iterations limit": 50, "Nonderivative linesearch": None, "Print file": "opt_SNOPT_print.txt", "Summary file": "opt_SNOPT_summary.txt", } elif args.opt == "ipopt": optOptions = { "tol": 1.0e-7, "constr_viol_tol": 1.0e-7, "max_iter": 50, "print_level": 5, "output_file": "opt_IPOPT.txt", "mu_strategy": "adaptive", "limited_memory_max_history": 10, "nlp_scaling_method": "none", "alpha_for_y": "full", "recalc_y": "yes", } elif args.opt == "slsqp": optOptions = { "ACC": 1.0e-7, "MAXIT": 50, "IFILE": "opt_SLSQP.txt", } else: print("opt arg not valid!") exit(0) # ============================================================================= # Design variable setup # ============================================================================= DVGeo = DVGeometry("./FFD/wingFFD.xyz") # nTwists is the number of FFD points in the spanwise direction nTwists = DVGeo.addRefAxis("bodyAxis", xFraction=0.25, alignIndex="k") # angle of attack def alpha(val, geo): aoa = val[0] * np.pi / 180.0 inletU = [float(U0 * np.cos(aoa)), 0, float(U0 * np.sin(aoa))] DASolver.setOption("primalBC", {"U0": {"variable": "U", "patches": ["FARFIELD"], "value": inletU}}) DASolver.updateDAOption() # select points pts = DVGeo.getLocalIndex(0) indexList = pts[:, :, :].flatten() PS = geo_utils.PointSelect("list", indexList) # shape DVGeo.addLocalDV("shapey", lower=-1.0, upper=1.0, axis="y", scale=1.0, pointSelect=PS) daOptions["designVar"]["shapey"] = {"designVarType": "FFD"} # alpha DVGeo.addGlobalDV("alpha", [alpha0], alpha, lower=0.0, upper=10.0, scale=1.0) daOptions["designVar"]["alpha"] = {"designVarType": "AOA", "patches": ["FARFIELD"], "flowAxis": "x", "normalAxis": "z"} # ============================================================================= # DAFoam initialization # ============================================================================= DASolver = PYDAFOAM(options=daOptions, comm=gcomm) DASolver.setDVGeo(DVGeo) mesh = USMesh(options=meshOptions, comm=gcomm) #########################deformation related DASolver.printFamilyList() DASolver.setMesh(mesh) #########################deformation related evalFuncs = [] DASolver.setEvalFuncs(evalFuncs) # ============================================================================= # Constraint setup # ============================================================================= DVCon = DVConstraints() # ============================================================================= # Initialize optFuncs for optimization # ============================================================================= optFuncs.DASolver = DASolver #########################deformation related optFuncs.DVGeo = DVGeo optFuncs.DVCon = DVCon optFuncs.evalFuncs = evalFuncs optFuncs.gcomm = gcomm meshSurfCoords=DASolver.getSurfaceCoordinates() DASolver.setSurfaceCoordinates(meshSurfCoords) DASolver.mesh.warpMesh() #DASolver.mesh.writeGrid() print("debug 1")