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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 615 - (hide annotations)
Wed Mar 22 02:12:00 2006 UTC (13 years, 4 months ago) by elspeth
File size: 15806 byte(s)
More copyright information.

1 jgs 82 // $Id$
2 jgs 121
3 jgs 82 /*
4 elspeth 615 ************************************************************
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 jgs 82 */
14    
15 jgs 474 #include "DataTagged.h"
16 jgs 496
17 jgs 474 #include "DataConstant.h"
18     #include "DataException.h"
19 jgs 82
20     using namespace std;
21    
22     namespace escript {
23    
24 jgs 121 DataTagged::DataTagged()
25     : DataAbstract(FunctionSpace())
26 jgs 82 {
27 jgs 509 // default constructor
28    
29 jgs 82 // create a scalar default value
30 jgs 151 m_data.resize(1,0.,1);
31 jgs 82 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 jgs 102 const FunctionSpace& what)
39     : DataAbstract(what)
40 jgs 82 {
41 jgs 509 // constructor
42    
43 jgs 121 // initialise the array of data values
44     // the default value is always the first item in the values list
45 jgs 151 int len = defaultValue.noValues();
46     m_data.resize(len,0.,len);
47 jgs 121 for (int i=0; i<defaultValue.noValues(); i++) {
48     m_data[i]=defaultValue.getData(i);
49     }
50    
51 jgs 119 // create the data view
52 jgs 82 DataArrayView temp(m_data,defaultValue.getShape());
53     setPointDataView(temp);
54 jgs 121
55 jgs 82 // add remaining tags and values
56     addTaggedValues(tagKeys,values);
57     }
58    
59 jgs 119 DataTagged::DataTagged(const FunctionSpace& what,
60     const DataArrayView::ShapeType &shape,
61     const int tags[],
62 jgs 500 const ValueType& data)
63 jgs 119 : DataAbstract(what)
64     {
65 jgs 509 // alternative constructor
66     // not unit_tested tested yet
67    
68 jgs 121 // copy the data
69 jgs 119 m_data=data;
70 jgs 121
71 jgs 119 // create the view of the data
72     DataArrayView tempView(m_data,shape);
73     setPointDataView(tempView);
74 jgs 121
75 jgs 119 // 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 jgs 102 DataTagged::DataTagged(const DataTagged& other)
82     : DataAbstract(other.getFunctionSpace()),
83 jgs 82 m_data(other.m_data),
84     m_offsetLookup(other.m_offsetLookup)
85     {
86 jgs 509 // copy constructor
87    
88 jgs 119 // create the data view
89 jgs 82 DataArrayView temp(m_data,other.getPointDataView().getShape());
90     setPointDataView(temp);
91     }
92    
93 jgs 102 DataTagged::DataTagged(const DataConstant& other)
94     : DataAbstract(other.getFunctionSpace())
95 jgs 82 {
96 jgs 509 // copy constructor
97    
98 jgs 121 // fill the default value with the constant value item from "other"
99 jgs 82 const DataArrayView& value=other.getPointDataView();
100 jgs 151 int len = value.noValues();
101     m_data.resize(len,0.,len);
102 jgs 121 for (int i=0; i<value.noValues(); i++) {
103     m_data[i]=value.getData(i);
104     }
105    
106 jgs 119 // create the data view
107 jgs 82 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 jgs 121
123 jgs 82 // 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 jgs 121
128     // loop through the tag values
129 jgs 82 DataMapType::iterator pos;
130     DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
131 jgs 121 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){
132 jgs 82 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 jgs 513 return new DataTagged(*this, region);
145 jgs 82 }
146    
147 jgs 513 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 jgs 82 void
180 jgs 513 DataTagged::setSlice(const DataAbstract* other,
181     const DataArrayView::RegionType& region)
182 jgs 82 {
183 jgs 513
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 jgs 82 throw DataException("Programming error - casting to DataTagged.");
189     }
190 jgs 121
191 jgs 513 // 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 jgs 82 if (getPointDataView().getRank()!=region.size()) {
199     throw DataException("Error - Invalid slice region.");
200     }
201 jgs 519 if (otherTemp->getPointDataView().getRank()>0 and !other->getPointDataView().checkShape(regionShape)) {
202 jgs 513 throw DataException (other->getPointDataView().createShapeErrorMessage(
203     "Error - Couldn't copy slice due to shape mismatch.",regionShape));
204 jgs 82 }
205 jgs 121
206 jgs 513 // copy slice from other default value to this default value
207     getDefaultValue().copySliceFrom(otherTemp->getDefaultValue(), regionLoopRange);
208 jgs 121
209 jgs 541 // 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 jgs 513 // loop through the tag values copying slices from other to this
218 jgs 121 for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) {
219 jgs 513 getDataPointByTag(pos->first).copySliceFrom(otherTemp->getDataPointByTag(pos->first), regionLoopRange);
220 jgs 121 }
221 jgs 513
222 jgs 82 }
223    
224 jgs 149 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 jgs 496 if (dpno<0 || dpno>numDataPoints-1) {
238 jgs 149 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 jgs 82 void
255 jgs 509 DataTagged::setTaggedValues(const TagListType& tagKeys,
256     const ValueListType& values)
257     {
258     addTaggedValues(tagKeys,values);
259     }
260    
261     void
262 jgs 82 DataTagged::setTaggedValue(int tagKey,
263     const DataArrayView& value)
264     {
265 jgs 121 if (!getPointDataView().checkShape(value.getShape())) {
266     throw DataException(getPointDataView().createShapeErrorMessage(
267     "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
268     }
269 jgs 82 DataMapType::iterator pos(m_offsetLookup.find(tagKey));
270     if (pos==m_offsetLookup.end()) {
271 jgs 121 // tag couldn't be found so use addTaggedValue
272 jgs 82 addTaggedValue(tagKey,value);
273     } else {
274 jgs 121 // 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 jgs 82 }
279     }
280     }
281    
282     void
283 jgs 509 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 jgs 82 DataTagged::addTaggedValue(int tagKey,
315     const DataArrayView& value)
316     {
317     if (!getPointDataView().checkShape(value.getShape())) {
318     throw DataException(getPointDataView().createShapeErrorMessage(
319 jgs 121 "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
320 jgs 82 }
321 jgs 121 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 jgs 151 m_data.resize(newSize,0.,newSize);
335 jgs 121 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 jgs 82 }
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 jgs 496 DataArrayView::ValueType::size_type
375     DataTagged::getPointOffset(int sampleNo,
376     int dataPointNo) const
377 jgs 82 {
378 jgs 496 int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
379     DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
380 jgs 82 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
381     if (pos!=m_offsetLookup.end()) {
382     offset=pos->second;
383     }
384 jgs 496 return offset;
385 jgs 82 }
386    
387 jgs 496 DataArrayView
388     DataTagged::getDataPointByTag(int tag) const
389 jgs 82 {
390 jgs 496 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
391 jgs 82 DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
392     if (pos!=m_offsetLookup.end()) {
393     offset=pos->second;
394     }
395 jgs 496 DataArrayView temp(getPointDataView());
396     temp.setOffset(offset);
397     return temp;
398 jgs 82 }
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 jgs 123 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 gross 580 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 jgs 123
467 gross 580
468     }
469    
470    
471 jgs 82 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26