forked from tutajrobert/grot
/
postpro.py
304 lines (253 loc) · 12.4 KB
/
postpro.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Postprocessor module used to plot pictures"""
import math
import os
import numpy
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.colors as clr
from matplotlib.collections import PatchCollection
import tools
import version
import stress
VERS = version.get()
#Matplotlib style functions
PATCH_LINE = 0.0
ALPHA = 1.0
plt.rcParams["font.family"] = "monospace"
plt.rcParams["font.size"] = 9
plt.rcParams["font.weight"] = 100
plt.rcParams["text.hinting_factor"] = 1
plt.rcParams["patch.linewidth"] = PATCH_LINE
plt.rcParams["legend.fontsize"] = 9
def minmax(colors, eles):
"""Min and max scatter plotting"""
#Max
max_value = max(colors)
max_string = "%.3e" % max_value
max_index = colors.index(max_value) + 1
#xlist_max = [eles[max_index][i][0] for i in range(0, 4)]
#ylist_max = [eles[max_index][i][1] for i in range(0, 4)]
x_pos_max = eles[max_index][0][0] + .5
y_pos_max = - eles[max_index][0][1] - .5
#Min
min_value = min(colors)
min_string = "%.3e" % min_value
min_index = colors.index(min_value) + 1
#xlist_min = [eles[min_index][i][0] for i in range(0, 4)]
#ylist_min = [eles[min_index][i][1] for i in range(0, 4)]
x_pos_min = eles[min_index][0][0] + .5
y_pos_min = - eles[min_index][0][1] - .5
return(x_pos_max, y_pos_max, max_string, x_pos_min, y_pos_min, min_string)
def discrete_cmap(ndiv, base_cmap=None):
"""Create an n-bin discrete colormap from the specified input map"""
base = plt.cm.get_cmap(base_cmap)
color_list = base(numpy.linspace(1 - (1 / ndiv), 1 / ndiv, ndiv))
color_list[3] = [.918, .918, .918, 1.]
#For edge colors retrieving: 0 is for max, 1 is for min, 2 is for min label
down_up = [base(numpy.linspace(1 - (0.2 / ndiv), 0.2 / ndiv, ndiv))[0],
base(numpy.linspace(1 - (0.2 / ndiv), 0.2 / ndiv, ndiv))[-1],
base(numpy.linspace(1 - (0.2 / ndiv), 0.2 / ndiv, ndiv))[-1]]
cmap_name = base.name + str(ndiv)
return [clr.LinearSegmentedColormap.from_list(cmap_name, color_list, ndiv),
down_up[0], down_up[1], down_up[2]]
class Prepare():
"""Prepares whole matplotlib enviroment for postprocessing"""
def __init__(self, elements, results):
self.eles = elements
self.res = results
self.ncol = 7
self.init_cmap = "coolwarm_r"
self.min_x, self.min_y = self.eles[1][0][0], -self.eles[1][0][1]
self.max_x, self.max_y = self.min_x, self.min_y
self.elen = len(elements)
self.patch_list = []
for i in self.eles:
xlist = [self.eles[i][0][0]]
ylist = [self.eles[i][0][1]]
#Rectangle of vertex in (x, y) and given width and height
self.patch_list.append(patches.Rectangle((min(xlist), -min(ylist)), 1.0, -1.0))
#Axes limits searching
self.min_x = tools.min_search(min(xlist), self.min_x)
self.min_y = tools.min_search(-max(ylist)-1, self.min_y)
self.max_x = tools.max_search(max(xlist)+1, self.max_x)
self.max_y = tools.max_search(-min(ylist), self.max_y)
#Axes range
diff_x = self.max_x - self.min_x
diff_y = self.max_y - self.min_y
sum_x = self.max_x + self.min_x
sum_y = self.max_y + self.min_y
if diff_x > diff_y:
self.max_y = (sum_y / 2) + (diff_x / 2)
self.min_y = (sum_y / 2) - (diff_x / 2)
else:
self.max_x = (sum_x / 2) + (diff_y / 2)
self.min_x = (sum_x / 2) - (diff_y / 2)
def save_dresults(self, results, proj_name, output_path):
"""Matplotlib script for results viewing
One element == one colour, so element solution
Reduced integration (so there are no other possibilities)"""
colors = []
fig, axes = plt.subplots()
#Nodes coordinates storing
for i in self.eles:
#Displacement results storing
dof1 = (self.eles[i][1] * 2) - 2
dof2 = (self.eles[i][2] * 2) - 2
dof3 = (self.eles[i][3] * 2) - 2
dof4 = (self.eles[i][4] * 2) - 2
dofs = [dof1, dof1 + 1, dof2, dof2 + 1, dof3, dof3 + 1, dof4, dof4 + 1]
#Results choosing and preparing
if results == "x":
colors.append(0.25 * (self.res[dofs[0]] + self.res[dofs[2]] + \
self.res[dofs[4]] + self.res[dofs[6]]))
title = "Displacement in X direction"
elif results == "y":
colors.append(0.25 * (self.res[dofs[1]] + self.res[dofs[3]] + \
self.res[dofs[5]] + self.res[dofs[7]]))
title = "Displacement in Y direction"
elif results in ["mag", "res"]:
colors.append(0.25 * (math.sqrt((self.res[dofs[0]] ** 2) +
(self.res[dofs[1]] ** 2)) +
math.sqrt((self.res[dofs[2]] ** 2) +
(self.res[dofs[3]] ** 2)) +
math.sqrt((self.res[dofs[4]] ** 2) +
(self.res[dofs[5]] ** 2)) +
math.sqrt((self.res[dofs[6]] ** 2) +
(self.res[dofs[7]] ** 2))))
if results == "mag":
title = "Displacement magnitude"
elif results == "res":
title = "Residual displacement magnitude"
plt.title(title)
#Matplotlib functions
dis_cmap = discrete_cmap(self.ncol, self.init_cmap)[0]
p_col = PatchCollection(self.patch_list, cmap=dis_cmap, alpha=ALPHA)
p_col.set_array(numpy.array(colors))
axes.add_collection(p_col)
#Plotting min/max
minmax_data = minmax(colors, self.eles)
x_pos_max = minmax_data[0]
y_pos_max = minmax_data[1]
max_string = minmax_data[2]
x_pos_min = minmax_data[3]
y_pos_min = minmax_data[4]
min_string = minmax_data[5]
logo_legend = plt.scatter(1e6, 1e6, marker="None", label="GRoT> ver. " + VERS)
#Markers border width change
plt.rcParams["patch.linewidth"] = 0.5
if float(min_string) < 0 <= float(max_string):
max_legend = plt.scatter(x_pos_max, y_pos_max, marker="^", c="white",
edgecolors="black", s=52, label="max: " + str(max_string))
else:
max_legend = plt.scatter(x_pos_max, y_pos_max, marker="^", c="white",
edgecolors="black", s=52, label="max: " + str(max_string))
min_legend = plt.scatter(x_pos_min, y_pos_min, marker="v", c="white",
edgecolors="black", s=52, label="min: " + str(min_string))
plt.rcParams["patch.linewidth"] = PATCH_LINE
cbar_lim = [min(colors), max(colors)]
plt.colorbar(p_col, ticks=numpy.linspace(cbar_lim[0], cbar_lim[1], 1 + self.ncol))
p_col.set_clim(cbar_lim)
plt.xlim(self.min_x - 1, self.max_x + 1)
plt.ylim(self.min_y - 1, self.max_y + 1)
axes.axes.xaxis.set_ticks([])
axes.axes.yaxis.set_ticks([])
legend_down = plt.legend(handles=[logo_legend], loc=4, scatterpoints=1)
frame = legend_down.get_frame()
frame.set_edgecolor("white")
plt.gca().add_artist(legend_down)
legend = axes.legend(handles=[max_legend, min_legend], loc=1, scatterpoints=1)
legend.get_texts()[0].set_color(discrete_cmap(self.ncol, self.init_cmap)[3])
legend.get_texts()[1].set_color(discrete_cmap(self.ncol, self.init_cmap)[1])
frame = legend.get_frame()
frame.set_edgecolor("white")
if not os.path.exists(output_path):
os.makedirs(output_path)
plt.savefig(output_path + os.sep + "disp_" + results + ".png", DPI=300)
plt.close()
fig, axes = None, None
return title
def save_sresults(self, results, proj_name, output_path):
"""Matplotlib script for results viewing
One element == one colour, so element solution"""
dev_factor = 2
colors = []
fig, axes = plt.subplots()
counter = -1
#Nodes coordinates storing
for i in range(self.elen):
counter += 1
#Results choosing and preparing
to_plot = stress.results(self.res, results, i)
colors.append(to_plot[0])
plt.title(to_plot[1])
title = to_plot[1]
#Matplotlib functions
dis_cmap = discrete_cmap(self.ncol, self.init_cmap)[0]
#Hardcoded colors of color bar extensions
dis_cmap.set_over(discrete_cmap(self.ncol, self.init_cmap)[2])
dis_cmap.set_under(discrete_cmap(self.ncol, self.init_cmap)[1])
p_col = PatchCollection(self.patch_list, cmap=dis_cmap, alpha=ALPHA)
p_col.set_array(numpy.array(colors))
axes.add_collection(p_col)
#Plotting min/max
minmax_data = minmax(colors, self.eles)
x_pos_max = minmax_data[0]
y_pos_max = minmax_data[1]
max_string = minmax_data[2]
x_pos_min = minmax_data[3]
y_pos_min = minmax_data[4]
min_string = minmax_data[5]
logo_legend = plt.scatter(1e6, 1e6, marker="None", label="GRoT> ver. " + VERS)
plt.rcParams["patch.linewidth"] = 0.5
if float(min_string) < 0 <= float(max_string):
max_legend = plt.scatter(x_pos_max, y_pos_max, marker="^", c="white",
edgecolors="black", s=52, label="max: " + str(max_string))
else:
max_legend = plt.scatter(x_pos_max, y_pos_max, marker="^", c="white",
edgecolors="black", s=52, label="max: " + str(max_string))
min_legend = plt.scatter(x_pos_min, y_pos_min, marker="v", c="white",
edgecolors="black", s=52, label="min: " + str(min_string))
plt.rcParams["patch.linewidth"] = PATCH_LINE
#Color bar limits set to (mean + (dev_factor) * standard deviation)
cbar_lim = [numpy.mean(colors) - (dev_factor * numpy.std(colors)),
numpy.mean(colors) + (dev_factor * numpy.std(colors))]
#Zero has to be in the middle!
if results == "sign_huber":
cbar_lim = [0 - (dev_factor * numpy.std(colors)),
0 + (dev_factor * numpy.std(colors))]
if (numpy.mean(colors) + (dev_factor * numpy.std(colors))) >= float(max_string) and \
(numpy.mean(colors) - (dev_factor * numpy.std(colors))) > float(min_string):
cbar_lim[1] = 1.0005 * float(max_string)
plt.colorbar(p_col, ticks=numpy.linspace(cbar_lim[0], cbar_lim[1], 1 + self.ncol),
extend='min')
elif (numpy.mean(colors) - (dev_factor * numpy.std(colors))) < float(min_string) and \
(numpy.mean(colors) + (dev_factor * numpy.std(colors))) <= float(max_string):
cbar_lim[0] = 0.9995 * float(min_string)
plt.colorbar(p_col, ticks=numpy.linspace(cbar_lim[0], cbar_lim[1], 1 + self.ncol),
extend='max')
else:
plt.colorbar(p_col, ticks=numpy.linspace(cbar_lim[0], cbar_lim[1], 1 + self.ncol),
extend='both')
if (results in ["huber", "eff_strain"]) and (numpy.mean(colors) - \
(dev_factor * numpy.std(colors))) < 0:
cbar_lim[0] = 0
p_col.set_clim(cbar_lim)
plt.xlim(self.min_x - 1, self.max_x + 1)
plt.ylim(self.min_y - 1, self.max_y + 1)
axes.axes.xaxis.set_ticks([])
axes.axes.yaxis.set_ticks([])
legend_down = plt.legend(handles=[logo_legend], loc=4, scatterpoints=1)
frame = legend_down.get_frame()
frame.set_edgecolor("white")
plt.gca().add_artist(legend_down)
legend = axes.legend(handles=[max_legend, min_legend], loc=1, scatterpoints=1)
legend.get_texts()[0].set_color(discrete_cmap(self.ncol, self.init_cmap)[3])
legend.get_texts()[1].set_color(discrete_cmap(self.ncol, self.init_cmap)[1])
frame = legend.get_frame()
frame.set_edgecolor("white")
if not os.path.exists(output_path):
os.makedirs(output_path)
plt.savefig(output_path + os.sep + results + ".png", DPI=300)
plt.close()
fig, axes = None, None
return title