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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 615 - (show annotations)
Wed Mar 22 02:12:00 2006 UTC (13 years, 8 months ago) by elspeth
File MIME type: text/plain
File size: 12670 byte(s)
More copyright information.

1 // $Id$
2
3 /*
4 ************************************************************
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 */
14
15 #if !defined escript_DataTagged_20040615_H
16 #define escript_DataTagged_20040615_H
17
18 #include "DataAbstract.h"
19 #include "DataArrayView.h"
20
21 #include <vector>
22 #include <map>
23
24 namespace escript {
25
26 class DataConstant;
27
28 /**
29 \brief
30 Simulates a full dataset accessible via sampleNo and dataPointNo.
31
32 Description:
33 Each data-point has an associated tag number, and a given tag can represent a
34 range of dataPointNo and sampleNo. Each tag indexes only a single data-point.
35 Thus only a single data-point needs to be stored for a range of sampleNo and
36 dataPointNo values.
37 */
38
39 class DataTagged : public DataAbstract {
40
41 public:
42
43 //
44 // Types for the lists of tags and values.
45 typedef std::vector<int> TagListType;
46 typedef std::vector<DataArrayView> ValueListType;
47 typedef DataArrayView::ValueType ValueType;
48
49 //
50 // Map from a tag to an offset into the data array.
51 typedef std::map<int, int> DataMapType;
52
53 /**
54 \brief
55 Default constructor for DataTagged.
56
57 Description:
58 Default constructor for DataTagged. Creates a DataTagged object for which
59 the default data-point is a scalar data-point with value 0.0, and no other
60 tag values are stored.
61 T
62 */
63 DataTagged();
64
65 /**
66 \brief
67 Constructor for DataTagged.
68
69 Description:
70 Constructor for DataTagged.
71 \param tagKeys - Input - A vector of integer tags.
72 \param values - Input - A vector of DataArrayViews. If this is empty
73 all tag values will be assigned a scalar data-point of value
74 0. If it contains one value all tag values will be assigned
75 this value. Otherwise consecutive tags will be assigned
76 consecutive values. If there is a mismatch between the
77 number of keys and the number of values an exception
78 will be generated.
79 \param defaultValue - Input - Value returned if a requested tag doesn't exist.
80 \param what - Input - A description of what this data represents.
81 T
82 */
83 DataTagged(const TagListType& tagKeys,
84 const ValueListType& values,
85 const DataArrayView& defaultValue,
86 const FunctionSpace& what);
87
88 /**
89 \brief
90 Alternative Constructor for DataTagged.
91
92 Description:
93 Alternative Constructor for DataTagged.
94 \param what - Input - A description of what this data object represents.
95 \param shape - Input - The shape of each data-point.
96 \param tags - Input - An array of tags, one for each sample number.
97 \param data - The data values for each tag.
98 NB: no unit testing yet
99 */
100 DataTagged(const FunctionSpace& what,
101 const DataArrayView::ShapeType &shape,
102 const int tags[],
103 const ValueType& data);
104
105 /**
106 \brief
107 Copy Constructor for DataTagged.
108 Performs a deep copy from the given DataTagged object.
109 T
110 */
111 DataTagged(const DataTagged& other);
112
113 /**
114 \brief
115 Copy Constructor for DataTagged.
116 Construct a DataTagged object from a DataConstant object.
117 The default value will be the value of the DataConstant object.
118 T
119 */
120 DataTagged(const DataConstant& other);
121
122 /**
123 \brief
124 getSampleDataByTag
125
126 Description:
127 Return the data-point for the given tag. All of the data for the
128 sample will be visible via the returned pointer.
129
130 ** This provides an interface into the data suitable for legacy C code.
131 ** NB: need to do array bounds checking when accessing returned value!
132 T
133 */
134 virtual
135 double*
136 getSampleDataByTag(int tag);
137
138 /**
139 \brief
140 Write the data as a string.
141 Writes out each tag, including the default, and the data-point which is
142 associated with each tag.
143 T
144 */
145 virtual
146 std::string
147 toString() const;
148
149 /**
150 \brief
151 Return the tag number associated with the given data-point number
152 according to the associated function space.
153 T
154 */
155 virtual
156 int
157 getTagNumber(int dpno);
158
159 /**
160 \brief
161 getPointOffset
162
163 Description:
164 Return the offset to the given data-point value in the underlying
165 data vector.
166
167 \param sampleNo - Input - sample number.
168 \param dataPointNo - Input - data-point number.
169 T
170 */
171 virtual
172 ValueType::size_type
173 getPointOffset(int sampleNo,
174 int dataPointNo) const;
175
176 /**
177 \brief
178 addTaggedValues
179
180 Description:
181 Add the given tags and values to this DataTagged object.
182 \param tagKeys - Input - A vector of integer tags.
183 \param values - Input - A vector of DataArrayViews. If this is empty
184 all tag values will be assigned a scalar data-point of value
185 0. If it contains one value all tag values will be assigned
186 this value. Otherwise consecutive tags will be assigned
187 consecutive values. If there is a mismatch between the
188 number of keys and the number of values an exception
189 will be generated.
190 T
191 */
192 void
193 addTaggedValues(const TagListType& tagKeys,
194 const ValueListType& values);
195
196 /**
197 \brief
198 addTaggedValue
199
200 Description:
201 Add a single tag and value to this DataTagged object. If this tag already has
202 a value associated with it, setTaggedValue will be used to update this value.
203 \param tagKey - Input - Integer tag.
204 \param value - Input - Single DataArrayView value to be assigned to the tag.
205 T
206 */
207 void
208 addTaggedValue(int tagKey,
209 const DataArrayView& value);
210
211 /**
212 \brief
213 setTaggedValues
214
215 Description:
216 Set the given tags to the given values in this DataTagged object.
217 \param tagKeys - Input - A vector of integer tag.
218 \param values - Input - A vector of DataArrayViews. If this is empty
219 all tag values will be assigned a scalar data-point of value
220 0. If it contains one value all tag values will be assigned
221 this value. Otherwise consecutive tags will be assigned
222 consecutive values. If there is a mismatch between the
223 number of keys and the number of values an exception
224 will be generated.
225 T
226 */
227 void
228 setTaggedValues(const TagListType& tagKeys,
229 const ValueListType& values);
230
231 /**
232 \brief
233 setTaggedValue
234
235 Description:
236 Assign the given value to the given tag.
237 \param tagKey - Input - Integer tag.
238 \param value - Input - Single DataArrayView value to be assigned to the tag.
239 T
240 */
241 virtual
242 void
243 setTaggedValue(int tagKey,
244 const DataArrayView& value);
245
246 /**
247 \brief
248 getDataPointByTag
249
250 Description:
251 Return data-point associated with the given tag as a DataArrayView.
252 \param tag - Input - Integer key.
253 T
254 */
255 DataArrayView
256 getDataPointByTag(int tag) const;
257
258 /**
259 \brief
260 getDataPoint
261
262 Description:
263 Return the data-point specified by the given sample and data-point
264 numbers as a DataArrayView.
265 \param sampleNo - Input.
266 \param dataPointNo - Input.
267 T
268 */
269 virtual
270 DataArrayView
271 getDataPoint(int sampleNo,
272 int dataPointNo);
273
274 /**
275 \brief
276 getTagLookup
277
278 Description:
279 Return a reference to the tag offset lookup table.
280 T
281 */
282 const DataMapType&
283 getTagLookup() const;
284
285 /**
286 \brief
287 isCurrentTag
288
289 Description:
290 Return true if the given tag exists within the DataTagged tag map.
291
292 *** NB: The DataTagged tag map does not necessarily coincide with the tag
293 keys in the associated function space.
294 T
295 */
296 bool
297 isCurrentTag(int tag) const;
298
299 /**
300 \brief
301 getDefaultValue
302
303 Description:
304 Return the default value. This value is associated with any tag which
305 is not explicitly recorded in this DataTagged object's tag map.
306 T
307 */
308 DataArrayView&
309 getDefaultValue();
310
311 const DataArrayView&
312 getDefaultValue() const;
313
314 /**
315 \brief
316 getLength
317
318 Description:
319 Return the total number of doubles stored for this DataTagged object.
320 T
321 */
322 virtual
323 ValueType::size_type
324 getLength() const;
325
326 /**
327 \brief
328 getSlice
329
330 Description:
331 Factory method that returns a newly created DataTagged object generated
332 by taking the specified slice from this DataTagged object.
333 The caller is reponsible for managing the returned object.
334 T
335 */
336 virtual
337 DataAbstract*
338 getSlice(const DataArrayView::RegionType& region) const;
339
340 /**
341 \brief
342 Slice Constructor for DataTagged.
343
344 Description:
345 Creates a DataTagged object which is the specified slice
346 from the given DataTagged object.
347 \param other - Input - DataTagged object to slice from.
348 \param region - Input - Region to slice.
349 T
350 */
351 DataTagged(const DataTagged& other,
352 const DataArrayView::RegionType& region);
353
354 /**
355 \brief
356 setSlice
357
358 Description:
359 Copy the given Data object into the specified region in this object.
360 \param other - Input - Data object to copy from.
361 \param region - Input - Region to copy into (NB: must have same shape as other!).
362 T
363 */
364 virtual
365 void
366 setSlice(const DataAbstract* other,
367 const DataArrayView::RegionType& region);
368
369 /**
370 \brief
371 reshapeDataPoint
372
373 Description:
374 Reshape each data-point in this object to the given shape, only
375 if current data-points are scalars. An exception is thrown if
376 the current data-points have rank other than 0.
377 The original values of the data-points are used for all values
378 of the new data-points.
379 T
380 */
381 void
382 reshapeDataPoint(const DataArrayView::ShapeType& shape);
383
384 /**
385 \brief
386 Archive the underlying data values to the file referenced
387 by ofstream. A count of the number of values expected to be written
388 is provided as a cross-check.
389
390 The return value indicates success (0) or otherwise (1).
391 */
392 int
393 archiveData(std::ofstream& archiveFile,
394 const DataArrayView::ValueType::size_type noValues) const;
395
396 /**
397 \brief
398 Extract the number of values specified by noValues from the file
399 referenced by ifstream to the underlying data structure.
400
401 The return value indicates success (0) or otherwise (1).
402 */
403 int
404 extractData(std::ifstream& archiveFile,
405 const DataArrayView::ValueType::size_type noValues);
406
407 /**
408 \brief
409 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev
410
411 \param ev - Output - eigenvalues in increasing order at each data point
412
413 */
414 virtual void
415 eigenvalues(DataAbstract* ev);
416
417 /**
418 \brief
419 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev and eigenvectors V
420
421 \param ev - Output - eigenvalues in increasing order at each data point
422 \param V - Output - corresponding eigenvectors. They are normalized such that their length is one
423 and the first nonzero component is positive.
424 \param tol - Input - eigenvalue with relative distance tol are treated as equal.
425
426 */
427
428 virtual void
429 eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol=1.e-13);
430
431
432 protected:
433
434 private:
435
436 //
437 // The offset lookup table
438 DataMapType m_offsetLookup;
439
440 //
441 // the offset to the default value
442 static const int m_defaultValueOffset = 0;
443
444 //
445 // The actual data
446 ValueType m_data;
447
448 };
449
450 inline
451 bool
452 DataTagged::isCurrentTag(int tag) const
453 {
454 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
455 return (pos!=m_offsetLookup.end());
456 }
457
458 inline
459 DataArrayView&
460 DataTagged::getDefaultValue()
461 {
462 // The default value is always the first value.
463 return getPointDataView();
464 }
465
466 inline
467 const DataArrayView&
468 DataTagged::getDefaultValue() const
469 {
470 // The default value is always the first value.
471 return getPointDataView();
472 }
473
474 inline
475 const DataTagged::DataMapType&
476 DataTagged::getTagLookup() const
477 {
478 return m_offsetLookup;
479 }
480
481 inline
482 DataArrayView::ValueType::size_type
483 DataTagged::getLength() const
484 {
485 return m_data.size();
486 }
487
488 } // end of namespace
489
490 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26