def run_test1(): #xdoe = np.array([[0.,0],[0,0.5],[0,1],[0.5,0],[0.5,0.5],[0.5,1],[1.,0],[1.,0.5],[1,1]]) xdoe = read_tabulated_data_without_header('LHC9_2.txt') ffd = read_tabulated_data_without_header('ffd5_2.txt') ffd = (ffd-1)/4.0 xdoe = (xdoe+1.0)/2. fdoe = [func(xx) for xx in xdoe] newfunc = Function(func) err1,errSq1 = newfunc(xdoe) fitness = get_fitness(err1) xnew = np.zeros(xdoe.shape) for i in range(4): idx1 = selection(fitness) idx2 = selection(fitness,idx1) xnew[2*i],xnew[2*i+1] = cross_over(xdoe[idx1],xdoe[idx2]) idx1 = selection(fitness) idx2 = selection(fitness,idx1) xnew[-1],_t = cross_over(xdoe[idx1],xdoe[idx2]) fnew = [func(xx) for xx in xnew] xnew1 = np.vstack([xdoe,xnew]) fnew1 = [func(xx) for xx in xnew1] err,errSq2 = newfunc(xnew1) print errSq1, errSq2 xrand = read_tabulated_data_without_header('LHC18_2.txt') xrand = (xrand+1.0)/2. #xrand = np.random.random([18,2]) frand = [func(xx) for xx in xrand] print newfunc(xrand)[1] model1 = RbfMod(xnew1,fnew1) model2 = RbfMod(xrand,frand) print calc_error(func,model1,ffd) print calc_error(func,model2,ffd) lb = np.array([0,0.]) ub = np.array([1.,1]) n = 40 xs = ys = np.linspace(lb[0],ub[0],n) X, Y = np.meshgrid(xs,ys) zs1 = np.array([func([xs,ys]) for xs,ys in zip(np.ravel(X),np.ravel(Y))]) Z1 = zs1.reshape(X.shape) zs2 = np.array([model1([xs,ys]) for xs,ys in zip(np.ravel(X),np.ravel(Y))]) Z2 = zs2.reshape(X.shape) zs3 = np.array([model2([xs,ys]) for xs,ys in zip(np.ravel(X),np.ravel(Y))]) Z3 = zs3.reshape(X.shape) fig2 = plt.figure(1) ax2 = Axes3D(fig2) ax2.hold(True) ax2.plot_wireframe(X,Y,Z1,color='b') ax2.plot_wireframe(X,Y,Z2,color='r') ax2.plot_wireframe(X,Y,Z3,color='y') ax2.plot(xdoe[:,0],xdoe[:,1],fdoe,'ro') ax2.plot(xnew[:,0],xnew[:,1],fnew,'go') #ax2.plot(xdoe[:,0],xdoe[:,1],err1,'ko') ax2.plot(xrand[:,0],xrand[:,1],frand,'bo') ax2.legend(['High fidelity function','Low fidelity function']) ax2.set_xlabel('x1') ax2.set_ylabel('x2') ax2.set_zlabel('f (x1,x2)') plt.show()
def calculate_scaling_factors(): def process_cd0(CL0=-.1,f=None): if f.ndim==2: CD1 = f[:,1] CD2 = f[:,4] CL1 = f[:,0] CL2 = f[:,3] else: CD1 = f[1] CD2 = f[4] CL1 = f[0] CL2 = f[3] tmp1 = (CL1-CL0)**2. tmp2 = (CL2-CL0)**2. k = (CD1 - CD2)/(tmp1 - tmp2) CD0 = CD1 - k*CL1*CL1 return CD0, k def adjust_cd0(f,cd0avl,kavl): bnds = ((-.1,.1),) def obj(x,f): cd0,k = process_cd0(x,f) return 1e4*(cd0-cd0avl)**2.0 + (k-kavl)**2.0 cl0 = fmin_slsqp(obj,[0],bounds=bnds,args=(f,))[0] cd0,k = process_cd0(cl0,f) return cd0,k,cl0 xpath = r'E:\1. Current work\2014 - UAV performance code\Results\DOE\LHS_dvar9_sample24.txt' fpath = r'E:\1. Current work\2014 - UAV performance code\Results\DOE\CFD_FW_results-longitudinal2.txt' out = r'E:\1. Current work\2014 - UAV performance code\Results\DOE\CFD_FW_results-longitudinal3.txt' ac = DesignFormulation() ac.load_xls('Baseline1') ac.setup() xData = read_tabulated_data_without_header(xpath) fData = read_tabulated_data_without_header(fpath,1) k = np.zeros(fData.shape[0]) CD0 = np.zeros(fData.shape[0]) #CD0cfd, kcfd = process_cd0(f=fData) fid = open(out,'wt') fid.write('CD0avl\tkavl\tCD0cfd\tkcfd\tCD0add\tkadd\tCL0\n') for i,x in enumerate(xData): ac.set_x(x) aero = ac.get_aero_single_point(.7,1e4,alpha=2.) k[i] = aero.k CD0[i] = aero.CD0 CD0cfd,kcfd,cl0 = adjust_cd0(fData[i],CD0[i],k[i]) print CD0cfd, kcfd, cl0 print 'case ',i fid.write('%.9f\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f\t%.9f\n'%(CD0[i],k[i],CD0cfd,kcfd,CD0cfd-CD0[i],kcfd-k[i],cl0)) fid.close()
def run_design_table(): from misc_tools import read_tabulated_data_without_header, Normalization data = read_tabulated_data_without_header('DOE_LHS250_FFD2_3_3.txt') pathOut = 'design_out4.txt' ac = DesignFormulation() ac.load_xls('Baseline1') ac.propulsion._build_thrust_table() ac.setup() normalizers = list() for l,u in zip(ac.lb, ac.ub): normalizers.append(Normalization(l,u)) dataNew = np.zeros(data.shape) n = data.shape[1] for i,xNorm in enumerate(data): dataNew[i] = np.array([normalizers[ii].denormalize(xNorm[ii]) for ii in range(n)]) ac.set_x(dataNew[i]) fid = open(pathOut,'at') for val in ac.analysisData: fid.write('%.20f\t'%val) fid.write('\n') fid.close() print i,'\n', ac.analysisData
def _load_rbf_models(self,DOEpath=None,rsltPath=None): if DOEpath==None: DOEpath = r'D:\1. Current work\2014 - UAV performance code\Results\DOE\LHS_dvar9_sample24.txt' if rsltPath==None: rsltPath = r'D:\1. Current work\2014 - UAV performance code\Results\DOE\CFDresults.txt' dCD0 = 0.0005 xDOE = read_tabulated_data_without_header(DOEpath) fDOE = read_tabulated_data_without_header(rsltPath,1) self.CLrbf = RbfMod(xDOE, fDOE[:,4]-fDOE[:,13]) self.CDrbf = RbfMod(xDOE, fDOE[:,5]-fDOE[:,14]) self.CD0rbf= RbfMod(xDOE, fDOE[:,11]-fDOE[:,19]+dCD0) self.krbf = RbfMod(xDOE, fDOE[:,12]-fDOE[:,20]) self.SMrbf = RbfMod(xDOE, fDOE[:,10]-fDOE[:,18],0.5) self.LDrbf = RbfMod(xDOE, fDOE[:,7] -fDOE[:,15]) self._xdoe = xDOE
def create_input_files(): from ucav_design_1 import DesignFormulation # Longitudinal (half wing) doePath = r'D:\1. Current work\2014 - UAV performance code\Results\norm_results.txt' #DOE = read_tabulated_data_without_header('LHS_dvar9_sample30.txt') DOE = read_tabulated_data_without_header(doePath) ac = DesignFormulation() ac.load_xls('Baseline1') ac.setup() startCase = 30 # Mach = ac.designGoals.cruiseMach # altitude = ac.designGoals.cruiseAltitude yplus = .5 wdir = 'D:\CFD' alpha = [-2,0,2] # beta = 2.0 niter = 5000 batFileLongitudinal = '%s\\batchCFD.bat'%wdir fid = open(batFileLongitudinal,'at') pathFluent = r'C:\Program Files\Ansys Inc\v130\fluent\ntbin\win64\fluent.exe' for i,xnorm in enumerate(DOE): if i>0: name = '%s\\case%d'%(wdir,i+startCase) caseName = 'case%d'%(i+startCase) print name ac.set_x(xnorm,False) print ac.xCurrent raw_input() #FIXME: this part adds section for payload on top #ac._adjust_root_airfoil() ac.save_2d_figure('%s.png'%name) igsPath = '%s.igs'%name symCasPath = '%s_sym.cas'%name nonsymCasPath = '%s_half.cas'%name glfPath = '%s.glf'%name #create_catia_wing(ac,igsPath) create_fw_cmesh(ac,igsPath,symCasPath,nonsymCasPath,yplus,glfPath) Sref = ac.wing.area/2.0 Cref = ac.wing.MAC #Lref = ac.wing.span ac._restore_root_airfoil() CG = ac.get_cg() #a = 2.0 # outPrefix = '%s\\results\\case%d_sym_a%d_b%d'%(wdir,i+1,a*100,beta*100) # journalFileRel = 'case%d_sym_a%d_b%d.jou'%(i+1, a*100,beta*100) # journalFile = '%s_sym_a%d_b%d.jou'%(name, a*100, beta*100) #run_wing_half(symCasPath, outPrefix, ac.designGoals.fc, a, beta, Sref, Lref, CG, journalFile, niter) for a in alpha: outPrefix = '%s\\results\\%s_half_a%d'%(wdir,caseName,a*100) journalFileRel = '%s_half_a%d.jou'%(name, a*100) journalFile = '%s_half_a%d.jou'%(name, a*100) run_wing_half(nonsymCasPath, outPrefix, ac.designGoals.fc, a, 0, Sref, Cref, CG,journalFile,niter) fid.write('\"%s\" 3ddp -t8 -i %s -wait\n'%(pathFluent, journalFileRel)) fid.close()
def run_full_table_analysis(wdir): X,lb,ub,fc = get_initial_data() pathDOE = os.getcwd() + '\\' + wdir + '\\1. input.txt' pathOutput = os.getcwd() + '\\' + wdir + '\\2. outputAero.txt' clcruise = 0.4 fid = open(pathOutput,'wt') fid.write('Au0\tAu1\tAu2\tAu3\tAu4\tAl0\tAl1\tAl2\tAl3\tAl4\tLDmax\talphaLDmax\tClmax\talphaClmax\tCdAtLDmax\tLD32max\tthickness\tcdcruise\n') fid.close() #designs = read_doe(pathDOE) designs = read_tabulated_data_without_header(pathDOE) for i,design in enumerate(designs): design = denormalize(design,lb,ub) af = airfoil.cst(design[0:5],design[5:10]) filename = wdir + 'polar_%d.txt'%(i+1) result = aero_analysis(af, fc, filename) write_output(design,result,pathOutput,clcruise) af.write_txt(wdir + 'coordinates_%d.txt'%(i+1))
def run_optimization_1(): pathIn = "design_out4.txt" pathInSamples = "DOE_LHS250_FFD2_3_3.txt" learnData = read_tabulated_data_without_header(pathIn) xNorm = read_tabulated_data_without_header(pathInSamples) # xNorm = np.transpose(xNorm) learnData = np.transpose(learnData) WemptyMax = 3500.0 CnbMin = 0.0001 ClbMax = -0.05 SMmin = -0.05 SMmax = 0.10 combatRadiusMin = 1000.0 RCmin = 125.0 VmaxMin = 0.90 # Mach LD = RbfMod(xNorm, learnData[0]) We = RbfMod(xNorm, learnData[1]) _Cnb = RbfMod(xNorm, (learnData[2]) * 1e3) Clb = RbfMod(xNorm, learnData[3]) _SM = RbfMod(xNorm, learnData[4] * 1e3) Rcombat = RbfMod(xNorm, learnData[5]) RC = RbfMod(xNorm, learnData[6]) Vmax = RbfMod(xNorm, learnData[7]) Cnb = lambda x: _Cnb(x) / 1e3 SM = lambda x: _SM(x) / 1e3 bnds = np.ones([9, 2]) bnds[:, 0] = -bnds[:, 0] bnds[8, 0] = 0 bnds[7, 0] = 0 bnds = tuple(bnds) f = lambda x: -LD(x) g1 = lambda x: WemptyMax - We(x) g2 = lambda x: (Cnb(x) - CnbMin) * 1e3 g3 = lambda x: ClbMax - Clb(x) g4 = lambda x: SMmax - SM(x) g5 = lambda x: SM(x) - SMmin g6 = lambda x: Rcombat(x) / 1e3 - combatRadiusMin g7 = lambda x: RC(x) - RCmin g8 = lambda x: Vmax(x) - VmaxMin cnstr = ( {"type": "ineq", "fun": g1}, {"type": "ineq", "fun": g2}, {"type": "ineq", "fun": g3}, {"type": "ineq", "fun": g4}, {"type": "ineq", "fun": g5}, {"type": "ineq", "fun": g6}, {"type": "ineq", "fun": g7}, {"type": "ineq", "fun": g8}, ) x0 = np.zeros(9) rslt = minimize(f, x0, method="SLSQP", constraints=cnstr, bounds=bnds) print rslt rslt2 = minimize(f, x0, constraints=cnstr, bounds=bnds, options={"maxiter": 10000}) print rslt2 xopt = rslt.x print g1(xopt), We(xopt) print g2(xopt), Cnb(xopt) print g3(xopt), Clb(xopt) print g4(xopt), SM(xopt) print g5(xopt), SM(xopt) print g6(xopt), Rcombat(xopt) print g7(xopt), RC(xopt) print g8(xopt), Vmax(xopt) ac = DesignFormulation() ac.load_xls("Baseline1") ac.setup() ac.set_x(ac.x0) x2dBaseline = ac.wing.x2d y2dBaseline = ac.wing.y2d print "Baseline" print ac.analysisData print "--->" normalize = Normalization(ac.lb, ac.ub) xoptDenorm = normalize.denormalize(xopt) print xoptDenorm ac.set_x(xoptDenorm) x2dOptimum = ac.wing.x2d y2dOptimum = ac.wing.y2d print ac.analysisData, "\n---" print (ac.analysisData[0] - LD(xopt)) / ac.analysisData[0] print (ac.analysisData[1] - We(xopt)) / ac.analysisData[1] print (ac.analysisData[2] - Cnb(xopt)) / ac.analysisData[2] print (ac.analysisData[3] - Clb(xopt)) / ac.analysisData[3] print (ac.analysisData[4] - SM(xopt)) / ac.analysisData[4] print (ac.analysisData[5] - Rcombat(xopt)) / ac.analysisData[5] print (ac.analysisData[6] - RC(xopt)) / ac.analysisData[6] print (ac.analysisData[7] - Vmax(xopt)) / ac.analysisData[7] plt.figure() plt.hold(True) plt.plot(x2dBaseline, y2dBaseline, "r-") plt.plot(x2dOptimum, y2dOptimum, "k-") plt.legend(["Baseline", "Low-fi Optimum"]) plt.show() ac.display()
def get_parasite_drag_fw(ac,altitude=0.0): path1 = pth.aeroCD0in path2 = pth.aeroCD0inWave W = ac.designGoals.grossMass*9.81 rho = ac.designGoals.fc.atm.density V = ac.designGoals.cruiseSpeed Sref = ac.wing.area CL=W/(0.5*rho*V*V*Sref) # --- friction + form drag input --- fid1 = open(path1,'wt') #h = ac.designGoals.cruiseAltitude #h = convert.m_to_ft(h) h = float(altitude) Sref = convert.sqm_to_sqft(Sref) Abase = 0.095 Dexit = 0.0 Ewdd = 1.8 prleak = 10.0 fid1.write('%d %.0f %.0f %.4f %.1f %.1f %.1f\n'%(1,h,Sref,Abase, Dexit, Ewdd, prleak)) ale = ac.wing.equivSweepLEdeg aqc = ac.wing.equivSweepC4deg ahc = ac.wing.equivSweepC2deg a4 = ac.wing.span a4 = convert.m_to_ft(a4) # --- t1 = ac.wing.airfoils[0].thickness*ac.wing.chords[0] t2 = ac.wing.airfoils[1].thickness*ac.wing.chords[1] Amax = (t1+t2)*ac.wing.segSpans[0] # --- ttc = ac.wing.equivThickness fc = ac.wing.equivCamber cld = CL xa = 1.02 fid1.write('%.4f %.4f %.4f %.4f %.4f %.4f %.6f %.4f %.2f\n'%(ale,aqc,ahc,a4,Amax,ttc,fc,cld,xa)) wSwet = ac.wing.wettedArea wSwet = convert.sqm_to_sqft(wSwet) wRefL = convert.m_to_ft(ac.wing.MAC) fid1.write('Wing %.4f %.4f 1 '%(wSwet,wRefL)) wtc = ac.wing.equivThickness wc4 = ac.wing.equivSweepC4deg wxtc = ac.wing.equivThicknessLoc q = 1.0 #??? fid1.write('%.4f %.4f %.4f %.4f'%(wtc,wc4,wxtc,q)) fid1.close() # --- wave drag input --- fid2 = open(path2,'wt') fid2.write('1 0 0 0 0 0 0 0\n') wrle = ac.wing.equivLEradius cam = ac.wing.equivCamber fid2.write('%.4f %.4f %.4f %.4f %d\n'%(wtc,wxtc,wrle,cam, 1)) fid2.write('0.0394 0.5 0. 0. 0\n') fid2.write('0. 0. 0. 0. 1\n') fid2.write('0.04 0.5 0. 0. 0\n') wte = ac.wing.equivSweepTEdeg lref = convert.m_to_ft(ac.wing.MAC) AR = ac.wing.aspectRatio fid2.write('%.4f %.4f %.4f %.4f %.4f\n'%(ale,wte,lref,AR,Sref)) fid2.write('44.56 0. 0.29 2.24\n') fid2.write('0. 0. 0. 0.\n') fid2.write('44. 3. 0.277 2.48\n') fid2.write('0. 0. 0. 0. 0.\n') fid2.write('2\n') fid2.write('21.08 20.92 19.364 4.97 2.9\n') fid2.write('0.35 0.45 0.47 0.9339\n') fid2.close() os.system(pth.aeroCD0) data = read_tabulated_data_without_header(pth.aeroCD0out,3) pth.clean_drag_files() CD = data[:-1,-1] M = data[:-1,0] Mdd = data[-1,0] CDdd = data[-1,-1] return M, CD, Mdd, CDdd