Created
May 22, 2018 13:01
-
-
Save helo9/796e413c3cf2895954f3ce923a87d877 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Spyder Editor | |
This is a temporary script file. | |
""" | |
import numpy as np | |
import pandas as pd | |
import glob, re | |
import collections as cl | |
from scipy.interpolate import LinearNDInterpolator | |
from abc import ABC, abstractmethod | |
class PolarInterpolator(ABC): | |
def __init__(self, path): | |
self.data = cl.defaultdict(dict) | |
self._importpath(path) | |
def _importpath(self, path): | |
self.interpolators = {} | |
for path in glob.glob(path+'/*_Re*_M*.txt'): | |
self._loadfile(path) | |
self._create_interpolation() | |
@abstractmethod | |
def _create_interpolation(self): | |
pass | |
class PolarInterpolatorRe(PolarInterpolator): | |
def _loadfile(self, filename): | |
with open(filename, 'r') as afile: | |
tmpdata = [] | |
for ii, line in enumerate(afile): | |
#print((ii,line)) | |
if ii>10: | |
linedata = np.fromstring(line.strip(), dtype=float, sep=' ') | |
if len(linedata)>0: | |
tmpdata.append(linedata) | |
else: | |
if ii == 2: | |
tmpairfoilname = line[22:].strip() | |
elif ii == 4 and 'Reynolds number fixed' not in line: | |
print('wrong polar type in file {}'.format(filename)) | |
return False | |
elif ii == 7: | |
reg = re.compile(r'\s*Mach\s*=\s*(\d+.\d+)\s*Re\s*=\s*(\d+.\d+\s?e\s+-?\d+)') | |
try: | |
restr = reg.match(line).groups()[1] | |
tmpreynolds = float(restr.replace(' ','')) | |
except: | |
raise Exception('no reynolds data found') | |
columns = 'alpha CL CD CDp Cm Top_Xtr Bot_Xtr Cpmin Chinge XCp'.split() | |
tmpdata = pd.DataFrame(np.array(tmpdata), columns=columns) | |
self.data[tmpairfoilname][tmpreynolds] = tmpdata | |
def _create_interpolation(self): | |
for airfoilname, af_data in self.data.items(): | |
values = [] | |
points = [] | |
for reynolds, rey_data in af_data.items(): | |
for ii, row in rey_data.iterrows(): | |
values.append(row.CD) | |
points.append((reynolds, row.CL)) | |
self.interpolators[airfoilname] = \ | |
LinearNDInterpolator(np.array(points), np.array(values), rescale=True) | |
def interpolate(self, airfoilname, reynolds, c_l): | |
if not airfoilname in self.interpolators.keys(): | |
raise Exception('No Polar Data for {}'.format(airfoilname)) | |
return float(self.interpolators[airfoilname](reynolds, c_l)) | |
class PolarInterpolatorFlight(PolarInterpolator): | |
def _loadfile(self, filename): | |
with open(filename, 'r') as afile: | |
tmpdata = [] | |
for ii, line in enumerate(afile): | |
#print((ii,line)) | |
if ii>10: | |
linedata = np.fromstring(line.strip(), dtype=float, sep=' ') | |
if len(linedata)>0: | |
tmpdata.append(linedata) | |
else: | |
if ii == 2: | |
tmpairfoilname = line[22:].strip() | |
elif ii == 4 and 'Reynolds number ~ 1/sqrt(CL)' not in line: | |
print('wrong polar type in file {}'.format(filename)) | |
return False | |
columns = 'alpha CL CD CDp Cm Top_Xtr Bot_Xtr Cpmin Chinge XCp'.split() | |
tmpdata = pd.DataFrame(np.array(tmpdata), columns=columns) | |
self.data[tmpairfoilname] = tmpdata | |
def _create_interpolation(self): | |
from scipy.interpolate import interp1d | |
for airfoilname, af_data in self.data.items(): | |
c_d = af_data.CD | |
c_l = af_data.CL | |
self.interpolators[airfoilname] = interp1d(c_l, c_d) | |
def interpolate(self, airfoilname, c_l): | |
if not airfoilname in self.interpolators.keys(): | |
raise Exception('No Polar Data for {}'.format(airfoilname)) | |
return float(self.interpolators[airfoilname](c_l)) | |
class PolarInterpolatorPlane(PolarInterpolator): | |
def __init__(self, path): | |
self._loadfile(path) | |
self._create_interpolation() | |
def _importpath(self, path): | |
raise Exception('not implementet for Plane Polars') | |
def _loadfile(self, filename): | |
with open(filename, 'r') as afile: | |
tmpdata = [] | |
for ii, line in enumerate(afile): | |
#print((ii,line)) | |
if ii>5: | |
linedata = np.fromstring(line.strip(), dtype=float, sep=' ') | |
if len(linedata)>0: | |
tmpdata.append(linedata) | |
else: | |
if ii == 3 and line.startswith('Plane name :'): | |
self.planename = line[13:].strip() | |
columns = 'alpha Beta CL CDi CDv CD CY Cl Cm Cn Cni QInf XCP'.split() | |
tmpdata = pd.DataFrame(np.array(tmpdata), columns=columns) | |
self.data = tmpdata | |
def _create_interpolation(self): | |
from scipy.interpolate import interp1d | |
# Interpolate viscous drag | |
c_d = self.data.CDv | |
c_l = self.data.CL | |
self.interpolator = interp1d(c_l, c_d) | |
def interpolate(self, cl): | |
return float(self.interpolator(cl)) | |
pi = None | |
if __name__ == '__main__': | |
pi = PolarInterpolatorRe('.') | |
pi2 = PolarInterpolatorFlight('.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment