Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • 41-dlc-highlevel-fails-on-empty-line-in-dlc-sheet
  • CMOS_FW_H2_Wrapper
  • DEBUG
  • FW_H2_Wrapper
  • master
  • py23
  • wrapper_dev_version
  • v0.0.1
  • v0.0.2
  • v0.0.5
  • v0.0.6
  • v0.0.7
  • v0.0.8
  • v0.0.9
14 results

Target

Select target project
  • toolbox/WindEnergyToolbox
  • tlbl/WindEnergyToolbox
  • cpav/WindEnergyToolbox
  • frza/WindEnergyToolbox
  • borg/WindEnergyToolbox
  • mmpe/WindEnergyToolbox
  • ozgo/WindEnergyToolbox
  • dave/WindEnergyToolbox
  • mmir/WindEnergyToolbox
  • wluo/WindEnergyToolbox
  • welad/WindEnergyToolbox
  • chpav/WindEnergyToolbox
  • rink/WindEnergyToolbox
  • shfe/WindEnergyToolbox
  • shfe1/WindEnergyToolbox
  • acdi/WindEnergyToolbox
  • angl/WindEnergyToolbox
  • wliang/WindEnergyToolbox
  • mimc/WindEnergyToolbox
  • wtlib/WindEnergyToolbox
  • cmos/WindEnergyToolbox
  • fabpi/WindEnergyToolbox
22 results
Select Git revision
  • 105-hawc2io-readascii-is-unable-to-read-incomplete-result-files
  • 106-htcfile-save-method-changes-back-slash-to-forward-slash
  • 106-save-backslash-forwardslash
  • 67-binary-results-reader-in-hawc2-12-6-does-not-find-number-of-blades
  • AbhinavANand
  • FW_H2_Wrapper
  • ModifyHawc2
  • add_future
  • add_set_Name_to_hawc2_input_writer
  • add_wake_sensor
  • bhawc_converter
  • data_manager
  • dlb
  • f/add_test_file
  • fail_bearing
  • fix_pip_install
  • hawc2flow
  • iodocs
  • licoreim
  • master
  • nicgo_dlb_offshore
  • ozgo
  • removed_build_on_install
  • rsod-coverage_example
  • rsod-crypto
  • rsod-dlchighlevel
  • rsod-main_body_analysis
  • rsod-offshore
  • rsod-pages
  • simple_setup
  • test_doc
  • test_docs
  • test_pypi
  • windIO_ozgo
  • v0.0.1
  • v0.0.10
  • v0.0.11
  • v0.0.12
  • v0.0.13
  • v0.0.14
  • v0.0.15
  • v0.0.16
  • v0.0.17
  • v0.0.18
  • v0.0.19
  • v0.0.2
  • v0.0.20
  • v0.0.21
  • v0.0.3
  • v0.0.4
  • v0.0.5
  • v0.0.6
  • v0.0.7
  • v0.0.8
  • v0.0.9
  • v0.1.0
  • v0.1.1
  • v0.1.10
  • v0.1.11
  • v0.1.12
  • v0.1.13
  • v0.1.14
  • v0.1.15
  • v0.1.16
  • v0.1.17
  • v0.1.18
  • v0.1.19
  • v0.1.2
  • v0.1.20
  • v0.1.21
  • v0.1.22
  • v0.1.23
  • v0.1.24
  • v0.1.25
  • v0.1.26
  • v0.1.27
  • v0.1.28
  • v0.1.29
  • v0.1.3
  • v0.1.30
  • v0.1.31
  • v0.1.4
  • v0.1.5
  • v0.1.6
  • v0.1.7
  • v0.1.8
  • v0.1.9
