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

Annotation of /branches/schroedinger/escript/src/Data.h

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26