-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdisplay_max_variable_value.py
More file actions
220 lines (171 loc) · 7.33 KB
/
display_max_variable_value.py
File metadata and controls
220 lines (171 loc) · 7.33 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import argparse
import numpy as np
import tecplot as tp
from tecplot.exception import *
from tecplot.constant import *
import tputils
"""
This script will find and display the maximum position for a specific
zone and variable.
Sample useage:
Finding the maximum of a variable (my_var_name) for a zone:
display_max_variable_value.py -c -v my_var_name -z myzone
Finding the maximum of variable my_var_name for each zone of a strand:
display_max_variable_value.py -c -v my_var_name -s 1
NOTE: This script assumes the data has be loaded into the Tecplot GUI,
and it runs in connected mode.
"""
def is_unstructured_zone(zone):
return zone.zone_type in [ZoneType.FEBrick,ZoneType.FELineSeg,ZoneType.FEQuad,ZoneType.FETetra,ZoneType.FETriangle]
def is_polytope_zone(zone):
return zone.zone_type in [ZoneType.FEPolygon,ZoneType.FEPolyhedron]
def find_maximum(zone, variable):
"""
Input: zone and variable
Returns: x,y,z,value
"""
#pull Tecplot 360 data set into Python Numpy
arr = zone.values(variable)[:]
# Find list index with variable max
# Use numpy.argmax() on the variable array to find the node/cell index of the maximum value
max_idx = np.argmax(arr)
# print('Node/Cell # of max value: '+ str(max_idx))
# Get max value in specified zone
maxval = zone.values(variable)[max_idx]
# Get handle to XYZ values arrays
plot = zone.dataset.frame.plot()
x = zone.values(plot.axes.x_axis.variable)
y = zone.values(plot.axes.y_axis.variable)
try: z = zone.values(plot.axes.z_axis.variable)
except: z = 0
location = zone.values(variable).location
if location == ValueLocation.Nodal :
# Getting position of the node
x_pos = x[max_idx]
y_pos = y[max_idx]
try: z_pos = z[max_idx]
except: z_pos = 0
elif location == ValueLocation.CellCentered:
element = max_idx
if is_unstructured_zone(zone):
nodes = zone.nodemap[element]
elif is_polytope_zone(zone):
fm = zone.facemap
nodes = []
# get the number of faces associated with the element
num_faces = fm.num_faces(element)
for f in range(num_faces):
num_nodes = fm.num_nodes(f, element) # Get the number of nodes for each face
for n in range(num_nodes):
node = fm.node(f,n,element) # find all the nodes for all the faces
if node not in nodes: # elimiate duplicates
nodes.append(node)
else:
print('Structured data not supported')
exit()
# Get the XYZ location of the the nodes of the element and calculate average
x_pos = np.average(x[:][nodes])
y_pos = np.average(y[:][nodes])
try: z_pos = np.average(z[:][nodes])
except: z_pos = 0
else:
print("Unknown zone and cell type. Structured CellCentered data not supported")
#print('Location: ({:.3g}, {:.3g}, {:.3g})'.format(x_pos, y_pos, z_pos))
#print('Max value: {:.5g}'.format(maxval))
return x_pos, y_pos, z_pos, maxval
def create_scatter_point_zone(dataset, x_pos, y_pos, z_pos,
znname='MAXVARCALC', sol_time=0, strand=0):
pointzone = dataset.add_ordered_zone(znname, 1,
solution_time=sol_time,
strand_id=strand)
print("New Point Zone created.")
axes = dataset.frame.plot().axes
pointzone.values(axes.x_axis.variable)[0] = x_pos
pointzone.values(axes.y_axis.variable)[0] = y_pos
try: pointzone.values(axes.z_axis.variable)[0] = z_pos
except: None
return pointzone
def highlight_maximum(frame, pointzone, variable, maxvar):
plot = frame.plot()
#plot scatter for new point zone
plot.fieldmaps().scatter.show = False
plot.show_scatter = True
fmap = plot.fieldmap(pointzone)
fmap.show = True
scatter = fmap.scatter
scatter.show=True
scatter.size=2
scatter.color=Color.RedOrange
scatter.symbol().shape=GeomShape.Diamond
scatter.line_thickness=0.3
#View Fit Surfaces
try: plot.view.fit_surfaces(consider_blanking=True)
except: None
def draw_text_single_timestep(frame,pointzone, variable, maxvar):
#draw text box displaying max variable
print("Adding text on plot for maximum value.")
text = list(filter(lambda t: t.text_string.startswith(pointzone.name), frame.texts()))
msg = '{}: {:.3f}'.format(pointzone.name, maxvar)
if text:
text[-1].text_string = msg
else:
frame.add_text(msg, (3, 97), bold=False)
def draw_text_multiple_timestep(frame, aux_str, variable):
#draw text box displaying max variable from aux data
active_offset = len(list(plot.active_fieldmaps)) #get last active fieldmaps
msg = "{} maximum: &(AUXZONE[ACTIVEOFFSET={}]:{})".format(variable.name, active_offset, aux_str)
text = list(filter(lambda t: t.text_string.startswith(variable.name), frame.texts()))
if text:
text[-1].text_string = msg
else:
frame.add_text(msg, (3, 97), bold=False)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', action='store_true', help='Connected mode')
parser.add_argument('-v', '--var', required=True, help='Zone name or 0 based index')
parser.add_argument('-z', '--zone', help='Zone name or 0 based index')
parser.add_argument('-s', '--strand', type=int, help='Strand ID for transient zones')
args = parser.parse_args()
if args.c:
tp.session.connect()
frame = tp.active_frame()
dataset = frame.dataset
plot = frame.plot()
try: var = int(args.var)
except: var = args.var
try:
variable = dataset.variable(var)
print(variable)
except:
print('unrecognized variable')
exit()
if args.zone:
try: zn = int(args.zone)
except: zn = args.zone
try: zone = dataset.zone(zn)
except:
print('unrecognized variable')
exit()
x,y,z, maxvar = find_maximum(zone, variable)
znname = "Maximum " + variable.name
pointzone = create_scatter_point_zone(dataset, x,y,z, znname)
highlight_maximum(frame, pointzone, variable, maxvar)
draw_text_single_timestep(frame, pointzone, variable, maxvar)
elif args.strand:
# loop through zones
# Get zones associated with the strand
zones_by_strands = tputils.get_zones_by_strand(dataset)
zns = zones_by_strands[args.strand]
aux_str = 'max_{}'.format(variable.name) # Create aux data string name
strand = tputils.max_strand(dataset) +1 # Strand number for the new point zones
for zn in zns:
x,y,z, maxvar = find_maximum(zn, variable)
print (zn.name, x,y,z, maxvar)
znname = "Maximum {} - t = {}".format(variable.name, zn.solution_time)
pointzone = create_scatter_point_zone(dataset, x,y,z, znname, zn.solution_time, strand)
pointzone.aux_data[aux_str] = str(maxvar)
highlight_maximum(frame, pointzone, variable, maxvar)
draw_text_multiple_timestep(frame,aux_str,variable)
else:
print('Unrecognized zone')
exit()