/[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

revision 97 by jgs, Tue Dec 14 05:39:33 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    
# Line 220  class Data { Line 220  class Data {
220    
221    /**    /**
222       \brief       \brief
223         Return the values of all data-points as a single python numarray object.
224      */
225      const boost::python::numeric::array
226      convertToNumArray();
227    
228      /**
229         \brief
230         Return the values of all data-points for the given sample as a single python numarray object.
231      */
232      const boost::python::numeric::array
233      convertToNumArrayFromSampleNo(int sampleNo);
234    
235      /**
236         \brief
237         Return the value of the specified data-point as a single python numarray object.
238      */
239      const boost::python::numeric::array
240      convertToNumArrayFromDPNo(int sampleNo,
241                                int dataPointNo);
242    
243      /**
244         \brief
245       Return the C wrapper for the Data object.       Return the C wrapper for the Data object.
246    */    */
247    escriptDataC    escriptDataC
# Line 397  class Data { Line 419  class Data {
419    
420    /**    /**
421       \brief       \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
461       Return a view into the data for the data point specified.       Return a view into the data for the data point specified.
462       NOTE: Construction of the DataArrayView is a relatively expensive       NOTE: Construction of the DataArrayView is a relatively expensive
463       operation.       operation.
# Line 422  class Data { Line 483  class Data {
483       \brief       \brief
484       Return the 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 459  class Data { Line 520  class Data {
520       cannot be converted to a DataTagged object.       cannot be converted to a DataTagged object.
521       \param tagKey - Input - Integer key.       \param tagKey - Input - Integer key.
522       \param value - Input - Value to associate with given key.       \param value - Input - Value to associate with given key.
      Note: removed for now - this version not needed, and breaks escript.cpp  
523    */    */
   /*  
524    void    void
525    setTaggedValue(int tagKey,    setTaggedValueFromCPP(int tagKey,
526                   const DataArrayView& value);                          const DataArrayView& value);
   */  
527    
528    /**    /**
529      \brief      \brief
# Line 483  class Data { Line 541  class Data {
541       \brief       \brief
542       Interpolates this onto the given functionspace and returns       Interpolates this onto the given functionspace and returns
543       the result as a Data object.       the result as a Data object.
544         *
545    */    */
546    Data    Data
547    interpolate(const FunctionSpace& functionspace) const;    interpolate(const FunctionSpace& functionspace) const;
# Line 491  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 501  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 508  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;
# Line 515  class Data { Line 577  class Data {
577    /**    /**
578       \brief       \brief
579       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 values and a 0 for +ive or 0 values.
580         *
581    */    */
582    Data    Data
583    whereNegative() const;    whereNegative() const;
# Line 522  class Data { Line 585  class Data {
585    /**    /**
586       \brief       \brief
587       Return a Data with a 1 for +ive or 0 values and a 0 for -ive values.       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;
# Line 529  class Data { Line 593  class Data {
593    /**    /**
594       \brief       \brief
595       Return a Data with a 1 for -ive or 0 values and a 0 for +ive values.       Return a Data with a 1 for -ive or 0 values and a 0 for +ive values.
596         *
597    */    */
598    Data    Data
599    whereNonPositive() const;    whereNonPositive() const;
# Line 536  class Data { Line 601  class Data {
601    /**    /**
602       \brief       \brief
603       Return a Data with a 1 for 0 values and a 0 for +ive or -ive values.       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;
# Line 543  class Data { Line 609  class Data {
609    /**    /**
610       \brief       \brief
611       Return a Data with a 0 for 0 values and a 1 for +ive or -ive values.       Return a Data with a 0 for 0 values and a 1 for +ive or -ive values.
612         *
613    */    */
614    Data    Data
615    whereNonZero() const;    whereNonZero() const;
616    
617    /**    /**
618       \brief       \brief
619       Return the sin of each data point of this Data object.       Return the maximum absolute value of this Data object.
620    */       *
   Data  
   sin() const;  
   
   /**  
      \brief  
      Return the cos of each data point of this Data object.  
   */  
   Data  
   cos() const;  
   
   /**  
      \brief  
      Return the tan of each data point of this Data object.  
   */  
   Data  
   tan() const;  
   
   /**  
      \brief  
      Return the log to base 10 of each data point of this Data object.  
   */  
   Data  
   log() const;  
   
   /**  
      \brief  
      Return the natural log of each data point of this Data object.  
621    */    */
622    Data    double
623    ln() const;    Lsup() const;
624    
625    /**    /**
626       \brief       \brief
627       Return the maximum absolute value of this Data object.       Return the minimum absolute value of this Data object.
628         *
629    */    */
630    double    double
631    Lsup() const;    Linf() const;
632    
633    /**    /**
634       \brief       \brief
635       Return the maximum value of this Data object.       Return the maximum value of this Data object.
636         *
637    */    */
638    double    double
639    sup() const;    sup() const;
# Line 599  class Data { Line 641  class Data {
641    /**    /**
642       \brief       \brief
643       Return the minimum value of this Data object.       Return the minimum value of this Data object.
644         *
645    */    */
646    double    double
647    inf() const;    inf() const;
# Line 606  class Data { Line 649  class Data {
649    /**    /**
650       \brief       \brief
651       Return the absolute value of each data point of this Data object.       Return the absolute value of each data point of this Data object.
652         *
653    */    */
654    Data    Data
655    abs() const;    abs() const;
# Line 613  class Data { Line 657  class Data {
657    /**    /**
658       \brief       \brief
659       Return the maximum value of each data point of this Data object.       Return the maximum value of each data point of this Data object.
660         *
661    */    */
662    Data    Data
663    maxval() const;    maxval() const;
# Line 620  class Data { Line 665  class Data {
665    /**    /**
666       \brief       \brief
667       Return the minimum value of each data point of this Data object.       Return the minimum value of each data point of this Data object.
668         *
669    */    */
670    Data    Data
671    minval() const;    minval() const;
672    
673    /**    /**
674       \brief       \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.       Return the length of each data point of this Data object.
684       sqrt(sum(A[i,j,k,l]^2))       sqrt(sum(A[i,j,k,l]^2))
685         *
686    */    */
687    Data    Data
688    length() const;    length() const;
# Line 636  class Data { Line 691  class Data {
691       \brief       \brief
692       Return the sign of each data point of this Data object.       Return the sign of each data point of this Data object.
693       -1 for negative values, zero for zero values, 1 for positive values.       -1 for negative values, zero for zero values, 1 for positive values.
694         *
695    */    */
696    Data    Data
697    sign() const;    sign() const;
698    
699    /**    /**
700      \transpose       \brief
701      Transpose each data point of this Data object around the given axis.       Transpose each data point of this Data object around the given axis.
702         --* not implemented yet *--
703         *
704    */    */
705    Data    Data
706    transpose(int axis) const;    transpose(int axis) const;
707    
708    /**    /**
709      \trace       \brief
710      Calculate the trace of each data point of this Data object.       Calculate the trace of each data point of this Data object.
711      sum(A[i,i,i,i])       sum(A[i,i,i,i])
712         *
713    */    */
714    Data    Data
715    trace() const;    trace() const;
716    
717    /**    /**
718      \exp       \brief
719      Return the exponential function of each data point of this Data object.       Return the sin of each data point of this Data object.
720         *
721      */
722      Data
723      sin() const;
724    
725      /**
726         \brief
727         Return the cos of each data point of this Data object.
728         *
729      */
730      Data
731      cos() const;
732    
733      /**
734         \brief
735         Return the tan of each data point of this Data object.
736         *
737      */
738      Data
739      tan() const;
740    
741      /**
742         \brief
743         Return the log to base 10 of each data point of this Data object.
744         *
745      */
746      Data
747      log() const;
748    
749      /**
750         \brief
751         Return the natural log of each data point of this Data object.
752         *
753      */
754      Data
755      ln() const;
756    
757      /**
758         \brief
759         Return the exponential function of each data point of this Data object.
760         *
761    */    */
762    Data    Data
763    exp() const;    exp() const;
764    
765    /**    /**
766      \sqrt       \brief
767      Return the square root of each data point of this Data object.       Return the square root of each data point of this Data object.
768         *
769    */    */
770    Data    Data
771    sqrt() const;    sqrt() const;
772    
773    /**    /**
774       \brief       \brief
775         Return the negation of each data point of this Data object.
776         *
777      */
778      Data
779      neg() const;
780    
781      /**
782         \brief
783         Return the identity of each data point of this Data object.
784         Simply returns this object unmodified.
785         *
786      */
787      Data
788      pos() const;
789    
790      /**
791         \brief
792       Return the given power of each data point of this Data object.       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    Data    Data
798    powD(const Data& right) const;    powD(const Data& right) const;
799    
800      /**
801         \brief
802         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    powO(const boost::python::object& right) const;    powO(const boost::python::object& right) const;
809    
810    /**    /**
811      \brief       \brief
812      Return the negation of each data point of this Data object.       writes the object to a file in the DX file format
813    */    */
814    Data    void
815    neg() const;    saveDX(std::string fileName) const;
816    
817    /**    /**
818      \brief       \brief
819      Return the identity of each data point of this Data object.       writes the object to a file in the VTK file format
     Simply returns this object unmodified.  
820    */    */
821    Data    void
822    pos() const;    saveVTK(std::string fileName) const;
823    
824    /**    /**
825       \brief       \brief
826       Overloaded operator +=       Overloaded operator +=
827       \param right - Input - The right hand side.       \param right - Input - The right hand side.
828         *
829    */    */
830    Data& operator+=(const Data& right);    Data& operator+=(const Data& right);
831    Data& operator+=(const boost::python::object& right);    Data& operator+=(const boost::python::object& right);
# Line 706  class Data { Line 834  class Data {
834       \brief       \brief
835       Overloaded operator -=       Overloaded operator -=
836       \param right - Input - The right hand side.       \param right - Input - The right hand side.
837         *
838    */    */
839    Data& operator-=(const Data& right);    Data& operator-=(const Data& right);
840    Data& operator-=(const boost::python::object& right);    Data& operator-=(const boost::python::object& right);
# Line 714  class Data { Line 843  class Data {
843       \brief       \brief
844       Overloaded operator *=       Overloaded operator *=
845       \param right - Input - The right hand side.       \param right - Input - The right hand side.
846         *
847    */    */
848    Data& operator*=(const Data& right);    Data& operator*=(const Data& right);
849    Data& operator*=(const boost::python::object& right);    Data& operator*=(const boost::python::object& right);
# Line 722  class Data { Line 852  class Data {
852       \brief       \brief
853       Overloaded operator /=       Overloaded operator /=
854       \param right - Input - The right hand side.       \param right - Input - The right hand side.
855         *
856    */    */
857    Data& operator/=(const Data& right);    Data& operator/=(const Data& right);
858    Data& operator/=(const boost::python::object& right);    Data& operator/=(const boost::python::object& right);
# Line 755  class Data { Line 886  class Data {
886       \brief       \brief
887       Copies slice from value into this Data object.       Copies slice from value into this Data object.
888    
      \description  
889       Implements the [] set operator in python.       Implements the [] set operator in python.
890       Calls setSlice.       Calls setSlice.
891    
# Line 788  class Data { Line 918  class Data {
918       Return a Data object containing the specified slice of       Return a Data object containing the specified slice of
919       this Data object.       this Data object.
920       \param region - Input - Region to copy.       \param region - Input - Region to copy.
921         *
922    */    */
923    Data    Data
924    getSlice(const DataArrayView::RegionType& region) const;    getSlice(const DataArrayView::RegionType& region) const;
# Line 798  class Data { Line 929  class Data {
929       Data object.       Data object.
930       \param value - Input - Data to copy from.       \param value - Input - Data to copy from.
931       \param region - Input - Region to copy.       \param region - Input - Region to copy.
932         *
933    */    */
934    void    void
935    setSlice(const Data& value,    setSlice(const Data& value,
936             const DataArrayView::RegionType& region);             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:   protected:
959    
960   private:   private:
# Line 823  class Data { Line 975  class Data {
975    /**    /**
976       \brief       \brief
977       Perform the specified reduction algorithm on every element of every data point in       Perform the specified reduction algorithm on every element of every data point in
978       this Data object and return the single double value result.       this Data object according to the given function and return the single value result.
979    */    */
980    template <class UnaryFunction>    template <class UnaryFunction>
981    inline    inline
# Line 832  class Data { Line 984  class Data {
984    
985    /**    /**
986       \brief       \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.       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       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       type of *this after the operation. For example if the right hand side
# Line 895  class Data { Line 1059  class Data {
1059    // pointer to the actual data object    // 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 1222  Data::algorithm(UnaryFunction operation) Line 1390  Data::algorithm(UnaryFunction operation)
1390  template <class UnaryFunction>  template <class UnaryFunction>
1391  inline  inline
1392  Data  Data
1393  dp_algorithm(const Data& data,  Data::dp_algorithm(UnaryFunction operation) const
              UnaryFunction operation)  
1394  {  {
1395    Data result(0,DataArrayView::ShapeType(),data.getFunctionSpace(),data.isExpanded());    Data result(0,DataArrayView::ShapeType(),getFunctionSpace(),isExpanded());
1396    if (data.isExpanded()) {    if (isExpanded()) {
1397      DataExpanded* dataE=dynamic_cast<DataExpanded*>(data.m_data.get());      DataExpanded* dataE=dynamic_cast<DataExpanded*>(m_data.get());
1398      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());      DataExpanded* resultE=dynamic_cast<DataExpanded*>(result.m_data.get());
1399      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");      EsysAssert((dataE!=0), "Programming error - casting data to DataExpanded.");
1400      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");      EsysAssert((resultE!=0), "Programming error - casting result to DataExpanded.");
1401      escript::dp_algorithm(*dataE,*resultE,operation);      escript::dp_algorithm(*dataE,*resultE,operation);
1402    } else if (data.isTagged()) {    } else if (isTagged()) {
1403      DataTagged* dataT=dynamic_cast<DataTagged*>(data.m_data.get());      DataTagged* dataT=dynamic_cast<DataTagged*>(m_data.get());
1404      DataTagged* resultT=dynamic_cast<DataTagged*>(result.m_data.get());      DataTagged* resultT=dynamic_cast<DataTagged*>(result.m_data.get());
1405      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");      EsysAssert((dataT!=0), "Programming error - casting data to DataTagged.");
1406      EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");      EsysAssert((resultT!=0), "Programming error - casting result to DataTagged.");
1407      escript::dp_algorithm(*dataT,*resultT,operation);      escript::dp_algorithm(*dataT,*resultT,operation);
1408    } else if (data.isConstant()) {    } else if (isConstant()) {
1409      DataConstant* dataC=dynamic_cast<DataConstant*>(data.m_data.get());      DataConstant* dataC=dynamic_cast<DataConstant*>(m_data.get());
1410      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());      DataConstant* resultC=dynamic_cast<DataConstant*>(result.m_data.get());
1411      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");      EsysAssert((dataC!=0), "Programming error - casting data to DataConstant.");
1412      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");      EsysAssert((resultC!=0), "Programming error - casting result to DataConstant.");

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

  ViewVC Help
Powered by ViewVC 1.1.26