87 results
Show changes
Commits on Source (34)
Showing with 5443 additions and 2515 deletions
...@@ -22,3 +22,7 @@ wetb/prepost/tests/data/demo_dlc/remote* ...@@ -22,3 +22,7 @@ wetb/prepost/tests/data/demo_dlc/remote*
/wetb/fatigue_tools/rainflowcounting/compile.py /wetb/fatigue_tools/rainflowcounting/compile.py
/docs/api /docs/api
/htmlcov /htmlcov
wetb/fatigue_tools/rainflowcounting/pair_range.c
wetb/fatigue_tools/rainflowcounting/peak_trough.c
wetb/fatigue_tools/rainflowcounting/rainflowcount_astm.c
wetb/signal/filters/cy_filters.c
This diff is collapsed.
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import print_function from __future__ import print_function
from __future__ import division from __future__ import division
from __future__ import absolute_import from __future__ import absolute_import
from future import standard_library from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
d = None d = None
d = dir() d = dir()
from .htc_file import HTCFile from .htc_file import HTCFile
from .log_file import LogFile from .log_file import LogFile
from .ae_file import AEFile from .ae_file import AEFile
from .at_time_file import AtTimeFile from .at_time_file import AtTimeFile
from .pc_file import PCFile from .pc_file import PCFile
from . import shear_file from . import shear_file
from .st_file import StFile from .st_file import StFile
__all__ = sorted([m for m in set(dir()) - set(d)]) __all__ = sorted([m for m in set(dir()) - set(d)])
''' '''
Created on 24/04/2014 Created on 24/04/2014
@author: MMPE @author: MMPE
''' '''
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import division from __future__ import division
from __future__ import absolute_import from __future__ import absolute_import
from io import open from io import open
from builtins import range from builtins import range
from builtins import int from builtins import int
from future import standard_library from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
import numpy as np import numpy as np
class AEFile(object): class AEFile(object):
"""Read HAWC2 AE (aerodynamic blade layout) file
"""Read and write the HAWC2 AE (aerodynamic blade layout) file
examples
-------- examples
>>> aefile = AEFile(r"tests/test_files/NREL_5MW_ae.txt") --------
>>> print (aefile.thickness(36)) # Interpolated thickness at radius 36 >>> aefile = AEFile(r"tests/test_files/NREL_5MW_ae.txt")
23.78048780487805 >>> print (aefile.thickness(36)) # Interpolated thickness at radius 36
>>> print (aefile.chord(36)) # Interpolated chord at radius 36 23.78048780487805
3.673 >>> print (aefile.chord(36)) # Interpolated chord at radius 36
>>> print (aefile.pc_set_nr(36)) # pc set number at radius 36 3.673
1 >>> print (aefile.pc_set_nr(36)) # pc set number at radius 36
""" 1
def __init__(self, filename): >>> aef = AEFile()
with open (filename) as fid: >>> aef.add_set(11)
lines = fid.readlines() >>> import numpy as np
nsets = int(lines[0].split()[0]) >>> aef['1.radius']=np.array([ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
lptr = 1 >>> aef['1.chord']=np.array([ 1.1, 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1])
self.ae_sets = {} >>> aef['1.relative_thickness']=np.array([ 100.0, 100.0, 90.0, 80.0, 70.0, 60.0, 50.0, 40.0, 30.0, 20.0, 10.0])
for _ in range(1, nsets + 1): >>> aef['1.pc_set_id']=np.array([ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
for _ in range(nsets): >>> print(aef.get_data_str())
set_nr, n_rows = [int(v) for v in lines[lptr ].split()[:2]] 1 r[m] Chord[m] T/C[%] Set no.
lptr += 1 1 11
data = np.array([[float(v) for v in l.split()[:4]] for l in lines[lptr:lptr + n_rows]]) 0.00000000000000000e+00 1.10000000000000009e+00 1.00000000000000000e+02 1
self.ae_sets[set_nr] = data 1.00000000000000006e-01 1.00000000000000000e+00 1.00000000000000000e+02 1
lptr += n_rows 2.00000000000000011e-01 9.00000000000000022e-01 9.00000000000000000e+01 1
2.99999999999999989e-01 8.00000000000000044e-01 8.00000000000000000e+01 1
4.00000000000000022e-01 6.99999999999999956e-01 7.00000000000000000e+01 1
def _value(self, radius, column, set_nr=1): 5.00000000000000000e-01 5.99999999999999978e-01 6.00000000000000000e+01 1
ae_data = self.ae_sets[set_nr] 5.99999999999999978e-01 5.00000000000000000e-01 5.00000000000000000e+01 1
if radius is None: 6.99999999999999956e-01 4.00000000000000022e-01 4.00000000000000000e+01 1
return ae_data[:,column] 8.00000000000000044e-01 2.99999999999999989e-01 3.00000000000000000e+01 1
else: 9.00000000000000022e-01 2.00000000000000011e-01 2.00000000000000000e+01 1
return np.interp(radius, ae_data[:, 0], ae_data[:, column]) 1.00000000000000000e+00 1.00000000000000006e-01 1.00000000000000000e+01 1
"""
def chord(self, radius=None, set_nr=1):
return self._value(radius, 1, set_nr) def __init__(self, filename=None):
self.ae_sets = {}
def thickness(self, radius=None, set_nr=1): if not filename is None:
return self._value(radius, 2, set_nr) self.read_file(filename)
def radius_ae(self, radius=None, set_nr=1): def _value(self, radius, column, set_nr=1):
radii = self.ae_sets[set_nr][:,0] ae_data = self.ae_sets[set_nr]
if radius: if radius is None:
return radii[np.argmin(np.abs(radii-radius))] return ae_data[:,column]
else: else:
return radii return np.interp(radius, ae_data[:, 0], ae_data[:, column])
def pc_set_nr(self, radius, set_nr=1): def chord(self, radius=None, set_nr=1):
ae_data = self.ae_sets[set_nr] return self._value(radius, 1, set_nr)
index = np.searchsorted(ae_data[:, 0], radius)
index = max(1, index) def thickness(self, radius=None, set_nr=1):
setnrs = ae_data[index - 1:index + 1, 3] return self._value(radius, 2, set_nr)
if setnrs[0] != setnrs[-1]:
raise NotImplementedError def radius_ae(self, radius=None, set_nr=1):
return setnrs[0] radii = self.ae_sets[set_nr][:,0]
if radius:
return radii[np.argmin(np.abs(radii-radius))]
else:
return radii
if __name__ == "__main__": def pc_set_nr(self, radius, set_nr=1):
ae = AEFile(r"tests/test_files/NREL_5MW_ae.txt") ae_data = self.ae_sets[set_nr]
print (ae.radius_ae(36)) index = np.searchsorted(ae_data[:, 0], radius)
print (ae.thickness()) index = max(1, index)
print (ae.chord(36)) setnrs = ae_data[index - 1:index + 1, 3]
print (ae.pc_set_nr(36)) if setnrs[0] != setnrs[-1]:
raise NotImplementedError
return setnrs[0]
def __getitem__(self, key):
'''Retrieves the data accoding to keys like "1.chord", with the integer is the set number, and the string is the variable'''
keys=key.split('.')
set_nr = 1
data_key = 'radius'
if len(keys)==1:
if len(self.ae_sets)!=1:
raise KeyError('Must specify the set in the key')
set_nr = self.ae_sets.keys()[0]
data_key = keys[0]
elif len(keys)==2:
set_nr = int(keys[0])
data_key = keys[1]
else:
raise KeyError('Key member nesting only 2 deep')
ae_data = self.ae_sets[set_nr]
if data_key=='radius':
return ae_data[:,0]
elif data_key=='chord':
return ae_data[:,1]
elif data_key=='relative_thickness':
return ae_data[:,2]
elif data_key=='pc_set_id':
return ae_data[:,3]
else:
raise KeyError('That variable does not exist')
def __setitem__(self, key, val):
'''Sets the data accoding to keys like "1.chord", with the integer is the set number, and the string is the variable'''
keys=key.split('.')
set_nr = 1
data_key = 'radius'
if len(keys)==1:
if len(self.ae_sets)!=1:
raise KeyError('Must specify the set in the key')
set_nr = self.ae_sets.keys()[0]
data_key = keys[0]
elif len(keys)==2:
set_nr = int(keys[0])
data_key = keys[1]
else:
raise KeyError('Key member nesting only 2 deep')
ae_data = self.ae_sets[set_nr]
if len(val)!=len(ae_data):
raise IndexError('The assigned data is not consistent with the current data')
if data_key=='radius':
ae_data[:,0]=val
elif data_key=='chord':
ae_data[:,1]=val
elif data_key=='relative_thickness':
ae_data[:,2]=val
elif data_key=='pc_set_id':
ae_data[:,3]=val
else:
raise KeyError('That variable does not exist')
def keys(self):
'''Retrieves the keys for this object'''
retval=[]
for set_id in self.ae_sets.keys():
retval.append(str(set_id)+'.'+'radius')
retval.append(str(set_id)+'.'+'chord')
retval.append(str(set_id)+'.'+'relative_thickness')
retval.append(str(set_id)+'.'+'pc_set_id')
return retval
def get_set_size(self, set_id):
'''This will retrieve the number of rows for a given set'''
if not set_id in self.ae_sets.keys():
raise KeyError('That set does not exist')
return len(self.ae_sets[set_id])
def resize_set(self, set_id, row_nr):
'''This will change the number of rows for a given set'''
if not set_id in self.ae_sets.keys():
raise KeyError('That set does not exist')
old_data = self.ae_sets[set_id]
old_rows = len(old_data)
# early exit if there is nothing to do
if old_rows==row_nr:
return
# must generate a new array
new_data = np.zeros((row_nr,4))
asg_rows=old_rows
if row_nr<asg_rows:
asg_rows=row_nr
if asg_rows>0:
new_data[:asg_rows,:]=old_data[:asg_rows,:]
self.ae_sets[set_id]=new_data
def get_set_count(self):
'''Returns the number of sets'''
return len(self.ae_sets)
def add_set(self, row_nr=0):
'''This method will add another set to the ae data'''
set_id = 1
while set_id in self.ae_sets.keys():
set_id+=1
self.ae_sets[set_id]=np.zeros((row_nr,4))
def get_data_str(self):
'''This method will create a string that is formatted like an ae file with the data in this class'''
n_sets = len(self.ae_sets)
retval=str(n_sets)+' r[m] Chord[m] T/C[%] Set no.\n'
for st_idx, st in self.ae_sets.items():
retval+=str(st_idx)+' '+str(len(st))+'\n'
for line in st:
retval+='%25.17e %25.17e %25.17e %5d\n'%(line[0], line[1], line[2], line[3])
return retval
def __str__(self):
return self.get_data_str()
def read_file(self, filename):
''' This method will read in the ae data from a HAWC2 ae file'''
with open (filename) as fid:
lines = fid.readlines()
nsets = int(lines[0].split()[0])
lptr = 1
self.ae_sets = {}
for _ in range(1, nsets + 1):
for _ in range(nsets):
set_nr, n_rows = [int(v) for v in lines[lptr ].split()[:2]]
lptr += 1
data = np.array([[float(v) for v in l.split()[:4]] for l in lines[lptr:lptr + n_rows]])
self.ae_sets[set_nr] = data
lptr += n_rows
if __name__ == "__main__":
ae = AEFile(r"tests/test_files/NREL_5MW_ae.txt")
print (ae.radius_ae(36))
print (ae.thickness())
print (ae.chord(36))
print (ae.pc_set_nr(36))
aef = AEFile()
aef.add_set(11)
import numpy as np
aef['1.radius']=np.array([ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
aef['1.chord']=np.array([ 1.1, 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1])
aef['1.relative_thickness']=np.array([ 100.0, 100.0, 90.0, 80.0, 70.0, 60.0, 50.0, 40.0, 30.0, 20.0, 10.0])
aef['1.pc_set_id']=np.array([ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
print(aef.get_data_str())
''' '''
Created on 24/04/2014 Created on 24/04/2014
@author: MMPE @author: MMPE
''' '''
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import division from __future__ import division
from __future__ import absolute_import from __future__ import absolute_import
from io import open from io import open
from builtins import range from builtins import range
from future import standard_library from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
import numpy as np import numpy as np
class AtTimeFile(object): class AtTimeFile(object):
"""Loads an at time file generated by HAWC2 """Loads an at time file generated by HAWC2
Functions are generated for each sensor in the file from the template: Functions are generated for each sensor in the file from the template:
xxx(l=None, curved_length=False) xxx(l=None, curved_length=False)
E.g. E.g.
twist(36, curved_length=True) # Twist 36m from the root along the (curved) center line twist(36, curved_length=True) # Twist 36m from the root along the (curved) center line
twist(36) # Twist at radius 36m (Straight line radius, nondim. with bladetip_radius which must be specified in constructor) twist(36) # Twist at radius 36m (Straight line radius, nondim. with bladetip_radius which must be specified in constructor)
twist() # Twist at all aerodynamic calculation points twist() # Twist at all aerodynamic calculation points
>>> at = AtTimeFile("at.dat", 86.3655) # load file >>> at = AtTimeFile("at.dat", 86.3655) # load file
>>> at.attribute_names # Attribute names >>> at.attribute_names # Attribute names
['radius_s', 'twist', 'chord'] ['radius_s', 'twist', 'chord']
>>> at[:3,1]) # first 3 twist rows >>> at[:3,1]) # first 3 twist rows
[-14.5 -14.5002 -14.5007] [-14.5 -14.5002 -14.5007]
>>> at.twist()[:3]) # Twist first 3 twist rows >>> at.twist()[:3]) # Twist first 3 twist rows
[-14.5 -14.5002 -14.5007] [-14.5 -14.5002 -14.5007]
>>> at.twist(36, curved_length=True) # Twist at curved_length = 36 (interpolated) >>> at.twist(36, curved_length=True) # Twist at curved_length = 36 (interpolated)
-5.172195702789108 -5.172195702789108
>>> at.twist(36) # Twist at radius = 36 (interpolated) >>> at.twist(36) # Twist at radius = 36 (interpolated)
-5.162084567646019 -5.162084567646019
""" """
def __init__(self, filename, bladetip_radius=None): def __init__(self, filename, bladetip_radius=None):
""" """
Parameters Parameters
---------- ----------
filename : string filename : string
Filename Filename
bladetip_radius : int, float or None, optional bladetip_radius : int, float or None, optional
Radius of blade tip. Used to convert from curved length to radius Radius of blade tip. Used to convert from curved length to radius
""" """
self.blade_radius = bladetip_radius self.blade_radius = bladetip_radius
with open(filename, encoding='utf-8') as fid: with open(filename, encoding='utf-8') as fid:
lines = fid.readlines() lines = fid.readlines()
atttribute_name_line = [l.strip().startswith("# Radius_s") for l in lines].index(True) atttribute_name_line = [l.strip().startswith("# Radius_s") for l in lines].index(True)
#self.attribute_names = lines[atttribute_name_line].lower().replace("#", "").split() #self.attribute_names = lines[atttribute_name_line].lower().replace("#", "").split()
self.attribute_names = [n.strip() for n in lines[atttribute_name_line].lower().split("#")[1:]] self.attribute_names = [n.strip() for n in lines[atttribute_name_line].lower().split("#")[1:]]
data = np.array([[float(l) for l in lines[i].split() ] for i in range(atttribute_name_line+1, len(lines))]) data = np.array([[float(l) for l in lines[i].split() ] for i in range(atttribute_name_line+1, len(lines))])
self.data = data self.data = data
def func_factory(column): def func_factory(column):
def values(l=None, curved_length=False): def values(l=None, curved_length=False):
assert curved_length==True or curved_length==False, "Curved length must be boolean, but is %s"%curved_length assert curved_length==True or curved_length==False, "Curved length must be boolean, but is %s"%curved_length
if l is None: if l is None:
return self.data[:, column] return self.data[:, column]
else: else:
if curved_length: if curved_length:
return np.interp(l, self.data[:, 0], self.data[:, column]) return np.interp(l, self.data[:, 0], self.data[:, column])
else: else:
assert self.blade_radius is not None, "bladetip_radius must be specified in __init__ when requesting value of radius (alternatively you can request for curved_length)" assert self.blade_radius is not None, "bladetip_radius must be specified in __init__ when requesting value of radius (alternatively you can request for curved_length)"
return np.interp(l/self.blade_radius, self.data[:, 0]/self.data[-1, 0], self.data[:, column]) return np.interp(l/self.blade_radius, self.data[:, 0]/self.data[-1, 0], self.data[:, column])
return values return values
for column, att_name in enumerate(self.attribute_names): for column, att_name in enumerate(self.attribute_names):
setattr(self, att_name, func_factory(column)) setattr(self, att_name, func_factory(column))
def radius_curved_ac(self, radius=None): def radius_curved_ac(self, radius=None):
"""Radius (curved distance) of aerodynamic calculation point(s) """Radius (curved distance) of aerodynamic calculation point(s)
Parameters Parameters
---------- ----------
radius : int or float, optional radius : int or float, optional
- if None (default): Radius of calculation points\n - if None (default): Radius of calculation points\n
- if int or float: Radius of calculation point nearest radius - if int or float: Radius of calculation point nearest radius
Returns Returns
------- -------
radius : float or array_like radius : float or array_like
Radius of calculation points or radius of calculation point nearest radius Radius of calculation points or radius of calculation point nearest radius
""" """
if radius is None: if radius is None:
return self.radius_s() return self.radius_s()
else: else:
return self.radius_s()[np.argmin(np.abs(self.radius_s() - radius))] return self.radius_s()[np.argmin(np.abs(self.radius_s() - radius))]
def value(self, radius, column): def value(self, radius, column):
return np.interp(radius, self.data[:, 0], self.data[:, column]) return np.interp(radius, self.data[:, 0], self.data[:, column])
def __getitem__(self, subset): def __getitem__(self, subset):
return self.data[subset] return self.data[subset]
if __name__ == "__main__": if __name__ == "__main__":
at = AtTimeFile(r"tests/test_files/at.dat", 86.3655) # load file at = AtTimeFile(r"tests/test_files/at.dat", 86.3655) # load file
at = AtTimeFile(r'U:\hama\HAWC2-AVATAR\res/avatar-7ntm-scaled-rad.dat') at = AtTimeFile(r'U:\hama\HAWC2-AVATAR\res/avatar-7ntm-scaled-rad.dat')
at.attribute_names # Attribute names at.attribute_names # Attribute names
at[:3,1] # first 3 twist rows at[:3,1] # first 3 twist rows
print (len(at.attribute_names)) print (len(at.attribute_names))
print ("\n".join(at.attribute_names)) print ("\n".join(at.attribute_names))
print (at.data.shape) print (at.data.shape)
#at.twist()[:3] # Twist first 3 twist rows #at.twist()[:3] # Twist first 3 twist rows
#print (at.twist(36, curved_length=True)) # Twist at curved_length = 36 (interpolated) #print (at.twist(36, curved_length=True)) # Twist at curved_length = 36 (interpolated)
#print (at.twist(36)) # Twist at 36 (interpolated) #print (at.twist(36)) # Twist at 36 (interpolated)
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import absolute_import from __future__ import absolute_import
from io import open from io import open
from builtins import range from builtins import range
from builtins import str from builtins import str
from builtins import zip from builtins import zip
from future import standard_library from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
import string import string
import shutil import shutil
import numpy as np import numpy as np
import os import os
import filecmp import filecmp
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import unittest import unittest
import numpy as np import numpy as np
import re import re
import sys import sys
from wetb.hawc2.Hawc2io import ReadHawc2 from wetb.hawc2.Hawc2io import ReadHawc2
class CompareTestCases(unittest.TestCase): class CompareTestCases(unittest.TestCase):
def compare_lines(self, ref_file, test_file, skip_first=0): def compare_lines(self, ref_file, test_file, skip_first=0):
with open(ref_file, encoding='utf-8') as ref: with open(ref_file, encoding='utf-8') as ref:
ref_lines = ref.readlines() ref_lines = ref.readlines()
with open(test_file, encoding='utf-8') as test: with open(test_file, encoding='utf-8') as test:
test_lines = test.readlines() test_lines = test.readlines()
self.assertEqual(len(ref_lines), len(test_lines), "\nNumber of lines differs in: '%s' and '%s'" % (ref_file, test_file)) self.assertEqual(len(ref_lines), len(test_lines), "\nNumber of lines differs in: '%s' and '%s'" % (ref_file, test_file))
for i, (ref_l, test_l) in enumerate(zip(ref_lines[skip_first:], test_lines[skip_first:])): for i, (ref_l, test_l) in enumerate(zip(ref_lines[skip_first:], test_lines[skip_first:])):
if ref_l.lower() != test_l.lower(): if ref_l.lower() != test_l.lower():
diff = "".join([[" ", "^"][a != b] for a, b in zip(ref_l, test_l)]) diff = "".join([[" ", "^"][a != b] for a, b in zip(ref_l, test_l)])
err_str = "%s%s%s\n\n" % (ref_l, test_l, diff) err_str = "%s%s%s\n\n" % (ref_l, test_l, diff)
raise AssertionError("Difference in line %d of %s\n%s" % (i, ref_file, err_str)) raise AssertionError("Difference in line %d of %s\n%s" % (i, ref_file, err_str))
def compare_sel(self, ref_file, test_file): def compare_sel(self, ref_file, test_file):
self.compare_lines(ref_file, test_file, 8) self.compare_lines(ref_file, test_file, 8)
def compare_dat_contents(self, ref_file, test_file): def compare_dat_contents(self, ref_file, test_file):
if filecmp.cmp(ref_file, test_file, shallow=False) is False: if filecmp.cmp(ref_file, test_file, shallow=False) is False:
self.compare_lines(ref_file, test_file) self.compare_lines(ref_file, test_file)
def min_tol(self, ref_data, test_data): def min_tol(self, ref_data, test_data):
def error(x, a, b): def error(x, a, b):
atol, rtol = x atol, rtol = x
if rtol > 0 and atol > 0 and np.allclose(b, a, rtol, atol): if rtol > 0 and atol > 0 and np.allclose(b, a, rtol, atol):
return rtol + atol return rtol + atol
else: else:
return 10 ** 99 return 10 ** 99
from scipy.optimize import fmin from scipy.optimize import fmin
atol, rtol = fmin(error, (1, 1), (ref_data, test_data), disp=False) atol, rtol = fmin(error, (1, 1), (ref_data, test_data), disp=False)
return atol, rtol return atol, rtol
def compare_dat_plot(self, ref_file, test_file, show_plot=False, rtol=1.e-5, atol=1.e-8): def compare_dat_plot(self, ref_file, test_file, show_plot=False, rtol=1.e-5, atol=1.e-8):
ref = ReadHawc2(os.path.splitext(ref_file)[0]) ref = ReadHawc2(os.path.splitext(ref_file)[0])
test = ReadHawc2(os.path.splitext(test_file)[0]) test = ReadHawc2(os.path.splitext(test_file)[0])
ref_data = ref() ref_data = ref()
test_data = test() test_data = test()
if not np.allclose(ref_data, test_data, rtol=rtol, atol=atol): if not np.allclose(ref_data, test_data, rtol=rtol, atol=atol):
different_sensors = [] different_sensors = []
for i in range(ref.NrCh): for i in range(ref.NrCh):
if not np.allclose(ref_data[:, i], test_data[:, i], rtol=rtol, atol=atol): if not np.allclose(ref_data[:, i], test_data[:, i], rtol=rtol, atol=atol):
different_sensors.append(i) different_sensors.append(i)
path = os.path.join(os.path.dirname(test_file), "Compare", os.path.splitext(os.path.basename(ref_file))[0]) path = os.path.join(os.path.dirname(test_file), "Compare", os.path.splitext(os.path.basename(ref_file))[0])
shutil.rmtree(path, ignore_errors=True) shutil.rmtree(path, ignore_errors=True)
try: try:
os.mkdir(path) os.mkdir(path)
except: except:
try: try:
os.mkdir(os.path.join(os.path.dirname(test_file), "Compare")) os.mkdir(os.path.join(os.path.dirname(test_file), "Compare"))
os.mkdir(path) os.mkdir(path)
except: except:
pass pass
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits) valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
sys.stderr.write("%d sensors are different in datafiles\n" % len(different_sensors)) sys.stderr.write("%d sensors are different in datafiles\n" % len(different_sensors))
sys.stderr.write("All close with\nAbsolute tolerance: %.6f\nRelative tolerance: %.6f\n\n" % self.min_tol(ref_data, test_data)) sys.stderr.write("All close with\nAbsolute tolerance: %.6f\nRelative tolerance: %.6f\n\n" % self.min_tol(ref_data, test_data))
sys.stderr.write(" ".join("%-20s" % s for s in ["Mean abs error", "Mean rel error (%)", "Max abs error", "Max rel error(%)", "Sensor"]) + "\n") sys.stderr.write(" ".join("%-20s" % s for s in ["Mean abs error", "Mean rel error (%)", "Max abs error", "Max rel error(%)", "Sensor"]) + "\n")
abs_err = np.abs(ref_data - test_data) abs_err = np.abs(ref_data - test_data)
mask = (np.abs(ref_data) != 0) mask = (np.abs(ref_data) != 0)
rel_err = abs_err[mask] / np.abs(ref_data[mask]) * 100 rel_err = abs_err[mask] / np.abs(ref_data[mask]) * 100
err_str = ["%.6f" % e for e in [np.mean(abs_err), np.mean(rel_err), np.max(abs_err), np.max(rel_err)]] err_str = ["%.6f" % e for e in [np.mean(abs_err), np.mean(rel_err), np.max(abs_err), np.max(rel_err)]]
err_str = " ".join(["%-20s" % e for e in err_str]) err_str = " ".join(["%-20s" % e for e in err_str])
sys.stderr.write("%s All data values\n" % (err_str)) sys.stderr.write("%s All data values\n" % (err_str))
for i in different_sensors: for i in different_sensors:
abs_err = np.abs(ref_data[:, i] - test_data[:, i]) abs_err = np.abs(ref_data[:, i] - test_data[:, i])
mask = (np.abs(ref_data[:, i]) != 0) mask = (np.abs(ref_data[:, i]) != 0)
rel_err = abs_err[mask] / np.abs(ref_data[mask, i]) * 100 rel_err = abs_err[mask] / np.abs(ref_data[mask, i]) * 100
err_str = ["%.6f" % e for e in [np.mean(abs_err), np.mean(rel_err), np.max(abs_err), np.max(rel_err)]] err_str = ["%.6f" % e for e in [np.mean(abs_err), np.mean(rel_err), np.max(abs_err), np.max(rel_err)]]
err_str = " ".join(["%-20s" % e for e in err_str]) err_str = " ".join(["%-20s" % e for e in err_str])
sys.stderr.write("%s %d %s [%s] %s\n" % (err_str, (i + 1), ref.ChInfo[0][i], ref.ChInfo[1][i], ref.ChInfo[2][i])) sys.stderr.write("%s %d %s [%s] %s\n" % (err_str, (i + 1), ref.ChInfo[0][i], ref.ChInfo[1][i], ref.ChInfo[2][i]))
sys.stderr.flush() sys.stderr.flush()
plt.cla() plt.cla()
plt.plot(ref_data[:, i], 'g', lw=3, label="Ref: %s [%s] %s" % (ref.ChInfo[0][i], ref.ChInfo[1][i], ref.ChInfo[2][i])) plt.plot(ref_data[:, i], 'g', lw=3, label="Ref: %s [%s] %s" % (ref.ChInfo[0][i], ref.ChInfo[1][i], ref.ChInfo[2][i]))
plt.plot(test_data[:, i], 'r', lw=1, label="test: %s [%s] %s" % (test.ChInfo[0][i], test.ChInfo[1][i], test.ChInfo[2][i])) plt.plot(test_data[:, i], 'r', lw=1, label="test: %s [%s] %s" % (test.ChInfo[0][i], test.ChInfo[1][i], test.ChInfo[2][i]))
from matplotlib.font_manager import FontProperties from matplotlib.font_manager import FontProperties
fontP = FontProperties() fontP = FontProperties()
fontP.set_size('small') fontP.set_size('small')
plt.legend(loc='best', prop=fontP) plt.legend(loc='best', prop=fontP)
plt.axes().set_title(os.path.basename(ref_file)) plt.axes().set_title(os.path.basename(ref_file))
if show_plot: if show_plot:
plt.show() plt.show()
else: else:
plot_file = os.path.join(path, ("%03d_" % (i + 1)) + "".join([c for c in ref.ChInfo[0][i] if c in valid_chars]) + ".png") plot_file = os.path.join(path, ("%03d_" % (i + 1)) + "".join([c for c in ref.ChInfo[0][i] if c in valid_chars]) + ".png")
plt.savefig(plot_file) plt.savefig(plot_file)
#raise AssertionError("Difference in the the values of:\n%s" % "\n".join(["%d %s" % (i + 1, ref.ChInfo[0][i]) for i in different_sensors])) #raise AssertionError("Difference in the the values of:\n%s" % "\n".join(["%d %s" % (i + 1, ref.ChInfo[0][i]) for i in different_sensors]))
def version_tag(self, filename): def version_tag(self, filename):
re_version = re.compile(r".*_(\d*\.\d*)\.sel") re_version = re.compile(r".*_(\d*\.\d*)\.sel")
match = re.match(re_version, filename) match = re.match(re_version, filename)
if match and len(match.groups()) == 1: if match and len(match.groups()) == 1:
return match.group(1) return match.group(1)
return "" return ""
def common_path(self, path1, path2): def common_path(self, path1, path2):
cp = [] cp = []
for f1, f2 in zip(os.path.realpath(path1).split(os.path.sep), os.path.realpath(path2).split(os.path.sep)): for f1, f2 in zip(os.path.realpath(path1).split(os.path.sep), os.path.realpath(path2).split(os.path.sep)):
if f1 == f2: if f1 == f2:
cp.append(f1) cp.append(f1)
else: else:
break break
return os.path.sep.join(cp) return os.path.sep.join(cp)
def compare_file(self, ref_file, test_file, show_plot=False, rtol=1.e-5, atol=1.e-8): def compare_file(self, ref_file, test_file, show_plot=False, rtol=1.e-5, atol=1.e-8):
try: try:
assert os.path.isfile(test_file), "File '%s' not found" % test_file assert os.path.isfile(test_file), "File '%s' not found" % test_file
try: try:
self.compare_sel(ref_file, test_file) self.compare_sel(ref_file, test_file)
self.compare_dat_plot(ref_file.replace(".sel", ".dat"), test_file.replace(".sel", ".dat"), show_plot=show_plot, rtol=rtol, atol=atol) self.compare_dat_plot(ref_file.replace(".sel", ".dat"), test_file.replace(".sel", ".dat"), show_plot=show_plot, rtol=rtol, atol=atol)
print ("ok\n\n\n") print ("ok\n\n\n")
except AssertionError as e: except AssertionError as e:
sys.stderr.write(str(e) + "\n") sys.stderr.write(str(e) + "\n")
self.compare_dat_plot(ref_file.replace(".sel", ".dat"), test_file.replace(".sel", ".dat"), show_plot=show_plot, rtol=rtol, atol=atol) self.compare_dat_plot(ref_file.replace(".sel", ".dat"), test_file.replace(".sel", ".dat"), show_plot=show_plot, rtol=rtol, atol=atol)
print ("Data file ok\n\n\n") print ("Data file ok\n\n\n")
except AssertionError as e: except AssertionError as e:
sys.stderr.write (str(e) + "\n\n") sys.stderr.write (str(e) + "\n\n")
def compare_folder(self, ref_res_path, test_res_path, ref_version_tag, test_version_tag, show_plot=False, rtol=1.e-5, atol=1.e-8): def compare_folder(self, ref_res_path, test_res_path, ref_version_tag, test_version_tag, show_plot=False, rtol=1.e-5, atol=1.e-8):
files = [f for f in os.listdir(ref_res_path) if f.endswith(".sel")] files = [f for f in os.listdir(ref_res_path) if f.endswith(".sel")]
common_path = self.common_path(ref_res_path, test_res_path) common_path = self.common_path(ref_res_path, test_res_path)
for filename in files: for filename in files:
self.version_tag(filename) self.version_tag(filename)
ref_file = os.path.join(ref_res_path, filename) ref_file = os.path.join(ref_res_path, filename)
ref_version_tag = self.version_tag(filename) ref_version_tag = self.version_tag(filename)
test_version_tag = self.version_tag([f for f in os.listdir(test_res_path) if f.endswith(".sel")][0]) test_version_tag = self.version_tag([f for f in os.listdir(test_res_path) if f.endswith(".sel")][0])
print ("-"*50) print ("-"*50)
try: try:
prefix = filename[:filename.index(ref_version_tag)] prefix = filename[:filename.index(ref_version_tag)]
postfix = filename[filename.index(ref_version_tag) + len(ref_version_tag):] postfix = filename[filename.index(ref_version_tag) + len(ref_version_tag):]
test_filename = [f for f in os.listdir(test_res_path) if f.startswith(prefix) and f.endswith(postfix)][0] test_filename = [f for f in os.listdir(test_res_path) if f.startswith(prefix) and f.endswith(postfix)][0]
test_file = os.path.join(test_res_path, test_filename) test_file = os.path.join(test_res_path, test_filename)
print ("Comparing %s and %s\n" % tuple(f.replace(common_path, "") for f in (ref_file, test_file))) print ("Comparing %s and %s\n" % tuple(f.replace(common_path, "") for f in (ref_file, test_file)))
except IndexError: except IndexError:
sys.stdout.flush() sys.stdout.flush()
sys.stderr.write ("\nNo matching test file found for %s\n\n" % ref_file.replace(common_path, "")) sys.stderr.write ("\nNo matching test file found for %s\n\n" % ref_file.replace(common_path, ""))
sys.stderr.flush() sys.stderr.flush()
continue continue
self.compare_file(ref_file, test_file, show_plot, rtol, atol) self.compare_file(ref_file, test_file, show_plot, rtol, atol)
def runTest(self): def runTest(self):
pass pass
if __name__ == "__main__": if __name__ == "__main__":
ref_path = r'S:\AED\HAWC2\HAWC2_release_test_cases\version_11.4\output\res/' ref_path = r'S:\AED\HAWC2\HAWC2_release_test_cases\version_11.4\output\res/'
test_path = r'S:\AED\HAWC2\HAWC2_release_test_cases\version_11.8w\output\res/' test_path = r'S:\AED\HAWC2\HAWC2_release_test_cases\version_11.8w\output\res/'
# rtol: relative tolerance # rtol: relative tolerance
# atol: absolute tolerance # atol: absolute tolerance
# absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`)) # absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`))
CompareTestCases().compare_folder(ref_path, test_path, "11.4", "11.8w", show_plot=False, rtol=1.e-5, atol=1.e-5) CompareTestCases().compare_folder(ref_path, test_path, "11.4", "11.8w", show_plot=False, rtol=1.e-5, atol=1.e-5)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
''' '''
Created on 18/11/2015 Created on 18/11/2015
@author: MMPE @author: MMPE
''' '''
from __future__ import print_function from __future__ import print_function
from __future__ import division from __future__ import division
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import absolute_import from __future__ import absolute_import
from io import open from io import open
from builtins import int from builtins import int
from future import standard_library from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
import os import os
from wetb.hawc2.htc_file import HTCFile from wetb.hawc2.htc_file import HTCFile
from collections import OrderedDict from collections import OrderedDict
import time import time
import math import math
UNKNOWN = "Unknown" UNKNOWN = "Unknown"
MISSING = "Log file not found (May be waiting for PBS allocation)" MISSING = "Log file not found (May be waiting for PBS allocation)"
PENDING = "Simulation not started yet" PENDING = "Simulation not started yet"
INITIALIZATION = 'Initializing simulation' INITIALIZATION = 'Initializing simulation'
SIMULATING = "Simulating" SIMULATING = "Simulating"
DONE = "Simulation succeded" DONE = "Simulation succeded"
class LogInterpreter(object): class LogInterpreter(object):
def __init__(self, time_stop): def __init__(self, time_stop):
self.time_stop = time_stop self.time_stop = time_stop
self.hawc2version = "Unknown" self.hawc2version = "Unknown"
self.reset() self.reset()
self.update_status() self.update_status()
def reset(self): def reset(self):
self.position = 0 self.position = 0
self.lastline = "" self.lastline = ""
self.txt = "" self.txt = ""
self.status = UNKNOWN self.status = UNKNOWN
self.pct = 0 self.pct = 0
self.errors = [] self.errors = []
self.info = [] self.info = []
self.start_time = None self.start_time = None
self.current_time = 0 self.current_time = 0
self.remaining_time = None self.remaining_time = None
def __str__(self): def __str__(self):
return self.txt return self.txt
def clear(self): def clear(self):
self.reset() self.reset()
def extract_time(self, txt): def extract_time(self, txt):
i1 = txt.rfind("Global time") i1 = txt.rfind("Global time")
if i1 == -1: if i1 == -1:
return self.current_time return self.current_time
else: else:
time_line = txt[i1:].strip() time_line = txt[i1:].strip()
if time_line == "": if time_line == "":
return self.current_time return self.current_time
try: try:
return float(time_line[time_line.index('=') + 1:time_line.index('Iter')]) return float(time_line[time_line.index('=') + 1:time_line.index('Iter')])
except: except:
print ("Cannot extract time from #" + time_line + "#") print ("Cannot extract time from #" + time_line + "#")
pass pass
def update_status(self, new_lines=""): def update_status(self, new_lines=""):
if self.txt == "" and new_lines == "": if self.txt == "" and new_lines == "":
self.status = MISSING self.status = MISSING
else: else:
if self.status == UNKNOWN or self.status == MISSING: if self.status == UNKNOWN or self.status == MISSING:
self.status = PENDING self.status = PENDING
txt = new_lines txt = new_lines
self.txt += txt self.txt += txt
if self.status == PENDING and self.position > 0: if self.status == PENDING and self.position > 0:
self.status = INITIALIZATION self.status = INITIALIZATION
if len(txt) > 0: if len(txt) > 0:
if len(txt.strip()): if len(txt.strip()):
self.lastline = (txt.strip()[max(0, txt.strip().rfind("\n")):]).strip() self.lastline = (txt.strip()[max(0, txt.strip().rfind("\n")):]).strip()
if self.status == INITIALIZATION: if self.status == INITIALIZATION:
_3to2list = list(txt.split("Starting simulation")) _3to2list = list(txt.split("Starting simulation"))
init_txt, rest, = _3to2list[:1] + [_3to2list[1:]] init_txt, rest, = _3to2list[:1] + [_3to2list[1:]]
if self.hawc2version == "Unknown" and "Version ID" in init_txt: if self.hawc2version == "Unknown" and "Version ID" in init_txt:
self.hawc2version = txt.split("Version ID : ")[1].split("\n", 1)[0].strip() self.hawc2version = txt.split("Version ID : ")[1].split("\n", 1)[0].strip()
if "*** ERROR ***" in init_txt: if "*** ERROR ***" in init_txt:
self.errors.extend([l.strip() for l in init_txt.strip().split("\n") if "error" in l.lower()]) self.errors.extend([l.strip() for l in init_txt.strip().split("\n") if "error" in l.lower()])
if rest: if rest:
txt = rest[0] txt = rest[0]
self.status = SIMULATING self.status = SIMULATING
if self.status == SIMULATING: if self.status == SIMULATING:
if self.start_time is None and not 'Elapsed time' in self.lastline: if self.start_time is None and not 'Elapsed time' in self.lastline:
i1 = txt.rfind("Global time") i1 = txt.rfind("Global time")
if i1 > -1: if i1 > -1:
self.start_time = (self.extract_time(txt[i1:]), time.time()) self.start_time = (self.extract_time(txt[i1:]), time.time())
_3to2list1 = list(txt.split('Elapsed time')) _3to2list1 = list(txt.split('Elapsed time'))
simulation_txt, rest, = _3to2list1[:1] + [_3to2list1[1:]] simulation_txt, rest, = _3to2list1[:1] + [_3to2list1[1:]]
if "*** ERROR ***" in simulation_txt: if "*** ERROR ***" in simulation_txt:
self.errors.extend([l.strip() for l in simulation_txt.strip().split("\n") if "error" in l.lower()]) self.errors.extend([l.strip() for l in simulation_txt.strip().split("\n") if "error" in l.lower()])
i1 = simulation_txt.rfind("Global time") i1 = simulation_txt.rfind("Global time")
if i1 > -1: if i1 > -1:
self.current_time = self.extract_time(simulation_txt[i1:]) self.current_time = self.extract_time(simulation_txt[i1:])
if self.current_time is not None and self.time_stop > 0: if self.current_time is not None and self.time_stop > 0:
self.pct = int(100 * self.current_time // self.time_stop) self.pct = int(100 * self.current_time // self.time_stop)
try: try:
self.remaining_time = (time.time() - self.start_time[1]) / (self.current_time - self.start_time[0]) * (self.time_stop - self.current_time) self.remaining_time = (time.time() - self.start_time[1]) / (self.current_time - self.start_time[0]) * (self.time_stop - self.current_time)
except: except:
pass pass
if rest: if rest:
self.status = DONE self.status = DONE
self.pct = 100 self.pct = 100
self.elapsed_time = float(rest[0].replace(":", "").strip()) self.elapsed_time = float(rest[0].replace(":", "").strip())
def error_str(self): def error_str(self):
error_dict = OrderedDict() error_dict = OrderedDict()
for error in self.errors: for error in self.errors:
error_dict[error] = error_dict.get(error, 0) + 1 error_dict[error] = error_dict.get(error, 0) + 1
return "\n".join([("%d x %s" % (v, k), k)[v == 1] for k, v in error_dict.items()]) return "\n".join([("%d x %s" % (v, k), k)[v == 1] for k, v in error_dict.items()])
def remaining_time_str(self): def remaining_time_str(self):
if self.remaining_time: if self.remaining_time:
if self.remaining_time < 3600: if self.remaining_time < 3600:
m, s = divmod(self.remaining_time, 60) m, s = divmod(self.remaining_time, 60)
return "%02d:%02d" % (m, int(s)) return "%02d:%02d" % (m, int(s))
else: else:
h, ms = divmod(self.remaining_time, 3600) h, ms = divmod(self.remaining_time, 3600)
m, s = divmod(ms, 60) m, s = divmod(ms, 60)
return "%d:%02d:%02d" % (h, m, int(s)) return "%d:%02d:%02d" % (h, m, int(s))
else: else:
return "--:--" return "--:--"
class LogFile(LogInterpreter): class LogFile(LogInterpreter):
def __init__(self, log_filename, time_stop): def __init__(self, log_filename, time_stop):
self.filename = log_filename self.filename = log_filename
LogInterpreter.__init__(self, time_stop) LogInterpreter.__init__(self, time_stop)
@staticmethod @staticmethod
def from_htcfile(htcfile, modelpath=None): def from_htcfile(htcfile, modelpath=None):
logfilename = htcfile.simulation.logfile[0] logfilename = htcfile.simulation.logfile[0]
if not os.path.isabs(logfilename): if not os.path.isabs(logfilename):
if modelpath is None: if modelpath is None:
modelpath = htcfile.modelpath modelpath = htcfile.modelpath
logfilename = os.path.join(modelpath, logfilename) logfilename = os.path.join(modelpath, logfilename)
return LogFile(logfilename, htcfile.simulation.time_stop[0]) return LogFile(logfilename, htcfile.simulation.time_stop[0])
def clear(self): def clear(self):
# exist_ok does not exist in Python27 # exist_ok does not exist in Python27
if not os.path.exists(os.path.dirname(self.filename)): if not os.path.exists(os.path.dirname(self.filename)):
os.makedirs(os.path.dirname(self.filename)) #, exist_ok=True) os.makedirs(os.path.dirname(self.filename)) #, exist_ok=True)
try: try:
with open(self.filename, 'w', encoding='utf-8'): with open(self.filename, 'w', encoding='utf-8'):
pass pass
except PermissionError as e: except PermissionError as e:
raise PermissionError(str(e) + "\nLog file cannot be cleared. Check if it is open in another program") raise PermissionError(str(e) + "\nLog file cannot be cleared. Check if it is open in another program")
LogInterpreter.clear(self) LogInterpreter.clear(self)
def update_status(self): def update_status(self):
if not os.path.isfile(self.filename): if not os.path.isfile(self.filename):
self.status = MISSING self.status = MISSING
else: else:
if self.status == UNKNOWN or self.status == MISSING: if self.status == UNKNOWN or self.status == MISSING:
self.status = PENDING self.status = PENDING
s = self.status s = self.status
with open(self.filename, 'rb') as fid: with open(self.filename, 'rb') as fid:
fid.seek(self.position) fid.seek(self.position)
txt = fid.read() txt = fid.read()
self.position += len(txt) self.position += len(txt)
txt = txt.decode(encoding='cp1252', errors='strict') txt = txt.decode(encoding='cp1252', errors='strict')
if txt != "": if txt != "":
LogInterpreter.update_status(self, txt) LogInterpreter.update_status(self, txt)
class LogInfo(LogFile): class LogInfo(LogFile):
def __init__(self, status, pct, remaining_time, lastline): def __init__(self, status, pct, remaining_time, lastline):
self.status = status self.status = status
self.pct = int(pct) self.pct = int(pct)
try: try:
self.remaining_time = float(remaining_time) self.remaining_time = float(remaining_time)
except: except:
self.remaining_time = None self.remaining_time = None
self.lastline = lastline self.lastline = lastline
self.errors = [] self.errors = []
def update_status(self): def update_status(self):
pass pass
''' '''
Created on 24/04/2014 Created on 24/04/2014
@author: MMPE @author: MMPE
''' '''
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from __future__ import unicode_literals from __future__ import unicode_literals
from __future__ import absolute_import from __future__ import absolute_import
from io import open from io import open
from builtins import range from builtins import range
from builtins import int from builtins import int
from future import standard_library from future import standard_library
standard_library.install_aliases() standard_library.install_aliases()
from wetb.hawc2.ae_file import AEFile from wetb.hawc2.ae_file import AEFile
import numpy as np import numpy as np
class PCFile(object): class PCData(object):
"""Read HAWC2 PC (profile coefficients) file
def __init__(self, filename=None):
examples self.sets = {}
-------- if not filename is None:
>>> pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt") self.read_file(filename)
>>> pcfile.CL(21,10) # CL for thickness 21% and AOA=10deg
1.358 def __getitem__(self, key):
>>> pcfile.CD(21,10) # CD for thickness 21% and AOA=10deg
0.0255 '''Retrieve a value according to keys like '1.0.alpha' where the first is the data, the second is the profile and the third is the variable'''
>>> pcfile.CM(21,10) # CM for thickness 21% and AOA=10deg keys=key.split('.')
-0.1103 set_nr = 1
""" pfl_nr = 1
def __init__(self, filename): data_key = 'alpha'
with open (filename) as fid: if len(keys)==2:
lines = fid.readlines() if len(self.sets)!=1:
nsets = int(lines[0].split()[0]) raise KeyError('Must specify the set in the key')
self.pc_sets = {} set_nr = self.sets.keys()[0]
lptr = 1 pfl_nr = int(keys[0])
for nset in range(1, nsets + 1): data_key = keys[1]
nprofiles = int(lines[lptr].split()[0]) elif len(keys)==3:
lptr += 1 set_nr = int(keys[0])
#assert nprofiles >= 2 pfl_nr = int(keys[1])
thicknesses = [] data_key = keys[2]
profiles = [] else:
for profile_nr in range(nprofiles): raise KeyError('Key member nesting only 2 deep')
profile_nr, n_rows, thickness = lines[lptr ].split()[:3] thickness = self.sets[set_nr][0]
profile_nr, n_rows, thickness = int(profile_nr), int(n_rows), float(thickness) profiles = self.sets[set_nr][1]
lptr += 1 pc_data = profiles[pfl_nr]
data = np.array([[float(v) for v in l.split()[:4]] for l in lines[lptr:lptr + n_rows]]) if data_key=='alpha':
thicknesses.append(thickness) return pc_data[:,0]
profiles.append(data) elif data_key=='cl':
lptr += n_rows return pc_data[:,1]
self.pc_sets[nset] = (np.array(thicknesses), profiles) elif data_key=='cd':
return pc_data[:,2]
def _Cxxx(self, thickness, alpha, column, pc_set_nr=1): elif data_key=='cm':
thicknesses, profiles = self.pc_sets[pc_set_nr] return pc_data[:,3]
index = np.searchsorted(thicknesses, thickness) elif data_key=='relative_thickness':
if index == 0: return thickness[pfl_nr]
index = 1 else:
raise KeyError('That variable does not exist')
Cx0, Cx1 = profiles[index - 1:index + 1]
Cx0 = np.interp(alpha, Cx0[:, 0], Cx0[:, column]) def __setitem__(self, key, val):
Cx1 = np.interp(alpha, Cx1[:, 0], Cx1[:, column])
th0, th1 = thicknesses[index - 1:index + 1] '''Retrieve a value according to keys like '1.0.alpha' where the first is the data, the second is the profile and the third is the variable'''
return Cx0 + (Cx1 - Cx0) * (thickness - th0) / (th1 - th0) keys=key.split('.')
set_nr = 1
def _CxxxH2(self, thickness, alpha, column, pc_set_nr=1): pfl_nr = 1
thicknesses, profiles = self.pc_sets[pc_set_nr] data_key = 'alpha'
index = np.searchsorted(thicknesses, thickness) if len(keys)==2:
if index == 0: if len(self.sets)!=1:
index = 1 raise KeyError('Must specify the set in the key')
set_nr = self.sets.keys()[0]
Cx0, Cx1 = profiles[index - 1:index + 1] pfl_nr = int(keys[0])
data_key = keys[1]
Cx0 = np.interp(np.arange(360), Cx0[:,0]+180, Cx0[:,column]) elif len(keys)==3:
Cx1 = np.interp(np.arange(360), Cx1[:,0]+180, Cx1[:,column]) set_nr = int(keys[0])
#Cx0 = np.interp(alpha, Cx0[:, 0], Cx0[:, column]) pfl_nr = int(keys[1])
#Cx1 = np.interp(alpha, Cx1[:, 0], Cx1[:, column]) data_key = keys[2]
th0, th1 = thicknesses[index - 1:index + 1] else:
cx = Cx0 + (Cx1 - Cx0) * (thickness - th0) / (th1 - th0) raise KeyError('Key member nesting only 2 deep')
return np.interp(alpha+180, np.arange(360), cx) thickness = self.sets[set_nr][0]
profiles = self.sets[set_nr][1]
pc_data = profiles[pfl_nr]
def CL(self, thickness, alpha, pc_set_nr=1): if data_key=='relative_thickness':
"""Lift coefficient thickness[pfl_nr]=val
return
Parameters
--------- if len(val)!=len(pc_data):
thickness : float raise IndexError('The value is not the same size as the data')
thickness [5]
alpha : float if data_key=='alpha':
Angle of attack [deg] pc_data[:,0]=val
pc_set_nr : int optional elif data_key=='cl':
pc set number, default is 1, normally obtained from ae-file pc_data[:,1]=val
elif data_key=='cd':
Returns pc_data[:,2]=val
------- elif data_key=='cm':
Lift coefficient : float pc_data[:,3]=val
""" else:
return self._Cxxx(thickness, alpha, 1, pc_set_nr) raise KeyError('That variable does not exist')
def keys(self):
def CL_H2(self, thickness, alpha, pc_set_nr=1): '''Retrieve the keys of the different data'''
return self._CxxxH2(thickness, alpha, 1, pc_set_nr) retval=[]
for I in self.sets.keys():
def CD(self, thickness, alpha, pc_set_nr=1): thickness = self.sets[I][0]
"""Drag coefficient profiles = self.sets[I][1]
for J in range(0,len(profiles)):
Parameters retval.append(str(I)+'.'+str(J)+'.relative_thickness')
--------- retval.append(str(I)+'.'+str(J)+'.alpha')
radius : float retval.append(str(I)+'.'+str(J)+'.cl')
radius [m] retval.append(str(I)+'.'+str(J)+'.cd')
alpha : float retval.append(str(I)+'.'+str(J)+'.cm')
Angle of attack [deg] return retval
pc_set_nr : int optional
pc set number, default is 1, normally obtained from ae-file def get_profile_size(self, set_id, profile_id):
'''This will change the number of rows for a given profile'''
Returns if not set_id in self.sets.keys():
------- raise KeyError('That set does not exist')
Drag coefficient : float thickness = self.sets[set_id][0]
""" profiles = self.sets[set_id][1]
return self._Cxxx(thickness, alpha, 2, pc_set_nr) if profile_id>=len(profiles):
raise KeyError('That profile does not exist')
def CM(self, thickness, alpha, pc_set_nr=1): return len(profiles[profile_id])
return self._Cxxx(thickness, alpha, 3, pc_set_nr)
def resize_profile(self, set_id, profile_id, row_nr):
if __name__ == "__main__": '''This will change the number of rows for a given profile'''
pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt") if not set_id in self.sets.keys():
aefile = AEFile("tests/test_files/NREL_5MW_ae.txt") raise KeyError('That set does not exist')
print (aefile.thickness(36)) thickness = self.sets[set_id][0]
profiles = self.sets[set_id][1]
print (pcfile.CL(21,10)) # CL for thickness 21% and AOA=10deg if profile_id>=len(profiles):
#1.358 raise KeyError('That profile does not exist')
print (pcfile.CD(21,10)) # CD for thickness 21% and AOA=10deg old_data = profiles[profile_id]
#0.0255 old_rows = len(old_data)
print (pcfile.CM(21,10)) # CM for thickness 21% and AOA=10deg # early exit if there is nothing to do
#-0.1103 if old_rows==row_nr:
return
# must generate a new array
new_data = np.zeros((row_nr,4))
asg_rows=old_rows
if row_nr<asg_rows:
asg_rows=row_nr
if asg_rows>0:
new_data[:asg_rows,:]=old_data[:asg_rows,:]
self.sets[set_id][1][profile_id]=new_data
def get_profile_count(self, set_id):
'''This will change the number of profiles in a given set'''
if not set_id in self.sets.keys():
raise KeyError('That set does not exist')
return len(self.sets[set_id][1])
def resize_set(self, set_id, polar_cnt, row_nr=0):
'''This will change the number of profiles in a given set'''
if not set_id in self.sets.keys():
raise KeyError('That set does not exist')
thickness = self.sets[set_id][0]
profiles = self.sets[set_id][1]
# do nothing if there is nothing to do
if polar_cnt==len(profiles):
return
thickness.resize(polar_cnt, refcheck=False)
if polar_cnt<len(profiles):
profiles=profiles[:polar_cnt]
else:
for I in range(len(profiles), polar_cnt):
profiles.append(np.zeros((row_nr,4)))
def get_set_count(self):
return len(self.sets)
def add_set(self, polar_cnt=0, row_nr=0):
I=1
while I in self.sets.keys():
I+=1
polar=np.zeros((row_nr,4))
polars=[polar]*polar_cnt
thickness=np.zeros(polar_cnt)
self.sets[I]=(thickness,polars)
def get_data_str(self):
retval=str(len(self.sets))+'\n'
for set_id, set_data in self.sets.items():
thickness = set_data[0]
profiles = set_data[1]
retval+=str(len(profiles))+'\n'
for I in range(0,len(profiles)):
profile = profiles[I]
thck = thickness[I]
retval+=str(I+1)+' '+str(len(profile))+' '+str(thck)+'\n'
for line in profile:
retval+='%25.17e %25.17e %25.17e %25.17e\n'%(line[0], line[1], line[2], line[3])
return retval
def __str__(self):
return self.get_data_str()
def read_file(self, filename):
with open (filename) as fid:
lines = fid.readlines()
nsets = int(lines[0].split()[0])
self.sets = {}
lptr = 1
for nset in range(1, nsets + 1):
nprofiles = int(lines[lptr].split()[0])
lptr += 1
#assert nprofiles >= 2
thicknesses = []
profiles = []
for profile_nr in range(nprofiles):
profile_nr, n_rows, thickness = lines[lptr ].split()[:3]
profile_nr, n_rows, thickness = int(profile_nr), int(n_rows), float(thickness)
lptr += 1
data = np.array([[float(v) for v in l.split()[:4]] for l in lines[lptr:lptr + n_rows]])
thicknesses.append(thickness)
profiles.append(data)
lptr += n_rows
self.sets[nset] = (np.array(thicknesses), profiles)
class PCFile(object):
"""Read HAWC2 PC (profile coefficients) file
examples
--------
>>> pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt")
>>> pcfile.CL(21,10) # CL for thickness 21% and AOA=10deg
1.358
>>> pcfile.CD(21,10) # CD for thickness 21% and AOA=10deg
0.0255
>>> pcfile.CM(21,10) # CM for thickness 21% and AOA=10deg
-0.1103
"""
def __init__(self, filename):
with open (filename) as fid:
lines = fid.readlines()
nsets = int(lines[0].split()[0])
self.pc_sets = {}
lptr = 1
for nset in range(1, nsets + 1):
nprofiles = int(lines[lptr].split()[0])
lptr += 1
#assert nprofiles >= 2
thicknesses = []
profiles = []
for profile_nr in range(nprofiles):
profile_nr, n_rows, thickness = lines[lptr ].split()[:3]
profile_nr, n_rows, thickness = int(profile_nr), int(n_rows), float(thickness)
lptr += 1
data = np.array([[float(v) for v in l.split()[:4]] for l in lines[lptr:lptr + n_rows]])
thicknesses.append(thickness)
profiles.append(data)
lptr += n_rows
self.pc_sets[nset] = (np.array(thicknesses), profiles)
def _Cxxx(self, thickness, alpha, column, pc_set_nr=1):
thicknesses, profiles = self.pc_sets[pc_set_nr]
index = np.searchsorted(thicknesses, thickness)
if index == 0:
index = 1
Cx0, Cx1 = profiles[index - 1:index + 1]
Cx0 = np.interp(alpha, Cx0[:, 0], Cx0[:, column])
Cx1 = np.interp(alpha, Cx1[:, 0], Cx1[:, column])
th0, th1 = thicknesses[index - 1:index + 1]
return Cx0 + (Cx1 - Cx0) * (thickness - th0) / (th1 - th0)
def _CxxxH2(self, thickness, alpha, column, pc_set_nr=1):
thicknesses, profiles = self.pc_sets[pc_set_nr]
index = np.searchsorted(thicknesses, thickness)
if index == 0:
index = 1
Cx0, Cx1 = profiles[index - 1:index + 1]
Cx0 = np.interp(np.arange(360), Cx0[:,0]+180, Cx0[:,column])
Cx1 = np.interp(np.arange(360), Cx1[:,0]+180, Cx1[:,column])
#Cx0 = np.interp(alpha, Cx0[:, 0], Cx0[:, column])
#Cx1 = np.interp(alpha, Cx1[:, 0], Cx1[:, column])
th0, th1 = thicknesses[index - 1:index + 1]
cx = Cx0 + (Cx1 - Cx0) * (thickness - th0) / (th1 - th0)
return np.interp(alpha+180, np.arange(360), cx)
def CL(self, thickness, alpha, pc_set_nr=1):
"""Lift coefficient
Parameters
---------
thickness : float
thickness [5]
alpha : float
Angle of attack [deg]
pc_set_nr : int optional
pc set number, default is 1, normally obtained from ae-file
Returns
-------
Lift coefficient : float
"""
return self._Cxxx(thickness, alpha, 1, pc_set_nr)
def CL_H2(self, thickness, alpha, pc_set_nr=1):
return self._CxxxH2(thickness, alpha, 1, pc_set_nr)
def CD(self, thickness, alpha, pc_set_nr=1):
"""Drag coefficient
Parameters
---------
radius : float
radius [m]
alpha : float
Angle of attack [deg]
pc_set_nr : int optional
pc set number, default is 1, normally obtained from ae-file
Returns
-------
Drag coefficient : float
"""
return self._Cxxx(thickness, alpha, 2, pc_set_nr)
def CM(self, thickness, alpha, pc_set_nr=1):
return self._Cxxx(thickness, alpha, 3, pc_set_nr)
if __name__ == "__main__":
pcfile = PCFile("tests/test_files/NREL_5MW_pc.txt")
aefile = AEFile("tests/test_files/NREL_5MW_ae.txt")
print (aefile.thickness(36))
print (pcfile.CL(21,10)) # CL for thickness 21% and AOA=10deg
#1.358
print (pcfile.CD(21,10)) # CD for thickness 21% and AOA=10deg
#0.0255
print (pcfile.CM(21,10)) # CM for thickness 21% and AOA=10deg
#-0.1103
from ae_file import AEFile
from pc_file import PCData
from st_file import StOrigData, StFPMData
from htc_file import HTCFile
import inspect
def _get_leafs(obj):
retval = []
if isinstance(obj, dict):
for k, v in obj.items():
retval.extend(_get_leafs(v))
elif isinstance(obj, list):
for v in obj:
retval.extend(_get_leafs(v))
elif isinstance(obj, set):
for v in obj:
retval.extend(_get_leafs(v))
else:
retval.append(obj)
return retval
#aef = AEFile('NREL_5MW_ae.txt')
#print(inspect.getmembers(aef, predicate=inspect.ismethod))
#for mmbr in inspect.getmembers(aef):
# #print(type(mmbr))
# print(mmbr)
#for key in aef.keys():
# print(key)
# print(type(aef[key]))
# print(aef[key])
#print(aef.get_data_str())
#print(aef.__dict__.keys())
#print(aef.__dict__)
#aef = AEFile()
#aef.add_set(11)
#import numpy as np
#aef['1.radius']=np.array([ 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
#aef['1.chord']=np.array([ 1.1, 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1])
#aef['1.relative_thickness']=np.array([ 100.0, 100.0, 90.0, 80.0, 70.0, 60.0, 50.0, 40.0, 30.0, 20.0, 10.0])
#aef['1.pc_set_id']=np.array([ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0])
#print(aef.get_data_str())
def test_AEFile():
#key_list = ['alpha', 'cl', 'cd', 'cm', 'relative_thickness']
aef_orig = AEFile('NREL_5MW_ae.txt')
str_orig = aef_orig.get_data_str()
print('Printing the AE file from the data file')
print(str_orig)
aef = AEFile()
set_cnt = aef_orig.get_set_count()
for I in range(1, set_cnt+1):
aef.add_set()
set_size = aef_orig.get_set_size(I)
aef.resize_set(I, set_size)
for key in aef_orig.keys():
aef[key]=aef_orig[key]
str_scnd = aef.get_data_str()
print('Printing the ae file that was constructed')
print(str_scnd)
if str_orig==str_scnd:
print('They are the same')
else:
print('They are NOT the same')
def test_PCData():
#key_list = ['alpha', 'cl', 'cd', 'cm', 'relative_thickness']
pcf_orig = PCData('NREL_5MW_pc.txt')
str_orig = pcf_orig.get_data_str()
print('Printing the PC file from the data file')
print(str_orig)
pcf = PCData()
set_cnt = pcf_orig.get_set_count()
for I in range(1, set_cnt+1):
pcf.add_set()
profile_cnt = pcf_orig.get_profile_count(I)
pcf.resize_set(I, profile_cnt)
for J in range(0, profile_cnt):
profile_size = pcf_orig.get_profile_size(I, J)
pcf.resize_profile(I, J, profile_size)
for key in pcf_orig.keys():
pcf[key]=pcf_orig[key]
str_scnd = pcf.get_data_str()
print('Printing the pc file that was constructed')
print(str_scnd)
if str_orig==str_scnd:
print('They are the same')
else:
print('They are NOT the same')
def test_StOrigData():
#key_list = ['r', 'm', 'x_cg', 'y_cg', 'ri_x', 'ri_y', 'x_sh', 'y_sh', 'E', 'G', 'I_x', 'I_y', 'I_p', 'k_x', 'k_y', 'A', 'pitch', 'x_e', 'y_e']
stf = StOrigData('DTU_10MW_RWT_Blade_st.dat')
str_orig = stf.get_data_str()
print('Printing the st file from the data file')
print(str_orig)
stf_1 = StOrigData()
set_cnt = stf.get_set_count()
for I in range(1,set_cnt+1):
stf_1.add_set()
sub_set_cnt = stf.get_sub_set_count(I)
for J in range(1,sub_set_cnt+1):
stf_1.add_sub_set(I)
data_size=stf.get_data_size(I,J)
stf_1.resize_data(I, J, data_size)
#for data_id in key_list:
# key='%d.%d.%s'%(I,J,data_id)
# stf_1[key]=stf[key]
for key in stf.keys():
stf_1[key]=stf[key]
str_scnd = stf_1.get_data_str()
print('Printing the st file that was constructed')
print(str_scnd)
if str_orig==str_scnd:
print('They are the same')
else:
print('They are NOT the same')
def test_htcFile():
folder = './play_tests'
htc_file = 'test.htc'
htcf = HTCFile(htc_file, folder)
stf = StOrigData('DTU_10MW_RWT_Blade_st.dat')
pcf = PCData('NREL_5MW_pc.txt')
aef = AEFile('NREL_5MW_ae.txt')
#import pdb; pdb.set_trace()
htcf['new_htc_structure.main_body_blade1.timoschenko_input.st_object'] = stf
htcf['aero.ae_object'] = aef
htcf['aero.pc_object'] = pcf
## This is a listing from the 'inspect' method
#print('Methods')
#method_list = []
#for m in inspect.getmembers(htcf, inspect.ismethod):
# print('\t',m[0])
# method_list.append(m[0])
#print('Other')
#long_values = ['lines', 'empty_htc', '__dict__']
#for m in inspect.getmembers(htcf):
# if m[0] not in method_list:
# if m[0] in long_values:
# print("\t ('%s', ... )"%(m[0]))
# else:
# print("\t",m)
## This is a listing from the all keys method
#for key in htcf.leaf_keys():
# print('LEAF key',key)
# print('LEAF',key,type(htcf[key]))
for key in htcf.all_keys():
#print('ALL',key)
obj = htcf[key]
print('ALL',key,type(obj))
## Lets print the file
#print(str(htcf))
#print('htcf.__dict__.keys():', htcf.__dict__.keys())
#print('htcf.__dict__:', htcf.__dict__)
#import pdb; pdb.set_trace()
#htcf.dll.filename[0]
#files = htcf.get_input_files_and_keys()
#leaf_list = _get_leafs(files)
#for leaf in leaf_list:
# print('\tAn input:', leaf)
#print('===========================================================================================================')
#print(str(htcf))
#print('===========================================================================================================')
#print('get_input_files_and_keys:', files)
#print('input_files:', htcf.input_files())
#print('output_files:', htcf.output_files())
if __name__ == '__main__':
#print('Running test_AEFile')
#test_AEFile()
#print('Running test_PCData')
#test_PCData()
#print('Running test_StOrigData')
#test_StOrigData()
print('Running test_htcFile')
test_htcFile()
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -134,10 +134,10 @@ class LocalSimulationHost(SimulationHost): ...@@ -134,10 +134,10 @@ class LocalSimulationHost(SimulationHost):
self.logFile.update_status() self.logFile.update_status()
def stop(self): def stop(self):
self.simulationThread.stop() self.simulationThread.stop()
if self.simulationThread.is_alive(): if self.simulationThread.is_alive():
self.simulationThread.join() self.simulationThread.join()
print ("simulatino_resources.stop joined") print ("simulatino_resources.stop joined")
......
This diff is collapsed.