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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26