1 |
// $Id$ |
2 |
|
3 |
/* |
4 |
****************************************************************************** |
5 |
* * |
6 |
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
7 |
* * |
8 |
* This software is the property of ACcESS. No part of this code * |
9 |
* may be copied in any form or by any means without the expressed written * |
10 |
* consent of ACcESS. Copying, use or modification of this software * |
11 |
* by any unauthorised person is illegal unless that person has a software * |
12 |
* license agreement with ACcESS. * |
13 |
* * |
14 |
****************************************************************************** |
15 |
*/ |
16 |
|
17 |
#include "escript/Data/DataTagged.h" |
18 |
#include "escript/Data/DataConstant.h" |
19 |
#include "escript/Data/DataExpanded.h" |
20 |
#include "escript/Data/DataException.h" |
21 |
|
22 |
#include <sstream> |
23 |
|
24 |
using namespace std; |
25 |
|
26 |
namespace escript { |
27 |
|
28 |
DataTagged::DataTagged() |
29 |
: DataAbstract(FunctionSpace()) |
30 |
{ |
31 |
// create a scalar default value |
32 |
m_data.resize(1,0.,1); |
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 |
// 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 |
// copy the data |
66 |
m_data=data; |
67 |
|
68 |
// create the view of the data |
69 |
DataArrayView tempView(m_data,shape); |
70 |
setPointDataView(tempView); |
71 |
|
72 |
// create the tag lookup map |
73 |
for (int sampleNo=0; sampleNo<getNumSamples(); sampleNo++) { |
74 |
m_offsetLookup.insert(DataMapType::value_type(sampleNo,tags[sampleNo])); |
75 |
} |
76 |
} |
77 |
|
78 |
DataTagged::DataTagged(const DataTagged& other) |
79 |
: DataAbstract(other.getFunctionSpace()), |
80 |
m_data(other.m_data), |
81 |
m_offsetLookup(other.m_offsetLookup) |
82 |
{ |
83 |
// create the data view |
84 |
DataArrayView temp(m_data,other.getPointDataView().getShape()); |
85 |
setPointDataView(temp); |
86 |
} |
87 |
|
88 |
DataTagged::DataTagged(const DataConstant& other) |
89 |
: DataAbstract(other.getFunctionSpace()) |
90 |
{ |
91 |
// fill the default value with the constant value item from "other" |
92 |
const DataArrayView& value=other.getPointDataView(); |
93 |
int len = value.noValues(); |
94 |
m_data.resize(len,0.,len); |
95 |
for (int i=0; i<value.noValues(); i++) { |
96 |
m_data[i]=value.getData(i); |
97 |
} |
98 |
|
99 |
// create the data view |
100 |
DataArrayView temp(m_data,value.getShape()); |
101 |
setPointDataView(temp); |
102 |
} |
103 |
|
104 |
DataTagged::DataTagged(const DataTagged& other, |
105 |
const DataArrayView::RegionType& region) |
106 |
: DataAbstract(other.getFunctionSpace()) |
107 |
{ |
108 |
// get the shape of the slice to copy from other |
109 |
DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region)); |
110 |
DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region); |
111 |
|
112 |
// allocate enough space for all values |
113 |
int len = DataArrayView::noValues(shape)*(other.m_offsetLookup.size()+1); |
114 |
m_data.resize(len,0.,len); |
115 |
|
116 |
// create the data view |
117 |
DataArrayView temp(m_data,shape); |
118 |
setPointDataView(temp); |
119 |
|
120 |
// copy the default value |
121 |
getDefaultValue().copySlice(other.getDefaultValue(),region_loop_range); |
122 |
|
123 |
// loop through the tag values copying these |
124 |
DataMapType::const_iterator pos; |
125 |
DataArrayView::ValueType::size_type tagOffset=getPointDataView().noValues(); |
126 |
for (pos=other.m_offsetLookup.begin();pos!=other.m_offsetLookup.end();pos++){ |
127 |
getPointDataView().copySlice(tagOffset,other.getPointDataView(),pos->second,region_loop_range); |
128 |
m_offsetLookup.insert(DataMapType::value_type(pos->first,tagOffset)); |
129 |
tagOffset+=getPointDataView().noValues(); |
130 |
} |
131 |
} |
132 |
|
133 |
void |
134 |
DataTagged::reshapeDataPoint(const DataArrayView::ShapeType& shape) |
135 |
{ |
136 |
// can only reshape a rank zero data point |
137 |
if (getPointDataView().getRank()!=0) { |
138 |
stringstream temp; |
139 |
temp << "Error - Can only reshape Data with data points of rank 0. " |
140 |
<< "This Data has data points with rank: " |
141 |
<< getPointDataView().getRank(); |
142 |
throw DataException(temp.str()); |
143 |
} |
144 |
|
145 |
// allocate enough space for all values |
146 |
DataArrayView::ValueType newData(DataArrayView::noValues(shape)*(m_offsetLookup.size()+1)); |
147 |
DataArrayView newView(newData,shape); |
148 |
newView.copy(0,getDefaultValue()()); |
149 |
|
150 |
// loop through the tag values |
151 |
DataMapType::iterator pos; |
152 |
DataArrayView::ValueType::size_type tagOffset=DataArrayView::noValues(shape); |
153 |
for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++){ |
154 |
newView.copy(tagOffset,m_data[pos->second]); |
155 |
pos->second=tagOffset; |
156 |
tagOffset+=DataArrayView::noValues(shape); |
157 |
} |
158 |
m_data=newData; |
159 |
DataArrayView temp(m_data,shape); |
160 |
setPointDataView(temp); |
161 |
} |
162 |
|
163 |
DataAbstract* |
164 |
DataTagged::getSlice(const DataArrayView::RegionType& region) const |
165 |
{ |
166 |
return new DataTagged(*this,region); |
167 |
} |
168 |
|
169 |
void |
170 |
DataTagged::setSlice(const DataAbstract* value, |
171 |
const DataArrayView::RegionType& region) |
172 |
{ |
173 |
const DataTagged* tempDataTag=dynamic_cast<const DataTagged*>(value); |
174 |
if (tempDataTag==0) { |
175 |
throw DataException("Programming error - casting to DataTagged."); |
176 |
} |
177 |
|
178 |
DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region)); |
179 |
DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region); |
180 |
if (getPointDataView().getRank()!=region.size()) { |
181 |
throw DataException("Error - Invalid slice region."); |
182 |
} |
183 |
if (tempDataTag->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) { |
184 |
throw DataException (value->getPointDataView().createShapeErrorMessage( |
185 |
"Error - Couldn't copy slice due to shape mismatch.",shape)); |
186 |
} |
187 |
|
188 |
getDefaultValue().copySliceFrom(tempDataTag->getDefaultValue(),region_loop_range); |
189 |
|
190 |
// loop through the tag values |
191 |
DataMapType::const_iterator pos; |
192 |
for (pos=m_offsetLookup.begin();pos!=m_offsetLookup.end();pos++) { |
193 |
getDataPointByTag(pos->first).copySliceFrom(tempDataTag->getDataPointByTag(pos->first),region_loop_range); |
194 |
} |
195 |
} |
196 |
|
197 |
int |
198 |
DataTagged::getTagNumber(int dpno) |
199 |
{ |
200 |
// |
201 |
// Get the number of samples and data-points per sample |
202 |
int numSamples = getNumSamples(); |
203 |
int numDataPointsPerSample = getNumDPPSample(); |
204 |
int numDataPoints = numSamples * numDataPointsPerSample; |
205 |
|
206 |
if (numDataPointsPerSample==0) { |
207 |
throw DataException("DataTagged::getTagNumber error: no data-points associated with this object."); |
208 |
} |
209 |
|
210 |
if (dpno<0 || dpno>numDataPoints) { |
211 |
throw DataException("DataTagged::getTagNumber error: invalid data-point number supplied."); |
212 |
} |
213 |
|
214 |
// |
215 |
// Determine the sample number which corresponds to this data-point number |
216 |
int sampleNo = dpno / numDataPointsPerSample; |
217 |
|
218 |
// |
219 |
// Determine the tag number which corresponds to this sample number |
220 |
int tagNo = getFunctionSpace().getTagFromSampleNo(sampleNo); |
221 |
|
222 |
// |
223 |
// return the tag number |
224 |
return(tagNo); |
225 |
} |
226 |
|
227 |
void |
228 |
DataTagged::setTaggedValue(int tagKey, |
229 |
const DataArrayView& value) |
230 |
{ |
231 |
if (!getPointDataView().checkShape(value.getShape())) { |
232 |
throw DataException(getPointDataView().createShapeErrorMessage( |
233 |
"Error - Cannot setTaggedValue due to shape mismatch.", value.getShape())); |
234 |
} |
235 |
DataMapType::iterator pos(m_offsetLookup.find(tagKey)); |
236 |
if (pos==m_offsetLookup.end()) { |
237 |
// tag couldn't be found so use addTaggedValue |
238 |
addTaggedValue(tagKey,value); |
239 |
} else { |
240 |
// copy the values into the data array at the offset determined by m_offsetLookup |
241 |
int offset=pos->second; |
242 |
for (int i=0; i<getPointDataView().noValues(); i++) { |
243 |
m_data[offset+i]=value.getData(i); |
244 |
} |
245 |
} |
246 |
} |
247 |
|
248 |
void |
249 |
DataTagged::addTaggedValue(int tagKey, |
250 |
const DataArrayView& value) |
251 |
{ |
252 |
if (!getPointDataView().checkShape(value.getShape())) { |
253 |
throw DataException(getPointDataView().createShapeErrorMessage( |
254 |
"Error - Cannot addTaggedValue due to shape mismatch.", value.getShape())); |
255 |
} |
256 |
DataMapType::iterator pos(m_offsetLookup.find(tagKey)); |
257 |
if (pos!=m_offsetLookup.end()) { |
258 |
// tag already exists so use setTaggedValue |
259 |
setTaggedValue(tagKey,value); |
260 |
} else { |
261 |
// save the key and the location of its data in the lookup tab |
262 |
m_offsetLookup.insert(DataMapType::value_type(tagKey,m_data.size())); |
263 |
// add the data given in "value" at the end of m_data |
264 |
// need to make a temp copy of m_data, resize m_data, then copy |
265 |
// all the old values plus the value to be added back into m_data |
266 |
ValueType m_data_temp(m_data); |
267 |
int oldSize=m_data.size(); |
268 |
int newSize=m_data.size()+value.noValues(); |
269 |
m_data.resize(newSize,0.,newSize); |
270 |
for (int i=0;i<oldSize;i++) { |
271 |
m_data[i]=m_data_temp[i]; |
272 |
} |
273 |
for (int i=0;i<value.noValues();i++) { |
274 |
m_data[oldSize+i]=value.getData(i); |
275 |
} |
276 |
} |
277 |
} |
278 |
|
279 |
void |
280 |
DataTagged::setTaggedValues(const TagListType& tagKeys, |
281 |
const ValueListType& values) |
282 |
{ |
283 |
addTaggedValues(tagKeys,values); |
284 |
} |
285 |
|
286 |
void |
287 |
DataTagged::addTaggedValues(const TagListType& tagKeys, |
288 |
const ValueListType& values) |
289 |
{ |
290 |
if (values.size()==0) { |
291 |
// copy the default value for each of the tags |
292 |
TagListType::const_iterator iT; |
293 |
for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) { |
294 |
// the point data view for DataTagged points at the default value |
295 |
addTaggedValue(*iT,getPointDataView()); |
296 |
} |
297 |
} else if (values.size()==1 && tagKeys.size()>1) { |
298 |
// assume the one value will be used for all tag values |
299 |
TagListType::const_iterator iT; |
300 |
for (iT=tagKeys.begin();iT!=tagKeys.end();iT++) { |
301 |
addTaggedValue(*iT,values[0]); |
302 |
} |
303 |
} else { |
304 |
if (tagKeys.size()!=values.size()) { |
305 |
stringstream temp; |
306 |
temp << "Error - (addTaggedValue) Number of tags: " << tagKeys.size() |
307 |
<< " doesn't match the number of values: " << values.size(); |
308 |
throw DataException(temp.str()); |
309 |
} else { |
310 |
for (int i=0;i<tagKeys.size();i++) { |
311 |
addTaggedValue(tagKeys[i],values[i]); |
312 |
} |
313 |
} |
314 |
} |
315 |
} |
316 |
|
317 |
double* |
318 |
DataTagged::getSampleDataByTag(int tag) |
319 |
{ |
320 |
DataMapType::iterator pos(m_offsetLookup.find(tag)); |
321 |
if (pos==m_offsetLookup.end()) { |
322 |
// tag couldn't be found so return the default value |
323 |
return &(m_data[0]); |
324 |
} else { |
325 |
// return the data-point corresponding to the given tag |
326 |
return &(m_data[pos->second]); |
327 |
} |
328 |
} |
329 |
|
330 |
string |
331 |
DataTagged::toString() const |
332 |
{ |
333 |
stringstream temp; |
334 |
DataMapType::const_iterator i; |
335 |
temp << "Tag(Default)" << endl; |
336 |
temp << getDefaultValue().toString() << endl; |
337 |
// create a temporary view as the offset will be changed |
338 |
DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape()); |
339 |
for (i=m_offsetLookup.begin();i!=m_offsetLookup.end();++i) { |
340 |
temp << "Tag(" << i->first << ")" << endl; |
341 |
tempView.setOffset(i->second); |
342 |
temp << tempView.toString() << endl; |
343 |
} |
344 |
return temp.str(); |
345 |
} |
346 |
|
347 |
DataArrayView |
348 |
DataTagged::getDataPointByTag(int tag) const |
349 |
{ |
350 |
DataMapType::const_iterator pos(m_offsetLookup.find(tag)); |
351 |
DataArrayView::ValueType::size_type offset=m_defaultValueOffset; |
352 |
if (pos!=m_offsetLookup.end()) { |
353 |
offset=pos->second; |
354 |
} |
355 |
DataArrayView temp(getPointDataView()); |
356 |
temp.setOffset(offset); |
357 |
return temp; |
358 |
} |
359 |
|
360 |
DataArrayView::ValueType::size_type |
361 |
DataTagged::getPointOffset(int sampleNo, |
362 |
int dataPointNo) const |
363 |
{ |
364 |
int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo); |
365 |
DataMapType::const_iterator pos(m_offsetLookup.find(tagKey)); |
366 |
DataArrayView::ValueType::size_type offset=m_defaultValueOffset; |
367 |
if (pos!=m_offsetLookup.end()) { |
368 |
offset=pos->second; |
369 |
} |
370 |
return offset; |
371 |
} |
372 |
|
373 |
DataArrayView |
374 |
DataTagged::getDataPoint(int sampleNo, |
375 |
int dataPointNo) |
376 |
{ |
377 |
EsysAssert(validSampleNo(sampleNo),"(getDataPoint) Invalid sampleNo: " << sampleNo); |
378 |
int tagKey=getFunctionSpace().getTagFromSampleNo(sampleNo); |
379 |
return getDataPointByTag(tagKey); |
380 |
} |
381 |
|
382 |
const DataTagged::DataMapType& |
383 |
DataTagged::getTagLookup() const |
384 |
{ |
385 |
return m_offsetLookup; |
386 |
} |
387 |
|
388 |
DataArrayView::ValueType::size_type |
389 |
DataTagged::getLength() const |
390 |
{ |
391 |
return m_data.size(); |
392 |
} |
393 |
|
394 |
int |
395 |
DataTagged::archiveData(ofstream& archiveFile, |
396 |
const DataArrayView::ValueType::size_type noValues) const |
397 |
{ |
398 |
return(m_data.archiveData(archiveFile, noValues)); |
399 |
} |
400 |
|
401 |
int |
402 |
DataTagged::extractData(ifstream& archiveFile, |
403 |
const DataArrayView::ValueType::size_type noValues) |
404 |
{ |
405 |
return(m_data.extractData(archiveFile, noValues)); |
406 |
} |
407 |
|
408 |
} // end of namespace |