[vtkusers] VTK xml Rectinlinear writer multiple Scalars (Vectors) and timesteps

Mohamad M. Nasr-Azadani mmnasr at gmail.com
Thu Jan 31 01:16:35 EST 2008


Hi,

I have just started using VTK. I tried to write xml rectilinear vtk
files. My code (see below) works fine only for ONE vector field and
ONE Scalar and also only one time step.
I am trying to write several scalars (or vectors) to file. It turns
out after each "vtkXMLWriterC_SetPointData()", the previous data
(Vector or Scalar) is replaced by the last one. So, I could only have
one Scalar and One Vector field.
Also, if I try to write several time steps in one xml file, I do not
get the right output.

I think someone had that problem before. I did not see any replies to
the problem in C.
Can someone help me with these two problems?

In advance, thanks.

-Mohamad

PS: Hope this seudo code helps the ones like me who could not find
good examples to start with xml writers in vtk.

**************************

.
.
.

  vtkXMLWriterC* writer = vtkXMLWriterC_New();
  int extent[6] = {0, Nx-1, 0, Ny-1, 0, Nz-1};
  int Nt = Nx*Ny*Nz;
.
.
.
  vtkXMLWriterC_SetDataObjectType(writer, VTK_RECTILINEAR_GRID);
  vtkXMLWriterC_SetExtent(writer, extent);

/* xc, yc, zc: arrays of the grid positions */
/****************************/
  axis           = 0;
  dataType       = VTK_FLOAT;
  numCoordinates = Nx;

  vtkXMLWriterC_SetCoordinates(writer, axis, VTK_FLOAT, xc, Nx);

/****************************/
  axis           = 1;
  dataType       = VTK_FLOAT;
  numCoordinates = Ny;

  vtkXMLWriterC_SetCoordinates(writer, axis, VTK_FLOAT, yc, Ny);
/****************************/
  axis           = 2;
  dataType       = VTK_FLOAT;
  numCoordinates = Nz;

  vtkXMLWriterC_SetCoordinates(writer, axis, VTK_FLOAT, zc, Nz);

  vtkXMLWriterC_SetFileName(writer, "data.vtr");
  vtkXMLWriterC_SetNumberOfTimeSteps(writer, NTIMESTEPS);
  vtkXMLWriterC_Start(writer);

  /* for all timesteps: */
  for(m=0; m<NTIMESTEPS; m++)
  {

	for (k=0; k<Nz; k++) {

		for (j=0; j<Ny; j++) {
			
			for (i=0; i<Nx; i++) {
				
				index = k*(Nx*Ny) + j*Nx + i;
				data1[index] = sin(xc[i])*sin(yc[j]);
			}
		}
	}

    vtkXMLWriterC_SetPointData(writer, "S1", VTK_FLOAT, data1, Nt, 1,
"SCALARS");
	for (k=0; k<Nz; k++) {

		for (j=0; j<Ny; j++) {
			
			for (i=0; i<Nx; i++) {
				
				index = k*(Nx*Ny) + j*Nx + i;
				data2[index] = cos(xc[i])*cos(yc[j]);
			}
		}
	}
	
    vtkXMLWriterC_SetPointData(writer, "S2", VTK_FLOAT, data2, Nt, 1,
"SCALARS");

    vtkXMLWriterC_WriteNextTimeStep(writer, m);
  }

  vtkXMLWriterC_Stop(writer);
  vtkXMLWriterC_Delete(writer);



More information about the vtkusers mailing list