XdmfGridCollection.cpp 11.9 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 : XdmfGridCollection.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
#include <utility>
25
#include "XdmfError.hpp"
26 27
#include "XdmfGeometry.hpp"
#include "XdmfTopology.hpp"
28
#include "XdmfGridCollection.hpp"
29
#include "XdmfGridCollectionType.hpp"
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
class XdmfGridCollection::XdmfGridCollectionImpl : public XdmfGridImpl
{
  public:
  XdmfGridCollectionImpl()
  {
    mGridType = "Collection";
  }

  ~XdmfGridCollectionImpl()
  {
  }

  XdmfGridImpl * duplicate()
  {
    return new XdmfGridCollectionImpl();
  }

  std::string getGridType() const
  {
    return mGridType;
  }
};

54
shared_ptr<XdmfGridCollection>
55
XdmfGridCollection::New()
56
{
57
  shared_ptr<XdmfGridCollection> p(new XdmfGridCollection());
58
  return p;
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
59
}
60

61
XdmfGridCollection::XdmfGridCollection() :
62
  XdmfDomain(),
63
  XdmfGrid(shared_ptr<XdmfGeometry>(), shared_ptr<XdmfTopology>(), "Collection"),
64
  mType(XdmfGridCollectionType::NoCollectionType())
65 66 67 68 69 70 71 72
{
    mImpl = new XdmfGridCollectionImpl();
}

XdmfGridCollection::XdmfGridCollection(XdmfGridCollection & refCollection) :
  XdmfDomain(refCollection),
  XdmfGrid(refCollection),
  mType(refCollection.mType)
73 74 75 76 77
{
}

XdmfGridCollection::~XdmfGridCollection()
{
78 79 80 81
  if (mImpl) {
    delete mImpl;
  }
  mImpl = NULL;
82 83
}

84
const std::string XdmfGridCollection::ItemTag = "Grid";
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
void
XdmfGridCollection::copyGrid(shared_ptr<XdmfGrid> sourceGrid)
{
  XdmfGrid::copyGrid(sourceGrid);
  if (shared_ptr<XdmfGridCollection> classedGrid = shared_dynamic_cast<XdmfGridCollection>(sourceGrid))
  {
    // Copy stucture from read grid to this grid
    while (this->getNumberGridCollections() > 0)
    {
      this->removeGridCollection(0);
    }
    for (unsigned int i = 0; i < classedGrid->getNumberGridCollections(); ++i)
    {
      this->insert(classedGrid->getGridCollection(i));
    }
    while (this->getNumberCurvilinearGrids() > 0)
    {
      this->removeCurvilinearGrid(0);
    }
    for (unsigned int i = 0; i < classedGrid->getNumberCurvilinearGrids(); ++i)
    {
      this->insert(classedGrid->getCurvilinearGrid(i));
    }
    while (this->getNumberGraphs() > 0)
    {
      this->removeGraph(0);
    }
    for (unsigned int i = 0; i < classedGrid->getNumberGraphs(); ++i)
    {
      this->insert(classedGrid->getGraph(i));
    }
    while (this->getNumberRectilinearGrids() > 0)
    {
      this->removeRectilinearGrid(0);
    }
    for (unsigned int i = 0; i < classedGrid->getNumberRectilinearGrids(); ++i)
    {
      this->insert(classedGrid->getRectilinearGrid(i));
    }
    while (this->getNumberRegularGrids() > 0)
    {
      this->removeRegularGrid(0);
    }
    for (unsigned int i = 0; i < classedGrid->getNumberRegularGrids(); ++i)
    {
      this->insert(classedGrid->getRegularGrid(i));
    }
    while (this->getNumberUnstructuredGrids() > 0)
    {
      this->removeUnstructuredGrid(0);
    }
    for (unsigned int i = 0; i < classedGrid->getNumberUnstructuredGrids(); ++i)
    {
      this->insert(classedGrid->getUnstructuredGrid(i));
    }
  }
}

144 145
std::map<std::string, std::string>
XdmfGridCollection::getItemProperties() const
146
{
147 148
  std::map<std::string, std::string> collectionProperties =
    XdmfGrid::getItemProperties();
149
  collectionProperties.insert(std::make_pair("GridType", "Collection"));
150 151
  mType->getProperties(collectionProperties);
  return collectionProperties;
152 153
}

