Error Reading Dimension Information From Vtr File

Earlier starting, you lot are strongly advised to read the VTK file formats documentation hither.

Introduction

The purpose of this wiki is to show how one can write VTK files using python. Why using python ? Because it's beautiful & powerful, of form ! ;-) Apparently, y'all can employ your favourite programming language to write such files. In the following, the data you desire to write to a VTK file are supposed to arise from scipy/numpy arrays. Please browse scipy.org to get more data if needed.

Before writing VTK files, y'all have to know that there are two kinds of VTK files: "legacy" VTK files and "XML" VTK files. The first kind is quite obsolete now, but still widely used. Although it is easier to write than the XML kind (sympathise: with low-level instructions like "printf" in C or "print" in python), this wiki will focus on the last kind, the XML format.

Datasets format

Note: Well, I accept initiated this wiki and never found time to write some materials to it :-(

Meanwhile, Gaël wrote a actually cool doc on VTK Information Structures, in the Mayavi Users Guide.

These pages show how one can easily write snipped lawmaking to create theses datasets, using TVTK. And believe me, this is far easier to write them using TVTK than "pure" VTK ;-)

But if one of yous, wikiers, think that this link is irrelevant (not "pure" VTK), no problem, you can remove it, I won't arraign you ;-)

Thank you,


Every bit you should already be aware if you have read the VTK file formats documentation, VTK can handle several kinds of datasets, for points information and cells data. The primary are:

  • StructuredPoints (aka ImageData, come across later);
  • RectilinearGrid;
  • StructuredGrid;
  • UnstructuredGrid;
  • PolyData.

Briefly:

  • StructuredPoints is the "simplest" format: you only have to set the mesh dimensions (nx, ny, nz), mesh origin (x0, y0, z0) and prison cell dimensions (dx, dy, dz). Thus, points data are regularly and uniformly spaced.

StructuredPoints dataset

  • RectilinearGrid is as well a regularly spaced points data just spacing can be non compatible. Thus, you lot have to specify the nodes coordinates along the three axes, Ox, Oy & Oz.

RectilinearGrid dataset

  • StructuredGrid is a not regular and not uniform points data. You have to specify the coordinates for all mesh nodes.

StructuredGrid dataset

  • UnstructuredGrid is a non regular and not uniform points data, similar the StructuredGrid, but can handle all prison cell types (see VTK file formats documentation for cell types bachelor).

File:Unstructured grid.png

  • PolyData can be used for whatever polygonal information. Thus, you accept to specify the coordinates for the points and the polygons defined by the points index.

PolyData dataset

"legacy"

Starting time, the "legacy" VTK format has only one file extension for all kind of datasets format: ".vtk", which is non the case for the "XML" every bit you lot will see later. Writing "legacy" VTK files is well documented in the VTK file formats documentation and is quite straightforward. You accept but to set a few parameters, such as:

  • file format (ASCII or binary);
  • data format (char, int, float, double, etc...)
  • specific information related to the kind of the dataset you want to use.

Annotation: The following result is frequently asked on the vtkusers mailing-list, so please read information technology carefully before complaining almost errors while reading your VTK file subsequently you wrote it. If you desire to write your data in binary format in your "legacy" VTK file (XML files are not concerned), you accept to write information technology in BigEndian, and non in LittleEndian, even if you lot use an intel or amd box which are known to be in LittleEndian. So, you have to convert your data in BigEndian earlier writing them in your VTK file. This is explicitly mentioned in the VTK file formats documentation.

Using python, you can write "legacy" VTK files with your ain methods or use pyvtk for VTK version two.0 format.

"XML Format"

The new XML format is somewhat more than complex, but ultimately more flexible. Different types of data have different file extensions. Recently, a simple open-source Python form for writing unstructured data in the VTK XML format has been released. Although this class is probably besides specialized for general-purpose utilise, it is an easily understood example that you can use to create your ain classes. It utilized the built-in Python tool xml.dom.minidom to simplify the creation of XML. It as well generates a .pvd file, which allows a serial of VTK files to be read into Paraview simultaneously.

