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

Legend:
Removed from v.100  
changed lines
  Added in v.474

  ViewVC Help
Powered by ViewVC 1.1.26