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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 82 - (hide annotations)
Tue Oct 26 06:53:54 2004 UTC (15 years ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataTagged.cpp
File size: 10281 byte(s)
Initial revision

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     const FunctionSpace& what):
39     DataAbstract(what)
40     {
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     DataTagged::DataTagged(const DataTagged& other):
51     DataAbstract(other.getFunctionSpace()),
52     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     DataTagged::DataTagged(const DataConstant& other):
60     DataAbstract(other.getFunctionSpace())
61     {
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     const DataArrayView::RegionType& region):
72     DataAbstract(other.getFunctionSpace())
73     {
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     getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region);
150     //
151     // Loop through the tag values
152     DataMapType::const_iterator pos;
153     for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();++pos){
154     getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region);
155     }
156     }
157    
158     void
159     DataTagged::setTaggedValue(int tagKey,
160     const DataArrayView& value)
161     {
162     DataMapType::iterator pos(m_offsetLookup.find(tagKey));
163     if (pos==m_offsetLookup.end()) {
164     //
165     // tag couldn't be found so add as a new tag
166     addTaggedValue(tagKey,value);
167     } else {
168     if (!getPointDataView().checkShape(value.getShape())) {
169     throw DataException(getPointDataView().createShapeErrorMessage(
170     "Error - Cannot setTaggedValue due to shape mismatch.", value.getShape()));
171     }
172     //
173     // copy the values into tagged data storage
174     copy(&value.getData(0), &value.getData(getPointDataView().noValues()), &m_data[pos->second]);
175     }
176     }
177    
178     void
179     DataTagged::setTaggedValues(const TagListType& tagKeys,
180     const ValueListType& values)
181     {
182     for (int i=0;i<tagKeys.size();++i) {
183     setTaggedValue(tagKeys[i],values[i]);
184     }
185     }
186    
187     void
188     DataTagged::addTaggedValue(int tagKey,
189     const DataArrayView& value)
190     {
191     if (!getPointDataView().checkShape(value.getShape())) {
192     throw DataException(getPointDataView().createShapeErrorMessage(
193     "Error - Cannot addTaggedValue due to shape mismatch.", value.getShape()));
194     }
195     //
196     // save the key and the location of its data
197     m_offsetLookup.insert( DataMapType::value_type(tagKey,m_data.size()) );
198     //
199     // insert the data given in value at the end of m_data
200     m_data.insert( m_data.end(), &(value.getData(0)), &(value.getData(value.noValues())) );
201     }
202    
203     void
204     DataTagged::addTaggedValues(const TagListType& tagKeys,
205     const ValueListType& values)
206     {
207     if (values.size()==0) {
208     //
209     // Copy the default value for each of the tags
210     TagListType::const_iterator iT;
211     for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {
212     //
213     // the point data view for DataTagged points at the default value
214     addTaggedValue(*iT,getPointDataView());
215     }
216     } else if (values.size()==1 && tagKeys.size()>1) {
217     //
218     // assume the one value will be used for all tag values
219     // Copy the input data
220     TagListType::const_iterator iT;
221     for (iT=tagKeys.begin();iT!=tagKeys.end();++iT) {
222     addTaggedValue(*iT,values[0]);
223     }
224     } else {
225     if (tagKeys.size()!=values.size()) {
226     stringstream temp;
227     temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size()
228     << " doesn't match the number of values: " << values.size();
229     throw DataException(temp.str());
230     } else {
231     for (int i=0;i<tagKeys.size();++i) {
232     addTaggedValue(tagKeys[i],values[i]);
233     }
234     }
235     }
236     }
237    
238     double*
239     DataTagged::getSampleDataByTag(int tag)
240     {
241     DataMapType::iterator pos(m_offsetLookup.find(tag));
242     if (pos==m_offsetLookup.end()) {
243     //
244     // tag couldn't be found so return the default value
245     return &(m_data[0]);
246     } else {
247     //
248     // return the data-point corresponding to the given tag
249     return &(m_data[pos->second]);
250     }
251     }
252    
253     string
254     DataTagged::toString() const
255     {
256     stringstream temp;
257     DataMapType::const_iterator i;
258     temp << "Tag(Default)" << endl;
259     temp << getDefaultValue().toString() << endl;
260     //
261     // create a temporary view as the offset will be changed
262     DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape());
263     for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) {
264     temp << "Tag(" << i->first << ")" << endl;
265     tempView.setOffset(i->second);
266     temp << tempView.toString() << endl;
267     }
268     return temp.str();
269     }
270    
271     DataArrayView
272     DataTagged::getDataPointByTag(int tag) const
273     {
274     DataMapType::const_iterator pos(m_offsetLookup.find(tag));
275     DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
276     if (pos!=m_offsetLookup.end()) {
277     offset=pos->second;
278     }
279     DataArrayView temp(getPointDataView());
280     temp.setOffset(offset);
281     return temp;
282     }
283    
284     DataArrayView::ValueType::size_type
285     DataTagged::getPointOffset(int sampleNo,
286     int dataPointNo) const
287     {
288     int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
289     DataMapType::const_iterator pos(m_offsetLookup.find(tagKey));
290     DataArrayView::ValueType::size_type offset=m_defaultValueOffset;
291     if (pos!=m_offsetLookup.end()) {
292     offset=pos->second;
293     }
294     return offset;
295     }
296    
297     DataArrayView
298     DataTagged::getDataPoint(int sampleNo,
299     int dataPointNo)
300     {
301     EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo);
302     int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo);
303     return getDataPointByTag(tagKey);
304     }
305    
306     const DataTagged::DataMapType&
307     DataTagged::getTagLookup() const
308     {
309     return m_offsetLookup;
310     }
311    
312     DataArrayView::ValueType::size_type
313     DataTagged::getLength() const
314     {
315     return m_data.size();
316     }
317    
318     } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26