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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 757 - (show annotations)
Mon Jun 26 13:12:56 2006 UTC (13 years, 2 months ago) by woo409
File size: 16416 byte(s)
+ Merge of intelc_win32 branch (revision 741:755) with trunk. Tested on iVEC altix (run_tests and py_tests all pass)

1 // $Id$
2
3 /*
4 ************************************************************
5 * Copyright 2006 by ACcESS MNRF *
6 * *
7 * http://www.access.edu.au *
8 * Primary Business: Queensland, Australia *
9 * Licensed under the Open Software License version 3.0 *
10 * http://www.opensource.org/licenses/osl-3.0.php *
11 * *
12 ************************************************************
13 */
14
15 #include "DataTagged.h"
16
17 #include "DataConstant.h"
18 #include "DataException.h"
19
20 using namespace std;
21
22 namespace escript {
23
24 DataTagged::DataTagged()
25 : DataAbstract(FunctionSpace())
26 {
27 // default constructor
28
29 // create a scalar default value
30 m_data.resize(1,0.,1);
31 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 const FunctionSpace& what)
39 : DataAbstract(what)
40 {
41 // constructor
42
43 // initialise the array of data values
44 // the default value is always the first item in the values list
45 int len = defaultValue.noValues();
46 m_data.resize(len,0.,len);
47 for (int i=0; i<defaultValue.noValues(); i++) {
48 m_data[i]=defaultValue.getData(i);
49 }
50
51 // create the data view
52 DataArrayView temp(m_data,defaultValue.getShape());
53 setPointDataView(temp);
54
55 // add remaining tags and values
56 addTaggedValues(tagKeys,values);
57 }
58
59 DataTagged::DataTagged(const FunctionSpace& what,
60 const DataArrayView::ShapeType &shape,
61 const int tags[],
62 const ValueType& data)
63 : DataAbstract(what)
64 {
65 // alternative constructor
66 // not unit_tested tested yet
67
68 // copy the data
69 m_data=data;
70
71 // create the view of the data
72 DataArrayView tempView(m_data,shape);
73 setPointDataView(tempView);
74
75 // create the tag lookup map
76 for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
77 m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
78 }
79 }
80
81 DataTagged::DataTagged(const FunctionSpace& what,
82 const DataArrayView::ShapeType &shape,
83 const TagListType& tags,
84 const ValueType& data)
85 : DataAbstract(what)
86 {
87 // alternative constructor
88 // not unit_tested tested yet
89
90 // copy the data
91 m_data=data;
92
93 // create the view of the data
94 DataArrayView tempView(m_data,shape);
95 setPointDataView(tempView);
96
97 // create the tag lookup map
98 for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) {
99 m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo]));
100 }
101 }
102
103
104 DataTagged::DataTagged(const DataTagged& other)
105 : DataAbstract(other.getFunctionSpace()),
106 m_data(other.m_data),
107 m_offsetLookup(other.m_offsetLookup)
108 {
109 // copy constructor
110
111 // create the data view
112 DataArrayView temp(m_data,other.getPointDataView().getShape());
113 setPointDataView(temp);
114 }
115
116 DataTagged::DataTagged(const DataConstant& other)
117 : DataAbstract(other.getFunctionSpace())
118 {
119 // copy constructor
120
121 // fill the default value with the constant value item from "other"
122 const DataArrayView& value=other.getPointDataView();
123 int len = value.noValues();
124 m_data.resize(len,0.,len);
125 for (int i=0; i<value.noValues(); i++) {
126 m_data[i]=value.getData(i);
127 }
128
129 // create the data view
130 DataArrayView temp(m_data,value.getShape());
131 setPointDataView(temp);
132 }
133
134 void
135 DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)
136 {
137 // can only reshape a rank zero data point
138 if (getPointDataView().getRank()!=0) {
139 stringstream temp;
140 temp << "Error - Can only reshape Data with data points of rank 0. "
141 << "This Data has data points with rank: "
142 << getPointDataView().getRank();
143 throw DataException(temp.str());
144 }
145
146 // allocate enough space for all values
147 DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));
148 DataArrayView newView(newData,shape);
149 newView.copy(0,getDefaultValue()());
150
151 // loop through the tag values
152 DataMapType::iterator pos;
153 DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
154 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){
155 newView.copy(tagOffset,m_data[pos->second]);
156 pos->second=tagOffset;
157 tagOffset+=DataArrayView::noValues(shape);
158 }
159 m_data=newData;
160 DataArrayView temp(m_data,shape);
161 setPointDataView(temp);
162 }
163
164 DataAbstract*
165 DataTagged::getSlice(const DataArrayView::RegionType& region) const
166 {
167 return new DataTagged(*this, region);
168 }
169
170 DataTagged::DataTagged(const DataTagged& other,
171 const DataArrayView::RegionType& region)
172 : DataAbstract(other.getFunctionSpace())
173 {
174 // slice constructor
175
176 // get the shape of the slice to copy from other
177 DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
178 DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
179
180 // allocate enough space in this for all values
181 // (need to add one to allow for the default value)
182 int len = DataArrayView::noValues(regionShape)*(other.m_offsetLookup.size()+1);
183 m_data.resize(len,0.0,len);
184
185 // create the data view
186 DataArrayView temp(m_data,regionShape);
187 setPointDataView(temp);
188
189 // copy the default value from other to this
190 getDefaultValue().copySlice(other.getDefaultValue(), regionLoopRange);
191
192 // loop through the tag values copying these
193 DataMapType::const_iterator pos;
194 DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
195 for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
196 getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,regionLoopRange);
197 m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
198 tagOffset+=getPointDataView().noValues();
199 }
200 }
201
202 void
203 DataTagged::setSlice(const DataAbstract* other,
204 const DataArrayView::RegionType& region)
205 {
206
207 // other must be another DataTagged object
208 // Data:setSlice implementation should ensure this
209 const DataTagged* otherTemp=dynamic_cast<const DataTagged*>(other);
210 if (otherTemp==0) {
211 throw DataException("Programming error - casting to DataTagged.");
212 }
213
214 // determine shape of the specified region
215 DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
216
217 // modify region specification as needed to match rank of this object
218 DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
219
220 // ensure rank/shape of this object is compatible with specified region
221 if (getPointDataView().getRank()!=region.size()) {
222 throw DataException("Error - Invalid slice region.");
223 }
224 if (otherTemp->getPointDataView().getRank()>0 && !other->getPointDataView().checkShape(regionShape)) {
225 throw DataException (other->getPointDataView().createShapeErrorMessage(
226 "Error - Couldn't copy slice due to shape mismatch.",regionShape));
227 }
228
229 // copy slice from other default value to this default value
230 getDefaultValue().copySliceFrom(otherTemp->getDefaultValue(), regionLoopRange);
231
232 // loop through tag values in other, adding any which aren't in this, using default value
233 DataMapType::const_iterator pos;
234 for (pos=otherTemp->m_offsetLookup.begin();pos!=otherTemp->m_offsetLookup.end();pos++) {
235 if (!isCurrentTag(pos->first)) {
236 addTaggedValue(pos->first,getDefaultValue());
237 }
238 }
239
240 // loop through the tag values copying slices from other to this
241 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
242 getDataPointByTag(pos->first).copySliceFrom(otherTemp->getDataPointByTag(pos->first), regionLoopRange);
243 }
244
245 }
246
247 int
248 DataTagged::getTagNumber(int dpno)
249 {
250 //
251 // Get the number of samples and data-points per sample
252 int numSamples = getNumSamples();
253 int numDataPointsPerSample = getNumDPPSample();
254 int numDataPoints = numSamples * numDataPointsPerSample;
255
256 if (numDataPointsPerSample==0) {
257 throw DataException("DataTagged::getTagNumber error: no data-points associated with this object.");
258 }
259
260 if (dpno<0 || dpno>numDataPoints-1) {
261 throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied.");
262 }
263
264 //
265 // Determine the sample number which corresponds to this data-point number
266 int sampleNo = dpno / numDataPointsPerSample;
267
268 //
269 // Determine the tag number which corresponds to this sample number
270 int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo);
271
272 //
273 // return the tag number
274 return(tagNo);
275 }
276
277 void
278 DataTagged::setTaggedValues(const TagListType& tagKeys,
279 const ValueListType& values)
280 {
281 addTaggedValues(tagKeys,values);
282 }
283
284 void
285 DataTagged::setTaggedValue(int tagKey,
286 const DataArrayView& value)
287 {
288 if (!getPointDataView().checkShape(value.getShape())) {
289 throw DataException(getPointDataView().createShapeErrorMessage(
290 "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
291 }
292 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
293 if (pos==m_offsetLookup.end()) {
294 // tag couldn't be found so use addTaggedValue
295 addTaggedValue(tagKey,value);
296 } else {
297 // copy the values into the data array at the offset determined by m_offsetLookup
298 int offset=pos->second;
299 for (int i=0; i<getPointDataView().noValues(); i++) {
300 m_data[offset+i]=value.getData(i);
301 }
302 }
303 }
304
305 void
306 DataTagged::addTaggedValues(const TagListType& tagKeys,
307 const ValueListType& values)
308 {
309 if (values.size()==0) {
310 // copy the current default value for each of the tags
311 TagListType::const_iterator iT;
312 for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
313 // the point data view for DataTagged points at the default value
314 addTaggedValue(*iT,getPointDataView());
315 }
316 } else if (values.size()==1 && tagKeys.size()>1) {
317 // assume the one given value will be used for all tag values
318 TagListType::const_iterator iT;
319 for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
320 addTaggedValue(*iT,values[0]);
321 }
322 } else {
323 if (tagKeys.size()!=values.size()) {
324 stringstream temp;
325 temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
326 << " doesn't match number of values: " << values.size();
327 throw DataException(temp.str());
328 } else {
329 for (int i=0;i<tagKeys.size();i++) {
330 addTaggedValue(tagKeys[i],values[i]);
331 }
332 }
333 }
334 }
335
336 void
337 DataTagged::addTaggedValue(int tagKey,
338 const DataArrayView& value)
339 {
340 if (!getPointDataView().checkShape(value.getShape())) {
341 throw DataException(getPointDataView().createShapeErrorMessage(
342 "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
343 }
344 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
345 if (pos!=m_offsetLookup.end()) {
346 // tag already exists so use setTaggedValue
347 setTaggedValue(tagKey,value);
348 } else {
349 // save the key and the location of its data in the lookup tab
350 m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size()));
351 // add the data given in "value" at the end of m_data
352 // need to make a temp copy of m_data, resize m_data, then copy
353 // all the old values plus the value to be added back into m_data
354 ValueType m_data_temp(m_data);
355 int oldSize=m_data.size();
356 int newSize=m_data.size()+value.noValues();
357 m_data.resize(newSize,0.,newSize);
358 for (int i=0;i<oldSize;i++) {
359 m_data[i]=m_data_temp[i];
360 }
361 for (int i=0;i<value.noValues();i++) {
362 m_data[oldSize+i]=value.getData(i);
363 }
364 }
365 }
366
367 double*
368 DataTagged::getSampleDataByTag(int tag)
369 {
370 DataMapType::iterator pos(m_offsetLookup.find(tag));
371 if (pos==m_offsetLookup.end()) {
372 // tag couldn't be found so return the default value
373 return &(m_data[0]);
374 } else {
375 // return the data-point corresponding to the given tag
376 return &(m_data[pos->second]);
377 }
378 }
379
380 string
381 DataTagged::toString() const
382 {
383 stringstream temp;
384 DataMapType::const_iterator i;
385 temp << "Tag(Default)" << endl;
386 temp << getDefaultValue().toString() << endl;
387 // create a temporary view as the offset will be changed
388 DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
389 for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
390 temp << "Tag(" << i->first << ")" << endl;
391 tempView.setOffset(i->second);
392 temp << tempView.toString() << endl;
393 }
394 return temp.str();
395 }
396
397 DataArrayView::ValueType::size_type
398 DataTagged::getPointOffset(int sampleNo,
399 int dataPointNo) const
400 {
401 int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
402 DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
403 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
404 if (pos!=m_offsetLookup.end()) {
405 offset=pos->second;
406 }
407 return offset;
408 }
409
410 DataArrayView
411 DataTagged::getDataPointByTag(int tag) const
412 {
413 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
414 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
415 if (pos!=m_offsetLookup.end()) {
416 offset=pos->second;
417 }
418 DataArrayView temp(getPointDataView());
419 temp.setOffset(offset);
420 return temp;
421 }
422
423 DataArrayView
424 DataTagged::getDataPoint(int sampleNo,
425 int dataPointNo)
426 {
427 EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo);
428 int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
429 return getDataPointByTag(tagKey);
430 }
431
432 int
433 DataTagged::archiveData(ofstream& archiveFile,
434 const DataArrayView::ValueType::size_type noValues) const
435 {
436 return(m_data.archiveData(archiveFile, noValues));
437 }
438
439 int
440 DataTagged::extractData(ifstream& archiveFile,
441 const DataArrayView::ValueType::size_type noValues)
442 {
443 return(m_data.extractData(archiveFile, noValues));
444 }
445 void
446 DataTagged::eigenvalues(DataAbstract* ev)
447 {
448 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
449 if (temp_ev==0) {
450 throw DataException("Error - DataTagged::eigenvalues casting to DataTagged failed (propably a programming error).");
451 }
452 const DataTagged::DataMapType& thisLookup=getTagLookup();
453 DataTagged::DataMapType::const_iterator i;
454 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
455 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
456 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
457 DataArrayView thisView=getDataPointByTag(i->first);
458 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
459 DataArrayView::eigenvalues(thisView,0,evView,0);
460 }
461 DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
462 }
463 void
464 DataTagged::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
465 {
466 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
467 if (temp_ev==0) {
468 throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
469 }
470 DataTagged* temp_V=dynamic_cast<DataTagged*>(V);
471 if (temp_V==0) {
472 throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
473 }
474 const DataTagged::DataMapType& thisLookup=getTagLookup();
475 DataTagged::DataMapType::const_iterator i;
476 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
477 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
478 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
479 temp_V->addTaggedValue(i->first,temp_V->getDefaultValue());
480 DataArrayView thisView=getDataPointByTag(i->first);
481 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
482 DataArrayView VView=temp_V->getDataPointByTag(i->first);
483 DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
484 }
485 DataArrayView::eigenvalues_and_eigenvectors(getDefaultValue(),0,
486 temp_ev->getDefaultValue(),0,
487 temp_V->getDefaultValue(),0,
488 tol);
489
490
491 }
492
493
494 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26