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

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

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

revision 474 by jgs, Mon Jan 30 04:23:44 2006 UTC revision 1137 by gross, Thu May 10 08:11:31 2007 UTC
# Line 1  Line 1 
1  // $Id$  // $Id$
 /*=============================================================================  
   
  ******************************************************************************  
  *                                                                            *  
  *       COPYRIGHT ACcESS 2004 -  All Rights Reserved                         *  
  *                                                                            *  
  * This software is the property of ACcESS.  No part of this code             *  
  * may be copied in any form or by any means without the expressed written    *  
  * consent of ACcESS.  Copying, use or modification of this software          *  
  * by any unauthorised person is illegal unless that                          *  
  * person has a software license agreement with ACcESS.                       *  
  *                                                                            *  
  ******************************************************************************  
   
 ******************************************************************************/  
2    
3    /*
4     ************************************************************
5     *          Copyright 2006 by ACcESS MNRF                   *
6     *                                                          *
7     *              http://www.access.edu.au                    *
8     *       Primary Business: Queensland, Australia            *
9     *  Licensed under the Open Software License version 3.0    *
10     *     http://www.opensource.org/licenses/osl-3.0.php       *
11     *                                                          *
12     ************************************************************
13    */
14  #include "Data.h"  #include "Data.h"
15    
 #include <iostream>  
 #include <fstream>  
 #include <algorithm>  
 #include <vector>  
 #include <exception>  
 #include <functional>  
 #include <math.h>  
   
 #include <boost/python/dict.hpp>  
 #include <boost/python/str.hpp>  
 #include <boost/python/extract.hpp>  
 #include <boost/python/long.hpp>  
 #include <boost/python/tuple.hpp>  
   
 #include "DataException.h"  
16  #include "DataExpanded.h"  #include "DataExpanded.h"
17  #include "DataConstant.h"  #include "DataConstant.h"
18  #include "DataTagged.h"  #include "DataTagged.h"
19  #include "DataEmpty.h"  #include "DataEmpty.h"
20  #include "DataArray.h"  #include "DataArray.h"
21  #include "DataProf.h"  #include "DataArrayView.h"
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
25    
26    #include <fstream>
27    #include <algorithm>
28    #include <vector>
29    #include <functional>
30    
31    #include <boost/python/dict.hpp>
32    #include <boost/python/extract.hpp>
33    #include <boost/python/long.hpp>
34    
35  using namespace std;  using namespace std;
36  using namespace boost::python;  using namespace boost::python;
37  using namespace boost;  using namespace boost;
38  using namespace escript;  using namespace escript;
39    
 #if defined DOPROF  
 //  
 // global table of profiling data for all Data objects  
 DataProf dataProfTable;  
 #endif  
   
40  Data::Data()  Data::Data()
41  {  {
42    //    //
# Line 60  Data::Data() Line 44  Data::Data()
44    DataAbstract* temp=new DataEmpty();    DataAbstract* temp=new DataEmpty();
45    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
46    m_data=temp_data;    m_data=temp_data;
47  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
48  }  }
49    
50  Data::Data(double value,  Data::Data(double value,
# Line 77  Data::Data(double value, Line 58  Data::Data(double value,
58    }    }
59    DataArray temp(dataPointShape,value);    DataArray temp(dataPointShape,value);
60    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
61  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
62  }  }
63    
64  Data::Data(double value,  Data::Data(double value,
# Line 91  Data::Data(double value, Line 69  Data::Data(double value,
69    DataArray temp(dataPointShape,value);    DataArray temp(dataPointShape,value);
70    pair<int,int> dataShape=what.getDataShape();    pair<int,int> dataShape=what.getDataShape();
71    initialise(temp.getView(),what,expanded);    initialise(temp.getView(),what,expanded);
72  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
73  }  }
74    
75  Data::Data(const Data& inData)  Data::Data(const Data& inData)
76  {  {
77    m_data=inData.m_data;    m_data=inData.m_data;
78  #if defined DOPROF    m_protected=inData.isProtected();
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
79  }  }
80    
81  Data::Data(const Data& inData,  Data::Data(const Data& inData,
# Line 114  Data::Data(const Data& inData, Line 86  Data::Data(const Data& inData,
86    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = inData.m_data->getSlice(region);
87    shared_ptr<DataAbstract> temp_data(tmp);    shared_ptr<DataAbstract> temp_data(tmp);
88    m_data=temp_data;    m_data=temp_data;
89  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
90  }  }
91    
92  Data::Data(const Data& inData,  Data::Data(const Data& inData,
# Line 139  Data::Data(const Data& inData, Line 108  Data::Data(const Data& inData,
108      }      }
109      m_data=tmp.m_data;      m_data=tmp.m_data;
110    }    }
111  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
112  }  }
113    
114  Data::Data(const DataTagged::TagListType& tagKeys,  Data::Data(const DataTagged::TagListType& tagKeys,
# Line 154  Data::Data(const DataTagged::TagListType Line 120  Data::Data(const DataTagged::TagListType
120    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
121    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
122    m_data=temp_data;    m_data=temp_data;
123      m_protected=false;
124    if (expanded) {    if (expanded) {
125      expand();      expand();
126    }    }
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
127  }  }
128    
129  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 168  Data::Data(const numeric::array& value, Line 131  Data::Data(const numeric::array& value,
131             bool expanded)             bool expanded)
132  {  {
133    initialise(value,what,expanded);    initialise(value,what,expanded);
134  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
135  }  }
136    
137  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
# Line 179  Data::Data(const DataArrayView& value, Line 139  Data::Data(const DataArrayView& value,
139             bool expanded)             bool expanded)
140  {  {
141    initialise(value,what,expanded);    initialise(value,what,expanded);
142  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
143  }  }
144    
145  Data::Data(const object& value,  Data::Data(const object& value,
# Line 191  Data::Data(const object& value, Line 148  Data::Data(const object& value,
148  {  {
149    numeric::array asNumArray(value);    numeric::array asNumArray(value);
150    initialise(asNumArray,what,expanded);    initialise(asNumArray,what,expanded);
151  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
152  }  }
153    
154  Data::Data(const object& value,  Data::Data(const object& value,
# Line 215  Data::Data(const object& value, Line 169  Data::Data(const object& value,
169      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
170      initialise(temp.getView(),other.getFunctionSpace(),false);      initialise(temp.getView(),other.getFunctionSpace(),false);
171    }    }
172  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
173  }  }
174    
175  Data::~Data()  Data::~Data()
# Line 262  Data::getShapeTuple() const Line 213  Data::getShapeTuple() const
213    }    }
214  }  }
215    
216    const
217    boost::python::str
218    Data::str() const
219    {
220      return boost::python::str(toString().c_str());
221    }
222    
223  void  void
224  Data::copy(const Data& other)  Data::copy(const Data& other)
225  {  {
# Line 314  Data::copy(const Data& other) Line 272  Data::copy(const Data& other)
272    throw DataException("Error - Copy not implemented for this Data type.");    throw DataException("Error - Copy not implemented for this Data type.");
273  }  }
274    
275    
276    void
277    Data::setToZero()
278    {
279      {
280        DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
281        if (temp!=0) {
282           temp->setToZero();
283           return;
284        }
285      }
286      {
287        DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
288        if (temp!=0) {
289          temp->setToZero();
290          return;
291        }
292      }
293      {
294        DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
295        if (temp!=0) {
296          temp->setToZero();
297          return;
298        }
299      }
300      throw DataException("Error - Data can not be set to zero.");
301    }
302    
303  void  void
304  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
305                     const Data& mask)                     const Data& mask)
# Line 360  Data::isConstant() const Line 346  Data::isConstant() const
346  }  }
347    
348  void  void
349    Data::setProtection()
350    {
351       m_protected=true;
352    }
353    
354    bool
355    Data::isProtected() const
356    {
357       return m_protected;
358    }
359    
360    
361    
362    void
363  Data::expand()  Data::expand()
364  {  {
365    if (isConstant()) {    if (isConstant()) {
# Line 401  Data::tag() Line 401  Data::tag()
401    }    }
402  }  }
403    
404  void  Data
405  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::oneOver() const
406  {  {
407    m_data->reshapeDataPoint(shape);    return escript::unaryOp(*this,bind1st(divides<double>(),1.));
408  }  }
409    
410  Data  Data
411  Data::wherePositive() const  Data::wherePositive() const
412  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
413    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));
414  }  }
415    
416  Data  Data
417  Data::whereNegative() const  Data::whereNegative() const
418  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
419    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));
420  }  }
421    
422  Data  Data
423  Data::whereNonNegative() const  Data::whereNonNegative() const
424  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
425    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));
426  }  }
427    
428  Data  Data
429  Data::whereNonPositive() const  Data::whereNonPositive() const
430  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
431    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));
432  }  }
433    
434  Data  Data
435  Data::whereZero() const  Data::whereZero(double tol) const
436  {  {
437  #if defined DOPROF    Data dataAbs=abs();
438    profData->where++;    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));
 #endif  
   return escript::unaryOp(*this,bind2nd(equal_to<double>(),0.0));  
