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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/escript/src/Data/Data.h revision 94 by jgs, Wed Oct 27 00:45:54 2004 UTC trunk/escript/src/Data.h revision 480 by jgs, Wed Feb 1 05:15:12 2006 UTC
# Line 1  Line 1 
1  // $Id$  // $Id$
2  /*=============================================================================  /*
   
3   ******************************************************************************   ******************************************************************************
4   *                                                                            *   *                                                                            *
5   *       COPYRIGHT ACcESS 2004 -  All Rights Reserved                         *   *       COPYRIGHT ACcESS 2004 -  All Rights Reserved                         *
# Line 12  Line 11 
11   * person has a software license agreement with ACcESS.                       *   * person has a software license agreement with ACcESS.                       *
12   *                                                                            *   *                                                                            *
13   ******************************************************************************   ******************************************************************************
14    */
15    
16  ******************************************************************************/  /** \file Data.h */
17    
18  #ifndef DATA_H  #ifndef DATA_H
19  #define DATA_H  #define DATA_H
20    
21  #include "escript/Data/DataAbstract.h"  #include "DataAbstract.h"
22  #include "escript/Data/DataTagged.h"  #include "DataAlgorithm.h"
23  #include "escript/Data/FunctionSpace.h"  #include "FunctionSpace.h"
24  #include "escript/Data/BinaryOp.h"  #include "BinaryOp.h"
25  #include "escript/Data/UnaryOp.h"  #include "UnaryOp.h"
26    #include "DataException.h"
27    
28  extern "C" {  extern "C" {
29  #include "escript/Data/DataC.h"  #include "DataC.h"
30  }  }
31    
 #include <iostream>  
32  #include <string>  #include <string>
 #include <memory>  
33  #include <algorithm>  #include <algorithm>
34    
35  #include <boost/shared_ptr.hpp>  #include <boost/shared_ptr.hpp>
36  #include <boost/python/object.hpp>  #include <boost/python/object.hpp>
 #include <boost/python/list.hpp>  
