XdmfRectilinearGrid.cpp 11.4 KB
Newer Older
Kenneth Leiter's avatar
Kenneth Leiter committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfRectilinearGrid.cpp                                             */
/*                                                                           */
/*  Author:                                                                  */
/*     Kenneth Leiter                                                        */
/*     kenneth.leiter@arl.army.mil                                           */
/*     US Army Research Laboratory                                           */
/*     Aberdeen Proving Ground, MD                                           */
/*                                                                           */
/*     Copyright @ 2011 US Army Research Laboratory                          */
/*     All Rights Reserved                                                   */
/*     See Copyright.txt for details                                         */
/*                                                                           */
/*     This software is distributed WITHOUT ANY WARRANTY; without            */
/*     even the implied warranty of MERCHANTABILITY or FITNESS               */
/*     FOR A PARTICULAR PURPOSE.  See the above copyright notice             */
/*     for more information.                                                 */
/*                                                                           */
/*****************************************************************************/
23 24 25 26 27

#include <cmath>
#include "XdmfArray.hpp"
#include "XdmfGeometry.hpp"
#include "XdmfGeometryType.hpp"
28
#include "XdmfRectilinearGrid.hpp"
29 30
#include "XdmfTopology.hpp"
#include "XdmfTopologyType.hpp"
31
#include "XdmfError.hpp"
32 33 34 35

/**
 * PIMPL
 */
36
class XdmfRectilinearGrid::XdmfRectilinearGridImpl {
37 38 39

public:

40 41 42 43 44
  class XdmfGeometryRectilinear : public XdmfGeometry
  {

  public:

45
    static shared_ptr<XdmfGeometryRectilinear>
46 47
    New(XdmfRectilinearGrid * const rectilinearGrid)
    {
48
      shared_ptr<XdmfGeometryRectilinear>
49 50 51 52 53 54 55
        p(new XdmfGeometryRectilinear(rectilinearGrid));
      return p;
    }

    unsigned int
    getNumberPoints() const
    {
56
      const shared_ptr<const XdmfArray> dimensions =
57 58 59 60 61 62 63 64 65 66 67 68
        mRectilinearGrid->getDimensions();
      if(dimensions->getSize() == 0) {
        return 0;
      }
      unsigned int toReturn = 1;
      for(unsigned int i=0; i<dimensions->getSize(); ++i) {
        toReturn *= dimensions->getValue<unsigned int>(i);
      }
      return toReturn;
    }

    void
69
    traverse(const shared_ptr<XdmfBaseVisitor> visitor)
70
    {
71
      const std::vector<shared_ptr<XdmfArray> > & coordinates =
72
        mRectilinearGrid->getCoordinates();
73
      for(std::vector<shared_ptr<XdmfArray> >::const_iterator iter =
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
            coordinates.begin();
          iter != coordinates.end();
          ++iter) {
        (*iter)->accept(visitor);
      }
    }

  private:

    XdmfGeometryRectilinear(XdmfRectilinearGrid * const rectilinearGrid) :
      mRectilinearGrid(rectilinearGrid)
    {
      this->setType(XdmfGeometryTypeRectilinear::New(mRectilinearGrid));
    }

    const XdmfRectilinearGrid * const mRectilinearGrid;
  };

  class XdmfGeometryTypeRectilinear : public XdmfGeometryType
  {

  public:

97
    static shared_ptr<const XdmfGeometryTypeRectilinear>
98 99
    New(const XdmfRectilinearGrid * const rectilinearGrid)
    {
100
      shared_ptr<const XdmfGeometryTypeRectilinear>
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
        p(new XdmfGeometryTypeRectilinear(rectilinearGrid));
      return p;
    }

    unsigned int
    getDimensions() const
    {
      return mRectilinearGrid->getDimensions()->getSize();
    }

    void
    getProperties(std::map<std::string, std::string> & collectedProperties) const
    {
      const unsigned int dimensions = this->getDimensions();
      if(dimensions == 3) {
        collectedProperties["Type"] = "VXVYVZ";
      }
      else if(dimensions == 2) {
        collectedProperties["Type"] = "VXVY";
      }
      else {
122
        XdmfError::message(XdmfError::FATAL, "Number of dimensions not 2 or 3 in XdmfGeometryTypeRectilinear::getProperties");
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
      }
    }

  private:

    XdmfGeometryTypeRectilinear(const XdmfRectilinearGrid * const rectilinearGrid) :
      XdmfGeometryType("", 0),
      mRectilinearGrid(rectilinearGrid)
    {
    }

    const XdmfRectilinearGrid * const mRectilinearGrid;

  };

  class XdmfTopologyRectilinear : public XdmfTopology
  {

