XdmfItemFactory.cpp 14.3 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 <cctype>
25
#include <boost/tokenizer.hpp>
26
#include "XdmfAttribute.hpp"
27
#include "XdmfCurvilinearGrid.hpp"
28
#include "XdmfDomain.hpp"
29
#include "XdmfError.hpp"
30
#include "XdmfFunction.hpp"
31
#include "XdmfGeometry.hpp"
32
#include "XdmfGeometryType.hpp"
33
#include "XdmfGraph.hpp"
34
#include "XdmfGridCollection.hpp"
35
#include "XdmfGridTemplate.hpp"
36
#include "XdmfInformation.hpp"
37
#include "XdmfItemFactory.hpp"
38
#include "XdmfAggregate.hpp"
39
#include "XdmfMap.hpp"
40 41
#include "XdmfRectilinearGrid.hpp"
#include "XdmfRegularGrid.hpp"
42
#include "XdmfSet.hpp"
43
#include "XdmfSparseMatrix.hpp"
44
#include "XdmfTemplate.hpp"
45
#include "XdmfTime.hpp"
46
#include "XdmfTopology.hpp"
47
#include "XdmfUnstructuredGrid.hpp"
48

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

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

XdmfItemFactory::~XdmfItemFactory()
{
}

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

81 82 83
  if(itemTag.compare(XdmfAttribute::ItemTag) == 0) {
    return XdmfAttribute::New();
  }
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 122 123
  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;
  }
124 125 126 127 128 129 130 131
  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");
132
    }
133 134 135 136
    
    if(type != itemProperties.end()) {
      const std::string & typeVal = type->second;
      if(typeVal.compare("ORIGIN_DXDY") == 0 ||
137 138
         typeVal.compare("ORIGIN_DXDYDZ") == 0 ||
         typeVal.compare("ORIGIN_DISPLACEMENT") == 0) {
139 140 141 142 143 144 145 146 147 148
        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;
149
            }
150 151 152
            else if(!brickSize) {
              brickSize = array;
              break;
153 154
            }
          }
155
        }
156 157 158 159 160 161
        if(origin && brickSize) {
          return XdmfRegularGrid::New(brickSize,
                                      shared_ptr<XdmfArray>(),
                                      origin);
        }
        return shared_ptr<XdmfItem>();
162
      }
163
      else if(typeVal.compare("VXVY") == 0 ||
164 165
              typeVal.compare("VXVYVZ") == 0 ||
              typeVal.compare("VECTORED") == 0) {
166
        std::vector<shared_ptr<XdmfArray> > coordinateValues;
167
        for(std::vector<shared_ptr<XdmfItem> >::const_iterator iter =
168 169 170
              childItems.begin();
            iter != childItems.end();
            ++iter) {
171 172 173
          if(shared_ptr<XdmfArray> array = 
             shared_dynamic_cast<XdmfArray>(*iter)) {
            coordinateValues.push_back(array);
174 175
          }
        }
176
        return shared_dynamic_cast<XdmfItem>(XdmfRectilinearGrid::New(coordinateValues));
177 178
      }
    }
179 180 181 182 183 184 185 186 187 188 189 190 191
    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();
192
    }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    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();
214
    }
215
  }
216 217 218 219 220 221 222
  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);
  }
223 224 225 226 227 228 229 230 231 232 233 234
  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);
  }
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  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();
  }
251 252 253 254 255 256 257 258
  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");
259
    }
260

261 262 263 264 265 266 267 268
    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 ||
269
         typeVal.compare("CORECTMESH") == 0 ||
270
         typeVal.compare("2DSMESH") == 0 ||
271 272
         typeVal.compare("3DSMESH") == 0 ||
         typeVal.compare("SMESH") == 0) {
273 274 275 276 277 278 279
        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;
        }
280 281 282 283 284 285
        boost::tokenizer<> tokens(dimensionsString);
        for(boost::tokenizer<>::const_iterator iter = tokens.begin();
            iter != tokens.end();
            ++iter) {
          dimensionsArray->pushBack<unsigned int>(atoi((*iter).c_str()));
        }
286
        if(typeVal.compare("2DCORECTMESH") == 0 ||
287 288
           typeVal.compare("3DCORECTMESH") == 0 ||
           typeVal.compare("CORECTMESH") == 0) {
289 290 291
          return XdmfRegularGrid::New(shared_ptr<XdmfArray>(),
                                      dimensionsArray,
                                      shared_ptr<XdmfArray>());
292
        }
293 294
        else {
          return XdmfCurvilinearGrid::New(dimensionsArray);
295
        }
296
      }
297
      else if(typeVal.compare("2DRECTMESH") == 0 ||
298 299
              typeVal.compare("3DRECTMESH") == 0 ||
              typeVal.compare("RECTMESH") == 0) {
300 301 302 303
        std::vector<shared_ptr<XdmfArray> > coordinateValues;
        return XdmfRectilinearGrid::New(coordinateValues);
      }

304
    }
305
    return XdmfTopology::New();
306
  }
307
  return shared_ptr<XdmfItem>();
308
}
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

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;
  }
}
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

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;
}