-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturbineLineSample.py
More file actions
executable file
·204 lines (152 loc) · 7.66 KB
/
Copy pathturbineLineSample.py
File metadata and controls
executable file
·204 lines (152 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
import logging
LEVEL = logging.INFO
logger = logging.getLogger(__name__)
import sys
import argparse
import numpy as np
import utils
import constants as const
QUANTITIES_TO_KEEP = {'UAvg', 'uuPrime2', 'kResolved'}
################################################################################
def turbineLineSample(casename, time, overwrite=False):
"""Initial processing of lineSample data. For older version of OpenFOAM,
this included separating files into each quantity. For newer versions, this
step is unnecessary. Vertical lines are expected to have a single z
coordinate column which is preserved. Horizontal lines are expected to have
x y and z coordinates, and these are used to calculate a distance from the
center of the line.
Intended order of operations:
turbineLineSample -> turbineLineSampleTransform -> turbineLineSampleFluxes"""
casedir = const.CASES_DIR / casename
if not casedir.is_dir():
logger.warning(f'{casename} directory does not exist. Skipping.')
return
sowfatoolsdir = casedir / const.SOWFATOOLS_DIR
utils.create_directory(sowfatoolsdir)
logfilename = 'log.turbineLineSample'
utils.configure_function_logger(sowfatoolsdir/logfilename, level=LEVEL)
############################################################################
logger.info(f'Processing lineSample for case {casename}')
readdir = casedir / 'postProcessing/lineSample' / time
if not readdir.is_dir():
logger.warning(f'{readdir.name} directory does not exist. Skipping.')
return
writedir = sowfatoolsdir / 'lineSample'
utils.create_directory(writedir)
filepaths = [file for file in readdir.iterdir()]
logger.debug(f'Found {len(filepaths)} filenames')
############################################################################
for filepath in filepaths:
logger.debug(f'Processing file {filepath.name}')
# Account for possible gzipped files
if filepath.name.endswith('.gz'):
filestem = filepath.name.removesuffix('.xy.gz')
else:
filestem = filepath.name.removesuffix('.xy')
# Separate information in filename
fileparts = filestem.split('_')
# Account for half diamters, e.g. lineV0_5
if fileparts[1] == '5':
linename = '_'.join(fileparts[:2])
quantities_found = fileparts[2:]
else:
linename = fileparts[0]
quantities_found = fileparts[1:]
# Account for p_rgh and p_rghAvg
finished = False
start_idx = 0
while not finished:
if len(quantities_found) == 1:
break
for i in range(start_idx,len(quantities_found)-1):
if (quantities_found[i] == 'p'
and quantities_found[i+1].startswith('rgh')):
quantities_found[i] += quantities_found[i+1]
quantities_found.pop(i+1)
start_idx = i+1
break
if i == len(quantities_found)-2:
finished = True
# Identify scalar, vector or tensor
scalar = False
vector = False
tensor = False
if quantities_found[0] in const.SCALAR_QUANTITIES:
scalar = True
quantities_to_keep = set.intersection(QUANTITIES_TO_KEEP,
const.SCALAR_QUANTITIES)
elif quantities_found[0] in const.VECTOR_QUANTITIES:
vector = True
quantities_to_keep = set.intersection(QUANTITIES_TO_KEEP,
const.VECTOR_QUANTITIES)
elif quantities_found[0] in const.SYMMTENSOR_QUANTITIES:
tensor = True
quantities_to_keep = set.intersection(QUANTITIES_TO_KEEP,
const.SYMMTENSOR_QUANTITIES)
########################################################################
if not overwrite:
files_exist = True
for quantity in quantities_to_keep:
writefile = (writedir / f'{linename}_{quantity}_{time}.gz')
if not writefile.exists():
files_exist = False
if files_exist:
logger.warning(f'Files exist. skipping. ')
continue
data = np.loadtxt(filepath)
if 'V' in linename:
# Vertical lines contain only z coordinate in first column.
# No transformation needed.
distance = data[:,0]
elif 'H' in linename:
# Horizontal lines contain x y and z coordinates. We use the x and
# y components to transform into a distance from centreline.
# We do not need the z component.
linelength = np.sqrt( (data[-1,0] - data[0,0])**2 # x_end-x_start
+ (data[-1,1] - data[0,1])**2) # y_end-y_start
distance = np.sqrt( (data[:,0] - data[0,0])**2 # x - s_start
+ (data[:,1] - data[0,1])**2) # y - y_start
distance -= linelength/2 # shift distance to center line
# Make data into same format as vertical lines for convenience.
data = data[:,2:]
for quantity in quantities_to_keep:
writefile = (writedir / f'{linename}_{quantity}_{time}.gz')
if writefile.exists() and not overwrite:
logger.warning(f'{writefile.name} exists. skipping. ')
continue
logger.info(f'Processing quantity {quantity}')
if quantity not in quantities_found:
logger.warning(f'{quantity} not found. Skipping')
continue
if scalar:
idx = quantities_found.index(quantity) + 1
data_to_write = np.column_stack((distance,data[:,idx]))
elif vector:
idx = (quantities_found.index(quantity) * 3) + 1
data_to_write = np.column_stack((distance,data[:,idx]))
for i in range(1,3):
data_to_write = np.column_stack((data_to_write,data[:,idx+i]))
elif tensor:
idx = (quantities_found.index(quantity) * 6) + 1
data_to_write = np.column_stack((distance,data[:,idx]))
for i in range(1,6):
data_to_write = np.column_stack((data_to_write,data[:,idx+i]))
logger.debug(f'Saving file {writefile.name}')
np.savetxt(writefile,data_to_write,fmt='%.11e')
################################################################################
if __name__=='__main__':
utils.configure_root_logger(level=LEVEL)
logger.debug(f'Python version: {sys.version}')
logger.debug(f'Python executable location: {sys.executable}')
description = """Intial Processing of lineSample Data.
See function doc string for more detail."""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('cases', help='cases to perform analysis for',
nargs='+')
parser.add_argument('-t','--time', help='time to perfrom analysis for',
required=True)
args = parser.parse_args()
logger.debug(f'Parsed Command Line Arguments: {args}')
for casename in args.cases:
turbineLineSample(casename,args.time)