XdmfGrid.cpp 14.6 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 : XdmfGrid.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.                                                 */
/*                                                                           */
/*****************************************************************************/
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
23

24
#include <utility>
25
#include <string.h>
26
#include "XdmfAttribute.hpp"
27
#include "XdmfError.hpp"
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
28
#include "XdmfGeometry.hpp"
29
#include "XdmfGrid.hpp"
30
#include "XdmfMap.hpp"
31
#include "XdmfSet.hpp"
32
#include "XdmfTime.hpp"
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
33 34
#include "XdmfTopology.hpp"

35
XDMF_CHILDREN_IMPLEMENTATION(XdmfGrid, XdmfAttribute, Attribute, Name)
36
XDMF_CHILDREN_IMPLEMENTATION(XdmfGrid, XdmfMap, Map, Name)
37 38
XDMF_CHILDREN_IMPLEMENTATION(XdmfGrid, XdmfSet, Set, Name)

39 40
XdmfGrid::XdmfGrid(const shared_ptr<XdmfGeometry> geometry,
                   const shared_ptr<XdmfTopology> topology,
41 42 43 44
                   const std::string & name) :
  mGeometry(geometry),
  mTopology(topology),
  mName(name),
45
  mTime(shared_ptr<XdmfTime>())
Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
46 47 48
{
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
XdmfGrid::XdmfGrid(XdmfGrid &refGrid):
  XdmfItem(refGrid),
  mAttributes(refGrid.mAttributes),
  mSets(refGrid.mSets),
  mMaps(refGrid.mMaps),
  mGeometry(refGrid.mGeometry),
  mTopology(refGrid.mTopology),
  mName(refGrid.mName),
  mTime(refGrid.mTime)
{
  XdmfGridImpl * holder = refGrid.mImpl;
  XdmfGridImpl * duplicateImpl = holder->duplicate();
  mImpl = duplicateImpl;
}

Ken Leiter (Civ's avatar
Ken Leiter (Civ committed
64 65 66 67
XdmfGrid::~XdmfGrid()
{
}

68
const std::string XdmfGrid::ItemTag = "Grid";
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
shared_ptr<XdmfGridController>
XdmfGrid::getGridController()
{
  return mGridController;
}

void
XdmfGrid::copyGrid(shared_ptr<XdmfGrid> sourceGrid)
{
  this->setName(sourceGrid->getName());
  this->setTime(sourceGrid->getTime());
  while(this->getNumberAttributes() > 0)
  {
    this->removeAttribute(0);
  }
  for (unsigned int i = 0; i < sourceGrid->getNumberAttributes(); ++i)
  {
    this->insert(sourceGrid->getAttribute(i));
  }
  while(this->getNumberInformations() > 0)
  {
    this->removeInformation(0);
  }
  for (unsigned int i = 0; i < sourceGrid->getNumberInformations(); ++i)
  {
    this->insert(sourceGrid->getInformation(i));
  }
  while(this->getNumberSets() > 0)
  {
    this->removeSet(0);
  }
  for (unsigned int i = 0; i < sourceGrid->getNumberSets(); ++i)
  {
    this->insert(sourceGrid->getSet(i));
  }
  while(this->getNumberMaps() > 0)
  {
    this->removeMap(0);
  }
  for (unsigned int i = 0; i < sourceGrid->getNumberMaps(); ++i)
  {
    this->insert(sourceGrid->getMap(i));
  }
}

115
shared_ptr<const XdmfGeometry>
116
XdmfGrid::getGeometry() const
117
{
118
  return mGeometry;
119 120
}

121 122
std::map<std::string, std::string>
XdmfGrid::getItemProperties() const
123
{
124
  std::map<std::string, std::string> gridProperties;
125
  gridProperties.insert(std::make_pair("Name", mName));
126
  return gridProperties;
127 128
}

129 130
std::string
XdmfGrid::getItemTag() const
131
{
132
  return ItemTag;
133 134
}

135 136
std::string
XdmfGrid::getName() const
137
{
138
    return mName;
139 140
}

141
shared_ptr<XdmfTime>
142
XdmfGrid::getTime()
143
{
144
  return boost::const_pointer_cast<XdmfTime>
145
    (static_cast<const XdmfGrid &>(*this).getTime());
146 147
}

148
shared_ptr<const XdmfTime>
149
XdmfGrid::getTime() const
150
{
151
  return mTime;
152 153
}

154
shared_ptr<const XdmfTopology>
155
XdmfGrid::getTopology() const
156
{
157
  return mTopology;
158 159
}

160 161
void
XdmfGrid::populateItem(const std::map<std::string, std::string> & itemProperties,
162
                       const std::vector<shared_ptr<XdmfItem> > & childItems,
163
                       const XdmfCoreReader * const reader)
164
{
165 166 167 168 169 170 171 172 173
  XdmfItem::populateItem(itemProperties, childItems, reader);
  std::map<std::string, std::string>::const_iterator name =
    itemProperties.find("Name");
  if(name != itemProperties.end()) {
    mName = name->second;
  }
  else {
    mName = "";
  }
174
  for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
175 176 177
        childItems.begin();
      iter != childItems.end();
      ++iter) {
178 179
    if(shared_ptr<XdmfAttribute> attribute =
       shared_dynamic_cast<XdmfAttribute>(*iter)) {
180 181
      this->insert(attribute);
    }
182 183
    else if(shared_ptr<XdmfGeometry> geometry =
            shared_dynamic_cast<XdmfGeometry>(*iter)) {
184 185
      mGeometry = geometry;
    }
186 187
    else if(shared_ptr<XdmfMap> map =
            shared_dynamic_cast<XdmfMap>(*iter)) {
188
      this->insert(map);
189
    }
190 191
    else if(shared_ptr<XdmfSet> set =
            shared_dynamic_cast<XdmfSet>(*iter)) {
192 193
      this->insert(set);
    }
194 195
    else if(shared_ptr<XdmfTime> time =
            shared_dynamic_cast<XdmfTime>(*iter)) {
196 197
      mTime = time;
    }
198 199
    else if(shared_ptr<XdmfTopology> topology =
            shared_dynamic_cast<XdmfTopology>(*iter)) {
200 201
      mTopology = topology;
    }
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
    else if(shared_ptr<XdmfGridController> gridController =
            shared_dynamic_cast<XdmfGridController>(*iter)) {
      this->setGridController(gridController);
    }
  }
}

void
XdmfGrid::read()
{

}

void
XdmfGrid::release()
{
  this->setName("");
  this->setTime(shared_ptr<XdmfTime>());
  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);
235
  }
236 237
}