439  }  }
440    
441  Data  Data
442  Data::whereNonZero() const  Data::whereNonZero(double tol) const
443  {  {
444  #if defined DOPROF    Data dataAbs=abs();
445    profData->where++;    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));
 #endif  
   return escript::unaryOp(*this,bind2nd(not_equal_to<double>(),0.0));  
446  }  }
447    
448  Data  Data
449  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
450  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
451    return Data(*this,functionspace);    return Data(*this,functionspace);
452  }  }
453    
# Line 488  Data::probeInterpolation(const FunctionS Line 469  Data::probeInterpolation(const FunctionS
469  Data  Data
470  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
471  {  {
 #if defined DOPROF  
   profData->grad++;  
 #endif  
472    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
473      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
474    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();
# Line 524  Data::getDataPointShape() const Line 502  Data::getDataPointShape() const
502    return getPointDataView().getShape();    return getPointDataView().getShape();
503  }  }
504    
 void  
 Data::fillFromNumArray(const boost::python::numeric::array num_array)  
 {  
   //  
   // check rank  
   if (num_array.getrank()<getDataPointRank())  
       throw DataException("Rank of numarray does not match Data object rank");  
505    
   //  
   // check shape of num_array  
   for (int i=0; i<getDataPointRank(); i++) {  
     if (extract<int>(num_array.getshape()[i+1])!=getDataPointShape()[i])  
        throw DataException("Shape of numarray does not match Data object rank");  
   }  
   
   //  
   // make sure data is expanded:  
   if (!isExpanded()) {  
     expand();  
   }  
   
   //  
   // and copy over  
   m_data->copyAll(num_array);  
 }  
506    
507  const  const
508  boost::python::numeric::array  boost::python::numeric::array
509  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
510  {  {
511    //    size_t length=0;
512    // determine the total number of data points    int i, j, k, l;
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
   
513    //    //
514    // determine the rank and shape of each data point    // determine the rank and shape of each data point
515    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 570  Data::convertToNumArray() Line 520  Data::convertToNumArray()
520    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
521    
522    //    //
523    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
524    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
525    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
526    // data points, whilst the first dimension will be equal to the    DataArrayView::ShapeType arrayShape = dataPointShape;
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPoints);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
   }  
527    
528    //    //
529    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
530      if (arrayRank==0) {
531        numArray.resize(1);
532      }
533    if (arrayRank==1) {    if (arrayRank==1) {
534      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
535    }    }
# Line 597  Data::convertToNumArray() Line 542  Data::convertToNumArray()
542    if (arrayRank==4) {    if (arrayRank==4) {
543      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
544    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
545    
546    //    if (getNumDataPointsPerSample()>0) {
547    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
548    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
549    int dataPoint = 0;         //
550    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
551      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
552        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
553        if (dataPointRank==0) {         }
554          numArray[dataPoint]=dataPointView();                
555        }         //
556        if (dataPointRank==1) {         // Check a valid data point number has been supplied
557          for (int i=0; i<dataPointShape[0]; i++) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
558            numArray[dataPoint][i]=dataPointView(i);             throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
559          }         }
560        }         // TODO: global error handling
561        if (dataPointRank==2) {         // create a view of the data if it is stored locally
562          for (int i=0; i<dataPointShape[0]; i++) {         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
563            for (int j=0; j<dataPointShape[1]; j++) {          
564              numArray[dataPoint][i][j] = dataPointView(i,j);         switch( dataPointRank ){
565            }              case 0 :
566          }                  numArray[0] = dataPointView();
567        }                  break;
568        if (dataPointRank==3) {              case 1 :        
569          for (int i=0; i<dataPointShape[0]; i++) {                  for( i=0; i<dataPointShape[0]; i++ )
570            for (int j=0; j<dataPointShape[1]; j++) {                      numArray[i]=dataPointView(i);
571              for (int k=0; k<dataPointShape[2]; k++) {                  break;
572                numArray[dataPoint][i][j][k]=dataPointView(i,j,k);              case 2 :        
573              }                  for( i=0; i<dataPointShape[0]; i++ )
574            }                      for( j=0; j<dataPointShape[1]; j++)
575          }                          numArray[make_tuple(i,j)]=dataPointView(i,j);
576        }                  break;
577        if (dataPointRank==4) {              case 3 :        
578          for (int i=0; i<dataPointShape[0]; i++) {                  for( i=0; i<dataPointShape[0]; i++ )
579            for (int j=0; j<dataPointShape[1]; j++) {                      for( j=0; j<dataPointShape[1]; j++ )
580              for (int k=0; k<dataPointShape[2]; k++) {                          for( k=0; k<dataPointShape[2]; k++)
581                for (int l=0; l<dataPointShape[3]; l++) {                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);
582                  numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);                  break;
583                }              case 4 :
584              }                  for( i=0; i<dataPointShape[0]; i++ )
585            }                      for( j=0; j<dataPointShape[1]; j++ )
586          }                          for( k=0; k<dataPointShape[2]; k++ )
587        }                              for( l=0; l<dataPointShape[3]; l++)
588        dataPoint++;                                  numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);
589      }                  break;
590        }
591    }    }
   
592    //    //
593    // return the loaded array    // return the array
594    return numArray;    return numArray;
 }  
595    
596  const  }
597  boost::python::numeric::array  void
598  Data::convertToNumArrayFromSampleNo(int sampleNo)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
599  {  {
600    //      // this will throw if the value cannot be represented
601    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
602    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
603    
   //  
   // determine the number of data points per sample  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
604    
605    //  }
   // determine the rank and shape of each data point  
   int dataPointRank = getDataPointRank();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
606    
607    void
608    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
609    {
610      if (isProtected()) {
611            throw DataException("Error - attempt to update protected Data object.");
612      }
613    //    //
614    // create the numeric array to be returned    // check rank
615    boost::python::numeric::array numArray(0.0);    if (num_array.getrank()<getDataPointRank())
616          throw DataException("Rank of numarray does not match Data object rank");
617    
618    //    //
619    // the rank of the returned numeric array will be the rank of    // check shape of num_array
620    // the data points, plus one. Where the rank of the array is n,    for (int i=0; i<getDataPointRank(); i++) {
621    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
622    // data points, whilst the first dimension will be equal to the         throw DataException("Shape of numarray does not match Data object rank");
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPointsPerSample);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
623    }    }
   
