dataadaptor.cpp 7.02 KB
Newer Older
1
#include "dataadaptor.h"
Burlen Loring's avatar
Burlen Loring committed
2
#include "Error.h"
3

Burlen Loring's avatar
Burlen Loring committed
4
#include <vtkInformation.h>
5 6 7 8
#include <vtkFloatArray.h>
#include <vtkImageData.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkObjectFactory.h>
9
#include <vtkCellData.h>
10 11 12 13 14 15 16
#include <vtkSmartPointer.h>

#include <diy/master.hpp>

namespace oscillators
{

Burlen Loring's avatar
Burlen Loring committed
17
struct DataAdaptor::DInternals
18
{
19
  std::vector<diy::DiscreteBounds> CellExtents;
20 21 22
  std::vector<float*> Data;
  vtkSmartPointer<vtkMultiBlockDataSet> Mesh;
  std::vector<vtkSmartPointer<vtkImageData> > BlockMesh;
Burlen Loring's avatar
Burlen Loring committed
23
  std::vector<int> DataExtent;
24 25 26 27
};

inline bool areBoundsValid(const diy::DiscreteBounds& bds)
{
Burlen Loring's avatar
Burlen Loring committed
28 29
  return ((bds.min[0] <= bds.max[0]) && (bds.min[1] <= bds.max[1])
    && (bds.min[2] <= bds.max[2]));
30 31
}

Burlen Loring's avatar
Burlen Loring committed
32 33
//-----------------------------------------------------------------------------
senseiNewMacro(DataAdaptor);
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
//-----------------------------------------------------------------------------
DataAdaptor::DataAdaptor() :
  Internals(new DataAdaptor::DInternals())
{
}

//-----------------------------------------------------------------------------
DataAdaptor::~DataAdaptor()
{
  delete this->Internals;
}

//-----------------------------------------------------------------------------
void DataAdaptor::Initialize(size_t nblocks)
{
  DInternals& internals = (*this->Internals);
51
  internals.CellExtents.resize(nblocks);
52 53
  internals.Data.resize(nblocks);
  internals.BlockMesh.resize(nblocks);
54 55 56 57 58 59 60 61 62
  for (size_t cc=0; cc < nblocks; cc++)
    {
    internals.CellExtents[cc].min[0] = 0;
    internals.CellExtents[cc].min[1] = 0;
    internals.CellExtents[cc].min[2] = 0;
    internals.CellExtents[cc].max[0] = -1;
    internals.CellExtents[cc].max[1] = -1;
    internals.CellExtents[cc].max[2] = -1;
    }
63 64 65 66 67 68 69 70 71 72
  this->ReleaseData();
}

//-----------------------------------------------------------------------------
void DataAdaptor::SetBlockExtent(int gid,
  int xmin, int xmax,
  int ymin, int ymax,
  int zmin, int zmax)
{
  DInternals& internals = (*this->Internals);
73 74 75
  internals.CellExtents[gid].min[0] = xmin;
  internals.CellExtents[gid].min[1] = ymin;
  internals.CellExtents[gid].min[2] = zmin;
76

77 78 79
  internals.CellExtents[gid].max[0] = xmax;
  internals.CellExtents[gid].max[1] = ymax;
  internals.CellExtents[gid].max[2] = zmax;
80 81
}

Burlen Loring's avatar
Burlen Loring committed
82 83 84 85
//-----------------------------------------------------------------------------
void DataAdaptor::SetDataExtent(int ext[6])
{
  this->Internals->DataExtent.assign(ext, ext+6);
Burlen Loring's avatar
Burlen Loring committed
86

Burlen Loring's avatar
Burlen Loring committed
87 88 89 90
  this->GetInformation()->Set(vtkDataObject::DATA_EXTENT(),
      this->Internals->DataExtent.data(), 6);
}

91 92 93 94 95 96 97 98
//-----------------------------------------------------------------------------
void DataAdaptor::SetBlockData(int gid, float* data)
{
  DInternals& internals = (*this->Internals);
  internals.Data[gid] = data;
}

//-----------------------------------------------------------------------------
Burlen Loring's avatar
Burlen Loring committed
99 100
int DataAdaptor::GetMesh(const std::string &meshName, bool structureOnly,
    vtkDataObject *&mesh)
101
{
Burlen Loring's avatar
Burlen Loring committed
102 103 104 105 106 107 108 109 110
  (void)structureOnly;

  if (meshName != "mesh")
    {
    SENSEI_ERROR("the miniapp provides a mesh named \"mesh\"" 
       " you requested \"" << meshName << "\"")
    return -1;
    }

111
  DInternals& internals = (*this->Internals);
Burlen Loring's avatar
Burlen Loring committed
112

113 114 115
  if (!internals.Mesh)
    {
    internals.Mesh = vtkSmartPointer<vtkMultiBlockDataSet>::New();
116 117
    internals.Mesh->SetNumberOfBlocks(static_cast<unsigned int>(internals.CellExtents.size()));
    for (size_t cc=0; cc < internals.CellExtents.size(); ++cc)
118 119 120 121
      {
      internals.Mesh->SetBlock(static_cast<unsigned int>(cc), this->GetBlockMesh(cc));
      }
    }
Burlen Loring's avatar
Burlen Loring committed
122 123 124 125

  mesh = internals.Mesh;

  return 0;
126 127 128 129 130 131 132
}

//-----------------------------------------------------------------------------
vtkDataObject* DataAdaptor::GetBlockMesh(int gid)
{
  DInternals& internals = (*this->Internals);
  vtkSmartPointer<vtkImageData>& blockMesh = internals.BlockMesh[gid];
133 134
  const diy::DiscreteBounds& cellExts = internals.CellExtents[gid];
  if (!blockMesh && areBoundsValid(cellExts))
135 136 137
    {
    blockMesh = vtkSmartPointer<vtkImageData>::New();
    blockMesh->SetExtent(
138 139 140
      cellExts.min[0], cellExts.max[0]+1,
      cellExts.min[1], cellExts.max[1]+1,
      cellExts.min[2], cellExts.max[2]+1);
141 142 143 144 145
    }
  return blockMesh;
}

//-----------------------------------------------------------------------------
Burlen Loring's avatar
Burlen Loring committed
146 147
int DataAdaptor::AddArray(vtkDataObject* mesh, const std::string &meshName,
    int association, const std::string &arrayName)
148
{
Burlen Loring's avatar
Burlen Loring committed
149
#ifndef NDEBUG
Burlen Loring's avatar
Burlen Loring committed
150 151
  if ((association != vtkDataObject::FIELD_ASSOCIATION_CELLS) ||
    (arrayName != "data") || (meshName != "mesh"))
152
    {
Burlen Loring's avatar
Burlen Loring committed
153 154 155
    SENSEI_ERROR("the miniapp provides a cell centered array named \"data\" "
      " on a mesh named \"mesh\"")
    return 1;
156
    }
Burlen Loring's avatar
Burlen Loring committed
157
#else
Burlen Loring's avatar
Burlen Loring committed
158
  (void)meshName;
Burlen Loring's avatar
Burlen Loring committed
159
  (void)association;
Burlen Loring's avatar
Burlen Loring committed
160
  (void)arrayName;
Burlen Loring's avatar
Burlen Loring committed
161
#endif
Burlen Loring's avatar
Burlen Loring committed
162
  int retVal = 1;
163 164 165 166 167 168 169 170 171
  DInternals& internals = (*this->Internals);
  vtkMultiBlockDataSet* md = vtkMultiBlockDataSet::SafeDownCast(mesh);
  for (unsigned int cc=0, max=md->GetNumberOfBlocks(); cc < max; ++cc)
    {
    if (!internals.Data[cc])
      {
      continue;
      }
    vtkSmartPointer<vtkImageData>& blockMesh = internals.BlockMesh[cc];
172
    if (vtkCellData* cd = (blockMesh? blockMesh->GetCellData(): NULL))
173
      {
Burlen Loring's avatar
Burlen Loring committed
174
      if (cd->GetArray(arrayName.c_str()) == NULL)
175 176
        {
        vtkFloatArray* fa = vtkFloatArray::New();
Burlen Loring's avatar
Burlen Loring committed
177
        fa->SetName(arrayName.c_str());
178 179 180
        fa->SetArray(internals.Data[cc], blockMesh->GetNumberOfCells(), 1);
        cd->SetScalars(fa);
        cd->SetActiveScalars("data");
181 182
        fa->FastDelete();
        }
Burlen Loring's avatar
Burlen Loring committed
183
      retVal = 0;
184 185 186 187 188 189
      }
    }
  return retVal;
}

//-----------------------------------------------------------------------------
Burlen Loring's avatar
Burlen Loring committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
int DataAdaptor::GetNumberOfMeshes(unsigned int &numMeshes)
{
  numMeshes = 1;
  return 0;
}

//-----------------------------------------------------------------------------
int DataAdaptor::GetMeshName(unsigned int id, std::string &meshName)
{
  if (id == 0)
    {
    meshName = "mesh";
    return 0;
    }

  SENSEI_ERROR("Failed to get mesh name")
  return -1;
}

//-----------------------------------------------------------------------------
int DataAdaptor::GetNumberOfArrays(const std::string &meshName, int association,
    unsigned int &numberOfArrays)
{
  if ((meshName == "mesh") && (association == vtkDataObject::CELL))
    {
    numberOfArrays = 1;
    return 0;
    }
  return 0;
}

//-----------------------------------------------------------------------------
int DataAdaptor::GetArrayName(const std::string &meshName, int association,
    unsigned int index, std::string &arrayName)
{
  if ((meshName == "mesh") &&
    (association == vtkDataObject::CELL) && (index == 0))
    {
    arrayName = "data";
    return 0;
    }

  SENSEI_ERROR("Failed to get array name")
  return -1;
}

//-----------------------------------------------------------------------------
int DataAdaptor::ReleaseData()
238 239 240
{
  DInternals& internals = (*this->Internals);
  internals.Mesh = NULL;
241
  for (auto i : internals.CellExtents)
242 243 244 245 246 247 248 249 250
    {
    i.min[0] = i.min[1] = i.min[2] = 0;
    i.max[0] = i.max[1] = i.max[2] = -1;
    }
  for (size_t cc=0, max = internals.Data.size(); cc < max; ++cc)
    {
    internals.Data[cc] = NULL;
    internals.BlockMesh[cc] = NULL;
    }
Burlen Loring's avatar
Burlen Loring committed
251
  return 0;
252 253 254
}

}