37  #include <boost/python/tuple.hpp>  #include <boost/python/tuple.hpp>
38  #include <boost/python/numeric.hpp>  #include <boost/python/numeric.hpp>
39    
40    namespace escript {
41    
42    //
43    // Forward declaration for various implementations of Data.
44    class DataConstant;
45    class DataTagged;
46    class DataExpanded;
47    
48  /**  /**
49     \brief     \brief
50     Data is essentially a factory class which creates the appropriate Data     Data creates the appropriate Data object for the given construction
51     object for the given construction arguments. It retains control over     arguments.
    the object created for the lifetime of the object.  
    The type of Data object referred to may change during the lifetime of  
    the Data object.  
52    
53     Description:     Description:
54     Data is essentially a factory class which creates the appropriate Data     Data is essentially a factory class which creates the appropriate Data
# Line 54  extern "C" { Line 57  extern "C" {
57     The type of Data object referred to may change during the lifetime of     The type of Data object referred to may change during the lifetime of
58     the Data object.     the Data object.
59  */  */
   
 namespace escript {  
   
 //  
 // Forward declaration for various implimentations of Data.  
 class DataEmpty;  
 class DataConstant;  
 class DataTagged;  
 class DataExpanded;  
   
60  class Data {  class Data {
61    
62    public:    public:
63    
64      // These typedefs allow function names to be cast to pointers
65      // to functions of the appropriate type when calling unaryOp etc.
66    typedef double (*UnaryDFunPtr)(double);    typedef double (*UnaryDFunPtr)(double);
67    typedef double (*BinaryDFunPtr)(double,double);    typedef double (*BinaryDFunPtr)(double,double);
68    
69    /**    /**
70         Constructors.
71      */
72    
73      /**
74       \brief       \brief
75       Default constructor.       Default constructor.
76       Creates a DataEmpty object.       Creates a DataEmpty object.
# Line 88  class Data { Line 87  class Data {
87    /**    /**
88       \brief       \brief
89       Constructor from another Data object. If "what" is different from the       Constructor from another Data object. If "what" is different from the
90       function space of inData the inData are tried to be interpolated to what       function space of inData the inData are tried to be interpolated to what,
91       otherwise a shallow copy of inData is returned.       otherwise a shallow copy of inData is returned.
92    */    */
93    Data(const Data& inData,    Data(const Data& inData,
# Line 202  class Data { Line 201  class Data {
201         const boost::python::tuple& shape=boost::python::make_tuple(),         const boost::python::tuple& shape=boost::python::make_tuple(),
202         const FunctionSpace& what=FunctionSpace(),         const FunctionSpace& what=FunctionSpace(),
203         bool expanded=false);         bool expanded=false);
   
204    /**    /**
205       \brief       \brief
206       Perform the specified algorithm on the Data and return result.       Destructor
207    */    */
208    template <class UnaryFunction>    ~Data();
   inline double  
   algorithm(UnaryFunction operation) const;  
209    
210    /**    /**
211       \brief       \brief
212       Perform the given unary operation on all of the data's elements.       Perform a deep copy.
213    */    */
   template <class UnaryFunction>  
214    void    void
215    unaryOp(UnaryFunction operation);    copy(const Data& other);
216    
217    /**    /**
218       \brief       Member access methods.
      Perform the given binary operation on all of the data's elements.  
      The underlying type of the right hand side (right) determines the final  
      type of *this after the operation. For example if the right hand side  
      is expanded *this will be expanded if necessary.  
219    */    */
   template <class BinaryFunction>  
   void  
   binaryOp(const Data& right,  
            BinaryFunction operation);  
220    
221    /**    /**
222       \brief       \brief
223       Perform the given binary operation on all of the data's elements.       Return the values of all data-points as a single python numarray object.
224    */    */
225    template <class BinaryFunction>    const boost::python::numeric::array
226    void    convertToNumArray();
   binaryOp(const boost::python::object& right,  
            BinaryFunction operation);  
227    
228    /**    /**
229       \brief       \brief
230       Overloaded operator +=       Return the values of all data-points for the given sample as a single python numarray object.
      \param right - Input - The right hand side.  
231    */    */
232    Data& operator+=(const Data& right);    const boost::python::numeric::array
233    Data& operator+=(const boost::python::object& right);    convertToNumArrayFromSampleNo(int sampleNo);
234    
235    /**    /**
236       \brief       \brief
237       Overloaded operator -=       Return the value of the specified data-point as a single python numarray object.
      \param right - Input - The right hand side.  
238    */    */
239    Data& operator-=(const Data& right);    const boost::python::numeric::array
240    Data& operator-=(const boost::python::object& right);    convertToNumArrayFromDPNo(int sampleNo,
241                                int dataPointNo);
  /**  
      \brief  
      Overloaded operator *=  
      \param right - Input - The right hand side.  
   */  
   Data& operator*=(const Data& right);  
   Data& operator*=(const boost::python::object& right);  
242    
243   /**    /**
244       \brief       \brief
245       Overloaded operator /=       Fills the expanded Data object from values of a python numarray object.
      \param right - Input - The right hand side.  
246    */    */
247    Data& operator/=(const Data& right);    void
248    Data& operator/=(const boost::python::object& right);    fillFromNumArray(const boost::python::numeric::array);
249    
250    /**    /**
251       \brief       \brief
252       Return the power of Data.       Return the tag number associated with the given data-point.
253    
254         The data-point number here corresponds to the data-point number in the
255         numarray returned by convertToNumArray.
256    */    */
257    Data powD(const Data& right) const;    int
258    Data powO(const boost::python::object& right) const;    getTagNumber(int dpno);
259    
260    /**    /**
261       \brief       \brief
262       Return the C wrapper for the Data object.       Return the C wrapper for the Data object.
263    */    */
264    escriptDataC getDataC();    escriptDataC
265      getDataC();
266    
267    /**    /**
268       \brief       \brief
269       Return the C wrapper for the Data object - const version.       Return the C wrapper for the Data object - const version.
270    */    */
271    escriptDataC getDataC() const;    escriptDataC
272      getDataC() const;
273    
274    /**    /**
275       \brief       \brief
276       Write the data as a string.       Write the data as a string.
277    */    */
278    std::string toString() const;    inline
279      std::string
280      toString() const
281      {
282        return m_data->toString();
283      }
284    
285    /**    /**
286       \brief       \brief
# Line 312  class Data { Line 297  class Data {
297    
298    /**    /**
299       \brief       \brief
300       Perform a deep copy.       Whatever the current Data type make this into a DataExpanded.
   */  
   void  
   copy(const Data& other);  
   
   /**  
     \brief  
     Copy other Data object into this Data object where mask is positive.  
   */  
   void  
   copyWithMask(const Data& other,  
                const Data& mask);  
   
   /**  
      \brief  
      Whatever the current Data type make it expanded.  
301    */    */
302    void    void
303    expand();    expand();
304    
305    /**    /**
306       \brief       \brief
307       If possible convert the Data type to tagged. This will only allow       If possible convert this Data to DataTagged. This will only allow
308       Constant data to be converted to tagged. An attempt to convert       Constant data to be converted to tagged. An attempt to convert
309       Expanded data to tagged will throw an exception.       Expanded data to tagged will throw an exception.
310    */    */
# Line 357  class Data { Line 327  class Data {
327    
328    /**    /**
329       \brief       \brief
330       Return true if this Data is empty.       Return true if this Data is constant.
331    */    */
332    bool    bool
333    isEmpty() const;    isConstant() const;
334    
335    /**    /**
336       \brief       \brief
337       Return true if this Data is constant.       Return true if this Data is empty.
338    */    */
339    bool    bool
340    isConstant() const;    isEmpty() const;
341    
342    /**    /**
343       \brief       \brief
# Line 418  class Data { Line 388  class Data {
388    
389    /**    /**
390       \brief       \brief
391       Return the number of data points per sample.       Return the number of samples.
392    */    */
393    inline    inline
394    int    int
395    getNumDataPointsPerSample() const    getNumSamples() const
396    {    {
397      return m_data->getNumDPPSample();      return m_data->getNumSamples();
398    }    }
399    
400    /**    /**
401       \brief       \brief
402       Return the number of samples.       Return the number of data points per sample.
403    */    */
404      inline
405    int    int
406    getNumSamples() const;    getNumDataPointsPerSample() const
407      {
408    /**      return m_data->getNumDPPSample();
409       \brief    }
      Check *this and the right operand are compatible. Throws  
      an exception if they aren't.  
      \param right - Input - The right hand side.  
   */  
   void  
   operandCheck(const Data& right) const;  
410    
411    /**    /**
412       \brief       \brief
# Line 449  class Data { Line 414  class Data {
414       preferred interface but is provided for use by C code.       preferred interface but is provided for use by C code.
415       \param sampleNo - Input - the given sample no.       \param sampleNo - Input - the given sample no.
416    */    */
417      inline
418    DataAbstract::ValueType::value_type*    DataAbstract::ValueType::value_type*
419    getSampleData(DataAbstract::ShapeType::size_type sampleNo);    getSampleData(DataAbstract::ValueType::size_type sampleNo)
420      {
421        return m_data->getSampleData(sampleNo);
422      }
423    
424    /**    /**
425       \brief       \brief
# Line 458  class Data { Line 427  class Data {
427       access data that isn't tagged an exception will be thrown.       access data that isn't tagged an exception will be thrown.
428       \param tag - Input - the tag key.       \param tag - Input - the tag key.
429    */    */
430      inline
431    DataAbstract::ValueType::value_type*    DataAbstract::ValueType::value_type*
432    getSampleDataByTag(int tag);    getSampleDataByTag(int tag)
433      {
434        return m_data->getSampleDataByTag(tag);
435      }
436    
437      /**
438         \brief
439         Assign the given value to the data-points referenced by the given
440         reference number.
441    
442         The value supplied is a python numarray object.  The data from this numarray
443         is unpacked into a DataArray, and this is used to set the corresponding
444         data-points in the underlying Data object.
445    
446         If the underlying Data object cannot be accessed via reference numbers, an
447         exception will be thrown.
448    
449         \param ref - Input - reference number.
450         \param value - Input - value to assign to data-points associated with
451                                the given reference number.
452      */
453      void
454      setRefValue(int ref,
455                  const boost::python::numeric::array& value);
456    
457      /**
458         \brief
459         Return the values associated with the data-points referenced by the given
460         reference number.
461    
462         The value supplied is a python numarray object. The data from the corresponding
463         data-points in this Data object are packed into the given numarray object.
464    
465         If the underlying Data object cannot be accessed via reference numbers, an
466         exception will be thrown.
467    
468         \param ref - Input - reference number.
469         \param value - Output - object to receive values from data-points
470                                 associated with the given reference number.
471      */
472      void
473      getRefValue(int ref,
474                  boost::python::numeric::array& value);
475    
476    /**    /**
477       \brief       \brief
# Line 486  class Data { Line 498  class Data {
498    
499    /**    /**
500       \brief       \brief
501       Return data point shape as a tuple of integers:       Return the data point shape as a tuple of integers.
502    */    */
503    boost::python::tuple    const boost::python::tuple
504    getShapeTuple() const;    getShapeTuple() const;
505    
506    /**    /**
# Line 501  class Data { Line 513  class Data {
513    
514    /**    /**
515       \brief       \brief
516       Return the number of doubles stored for Data.       Return the number of doubles stored for this Data.
517    */    */
518    DataArrayView::ValueType::size_type    DataArrayView::ValueType::size_type
519    getLength() const;    getLength() const;
520    
521    /**    /**
522       \brief       \brief
523       Interpolates this onto the given functionspace and returns the result as a Data object.       Assign the given value to the tag. Implicitly converts this
524         object to type DataTagged. Throws an exception if this object
525         cannot be converted to a DataTagged object.
526         \param tagKey - Input - Integer key.
527         \param value - Input - Value to associate with given key.
528      */
529      void
530      setTaggedValue(int tagKey,
531                     const boost::python::object& value);
532    
533      /**
534         \brief
535         Assign the given value to the tag. Implicitly converts this
536         object to type DataTagged. Throws an exception if this object
537         cannot be converted to a DataTagged object.
538         \param tagKey - Input - Integer key.
539         \param value - Input - Value to associate with given key.
540      */
541      void
542      setTaggedValueFromCPP(int tagKey,
543                            const DataArrayView& value);
544    
545      /**
546        \brief
547        Copy other Data object into this Data object where mask is positive.
548      */
549      void
550      copyWithMask(const Data& other,
551                   const Data& mask);
552    
553      /**
554         Data object operation methods and operators.
555      */
556    
557      /**
558         \brief
559         Interpolates this onto the given functionspace and returns
560         the result as a Data object.
561         *
562    */    */
563    Data    Data
564    interpolate(const FunctionSpace& functionspace) const;    interpolate(const FunctionSpace& functionspace) const;
# Line 517  class Data { Line 567  class Data {
567       \brief       \brief
568       Calculates the gradient of the data at the data points of functionspace.       Calculates the gradient of the data at the data points of functionspace.
569       If functionspace is not present the function space of Function(getDomain()) is used.       If functionspace is not present the function space of Function(getDomain()) is used.
570         *
571    */    */
572    Data    Data
573    gradOn(const FunctionSpace& functionspace) const;    gradOn(const FunctionSpace& functionspace) const;
# Line 527  class Data { Line 578  class Data {
578    /**    /**
579       \brief       \brief
580       Calculate the integral over the function space domain.       Calculate the integral over the function space domain.
581         *
582    */    */
583    boost::python::numeric::array    boost::python::numeric::array
584    integrate() const;    integrate() const;
# Line 534  class Data { Line 586  class Data {
586    /**    /**
587       \brief       \brief
588       Return a Data with a 1 for +ive values and a 0 for 0 or -ive values.       Return a Data with a 1 for +ive values and a 0 for 0 or -ive values.
589         *
590    */    */
591    Data    Data
592    wherePositive() const;    wherePositive() const;
593    
594    /**    /**
595       \brief       \brief
596       Return a Data with a 1 for +ive values and a 0 for -ive values.       Return a Data with a 1 for -ive values and a 0 for +ive or 0 values.
597         *
598      */
599      Data
600      whereNegative() const;
601    
602      /**
603         \brief
604         Return a Data with a 1 for +ive or 0 values and a 0 for -ive values.
605         *
606    */    */
607    Data    Data
608    whereNonNegative() const;    whereNonNegative() const;
609    
610    /**    /**
611       \brief       \brief
612       Return a Data with a 1 for -ive values and a 0 for +ive or 0 values.       Return a Data with a 1 for -ive or 0 values and a 0 for +ive values.
613         *
614    */    */
615    Data    Data
616    whereNegative() const;    whereNonPositive() const;
617    
618    /**    /**
619       \brief       \brief
620       Return a Data with a 1 for 0 values a 0 for +ive or -ive.       Return a Data with a 1 for 0 values and a 0 for +ive or -ive values.
621         *
622    */    */
623    Data    Data
624    whereZero() const;    whereZero() const;
625    
626    /**    /**
627       \brief       \brief
628       Return the sin of Data.       Return a Data with a 0 for 0 values and a 1 for +ive or -ive values.
629         *
630      */
631      Data
632      whereNonZero() const;
633    
634      /**
635         \brief
636         Return the maximum absolute value of this Data object.
637         *
638      */
639      double
640      Lsup() const;
641    
642      /**
643         \brief
644         Return the minimum absolute value of this Data object.
645         *
646      */
647      double
648      Linf() const;
649    
650      /**
651         \brief
652         Return the maximum value of this Data object.
653         *
654      */
655      double
656      sup() const;
657    
658      /**
659         \brief
660         Return the minimum value of this Data object.
661         *
662      */
663      double
664      inf() const;
665    
666      /**
667         \brief
668         Return the absolute value of each data point of this Data object.
669         *
670      */
671      Data
672      abs() const;
673    
674      /**
675         \brief
676         Return the maximum value of each data point of this Data object.
677         *
678      */
679      Data
680      maxval() const;
681    
682      /**
683         \brief
684         Return the minimum value of each data point of this Data object.
685         *
686      */
687      Data
688      minval() const;
689    
690      /**
691         \brief
692         Return the (sample number, data-point number) of the data point with
693         the minimum value in this Data object.
694      */
695      const boost::python::tuple
696      mindp() const;
697    
698      void
699      calc_mindp(int& SampleNo,
700                 int& DataPointNo) const;
701    
702      /**
703         \brief
704         Return the sign of each data point of this Data object.
705         -1 for negative values, zero for zero values, 1 for positive values.
706         *
707      */
708      Data
709      sign() const;
710    
711      /**
712         \brief
713         Transpose each data point of this Data object around the given axis.
714         --* not implemented yet *--
715         *
716      */
717      Data
718      transpose(int axis) const;
719    
720      /**
721         \brief
722         Calculate the trace of each data point of this Data object.
723         sum(A[i,i,i,i])
724         *
725      */
726      Data
727      trace() const;
728    
729      /**
730         \brief
731         Return the sin of each data point of this Data object.
732         *
733    */    */
734    Data    Data
735    sin() const;    sin() const;
736    
737    /**    /**
738       \brief       \brief
739       Return the cos of Data.       Return the cos of each data point of this Data object.
740         *
741    */    */
742    Data    Data
743    cos() const;    cos() const;
744    
745    /**    /**
746       \brief       \brief
747       Return the tan of Data.       Return the tan of each data point of this Data object.
748         *
749    */    */
750    Data    Data
751    tan() const;    tan() const;
752    
753    /**    /**
754       \brief       \brief
755       Return the log to base 10 of Data.       Return the asin of each data point of this Data object.
756         *
757    */    */
758    Data    Data
759    log() const;    asin() const;
760    
761    /**    /**
762       \brief       \brief
763       Return the natural log of Data.       Return the acos of each data point of this Data object.
764         *
765    */    */
766    Data    Data
767    ln() const;    acos() const;
768    
769    /**    /**
770       \brief       \brief
771       Return a Data containing a slice of this Data.       Return the atan of each data point of this Data object.
772         *
773    */    */
774    Data    Data
775    getSlice(const DataArrayView::RegionType& region) const;    atan() const;
776    
777    /**    /**
778       \brief       \brief
779       Copy the specified region from the given value.       Return the sinh of each data point of this Data object.
780       \param value - Input - Data to copy from.       *
      \param region - Input - Region to copy.  
781    */    */
782    void    Data
783    setSlice(const Data& value,    sinh() const;
            const DataArrayView::RegionType& region);  
784    
785    /**    /**
786       \brief       \brief
787       Return the maximum absolute value.       Return the cosh of each data point of this Data object.
788         *
789    */    */
790    double    Data
791    Lsup() const;    cosh() const;
792    
793    /**    /**
794       \brief       \brief
795       Return the maximum value.       Return the tanh of each data point of this Data object.
796         *
797    */    */
798    double    Data
799    sup() const;    tanh() const;
800    
801    /**    /**
802       \brief       \brief
803       Return the minimum value.       Return the asinh of each data point of this Data object.
804         *
805    */    */
806    double    Data
807    inf() const;    asinh() const;
808    
809    /**    /**
810       \brief       \brief
811       Returns a slice from this.       Return the acosh of each data point of this Data object.
812         *
813    */    */
814    Data    Data
815    getItem(const boost::python::object& key) const;    acosh() const;
816    
817    /**    /**
818       \brief       \brief
819       Copies slice from value into this.       Return the atanh of each data point of this Data object.
820         *
821      */
822      Data
823      atanh() const;
824    
825      /**
826         \brief
827         Return the log to base 10 of each data point of this Data object.
828         *
829      */
830      Data
831      log10() const;
832    
833      /**
834         \brief
835         Return the natural log of each data point of this Data object.
836         *
837      */
838      Data
839      log() const;
840    
841      /**
842         \brief
843         Return the exponential function of each data point of this Data object.
844         *
845      */
846      Data
847      exp() const;
848    
849      /**
850         \brief
851         Return the square root of each data point of this Data object.
852         *
853      */
854      Data
855      sqrt() const;
856    
857      /**
858         \brief
859         Return the negation of each data point of this Data object.
860         *
861      */
862      Data
863      neg() const;
864    
865      /**
866         \brief
867         Return the identity of each data point of this Data object.
868         Simply returns this object unmodified.
869         *
870      */
871      Data
872      pos() const;
873    
874      /**
875         \brief
876         Return the given power of each data point of this Data object.
877    
878         \param right Input - the power to raise the object to.
879         *
880      */
881      Data
882      powD(const Data& right) const;
883    
884      /**
885         \brief
886         Return the given power of each data point of this boost python object.
887        
888         \param right Input - the power to raise the object to.
889         *
890       */
891      Data
892      powO(const boost::python::object& right) const;
893    
894      /**
895         \brief
896         writes the object to a file in the DX file format
897    */    */
898    void    void
899    setItem(const boost::python::object& key,    saveDX(std::string fileName) const;
           const Data& value);  
900    
901    /**    /**
902       \brief       \brief
903       Convert the underlying data type to match the RHS.       writes the object to a file in the VTK file format
      \param right - Input - data type to match.  
904    */    */
905    void    void
906    typeMatch(const Data& right);    saveVTK(std::string fileName) const;
907    
908      /**
909         \brief
910         Overloaded operator +=
911         \param right - Input - The right hand side.
912         *
913      */
914      Data& operator+=(const Data& right);
915      Data& operator+=(const boost::python::object& right);
916    
917      /**
918         \brief
919         Overloaded operator -=
920         \param right - Input - The right hand side.
921         *
922      */
923      Data& operator-=(const Data& right);
924      Data& operator-=(const boost::python::object& right);
925    
926     /**
927         \brief
928         Overloaded operator *=
929         \param right - Input - The right hand side.
930         *
931      */
932      Data& operator*=(const Data& right);
933      Data& operator*=(const boost::python::object& right);
934    
935     /**
936         \brief
937         Overloaded operator /=
938         \param right - Input - The right hand side.
939         *
940      */
941      Data& operator/=(const Data& right);
942      Data& operator/=(const boost::python::object& right);
943    
944    /**    /**
945       \brief       \brief
# Line 663  class Data { Line 949  class Data {
949    probeInterpolation(const FunctionSpace& functionspace) const;    probeInterpolation(const FunctionSpace& functionspace) const;
950    
951    /**    /**
952         Data object slicing methods.
953      */
954    
955      /**
956       \brief       \brief
957       Assign the given value to the tag. Implicitly converts this       Returns a slice from this Data object.
958       object to type DataTagged. Throws an exception if this object  
959       cannot be converted to a DataTagged object.       /description
960       \param tagKey - Input - Integer key.       Implements the [] get operator in python.
961       \param value - Input - Value to associate with given key.       Calls getSlice.
962    
963         \param key - Input - python slice tuple specifying
964         slice to return.
965      */
966      Data
967      getItem(const boost::python::object& key) const;
968    
969      /**
970         \brief
971         Copies slice from value into this Data object.
972    
973         Implements the [] set operator in python.
974         Calls setSlice.
975    
976         \param key - Input - python slice tuple specifying
977         slice to copy from value.
978         \param value - Input - Data object to copy from.
979    */    */
980    void    void
981    setTaggedValue(int tagKey,    setItemD(const boost::python::object& key,
982                   const boost::python::object& value);             const Data& value);
983    
984      void
985      setItemO(const boost::python::object& key,
986               const boost::python::object& value);
987    
988      // These following public methods should be treated as private.
989    
990    /**    /**
991       \brief       \brief
992       Assign the given value to the tag. Implicitly converts this       Perform the given unary operation on every element of every data point in
993       object to type DataTagged. Throws an exception if this object       this Data object.
994       cannot be converted to a DataTagged object.    */
995       \param tagKey - Input - Integer key.    template <class UnaryFunction>
996       \param value - Input - Value to associate with given key.    inline
997       Note: removed for now - this version not needed, and breaks escript.cpp    void
998      unaryOp(UnaryFunction operation);
999    
1000      /**
1001         \brief
1002         Return a Data object containing the specified slice of
1003         this Data object.
1004         \param region - Input - Region to copy.
1005         *
1006      */
1007      Data
1008      getSlice(const DataArrayView::RegionType& region) const;
1009    
1010      /**
1011         \brief
1012         Copy the specified slice from the given value into this
1013         Data object.
1014         \param value - Input - Data to copy from.
1015         \param region - Input - Region to copy.
1016         *
1017    */    */
   /*  
1018    void    void
1019    setTaggedValue(int tagKey,    setSlice(const Data& value,
1020                   const DataArrayView& value);             const DataArrayView::RegionType& region);
1021    
1022      /**
1023         \brief
1024         Archive the current Data object to the given file.
1025         \param fileName - Input - file to archive to.
1026      */
1027      void
1028      archiveData(const std::string fileName);
1029    
1030      /**
1031         \brief
1032         Extract the Data object archived in the given file, overwriting
1033         the current Data object.
1034         Note - the current object must be of type DataEmpty.
1035         \param fileName - Input - file to extract from.
1036         \param fspace - Input - a suitable FunctionSpace descibing the data.
1037    */    */
1038      void
1039      extractData(const std::string fileName,
1040                  const FunctionSpace& fspace);
1041    
1042     protected:
1043    
1044   private:   private:
1045    
1046    /**    /**
1047       \brief       \brief
1048         Check *this and the right operand are compatible. Throws
1049         an exception if they aren't.
1050         \param right - Input - The right hand side.
1051      */
1052      inline
1053      void
1054      operandCheck(const Data& right) const
1055      {
1056        return m_data->operandCheck(*(right.m_data.get()));
1057      }
1058    
1059      /**
1060         \brief
1061         Perform the specified reduction algorithm on every element of every data point in
1062         this Data object according to the given function and return the single value result.
1063      */
1064      template <class BinaryFunction>
1065      inline
1066      double
1067      algorithm(BinaryFunction operation,
1068                double initial_value) const;
1069    
1070      /**
1071         \brief
1072         Reduce each data-point in this Data object using the given operation. Return a Data
1073         object with the same number of data-points, but with each data-point containing only
1074         one value - the result of the reduction operation on the corresponding data-point in
1075         this Data object
1076      */
1077      template <class BinaryFunction>
1078      inline
1079      Data
1080      dp_algorithm(BinaryFunction operation,
1081                   double initial_value) const;
1082    
1083      /**
1084         \brief
1085         Perform the given binary operation on all of the data's elements.
1086         The underlying type of the right hand side (right) determines the final
1087         type of *this after the operation. For example if the right hand side
1088         is expanded *this will be expanded if necessary.
1089         RHS is a Data object.
1090      */
1091      template <class BinaryFunction>
1092      inline
1093      void
1094      binaryOp(const Data& right,
1095               BinaryFunction operation);
1096    
1097      /**
1098         \brief
1099         Perform the given binary operation on all of the data's elements.
1100         RHS is a boost::python object.
1101      */
1102      template <class BinaryFunction>
1103      inline
1104      void
1105      binaryOp(const boost::python::object& right,
1106               BinaryFunction operation);
1107    
1108      /**
1109         \brief
1110         Convert the data type of the RHS to match this.
1111         \param right - Input - data type to match.
1112      */
1113      void
1114      typeMatchLeft(Data& right) const;
1115    
1116      /**
1117         \brief
1118         Convert the data type of this to match the RHS.
1119         \param right - Input - data type to match.
1120      */
1121      void
1122      typeMatchRight(const Data& right);
1123    
1124      /**
1125         \brief
1126       Construct a Data object of the appropriate type.       Construct a Data object of the appropriate type.
1127    */    */
1128    template <class IValueType>    template <class IValueType>
# Line 712  class Data { Line 1142  class Data {
1142    reshapeDataPoint(const DataArrayView::ShapeType& shape);    reshapeDataPoint(const DataArrayView::ShapeType& shape);
1143    
1144    //    //
1145    // pointer to the actual data    // pointer to the actual data object
1146    boost::shared_ptr<DataAbstract> m_data;    boost::shared_ptr<DataAbstract> m_data;
1147    
1148      //
1149      // pointer to the internal profiling data
1150      struct profDataEntry *profData;
1151    
1152  };  };
1153    
1154  template <class IValueType>  template <class IValueType>
# Line 730  Data::initialise(const IValueType& value Line 1164  Data::initialise(const IValueType& value
1164    // within the shared_ptr constructor.    // within the shared_ptr constructor.
1165    if (expanded) {    if (expanded) {
1166      DataAbstract* temp=new DataExpanded(value,what);      DataAbstract* temp=new DataExpanded(value,what);
1167      m_data=boost::shared_ptr<DataAbstract>(temp);      boost::shared_ptr<DataAbstract> temp_data(temp);
1168        m_data=temp_data;
1169    } else {    } else {
1170      DataAbstract* temp=new DataConstant(value,what);      DataAbstract* temp=new DataConstant(value,what);
1171      m_data=boost::shared_ptr<DataAbstract>(temp);      boost::shared_ptr<DataAbstract> temp_data(temp);
1172        m_data=temp_data;
1173    }    }
1174  }  }
1175    
1176  inline  /**
1177  DataAbstract::ValueType::value_type*     Binary Data object operators.
1178  Data::getSampleData(DataAbstract::ValueType::size_type sampleNo)  */
 {  
   return m_data->getSampleData(sampleNo);  
 }  
   
 inline  
 DataAbstract::ValueType::value_type*  
 Data::getSampleDataByTag(int tag)  
 {  
   return m_data->getSampleDataByTag(tag);  
 }  
   
 inline  
 void  
 Data::operandCheck(const Data& right) const  
 {  
   return m_data->operandCheck(*(right.m_data.get()));  
 }  
   
 inline  
 int  
 Data::getNumSamples() const  
 {  
   return m_data->getNumSamples();  
 }  
   
 inline  
 std::string  
 Data::toString() const  
 {  
   return m_data->toString();  
 }  
1179    
1180  /**  /**
1181    \brief    \brief
# Line 876  std::ostream& operator<<(std::ostream& o Line 1281  std::ostream& operator<<(std::ostream& o
1281    NB: this operator does very little at this point, and isn't to    NB: this operator does very little at this point, and isn't to
1282    be relied on. Requires further implementation.    be relied on. Requires further implementation.
1283  */  */
1284  bool operator==(const Data& left, const Data& right);  //bool operator==(const Data& left, const Data& right);
1285    
1286    /**
1287      \brief
1288      Perform the given binary operation with this and right as operands.
1289      Right is a Data object.
1290    */
1291  template <class BinaryFunction>  template <class BinaryFunction>
1292  inline  inline
1293  void  void
# Line 907  Data::binaryOp(const Data& right, Line 1317  Data::binaryOp(const Data& right,
1317     operandCheck(tempRight);     operandCheck(tempRight);
1318     //     //
1319     // ensure this has the right type for the RHS     // ensure this has the right type for the RHS
1320     typeMatch(tempRight);     typeMatchRight(tempRight);
1321     //     //
1322     // Need to cast to the concrete types so that the correct binaryOp     // Need to cast to the concrete types so that the correct binaryOp
1323     // is called.     // is called.
# Line 933  Data::binaryOp(const Data& right, Line 1343  Data::binaryOp(const Data& right,
1343         EsysAssert((rightC!=0), "Programming error - casting to DataConstant.");         EsysAssert((rightC!=0), "Programming error - casting to DataConstant.");
1344         escript::binaryOp(*leftC,*rightC,operation);         escript::binaryOp(*leftC,*rightC,operation);
1345       }       }
1346     } else {     } else if (isConstant()) {
      //  
      // can only be DataConstant  
1347       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1348       DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());       DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());
1349       EsysAssert((leftC!=0 && rightC!=0), "Programming error - casting to DataConstant.");       EsysAssert((leftC!=0 && rightC!=0), "Programming error - casting to DataConstant.");
# Line 943  Data::binaryOp(const Data& right, Line 1351  Data::binaryOp(const Data& right,
1351     }     }
1352  }  }
1353    
1354    /**
1355      \brief
1356      Perform the given binary operation with this and right as operands.
1357      Right is a boost::python object.
1358    */
1359  template <class BinaryFunction>  template <class BinaryFunction>
1360  inline  inline
1361  void  void
# Line 963  Data::binaryOp(const boost::python::obje Line 1376  Data::binaryOp(const boost::python::obje
1376                    "Error - RHS shape doesn't match LHS shape.",temp.getView().getShape()));                    "Error - RHS shape doesn't match LHS shape.",temp.getView().getShape()));
1377       }       }
1378     }     }
   