624    //    //
625    // resize the numeric array to the shape just calculated    // make sure data is expanded:
626    if (arrayRank==1) {    if (!isExpanded()) {
627      numArray.resize(arrayShape[0]);      expand();
   }  
   if (arrayRank==2) {  
     numArray.resize(arrayShape[0],arrayShape[1]);  
   }  
   if (arrayRank==3) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);  
   }  
   if (arrayRank==4) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);  
   }  
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
628    }    }
629      if (getNumDataPointsPerSample()>0) {
630    //         int sampleNo = dataPointNo/getNumDataPointsPerSample();
631    // loop through each data point in turn, loading the values for that data point         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
632    // into the numeric array.         m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
633    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    } else {
634      DataArrayView dataPointView = getDataPoint(sampleNo, dataPoint);         m_data->copyToDataPoint(-1, 0,num_array);
     if (dataPointRank==0) {  
       numArray[dataPoint]=dataPointView();  
     }  
     if (dataPointRank==1) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         numArray[dataPoint][i]=dataPointView(i);  
       }  
     }  
     if (dataPointRank==2) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           numArray[dataPoint][i][j] = dataPointView(i,j);  
         }  
       }  
     }  
     if (dataPointRank==3) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             numArray[dataPoint][i][j][k]=dataPointView(i,j,k);  
           }  
         }  
       }  
     }  
     if (dataPointRank==4) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             for (int l=0; l<dataPointShape[3]; l++) {  
               numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);  
             }  
           }  
         }  
       }  
     }  
635    }    }
   
   //  
   // return the loaded array  
   return numArray;  
636  }  }
637    
638  const  void
639  boost::python::numeric::array  Data::setValueOfDataPoint(int dataPointNo, const double value)
 Data::convertToNumArrayFromDPNo(int sampleNo,  
                                 int dataPointNo)  
640  {  {
641    //    if (isProtected()) {
642    // Check a valid sample number has been supplied          throw DataException("Error - attempt to update protected Data object.");
   if (sampleNo >= getNumSamples()) {  
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
643    }    }
   
644    //    //
645    // Check a valid data point number has been supplied    // make sure data is expanded:
646    if (dataPointNo >= getNumDataPointsPerSample()) {    if (!isExpanded()) {
647      throw DataException("Error - Data::convertToNumArray: invalid dataPointNo.");      expand();
648    }    }
649      if (getNumDataPointsPerSample()>0) {
650           int sampleNo = dataPointNo/getNumDataPointsPerSample();
651           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
652           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
653      } else {
654           m_data->copyToDataPoint(-1, 0,value);
655      }
656    }
657    
658    const
659    boost::python::numeric::array
660    Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
661    {
662      size_t length=0;
663      int i, j, k, l, pos;
664    //    //
665    // determine the rank and shape of each data point    // determine the rank and shape of each data point
666    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 804  Data::convertToNumArrayFromDPNo(int samp Line 694  Data::convertToNumArrayFromDPNo(int samp
694      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
695    }    }
696    
697      // added for the MPI communication
698      length=1;
699      for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
700      double *tmpData = new double[length];
701    
702    //    //
703    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
   DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);  
   if (dataPointRank==0) {  
     numArray[0]=dataPointView();  
   }  
   if (dataPointRank==1) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       numArray[i]=dataPointView(i);  
     }  
   }  
   if (dataPointRank==2) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         numArray[i][j] = dataPointView(i,j);  
       }  
     }  
   }  
   if (dataPointRank==3) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           numArray[i][j][k]=dataPointView(i,j,k);  
         }  
       }  
     }  
   }  
   if (dataPointRank==4) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           for (int l=0; l<dataPointShape[3]; l++) {  
             numArray[i][j][k][l]=dataPointView(i,j,k,l);  
           }  
         }  
       }  
     }  
   }  
