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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 783 - (hide annotations)
Tue Jul 18 01:32:50 2006 UTC (13 years, 4 months ago) by gross
File MIME type: text/plain
File size: 42897 byte(s)
coordinates, element size and normals returned by corresponding
FunctionSpace mesthods are now protected against updates. So 
+=, -=, *=, /=, setTaggedValue, fillFromNumArray will through an
excpetion.

The FunctionSpace class does nut buffer the oordinates, element size and
normals yet.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26