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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26