/[escript]/branches/schroedinger/escript/src/DataTagged.h
ViewVC logotype

Contents of /branches/schroedinger/escript/src/DataTagged.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1935 - (show annotations)
Mon Oct 27 06:06:39 2008 UTC (10 years, 5 months ago) by jfenwick
File MIME type: text/plain
File size: 21234 byte(s)
Branch commit
More cleanup of DataTestCase - still don't have all the LazyTests in 
there yet.
Added tests to make sure the resolve() operation does what it is 
supposed to.
Added non-constant versions of getPointOffset to DataAbstract 
classes.
Fixed a bug in deepCopy on DataLazy.
Changed setToZero to not check the type of the data.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 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 // Constructor for DataTagged.
71 //
72 // Description:
73 // Constructor for DataTagged.
74 // \param tagKeys - Input - A vector of integer tags.
75 // \param values - Input - A vector of DataArrayViews. If this is empty
76 // all tag values will be assigned a scalar data-point of value
77 // 0. If it contains one value all tag values will be assigned
78 // this value. Otherwise consecutive tags will be assigned
79 // consecutive values. If there is a mismatch between the
80 // number of keys and the number of values an exception
81 // will be generated.
82 // \param defaultValue - Input - Value returned if a requested tag doesn't exist.
83 // \param what - Input - A description of what this data represents.
84 // T
85 // */
86 // ESCRIPT_DLL_API
87 // DataTagged(const TagListType& tagKeys,
88 // const ValueListType& values,
89 // const DataArrayView& defaultValue,
90 // const FunctionSpace& what);
91
92 /**
93 \brief
94 Alternative Constructor for DataTagged.
95
96 Description:
97 Alternative Constructor for DataTagged.
98 \param what - Input - A description of what this data object represents.
99 \param shape - Input - The shape of each data-point.
100 \param tags - Input - An array of tags, one for each sample number (starts at tag[1]).
101 \param data - The data values for each tag.
102 NB: no unit testing yet
103 */
104 ESCRIPT_DLL_API
105 DataTagged(const FunctionSpace& what,
106 const DataTypes::ShapeType &shape,
107 const int tags[],
108 const ValueType& data);
109
110 /**
111 \brief
112 Alternative Constructor for DataTagged.
113
114 Description:
115 Alternative Constructor for DataTagged.
116 \param what - Input - A description of what this data object represents.
117 \param shape - Input - The shape of each data-point.
118 \param tags - Input - An vector of tags, one for each sample number.
119 \param data - The data values for each tag.
120 TODO Make sure to document the relationship between tags and data, ie: data also contains the default value
121 */
122 ESCRIPT_DLL_API
123 DataTagged(const FunctionSpace& what,
124 const DataTypes::ShapeType &shape,
125 const TagListType& tags,
126 const ValueType& data);
127
128 /**
129 \brief
130 Copy Constructor for DataTagged.
131 Performs a deep copy from the given DataTagged object.
132 T
133 */
134 ESCRIPT_DLL_API
135 DataTagged(const DataTagged& other);
136
137 /**
138 \brief
139 Copy Constructor for DataTagged.
140 Construct a DataTagged object from a DataConstant object.
141 The default value will be the value of the DataConstant object.
142 T
143 */
144 ESCRIPT_DLL_API
145 DataTagged(const DataConstant& other);
146
147 /**
148 \brief
149 Copies the tags from a DataTagged into a new Data Tagged and assigns them the default value. ** Not unit tested **
150
151 This is different from a deep copy because we are not copying shape or other information, just tags.
152 \param what - Input - FunctionSpace for the new DataTagged
153 \param shape - Input - Shape for points in the new DataTagged
154 \param defaultvalue - Input - Default value for new DataTagged
155 \param tagsource - Input - A DataTagged object which supplies the tags.
156 */
157 ESCRIPT_DLL_API
158 DataTagged(const FunctionSpace& what,
159 const DataTypes::ShapeType& shape,
160 const DataTypes::ValueType& defaultvalue,
161 const DataTagged* tagsource=0);
162
163 /**
164 \brief
165 Destructor
166 */
167 ESCRIPT_DLL_API
168 inline virtual
169 ~DataTagged() {};
170
171 ESCRIPT_DLL_API
172 bool
173 isTagged() const
174 {
175 return true;
176 };
177
178
179 /**
180 \brief Return a deep copy of the current object.
181 */
182 ESCRIPT_DLL_API
183 virtual
184 DataAbstract*
185 deepCopy();
186
187
188 /**
189 \brief
190 getSampleDataByTag
191
192 Description:
193 Return the data-point for the given tag. All of the data for the
194 sample will be visible via the returned pointer.
195
196 ** This provides an interface into the data suitable for legacy C code.
197 ** NB: need to do array bounds checking when accessing returned value!
198 T
199 */
200 ESCRIPT_DLL_API
201 virtual
202 double*
203 getSampleDataByTag(int tag);
204
205 /**
206 \brief
207 Write the data as a string.
208 Writes out each tag, including the default, and the data-point which is
209 associated with each tag.
210 T
211 */
212 ESCRIPT_DLL_API
213 virtual
214 std::string
215 toString() const;
216 /**
217 \brief
218 dumps the object into a netCDF file
219 */
220 ESCRIPT_DLL_API
221 virtual
222 void
223 dump(const std::string fileName) const;
224
225 /**
226 \brief
227 sets all values to zero
228 */
229 ESCRIPT_DLL_API
230 virtual
231 void
232 setToZero();
233
234 /**
235 \brief
236 Return the tag number associated with the given data-point number
237 according to the associated function space.
238 T
239 */
240 ESCRIPT_DLL_API
241 virtual
242 int
243 getTagNumber(int dpno);
244
245 /**
246 \brief
247 getPointOffset
248
249 Description:
250 Return the offset to the given data-point value in the underlying
251 data vector.
252
253 \param sampleNo - Input - sample number.
254 \param dataPointNo - Input - data-point number.
255 T
256 */
257 ESCRIPT_DLL_API
258 virtual
259 ValueType::size_type
260 getPointOffset(int sampleNo,
261 int dataPointNo) const;
262
263 ESCRIPT_DLL_API
264 virtual
265 ValueType::size_type
266 getPointOffset(int sampleNo,
267 int dataPointNo);
268
269
270
271 // /**
272 // \brief
273 // addTaggedValues
274 //
275 // Description:
276 // Add the given tags and values to this DataTagged object.
277 // \param tagKeys - Input - A vector of integer tags.
278 // \param values - Input - A vector of DataArrayViews. If this is empty
279 // all tag values will be assigned a scalar data-point of value
280 // 0. If it contains one value all tag values will be assigned
281 // this value. Otherwise consecutive tags will be assigned
282 // consecutive values. If there is a mismatch between the
283 // number of keys and the number of values an exception
284 // will be generated.
285 // T
286 // */
287 // ESCRIPT_DLL_API
288 // void
289 // addTaggedValues(const TagListType& tagKeys,
290 // const ValueListType& values);
291
292 // /**
293 // \brief
294 // addTaggedValue
295 //
296 // Description:
297 // Add a single tag and value to this DataTagged object. If this tag already has
298 // a value associated with it, setTaggedValue will be used to update this value.
299 // \param tagKey - Input - Integer tag.
300 // \param value - Input - Single DataArrayView value to be assigned to the tag.
301 // T
302 // */
303 // ESCRIPT_DLL_API
304 // void
305 // addTaggedValue(int tagKey,
306 // const DataArrayView& value);
307
308
309
310 /**
311 \brief
312 addTaggedValues
313
314 Description:
315 Add the given tags and values to this DataTagged object.
316 \param tagKeys - Input - A vector of integer tags.
317 \param values - Input - A vector of doubles. If this is empty, the default value for
318 this DataTagged will be used for all tags.
319 If it contains one value all tag values will be assigned
320 this value. Otherwise consecutive tags will be assigned
321 consecutive values. If there is a mismatch between the
322 number of keys and the number of values an exception
323 will be generated.
324 \param vShape - shape of the datapoints in "values"
325 T
326 */
327 ESCRIPT_DLL_API
328 void
329 addTaggedValues(const TagListType& tagKeys,
330 const ValueBatchType& values,
331 const ShapeType& vShape);
332
333
334 /**
335 Description:
336 Add the given tags and values to this DataTagged object.
337 \param tagKeys - Input - A vector of integer tags.
338 \param values - Input - A DataVector containing the datapoints.
339 If this is empty, the default value for
340 this DataTagged will be used for all tags.
341 If it contains one value all tag values will be assigned
342 this value. Otherwise consecutive tags will be assigned
343 consecutive values. If there is a mismatch between the
344 number of keys and the number of values an exception
345 will be generated.
346 \param vShape - shape of the datapoints in "values"
347
348 TODO Makesure this is properly unit tested
349 */
350 ESCRIPT_DLL_API
351 void
352 addTaggedValues(const TagListType& tagKeys,
353 const ValueType& values,
354 const ShapeType& vShape);
355
356
357
358
359 /**
360 \brief
361 addTaggedValue
362
363 Description:
364 Add a single tag and value to this DataTagged object. If this tag already has
365 a value associated with it, setTaggedValue will be used to update this value.
366 \param tagKey - Input - Integer tag.
367 \param pointshape - Shape of the value parameter
368 \param value - Input - Single DataArrayView value to be assigned to the tag.
369 \param dataOffset - Input - Offset of the beginning of the point in the value parameter
370 */
371 ESCRIPT_DLL_API
372 void
373 addTaggedValue(int tagKey,
374 const DataTypes::ShapeType& pointshape,
375 const ValueType& value,
376 int dataOffset=0);
377
378 /**
379 \brief
380 addTag - does not modify the default value for this object. ** Not unit tested **
381
382 Description:
383 Add a single tag. The default value for this DataTagged will be associated with the tag.
384 If this tag already has a value associated with it, then no change will be made.
385 \param tagKey - Input - Integer tag.
386 TODO: Make sure this is unit tested
387 */
388 ESCRIPT_DLL_API
389 void
390 addTag(int tagKey);
391
392
393 // /**
394 // \brief
395 // setTaggedValues
396 //
397 // Description:
398 // Set the given tags to the given values in this DataTagged object.
399 // \param tagKeys - Input - A vector of integer tag.
400 // \param values - Input - A vector of DataArrayViews. If this is empty
401 // all tag values will be assigned a scalar data-point of value
402 // 0. If it contains one value all tag values will be assigned
403 // this value. Otherwise consecutive tags will be assigned
404 // consecutive values. If there is a mismatch between the
405 // number of keys and the number of values an exception
406 // will be generated.
407 // T
408 // */
409 // ESCRIPT_DLL_API
410 // void
411 // setTaggedValues(const TagListType& tagKeys,
412 // const ValueListType& values);
413
414 // /**
415 // \brief
416 // setTaggedValue
417 //
418 // Description:
419 // Assign the given value to the given tag.
420 // \param tagKey - Input - Integer tag.
421 // \param value - Input - Single DataArrayView value to be assigned to the tag.
422 // T
423 // */
424 // ESCRIPT_DLL_API
425 // virtual
426 // void
427 // setTaggedValue(int tagKey,
428 // const DataArrayView& value);
429
430 /**
431 \brief
432 setTaggedValue
433
434 Description:
435 Assign the given value to the given tag.
436 \param tagKey - Input - Integer tag.
437 \param pointshape - the shape of the value parameter
438 \param value - Input - Vector storing the datapoint to be assigned to the tag.
439 \param dataOffset - beginning of the datapoint within "value".
440 T
441 */
442 ESCRIPT_DLL_API
443 void
444 setTaggedValue(int tagKey,
445 const DataTypes::ShapeType& pointshape,
446 const ValueType& value,
447 int dataOffset=0);
448
449
450 // /**
451 // \brief
452 // getDataPointByTag
453 //
454 // Description:
455 // Return data-point associated with the given tag as a DataArrayView.
456 // \param tag - Input - Integer key.
457 // T
458 // */
459 // ESCRIPT_DLL_API
460 // DataArrayView
461 // getDataPointByTag(int tag) const;
462
463 /**
464 \brief
465 getDataByTag
466
467 Return a pointer to the beginning of the datapoint with the specified tag.
468 TODO Eventually these should be inlined.
469 \param tag - Input - Integer key.
470 \param i - position in the underlying datastructure
471 T
472 */
473 ESCRIPT_DLL_API
474 DataTypes::ValueType::const_reference
475 getDataByTag(int tag, DataTypes::ValueType::size_type i) const;
476
477 ESCRIPT_DLL_API
478 DataTypes::ValueType::reference
479 getDataByTag(int tag, DataTypes::ValueType::size_type i);
480
481
482 /**
483 \brief
484 getOffsetForTag
485
486 \param tag
487 \return the offset of the beginning of the datapoint corresponding to tag.
488
489 Note: If the tag is not valid, the offset of the default value is returned instead.
490 */
491 ESCRIPT_DLL_API
492 DataTypes::ValueType::size_type
493 getOffsetForTag(int tag) const;
494
495 // /**
496 // \brief
497 // getDataPoint
498 //
499 // Description:
500 // Return the data-point specified by the given sample and data-point
501 // numbers as a DataArrayView.
502 // \param sampleNo - Input.
503 // \param dataPointNo - Input.
504 // T
505 // */
506 // ESCRIPT_DLL_API
507 // virtual
508 // DataArrayView
509 // getDataPoint(int sampleNo,
510 // int dataPointNo);
511
512
513 /**
514 \brief
515 Return a reference to the underlying DataVector.
516 */
517
518 ESCRIPT_DLL_API
519 DataTypes::ValueType&
520 getVector();
521
522 ESCRIPT_DLL_API
523 const DataTypes::ValueType&
524 getVector() const;
525
526
527
528 /**
529 \brief
530 getData
531
532 Description:
533 Return pointer to the data
534 T
535 */
536 // ESCRIPT_DLL_API
537 // const DataTypes::ValueType::ElementType*
538 // getData() const;
539
540 /**
541 \brief
542 getTagLookup
543
544 Description:
545 Return a reference to the tag offset lookup table.
546 T
547 */
548 ESCRIPT_DLL_API
549 const DataMapType&
550 getTagLookup() const;
551
552 /**
553 \brief
554 isCurrentTag
555
556 Description:
557 Return true if the given tag exists within the DataTagged tag map.
558
559 *** NB: The DataTagged tag map does not necessarily coincide with the tag
560 keys in the associated function space.
561 T
562 */
563 ESCRIPT_DLL_API
564 bool
565 isCurrentTag(int tag) const;
566
567 // /**
568 // \brief
569 // getDefaultValue
570 //
571 // Description:
572 // Return the default value. This value is associated with any tag which
573 // is not explicitly recorded in this DataTagged object's tag map.
574 // T
575 // */
576 // ESCRIPT_DLL_API
577 // DataArrayView&
578 // getDefaultValue();
579 //
580 // ESCRIPT_DLL_API
581 // const DataArrayView&
582 // getDefaultValue() const;
583
584 /**
585 \brief
586 getDefaultValue
587
588 Description:
589 Return the default value. This value is associated with any tag which
590 is not explicitly recorded in this DataTagged object's tag map.
591 \param i - position in the underlying datastructure
592 T
593 */
594 ESCRIPT_DLL_API
595 DataTypes::ValueType::reference
596 getDefaultValue(DataTypes::ValueType::size_type i);
597
598 ESCRIPT_DLL_API
599 DataTypes::ValueType::const_reference
600 getDefaultValue(DataTypes::ValueType::size_type i) const;
601
602
603
604
605 /**
606 \brief
607 getLength
608
609 Description:
610 Return the total number of doubles stored for this DataTagged object.
611 T
612 */
613 ESCRIPT_DLL_API
614 virtual
615 ValueType::size_type
616 getLength() const;
617
618 /**
619 \brief
620 getSlice
621
622 Description:
623 Factory method that returns a newly created DataTagged object generated
624 by taking the specified slice from this DataTagged object.
625 The caller is reponsible for managing the returned object.
626 T
627 */
628 ESCRIPT_DLL_API
629 virtual
630 DataAbstract*
631 getSlice(const DataTypes::RegionType& region) const;
632
633 /**
634 \brief
635 Slice Constructor for DataTagged.
636
637 Description:
638 Creates a DataTagged object which is the specified slice
639 from the given DataTagged object.
640 \param other - Input - DataTagged object to slice from.
641 \param region - Input - Region to slice.
642 T
643 */
644 ESCRIPT_DLL_API
645 DataTagged(const DataTagged& other,
646 const DataTypes::RegionType& region);
647
648 /**
649 \brief
650 setSlice
651
652 Description:
653 Copy the given Data object into the specified region in this object.
654 \param other - Input - Data object to copy from.
655 \param region - Input - Region to copy into (NB: must have same shape as other!).
656 T
657 */
658 ESCRIPT_DLL_API
659 virtual
660 void
661 setSlice(const DataAbstract* other,
662 const DataTypes::RegionType& region);
663
664
665 /**
666 \brief
667 Computes a symmetric matrix (A + AT) / 2
668
669 \param ev - Output - symmetric matrix
670
671 */
672 ESCRIPT_DLL_API
673 virtual void
674 symmetric(DataAbstract* ev);
675
676 /**
677 \brief
678 Computes a nonsymmetric matrix (A - AT) / 2
679
680 \param ev - Output - nonsymmetric matrix
681
682 */
683 ESCRIPT_DLL_API
684 virtual void
685 nonsymmetric(DataAbstract* ev);
686
687 /**
688 \brief
689 Computes the trace of a matrix
690
691 \param ev - Output - the trace of a matrix
692
693 */
694 ESCRIPT_DLL_API
695 virtual void
696 trace(DataAbstract* ev, int axis_offset);
697
698 /**
699 \brief
700 swaps components axis0 and axis1
701
702 \param ev - Output - swapped components
703
704 */
705 ESCRIPT_DLL_API
706 virtual void
707 swapaxes(DataAbstract* ev, int axis0, int axis1);
708
709 /**
710 \brief
711 Transpose each data point of this Data object around the given axis.
712
713 \param ev - Output - the transpose of a matrix
714
715 */
716 ESCRIPT_DLL_API
717 virtual void
718 transpose(DataAbstract* ev, int axis_offset);
719
720 /**
721 \brief
722 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev
723
724 \param ev - Output - eigenvalues in increasing order at each data point
725
726 */
727 ESCRIPT_DLL_API
728 virtual void
729 eigenvalues(DataAbstract* ev);
730
731 /**
732 \brief
733 solves the eigenvalue problem this*V=ev*V for the eigenvalues ev and eigenvectors V
734
735 \param ev - Output - eigenvalues in increasing order at each data point
736 \param V - Output - corresponding eigenvectors. They are normalized such that their length is one
737 and the first nonzero component is positive.
738 \param tol - Input - eigenvalue with relative distance tol are treated as equal.
739
740 */
741
742 ESCRIPT_DLL_API
743 virtual void
744 eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol=1.e-13);
745
746
747 /**
748 \brief Returns the offset in the structure which stores the default value
749 */
750 ESCRIPT_DLL_API
751 DataTypes::ValueType::size_type
752 getDefaultOffset() const;
753
754 protected:
755
756 private:
757
758 //
759 // The offset lookup table
760 DataMapType m_offsetLookup;
761
762 //
763 // the offset to the default value
764 static const int m_defaultValueOffset = 0;
765
766 //
767 // The actual data
768 ValueType m_data;
769
770 };
771
772 inline
773 bool
774 DataTagged::isCurrentTag(int tag) const
775 {
776 DataMapType::const_iterator pos(m_offsetLookup.find(tag));
777 return (pos!=m_offsetLookup.end());
778 }
779
780 // inline
781 // DataArrayView&
782 // DataTagged::getDefaultValue()
783 // {
784 // // The default value is always the first value.
785 // return getPointDataView();
786 // }
787
788 // inline
789 // const DataArrayView&
790 // DataTagged::getDefaultValue() const
791 // {
792 // // The default value is always the first value.
793 // return getPointDataView();
794 // }
795
796
797 inline
798 DataTypes::ValueType::size_type
799 DataTagged::getDefaultOffset() const
800 {
801 return m_defaultValueOffset;
802 }
803
804 inline
805 DataTypes::ValueType::reference
806 DataTagged::getDefaultValue(DataTypes::ValueType::size_type i)
807 {
808 return getVector()[i];
809 }
810
811 inline
812 DataTypes::ValueType::const_reference
813 DataTagged::getDefaultValue(DataTypes::ValueType::size_type i) const
814 {
815 return getVector()[i];
816 }
817
818
819
820
821 // inline
822 // const DataTypes::ValueType::ElementType*
823 // DataTagged::getData() const
824 // {
825 // return &(m_data[0]);
826 // }
827
828 inline
829 const DataTagged::DataMapType&
830 DataTagged::getTagLookup() const
831 {
832 return m_offsetLookup;
833 }
834
835 inline
836 DataTypes::ValueType::size_type
837 DataTagged::getLength() const
838 {
839 return m_data.size();
840 }
841
842 } // end of namespace
843
844 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26