[Numpy-discussion] Reading in a mesh file
Robert Kern
robert.kern at gmail.com
Wed Aug 31 11:06:57 EDT 2016
On Wed, Aug 31, 2016 at 4:00 PM, Florian Lindner <mailinglists at xgm.de>
wrote:
>
> Hello,
>
> I have mesh (more exactly: just a bunch of nodes) description with values
associated to the nodes in a file, e.g. for a
> 3x3 mesh:
>
> 0 0 10
> 0 0.3 11
> 0 0.6 12
> 0.3 0 20
> 0.3 0.3 21
> 0.3 0.6 22
> 0.6 0 30
> 0.6 0.3 31
> 0.6 0.6 32
>
> What is best way to read it in and get data structures like the ones I
get from np.meshgrid?
>
> Of course, I know about np.loadtxt, but I'm having trouble getting the
resulting arrays (x, y, values) in the right form
> and to retain association to the values.
For this particular case (known shape and ordering), this is what I would
do. Maybe throw in a .T or three depending on exactly how you want them to
be laid out.
[~/scratch]
|1> !cat mesh.txt
0 0 10
0 0.3 11
0 0.6 12
0.3 0 20
0.3 0.3 21
0.3 0.6 22
0.6 0 30
0.6 0.3 31
0.6 0.6 32
[~/scratch]
|2> nodes = np.loadtxt('mesh.txt')
[~/scratch]
|3> nodes
array([[ 0. , 0. , 10. ],
[ 0. , 0.3, 11. ],
[ 0. , 0.6, 12. ],
[ 0.3, 0. , 20. ],
[ 0.3, 0.3, 21. ],
[ 0.3, 0.6, 22. ],
[ 0.6, 0. , 30. ],
[ 0.6, 0.3, 31. ],
[ 0.6, 0.6, 32. ]])
[~/scratch]
|4> reshaped = nodes.reshape((3, 3, -1))
[~/scratch]
|5> reshaped
array([[[ 0. , 0. , 10. ],
[ 0. , 0.3, 11. ],
[ 0. , 0.6, 12. ]],
[[ 0.3, 0. , 20. ],
[ 0.3, 0.3, 21. ],
[ 0.3, 0.6, 22. ]],
[[ 0.6, 0. , 30. ],
[ 0.6, 0.3, 31. ],
[ 0.6, 0.6, 32. ]]])
[~/scratch]
|7> x = reshaped[..., 0]
[~/scratch]
|8> y = reshaped[..., 1]
[~/scratch]
|9> values = reshaped[..., 2]
[~/scratch]
|10> x
array([[ 0. , 0. , 0. ],
[ 0.3, 0.3, 0.3],
[ 0.6, 0.6, 0.6]])
[~/scratch]
|11> y
array([[ 0. , 0.3, 0.6],
[ 0. , 0.3, 0.6],
[ 0. , 0.3, 0.6]])
[~/scratch]
|12> values
array([[ 10., 11., 12.],
[ 20., 21., 22.],
[ 30., 31., 32.]])
--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160831/84f69188/attachment.html>
More information about the NumPy-Discussion
mailing list