XdmfItemFactory.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 23
/*****************************************************************************/
/*                                    XDMF                                   */
/*                       eXtensible Data Model and Format                    */
/*                                                                           */
/*  Id : XdmfItemFactory.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.                                                 */
/*                                                                           */
/*****************************************************************************/

24
#include <algorithm>
25
#include <cctype>
26
#include <cstring>
27
#include "XdmfAttribute.hpp"
28
#include "XdmfCurvilinearGrid.hpp"
29
#include "XdmfDomain.hpp"
30
#include "XdmfError.hpp"
31
#include "XdmfFunction.hpp"
32
#include "XdmfGeometry.hpp"
33
#include "XdmfGeometryType.hpp"
Kenneth Leiter's avatar
Kenneth Leiter committed
34
#include "XdmfGraph.hpp"
35
#include "XdmfGridCollection.hpp"
36
#include "XdmfGridTemplate.hpp"
37
#include "XdmfInformation.hpp"
38
#include "XdmfItemFactory.hpp"
39
#include "XdmfAggregate.hpp"
40
#include "XdmfMap.hpp"
41 42
#include "XdmfRectilinearGrid.hpp"
#include "XdmfRegularGrid.hpp"
43
#include "XdmfSet.hpp"
Kenneth Leiter's avatar
Kenneth Leiter committed
44
#include "XdmfSparseMatrix.hpp"
45
#include "XdmfTemplate.hpp"
46
#include "XdmfTime.hpp"
47
#include "XdmfTopology.hpp"
48
#include "XdmfUnstructuredGrid.hpp"
49

50
shared_ptr<XdmfItemFactory>
51
XdmfItemFactory::New()
52
{
53
  shared_ptr<XdmfItemFactory> p(new XdmfItemFactory());
54
  return p;
55 56
}

57 58 59 60 61 62 63 64
XdmfItemFactory::XdmfItemFactory()
{
}

XdmfItemFactory::~XdmfItemFactory()
{
}

65
shared_ptr<XdmfItem>
66 67
XdmfItemFactory::createItem(const std::string & itemTag,
                            const std::map<std::string, std::string> & itemProperties,
68
                            const std::vector<shared_ptr<XdmfItem> > & childItems) const
69
{
70 71 72 73
#ifdef XDMF_BUILD_DSM
  shared_ptr<XdmfItem> newItem =
    XdmfDSMItemFactory::createItem(itemTag, itemProperties, childItems);
#else
74 75
  shared_ptr<XdmfItem> newItem =
    XdmfCoreItemFactory::createItem(itemTag, itemProperties, childItems);
76
#endif
77 78 79 80
  
  if(newItem) {
    return newItem;
  }
81

82 83 84
  if(itemTag.compare(XdmfAttribute::ItemTag) == 0) {
    return XdmfAttribute::New();
  }
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
  else if(itemTag.compare(XdmfAggregate::ItemTag) == 0) {
    std::map<std::string, std::string>::const_iterator type =
      itemProperties.find("ConstructedType");
    std::string arraySubType;
    if(type == itemProperties.end()) {
      // If no type is specified an array is generated
      arraySubType = XdmfArray::ItemTag;
    }
    else {
      arraySubType = type->second;
    }
    std::vector<shared_ptr<XdmfItem> > newArrayChildren;
    shared_ptr<XdmfItem> createdItem = createItem(arraySubType,
                                       itemProperties,
                                       newArrayChildren);

    shared_ptr<XdmfArray> returnArray = shared_dynamic_cast<XdmfArray>(createdItem);

    shared_ptr<XdmfAggregate> returnAggregate = XdmfAggregate::New();

    bool placeholderFound = false;
    for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
          childItems.begin();
        iter != childItems.end();
        ++iter) {
      if(shared_ptr<XdmfArray> array = shared_dynamic_cast<XdmfArray>(*iter)) {
        if (!placeholderFound) {
          placeholderFound = true;
        }
        else {
          returnAggregate->insert(array);
        }
      }
    }

    returnArray->setReference(returnAggregate);
    returnArray->setReadMode(XdmfArray::Reference);

    return returnArray;
  }
