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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2766 - (show annotations)
Mon Nov 23 05:10:53 2009 UTC (10 years ago) by jfenwick
File MIME type: text/plain
File size: 16353 byte(s)
inf, sup and Lsup now correctly handle +-infinity.
They also will return NaN if any part of their input is NaN.

This will break unit tests since it exposes the hidden bug (#447 in mantis)

This code relies on the ability to test for NaNs.
To do this it makes use of macros and functions from C99.
If you do not have a C99 compiler, then you will probably get the old behaviour.
That is, you won't know when you have NaNs.


Also did minor tweak to saveDataCSV doco.
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
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 #include "system_dep.h"
18
19 #include "DataReady.h"
20 #include "DataTypes.h"
21
22 #include <vector>
23 #include <map>
24
25 namespace escript {
26
27 class DataConstant;
28
29 /**
30 \brief
31 Simulates a full dataset accessible via sampleNo and dataPointNo.
32
33 Description:
34 Each data-point has an associated tag number, and a given tag can represent a
35 range of dataPointNo and sampleNo. Each tag indexes only a single data-point.
36 Thus only a single data-point needs to be stored for a range of sampleNo and
37 dataPointNo values.
38 */
39
40 class DataTagged : public DataReady {
41 typedef DataReady parent;
42 public:
43
44 //
45 // Types for the lists of tags and values.
46 typedef std::vector<int> TagListType;
47 // typedef std::vector<DataArrayView> ValueListType;
48 typedef DataTypes::ValueType ValueType;
49 typedef std::vector<ValueType::ElementType> ValueBatchType;
50
51 //
52 // Map from a tag to an offset into the data array.
53 typedef std::map<int, int> DataMapType;
54
55 /**
56 \brief
57 Default constructor for DataTagged.
58
59 Description:
60 Default constructor for DataTagged. Creates a DataTagged object for which
61 the default data-point is a scalar data-point with value 0.0, and no other
62 tag values are stored.
63 T
64 */
65 ESCRIPT_DLL_API
66 DataTagged();
67
68 /**
69 \brief
70 Alternative Constructor for DataTagged.
71
72 Description:
73 Alternative Constructor for DataTagged.
74 \param what - Input - A description of what this data object represents.
75 \param shape - Input - The shape of each data-point.
76 \param tags - Input - An array of tags, one for each sample number (starts at tag[1]).
77 \param data - The data values for each tag.
78 NB: no unit testing yet
79 */
80 ESCRIPT_DLL_API
81 DataTagged(const FunctionSpace& what,
82 const DataTypes::ShapeType &shape,
83 const int tags[],
84 const ValueType& data);
85
86 /**
87 \brief
88 Alternative Constructor for DataTagged.
89
90 Description:
91 Alternative Constructor for DataTagged.
92 \param what - Input - A description of what this data object represents.
93 \param shape - Input - The shape of each data-point.
94 \param tags - Input - An vector of tags, one for each sample number.
95 \param data - The data values for each tag.
96 TODO Make sure to document the relationship between tags and data, ie: data also contains the default value
97 */
98 ESCRIPT_DLL_API
99 DataTagged(const FunctionSpace& what,
100 const DataTypes::ShapeType &shape,
101 const TagListType& tags,
102 const ValueType& data);
103
104 /**
105 \brief
106 Copy Constructor for DataTagged.
107 Performs a deep copy from the given DataTagged object.
108 T
109 */
110 ESCRIPT_DLL_API
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 ESCRIPT_DLL_API
121 DataTagged(const DataConstant& other);
122
123 /**
124 \brief
125 Copies the tags from a DataTagged into a new Data Tagged and assigns them the default value. ** Not unit tested **
126
127 This is different from a deep copy because we are not copying shape or other information, just tags.
128 \param what - Input - FunctionSpace for the new DataTagged
129 \param shape - Input - Shape for points in the new DataTagged
130 \param defaultvalue - Input - Default value for new DataTagged
131 \param tagsource - Input - A DataTagged object which supplies the tags.
132 */
133 ESCRIPT_DLL_API
134 DataTagged(const FunctionSpace& what,
135 const DataTypes::ShapeType& shape,
136 const DataTypes::ValueType& defaultvalue,
137 const DataTagged* tagsource=0);
138
139 /**
140 \brief
141 Destructor
142 */
143 ESCRIPT_DLL_API
144 inline virtual
145 ~DataTagged() {};
146
147 ESCRIPT_DLL_API
148 bool
149 isTagged() const
150 {
151 return true;
152 };
153
154 /**
155 \brief Return true if any one of the datapoints contains a NaN.
156 */
157 ESCRIPT_DLL_API
158 bool
159 hasNaN() const;
160
161 /**
162 \brief Return a deep copy of the current object.
163 */
164 ESCRIPT_DLL_API
165 virtual
166 DataAbstract*
167 deepCopy();
168
169
170 /**
171 \brief
172 getSampleDataByTag
173
174 Description:
175 Return the data-point for the given tag. All of the data for the
176 sample will be visible via the returned pointer.
177
178 ** This provides an interface into the data suitable for legacy C code.
179 ** NB: need to do array bounds checking when accessing returned value!
180 T
181 */
182 ESCRIPT_DLL_API
183 virtual
184 double*
185 getSampleDataByTag(int tag);
186
187 /**
188 \brief
189 Write the data as a string.
190 Writes out each tag, including the default, and the data-point which is
191 associated with each tag.
192 T
193 */
194 ESCRIPT_DLL_API
195 virtual
196 std::string
197 toString() const;
198 /**
199 \brief
200 dumps the object into a netCDF file
201 */
202 ESCRIPT_DLL_API
203 virtual
204 void
205 dump(const std::string fileName) const;
206
207 /**
208 \brief invert square matricies
209 \param out - Where to store the results
210 */
211 ESCRIPT_DLL_API
212 virtual void
213 matrixInverse(DataAbstract* out) const;
214
215 /**
216 \brief
217 sets all values to zero
218 */
219 ESCRIPT_DLL_API
220 virtual
221 void
222 setToZero();
223
224 /**
225 \brief
226 Return the tag number associated with the given data-point number
227 according to the associated function space.
228 T
229 */
230 ESCRIPT_DLL_API
231 virtual
232 int
233 getTagNumber(int dpno);
234
235 /**
236 \brief
237 getPointOffset
238
239 Description:
240 Return the offset to the given data-point value in the underlying
241 data vector.
242
243 \param sampleNo - Input - sample number.
244 \param dataPointNo - Input - data-point number.
245 T
246 */
247 ESCRIPT_DLL_API
248 virtual
249 ValueType::size_type
250 getPointOffset(int sampleNo,
251 int dataPointNo) const;
252
253 ESCRIPT_DLL_API
254 virtual
255 ValueType::size_type
256 getPointOffset(int sampleNo,
257 int dataPointNo);
258
259 /**
260 \brief
261 addTaggedValues
262
263 Description:
264 Add the given tags and values to this DataTagged object.
265 \param tagKeys - Input - A vector of integer tags.
266 \param values - Input - A vector of doubles. If this is empty, the default value for
267 this DataTagged will be used for all tags.
268 If it contains one value all tag values will be assigned
269 this value. Otherwise consecutive tags will be assigned
270 consecutive values. If there is a mismatch between the
271 number of keys and the number of values an exception
272 will be generated.
273 \param vShape - shape of the datapoints in "values"
274 T
275 */
276 ESCRIPT_DLL_API
277 void
278 addTaggedValues(const TagListType& tagKeys,
279 const ValueBatchType& values,
280 const ShapeType& vShape);
281
282
283 /**
284 Description:
285 Add the given tags and values to this DataTagged object.
286 \param tagKeys - Input - A vector of integer tags.
287 \param values - Input - A DataVector containing the datapoints.
288 If this is empty, the default value for
289 this DataTagged will be used for all tags.
290 If it contains one value all tag values will be assigned
291 this value. Otherwise consecutive tags will be assigned
292 consecutive values. If there is a mismatch between the
293 number of keys and the number of values an exception
294 will be generated.
295 \param vShape - shape of the datapoints in "values"
296
297 TODO Makesure this is properly unit tested
298 */
299 ESCRIPT_DLL_API
300 void
301 addTaggedValues(const TagListType& tagKeys,
302 const ValueType& values,
303 const ShapeType& vShape);
304
305
306
307
308 /**
309 \brief
310 addTaggedValue
311
312 Description:
313 Add a single tag and value to this DataTagged object. If this tag already has
314 a value associated with it, setTaggedValue will be used to update this value.
315 \param tagKey - Input - Integer tag.
316 \param pointshape - Shape of the value parameter
317 \param value - Input - Single DataArrayView value to be assigned to the tag.
318 \param dataOffset - Input - Offset of the beginning of the point in the value parameter
319 */
320 ESCRIPT_DLL_API
321 void
322 addTaggedValue(int tagKey,
323 const DataTypes::ShapeType& pointshape,
324 const ValueType& value,
325 int dataOffset=0);
326
327 /**
328 \brief
329 addTag - does not modify the default value for this object. ** Not unit tested **
330
331 Description:
332 Add a single tag. The default value for this DataTagged will be associated with the tag.
333 If this tag already has a value associated with it, then no change will be made.
334 \param tagKey - Input - Integer tag.
335 TODO: Make sure this is unit tested
336 */
337 ESCRIPT_DLL_API
338 void
339 addTag(int tagKey);
340
341 /**
342 \brief
343 setTaggedValue
344
345 Description:
346 Assign the given value to the given tag.
347 \param tagKey - Input - Integer tag.
348 \param pointshape - the shape of the value parameter
349 \param value - Input - Vector storing the datapoint to be assigned to the tag.
350 \param dataOffset - beginning of the datapoint within "value".
351 T
352 */
353 ESCRIPT_DLL_API
354 void
355 setTaggedValue(int tagKey,
356 const DataTypes::ShapeType& pointshape,
357 const ValueType& value,
358 int dataOffset=0);
359
360 /**
361 \brief
362 getDataByTag
363
364 Return a pointer to the beginning of the datapoint with the specified tag.
365 TODO Eventually these should be inlined.
366 \param tag - Input - Integer key.
367 \param i - position in the underlying datastructure
368 */
369
370 ESCRIPT_DLL_API
371 DataTypes::ValueType::reference
372 getDataByTagRW(int tag, DataTypes::ValueType::size_type i);
373
374 ESCRIPT_DLL_API
375 DataTypes::ValueType::const_reference
376 getDataByTagRO(int tag, DataTypes::ValueType::size_type i) const;
377
378
379
380 /**
381 \brief
382 getOffsetForTag
383
384 \param tag
385 \return the offset of the beginning of the datapoint corresponding to tag.
386
387 Note: If the tag is not valid, the offset of the default value is returned instead.
388 */
389 ESCRIPT_DLL_API
390 DataTypes::ValueType::size_type
391 getOffsetForTag(int tag) const;
392
393
394 /**
395 \brief
396 Return a reference to the underlying DataVector.
397 */
398
399 ESCRIPT_DLL_API
400 DataTypes::ValueType&
401 getVectorRW();
402
403 ESCRIPT_DLL_API
404 const DataTypes::ValueType&
405 getVectorRO() const;
406
407
408
409 /**
410 \brief
411 getTagLookup
412
413 Description:
414 Return a reference to the tag offset lookup table.
415 T
416 */
417 ESCRIPT_DLL_API
418 const DataMapType&
419 getTagLookup() const;
420
421 /**
422 \brief
423 isCurrentTag
424
425 Description:
426 Return true if the given tag exists within the DataTagged tag map.
427
428 *** NB: The DataTagged tag map does not necessarily coincide with the tag
429 keys in the associated function space.
430 T
431 */
432 ESCRIPT_DLL_API
433 bool
434 isCurrentTag(int tag) const;
435
436 /**
437 \brief
438 getDefaultValue
439
440 Description:
441 Return the default value. This value is associated with any tag which
442 is not explicitly recorded in this DataTagged object's tag map.
443 \param i - position in the underlying datastructure
444 */
445 ESCRIPT_DLL_API
446 DataTypes::ValueType::reference
447 getDefaultValueRW(DataTypes::ValueType::size_type i);
448
449 ESCRIPT_DLL_API
450 DataTypes::ValueType::const_reference
451 getDefaultValueRO(DataTypes::ValueType::size_type i) const;
452
453
454
455
456
457 /**
458 \brief
459 getLength
460
461 Description:
462 Return the total number of doubles stored for this DataTagged object.
463 T
464 */
465 ESCRIPT_DLL_API
466 virtual
467 ValueType::size_type
468 getLength() const;
469
470 /**
471 \brief
472 getSlice
473
474 Description:
475 Factory method that returns a newly created DataTagged object generated
476 by taking the specified slice from this DataTagged object.
477 The caller is reponsible for managing the returned object.
478 T
479 */
480 ESCRIPT_DLL_API
481 virtual
482 DataAbstract*
483 getSlice(const DataTypes::RegionType& region) const;
484
485 /**
486 \brief
487 Slice Constructor for DataTagged.
488
489 Description:
490 Creates a DataTagged object which is the specified slice
491 from the given DataTagged object.
492 \param other - Input - DataTagged object to slice from.
493 \param region - Input - Region to slice.
494 T
495 */
496 ESCRIPT_DLL_API
497 DataTagged(const DataTagged& other,
498 const DataTypes::RegionType& region);
499
500 /**
501 \brief
502 setSlice
503
504 Description:
505 Copy the given Data object into the specified region in this object.
506 \param other - Input - Data object to copy from.
507 \param region - Input - Region to copy into (NB: must have same shape as other!).
508 T
509 */
510 ESCRIPT_DLL_API
511 virtual
512 void
513 setSlice(const DataAbstract* other,
514 const DataTypes::RegionType& region);
515
516
517 /**
518 \brief
519 Computes a symmetric matrix (A + AT) / 2
520
521 \param ev - Output - symmetric matrix
522
523 */
524 ESCRIPT_DLL_API
525 virtual void
526 symmetric(DataAbstract* ev);
527
528 /**
529 \brief
530 Computes a nonsymmetric matrix (A - AT) / 2
531
532 \param ev - Output - nonsymmetric matrix
533
534 */
535 ESCRIPT_DLL_API
536 virtual void
537 nonsymmetric(DataAbstract* ev);
538
539 /**
540 \brief
541 Computes the trace of a matrix
542
543 \param ev - Output - the trace of a matrix
544 \param axis_offset
545 */
546 ESCRIPT_DLL_API
547 virtual void
548 trace(DataAbstract* ev, int axis_offset);
549
550 /**
551 \brief
552 swaps components axis0 and axis1
553
554 \param ev - Output - swapped components
555 \param axis0
556 \param axis1
557 */
558 ESCRIPT_DLL_API
559 virtual void
560 swapaxes(DataAbstract* ev, int axis0, int axis1);
561
562 /**
563 \brief
564 Transpose each data point of this Data object around the given axis.
565
566 \param ev - Output - the transpose of a matrix
567 \param axis_offset
568 */
569 ESCRIPT_DLL_API
570 virtual void
571 transpose(DataAbstract* ev, int axis_offset);
572
573 /**
574 \brief
575 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev
576
577 \param ev - Output - eigenvalues in increasing order at each data point
578
579 */
580 ESCRIPT_DLL_API
581 virtual void
582 eigenvalues(DataAbstract* ev);
583
584 /**
585 \brief
586 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev and eigenvectors V
587
588 \param ev - Output - eigenvalues in increasing order at each data point
589 \param V - Output - corresponding eigenvectors. They are normalized such that their length is one
590 and the first nonzero component is positive.
591 \param tol - Input - eigenvalue with relative distance tol are treated as equal.
592
593 */
594
595 ESCRIPT_DLL_API
596 virtual void
597 eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol=1.e-13);
598
599
600 /**
601 \brief Returns the offset in the structure which stores the default value
602 */
603 ESCRIPT_DLL_API
604 DataTypes::ValueType::size_type
605 getDefaultOffset() const;
606
607 protected:
608
609 private:
610
611 //
612 // The offset lookup table
613 DataMapType m_offsetLookup;
614
615 //
616 // the offset to the default value
617 static const int m_defaultValueOffset = 0;
618
619 //
620 // The actual data
621 ValueType m_data;
622
623 };
624
625 inline
626 bool
627 DataTagged::isCurrentTag(int tag) const
628 {
629 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
630 return (pos!=m_offsetLookup.end());
631 }
632
633 inline
634 DataTypes::ValueType::size_type
635 DataTagged::getDefaultOffset() const
636 {
637 return m_defaultValueOffset;
638 }
639
640 inline
641 DataTypes::ValueType::reference
642 DataTagged::getDefaultValueRW(DataTypes::ValueType::size_type i)
643 {
644 return getVectorRW()[i]; // getVectorRW has exclusive write checks
645 }
646
647 inline
648 DataTypes::ValueType::const_reference
649 DataTagged::getDefaultValueRO(DataTypes::ValueType::size_type i) const
650 {
651 return getVectorRO()[i];
652 }
653
654 inline
655 const DataTagged::DataMapType&
656 DataTagged::getTagLookup() const
657 {
658 return m_offsetLookup;
659 }
660
661 inline
662 DataTypes::ValueType::size_type
663 DataTagged::getLength() const
664 {
665 return m_data.size();
666 }
667
668 } // end of namespace
669
670 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26