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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 615 - (show annotations)
Wed Mar 22 02:12:00 2006 UTC (13 years, 8 months ago) by elspeth
Original Path: trunk/escript/src/DataTagged.cpp
File size: 15806 byte(s)
More copyright information.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26