704    
705        // updated for the MPI case
706        if( get_MPIRank()==procNo ){
707                 if (getNumDataPointsPerSample()>0) {
708                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
709                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
710                    //
711                    // Check a valid sample number has been supplied
712                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
713                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
714                    }
715                  
716                    //
717                    // Check a valid data point number has been supplied
718                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
719                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
720                    }
721                    // TODO: global error handling
722            // create a view of the data if it is stored locally
723            DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
724            
725            // pack the data from the view into tmpData for MPI communication
726            pos=0;
727            switch( dataPointRank ){
728                case 0 :
729                    tmpData[0] = dataPointView();
730                    break;
731                case 1 :        
732                    for( i=0; i<dataPointShape[0]; i++ )
733                        tmpData[i]=dataPointView(i);
734                    break;
735                case 2 :        
736                    for( i=0; i<dataPointShape[0]; i++ )
737                        for( j=0; j<dataPointShape[1]; j++, pos++ )
738                            tmpData[pos]=dataPointView(i,j);
739                    break;
740                case 3 :        
741                    for( i=0; i<dataPointShape[0]; i++ )
742                        for( j=0; j<dataPointShape[1]; j++ )
743                            for( k=0; k<dataPointShape[2]; k++, pos++ )
744                                tmpData[pos]=dataPointView(i,j,k);
745                    break;
746                case 4 :
747                    for( i=0; i<dataPointShape[0]; i++ )
748                        for( j=0; j<dataPointShape[1]; j++ )
749                            for( k=0; k<dataPointShape[2]; k++ )
750                                for( l=0; l<dataPointShape[3]; l++, pos++ )
751                                    tmpData[pos]=dataPointView(i,j,k,l);
752                    break;
753            }
754                }
755        }
756            #ifdef PASO_MPI
757            // broadcast the data to all other processes
758        MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
759            #endif
760    
761        // unpack the data
762        switch( dataPointRank ){
763            case 0 :
764                numArray[0]=tmpData[0];
765                break;
766            case 1 :        
767                for( i=0; i<dataPointShape[0]; i++ )
768                    numArray[i]=tmpData[i];
769                break;
770            case 2 :        
771                for( i=0; i<dataPointShape[0]; i++ )
772                    for( j=0; j<dataPointShape[1]; j++ )
773                       numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
774                break;
775            case 3 :        
776                for( i=0; i<dataPointShape[0]; i++ )
777                    for( j=0; j<dataPointShape[1]; j++ )
778                        for( k=0; k<dataPointShape[2]; k++ )
779                            numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
780                break;
781            case 4 :
782                for( i=0; i<dataPointShape[0]; i++ )
783                    for( j=0; j<dataPointShape[1]; j++ )
784                        for( k=0; k<dataPointShape[2]; k++ )
785                            for( l=0; l<dataPointShape[3]; l++ )
786                                    numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
787                break;
788        }
789    
790        delete [] tmpData;  
791    //    //
792    // return the loaded array    // return the loaded array
793    return numArray;    return numArray;
794  }  }
795    
796    
797    
798  boost::python::numeric::array  boost::python::numeric::array
799  Data::integrate() const  Data::integrate() const
800  {  {
# Line 855  Data::integrate() const Line 802  Data::integrate() const
802    int rank = getDataPointRank();    int rank = getDataPointRank();
803    DataArrayView::ShapeType shape = getDataPointShape();    DataArrayView::ShapeType shape = getDataPointShape();
804    
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
805    
806    //    //
807    // calculate the integral values    // calculate the integral values
# Line 922  Data::integrate() const Line 866  Data::integrate() const
866  Data  Data
867  Data::sin() const  Data::sin() const
868  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
869    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);
870  }  }
871    
872  Data  Data
873  Data::cos() const  Data::cos() const
874  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
875    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);
876  }  }
877    
878  Data  Data
879  Data::tan() const  Data::tan() const
880  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
881    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);
882  }  }
883    
884  Data  Data
885  Data::asin() const  Data::asin() const
886  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
887    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);
888  }  }
889    
890  Data  Data
891  Data::acos() const  Data::acos() const
892  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
893    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);
894  }  }
895    
896    
897  Data  Data
898  Data::atan() const  Data::atan() const
899  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
900    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);
901  }  }
902    
903  Data  Data
904  Data::sinh() const  Data::sinh() const
905  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
906    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);
907  }  }
908    
909  Data  Data
910  Data::cosh() const  Data::cosh() const
911  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
912    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);
913  }  }
914    
915  Data  Data
916  Data::tanh() const  Data::tanh() const
917  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
918    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);
919  }  }
920    
921    
922  Data  Data
923  Data::asinh() const  Data::erf() const
924  {  {
925  #if defined DOPROF  #ifdef _WIN32
926    profData->unary++;    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
927    #else
928      return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);
929  #endif  #endif
930    }
931    
932    Data
933    Data::asinh() const
934    {
935    #ifdef _WIN32
936      return escript::unaryOp(*this,escript::asinh_substitute);
937    #else
938    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);
939    #endif
940  }  }
941    
942  Data  Data
943  Data::acosh() const  Data::acosh() const
944  {  {
945  #if defined DOPROF  #ifdef _WIN32
946    profData->unary++;    return escript::unaryOp(*this,escript::acosh_substitute);
947  #endif  #else
948    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);
949    #endif
950  }  }
951    
952  Data  Data
953  Data::atanh() const  Data::atanh() const
954  {  {
955  #if defined DOPROF  #ifdef _WIN32
956    profData->unary++;    return escript::unaryOp(*this,escript::atanh_substitute);
957  #endif  #else
958    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);
959    #endif
960  }  }
961    
962  Data  Data
963  Data::log10() const  Data::log10() const
964  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
965    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);
966  }  }
967    
968  Data  Data
969  Data::log() const  Data::log() const
970  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
971    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);
972  }  }
973    
974  Data  Data
975  Data::sign() const  Data::sign() const
976  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
977    return escript::unaryOp(*this,escript::fsign);    return escript::unaryOp(*this,escript::fsign);
978  }  }
979    
980  Data  Data
981  Data::abs() const  Data::abs() const
982  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
983    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);
984  }  }
985    
986  Data  Data
987  Data::neg() const  Data::neg() const
988  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
989    return escript::unaryOp(*this,negate<double>());    return escript::unaryOp(*this,negate<double>());
990  }  }
991    
992  Data  Data
993  Data::pos() const  Data::pos() const
994  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
995    Data result;    Data result;
996    // perform a deep copy    // perform a deep copy
997    result.copy(*this);    result.copy(*this);
# Line 1087  Data::pos() const Line 1001  Data::pos() const
1001  Data  Data
1002  Data::exp() const  Data::exp() const
1003  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1004    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);
1005  }  }
1006    
1007  Data  Data
1008  Data::sqrt() const  Data::sqrt() const
1009  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1010    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);
1011  }  }
1012    
1013  double  double
1014  Data::Lsup() const  Data::Lsup() const
1015  {  {
1016  #if defined DOPROF    double localValue, globalValue;
   profData->reduction1++;  
 #endif  
1017    //    //
1018    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1019    
1020    AbsMax abs_max_func;    AbsMax abs_max_func;
1021    return algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1022    #ifdef PASO_MPI
1023      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1024     return globalValue;
1025    #else
1026      return localValue;
1027    #endif
1028  }  }
1029    
1030  double  double
1031  Data::Linf() const  Data::Linf() const
1032  {  {
1033  #if defined DOPROF    double localValue, globalValue;
   profData->reduction1++;  
 #endif  
1034    //    //
1035    // set the initial absolute minimum value to max double    // set the initial absolute minimum value to max double
1036    AbsMin abs_min_func;    AbsMin abs_min_func;
1037    return algorithm(abs_min_func,numeric_limits<double>::max());    localValue = algorithm(abs_min_func,numeric_limits<double>::max());
1038    
1039    #ifdef PASO_MPI
1040      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1041      return globalValue;
1042    #else
1043      return localValue;
1044    #endif
1045  }  }
1046    
1047  double  double
1048  Data::sup() const  Data::sup() const
1049  {  {
1050  #if defined DOPROF    double localValue, globalValue;
   profData->reduction1++;  
 #endif  
1051    //    //
1052    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1053    FMax fmax_func;    FMax fmax_func;
1054    return algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1055    #ifdef PASO_MPI
1056      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1057      return globalValue;
1058    #else
1059      return localValue;
1060    #endif
1061  }  }
1062    
1063  double  double
1064  Data::inf() const  Data::inf() const
1065  {  {
1066  #if defined DOPROF    double localValue, globalValue;
   profData->reduction1++;  
 #endif  
1067    //    //
1068    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1069    FMin fmin_func;    FMin fmin_func;
1070    return algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1071    #ifdef PASO_MPI
1072      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1073      return globalValue;
1074    #else
1075      return localValue;
1076    #endif
1077  }  }
1078    
1079    /* TODO */
1080    /* global reduction */
1081  Data  Data
1082  Data::maxval() const  Data::maxval() const
1083  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1084    //    //
1085    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1086    FMax fmax_func;    FMax fmax_func;
# Line 1165  Data::maxval() const Line 1090  Data::maxval() const
1090  Data  Data
1091  Data::minval() const  Data::minval() const
1092  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1093    //    //
1094    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1095    FMin fmin_func;    FMin fmin_func;
# Line 1175  Data::minval() const Line 1097  Data::minval() const
1097  }  }
1098    
1099  Data  Data
1100  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1101  {  {
1102  #if defined DOPROF       int axis0_tmp,axis1_tmp;
1103    profData->reduction2++;       DataArrayView::ShapeType s=getDataPointShape();
1104  #endif       DataArrayView::ShapeType ev_shape;
1105    Trace trace_func;       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1106    return dp_algorithm(trace_func,0);       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1107         int rank=getDataPointRank();
1108         if (rank<2) {
1109            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1110         }
1111         if (axis0<0 || axis0>rank-1) {
1112            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1113         }
1114         if (axis1<0 || axis1>rank-1) {
1115             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1116         }
1117         if (axis0 == axis1) {
1118             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1119         }
1120         if (axis0 > axis1) {
1121             axis0_tmp=axis1;
1122             axis1_tmp=axis0;
1123         } else {
1124             axis0_tmp=axis0;
1125             axis1_tmp=axis1;
1126         }
1127         for (int i=0; i<rank; i++) {
1128           if (i == axis0_tmp) {
1129              ev_shape.push_back(s[axis1_tmp]);
1130           } else if (i == axis1_tmp) {
1131              ev_shape.push_back(s[axis0_tmp]);
1132           } else {
1133              ev_shape.push_back(s[i]);
1134           }
1135         }
1136         Data ev(0.,ev_shape,getFunctionSpace());
1137         ev.typeMatchRight(*this);
1138         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1139         return ev;
1140    
1141    }
1142    
1143    Data
1144    Data::symmetric() const
1145    {
1146         // check input
1147         DataArrayView::ShapeType s=getDataPointShape();
1148         if (getDataPointRank()==2) {
1149            if(s[0] != s[1])
1150               throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");
1151         }
1152         else if (getDataPointRank()==4) {
1153            if(!(s[0] == s[2] && s[1] == s[3]))
1154               throw DataException("Error - Data::symmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1155         }
1156         else {
1157            throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1158         }
1159         Data ev(0.,getDataPointShape(),getFunctionSpace());
1160         ev.typeMatchRight(*this);
1161         m_data->symmetric(ev.m_data.get());
1162         return ev;
1163    }
1164    
1165    Data
1166    Data::nonsymmetric() const
1167    {
1168         // check input
1169         DataArrayView::ShapeType s=getDataPointShape();
1170         if (getDataPointRank()==2) {
1171            if(s[0] != s[1])
1172               throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");
1173            DataArrayView::ShapeType ev_shape;
1174            ev_shape.push_back(s[0]);
1175            ev_shape.push_back(s[1]);
1176            Data ev(0.,ev_shape,getFunctionSpace());
1177            ev.typeMatchRight(*this);
1178            m_data->nonsymmetric(ev.m_data.get());
1179            return ev;
1180         }
1181         else if (getDataPointRank()==4) {
1182            if(!(s[0] == s[2] && s[1] == s[3]))
1183               throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1184            DataArrayView::ShapeType ev_shape;
1185            ev_shape.push_back(s[0]);
1186            ev_shape.push_back(s[1]);
1187            ev_shape.push_back(s[2]);
1188            ev_shape.push_back(s[3]);
1189            Data ev(0.,ev_shape,getFunctionSpace());
1190            ev.typeMatchRight(*this);
1191            m_data->nonsymmetric(ev.m_data.get());
1192            return ev;
1193         }
1194         else {
1195            throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 or 4 object.");
1196         }
1197    }
1198    
1199    Data
1200    Data::trace(int axis_offset) const
1201    {
1202         DataArrayView::ShapeType s=getDataPointShape();
1203         if (getDataPointRank()==2) {
1204            DataArrayView::ShapeType ev_shape;
1205            Data ev(0.,ev_shape,getFunctionSpace());
1206            ev.typeMatchRight(*this);
1207            m_data->trace(ev.m_data.get(), axis_offset);
1208            return ev;
1209         }
1210         if (getDataPointRank()==3) {
1211            DataArrayView::ShapeType ev_shape;
1212            if (axis_offset==0) {
1213              int s2=s[2];
1214              ev_shape.push_back(s2);
1215            }
1216            else if (axis_offset==1) {
1217              int s0=s[0];
1218              ev_shape.push_back(s0);
1219            }
1220            Data ev(0.,ev_shape,getFunctionSpace());
1221            ev.typeMatchRight(*this);
1222            m_data->trace(ev.m_data.get(), axis_offset);
1223            return ev;
1224         }
1225         if (getDataPointRank()==4) {
1226            DataArrayView::ShapeType ev_shape;
1227            if (axis_offset==0) {
1228              ev_shape.push_back(s[2]);
1229              ev_shape.push_back(s[3]);
1230            }
1231            else if (axis_offset==1) {
1232              ev_shape.push_back(s[0]);
1233              ev_shape.push_back(s[3]);
1234            }
1235        else if (axis_offset==2) {
1236          ev_shape.push_back(s[0]);
1237          ev_shape.push_back(s[1]);
1238        }
1239            Data ev(0.,ev_shape,getFunctionSpace());
1240            ev.typeMatchRight(*this);
1241        m_data->trace(ev.m_data.get(), axis_offset);
1242            return ev;
1243         }
1244         else {
1245            throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1246         }
1247  }  }
1248    
1249  Data  Data
1250  Data::transpose(int axis) const  Data::transpose(int axis_offset) const
1251    {
1252         DataArrayView::ShapeType s=getDataPointShape();
1253         DataArrayView::ShapeType ev_shape;
1254         // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1255         // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1256         int rank=getDataPointRank();
1257         if (axis_offset<0 || axis_offset>rank) {
1258            throw DataException("Error - Data::transpose must have 0 <= axis_offset <= rank=" + rank);
1259         }
1260         for (int i=0; i<rank; i++) {
1261           int index = (axis_offset+i)%rank;
1262           ev_shape.push_back(s[index]); // Append to new shape
1263         }
1264         Data ev(0.,ev_shape,getFunctionSpace());
1265         ev.typeMatchRight(*this);
1266         m_data->transpose(ev.m_data.get(), axis_offset);
1267         return ev;
1268    }
1269    
1270    Data
1271    Data::eigenvalues() const
1272    {
1273         // check input
1274         DataArrayView::ShapeType s=getDataPointShape();
1275         if (getDataPointRank()!=2)
1276            throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");
1277         if(s[0] != s[1])
1278            throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");
1279         // create return
1280         DataArrayView::ShapeType ev_shape(1,s[0]);
1281         Data ev(0.,ev_shape,getFunctionSpace());
1282         ev.typeMatchRight(*this);
1283         m_data->eigenvalues(ev.m_data.get());
1284         return ev;
1285    }
1286    
1287    const boost::python::tuple
1288    Data::eigenvalues_and_eigenvectors(const double tol) const
1289  {  {
1290  #if defined DOPROF       DataArrayView::ShapeType s=getDataPointShape();
1291    profData->reduction2++;       if (getDataPointRank()!=2)
1292  #endif          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
1293    // not implemented       if(s[0] != s[1])
1294    throw DataException("Error - Data::transpose not implemented yet.");          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for object with equal first and second dimension.");
1295    return Data();       // create return
1296         DataArrayView::ShapeType ev_shape(1,s[0]);
1297         Data ev(0.,ev_shape,getFunctionSpace());
1298         ev.typeMatchRight(*this);
1299         DataArrayView::ShapeType V_shape(2,s[0]);
1300         Data V(0.,V_shape,getFunctionSpace());
1301         V.typeMatchRight(*this);
1302         m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
1303         return make_tuple(boost::python::object(ev),boost::python::object(V));
1304  }  }
1305    
1306  const boost::python::tuple  const boost::python::tuple
1307  Data::mindp() const  Data::minGlobalDataPoint() const
1308  {  {
1309    // NB: calc_mindp had to be split off from mindp as boost::make_tuple causes an    // NB: calc_minGlobalDataPoint( had to be split off from minGlobalDataPoint( as boost::make_tuple causes an
1310    // abort (for unknown reasons) if there are openmp directives with it in the    // abort (for unknown reasons) if there are openmp directives with it in the
1311    // surrounding function    // surrounding function
1312    
   int SampleNo;  
1313    int DataPointNo;    int DataPointNo;
1314      int ProcNo;
1315    calc_mindp(SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1316      return make_tuple(ProcNo,DataPointNo);
   return make_tuple(SampleNo,DataPointNo);  
1317  }  }
1318    
1319  void  void
1320  Data::calc_mindp(int& SampleNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1321                   int& DataPointNo) const                          int& DataPointNo) const
1322  {  {
1323    int i,j;    int i,j;
1324    int lowi=0,lowj=0;    int lowi=0,lowj=0;
# Line 1248  Data::calc_mindp(int& SampleNo, Line 1354  Data::calc_mindp(int& SampleNo,
1354      }      }
1355    }    }
1356    
1357    SampleNo = lowi;  #ifdef PASO_MPI
1358    DataPointNo = lowj;      // determine the processor on which the minimum occurs
1359        next = temp.getDataPoint(lowi,lowj)();
1360        int lowProc = 0;
1361        double *globalMins = new double[get_MPISize()+1];
1362        int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1363        
1364        if( get_MPIRank()==0 ){
1365            next = globalMins[lowProc];
1366            for( i=1; i<get_MPISize(); i++ )
1367                if( next>globalMins[i] ){
1368                    lowProc = i;
1369                    next = globalMins[i];
1370                }
1371        }
1372        MPI_Bcast( &lowProc, 1, MPI_DOUBLE, 0, get_MPIComm() );
1373    
1374        delete [] globalMins;
1375        ProcNo = lowProc;
1376    #else
1377        ProcNo = 0;
1378    #endif
1379      DataPointNo = lowj + lowi * numDPPSample;
1380  }  }
1381    
1382  void  void
# Line 1273  Data::saveVTK(std::string fileName) cons Line 1400  Data::saveVTK(std::string fileName) cons
1400  Data&  Data&
1401  Data::operator+=(const Data& right)  Data::operator+=(const Data& right)
1402  {  {
1403  #if defined DOPROF    if (isProtected()) {
1404    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1405  #endif    }
1406    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1407    return (*this);    return (*this);
1408  }  }
# Line 1283  Data::operator+=(const Data& right) Line 1410  Data::operator+=(const Data& right)
1410  Data&  Data&
1411  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
1412  {  {
1413  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1414    profData->binary++;    binaryOp(tmp,plus<double>());
 #endif  
   binaryOp(right,plus<double>());  
1415    return (*this);    return (*this);
1416  }  }
1417    
1418  Data&  Data&
1419  Data::operator-=(const Data& right)  Data::operator-=(const Data& right)
1420  {  {
1421  #if defined DOPROF    if (isProtected()) {
1422    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1423  #endif    }
1424    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1425    return (*this);    return (*this);
1426  }  }
# Line 1303  Data::operator-=(const Data& right) Line 1428  Data::operator-=(const Data& right)
1428  Data&  Data&
1429  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
1430  {  {
1431  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1432    profData->binary++;    binaryOp(tmp,minus<double>());
 #endif  
   binaryOp(right,minus<double>());  
1433    return (*this);    return (*this);
1434  }  }
1435    
1436  Data&  Data&
1437  Data::operator*=(const Data& right)  Data::operator*=(const Data& right)
1438  {  {
1439  #if defined DOPROF    if (isProtected()) {
1440    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1441  #endif    }
1442    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1443    return (*this);    return (*this);
1444  }  }
# Line 1323  Data::operator*=(const Data& right) Line 1446  Data::operator*=(const Data& right)
1446  Data&  Data&
1447  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1448  {  {
1449  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1450    profData->binary++;    binaryOp(tmp,multiplies<double>());
 #endif  
   binaryOp(right,multiplies<double>());  
1451    return (*this);    return (*this);
1452  }  }
1453    
1454  Data&  Data&
1455  Data::operator/=(const Data& right)  Data::operator/=(const Data& right)
1456  {  {
1457  #if defined DOPROF    if (isProtected()) {
1458    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1459  #endif    }
1460    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1461    return (*this);    return (*this);
1462  }  }
# Line 1343  Data::operator/=(const Data& right) Line 1464  Data::operator/=(const Data& right)
1464  Data&  Data&
1465  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
1466  {  {
1467  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1468    profData->binary++;    binaryOp(tmp,divides<double>());
 #endif  
   binaryOp(right,divides<double>());  
1469    return (*this);    return (*this);
1470  }  }
1471    
1472  Data  Data
1473    Data::rpowO(const boost::python::object& left) const
1474    {
1475      Data left_d(left,*this);
1476      return left_d.powD(*this);
1477    }
1478    
1479    Data
1480  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1481  {  {
1482  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1483    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1484  }  }
1485    
1486  Data  Data
1487  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1488  {  {
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1489    Data result;    Data result;
1490    result.copy(*this);    if (getDataPointRank()<right.getDataPointRank()) {
1491    result.binaryOp(right,(Data::BinaryDFunPtr)::pow);       result.copy(right);
1492         result.binaryOp(*this,escript::rpow);
1493      } else {
1494         result.copy(*this);
1495         result.binaryOp(right,(Data::BinaryDFunPtr)::pow);
1496      }
1497    return result;    return result;
1498  }  }
1499    
1500    
1501  //  //
1502  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1503  Data  Data
# Line 1382  escript::operator+(const Data& left, con Line 1506  escript::operator+(const Data& left, con
1506    Data result;    Data result;
1507    //    //
1508    // perform a deep copy    // perform a deep copy
1509    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1510    result+=right;       result.copy(right);
1511         result+=left;
1512      } else {
1513         result.copy(left);
1514         result+=right;
1515      }
1516    return result;    return result;
1517  }  }
1518    
# Line 1395  escript::operator-(const Data& left, con Line 1524  escript::operator-(const Data& left, con
1524    Data result;    Data result;
1525    //    //
1526    // perform a deep copy    // perform a deep copy
1527    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1528    result-=right;       result=right.neg();
1529         result+=left;
1530      } else {
1531         result.copy(left);
1532         result-=right;
1533      }
1534    return result;    return result;
1535  }  }
1536    
# Line 1408  escript::operator*(const Data& left, con Line 1542  escript::operator*(const Data& left, con
1542    Data result;    Data result;
1543    //    //
1544    // perform a deep copy    // perform a deep copy
1545    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1546    result*=right;       result.copy(right);
1547         result*=left;
1548      } else {
1549         result.copy(left);
1550         result*=right;
1551      }
1552    return result;    return result;
1553  }  }
1554    
# Line 1421  escript::operator/(const Data& left, con Line 1560  escript::operator/(const Data& left, con
1560    Data result;    Data result;
1561    //    //
1562    // perform a deep copy    // perform a deep copy
1563    result.copy(left);    if (left.getDataPointRank()<right.getDataPointRank()) {
1564    result/=right;       result=right.oneOver();
1565         result*=left;
1566      } else {
1567         result.copy(left);
1568         result/=right;
1569      }
1570    return result;    return result;
1571  }  }
1572    
# Line 1431  escript::operator/(const Data& left, con Line 1575  escript::operator/(const Data& left, con
1575  Data  Data
1576  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
1577  {  {
1578    //    return left+Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1579  }  }
1580    
1581  //  //
# Line 1447  escript::operator+(const Data& left, con Line 1583  escript::operator+(const Data& left, con
1583  Data  Data
1584  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
1585  {  {
1586    //    return left-Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
1587  }  }
1588    
1589  //  //
# Line 1463  escript::operator-(const Data& left, con Line 1591  escript::operator-(const Data& left, con
1591  Data  Data
1592  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
1593  {  {
1594    //    return left*Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
1595  }  }
1596    
1597  //  //
# Line 1479  escript::operator*(const Data& left, con Line 1599  escript::operator*(const Data& left, con
1599  Data  Data
1600  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
1601  {  {
1602    //    return left/Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
1603  }  }
1604    
1605  //  //
# Line 1495  escript::operator/(const Data& left, con Line 1607  escript::operator/(const Data& left, con
1607  Data  Data
1608  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
1609  {  {
1610    //    return Data(left,right.getFunctionSpace(),false)+right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result+=right;  
   return result;  
1611  }  }
1612    
1613  //  //
# Line 1508  escript::operator+(const boost::python:: Line 1615  escript::operator+(const boost::python::
1615  Data  Data
1616  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
1617  {  {
1618    //    return Data(left,right.getFunctionSpace(),false)-right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result-=right;  
   return result;  
1619  }  }
1620    
1621  //  //
# Line 1521  escript::operator-(const boost::python:: Line 1623  escript::operator-(const boost::python::
1623  Data  Data
1624  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
1625  {  {
1626    //    return Data(left,right.getFunctionSpace(),false)*right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result*=right;  
   return result;  
1627  }  }
1628    
1629  //  //
# Line 1534  escript::operator*(const boost::python:: Line 1631  escript::operator*(const boost::python::
1631  Data  Data
1632  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
1633  {  {
1634    //    return Data(left,right.getFunctionSpace(),false)/right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result/=right;  
   return result;  
1635  }  }
1636    
1637  //  //
# Line 1587  escript::operator/(const boost::python:: Line 1679  escript::operator/(const boost::python::
1679  //  return ret;  //  return ret;
1680  //}  //}
1681    
1682    /* TODO */
1683    /* global reduction */
1684  Data  Data
1685  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1686  {  {
# Line 1601  Data::getItem(const boost::python::objec Line 1695  Data::getItem(const boost::python::objec
1695    return getSlice(slice_region);    return getSlice(slice_region);
1696  }  }
1697    
1698    /* TODO */
1699    /* global reduction */
1700  Data  Data
1701  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataArrayView::RegionType& region) const
1702  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1703    return Data(*this,region);    return Data(*this,region);
1704  }  }
1705    
1706    /* TODO */
1707    /* global reduction */
1708  void  void
1709  Data::setItemO(const boost::python::object& key,  Data::setItemO(const boost::python::object& key,
1710                 const boost::python::object& value)                 const boost::python::object& value)
# Line 1618  Data::setItemO(const boost::python::obje Line 1713  Data::setItemO(const boost::python::obje
1713    setItemD(key,tempData);    setItemD(key,tempData);
1714  }  }
1715    
1716    /* TODO */
1717    /* global reduction */
1718  void  void
1719  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
1720                 const Data& value)                 const Data& value)
# Line 1635  Data::setItemD(const boost::python::obje Line 1732  Data::setItemD(const boost::python::obje
1732    }    }
1733  }  }
1734    
1735    /* TODO */
1736    /* global reduction */
1737  void  void
1738  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1739                 const DataArrayView::RegionType& region)                 const DataArrayView::RegionType& region)
1740  {  {
1741  #if defined DOPROF    if (isProtected()) {
1742    profData->slicing++;          throw DataException("Error - attempt to update protected Data object.");
1743  #endif    }
1744    Data tempValue(value);    Data tempValue(value);
1745    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
1746    typeMatchRight(tempValue);    typeMatchRight(tempValue);
# Line 1677  Data::typeMatchRight(const Data& right) Line 1776  Data::typeMatchRight(const Data& right)
1776  }  }
1777    
1778  void  void
1779    Data::setTaggedValueByName(std::string name,
1780                               const boost::python::object& value)
1781    {
1782         if (getFunctionSpace().getDomain().isValidTagName(name)) {
1783            int tagKey=getFunctionSpace().getDomain().getTag(name);
1784            setTaggedValue(tagKey,value);
1785         }
1786    }
1787    void
1788  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
1789                       const boost::python::object& value)                       const boost::python::object& value)
1790  {  {
1791      if (isProtected()) {
1792            throw DataException("Error - attempt to update protected Data object.");
1793      }
1794    //    //
1795    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1796    tag();    tag();
# Line 1701  void Line 1812  void
1812  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
1813                              const DataArrayView& value)                              const DataArrayView& value)
1814  {  {
1815      if (isProtected()) {
1816            throw DataException("Error - attempt to update protected Data object.");
1817      }
1818    //    //
1819    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1820    tag();    tag();
# Line 1721  Data::getTagNumber(int dpno) Line 1835  Data::getTagNumber(int dpno)
1835  }  }
1836    
1837  void  void
 Data::setRefValue(int ref,  
                   const boost::python::numeric::array& value)  
 {  
   //  
   // Construct DataArray from boost::python::object input value  
   DataArray valueDataArray(value);  
   
   //  
   // Call DataAbstract::setRefValue  
   m_data->setRefValue(ref,valueDataArray);  
 }  
   
 void  
 Data::getRefValue(int ref,  
                   boost::python::numeric::array& value)  
 {  
   //  
   // Construct DataArray for boost::python::object return value  
   DataArray valueDataArray(value);  
   
   //  
   // Load DataArray with values from data-points specified by ref  
   m_data->getRefValue(ref,valueDataArray);  
   
   //  
   // Load values from valueDataArray into return numarray  
   
   // extract the shape of the numarray  
   int rank = value.getrank();  
   DataArrayView::ShapeType shape;  
   for (int i=0; i < rank; i++) {  
     shape.push_back(extract<int>(value.getshape()[i]));  
   }  
   
   // and load the numarray with the data from the DataArray  
   DataArrayView valueView = valueDataArray.getView();  
   
   if (rank==0) {  
       boost::python::numeric::array temp_numArray(valueView());  
       value = temp_numArray;  
   }  
   if (rank==1) {  
     for (int i=0; i < shape[0]; i++) {  
       value[i] = valueView(i);  
     }  
   }  
   if (rank==2) {  
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         value[i][j] = valueView(i,j);  
       }  
     }  
   }  
   if (rank==3) {  
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         for (int k=0; k < shape[2]; k++) {  
           value[i][j][k] = valueView(i,j,k);  
         }  
       }  
     }  
   }  
   if (rank==4) {  
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         for (int k=0; k < shape[2]; k++) {  
           for (int l=0; l < shape[3]; l++) {  
             value[i][j][k][l] = valueView(i,j,k,l);  
           }  
         }  
       }  
     }  
   }  
   
 }  
   
 void  
