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

Legend:
Removed from v.97  
changed lines
  Added in v.155

  ViewVC Help
Powered by ViewVC 1.1.26