1379     if (isExpanded()) {     if (isExpanded()) {
      //  
      // Expanded data will be done in parallel  
1380       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1381       EsysAssert((leftC!=0),"Programming error - casting to DataExpanded.");       EsysAssert((leftC!=0),"Programming error - casting to DataExpanded.");
1382       escript::binaryOp(*leftC,temp.getView(),operation);       escript::binaryOp(*leftC,temp.getView(),operation);
# Line 974  Data::binaryOp(const boost::python::obje Line 1384  Data::binaryOp(const boost::python::obje
1384       DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());       DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1385       EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");       EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1386       escript::binaryOp(*leftC,temp.getView(),operation);       escript::binaryOp(*leftC,temp.getView(),operation);
1387     } else {     } else if (isConstant()) {
      //  
      // can only be DataConstant  
1388       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1389       EsysAssert((leftC!=0),"Programming error - casting to DataConstant.");       EsysAssert((leftC!=0),"Programming error - casting to DataConstant.");
1390       escript::binaryOp(*leftC,temp.getView(),operation);       escript::binaryOp(*leftC,temp.getView(),operation);
1391     }     }
1392  }  }
1393    
1394    /**
1395      \brief
1396      Perform the given unary operation on other and return the result.
1397      Given operation is performed on each element of each data point, thus
1398      argument object is a rank n Data object, and returned object is a rank n
1399      Data object.
1400      Calls Data::unaryOp.
1401    */
1402    template <class UnaryFunction>
1403    inline
1404    Data
1405    unaryOp(const Data& other,
1406            UnaryFunction operation)
1407    {
1408      Data result;
1409      result.copy(other);
1410      result.unaryOp(operation);
1411      return result;
1412    }
1413    
1414    /**
1415      \brief
1416      Perform the given unary operation on this.
1417      Given operation is performed on each element of each data point.
1418      Calls escript::unaryOp.
1419    */
1420  template <class UnaryFunction>  template <class UnaryFunction>
1421  inline  inline
1422  void  void
1423  Data::unaryOp(UnaryFunction operation)  Data::unaryOp(UnaryFunction operation)
1424  {  {
1425    if (isExpanded()) {    if (isExpanded()) {
     //  
     // Expanded data will be done in parallel  
1426      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1427      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1428      escript::unaryOp(*leftC,operation);      escript::unaryOp(*leftC,operation);
# Line 998  Data::unaryOp(UnaryFunction operation) Line 1430  Data::unaryOp(UnaryFunction operation)
1430      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1431      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1432      escript::unaryOp(*leftC,operation);      escript::unaryOp(*leftC,operation);
1433    } else {    } else if (isConstant()) {
1434      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1435      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1436      escript::unaryOp(*leftC,operation);      escript::unaryOp(*leftC,operation);
1437    }    }
1438  }  }
1439    
1440  template <class UnaryFunction>  /**
1441      \brief
1442      Perform the given Data object reduction algorithm on this and return the result.
1443      Given operation combines each element of each data point, thus argument
1444      object (*this) is a rank n Data object, and returned object is a scalar.
1445      Calls escript::algorithm.
1446    */
1447    template <class BinaryFunction>
1448  inline  inline
1449  double  double
1450  Data::algorithm(UnaryFunction operation) const  Data::algorithm(BinaryFunction operation, double initial_value) const
1451  {  {
1452    if (isExpanded()) {    if (isExpanded()) {
     //  
     // Expanded data will be done in parallel  
1453      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1454      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1455      return escript::algorithm(*leftC,operation);      return escript::algorithm(*leftC,operation,initial_value);
1456    }    } else if (isTagged()) {
   else if (isTagged()) {  
1457      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1458      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1459      return escript::algorithm(*leftC,operation);      return escript::algorithm(*leftC,operation,initial_value);
1460    } else {    } else if (isConstant()) {
1461      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1462      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1463      return escript::algorithm(*leftC,operation);      return escript::algorithm(*leftC,operation,initial_value);
1464    }    }
1465      return 0;
1466  }  }
1467    
1468  template <class UnaryFunction>  /**
1469      \brief
1470      Perform the given data point reduction algorithm on data and return the result.
1471      Given operation combines each element within each data point into a scalar,
1472      thus argument object is a rank n Data object, and returned object is a
1473      rank 0 Data object.
1474      Calls escript::dp_algorithm.
1475    */
1476    template <class BinaryFunction>
1477  inline  inline
1478  Data  Data
1479  unaryOp(const Data& other,  Data::dp_algorithm(BinaryFunction operation, double initial_value) const
         UnaryFunction operation)  
1480  {  {
1481    //    Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());
1482    // Perform the given operation on a copy of the input data and return the result    if (isExpanded()) {
1483    Data result;      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
1484    //      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
1485    // perform a deep copy      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
1486    result.copy(other);      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
1487    result.unaryOp(operation);      escript::dp_algorithm(*dataE,*resultE,operation,initial_value);
1488      } else if (isTagged()) {
1489        DataTagged* dataT=dynamic_cast<DataTagged*>(m_data.get());
1490        DataTagged* resultT=dynamic_cast<DataTagged*>(result.m_data.get());
1491        EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
1492        EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");
1493        escript::dp_algorithm(*dataT,*resultT,operation,initial_value);
1494      } else if (isConstant()) {
1495        DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
1496        DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
1497        EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
1498        EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");
1499        escript::dp_algorithm(*dataC,*resultC,operation,initial_value);
1500      }
1501    return result;    return result;
1502  }  }
1503    
1504  }  }
   
1505  #endif  #endif

Legend:
Removed from v.94  
changed lines
  Added in v.480

  ViewVC Help
Powered by ViewVC 1.1.26