/[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 1455 by phornby, Thu Feb 28 17:19:44 2008 UTC
# Line 1  Line 1 
 // $Id$  
 /*  
  ******************************************************************************  
  *                                                                            *  
  *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *  
  *                                                                            *  
  * This software is the property of ACcESS. No part of this code              *  
  * may be copied in any form or by any means without the expressed written    *  
  * consent of ACcESS.  Copying, use or modification of this software          *  
  * by any unauthorised person is illegal unless that person has a software    *  
  * license agreement with ACcESS.                                             *  
  *                                                                            *  
  ******************************************************************************  
 */  
   
 #include "escript/Data/DataTagged.h"  
 #include "escript/Data/DataConstant.h"  
 #include "escript/Data/DataExpanded.h"  
 #include "escript/Data/DataException.h"  
1    
2  #include <sstream>  /* $Id$ */
3    
4    /*******************************************************
5     *
6     *           Copyright 2003-2007 by ACceSS MNRF
7     *       Copyright 2007 by University of Queensland
8     *
9     *                http://esscc.uq.edu.au
10     *        Primary Business: Queensland, Australia
11     *  Licensed under the Open Software License version 3.0
12     *     http://www.opensource.org/licenses/osl-3.0.php
13     *
14     *******************************************************/
15    
16    #include "DataTagged.h"
17    
18    #include "DataConstant.h"
19    #include "DataException.h"
20    #ifdef USE_NETCDF
21    #include <netcdfcpp.h>
22    #endif
23    
24  using namespace std;  using namespace std;
25    
26  namespace escript {  namespace escript {
27    
28  DataTagged::DataTagged():  DataTagged::DataTagged()
29    DataAbstract(FunctionSpace())    : DataAbstract(FunctionSpace())
30  {  {
31    //    // default constructor
32    
33    // create a scalar default value    // create a scalar default value
34    m_data.push_back(0.0);    m_data.resize(1,0.,1);
35    DataArrayView temp(m_data,DataArrayView::ShapeType());    DataArrayView temp(m_data,DataArrayView::ShapeType());
36    setPointDataView(temp);    setPointDataView(temp);
37  }  }
# Line 40  DataTagged::DataTagged(const TagListType Line 42  DataTagged::DataTagged(const TagListType
42                 const FunctionSpace& what)                 const FunctionSpace& what)
43    : DataAbstract(what)    : DataAbstract(what)
44  {  {
45    //    // constructor
46    // Initialise the array of data values  
47    // The default value is always the first item in the values list    // initialise the array of data values
48    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
49      int len = defaultValue.noValues();
50      m_data.resize(len,0.,len);
51      for (int i=0; i<defaultValue.noValues(); i++) {
52        m_data[i]=defaultValue.getData(i);
53      }
54    
55    // create the data view    // create the data view
56    DataArrayView temp(m_data,defaultValue.getShape());    DataArrayView temp(m_data,defaultValue.getShape());
57    setPointDataView(temp);    setPointDataView(temp);
58    
59    // add remaining tags and values    // add remaining tags and values
60    addTaggedValues(tagKeys,values);    addTaggedValues(tagKeys,values);
61  }  }
# Line 54  DataTagged::DataTagged(const TagListType Line 63  DataTagged::DataTagged(const TagListType
63  DataTagged::DataTagged(const FunctionSpace& what,  DataTagged::DataTagged(const FunctionSpace& what,
64                         const DataArrayView::ShapeType &shape,                         const DataArrayView::ShapeType &shape,
65                         const int tags[],                         const int tags[],
66                         const DataArrayView::ValueType &data)                         const ValueType& data)
67    : DataAbstract(what)    : DataAbstract(what)
68  {  {
69    //    // alternative constructor
70    // copy the data in the correct format    // not unit_tested tested yet
71    
72      // copy the data
73    m_data=data;    m_data=data;
74    //  
75    // create the view of the data    // create the view of the data
76    DataArrayView tempView(m_data,shape);    DataArrayView tempView(m_data,shape);
77    setPointDataView(tempView);    setPointDataView(tempView);
78    //  
79      // create the tag lookup map
80      for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
81        m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
82      }
83    }
84    
85    DataTagged::DataTagged(const FunctionSpace& what,
86                           const DataArrayView::ShapeType &shape,
87                           const TagListType& tags,
88                           const ValueType& data)
89      : DataAbstract(what)
90    {
91      // alternative constructor
92      // not unit_tested tested yet
93    
94      // copy the data
95      m_data=data;
96    
97      // create the view of the data
98      DataArrayView tempView(m_data,shape);
99      setPointDataView(tempView);
100    
101    // create the tag lookup map    // create the tag lookup map
102    for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {    for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
103      m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));      m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
104    }    }
105  }  }
106    
107    
108  DataTagged::DataTagged(const DataTagged& other)  DataTagged::DataTagged(const DataTagged& other)
109    : DataAbstract(other.getFunctionSpace()),    : DataAbstract(other.getFunctionSpace()),
110    m_data(other.m_data),    m_data(other.m_data),
111    m_offsetLookup(other.m_offsetLookup)    m_offsetLookup(other.m_offsetLookup)
112  {  {
113      // copy constructor
114    
115    // create the data view    // create the data view
116    DataArrayView temp(m_data,other.getPointDataView().getShape());    DataArrayView temp(m_data,other.getPointDataView().getShape());
117    setPointDataView(temp);    setPointDataView(temp);
# Line 84  DataTagged::DataTagged(const DataTagged& Line 120  DataTagged::DataTagged(const DataTagged&
120  DataTagged::DataTagged(const DataConstant& other)  DataTagged::DataTagged(const DataConstant& other)
121    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
122  {  {
123    //    // copy constructor
124    // Fill the default value with the constant value item from other  
125      // fill the default value with the constant value item from "other"
126    const DataArrayView& value=other.getPointDataView();    const DataArrayView& value=other.getPointDataView();
127    m_data.insert(m_data.end(), &value.getData(0), &value.getData(value.noValues()) );    int len = value.noValues();
128      m_data.resize(len,0.,len);
129      for (int i=0; i<value.noValues(); i++) {
130        m_data[i]=value.getData(i);
131      }
132    
133    // create the data view    // create the data view
134    DataArrayView temp(m_data,value.getShape());    DataArrayView temp(m_data,value.getShape());
135    setPointDataView(temp);    setPointDataView(temp);
136  }  }
137    
138    DataAbstract*
139    DataTagged::getSlice(const DataArrayView::RegionType& region) const
140    {
141      return new DataTagged(*this, region);
142    }
143    
144  DataTagged::DataTagged(const DataTagged& other,  DataTagged::DataTagged(const DataTagged& other,
145                 const DataArrayView::RegionType& region)                 const DataArrayView::RegionType& region)
146    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
147  {  {
148    //    // slice constructor
149    
150    // get the shape of the slice to copy from other    // get the shape of the slice to copy from other
151    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
152    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
153    // allocate enough space for all values  
154    m_data.resize(DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1));    // allocate enough space in this for all values
155      // (need to add one to allow for the default value)
156      int len = DataArrayView::noValues(regionShape)*(other.m_offsetLookup.size()+1);
157      m_data.resize(len,0.0,len);
158    
159    // create the data view    // create the data view
160    DataArrayView temp(m_data,shape);    DataArrayView temp(m_data,regionShape);
161    setPointDataView(temp);    setPointDataView(temp);
162    // copy the default value  
163    getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range);    // copy the default value from other to this
164    //    getDefaultValue().copySlice(other.getDefaultValue(), regionLoopRange);
165    // Loop through the tag values copying these  
166      // loop through the tag values copying these
167    DataMapType::const_iterator pos;    DataMapType::const_iterator pos;
168    DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();    DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
169    for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();++pos){    for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
170      getPointDataView().copySlice(tagOffset,other.getPointDataView(), pos->second,region_loop_range);      getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,regionLoopRange);
171      m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));      m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
172      tagOffset+=getPointDataView().noValues();      tagOffset+=getPointDataView().noValues();
   }  
 }  
   
 void  
 DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)  
 {  
   //  
   // can only reshape a rank zero data point  
   if (getPointDataView().getRank()!=0) {  
     stringstream temp;  
     temp << "Error - Can only reshape Data with data points of rank 0. "  
      << "This Data has data points with rank: "  
      << getPointDataView().getRank();  
     throw DataException(temp.str());  
173    }    }
   //  
   // allocate enough space for all values  
   DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));  
   DataArrayView newView(newData,shape);  
   newView.copy(0,getDefaultValue()());  
   //  
   // Loop through the tag values  
   DataMapType::iterator pos;  
   DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);  
   for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){  
     newView.copy(tagOffset,m_data[pos->second]);  
     pos->second=tagOffset;  
     tagOffset+=DataArrayView::noValues(shape);  
   }  
   m_data=newData;  
   DataArrayView temp(m_data,shape);  
   setPointDataView(temp);  
 }  
   
 DataAbstract*  
 DataTagged::getSlice(const DataArrayView::RegionType& region) const  
 {  
   return new DataTagged(*this,region);  
174  }  }
175    
176  void  void
177  DataTagged::setSlice(const DataAbstract* value,  DataTagged::setSlice(const DataAbstract* other,
178                       const DataArrayView::RegionType& region)                       const DataArrayView::RegionType& region)
179  {  {
180    const DataTagged* tempDataTag=dynamic_cast<const DataTagged*>(value);  
181    if (tempDataTag==0) {    // other must be another DataTagged object
182      // Data:setSlice implementation should ensure this
183      const DataTagged* otherTemp=dynamic_cast<const DataTagged*>(other);
184      if (otherTemp==0) {
185      throw DataException("Programming error - casting to DataTagged.");      throw DataException("Programming error - casting to DataTagged.");
186    }    }
187    //  
188    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    // determine shape of the specified region
189    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
190    //  
191      // modify region specification as needed to match rank of this object
192      DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
193    
194      // ensure rank/shape of this object is compatible with specified region
195    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
196      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
197    }    }
198    if (tempDataTag->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {    if (otherTemp->getPointDataView().getRank()>0 && !other->getPointDataView().checkShape(regionShape)) {
199      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (other->getPointDataView().createShapeErrorMessage(
200                  "Error - Couldn't copy slice due to shape mismatch.",shape));                           "Error - Couldn't copy slice due to shape mismatch.",regionShape));
201    }    }
202    //  
203    getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range);    // copy slice from other default value to this default value
204    //    getDefaultValue().copySliceFrom(otherTemp->getDefaultValue(), regionLoopRange);
205    // Loop through the tag values  
206      // loop through tag values in other, adding any which aren't in this, using default value
207    DataMapType::const_iterator pos;    DataMapType::const_iterator pos;
208    for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){    for (pos=otherTemp->m_offsetLookup.begin();pos!=otherTemp->m_offsetLookup.end();pos++) {
209      getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range);      if (!isCurrentTag(pos->first)) {
210    }        addTaggedValue(pos->first,getDefaultValue());
211        }
212      }
213    
214      // loop through the tag values copying slices from other to this
215      for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
216        getDataPointByTag(pos->first).copySliceFrom(otherTemp->getDataPointByTag(pos->first), regionLoopRange);
217      }
218    
219  }  }
220    
221  void  int
222  DataTagged::setTaggedValue(int tagKey,  DataTagged::getTagNumber(int dpno)
                            const DataArrayView& value)  
