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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (show annotations)
Thu Jan 27 06:21:59 2005 UTC (14 years, 8 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataTagged.cpp
File size: 10604 byte(s)
*** empty log message ***

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26