238 239 240 241 242 243 244
void
XdmfGrid::setGridController(shared_ptr<XdmfGridController> newController)
{
  mGridController = newController;
}


245 246
void
XdmfGrid::setName(const std::string & name)
247
{
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
248
  mName = name;
249
  this->setIsChanged(true);
250 251
}

252
void
253
XdmfGrid::setTime(const shared_ptr<XdmfTime> time)
254
{
255
  mTime = time;
256
  this->setIsChanged(true);
257
}
258 259

void
260
XdmfGrid::traverse(const shared_ptr<XdmfBaseVisitor> visitor)
261 262
{
  XdmfItem::traverse(visitor);
263 264 265
  if (mGridController) {
    mGridController->accept(visitor);
  }
266 267 268 269 270 271 272 273 274
  if(mTime) {
    mTime->accept(visitor);
  }
  if(mGeometry) {
    mGeometry->accept(visitor);
  }
  if(mTopology) {
    mTopology->accept(visitor);
  }
275 276 277
  for (unsigned int i = 0; i < mAttributes.size(); ++i)
  {
    mAttributes[i]->accept(visitor);
278
  }
279 280 281
  for (unsigned int i = 0; i < mMaps.size(); ++i)
  {
    mMaps[i]->accept(visitor);
282
  }
283 284 285
  for (unsigned int i = 0; i < mSets.size(); ++i)
  {
    mSets[i]->accept(visitor);
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

// C Wrappers

XDMFATTRIBUTE * XdmfGridGetAttribute(XDMFGRID * grid, unsigned int index)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return (XDMFATTRIBUTE *)((void *)(gridPointer->getAttribute(index).get()));
}

XDMFATTRIBUTE * XdmfGridGetAttributeByName(XDMFGRID * grid, char * Name)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return (XDMFATTRIBUTE *)((void *)(gridPointer->getAttribute(Name).get()));
}

unsigned int XdmfGridGetNumberAttributes(XDMFGRID * grid)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return gridPointer->getNumberAttributes();
}

void XdmfGridInsertAttribute(XDMFGRID * grid, XDMFATTRIBUTE * Attribute, int passControl)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  if (passControl) {
    gridPointer->insert(shared_ptr<XdmfAttribute>((XdmfAttribute *)Attribute));
  }
  else {
    gridPointer->insert(shared_ptr<XdmfAttribute>((XdmfAttribute *)Attribute, XdmfNullDeleter()));
  }
}

void XdmfGridRemoveAttribute(XDMFGRID * grid, unsigned int index)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  gridPointer->removeAttribute(index);
}

void XdmfGridRemoveAttributeByName(XDMFGRID * grid, char * Name)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  gridPointer->removeAttribute(Name);
}

XDMFSET * XdmfGridGetSet(XDMFGRID * grid, unsigned int index)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return (XDMFSET *)((void *)(gridPointer->getSet(index).get()));
}

XDMFSET * XdmfGridGetSetByName(XDMFGRID * grid, char * Name)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return (XDMFSET *)((void *)(gridPointer->getSet(Name).get()));
}