223  {  {
224    DataMapType::iterator pos(m_offsetLookup.find(tagKey));    //
225    if (pos==m_offsetLookup.end()) {    // Get the number of samples and data-points per sample
226      //    int numSamples = getNumSamples();
227      // tag couldn't be found so add as a new tag    int numDataPointsPerSample = getNumDPPSample();
228      addTaggedValue(tagKey,value);    int numDataPoints = numSamples * numDataPointsPerSample;
229    } else {  
230      if (!getPointDataView().checkShape(value.getShape())) {    if (numDataPointsPerSample==0) {
231        throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException("DataTagged::getTagNumber error: no data-points associated with this object.");
          "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));  
     }  
     //  
     // copy the values into tagged data storage  
     copy(&value.getData(0), &value.getData(getPointDataView().noValues()), &m_data[pos->second]);  
232    }    }
233    
234      if (dpno<0 || dpno>numDataPoints-1) {
235        throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied.");
236      }
237    
238      //
239      // Determine the sample number which corresponds to this data-point number
240      int sampleNo = dpno / numDataPointsPerSample;
241    
242      //
243      // Determine the tag number which corresponds to this sample number
244      int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo);
245    
246      //
247      // return the tag number
248      return(tagNo);
249  }  }
250    
251  void  void
252  DataTagged::setTaggedValues(const TagListType& tagKeys,  DataTagged::setTaggedValues(const TagListType& tagKeys,
253                              const ValueListType& values)                              const ValueListType& values)
254  {  {
255    for (int i=0;i<tagKeys.size();++i) {    addTaggedValues(tagKeys,values);
     setTaggedValue(tagKeys[i],values[i]);  
   }  
256  }  }
257    
258  void  void
259  DataTagged::addTaggedValue(int tagKey,  DataTagged::setTaggedValue(int tagKey,
260                             const DataArrayView& value)                             const DataArrayView& value)
261  {  {
262    if (!getPointDataView().checkShape(value.getShape())) {    if (!getPointDataView().checkShape(value.getShape())) {
263      throw DataException(getPointDataView().createShapeErrorMessage(        throw DataException(getPointDataView().createShapeErrorMessage(
264           "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));                            "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
265      }
266      DataMapType::iterator pos(m_offsetLookup.find(tagKey));
267      if (pos==m_offsetLookup.end()) {
268        // tag couldn't be found so use addTaggedValue
269        addTaggedValue(tagKey,value);
270      } else {
271        // copy the values into the data array at the offset determined by m_offsetLookup
272        int offset=pos->second;
273        for (int i=0; i<getPointDataView().noValues(); i++) {
274          m_data[offset+i]=value.getData(i);
275        }
276    }    }
   //  
   // 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())) );  
277  }  }
278    
279  void  void
# Line 235  DataTagged::addTaggedValues(const TagLis Line 281  DataTagged::addTaggedValues(const TagLis
281                              const ValueListType& values)                              const ValueListType& values)
282  {  {
283    if (values.size()==0) {    if (values.size()==0) {
284      //      // copy the current default value for each of the tags
     // Copy the default value for each of the tags  
285      TagListType::const_iterator iT;      TagListType::const_iterator iT;
286      for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {      for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
       //  
287        // the point data view for DataTagged points at the default value        // the point data view for DataTagged points at the default value
288        addTaggedValue(*iT,getPointDataView());        addTaggedValue(*iT,getPointDataView());
289      }      }
290    } else if (values.size()==1 && tagKeys.size()>1) {    } else if (values.size()==1 && tagKeys.size()>1) {
291      //      // 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  
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        addTaggedValue(*iT,values[0]);        addTaggedValue(*iT,values[0]);
295      }      }
296    } else {    } else {
297      if (tagKeys.size()!=values.size()) {      if (tagKeys.size()!=values.size()) {
298        stringstream temp;        stringstream temp;
299        temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()        temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
300         << " doesn't match the number of values: " << values.size();         << " doesn't match number of values: " << values.size();
301        throw DataException(temp.str());        throw DataException(temp.str());
302      } else {      } else {
303        for (int i=0;i<tagKeys.size();++i) {        unsigned int i;
304          for (i=0;i<tagKeys.size();i++) {
305          addTaggedValue(tagKeys[i],values[i]);          addTaggedValue(tagKeys[i],values[i]);
306        }        }
307      }      }
308    }    }
309  }  }
310    
311    void
312    DataTagged::addTaggedValue(int tagKey,
313                               const DataArrayView& value)
314    {
315      if (!getPointDataView().checkShape(value.getShape())) {
316        throw DataException(getPointDataView().createShapeErrorMessage(
317                            "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
318      }
319      DataMapType::iterator pos(m_offsetLookup.find(tagKey));
320      if (pos!=m_offsetLookup.end()) {
321        // tag already exists so use setTaggedValue
322        setTaggedValue(tagKey,value);
323      } else {
324        // save the key and the location of its data in the lookup tab
325        m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size()));
326        // add the data given in "value" at the end of m_data
327        // need to make a temp copy of m_data, resize m_data, then copy
328        // all the old values plus the value to be added back into m_data
329        ValueType m_data_temp(m_data);
330        int oldSize=m_data.size();
331        int newSize=m_data.size()+value.noValues();
332        m_data.resize(newSize,0.,newSize);
333        for (int i=0;i<oldSize;i++) {
334          m_data[i]=m_data_temp[i];
335        }
336        for (int i=0;i<value.noValues();i++) {
337          m_data[oldSize+i]=value.getData(i);
338        }
339      }
340    }
341    
342  double*  double*
343  DataTagged::getSampleDataByTag(int tag)  DataTagged::getSampleDataByTag(int tag)
344  {  {
345    DataMapType::iterator pos(m_offsetLookup.find(tag));    DataMapType::iterator pos(m_offsetLookup.find(tag));
346    if (pos==m_offsetLookup.end()) {    if (pos==m_offsetLookup.end()) {
     //  
347      // tag couldn't be found so return the default value      // tag couldn't be found so return the default value
348      return &(m_data[0]);      return &(m_data[0]);
349    } else {    } else {
     //  
350      // return the data-point corresponding to the given tag      // return the data-point corresponding to the given tag
351      return &(m_data[pos->second]);      return &(m_data[pos->second]);
352    }    }
# Line 287  DataTagged::toString() const Line 359  DataTagged::toString() const
359    DataMapType::const_iterator i;    DataMapType::const_iterator i;
360    temp << "Tag(Default)" << endl;    temp << "Tag(Default)" << endl;
361    temp << getDefaultValue().toString() << endl;    temp << getDefaultValue().toString() << endl;
   //  
362    // create a temporary view as the offset will be changed    // create a temporary view as the offset will be changed
363    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());    DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
364    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 369  DataTagged::toString() const
369    return temp.str();    return temp.str();
370  }  }
371    
372  DataArrayView  DataArrayView::ValueType::size_type
373  DataTagged::getDataPointByTag(int tag) const  DataTagged::getPointOffset(int sampleNo,
374                               int dataPointNo) const
375  {  {
376    DataMapType::const_iterator pos(m_offsetLookup.find(tag));    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
377      DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
378    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
379    if (pos!=m_offsetLookup.end()) {    if (pos!=m_offsetLookup.end()) {
380      offset=pos->second;      offset=pos->second;
381    }    }
382    DataArrayView temp(getPointDataView());    return offset;
   temp.setOffset(offset);  
   return temp;  
383  }  }
384    
385  DataArrayView::ValueType::size_type  DataArrayView
386  DataTagged::getPointOffset(int sampleNo,  DataTagged::getDataPointByTag(int tag) const
                            int dataPointNo) const  