EVTK

EVTK (Export to VTK) package allows exporting data to binary VTK files. EVTK depends only on common libraries, so information technology is easy to install in dissimilar systems. EVTK is released under a BSD license.

EVTK has been released in ii languages: Python and Java.

PyEVTK [1] is composed of a set of pure Python files and it merely requires Numpy as additional requirement. EVTK provides low and high level interfaces. While the low level interface can exist used to consign data that is stored in whatever blazon of container, the loftier level functions brand easy to consign data stored in Numpy arrays.

JEVTK [two] is a newer version of EVTK that is only equanimous of pure Java files and it does not requiere whatever additional library. It provides about the aforementioned interface every bit PyEVTK with depression and loftier-level interfaces.

Examples

The post-obit examples testify how to use the loftier level functions to export data stored in unlike grid format (run across above for grid definitions).

Epitome

To export information stored in a regular Cartesian filigree using PyEVTK:

                        from            evtk.hl            import            imageToVTK            <            br            >            import            numpy            as            np            <            br            >            # Dimensions <br>            nx            ,            ny            ,            nz            =            6            ,            half-dozen            ,            two            <            br            >            ncells            =            nx            *            ny            *            nz            <            br            >            npoints            =            (            nx            +            1            )            *            (            ny            +            1            )            *            (            nz            +            1            )            <            br            >            # Variables <br>            pressure            =            np            .            random            .            rand            (            ncells            )            .            reshape            (            (            nx            ,            ny            ,            nz            ),            order            =            'C'            )            <            br            >            temp            =            np            .            random            .            rand            (            npoints            )            .            reshape            (            (            nx            +            1            ,            ny            +            i            ,            nz            +            1            ))            <            br            >            imageToVTK            (            "./epitome"            ,            cellData            =            {            "pressure"            :            force per unit area            },            pointData            =            {            "temp"            :            temp            }            )          

A similar case using JEVTK:

                        var            nc            =            10            ;            var            ncells            =            new            int            []            {            nc            ,            nc            ,            nc            };            var            nnpoints            =            (            nc            +            ane            )            *            (            nc            +            i            )            *            (            nc            +            ane            );            var            nncells            =            nc            *            nc            *            nc            ;            var            origin            =            new            double            []{            0.0            ,            0.0            ,            0.0            };            var            spacing            =            new            double            []{            1.0            ,            1.0            ,            i.0            };            var            cellData            =            makeCellData            ();            var            pointData            =            makePointData            ();            // Add a variable associated to each jail cell and fill information technology with some random values            var            temp            =            new            double            [            nncells            ];            var            rnd            =            new            Random            ();            for            (            int            i            =            0            ;            i            <            nncells            ;            i            ++)            {            temp            [            i            ]            =            rnd            .            nextDouble            ();            }            cellData            .            addData            (            "temp"            ,            temp            );            imageToVTK            (            "paradigm"            ,            ncells            ,            origin            ,            spacing            ,            cellData            ,            pointData            );          

Structured grid

