Skip to content
Snippets Groups Projects
Commit 8403beb0 authored by David Verelst's avatar David Verelst
Browse files

Merge branch 'master' of gitlab.windenergy.dtu.dk:toolbox/WindEnergyToolbox

parents 3fad045e 6c9d8bda
No related branches found
No related tags found
No related merge requests found
......@@ -26,7 +26,7 @@ class TestDLCHighLevel(unittest.TestCase):
self.assertEqual(os.path.realpath(self.dlc_hl.res_path), os.path.realpath(testfilepath + "res"))
def test_sensor_info(self):
self.assertEqual(list(self.dlc_hl.sensor_info().name), ['MxTB', 'MyTB', 'MxBR', 'PyBT', 'Pitch', 'PitchBearing', 'Tip1TowerDistance', 'TipTowerDistance'])
self.assertEqual(list(self.dlc_hl.sensor_info().name), ['MxTB', 'MyTB', 'MxBR', 'PyBT', 'Power', 'Pitch', 'PitchBearing', 'Tip1TowerDistance', 'TipTowerDistance'])
def test_sensor_info_filter(self):
self.assertEqual(list(self.dlc_hl.sensor_info(['fatigue']).m), [4, 4, 10])
......
......@@ -15,6 +15,8 @@ standard_library.install_aliases()
import numpy as np
class AtTimeFile(object):
"""Loads an at time file generated by HAWC2
Note that the radius in this context is the curved radius
>>> atfile = AtTimeFile("at_time.dat") # load file
>>> atfile.attribute_names # Attribute names
......@@ -23,27 +25,34 @@ class AtTimeFile(object):
[ 0. -0.775186 -2.91652 ]
>>> atfile.twist()[:3]) # first 3 twist rows
[ 0. -0.775186 -2.91652 ]
>>> atfile.twist(10) # Twist at radius = 10 (interpolated)
>>> atfile.twist(curved_length=10) # Twist at curved_length = 10 (interpolated)
-5.34743208242399
>>> atfile.twist(radius=10) # Twist at curved_length = 10 (interpolated)
-5.34743208242399
"""
def __init__(self, filename):
def __init__(self, filename, blade_radius=None):
self.blade_radius = blade_radius
with open(filename, encoding='utf-8') as fid:
lines = fid.readlines()
self.attribute_names = lines[2].lower().replace("#", "").split()
data = np.array([[float(l) for l in lines[i].split() ] for i in range(3, len(lines))])
self.data = data
def func_factory(column):
def values(radius=None):
if radius is None:
def values(radius=None, curved_length=None):
assert radius is None or curved_length is None, "Specify either radius or curved_length"
if radius is None and curved_length is None:
return self.data[:, column]
elif radius is not None:
assert self.blade_radius is not None, "blade_radius must be specified in __init__ when requesting value of radius (alternatively you can request for curved_length)"
return np.interp(radius/self.blade_radius, self.data[:, 0]/self.data[-1, 0], self.data[:, column])
else:
return np.interp(radius, self.data[:, 0], self.data[:, column])
return np.interp(curved_length, self.data[:, 0], self.data[:, column])
return values
for column, att_name in enumerate(self.attribute_names):
setattr(self, att_name, func_factory(column))
def curved_radius(self, radius=None):
"""Radius of calculation point(s)
def ac_radius(self, radius=None):
"""Radius (curved distance) of aerodynamic calculation point(s)
Parameters
----------
......
......@@ -43,7 +43,7 @@ class HTCFile(HTCContents, HTCDefaults):
modelpath = "../"
initial_comments = None
_contents = None
def __init__(self, filename=None, modelpath="../"):
def __init__(self, filename=None, modelpath=None):
"""
Parameters
---------
......@@ -54,15 +54,28 @@ class HTCFile(HTCContents, HTCDefaults):
"""
if filename is not None:
self.modelpath = os.path.realpath(os.path.join(os.path.dirname(filename), modelpath))
self.filename = filename
else:
self.modelpath = modelpath
self.filename = filename
self.modelpath = modelpath or self.auto_detect_modelpath()
if filename and not os.path.isabs(self.modelpath):
self.modelpath = os.path.realpath(os.path.join(os.path.dirname(self.filename), self.modelpath))
#assert 'simulation' in self.contents, "%s could not be loaded. 'simulation' section missing" % filename
def auto_detect_modelpath(self):
if self.filename is None:
return "../"
#print (["../"*i for i in range(3)])
import numpy as np
found = ([np.sum([os.path.isfile(os.path.join(os.path.dirname(self.filename), "../"*i, f)) for f in self.input_files() if not os.path.isabs(f)]) for i in range(4)])
#for f in self.input_files():
# print (os.path.isfile(os.path.join(os.path.dirname(self.filename), "../",f)), f)
if max(found)>0:
return "../"* np.argmax(found)
else:
raise ValueError("Modelpath cannot be autodetected for '%s'.\nInput files not found near htc file"%self.filename)
def _load(self):
self.reset()
self.initial_comments = []
......@@ -174,7 +187,7 @@ class HTCFile(HTCContents, HTCDefaults):
def input_files(self):
self.contents # load if not loaded
files = self.htc_inputfiles
files = [os.path.abspath(f).replace("\\","/") for f in self.htc_inputfiles]
if 'new_htc_structure' in self:
for mb in [self.new_htc_structure[mb] for mb in self.new_htc_structure.keys() if mb.startswith('main_body')]:
if "timoschenko_input" in mb:
......
......@@ -261,8 +261,9 @@ class PBSClusterSimulationHost(SimulationHost):
hawc2exe = property(lambda self : os.path.basename(self.sim.hawc2exe))
def glob(self, *args,**kwargs):
return self.ssh.glob(*args,**kwargs)
def glob(self, filepattern, cwd="", recursive=False):
return self.ssh.glob(filepattern, cwd, recursive)
def get_datetime(self):
v, out, err = self.ssh.execute('date "+%Y,%m,%d,%H,%M,%S"')
if v == 0:
......
......@@ -24,11 +24,12 @@ class TestAtTimeFile(unittest.TestCase):
def test_doc_examples(self):
atfile = AtTimeFile(self.testfilepath + "at_time.dat") # load file
atfile = AtTimeFile(self.testfilepath + "at_time.dat", blade_radius=20.501) # load file
self.assertEqual(atfile.attribute_names, ['radius_s', 'twist', 'chord'])
np.testing.assert_array_equal(atfile[:3, 1], [ 0., -0.775186, -2.91652 ])
np.testing.assert_array_equal(atfile.twist()[:3], [ 0. , -0.775186 , -2.91652 ])
self.assertEqual(atfile.twist(10), -5.34743208242399) # Twist at radius = 10 (interpolated)
self.assertAlmostEqual(atfile.twist(radius=10), -5.34743208242399) # Twist at radius = 10 (interpolated)
self.assertEqual(atfile.twist(curved_length=10), -5.34743208242399) # Twist at radius = 10 (interpolated)
......@@ -40,18 +41,24 @@ class TestAtTimeFile(unittest.TestCase):
self.assertEqual(atfile.chord()[9], 1.54999)
def test_at_time_file_at_radius(self):
def test_at_time_file_at_curved_radius(self):
atfile = AtTimeFile(self.testfilepath + "at_time.dat")
self.assertEqual(atfile.radius_s(9), 9)
self.assertEqual(atfile.twist(9), -6.635983309665461)
self.assertEqual(atfile.chord(9), 1.3888996578373045)
self.assertEqual(atfile.radius_s(curved_length=9), 9)
self.assertEqual(atfile.twist(curved_length=9), -6.635983309665461)
self.assertEqual(atfile.chord(curved_length=9), 1.3888996578373045)
def test_at_time_file_at_radius(self):
atfile = AtTimeFile(self.testfilepath + "at_time.dat", blade_radius=20.501/2)
self.assertEqual(atfile.radius_s(radius=9/2), 9)
self.assertEqual(atfile.twist(radius=9/2), -6.635983309665461)
self.assertEqual(atfile.chord(radius=9/2), 1.3888996578373045)
def test_at_time_file_radius(self):
atfile = AtTimeFile(self.testfilepath + "at_time.dat")
self.assertEqual(atfile.curved_radius()[12], 10.2505)
self.assertEqual(atfile.curved_radius(10), 10.2505)
self.assertEqual(atfile.curved_radius(10.5), 10.2505)
self.assertEqual(atfile.ac_radius()[12], 10.2505)
self.assertEqual(atfile.ac_radius(10), 10.2505)
self.assertEqual(atfile.ac_radius(10.5), 10.2505)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
......
This diff is collapsed.
......@@ -32,16 +32,16 @@ class TestHtcFile(unittest.TestCase):
def check_htc_file(self, f):
with open(f) as fid:
orglines = fid.readlines()
htcfile = HTCFile(f)
htcfile = HTCFile(f,"../")
newlines = str(htcfile).split("\n")
htcfile.save(self.testfilepath + 'tmp.htc')
#with open(self.testfilepath + 'tmp.htc') as fid:
# newlines = fid.readlines()
for i, (org, new) in enumerate(zip(orglines, newlines), 1):
fmt = lambda x : x.strip().replace("\t", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ")
if fmt(org) != fmt(new):
......@@ -52,14 +52,13 @@ class TestHtcFile(unittest.TestCase):
break
print ()
assert len(orglines) == len(newlines)
def test_htc_files(self):
for f in ['test3.htc']:
self.check_htc_file(self.testfilepath + f)
def test_htc_file_get(self):
htcfile = HTCFile(self.testfilepath + "test3.htc")
htcfile = HTCFile(self.testfilepath + "test3.htc",'../')
self.assertEqual(htcfile['simulation']['time_stop'][0], 200)
self.assertEqual(htcfile['simulation/time_stop'][0], 200)
self.assertEqual(htcfile['simulation.time_stop'][0], 200)
......@@ -68,7 +67,7 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.dll.type2_dll__2.name[0], "risoe_controller2")
s = """begin simulation;\n time_stop\t200;"""
self.assertEqual(str(htcfile.simulation)[:len(s)], s)
def test_htc_file_get2(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
self.assertEqual(htcfile['simulation']['logfile'][0], './logfiles/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004.log')
......@@ -76,14 +75,14 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile['simulation.logfile'][0], './logfiles/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004.log')
self.assertEqual(htcfile.simulation.logfile[0], './logfiles/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004.log')
self.assertEqual(htcfile.simulation.newmark.deltat[0], 0.02)
def test_htc_file_set(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
time_stop = htcfile.simulation.time_stop[0]
htcfile.simulation.time_stop = time_stop * 2
self.assertEqual(htcfile.simulation.time_stop[0], 2 * time_stop)
self.assertEqual(htcfile.simulation.time_stop.__class__, HTCLine)
htcfile.output.time = 10, 20
self.assertEqual(htcfile.output.time[:2], [10, 20])
self.assertEqual(str(htcfile.output.time), "time\t10 20;\n")
......@@ -91,12 +90,12 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.output.time[:2], [11, 21])
htcfile.output.time = "12 22"
self.assertEqual(htcfile.output.time[:2], [12, 22])
def test_htc_file_set_key(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
htcfile.simulation.name = "value"
self.assertEqual(htcfile.simulation.name[0], "value")
def test_htc_file_del_key(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
del htcfile.simulation.logfile
......@@ -105,16 +104,16 @@ class TestHtcFile(unittest.TestCase):
del htcfile.hydro.water_properties.water_kinematics_dll
except KeyError:
pass
def test_htcfile_setname(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
htcfile.set_name("mytest", htc_folder="htcfiles")
self.assertEqual(os.path.relpath(htcfile.filename, self.testfilepath), r'mytest.htc')
self.assertEqual(htcfile.simulation.logfile[0], './log/mytest.log')
self.assertEqual(htcfile.output.filename[0], './res/mytest')
def test_set_time(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
htcfile.set_time(10, 20, 0.2)
......@@ -122,9 +121,9 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.simulation.newmark.deltat[0], 0.2)
self.assertEqual(htcfile.wind.scale_time_start[0], 10)
self.assertEqual(htcfile.output.time[:2], [10, 20])
def test_add_section(self):
htcfile = HTCFile()
htcfile.wind.add_section('mann')
......@@ -132,7 +131,7 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.wind.mann.create_turb_parameters[0], 29.4)
self.assertEqual(htcfile.wind.mann.create_turb_parameters[3], 1004)
self.assertEqual(htcfile.wind.mann.create_turb_parameters.comments, "L, alfaeps, gamma, seed, highfrq compensation")
def test_add_mann(self):
htcfile = HTCFile()
htcfile.add_mann_turbulence(30.1, 1.1, 3.3, 102, False)
......@@ -149,8 +148,8 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(a.strip(), b.strip())
self.assertEqual(htcfile.wind.turb_format[0], 1)
self.assertEqual(htcfile.wind.turb_format.comments, "")
def test_sensors(self):
htcfile = HTCFile()
htcfile.set_name("test")
......@@ -162,13 +161,13 @@ class TestHtcFile(unittest.TestCase):
for a, b in zip(s.split("\n"), str(htcfile.output).split("\n")):
self.assertEqual(a.strip(), b.strip())
#print (htcfile)
def test_output_at_time(self):
htcfile = HTCFile(self.testfilepath + "test2.htc")
htcfile = HTCFile(self.testfilepath + "test2.htc",'../')
self.assertTrue('begin output_at_time aero 15.0;' in str(htcfile))
def test_output_files(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
output_files = htcfile.output_files()
......@@ -192,11 +191,11 @@ class TestHtcFile(unittest.TestCase):
except ValueError:
raise ValueError(f + " is not in list")
self.assertFalse(output_files)
def test_turbulence_files(self):
htcfile = HTCFile(self.testfilepath + "dlc14_wsp10_wdir000_s0000.htc")
htcfile = HTCFile(self.testfilepath + "dlc14_wsp10_wdir000_s0000.htc",'../')
self.assertEqual(htcfile.turbulence_files(), ['./turb/turb_wsp10_s0000u.bin', './turb/turb_wsp10_s0000v.bin', './turb/turb_wsp10_s0000w.bin'])
def test_input_files(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
input_files = htcfile.input_files()
......@@ -213,20 +212,20 @@ class TestHtcFile(unittest.TestCase):
'./control/mech_brake.dll',
'./control/servo_with_limits.dll',
'./control/towclearsens.dll',
self.testfilepath + 'test.htc'
self.testfilepath.replace("\\","/") + 'test.htc'
]:
try:
input_files.remove(f)
except ValueError:
raise ValueError(f + " is not in list")
self.assertFalse(input_files)
def test_input_files2(self):
htcfile = HTCFile(self.testfilepath + "ansi.htc")
htcfile = HTCFile(self.testfilepath + "ansi.htc",'../')
input_files = htcfile.input_files()
self.assertTrue('./htc_hydro/ireg_airy_h6_t10.inp' in input_files)
#
def test_continue_in_files(self):
htcfile = HTCFile(self.testfilepath + "continue_in_file.htc", ".")
self.assertIn('main_body__31', htcfile.new_htc_structure.keys())
......@@ -234,22 +233,32 @@ class TestHtcFile(unittest.TestCase):
self.assertIn('./data/NREL_5MW_st1.txt', htcfile.input_files())
self.assertEqual(str(htcfile).count("exit"), 1)
self.assertIn('filename\t./res/oc4_p2_load_case_eq;', str(htcfile))
def test_tjul_example(self):
htcfile = HTCFile(self.testfilepath + "./tjul.htc", ".")
htcfile.save("./temp.htc")
def test_ansi(self):
htcfile = HTCFile(self.testfilepath + "./ansi.htc")
htcfile = HTCFile(self.testfilepath + "./ansi.htc",'../')
def test_file_with_BOM(self):
htcfile = HTCFile(self.testfilepath + 'DLC15_wsp11_wdir000_s0000_phi000_Free_v2_visual.htc')
self.assertEqual(str(htcfile)[0], ";")
def test_htc_reset(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
self.assertEqual(htcfile.wind.wsp[0], 10)
def test_htc_model_autodetect(self):
htcfile = HTCFile(self.testfilepath + "test.htc")
self.assertEqual(os.path.relpath(htcfile.modelpath,os.path.dirname(htcfile.filename)), "..")
htcfile = HTCFile(self.testfilepath + "sub/test.htc")
self.assertEqual(os.path.relpath(htcfile.modelpath,os.path.dirname(htcfile.filename)).replace("\\","/"), "../..")
self.assertRaisesRegex(ValueError, "Modelpath cannot be autodetected", HTCFile, self.testfilepath + "test2.htc")
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
......
......@@ -40,7 +40,7 @@ class TestLogFile(unittest.TestCase):
self.tfp = os.path.join(os.path.dirname(__file__), 'test_files/') # test file path
def test_from_htcfile(self):
htcfile = HTCFile(self.tfp + 'logfiles/model/htc/dlc14_iec61400-1ed3/dlc14_wsp10_wdir000_s0000.htc')
htcfile = HTCFile(self.tfp + 'logfiles/model/htc/dlc14_iec61400-1ed3/dlc14_wsp10_wdir000_s0000.htc',"../")
logfile = LogFile.from_htcfile(htcfile, self.tfp + 'logfiles/model/')
self.assertEqual(logfile.status, DONE)
......@@ -210,7 +210,7 @@ class TestLogFile(unittest.TestCase):
def test_reset(self):
htcfile = HTCFile(self.tfp + 'logfiles/model/htc/dlc14_iec61400-1ed3/dlc14_wsp10_wdir000_s0000.htc')
htcfile = HTCFile(self.tfp + 'logfiles/model/htc/dlc14_iec61400-1ed3/dlc14_wsp10_wdir000_s0000.htc',"../")
logfile = LogFile.from_htcfile(htcfile, self.tfp + 'logfiles/model/')
self.assertEqual(logfile.status, DONE)
logfile.reset()
......
......@@ -112,6 +112,9 @@ class SSHPBSClusterResource(Resource):
Resource.__init__(self, min_cpu, min_free)
self.ssh = sshclient
self.resource_lock = threading.Lock()
def glob(self, filepattern, cwd="", recursive=False):
return self.ssh.glob(filepattern, cwd, recursive)
@property
def host(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment