/[escript]/trunk/escript/src/DataTagged.cpp
ViewVC logotype

Diff of /trunk/escript/src/DataTagged.cpp

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

trunk/esys2/escript/src/Data/DataTagged.cpp revision 119 by jgs, Tue Apr 12 04:45:05 2005 UTC trunk/escript/src/DataTagged.cpp revision 509 by jgs, Fri Feb 10 03:24:44 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"  
 #include "escript/Data/DataExpanded.h"  
 #include "escript/Data/DataException.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 40  DataTagged::DataTagged(const TagListType Line 40  DataTagged::DataTagged(const TagListType
40                 const FunctionSpace& what)                 const FunctionSpace& what)
41    : DataAbstract(what)    : DataAbstract(what)
42  {  {
43    //    // constructor
44    // Initialise the array of data values  
45    // The default value is always the first item in the values list    // initialise the array of data values
46    m_data.insert(m_data.end(), &defaultValue.getData(0), &defaultValue.getData(defaultValue.noValues()) );    // 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    // 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  }  }
# Line 54  DataTagged::DataTagged(const TagListType Line 61  DataTagged::DataTagged(const TagListType
61  DataTagged::DataTagged(const FunctionSpace& what,  DataTagged::DataTagged(const FunctionSpace& what,
62                         const DataArrayView::ShapeType &shape,                         const DataArrayView::ShapeType &shape,
63                         const int tags[],                         const int tags[],
64                         const DataArrayView::ValueType &data)                         const ValueType& data)
65    : DataAbstract(what)    : DataAbstract(what)
66  {  {
67    //    // alternative constructor
68    // copy the data in the correct format    // not unit_tested tested yet
69    
70      // copy the data
71    m_data=data;    m_data=data;
72    //  
73    // create the view of the data    // create the view of the data
74    DataArrayView tempView(m_data,shape);    DataArrayView tempView(m_data,shape);
75    setPointDataView(tempView);    setPointDataView(tempView);
76    //  
77    // create the tag lookup map    // create the tag lookup map
78    for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {    for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
79      m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));      m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
# Line 76  DataTagged::DataTagged(const DataTagged& Line 85  DataTagged::DataTagged(const DataTagged&
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    // create the data view
91    DataArrayView temp(m_data,other.getPointDataView().getShape());    DataArrayView temp(m_data,other.getPointDataView().getShape());
92    setPointDataView(temp);    setPointDataView(temp);
# Line 84  DataTagged::DataTagged(const DataTagged& Line 95  DataTagged::DataTagged(const DataTagged&
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      m_data.resize(len,0.,len);
104      for (int i=0; i<value.noValues(); i++) {
105        m_data[i]=value.getData(i);
106      }
107    
108    // create the data view    // create the data view
109    DataArrayView temp(m_data,value.getShape());    DataArrayView temp(m_data,value.getShape());
110    setPointDataView(temp);    setPointDataView(temp);
# Line 97  DataTagged::DataTagged(const DataTagged& Line 114  DataTagged::DataTagged(const DataTagged&
114                 const DataArrayView::RegionType& region)                 const DataArrayView::RegionType& region)
115    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
116  {  {
117    //    // slice constructor
118    
119    // get the shape of the slice to copy from other    // get the shape of the slice to copy from other
120    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
121    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
122    
123    // allocate enough space for all values    // allocate enough space for all values
124    m_data.resize(DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1));    int len = DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1);
125      m_data.resize(len,0.,len);
126    
127    // create the data view    // create the data view
128    DataArrayView temp(m_data,shape);    DataArrayView temp(m_data,shape);
129    setPointDataView(temp);    setPointDataView(temp);
130    
131    // copy the default value    // copy the default value
132    getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range);    getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range);
133    //  
134    // Loop through the tag values copying these    // loop through the tag values copying these
135    DataMapType::const_iterator pos;    DataMapType::const_iterator pos;
136    DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();    DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
137    for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();++pos){    for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
138      getPointDataView().copySlice(tagOffset,other.getPointDataView(), pos->second,region_loop_range);      getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,region_loop_range);
139      m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));      m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
140      tagOffset+=getPointDataView().noValues();      tagOffset+=getPointDataView().noValues();
141    }    }
142  }  }
143    
144  void  void
145  DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)  DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)
146  {  {
   //  
147    // can only reshape a rank zero data point    // can only reshape a rank zero data point
148    if (getPointDataView().getRank()!=0) {    if (getPointDataView().getRank()!=0) {
149      stringstream temp;      stringstream temp;
# Line 131  DataTagged::reshapeDataPoint(const DataA Line 152  DataTagged::reshapeDataPoint(const DataA
152       << getPointDataView().getRank();       << getPointDataView().getRank();
153      throw DataException(temp.str());      throw DataException(temp.str());
154    }    }
155    //  
156    // allocate enough space for all values    // allocate enough space for all values
157    DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));    DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));
158    DataArrayView newView(newData,shape);    DataArrayView newView(newData,shape);
159    newView.copy(0,getDefaultValue()());    newView.copy(0,getDefaultValue()());
160    //  
161    // Loop through the tag values    // loop through the tag values
162    DataMapType::iterator pos;    DataMapType::iterator pos;
163    DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);    DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
164    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){
165      newView.copy(tagOffset,m_data[pos->second]);      newView.copy(tagOffset,m_data[pos->second]);
166      pos->second=tagOffset;      pos->second=tagOffset;
167      tagOffset+=DataArrayView::noValues(shape);      tagOffset+=DataArrayView::noValues(shape);
# Line 164  DataTagged::setSlice(const DataAbstract* Line 185  DataTagged::setSlice(const DataAbstract*
185    if (tempDataTag==0) {    if (tempDataTag==0) {
186      throw DataException("Programming error - casting to DataTagged.");      throw DataException("Programming error - casting to DataTagged.");
187    }    }
188    //  
189    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
190    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
   //  
191    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
192      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
193    }    }
# Line 175  DataTagged::setSlice(const DataAbstract* Line 195  DataTagged::setSlice(const DataAbstract*
195      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (value->getPointDataView().createShapeErrorMessage(
196                  "Error - Couldn't copy slice due to shape mismatch.",shape));                  "Error - Couldn't copy slice due to shape mismatch.",shape));
197    }    }
198    //  
199    getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range);    getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range);
200    //  
201    // Loop through the tag values    // loop through the tag values
202    DataMapType::const_iterator pos;    DataMapType::const_iterator pos;
203    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
204      getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range);      getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range);
205    }    }
206  }  }
207    
208  void  int
209  DataTagged::setTaggedValue(int tagKey,  DataTagged::getTagNumber(int dpno)
                            const DataArrayView& value)  
