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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1137 - (show annotations)
Thu May 10 08:11:31 2007 UTC (12 years, 5 months ago) by gross
Original Path: trunk/escript/src/Data.h
File MIME type: text/plain
File size: 41423 byte(s)
This version passes the tests on windows except for 

   * vtk
   * netCDF

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26