/[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 500 by jgs, Tue Feb 7 05:19:07 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    // create a scalar default value    // create a scalar default value
30    m_data.push_back(0.0);    m_data.resize(1,0.,1);
31    DataArrayView temp(m_data,DataArrayView::ShapeType());    DataArrayView temp(m_data,DataArrayView::ShapeType());
32    setPointDataView(temp);    setPointDataView(temp);
33  }  }
# Line 40  DataTagged::DataTagged(const TagListType Line 38  DataTagged::DataTagged(const TagListType
38                 const FunctionSpace& what)                 const FunctionSpace& what)
39    : DataAbstract(what)    : DataAbstract(what)
40  {  {
41    //    // initialise the array of data values
42    // Initialise the array of data values    // the default value is always the first item in the values list
43    // The default value is always the first item in the values list    int len = defaultValue.noValues();
44    m_data.insert(m_data.end(), &defaultValue.getData(0), &defaultValue.getData(defaultValue.noValues()) );    m_data.resize(len,0.,len);
45      for (int i=0; i<defaultValue.noValues(); i++) {
46        m_data[i]=defaultValue.getData(i);
47      }
48    
49    // create the data view    // create the data view
50    DataArrayView temp(m_data,defaultValue.getShape());    DataArrayView temp(m_data,defaultValue.getShape());
51    setPointDataView(temp);    setPointDataView(temp);
52    
53    // add remaining tags and values    // add remaining tags and values
54    addTaggedValues(tagKeys,values);    addTaggedValues(tagKeys,values);
55  }  }
# Line 54  DataTagged::DataTagged(const TagListType Line 57  DataTagged::DataTagged(const TagListType
57  DataTagged::DataTagged(const FunctionSpace& what,  DataTagged::DataTagged(const FunctionSpace& what,
58                         const DataArrayView::ShapeType &shape,                         const DataArrayView::ShapeType &shape,
59                         const int tags[],                         const int tags[],
60                         const DataArrayView::ValueType &data)                         const ValueType& data)
61    : DataAbstract(what)    : DataAbstract(what)
62  {  {
63    //    // copy the data
   // copy the data in the correct format  
64    m_data=data;    m_data=data;
65    //  
66    // create the view of the data    // create the view of the data
67    DataArrayView tempView(m_data,shape);    DataArrayView tempView(m_data,shape);
68    setPointDataView(tempView);    setPointDataView(tempView);
69    //  
70    // create the tag lookup map    // create the tag lookup map
71    for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {    for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
72      m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));      m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
# Line 84  DataTagged::DataTagged(const DataTagged& Line 86  DataTagged::DataTagged(const DataTagged&
86  DataTagged::DataTagged(const DataConstant& other)  DataTagged::DataTagged(const DataConstant& other)
87    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
88  {  {
89    //    // fill the default value with the constant value item from "other"
   // Fill the default value with the constant value item from other  
90    const DataArrayView& value=other.getPointDataView();    const DataArrayView& value=other.getPointDataView();
91    m_data.insert(m_data.end(), &value.getData(0), &value.getData(value.noValues()) );    int len = value.noValues();
92      m_data.resize(len,0.,len);
93      for (int i=0; i<value.noValues(); i++) {
94        m_data[i]=value.getData(i);
95      }
96    
97    // create the data view    // create the data view
98    DataArrayView temp(m_data,value.getShape());    DataArrayView temp(m_data,value.getShape());
99    setPointDataView(temp);    setPointDataView(temp);
# Line 97  DataTagged::DataTagged(const DataTagged& Line 103  DataTagged::DataTagged(const DataTagged&
103                 const DataArrayView::RegionType& region)                 const DataArrayView::RegionType& region)
104    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
105  {  {
   //  
106    // get the shape of the slice to copy from other    // get the shape of the slice to copy from other
107    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
108    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
109    
110    // allocate enough space for all values    // allocate enough space for all values
111    m_data.resize(DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1));    int len = DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1);
112      m_data.resize(len,0.,len);
113    
114    // create the data view    // create the data view
115    DataArrayView temp(m_data,shape);    DataArrayView temp(m_data,shape);
116    setPointDataView(temp);    setPointDataView(temp);
117    
118    // copy the default value    // copy the default value
119    getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range);    getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range);
120    //  
121    // Loop through the tag values copying these    // loop through the tag values copying these
122    DataMapType::const_iterator pos;    DataMapType::const_iterator pos;
123    DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();    DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
124    for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();++pos){    for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
125      getPointDataView().copySlice(tagOffset,other.getPointDataView(), pos->second,region_loop_range);      getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,region_loop_range);
126      m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));      m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
127      tagOffset+=getPointDataView().noValues();      tagOffset+=getPointDataView().noValues();
128    }    }
129  }  }
130    
131  void  void
132  DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)  DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)
133  {  {
   //  
134    // can only reshape a rank zero data point    // can only reshape a rank zero data point
135    if (getPointDataView().getRank()!=0) {    if (getPointDataView().getRank()!=0) {
136      stringstream temp;      stringstream temp;
# Line 131  DataTagged::reshapeDataPoint(const DataA Line 139  DataTagged::reshapeDataPoint(const DataA
139       << getPointDataView().getRank();       << getPointDataView().getRank();
140      throw DataException(temp.str());      throw DataException(temp.str());
141    }    }
142    //  
143    // allocate enough space for all values    // allocate enough space for all values
144    DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));    DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));
145    DataArrayView newView(newData,shape);    DataArrayView newView(newData,shape);
146    newView.copy(0,getDefaultValue()());    newView.copy(0,getDefaultValue()());
147    //  
148    // Loop through the tag values    // loop through the tag values
149    DataMapType::iterator pos;    DataMapType::iterator pos;
150    DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);    DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
151    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){
152      newView.copy(tagOffset,m_data[pos->second]);      newView.copy(tagOffset,m_data[pos->second]);
153      pos->second=tagOffset;      pos->second=tagOffset;
154      tagOffset+=DataArrayView::noValues(shape);      tagOffset+=DataArrayView::noValues(shape);
# Line 164  DataTagged::setSlice(const DataAbstract* Line 172  DataTagged::setSlice(const DataAbstract*
172    if (tempDataTag==0) {    if (tempDataTag==0) {
173      throw DataException("Programming error - casting to DataTagged.");      throw DataException("Programming error - casting to DataTagged.");
174    }    }
175    //  
176    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
177    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
   //  
178    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
179      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
180    }    }
# Line 175  DataTagged::setSlice(const DataAbstract* Line 182  DataTagged::setSlice(const DataAbstract*
182      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (value->getPointDataView().createShapeErrorMessage(
183                  "Error - Couldn't copy slice due to shape mismatch.",shape));                  "Error - Couldn't copy slice due to shape mismatch.",shape));
184    }    }
185    //  
186    getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range);    getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range);
187    //  
188    // Loop through the tag values    // loop through the tag values
189    DataMapType::const_iterator pos;    DataMapType::const_iterator pos;
190    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
191      getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range);      getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range);
192    }    }
193    }
194    
195    int
196    DataTagged::getTagNumber(int dpno)
197    {
198      //
199      // Get the number of samples and data-points per sample
200      int numSamples = getNumSamples();
201      int numDataPointsPerSample = getNumDPPSample();
202      int numDataPoints = numSamples * numDataPointsPerSample;
203    
204      if (numDataPointsPerSample==0) {
205        throw DataException("DataTagged::getTagNumber error: no data-points associated with this object.");
206      }
207    
208      if (dpno<0 || dpno>numDataPoints-1) {
209        throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied.");
210      }
211    
212      //
213      // Determine the sample number which corresponds to this data-point number
214      int sampleNo = dpno / numDataPointsPerSample;
215    
216      //
217      // Determine the tag number which corresponds to this sample number
218      int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo);
219    
220      //
221      // return the tag number
222      return(tagNo);
223  }  }
224    
225  void  void
226  DataTagged::setTaggedValue(int tagKey,  DataTagged::setTaggedValue(int tagKey,
227                             const DataArrayView& value)                             const DataArrayView& value)
228  {  {
229      if (!getPointDataView().checkShape(value.getShape())) {
230          throw DataException(getPointDataView().createShapeErrorMessage(
231                              "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
232      }
233    DataMapType::iterator pos(m_offsetLookup.find(tagKey));    DataMapType::iterator pos(m_offsetLookup.find(tagKey));
234    if (pos==m_offsetLookup.end()) {    if (pos==m_offsetLookup.end()) {
235      //      // tag couldn't be found so use addTaggedValue
     // tag couldn't be found so add as a new tag  
236      addTaggedValue(tagKey,value);      addTaggedValue(tagKey,value);
237    } else {    } else {
238      if (!getPointDataView().checkShape(value.getShape())) {      // copy the values into the data array at the offset determined by m_offsetLookup
239        throw DataException(getPointDataView().createShapeErrorMessage(      int offset=pos->second;
240           "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));      for (int i=0; i<getPointDataView().noValues(); i++) {
241          m_data[offset+i]=value.getData(i);
242      }      }
     //  
     // copy the values into tagged data storage  
     copy(&value.getData(0), &value.getData(getPointDataView().noValues()), &m_data[pos->second]);  
   }  
 }  
   
 void  
 DataTagged::setTaggedValues(const TagListType& tagKeys,  
                             const ValueListType& values)  
 {  
   for (int i=0;i<tagKeys.size();++i) {  
     setTaggedValue(tagKeys[i],values[i]);  
243    }    }
244  }  }
245    
# Line 220  DataTagged::addTaggedValue(int tagKey, Line 249  DataTagged::addTaggedValue(int tagKey,
249  {  {
250    if (!getPointDataView().checkShape(value.getShape())) {    if (!getPointDataView().checkShape(value.getShape())) {
251      throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException(getPointDataView().createShapeErrorMessage(
252           "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));                          "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
253    }    }
254    //    DataMapType::iterator pos(m_offsetLookup.find(tagKey));
255    // save the key and the location of its data    if (pos!=m_offsetLookup.end()) {
256    m_offsetLookup.insert( DataMapType::value_type(tagKey,m_data.size()) );      // tag already exists so use setTaggedValue
257    //      setTaggedValue(tagKey,value);
258    // insert the data given in value at the end of m_data    } else {
259    m_data.insert( m_data.end(), &(value.getData(0)), &(value.getData(value.noValues())) );      // save the key and the location of its data in the lookup tab
260        m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size()));
261        // add the data given in "value" at the end of m_data
262        // need to make a temp copy of m_data, resize m_data, then copy
263        // all the old values plus the value to be added back into m_data
264        ValueType m_data_temp(m_data);
265        int oldSize=m_data.size();
266        int newSize=m_data.size()+value.noValues();
267        m_data.resize(newSize,0.,newSize);
268        for (int i=0;i<oldSize;i++) {
269          m_data[i]=m_data_temp[i];
270        }
271        for (int i=0;i<value.noValues();i++) {
272          m_data[oldSize+i]=value.getData(i);
273        }
274      }
275    }
276    
277    void
278    DataTagged::setTaggedValues(const TagListType& tagKeys,
279                                const ValueListType& values)
280    {
281      addTaggedValues(tagKeys,values);
282  }  }
283    
284  void  void
# Line 235  DataTagged::addTaggedValues(const TagLis Line 286  DataTagged::addTaggedValues(const TagLis
286                              const ValueListType& values)                              const ValueListType& values)
287  {  {
288    if (values.size()==0) {    if (values.size()==0) {
289      //      // copy the default value for each of the tags
     // Copy the default value for each of the tags  
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        // the point data view for DataTagged points at the default value        // the point data view for DataTagged points at the default value
293        addTaggedValue(*iT,getPointDataView());        addTaggedValue(*iT,getPointDataView());
294      }      }
295    } else if (values.size()==1 && tagKeys.size()>1) {    } else if (values.size()==1 && tagKeys.size()>1) {
     //  
296      // assume the one value will be used for all tag values      // assume the one value will be used for all tag values
     // Copy the input data  
297      TagListType::const_iterator iT;      TagListType::const_iterator iT;
298      for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {      for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
299        addTaggedValue(*iT,values[0]);        addTaggedValue(*iT,values[0]);
300      }      }
301    } else {    } else {
# Line 258  DataTagged::addTaggedValues(const TagLis Line 305  DataTagged::addTaggedValues(const TagLis
305         << " doesn't match the number of values: " << values.size();         << " doesn't match the number of values: " << values.size();
306        throw DataException(temp.str());        throw DataException(temp.str());
307      } else {      } else {
308        for (int i=0;i<tagKeys.size();++i) {        for (int i=0;i<tagKeys.size();i++) {
309          addTaggedValue(tagKeys[i],values[i]);          addTaggedValue(tagKeys[i],values[i]);
310        }        }
311      }      }
# Line 270  DataTagged::getSampleDataByTag(int tag) Line 317  DataTagged::getSampleDataByTag(int tag)
317  {  {
318    DataMapType::iterator pos(m_offsetLookup.find(tag));    DataMapType::iterator pos(m_offsetLookup.find(tag));
319    if (pos==m_offsetLookup.end()) {    if (pos==m_offsetLookup.end()) {
     //  
320      // tag couldn't be found so return the default value      // tag couldn't be found so return the default value
321      return &(m_data[0]);      return &(m_data[0]);
322    } else {    } else {
     //  
323      // return the data-point corresponding to the given tag      // return the data-point corresponding to the given tag
324      return &(m_data[pos->second]);      return &(m_data[pos->second]);
325    }    }
# Line 287  DataTagged::toString() const Line 332  DataTagged::toString() const
332    DataMapType::const_iterator i;    DataMapType::const_iterator i;
333    temp << "Tag(Default)" << endl;    temp << "Tag(Default)" << endl;
334    temp << getDefaultValue().toString() << endl;    temp << getDefaultValue().toString() << endl;
   //  
335    // create a temporary view as the offset will be changed    // create a temporary view as the offset will be changed
336    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
337    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 342  DataTagged::toString() const
342    return temp.str();    return temp.str();
343  }  }
344    
345  DataArrayView  DataArrayView::ValueType::size_type
346  DataTagged::getDataPointByTag(int tag) const  DataTagged::getPointOffset(int sampleNo,
347                               int dataPointNo) const
348  {  {
349    DataMapType::const_iterator pos(m_offsetLookup.find(tag));    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
350      DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
351    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
352    if (pos!=m_offsetLookup.end()) {    if (pos!=m_offsetLookup.end()) {
353      offset=pos->second;      offset=pos->second;
354    }    }
355    DataArrayView temp(getPointDataView());    return offset;
   temp.setOffset(offset);  
   return temp;  
356  }  }
357    
358  DataArrayView::ValueType::size_type  DataArrayView
359  DataTagged::getPointOffset(int sampleNo,  DataTagged::getDataPointByTag(int tag) const
                            int dataPointNo) const  
