I’m trying to add cell data to a grid. This is what my FECxx.cxx
looks like:
// Adaptor for getting Fortran simulation code into ParaView CoProcessor.
// CoProcessor specific headers
#include "vtkCPDataDescription.h"
#include "vtkCPInputDataDescription.h"
#include "vtkCPProcessor.h"
#include "vtkCPPythonScriptPipeline.h"
#include "vtkDoubleArray.h"
#include "vtkCellData.h"
#include "vtkSmartPointer.h"
#include "vtkRectilinearGrid.h"
// Fortran specific header
#include "vtkCPPythonAdaptorAPI.h"
// These will be called from the Fortran "glue" code"
// Completely dependent on data layout, structured vs. unstructured, etc.
// since VTK/ParaView uses different internal layouts for each.
// Creates the data container for the CoProcessor.
extern "C" void createcpdata_(int* is, int* ie, int* js, int* je, int* ks, int* ke, int* nx, int* ny, int* nz, double* x, double* y, double* z)
{
if (!vtkCPPythonAdaptorAPI::GetCoProcessorData())
{
vtkGenericWarningMacro("Unable to access CoProcessorData.");
return;
}
// Set whole extent
vtkCPPythonAdaptorAPI::GetCoProcessorData()->
GetInputDescriptionByName("input")->SetWholeExtent(0, *nx, 0, *ny, 0, *nz);
// create a Rectilinear grid
cout << "FECxx.cxx : Creating Rectilinear Grid\n";
auto grid = vtkSmartPointer<vtkRectilinearGrid>::New();
grid->SetExtent(*is-1, *ie, *js-1, *je, *ks-1, *ke);
vtkSmartPointer<vtkDoubleArray> xCoords = vtkSmartPointer<vtkDoubleArray>::New();
xCoords->SetNumberOfComponents(1);
vtkSmartPointer<vtkDoubleArray> yCoords = vtkSmartPointer<vtkDoubleArray>::New();
yCoords->SetNumberOfComponents(1);
vtkSmartPointer<vtkDoubleArray> zCoords = vtkSmartPointer<vtkDoubleArray>::New();
zCoords->SetNumberOfComponents(1);
for(int i = *is-1; i <= *ie; i++)
{
xCoords->InsertNextValue( x [i] );
}
for(int i = *js-1; i <= *je; i++)
{
yCoords->InsertNextValue( y [i] );
}
for(int i = *ks-1; i <= *ke; i++)
{
zCoords->InsertNextValue( z [i] );
}
grid->SetXCoordinates(xCoords);
grid->SetYCoordinates(yCoords);
grid->SetZCoordinates(yCoords);
// Name should be consistent between here, Fortran and Python client script.q
vtkCPPythonAdaptorAPI::GetCoProcessorData()->
GetInputDescriptionByName("input")->SetGrid(grid);
}
// Add field(s) to the data container.
// Separate from above because this will be dynamic, grid is static.
// By hand name mangling for fortran.
extern "C" void addfield_(double* scalars, char* name)
{
auto idd = vtkCPPythonAdaptorAPI::GetCoProcessorData()->
GetInputDescriptionByName("input");
auto grid = vtkRectilinearGrid::SafeDownCast(idd->GetGrid());
if (!grid)
{
vtkGenericWarningMacro("No adaptor grid to attach field data to.");
return;
}
// field name must match that in the fortran code.
if (idd->IsFieldNeeded(name))
{
auto field = vtkSmartPointer<vtkDoubleArray>::New();
field->SetName(name);
field->SetNumberOfComponents(1);
//field->SetNumberOfTuples(grid->GetNumberOfCells());
field->SetArray(scalars, 1 * grid->GetNumberOfCells(), 1);
// for debugging =================================================
for(int i = 0; i < grid->GetNumberOfCells(); i++)
{
cout << scalars[i] << " ";
}
cout << "\n";
// for debugging =================================================
grid->GetCellData()->AddArray(field);
}
}
I have the coprocessor write the data to a pvtr file which I’m inspecting in Paraview. My grid is fine, but there is an error in the file addfield_
function that I just can’t find. For some reason the cell data that is written to the pvtr file is strange (holding zeroes and really,really,really huge values). As you can see I am printing out the values of scalars
and there they are fine.
Any idea what I’m doing wrong?