To export data stored in a logically structured grid using PyEVTK:

                        from            evtk.hl            import            gridToVTK            <            br            >            import            numpy            every bit            np            <            br            >            import            random            every bit            rnd            <            br            >            # Dimensions <br>            nx            ,            ny            ,            nz            =            vi            ,            6            ,            2            <            br            >            60            ,            ly            ,            lz            =            one.0            ,            1.0            ,            ane.0            <            br            >            dx            ,            dy            ,            dz            =            lx            /            nx            ,            ly            /            ny            ,            lz            /            nz            <            br            >            ncells            =            nx            *            ny            *            nz            <            br            >            npoints            =            (            nx            +            ane            )            *            (            ny            +            one            )            *            (            nz            +            1            )            <            br            >            # Coordinates <br>            X            =            np            .            arange            (            0            ,            60            +            0.ane            *            dx            ,            dx            ,            dtype            =            'float64'            )            <            br            >            Y            =            np            .            arange            (            0            ,            ly            +            0.1            *            dy            ,            dy            ,            dtype            =            'float64'            )            <            br            >            Z            =            np            .            arange            (            0            ,            lz            +            0.1            *            dz            ,            dz            ,            dtype            =            'float64'            )            <            br            >            x            =            np            .            zeros            ((            nx            +            1            ,            ny            +            1            ,            nz            +            ane            ))            <            br            >            y            =            np            .            zeros            ((            nx            +            i            ,            ny            +            1            ,            nz            +            1            ))            <            br            >            z            =            np            .            zeros            ((            nx            +            i            ,            ny            +            one            ,            nz            +            i            ))            <            br            >            # Nosotros add some random fluctuation to make the filigree more than interesting <br>            for            k            in            range            (            nz            +            1            ):            <            br            >            for            j            in            range            (            ny            +            1            ):            for            i            in            range            (            nx            +            i            ):            <            br            >            ten            [            i            ,            j            ,            k            ]            =            X            [            i            ]            +            (            0.v            -            rnd            .            random            ())            *            0.two            *            dx            <            br            >            y            [            i            ,            j            ,            k            ]            =            Y            [            j            ]            +            (            0.v            -            rnd            .            random            ())            *            0.2            *            dy            <            br            >            z            [            i            ,            j            ,            k            ]            =            Z            [            k            ]            +            (            0.v            -            rnd            .            random            ())            *            0.2            *            dz            <            br            >            # Variables <br>            pressure            =            np            .            random            .            rand            (            ncells            )            .            reshape            (            (            nx            ,            ny            ,            nz            ))            <            br            >            temp            =            np            .            random            .            rand            (            npoints            )            .            reshape            (            (            nx            +            i            ,            ny            +            one            ,            nz            +            i            ))            <            br            >            gridToVTK            (            "./structured"            ,            x            ,            y            ,            z            ,            cellData            =            {            "pressure"            :            pressure            },            pointData            =            {            "temp"            :            temp            })          

A similar example using JEVTK (see image of exported grid below):

                        var            nc            =            5            ;            var            dx            =            i.0            ;            var            dy            =            i.0            ;            var            dz            =            2.0            ;            var            nncells            =            nc            *            nc            *            nc            ;            var            nnpoints            =            (            nc            +            ane            )            *            (            nc            +            1            )            *            (            nc            +            1            );            // Fix coordinates of nodes with some random perturbation            var            10            =            new            double            [            nc            ][            nc            ][            nc            ];            var            y            =            new            double            [            nc            ][            nc            ][            nc            ];            var            z            =            new            double            [            nc            ][            nc            ][            nc            ];            var            rnd            =            new            Random            ();            var            factor            =            0.ii            ;            for            (            int            k            =            0            ;            thousand            <            nc            ;            one thousand            ++)            {            for            (            int            j            =            0            ;            j            <            nc            ;            j            ++)            {            for            (            int            i            =            0            ;            i            <            nc            ;            i            ++)            {            10            [            i            ][            j            ][            k            ]            =            dx            *            i            +            rnd            .            nextDouble            ()            *            dx            *            cistron            ;            y            [            i            ][            j            ][            k            ]            =            dy            *            j            +            rnd            .            nextDouble            ()            *            dy            *            gene            ;            z            [            i            ][            j            ][            thousand            ]            =            dz            *            m            +            rnd            .            nextDouble            ()            *            dz            *            gene            ;            }            }            }            var            cellData            =            EVTK            .            makeCellData            ();            var            pointData            =            EVTK            .            makePointData            ();            // Add a variable associated to each prison cell and fill it with random values            var            temp            =            new            double            [            nncells            ];            for            (            int            i            =            0            ;            i            <            nncells            ;            i            ++)            {            temp            [            i            ]            =            rnd            .            nextDouble            ();            }            cellData            .            addData            (            "temperature"            ,            temp            );            // Add together a variable associated to each node and fill information technology with random values            var            pressure level            =            new            double            [            nnpoints            ];            for            (            int            i            =            0            ;            i            <            nnpoints            ;            i            ++)            {            pressure            [            i            ]            =            rnd            .            nextDouble            ();            }            pointData            .            addData            (            "pressure"            ,            force per unit area            );            EVTK            .            structuredGridToVTK            (            "structured"            ,            ten            ,            y            ,            z            ,            cellData            ,            pointData            );          

