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

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

  ViewVC Help
Powered by ViewVC 1.1.26