125 126 127 128 129 130 131 132
  else if(itemTag.compare(XdmfDomain::ItemTag) == 0) {
    return XdmfDomain::New();
  }
  else if(itemTag.compare(XdmfGeometry::ItemTag) == 0) {
    std::map<std::string, std::string>::const_iterator type =
      itemProperties.find("Type");
    if(type == itemProperties.end()) {
      type = itemProperties.find("GeometryType");
133
    }
134 135 136 137
    
    if(type != itemProperties.end()) {
      const std::string & typeVal = type->second;
      if(typeVal.compare("ORIGIN_DXDY") == 0 ||
138 139
         typeVal.compare("ORIGIN_DXDYDZ") == 0 ||
         typeVal.compare("ORIGIN_DISPLACEMENT") == 0) {
140 141 142 143 144 145 146 147 148 149
        shared_ptr<XdmfArray> origin = shared_ptr<XdmfArray>();
        shared_ptr<XdmfArray> brickSize = shared_ptr<XdmfArray>();
        for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
              childItems.begin();
            iter != childItems.end();
            ++iter) {
          if(shared_ptr<XdmfArray> array = 
             shared_dynamic_cast<XdmfArray>(*iter)) {
            if(!origin) {
              origin = array;
150
            }
151 152 153
            else if(!brickSize) {
              brickSize = array;
              break;
154 155
            }
          }
156
        }
157 158 159 160 161 162
        if(origin && brickSize) {
          return XdmfRegularGrid::New(brickSize,
                                      shared_ptr<XdmfArray>(),
                                      origin);
        }
        return shared_ptr<XdmfItem>();
163
      }
164
      else if(typeVal.compare("VXVY") == 0 ||
165 166
              typeVal.compare("VXVYVZ") == 0 ||
              typeVal.compare("VECTORED") == 0) {
167
        std::vector<shared_ptr<XdmfArray> > coordinateValues;
168
        for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
169 170 171
              childItems.begin();
            iter != childItems.end();
            ++iter) {
172 173 174
          if(shared_ptr<XdmfArray> array = 
             shared_dynamic_cast<XdmfArray>(*iter)) {
            coordinateValues.push_back(array);
175 176
          }
        }
177
        return shared_dynamic_cast<XdmfItem>(XdmfRectilinearGrid::New(coordinateValues));
178 179
      }
    }
180 181 182 183 184 185 186 187 188 189 190 191 192
    return XdmfGeometry::New();
  }
  else if(itemTag.compare(XdmfGraph::ItemTag) == 0) {
    return XdmfGraph::New(0);
  }
  else if(itemTag.compare(XdmfGrid::ItemTag) == 0) {
    // For backwards compatibility with the old format, this tag can
    // correspond to multiple XdmfItems.
    std::map<std::string, std::string>::const_iterator gridType =
      itemProperties.find("GridType");
    if(gridType != itemProperties.end() &&
       gridType->second.compare("Collection") == 0) {
      return XdmfGridCollection::New();
193
    }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    else {
      // Find out what kind of grid we have
      for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
            childItems.begin();
          iter != childItems.end();
          ++iter) {
        if(shared_ptr<XdmfCurvilinearGrid> curvilinear =
           shared_dynamic_cast<XdmfCurvilinearGrid>(*iter)) {
          return XdmfCurvilinearGrid::New(0, 0);
        }
        else if(shared_ptr<XdmfRegularGrid> regularGrid =
                shared_dynamic_cast<XdmfRegularGrid>(*iter)) {
          return XdmfRegularGrid::New(0, 0, 0, 0, 0, 0);
        }
        else if(shared_ptr<XdmfRectilinearGrid> rectilinearGrid =
                shared_dynamic_cast<XdmfRectilinearGrid>(*iter)) {
          std::vector<shared_ptr<XdmfArray> > coordinateValues;
          return XdmfRectilinearGrid::New(coordinateValues);
        }
      }
      return XdmfUnstructuredGrid::New();
215
    }
