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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 121 - (hide annotations)
Fri May 6 04:26:16 2005 UTC (14 years, 5 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataTagged.cpp
File size: 11875 byte(s)
Merge of development branch back to main trunk on 2005-05-06

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26