Skip to content
Snippets Groups Projects
Commit affde360 authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

Merge branch 'mmpe' into 'master'

Mmpe

See merge request !44
parents 83f9bc0e 5da20b4c
No related branches found
No related tags found
1 merge request!44Mmpe
Pipeline #
......@@ -21,20 +21,12 @@ class PCFile(object):
examples
--------
>>> pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt", "tests/test_files/NREL_5MW_ae.txt")
# Same attributes as AEFile
>>> pcfile.thickness(36) # Interpolated thickness at radius 36
23.78048780487805
>>> pcfile.chord(36) # Interpolated chord at radius 36
3.673
>>> pcfile.pc_set_nr(36) # pc set number at radius 36
1
# Additional attributes
>>> pcfile.CL(36,10) # CL at radius=36m and AOA=10deg
>>> pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt")
>>> pcfile.CL(21,10) # CL for thickness 21% and AOA=10deg
1.358
>>> pcfile.CD(36,10) # CD at radius=36m and AOA=10deg
>>> pcfile.CD(21,10) # CD for thickness 21% and AOA=10deg
0.0255
>>> pcfile.CM(36,10) # CM at radius=36m and AOA=10deg
>>> pcfile.CM(21,10) # CM for thickness 21% and AOA=10deg
-0.1103
"""
def __init__(self, filename):
......@@ -59,7 +51,7 @@ class PCFile(object):
lptr += n_rows
self.pc_sets[nset] = (np.array(thicknesses), profiles)
def _Cxxx(self, thickness, alpha, column, ae_set_nr=1, pc_set_nr=1):
def _Cxxx(self, thickness, alpha, column, pc_set_nr=1):
thicknesses, profiles = self.pc_sets[pc_set_nr]
index = np.searchsorted(thicknesses, thickness)
if index == 0:
......@@ -71,7 +63,7 @@ class PCFile(object):
th0, th1 = thicknesses[index - 1:index + 1]
return Cx0 + (Cx1 - Cx0) * (thickness - th0) / (th1 - th0)
def _CxxxH2(self, thickness, alpha, column, ae_set_nr=1,pc_set_nr=1):
def _CxxxH2(self, thickness, alpha, column, pc_set_nr=1):
thicknesses, profiles = self.pc_sets[pc_set_nr]
index = np.searchsorted(thicknesses, thickness)
if index == 0:
......@@ -89,7 +81,7 @@ class PCFile(object):
def CL(self, thickness, alpha, ae_set_nr=1,pc_set_nr=1):
def CL(self, thickness, alpha, pc_set_nr=1):
"""Lift coefficient
Parameters
......@@ -98,20 +90,20 @@ class PCFile(object):
thickness [5]
alpha : float
Angle of attack [deg]
ae_set_nr : int optional
Aerdynamic set number, default is 1
pc_set_nr : int optional
pc set number, default is 1, normally obtained from ae-file
Returns
-------
Lift coefficient : float
"""
return self._Cxxx(thickness, alpha, 1, ae_set_nr, pc_set_nr)
return self._Cxxx(thickness, alpha, 1, pc_set_nr)
def CL_H2(self, thickness, alpha, ae_set_nr=1, pc_set_nr=1):
return self._CxxxH2(thickness, alpha, 1, ae_set_nr, pc_set_nr)
def CL_H2(self, thickness, alpha, pc_set_nr=1):
return self._CxxxH2(thickness, alpha, 1, pc_set_nr)
def CD(self, thickness, alpha, ae_set_nr=1, pc_set_nr=1):
def CD(self, thickness, alpha, pc_set_nr=1):
"""Drag coefficient
Parameters
......@@ -120,18 +112,26 @@ class PCFile(object):
radius [m]
alpha : float
Angle of attack [deg]
ae_set_nr : int optional
Aerdynamic set number, default is 1
pc_set_nr : int optional
pc set number, default is 1, normally obtained from ae-file
Returns
-------
Drag coefficient : float
"""
return self._Cxxx(thickness, alpha, 2, ae_set_nr, pc_set_nr)
return self._Cxxx(thickness, alpha, 2, pc_set_nr)
def CM(self, thickness, alpha, ae_set_nr=1, pc_set_nr=1):
return self._Cxxx(thickness, alpha, 3, ae_set_nr,pc_set_nr)
def CM(self, thickness, alpha, pc_set_nr=1):
return self._Cxxx(thickness, alpha, 3, pc_set_nr)
if __name__ == "__main__":
pc = PCFile(r"C:\mmpe\Projects\inflow\Hawc2aero_setup/data/Hawc_pc.b52", r"C:\mmpe\Projects\inflow\Hawc2aero_setup/data/S36_ae_h2.001")
print (pc)
pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt")
aefile = AEFile("tests/test_files/NREL_5MW_ae.txt")
print (aefile.thickness(36))
print (pcfile.CL(21,10)) # CL for thickness 21% and AOA=10deg
#1.358
print (pcfile.CD(21,10)) # CD for thickness 21% and AOA=10deg
#0.0255
print (pcfile.CM(21,10)) # CM for thickness 21% and AOA=10deg
#-0.1103
......@@ -96,26 +96,61 @@ def cache_function(f):
class cache_method():
def __init__(self, N):
self.N = N
self.cache_dict = OrderedDict()
def __call__(self, f):
def wrapped(*args):
def wrapped(caller_obj, *args):
name = "_" + f.__name__
arg_id = ";".join([str(a) for a in args])
if arg_id not in self.cache_dict:
self.cache_dict[arg_id] = f(*args)
if len(self.cache_dict)>self.N:
self.cache_dict.popitem(last=False)
return self.cache_dict[arg_id]
if not hasattr(caller_obj,'%s_cache_dict'%name):
setattr(caller_obj,'%s_cache_dict'%name, OrderedDict())
cache_dict = getattr(caller_obj,'%s_cache_dict'%name)
if arg_id not in cache_dict:
cache_dict[arg_id] = f(caller_obj, *args)
if len(cache_dict)>self.N:
cache_dict.popitem(last=False)
return cache_dict[arg_id]
return wrapped
def cache_binary(f):
def cache_npsave(f):
def wrap(filename,*args,**kwargs):
np_filename = os.path.splitext(filename)[0] + ".npy"
def loadsave():
res = f(filename,*args,**kwargs)
np.save(np_filename,res)
return res
if os.path.isfile(np_filename) and (not os.path.isfile(filename) or os.path.getmtime(np_filename) > os.path.getmtime(filename)):
return np.load(np_filename)
try:
return np.load(np_filename)
except:
return loadsave()
else:
return loadsave()
return wrap
def _get_npsavez_wrap(f, compress):
def wrap(filename,*args,**kwargs):
np_filename = os.path.splitext(filename)[0] + ".npy%s.npz"%("",".c")[compress]
def loadsave():
res = f(filename,*args,**kwargs)
np.save(np_filename,res)
if compress:
np.savez_compressed(np_filename,*res)
else:
np.savez(np_filename,*res)
return res
return wrap
\ No newline at end of file
if os.path.isfile(np_filename) and (not os.path.isfile(filename) or os.path.getmtime(np_filename) > os.path.getmtime(filename)):
try:
npzfile = np.load(np_filename)
return [npzfile['arr_%d'%i] for i in range(len(npzfile.files))]
except:
return loadsave()
else:
return loadsave()
return wrap
def cache_npsavez(f):
return _get_npsavez_wrap(f,False)
def cache_npsavez_compressed(f):
return _get_npsavez_wrap(f, True)
\ No newline at end of file
......@@ -18,7 +18,7 @@ import numpy as np
from wetb.utils.timing import get_time
from wetb.utils.caching import cache_function, set_cache_property, cache_method,\
cache_binary
cache_npsavez, cache_npsave, cache_npsavez_compressed
tfp = os.path.dirname(__file__) + "/test_files/"
class Example(object):
......@@ -56,10 +56,18 @@ class Example(object):
time.sleep(1)
return x*2
@cache_binary
@cache_npsave
def open_csv(filename):
return np.loadtxt(filename)
@cache_npsavez
def open_csv2(filename):
return np.loadtxt(filename), np.loadtxt(filename)
@cache_npsavez_compressed
def open_csv3(filename):
return np.loadtxt(filename), np.loadtxt(filename)
def f(x):
return x ** 2
......@@ -115,7 +123,7 @@ class TestCacheProperty(unittest.TestCase):
e.test_cache_property
self.assertAlmostEqual(time.time()-t, 0, places=1)
def test_cache_binary(self):
def test_cache_save(self):
if os.path.isfile(tfp+"test.npy"):
os.remove(tfp+'test.npy')
A = open_csv(tfp + "test.csv")
......@@ -126,7 +134,35 @@ class TestCacheProperty(unittest.TestCase):
B = open_csv(tfp + "test.csv")
np.testing.assert_array_equal(A,B)
os.remove(tfp+'test.npy')
def test_cache_savez(self):
npfilename = tfp+"test.npy.npz"
func = open_csv2
if os.path.isfile(npfilename):
os.remove(npfilename)
A = func(tfp + "test.csv")
self.assertTrue(os.path.isfile(npfilename))
np.testing.assert_array_equal(A[0],np.loadtxt(tfp + "test.csv"))
A[0][0]=-1
np.savez(npfilename,A[0],A[1])
B = func(tfp + "test.csv")
np.testing.assert_array_equal(A,B)
os.remove(npfilename)
def test_cache_savez_compressed(self):
npfilename = tfp+"test.npy.c.npz"
func = open_csv3
if os.path.isfile(npfilename):
os.remove(npfilename)
A = func(tfp + "test.csv")
self.assertTrue(os.path.isfile(npfilename))
np.testing.assert_array_equal(A[0],np.loadtxt(tfp + "test.csv"))
A[0][0]=-1
np.savez(npfilename,A[0],A[1])
B = func(tfp + "test.csv")
np.testing.assert_array_equal(A,B)
os.remove(npfilename)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment