/[escript]/branches/arrexp_2137_win/escript/src/DataTagged.cpp
ViewVC logotype

Diff of /branches/arrexp_2137_win/escript/src/DataTagged.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/escript/src/Data/DataTagged.cpp revision 82 by jgs, Tue Oct 26 06:53:54 2004 UTC trunk/escript/src/DataTagged.cpp revision 519 by jgs, Tue Feb 14 05:30:50 2006 UTC
# Line 1  Line 1 
1  // $Id$  // $Id$
2    
3  /*  /*
4   ******************************************************************************   ******************************************************************************
5   *                                                                            *   *                                                                            *
# Line 13  Line 14 
14   ******************************************************************************   ******************************************************************************
15  */  */
16    
17  #include "escript/Data/DataTagged.h"  #include "DataTagged.h"
 #include "escript/Data/DataConstant.h"  
18    
19  #include <sstream>  #include "DataConstant.h"
20    #include "DataException.h"
21    
22  using namespace std;  using namespace std;
23    
24  namespace escript {  namespace escript {
25    
26  DataTagged::DataTagged():  DataTagged::DataTagged()
27    DataAbstract(FunctionSpace())    : DataAbstract(FunctionSpace())
28  {  {
29    //    // default constructor
30    
31    // create a scalar default value    // create a scalar default value
32    m_data.push_back(0.0);    m_data.resize(1,0.,1);
33    DataArrayView temp(m_data,DataArrayView::ShapeType());    DataArrayView temp(m_data,DataArrayView::ShapeType());
34    setPointDataView(temp);    setPointDataView(temp);
35  }  }
# Line 35  DataTagged::DataTagged(): Line 37  DataTagged::DataTagged():
37  DataTagged::DataTagged(const TagListType& tagKeys,  DataTagged::DataTagged(const TagListType& tagKeys,
38                 const ValueListType& values,                 const ValueListType& values,
39                 const DataArrayView& defaultValue,                 const DataArrayView& defaultValue,
40                 const FunctionSpace& what):                 const FunctionSpace& what)
41    DataAbstract(what)    : DataAbstract(what)
42  {  {
43    //    // constructor
44    // The default value is always the first item in the values list  
45    m_data.insert(m_data.end(), &defaultValue.getData(0), &defaultValue.getData(defaultValue.noValues()) );    // initialise the array of data values
46      // the default value is always the first item in the values list
47      int len = defaultValue.noValues();
48      m_data.resize(len,0.,len);
49      for (int i=0; i<defaultValue.noValues(); i++) {
50        m_data[i]=defaultValue.getData(i);
51      }
52    
53      // create the data view
54    DataArrayView temp(m_data,defaultValue.getShape());    DataArrayView temp(m_data,defaultValue.getShape());
55    setPointDataView(temp);    setPointDataView(temp);
56    
57    // add remaining tags and values    // add remaining tags and values
58    addTaggedValues(tagKeys,values);    addTaggedValues(tagKeys,values);
59  }  }
60    
61  DataTagged::DataTagged(const DataTagged& other):  DataTagged::DataTagged(const FunctionSpace& what,
62    DataAbstract(other.getFunctionSpace()),                         const DataArrayView::ShapeType &shape,
63                           const int tags[],
64                           const ValueType& data)
65      : DataAbstract(what)
66    {
67      // alternative constructor
68      // not unit_tested tested yet
69    
70      // copy the data
71      m_data=data;
72    
73      // create the view of the data
74      DataArrayView tempView(m_data,shape);
75      setPointDataView(tempView);
76    
77      // create the tag lookup map
78      for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
79        m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
80      }
81    }
82    
83    DataTagged::DataTagged(const DataTagged& other)
84      : DataAbstract(other.getFunctionSpace()),
85    m_data(other.m_data),    m_data(other.m_data),
86    m_offsetLookup(other.m_offsetLookup)    m_offsetLookup(other.m_offsetLookup)
87  {  {
88      // copy constructor
89    
90      // create the data view
91    DataArrayView temp(m_data,other.getPointDataView().getShape());    DataArrayView temp(m_data,other.getPointDataView().getShape());
92    setPointDataView(temp);    setPointDataView(temp);
93  }  }
94    
95  DataTagged::DataTagged(const DataConstant& other):  DataTagged::DataTagged(const DataConstant& other)
96    DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
97  {  {
98    //    // copy constructor
99    // Fill the default value with the constant value item from other  
100      // fill the default value with the constant value item from "other"
101    const DataArrayView& value=other.getPointDataView();    const DataArrayView& value=other.getPointDataView();
102    m_data.insert(m_data.end(), &value.getData(0), &value.getData(value.noValues()) );    int len = value.noValues();
103    DataArrayView temp(m_data,value.getShape());    m_data.resize(len,0.,len);
104    setPointDataView(temp);    for (int i=0; i<value.noValues(); i++) {
105  }      m_data[i]=value.getData(i);
106      }
107    
108  DataTagged::DataTagged(const DataTagged& other,    // create the data view
109                 const DataArrayView::RegionType& region):    DataArrayView temp(m_data,value.getShape());
   DataAbstract(other.getFunctionSpace())  
 {  
   //  
   // get the shape of the slice to copy from other  
   DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));  
   //  
   // allocate enough space for all values  
   m_data.resize(DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1));  
   DataArrayView temp(m_data,shape);  
