def setup_cb(comm): # Create the solver CFDSolver = ADFLOW(options=options, comm=comm, debug=False) # Setup geometry/mesh DVGeo = DVGeometry(ffdFile) nTwist = 6 DVGeo.addRefAxis( 'wing', Curve(x=numpy.linspace(5.0 / 4.0, 1.5 / 4.0 + 7.5, nTwist), y=numpy.zeros(nTwist), z=numpy.linspace(0, 14, nTwist), k=2)) def twist(val, geo): for i in range(nTwist): geo.rot_z['wing'].coef[i] = val[i] DVGeo.addGeoDVGlobal('twist', [0] * nTwist, twist, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVLocal('shape', lower=-0.5, upper=0.5, axis='y', scale=10.0) mesh = USMesh(options=meshOptions, comm=comm) CFDSolver.setMesh(mesh) CFDSolver.setDVGeo(DVGeo) return CFDSolver, mesh, DVGeo, None
def generate_dvgeo_dvcon_c172(self): meshfile = os.path.join(self.base_path, '../inputFiles/c172.stl') ffdfile = os.path.join(self.base_path, '../inputFiles/c172.xyz') testmesh = mesh.Mesh.from_file(meshfile) # test mesh dim 0 is triangle index # dim 1 is each vertex of the triangle # dim 2 is x, y, z dimension # create a DVGeo object with a few local thickness variables DVGeo = DVGeometry(ffdfile) nRefAxPts = DVGeo.addRefAxis("wing", xFraction=0.25, alignIndex="k") self.nTwist = nRefAxPts - 1 def twist(val, geo): for i in range(1, nRefAxPts): geo.rot_z["wing"].coef[i] = val[i - 1] DVGeo.addGeoDVGlobal(dvName="twist", value=[0] * self.nTwist, func=twist, lower=-10, upper=10, scale=1) DVGeo.addGeoDVLocal("local", lower=-0.5, upper=0.5, axis="y", scale=1) # create a DVConstraints object for the wing DVCon =DVConstraints() DVCon.setDVGeo(DVGeo) p0 = testmesh.vectors[:,0,:] / 1000 v1 = testmesh.vectors[:,1,:] / 1000 - p0 v2 = testmesh.vectors[:,2,:] / 1000 - p0 DVCon.setSurface([p0, v1, v2]) return DVGeo, DVCon
def setDVGeo(ffdFile, cmplx=False): # Setup geometry/mesh DVGeo = DVGeometry(ffdFile, complex=cmplx) nTwist = 6 DVGeo.addRefAxis( "wing", Curve( x=numpy.linspace(5.0 / 4.0, 1.5 / 4.0 + 7.5, nTwist), y=numpy.zeros(nTwist), z=numpy.linspace(0, 14, nTwist), k=2, ), ) def twist(val, geo): for i in range(nTwist): geo.rot_z["wing"].coef[i] = val[i] def span(val, geo): # Span C = geo.extractCoef("wing") for i in range(len(C) - 1): C[-1, 2] = C[-1, 2] + val[0] geo.restoreCoef(C, "wing") DVGeo.addGeoDVGlobal("twist", [0] * nTwist, twist, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVGlobal("span", [0], span, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVLocal("shape", lower=-0.5, upper=0.5, axis="y", scale=10.0) return DVGeo
def test_1(self, train=False, refDeriv=False): refFile = os.path.join(self.base_path, 'ref/test_Cylinder_01.ref') with BaseRegTest(refFile, train=train) as handler: handler.root_print("Test 1: Basic FFD, global DVs") radius = 1.0 height = 10.0 DVCon = DVConstraints() surf = self.make_cylinder_mesh(radius, height) DVCon.setSurface(surf) # DVCon.writeSurfaceTecplot('cylinder_surface.dat') ffd_name = os.path.join(self.base_path, '../inputFiles/cylinder_ffd.xyz') self.make_ffd(ffd_name, radius, height) DVGeo = DVGeometry(ffd_name) nAxPts = DVGeo.addRefAxis('thru', xFraction=0.5, alignIndex='i', raySize=1.0) def scale_circle(val, geo): for i in range(nAxPts): geo.scale['thru'].coef[i] = val[0] DVGeo.addGeoDVGlobal('scale_circle', func=scale_circle, value=[1]) DVCon.setDVGeo(DVGeo) leList = [[0, 0, 0], [-radius / 2, 0, height]] xAxis = [-1, 0, 0] yAxis = [0, 1, 0] DVCon.addLERadiusConstraints(leList, nSpan=5, axis=yAxis, chordDir=xAxis, scaled=False) # DVCon.writeTecplot('cylinder_constraints.dat') funcs = {} DVCon.evalFunctions(funcs) print(funcs) handler.root_add_dict('funcs1', funcs, rtol=1e-6, atol=1e-6) DVGeo.setDesignVars({'scale_circle': 0.5}) funcs = {} DVCon.evalFunctions(funcs) handler.root_add_dict('funcs2', funcs, rtol=1e-6, atol=1e-6) print(funcs) funcsSens = {} DVCon.evalFunctionsSens(funcsSens) print(funcsSens) handler.root_add_dict('funcsSens', funcsSens, rtol=1e-6, atol=1e-6) print(funcsSens)
# rst dvgeo (beg) # Create DVGeometry object FFDFile = "ffd.xyz" DVGeo = DVGeometry(FFDFile) # Create reference axis nRefAxPts = DVGeo.addRefAxis("wing", xFraction=0.25, alignIndex="k") nTwist = nRefAxPts - 1 # Set up global design variables def twist(val, geo): for i in range(1, nRefAxPts): geo.rot_z["wing"].coef[i] = val[i - 1] DVGeo.addGeoDVGlobal(dvName="twist", value=[0] * nTwist, func=twist, lower=-10, upper=10, scale=0.01) # Set up local design variables DVGeo.addGeoDVLocal("local", lower=-0.5, upper=0.5, axis="y", scale=1) # Add DVGeo object to CFD solver CFDSolver.setDVGeo(DVGeo) # rst dvgeo (end) # ====================================================================== # DVConstraint Setup, and Thickness and Volume Constraints # ====================================================================== # rst dvconVolThick (beg) DVCon = DVConstraints() DVCon.setDVGeo(DVGeo) # Only ADflow has the getTriangulatedSurface Function
DVGeo.addRefAxis('wing', pyspline.Curve(x=numpy.linspace(5.0/4.0, 1.5/4.0+7.5, nTwist), y=numpy.zeros(nTwist), z=numpy.linspace(0,14, nTwist), k=2)) def twist(val, geo): for i in range(nTwist): geo.rot_z['wing'].coef[i] = val[i] def span(val, geo): # Span C = geo.extractCoef('wing') s = geo.extractS('wing') for i in range(len(C)-1): C[-1, 2] = C[-1, 2] + val[0] geo.restoreCoef(C, 'wing') DVGeo.addGeoDVGlobal('twist', [0]*nTwist, twist, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVGlobal('span', [0], span, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVLocal('shape', lower=-0.5, upper=0.5, axis='y', scale=10.0) mesh = MBMesh(options={'gridFile':'../inputFiles/mdo_tutorial_euler.cgns'}) CFDSolver.setMesh(mesh) CFDSolver.setDVGeo(DVGeo) #Aeroproblem must be set before we can call DVGeo.setDesignVars CFDSolver.setAeroProblem(ap) if not 'complex' in sys.argv: # Solve system CFDSolver(ap, writeSolution=False) funcs = {} CFDSolver.evalFunctions(ap, funcs) # Solve sensitivities funcsSens = {} CFDSolver.evalFunctionsSens(ap, funcsSens)
geo.rot_z['wing'].coef[i] = val[i - 1] #rst Taper def taper(val, geo): s = geo.extractS('wing') slope = (val[1] - val[0]) / (s[-1] - s[0]) for i in range(nRefAxPts): geo.scale_x['wing'].coef[i] = slope * (s[i] - s[0]) + val[0] #rst Add global dvs nTwist = nRefAxPts - 1 DVGeo.addGeoDVGlobal(dvName='dihedral', value=[0] * nTwist, func=dihedral, lower=-10, upper=10, scale=1) DVGeo.addGeoDVGlobal(dvName='twist', value=[0] * nTwist, func=twist, lower=-10, upper=10, scale=1) DVGeo.addGeoDVGlobal(dvName='taper', value=[1] * 2, func=taper, lower=0.5, upper=1.5, scale=1)
length = val[0] C[1, 0] = C[2, 0] - length geo.restoreCoef(C, "bodyAxis") return # lower = [-2.,-2.,-2.,-2.,5.] # upper = [-2.,5.,5.,5.,5.] # DVGeo.addGeoDVGlobal('length', x, length, # lower=lower, upper=upper, scale=1.0) DVGeo.addGeoDVGlobal("noseLength", 0.3, noseLength, lower=0, upper=0.5, scale=1.0) # DVGeoChild.addGeoDVGlobal('rampAngle', 35.1, rampAngle, # lower=0., upper=90., scale=1.0) DVGeoChild.addGeoDVGlobal("doubleRampAngle", [35.1, -5.0], doubleRampAngle, lower=[0.0, -45.0], upper=[45.0, 5.0], scale=[1.0, 1.0]) # lowerA = [0.,0.,0.,0.] # upperA = [0.3,0.3,0.3,0.3] # DVGeoChild.addGeoDVGlobal('angleVars', z1, angleVars, # lower=lowerA, upper=upperA, scale=1.0)
# Set the chord as well geo.scale["wing"].coef[-1] = val[3] coords = hyp.getSurfaceCoordinates() DVGeo = DVGeometry(ffd_file) coef = DVGeo.FFD.vols[0].coef.copy() # First determine the reference chord lengths: nSpan = coef.shape[2] ref = numpy.zeros((nSpan, 3)) for k in range(nSpan): max_x = numpy.max(coef[:, :, k, 0]) min_x = numpy.min(coef[:, :, k, 0]) ref[k, 0] = min_x + 0.25 * (max_x - min_x) ref[k, 1] = numpy.average(coef[:, :, k, 1]) ref[k, 2] = numpy.average(coef[:, :, k, 2]) c0 = Curve(X=ref, k=2) DVGeo.addRefAxis("wing", c0) DVGeo.addGeoDVGlobal("winglet", [0, 0, 0, 1], winglet, lower=-5, upper=5) DVGeo.addPointSet(coords, "coords") DVGeo.setDesignVars({"winglet": [1.5, 2.5, -2.0, 0.60]}) hyp.setSurfaceCoordinates(DVGeo.update("coords")) # Run and write grid hyp.run() hyp.writeCGNS("717.cgns")
def regression_test(self, handler): ''' This is where the actual testing happens. ''' import copy from mpi4py import MPI from baseclasses import AeroProblem from ... import ADFLOW from pyspline import Curve from pygeo import DVGeometry from idwarp import USMesh from commonUtils import adjoint_test, adflowDefOpts, IDWarpDefOpts comm = MPI.COMM_WORLD gridFile = 'input_files/mdo_tutorial_euler_scalar_jst.cgns' ffdFile = 'input_files/mdo_tutorial_ffd.fmt' options = copy.copy(adflowDefOpts) options.update({ 'gridfile': gridFile, 'restartfile': gridFile, 'mgcycle': '2w', 'ncyclescoarse': 250, 'ncycles': 500, 'monitorvariables': ['cpu', 'resrho', 'cl', 'cd', 'cmz', 'totalr'], 'usenksolver': True, 'l2convergence': 1e-14, 'l2convergencecoarse': 1e-2, 'nkswitchtol': 1e-2, 'adjointl2convergence': 1e-14, 'solutionprecision': 'double', 'gridprecision': 'double', }) # Setup aeroproblem, cfdsolver, mesh and geometry. ap = AeroProblem(name='mdo_tutorial', alpha=1.8, mach=0.80, P=20000.0, T=220.0, areaRef=45.5, chordRef=3.25, beta=0.0, R=287.87, xRef=0.0, yRef=0.0, zRef=0.0, evalFuncs=['cl', 'cd']) # Create the solver CFDSolver = ADFLOW(options=options, comm=comm, debug=False) # Create mesh warper meshOptions = copy.copy(IDWarpDefOpts) meshOptions.update({'gridFile': gridFile}) mesh = USMesh(options=meshOptions, comm=comm) CFDSolver.setMesh(mesh) # Setup geometry/mesh DVGeo = DVGeometry(ffdFile) nTwist = 6 DVGeo.addRefAxis( 'wing', Curve(x=numpy.linspace(5.0 / 4.0, 1.5 / 4.0 + 7.5, nTwist), y=numpy.zeros(nTwist), z=numpy.linspace(0, 14, nTwist), k=2)) def twist(val, geo): for i in range(nTwist): geo.rot_z['wing'].coef[i] = val[i] DVGeo.addGeoDVGlobal('twist', [0] * nTwist, twist, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVLocal('shape', lower=-0.5, upper=0.5, axis='y', scale=10.0) CFDSolver.setDVGeo(DVGeo) # Run adjoint test adjoint_test(handler, CFDSolver, ap)
# Set up global design variables def twist_front(val, geo): for i in range(1, nRefAxPts_front): geo.rot_z['wing_front'].coef[i] = val[i - 1] def twist_back(val, geo): for i in range(1, nRefAxPts_back): geo.rot_z['wing_back'].coef[i] = val[i - 1] DVGeo_front.addGeoDVGlobal(dvName='twist_front', value=[0] * nTwist_front, func=twist_front, lower=-10, upper=10, scale=0.01) DVGeo_back.addGeoDVGlobal(dvName='twist_back', value=[0] * nTwist_back, func=twist_back, lower=-10, upper=10, scale=0.01) # Set up local design variables DVGeo_front.addGeoDVLocal('local_front', lower=-0.05, upper=0.05, axis='y',
def test6(): # **************************************************************************** printHeader('MDO tutorial RANS Geometric Variables') # **************************************************************************** aeroOptions = copy.deepcopy(defOpts) # Now set the options that need to be overwritten for this example: aeroOptions.update( {'gridfile': '../inputFiles/mdo_tutorial_rans.cgns', 'mgcycle':'2w', 'equationtype':'RANS', 'smoother':'dadi', 'nsubiterturb':3, 'nsubiter':3, 'cfl':1.5, 'cflcoarse':1.25, 'ncyclescoarse':250, 'ncycles':750, 'monitorvariables':['resrho','resturb','cl','cd','cmz','yplus','totalr'], 'usenksolver':True, 'l2convergence':1e-17, 'l2convergencecoarse':1e-2, 'nkswitchtol':1e-4, 'adjointl2convergence': 1e-16, 'nkls': 'non monotone', 'frozenturbulence':False, 'nkjacobianlag':2, } ) # Setup aeroproblem, cfdsolver ap = AeroProblem(name='mdo_tutorial', alpha=1.8, mach=0.80, altitude=10000.0, areaRef=45.5, chordRef=3.25, evalFuncs=['cl','cmz','drag']) ap.addDV('alpha') ap.addDV('mach') CFDSolver = ADFLOW(options=aeroOptions) if 'complex' in sys.argv: DVGeo = DVGeometry('../inputFiles/mdo_tutorial_ffd.fmt', complex=True) else: DVGeo = DVGeometry('../inputFiles/mdo_tutorial_ffd.fmt', complex=False) nTwist = 2 DVGeo.addRefAxis('wing', pyspline.Curve(x=numpy.linspace(5.0/4.0, 1.5/4.0+7.5, nTwist), y=numpy.zeros(nTwist), z=numpy.linspace(0,14, nTwist), k=2)) def twist(val, geo): for i in xrange(nTwist): geo.rot_z['wing'].coef[i] = val[i] def span(val, geo): # Span C = geo.extractCoef('wing') s = geo.extractS('wing') for i in xrange(len(C)-1): C[-1, 2] = C[-1, 2] + val[0] geo.restoreCoef(C, 'wing') DVGeo.addGeoDVGlobal('twist', [0]*nTwist, twist, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVGlobal('span', [0], span, lower=-10, upper=10, scale=1.0) DVGeo.addGeoDVLocal('shape', lower=-0.5, upper=0.5, axis='y', scale=10.0) mesh = MBMesh(options={'gridFile':'../inputFiles/mdo_tutorial_rans.cgns'}) CFDSolver.setMesh(mesh) CFDSolver.setDVGeo(DVGeo) #Aeroproblem must be set before we can call DVGeo.setDesignVars CFDSolver.setAeroProblem(ap) if not 'complex' in sys.argv: # Solve system CFDSolver(ap, writeSolution=False) funcs = {} CFDSolver.evalFunctions(ap, funcs) # Solve sensitivities funcsSens = {} CFDSolver.evalFunctionsSens(ap, funcsSens) # Write values and derivatives out: if MPI.COMM_WORLD.rank == 0: for key in ['cl','cmz','drag']: print 'funcs[%s]:'%key reg_write(funcs['mdo_tutorial_%s'%key],1e-10,1e-10) # Now write the derivatives in the same order the CS will do them: print ('Twist[0] Derivatives:') reg_write(funcsSens['mdo_tutorial_cl']['twist'][0][0], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_cmz']['twist'][0][0], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_drag']['twist'][0][0], 1e-10,1e-10) print ('Span Derivatives:') reg_write(funcsSens['mdo_tutorial_cl']['span'][0], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_cmz']['span'][0], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_drag']['span'][0], 1e-10,1e-10) print ('shape[13] Derivatives:') reg_write(funcsSens['mdo_tutorial_cl']['shape'][0][13], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_cmz']['shape'][0][13], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_drag']['shape'][0][13], 1e-10,1e-10) print ('mach Derivatives:') reg_write(funcsSens['mdo_tutorial_cl']['mach_mdo_tutorial'], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_cmz']['mach_mdo_tutorial'], 1e-10,1e-10) reg_write(funcsSens['mdo_tutorial_drag']['mach_mdo_tutorial'], 1e-10,1e-10) else: # For the complex....we just do successive perturbation for ii in range(4): xRef = {'twist':[0.0, 0.0], 'span':[0.0], 'shape':numpy.zeros(72, dtype='D'), 'mach_mdo_tutorial':0.8} if ii == 0: xRef['twist'][0] += h*1j elif ii == 1: xRef['span'][0] += h*1j elif ii == 2: xRef['shape'][13] += h*1j else: xRef['mach_mdo_tutorial']+=h*1j ap.setDesignVars(xRef) CFDSolver.resetFlow(ap) DVGeo.setDesignVars(xRef) CFDSolver(ap, writeSolution=False) funcs = {} CFDSolver.evalFunctions(ap, funcs) if MPI.COMM_WORLD.rank == 0: if ii == 0: for key in ['cl','cmz','drag']: print 'funcs[%s]:'%key reg_write(numpy.real(funcs['mdo_tutorial_%s'%key]),1e-10,1e-10) if ii == 0: print ('Twist[0] Derivatives:') elif ii == 1: print ('Span Derivatives:') elif ii == 2: print ('shape[13] Derivatives:') elif ii == 3: print ('mach Derivatives:') for key in ['cl','cmz','drag']: deriv = numpy.imag(funcs['mdo_tutorial_%s'%key])/h reg_write(deriv,1e-10,1e-10) del CFDSolver del mesh