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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 496 - (hide annotations)
Tue Feb 7 01:27:14 2006 UTC (13 years, 8 months ago) by jgs
File size: 12910 byte(s)
minor fix to DataTagged::getTagNumber
changes to comments and formatting
inline two methods

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