387  {  {
388    int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);    DataMapType::const_iterator pos(m_offsetLookup.find(tag));
   DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));  
389    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;    DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
390    if (pos!=m_offsetLookup.end()) {    if (pos!=m_offsetLookup.end()) {
391      offset=pos->second;      offset=pos->second;
392    }    }
393    return offset;    DataArrayView temp(getPointDataView());
394      temp.setOffset(offset);
395      return temp;
396  }  }
397    
398  DataArrayView  DataArrayView
# Line 333  DataTagged::getDataPoint(int sampleNo, Line 404  DataTagged::getDataPoint(int sampleNo,
404    return getDataPointByTag(tagKey);    return getDataPointByTag(tagKey);
405  }  }
406    
407  const DataTagged::DataMapType&  int
408  DataTagged::getTagLookup() const  DataTagged::archiveData(ofstream& archiveFile,
409                            const DataArrayView::ValueType::size_type noValues) const
410  {  {
411    return m_offsetLookup;    return(m_data.archiveData(archiveFile, noValues));
412  }  }
413    
414  DataArrayView::ValueType::size_type  int
415  DataTagged::getLength() const  DataTagged::extractData(ifstream& archiveFile,
416                            const DataArrayView::ValueType::size_type noValues)
417    {
418      return(m_data.extractData(archiveFile, noValues));
419    }
420    void
421    DataTagged::symmetric(DataAbstract* ev)
422  {  {
423    return m_data.size();    DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
424      if (temp_ev==0) {
425        throw DataException("Error - DataTagged::symmetric casting to DataTagged failed (probably a programming error).");
426      }
427      const DataTagged::DataMapType& thisLookup=getTagLookup();
428      DataTagged::DataMapType::const_iterator i;
429      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
430      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
431          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
432          DataArrayView thisView=getDataPointByTag(i->first);
433          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
434          DataArrayView::symmetric(thisView,0,evView,0);
435      }
436      DataArrayView::symmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
437    }
438    void
439    DataTagged::nonsymmetric(DataAbstract* ev)
440    {
441      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
442      if (temp_ev==0) {
443        throw DataException("Error - DataTagged::nonsymmetric casting to DataTagged failed (probably a programming error).");
444      }
445      const DataTagged::DataMapType& thisLookup=getTagLookup();
446      DataTagged::DataMapType::const_iterator i;
447      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
448      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
449          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
450          DataArrayView thisView=getDataPointByTag(i->first);
451          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
452          DataArrayView::nonsymmetric(thisView,0,evView,0);
453      }
454      DataArrayView::nonsymmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
455    }
456    void
457    DataTagged::trace(DataAbstract* ev, int axis_offset)
458    {
459      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
460      if (temp_ev==0) {
461        throw DataException("Error - DataTagged::trace casting to DataTagged failed (probably a programming error).");
462      }
463      const DataTagged::DataMapType& thisLookup=getTagLookup();
464      DataTagged::DataMapType::const_iterator i;
465      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
466      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
467          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
468          DataArrayView thisView=getDataPointByTag(i->first);
469          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
470          DataArrayView::trace(thisView,0,evView,0, axis_offset);
471      }
472      DataArrayView::trace(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
473  }  }
474    
475    void
476    DataTagged::transpose(DataAbstract* ev, int axis_offset)
477    {
478      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
479      if (temp_ev==0) {
480        throw DataException("Error - DataTagged::transpose casting to DataTagged failed (probably a programming error).");
481      }
482      const DataTagged::DataMapType& thisLookup=getTagLookup();
483      DataTagged::DataMapType::const_iterator i;
484      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
485      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
486          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
487          DataArrayView thisView=getDataPointByTag(i->first);
488          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
489          DataArrayView::transpose(thisView,0,evView,0, axis_offset);
490      }
491      DataArrayView::transpose(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
492    }
493    
494    void
495    DataTagged::swapaxes(DataAbstract* ev, int axis0, int axis1)
496    {
497      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
498      if (temp_ev==0) {
499        throw DataException("Error - DataTagged::swapaxes casting to DataTagged failed (probably a programming error).");
500      }
501      const DataTagged::DataMapType& thisLookup=getTagLookup();
502      DataTagged::DataMapType::const_iterator i;
503      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
504      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
505          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
506          DataArrayView thisView=getDataPointByTag(i->first);
507          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
508          DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
509      }
510      DataArrayView::swapaxes(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis0,axis1);
511    }
512    
513    void
514    DataTagged::eigenvalues(DataAbstract* ev)
515    {
516      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
517      if (temp_ev==0) {
518        throw DataException("Error - DataTagged::eigenvalues casting to DataTagged failed (propably a programming error).");
519      }
520      const DataTagged::DataMapType& thisLookup=getTagLookup();
521      DataTagged::DataMapType::const_iterator i;
522      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
523      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
524          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
525          DataArrayView thisView=getDataPointByTag(i->first);
526          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
527          DataArrayView::eigenvalues(thisView,0,evView,0);
528      }
529      DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
530    }
531    void
532    DataTagged::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
533    {
534      DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
535      if (temp_ev==0) {
536        throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
537      }
538      DataTagged* temp_V=dynamic_cast<DataTagged*>(V);
539      if (temp_V==0) {
540        throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
541      }
542      const DataTagged::DataMapType& thisLookup=getTagLookup();
543      DataTagged::DataMapType::const_iterator i;
544      DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
545      for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
546          temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
547          temp_V->addTaggedValue(i->first,temp_V->getDefaultValue());
548          DataArrayView thisView=getDataPointByTag(i->first);
549          DataArrayView evView=temp_ev->getDataPointByTag(i->first);
550          DataArrayView VView=temp_V->getDataPointByTag(i->first);
551          DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
552      }
553      DataArrayView::eigenvalues_and_eigenvectors(getDefaultValue(),0,
554                                                  temp_ev->getDefaultValue(),0,
555                                                  temp_V->getDefaultValue(),0,
556                                                  tol);
557    
558    
559    }
560    
561    void
562    DataTagged::setToZero(){
563        DataArrayView::ValueType::size_type n=m_data.size();
564        for (int i=0; i<n ;++i) m_data[i]=0.;
565    }
566    
567    void
568    DataTagged::dump(const std::string fileName) const
569    {
570       #ifdef PASO_MPI
571       throw DataException("Error - DataTagged:: dump is not implemented for MPI yet.");
572       #endif
573       #ifdef USE_NETCDF
574       const int ldims=DataArrayView::maxRank+1;
575       const NcDim* ncdims[ldims];
576       NcVar *var, *tags_var;
577       int rank = getPointDataView().getRank();
578       int type=  getFunctionSpace().getTypeCode();
579       int ndims =0;
580       long dims[ldims];
581       const double* d_ptr=&(m_data[0]);
582       DataArrayView::ShapeType shape = getPointDataView().getShape();
583    
584       // netCDF error handler
585       NcError err(NcError::verbose_nonfatal);
586       // Create the file.
587       NcFile dataFile(fileName.c_str(), NcFile::Replace);
588       // check if writing was successful
589       if (!dataFile.is_valid())
590            throw DataException("Error - DataTagged:: opening of netCDF file for output failed.");
591       if (!dataFile.add_att("type_id",1) )
592            throw DataException("Error - DataTagged:: appending data type to netCDF file failed.");
593       if (!dataFile.add_att("rank",rank) )
594            throw DataException("Error - DataTagged:: appending rank attribute to netCDF file failed.");
595       if (!dataFile.add_att("function_space_type",type))
596            throw DataException("Error - DataTagged:: appending function space attribute to netCDF file failed.");
597       ndims=rank+1;
598       if ( rank >0 ) {
599           dims[0]=shape[0];
600           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
601                throw DataException("Error - DataTagged:: appending ncdimsion 0 to netCDF file failed.");
602       }
603       if ( rank >1 ) {
604           dims[1]=shape[1];
605           if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
606                throw DataException("Error - DataTagged:: appending ncdimsion 1 to netCDF file failed.");
607       }
608       if ( rank >2 ) {
609           dims[2]=shape[2];
610           if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
611                throw DataException("Error - DataTagged:: appending ncdimsion 2 to netCDF file failed.");
612       }
613       if ( rank >3 ) {
614           dims[3]=shape[3];
615           if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
616                throw DataException("Error - DataTagged:: appending ncdimsion 3 to netCDF file failed.");
617       }
618       const DataTagged::DataMapType& thisLookup=getTagLookup();
619       DataTagged::DataMapType::const_iterator i;
620       DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
621       int ntags=1;
622       for (i=thisLookup.begin();i!=thisLookupEnd;i++) ntags++;
623       int* tags =(int*) malloc(ntags*sizeof(int));
624       int c=1;
625       tags[0]=-1;
626       for (i=thisLookup.begin();i!=thisLookupEnd;i++) tags[c++]=i->first;
627       dims[rank]=ntags;
628       if (! (ncdims[rank] = dataFile.add_dim("num_tags", dims[rank])) )
629       {
630           free(tags);
631               throw DataException("Error - DataTagged:: appending num_tags to netCDF file failed.");
632       }
633       if (! ( tags_var = dataFile.add_var("tags", ncInt, ncdims[rank])) )
634       {
635        free(tags);
636            throw DataException("Error - DataTagged:: appending tags to netCDF file failed.");
637       }
638       if (! (tags_var->put(tags,dims[rank])) )
639       {
640        free(tags);
641            throw DataException("Error - DataTagged:: copy tags to netCDF buffer failed.");
642       }
643       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
644       {
645        free(tags);
646            throw DataException("Error - DataTagged:: appending variable to netCDF file failed.");
647       }
648       if (! (var->put(d_ptr,dims)) )
649       {
650        free(tags);
651            throw DataException("Error - DataTagged:: copy data to netCDF buffer failed.");
652       }
653       #else
654       throw DataException("Error - DataTagged:: dump is not configured with netCDF. Please contact your installation manager.");
655       #endif
656    }
657  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26