110    setPointDataView(temp);    setPointDataView(temp);
   getDefaultValue().copySlice(other.getDefaultValue(),region);  
   //  
   // Loop through the tag values  
   DataMapType::const_iterator pos;  
   DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();  
   for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();++pos){  
     getPointDataView().copySlice(tagOffset,other.getPointDataView(), pos->second,region);  
     m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));  
     tagOffset+=getPointDataView().noValues();  
   }  
111  }  }
112    
113  void  void
114  DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)  DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)
115  {  {
   //  
116    // can only reshape a rank zero data point    // can only reshape a rank zero data point
117    if (getPointDataView().getRank()!=0) {    if (getPointDataView().getRank()!=0) {
118      stringstream temp;      stringstream temp;
# Line 103  DataTagged::reshapeDataPoint(const DataA Line 121  DataTagged::reshapeDataPoint(const DataA
121       << getPointDataView().getRank();       << getPointDataView().getRank();
122      throw DataException(temp.str());      throw DataException(temp.str());
123    }    }
124    //  
125    // allocate enough space for all values    // allocate enough space for all values
126    DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));    DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));
127    DataArrayView newView(newData,shape);    DataArrayView newView(newData,shape);
128    newView.copy(0,getDefaultValue()());    newView.copy(0,getDefaultValue()());
129    //  
130    // Loop through the tag values    // loop through the tag values
131    DataMapType::iterator pos;    DataMapType::iterator pos;
132    DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);    DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
133    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){
134      newView.copy(tagOffset,m_data[pos->second]);      newView.copy(tagOffset,m_data[pos->second]);
135      pos->second=tagOffset;      pos->second=tagOffset;
136      tagOffset+=DataArrayView::noValues(shape);      tagOffset+=DataArrayView::noValues(shape);
# Line 125  DataTagged::reshapeDataPoint(const DataA Line 143  DataTagged::reshapeDataPoint(const DataA
143  DataAbstract*  DataAbstract*
144  DataTagged::getSlice(const DataArrayView::RegionType& region) const  DataTagged::getSlice(const DataArrayView::RegionType& region) const
145  {  {
146    return new DataTagged(*this,region);    return new DataTagged(*this, region);
147    }
148    
149    DataTagged::DataTagged(const DataTagged& other,
150                   const DataArrayView::RegionType& region)
151      : DataAbstract(other.getFunctionSpace())
152    {
153      // slice constructor
154    
155      // get the shape of the slice to copy from other
156      DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
157      DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
158    
159      // allocate enough space in this for all values
160      // (need to add one to allow for the default value)
161      int len = DataArrayView::noValues(regionShape)*(other.m_offsetLookup.size()+1);
162      m_data.resize(len,0.0,len);
163    
164      // create the data view
165      DataArrayView temp(m_data,regionShape);
166      setPointDataView(temp);
167    
168      // copy the default value from other to this
169      getDefaultValue().copySlice(other.getDefaultValue(), regionLoopRange);
170    
171      // loop through the tag values copying these
172      DataMapType::const_iterator pos;
173      DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
174      for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
175        getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,regionLoopRange);
176        m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
177        tagOffset+=getPointDataView().noValues();
178      }
179  }  }
180    
181  void  void
182  DataTagged::setSlice(const DataAbstract* value,  DataTagged::setSlice(const DataAbstract* other,
183                       const DataArrayView::RegionType& region)                       const DataArrayView::RegionType& region)
184  {  {
185    const DataTagged* tempDataTag=dynamic_cast<const DataTagged*>(value);  
186    if (tempDataTag==0) {    // other must be another DataTagged object
187      // Data:setSlice implementation should ensure this
188      const DataTagged* otherTemp=dynamic_cast<const DataTagged*>(other);
189      if (otherTemp==0) {
190      throw DataException("Programming error - casting to DataTagged.");      throw DataException("Programming error - casting to DataTagged.");
191    }    }
192    
193      // determine shape of the specified region
194      DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
195    
196      // modify region specification as needed to match rank of this object
197      DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
198    
199      // ensure rank/shape of this object is compatible with specified region
200    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
201      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
202    }    }
203    //    if (otherTemp->getPointDataView().getRank()>0 and !other->getPointDataView().checkShape(regionShape)) {
204    // get the shape of the slice      throw DataException (other->getPointDataView().createShapeErrorMessage(
205    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));                           "Error - Couldn't copy slice due to shape mismatch.",regionShape));
   if (!value->getPointDataView().checkShape(shape)) {  
     throw DataException (value->getPointDataView().createShapeErrorMessage(  
                 "Error - Couldn't copy slice due to shape mismatch.",shape));  
206    }    }
207    getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region);  
208    //    // copy slice from other default value to this default value
209    // Loop through the tag values    getDefaultValue().copySliceFrom(otherTemp->getDefaultValue(), regionLoopRange);
210    
211      // loop through the tag values copying slices from other to this
212    DataMapType::const_iterator pos;    DataMapType::const_iterator pos;
213    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
214      getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region);      getDataPointByTag(pos->first).copySliceFrom(otherTemp->getDataPointByTag(pos->first), regionLoopRange);
215    }    }
216    
217  }  }
218    
219  void  int
220  DataTagged::setTaggedValue(int tagKey,  DataTagged::getTagNumber(int dpno)
                            const DataArrayView& value)  