216
  }
217 218 219 220 221 222 223
  else if(itemTag.compare(XdmfGridController::ItemTag) == 0) {
    std::map<std::string, std::string>::const_iterator filename =
      itemProperties.find("File");
    std::map<std::string, std::string>::const_iterator xpath =
      itemProperties.find("XPath");
    return XdmfGridController::New(filename->second, xpath->second);
  }
224 225 226 227 228 229 230 231 232 233 234 235
  else if(itemTag.compare(XdmfInformation::ItemTag) == 0) {
    return XdmfInformation::New();
  }
  else if(itemTag.compare(XdmfMap::ItemTag) == 0) {
    return XdmfMap::New();
  }
  else if(itemTag.compare(XdmfSet::ItemTag) == 0) {
    return XdmfSet::New();
  }
  else if(itemTag.compare(XdmfSparseMatrix::ItemTag) == 0) {
    return XdmfSparseMatrix::New(0, 0);
  }
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
  else if (itemTag.compare(XdmfTemplate::ItemTag) == 0) {
    std::map<std::string, std::string>::const_iterator type =
      itemProperties.find("BaseType");
    if(type == itemProperties.end()) {
      return XdmfTemplate::New();
    }
    else {
      if (type->second.compare("Grid") == 0) {
        return XdmfGridTemplate::New();
      }
      else {
        return XdmfTemplate::New();
      }
    }
    return XdmfTemplate::New();
  }
252 253 254 255 256 257 258 259
  else if(itemTag.compare(XdmfTime::ItemTag) == 0) {
    return XdmfTime::New();
  }
  else if(itemTag.compare(XdmfTopology::ItemTag) == 0) {
    std::map<std::string, std::string>::const_iterator type =
      itemProperties.find("Type");
    if(type == itemProperties.end()) {
      type = itemProperties.find("TopologyType");
260
    }
261

262 263 264 265 266 267 268 269
    if(type != itemProperties.end()) {
      std::string typeVal = type->second;
      std::transform(typeVal.begin(),
                     typeVal.end(),
                     typeVal.begin(),
                     (int(*)(int))toupper);
      if(typeVal.compare("2DCORECTMESH") == 0 ||
         typeVal.compare("3DCORECTMESH") == 0 ||
270
         typeVal.compare("CORECTMESH") == 0 ||
271
         typeVal.compare("2DSMESH") == 0 ||
272 273
         typeVal.compare("3DSMESH") == 0 ||
         typeVal.compare("SMESH") == 0) {
274 275 276 277 278 279 280
        shared_ptr<XdmfArray> dimensionsArray = XdmfArray::New();
        std::string dimensionsString = "";
        std::map<std::string, std::string>::const_iterator dimensions =
          itemProperties.find("Dimensions");
        if(dimensions != itemProperties.end()) {
          dimensionsString = dimensions->second;
        }
281
#ifdef HAVE_CXX11_SHARED_PTR
282
        char * dimString = strdup(dimensionsString.c_str());
283 284 285
        char * token = std::strtok(dimString, " ");
        while (token != NULL)
        {
286
          dimensionsArray->pushBack(atoi(token));
287 288 289 290 291 292
          token = std::strtok(NULL, " ");
        }

        free(dimString);
        dimString = NULL;
#else
293 294 295 296 297 298
        boost::tokenizer<> tokens(dimensionsString);
        for(boost::tokenizer<>::const_iterator iter = tokens.begin();
            iter != tokens.end();
            ++iter) {
          dimensionsArray->pushBack<unsigned int>(atoi((*iter).c_str()));
        }
299
#endif
300
        if(typeVal.compare("2DCORECTMESH") == 0 ||
301 302
           typeVal.compare("3DCORECTMESH") == 0 ||
           typeVal.compare("CORECTMESH") == 0) {
303 304 305
          return XdmfRegularGrid::New(shared_ptr<XdmfArray>(),
                                      dimensionsArray,
                                      shared_ptr<XdmfArray>());
306
        }
307 308
        else {
          return XdmfCurvilinearGrid::New(dimensionsArray);
309
        }
310
      }
311
      else if(typeVal.compare("2DRECTMESH") == 0 ||
312 313
              typeVal.compare("3DRECTMESH") == 0 ||
              typeVal.compare("RECTMESH") == 0) {
314 315 316 317
        std::vector<shared_ptr<XdmfArray> > coordinateValues;
        return XdmfRectilinearGrid::New(coordinateValues);
      }

318
    }
319
    return XdmfTopology::New();
320
  }