360  {  {
361    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);    DataMapType::const_iterator pos(m_offsetLookup.find(tag));
   DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));  
362    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
363    if (pos!=m_offsetLookup.end()) {    if (pos!=m_offsetLookup.end()) {
364      offset=pos->second;      offset=pos->second;
365    }    }
366    return offset;    DataArrayView temp(getPointDataView());
367      temp.setOffset(offset);
368      return temp;
369  }  }
370    
371  DataArrayView  DataArrayView
# Line 333  DataTagged::getDataPoint(int sampleNo, Line 377  DataTagged::getDataPoint(int sampleNo,
377    return getDataPointByTag(tagKey);    return getDataPointByTag(tagKey);
378  }  }
379    
380  const DataTagged::DataMapType&  int
381  DataTagged::getTagLookup() const  DataTagged::archiveData(ofstream& archiveFile,
382                            const DataArrayView::ValueType::size_type noValues) const
383  {  {
384    return m_offsetLookup;    return(m_data.archiveData(archiveFile, noValues));
385  }  }
386    
387  DataArrayView::ValueType::size_type  int
388  DataTagged::getLength() const  DataTagged::extractData(ifstream& archiveFile,
389                            const DataArrayView::ValueType::size_type noValues)
390  {  {
391    return m_data.size();    return(m_data.extractData(archiveFile, noValues));
392  }  }
393    
394  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26