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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 921 - (show annotations)
Fri Jan 5 00:54:37 2007 UTC (12 years, 3 months ago) by gross
File MIME type: text/plain
File size: 42013 byte(s)
I have done some clarification on functions that allow to access individual data point values in a Data object. 
The term "data point number" is always local on a MPI process and referes to the value (data_point_in_sample, sample)
as a single identifyer (data_point_in_sample + sample * number_data_points_per_sample). a "global data point number"
referes to a tuple of a processour id and local data point number.

The function convertToNumArrayFromSampleNo has been removed now and convertToNumArrayFromDPNo renamed to getValueOfDataPoint.
There are two new functions:

   getNumberOfDataPoints
   setValueOfDataPoint

This allows you to do things like:

  in=Data(..)
  out=Data(..)
   for i in xrange(in.getNumberOfDataPoints())
       in_loc=in.getValueOfDataPoint(i)
       out_loc=< some operations on in_loc>
       out.setValueOfDataPoint(i,out_loc)


Also mindp  is renamed to  minGlobalDataPoint and there is a new function getValueOfGlobalDataPoint. While in MPI the functions getNumberOfDataPoints and getValueOfDataPoint are working locally on each process (so the code above is executed in parallel).
the latter allows getting a single value across all processors. 


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26