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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 789 - (show annotations)
Wed Jul 26 05:41:11 2006 UTC (12 years, 8 months ago) by dhawcroft
File MIME type: text/plain
File size: 43074 byte(s)
Inserted PASO_MPI macro in Data.h to match source file
1 // $Id$
2 /*
3 ************************************************************
4 * Copyright 2006 by ACcESS MNRF *
5 * *
6 * http://www.access.edu.au *
7 * Primary Business: Queensland, Australia *
8 * Licensed under the Open Software License version 3.0 *
9 * http://www.opensource.org/licenses/osl-3.0.php *
10 * *
11 ************************************************************
12 */
13
14 /** \file Data.h */
15
16 #ifndef DATA_H
17 #define DATA_H
18 #include "system_dep.h"
19
20 #include "DataAbstract.h"
21 #include "DataAlgorithm.h"
22 #include "FunctionSpace.h"
23 #include "BinaryOp.h"
24 #include "UnaryOp.h"
25 #include "DataException.h"
26
27 extern "C" {
28 #include "DataC.h"
29 #include "paso/Paso.h"
30 }
31
32 #include <string>
33 #include <algorithm>
34
35 #include <boost/shared_ptr.hpp>
36 #include <boost/python/object.hpp>
37 #include <boost/python/tuple.hpp>
38 #include <boost/python/numeric.hpp>
39
40 namespace escript {
41
42 //
43 // Forward declaration for various implementations of Data.
44 class DataConstant;
45 class DataTagged;
46 class DataExpanded;
47
48 /**
49 \brief
50 Data creates the appropriate Data object for the given construction
51 arguments.
52
53 Description:
54 Data is essentially a factory class which creates the appropriate Data
55 object for the given construction arguments. It retains control over
56 the object created for the lifetime of the object.
57 The type of Data object referred to may change during the lifetime of
58 the Data object.
59 */
60 class Data {
61
62 public:
63
64 // These typedefs allow function names to be cast to pointers
65 // to functions of the appropriate type when calling unaryOp etc.
66 typedef double (*UnaryDFunPtr)(double);
67 typedef double (*BinaryDFunPtr)(double,double);
68
69 /**
70 Constructors.
71 */
72
73 /**
74 \brief
75 Default constructor.
76 Creates a DataEmpty object.
77 */
78 ESCRIPT_DLL_API
79 Data();
80
81 /**
82 \brief
83 Copy constructor.
84 WARNING: Only performs a shallow copy.
85 */
86 ESCRIPT_DLL_API
87 Data(const Data& inData);
88
89 /**
90 \brief
91 Constructor from another Data object. If "what" is different from the
92 function space of inData the inData are tried to be interpolated to what,
93 otherwise a shallow copy of inData is returned.
94 */
95 ESCRIPT_DLL_API
96 Data(const Data& inData,
97 const FunctionSpace& what);
98
99 /**
100 \brief
101 Constructor which copies data from a DataArrayView.
102
103 \param value - Input - Data value for a single point.
104 \param what - Input - A description of what this data represents.
105 \param expanded - Input - Flag, if true fill the entire container with
106 the value. Otherwise a more efficient storage
107 mechanism will be used.
108 */
109 ESCRIPT_DLL_API
110 Data(const DataArrayView& value,
111 const FunctionSpace& what=FunctionSpace(),
112 bool expanded=false);
113
114 /**
115 \brief
116 Constructor which creates a Data from a DataArrayView shape.
117
118 \param value - Input - Single value applied to all Data.
119 \param dataPointShape - Input - The shape of each data point.
120 \param what - Input - A description of what this data represents.
121 \param expanded - Input - Flag, if true fill the entire container with
122 the given value. Otherwise a more efficient storage
123 mechanism will be used.
124 */
125 ESCRIPT_DLL_API
126 Data(double value,
127 const DataArrayView::ShapeType& dataPointShape=DataArrayView::ShapeType(),
128 const FunctionSpace& what=FunctionSpace(),
129 bool expanded=false);
130
131 /**
132 \brief
133 Constructor which performs a deep copy of a region from another Data object.
134
135 \param inData - Input - Input Data object.
136 \param region - Input - Region to copy.
137 */
138 ESCRIPT_DLL_API
139 Data(const Data& inData,
140 const DataArrayView::RegionType& region);
141
142 /**
143 \brief
144 Constructor which will create Tagged data if expanded is false.
145 No attempt is made to ensure the tag keys match the tag keys
146 within the function space.
147
148 \param tagKeys - Input - List of tag values.
149 \param values - Input - List of values, one for each tag.
150 \param defaultValue - Input - A default value, used if tag doesn't exist.
151 \param what - Input - A description of what this data represents.
152 \param expanded - Input - Flag, if true fill the entire container with
153 the appropriate values.
154 ==>*
155 */
156 ESCRIPT_DLL_API
157 Data(const DataTagged::TagListType& tagKeys,
158 const DataTagged::ValueListType& values,
159 const DataArrayView& defaultValue,
160 const FunctionSpace& what=FunctionSpace(),
161 bool expanded=false);
162
163 /**
164 \brief
165 Constructor which copies data from a python numarray.
166
167 \param value - Input - Data value for a single point.
168 \param what - Input - A description of what this data represents.
169 \param expanded - Input - Flag, if true fill the entire container with
170 the value. Otherwise a more efficient storage
171 mechanism will be used.
172 */
173 ESCRIPT_DLL_API
174 Data(const boost::python::numeric::array& value,
175 const FunctionSpace& what=FunctionSpace(),
176 bool expanded=false);
177
178 /**
179 \brief
180 Constructor which copies data from any object that can be converted into
181 a python numarray.
182
183 \param value - Input - Input data.
184 \param what - Input - A description of what this data represents.
185 \param expanded - Input - Flag, if true fill the entire container with
186 the value. Otherwise a more efficient storage
187 mechanism will be used.
188 */
189 ESCRIPT_DLL_API
190 Data(const boost::python::object& value,
191 const FunctionSpace& what=FunctionSpace(),
192 bool expanded=false);
193
194 /**
195 \brief
196 Constructor which creates a DataConstant.
197 Copies data from any object that can be converted
198 into a numarray. All other parameters are copied from other.
199
200 \param value - Input - Input data.
201 \param other - Input - contains all other parameters.
202 */
203 ESCRIPT_DLL_API
204 Data(const boost::python::object& value,
205 const Data& other);
206
207 /**
208 \brief
209 Constructor which creates a DataConstant of "shape" with constant value.
210 */
211 ESCRIPT_DLL_API
212 Data(double value,
213 const boost::python::tuple& shape=boost::python::make_tuple(),
214 const FunctionSpace& what=FunctionSpace(),
215 bool expanded=false);
216 /**
217 \brief
218 Destructor
219 */
220 ESCRIPT_DLL_API
221 ~Data();
222
223 /**
224 \brief
225 Perform a deep copy.
226 */
227 ESCRIPT_DLL_API
228 void
229 copy(const Data& other);
230
231 /**
232 Member access methods.
233 */
234
235 /**
236 \brief
237 switches on update protection
238
239 */
240 ESCRIPT_DLL_API
241 void
242 setProtection();
243
244 /**
245 \brief
246 Returns trueif the data object is protected against update
247
248 */
249 ESCRIPT_DLL_API
250 bool
251 isProtected() const;
252 /**
253 \brief
254 Return the values of all data-points as a single python numarray object.
255 */
256 ESCRIPT_DLL_API
257 const boost::python::numeric::array
258 convertToNumArray();
259
260 /**
261 \brief
262 Return the values of all data-points for the given sample as a single python numarray object.
263 */
264 ESCRIPT_DLL_API
265 const boost::python::numeric::array
266 convertToNumArrayFromSampleNo(int sampleNo);
267
268 /**
269 \brief
270 Return the value of the specified data-point as a single python numarray object.
271 */
272 #ifndef PASO_MPI
273 ESCRIPT_DLL_API
274 const boost::python::numeric::array
275 convertToNumArrayFromDPNo(int sampleNo,
276 int dataPointNo);
277 #else
278 ESCRIPT_DLL_API
279 const boost::python::numeric::array
280 convertToNumArrayFromDPNo(int procNo,
281 int sampleNo,
282 int dataPointNo);
283 #endif
284
285
286 /**
287 \brief
288 Fills the expanded Data object from values of a python numarray object.
289 */
290 ESCRIPT_DLL_API
291 void
292 fillFromNumArray(const boost::python::numeric::array);
293
294 /**
295 \brief
296 Return the tag number associated with the given data-point.
297
298 The data-point number here corresponds to the data-point number in the
299 numarray returned by convertToNumArray.
300 */
301 ESCRIPT_DLL_API
302 int
303 getTagNumber(int dpno);
304
305 /**
306 \brief
307 Return the C wrapper for the Data object.
308 */
309 ESCRIPT_DLL_API
310 escriptDataC
311 getDataC();
312
313 /**
314 \brief
315 Return the C wrapper for the Data object - const version.
316 */
317 ESCRIPT_DLL_API
318 escriptDataC
319 getDataC() const;
320
321 /**
322 \brief
323 Write the data as a string.
324 */
325 ESCRIPT_DLL_API
326 inline
327 std::string
328 toString() const
329 {
330 return m_data->toString();
331 }
332
333 /**
334 \brief
335 Return the DataArrayView of the point data. This essentially contains
336 the shape information for each data point although it also may be used
337 to manipulate the point data.
338 */
339 ESCRIPT_DLL_API
340 inline
341 const DataArrayView&
342 getPointDataView() const
343 {
344 return m_data->getPointDataView();
345 }
346
347 /**
348 \brief
349 Whatever the current Data type make this into a DataExpanded.
350 */
351 ESCRIPT_DLL_API
352 void
353 expand();
354
355 /**
356 \brief
357 If possible convert this Data to DataTagged. This will only allow
358 Constant data to be converted to tagged. An attempt to convert
359 Expanded data to tagged will throw an exception.
360 ==>*
361 */
362 ESCRIPT_DLL_API
363 void
364 tag();
365
366 /**
367 \brief
368 Return true if this Data is expanded.
369 */
370 ESCRIPT_DLL_API
371 bool
372 isExpanded() const;
373
374 /**
375 \brief
376 Return true if this Data is tagged.
377 */
378 ESCRIPT_DLL_API
379 bool
380 isTagged() const;
381
382 /**
383 \brief
384 Return true if this Data is constant.
385 */
386 ESCRIPT_DLL_API
387 bool
388 isConstant() const;
389
390 /**
391 \brief
392 Return true if this Data is empty.
393 */
394 ESCRIPT_DLL_API
395 bool
396 isEmpty() const;
397
398 /**
399 \brief
400 Return the function space.
401 */
402 ESCRIPT_DLL_API
403 inline
404 const FunctionSpace&
405 getFunctionSpace() const
406 {
407 return m_data->getFunctionSpace();
408 }
409
410 /**
411 \brief
412 Return a copy of the function space.
413 */
414 ESCRIPT_DLL_API
415 const FunctionSpace
416 getCopyOfFunctionSpace() const;
417
418 /**
419 \brief
420 Return the domain.
421 */
422 ESCRIPT_DLL_API
423 inline
424 const AbstractDomain&
425 getDomain() const
426 {
427 return getFunctionSpace().getDomain();
428 }
429
430 /**
431 \brief
432 Return a copy of the domain.
433 */
434 ESCRIPT_DLL_API
435 const AbstractDomain
436 getCopyOfDomain() const;
437
438 /**
439 \brief
440 Return the rank of the point data.
441 */
442 ESCRIPT_DLL_API
443 inline
444 int
445 getDataPointRank() const
446 {
447 return m_data->getPointDataView().getRank();
448 }
449
450 /**
451 \brief
452 Return the number of samples.
453 */
454 ESCRIPT_DLL_API
455 inline
456 int
457 getNumSamples() const
458 {
459 return m_data->getNumSamples();
460 }
461
462 /**
463 \brief
464 Return the number of data points per sample.
465 */
466 ESCRIPT_DLL_API
467 inline
468 int
469 getNumDataPointsPerSample() const
470 {
471 return m_data->getNumDPPSample();
472 }
473
474 /**
475 \brief
476 Return the sample data for the given sample no. This is not the
477 preferred interface but is provided for use by C code.
478 \param sampleNo - Input - the given sample no.
479 */
480 ESCRIPT_DLL_API
481 inline
482 DataAbstract::ValueType::value_type*
483 getSampleData(DataAbstract::ValueType::size_type sampleNo)
484 {
485 return m_data->getSampleData(sampleNo);
486 }
487
488 /**
489 \brief
490 Return the sample data for the given tag. If an attempt is made to
491 access data that isn't tagged an exception will be thrown.
492 \param tag - Input - the tag key.
493 */
494 ESCRIPT_DLL_API
495 inline
496 DataAbstract::ValueType::value_type*
497 getSampleDataByTag(int tag)
498 {
499 return m_data->getSampleDataByTag(tag);
500 }
501
502 /**
503 \brief
504 Assign the given value to the data-points referenced by the given
505 reference number.
506
507 The value supplied is a python numarray object. The data from this numarray
508 is unpacked into a DataArray, and this is used to set the corresponding
509 data-points in the underlying Data object.
510
511 If the underlying Data object cannot be accessed via reference numbers, an
512 exception will be thrown.
513
514 \param ref - Input - reference number.
515 \param value - Input - value to assign to data-points associated with
516 the given reference number.
517 */
518 ESCRIPT_DLL_API
519 void
520 setRefValue(int ref,
521 const boost::python::numeric::array& value);
522
523 /**
524 \brief
525 Return the values associated with the data-points referenced by the given
526 reference number.
527
528 The value supplied is a python numarray object. The data from the corresponding
529 data-points in this Data object are packed into the given numarray object.
530
531 If the underlying Data object cannot be accessed via reference numbers, an
532 exception will be thrown.
533
534 \param ref - Input - reference number.
535 \param value - Output - object to receive values from data-points
536 associated with the given reference number.
537 */
538 ESCRIPT_DLL_API
539 void
540 getRefValue(int ref,
541 boost::python::numeric::array& value);
542
543 /**
544 \brief
545 Return a view into the data for the data point specified.
546 NOTE: Construction of the DataArrayView is a relatively expensive
547 operation.
548 \param sampleNo - Input -
549 \param dataPointNo - Input -
550 */
551 ESCRIPT_DLL_API
552 inline
553 DataArrayView
554 getDataPoint(int sampleNo,
555 int dataPointNo)
556 {
557 return m_data->getDataPoint(sampleNo,dataPointNo);
558 }
559
560 /**
561 \brief
562 Return a reference to the data point shape.
563 */
564 ESCRIPT_DLL_API
565 const DataArrayView::ShapeType&
566 getDataPointShape() const;
567
568 /**
569 \brief
570 Return the data point shape as a tuple of integers.
571 */
572 ESCRIPT_DLL_API
573 const boost::python::tuple
574 getShapeTuple() const;
575
576 /**
577 \brief
578 Return the size of the data point. It is the product of the
579 data point shape dimensions.
580 */
581 ESCRIPT_DLL_API
582 int
583 getDataPointSize() const;
584
585 /**
586 \brief
587 Return the number of doubles stored for this Data.
588 */
589 ESCRIPT_DLL_API
590 DataArrayView::ValueType::size_type
591 getLength() const;
592
593 /**
594 \brief
595 Assign the given value to the tag. Implicitly converts this
596 object to type DataTagged. Throws an exception if this object
597 cannot be converted to a DataTagged object.
598 \param tagKey - Input - Integer key.
599 \param value - Input - Value to associate with given key.
600 ==>*
601 */
602 ESCRIPT_DLL_API
603 void
604 setTaggedValue(int tagKey,
605 const boost::python::object& value);
606
607 /**
608 \brief
609 Assign the given value to the tag. Implicitly converts this
610 object to type DataTagged. Throws an exception if this object
611 cannot be converted to a DataTagged object.
612 \param tagKey - Input - Integer key.
613 \param value - Input - Value to associate with given key.
614 ==>*
615 */
616 ESCRIPT_DLL_API
617 void
618 setTaggedValueFromCPP(int tagKey,
619 const DataArrayView& value);
620
621 /**
622 \brief
623 Copy other Data object into this Data object where mask is positive.
624 */
625 ESCRIPT_DLL_API
626 void
627 copyWithMask(const Data& other,
628 const Data& mask);
629
630 /**
631 Data object operation methods and operators.
632 */
633
634 /**
635 \brief
636 Interpolates this onto the given functionspace and returns
637 the result as a Data object.
638 *
639 */
640 ESCRIPT_DLL_API
641 Data
642 interpolate(const FunctionSpace& functionspace) const;
643
644 /**
645 \brief
646 Calculates the gradient of the data at the data points of functionspace.
647 If functionspace is not present the function space of Function(getDomain()) is used.
648 *
649 */
650 ESCRIPT_DLL_API
651 Data
652 gradOn(const FunctionSpace& functionspace) const;
653
654 ESCRIPT_DLL_API
655 Data
656 grad() const;
657
658 /**
659 \brief
660 Calculate the integral over the function space domain.
661 *
662 */
663 ESCRIPT_DLL_API
664 boost::python::numeric::array
665 integrate() const;
666
667 /**
668 \brief
669 Return a Data with a 1 for +ive values and a 0 for 0 or -ive values.
670 *
671 */
672 ESCRIPT_DLL_API
673 Data
674 wherePositive() const;
675
676 /**
677 \brief
678 Return a Data with a 1 for -ive values and a 0 for +ive or 0 values.
679 *
680 */
681 ESCRIPT_DLL_API
682 Data
683 whereNegative() const;
684
685 /**
686 \brief
687 Return a Data with a 1 for +ive or 0 values and a 0 for -ive values.
688 *
689 */
690 ESCRIPT_DLL_API
691 Data
692 whereNonNegative() const;
693
694 /**
695 \brief
696 Return a Data with a 1 for -ive or 0 values and a 0 for +ive values.
697 *
698 */
699 ESCRIPT_DLL_API
700 Data
701 whereNonPositive() const;
702
703 /**
704 \brief
705 Return a Data with a 1 for 0 values and a 0 for +ive or -ive values.
706 *
707 */
708 ESCRIPT_DLL_API
709 Data
710 whereZero(double tol=0.0) const;
711
712 /**
713 \brief
714 Return a Data with a 0 for 0 values and a 1 for +ive or -ive values.
715 *
716 */
717 ESCRIPT_DLL_API
718 Data
719 whereNonZero(double tol=0.0) const;
720
721 /**
722 \brief
723 Return the maximum absolute value of this Data object.
724 *
725 */
726 ESCRIPT_DLL_API
727 double
728 Lsup() const;
729
730 /**
731 \brief
732 Return the minimum absolute value of this Data object.
733 *
734 */
735 ESCRIPT_DLL_API
736 double
737 Linf() const;
738
739 /**
740 \brief
741 Return the maximum value of this Data object.
742 *
743 */
744 ESCRIPT_DLL_API
745 double
746 sup() const;
747
748 /**
749 \brief
750 Return the minimum value of this Data object.
751 *
752 */
753 ESCRIPT_DLL_API
754 double
755 inf() const;
756
757 /**
758 \brief
759 Return the absolute value of each data point of this Data object.
760 *
761 */
762 ESCRIPT_DLL_API
763 Data
764 abs() const;
765
766 /**
767 \brief
768 Return the maximum value of each data point of this Data object.
769 *
770 */
771 ESCRIPT_DLL_API
772 Data
773 maxval() const;
774
775 /**
776 \brief
777 Return the minimum value of each data point of this Data object.
778 *
779 */
780 ESCRIPT_DLL_API
781 Data
782 minval() const;
783
784 /**
785 \brief
786 Return the (sample number, data-point number) of the data point with
787 the minimum value in this Data object.
788 */
789 ESCRIPT_DLL_API
790 const boost::python::tuple
791 mindp() const;
792
793 ESCRIPT_DLL_API
794 void
795 #ifndef PASO_MPI
796 calc_mindp(int& SampleNo,
797 int& DataPointNo) const;
798 #else
799 calc_mindp(int& ProcNo,
800 int& SampleNo,
801 int& DataPointNo) const;
802 #endif
803 /**
804 \brief
805 Return the sign of each data point of this Data object.
806 -1 for negative values, zero for zero values, 1 for positive values.
807 *
808 */
809 ESCRIPT_DLL_API
810 Data
811 sign() const;
812
813 /**
814 \brief
815 Return the symmetric part of a matrix which is half the matrix plus its transpose.
816 *
817 */
818 ESCRIPT_DLL_API
819 Data
820 symmetric() const;
821
822 /**
823 \brief
824 Return the nonsymmetric part of a matrix which is half the matrix minus its transpose.
825 *
826 */
827 ESCRIPT_DLL_API
828 Data
829 nonsymmetric() const;
830
831 /**
832 \brief
833 Return the trace of a matrix
834 *
835 */
836 ESCRIPT_DLL_API
837 Data
838 matrixtrace(int axis_offset) const;
839
840 /**
841 \brief
842 Transpose each data point of this Data object around the given axis.
843 *
844 */
845 ESCRIPT_DLL_API
846 Data
847 transpose(int axis_offset) const;
848
849 /**
850 \brief
851 Return the eigenvalues of the symmetric part at each data point of this Data object in increasing values.
852 Currently this function is restricted to rank 2, square shape, and dimension 3.
853 *
854 */
855 ESCRIPT_DLL_API
856 Data
857 eigenvalues() const;
858
859 /**
860 \brief
861 Return the eigenvalues and corresponding eigenvcetors of the symmetric part at each data point of this Data object.
862 the eigenvalues are ordered in increasing size where eigenvalues with relative difference less than
863 tol are treated as equal. The eigenvectors are orthogonal, normalized and the sclaed such that the
864 first non-zero entry is positive.
865 Currently this function is restricted to rank 2, square shape, and dimension 3
866 *
867 */
868 ESCRIPT_DLL_API
869 const boost::python::tuple
870 eigenvalues_and_eigenvectors(const double tol=1.e-12) const;
871
872 /**
873 \brief
874 Calculate the trace of each data point of this Data object.
875 *
876 */
877 ESCRIPT_DLL_API
878 Data
879 trace() const;
880
881 /**
882 \brief
883 Return the sin of each data point of this Data object.
884 *
885 */
886 ESCRIPT_DLL_API
887 Data
888 sin() const;
889
890 /**
891 \brief
892 Return the cos of each data point of this Data object.
893 *
894 */
895 ESCRIPT_DLL_API
896 Data
897 cos() const;
898
899 /**
900 \brief
901 Return the tan of each data point of this Data object.
902 *
903 */
904 ESCRIPT_DLL_API
905 Data
906 tan() const;
907
908 /**
909 \brief
910 Return the asin of each data point of this Data object.
911 *
912 */
913 ESCRIPT_DLL_API
914 Data
915 asin() const;
916
917 /**
918 \brief
919 Return the acos of each data point of this Data object.
920 *
921 */
922 ESCRIPT_DLL_API
923 Data
924 acos() const;
925
926 /**
927 \brief
928 Return the atan of each data point of this Data object.
929 *
930 */
931 ESCRIPT_DLL_API
932 Data
933 atan() const;
934
935 /**
936 \brief
937 Return the sinh of each data point of this Data object.
938 *
939 */
940 ESCRIPT_DLL_API
941 Data
942 sinh() const;
943
944 /**
945 \brief
946 Return the cosh of each data point of this Data object.
947 *
948 */
949 ESCRIPT_DLL_API
950 Data
951 cosh() const;
952
953 /**
954 \brief
955 Return the tanh of each data point of this Data object.
956 *
957 */
958 ESCRIPT_DLL_API
959 Data
960 tanh() const;
961
962 /**
963 \brief
964 Return the asinh of each data point of this Data object.
965 *
966 */
967 ESCRIPT_DLL_API
968 Data
969 asinh() const;
970
971 /**
972 \brief
973 Return the acosh of each data point of this Data object.
974 *
975 */
976 ESCRIPT_DLL_API
977 Data
978 acosh() const;
979
980 /**
981 \brief
982 Return the atanh of each data point of this Data object.
983 *
984 */
985 ESCRIPT_DLL_API
986 Data
987 atanh() const;
988
989 /**
990 \brief
991 Return the log to base 10 of each data point of this Data object.
992 *
993 */
994 ESCRIPT_DLL_API
995 Data
996 log10() const;
997
998 /**
999 \brief
1000 Return the natural log of each data point of this Data object.
1001 *
1002 */
1003 ESCRIPT_DLL_API
1004 Data
1005 log() const;
1006
1007 /**
1008 \brief
1009 Return the exponential function of each data point of this Data object.
1010 *
1011 */
1012 ESCRIPT_DLL_API
1013 Data
1014 exp() const;
1015
1016 /**
1017 \brief
1018 Return the square root of each data point of this Data object.
1019 *
1020 */
1021 ESCRIPT_DLL_API
1022 Data
1023 sqrt() const;
1024
1025 /**
1026 \brief
1027 Return the negation of each data point of this Data object.
1028 *
1029 */
1030 ESCRIPT_DLL_API
1031 Data
1032 neg() const;
1033
1034 /**
1035 \brief
1036 Return the identity of each data point of this Data object.
1037 Simply returns this object unmodified.
1038 *
1039 */
1040 ESCRIPT_DLL_API
1041 Data
1042 pos() const;
1043
1044 /**
1045 \brief
1046 Return the given power of each data point of this Data object.
1047
1048 \param right Input - the power to raise the object to.
1049 *
1050 */
1051 ESCRIPT_DLL_API
1052 Data
1053 powD(const Data& right) const;
1054
1055 /**
1056 \brief
1057 Return the given power of each data point of this boost python object.
1058
1059 \param right Input - the power to raise the object to.
1060 *
1061 */
1062 ESCRIPT_DLL_API
1063 Data
1064 powO(const boost::python::object& right) const;
1065
1066 /**
1067 \brief
1068 Return the given power of each data point of this boost python object.
1069
1070 \param left Input - the bases
1071 *
1072 */
1073
1074 ESCRIPT_DLL_API
1075 Data
1076 rpowO(const boost::python::object& left) const;
1077
1078 /**
1079 \brief
1080 writes the object to a file in the DX file format
1081 */
1082 ESCRIPT_DLL_API
1083 void
1084 saveDX(std::string fileName) const;
1085
1086 /**
1087 \brief
1088 writes the object to a file in the VTK file format
1089 */
1090 ESCRIPT_DLL_API
1091 void
1092 saveVTK(std::string fileName) const;
1093
1094 /**
1095 \brief
1096 Overloaded operator +=
1097 \param right - Input - The right hand side.
1098 *
1099 */
1100 ESCRIPT_DLL_API
1101 Data& operator+=(const Data& right);
1102 ESCRIPT_DLL_API
1103 Data& operator+=(const boost::python::object& right);
1104
1105 /**
1106 \brief
1107 Overloaded operator -=
1108 \param right - Input - The right hand side.
1109 *
1110 */
1111 ESCRIPT_DLL_API
1112 Data& operator-=(const Data& right);
1113 ESCRIPT_DLL_API
1114 Data& operator-=(const boost::python::object& right);
1115
1116 /**
1117 \brief
1118 Overloaded operator *=
1119 \param right - Input - The right hand side.
1120 *
1121 */
1122 ESCRIPT_DLL_API
1123 Data& operator*=(const Data& right);
1124 ESCRIPT_DLL_API
1125 Data& operator*=(const boost::python::object& right);
1126
1127 /**
1128 \brief
1129 Overloaded operator /=
1130 \param right - Input - The right hand side.
1131 *
1132 */
1133 ESCRIPT_DLL_API
1134 Data& operator/=(const Data& right);
1135 ESCRIPT_DLL_API
1136 Data& operator/=(const boost::python::object& right);
1137
1138 /**
1139 \brief
1140 Returns true if this can be interpolated to functionspace.
1141 */
1142 ESCRIPT_DLL_API
1143 bool
1144 probeInterpolation(const FunctionSpace& functionspace) const;
1145
1146 /**
1147 Data object slicing methods.
1148 */
1149
1150 /**
1151 \brief
1152 Returns a slice from this Data object.
1153
1154 /description
1155 Implements the [] get operator in python.
1156 Calls getSlice.
1157
1158 \param key - Input - python slice tuple specifying
1159 slice to return.
1160 */
1161 ESCRIPT_DLL_API
1162 Data
1163 getItem(const boost::python::object& key) const;
1164
1165 /**
1166 \brief
1167 Copies slice from value into this Data object.
1168
1169 Implements the [] set operator in python.
1170 Calls setSlice.
1171
1172 \param key - Input - python slice tuple specifying
1173 slice to copy from value.
1174 \param value - Input - Data object to copy from.
1175 */
1176 ESCRIPT_DLL_API
1177 void
1178 setItemD(const boost::python::object& key,
1179 const Data& value);
1180
1181 ESCRIPT_DLL_API
1182 void
1183 setItemO(const boost::python::object& key,
1184 const boost::python::object& value);
1185
1186 // These following public methods should be treated as private.
1187
1188 /**
1189 \brief
1190 Perform the given unary operation on every element of every data point in
1191 this Data object.
1192 */
1193 template <class UnaryFunction>
1194 ESCRIPT_DLL_API
1195 inline
1196 void
1197 unaryOp(UnaryFunction operation);
1198
1199 /**
1200 \brief
1201 Return a Data object containing the specified slice of
1202 this Data object.
1203 \param region - Input - Region to copy.
1204 *
1205 */
1206 ESCRIPT_DLL_API
1207 Data
1208 getSlice(const DataArrayView::RegionType& region) const;
1209
1210 /**
1211 \brief
1212 Copy the specified slice from the given value into this
1213 Data object.
1214 \param value - Input - Data to copy from.
1215 \param region - Input - Region to copy.
1216 *
1217 */
1218 ESCRIPT_DLL_API
1219 void
1220 setSlice(const Data& value,
1221 const DataArrayView::RegionType& region);
1222
1223 /**
1224 \brief
1225 Archive the current Data object to the given file.
1226 \param fileName - Input - file to archive to.
1227 */
1228 ESCRIPT_DLL_API
1229 void
1230 archiveData(const std::string fileName);
1231
1232 /**
1233 \brief
1234 Extract the Data object archived in the given file, overwriting
1235 the current Data object.
1236 Note - the current object must be of type DataEmpty.
1237 \param fileName - Input - file to extract from.
1238 \param fspace - Input - a suitable FunctionSpace descibing the data.
1239 */
1240 ESCRIPT_DLL_API
1241 void
1242 extractData(const std::string fileName,
1243 const FunctionSpace& fspace);
1244
1245
1246 #ifdef PASO_MPI
1247 /**
1248 \brief
1249 print the data values to stdout. Used for debugging
1250 */
1251 ESCRIPT_DLL_API
1252 void
1253 print(void);
1254
1255 /**
1256 \brief
1257 return the MPI rank number of the local data
1258 MPI_COMM_WORLD is assumed and the result of MPI_Comm_size()
1259 is returned
1260 */
1261 ESCRIPT_DLL_API
1262 int
1263 get_MPIRank(void) const;
1264
1265 /**
1266 \brief
1267 return the MPI rank number of the local data
1268 MPI_COMM_WORLD is assumed and the result of MPI_Comm_rank()
1269 is returned
1270 */
1271 ESCRIPT_DLL_API
1272 int
1273 get_MPISize(void) const;
1274
1275 /**
1276 \brief
1277 return the MPI rank number of the local data
1278 MPI_COMM_WORLD is assumed and returned.
1279 */
1280 ESCRIPT_DLL_API
1281 MPI_Comm
1282 get_MPIComm(void) const;
1283 #endif
1284
1285 protected:
1286
1287 private:
1288
1289 /**
1290 \brief
1291 Check *this and the right operand are compatible. Throws
1292 an exception if they aren't.
1293 \param right - Input - The right hand side.
1294 */
1295 inline
1296 void
1297 operandCheck(const Data& right) const
1298 {
1299 return m_data->operandCheck(*(right.m_data.get()));
1300 }
1301
1302 /**
1303 \brief
1304 Perform the specified reduction algorithm on every element of every data point in
1305 this Data object according to the given function and return the single value result.
1306 */
1307 template <class BinaryFunction>
1308 inline
1309 double
1310 algorithm(BinaryFunction operation,
1311 double initial_value) const;
1312
1313 /**
1314 \brief
1315 Reduce each data-point in this Data object using the given operation. Return a Data
1316 object with the same number of data-points, but with each data-point containing only
1317 one value - the result of the reduction operation on the corresponding data-point in
1318 this Data object
1319 */
1320 template <class BinaryFunction>
1321 inline
1322 Data
1323 dp_algorithm(BinaryFunction operation,
1324 double initial_value) const;
1325
1326 /**
1327 \brief
1328 Perform the given binary operation on all of the data's elements.
1329 The underlying type of the right hand side (right) determines the final
1330 type of *this after the operation. For example if the right hand side
1331 is expanded *this will be expanded if necessary.
1332 RHS is a Data object.
1333 */
1334 template <class BinaryFunction>
1335 inline
1336 void
1337 binaryOp(const Data& right,
1338 BinaryFunction operation);
1339
1340 /**
1341 \brief
1342 Perform the given binary operation on all of the data's elements.
1343 RHS is a boost::python object.
1344 */
1345 template <class BinaryFunction>
1346 inline
1347 void
1348 binaryOp(const boost::python::object& right,
1349 BinaryFunction operation);
1350
1351 /**
1352 \brief
1353 Convert the data type of the RHS to match this.
1354 \param right - Input - data type to match.
1355 */
1356 void
1357 typeMatchLeft(Data& right) const;
1358
1359 /**
1360 \brief
1361 Convert the data type of this to match the RHS.
1362 \param right - Input - data type to match.
1363 */
1364 void
1365 typeMatchRight(const Data& right);
1366
1367 /**
1368 \brief
1369 Construct a Data object of the appropriate type.
1370 */
1371 template <class IValueType>
1372 void
1373 initialise(const IValueType& value,
1374 const FunctionSpace& what,
1375 bool expanded);
1376
1377 /**
1378 \brief
1379 Reshape the data point if the data point is currently rank 0.
1380 Will throw an exception if the data points are not rank 0.
1381 The original data point value is used for all values of the new
1382 data point.
1383 */
1384 void
1385 reshapeDataPoint(const DataArrayView::ShapeType& shape);
1386
1387 //
1388 // flag to protect the data object against any update
1389 bool m_protected;
1390
1391 //
1392 // pointer to the actual data object
1393 boost::shared_ptr<DataAbstract> m_data;
1394
1395 //
1396 // pointer to the internal profiling data
1397 struct profDataEntry *profData;
1398
1399 };
1400
1401 template <class IValueType>
1402 void
1403 Data::initialise(const IValueType& value,
1404 const FunctionSpace& what,
1405 bool expanded)
1406 {
1407 //
1408 // Construct a Data object of the appropriate type.
1409 // Construct the object first as there seems to be a bug which causes
1410 // undefined behaviour if an exception is thrown during construction
1411 // within the shared_ptr constructor.
1412 if (expanded) {
1413 DataAbstract* temp=new DataExpanded(value,what);
1414 boost::shared_ptr<DataAbstract> temp_data(temp);
1415 m_data=temp_data;
1416 } else {
1417 DataAbstract* temp=new DataConstant(value,what);
1418 boost::shared_ptr<DataAbstract> temp_data(temp);
1419 m_data=temp_data;
1420 }
1421 }
1422
1423 /**
1424 Binary Data object operators.
1425 */
1426
1427 /**
1428 \brief
1429 Operator+
1430 Takes two Data objects.
1431 */
1432 ESCRIPT_DLL_API Data operator+(const Data& left, const Data& right);
1433
1434 /**
1435 \brief
1436 Operator-
1437 Takes two Data objects.
1438 */
1439 ESCRIPT_DLL_API Data operator-(const Data& left, const Data& right);
1440
1441 /**
1442 \brief
1443 Operator*
1444 Takes two Data objects.
1445 */
1446 ESCRIPT_DLL_API Data operator*(const Data& left, const Data& right);
1447
1448 /**
1449 \brief
1450 Operator/
1451 Takes two Data objects.
1452 */
1453 ESCRIPT_DLL_API Data operator/(const Data& left, const Data& right);
1454
1455 /**
1456 \brief
1457 Operator+
1458 Takes LHS Data object and RHS python::object.
1459 python::object must be convertable to Data type.
1460 */
1461 ESCRIPT_DLL_API Data operator+(const Data& left, const boost::python::object& right);
1462
1463 /**
1464 \brief
1465 Operator-
1466 Takes LHS Data object and RHS python::object.
1467 python::object must be convertable to Data type.
1468 */
1469 ESCRIPT_DLL_API Data operator-(const Data& left, const boost::python::object& right);
1470
1471 /**
1472 \brief
1473 Operator*
1474 Takes LHS Data object and RHS python::object.
1475 python::object must be convertable to Data type.
1476 */
1477 ESCRIPT_DLL_API Data operator*(const Data& left, const boost::python::object& right);
1478
1479 /**
1480 \brief
1481 Operator/
1482 Takes LHS Data object and RHS python::object.
1483 python::object must be convertable to Data type.
1484 */
1485 ESCRIPT_DLL_API Data operator/(const Data& left, const boost::python::object& right);
1486
1487 /**
1488 \brief
1489 Operator+
1490 Takes LHS python::object and RHS Data object.
1491 python::object must be convertable to Data type.
1492 */
1493 ESCRIPT_DLL_API Data operator+(const boost::python::object& left, const Data& right);
1494
1495 /**
1496 \brief
1497 Operator-
1498 Takes LHS python::object and RHS Data object.
1499 python::object must be convertable to Data type.
1500 */
1501 ESCRIPT_DLL_API Data operator-(const boost::python::object& left, const Data& right);
1502
1503 /**
1504 \brief
1505 Operator*
1506 Takes LHS python::object and RHS Data object.
1507 python::object must be convertable to Data type.
1508 */
1509 ESCRIPT_DLL_API Data operator*(const boost::python::object& left, const Data& right);
1510
1511 /**
1512 \brief
1513 Operator/
1514 Takes LHS python::object and RHS Data object.
1515 python::object must be convertable to Data type.
1516 */
1517 ESCRIPT_DLL_API Data operator/(const boost::python::object& left, const Data& right);
1518
1519 /**
1520 \brief
1521 Output operator
1522 */
1523 ESCRIPT_DLL_API std::ostream& operator<<(std::ostream& o, const Data& data);
1524
1525 /**
1526 \brief
1527 Return true if operands are equivalent, else return false.
1528 NB: this operator does very little at this point, and isn't to
1529 be relied on. Requires further implementation.
1530 */
1531 //ESCRIPT_DLL_API bool operator==(const Data& left, const Data& right);
1532
1533 /**
1534 \brief
1535 Perform the given binary operation with this and right as operands.
1536 Right is a Data object.
1537 */
1538 template <class BinaryFunction>
1539 inline
1540 void
1541 Data::binaryOp(const Data& right,
1542 BinaryFunction operation)
1543 {
1544 //
1545 // if this has a rank of zero promote it to the rank of the RHS
1546 if (getPointDataView().getRank()==0 && right.getPointDataView().getRank()!=0) {
1547 reshapeDataPoint(right.getPointDataView().getShape());
1548 }
1549 //
1550 // initially make the temporary a shallow copy
1551 Data tempRight(right);
1552 if (getFunctionSpace()!=right.getFunctionSpace()) {
1553 if (right.probeInterpolation(getFunctionSpace())) {
1554 //
1555 // an interpolation is required so create a new Data
1556 tempRight=Data(right,this->getFunctionSpace());
1557 } else if (probeInterpolation(right.getFunctionSpace())) {
1558 //
1559 // interpolate onto the RHS function space
1560 Data tempLeft(*this,right.getFunctionSpace());
1561 m_data=tempLeft.m_data;
1562 }
1563 }
1564 operandCheck(tempRight);
1565 //
1566 // ensure this has the right type for the RHS
1567 typeMatchRight(tempRight);
1568 //
1569 // Need to cast to the concrete types so that the correct binaryOp
1570 // is called.
1571 if (isExpanded()) {
1572 //
1573 // Expanded data will be done in parallel, the right hand side can be
1574 // of any data type
1575 DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1576 EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1577 escript::binaryOp(*leftC,*(tempRight.m_data.get()),operation);
1578 } else if (isTagged()) {
1579 //
1580 // Tagged data is operated on serially, the right hand side can be
1581 // either DataConstant or DataTagged
1582 DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1583 EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1584 if (right.isTagged()) {
1585 DataTagged* rightC=dynamic_cast<DataTagged*>(tempRight.m_data.get());
1586 EsysAssert((rightC!=0), "Programming error - casting to DataTagged.");
1587 escript::binaryOp(*leftC,*rightC,operation);
1588 } else {
1589 DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());
1590 EsysAssert((rightC!=0), "Programming error - casting to DataConstant.");
1591 escript::binaryOp(*leftC,*rightC,operation);
1592 }
1593 } else if (isConstant()) {
1594 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1595 DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());
1596 EsysAssert((leftC!=0 && rightC!=0), "Programming error - casting to DataConstant.");
1597 escript::binaryOp(*leftC,*rightC,operation);
1598 }
1599 }
1600
1601 /**
1602 \brief
1603 Perform the given binary operation with this and right as operands.
1604 Right is a boost::python object.
1605 */
1606 template <class BinaryFunction>
1607 inline
1608 void
1609 Data::binaryOp(const boost::python::object& right,
1610 BinaryFunction operation)
1611 {
1612 DataArray temp(right);
1613 //
1614 // if this has a rank of zero promote it to the rank of the RHS.
1615 if (getPointDataView().getRank()==0 && temp.getView().getRank()!=0) {
1616 reshapeDataPoint(temp.getView().getShape());
1617 }
1618 //
1619 // Always allow scalar values for the RHS but check other shapes
1620 if (temp.getView().getRank()!=0) {
1621 if (!getPointDataView().checkShape(temp.getView().getShape())) {
1622 throw DataException(getPointDataView().createShapeErrorMessage(
1623 "Error - RHS shape doesn't match LHS shape.",temp.getView().getShape()));
1624 }
1625 }
1626 if (isExpanded()) {
1627 DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1628 EsysAssert((leftC!=0),"Programming error - casting to DataExpanded.");
1629 escript::binaryOp(*leftC,temp.getView(),operation);
1630 } else if (isTagged()) {
1631 DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1632 EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1633 escript::binaryOp(*leftC,temp.getView(),operation);
1634 } else if (isConstant()) {
1635 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1636 EsysAssert((leftC!=0),"Programming error - casting to DataConstant.");
1637 escript::binaryOp(*leftC,temp.getView(),operation);
1638 }
1639 }
1640
1641 /**
1642 \brief
1643 Perform the given unary operation on other and return the result.
1644 Given operation is performed on each element of each data point, thus
1645 argument object is a rank n Data object, and returned object is a rank n
1646 Data object.
1647 Calls Data::unaryOp.
1648 */
1649 template <class UnaryFunction>
1650 inline
1651 Data
1652 unaryOp(const Data& other,
1653 UnaryFunction operation)
1654 {
1655 Data result;
1656 result.copy(other);
1657 result.unaryOp(operation);
1658 return result;
1659 }
1660
1661 /**
1662 \brief
1663 Perform the given unary operation on this.
1664 Given operation is performed on each element of each data point.
1665 Calls escript::unaryOp.
1666 */
1667 template <class UnaryFunction>
1668 inline
1669 void
1670 Data::unaryOp(UnaryFunction operation)
1671 {
1672 if (isExpanded()) {
1673 DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1674 EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1675 escript::unaryOp(*leftC,operation);
1676 } else if (isTagged()) {
1677 DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1678 EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1679 escript::unaryOp(*leftC,operation);
1680 } else if (isConstant()) {
1681 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1682 EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1683 escript::unaryOp(*leftC,operation);
1684 }
1685 }
1686
1687 /**
1688 \brief
1689 Perform the given Data object reduction algorithm on this and return the result.
1690 Given operation combines each element of each data point, thus argument
1691 object (*this) is a rank n Data object, and returned object is a scalar.
1692 Calls escript::algorithm.
1693 */
1694 template <class BinaryFunction>
1695 inline
1696 double
1697 Data::algorithm(BinaryFunction operation, double initial_value) const
1698 {
1699 if (isExpanded()) {
1700 DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1701 EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1702 return escript::algorithm(*leftC,operation,initial_value);
1703 } else if (isTagged()) {
1704 DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1705 EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1706 return escript::algorithm(*leftC,operation,initial_value);
1707 } else if (isConstant()) {
1708 DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1709 EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1710 return escript::algorithm(*leftC,operation,initial_value);
1711 }
1712 return 0;
1713 }
1714
1715 /**
1716 \brief
1717 Perform the given data point reduction algorithm on data and return the result.
1718 Given operation combines each element within each data point into a scalar,
1719 thus argument object is a rank n Data object, and returned object is a
1720 rank 0 Data object.
1721 Calls escript::dp_algorithm.
1722 */
1723 template <class BinaryFunction>
1724 inline
1725 Data
1726 Data::dp_algorithm(BinaryFunction operation, double initial_value) const
1727 {
1728 if (isExpanded()) {
1729 Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());
1730 DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
1731 DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
1732 EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
1733 EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
1734 escript::dp_algorithm(*dataE,*resultE,operation,initial_value);
1735 return result;
1736 } else if (isTagged()) {
1737 DataTagged* dataT=dynamic_cast<DataTagged*>(m_data.get());
1738 DataArrayView::ShapeType viewShape;
1739 DataArrayView::ValueType viewData(1);
1740 viewData[0]=0;
1741 DataArrayView defaultValue(viewData,viewShape);
1742 DataTagged::TagListType keys;
1743 DataTagged::ValueListType values;
1744 DataTagged::DataMapType::const_iterator i;
1745 for (i=dataT->getTagLookup().begin();i!=dataT->getTagLookup().end();i++) {
1746 keys.push_back(i->first);
1747 values.push_back(defaultValue);
1748 }
1749 Data result(keys,values,defaultValue,getFunctionSpace());
1750 DataTagged* resultT=dynamic_cast<DataTagged*>(result.m_data.get());
1751 EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
1752 EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");
1753 escript::dp_algorithm(*dataT,*resultT,operation,initial_value);
1754 return result;
1755 } else if (isConstant()) {
1756 Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());
1757 DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
1758 DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
1759 EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
1760 EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");
1761 escript::dp_algorithm(*dataC,*resultC,operation,initial_value);
1762 return result;
1763 }
1764 Data falseRetVal; // to keep compiler quiet
1765 return falseRetVal;
1766 }
1767
1768 }
1769 #endif

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26