@@ -128,6 +128,12 @@ def _vf_ground_sky_2d(x, rotation, gcr, pitch, height, max_rows=10):
128
128
is the starting angle of each wedge, ``wedge_angles[1,:,:,:]`` is the
129
129
end angle. [degree]
130
130
"""
131
+ # This function creates large float64 arrays of size
132
+ # (2*len(x)*len(rotation)*len(max_rows)) or ~100 MB for
133
+ # typical time series inputs. This function uses `del` to
134
+ # allow python to recapture intermediate variables and
135
+ # reduce peak memory usage.
136
+
131
137
# handle floats:
132
138
x = np .atleast_1d (x )[:, np .newaxis , np .newaxis ]
133
139
rotation = np .atleast_1d (rotation )[np .newaxis , :, np .newaxis ]
@@ -136,19 +142,30 @@ def _vf_ground_sky_2d(x, rotation, gcr, pitch, height, max_rows=10):
136
142
distance_to_row_centers = (all_k - x ) * pitch
137
143
dy = width * sind (rotation )
138
144
dx = width * cosd (rotation )
145
+
139
146
# angles from x to right edge of each row
140
147
a1 = height + dy
141
148
b1 = distance_to_row_centers + dx
142
149
phi_1 = np .arctan2 (a1 , b1 ) # dimensions: (x, rotation, row)
150
+ del a1 , b1 # reduce max memory usage
151
+
143
152
# angles from x to left edge of each row
144
153
a2 = height - dy
145
154
b2 = distance_to_row_centers - dx
146
155
phi_2 = np .arctan2 (a2 , b2 ) # dimensions: (x, rotation, row)
156
+ del a2 , b2 # reduce max memory usage
157
+
147
158
phi = np .stack ([phi_1 , phi_2 ]) # dimensions: (row edge, x, rotation, row)
148
159
swap = phi_1 > phi_2
160
+ del phi_1 , phi_2 # reduce max memory usage
161
+
149
162
# swap where phi_1 > phi_2 so that phi[0,:,:,:] is the lesser angle
150
163
phi = np .where (swap , phi [::- 1 ], phi )
164
+ del swap # reduce max memory usage
165
+
151
166
# right edge of next row - left edge of previous row
152
167
wedge_vfs = 0.5 * (np .cos (phi [1 , :, :, 1 :]) - np .cos (phi [0 , :, :, :- 1 ]))
153
168
vf = np .sum (np .clip (wedge_vfs , a_min = 0. , a_max = None ), axis = - 1 )
169
+ del wedge_vfs # reduce max memory usage
170
+
154
171
return vf , np .degrees (phi )
0 commit comments