221  {  {
222    DataMapType::iterator pos(m_offsetLookup.find(tagKey));    //
223    if (pos==m_offsetLookup.end()) {    // Get the number of samples and data-points per sample
224      //    int numSamples = getNumSamples();
225      // tag couldn't be found so add as a new tag    int numDataPointsPerSample = getNumDPPSample();
226      addTaggedValue(tagKey,value);    int numDataPoints = numSamples * numDataPointsPerSample;
227    } else {  
228      if (!getPointDataView().checkShape(value.getShape())) {    if (numDataPointsPerSample==0) {
229        throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException("DataTagged::getTagNumber error: no data-points associated with this object.");
230           "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));    }
231      }  
232      //    if (dpno<0 || dpno>numDataPoints-1) {
233      // copy the values into tagged data storage      throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied.");
     copy(&value.getData(0), &value.getData(getPointDataView().noValues()), &m_data[pos->second]);  
234    }    }
235    
236      //
237      // Determine the sample number which corresponds to this data-point number
238      int sampleNo = dpno / numDataPointsPerSample;
239    
240      //
241      // Determine the tag number which corresponds to this sample number
242      int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo);
243    
244      //
245      // return the tag number
246      return(tagNo);
247  }  }
248    
249  void  void
250  DataTagged::setTaggedValues(const TagListType& tagKeys,  DataTagged::setTaggedValues(const TagListType& tagKeys,
251                              const ValueListType& values)                              const ValueListType& values)
252  {  {
253    for (int i=0;i<tagKeys.size();++i) {    addTaggedValues(tagKeys,values);
     setTaggedValue(tagKeys[i],values[i]);  
   }  
254  }  }
255    
256  void  void
257  DataTagged::addTaggedValue(int tagKey,  DataTagged::setTaggedValue(int tagKey,
258                             const DataArrayView& value)                             const DataArrayView& value)
259  {  {
260    if (!getPointDataView().checkShape(value.getShape())) {    if (!getPointDataView().checkShape(value.getShape())) {
261      throw DataException(getPointDataView().createShapeErrorMessage(        throw DataException(getPointDataView().createShapeErrorMessage(
262           "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));                            "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
263      }
264      DataMapType::iterator pos(m_offsetLookup.find(tagKey));
265      if (pos==m_offsetLookup.end()) {
266        // tag couldn't be found so use addTaggedValue
267        addTaggedValue(tagKey,value);
268      } else {
269        // copy the values into the data array at the offset determined by m_offsetLookup
270        int offset=pos->second;
271        for (int i=0; i<getPointDataView().noValues(); i++) {
272          m_data[offset+i]=value.getData(i);
273        }
274    }    }
   //  
   // save the key and the location of its data  
   m_offsetLookup.insert( DataMapType::value_type(tagKey,m_data.size()) );  
   //  
   // insert the data given in value at the end of m_data  
   m_data.insert( m_data.end(), &(value.getData(0)), &(value.getData(value.noValues())) );  
275  }  }
276    
277  void  void
# Line 205  DataTagged::addTaggedValues(const TagLis Line 279  DataTagged::addTaggedValues(const TagLis
279                              const ValueListType& values)                              const ValueListType& values)
280  {  {
281    if (values.size()==0) {    if (values.size()==0) {
282      //      // copy the current default value for each of the tags
     // Copy the default value for each of the tags  
283      TagListType::const_iterator iT;      TagListType::const_iterator iT;
284      for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {      for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
       //  
285        // the point data view for DataTagged points at the default value        // the point data view for DataTagged points at the default value
286        addTaggedValue(*iT,getPointDataView());        addTaggedValue(*iT,getPointDataView());
287      }      }
288    } else if (values.size()==1 && tagKeys.size()>1) {    } else if (values.size()==1 && tagKeys.size()>1) {
289      //      // assume the one given value will be used for all tag values
     // assume the one value will be used for all tag values  
     // Copy the input data  
290      TagListType::const_iterator iT;      TagListType::const_iterator iT;
291      for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {      for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
292        addTaggedValue(*iT,values[0]);        addTaggedValue(*iT,values[0]);
293      }      }
294    } else {    } else {
295      if (tagKeys.size()!=values.size()) {      if (tagKeys.size()!=values.size()) {
296        stringstream temp;        stringstream temp;
297        temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()        temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
298         << " doesn't match the number of values: " << values.size();         << " doesn't match number of values: " << values.size();
299        throw DataException(temp.str());        throw DataException(temp.str());
300      } else {      } else {
301        for (int i=0;i<tagKeys.size();++i) {        for (int i=0;i<tagKeys.size();i++) {
302          addTaggedValue(tagKeys[i],values[i]);          addTaggedValue(tagKeys[i],values[i]);
303        }        }
304      }      }
305    }    }
306  }  }
307    
308    void
309    DataTagged::addTaggedValue(int tagKey,
310                               const DataArrayView& value)
311    {
312      if (!getPointDataView().checkShape(value.getShape())) {
313        throw DataException(getPointDataView().createShapeErrorMessage(
314                            "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
315      }
316      DataMapType::iterator pos(m_offsetLookup.find(tagKey));
317      if (pos!=m_offsetLookup.end()) {
318        // tag already exists so use setTaggedValue
319        setTaggedValue(tagKey,value);
320      } else {
321        // save the key and the location of its data in the lookup tab
322        m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size()));
323        // add the data given in "value" at the end of m_data
324        // need to make a temp copy of m_data, resize m_data, then copy
325        // all the old values plus the value to be added back into m_data
326        ValueType m_data_temp(m_data);
327        int oldSize=m_data.size();
328        int newSize=m_data.size()+value.noValues();
329        m_data.resize(newSize,0.,newSize);
330        for (int i=0;i<oldSize;i++) {
331          m_data[i]=m_data_temp[i];
332        }
333        for (int i=0;i<value.noValues();i++) {
334          m_data[oldSize+i]=value.getData(i);
335        }
336      }
337    }
338    
339  double*  double*
340  DataTagged::getSampleDataByTag(int tag)  DataTagged::getSampleDataByTag(int tag)
341  {  {
342    DataMapType::iterator pos(m_offsetLookup.find(tag));    DataMapType::iterator pos(m_offsetLookup.find(tag));
343    if (pos==m_offsetLookup.end()) {    if (pos==m_offsetLookup.end()) {
     //  
344      // tag couldn't be found so return the default value      // tag couldn't be found so return the default value
345      return &(m_data[0]);      return &(m_data[0]);
346    } else {    } else {
     //  
347      // return the data-point corresponding to the given tag      // return the data-point corresponding to the given tag
348      return &(m_data[pos->second]);      return &(m_data[pos->second]);
349    }    }
# Line 257  DataTagged::toString() const Line 356  DataTagged::toString() const
356    DataMapType::const_iterator i;    DataMapType::const_iterator i;
357    temp << "Tag(Default)" << endl;    temp << "Tag(Default)" << endl;
358    temp << getDefaultValue().toString() << endl;    temp << getDefaultValue().toString() << endl;
   //  
359    // create a temporary view as the offset will be changed    // create a temporary view as the offset will be changed
360    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
361    for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {    for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
# Line 268  DataTagged::toString() const Line 366  DataTagged::toString() const
366    return temp.str();    return temp.str();
367  }  }
368    
369  DataArrayView  DataArrayView::ValueType::size_type
370  DataTagged::getDataPointByTag(int tag) const  DataTagged::getPointOffset(int sampleNo,
371                               int dataPointNo) const
372  {  {
373    DataMapType::const_iterator pos(m_offsetLookup.find(tag));    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
374      DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
375    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
376    if (pos!=m_offsetLookup.end()) {    if (pos!=m_offsetLookup.end()) {
377      offset=pos->second;      offset=pos->second;
378    }    }
379    DataArrayView temp(getPointDataView());    return offset;
   temp.setOffset(offset);  
   return temp;  
380  }  }
381    
382  DataArrayView::ValueType::size_type  DataArrayView
383  DataTagged::getPointOffset(int sampleNo,  DataTagged::getDataPointByTag(int tag) const
                            int dataPointNo) const  
384  {  {
385    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);    DataMapType::const_iterator pos(m_offsetLookup.find(tag));
   DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));  
