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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26