unsigned int XdmfGridGetNumberSets(XDMFGRID * grid)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return gridPointer->getNumberSets();
}

void XdmfGridInsertSet(XDMFGRID * grid, XDMFSET * Set, int passControl)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  if (passControl) {
    gridPointer->insert(shared_ptr<XdmfSet>((XdmfSet *)Set));
  }
  else {
    gridPointer->insert(shared_ptr<XdmfSet>((XdmfSet *)Set, XdmfNullDeleter()));
  }
}

void XdmfGridRemoveSet(XDMFGRID * grid, unsigned int index)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  gridPointer->removeSet(index);
}

void XdmfGridRemoveSetByName(XDMFGRID * grid, char * Name)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  gridPointer->removeSet(Name);
}

XDMFMAP * XdmfGridGetMap(XDMFGRID * grid, unsigned int index)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return (XDMFMAP *)((void *)(gridPointer->getMap(index).get()));
}

XDMFMAP * XdmfGridGetMapByName(XDMFGRID * grid, char * Name)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return (XDMFMAP *)((void *)(gridPointer->getMap(Name).get()));
}

unsigned int XdmfGridGetNumberMaps(XDMFGRID * grid)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return gridPointer->getNumberMaps();
}

void XdmfGridInsertMap(XDMFGRID * grid, XDMFMAP * Map, int passControl)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  if (passControl) {
    gridPointer->insert(shared_ptr<XdmfMap>((XdmfMap *)Map));
  }
  else {
    gridPointer->insert(shared_ptr<XdmfMap>((XdmfMap *)Map, XdmfNullDeleter()));
  }
}

void XdmfGridRemoveMap(XDMFGRID * grid, unsigned int index)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  gridPointer->removeMap(index);
}

void XdmfGridRemoveMapByName(XDMFGRID * grid, char * Name)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  gridPointer->removeMap(Name);
}

XDMFGRIDCONTROLLER * XdmfGridGetGridController(XDMFGRID * grid)
{
  XdmfItem * classedPointer = (XdmfItem *)grid;
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(classedPointer);
  shared_ptr<XdmfGridController> generatedController = gridPointer->getGridController();
  return (XDMFGRIDCONTROLLER *)((void *)(generatedController.get()));

}

char * XdmfGridGetName(XDMFGRID * grid)
{
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  return strdup(gridPointer->getName().c_str());
}

XDMFTIME * XdmfGridGetTime(XDMFGRID * grid)
{
  XdmfItem * classedPointer = (XdmfItem *)grid;
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(classedPointer);
  shared_ptr<XdmfTime> generatedTime = gridPointer->getTime();
  return (XDMFTIME *)((void *)(generatedTime.get()));
}

void
XdmfGridRead(XDMFGRID * grid, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  try
  {
    XdmfItem * classedPointer = (XdmfItem *)grid;
    XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(classedPointer);
    gridPointer->read();
  }
  catch (...)
  {
    XdmfItem * classedPointer = (XdmfItem *)grid;
    XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(classedPointer);
    gridPointer->read();
  }
  XDMF_ERROR_WRAP_END(status)
}

void
XdmfGridRelease(XDMFGRID * grid)
{
  XdmfItem * classedPointer = (XdmfItem *)grid;
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(classedPointer);
  gridPointer->release();
}

void XdmfGridSetGridController(XDMFGRID * grid, XDMFGRIDCONTROLLER * controller, int passControl)
{
  XdmfItem * classedPointer = (XdmfItem *)grid;
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(classedPointer);
  XdmfItem * controllerPointer = (XdmfItem *) controller;
  XdmfGridController * classedController = dynamic_cast<XdmfGridController *>(controllerPointer);
  if (passControl) {
    gridPointer->setGridController(shared_ptr<XdmfGridController>(classedController));
  }
  else {
    gridPointer->setGridController(shared_ptr<XdmfGridController>(classedController, XdmfNullDeleter()));
  }
}

void XdmfGridSetName(XDMFGRID * grid, char * name, int * status)
{
  XDMF_ERROR_WRAP_START(status)
  XdmfItem * tempPointer = (XdmfItem *)(grid);
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(tempPointer);
  gridPointer->setName(name);
  XDMF_ERROR_WRAP_END(status)
}

void XdmfGridSetTime(XDMFGRID * grid, XDMFTIME * time, int passControl)
{
  XdmfItem * classedPointer = (XdmfItem *)grid;
  XdmfGrid * gridPointer = dynamic_cast<XdmfGrid *>(classedPointer);
  if (passControl) {
    gridPointer->setTime(shared_ptr<XdmfTime>((XdmfTime *)time));
  }
  else {
    gridPointer->setTime(shared_ptr<XdmfTime>((XdmfTime *)time, XdmfNullDeleter()));
  }
}

XDMF_ITEM_C_CHILD_WRAPPER(XdmfGrid, XDMFGRID)