386    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
387    if (pos!=m_offsetLookup.end()) {    if (pos!=m_offsetLookup.end()) {
388      offset=pos->second;      offset=pos->second;
389    }    }
390    return offset;    DataArrayView temp(getPointDataView());
391      temp.setOffset(offset);
392      return temp;
393  }  }
394    
395  DataArrayView  DataArrayView
# Line 303  DataTagged::getDataPoint(int sampleNo, Line 401  DataTagged::getDataPoint(int sampleNo,
401    return getDataPointByTag(tagKey);    return getDataPointByTag(tagKey);
402  }  }
403    
404  const DataTagged::DataMapType&  int
405  DataTagged::getTagLookup() const  DataTagged::archiveData(ofstream& archiveFile,
406                            const DataArrayView::ValueType::size_type noValues) const
407  {  {
408    return m_offsetLookup;    return(m_data.archiveData(archiveFile, noValues));
409  }  }
410    
411  DataArrayView::ValueType::size_type  int
412  DataTagged::getLength() const  DataTagged::extractData(ifstream& archiveFile,
413                            const DataArrayView::ValueType::size_type noValues)
414  {  {
415    return m_data.size();    return(m_data.extractData(archiveFile, noValues));
416  }  }
417    
418  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.82  
changed lines
  Added in v.519

  ViewVC Help
Powered by ViewVC 1.1.26