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

auto_detect_modelpath implemented in HTCFile

parent e6e099a8
No related branches found
No related tags found
No related merge requests found
Pipeline #
...@@ -43,7 +43,7 @@ class HTCFile(HTCContents, HTCDefaults): ...@@ -43,7 +43,7 @@ class HTCFile(HTCContents, HTCDefaults):
modelpath = "../" modelpath = "../"
initial_comments = None initial_comments = None
_contents = None _contents = None
def __init__(self, filename=None, modelpath="../"): def __init__(self, filename=None, modelpath=None):
""" """
Parameters Parameters
--------- ---------
...@@ -54,15 +54,28 @@ class HTCFile(HTCContents, HTCDefaults): ...@@ -54,15 +54,28 @@ class HTCFile(HTCContents, HTCDefaults):
""" """
if filename is not None: if filename is not None:
self.modelpath = os.path.realpath(os.path.join(os.path.dirname(filename), modelpath)) self.filename = filename
self.filename = filename self.modelpath = modelpath or self.auto_detect_modelpath()
else: if filename and not os.path.isabs(self.modelpath):
self.modelpath = 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 #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): def _load(self):
self.reset() self.reset()
self.initial_comments = [] self.initial_comments = []
...@@ -174,7 +187,7 @@ class HTCFile(HTCContents, HTCDefaults): ...@@ -174,7 +187,7 @@ class HTCFile(HTCContents, HTCDefaults):
def input_files(self): def input_files(self):
self.contents # load if not loaded 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: 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')]: 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: if "timoschenko_input" in mb:
......
This diff is collapsed.
...@@ -32,16 +32,16 @@ class TestHtcFile(unittest.TestCase): ...@@ -32,16 +32,16 @@ class TestHtcFile(unittest.TestCase):
def check_htc_file(self, f): def check_htc_file(self, f):
with open(f) as fid: with open(f) as fid:
orglines = fid.readlines() orglines = fid.readlines()
htcfile = HTCFile(f) htcfile = HTCFile(f,"../")
newlines = str(htcfile).split("\n") newlines = str(htcfile).split("\n")
htcfile.save(self.testfilepath + 'tmp.htc') htcfile.save(self.testfilepath + 'tmp.htc')
#with open(self.testfilepath + 'tmp.htc') as fid: #with open(self.testfilepath + 'tmp.htc') as fid:
# newlines = fid.readlines() # newlines = fid.readlines()
for i, (org, new) in enumerate(zip(orglines, newlines), 1): for i, (org, new) in enumerate(zip(orglines, newlines), 1):
fmt = lambda x : x.strip().replace("\t", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ") fmt = lambda x : x.strip().replace("\t", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ").replace(" ", " ")
if fmt(org) != fmt(new): if fmt(org) != fmt(new):
...@@ -52,14 +52,13 @@ class TestHtcFile(unittest.TestCase): ...@@ -52,14 +52,13 @@ class TestHtcFile(unittest.TestCase):
break break
print () print ()
assert len(orglines) == len(newlines) assert len(orglines) == len(newlines)
def test_htc_files(self): def test_htc_files(self):
for f in ['test3.htc']: for f in ['test3.htc']:
self.check_htc_file(self.testfilepath + f) self.check_htc_file(self.testfilepath + f)
def test_htc_file_get(self): 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) 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): ...@@ -68,7 +67,7 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.dll.type2_dll__2.name[0], "risoe_controller2") self.assertEqual(htcfile.dll.type2_dll__2.name[0], "risoe_controller2")
s = """begin simulation;\n time_stop\t200;""" s = """begin simulation;\n time_stop\t200;"""
self.assertEqual(str(htcfile.simulation)[:len(s)], s) self.assertEqual(str(htcfile.simulation)[:len(s)], s)
def test_htc_file_get2(self): def test_htc_file_get2(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
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')
...@@ -76,14 +75,14 @@ class TestHtcFile(unittest.TestCase): ...@@ -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.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) self.assertEqual(htcfile.simulation.newmark.deltat[0], 0.02)
def test_htc_file_set(self): def test_htc_file_set(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
time_stop = htcfile.simulation.time_stop[0] time_stop = htcfile.simulation.time_stop[0]
htcfile.simulation.time_stop = time_stop * 2 htcfile.simulation.time_stop = time_stop * 2
self.assertEqual(htcfile.simulation.time_stop[0], 2 * time_stop) self.assertEqual(htcfile.simulation.time_stop[0], 2 * time_stop)
self.assertEqual(htcfile.simulation.time_stop.__class__, HTCLine) self.assertEqual(htcfile.simulation.time_stop.__class__, HTCLine)
htcfile.output.time = 10, 20 htcfile.output.time = 10, 20
self.assertEqual(htcfile.output.time[:2], [10, 20]) self.assertEqual(htcfile.output.time[:2], [10, 20])
self.assertEqual(str(htcfile.output.time), "time\t10 20;\n") self.assertEqual(str(htcfile.output.time), "time\t10 20;\n")
...@@ -91,12 +90,12 @@ class TestHtcFile(unittest.TestCase): ...@@ -91,12 +90,12 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.output.time[:2], [11, 21]) self.assertEqual(htcfile.output.time[:2], [11, 21])
htcfile.output.time = "12 22" htcfile.output.time = "12 22"
self.assertEqual(htcfile.output.time[:2], [12, 22]) self.assertEqual(htcfile.output.time[:2], [12, 22])
def test_htc_file_set_key(self): def test_htc_file_set_key(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
htcfile.simulation.name = "value" htcfile.simulation.name = "value"
self.assertEqual(htcfile.simulation.name[0], "value") self.assertEqual(htcfile.simulation.name[0], "value")
def test_htc_file_del_key(self): def test_htc_file_del_key(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
del htcfile.simulation.logfile del htcfile.simulation.logfile
...@@ -105,16 +104,16 @@ class TestHtcFile(unittest.TestCase): ...@@ -105,16 +104,16 @@ class TestHtcFile(unittest.TestCase):
del htcfile.hydro.water_properties.water_kinematics_dll del htcfile.hydro.water_properties.water_kinematics_dll
except KeyError: except KeyError:
pass pass
def test_htcfile_setname(self): def test_htcfile_setname(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
htcfile.set_name("mytest", htc_folder="htcfiles") htcfile.set_name("mytest", htc_folder="htcfiles")
self.assertEqual(os.path.relpath(htcfile.filename, self.testfilepath), r'mytest.htc') self.assertEqual(os.path.relpath(htcfile.filename, self.testfilepath), r'mytest.htc')
self.assertEqual(htcfile.simulation.logfile[0], './log/mytest.log') self.assertEqual(htcfile.simulation.logfile[0], './log/mytest.log')
self.assertEqual(htcfile.output.filename[0], './res/mytest') self.assertEqual(htcfile.output.filename[0], './res/mytest')
def test_set_time(self): def test_set_time(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
htcfile.set_time(10, 20, 0.2) htcfile.set_time(10, 20, 0.2)
...@@ -122,9 +121,9 @@ class TestHtcFile(unittest.TestCase): ...@@ -122,9 +121,9 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(htcfile.simulation.newmark.deltat[0], 0.2) self.assertEqual(htcfile.simulation.newmark.deltat[0], 0.2)
self.assertEqual(htcfile.wind.scale_time_start[0], 10) self.assertEqual(htcfile.wind.scale_time_start[0], 10)
self.assertEqual(htcfile.output.time[:2], [10, 20]) self.assertEqual(htcfile.output.time[:2], [10, 20])
def test_add_section(self): def test_add_section(self):
htcfile = HTCFile() htcfile = HTCFile()
htcfile.wind.add_section('mann') htcfile.wind.add_section('mann')
...@@ -132,7 +131,7 @@ class TestHtcFile(unittest.TestCase): ...@@ -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[0], 29.4)
self.assertEqual(htcfile.wind.mann.create_turb_parameters[3], 1004) 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") self.assertEqual(htcfile.wind.mann.create_turb_parameters.comments, "L, alfaeps, gamma, seed, highfrq compensation")
def test_add_mann(self): def test_add_mann(self):
htcfile = HTCFile() htcfile = HTCFile()
htcfile.add_mann_turbulence(30.1, 1.1, 3.3, 102, False) htcfile.add_mann_turbulence(30.1, 1.1, 3.3, 102, False)
...@@ -149,8 +148,8 @@ class TestHtcFile(unittest.TestCase): ...@@ -149,8 +148,8 @@ class TestHtcFile(unittest.TestCase):
self.assertEqual(a.strip(), b.strip()) self.assertEqual(a.strip(), b.strip())
self.assertEqual(htcfile.wind.turb_format[0], 1) self.assertEqual(htcfile.wind.turb_format[0], 1)
self.assertEqual(htcfile.wind.turb_format.comments, "") self.assertEqual(htcfile.wind.turb_format.comments, "")
def test_sensors(self): def test_sensors(self):
htcfile = HTCFile() htcfile = HTCFile()
htcfile.set_name("test") htcfile.set_name("test")
...@@ -162,13 +161,13 @@ class TestHtcFile(unittest.TestCase): ...@@ -162,13 +161,13 @@ class TestHtcFile(unittest.TestCase):
for a, b in zip(s.split("\n"), str(htcfile.output).split("\n")): for a, b in zip(s.split("\n"), str(htcfile.output).split("\n")):
self.assertEqual(a.strip(), b.strip()) self.assertEqual(a.strip(), b.strip())
#print (htcfile) #print (htcfile)
def test_output_at_time(self): 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)) self.assertTrue('begin output_at_time aero 15.0;' in str(htcfile))
def test_output_files(self): def test_output_files(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
output_files = htcfile.output_files() output_files = htcfile.output_files()
...@@ -192,11 +191,11 @@ class TestHtcFile(unittest.TestCase): ...@@ -192,11 +191,11 @@ class TestHtcFile(unittest.TestCase):
except ValueError: except ValueError:
raise ValueError(f + " is not in list") raise ValueError(f + " is not in list")
self.assertFalse(output_files) self.assertFalse(output_files)
def test_turbulence_files(self): 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']) 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): def test_input_files(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
input_files = htcfile.input_files() input_files = htcfile.input_files()
...@@ -213,20 +212,20 @@ class TestHtcFile(unittest.TestCase): ...@@ -213,20 +212,20 @@ class TestHtcFile(unittest.TestCase):
'./control/mech_brake.dll', './control/mech_brake.dll',
'./control/servo_with_limits.dll', './control/servo_with_limits.dll',
'./control/towclearsens.dll', './control/towclearsens.dll',
self.testfilepath + 'test.htc' self.testfilepath.replace("\\","/") + 'test.htc'
]: ]:
try: try:
input_files.remove(f) input_files.remove(f)
except ValueError: except ValueError:
raise ValueError(f + " is not in list") raise ValueError(f + " is not in list")
self.assertFalse(input_files) self.assertFalse(input_files)
def test_input_files2(self): def test_input_files2(self):
htcfile = HTCFile(self.testfilepath + "ansi.htc") htcfile = HTCFile(self.testfilepath + "ansi.htc",'../')
input_files = htcfile.input_files() input_files = htcfile.input_files()
self.assertTrue('./htc_hydro/ireg_airy_h6_t10.inp' in input_files) self.assertTrue('./htc_hydro/ireg_airy_h6_t10.inp' in input_files)
# #
def test_continue_in_files(self): def test_continue_in_files(self):
htcfile = HTCFile(self.testfilepath + "continue_in_file.htc", ".") htcfile = HTCFile(self.testfilepath + "continue_in_file.htc", ".")
self.assertIn('main_body__31', htcfile.new_htc_structure.keys()) self.assertIn('main_body__31', htcfile.new_htc_structure.keys())
...@@ -234,22 +233,32 @@ class TestHtcFile(unittest.TestCase): ...@@ -234,22 +233,32 @@ class TestHtcFile(unittest.TestCase):
self.assertIn('./data/NREL_5MW_st1.txt', htcfile.input_files()) self.assertIn('./data/NREL_5MW_st1.txt', htcfile.input_files())
self.assertEqual(str(htcfile).count("exit"), 1) self.assertEqual(str(htcfile).count("exit"), 1)
self.assertIn('filename\t./res/oc4_p2_load_case_eq;', str(htcfile)) self.assertIn('filename\t./res/oc4_p2_load_case_eq;', str(htcfile))
def test_tjul_example(self): def test_tjul_example(self):
htcfile = HTCFile(self.testfilepath + "./tjul.htc", ".") htcfile = HTCFile(self.testfilepath + "./tjul.htc", ".")
htcfile.save("./temp.htc") htcfile.save("./temp.htc")
def test_ansi(self): def test_ansi(self):
htcfile = HTCFile(self.testfilepath + "./ansi.htc") htcfile = HTCFile(self.testfilepath + "./ansi.htc",'../')
def test_file_with_BOM(self): def test_file_with_BOM(self):
htcfile = HTCFile(self.testfilepath + 'DLC15_wsp11_wdir000_s0000_phi000_Free_v2_visual.htc') htcfile = HTCFile(self.testfilepath + 'DLC15_wsp11_wdir000_s0000_phi000_Free_v2_visual.htc')
self.assertEqual(str(htcfile)[0], ";") self.assertEqual(str(htcfile)[0], ";")
def test_htc_reset(self): def test_htc_reset(self):
htcfile = HTCFile(self.testfilepath + "test.htc") htcfile = HTCFile(self.testfilepath + "test.htc")
self.assertEqual(htcfile.wind.wsp[0], 10) 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)), "..\\..")
self.assertRaisesRegex(ValueError, "Modelpath cannot be autodetected", HTCFile, self.testfilepath + "test2.htc")
if __name__ == "__main__": if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName'] #import sys;sys.argv = ['', 'Test.testName']
......
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