/[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 950 - (show annotations)
Tue Feb 6 07:01:11 2007 UTC (12 years, 3 months ago) by gross
Original Path: trunk/escript/src/DataTagged.cpp
File size: 19667 byte(s)
escript data objects can now be saved to netCDF files, see http://www.unidata.ucar.edu/software/netcdf/.
Currently only constant data are implemented with expanded and tagged data to follow.
There are two new functions to dump a data object

   s=Data(...)
   s.dump(<filename>)

and to recover it

   s=load(<filename>, domain)

Notice that the function space of s is recovered but domain is still need. 

dump and load will replace archive and extract.

The installation needs now the netCDF installed. 


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 DataAbstract*
135 DataTagged::getSlice(const DataArrayView::RegionType& region) const
136 {
137 return new DataTagged(*this, region);
138 }
139
140 DataTagged::DataTagged(const DataTagged& other,
141 const DataArrayView::RegionType& region)
142 : DataAbstract(other.getFunctionSpace())
143 {
144 // slice constructor
145
146 // get the shape of the slice to copy from other
147 DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
148 DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
149
150 // allocate enough space in this for all values
151 // (need to add one to allow for the default value)
152 int len = DataArrayView::noValues(regionShape)*(other.m_offsetLookup.size()+1);
153 m_data.resize(len,0.0,len);
154
155 // create the data view
156 DataArrayView temp(m_data,regionShape);
157 setPointDataView(temp);
158
159 // copy the default value from other to this
160 getDefaultValue().copySlice(other.getDefaultValue(), regionLoopRange);
161
162 // loop through the tag values copying these
163 DataMapType::const_iterator pos;
164 DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
165 for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){
166 getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,regionLoopRange);
167 m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
168 tagOffset+=getPointDataView().noValues();
169 }
170 }
171
172 void
173 DataTagged::setSlice(const DataAbstract* other,
174 const DataArrayView::RegionType& region)
175 {
176
177 // other must be another DataTagged object
178 // Data:setSlice implementation should ensure this
179 const DataTagged* otherTemp=dynamic_cast<const DataTagged*>(other);
180 if (otherTemp==0) {
181 throw DataException("Programming error - casting to DataTagged.");
182 }
183
184 // determine shape of the specified region
185 DataArrayView::ShapeType regionShape(DataArrayView::getResultSliceShape(region));
186
187 // modify region specification as needed to match rank of this object
188 DataArrayView::RegionLoopRangeType regionLoopRange=getSliceRegionLoopRange(region);
189
190 // ensure rank/shape of this object is compatible with specified region
191 if (getPointDataView().getRank()!=region.size()) {
192 throw DataException("Error - Invalid slice region.");
193 }
194 if (otherTemp->getPointDataView().getRank()>0 && !other->getPointDataView().checkShape(regionShape)) {
195 throw DataException (other->getPointDataView().createShapeErrorMessage(
196 "Error - Couldn't copy slice due to shape mismatch.",regionShape));
197 }
198
199 // copy slice from other default value to this default value
200 getDefaultValue().copySliceFrom(otherTemp->getDefaultValue(), regionLoopRange);
201
202 // loop through tag values in other, adding any which aren't in this, using default value
203 DataMapType::const_iterator pos;
204 for (pos=otherTemp->m_offsetLookup.begin();pos!=otherTemp->m_offsetLookup.end();pos++) {
205 if (!isCurrentTag(pos->first)) {
206 addTaggedValue(pos->first,getDefaultValue());
207 }
208 }
209
210 // loop through the tag values copying slices from other to this
211 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
212 getDataPointByTag(pos->first).copySliceFrom(otherTemp->getDataPointByTag(pos->first), regionLoopRange);
213 }
214
215 }
216
217 int
218 DataTagged::getTagNumber(int dpno)
219 {
220 //
221 // Get the number of samples and data-points per sample
222 int numSamples = getNumSamples();
223 int numDataPointsPerSample = getNumDPPSample();
224 int numDataPoints = numSamples * numDataPointsPerSample;
225
226 if (numDataPointsPerSample==0) {
227 throw DataException("DataTagged::getTagNumber error: no data-points associated with this object.");
228 }
229
230 if (dpno<0 || dpno>numDataPoints-1) {
231 throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied.");
232 }
233
234 //
235 // Determine the sample number which corresponds to this data-point number
236 int sampleNo = dpno / numDataPointsPerSample;
237
238 //
239 // Determine the tag number which corresponds to this sample number
240 int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo);
241
242 //
243 // return the tag number
244 return(tagNo);
245 }
246
247 void
248 DataTagged::setTaggedValues(const TagListType& tagKeys,
249 const ValueListType& values)
250 {
251 addTaggedValues(tagKeys,values);
252 }
253
254 void
255 DataTagged::setTaggedValue(int tagKey,
256 const DataArrayView& value)
257 {
258 if (!getPointDataView().checkShape(value.getShape())) {
259 throw DataException(getPointDataView().createShapeErrorMessage(
260 "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
261 }
262 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
263 if (pos==m_offsetLookup.end()) {
264 // tag couldn't be found so use addTaggedValue
265 addTaggedValue(tagKey,value);
266 } else {
267 // copy the values into the data array at the offset determined by m_offsetLookup
268 int offset=pos->second;
269 for (int i=0; i<getPointDataView().noValues(); i++) {
270 m_data[offset+i]=value.getData(i);
271 }
272 }
273 }
274
275 void
276 DataTagged::addTaggedValues(const TagListType& tagKeys,
277 const ValueListType& values)
278 {
279 if (values.size()==0) {
280 // copy the current default value for each of the tags
281 TagListType::const_iterator iT;
282 for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
283 // the point data view for DataTagged points at the default value
284 addTaggedValue(*iT,getPointDataView());
285 }
286 } else if (values.size()==1 && tagKeys.size()>1) {
287 // assume the one given value will be used for all tag values
288 TagListType::const_iterator iT;
289 for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) {
290 addTaggedValue(*iT,values[0]);
291 }
292 } else {
293 if (tagKeys.size()!=values.size()) {
294 stringstream temp;
295 temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
296 << " doesn't match number of values: " << values.size();
297 throw DataException(temp.str());
298 } else {
299 for (int i=0;i<tagKeys.size();i++) {
300 addTaggedValue(tagKeys[i],values[i]);
301 }
302 }
303 }
304 }
305
306 void
307 DataTagged::addTaggedValue(int tagKey,
308 const DataArrayView& value)
309 {
310 if (!getPointDataView().checkShape(value.getShape())) {
311 throw DataException(getPointDataView().createShapeErrorMessage(
312 "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
313 }
314 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
315 if (pos!=m_offsetLookup.end()) {
316 // tag already exists so use setTaggedValue
317 setTaggedValue(tagKey,value);
318 } else {
319 // save the key and the location of its data in the lookup tab
320 m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size()));
321 // add the data given in "value" at the end of m_data
322 // need to make a temp copy of m_data, resize m_data, then copy
323 // all the old values plus the value to be added back into m_data
324 ValueType m_data_temp(m_data);
325 int oldSize=m_data.size();
326 int newSize=m_data.size()+value.noValues();
327 m_data.resize(newSize,0.,newSize);
328 for (int i=0;i<oldSize;i++) {
329 m_data[i]=m_data_temp[i];
330 }
331 for (int i=0;i<value.noValues();i++) {
332 m_data[oldSize+i]=value.getData(i);
333 }
334 }
335 }
336
337 double*
338 DataTagged::getSampleDataByTag(int tag)
339 {
340 DataMapType::iterator pos(m_offsetLookup.find(tag));
341 if (pos==m_offsetLookup.end()) {
342 // tag couldn't be found so return the default value
343 return &(m_data[0]);
344 } else {
345 // return the data-point corresponding to the given tag
346 return &(m_data[pos->second]);
347 }
348 }
349
350 string
351 DataTagged::toString() const
352 {
353 stringstream temp;
354 DataMapType::const_iterator i;
355 temp << "Tag(Default)" << endl;
356 temp << getDefaultValue().toString() << endl;
357 // create a temporary view as the offset will be changed
358 DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
359 for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
360 temp << "Tag(" << i->first << ")" << endl;
361 tempView.setOffset(i->second);
362 temp << tempView.toString() << endl;
363 }
364 return temp.str();
365 }
366
367 DataArrayView::ValueType::size_type
368 DataTagged::getPointOffset(int sampleNo,
369 int dataPointNo) const
370 {
371 int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
372 DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
373 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
374 if (pos!=m_offsetLookup.end()) {
375 offset=pos->second;
376 }
377 return offset;
378 }
379
380 DataArrayView
381 DataTagged::getDataPointByTag(int tag) const
382 {
383 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
384 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
385 if (pos!=m_offsetLookup.end()) {
386 offset=pos->second;
387 }
388 DataArrayView temp(getPointDataView());
389 temp.setOffset(offset);
390 return temp;
391 }
392
393 DataArrayView
394 DataTagged::getDataPoint(int sampleNo,
395 int dataPointNo)
396 {
397 EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo);
398 int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
399 return getDataPointByTag(tagKey);
400 }
401
402 int
403 DataTagged::archiveData(ofstream& archiveFile,
404 const DataArrayView::ValueType::size_type noValues) const
405 {
406 return(m_data.archiveData(archiveFile, noValues));
407 }
408
409 int
410 DataTagged::extractData(ifstream& archiveFile,
411 const DataArrayView::ValueType::size_type noValues)
412 {
413 return(m_data.extractData(archiveFile, noValues));
414 }
415 void
416 DataTagged::symmetric(DataAbstract* ev)
417 {
418 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
419 if (temp_ev==0) {
420 throw DataException("Error - DataTagged::symmetric casting to DataTagged failed (probably a programming error).");
421 }
422 const DataTagged::DataMapType& thisLookup=getTagLookup();
423 DataTagged::DataMapType::const_iterator i;
424 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
425 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
426 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
427 DataArrayView thisView=getDataPointByTag(i->first);
428 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
429 DataArrayView::symmetric(thisView,0,evView,0);
430 }
431 DataArrayView::symmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
432 }
433 void
434 DataTagged::nonsymmetric(DataAbstract* ev)
435 {
436 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
437 if (temp_ev==0) {
438 throw DataException("Error - DataTagged::nonsymmetric casting to DataTagged failed (probably a programming error).");
439 }
440 const DataTagged::DataMapType& thisLookup=getTagLookup();
441 DataTagged::DataMapType::const_iterator i;
442 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
443 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
444 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
445 DataArrayView thisView=getDataPointByTag(i->first);
446 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
447 DataArrayView::nonsymmetric(thisView,0,evView,0);
448 }
449 DataArrayView::nonsymmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
450 }
451 void
452 DataTagged::trace(DataAbstract* ev, int axis_offset)
453 {
454 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
455 if (temp_ev==0) {
456 throw DataException("Error - DataTagged::trace casting to DataTagged failed (probably a programming error).");
457 }
458 const DataTagged::DataMapType& thisLookup=getTagLookup();
459 DataTagged::DataMapType::const_iterator i;
460 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
461 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
462 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
463 DataArrayView thisView=getDataPointByTag(i->first);
464 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
465 DataArrayView::trace(thisView,0,evView,0, axis_offset);
466 }
467 DataArrayView::trace(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
468 }
469
470 void
471 DataTagged::transpose(DataAbstract* ev, int axis_offset)
472 {
473 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
474 if (temp_ev==0) {
475 throw DataException("Error - DataTagged::transpose casting to DataTagged failed (probably a programming error).");
476 }
477 const DataTagged::DataMapType& thisLookup=getTagLookup();
478 DataTagged::DataMapType::const_iterator i;
479 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
480 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
481 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
482 DataArrayView thisView=getDataPointByTag(i->first);
483 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
484 DataArrayView::transpose(thisView,0,evView,0, axis_offset);
485 }
486 DataArrayView::transpose(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset);
487 }
488
489 void
490 DataTagged::swapaxes(DataAbstract* ev, int axis0, int axis1)
491 {
492 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
493 if (temp_ev==0) {
494 throw DataException("Error - DataTagged::swapaxes casting to DataTagged failed (probably a programming error).");
495 }
496 const DataTagged::DataMapType& thisLookup=getTagLookup();
497 DataTagged::DataMapType::const_iterator i;
498 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
499 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
500 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
501 DataArrayView thisView=getDataPointByTag(i->first);
502 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
503 DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
504 }
505 DataArrayView::swapaxes(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis0,axis1);
506 }
507
508 void
509 DataTagged::eigenvalues(DataAbstract* ev)
510 {
511 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
512 if (temp_ev==0) {
513 throw DataException("Error - DataTagged::eigenvalues casting to DataTagged failed (propably a programming error).");
514 }
515 const DataTagged::DataMapType& thisLookup=getTagLookup();
516 DataTagged::DataMapType::const_iterator i;
517 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
518 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
519 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
520 DataArrayView thisView=getDataPointByTag(i->first);
521 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
522 DataArrayView::eigenvalues(thisView,0,evView,0);
523 }
524 DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0);
525 }
526 void
527 DataTagged::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
528 {
529 DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev);
530 if (temp_ev==0) {
531 throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
532 }
533 DataTagged* temp_V=dynamic_cast<DataTagged*>(V);
534 if (temp_V==0) {
535 throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error).");
536 }
537 const DataTagged::DataMapType& thisLookup=getTagLookup();
538 DataTagged::DataMapType::const_iterator i;
539 DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end();
540 for (i=thisLookup.begin();i!=thisLookupEnd;i++) {
541 temp_ev->addTaggedValue(i->first,temp_ev->getDefaultValue());
542 temp_V->addTaggedValue(i->first,temp_V->getDefaultValue());
543 DataArrayView thisView=getDataPointByTag(i->first);
544 DataArrayView evView=temp_ev->getDataPointByTag(i->first);
545 DataArrayView VView=temp_V->getDataPointByTag(i->first);
546 DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
547 }
548 DataArrayView::eigenvalues_and_eigenvectors(getDefaultValue(),0,
549 temp_ev->getDefaultValue(),0,
550 temp_V->getDefaultValue(),0,
551 tol);
552
553
554 }
555
556 void
557 DataTagged::dump(const std::string fileName) const
558 {
559 throw DataException("Error - DataTagged:: dump: not implemented.");
560 }
561
562
563 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26