XdmfItemFactory.cpp 13.8 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"
Kenneth Leiter's avatar
Kenneth Leiter committed
33
#include "XdmfGraph.hpp"
34
#include "XdmfGridCollection.hpp"
35
#include "XdmfInformation.hpp"
36
#include "XdmfItemFactory.hpp"
37
#include "XdmfAggregate.hpp"
38
#include "XdmfMap.hpp"
39 40
#include "XdmfRectilinearGrid.hpp"
#include "XdmfRegularGrid.hpp"
41
#include "XdmfSet.hpp"
Kenneth Leiter's avatar
Kenneth Leiter committed
42
#include "XdmfSparseMatrix.hpp"
43
#include "XdmfTime.hpp"
44
#include "XdmfTopology.hpp"
45
#include "XdmfUnstructuredGrid.hpp"
46

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

54 55 56 57 58 59 60 61
XdmfItemFactory::XdmfItemFactory()
{
}

XdmfItemFactory::~XdmfItemFactory()
{
}

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

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

243 244 245 246 247 248 249 250
    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 ||
251
         typeVal.compare("CORECTMESH") == 0 ||
252
         typeVal.compare("2DSMESH") == 0 ||
253 254
         typeVal.compare("3DSMESH") == 0 ||
         typeVal.compare("SMESH") == 0) {
255 256 257 258 259 260 261 262 263 264 265 266 267
        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;
        }
        boost::tokenizer<> tokens(dimensionsString);
        for(boost::tokenizer<>::const_iterator iter = tokens.begin();
            iter != tokens.end();
            ++iter) {
          dimensionsArray->pushBack<unsigned int>(atoi((*iter).c_str()));
        }
268
        if(typeVal.compare("2DCORECTMESH") == 0 ||
269 270
           typeVal.compare("3DCORECTMESH") == 0 ||
           typeVal.compare("CORECTMESH") == 0) {
271 272 273
          return XdmfRegularGrid::New(shared_ptr<XdmfArray>(),
                                      dimensionsArray,
                                      shared_ptr<XdmfArray>());
274
        }
275 276
        else {
          return XdmfCurvilinearGrid::New(dimensionsArray);
277
        }
278
      }
279
      else if(typeVal.compare("2DRECTMESH") == 0 ||
280 281
              typeVal.compare("3DRECTMESH") == 0 ||
              typeVal.compare("RECTMESH") == 0) {
282 283 284 285
        std::vector<shared_ptr<XdmfArray> > coordinateValues;
        return XdmfRectilinearGrid::New(coordinateValues);
      }

286
    }
287
    return XdmfTopology::New();
288
  }
289
  return shared_ptr<XdmfItem>();
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

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