XdmfRectilinearGrid.cpp 11.7 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  class XdmfGeometryRectilinear : public XdmfGeometry
  {

  public:

    static boost::shared_ptr<XdmfGeometryRectilinear>
    New(XdmfRectilinearGrid * const rectilinearGrid)
    {
      boost::shared_ptr<XdmfGeometryRectilinear>
        p(new XdmfGeometryRectilinear(rectilinearGrid));
      return p;
    }

    unsigned int
    getNumberPoints() const
    {
      const boost::shared_ptr<const XdmfArray> dimensions =
        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
    traverse(const boost::shared_ptr<XdmfBaseVisitor> visitor)
    {
      const std::vector<boost::shared_ptr<XdmfArray> > & coordinates =
        mRectilinearGrid->getCoordinates();
      for(std::vector<boost::shared_ptr<XdmfArray> >::const_iterator iter =
            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:

    static boost::shared_ptr<const XdmfGeometryTypeRectilinear>
    New(const XdmfRectilinearGrid * const rectilinearGrid)
    {
      boost::shared_ptr<const XdmfGeometryTypeRectilinear>
        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 143 144 145 146 147 148 149 150 151 152 153 154 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
      }
    }

  private:

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

    const XdmfRectilinearGrid * const mRectilinearGrid;

  };

  class XdmfTopologyRectilinear : public XdmfTopology
  {

  public:

    static boost::shared_ptr<XdmfTopologyRectilinear>
    New(const XdmfRectilinearGrid * const rectilinearGrid)
    {
      boost::shared_ptr<XdmfTopologyRectilinear>
        p(new XdmfTopologyRectilinear(rectilinearGrid));
      return p;
    }

    unsigned int
    getNumberElements() const
    {
      const boost::shared_ptr<const XdmfArray> dimensions =
        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:

    static boost::shared_ptr<const XdmfTopologyTypeRectilinear>
    New(const XdmfRectilinearGrid * const rectilinearGrid)
    {
      boost::shared_ptr<const XdmfTopologyTypeRectilinear>
        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
    {
      boost::shared_ptr<const XdmfArray> dimensions =
        mRectilinearGrid->getDimensions();
      if(dimensions->getSize() == 3) {
        collectedProperties["Type"] = "3DRectMesh";
      }
      else if(dimensions->getSize() == 2) {
        collectedProperties["Type"] = "2DRectMesh";
      }
      else {
211
        XdmfError::message(XdmfError::FATAL, "Number of dimensions not 2 or 3 in XdmfTopologyTypeRectilinear::getProperties");
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
      }
      collectedProperties["Dimensions"] = dimensions->getValuesString();
    }

  private:

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

    const XdmfRectilinearGrid * const mRectilinearGrid;

  };

  XdmfRectilinearGridImpl(const std::vector<boost::shared_ptr<XdmfArray> > & coordinates) :
    mCoordinates(coordinates.begin(), coordinates.end())
  {
  }

  std::vector<boost::shared_ptr<XdmfArray> > mCoordinates;
234 235 236

};

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

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

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

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

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

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

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

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

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

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

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

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

337 338 339 340
void
XdmfRectilinearGrid::populateItem(const std::map<std::string, std::string> & itemProperties,
                                  std::vector<boost::shared_ptr<XdmfItem> > & childItems,
                                  const XdmfCoreReader * const reader)
341
{
342 343 344 345 346 347 348 349 350 351 352 353 354 355
  XdmfGrid::populateItem(itemProperties, childItems, reader);

  for(std::vector<boost::shared_ptr<XdmfItem> >::const_iterator iter =
        childItems.begin();
      iter != childItems.end();
      ++iter) {
    if(boost::shared_ptr<XdmfRectilinearGrid> rectilinearGrid =
       boost::shared_dynamic_cast<XdmfRectilinearGrid>(*iter)) {
      if(rectilinearGrid->getGeometry()->getType()->getDimensions() > 0) {
        this->setCoordinates(rectilinearGrid->getCoordinates());
        break;
      }
    }
  }
356 357
}

358 359 360
void
XdmfRectilinearGrid::setCoordinates(const unsigned int axisIndex,
                                    const boost::shared_ptr<XdmfArray> axisCoordinates)
361
{
362 363 364 365 366 367 368 369 370
  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;
371 372
}

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