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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 950 - (show annotations)
Tue Feb 6 07:01:11 2007 UTC (12 years, 1 month ago) by gross
File MIME type: text/plain
File size: 42415 byte(s)
escript data objects can now be saved to netCDF files, see http://www.unidata.ucar.edu/software/netcdf/.
Currently only constant data are implemented with expanded and tagged data to follow.
There are two new functions to dump a data object

   s=Data(...)
   s.dump(<filename>)

and to recover it

   s=load(<filename>, domain)

Notice that the function space of s is recovered but domain is still need. 

dump and load will replace archive and extract.

The installation needs now the netCDF installed. 


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26