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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1799 - (show annotations)
Wed Sep 17 06:33:18 2008 UTC (11 years, 5 months ago) by jfenwick
File MIME type: text/plain
File size: 21059 byte(s)
Added Data::copySelf() [Note: this is exposed as copy() in python].
This method returns a pointer to a deep copy of the target.
There are c++ tests but no python tests for this yet.

All DataAbstracts now have a deepCopy() which simplifies the 
implementation of the compy methods.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26