def get_cls(coords): xf = XFoil() xf.print = False cl_s = [] for coord in coords: cl = get_cl(coord, xf) cl_s.append(cl) return np.array(cl_s)
def get_cl(coord, xf=None, angle=5): if xf is None: xf = XFoil() xf.print = False xf.Re = 3e6 xf.max_iter = 100 datax, datay = coord.reshape(2, -1) xf.airfoil = Airfoil(x=datax, y=datay) c = xf.a(angle) cl= c[0] return cl
def initialize(self): super().initialize() self.options.declare("print", default=False, types=bool) xf = XFoil() xf.print = False self.options.declare("_xf", default=xf, types=XFoil, allow_none=True) self.options.declare("_pool", default=ThreadPool(processes=1), types=ThreadPool, allow_none=True) self.recording_options["options_excludes"] = ["_xf", "_pool"]
def evaluate(self,individual): DELTA = 1e10 #---------------------------------- #遺伝子に基づいて新翼型を生成 #---------------------------------- #遺伝子に基づきスプライン翼型を作成 x = individual[:int(len(individual)/2)] x.insert(0,1.0) x.insert(int(len(x)/2)+1,0.0) x.append(1.0) y = individual[int(len(individual)/2):] if not (all([u - d > 0 for u, d in zip(y[:int(len(y)/2)], y[int(len(y)/2):])]) or all([u - d < 0 for u, d in zip(y[:int(len(y)/2)], y[int(len(y)/2):])])): print("crossed") return [DELTA*10]*self.NOBJ y.insert(0,0.0) y.insert(int(len(y)/2)+1,0.0) y.append(0.0) newdat = fc.spline_foil(x, y, 200) shape_dat = fc.shape_dat([[a, b] for a, b in zip(newdat[0][::-1], newdat[1][::-1])]) #翼型の形に関する情報を取得する foil_para = fc.get_foil_para(shape_dat) mt, mta, mc, mca, s, crossed, bd, bt, bc, smooth, td = foil_para # mt: 最大翼厚(百分率) # mta: 最大翼厚位置(百分率) # mc: 最大キャンバー(百分率) # mca: 最大きゃんばー位置(百分率) # s: 翼型の下面における、最大y座標-最小y座標 # crossed: 翼型が交差しているならTrue,それ以外ならFalse # bd: 翼型の粗さ(大きいほど粗い) # bt: 翼厚分布の粗さ(大きいほど粗い) # bc: キャンバー分布の粗さ(大きいほど粗い) # smooth: 無視 # td: 翼厚分布 if crossed: print("crossed_a") return [DELTA*10]*self.NOBJ else: print("hi_a") #新しい翼型をAerofoilオブジェクトに適用 datx = np.array(newdat[0][::-1]) daty = np.array(newdat[1][::-1]) newfoil = Airfoil(x = datx, y = daty) #翼型の形に関する拘束条件 penalty = 0 if not all([t >= 0.0035 for t in td[10:80]]): penalty += 100 * (sum([abs(t - 0.0035)*10 for t in td[15:85] if t - 0.0035 < 0])) if not all([t <= 0.015 for t in td[:15]]): penalty += 100 * (sum([abs(t - 0.015)*10 for t in td[:15] if t > 0.015])) if mta > 0.4: penalty += 100 * (mta - 0.4) if mc < 0.0: penalty += 100 * (-mc) if datx[0] > 1.002 or datx[0] < 0.998: print("invalid foil") return [DELTA*10]*self.NOBJ #---------------------------------- #新翼型の解析 #---------------------------------- try: xf = XFoil() #レイノルズ数の設定 xf.airfoil = newfoil xf.Re = self.re xf.print = False xf.max_iter = 40 #xf.polar = "polar" + id #境界要素法計算時1ステップにおける計算回数 #xf.repanel(n_nodes = 180) #計算結果格納 #result = xf.OneAlpha() cl, cd, cm, cp = xf.a(5.0) #---------------------------------- #目的値 #---------------------------------- if cl >= 0: obj1 = 1/cl else: obj1 = self.delta obj2 = cd except Exception as e: obj1,obj2=[DELTA]*self.NOBJ traceback.print_exc() if (np.isnan(obj1)): obj1 = DELTA if (np.isnan(obj2)): obj2 = DELTA return [obj1 + penalty, obj2 + penalty]
def analyze_airfoil(x, y_u, y_l, cl, rey, mach=0, xf=None, pool=None, show_output=False): """ Analyze an airfoil at a given lift coefficient for given Reynolds and Mach numbers using XFoil. Parameters ---------- x : array_like Airfoil x-coordinates y_u, y_l : array_like Airfoil upper and lower curve y-coordinates cl : float Target lift coefficient rey, mach : float Reynolds and Mach numbers xf : XFoil, optional An instance of the XFoil class to use to perform the analysis. Will be created if not given pool : multiprocessing.ThreadPool, optional An instance of the multiprocessing.Threadpool class used to run the xfoil_worker. Will be created if not given show_output : bool, optional If True, a debug string will be printed after analyses. False by default. Returns ------- cd, cm : float or np.nan Drag and moment coefficients of the airfoil at specified conditions, or nan if XFoil did not run successfully """ # If the lower and upper curves swap, this is a bad, self-intersecting airfoil. Return 1e27 immediately. if np.any(y_l > y_u): return np.nan else: clean_xf = False if xf is None: xf = XFoil() xf.print = show_output clean_xf = True clean_pool = False if pool is None: pool = ThreadPool(processes=1) clean_pool = True xf.airfoil = Airfoil(x=np.concatenate((x[-1:0:-1], x)), y=np.concatenate((y_u[-1:0:-1], y_l))) xf.Re = rey xf.M = mach xf.max_iter = 100 xf.n_crit = 0.1 cd, cm = pool.apply(xfoil_worker, args=(xf, cl)) if clean_xf: del xf if clean_pool: del pool return cd, cm, None if clean_xf else xf
def evaluate(individual): global code_division #---------------------------------- #遺伝子にも続いて新翼型を生成 #---------------------------------- #遺伝子をデコード ratios = decoder(individual, code_division) #遺伝子に基づき翼型を混合して、新しい翼型を作る datlist_list = [fc.read_datfile(file) for file in datfiles] datlist_shaped_list = [fc.shape_dat(datlist) for datlist in datlist_list] newdat = fc.interpolate_dat(datlist_shaped_list,ratios) #翼型の形に関する情報を取得する #foilpara == [最大翼厚、最大翼厚位置、最大キャンバ、最大キャンバ位置、S字の強さ] foil_para = fc.get_foil_para(newdat) #新しい翼型をAerofoilオブジェクトに適用 datx = np.array([ax[0] for ax in newdat]) daty = np.array([ax[1] for ax in newdat]) newfoil = Airfoil(x = datx, y = daty) mt, mta, mc, mca, s = foil_para #---------------------------------- #翼の形に関する拘束条件 #---------------------------------- penalty = 0 print('===================') if(mc<0): print("out of the border") print("reverse_cmaber") penalty -= mc if(mt<0.08): print("out of the border") print("too_thin") penalty += 0.08-mt if(mt>0.11): print("out of the border") print("too_fat") penalty += mt-0.11 #if(foil_para[4]>0.03): # print("out of the border") # print("peacock") # print('===================') # return (1.0+(foil_para[4]-0.03),)*NOBJ if(mta<0.23): print("out of the border") print("Atama Dekkachi!") penalty += 0.23 - mta if(mta>0.3): print("out of the border") print("Oshiri Dekkachi!") penalty += mta - 0.3 #---------------------------------- #新翼型の解析 #---------------------------------- xf = XFoil() xf.airfoil = newfoil #レイノルズ数の設定 xf.Re = 1.5e5 #境界要素法計算時1ステップにおける計算回数 xf.max_iter = 60 #座標整形 xf.repanel(n_nodes = 300) xf.print = False #計算結果格納 a, cl, cd, cm, cp = xf.cseq(0.4, 1.1, 0.1) lr = [l/d for l, d in zip(cl,cd)] #---------------------------------- #目的値 #---------------------------------- try: #揚抗比の逆数を最小化 obj1 = 1/lr[1] #揚抗比のピークを滑らかに(安定性の最大化) maxlr = max(lr) maxlr_index = lr.index(maxlr) obj2 = abs(maxlr - lr[maxlr_index+1]) #下面の反りを最小化(製作再現性の最大化) obj3 = foil_para[4] except Exception as e: obj1,obj2,obj3=[1.0]*NOBJ traceback.print_exc() if (np.isnan(obj1) or obj1 > 1): obj1 = 1 if (np.isnan(obj2) or obj2 > 1): obj2 = 1 if (np.isnan(obj3) or obj3 > 1): obj3 = 1 obj1 += penalty obj2 += penalty obj3 += penalty print("individual",individual) print("evaluate",obj1,obj2,obj3) print("max_thickness",foil_para[0]) print("at",foil_para[1]) print("max_camber",foil_para[2]) print("at",foil_para[3]) print("S",foil_para[4]) print('===================') return [obj1, obj2, obj3]
if not use_dataset: npz = np.load(input_path) labels = npz[npz.files[0]] coords = npz[npz.files[1]] else: perfs_npz = np.load("./dataset/standardized_perfs.npz") coords_npz = np.load("./dataset/standardized_coords.npz") coords = coords_npz[coords_npz.files[0]] coord_mean = coords_npz[coords_npz.files[1]] coord_std = coords_npz[coords_npz.files[2]] perfs = perfs_npz[perfs_npz.files[0]] perf_mean = perfs_npz[perfs_npz.files[1]] perf_std = perfs_npz[perfs_npz.files[2]] xf = XFoil() xf.print = False cnt = 0 print("start calculating!") start = time.time() coords = coords*coord_std+coord_mean if use_dataset else coords for label, coord in zip(labels, coords): cl = get_cl(xf, coord) if not np.isnan(cl): cnt+=1 if type(label) is np.float64: label = str(round(label, 3)) else: label = str(round(label[0],3)) print("label: {0}, cl: {1}".format(label, cl)) end = time.time()
def feature_xfoil(cst_u, cst_l, t, Minf: float, Re, AoA, n_crit=0.1, fname='feature-xfoil.txt'): ''' Evaluate by xfoil and extract features. Inputs: --- cst-u, cst-l: list of upper/lower CST coefficients of the airfoil. \n t: airfoil thickness or None \n Minf: free stream Mach number for wall Mach number calculation \n Re, AoA (deg): flight condition (s), float or list, for Xfoil \n n_crit: critical amplification ratio for transition in xfoil \n fname: output file name. If None, then no output \n ### Dependencies: cst-modeling3d, xfoil ''' from cst_modeling.foil import cst_foil from xfoil import XFoil from xfoil.model import Airfoil #TODO: Build foil #! 201 is the maximum amount of points that xfoil can handle #! tail = 0.001 is to avoid point overlap xx, yu, yl, t0, R0 = cst_foil(201, cst_u, cst_l, x=None, t=t, tail=0.001) #! xfoil do not support leading edge of (0,0) on both upper and lower surface x = np.array(list(reversed(xx[1:])) + xx[1:]) y = np.array(list(reversed(yu[1:])) + yl[1:]) foil = Airfoil(x, y) #TODO: Xfoil xf = XFoil() xf.print = False xf.airfoil = foil xf.max_iter = 40 #* Transition by power law xf.n_crit = n_crit #TODO: Xfoil calculation if not isinstance(Re, list): Re = [Re] AoA = [AoA] n = len(Re) for i in range(n): xf.reset_bls() if Re[i] is not None: xf.Re = Re[i] cl, cd, cm, cp = xf.a(AoA[i]) x, cp = xf.get_cp_distribution() print(xf.Re, AoA[i], cl) #* Extract features fF = PhysicalXfoil(Minf, AoA[i], Re[i]) fF.setdata(x, y, cp) fF.extract_features() #* Output if fname is None: continue if i == 0: f = open(fname, 'w') else: f = open(fname, 'a') f.write('\n') f.write('%10s %15.6f \n' % ('Minf', Minf)) f.write('%10s %15.6f \n' % ('AoA', AoA[i])) f.write('%10s %15.6f \n' % ('Re', Re[i] / 1e6)) f.write('%10s %15.6f \n' % ('CL', cl)) f.write('%10s %15.6f \n' % ('Cd', cd)) f.write('%10s %15.6f \n' % ('Cm', cm)) f.close() fF.output_features(fname=fname, append=True)
def evaluate(self, individual): #解析が発散した際の評価値 DELTA = 1e10 #---------------------------------- #遺伝子に基づいて新翼型を生成 #---------------------------------- #遺伝子をデコード ratios = self.decoder(individual, self.code_division) #遺伝子に基づき翼型を混合して、新しい翼型を作る datlist_list = [fc.read_datfile(file) for file in self.datfiles] datlist_shaped_list = [ fc.shape_dat(datlist) for datlist in datlist_list ] newdat = fc.interpolate_dat(datlist_shaped_list, ratios) #翼型の形に関する情報を取得する mt, mta, mc, mca, s, crossed, bd, bt, bc, smooth, td = fc.get_foil_para( newdat) #新しい翼型をAerofoilオブジェクトに適用 datx = np.array([ax[0] for ax in newdat]) daty = np.array([ax[1] for ax in newdat]) newfoil = Airfoil(x=datx, y=daty) #---------------------------------- #翼の形に関する拘束条件 #---------------------------------- penalty = 0 #キャンバに関する拘束条件 if (mc < 0): penalty -= mc #最大翼厚に関する拘束条件 if (mt < 0.08): penalty += 0.08 - mt if (mt > 0.11): penalty += mt - 0.11 #最大翼厚位置に関する拘束条件 if (mta < 0.23): penalty += 0.23 - mta if (mta > 0.3): penalty += mta - 0.3 #---------------------------------- #新翼型の解析 #---------------------------------- xf = XFoil() xf.airfoil = newfoil #レイノルズ数の設定 xf.Re = self.re #境界要素法計算時1ステップにおける計算回数 xf.max_iter = 60 xf.print = False #計算結果格納 a, cl, cd, cm, cp = xf.cseq(0.4, 1.1, 0.1) #---------------------------------- #目的値 #---------------------------------- try: #揚抗比の逆数を最小化 obj1 = 1 / lr[1] #揚抗比のピークを滑らかに(安定性の最大化) maxlr = max(lr) maxlr_index = lr.index(maxlr) obj2 = abs(maxlr - lr[maxlr_index + 1]) #下面の反りを最小化(製作再現性の最大化) obj3 = s except Exception as e: obj1, obj2, obj3 = [DELTA] * self.NOBJ traceback.print_exc() if (np.isnan(obj1) or obj1 > 1): obj1 = DELTA if (np.isnan(obj2) or obj2 > 1): obj2 = DELTA if (np.isnan(obj3) or obj3 > 1): obj3 = DELTA obj1 += penalty obj2 += penalty obj3 += penalty return [obj1, obj2, obj3]