Newer
Older
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from builtins import str
from future import standard_library
standard_library.install_aliases()
from wetb.hawc2.htc_contents import HTCContents, HTCSection, HTCLine, \
HTCDefaults
import os
from copy import copy
"""Wrapper for HTC files
Examples:
---------
>>> htcfile = HTCFile('htc/test.htc')
>>> htcfile.wind.wsp = 10
>>> htcfile.save()
"""
def __init__(self, filename=None, modelpath="../"):
self.modelpath = modelpath
self.contents = OrderedDict()
self.initial_comments = []
self.modelpath = os.path.realpath(os.path.join(os.path.dirname(self.filename), modelpath))
self.lines = self.readlines(filename)
# with open(filename) as fid:
# self.lines = fid.readlines()
self.lines = [l.strip() for l in self.lines]
lines = copy(self.lines)
while lines:
if lines[0].startswith(";"):
elif lines[0].lower().startswith("begin"):
self._add_contents(HTCSection.from_lines(lines))
else:
line = HTCLine.from_lines(lines)
self._add_contents(line)
if line.name_ == "exit":
break
#assert 'simulation' in self.contents, "%s could not be loaded. 'simulation' section missing" % filename
def readlines(self, filename):
self.htc_inputfiles.append(filename)
htc_lines = []
with open(filename, encoding='cp1252') as fid:
lines = fid.readlines()
for l in lines:
if l.lower().lstrip().startswith('continue_in_file'):
filename = l.lstrip().split(";")[0][len("continue_in_file"):].strip()
for line in self.readlines(filename):
if line.lstrip().lower().startswith('exit'):
break
htc_lines.append(line)
def __setitem__(self, key, value):
self.contents[key] = value
def save(self, filename=None):
if filename is None:
filename = self.filename
else:
self.filename = filename
# exist_ok does not exist in Python27
os.makedirs(os.path.dirname(filename)) #, exist_ok=True)
with open(filename, 'w', encoding='utf-8') as fid:
#if os.path.isabs(folder) is False and os.path.relpath(folder).startswith("htc" + os.path.sep):
folder = "./" + os.path.relpath(folder).replace("\\", "/")
self.filename = os.path.relpath(os.path.join(self.modelpath, folder, "%s.htc" % name)).replace("\\", "/")
self.simulation.logfile = os.path.join(folder.replace("htc", "log", 1), "%s.log" % name).replace("\\", "/")
elif 'test_structure' in self and 'logfile' in self.test_structure:
self.test_structure.logfile = os.path.join(folder.replace("htc", "log", 1), "%s.log" % name).replace("\\", "/")
self.output.filename = os.path.join(folder.replace("htc", "res", 1), "%s" % name).replace("\\", "/")
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:
files.append(mb.timoschenko_input.filename[0])
files.append(mb.get('external_bladedata_dll', [None, None, None])[2])
if 'aero' in self:
files.append(self.aero.ae_filename[0])
files.append(self.aero.pc_filename[0])
files.append(self.aero.get('external_bladedata_dll', [None, None, None])[2])
files.append(self.aero.get('output_profile_coef_filename', [None])[0])
if 'dynstall_ateflap' in self.aero:
files.append(self.aero.dynstall_ateflap.get('flap', [None] * 3)[2])
if 'bemwake_method' in self.aero:
files.append(self.aero.bemwake_method.get('a-ct-filename', [None] * 3)[0])
for dll in [self.dll[dll] for dll in self.get('dll', {}).keys()]:
files.append(dll.filename[0])
if 'wind' in self:
files.append(self.wind.get('user_defined_shear', [None])[0])
files.append(self.wind.get('wind.user_defined_shear_turbulence', [None])[0])
if 'wakes' in self:
files.append(self.wind.get('use_specific_deficit_file', [None])[0])
files.append(self.wind.get('write_ct_cq_file', [None])[0])
files.append(self.wind.get('write_final_deficits', [None])[0])
if 'hydro' in self:
if 'water_properties' in self.hydro:
files.append(self.hydro.water_properties.get('water_kinematics_dll', [None])[0])
files.append(self.hydro.water_properties.get('water_kinematics_dll', [None, None])[1])
if 'soil' in self:
if 'soil_element' in self.soil:
files.append(self.soil.soil_element.get('datafile', [None])[0])
if 'force' in self:
files.append(self.force.get('dll', [None])[0])
return [f for f in set(files) if f]
def output_files(self):
files = []
for k, index in [('simulation/logfile', 0),
('simulation/animation', 0),
('simulation/visualization', 0),
('new_htc_structure/beam_output_file_name', 0),
('new_htc_structure/body_output_file_name', 0),
('new_htc_structure/struct_inertia_output_file_name', 0),
('new_htc_structure/body_eigenanalysis_file_name', 0),
('new_htc_structure/constraint_output_file_name', 0),
('new_htc_structure/structure_eigenanalysis_file_name', 0),
('turb_export/filename_u', 0),
('turb_export/filename_v', 0),
('turb_export/filename_w', 0)]:
line = self.get(k)
if line:
files.append(line[index])
if 'new_htc_structure' in self:
if 'system_eigenanalysis' in self.new_htc_structure:
f = self.new_htc_structure.system_eigenanalysis[0]
files.append(f)
files.append(os.path.join(os.path.dirname(f), 'mode*.dat').replace("\\", "/"))
for key in [k for k in self.contents.keys() if k.startswith("output_at_time")]:
return []
elif self.wind.turb_format[0] == 1:
files = [self.get('wind.mann.filename_%s' % comp, [None])[0] for comp in ['u', 'v', 'w']]
elif self.wind.turb_format[0] == 2:
files = [self.get('wind.flex.filename_%s' % comp, [None])[0] for comp in ['u', 'v', 'w']]
def res_file_lst(self):
if 'output' not in self:
return []
dataformat = self.output.get('data_format', 'hawc_ascii')
res_filename = self.output.filename[0]
return [res_filename + ".int", os.path.join(os.path.dirname(res_filename), 'sensor')]
else:
return [res_filename + ".sel", res_filename + ".dat"]
def simulate(self, exe):
self.save()
htcfile = os.path.relpath(self.filename, self.modelpath)
hawc2exe = exe
errorcode, stdout, stderr, cmd = pexec([hawc2exe, htcfile], self.modelpath)
if errorcode or 'Elapsed time' not in stderr:
raise Exception (str(stdout) + str(stderr))
f = HTCFile(r"C:\mmpe\HAWC2\models\PhaseIJacketv30\htc", "../../")