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

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

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

revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC revision 123 by jgs, Fri Jul 8 04:08:13 2005 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 "escript/Data/DataAbstract.h"
22  #include "escript/Data/DataTagged.h"  #include "escript/Data/DataTagged.h"
23    #include "escript/Data/DataAlgorithm.h"
24  #include "escript/Data/FunctionSpace.h"  #include "escript/Data/FunctionSpace.h"
25  #include "escript/Data/BinaryOp.h"  #include "escript/Data/BinaryOp.h"
26  #include "escript/Data/UnaryOp.h"  #include "escript/Data/UnaryOp.h"
27    #include "escript/Data/DataException.h"
28    
29  extern "C" {  extern "C" {
30  #include "escript/Data/DataC.h"  #include "escript/Data/DataC.h"
# 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 implimentations 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 205  class Data { Line 209  class Data {
209    
210    /**    /**
211       \brief       \brief
212       Perform the specified algorithm on the Data and return result.       Perform a deep copy.
   */  
   template <class UnaryFunction>  
   inline double  
   algorithm(UnaryFunction operation) const;  
   
   /**  
      \brief  
      Perform the given unary operation on all of the data's elements.  
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);  
   
  /**  
      \brief  
      Overloaded operator /=  
      \param right - Input - The right hand side.  
   */  
   Data& operator/=(const Data& right);  
   Data& operator/=(const boost::python::object& right);  
   
   /**  
      \brief  
      Return the power of Data.  
   */  
   Data powD(const Data& right) const;  
   Data powO(const boost::python::object& right) const;  
242    
243    /**    /**
244       \brief       \brief
245       Return the C wrapper for the Data object.       Return the C wrapper for the Data object.
246    */    */
247    escriptDataC getDataC();    escriptDataC
248      getDataC();
249    
250    /**    /**
251       \brief       \brief
252       Return the C wrapper for the Data object - const version.       Return the C wrapper for the Data object - const version.
253    */    */
254    escriptDataC getDataC() const;    escriptDataC
255      getDataC() const;
256    
257    /**    /**
258       \brief       \brief
259       Write the data as a string.       Write the data as a string.
260    */    */
261    std::string toString() const;    inline
262      std::string
263      toString() const
264      {
265        return m_data->toString();
266      }
267    
268    /**    /**
269       \brief       \brief
# Line 312  class Data { Line 280  class Data {
280    
281    /**    /**
282       \brief       \brief
283       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.  
284    */    */
285    void    void
286    expand();    expand();
287    
288    /**    /**
289       \brief       \brief
290       If possible convert the Data type to tagged. This will only allow       If possible convert this Data to DataTagged. This will only allow
291       Constant data to be converted to tagged. An attempt to convert       Constant data to be converted to tagged. An attempt to convert
292       Expanded data to tagged will throw an exception.       Expanded data to tagged will throw an exception.
293    */    */
# Line 357  class Data { Line 310  class Data {
310    
311    /**    /**
312       \brief       \brief
313       Return true if this Data is empty.       Return true if this Data is constant.
314    */    */
315    bool    bool
316    isEmpty() const;    isConstant() const;
317    
318    /**    /**
319       \brief       \brief
320       Return true if this Data is constant.       Return true if this Data is empty.
321    */    */
322    bool    bool
323    isConstant() const;    isEmpty() const;
324    
325    /**    /**
326       \brief       \brief
# Line 418  class Data { Line 371  class Data {
371    
372    /**    /**
373       \brief       \brief
374       Return the number of data points per sample.       Return the number of samples.
375    */    */
376    inline    inline
377    int    int
378    getNumDataPointsPerSample() const    getNumSamples() const
379    {    {
380      return m_data->getNumDPPSample();      return m_data->getNumSamples();
381    }    }
382    
383    /**    /**
384       \brief       \brief
385       Return the number of samples.       Return the number of data points per sample.
386    */    */
387      inline
388    int    int
389    getNumSamples() const;    getNumDataPointsPerSample() const
390      {
391    /**      return m_data->getNumDPPSample();
392       \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;  
393    
394    /**    /**
395       \brief       \brief
# Line 449  class Data { Line 397  class Data {
397       preferred interface but is provided for use by C code.       preferred interface but is provided for use by C code.
398       \param sampleNo - Input - the given sample no.       \param sampleNo - Input - the given sample no.
399    */    */
400      inline
401    DataAbstract::ValueType::value_type*    DataAbstract::ValueType::value_type*
402    getSampleData(DataAbstract::ShapeType::size_type sampleNo);    getSampleData(DataAbstract::ValueType::size_type sampleNo)
403      {
404        return m_data->getSampleData(sampleNo);
405      }
406    
407    /**    /**
408       \brief       \brief
# Line 458  class Data { Line 410  class Data {
410       access data that isn't tagged an exception will be thrown.       access data that isn't tagged an exception will be thrown.
411       \param tag - Input - the tag key.       \param tag - Input - the tag key.
412    */    */
413      inline
414    DataAbstract::ValueType::value_type*    DataAbstract::ValueType::value_type*
415    getSampleDataByTag(int tag);    getSampleDataByTag(int tag)
416      {
417        return m_data->getSampleDataByTag(tag);
418      }
419    
420      /**
421         \brief
422         Assign the given value to the data-points referenced by the given
423         reference number.
424    
425         The value supplied is a python numarray object.  The data from this numarray
426         is unpacked into a DataArray, and this is used to set the corresponding
427         data-points in the underlying Data object.
428    
429         If the underlying Data object cannot be accessed via reference numbers, an
430         exception will be thrown.
431    
432         \param ref - Input - reference number.
433         \param value - Input - value to assign to data-points associated with
434                                the given reference number.
435      */
436      void
437      setRefValue(int ref,
438                  const boost::python::numeric::array& value);
439    
440      /**
441         \brief
442         Return the values associated with the data-points referenced by the given
443         reference number.
444    
445         The value supplied is a python numarray object. The data from the corresponding
446         data-points in this Data object are packed into the given numarray object.
447    
448         If the underlying Data object cannot be accessed via reference numbers, an
449         exception will be thrown.
450    
451         \param ref - Input - reference number.
452         \param value - Output - object to receive values from data-points
453                                 associated with the given reference number.
454      */
455      void
456      getRefValue(int ref,
457                  boost::python::numeric::array& value);
458    
459    /**    /**
460       \brief       \brief
# Line 486  class Data { Line 481  class Data {
481    
482    /**    /**
483       \brief       \brief
484       Return data point shape as a tuple of integers:       Return the data point shape as a tuple of integers.
485    */    */
486    boost::python::tuple    const boost::python::tuple
487    getShapeTuple() const;    getShapeTuple() const;
488    
489    /**    /**
# Line 501  class Data { Line 496  class Data {
496    
497    /**    /**
498       \brief       \brief
499       Return the number of doubles stored for Data.       Return the number of doubles stored for this Data.
500    */    */
501    DataArrayView::ValueType::size_type    DataArrayView::ValueType::size_type
502    getLength() const;    getLength() const;
503    
504    /**    /**
505       \brief       \brief
506       Interpolates this onto the given functionspace and returns the result as a Data object.       Assign the given value to the tag. Implicitly converts this
507         object to type DataTagged. Throws an exception if this object
508         cannot be converted to a DataTagged object.
509         \param tagKey - Input - Integer key.
510         \param value - Input - Value to associate with given key.
511      */
512      void
513      setTaggedValue(int tagKey,
514                     const boost::python::object& value);
515    
516      /**
517         \brief
518         Assign the given value to the tag. Implicitly converts this
519         object to type DataTagged. Throws an exception if this object
520         cannot be converted to a DataTagged object.
521         \param tagKey - Input - Integer key.
522         \param value - Input - Value to associate with given key.
523      */
524      void
525      setTaggedValueFromCPP(int tagKey,
526                            const DataArrayView& value);
527    
528      /**
529        \brief
530        Copy other Data object into this Data object where mask is positive.
531      */
532      void
533      copyWithMask(const Data& other,
534                   const Data& mask);
535    
536      /**
537         Data object operation methods and operators.
538      */
539    
540      /**
541         \brief
542         Interpolates this onto the given functionspace and returns
543         the result as a Data object.
544         *
545    */    */
546    Data    Data
547    interpolate(const FunctionSpace& functionspace) const;    interpolate(const FunctionSpace& functionspace) const;
# Line 517  class Data { Line 550  class Data {
550       \brief       \brief
551       Calculates the gradient of the data at the data points of functionspace.       Calculates the gradient of the data at the data points of functionspace.
552       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.
553         *
554    */    */
555    Data    Data
556    gradOn(const FunctionSpace& functionspace) const;    gradOn(const FunctionSpace& functionspace) const;
# Line 527  class Data { Line 561  class Data {
561    /**    /**
562       \brief       \brief
563       Calculate the integral over the function space domain.       Calculate the integral over the function space domain.
564         *
565    */    */
566    boost::python::numeric::array    boost::python::numeric::array
567    integrate() const;    integrate() const;
# Line 534  class Data { Line 569  class Data {
569    /**    /**
570       \brief       \brief
571       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.
572         *
573    */    */
574    Data    Data
575    wherePositive() const;    wherePositive() const;
576    
577    /**    /**
578       \brief       \brief
579       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.
580         *
581      */
582      Data
583      whereNegative() const;
584    
585      /**
586         \brief
587         Return a Data with a 1 for +ive or 0 values and a 0 for -ive values.
588         *
589    */    */
590    Data    Data
591    whereNonNegative() const;    whereNonNegative() const;
592    
593    /**    /**
594       \brief       \brief
595       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.
596         *
597    */    */
598    Data    Data
599    whereNegative() const;    whereNonPositive() const;
600    
601    /**    /**
602       \brief       \brief
603       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.
604         *
605    */    */
606    Data    Data
607    whereZero() const;    whereZero() const;
608    
609    /**    /**
610       \brief       \brief
611       Return the sin of Data.       Return a Data with a 0 for 0 values and a 1 for +ive or -ive values.
612         *
613      */
614      Data
615      whereNonZero() const;
616    
617      /**
618         \brief
619         Return the maximum absolute value of this Data object.
620         *
621      */
622      double
623      Lsup() const;
624    
625      /**
626         \brief
627         Return the minimum absolute value of this Data object.
628         *
629      */
630      double
631      Linf() const;
632    
633      /**
634         \brief
635         Return the maximum value of this Data object.
636         *
637      */
638      double
639      sup() const;
640    
641      /**
642         \brief
643         Return the minimum value of this Data object.
644         *
645      */
646      double
647      inf() const;
648    
649      /**
650         \brief
651         Return the absolute value of each data point of this Data object.
652         *
653      */
654      Data
655      abs() const;
656    
657      /**
658         \brief
659         Return the maximum value of each data point of this Data object.
660         *
661      */
662      Data
663      maxval() const;
664    
665      /**
666         \brief
667         Return the minimum value of each data point of this Data object.
668         *
669      */
670      Data
671      minval() const;
672    
673      /**
674         \brief
675         Return the (sample number, data-point number) of the data point with
676         the minimum value in this Data object.
677      */
678      const boost::python::tuple
679      mindp() const;
680    
681      /**
682         \brief
683         Return the length of each data point of this Data object.
684         sqrt(sum(A[i,j,k,l]^2))
685         *
686      */
687      Data
688      length() const;
689    
690      /**
691         \brief
692         Return the sign of each data point of this Data object.
693         -1 for negative values, zero for zero values, 1 for positive values.
694         *
695      */
696      Data
697      sign() const;
698    
699      /**
700         \brief
701         Transpose each data point of this Data object around the given axis.
702         --* not implemented yet *--
703         *
704      */
705      Data
706      transpose(int axis) const;
707    
708      /**
709         \brief
710         Calculate the trace of each data point of this Data object.
711         sum(A[i,i,i,i])
712         *
713      */
714      Data
715      trace() const;
716    
717      /**
718         \brief
719         Return the sin of each data point of this Data object.
720         *
721    */    */
722    Data    Data
723    sin() const;    sin() const;
724    
725    /**    /**
726       \brief       \brief
727       Return the cos of Data.       Return the cos of each data point of this Data object.
728         *
729    */    */
730    Data    Data
731    cos() const;    cos() const;
732    
733    /**    /**
734       \brief       \brief
735       Return the tan of Data.       Return the tan of each data point of this Data object.
736         *
737    */    */
738    Data    Data
739    tan() const;    tan() const;
740    
741    /**    /**
742       \brief       \brief
743       Return the log to base 10 of Data.       Return the log to base 10 of each data point of this Data object.
744         *
745    */    */
746    Data    Data
747    log() const;    log() const;
748    
749    /**    /**
750       \brief       \brief
751       Return the natural log of Data.       Return the natural log of each data point of this Data object.
752         *
753    */    */
754    Data    Data
755    ln() const;    ln() const;
756    
757    /**    /**
758       \brief       \brief
759       Return a Data containing a slice of this Data.       Return the exponential function of each data point of this Data object.
760         *
761    */    */
762    Data    Data
763    getSlice(const DataArrayView::RegionType& region) const;    exp() const;
764    
765    /**    /**
766       \brief       \brief
767       Copy the specified region from the given value.       Return the square root of each data point of this Data object.
768       \param value - Input - Data to copy from.       *
      \param region - Input - Region to copy.  
769    */    */
770    void    Data
771    setSlice(const Data& value,    sqrt() const;
            const DataArrayView::RegionType& region);  
772    
773    /**    /**
774       \brief       \brief
775       Return the maximum absolute value.       Return the negation of each data point of this Data object.
776         *
777    */    */
778    double    Data
779    Lsup() const;    neg() const;
780    
781    /**    /**
782       \brief       \brief
783       Return the maximum value.       Return the identity of each data point of this Data object.
784         Simply returns this object unmodified.
785         *
786    */    */
787    double    Data
788    sup() const;    pos() const;
789    
790    /**    /**
791       \brief       \brief
792       Return the minimum value.       Return the given power of each data point of this Data object.
793    
794         \param right Input - the power to raise the object to.
795         *
796    */    */
797    double    Data
798    inf() const;    powD(const Data& right) const;
799    
800    /**    /**
801       \brief       \brief
802       Returns a slice from this.       Return the given power of each data point of this boost python object.
803    */      
804         \param right Input - the power to raise the object to.
805         *
806       */
807    Data    Data
808    getItem(const boost::python::object& key) const;    powO(const boost::python::object& right) const;
809    
810    /**    /**
811       \brief       \brief
812       Copies slice from value into this.       writes the object to a file in the DX file format
813    */    */
814    void    void
815    setItem(const boost::python::object& key,    saveDX(std::string fileName) const;
           const Data& value);  
816    
817    /**    /**
818       \brief       \brief
819       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.  
820    */    */
821    void    void
822    typeMatch(const Data& right);    saveVTK(std::string fileName) const;
823    
824      /**
825         \brief
826         Overloaded operator +=
827         \param right - Input - The right hand side.
828         *
829      */
830      Data& operator+=(const Data& right);
831      Data& operator+=(const boost::python::object& right);
832    
833      /**
834         \brief
835         Overloaded operator -=
836         \param right - Input - The right hand side.
837         *
838      */
839      Data& operator-=(const Data& right);
840      Data& operator-=(const boost::python::object& right);
841    
842     /**
843         \brief
844         Overloaded operator *=
845         \param right - Input - The right hand side.
846         *
847      */
848      Data& operator*=(const Data& right);
849      Data& operator*=(const boost::python::object& right);
850    
851     /**
852         \brief
853         Overloaded operator /=
854         \param right - Input - The right hand side.
855         *
856      */
857      Data& operator/=(const Data& right);
858      Data& operator/=(const boost::python::object& right);
859    
860    /**    /**
861       \brief       \brief
# Line 663  class Data { Line 865  class Data {
865    probeInterpolation(const FunctionSpace& functionspace) const;    probeInterpolation(const FunctionSpace& functionspace) const;
866    
867    /**    /**
868         Data object slicing methods.
869      */
870    
871      /**
872       \brief       \brief
873       Assign the given value to the tag. Implicitly converts this       Returns a slice from this Data object.
874       object to type DataTagged. Throws an exception if this object  
875       cannot be converted to a DataTagged object.       /description
876       \param tagKey - Input - Integer key.       Implements the [] get operator in python.
877       \param value - Input - Value to associate with given key.       Calls getSlice.
878    
879         \param key - Input - python slice tuple specifying
880         slice to return.
881      */
882      Data
883      getItem(const boost::python::object& key) const;
884    
885      /**
886         \brief
887         Copies slice from value into this Data object.
888    
889         Implements the [] set operator in python.
890         Calls setSlice.
891    
892         \param key - Input - python slice tuple specifying
893         slice to copy from value.
894         \param value - Input - Data object to copy from.
895    */    */
896    void    void
897    setTaggedValue(int tagKey,    setItemD(const boost::python::object& key,
898                   const boost::python::object& value);             const Data& value);
899    
900      void
901      setItemO(const boost::python::object& key,
902               const boost::python::object& value);
903    
904      // These following public methods should be treated as private.
905    
906    /**    /**
907       \brief       \brief
908       Assign the given value to the tag. Implicitly converts this       Perform the given unary operation on every element of every data point in
909       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  
910    */    */
911    /*    template <class UnaryFunction>
912      inline
913    void    void
914    setTaggedValue(int tagKey,    unaryOp(UnaryFunction operation);
915                   const DataArrayView& value);  
916      /**
917         \brief
918         Return a Data object containing the specified slice of
919         this Data object.
920         \param region - Input - Region to copy.
921         *
922      */
923      Data
924      getSlice(const DataArrayView::RegionType& region) const;
925    
926      /**
927         \brief
928         Copy the specified slice from the given value into this
929         Data object.
930         \param value - Input - Data to copy from.
931         \param region - Input - Region to copy.
932         *
933      */
934      void
935      setSlice(const Data& value,
936               const DataArrayView::RegionType& region);
937    
938      /**
939         \brief
940         Archive the current Data object to the given file.
941         \param fileName - Input - file to archive to.
942    */    */
943      void
944      archiveData(const std::string fileName);
945    
946      /**
947         \brief
948         Extract the Data object archived in the given file, overwriting
949         the current Data object.
950         Note - the current object must be of type DataEmpty.
951         \param fileName - Input - file to extract from.
952         \param fspace - Input - a suitable FunctionSpace descibing the data.
953      */
954      void
955      extractData(const std::string fileName,
956                  const FunctionSpace& fspace);
957    
958     protected:
959    
960   private:   private:
961    
962    /**    /**
963       \brief       \brief
964         Check *this and the right operand are compatible. Throws
965         an exception if they aren't.
966         \param right - Input - The right hand side.
967      */
968      inline
969      void
970      operandCheck(const Data& right) const
971      {
972        return m_data->operandCheck(*(right.m_data.get()));
973      }
974    
975      /**
976         \brief
977         Perform the specified reduction algorithm on every element of every data point in
978         this Data object according to the given function and return the single value result.
979      */
980      template <class UnaryFunction>
981      inline
982      double
983      algorithm(UnaryFunction operation) const;
984    
985      /**
986         \brief
987         Reduce each data-point in this Data object using the given operation. Return a Data
988         object with the same number of data-points, but with each data-point containing only
989         one value - the result of the reduction operation on the corresponding data-point in
990         this Data object
991      */
992      template <class UnaryFunction>
993      inline
994      Data
995      dp_algorithm(UnaryFunction operation) const;
996    
997      /**
998         \brief
999         Perform the given binary operation on all of the data's elements.
1000         The underlying type of the right hand side (right) determines the final
1001         type of *this after the operation. For example if the right hand side
1002         is expanded *this will be expanded if necessary.
1003         RHS is a Data object.
1004      */
1005      template <class BinaryFunction>
1006      inline
1007      void
1008      binaryOp(const Data& right,
1009               BinaryFunction operation);
1010    
1011      /**
1012         \brief
1013         Perform the given binary operation on all of the data's elements.
1014         RHS is a boost::python object.
1015      */
1016      template <class BinaryFunction>
1017      inline
1018      void
1019      binaryOp(const boost::python::object& right,
1020               BinaryFunction operation);
1021    
1022      /**
1023         \brief
1024         Convert the data type of the RHS to match this.
1025         \param right - Input - data type to match.
1026      */
1027      void
1028      typeMatchLeft(Data& right) const;
1029    
1030      /**
1031         \brief
1032         Convert the data type of this to match the RHS.
1033         \param right - Input - data type to match.
1034      */
1035      void
1036      typeMatchRight(const Data& right);
1037    
1038      /**
1039         \brief
1040       Construct a Data object of the appropriate type.       Construct a Data object of the appropriate type.
1041    */    */
1042    template <class IValueType>    template <class IValueType>
# Line 712  class Data { Line 1056  class Data {
1056    reshapeDataPoint(const DataArrayView::ShapeType& shape);    reshapeDataPoint(const DataArrayView::ShapeType& shape);
1057    
1058    //    //
1059    // pointer to the actual data    // pointer to the actual data object
1060    boost::shared_ptr<DataAbstract> m_data;    boost::shared_ptr<DataAbstract> m_data;
1061    
1062      //
1063      // pointer to the internal profiling data
1064      struct profDataEntry *profData;
1065    
1066  };  };
1067    
1068  template <class IValueType>  template <class IValueType>
# Line 730  Data::initialise(const IValueType& value Line 1078  Data::initialise(const IValueType& value
1078    // within the shared_ptr constructor.    // within the shared_ptr constructor.
1079    if (expanded) {    if (expanded) {
1080      DataAbstract* temp=new DataExpanded(value,what);      DataAbstract* temp=new DataExpanded(value,what);
1081      m_data=boost::shared_ptr<DataAbstract>(temp);      boost::shared_ptr<DataAbstract> temp_data(temp);
1082        m_data=temp_data;
1083    } else {    } else {
1084      DataAbstract* temp=new DataConstant(value,what);      DataAbstract* temp=new DataConstant(value,what);
1085      m_data=boost::shared_ptr<DataAbstract>(temp);      boost::shared_ptr<DataAbstract> temp_data(temp);
1086        m_data=temp_data;
1087    }    }
1088  }  }
1089    
1090  inline  /**
1091  DataAbstract::ValueType::value_type*     Binary Data object operators.
1092  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();  
 }  
1093    
1094  /**  /**
1095    \brief    \brief
# Line 876  std::ostream& operator<<(std::ostream& o Line 1195  std::ostream& operator<<(std::ostream& o
1195    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
1196    be relied on. Requires further implementation.    be relied on. Requires further implementation.
1197  */  */
1198  bool operator==(const Data& left, const Data& right);  //bool operator==(const Data& left, const Data& right);
1199    
1200    /**
1201      \brief
1202      Perform the given binary operation with this and right as operands.
1203      Right is a Data object.
1204    */
1205  template <class BinaryFunction>  template <class BinaryFunction>
1206  inline  inline
1207  void  void
# Line 907  Data::binaryOp(const Data& right, Line 1231  Data::binaryOp(const Data& right,
1231     operandCheck(tempRight);     operandCheck(tempRight);
1232     //     //
1233     // ensure this has the right type for the RHS     // ensure this has the right type for the RHS
1234     typeMatch(tempRight);     typeMatchRight(tempRight);
1235     //     //
1236     // Need to cast to the concrete types so that the correct binaryOp     // Need to cast to the concrete types so that the correct binaryOp
1237     // is called.     // is called.
# Line 933  Data::binaryOp(const Data& right, Line 1257  Data::binaryOp(const Data& right,
1257         EsysAssert((rightC!=0), "Programming error - casting to DataConstant.");         EsysAssert((rightC!=0), "Programming error - casting to DataConstant.");
1258         escript::binaryOp(*leftC,*rightC,operation);         escript::binaryOp(*leftC,*rightC,operation);
1259       }       }
1260     } else {     } else if (isConstant()) {
      //  
      // can only be DataConstant  
1261       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1262       DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());       DataConstant* rightC=dynamic_cast<DataConstant*>(tempRight.m_data.get());
1263       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 1265  Data::binaryOp(const Data& right,
1265     }     }
1266  }  }
1267    
1268    /**
1269      \brief
1270      Perform the given binary operation with this and right as operands.
1271      Right is a boost::python object.
1272    */
1273  template <class BinaryFunction>  template <class BinaryFunction>
1274  inline  inline
1275  void  void
# Line 963  Data::binaryOp(const boost::python::obje Line 1290  Data::binaryOp(const boost::python::obje
1290                    "Error - RHS shape doesn't match LHS shape.",temp.getView().getShape()));                    "Error - RHS shape doesn't match LHS shape.",temp.getView().getShape()));
1291       }       }
1292     }     }
   
