XdmfCoreItemFactory.cpp 8.09 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 : XdmfCoreItemFactory.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 "XdmfArray.hpp"
25
#include "XdmfCoreItemFactory.hpp"
26
#include "XdmfError.hpp"
27
28
#include "XdmfFunction.hpp"
#include "XdmfSubset.hpp"
29
30
#include "XdmfInformation.hpp"
#include "XdmfSparseMatrix.hpp"
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
31
#include <boost/tokenizer.hpp>
32
33
34
35
36
37
38
39

XdmfCoreItemFactory::XdmfCoreItemFactory()
{
}

XdmfCoreItemFactory::~XdmfCoreItemFactory()
{
}
40

41
shared_ptr<XdmfItem>
42
XdmfCoreItemFactory::createItem(const std::string & itemTag,
43
44
                                const std::map<std::string, std::string> & itemProperties,
                                const std::vector<shared_ptr<XdmfItem> > & childItems) const
45
{
46
47
48
  if(itemTag.compare(XdmfArray::ItemTag) == 0) {
    return XdmfArray::New();
  }
49
50
51
52
  else if(itemTag.compare("DataStructure") == 0) {
    // to support old xdmf DataStructure tag
    return XdmfArray::New();
  }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  else if (itemTag.compare(XdmfFunction::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::map<std::string, std::string>::const_iterator expression =
      itemProperties.find("Expression");
    std::string expressionToParse;
    if(expression == itemProperties.end()) {
68
69
      XdmfError::message(XdmfError::FATAL,
                         "Error: Function found no expression");
70
71
72
73
74
75
76
77
78
    }
    else {
      expressionToParse = expression->second;
    }
    std::map<std::string, shared_ptr<XdmfArray> > variableCollection;
    for (unsigned int i = 0; i < childItems.size(); ++i) {
      try {
        shared_ptr<XdmfArray> tempArray =
          shared_dynamic_cast<XdmfArray>(childItems[i]);
79
80
81
82
83
        if (tempArray->getName().compare("") != 0)
        {
          variableCollection[tempArray->getName()] = tempArray;
          tempArray->read();
        }
84
85
      }
      catch (...) {
86
87
        XdmfError::message(XdmfError::FATAL,
                           "Error: Function passed non-Array item");
88
89
90
      }
    }
    shared_ptr<XdmfArray> parsedArray = shared_ptr<XdmfArray>();
91
92
    parsedArray = XdmfFunction::evaluateExpression(expressionToParse,
                                                   variableCollection);
93
94
95
96
    if (arraySubType != XdmfArray::ItemTag) {
      // The properties and children aren't really needed
      // to generate the object, but the factory still requires them.
      std::vector<shared_ptr<XdmfItem> > newArrayChildren;
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
97
      shared_ptr<XdmfArray> returnArray = shared_ptr<XdmfArray>();
98
99
100
101
102
103
104
105
106
107

      // This should generate an item that corresponds to the tag provided
      // the casting ensures that it is a subtype of array
      // Using a factory to be able to build things outside of core
      returnArray = shared_dynamic_cast<XdmfArray>(createItem(
                                                     arraySubType,
                                                     itemProperties,
                                                     newArrayChildren));

      returnArray->insert(0, parsedArray, 0, parsedArray->getSize());
108
109
110
      returnArray->setReference(XdmfFunction::New(expressionToParse,
                                                  variableCollection));
      returnArray->setReadMode(XdmfArray::Reference);
111
112
113
      return returnArray;
    }
    else {
114
115
116
      parsedArray->setReference(XdmfFunction::New(expressionToParse,
                                                  variableCollection));
      parsedArray->setReadMode(XdmfArray::Reference);
117
118
119
      return parsedArray;
    }
  }
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  else if(itemTag.compare(XdmfSubset::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<XdmfArray> returnArray = shared_ptr<XdmfArray>();

    returnArray = shared_dynamic_cast<XdmfArray>(createItem(
                                                   arraySubType,
                                                   itemProperties,
                                                   newArrayChildren));

    std::vector<unsigned int> startVector;
    std::vector<unsigned int> strideVector;
    std::vector<unsigned int> dimensionVector;
    shared_ptr<XdmfArray> referenceArray;

    std::map<std::string, std::string>::const_iterator starts =
      itemProperties.find("SubsetStarts");

    boost::tokenizer<> tokens(starts->second);
    for(boost::tokenizer<>::const_iterator iter = tokens.begin();
        iter != tokens.end();
        ++iter) {
      startVector.push_back(atoi((*iter).c_str()));
    }

    std::map<std::string, std::string>::const_iterator strides =
      itemProperties.find("SubsetStrides");

    boost::tokenizer<> stridetokens(strides->second);
    for(boost::tokenizer<>::const_iterator iter = stridetokens.begin();
        iter != stridetokens.end();
        ++iter) {
      strideVector.push_back(atoi((*iter).c_str()));
    }

    std::map<std::string, std::string>::const_iterator dimensions =
      itemProperties.find("SubsetDimensions");

    boost::tokenizer<> dimtokens(dimensions->second);
    for(boost::tokenizer<>::const_iterator iter = dimtokens.begin();
        iter != dimtokens.end();
        ++iter) {
      dimensionVector.push_back(atoi((*iter).c_str()));
    }

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
    bool foundspacer = 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 (foundspacer) {
          referenceArray = shared_dynamic_cast<XdmfArray>(array);
          break;
        }
        else {
          foundspacer = true;
        }
      }
    }
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
191
192
193
194
195
196

    shared_ptr<XdmfSubset> newSubset = XdmfSubset::New(referenceArray,
                                                       startVector,
                                                       strideVector,
                                                       dimensionVector);

197
198
    returnArray->setReference(newSubset);
    returnArray->setReadMode(XdmfArray::Reference);
Andrew J. Burns (Cont's avatar
Andrew J. Burns (Cont committed
199
200
201
202

    return returnArray;

  }
203
  return shared_ptr<XdmfItem>();
204
}