210  {  {
211    DataMapType::iterator pos(m_offsetLookup.find(tagKey));    //
212    if (pos==m_offsetLookup.end()) {    // Get the number of samples and data-points per sample
213      //    int numSamples = getNumSamples();
214      // tag couldn't be found so add as a new tag    int numDataPointsPerSample = getNumDPPSample();
215      addTaggedValue(tagKey,value);    int numDataPoints = numSamples * numDataPointsPerSample;
216    } else {  
217      if (!getPointDataView().checkShape(value.getShape())) {    if (numDataPointsPerSample==0) {
218        throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException("DataTagged::getTagNumber error: no data-points associated with this object.");
219           "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));    }
220      }  
221      //    if (dpno<0 || dpno>numDataPoints-1) {
222      // 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]);  
223    }    }
224    
225      //
226      // Determine the sample number which corresponds to this data-point number
227      int sampleNo = dpno / numDataPointsPerSample;
228    
229      //
230      // Determine the tag number which corresponds to this sample number
231      int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo);
232    
233      //
234      // return the tag number
235      return(tagNo);
236  }  }
237    
238  void  void
239  DataTagged::setTaggedValues(const TagListType& tagKeys,  DataTagged::setTaggedValues(const TagListType& tagKeys,
240                              const ValueListType& values)                              const ValueListType& values)
241  {  {
242    for (int i=0;i<tagKeys.size();++i) {    addTaggedValues(tagKeys,values);
     setTaggedValue(tagKeys[i],values[i]);  
   }  
243  }  }
244    
245  void  void
246  DataTagged::addTaggedValue(int tagKey,  DataTagged::setTaggedValue(int tagKey,
247                             const DataArrayView& value)                             const DataArrayView& value)
248  {  {
249    if (!getPointDataView().checkShape(value.getShape())) {    if (!getPointDataView().checkShape(value.getShape())) {
250      throw DataException(getPointDataView().createShapeErrorMessage(        throw DataException(getPointDataView().createShapeErrorMessage(
251           "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));                            "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
252      }
253      DataMapType::iterator pos(m_offsetLookup.find(tagKey));
254      if (pos==m_offsetLookup.end()) {
255        // tag couldn't be found so use addTaggedValue
256        addTaggedValue(tagKey,value);
257      } else {
258        // copy the values into the data array at the offset determined by m_offsetLookup
259        int offset=pos->second;
260        for (int i=0; i<getPointDataView().noValues(); i++) {
261          m_data[offset+i]=value.getData(i);
262        }
263    }    }
   //  
   // 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())) );  
264  }  }
265    
266  void  void
# Line 235  DataTagged::addTaggedValues(const TagLis Line 268  DataTagged::addTaggedValues(const TagLis
268                              const ValueListType& values)                              const ValueListType& values)
269  {  {
270    if (values.size()==0) {    if (values.size()==0) {
271      //      // copy the current default value for each of the tags
     // Copy the default value for each of the tags  
272      TagListType::const_iterator iT;      TagListType::const_iterator iT;
273      for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {      for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
       //  
274        // the point data view for DataTagged points at the default value        // the point data view for DataTagged points at the default value
275        addTaggedValue(*iT,getPointDataView());        addTaggedValue(*iT,getPointDataView());
276      }      }
277    } else if (values.size()==1 && tagKeys.size()>1) {    } else if (values.size()==1 && tagKeys.size()>1) {
278      //      // 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  
279      TagListType::const_iterator iT;      TagListType::const_iterator iT;
280      for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {      for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
281        addTaggedValue(*iT,values[0]);        addTaggedValue(*iT,values[0]);
282      }      }
283    } else {    } else {
284      if (tagKeys.size()!=values.size()) {      if (tagKeys.size()!=values.size()) {
285        stringstream temp;        stringstream temp;
286        temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()        temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
287         << " doesn't match the number of values: " << values.size();         << " doesn't match number of values: " << values.size();
288        throw DataException(temp.str());        throw DataException(temp.str());
289      } else {      } else {
290        for (int i=0;i<tagKeys.size();++i) {        for (int i=0;i<tagKeys.size();i++) {
291          addTaggedValue(tagKeys[i],values[i]);          addTaggedValue(tagKeys[i],values[i]);
292        }        }
293      }      }
294    }    }
295  }  }
296    
297    void
298    DataTagged::addTaggedValue(int tagKey,
299                               const DataArrayView& value)
300    {
301      if (!getPointDataView().checkShape(value.getShape())) {
302        throw DataException(getPointDataView().createShapeErrorMessage(
303                            "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
304      }
305      DataMapType::iterator pos(m_offsetLookup.find(tagKey));
306      if (pos!=m_offsetLookup.end()) {
307        // tag already exists so use setTaggedValue
308        setTaggedValue(tagKey,value);
309      } else {
310        // save the key and the location of its data in the lookup tab
311        m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size()));
312        // add the data given in "value" at the end of m_data
313        // need to make a temp copy of m_data, resize m_data, then copy
314        // all the old values plus the value to be added back into m_data
315        ValueType m_data_temp(m_data);
316        int oldSize=m_data.size();
317        int newSize=m_data.size()+value.noValues();
318        m_data.resize(newSize,0.,newSize);
319        for (int i=0;i<oldSize;i++) {
320          m_data[i]=m_data_temp[i];
321        }
322        for (int i=0;i<value.noValues();i++) {
323          m_data[oldSize+i]=value.getData(i);
324        }
325      }
326    }
327    
328  double*  double*
329  DataTagged::getSampleDataByTag(int tag)  DataTagged::getSampleDataByTag(int tag)
330  {  {
331    DataMapType::iterator pos(m_offsetLookup.find(tag));    DataMapType::iterator pos(m_offsetLookup.find(tag));
332    if (pos==m_offsetLookup.end()) {    if (pos==m_offsetLookup.end()) {
     //  
333      // tag couldn't be found so return the default value      // tag couldn't be found so return the default value
334      return &(m_data[0]);      return &(m_data[0]);
335    } else {    } else {
     //  
336      // return the data-point corresponding to the given tag      // return the data-point corresponding to the given tag
337      return &(m_data[pos->second]);      return &(m_data[pos->second]);
338    }    }
# Line 287  DataTagged::toString() const Line 345  DataTagged::toString() const
345    DataMapType::const_iterator i;    DataMapType::const_iterator i;
346    temp << "Tag(Default)" << endl;    temp << "Tag(Default)" << endl;
347    temp << getDefaultValue().toString() << endl;    temp << getDefaultValue().toString() << endl;
   //  
348    // create a temporary view as the offset will be changed    // create a temporary view as the offset will be changed
349    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
350    for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {    for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
# Line 298  DataTagged::toString() const Line 355  DataTagged::toString() const
355    return temp.str();    return temp.str();
356  }  }
357    
358  DataArrayView  DataArrayView::ValueType::size_type
359  DataTagged::getDataPointByTag(int tag) const  DataTagged::getPointOffset(int sampleNo,
360                               int dataPointNo) const
361  {  {
362    DataMapType::const_iterator pos(m_offsetLookup.find(tag));    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
363      DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
364    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
365    if (pos!=m_offsetLookup.end()) {    if (pos!=m_offsetLookup.end()) {
366      offset=pos->second;      offset=pos->second;
367    }    }
368    DataArrayView temp(getPointDataView());    return offset;
   temp.setOffset(offset);  
   return temp;  
369  }  }
370    
371  DataArrayView::ValueType::size_type  DataArrayView
372  DataTagged::getPointOffset(int sampleNo,  DataTagged::getDataPointByTag(int tag) const
                            int dataPointNo) const  
373  {  {
374    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);    DataMapType::const_iterator pos(m_offsetLookup.find(tag));
   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    return offset;    DataArrayView temp(getPointDataView());
380      temp.setOffset(offset);
381      return temp;
382  }  }
383    
384  DataArrayView  DataArrayView
# Line 333  DataTagged::getDataPoint(int sampleNo, Line 390  DataTagged::getDataPoint(int sampleNo,
390    return getDataPointByTag(tagKey);    return getDataPointByTag(tagKey);
391  }  }
392    
393  const DataTagged::DataMapType&  int
394  DataTagged::getTagLookup() const  DataTagged::archiveData(ofstream& archiveFile,
395                            const DataArrayView::ValueType::size_type noValues) const
396  {  {
397    return m_offsetLookup;    return(m_data.archiveData(archiveFile, noValues));
398  }  }
399    
400  DataArrayView::ValueType::size_type  int
401  DataTagged::getLength() const  DataTagged::extractData(ifstream& archiveFile,
402                            const DataArrayView::ValueType::size_type noValues)
403  {  {
404    return m_data.size();    return(m_data.extractData(archiveFile, noValues));
405  }  }
406    
407  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.119  
changed lines
  Added in v.509

  ViewVC Help
Powered by ViewVC 1.1.26