Saving vector variables with VisItWriter

It is possible to save vector variables using visit_writer. There are just a couple of things to remember.

  1. Add another dimension for the number of vector components to your arrays. The new dimension must be last so it changes the most quickly. This gives you data[NZ][NY][NX][NC] where NC is the number of vector components (2 or 3).
  2. Set the vector's entry in the vardims array to the number of components in the vector.
#include <visit_writer.h>

int
main(int argc, char *argv[])
{
#define NX 4
#define NY 5
#define NZ 3
    /* Rectilinear mesh coordinates. */
    float x[] = {0., 1., 2.5, 5.};
    float y[] = {0., 2., 2.25, 2.55,  5.};
    float z[] = {0., 1., 3.};
    int dims[] = {NX, NY, NZ};
    int ndims = 3;
    /* Zonal and Nodal variable data. */
    float zonal[NZ-1][NY-1][NX-1], nodal[NZ][NY][NX];
    float zonalvec[NZ-1][NY-1][NX-1][3], nodalvec[NZ][NY][NX][3];
    /* Info about the variables to pass to visit_writer. */
    int nvars = 4;
    int vardims[] = {1, 1, 3, 3};
    int centering[] = {0, 1, 0, 1};
    const char *varnames[] = {"zonal", "nodal", "zonalvec", "nodalvec"};
    float *vars[] = {(float*)zonal, (float*)nodal, (float*)zonalvec, (float*)nodalvec};

    /* Create 2 zonal variables; 1 scalar, 1 vector. */
    int i,j,k,index = 0;
    for(k = 0; k < NZ-1; ++k)
        for(j = 0; j < NY-1; ++j)
            for(i = 0; i < NX-1; ++i, ++index)
            {
                zonal[k][j][i] = (float)index;

                zonalvec[k][j][i][0] = 1.f;
                zonalvec[k][j][i][1] = 0.f;
                zonalvec[k][j][i][2] = 0.f;
            }

    /* Create 2 nodal variables; 1 scalar, 1 vector. */
    index = 0;
    for(k = 0; k < NZ; ++k)
        for(j = 0; j < NY; ++j)
            for(i = 0; i < NX; ++i, ++index)
            {
                nodal[k][j][i] = (float)index;

                nodalvec[k][j][i][0] = 0.f;
                nodalvec[k][j][i][1] = 1.f;
                nodalvec[k][j][i][2] = 0.f;
            }

    /* Pass the data to visit_writer to write a VTK file.*/
    write_rectilinear_mesh("vwrect3d.vtk", 0, dims, x, y, z, nvars,
        vardims, centering, varnames, vars);

    return 0;
}