  public:

143
    static shared_ptr<XdmfTopologyRectilinear>
144 145
    New(const XdmfRectilinearGrid * const rectilinearGrid)
    {
146
      shared_ptr<XdmfTopologyRectilinear>
147 148 149 150 151 152 153
        p(new XdmfTopologyRectilinear(rectilinearGrid));
      return p;
    }

    unsigned int
    getNumberElements() const
    {
154
      const shared_ptr<const XdmfArray> dimensions = 
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        mRectilinearGrid->getDimensions();
      if(dimensions->getSize() == 0) {
        return 0;
      }
      unsigned int toReturn = 1;
      for(unsigned int i=0; i<dimensions->getSize(); ++i) {
        toReturn *= (dimensions->getValue<unsigned int>(i) - 1);
      }
      return toReturn;
    }

  private:

    XdmfTopologyRectilinear(const XdmfRectilinearGrid * const rectilinearGrid) :
      mRectilinearGrid(rectilinearGrid)
    {
      this->setType(XdmfTopologyTypeRectilinear::New(rectilinearGrid));
    }

    const XdmfRectilinearGrid * const mRectilinearGrid;
  };

  class XdmfTopologyTypeRectilinear : public XdmfTopologyType
  {

  public:

182
    static shared_ptr<const XdmfTopologyTypeRectilinear>
183 184
    New(const XdmfRectilinearGrid * const rectilinearGrid)
    {
185
      shared_ptr<const XdmfTopologyTypeRectilinear>
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        p(new XdmfTopologyTypeRectilinear(rectilinearGrid));
      return p;
    }

    unsigned int
    getNodesPerElement() const
    {
      // 2^Dimensions
      // e.g. 1D = 2 nodes per element and 2D = 4 nodes per element.
      return (unsigned int)
        std::pow(2, (double)mRectilinearGrid->getDimensions()->getSize());
    }

    void
    getProperties(std::map<std::string, std::string> & collectedProperties) const
    {
202
      shared_ptr<const XdmfArray> dimensions = 
203 204 205 206 207 208 209 210
        mRectilinearGrid->getDimensions();
      if(dimensions->getSize() == 3) {
        collectedProperties["Type"] = "3DRectMesh";
      }
      else if(dimensions->getSize() == 2) {
        collectedProperties["Type"] = "2DRectMesh";
      }
      else {
211 212 213
        XdmfError::message(XdmfError::FATAL, 
                           "Number of dimensions not 2 or 3 in "
                           "XdmfTopologyTypeRectilinear::getProperties");
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
      }
      collectedProperties["Dimensions"] = dimensions->getValuesString();
    }

  private:

    XdmfTopologyTypeRectilinear(const XdmfRectilinearGrid * const rectilinearGrid) :
      XdmfTopologyType(0, "foo", XdmfTopologyType::Structured, 0x1101),
      mRectilinearGrid(rectilinearGrid)
    {
    }

    const XdmfRectilinearGrid * const mRectilinearGrid;

  };

230
  XdmfRectilinearGridImpl(const std::vector<shared_ptr<XdmfArray> > & coordinates) :
231 232 233 234
    mCoordinates(coordinates.begin(), coordinates.end())
  {
  }

235
  std::vector<shared_ptr<XdmfArray> > mCoordinates;
236 237 238

};

239 240 241
shared_ptr<XdmfRectilinearGrid>
XdmfRectilinearGrid::New(const shared_ptr<XdmfArray> xCoordinates,
                         const shared_ptr<XdmfArray> yCoordinates)
242
{
243
  std::vector<shared_ptr<XdmfArray> > axesCoordinates;
244 245 246
  axesCoordinates.resize(2);
  axesCoordinates[0] = xCoordinates;
  axesCoordinates[1] = yCoordinates;
247
  shared_ptr<XdmfRectilinearGrid> p(new XdmfRectilinearGrid(axesCoordinates));
248
  return p;
249 250
}

251 252 253 254
shared_ptr<XdmfRectilinearGrid>
XdmfRectilinearGrid::New(const shared_ptr<XdmfArray> xCoordinates,
                         const shared_ptr<XdmfArray> yCoordinates,
                         const shared_ptr<XdmfArray> zCoordinates)
255
{
256
  std::vector<shared_ptr<XdmfArray> > axesCoordinates;
257 258 259 260
  axesCoordinates.resize(3);
  axesCoordinates[0] = xCoordinates;
  axesCoordinates[1] = yCoordinates;
  axesCoordinates[2] = zCoordinates;
261
  shared_ptr<XdmfRectilinearGrid> p(new XdmfRectilinearGrid(axesCoordinates));
262
  return p;
263 264
}

