/[escript]/branches/arrexp_2137_win/escript/src/DataTagged.cpp
ViewVC logotype

Contents of /branches/arrexp_2137_win/escript/src/DataTagged.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 509 - (show annotations)
Fri Feb 10 03:24:44 2006 UTC (13 years, 3 months ago) by jgs
Original Path: trunk/escript/src/DataTagged.cpp
File size: 13096 byte(s)
minor comment changes

1 // $Id$
2
3 /*
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 "DataTagged.h"
18
19 #include "DataConstant.h"
20 #include "DataException.h"
21
22 using namespace std;
23
24 namespace escript {
25
26 DataTagged::DataTagged()
27 : DataAbstract(FunctionSpace())
28 {
29 // default constructor
30
31 // create a scalar default value
32 m_data.resize(1,0.,1);
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 const FunctionSpace& what)
41 : DataAbstract(what)
42 {
43 // constructor
44
45 // initialise the array of data values
46 // the default value is always the first item in the values list
47 int len = defaultValue.noValues();
48 m_data.resize(len,0.,len);
49 for (int i=0; i<defaultValue.noValues(); i++) {
50 m_data[i]=defaultValue.getData(i);
51 }
52
53 // create the data view
54 DataArrayView temp(m_data,defaultValue.getShape());
55 setPointDataView(temp);
56
57 // add remaining tags and values
58 addTaggedValues(tagKeys,values);
59 }
60
61 DataTagged::DataTagged(const FunctionSpace& what,
62 const DataArrayView::ShapeType &shape,
63 const int tags[],
64 const ValueType& data)
65 : DataAbstract(what)
66 {
67 // alternative constructor
68 // not unit_tested tested yet
69
70 // copy the data
71 m_data=data;
72
73 // create the view of the data
74 DataArrayView tempView(m_data,shape);
75 setPointDataView(tempView);
76
77 // 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 DataTagged::DataTagged(const DataTagged& other)
84 : DataAbstract(other.getFunctionSpace()),
85 m_data(other.m_data),
86 m_offsetLookup(other.m_offsetLookup)
87 {
88 // copy constructor
89
90 // create the data view
91 DataArrayView temp(m_data,other.getPointDataView().getShape());
92 setPointDataView(temp);
93 }
94
95 DataTagged::DataTagged(const DataConstant& other)
96 : DataAbstract(other.getFunctionSpace())
97 {
98 // copy constructor
99
100 // fill the default value with the constant value item from "other"
101 const DataArrayView& value=other.getPointDataView();
102 int len = value.noValues();
103 m_data.resize(len,0.,len);
104 for (int i=0; i<value.noValues(); i++) {
105 m_data[i]=value.getData(i);
106 }
107
108 // create the data view
109 DataArrayView temp(m_data,value.getShape());
110 setPointDataView(temp);
111 }
112
113 DataTagged::DataTagged(const DataTagged& other,
114 const DataArrayView::RegionType& region)
115 : DataAbstract(other.getFunctionSpace())
116 {
117 // slice constructor
118
119 // get the shape of the slice to copy from other
120 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
121 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
122
123 // allocate enough space for all values
124 int len = DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1);
125 m_data.resize(len,0.,len);
126
127 // create the data view
128 DataArrayView temp(m_data,shape);
129 setPointDataView(temp);
130
131 // copy the default value
132 getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range);
133
134 // loop through the tag values copying these
135 DataMapType::const_iterator pos;
136 DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
137 for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
138 getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,region_loop_range);
139 m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
140 tagOffset+=getPointDataView().noValues();
141 }
142 }
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
156 // 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
161 // loop through the tag values
162 DataMapType::iterator pos;
163 DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
164 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){
165 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
189 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
190 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
191 if (getPointDataView().getRank()!=region.size()) {
192 throw DataException("Error - Invalid slice region.");
193 }
194 if (tempDataTag->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {
195 throw DataException (value->getPointDataView().createShapeErrorMessage(
196 "Error - Couldn't copy slice due to shape mismatch.",shape));
197 }
198
199 getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range);
200
201 // loop through the tag values
202 DataMapType::const_iterator pos;
203 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
204 getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range);
205 }
206 }
207
208 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 if (dpno<0 || dpno>numDataPoints-1) {
222 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 void
239 DataTagged::setTaggedValues(const TagListType& tagKeys,
240 const ValueListType& values)
241 {
242 addTaggedValues(tagKeys,values);
243 }
244
245 void
246 DataTagged::setTaggedValue(int tagKey,
247 const DataArrayView& value)
248 {
249 if (!getPointDataView().checkShape(value.getShape())) {
250 throw DataException(getPointDataView().createShapeErrorMessage(
251 "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
252 }
253 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
254 if (pos==m_offsetLookup.end()) {
255 // tag couldn't be found so use addTaggedValue
256 addTaggedValue(tagKey,value);
257 } else {
258 // 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 }
263 }
264 }
265
266 void
267 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 DataTagged::addTaggedValue(int tagKey,
299 const DataArrayView& value)
300 {
301 if (!getPointDataView().checkShape(value.getShape())) {
302 throw DataException(getPointDataView().createShapeErrorMessage(
303 "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
304 }
305 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 m_data.resize(newSize,0.,newSize);
319 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 }
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 DataArrayView::ValueType::size_type
359 DataTagged::getPointOffset(int sampleNo,
360 int dataPointNo) const
361 {
362 int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
363 DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
364 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
365 if (pos!=m_offsetLookup.end()) {
366 offset=pos->second;
367 }
368 return offset;
369 }
370
371 DataArrayView
372 DataTagged::getDataPointByTag(int tag) const
373 {
374 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
375 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
376 if (pos!=m_offsetLookup.end()) {
377 offset=pos->second;
378 }
379 DataArrayView temp(getPointDataView());
380 temp.setOffset(offset);
381 return temp;
382 }
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 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 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26