1838  Data::archiveData(const std::string fileName)  Data::archiveData(const std::string fileName)
1839  {  {
1840    cout << "Archiving Data object to: " << fileName << endl;    cout << "Archiving Data object to: " << fileName << endl;
# Line 1836  Data::archiveData(const std::string file Line 1873  Data::archiveData(const std::string file
1873    int dataPointSize = getDataPointSize();    int dataPointSize = getDataPointSize();
1874    int dataLength = getLength();    int dataLength = getLength();
1875    DataArrayView::ShapeType dataPointShape = getDataPointShape();    DataArrayView::ShapeType dataPointShape = getDataPointShape();
1876    int referenceNumbers[noSamples];    vector<int> referenceNumbers(noSamples);
1877    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1878      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);
1879    }    }
1880    int tagNumbers[noSamples];    vector<int> tagNumbers(noSamples);
1881    if (isTagged()) {    if (isTagged()) {
1882      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1883        tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);        tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);
# Line 1994  Data::extractData(const std::string file Line 2031  Data::extractData(const std::string file
2031        dataPointShape.push_back(flatShape[dim]);        dataPointShape.push_back(flatShape[dim]);
2032      }      }
2033    }    }
2034    int referenceNumbers[noSamples];    vector<int> referenceNumbers(noSamples);
2035    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2036      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));
2037    }    }
2038    int tagNumbers[noSamples];    vector<int> tagNumbers(noSamples);
2039    if (dataType==2) {    if (dataType==2) {
2040      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2041        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));
# Line 2047  Data::extractData(const std::string file Line 2084  Data::extractData(const std::string file
2084      throw DataException("extractData Error: incompatible FunctionSpace");      throw DataException("extractData Error: incompatible FunctionSpace");
2085    }    }
2086    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2087      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {      if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {
2088        throw DataException("extractData Error: incompatible FunctionSpace");        throw DataException("extractData Error: incompatible FunctionSpace");
2089      }      }
2090    }    }
# Line 2137  ostream& escript::operator<<(ostream& o, Line 2174  ostream& escript::operator<<(ostream& o,
2174    o << data.toString();    o << data.toString();
2175    return o;    return o;
2176  }  }
2177    
2178    Data
2179    escript::C_GeneralTensorProduct(Data& arg_0,
2180                         Data& arg_1,
2181                         int axis_offset,
2182                         int transpose)
2183    {
2184      // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2185      // SM is the product of the last axis_offset entries in arg_0.getShape().
2186    
2187    
2188      // Interpolate if necessary and find an appropriate function space
2189      Data arg_0_Z, arg_1_Z;
2190      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2191        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2192          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2193          arg_1_Z = Data(arg_1);
2194        }
2195        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2196          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2197          arg_0_Z =Data(arg_0);
2198        }
2199        else {
2200          throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2201        }
2202      } else {
2203          arg_0_Z = Data(arg_0);
2204          arg_1_Z = Data(arg_1);
2205      }
2206      // Get rank and shape of inputs
2207      int rank0 = arg_0_Z.getDataPointRank();
2208      int rank1 = arg_1_Z.getDataPointRank();
2209      DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();
2210      DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();
2211    
2212      // Prepare for the loops of the product and verify compatibility of shapes
2213      int start0=0, start1=0;
2214      if (transpose == 0)       {}
2215      else if (transpose == 1)  { start0 = axis_offset; }
2216      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2217      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2218    
2219      // Adjust the shapes for transpose
2220      DataArrayView::ShapeType tmpShape0;
2221      DataArrayView::ShapeType tmpShape1;
2222      for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }
2223      for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }
2224    
2225    #if 0
2226      // For debugging: show shape after transpose
2227      char tmp[100];
2228      std::string shapeStr;
2229      shapeStr = "(";
2230      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2231      shapeStr += ")";
2232      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2233      shapeStr = "(";
2234      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2235      shapeStr += ")";
2236      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2237    #endif
2238    
2239      // Prepare for the loops of the product
2240      int SL=1, SM=1, SR=1;
2241      for (int i=0; i<rank0-axis_offset; i++)   {
2242        SL *= tmpShape0[i];
2243      }
2244      for (int i=rank0-axis_offset; i<rank0; i++)   {
2245        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2246          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2247        }
2248        SM *= tmpShape0[i];
2249      }
2250      for (int i=axis_offset; i<rank1; i++)     {
2251        SR *= tmpShape1[i];
2252      }
2253    
2254      // Define the shape of the output
2255      DataArrayView::ShapeType shape2;
2256      for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z
2257      for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z
2258    
2259      // Declare output Data object
2260      Data res;
2261    
2262      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2263        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2264        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2265        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);
2266        double *ptr_2 = &((res.getPointDataView().getData())[0]);
2267        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2268      }
2269      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2270    
2271        // Prepare the DataConstant input
2272        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2273        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2274    
2275        // Borrow DataTagged input from Data object
2276        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2277        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2278    
2279        // Prepare a DataTagged output 2
2280        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2281        res.tag();
2282        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2283        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2284    
2285        // Prepare offset into DataConstant
2286        int offset_0 = tmp_0->getPointOffset(0,0);
2287        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2288        // Get the views
2289        DataArrayView view_1 = tmp_1->getDefaultValue();
2290        DataArrayView view_2 = tmp_2->getDefaultValue();
2291        // Get the pointers to the actual data
2292        double *ptr_1 = &((view_1.getData())[0]);
2293        double *ptr_2 = &((view_2.getData())[0]);
2294        // Compute an MVP for the default
2295        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2296        // Compute an MVP for each tag
2297        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2298        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2299        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2300          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2301          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2302          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2303          double *ptr_1 = &view_1.getData(0);
2304          double *ptr_2 = &view_2.getData(0);
2305          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2306        }
2307    
2308      }
2309      else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2310    
2311        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2312        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2313        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2314        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2315        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2316        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2317        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2318        int sampleNo_1,dataPointNo_1;
2319        int numSamples_1 = arg_1_Z.getNumSamples();
2320        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2321        int offset_0 = tmp_0->getPointOffset(0,0);
2322        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2323        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2324          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2325            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2326            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2327            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2328            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2329            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2330            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2331          }
2332        }
2333    
2334      }
2335      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2336    
2337        // Borrow DataTagged input from Data object
2338        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2339        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2340    
2341        // Prepare the DataConstant input
2342        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2343        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2344    
2345        // Prepare a DataTagged output 2
2346        res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2347        res.tag();
2348        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2349        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2350    
2351        // Prepare offset into DataConstant
2352        int offset_1 = tmp_1->getPointOffset(0,0);
2353        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2354        // Get the views
2355        DataArrayView view_0 = tmp_0->getDefaultValue();
2356        DataArrayView view_2 = tmp_2->getDefaultValue();
2357        // Get the pointers to the actual data
2358        double *ptr_0 = &((view_0.getData())[0]);
2359        double *ptr_2 = &((view_2.getData())[0]);
2360        // Compute an MVP for the default
2361        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2362        // Compute an MVP for each tag
2363        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2364        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2365        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2366          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2367          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2368          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2369          double *ptr_0 = &view_0.getData(0);
2370          double *ptr_2 = &view_2.getData(0);
2371          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2372        }
2373    
2374      }
2375      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2376    
2377        // Borrow DataTagged input from Data object
2378        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2379        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2380    
2381        // Borrow DataTagged input from Data object
2382        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2383        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2384    
2385        // Prepare a DataTagged output 2
2386        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2387        res.tag();  // DataTagged output
2388        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2389        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2390    
2391        // Get the views
2392        DataArrayView view_0 = tmp_0->getDefaultValue();
2393        DataArrayView view_1 = tmp_1->getDefaultValue();
2394        DataArrayView view_2 = tmp_2->getDefaultValue();
2395        // Get the pointers to the actual data
2396        double *ptr_0 = &((view_0.getData())[0]);
2397        double *ptr_1 = &((view_1.getData())[0]);
2398        double *ptr_2 = &((view_2.getData())[0]);
2399        // Compute an MVP for the default
2400        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2401        // Merge the tags
2402        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2403        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2404        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2405        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2406          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape
2407        }
2408        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2409          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2410        }
2411        // Compute an MVP for each tag
2412        const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2413        for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2414          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2415          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2416          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2417          double *ptr_0 = &view_0.getData(0);
2418          double *ptr_1 = &view_1.getData(0);
2419          double *ptr_2 = &view_2.getData(0);
2420          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2421        }
2422    
2423      }
2424      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2425    
2426        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2427        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2428        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2429        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2430        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2431        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2432        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2433        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2434        int sampleNo_0,dataPointNo_0;
2435        int numSamples_0 = arg_0_Z.getNumSamples();
2436        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2437        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2438        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2439          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2440          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2441          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2442            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2443            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2444            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2445            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2446            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2447          }
2448        }
2449    
2450      }
2451      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2452    
2453        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2454        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2455        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2456        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2457        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2458        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2459        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2460        int sampleNo_0,dataPointNo_0;
2461        int numSamples_0 = arg_0_Z.getNumSamples();
2462        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2463        int offset_1 = tmp_1->getPointOffset(0,0);
2464        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2465        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2466          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2467            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2468            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2469            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2470            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2471            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2472            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2473          }
2474        }
2475    
2476    
2477      }
2478      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2479    
2480        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2481        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2482        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2483        DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2484        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2485        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2486        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2487        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2488        int sampleNo_0,dataPointNo_0;
2489        int numSamples_0 = arg_0_Z.getNumSamples();
2490        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2491        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2492        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2493          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2494          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2495          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2496            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2497            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2498            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2499            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2500            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2501          }
2502        }
2503    
2504      }
2505      else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2506    
2507        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2508        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2509        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2510        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2511        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2512        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2513        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2514        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2515        int sampleNo_0,dataPointNo_0;
2516        int numSamples_0 = arg_0_Z.getNumSamples();
2517        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2518        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2519        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2520          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2521            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2522            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2523            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2524            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2525            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2526            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2527            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2528          }
2529        }
2530    
2531      }
2532      else {
2533        throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
2534      }
2535    
2536      return res;
2537    }
2538    
2539    DataAbstract*
2540    Data::borrowData() const
2541    {
2542      return m_data.get();
2543    }
2544    
2545    /* Member functions specific to the MPI implementation */
2546    
2547    void
2548    Data::print()
2549    {
2550      int i,j;
2551      
2552      printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2553      for( i=0; i<getNumSamples(); i++ )
2554      {
2555        printf( "[%6d]", i );
2556        for( j=0; j<getNumDataPointsPerSample(); j++ )
2557          printf( "\t%10.7g", (getSampleData(i))[j] );
2558        printf( "\n" );
2559      }
2560    }
2561    
2562    int
2563    Data::get_MPISize() const
2564    {
2565        int error, size;
2566    #ifdef PASO_MPI
2567        error = MPI_Comm_size( get_MPIComm(), &size );
2568    #else
2569        size = 1;
2570    #endif
2571        return size;
2572    }
2573    
2574    int
2575    Data::get_MPIRank() const
2576    {
2577        int error, rank;
2578    #ifdef PASO_MPI
2579        error = MPI_Comm_rank( get_MPIComm(), &rank );
2580    #else
2581        rank = 0;
2582    #endif
2583        return rank;
2584    }
2585    
2586    MPI_Comm
2587    Data::get_MPIComm() const
2588    {
2589    #ifdef PASO_MPI
2590        return MPI_COMM_WORLD;
2591    #else
2592        return -1;
2593    #endif
2594    }
2595    

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

  ViewVC Help
Powered by ViewVC 1.1.26