265 266
shared_ptr<XdmfRectilinearGrid>
XdmfRectilinearGrid::New(const std::vector<shared_ptr<XdmfArray> > & axesCoordinates)
267
{
268
  shared_ptr<XdmfRectilinearGrid> p(new XdmfRectilinearGrid(axesCoordinates));
269
  return p;
270 271
}

272
XdmfRectilinearGrid::XdmfRectilinearGrid(const std::vector<shared_ptr<XdmfArray> > & axesCoordinates) :
273 274 275
  XdmfGrid(XdmfRectilinearGridImpl::XdmfGeometryRectilinear::New(this),
           XdmfRectilinearGridImpl::XdmfTopologyRectilinear::New(this)),
  mImpl(new XdmfRectilinearGridImpl(axesCoordinates))
276 277 278
{
}

279
XdmfRectilinearGrid::~XdmfRectilinearGrid()
280
{
281
  delete mImpl;
282 283
}

284
const std::string XdmfRectilinearGrid::ItemTag = "Grid";
285

286
shared_ptr<XdmfArray>
287
XdmfRectilinearGrid::getCoordinates(const unsigned int axisIndex)
288
{
289 290 291
  return boost::const_pointer_cast<XdmfArray>
    (static_cast<const XdmfRectilinearGrid &>
     (*this).getCoordinates(axisIndex));
292 293
}

294
shared_ptr<const XdmfArray>
295
XdmfRectilinearGrid::getCoordinates(const unsigned int axisIndex) const
296
{
297 298 299
  if(axisIndex < mImpl->mCoordinates.size()) {
    return mImpl->mCoordinates[axisIndex];
  }
300
  return shared_ptr<XdmfArray>();
301 302
}

303
std::vector<shared_ptr<XdmfArray> >
304
XdmfRectilinearGrid::getCoordinates()
305
{
306
  return static_cast<const XdmfRectilinearGrid &>(*this).getCoordinates();
307 308
}

309
const std::vector<shared_ptr<XdmfArray> >
310
XdmfRectilinearGrid::getCoordinates() const
311
{
312
  return mImpl->mCoordinates;
313 314
}

315
shared_ptr<XdmfArray>
316
XdmfRectilinearGrid::getDimensions()
317
{
318 319
  return boost::const_pointer_cast<XdmfArray>
    (static_cast<const XdmfRectilinearGrid &>(*this).getDimensions());
320 321
}

322
shared_ptr<const XdmfArray>
323
XdmfRectilinearGrid::getDimensions() const
324
{
325
  shared_ptr<XdmfArray> dimensions = XdmfArray::New();
326
  dimensions->reserve(mImpl->mCoordinates.size());
327
  for(std::vector<shared_ptr<XdmfArray> >::const_iterator iter =
328 329 330 331 332 333
        mImpl->mCoordinates.begin();
      iter != mImpl->mCoordinates.end();
      ++iter) {
    dimensions->pushBack((*iter)->getSize());
  }
  return dimensions;
334 335
}

336 337
void
XdmfRectilinearGrid::populateItem(const std::map<std::string, std::string> & itemProperties,
338
                                  std::vector<shared_ptr<XdmfItem> > & childItems,
339
                                  const XdmfCoreReader * const reader)
340
{
341 342
  XdmfGrid::populateItem(itemProperties, childItems, reader);

343
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
344 345 346
        childItems.begin();
      iter != childItems.end();
      ++iter) {
347 348
    if(shared_ptr<XdmfRectilinearGrid> rectilinearGrid =
       shared_dynamic_cast<XdmfRectilinearGrid>(*iter)) {
349 350 351 352 353 354
      if(rectilinearGrid->getGeometry()->getType()->getDimensions() > 0) {
        this->setCoordinates(rectilinearGrid->getCoordinates());
        break;
      }
    }
  }
355 356
}

357 358
void
XdmfRectilinearGrid::setCoordinates(const unsigned int axisIndex,
359
                                    const shared_ptr<XdmfArray> axisCoordinates)
360
{
361 362 363 364 365 366 367 368 369
  if(mImpl->mCoordinates.size() <= axisIndex) {
    mImpl->mCoordinates.reserve(axisIndex + 1);
    unsigned int numArraysToInsert =
      axisIndex - mImpl->mCoordinates.size() + 1;
    for(unsigned int i=0; i<numArraysToInsert; ++i) {
      mImpl->mCoordinates.push_back(XdmfArray::New());
    }
  }
  mImpl->mCoordinates[axisIndex] = axisCoordinates;
370 371
}

372
void
373
XdmfRectilinearGrid::setCoordinates(const std::vector<shared_ptr<XdmfArray> > axesCoordinates)
374
{
375
  mImpl->mCoordinates = axesCoordinates;
376
}