1 |
|
2 |
/* $Id$ */ |
3 |
|
4 |
/******************************************************* |
5 |
* |
6 |
* Copyright 2003-2007 by ACceSS MNRF |
7 |
* Copyright 2007 by University of Queensland |
8 |
* |
9 |
* http://esscc.uq.edu.au |
10 |
* Primary Business: Queensland, Australia |
11 |
* Licensed under the Open Software License version 3.0 |
12 |
* http://www.opensource.org/licenses/osl-3.0.php |
13 |
* |
14 |
*******************************************************/ |
15 |
|
16 |
#include "DataTagged.h" |
17 |
#include "esysUtils/esys_malloc.h" |
18 |
|
19 |
#include "DataConstant.h" |
20 |
#include "DataException.h" |
21 |
#ifdef USE_NETCDF |
22 |
#include <netcdfcpp.h> |
23 |
#endif |
24 |
|
25 |
using namespace std; |
26 |
|
27 |
namespace escript { |
28 |
|
29 |
DataTagged::DataTagged() |
30 |
: DataAbstract(FunctionSpace()) |
31 |
{ |
32 |
// default constructor |
33 |
|
34 |
// create a scalar default value |
35 |
m_data.resize(1,0.,1); |
36 |
DataArrayView temp(m_data,DataTypes::ShapeType()); |
37 |
setPointDataView(temp); |
38 |
} |
39 |
|
40 |
DataTagged::DataTagged(const TagListType& tagKeys, |
41 |
const ValueListType& values, |
42 |
const DataArrayView& defaultValue, |
43 |
const FunctionSpace& what) |
44 |
: DataAbstract(what) |
45 |
{ |
46 |
// constructor |
47 |
|
48 |
// initialise the array of data values |
49 |
// the default value is always the first item in the values list |
50 |
int len = defaultValue.noValues(); |
51 |
m_data.resize(len,0.,len); |
52 |
for (int i=0; i<defaultValue.noValues(); i++) { |
53 |
m_data[i]=defaultValue.getData(i); |
54 |
} |
55 |
|
56 |
// create the data view |
57 |
DataArrayView temp(m_data,defaultValue.getShape()); |
58 |
setPointDataView(temp); |
59 |
|
60 |
// add remaining tags and values |
61 |
addTaggedValues(tagKeys,values); |
62 |
} |
63 |
|
64 |
DataTagged::DataTagged(const FunctionSpace& what, |
65 |
const DataTypes::ShapeType &shape, |
66 |
const int tags[], |
67 |
const ValueType& data) |
68 |
: DataAbstract(what) |
69 |
{ |
70 |
// alternative constructor |
71 |
// not unit_tested tested yet |
72 |
|
73 |
// copy the data |
74 |
m_data=data; |
75 |
|
76 |
// create the view of the data |
77 |
DataArrayView tempView(m_data,shape); |
78 |
setPointDataView(tempView); |
79 |
|
80 |
// create the tag lookup map |
81 |
for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) { |
82 |
m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo])); |
83 |
} |
84 |
} |
85 |
|
86 |
DataTagged::DataTagged(const FunctionSpace& what, |
87 |
const DataTypes::ShapeType &shape, |
88 |
const TagListType& tags, |
89 |
const ValueType& data) |
90 |
: DataAbstract(what) |
91 |
{ |
92 |
// alternative constructor |
93 |
// not unit_tested tested yet |
94 |
|
95 |
// copy the data |
96 |
m_data=data; |
97 |
|
98 |
// create the view of the data |
99 |
DataArrayView tempView(m_data,shape); |
100 |
setPointDataView(tempView); |
101 |
|
102 |
// create the tag lookup map |
103 |
for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) { |
104 |
m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo])); |
105 |
} |
106 |
} |
107 |
|
108 |
|
109 |
DataTagged::DataTagged(const DataTagged& other) |
110 |
: DataAbstract(other.getFunctionSpace()), |
111 |
m_data(other.m_data), |
112 |
m_offsetLookup(other.m_offsetLookup) |
113 |
{ |
114 |
// copy constructor |
115 |
|
116 |
// create the data view |
117 |
DataArrayView temp(m_data,other.getPointDataView().getShape()); |
118 |
setPointDataView(temp); |
119 |
} |
120 |
|
121 |
DataTagged::DataTagged(const DataConstant& other) |
122 |
: DataAbstract(other.getFunctionSpace()) |
123 |
{ |
124 |
// copy constructor |
125 |
|
126 |
// fill the default value with the constant value item from "other" |
127 |
const DataArrayView& value=other.getPointDataView(); |
128 |
int len = value.noValues(); |
129 |
m_data.resize(len,0.,len); |
130 |
for (int i=0; i<value.noValues(); i++) { |
131 |
m_data[i]=value.getData(i); |
132 |
} |
133 |
|
134 |
// create the data view |
135 |
DataArrayView temp(m_data,value.getShape()); |
136 |
setPointDataView(temp); |
137 |
} |
138 |
|
139 |
|
140 |
// Create a new object by copying tags |
141 |
DataTagged::DataTagged(const FunctionSpace& what, |
142 |
const DataTypes::ShapeType& shape, |
143 |
const DataTypes::ValueType& defaultvalue, |
144 |
const DataTagged& tagsource) |
145 |
: DataAbstract(what) |
146 |
{ |
147 |
// This constructor has not been unit tested yet |
148 |
|
149 |
if (defaultvalue.size()!=DataTypes::noValues(shape)) { |
150 |
throw DataException("Programming error - defaultvalue does not match supplied shape."); |
151 |
} |
152 |
|
153 |
setShape(shape); |
154 |
|
155 |
int numtags=tagsource.getTagLookup().size(); |
156 |
// m_offsetLookup.reserve(tagsource.getTagLookup().size()); |
157 |
m_data.resize(defaultvalue.size(),0.); // since this is tagged data, we should have blocksize=1 |
158 |
|
159 |
// need to set the default value .... |
160 |
for (int i=0; i<defaultvalue.size(); i++) { |
161 |
m_data[i]=defaultvalue[i]; |
162 |
} |
163 |
|
164 |
|
165 |
// create the data view |
166 |
DataArrayView temp(m_data,shape); |
167 |
setPointDataView(temp); |
168 |
|
169 |
DataTagged::DataMapType::const_iterator i; |
170 |
for (i=tagsource.getTagLookup().begin();i!=tagsource.getTagLookup().end();i++) { |
171 |
addTag(i->first); |
172 |
} |
173 |
} |
174 |
|
175 |
|
176 |
DataAbstract* |
177 |
DataTagged::getSlice(const DataTypes::RegionType& region) const |
178 |
{ |
179 |
return new DataTagged(*this, region); |
180 |
} |
181 |
|
182 |
DataTagged::DataTagged(const DataTagged& other, |
183 |
const DataTypes::RegionType& region) |
184 |
: DataAbstract(other.getFunctionSpace()) |
185 |
{ |
186 |
// slice constructor |
187 |
|
188 |
// get the shape of the slice to copy from other |
189 |
DataTypes::ShapeType regionShape(DataTypes::getResultSliceShape(region)); |
190 |
DataTypes::RegionLoopRangeType regionLoopRange=DataTypes::getSliceRegionLoopRange(region); |
191 |
|
192 |
// allocate enough space in this for all values |
193 |
// (need to add one to allow for the default value) |
194 |
int len = DataTypes::noValues(regionShape)*(other.m_offsetLookup.size()+1); |
195 |
m_data.resize(len,0.0,len); |
196 |
|
197 |
// create the data view |
198 |
DataArrayView temp(m_data,regionShape); |
199 |
setPointDataView(temp); |
200 |
|
201 |
// copy the default value from other to this |
202 |
getDefaultValue().copySlice(other.getDefaultValue(), regionLoopRange); |
203 |
|
204 |
// loop through the tag values copying these |
205 |
DataMapType::const_iterator pos; |
206 |
DataTypes::ValueType::size_type tagOffset=getPointDataView().noValues(); |
207 |
for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){ |
208 |
getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,regionLoopRange); |
209 |
m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset)); |
210 |
tagOffset+=getPointDataView().noValues(); |
211 |
} |
212 |
} |
213 |
|
214 |
void |
215 |
DataTagged::setSlice(const DataAbstract* other, |
216 |
const DataTypes::RegionType& region) |
217 |
{ |
218 |
|
219 |
// other must be another DataTagged object |
220 |
// Data:setSlice implementation should ensure this |
221 |
const DataTagged* otherTemp=dynamic_cast<const DataTagged*>(other); |
222 |
if (otherTemp==0) { |
223 |
throw DataException("Programming error - casting to DataTagged."); |
224 |
} |
225 |
|
226 |
// determine shape of the specified region |
227 |
DataTypes::ShapeType regionShape(DataTypes::getResultSliceShape(region)); |
228 |
|
229 |
// modify region specification as needed to match rank of this object |
230 |
DataTypes::RegionLoopRangeType regionLoopRange=DataTypes::getSliceRegionLoopRange(region); |
231 |
|
232 |
// ensure rank/shape of this object is compatible with specified region |
233 |
if (getPointDataView().getRank()!=region.size()) { |
234 |
throw DataException("Error - Invalid slice region."); |
235 |
} |
236 |
if (otherTemp->getPointDataView().getRank()>0 && !other->getPointDataView().checkShape(regionShape)) { |
237 |
throw DataException (other->getPointDataView().createShapeErrorMessage( |
238 |
"Error - Couldn't copy slice due to shape mismatch.",regionShape)); |
239 |
} |
240 |
|
241 |
// copy slice from other default value to this default value |
242 |
getDefaultValue().copySliceFrom(otherTemp->getDefaultValue(), regionLoopRange); |
243 |
|
244 |
// loop through tag values in other, adding any which aren't in this, using default value |
245 |
DataMapType::const_iterator pos; |
246 |
for (pos=otherTemp->m_offsetLookup.begin();pos!=otherTemp->m_offsetLookup.end();pos++) { |
247 |
if (!isCurrentTag(pos->first)) { |
248 |
addTag(pos->first); |
249 |
} |
250 |
} |
251 |
|
252 |
// loop through the tag values copying slices from other to this |
253 |
for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) { |
254 |
getDataPointByTag(pos->first).copySliceFrom(otherTemp->getDataPointByTag(pos->first), regionLoopRange); |
255 |
} |
256 |
|
257 |
} |
258 |
|
259 |
int |
260 |
DataTagged::getTagNumber(int dpno) |
261 |
{ |
262 |
// |
263 |
// Get the number of samples and data-points per sample |
264 |
int numSamples = getNumSamples(); |
265 |
int numDataPointsPerSample = getNumDPPSample(); |
266 |
int numDataPoints = numSamples * numDataPointsPerSample; |
267 |
|
268 |
if (numDataPointsPerSample==0) { |
269 |
throw DataException("DataTagged::getTagNumber error: no data-points associated with this object."); |
270 |
} |
271 |
|
272 |
if (dpno<0 || dpno>numDataPoints-1) { |
273 |
throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied."); |
274 |
} |
275 |
|
276 |
// |
277 |
// Determine the sample number which corresponds to this data-point number |
278 |
int sampleNo = dpno / numDataPointsPerSample; |
279 |
|
280 |
// |
281 |
// Determine the tag number which corresponds to this sample number |
282 |
int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo); |
283 |
|
284 |
// |
285 |
// return the tag number |
286 |
return(tagNo); |
287 |
} |
288 |
|
289 |
void |
290 |
DataTagged::setTaggedValues(const TagListType& tagKeys, |
291 |
const ValueListType& values) |
292 |
{ |
293 |
addTaggedValues(tagKeys,values); |
294 |
} |
295 |
|
296 |
void |
297 |
DataTagged::setTaggedValue(int tagKey, |
298 |
const DataTypes::ShapeType& pointshape, |
299 |
const ValueType& value, |
300 |
int dataOffset) |
301 |
{ |
302 |
if (!DataTypes::checkShape(getShape(), pointshape)) { |
303 |
throw DataException(getPointDataView().createShapeErrorMessage( |
304 |
"Error - Cannot setTaggedValue due to shape mismatch.", pointshape)); |
305 |
} |
306 |
DataMapType::iterator pos(m_offsetLookup.find(tagKey)); |
307 |
if (pos==m_offsetLookup.end()) { |
308 |
// tag couldn't be found so use addTaggedValue |
309 |
addTaggedValue(tagKey,pointshape, value, dataOffset); |
310 |
} else { |
311 |
// copy the values into the data array at the offset determined by m_offsetLookup |
312 |
int offset=pos->second; |
313 |
for (int i=0; i<getNoValues(); i++) { |
314 |
m_data[offset+i]=value[i+dataOffset]; |
315 |
} |
316 |
} |
317 |
} |
318 |
|
319 |
|
320 |
|
321 |
void |
322 |
DataTagged::setTaggedValue(int tagKey, |
323 |
const DataArrayView& value) |
324 |
{ |
325 |
if (!getPointDataView().checkShape(value.getShape())) { |
326 |
throw DataException(getPointDataView().createShapeErrorMessage( |
327 |
"Error - Cannot setTaggedValue due to shape mismatch.", value.getShape())); |
328 |
} |
329 |
DataMapType::iterator pos(m_offsetLookup.find(tagKey)); |
330 |
if (pos==m_offsetLookup.end()) { |
331 |
// tag couldn't be found so use addTaggedValue |
332 |
addTaggedValue(tagKey,value); |
333 |
} else { |
334 |
// copy the values into the data array at the offset determined by m_offsetLookup |
335 |
int offset=pos->second; |
336 |
for (int i=0; i<getPointDataView().noValues(); i++) { |
337 |
m_data[offset+i]=value.getData(i); |
338 |
} |
339 |
} |
340 |
} |
341 |
|
342 |
void |
343 |
DataTagged::addTaggedValues(const TagListType& tagKeys, |
344 |
const ValueListType& values) |
345 |
{ |
346 |
if (values.size()==0) { |
347 |
// copy the current default value for each of the tags |
348 |
TagListType::const_iterator iT; |
349 |
for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) { |
350 |
// the point data view for DataTagged points at the default value |
351 |
addTaggedValue(*iT,getPointDataView()); |
352 |
} |
353 |
} else if (values.size()==1 && tagKeys.size()>1) { |
354 |
// assume the one given value will be used for all tag values |
355 |
TagListType::const_iterator iT; |
356 |
for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) { |
357 |
addTaggedValue(*iT,values[0]); |
358 |
} |
359 |
} else { |
360 |
if (tagKeys.size()!=values.size()) { |
361 |
stringstream temp; |
362 |
temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size() |
363 |
<< " doesn't match number of values: " << values.size(); |
364 |
throw DataException(temp.str()); |
365 |
} else { |
366 |
unsigned int i; |
367 |
for (i=0;i<tagKeys.size();i++) { |
368 |
addTaggedValue(tagKeys[i],values[i]); |
369 |
} |
370 |
} |
371 |
} |
372 |
} |
373 |
|
374 |
|
375 |
|
376 |
void |
377 |
DataTagged::addTaggedValue(int tagKey, |
378 |
const DataTypes::ShapeType& pointshape, |
379 |
const ValueType& value, |
380 |
int dataOffset) |
381 |
{ |
382 |
if (!DataTypes::checkShape(getShape(), pointshape)) { |
383 |
throw DataException(getPointDataView().createShapeErrorMessage( |
384 |
"Error - Cannot addTaggedValue due to shape mismatch.", pointshape)); |
385 |
} |
386 |
DataMapType::iterator pos(m_offsetLookup.find(tagKey)); |
387 |
if (pos!=m_offsetLookup.end()) { |
388 |
// tag already exists so use setTaggedValue |
389 |
setTaggedValue(tagKey,pointshape, value, dataOffset); |
390 |
} else { |
391 |
// save the key and the location of its data in the lookup tab |
392 |
m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size())); |
393 |
// add the data given in "value" at the end of m_data |
394 |
// need to make a temp copy of m_data, resize m_data, then copy |
395 |
// all the old values plus the value to be added back into m_data |
396 |
ValueType m_data_temp(m_data); |
397 |
int oldSize=m_data.size(); |
398 |
int newSize=m_data.size()+getNoValues(); |
399 |
m_data.resize(newSize,0.,newSize); |
400 |
for (int i=0;i<oldSize;i++) { |
401 |
m_data[i]=m_data_temp[i]; |
402 |
} |
403 |
for (int i=0;i<getNoValues();i++) { |
404 |
m_data[oldSize+i]=value[i+dataOffset]; |
405 |
} |
406 |
} |
407 |
} |
408 |
|
409 |
|
410 |
|
411 |
|
412 |
void |
413 |
DataTagged::addTaggedValue(int tagKey, |
414 |
const DataArrayView& value) |
415 |
{ |
416 |
if (!getPointDataView().checkShape(value.getShape())) { |
417 |
throw DataException(getPointDataView().createShapeErrorMessage( |
418 |
"Error - Cannot addTaggedValue due to shape mismatch.", value.getShape())); |
419 |
} |
420 |
DataMapType::iterator pos(m_offsetLookup.find(tagKey)); |
421 |
if (pos!=m_offsetLookup.end()) { |
422 |
// tag already exists so use setTaggedValue |
423 |
setTaggedValue(tagKey,value); |
424 |
} else { |
425 |
// save the key and the location of its data in the lookup tab |
426 |
m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size())); |
427 |
// add the data given in "value" at the end of m_data |
428 |
// need to make a temp copy of m_data, resize m_data, then copy |
429 |
// all the old values plus the value to be added back into m_data |
430 |
ValueType m_data_temp(m_data); |
431 |
int oldSize=m_data.size(); |
432 |
int newSize=m_data.size()+value.noValues(); |
433 |
m_data.resize(newSize,0.,newSize); |
434 |
for (int i=0;i<oldSize;i++) { |
435 |
m_data[i]=m_data_temp[i]; |
436 |
} |
437 |
for (int i=0;i<value.noValues();i++) { |
438 |
m_data[oldSize+i]=value.getData(i); |
439 |
} |
440 |
} |
441 |
} |
442 |
|
443 |
|
444 |
void |
445 |
DataTagged::addTag(int tagKey) |
446 |
{ |
447 |
DataMapType::iterator pos(m_offsetLookup.find(tagKey)); |
448 |
if (pos!=m_offsetLookup.end()) { |
449 |
// tag already exists so use setTaggedValue |
450 |
// setTaggedValue(tagKey,value); |
451 |
} else { |
452 |
// save the key and the location of its data in the lookup tab |
453 |
m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size())); |
454 |
// add the data given in "value" at the end of m_data |
455 |
// need to make a temp copy of m_data, resize m_data, then copy |
456 |
// all the old values plus the value to be added back into m_data |
457 |
ValueType m_data_temp(m_data); |
458 |
int oldSize=m_data.size(); |
459 |
int newSize=m_data.size()+getNoValues(); |
460 |
m_data.resize(newSize,0.,newSize); |
461 |
for (int i=0;i<oldSize;i++) { |
462 |
m_data[i]=m_data_temp[i]; |
463 |
} |
464 |
for (int i=0;i<getNoValues();i++) { |
465 |
m_data[oldSize+i]=m_data[m_defaultValueOffset+i]; |
466 |
} |
467 |
} |
468 |
} |
469 |
|
470 |
|
471 |
double* |
472 |
DataTagged::getSampleDataByTag(int tag) |
473 |
{ |
474 |
DataMapType::iterator pos(m_offsetLookup.find(tag)); |
475 |
if (pos==m_offsetLookup.end()) { |
476 |
// tag couldn't be found so return the default value |
477 |
return &(m_data[0]); |
478 |
} else { |
479 |
// return the data-point corresponding to the given tag |
480 |
return &(m_data[pos->second]); |
481 |
} |
482 |
} |
483 |
|
484 |
string |
485 |
DataTagged::toString() const |
486 |
{ |
487 |
stringstream temp; |
488 |
DataMapType::const_iterator i; |
489 |
temp << "Tag(Default)" << endl; |
490 |
temp << getDefaultValue().toString() << endl; |
491 |
// create a temporary view as the offset will be changed |
492 |
DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape()); |
493 |
for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) { |
494 |
temp << "Tag(" << i->first << ")" << endl; |
495 |
tempView.setOffset(i->second); |
496 |
temp << tempView.toString() << endl; |
497 |
} |
498 |
return temp.str(); |
499 |
} |
500 |
|
501 |
DataTypes::ValueType::size_type |
502 |
DataTagged::getPointOffset(int sampleNo, |
503 |
int dataPointNo) const |
504 |
{ |
505 |
int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo); |
506 |
DataMapType::const_iterator pos(m_offsetLookup.find(tagKey)); |
507 |
DataTypes::ValueType::size_type offset=m_defaultValueOffset; |
508 |
if (pos!=m_offsetLookup.end()) { |
509 |
offset=pos->second; |
510 |
} |
511 |
return offset; |
512 |
} |
513 |
|
514 |
DataArrayView |
515 |
DataTagged::getDataPointByTag(int tag) const |
516 |
{ |
517 |
DataMapType::const_iterator pos(m_offsetLookup.find(tag)); |
518 |
DataTypes::ValueType::size_type offset=m_defaultValueOffset; |
519 |
if (pos!=m_offsetLookup.end()) { |
520 |
offset=pos->second; |
521 |
} |
522 |
DataArrayView temp(getPointDataView()); |
523 |
temp.setOffset(offset); |
524 |
return temp; |
525 |
} |
526 |
|
527 |
|
528 |
DataTypes::ValueType::const_reference |
529 |
DataTagged::getDataByTag(int tag, DataTypes::ValueType::size_type i) const |
530 |
{ |
531 |
DataMapType::const_iterator pos(m_offsetLookup.find(tag)); |
532 |
DataTypes::ValueType::size_type offset=m_defaultValueOffset; |
533 |
if (pos!=m_offsetLookup.end()) { |
534 |
offset=pos->second; |
535 |
} |
536 |
DataArrayView temp(getPointDataView()); |
537 |
temp.setOffset(offset); |
538 |
return temp.getData()[offset+i]; |
539 |
} |
540 |
|
541 |
|
542 |
DataTypes::ValueType::reference |
543 |
DataTagged::getDataByTag(int tag, DataTypes::ValueType::size_type i) |
544 |
{ |
545 |
DataMapType::const_iterator pos(m_offsetLookup.find(tag)); |
546 |
DataTypes::ValueType::size_type offset=m_defaultValueOffset; |
547 |
if (pos!=m_offsetLookup.end()) { |
548 |
offset=pos->second; |
549 |
} |
550 |
DataArrayView temp(getPointDataView()); |
551 |
temp.setOffset(offset); |
552 |
return temp.getData()[offset+i]; |
553 |
} |
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
DataArrayView |
561 |
DataTagged::getDataPoint(int sampleNo, |
562 |
int dataPointNo) |
563 |
{ |
564 |
EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo); |
565 |
int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo); |
566 |
return getDataPointByTag(tagKey); |
567 |
} |
568 |
|
569 |
int |
570 |
DataTagged::archiveData(ofstream& archiveFile, |
571 |
const DataTypes::ValueType::size_type noValues) const |
572 |
{ |
573 |
return(m_data.archiveData(archiveFile, noValues)); |
574 |
} |
575 |
|
576 |
int |
577 |
DataTagged::extractData(ifstream& archiveFile, |
578 |
const DataTypes::ValueType::size_type noValues) |
579 |
{ |
580 |
return(m_data.extractData(archiveFile, noValues)); |
581 |
} |
582 |
void |
583 |
DataTagged::symmetric(DataAbstract* ev) |
584 |
{ |
585 |
DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev); |
586 |
if (temp_ev==0) { |
587 |
throw DataException("Error - DataTagged::symmetric casting to DataTagged failed (probably a programming error)."); |
588 |
} |
589 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
590 |
DataTagged::DataMapType::const_iterator i; |
591 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
592 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) { |
593 |
temp_ev->addTag(i->first); |
594 |
DataArrayView thisView=getDataPointByTag(i->first); |
595 |
DataArrayView evView=temp_ev->getDataPointByTag(i->first); |
596 |
DataArrayView::symmetric(thisView,0,evView,0); |
597 |
} |
598 |
DataArrayView::symmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0); |
599 |
} |
600 |
void |
601 |
DataTagged::nonsymmetric(DataAbstract* ev) |
602 |
{ |
603 |
DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev); |
604 |
if (temp_ev==0) { |
605 |
throw DataException("Error - DataTagged::nonsymmetric casting to DataTagged failed (probably a programming error)."); |
606 |
} |
607 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
608 |
DataTagged::DataMapType::const_iterator i; |
609 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
610 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) { |
611 |
temp_ev->addTag(i->first); |
612 |
DataArrayView thisView=getDataPointByTag(i->first); |
613 |
DataArrayView evView=temp_ev->getDataPointByTag(i->first); |
614 |
DataArrayView::nonsymmetric(thisView,0,evView,0); |
615 |
} |
616 |
DataArrayView::nonsymmetric(getDefaultValue(),0,temp_ev->getDefaultValue(),0); |
617 |
} |
618 |
void |
619 |
DataTagged::trace(DataAbstract* ev, int axis_offset) |
620 |
{ |
621 |
DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev); |
622 |
if (temp_ev==0) { |
623 |
throw DataException("Error - DataTagged::trace casting to DataTagged failed (probably a programming error)."); |
624 |
} |
625 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
626 |
DataTagged::DataMapType::const_iterator i; |
627 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
628 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) { |
629 |
temp_ev->addTag(i->first); |
630 |
DataArrayView thisView=getDataPointByTag(i->first); |
631 |
DataArrayView evView=temp_ev->getDataPointByTag(i->first); |
632 |
DataArrayView::trace(thisView,0,evView,0, axis_offset); |
633 |
} |
634 |
DataArrayView::trace(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset); |
635 |
} |
636 |
|
637 |
void |
638 |
DataTagged::transpose(DataAbstract* ev, int axis_offset) |
639 |
{ |
640 |
DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev); |
641 |
if (temp_ev==0) { |
642 |
throw DataException("Error - DataTagged::transpose casting to DataTagged failed (probably a programming error)."); |
643 |
} |
644 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
645 |
DataTagged::DataMapType::const_iterator i; |
646 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
647 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) { |
648 |
temp_ev->addTag(i->first); |
649 |
DataArrayView thisView=getDataPointByTag(i->first); |
650 |
DataArrayView evView=temp_ev->getDataPointByTag(i->first); |
651 |
DataArrayView::transpose(thisView,0,evView,0, axis_offset); |
652 |
} |
653 |
DataArrayView::transpose(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis_offset); |
654 |
} |
655 |
|
656 |
void |
657 |
DataTagged::swapaxes(DataAbstract* ev, int axis0, int axis1) |
658 |
{ |
659 |
DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev); |
660 |
if (temp_ev==0) { |
661 |
throw DataException("Error - DataTagged::swapaxes casting to DataTagged failed (probably a programming error)."); |
662 |
} |
663 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
664 |
DataTagged::DataMapType::const_iterator i; |
665 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
666 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) { |
667 |
temp_ev->addTag(i->first); |
668 |
DataArrayView thisView=getDataPointByTag(i->first); |
669 |
DataArrayView evView=temp_ev->getDataPointByTag(i->first); |
670 |
DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1); |
671 |
} |
672 |
DataArrayView::swapaxes(getDefaultValue(),0,temp_ev->getDefaultValue(),0,axis0,axis1); |
673 |
} |
674 |
|
675 |
void |
676 |
DataTagged::eigenvalues(DataAbstract* ev) |
677 |
{ |
678 |
DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev); |
679 |
if (temp_ev==0) { |
680 |
throw DataException("Error - DataTagged::eigenvalues casting to DataTagged failed (propably a programming error)."); |
681 |
} |
682 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
683 |
DataTagged::DataMapType::const_iterator i; |
684 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
685 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) { |
686 |
temp_ev->addTag(i->first); |
687 |
DataArrayView thisView=getDataPointByTag(i->first); |
688 |
DataArrayView evView=temp_ev->getDataPointByTag(i->first); |
689 |
DataArrayView::eigenvalues(thisView,0,evView,0); |
690 |
} |
691 |
DataArrayView::eigenvalues(getDefaultValue(),0,temp_ev->getDefaultValue(),0); |
692 |
} |
693 |
void |
694 |
DataTagged::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol) |
695 |
{ |
696 |
DataTagged* temp_ev=dynamic_cast<DataTagged*>(ev); |
697 |
if (temp_ev==0) { |
698 |
throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error)."); |
699 |
} |
700 |
DataTagged* temp_V=dynamic_cast<DataTagged*>(V); |
701 |
if (temp_V==0) { |
702 |
throw DataException("Error - DataTagged::eigenvalues_and_eigenvectors casting to DataTagged failed (propably a programming error)."); |
703 |
} |
704 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
705 |
DataTagged::DataMapType::const_iterator i; |
706 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
707 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) { |
708 |
temp_ev->addTag(i->first); |
709 |
temp_V->addTag(i->first); |
710 |
DataArrayView thisView=getDataPointByTag(i->first); |
711 |
DataArrayView evView=temp_ev->getDataPointByTag(i->first); |
712 |
DataArrayView VView=temp_V->getDataPointByTag(i->first); |
713 |
DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol); |
714 |
} |
715 |
DataArrayView::eigenvalues_and_eigenvectors(getDefaultValue(),0, |
716 |
temp_ev->getDefaultValue(),0, |
717 |
temp_V->getDefaultValue(),0, |
718 |
tol); |
719 |
|
720 |
|
721 |
} |
722 |
|
723 |
void |
724 |
DataTagged::setToZero(){ |
725 |
DataTypes::ValueType::size_type n=m_data.size(); |
726 |
for (int i=0; i<n ;++i) m_data[i]=0.; |
727 |
} |
728 |
|
729 |
void |
730 |
DataTagged::dump(const std::string fileName) const |
731 |
{ |
732 |
#ifdef PASO_MPI |
733 |
throw DataException("Error - DataTagged:: dump is not implemented for MPI yet."); |
734 |
#endif |
735 |
#ifdef USE_NETCDF |
736 |
const int ldims=DataTypes::maxRank+1; |
737 |
const NcDim* ncdims[ldims]; |
738 |
NcVar *var, *tags_var; |
739 |
int rank = getPointDataView().getRank(); |
740 |
int type= getFunctionSpace().getTypeCode(); |
741 |
int ndims =0; |
742 |
long dims[ldims]; |
743 |
const double* d_ptr=&(m_data[0]); |
744 |
DataTypes::ShapeType shape = getPointDataView().getShape(); |
745 |
|
746 |
// netCDF error handler |
747 |
NcError err(NcError::verbose_nonfatal); |
748 |
// Create the file. |
749 |
NcFile dataFile(fileName.c_str(), NcFile::Replace); |
750 |
// check if writing was successful |
751 |
if (!dataFile.is_valid()) |
752 |
throw DataException("Error - DataTagged:: opening of netCDF file for output failed."); |
753 |
if (!dataFile.add_att("type_id",1) ) |
754 |
throw DataException("Error - DataTagged:: appending data type to netCDF file failed."); |
755 |
if (!dataFile.add_att("rank",rank) ) |
756 |
throw DataException("Error - DataTagged:: appending rank attribute to netCDF file failed."); |
757 |
if (!dataFile.add_att("function_space_type",type)) |
758 |
throw DataException("Error - DataTagged:: appending function space attribute to netCDF file failed."); |
759 |
ndims=rank+1; |
760 |
if ( rank >0 ) { |
761 |
dims[0]=shape[0]; |
762 |
if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) ) |
763 |
throw DataException("Error - DataTagged:: appending ncdimsion 0 to netCDF file failed."); |
764 |
} |
765 |
if ( rank >1 ) { |
766 |
dims[1]=shape[1]; |
767 |
if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) ) |
768 |
throw DataException("Error - DataTagged:: appending ncdimsion 1 to netCDF file failed."); |
769 |
} |
770 |
if ( rank >2 ) { |
771 |
dims[2]=shape[2]; |
772 |
if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) ) |
773 |
throw DataException("Error - DataTagged:: appending ncdimsion 2 to netCDF file failed."); |
774 |
} |
775 |
if ( rank >3 ) { |
776 |
dims[3]=shape[3]; |
777 |
if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) ) |
778 |
throw DataException("Error - DataTagged:: appending ncdimsion 3 to netCDF file failed."); |
779 |
} |
780 |
const DataTagged::DataMapType& thisLookup=getTagLookup(); |
781 |
DataTagged::DataMapType::const_iterator i; |
782 |
DataTagged::DataMapType::const_iterator thisLookupEnd=thisLookup.end(); |
783 |
int ntags=1; |
784 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) ntags++; |
785 |
int* tags =(int*) esysUtils::malloc(ntags*sizeof(int)); |
786 |
int c=1; |
787 |
tags[0]=-1; |
788 |
for (i=thisLookup.begin();i!=thisLookupEnd;i++) tags[c++]=i->first; |
789 |
dims[rank]=ntags; |
790 |
if (! (ncdims[rank] = dataFile.add_dim("num_tags", dims[rank])) ) |
791 |
{ |
792 |
esysUtils::free(tags); |
793 |
throw DataException("Error - DataTagged:: appending num_tags to netCDF file failed."); |
794 |
} |
795 |
if (! ( tags_var = dataFile.add_var("tags", ncInt, ncdims[rank])) ) |
796 |
{ |
797 |
esysUtils::free(tags); |
798 |
throw DataException("Error - DataTagged:: appending tags to netCDF file failed."); |
799 |
} |
800 |
if (! (tags_var->put(tags,dims[rank])) ) |
801 |
{ |
802 |
esysUtils::free(tags); |
803 |
throw DataException("Error - DataTagged:: copy tags to netCDF buffer failed."); |
804 |
} |
805 |
if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) ) |
806 |
{ |
807 |
esysUtils::free(tags); |
808 |
throw DataException("Error - DataTagged:: appending variable to netCDF file failed."); |
809 |
} |
810 |
if (! (var->put(d_ptr,dims)) ) |
811 |
{ |
812 |
esysUtils::free(tags); |
813 |
throw DataException("Error - DataTagged:: copy data to netCDF buffer failed."); |
814 |
} |
815 |
#else |
816 |
throw DataException("Error - DataTagged:: dump is not configured with netCDF. Please contact your installation manager."); |
817 |
#endif |
818 |
} |
819 |
} // end of namespace |