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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 783 - (show annotations)
Tue Jul 18 01:32:50 2006 UTC (13 years ago) by gross
File MIME type: text/plain
File size: 42897 byte(s)
coordinates, element size and normals returned by corresponding
FunctionSpace mesthods are now protected against updates. So 
+=, -=, *=, /=, setTaggedValue, fillFromNumArray will through an
excpetion.

The FunctionSpace class does nut buffer the oordinates, element size and
normals yet.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26