154 155
std::string
XdmfGridCollection::getItemTag() const
156
{
157
  return ItemTag;
158 159
}

160
shared_ptr<const XdmfGridCollectionType>
161
XdmfGridCollection::getType() const
162
{
163
  return mType;
164 165
}

166
void
167
XdmfGridCollection::insert(const shared_ptr<XdmfInformation> information)
168
{
169
  XdmfItem::insert(information);
170 171
}

172 173
void
XdmfGridCollection::populateItem(const std::map<std::string, std::string> & itemProperties,
174
                                 const std::vector<shared_ptr<XdmfItem> > & childItems,
175
                                 const XdmfCoreReader * const reader)
176
{
177 178 179 180
  mType = XdmfGridCollectionType::New(itemProperties);
  XdmfDomain::populateItem(itemProperties, childItems, reader);
  mInformations.clear();
  XdmfGrid::populateItem(itemProperties, childItems, reader);
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 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
void
XdmfGridCollection::read()
{
  if (mGridController)
  {
    if (shared_ptr<XdmfGridCollection> grid = shared_dynamic_cast<XdmfGridCollection>(mGridController->read()))
    {
      // Copy stucture from read grid to this grid
      while(this->getNumberGridCollections() > 0)
      {
        this->removeGridCollection(0);
      }
      for (unsigned int i = 0; i < grid->getNumberGridCollections(); ++i)
      {
        this->insert(grid->getGridCollection(i));
      }
      while(this->getNumberUnstructuredGrids() > 0)
      {
        this->removeUnstructuredGrid(0);
      }
      for (unsigned int i = 0; i < grid->getNumberUnstructuredGrids(); ++i)
      {
        this->insert(grid->getUnstructuredGrid(i));
      }
      while(this->getNumberCurvilinearGrids() > 0)
      {
        this->removeCurvilinearGrid(0);
      }
      for (unsigned int i = 0; i < grid->getNumberCurvilinearGrids(); ++i)
      {
        this->insert(grid->getCurvilinearGrid(i));
      }
      while(this->getNumberRectilinearGrids() > 0)
      {
        this->removeRectilinearGrid(0);
      }
      for (unsigned int i = 0; i < grid->getNumberRectilinearGrids(); ++i)
      {
        this->insert(grid->getRectilinearGrid(i));
      }
      while(this->getNumberRegularGrids() > 0)
      {
        this->removeRegularGrid(0);
      }
      for (unsigned int i = 0; i < grid->getNumberRegularGrids(); ++i)
      {
        this->insert(grid->getRegularGrid(i));
      }
      while(this->getNumberAttributes() > 0)
      {
        this->removeAttribute(0);
      }
      for (unsigned int i = 0; i < grid->getNumberAttributes(); ++i)
      {
        this->insert(grid->getAttribute(i));
      }
      while(this->getNumberInformations() > 0)
      {
        this->removeInformation(0);
      }
      for (unsigned int i = 0; i < grid->getNumberInformations(); ++i)
      {
        this->insert(grid->getInformation(i));
      }
      while(this->getNumberSets() > 0)
      {
        this->removeSet(0);
      }
      for (unsigned int i = 0; i < grid->getNumberSets(); ++i)
      {
        this->insert(grid->getSet(i));
      }
      while(this->getNumberMaps() > 0)
      {
        this->removeMap(0);
      }
      for (unsigned int i = 0; i < grid->getNumberMaps(); ++i)
      {
        this->insert(grid->getMap(i));
      }
    }
    else if (shared_ptr<XdmfGrid> grid = shared_dynamic_cast<XdmfGrid>(mGridController->read()))
    {
      XdmfError::message(XdmfError::FATAL, "Error: Grid Type Mismatch");
    }
    else
    {
      XdmfError::message(XdmfError::FATAL, "Error: Invalid Grid Reference");
    }
  }
}

void
XdmfGridCollection::release()
{
  while(this->getNumberGridCollections() > 0)
  {
    this->removeGridCollection(0);
  }
  while(this->getNumberUnstructuredGrids() > 0)
  {
    this->removeUnstructuredGrid(0);
  }
  while(this->getNumberCurvilinearGrids() > 0)
  {
    this->removeCurvilinearGrid(0);
  }
  while(this->getNumberRectilinearGrids() > 0)
  {
    this->removeRectilinearGrid(0);
  }
  while(this->getNumberRegularGrids() > 0)
  {
    this->removeRegularGrid(0);
  }
  while(this->getNumberAttributes() > 0)
  {
    this->removeAttribute(0);
  }
  while(this->getNumberInformations() > 0)
  {
    this->removeInformation(0);
  }
  while(this->getNumberSets() > 0)
  {
    this->removeSet(0);
  }
  while(this->getNumberMaps() > 0)
  {
    this->removeMap(0);
  }
}

316
void
317
XdmfGridCollection::setType(const shared_ptr<const XdmfGridCollectionType> type)
318
{
319
  mType = type;
320
  this->setIsChanged(true);
321 322
}

323
void
324
XdmfGridCollection::traverse(const shared_ptr<XdmfBaseVisitor> visitor)
325
{
326
  XdmfGrid::traverse(visitor);
327

328
  // Only write XdmfInformations once (deal with diamond inheritance)
329
  std::vector<shared_ptr<XdmfInformation> > informations;
330 331 332
  informations.swap(mInformations);
  XdmfDomain::traverse(visitor);
  informations.swap(mInformations);
333
}
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

// C Wrappers

XDMFGRIDCOLLECTION * XdmfGridCollectionNew()
{
  try
  {
    XDMFGRIDCOLLECTION * returnCollection = NULL;
    shared_ptr<XdmfGridCollection> generatedCollection = XdmfGridCollection::New();
    returnCollection = (XDMFGRIDCOLLECTION *)((void *)((XdmfItem *)(new XdmfGridCollection(*generatedCollection.get()))));
    generatedCollection.reset();
    return returnCollection;
  }
  catch (...)
  {
    XDMFGRIDCOLLECTION * returnCollection = NULL;
    shared_ptr<XdmfGridCollection> generatedCollection = XdmfGridCollection::New();
    returnCollection = (XDMFGRIDCOLLECTION *)((void *)((XdmfItem *)(new XdmfGridCollection(*generatedCollection.get()))));
    generatedCollection.reset();
    return returnCollection;
  }
}

int XdmfGridCollectionGetType(XDMFGRIDCOLLECTION * collection, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  XdmfItem * tempPointer = (XdmfItem *)collection;
  XdmfGridCollection * tempCollection = dynamic_cast<XdmfGridCollection *>(tempPointer);
  shared_ptr<const XdmfGridCollectionType> checkType = tempCollection->getType();
  if (checkType == XdmfGridCollectionType::NoCollectionType()) {
    return XDMF_GRID_COLLECTION_TYPE_NO_COLLECTION_TYPE;
  }
  else if (checkType == XdmfGridCollectionType::Spatial()) {
    return XDMF_GRID_COLLECTION_TYPE_SPATIAL;
  }
  else if (checkType == XdmfGridCollectionType::Temporal()) {
    return XDMF_GRID_COLLECTION_TYPE_TEMPORAL;
  }
  else {
    XdmfError::message(XdmfError::FATAL,
                       "Error: Invalid ArrayType.");
  }
  XDMF_ERROR_WRAP_END(status)
  return -1;
}

void XdmfGridCollectionSetType(XDMFGRIDCOLLECTION * collection, int type, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  XdmfItem * tempPointer = (XdmfItem *)collection;
  XdmfGridCollection * tempCollection = dynamic_cast<XdmfGridCollection *>(tempPointer);
  switch (type) {
    case XDMF_GRID_COLLECTION_TYPE_NO_COLLECTION_TYPE:
      tempCollection->setType(XdmfGridCollectionType::NoCollectionType());
      break;
    case XDMF_GRID_COLLECTION_TYPE_SPATIAL:
      tempCollection->setType(XdmfGridCollectionType::Spatial());
      break;
    case XDMF_GRID_COLLECTION_TYPE_TEMPORAL:
      tempCollection->setType(XdmfGridCollectionType::Temporal());
      break;
    default:
      XdmfError::message(XdmfError::FATAL,
                         "Error: Invalid ArrayType.");
      break;
  }
  XDMF_ERROR_WRAP_END(status)
}

XDMF_DOMAIN_C_CHILD_WRAPPER(XdmfGridCollection, XDMFGRIDCOLLECTION)
XDMF_GRID_C_CHILD_WRAPPER(XdmfGridCollection, XDMFGRIDCOLLECTION)
XDMF_ITEM_C_CHILD_WRAPPER(XdmfGridCollection, XDMFGRIDCOLLECTION)