ExStructuredrGrid.png

Rectilinear grid

To export data stored in a rectilinear filigree (AKA irregular Cartesian grid) using PyEVTK:

                        from            evtk.hl            import            gridToVTK            <            br            >            import            numpy            equally            np            <            br            >            # Dimensions <br>            nx            ,            ny            ,            nz            =            six            ,            six            ,            2            <            br            >            60            ,            ly            ,            lz            =            1.0            ,            1.0            ,            1.0            <            br            >            dx            ,            dy            ,            dz            =            lx            /            nx            ,            ly            /            ny            ,            lz            /            nz            <            br            >            ncells            =            nx            *            ny            *            nz            <            br            >            npoints            =            (            nx            +            1            )            *            (            ny            +            ane            )            *            (            nz            +            one            )            <            br            >            # Coordinates <br>            x            =            np            .            arange            (            0            ,            lx            +            0.1            *            dx            ,            dx            ,            dtype            =            'float64'            )            <            br            >            y            =            np            .            arange            (            0            ,            ly            +            0.one            *            dy            ,            dy            ,            dtype            =            'float64'            )            <            br            >            z            =            np            .            arange            (            0            ,            lz            +            0.1            *            dz            ,            dz            ,            dtype            =            'float64'            )            <            br            >            # Variables <br>                        pressure            =            np            .            random            .            rand            (            ncells            )            .            reshape            (            (            nx            ,            ny            ,            nz            ))            <            br            >            temp            =            np            .            random            .            rand            (            npoints            )            .            reshape            (            (            nx            +            one            ,            ny            +            one            ,            nz            +            1            ))            <            br            >            gridToVTK            (            "./rectilinear"            ,            x            ,            y            ,            z            ,            cellData            =            {            "pressure"            :            pressure            },            pointData            =            {            "temp"            :            temp            })          

Points

To export a set points with two scalar variables (temp and pressure level) at each point using PyEVTK:

                        from            evtk.hl            import            pointsToVTK            <            br            >            import            numpy            as            np            <            br            >            npoints            =            100            <            br            >            x            =            np            .            random            .            rand            (            npoints            )            <            br            >            y            =            np            .            random            .            rand            (            npoints            )            <            br            >            z            =            np            .            random            .            rand            (            npoints            )            <            br            >            pressure level            =            np            .            random            .            rand            (            npoints            )            <            br            >            temp            =            np            .            random            .            rand            (            npoints            )            <            br            >            pointsToVTK            (            "./points"            ,            x            ,            y            ,            z            ,            information            =            {            "temp"            :            temp            ,            "pressure"            :            pressure            })          


A similar example using JEVTK:

                        // Creates some points with random coordinates betwixt 0 and 1            var            npoints            =            25            ;            var            x            =            new            double            [            npoints            ];            var            y            =            new            double            [            npoints            ];            var            z            =            new            double            [            npoints            ];            var            rnd            =            new            Random            ();            for            (            int            i            =            0            ;            i            <            npoints            ;            i            ++)            {            x            [            i            ]            =            rnd            .            nextDouble            ();            y            [            i            ]            =            rnd            .            nextDouble            ();            z            [            i            ]            =            rnd            .            nextDouble            ();            }            // Add a variable associated to each point abd fill with some random values            var            temp            =            new            double            [            npoints            ];            for            (            int            i            =            0            ;            i            <            npoints            ;            i            ++)            {            temp            [            i            ]            =            rnd            .            nextDouble            ();            }            var            pointData            =            EVTK            .            makePointData            ();            pointData            .            addData            (            "temp"            ,            temp            );            EVTK            .            pointsToVTK            (            "points"            ,            x            ,            y            ,            z            ,            pointData            );          

fryerbefurely1959.blogspot.com

Source: https://vtk.org/Wiki/VTK/Writing_VTK_files_using_python

0 Response to "Error Reading Dimension Information From Vtr File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel