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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 775 - (show annotations)
Mon Jul 10 04:00:08 2006 UTC (12 years, 11 months ago) by ksteube
File MIME type: text/plain
File size: 41834 byte(s)
Modified the following python methods in escript/py_src/util.py to
call faster C++ methods:
	escript_trace
	escript_transpose
	escript_symmetric
	escript_nonsymmetric

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26