def check_htc_file(self, f): with open(f) as fid: orglines = fid.read().strip().split("\n") 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): def fmt(x): return x.strip().replace("\t", " ").replace(" ", " ").replace( " ", " ").replace(" ", " ").replace(" ", " ") if fmt(org) != fmt(new): print("----------%d-------------" % i) print(fmt(org)) print(fmt(new)) self.assertEqual(fmt(org), fmt(new)) break print() assert len(orglines) == len(newlines)
def test_set_time(self): htcfile = HTCFile(self.testfilepath + "test.htc") htcfile.set_time(10, 20, 0.2) self.assertEqual(htcfile.simulation.time_stop[0], 20) 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_htc_file_set_key2(self): htcfile = HTCFile(self.testfilepath + "test.htc") htcfile.simulation['name'] = "value" self.assertEqual(htcfile.simulation.name[0], "value") htcfile.simulation['name2'] = ("value", 1) self.assertEqual(htcfile.simulation.name2[0], "value") self.assertEqual(htcfile.simulation.name2[1], 1)
def test_turbulence_files(self): 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 write(self, out_fn, **kwargs): ''' Renders a single htc file for a given set of 'tagless' contents. args: out_fn (str or pathlib.Path): The output filename where to render the jinja template. params (pd.DataFrame or pd.Series) : The input contents. ''' # if isinstance(params, PandasObject): # params = params.to_dict() htc = HTCFile(self.base_htc_file) for k, v in kwargs.items(): k = k.replace('/', '.') if '.' in k: line = htc[k] v = str(v).strip().replace(",", " ") line.values = v.split() elif k in ['Name', 'Folder', 'DLC']: continue else: getattr(self, 'set_%s' % k)(htc, **kwargs) htc.save(out_fn)
def test_output_files(self): htcfile = HTCFile(self.testfilepath + "test.htc") output_files = htcfile.output_files() #print (htcfile.output) for f in [ './logfiles/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004.log', './visualization/dlc12_wsp10_wdir000_s1004.hdf5', './animation/structure_aero_control_turb.dat', './res_eigen/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_wsp10_wdir000_s1004_beam.dat', './res_eigen/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_wsp10_wdir000_s1004_body.dat', './res_eigen/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_wsp10_wdir000_s1004_struct.dat', './res_eigen/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_wsp10_wdir000_s1004_body_eigen.dat', './res_eigen/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_wsp10_wdir000_s1004_strc_eigen.dat', './res_eigen/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004/mode*.dat', './launcher_test/ssystem_eigenanalysis.dat', './launcher_test/mode*.dat', './res/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004.sel', './res/dlc12_iec61400-1ed3/dlc12_wsp10_wdir000_s1004.dat', './res/rotor_check_inipos.dat', './res/rotor_check_inipos2.dat' ]: try: output_files.remove(f) except ValueError: raise ValueError(f + " is not in list") self.assertFalse(output_files)
def write(self, path, **kwargs): """Write a single htc file. Notes ----- This function works both with the tagless and jinja file-writing systems. Any methods of the form `set_<tag>` are called during file writing. Parameters ---------- path : str or pathlib.Path The path to save the htc file to. **kwargs : pd.DataFrame, pd.Series or dict Keyword arguments. The tags to update/replace in the file. """ htc = HTCFile(self.base_htc_file, jinja_tags=kwargs) for k, v in kwargs.items(): k = k.replace('/', '.') if '.' in k: # directly replace tags like "wind.wsp" line = htc[k] v = str(v).strip().replace(",", " ") line.values = v.split() else: # otherwise, use the "set_" attribute if hasattr(self, 'set_%s' % k): getattr(self, 'set_%s' % k)(htc, **kwargs) htc.save(path)
def test_pbs_file_inout(self): htc = HTCFile(self.testfilepath + "../simulation_setup/DTU10MWRef6.0_IOS/input/htc/DTU_10MW_RWT.htc") assert os.path.relpath(htc.modelpath, self.testfilepath) == os.path.relpath( "../simulation_setup/DTU10MWRef6.0_IOS/input") from wetb.hawc2.hawc2_pbs_file import JESS_WINE32_HAWC2MB print(htc.pbs_file(r"R:\HAWC2_tests\v12.6_mmpe3\hawc2\win32", JESS_WINE32_HAWC2MB, input_files=["./input/*"], output_files=['./output/*']))
def test_continue_in_files_autodetect_path(self): htcfile = HTCFile(self.testfilepath + "sub/continue_in_file.htc") self.assertIn('main_body__31', htcfile.new_htc_structure.keys()) self.assertIn(os.path.abspath(self.testfilepath + 'orientation.dat'), [os.path.abspath(f) for f in htcfile.input_files()]) 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).lower())
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 + "missing_input_files.htc")
def test_pbs_file(self): htc = HTCFile(self.testfilepath + "../simulation_setup/DTU10MWRef6.0/htc/DTU_10MW_RWT.htc") assert os.path.relpath(htc.modelpath, self.testfilepath) == os.path.relpath( "../simulation_setup/DTU10MWRef6.0/") from wetb.hawc2.hawc2_pbs_file import JESS_WINE32_HAWC2MB htc.pbs_file(r"R:\HAWC2_tests\v12.6_mmpe3\hawc2\win32", JESS_WINE32_HAWC2MB)
def test_sensors(self): htcfile = HTCFile() htcfile.set_name("test") htcfile.output.add_sensor('wind', 'free_wind', [1, 0, 0, -30]) s = """begin output; filename\t./res/test; general time; wind free_wind\t1 0 0 -30;""" for a, b in zip(s.split("\n"), str(htcfile.output).split("\n")): self.assertEqual(a.strip(), b.strip())
def test_add_section2(self): htcfile = HTCFile() htcfile.add_section('hydro') #self.assertEqual(str(htcfile).strip()[-5:], "exit;") htcfile = HTCFile(self.testfilepath + "test.htc") htcfile.add_section('hydro') self.assertEqual(str(htcfile).strip()[-5:], "exit;")
def test_add_mann(self): htcfile = HTCFile() htcfile.add_mann_turbulence(30.1, 1.1, 3.3, 102, False) s = """begin mann; create_turb_parameters\t30.1 1.1 3.3 102 0;\tL, alfaeps, gamma, seed, highfrq compensation filename_u\t./turb/mann_l30.1_ae1.1000_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102u.turb; filename_v\t./turb/mann_l30.1_ae1.1000_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102v.turb; filename_w\t./turb/mann_l30.1_ae1.1000_g3.3_h0_16384x32x32_0.366x3.12x3.12_s0102w.turb; box_dim_u\t16384 0.3662; box_dim_v\t32 3.125; box_dim_w\t32 3.125;""" for a, b in zip(s.split("\n"), str(htcfile.wind.mann).split("\n")): self.assertEqual(a.strip(), b.strip()) self.assertEqual(htcfile.wind.turb_format[0], 1) self.assertEqual(htcfile.wind.turb_format.comments, "")
def test_htc_model_autodetect_upper_case_files(self): htcfile = HTCFile( self.testfilepath + "../simulation_setup/DTU10MWRef6.0/htc/DTU_10MW_RWT.htc") self.assertEqual( os.path.relpath(htcfile.modelpath, os.path.dirname(htcfile.filename)), "..")
def __init__(self, htcfile=None, ae_filename=None, pc_filename=None, at_time_filename=None, st_filename=None, blade_name=None): if htcfile is not None: if isinstance(htcfile, str): htcfile = HTCFile(htcfile) s = htcfile.new_htc_structure # at_time_filename = at_time_filename or ("output_at_time" in htcfile and os.path.join(htcfile.modelpath, htcfile.output_at_time.filename[0] + ".dat")) # pc_filename = pc_filename or os.path.join(htcfile.modelpath, htcfile.aero.pc_filename[0]) # ae_filename = ae_filename or os.path.join(htcfile.modelpath, htcfile.aero.ae_filename[0]) # mainbodies = [s[k] for k in s.keys() if s[k].name_ == "main_body"] if blade_name is None: blade_name = htcfile.aero.link[2] self.mainbody_blade = htcfile.new_htc_structure.get_subsection_by_name( blade_name) st_filename = st_filename or os.path.join( htcfile.modelpath, self.mainbody_blade.timoschenko_input.filename[0]) MainBody.__init__(self, htcfile, blade_name) elif st_filename and os.path.isfile(st_filename): StFile.__init__(self, st_filename) H2aeroBlade.__init__(self, htcfile, ae_filename=ae_filename, pc_filename=pc_filename, at_time_filename=at_time_filename, blade_name=blade_name)
def test_excel2htc(h2writer): h2writer.from_excel( os.path.dirname(test_files.__file__) + "/htc_input_table.xlsx") h2writer.write_all(path) for i, wsp in enumerate([4, 6], 1): htc = HTCFile(path + "tmp/a%d.htc" % i) assert htc.wind.wsp[0] == wsp
def __init__(self, htcfile=None, ae_filename=None, pc_filename=None, at_time_filename=None, blade_name=None): if htcfile: if isinstance(htcfile, str): assert htcfile.lower().endswith('.htc') htcfile = HTCFile(htcfile) self.htcfile = htcfile blade_name = blade_name or htcfile.aero.link[2] at_time_filename = at_time_filename or ( "output_at_time" in htcfile and os.path.join(htcfile.modelpath, htcfile.output_at_time.filename[0] + ".dat")) pc_filename = pc_filename or os.path.join( htcfile.modelpath, htcfile.aero.pc_filename[0]) ae_filename = ae_filename or os.path.join( htcfile.modelpath, htcfile.aero.ae_filename[0]) self.hawc2_splines_data = self.hawc2_splines() #mainbodies = [s[k] for k in s.keys() if s[k].name_ == "main_body"] #self.mainbody_blade = [mb for mb in mainbodies if mb.name[0] == blade_name][0] if os.path.isfile(pc_filename) and os.path.isfile(ae_filename): AEFile.__init__(self, ae_filename) PCFile.__init__(self, pc_filename) blade_radius = self.ae_sets[1][-1, 0] if at_time_filename and os.path.isfile(at_time_filename): AtTimeFile.__init__(self, at_time_filename, blade_radius) self.curved_length = self.radius_curved_ac()[-1] else: self.curved_length = None
def test_from_htcfile(self): 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)
def generate_pbs_files(self, template_fn, overwrite=True): with open(template_fn) as f: template = f.read() p = { 'walltime': '00:40:00', 'umask': '003', 'lnodes': '1', 'ppn': '1', 'modelzip': 'JL0001.zip', 'jobname': 'ERROR', 'htcdir': 'ERROR', #from htc file 'logdir': 'ERROR', 'resdir': 'ERROR', 'turbdir': 'ERROR', 'turbfileroot': 'ERROR' } #from htc file pbs_in_dir = Config.modelpath + 'pbs_in/' + self.basename htc_dir = Config.modelpath + 'htc/' + self.basename #if not, check if zip files exist. if more than one, return error zipfiles = [x for x in os.listdir(self.modelpath) if '.zip' in x] if len(zipfiles) == 0: raise ModelMissingException( 'No zipped model file found in this folder.') elif len(zipfiles) > 1: raise ModelMissingException( 'Only one zipped model file should be supplied in this folder.' ) #set zip file p['modelzip'] = zipfiles[0] if not os.path.exists(pbs_in_dir): os.makedirs(pbs_in_dir) filenames = [x for x in os.listdir(htc_dir) if x[-4:] == '.htc'] for file in filenames: htc = HTCFile(os.path.join(htc_dir, file), modelpath='../..') p['jobname'] = file.split('.')[0] p['htcdir'] = 'htc/' + basename p['logdir'] = os.path.dirname( htc.simulation.logfile.str_values())[2:] + '/' p['resdir'] = os.path.dirname( htc.output.filename.str_values())[2:] + '/' p['turbdir'] = os.path.dirname( htc.wind.mann.filename_u.str_values()) + '/' p['turbfileroot'] = os.path.basename( htc.wind.mann.filename_u.str_values()).split('u.')[0] p['pbsoutdir'] = 'pbs_out/' + basename template_ = template for key, value in p.items(): template_ = template_.replace('[' + key + ']', value) with open(os.path.join(pbs_in_dir, file[:-4] + '.p'), 'w') as f: f.write(template_) print('{}.p created.'.format(file[:-4]))
def test_pandas2htc(h2writer): wsp_lst = [4, 6, 8] df = pd.DataFrame({'wind.wsp': wsp_lst, 'Name': ['c1', 'c2', 'c3'], 'Folder': ['tmp', 'tmp', 'tmp']}) h2writer.from_pandas(df) for i, wsp in enumerate(wsp_lst, 1): htc = HTCFile(path + "htc/tmp/c%d.htc" % i) assert htc.wind.wsp[0] == wsp
def test_htc_file_del_key(self): htcfile = HTCFile(self.testfilepath + "test.htc") del htcfile.simulation.logfile self.assertTrue("logfile" not in str(htcfile.simulation)) try: del htcfile.hydro.water_properties.water_kinematics_dll except KeyError: pass
def main(): if __name__ == '__main__': # ====================================================================== # load existing htc file # ====================================================================== path = os.path.dirname(test_files.__file__) + "/simulation_setup/DTU10MWRef6.0/" htc = HTCFile(path + "htc/DTU_10MW_RWT.htc") # ====================================================================== # modify wind speed and turbulence intensity # ====================================================================== htc.wind.wsp = 10 # access wind section and change turbulence intensity wind = htc.wind wind.tint = .1 #======================================================================= # print contents #======================================================================= print(htc) # print htc file print(htc.keys()) # print htc sections print(wind) # print wind section #======================================================================= # change tilt angle #======================================================================= orientation = htc.new_htc_structure.orientation # Two ways to access the relative orientation between towertop and shaft # 1) Knowning that it is the second relative section: rel_tt_shaft = orientation.relative__2 # 2) Knowning that the section contains a field "body1" with value "topertop" rel_tt_shaft = orientation.get_subsection_by_name(name='towertop', field='body1') rel_tt_shaft.body2_eulerang__2 = 6, 0, 0 print(rel_tt_shaft.body2_eulerang__2) # ====================================================================== # set time, name and save # ====================================================================== # set time of simulation, first output section and wind.scale_time_start htc.set_time(start=5, stop=10, step=0.1) # change name of logfile, animation, visualization and first output section htc.set_name("tmp_wsp10_tint0.1_tilt6") htc.save() # Save htc modified htcfile as "tmp_wsp10_tint0.1_tilt6"
def test_add_section(self): htcfile = HTCFile() htcfile.wind.add_section('mann') htcfile.wind.mann.add_line("create_turb_parameters", [ 29.4, 1.0, 3.9, 1004, 1.0], "L, alfaeps, gamma, seed, highfrq compensation") 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 load_from_htc(htc_file): """Load shear file from HTC file including shear function Parameters ---------- htc_file : str or HTCFile Filename or HTCFile Returns ------- shear file : ShearFile-object """ if isinstance(htc_file, str): htc_file = HTCFile(htc_file) user_defined_shear_filename = os.path.join( htc_file.modelpath, htc_file.wind.user_defined_shear[0]) shear_file = ShearFile.load(user_defined_shear_filename) shear_file.shear = htc_file.get_shear() shear_file.u0 = htc_file.wind.wsp[0] return shear_file
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') 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)
def test_htc_file_get(self): 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.dll.type2_dll.name[0], "risoe_controller") 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_delete(self): htcfile = HTCFile(self.testfilepath + "test.htc") self.assertTrue("logfile" in str(htcfile.simulation)) htcfile.simulation.logfile.delete() self.assertTrue("logfile" not in str(htcfile.simulation)) self.assertTrue('newmark' in str(htcfile.simulation)) htcfile.simulation.newmark.delete() with self.assertRaises(KeyError): htcfile.simulation.newmark
def test_reset(self): 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() self.assertEqual(logfile.status, UNKNOWN) self.assertEqual(logfile.txt, "") logfile.update_status() self.assertEqual(logfile.status, DONE)
def test_open_eq_save(self): HTCFile(self.testfilepath + "test3.htc", "../").save(self.testfilepath + "tmp.htc") htcfile = HTCFile(self.testfilepath + "tmp.htc", "../") htcfile.save(self.testfilepath + "tmp.htc") self.assertEqual( str(htcfile).count("\t"), str(HTCFile(self.testfilepath + "tmp.htc", "../")).count("\t")) self.assertEqual(str(htcfile), str(HTCFile(self.testfilepath + "tmp.htc", "../")))