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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 102 - (hide annotations)
Wed Dec 15 07:08:39 2004 UTC (14 years, 7 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataTagged.cpp
File size: 10290 byte(s)
*** empty log message ***

1 jgs 82 // $Id$
2     /*
3     ******************************************************************************
4     * *
5     * COPYRIGHT ACcESS 2004 - All Rights Reserved *
6     * *
7     * This software is the property of ACcESS. No part of this code *
8     * may be copied in any form or by any means without the expressed written *
9     * consent of ACcESS. Copying, use or modification of this software *
10     * by any unauthorised person is illegal unless that person has a software *
11     * license agreement with ACcESS. *
12     * *
13     ******************************************************************************
14     */
15    
16     #include "escript/Data/DataTagged.h"
17     #include "escript/Data/DataConstant.h"
18    
19     #include <sstream>
20    
21     using namespace std;
22    
23     namespace escript {
24    
25     DataTagged::DataTagged():
26     DataAbstract(FunctionSpace())
27     {
28     //
29     // create a scalar default value
30     m_data.push_back(0.0);
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 jgs 102 const FunctionSpace& what)
39     : DataAbstract(what)
40 jgs 82 {
41     //
42     // The default value is always the first item in the values list
43     m_data.insert(m_data.end(), &defaultValue.getData(0), &defaultValue.getData(defaultValue.noValues()) );
44     DataArrayView temp(m_data,defaultValue.getShape());
45     setPointDataView(temp);
46     // add remaining tags and values
47     addTaggedValues(tagKeys,values);
48     }
49    
50 jgs 102 DataTagged::DataTagged(const DataTagged& other)
51     : DataAbstract(other.getFunctionSpace()),
52 jgs 82 m_data(other.m_data),
53     m_offsetLookup(other.m_offsetLookup)
54     {
55     DataArrayView temp(m_data,other.getPointDataView().getShape());
56     setPointDataView(temp);
57     }
58    
59 jgs 102 DataTagged::DataTagged(const DataConstant& other)
60     : DataAbstract(other.getFunctionSpace())
61 jgs 82 {
62     //
63     // Fill the default value with the constant value item from other
64     const DataArrayView& value=other.getPointDataView();
65     m_data.insert(m_data.end(), &value.getData(0), &value.getData(value.noValues()) );
66     DataArrayView temp(m_data,value.getShape());
67     setPointDataView(temp);
68     }
69    
70     DataTagged::DataTagged(const DataTagged& other,
71 jgs 102 const DataArrayView::RegionType& region)
72     : DataAbstract(other.getFunctionSpace())
73 jgs 82 {
74     //
75     // get the shape of the slice to copy from other
76     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
77     //
78     // allocate enough space for all values
79     m_data.resize(DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1));
80     DataArrayView temp(m_data,shape);
81     setPointDataView(temp);
82     getDefaultValue().copySlice(other.getDefaultValue(),region);
83     //
84     // Loop through the tag values
85     DataMapType::const_iterator pos;
86     DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues();
87     for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();++pos){
88     getPointDataView().copySlice(tagOffset,other.getPointDataView(), pos->second,region);
89     m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset));
90     tagOffset+=getPointDataView().noValues();
91     }
92     }
93    
94     void
95     DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape)
96     {
97     //
98     // can only reshape a rank zero data point
99     if (getPointDataView().getRank()!=0) {
100     stringstream temp;
101     temp << "Error - Can only reshape Data with data points of rank 0. "
102     << "This Data has data points with rank: "
103     << getPointDataView().getRank();
104     throw DataException(temp.str());
105     }
106     //
107     // allocate enough space for all values
108     DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1));
109     DataArrayView newView(newData,shape);
110     newView.copy(0,getDefaultValue()());
111     //
112     // Loop through the tag values
113     DataMapType::iterator pos;
114     DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape);
115     for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){
116     newView.copy(tagOffset,m_data[pos->second]);
117     pos->second=tagOffset;
118     tagOffset+=DataArrayView::noValues(shape);
119     }
120     m_data=newData;
121     DataArrayView temp(m_data,shape);
122     setPointDataView(temp);
123     }
124    
125     DataAbstract*
126     DataTagged::getSlice(const DataArrayView::RegionType& region) const
127     {
128     return new DataTagged(*this,region);
129     }
130    
131     void
132     DataTagged::setSlice(const DataAbstract* value,
133     const DataArrayView::RegionType& region)
134     {
135     const DataTagged* tempDataTag=dynamic_cast<const DataTagged*>(value);
136     if (tempDataTag==0) {
137     throw DataException("Programming error - casting to DataTagged.");
138     }
139     if (getPointDataView().getRank()!=region.size()) {
140     throw DataException("Error - Invalid slice region.");
141     }
142     //
143     // get the shape of the slice
144     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
145     if (!value->getPointDataView().checkShape(shape)) {
146     throw DataException (value->getPointDataView().createShapeErrorMessage(
147     "Error - Couldn't copy slice due to shape mismatch.",shape));
148     }
149 jgs 102 //
150 jgs 82 getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region);
151     //
152     // Loop through the tag values
153     DataMapType::const_iterator pos;
154     for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){
155     getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region);
156     }
157     }
158    
159     void
160     DataTagged::setTaggedValue(int tagKey,
161     const DataArrayView& value)
162     {
163     DataMapType::iterator pos(m_offsetLookup.find(tagKey));
164     if (pos==m_offsetLookup.end()) {
165     //
166     // tag couldn't be found so add as a new tag
167     addTaggedValue(tagKey,value);
168     } else {
169     if (!getPointDataView().checkShape(value.getShape())) {
170     throw DataException(getPointDataView().createShapeErrorMessage(
171     "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
172     }
173     //
174     // copy the values into tagged data storage
175     copy(&value.getData(0), &value.getData(getPointDataView().noValues()), &m_data[pos->second]);
176     }
177     }
178    
179     void
180     DataTagged::setTaggedValues(const TagListType& tagKeys,
181     const ValueListType& values)
182     {
183     for (int i=0;i<tagKeys.size();++i) {
184     setTaggedValue(tagKeys[i],values[i]);
185     }
186     }
187    
188     void
189     DataTagged::addTaggedValue(int tagKey,
190     const DataArrayView& value)
191     {
192     if (!getPointDataView().checkShape(value.getShape())) {
193     throw DataException(getPointDataView().createShapeErrorMessage(
194     "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
195     }
196     //
197     // save the key and the location of its data
198     m_offsetLookup.insert( DataMapType::value_type(tagKey,m_data.size()) );
199     //
200     // insert the data given in value at the end of m_data
201     m_data.insert( m_data.end(), &(value.getData(0)), &(value.getData(value.noValues())) );
202     }
203    
204     void
205     DataTagged::addTaggedValues(const TagListType& tagKeys,
206     const ValueListType& values)
207     {
208     if (values.size()==0) {
209     //
210     // Copy the default value for each of the tags
211     TagListType::const_iterator iT;
212     for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {
213     //
214     // the point data view for DataTagged points at the default value
215     addTaggedValue(*iT,getPointDataView());
216     }
217     } else if (values.size()==1 && tagKeys.size()>1) {
218     //
219     // assume the one value will be used for all tag values
220     // Copy the input data
221     TagListType::const_iterator iT;
222     for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {
223     addTaggedValue(*iT,values[0]);
224     }
225     } else {
226     if (tagKeys.size()!=values.size()) {
227     stringstream temp;
228     temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
229     << " doesn't match the number of values: " << values.size();
230     throw DataException(temp.str());
231     } else {
232     for (int i=0;i<tagKeys.size();++i) {
233     addTaggedValue(tagKeys[i],values[i]);
234     }
235     }
236     }
237     }
238    
239     double*
240     DataTagged::getSampleDataByTag(int tag)
241     {
242     DataMapType::iterator pos(m_offsetLookup.find(tag));
243     if (pos==m_offsetLookup.end()) {
244     //
245     // tag couldn't be found so return the default value
246     return &(m_data[0]);
247     } else {
248     //
249     // return the data-point corresponding to the given tag
250     return &(m_data[pos->second]);
251     }
252     }
253    
254     string
255     DataTagged::toString() const
256     {
257     stringstream temp;
258     DataMapType::const_iterator i;
259     temp << "Tag(Default)" << endl;
260     temp << getDefaultValue().toString() << endl;
261     //
262     // create a temporary view as the offset will be changed
263     DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
264     for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
265     temp << "Tag(" << i->first << ")" << endl;
266     tempView.setOffset(i->second);
267     temp << tempView.toString() << endl;
268     }
269     return temp.str();
270     }
271    
272     DataArrayView
273     DataTagged::getDataPointByTag(int tag) const
274     {
275     DataMapType::const_iterator pos(m_offsetLookup.find(tag));
276     DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
277     if (pos!=m_offsetLookup.end()) {
278     offset=pos->second;
279     }
280     DataArrayView temp(getPointDataView());
281     temp.setOffset(offset);
282     return temp;
283     }
284    
285     DataArrayView::ValueType::size_type
286     DataTagged::getPointOffset(int sampleNo,
287     int dataPointNo) const
288     {
289     int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
290     DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
291     DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
292     if (pos!=m_offsetLookup.end()) {
293     offset=pos->second;
294     }
295     return offset;
296     }
297    
298     DataArrayView
299     DataTagged::getDataPoint(int sampleNo,
300     int dataPointNo)
301     {
302     EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo);
303     int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
304     return getDataPointByTag(tagKey);
305     }
306    
307     const DataTagged::DataMapType&
308     DataTagged::getTagLookup() const
309     {
310     return m_offsetLookup;
311     }
312    
313     DataArrayView::ValueType::size_type
314     DataTagged::getLength() const
315     {
316     return m_data.size();
317     }
318    
319     } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26