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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26