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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 509 - (hide annotations)
Fri Feb 10 03:24:44 2006 UTC (13 years, 8 months ago) by jgs
File size: 13096 byte(s)
minor comment changes

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26