Issue
I would like to create a 4D array meant to store a 3D vector field like
U = np.array((3,N,N,N), dtype = float)
where N = 2^n with n = 0,1,2,3,4,...
To do this I tried
U = np.array(np.meshgrid(Ux, Uy, Uz, indexing='ij'), dtype=float)
where Ux, Uy, Uz are the three components of a vector generated using list comprehensions and are of size (N^3,). If I do it this way, I end up with a memory error
I'm not getting this error if I create the U array slightly different, namely:
U = np.array((Ux, Uy, Uz), dtype=float).reshape(3,N,N,N)
I feel like I'm missing something important. Could you please explain what is the difference between the two approaches, and why the former causes the memory error?
Solution
With N=4:
In [131]: N=4; Ux = np.arange(N**3)
In [132]: Ux.shape
Out[132]: (64,)
The 2nd way of combining 3 arrays of this shape:
In [133]: np.array((Ux,Ux,Ux)).shape
Out[133]: (3, 64)
In [134]: np.array((Ux,Ux,Ux)).reshape(3,N,N,N).shape
Out[134]: (3, 4, 4, 4)
The meshgrid shape is much bigger:
In [135]: I,J,K=np.meshgrid(Ux, Ux, Ux, indexing='ij')
In [136]: I.shape
Out[136]: (64, 64, 64)
In [137]: np.array((I,J,K)).shape
Out[137]: (3, 64, 64, 64)
So a memory error is much more likely. If it didn't just kill the process, the error should have told you what it was try to create - e.g. not enough memory to create an array of size 10342Gb etc. And from that you should have been able to deduce what shape it was trying to create.
In [138]: np.array((I,J,K)).nbytes
Out[138]: 3145728
In [146]: 3*((N**3)**3)*4
Out[146]: 3145728
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.