1293     if (isExpanded()) {     if (isExpanded()) {
      //  
      // Expanded data will be done in parallel  
1294       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());       DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1295       EsysAssert((leftC!=0),"Programming error - casting to DataExpanded.");       EsysAssert((leftC!=0),"Programming error - casting to DataExpanded.");
1296       escript::binaryOp(*leftC,temp.getView(),operation);       escript::binaryOp(*leftC,temp.getView(),operation);
# Line 974  Data::binaryOp(const boost::python::obje Line 1298  Data::binaryOp(const boost::python::obje
1298       DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());       DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1299       EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");       EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1300       escript::binaryOp(*leftC,temp.getView(),operation);       escript::binaryOp(*leftC,temp.getView(),operation);
1301     } else {     } else if (isConstant()) {
      //  
      // can only be DataConstant  
1302       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());       DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1303       EsysAssert((leftC!=0),"Programming error - casting to DataConstant.");       EsysAssert((leftC!=0),"Programming error - casting to DataConstant.");
1304       escript::binaryOp(*leftC,temp.getView(),operation);       escript::binaryOp(*leftC,temp.getView(),operation);
1305     }     }
1306  }  }
1307    
1308    /**
1309      \brief
1310      Perform the given unary operation on other and return the result.
1311      Given operation is performed on each element of each data point, thus
1312      argument object is a rank n Data object, and returned object is a rank n
1313      Data object.
1314      Calls Data::unaryOp.
1315    */
1316    template <class UnaryFunction>
1317    inline
1318    Data
1319    unaryOp(const Data& other,
1320            UnaryFunction operation)
1321    {
1322      Data result;
1323      result.copy(other);
1324      result.unaryOp(operation);
1325      return result;
1326    }
1327    
1328    /**
1329      \brief
1330      Perform the given unary operation on this.
1331      Given operation is performed on each element of each data point.
1332      Calls escript::unaryOp.
1333    */
1334  template <class UnaryFunction>  template <class UnaryFunction>
1335  inline  inline
1336  void  void
1337  Data::unaryOp(UnaryFunction operation)  Data::unaryOp(UnaryFunction operation)
1338  {  {
1339    if (isExpanded()) {    if (isExpanded()) {
     //  
     // Expanded data will be done in parallel  
1340      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1341      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1342      escript::unaryOp(*leftC,operation);      escript::unaryOp(*leftC,operation);
# Line 998  Data::unaryOp(UnaryFunction operation) Line 1344  Data::unaryOp(UnaryFunction operation)
1344      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1345      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1346      escript::unaryOp(*leftC,operation);      escript::unaryOp(*leftC,operation);
1347    } else {    } else if (isConstant()) {
1348      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1349      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1350      escript::unaryOp(*leftC,operation);      escript::unaryOp(*leftC,operation);
1351    }    }
1352  }  }
1353    
1354    /**
1355      \brief
1356      Perform the given Data object reduction algorithm on this and return the result.
1357      Given operation combines each element of each data point, thus argument
1358      object (*this) is a rank n Data object, and returned object is a scalar.
1359      Calls escript::algorithm.
1360    */
1361  template <class UnaryFunction>  template <class UnaryFunction>
1362  inline  inline
1363  double  double
1364  Data::algorithm(UnaryFunction operation) const  Data::algorithm(UnaryFunction operation) const
1365  {  {
1366    if (isExpanded()) {    if (isExpanded()) {
     //  
     // Expanded data will be done in parallel  
1367      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* leftC=dynamic_cast<DataExpanded*>(m_data.get());
1368      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");      EsysAssert((leftC!=0), "Programming error - casting to DataExpanded.");
1369      return escript::algorithm(*leftC,operation);      return escript::algorithm(*leftC,operation);
1370    }    } else if (isTagged()) {
   else if (isTagged()) {  
1371      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* leftC=dynamic_cast<DataTagged*>(m_data.get());
1372      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");      EsysAssert((leftC!=0), "Programming error - casting to DataTagged.");
1373      return escript::algorithm(*leftC,operation);      return escript::algorithm(*leftC,operation);
1374    } else {    } else if (isConstant()) {
1375      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* leftC=dynamic_cast<DataConstant*>(m_data.get());
1376      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");      EsysAssert((leftC!=0), "Programming error - casting to DataConstant.");
1377      return escript::algorithm(*leftC,operation);      return escript::algorithm(*leftC,operation);
1378    }    }
1379      return 0;
1380  }  }
1381    
1382    /**
1383      \brief
1384      Perform the given data point reduction algorithm on data and return the result.
1385      Given operation combines each element within each data point into a scalar,
1386      thus argument object is a rank n Data object, and returned object is a
1387      rank 0 Data object.
1388      Calls escript::dp_algorithm.
1389    */
1390  template <class UnaryFunction>  template <class UnaryFunction>
1391  inline  inline
1392  Data  Data
1393  unaryOp(const Data& other,  Data::dp_algorithm(UnaryFunction operation) const
         UnaryFunction operation)  
1394  {  {
1395    //    Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());
1396    // Perform the given operation on a copy of the input data and return the result    if (isExpanded()) {
1397    Data result;      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
1398    //      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
1399    // perform a deep copy      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
1400    result.copy(other);      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
1401    result.unaryOp(operation);      escript::dp_algorithm(*dataE,*resultE,operation);
1402      } else if (isTagged()) {
1403        DataTagged* dataT=dynamic_cast<DataTagged*>(m_data.get());
1404        DataTagged* resultT=dynamic_cast<DataTagged*>(result.m_data.get());
1405        EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
1406        EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");
1407        escript::dp_algorithm(*dataT,*resultT,operation);
1408      } else if (isConstant()) {
1409        DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
1410        DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
1411        EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
1412        EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");
1413        escript::dp_algorithm(*dataC,*resultC,operation);
1414      }
1415    return result;    return result;
1416  }  }
1417    
1418  }  }
   
1419  #endif  #endif

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

  ViewVC Help
Powered by ViewVC 1.1.26