321
  return shared_ptr<XdmfItem>();
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

bool
XdmfItemFactory::isArrayTag(char * tag) const
{
#ifdef XDMF_BUILD_DSM
  if (XdmfDSMItemFactory::isArrayTag(tag))
  {
    return true;
  }
#else
  if (XdmfCoreItemFactory::isArrayTag(tag))
  {
    return true;
  }
#endif
  else if (XdmfAggregate::ItemTag.compare(tag) == 0) {
    return true;
  }
  else {
    return false;
  }
}

XdmfItem *
XdmfItemFactory::DuplicatePointer(shared_ptr<XdmfItem> original) const
{
#ifdef XDMF_BUILD_DSM
  XdmfItem * returnPointer = XdmfDSMItemFactory::DuplicatePointer(original);
#else
  XdmfItem * returnPointer = XdmfCoreItemFactory::DuplicatePointer(original);
#endif

  if (returnPointer) {
    return returnPointer;
  }
  else {
   if (original->getItemTag().compare(XdmfTime::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfTime(*((XdmfTime *)original.get())));
   }
   else if (original->getItemTag().compare(XdmfAttribute::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfAttribute(*((XdmfAttribute *)original.get())));
   }
   else if (original->getItemTag().compare(XdmfDomain::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfDomain(*(shared_dynamic_cast<XdmfDomain>(original).get())));
   }
   else if (original->getItemTag().compare(XdmfTopology::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfTopology(*((XdmfTopology *)original.get())));
   }
   else if (original->getItemTag().compare(XdmfGeometry::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfGeometry(*((XdmfGeometry *)original.get())));
   }
   else if (original->getItemTag().compare(XdmfGraph::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfGraph(*((XdmfGraph *)original.get())));
   }
   else if (original->getItemTag().compare(XdmfSet::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfSet(*((XdmfSet *)original.get())));
   }
   else if (original->getItemTag().compare(XdmfMap::ItemTag) == 0) {
     return (XdmfItem *)(new XdmfMap(*((XdmfMap *)original.get())));
   }
   else if (original->getItemTag().compare(XdmfGrid::ItemTag) == 0) {
       if (shared_ptr<XdmfGridCollection> collection =
           shared_dynamic_cast<XdmfGridCollection>(original)) {
         return (XdmfItem *)(new XdmfGridCollection(*(shared_dynamic_cast<XdmfGridCollection>(original).get())));
       }
       else if (shared_ptr<XdmfCurvilinearGrid> curvilinear =
           shared_dynamic_cast<XdmfCurvilinearGrid>(original)) {
         return (XdmfItem *)(new XdmfCurvilinearGrid(*(shared_dynamic_cast<XdmfCurvilinearGrid>(original).get())));
       }
       else if(shared_ptr<XdmfRegularGrid> regularGrid =
               shared_dynamic_cast<XdmfRegularGrid>(original)) {
           return (XdmfItem *)(new XdmfRegularGrid(*(shared_dynamic_cast<XdmfRegularGrid>(original).get())));
       }
       else if(shared_ptr<XdmfRectilinearGrid> rectilinearGrid =
               shared_dynamic_cast<XdmfRectilinearGrid>(original)) {
         return (XdmfItem *)(new XdmfRectilinearGrid(*(shared_dynamic_cast<XdmfRectilinearGrid>(original).get())));
       }
       return (XdmfItem *)(new XdmfUnstructuredGrid(*(shared_dynamic_cast<XdmfUnstructuredGrid>(original).get())));
   }
  }
  return NULL;
}