1
+ def strainrate_calculator (x_axis ,y_axis ,u ,v ,strain_selections ,debug_flag = 0 ):
2
+ '''
3
+ % (C) Nick Holschuh - Penn State University - 2016 ([email protected] )
4
+ % This calculates the maximum longitudinal strain ("along flow"), the
5
+ % minimum longitudinal strain ("across flow"), and the maximum shear, using
6
+ % properties of the eigenvectors and eigenvalues of DV tensor. This
7
+ % solution is presented in (Hackl et al. 2009), although it is foundational
8
+ % theory in continuum mechanics.
9
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
10
+ % The inputs are as follows:
11
+ %
12
+ % x_axis - The xaxis values that are associated with the velocity grids
13
+ % y_axis - The yaxis values that are associated with the velocity grids
14
+ % u - The velocity component in the orientation of the x axis
15
+ % v - The velocity component in the orientation of the y axis
16
+ % strain_selections - This input allows you to select which of the output
17
+ % products you would like to retain. It should be a matrix with 0s and 1s
18
+ % in the following positions, to indicate which values you are most
19
+ % interested in.
20
+ %%%%%%%
21
+ % 1 - Max Longitudinal Strain Rate
22
+ % 2 - Min Longitudinal Strain Rate
23
+ % 3 - Max Shear Strain Rate
24
+ % 4 - Max Longitudinal Orientation
25
+ % 5 - Min Longitudinal Orientation
26
+ % 6 - Rotation Matrix
27
+ % 7 - Vertical Strain Rate (assuming incompressibility)
28
+ %
29
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30
+ % The Output is as follows:
31
+ % sr - a Cell array, containing 6 entries.
32
+ % 1 - The principle longitudinal strain rate (scaler)
33
+ % 2 - The secondary longitudinal strian rate (scaler)
34
+ % 3 - The maximum shear strain rate (scaler)
35
+ % 4 - The orientation of maximum longitudinal strain (vector)
36
+ % 5 - The orientation of minimum longitudinal strain (vector)
37
+ % 6 - The rotation matrix (matrix, with positions corresponding to:
38
+ % [ 1 2;
39
+ % 3 4 ]
40
+ % 7 - The vertical strain rate value
41
+ %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42
+ %%
43
+ '''
44
+
45
+ import numpy as np
46
+
47
+ ##################################################################
48
+ ##################################### Variable Initialization
49
+ while len (strain_selections ) < 7 :
50
+ strain_selections .append (0 )
51
+
52
+ # Initialize the principle eigenvector matrix (1), if desired
53
+ if strain_selections [0 ] == 1 :
54
+ e1 = np .zeros (u .shape + (1 ,))
55
+ else :
56
+ e1 = 0
57
+
58
+ # Initialize the principle eigenvector matrix (2), if desired
59
+ if strain_selections [1 ] == 1 :
60
+ e2 = np .zeros (u .shape + (1 ,))
61
+ else :
62
+ e2 = 0
63
+
64
+ # Initialize the principle eigenvector matrix (3), if desired
65
+ if strain_selections [2 ] == 1 :
66
+ ss = np .zeros (u .shape + (1 ,))
67
+ else :
68
+ ss = 0
69
+
70
+ # Initialize the principle eigenvector matrix (4), if desired
71
+ if strain_selections [3 ] == 1 :
72
+ ev1 = np .zeros (u .shape + (2 ,))
73
+ else :
74
+ ev1 = 0
75
+
76
+ # Initialize the secondary eigenvector matrix (5), if desired
77
+ if strain_selections [4 ] == 1 :
78
+ ev2 = np .zeros (u .shape + (2 ,))
79
+ else :
80
+ ev2 = 0
81
+
82
+ # Initialize the rotation matrix (6), if desired
83
+ if strain_selections [5 ] == 1 :
84
+ rm = np .zeros (u .shape + (4 ,))
85
+ else :
86
+ rm = 0
87
+
88
+ # Initialize the vertical strain rate matrix (7), if desired
89
+ if strain_selections [6 ] == 1 :
90
+ vs = np .zeros (u .shape + (1 ,))
91
+ else :
92
+ vs = 0
93
+ ##################################################################
94
+
95
+ # Generate the change in velocity components
96
+ du_y , du_x = np .gradient (u )
97
+ dv_y , dv_x = np .gradient (v )
98
+
99
+ dx = x_axis [1 ] - x_axis [0 ]
100
+ dy = y_axis [1 ] - y_axis [0 ]
101
+
102
+ dudx = du_x / dx
103
+ dudy = du_y / dy
104
+ dvdx = dv_x / dx
105
+ dvdy = dv_y / dy
106
+
107
+ # Fill in the components of the strain rate tensor
108
+ shear1 = 0.5 * (dudy + dvdx )
109
+ rotation1 = 0.5 * (dudy - dvdx )
110
+ rotation2 = 0.5 * (dvdx - dudy )
111
+
112
+ zeromat = np .zeros_like (shear1 )
113
+
114
+ # Initialize arrays to store results
115
+ e1 = np .zeros_like (zeromat )
116
+ e2 = np .zeros_like (zeromat )
117
+ ss = np .zeros_like (zeromat )
118
+ ev1 = np .zeros ((len (zeromat ), len (zeromat [0 ]), 2 ))
119
+ ev2 = np .zeros ((len (zeromat ), len (zeromat [0 ]), 2 ))
120
+ rm = np .zeros ((len (zeromat ), len (zeromat [0 ]), 4 ))
121
+ vs = np .zeros_like (zeromat )
122
+
123
+ for i in range (len (zeromat )):
124
+ for j in range (len (zeromat [0 ])):
125
+ # Compute eigenvalues and eigenvectors
126
+ calc_mat = [[dudx [i , j ], 0.5 * (dudy [i , j ] + dvdx [i , j ])],
127
+ [0.5 * (dudy [i , j ] + dvdx [i , j ]), dvdy [i , j ]]]
128
+
129
+ #print(calc_mat)
130
+ if strain_selections [3 ] == 1 or strain_selections [4 ] == 1 :
131
+ e_val , e_vec = np .linalg .eig (calc_mat )
132
+ else :
133
+ e_val = np .linalg .eigvals ([[dudx [i , j ], 0.5 * (dudy [i , j ] + dvdx [i , j ])],
134
+ [0.5 * (dudy [i , j ] + dvdx [i , j ]), dvdy [i , j ]]])
135
+
136
+ #print(e_val)
137
+ #print('-----------------')
138
+
139
+ # Store results based on strain_selections
140
+ if strain_selections [0 ] == 1 :
141
+ e1 [i , j ] = e_val [0 ]
142
+ if strain_selections [1 ] == 1 :
143
+ e2 [i , j ] = e_val [1 ]
144
+ if strain_selections [2 ] == 1 :
145
+ ss [i , j ] = (np .max (e_val ) - np .min (e_val )) / 2
146
+ if strain_selections [3 ] == 1 :
147
+ ev1 [i , j ] = e_vec [:, 0 ]
148
+ if strain_selections [4 ] == 1 :
149
+ ev2 [i , j ] = e_vec [:, 1 ]
150
+ if strain_selections [5 ] == 1 :
151
+ rm [i , j ] = [0 , rotation1 [i , j ], rotation2 [i , j ], 0 ]
152
+ if strain_selections [6 ] == 1 :
153
+ vs [i , j ] = - e_val [0 ] - e_val [1 ]
154
+
155
+ # Debugging
156
+ if debug_flag == 1 :
157
+ test_vs = - dudx - dudy
158
+ import matplotlib .pyplot as plt
159
+ plt .imshow (vs - test_vs )
160
+ plt .clim (- 0.01 , 0.01 )
161
+ plt .colorbar ()
162
+
163
+ # Store results
164
+ sr = [e1 , e2 , ss , ev1 , ev2 , rm , vs ]
165
+ sr_meta = ['Max Longitudinal Strain Rate' ,
166
+ 'Min Longitudinal Strain Rate' ,
167
+ 'Max Shear Strain Rate' ,
168
+ 'Max Longitudinal Orientation' ,
169
+ 'Min Longitudinal Orientation' ,
170
+ 'Rotation Matrix' ,
171
+ 'Vertical Strain Rate' ]
172
+
173
+ return sr ,sr_meta
0 commit comments