def test_input_files(self): htcfile = HTCFile(self.testfilepath + "test.htc") input_files = htcfile.input_files() #print (htcfile.output) for f in [ './data/DTU_10MW_RWT_Tower_st.dat', './data/DTU_10MW_RWT_Towertop_st.dat', './data/DTU_10MW_RWT_Shaft_st.dat', './data/DTU_10MW_RWT_Hub_st.dat', './data/DTU_10MW_RWT_Blade_st.dat', './data/DTU_10MW_RWT_ae.dat', './data/DTU_10MW_RWT_pc.dat', './control/risoe_controller.dll', './control/risoe_controller_64.dll', './control/generator_servo.dll', './control/generator_servo_64.dll', './control/mech_brake.dll', './control/mech_brake_64.dll', './control/servo_with_limits.dll', './control/servo_with_limits_64.dll', './control/towclearsens.dll', './control/towclearsens_64.dll', './data/user_shear.dat', self.testfilepath.replace("\\", "/") + 'test.htc' ]: try: input_files.remove(f) except ValueError: raise ValueError(f + " is not in list") self.assertFalse(input_files) htcfile = HTCFile(self.testfilepath + "DTU_10MW_RWT.htc") self.assertTrue('./control/wpdata.100' in htcfile.input_files())
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_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_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", "../")))
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 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 __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 __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 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_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 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_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_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 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_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 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_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 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_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_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 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_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 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_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_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_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_DLC22y(writer): dlc = DTU_IEC64100_1_Ref_DLB(iec_wt_class='1A', Vin=4, Vout=26, Vr=10, D=180, z_hub=90)['DLC22y'] assert len(dlc) == 276 # 12 wsp, 23 wdir, 1 seeds writer.from_pandas(dlc[::24][:2]) writer.write_all(path + "htc/DLC22y") npt.assert_array_equal(sorted(os.listdir(path + "htc/DLC22y")), ['DLC22y_wsp04_wdir015_s1001.htc', 'DLC22y_wsp06_wdir030_s1101.htc']) htc = HTCFile(path + "htc/DLC22y/DLC22y_wsp04_wdir015_s1001.htc") assert htc.wind.wsp[0] == 4 npt.assert_array_equal(htc.wind.windfield_rotations.values, [15, 0, 0]) assert htc.wind.turb_format[0] == 1 assert htc.wind.mann.create_turb_parameters[3] == 1001
def test_pandas2htc_dottag(h2writer, tmp_path): """Load dlc from dict + pandas dataframe (wind speed tag with dot, name), write htc""" wsp_lst = [4, 6, 8] df = pd.DataFrame({'wind.wsp': wsp_lst, 'Name': ['c1', 'c2', 'c3'], 'Folder': ['tmp']*3}) h2writer.from_pandas(df) h2writer.write_all(tmp_path) for i, wsp in enumerate(wsp_lst, 1): htc_path = tmp_path / ('tmp/c%d.htc' % i) htc = HTCFile(htc_path, modelpath=tmp_path.as_posix()) assert htc.wind.wsp[0] == wsp assert htc.simulation.logfile[0] == ('./log/tmp/c%d.log' % i) assert htc.output.filename[0] == ('./res/tmp/c%d' % i)