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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (hide annotations)
Tue Apr 12 04:45:05 2005 UTC (14 years, 6 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataTagged.cpp
File size: 11382 byte(s)
*** empty log message ***

1 jgs 82 // $Id$
2     /*
3     ******************************************************************************
4     * *
5     * COPYRIGHT ACcESS 2004 - All Rights Reserved *
6     * *
7     * This software is the property of ACcESS. No part of this code *
8     * may be copied in any form or by any means without the expressed written *
9     * consent of ACcESS. Copying, use or modification of this software *
10     * by any unauthorised person is illegal unless that person has a software *
11     * license agreement with ACcESS. *
12     * *
13     ******************************************************************************
14     */
15    
16     #include "escript/Data/DataTagged.h"
17     #include "escript/Data/DataConstant.h"
18 jgs 108 #include "escript/Data/DataExpanded.h"
19     #include "escript/Data/DataException.h"
20 jgs 82
21     #include <sstream>
22    
23     using namespace std;
24    
25     namespace escript {
26    
27     DataTagged::DataTagged():
28     DataAbstract(FunctionSpace())
29     {
30     //
31     // create a scalar default value
32     m_data.push_back(0.0);
33     DataArrayView temp(m_data,DataArrayView::ShapeType());
34     setPointDataView(temp);
35     }
36    
37     DataTagged::DataTagged(const TagListType& tagKeys,
38     const ValueListType& values,
39     const DataArrayView& defaultValue,
40 jgs 102 const FunctionSpace& what)
41     : DataAbstract(what)
42 jgs 82 {
43     //
44 jgs 119 // Initialise the array of data values
45 jgs 82 // The default value is always the first item in the values list
46     m_data.insert(m_data.end(), &defaultValue.getData(0), &defaultValue.getData(defaultValue.noValues()) );
47 jgs 119 // create the data view
48 jgs 82 DataArrayView temp(m_data,defaultValue.getShape());
49     setPointDataView(temp);
50     // add remaining tags and values
51     addTaggedValues(tagKeys,values);
52     }
53    
54 jgs 119 DataTagged::DataTagged(const FunctionSpace& what,
55     const DataArrayView::ShapeType &shape,
56     const int tags[],
57     const DataArrayView::ValueType &data)
58     : DataAbstract(what)
59     {
60     //
61     // copy the data in the correct format
62     m_data=data;
63     //
64     // create the view of the data
65     DataArrayView tempView(m_data,shape);
66     setPointDataView(tempView);
67     //
68     // create the tag lookup map
69     for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
70     m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
71     }
72     }
73    
74 jgs 102 DataTagged::DataTagged(const DataTagged& other)
75     : DataAbstract(other.getFunctionSpace()),
76 jgs 82 m_data(other.m_data),
77     m_offsetLookup(other.m_offsetLookup)
78     {
79 jgs 119 // create the data view
80 jgs 82 DataArrayView temp(m_data,other.getPointDataView().getShape());
81     setPointDataView(temp);
82     }
83    
84 jgs 102 DataTagged::DataTagged(const DataConstant& other)
85     : DataAbstract(other.getFunctionSpace())
86 jgs 82 {
87     //
88     // Fill the default value with the constant value item from other
89     const DataArrayView& value=other.getPointDataView();
90     m_data.insert(m_data.end(), &value.getData(0), &value.getData(value.noValues()) );
91 jgs 119 // create the data view
92 jgs 82 DataArrayView temp(m_data,value.getShape());
93     setPointDataView(temp);
94     }
95    
96     DataTagged::DataTagged(const DataTagged& other,
97 jgs 102 const DataArrayView::RegionType& region)
98     : DataAbstract(other.getFunctionSpace())
99 jgs 82 {
100     //
101     // get the shape of the slice to copy from other
102     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
103 jgs 108 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
104 jgs 82 // allocate enough space for all values
105     m_data.resize(DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1));
106 jgs 119 // create the data view
107 jgs 82 DataArrayView temp(m_data,shape);
108     setPointDataView(temp);
109 jgs 119 // copy the default value
110 jgs 108 getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range);
111 jgs 82 //
112 jgs 119 // Loop through the tag values copying these
113 jgs 82 DataMapType::const_iterator pos;
114     DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
115     for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();++pos){
116 jgs 108 getPointDataView().copySlice(tagOffset,other.getPointDataView(), pos->second,region_loop_range);
117 jgs 82 m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
118     tagOffset+=getPointDataView().noValues();
119     }
120     }
121    
122     void
123     DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)
124     {
125     //
126     // can only reshape a rank zero data point
127     if (getPointDataView().getRank()!=0) {
128     stringstream temp;
129     temp << "Error - Can only reshape Data with data points of rank 0. "
130     << "This Data has data points with rank: "
131     << getPointDataView().getRank();
132     throw DataException(temp.str());
133     }
134     //
135     // allocate enough space for all values
136     DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));
137     DataArrayView newView(newData,shape);
138     newView.copy(0,getDefaultValue()());
139     //
140     // Loop through the tag values
141     DataMapType::iterator pos;
142     DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
143     for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){
144     newView.copy(tagOffset,m_data[pos->second]);
145     pos->second=tagOffset;
146     tagOffset+=DataArrayView::noValues(shape);
147     }
148     m_data=newData;
149     DataArrayView temp(m_data,shape);
150     setPointDataView(temp);
151     }
152    
153     DataAbstract*
154     DataTagged::getSlice(const DataArrayView::RegionType& region) const
155     {
156     return new DataTagged(*this,region);
157     }
158    
159     void
160     DataTagged::setSlice(const DataAbstract* value,
161     const DataArrayView::RegionType& region)
162     {
163     const DataTagged* tempDataTag=dynamic_cast<const DataTagged*>(value);
164     if (tempDataTag==0) {
165     throw DataException("Programming error - casting to DataTagged.");
166     }
167 jgs 108 //
168     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
169     DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
170     //
171 jgs 82 if (getPointDataView().getRank()!=region.size()) {
172     throw DataException("Error - Invalid slice region.");
173     }
174 jgs 108 if (tempDataTag->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {
175 jgs 82 throw DataException (value->getPointDataView().createShapeErrorMessage(
176     "Error - Couldn't copy slice due to shape mismatch.",shape));
177     }
178 jgs 102 //
179 jgs 108 getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range);
180 jgs 82 //
181     // Loop through the tag values
182     DataMapType::const_iterator pos;
183     for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){
184 jgs 108 getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range);
185 jgs 82 }
186     }
187    
188     void
189     DataTagged::setTaggedValue(int tagKey,
190     const DataArrayView& value)
191     {
192     DataMapType::iterator pos(m_offsetLookup.find(tagKey));
193     if (pos==m_offsetLookup.end()) {
194     //
195     // tag couldn't be found so add as a new tag
196     addTaggedValue(tagKey,value);
197     } else {
198     if (!getPointDataView().checkShape(value.getShape())) {
199     throw DataException(getPointDataView().createShapeErrorMessage(
200     "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
201     }
202     //
203     // copy the values into tagged data storage
204     copy(&value.getData(0), &value.getData(getPointDataView().noValues()), &m_data[pos->second]);
205     }
206     }
207    
208     void
209     DataTagged::setTaggedValues(const TagListType& tagKeys,
210     const ValueListType& values)
211     {
212     for (int i=0;i<tagKeys.size();++i) {
213     setTaggedValue(tagKeys[i],values[i]);
214     }
215     }
216    
217     void
218     DataTagged::addTaggedValue(int tagKey,
219     const DataArrayView& value)
220     {
221     if (!getPointDataView().checkShape(value.getShape())) {
222     throw DataException(getPointDataView().createShapeErrorMessage(
223     "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
224     }
225     //
226     // save the key and the location of its data
227     m_offsetLookup.insert( DataMapType::value_type(tagKey,m_data.size()) );
228     //
229     // insert the data given in value at the end of m_data
230     m_data.insert( m_data.end(), &(value.getData(0)), &(value.getData(value.noValues())) );
231     }
232    
233     void
234     DataTagged::addTaggedValues(const TagListType& tagKeys,
235     const ValueListType& values)
236     {
237     if (values.size()==0) {
238     //
239     // Copy the default value for each of the tags
240     TagListType::const_iterator iT;
241     for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {
242     //
243     // the point data view for DataTagged points at the default value
244     addTaggedValue(*iT,getPointDataView());
245     }
246     } else if (values.size()==1 && tagKeys.size()>1) {
247     //
248     // assume the one value will be used for all tag values
249     // Copy the input data
250     TagListType::const_iterator iT;
251     for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {
252     addTaggedValue(*iT,values[0]);
253     }
254     } else {
255     if (tagKeys.size()!=values.size()) {
256     stringstream temp;
257     temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
258     << " doesn't match the number of values: " << values.size();
259     throw DataException(temp.str());
260     } else {
261     for (int i=0;i<tagKeys.size();++i) {
262     addTaggedValue(tagKeys[i],values[i]);
263     }
264     }
265     }
266     }
267    
268     double*
269     DataTagged::getSampleDataByTag(int tag)
270     {
271     DataMapType::iterator pos(m_offsetLookup.find(tag));
272     if (pos==m_offsetLookup.end()) {
273     //
274     // tag couldn't be found so return the default value
275     return &(m_data[0]);
276     } else {
277     //
278     // return the data-point corresponding to the given tag
279     return &(m_data[pos->second]);
280     }
281     }
282    
283     string
284     DataTagged::toString() const
285     {
286     stringstream temp;
287     DataMapType::const_iterator i;
288     temp << "Tag(Default)" << endl;
289     temp << getDefaultValue().toString() << endl;
290     //
291     // create a temporary view as the offset will be changed
292     DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
293     for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
294     temp << "Tag(" << i->first << ")" << endl;
295     tempView.setOffset(i->second);
296     temp << tempView.toString() << endl;
297     }
298     return temp.str();
299     }
300    
301     DataArrayView
302     DataTagged::getDataPointByTag(int tag) const
303     {
304     DataMapType::const_iterator pos(m_offsetLookup.find(tag));
305     DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
306     if (pos!=m_offsetLookup.end()) {
307     offset=pos->second;
308     }
309     DataArrayView temp(getPointDataView());
310     temp.setOffset(offset);
311     return temp;
312     }
313    
314     DataArrayView::ValueType::size_type
315     DataTagged::getPointOffset(int sampleNo,
316     int dataPointNo) const
317     {
318     int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
319     DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
320     DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
321     if (pos!=m_offsetLookup.end()) {
322     offset=pos->second;
323     }
324     return offset;
325     }
326    
327     DataArrayView
328     DataTagged::getDataPoint(int sampleNo,
329     int dataPointNo)
330     {
331     EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo);
332     int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
333     return getDataPointByTag(tagKey);
334     }
335    
336     const DataTagged::DataMapType&
337     DataTagged::getTagLookup() const
338     {
339     return m_offsetLookup;
340     }
341    
342     DataArrayView::ValueType::size_type
343     DataTagged::getLength() const
344     {
345     return m_data.size();
346     }
347    
348     } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26