XdmfCurvilinearGrid.cpp 7.31 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 : XdmfCurviliniearGrid.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

#include <cmath>
#include "XdmfArray.hpp"
26
#include "XdmfCurvilinearGrid.hpp"
27
#include "XdmfGeometry.hpp"
28 29
#include "XdmfTopology.hpp"
#include "XdmfTopologyType.hpp"
30
#include "XdmfError.hpp"
31 32 33 34

/**
 * PIMPL
 */
35
class XdmfCurvilinearGrid::XdmfCurvilinearGridImpl {
36 37 38

public:

39 40 41 42 43
  class XdmfTopologyCurvilinear : public XdmfTopology
  {

  public:

44
    static shared_ptr<XdmfTopologyCurvilinear>
45 46
    New(const XdmfCurvilinearGrid * const curvilinearGrid)
    {
47
      shared_ptr<XdmfTopologyCurvilinear> 
48 49 50 51 52 53 54
        p(new XdmfTopologyCurvilinear(curvilinearGrid));
      return p;
    }

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

    XdmfTopologyCurvilinear(const XdmfCurvilinearGrid * const curvilinearGrid) :
      mCurvilinearGrid(curvilinearGrid)
    {
      this->setType(XdmfTopologyTypeCurvilinear::New(curvilinearGrid));
    }

    const XdmfCurvilinearGrid * const mCurvilinearGrid;
  };

  class XdmfTopologyTypeCurvilinear : public XdmfTopologyType
  {

  public:

83
    static shared_ptr<const XdmfTopologyTypeCurvilinear>
84 85
    New(const XdmfCurvilinearGrid * const curvilinearGrid)
    {
86
      shared_ptr<const XdmfTopologyTypeCurvilinear>
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
        p(new XdmfTopologyTypeCurvilinear(curvilinearGrid));
      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)mCurvilinearGrid->getDimensions()->getSize());
    }

    void
    getProperties(std::map<std::string, std::string> & collectedProperties) const
    {
103
      shared_ptr<const XdmfArray> dimensions =
104 105 106 107 108 109 110 111
        mCurvilinearGrid->getDimensions();
      if(dimensions->getSize() == 3) {
        collectedProperties["Type"] = "3DSMesh";
      }
      else if(dimensions->getSize() == 2) {
        collectedProperties["Type"] = "2DSMesh";
      }
      else {
112 113 114
        XdmfError::message(XdmfError::FATAL, 
                           "Grid dimensions not 2 or 3 in "
                           "XdmfTopologyTypeCurvilinear::getProperties");
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      }
      collectedProperties["Dimensions"] = dimensions->getValuesString();
    }

  private:

    XdmfTopologyTypeCurvilinear(const XdmfCurvilinearGrid * const curvilinearGrid) :
      XdmfTopologyType(0, "foo", XdmfTopologyType::Structured, 0x1110),
      mCurvilinearGrid(curvilinearGrid)
    {
    }

    const XdmfCurvilinearGrid * const mCurvilinearGrid;

  };

131
  XdmfCurvilinearGridImpl(const shared_ptr<XdmfArray> numPoints) :
132 133 134 135
    mDimensions(numPoints)
  {
  }

136
  shared_ptr<XdmfArray> mDimensions;
137 138 139

};

140
shared_ptr<XdmfCurvilinearGrid>
141 142
XdmfCurvilinearGrid::New(const unsigned int xNumPoints,
                         const unsigned int yNumPoints)
143
{
144
  shared_ptr<XdmfArray> numPoints = XdmfArray::New();
145 146 147
  numPoints->resize<unsigned int>(2);
  numPoints->insert(0, xNumPoints);
  numPoints->insert(1, yNumPoints);
148
  shared_ptr<XdmfCurvilinearGrid> p(new XdmfCurvilinearGrid(numPoints));
149
  return p;
150 151
}

152
shared_ptr<XdmfCurvilinearGrid>
153 154 155
XdmfCurvilinearGrid::New(const unsigned int xNumPoints,
                         const unsigned int yNumPoints,
                         const unsigned int zNumPoints)
156
{
157
  shared_ptr<XdmfArray> numPoints = XdmfArray::New();
158 159 160 161
  numPoints->resize<unsigned int>(3);
  numPoints->insert(0, xNumPoints);
  numPoints->insert(1, yNumPoints);
  numPoints->insert(2, zNumPoints);
162
  shared_ptr<XdmfCurvilinearGrid> p(new XdmfCurvilinearGrid(numPoints));
163
  return p;
164 165
}

166 167
shared_ptr<XdmfCurvilinearGrid>
XdmfCurvilinearGrid::New(const shared_ptr<XdmfArray> numPoints)
168
{
169
  shared_ptr<XdmfCurvilinearGrid> p(new XdmfCurvilinearGrid(numPoints));
170
  return p;
171 172
}

173
XdmfCurvilinearGrid::XdmfCurvilinearGrid(const shared_ptr<XdmfArray> numPoints) :
174 175 176
  XdmfGrid(XdmfGeometry::New(),
           XdmfCurvilinearGridImpl::XdmfTopologyCurvilinear::New(this)),
  mImpl(new XdmfCurvilinearGridImpl(numPoints))
177 178 179
{
}

180
XdmfCurvilinearGrid::~XdmfCurvilinearGrid()
181
{
182
  delete mImpl;
183 184
}

185
const std::string XdmfCurvilinearGrid::ItemTag = "Grid";
186

187
shared_ptr<XdmfArray>
188
XdmfCurvilinearGrid::getDimensions()
189
{
190 191
  return boost::const_pointer_cast<XdmfArray>
    (static_cast<const XdmfCurvilinearGrid &>(*this).getDimensions());
192 193
}

194
shared_ptr<const XdmfArray>
195
XdmfCurvilinearGrid::getDimensions() const
196
{
197
  return mImpl->mDimensions;
198 199
}

200
shared_ptr<XdmfGeometry>
201
XdmfCurvilinearGrid::getGeometry()
202
{
203 204
  return boost::const_pointer_cast<XdmfGeometry>
    (static_cast<const XdmfGrid &>(*this).getGeometry());
205 206
}

207 208
void
XdmfCurvilinearGrid::populateItem(const std::map<std::string, std::string> & itemProperties,
209
                                  std::vector<shared_ptr<XdmfItem> > & childItems,
210
                                  const XdmfCoreReader * const reader)
211
{
212 213
  XdmfGrid::populateItem(itemProperties, childItems, reader);

214
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
215 216 217
        childItems.begin();
      iter != childItems.end();
      ++iter) {
218 219
    if(shared_ptr<XdmfCurvilinearGrid> curvilinearGrid =
       shared_dynamic_cast<XdmfCurvilinearGrid>(*iter)) {
220 221 222
      mImpl->mDimensions = curvilinearGrid->getDimensions();
    }
  }
223 224
}

225
void
226
XdmfCurvilinearGrid::setDimensions(const shared_ptr<XdmfArray> dimensions)
227
{
228
  mImpl->mDimensions = dimensions;
229 230
}

231
void
232
XdmfCurvilinearGrid::setGeometry(const shared_ptr<XdmfGeometry> geometry)
233
{
234
  mGeometry = geometry;
235 236
}