/[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 1693 by jfenwick, Fri Aug 8 04:22:58 2008 UTC
# Line 1  Line 1 
 // $Id$  
 /*=============================================================================  
1    
2   ******************************************************************************  /* $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.                       *  
  *                                                                            *  
  ******************************************************************************  
3    
4  ******************************************************************************/  /*******************************************************
5     *
6     *           Copyright 2003-2007 by ACceSS MNRF
7     *       Copyright 2007 by University of Queensland
8     *
9     *                http://esscc.uq.edu.au
10     *        Primary Business: Queensland, Australia
11     *  Licensed under the Open Software License version 3.0
12     *     http://www.opensource.org/licenses/osl-3.0.php
13     *
14     *******************************************************/
15    
16  #include "Data.h"  #include "Data.h"
17    
18  #include <iostream>  #include "DataExpanded.h"
19    #include "DataConstant.h"
20    #include "DataTagged.h"
21    #include "DataEmpty.h"
22    #include "DataArrayView.h"
23    #include "FunctionSpaceFactory.h"
24    #include "AbstractContinuousDomain.h"
25    #include "UnaryFuncs.h"
26    extern "C" {
27    #include "escript/blocktimer.h"
28    }
29    
30  #include <fstream>  #include <fstream>
31  #include <algorithm>  #include <algorithm>
32  #include <vector>  #include <vector>
 #include <exception>  
33  #include <functional>  #include <functional>
 #include <math.h>  
34    
35  #include <boost/python/dict.hpp>  #include <boost/python/dict.hpp>
 #include <boost/python/str.hpp>  
36  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
37  #include <boost/python/long.hpp>  #include <boost/python/long.hpp>
 #include <boost/python/tuple.hpp>  
   
 #include "DataException.h"  
 #include "DataExpanded.h"  
 #include "DataConstant.h"  
 #include "DataTagged.h"  
 #include "DataEmpty.h"  
 #include "DataArray.h"  
 #include "DataProf.h"  
 #include "FunctionSpaceFactory.h"  
 #include "AbstractContinuousDomain.h"  
 #include "UnaryFuncs.h"  
38    
39  using namespace std;  using namespace std;
40  using namespace boost::python;  using namespace boost::python;
41  using namespace boost;  using namespace boost;
42  using namespace escript;  using namespace escript;
43    
 #if defined DOPROF  
 //  
 // global table of profiling data for all Data objects  
 DataProf dataProfTable;  
 #endif  
   
44  Data::Data()  Data::Data()
45  {  {
46    //    //
# Line 60  Data::Data() Line 48  Data::Data()
48    DataAbstract* temp=new DataEmpty();    DataAbstract* temp=new DataEmpty();
49    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
50    m_data=temp_data;    m_data=temp_data;
51  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
52  }  }
53    
54  Data::Data(double value,  Data::Data(double value,
# Line 75  Data::Data(double value, Line 60  Data::Data(double value,
60    for (int i = 0; i < shape.attr("__len__")(); ++i) {    for (int i = 0; i < shape.attr("__len__")(); ++i) {
61      dataPointShape.push_back(extract<const int>(shape[i]));      dataPointShape.push_back(extract<const int>(shape[i]));
62    }    }
63    DataArray temp(dataPointShape,value);  
64    initialise(temp.getView(),what,expanded);    int len = DataArrayView::noValues(dataPointShape);
65  #if defined DOPROF    DataVector temp_data(len,value,len);
66    // create entry in global profiling table for this object    DataArrayView temp_dataView(temp_data, dataPointShape);
67    profData = dataProfTable.newData();  
68  #endif    initialise(temp_dataView, what, expanded);
69    
70      m_protected=false;
71  }  }
72    
73  Data::Data(double value,  Data::Data(double value,
# Line 88  Data::Data(double value, Line 75  Data::Data(double value,
75         const FunctionSpace& what,         const FunctionSpace& what,
76             bool expanded)             bool expanded)
77  {  {
78    DataArray temp(dataPointShape,value);    int len = DataArrayView::noValues(dataPointShape);
79    pair<int,int> dataShape=what.getDataShape();  
80    initialise(temp.getView(),what,expanded);    DataVector temp_data(len,value,len);
81  #if defined DOPROF    DataArrayView temp_dataView(temp_data, dataPointShape);
82    // create entry in global profiling table for this object  
83    profData = dataProfTable.newData();    initialise(temp_dataView, what, expanded);
84  #endif  
85      m_protected=false;
86  }  }
87    
88  Data::Data(const Data& inData)  Data::Data(const Data& inData)
89  {  {
90    m_data=inData.m_data;    m_data=inData.m_data;
91  #if defined DOPROF    m_protected=inData.isProtected();
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
92  }  }
93    
94  Data::Data(const Data& inData,  Data::Data(const Data& inData,
# Line 114  Data::Data(const Data& inData, Line 99  Data::Data(const Data& inData,
99    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = inData.m_data->getSlice(region);
100    shared_ptr<DataAbstract> temp_data(tmp);    shared_ptr<DataAbstract> temp_data(tmp);
101    m_data=temp_data;    m_data=temp_data;
102  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
103  }  }
104    
105  Data::Data(const Data& inData,  Data::Data(const Data& inData,
# Line 139  Data::Data(const Data& inData, Line 121  Data::Data(const Data& inData,
121      }      }
122      m_data=tmp.m_data;      m_data=tmp.m_data;
123    }    }
124  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
125  }  }
126    
127  Data::Data(const DataTagged::TagListType& tagKeys,  Data::Data(const DataTagged::TagListType& tagKeys,
# Line 154  Data::Data(const DataTagged::TagListType Line 133  Data::Data(const DataTagged::TagListType
133    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
134    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
135    m_data=temp_data;    m_data=temp_data;
136      m_protected=false;
137    if (expanded) {    if (expanded) {
138      expand();      expand();
139    }    }
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
140  }  }
141    
142  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 168  Data::Data(const numeric::array& value, Line 144  Data::Data(const numeric::array& value,
144             bool expanded)             bool expanded)
145  {  {
146    initialise(value,what,expanded);    initialise(value,what,expanded);
147  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
148  }  }
149    
150  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
# Line 179  Data::Data(const DataArrayView& value, Line 152  Data::Data(const DataArrayView& value,
152             bool expanded)             bool expanded)
153  {  {
154    initialise(value,what,expanded);    initialise(value,what,expanded);
155  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
156  }  }
157    
158  Data::Data(const object& value,  Data::Data(const object& value,
# Line 191  Data::Data(const object& value, Line 161  Data::Data(const object& value,
161  {  {
162    numeric::array asNumArray(value);    numeric::array asNumArray(value);
163    initialise(asNumArray,what,expanded);    initialise(asNumArray,what,expanded);
164  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
165  }  }
166    
167    
168  Data::Data(const object& value,  Data::Data(const object& value,
169             const Data& other)             const Data& other)
170  {  {
171    
172      numeric::array asNumArray(value);
173    
174    
175      // extract the shape of the numarray
176      DataArrayView::ShapeType tempShape;
177      for (int i=0; i < asNumArray.getrank(); i++) {
178        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
179      }
180      // get the space for the data vector
181      int len = DataArrayView::noValues(tempShape);
182      DataVector temp_data(len, 0.0, len);
183      DataArrayView temp_dataView(temp_data, tempShape);
184      temp_dataView.copy(asNumArray);
185    
186    //    //
187    // Create DataConstant using the given value and all other parameters    // Create DataConstant using the given value and all other parameters
188    // copied from other. If value is a rank 0 object this Data    // copied from other. If value is a rank 0 object this Data
189    // will assume the point data shape of other.    // will assume the point data shape of other.
190    DataArray temp(value);  
191    if (temp.getView().getRank()==0) {    if (temp_dataView.getRank()==0) {
192      //      int len = DataArrayView::noValues(other.getPointDataView().getShape());
193      // Create a DataArray with the scalar value for all elements  
194      DataArray temp2(other.getPointDataView().getShape(),temp.getView()());      DataVector temp2_data(len, temp_dataView(), len);
195      initialise(temp2.getView(),other.getFunctionSpace(),false);      DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
196        initialise(temp2_dataView, other.getFunctionSpace(), false);
197    
198    } else {    } else {
199      //      //
200      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
201      initialise(temp.getView(),other.getFunctionSpace(),false);      initialise(temp_dataView, other.getFunctionSpace(), false);
202    }    }
203  #if defined DOPROF    m_protected=false;
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
204  }  }
205    
206  Data::~Data()  Data::~Data()
# Line 261  Data::getShapeTuple() const Line 243  Data::getShapeTuple() const
243          throw DataException("Error - illegal Data rank.");          throw DataException("Error - illegal Data rank.");
244    }    }
245  }  }
   
246  void  void
247  Data::copy(const Data& other)  Data::copy(const Data& other)
248  {  {
# Line 314  Data::copy(const Data& other) Line 295  Data::copy(const Data& other)
295    throw DataException("Error - Copy not implemented for this Data type.");    throw DataException("Error - Copy not implemented for this Data type.");
296  }  }
297    
298    
299    void
300    Data::setToZero()
301    {
302      {
303        DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
304        if (temp!=0) {
305           temp->setToZero();
306           return;
307        }
308      }
309      {
310        DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
311        if (temp!=0) {
312          temp->setToZero();
313          return;
314        }
315      }
316      {
317        DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
318        if (temp!=0) {
319          temp->setToZero();
320          return;
321        }
322      }
323      throw DataException("Error - Data can not be set to zero.");
324    }
325    
326  void  void
327  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
328                     const Data& mask)                     const Data& mask)
# Line 360  Data::isConstant() const Line 369  Data::isConstant() const
369  }  }
370    
371  void  void
372    Data::setProtection()
373    {
374       m_protected=true;
375    }
376    
377    bool
378    Data::isProtected() const
379    {
380       return m_protected;
381    }
382    
383    
384    
385    void
386  Data::expand()  Data::expand()
387  {  {
388    if (isConstant()) {    if (isConstant()) {
# Line 401  Data::tag() Line 424  Data::tag()
424    }    }
425  }  }
426    
427  void  Data
428  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::oneOver() const
429  {  {
430    m_data->reshapeDataPoint(shape);    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
431  }  }
432    
433  Data  Data
434  Data::wherePositive() const  Data::wherePositive() const
435  {  {
436  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));  
437  }  }
438    
439  Data  Data
440  Data::whereNegative() const  Data::whereNegative() const
441  {  {
442  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(less<double>(),0.0));  
443  }  }
444    
445  Data  Data
446  Data::whereNonNegative() const  Data::whereNonNegative() const
447  {  {
448  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));  
449  }  }
450    
451  Data  Data
452  Data::whereNonPositive() const  Data::whereNonPositive() const
453  {  {
454  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));  
455  }  }
456    
457  Data  Data
458  Data::whereZero() const  Data::whereZero(double tol) const
459  {  {
460  #if defined DOPROF    Data dataAbs=abs();
461    profData->where++;    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
 #endif  
   return escript::unaryOp(*this,bind2nd(equal_to<double>(),0.0));  
462  }  }
463    
464  Data  Data
465  Data::whereNonZero() const  Data::whereNonZero(double tol) const
466  {  {
467  #if defined DOPROF    Data dataAbs=abs();
468    profData->where++;    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
 #endif  
   return escript::unaryOp(*this,bind2nd(not_equal_to<double>(),0.0));  
469  }  }
470    
471  Data  Data
472  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
473  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
474    return Data(*this,functionspace);    return Data(*this,functionspace);
475  }  }
476    
# Line 488  Data::probeInterpolation(const FunctionS Line 492  Data::probeInterpolation(const FunctionS
492  Data  Data
493  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
494  {  {
495  #if defined DOPROF    double blocktimer_start = blocktimer_time();
   profData->grad++;  
 #endif  
496    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
497      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
498    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();
499    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
500    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
501    getDomain().setToGradient(out,*this);    getDomain().setToGradient(out,*this);
502      blocktimer_increment("grad()", blocktimer_start);
503    return out;    return out;
504  }  }
505    
# Line 524  Data::getDataPointShape() const Line 527  Data::getDataPointShape() const
527    return getPointDataView().getShape();    return getPointDataView().getShape();
528  }  }
529    
 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");  
   
   //  
   // 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();  
   }  
530    
   //  
   // and copy over  
   m_data->copyAll(num_array);  
 }  
531    
532  const  const
533  boost::python::numeric::array  boost::python::numeric::array
534  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
535  {  {
536    //    size_t length=0;
537    // determine the total number of data points    int i, j, k, l;
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
   
538    //    //
539    // determine the rank and shape of each data point    // determine the rank and shape of each data point
540    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 570  Data::convertToNumArray() Line 545  Data::convertToNumArray()
545    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
546    
547    //    //
548    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
549    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
550    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
551    // 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]);  
   }  
552    
553    //    //
554    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
555      if (arrayRank==0) {
556        numArray.resize(1);
557      }
558    if (arrayRank==1) {    if (arrayRank==1) {
559      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
560    }    }
# Line 597  Data::convertToNumArray() Line 567  Data::convertToNumArray()
567    if (arrayRank==4) {    if (arrayRank==4) {
568      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
569    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
570    
571    //    if (getNumDataPointsPerSample()>0) {
572    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
573    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
574    int dataPoint = 0;         //
575    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
576      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
577        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
578        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);  
               }  
             }  
           }  
         }  
       }  
       dataPoint++;  
     }  
   }  
579    
580           //
581           // Check a valid data point number has been supplied
582           if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
583               throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
584           }
585           // TODO: global error handling
586           // create a view of the data if it is stored locally
587           DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
588    
589           switch( dataPointRank ){
590                case 0 :
591                    numArray[0] = dataPointView();
592                    break;
593                case 1 :
594                    for( i=0; i<dataPointShape[0]; i++ )
595                        numArray[i]=dataPointView(i);
596                    break;
597                case 2 :
598                    for( i=0; i<dataPointShape[0]; i++ )
599                        for( j=0; j<dataPointShape[1]; j++)
600                            numArray[make_tuple(i,j)]=dataPointView(i,j);
601                    break;
602                case 3 :
603                    for( i=0; i<dataPointShape[0]; i++ )
604                        for( j=0; j<dataPointShape[1]; j++ )
605                            for( k=0; k<dataPointShape[2]; k++)
606                                numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);
607                    break;
608                case 4 :
609                    for( i=0; i<dataPointShape[0]; i++ )
610                        for( j=0; j<dataPointShape[1]; j++ )
611                            for( k=0; k<dataPointShape[2]; k++ )
612                                for( l=0; l<dataPointShape[3]; l++)
613                                    numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);
614                    break;
615        }
616      }
617    //    //
618    // return the loaded array    // return the array
619    return numArray;    return numArray;
 }  
620    
621  const  }
622  boost::python::numeric::array  void
623  Data::convertToNumArrayFromSampleNo(int sampleNo)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
624  {  {
625    //      // this will throw if the value cannot be represented
626    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
627    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
628    
   //  
   // determine the number of data points per sample  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
629    
630    //  }
   // determine the rank and shape of each data point  
   int dataPointRank = getDataPointRank();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
631    
632    void
633    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
634    {
635      if (isProtected()) {
636            throw DataException("Error - attempt to update protected Data object.");
637      }
638    //    //
639    // create the numeric array to be returned    // check rank
640    boost::python::numeric::array numArray(0.0);    if (num_array.getrank()<getDataPointRank())
641          throw DataException("Rank of numarray does not match Data object rank");
642    
643    //    //
644    // the rank of the returned numeric array will be the rank of    // check shape of num_array
645    // the data points, plus one. Where the rank of the array is n,    for (int i=0; i<getDataPointRank(); i++) {
646    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
647    // 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]);  
648    }    }
   
649    //    //
650    // resize the numeric array to the shape just calculated    // make sure data is expanded:
651    if (arrayRank==1) {    if (!isExpanded()) {
652      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]);  
653    }    }
654    if (arrayRank==5) {    if (getNumDataPointsPerSample()>0) {
655      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);         int sampleNo = dataPointNo/getNumDataPointsPerSample();
656           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
657           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
658      } else {
659           m_data->copyToDataPoint(-1, 0,num_array);
660    }    }
661    }
662    
663    //  void
664    // loop through each data point in turn, loading the values for that data point  Data::setValueOfDataPoint(int dataPointNo, const double value)
665    // into the numeric array.  {
666    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    if (isProtected()) {
667      DataArrayView dataPointView = getDataPoint(sampleNo, dataPoint);          throw DataException("Error - attempt to update protected Data object.");
     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);  
             }  
           }  
         }  
       }  
     }  
668    }    }
   
669    //    //
670    // return the loaded array    // make sure data is expanded:
671    return numArray;    if (!isExpanded()) {
672        expand();
673      }
674      if (getNumDataPointsPerSample()>0) {
675           int sampleNo = dataPointNo/getNumDataPointsPerSample();
676           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
677           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
678      } else {
679           m_data->copyToDataPoint(-1, 0,value);
680      }
681  }  }
682    
683  const  const
684  boost::python::numeric::array  boost::python::numeric::array
685  Data::convertToNumArrayFromDPNo(int sampleNo,  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
                                 int dataPointNo)  
686  {  {
687    //    size_t length=0;
688    // Check a valid sample number has been supplied    int i, j, k, l, pos;
   if (sampleNo >= getNumSamples()) {  
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
   
   //  
   // Check a valid data point number has been supplied  
   if (dataPointNo >= getNumDataPointsPerSample()) {  
     throw DataException("Error - Data::convertToNumArray: invalid dataPointNo.");  
   }  
   
689    //    //
690    // determine the rank and shape of each data point    // determine the rank and shape of each data point
691    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 804  Data::convertToNumArrayFromDPNo(int samp Line 719  Data::convertToNumArrayFromDPNo(int samp
719      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
720    }    }
721    
722      // added for the MPI communication
723      length=1;
724      for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
725      double *tmpData = new double[length];
726    
727    //    //
728    // 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);  
           }  
         }  
       }  
     }  
   }  
729    
730        // updated for the MPI case
731        if( get_MPIRank()==procNo ){
732                 if (getNumDataPointsPerSample()>0) {
733                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
734                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
735                    //
736                    // Check a valid sample number has been supplied
737                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
738                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
739                    }
740    
741                    //
742                    // Check a valid data point number has been supplied
743                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
744                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
745                    }
746                    // TODO: global error handling
747            // create a view of the data if it is stored locally
748            DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
749    
750            // pack the data from the view into tmpData for MPI communication
751            pos=0;
752            switch( dataPointRank ){
753                case 0 :
754                    tmpData[0] = dataPointView();
755                    break;
756                case 1 :
757                    for( i=0; i<dataPointShape[0]; i++ )
758                        tmpData[i]=dataPointView(i);
759                    break;
760                case 2 :
761                    for( i=0; i<dataPointShape[0]; i++ )
762                        for( j=0; j<dataPointShape[1]; j++, pos++ )
763                            tmpData[pos]=dataPointView(i,j);
764                    break;
765                case 3 :
766                    for( i=0; i<dataPointShape[0]; i++ )
767                        for( j=0; j<dataPointShape[1]; j++ )
768                            for( k=0; k<dataPointShape[2]; k++, pos++ )
769                                tmpData[pos]=dataPointView(i,j,k);
770                    break;
771                case 4 :
772                    for( i=0; i<dataPointShape[0]; i++ )
773                        for( j=0; j<dataPointShape[1]; j++ )
774                            for( k=0; k<dataPointShape[2]; k++ )
775                                for( l=0; l<dataPointShape[3]; l++, pos++ )
776                                    tmpData[pos]=dataPointView(i,j,k,l);
777                    break;
778            }
779                }
780        }
781            #ifdef PASO_MPI
782            // broadcast the data to all other processes
783        MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
784            #endif
785    
786        // unpack the data
787        switch( dataPointRank ){
788            case 0 :
789                numArray[0]=tmpData[0];
790                break;
791            case 1 :
792                for( i=0; i<dataPointShape[0]; i++ )
793                    numArray[i]=tmpData[i];
794                break;
795            case 2 :
796                for( i=0; i<dataPointShape[0]; i++ )
797                    for( j=0; j<dataPointShape[1]; j++ )
798                       numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
799                break;
800            case 3 :
801                for( i=0; i<dataPointShape[0]; i++ )
802                    for( j=0; j<dataPointShape[1]; j++ )
803                        for( k=0; k<dataPointShape[2]; k++ )
804                            numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
805                break;
806            case 4 :
807                for( i=0; i<dataPointShape[0]; i++ )
808                    for( j=0; j<dataPointShape[1]; j++ )
809                        for( k=0; k<dataPointShape[2]; k++ )
810                            for( l=0; l<dataPointShape[3]; l++ )
811                                    numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
812                break;
813        }
814    
815        delete [] tmpData;
816    //    //
817    // return the loaded array    // return the loaded array
818    return numArray;    return numArray;
819  }  }
820    
821    
822    
823  boost::python::numeric::array  boost::python::numeric::array
824  Data::integrate() const  Data::integrate() const
825  {  {
826    int index;    int index;
827    int rank = getDataPointRank();    int rank = getDataPointRank();
828    DataArrayView::ShapeType shape = getDataPointShape();    DataArrayView::ShapeType shape = getDataPointShape();
829      int dataPointSize = getDataPointSize();
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
830    
831    //    //
832    // calculate the integral values    // calculate the integral values
833    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
834      vector<double> integrals_local(dataPointSize);
835    #ifdef PASO_MPI
836      AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals_local,*this);
837      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
838      double *tmp = new double[dataPointSize];
839      double *tmp_local = new double[dataPointSize];
840      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
841      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
842      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
843      delete[] tmp;
844      delete[] tmp_local;
845    #else
846    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);
847    #endif
848    
849    //    //
850    // create the numeric array to be returned    // create the numeric array to be returned
# Line 922  Data::integrate() const Line 904  Data::integrate() const
904  Data  Data
905  Data::sin() const  Data::sin() const
906  {  {
907  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);  
908  }  }
909    
910  Data  Data
911  Data::cos() const  Data::cos() const
912  {  {
913  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);  
914  }  }
915    
916  Data  Data
917  Data::tan() const  Data::tan() const
918  {  {
919  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);  
920  }  }
921    
922  Data  Data
923  Data::asin() const  Data::asin() const
924  {  {
925  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);  
926  }  }
927    
928  Data  Data
929  Data::acos() const  Data::acos() const
930  {  {
931  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);  
932  }  }
933    
934    
935  Data  Data
936  Data::atan() const  Data::atan() const
937  {  {
938  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);  
939  }  }
940    
941  Data  Data
942  Data::sinh() const  Data::sinh() const
943  {  {
944  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
945    profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);  
946  }  }
947    
948  Data  Data
949  Data::cosh() const  Data::cosh() const
950  {  {
951  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);  
952  }  }
953    
954  Data  Data
955  Data::tanh() const  Data::tanh() const
956  {  {
957  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
958    profData->unary++;  }
959    
960    
961    Data
962    Data::erf() const
963    {
964    #ifdef _WIN32
965      throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
966    #else
967      return C_TensorUnaryOperation(*this, ::erf);
968  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);  
969  }  }
970    
971  Data  Data
972  Data::asinh() const  Data::asinh() const
973  {  {
974  #if defined DOPROF  #ifdef _WIN32
975    profData->unary++;    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
976    #else
977      return C_TensorUnaryOperation(*this, ::asinh);
978  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);  
979  }  }
980    
981  Data  Data
982  Data::acosh() const  Data::acosh() const
983  {  {
984  #if defined DOPROF  #ifdef _WIN32
985    profData->unary++;    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
986    #else
987      return C_TensorUnaryOperation(*this, ::acosh);
988  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);  
989  }  }
990    
991  Data  Data
992  Data::atanh() const  Data::atanh() const
993  {  {
994  #if defined DOPROF  #ifdef _WIN32
995    profData->unary++;    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
996    #else
997      return C_TensorUnaryOperation(*this, ::atanh);
998  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);  
999  }  }
1000    
1001  Data  Data
1002  Data::log10() const  Data::log10() const
1003  {  {
1004  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);  
1005  }  }
1006    
1007  Data  Data
1008  Data::log() const  Data::log() const
1009  {  {
1010  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);  
1011  }  }
1012    
1013  Data  Data
1014  Data::sign() const  Data::sign() const
1015  {  {
1016  #if defined DOPROF    return C_TensorUnaryOperation(*this, escript::fsign);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,escript::fsign);  
1017  }  }
1018    
1019  Data  Data
1020  Data::abs() const  Data::abs() const
1021  {  {
1022  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);  
1023  }  }
1024    
1025  Data  Data
1026  Data::neg() const  Data::neg() const
1027  {  {
1028  #if defined DOPROF    return C_TensorUnaryOperation(*this, negate<double>());
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,negate<double>());  
1029  }  }
1030    
1031  Data  Data
1032  Data::pos() const  Data::pos() const
1033  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1034    Data result;    Data result;
1035    // perform a deep copy    // perform a deep copy
1036    result.copy(*this);    result.copy(*this);
# Line 1087  Data::pos() const Line 1040  Data::pos() const
1040  Data  Data
1041  Data::exp() const  Data::exp() const
1042  {  {
1043  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);  
1044  }  }
1045    
1046  Data  Data
1047  Data::sqrt() const  Data::sqrt() const
1048  {  {
1049  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);  
1050  }  }
1051    
1052  double  double
1053  Data::Lsup() const  Data::Lsup() const
1054  {  {
1055  #if defined DOPROF    double localValue;
   profData->reduction1++;  
 #endif  
1056    //    //
1057    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
   AbsMax abs_max_func;  
   return algorithm(abs_max_func,0);  
 }  
1058    
1059  double    AbsMax abs_max_func;
1060  Data::Linf() const    localValue = algorithm(abs_max_func,0);
1061  {  #ifdef PASO_MPI
1062  #if defined DOPROF    double globalValue;
1063    profData->reduction1++;    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1064      return globalValue;
1065    #else
1066      return localValue;
1067  #endif  #endif
   //  
   // set the initial absolute minimum value to max double  
   AbsMin abs_min_func;  
   return algorithm(abs_min_func,numeric_limits<double>::max());  
1068  }  }
1069    
1070  double  double
1071  Data::sup() const  Data::sup() const
1072  {  {
1073  #if defined DOPROF    double localValue;
   profData->reduction1++;  
 #endif  
1074    //    //
1075    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1076    FMax fmax_func;    FMax fmax_func;
1077    return algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1078    #ifdef PASO_MPI
1079      double globalValue;
1080      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1081      return globalValue;
1082    #else
1083      return localValue;
1084    #endif
1085  }  }
1086    
1087  double  double
1088  Data::inf() const  Data::inf() const
1089  {  {
1090  #if defined DOPROF    double localValue;
   profData->reduction1++;  
 #endif  
1091    //    //
1092    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1093    FMin fmin_func;    FMin fmin_func;
1094    return algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1095    #ifdef PASO_MPI
1096      double globalValue;
1097      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1098      return globalValue;
1099    #else
1100      return localValue;
1101    #endif
1102  }  }
1103    
1104    /* TODO */
1105    /* global reduction */
1106  Data  Data
1107  Data::maxval() const  Data::maxval() const
1108  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1109    //    //
1110    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1111    FMax fmax_func;    FMax fmax_func;
# Line 1165  Data::maxval() const Line 1115  Data::maxval() const
1115  Data  Data
1116  Data::minval() const  Data::minval() const
1117  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1118    //    //
1119    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1120    FMin fmin_func;    FMin fmin_func;
# Line 1175  Data::minval() const Line 1122  Data::minval() const
1122  }  }
1123    
1124  Data  Data
1125  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1126  {  {
1127  #if defined DOPROF       int axis0_tmp,axis1_tmp;
1128    profData->reduction2++;       DataArrayView::ShapeType s=getDataPointShape();
1129  #endif       DataArrayView::ShapeType ev_shape;
1130    Trace trace_func;       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1131    return dp_algorithm(trace_func,0);       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1132         int rank=getDataPointRank();
1133         if (rank<2) {
1134            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1135         }
1136         if (axis0<0 || axis0>rank-1) {
1137            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1138         }
1139         if (axis1<0 || axis1>rank-1) {
1140             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1141         }
1142         if (axis0 == axis1) {
1143             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1144         }
1145         if (axis0 > axis1) {
1146             axis0_tmp=axis1;
1147             axis1_tmp=axis0;
1148         } else {
1149             axis0_tmp=axis0;
1150             axis1_tmp=axis1;
1151         }
1152         for (int i=0; i<rank; i++) {
1153           if (i == axis0_tmp) {
1154              ev_shape.push_back(s[axis1_tmp]);
1155           } else if (i == axis1_tmp) {
1156              ev_shape.push_back(s[axis0_tmp]);
1157           } else {
1158              ev_shape.push_back(s[i]);
1159           }
1160         }
1161         Data ev(0.,ev_shape,getFunctionSpace());
1162         ev.typeMatchRight(*this);
1163         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1164         return ev;
1165    
1166    }
1167    
1168    Data
1169    Data::symmetric() const
1170    {
1171         // check input
1172         DataArrayView::ShapeType s=getDataPointShape();
1173         if (getDataPointRank()==2) {
1174            if(s[0] != s[1])
1175               throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");
1176         }
1177         else if (getDataPointRank()==4) {
1178            if(!(s[0] == s[2] && s[1] == s[3]))
1179               throw DataException("Error - Data::symmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1180         }
1181         else {
1182            throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1183         }
1184         Data ev(0.,getDataPointShape(),getFunctionSpace());
1185         ev.typeMatchRight(*this);
1186         m_data->symmetric(ev.m_data.get());
1187         return ev;
1188    }
1189    
1190    Data
1191    Data::nonsymmetric() const
1192    {
1193         // check input
1194         DataArrayView::ShapeType s=getDataPointShape();
1195         if (getDataPointRank()==2) {
1196            if(s[0] != s[1])
1197               throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");
1198            DataArrayView::ShapeType ev_shape;
1199            ev_shape.push_back(s[0]);
1200            ev_shape.push_back(s[1]);
1201            Data ev(0.,ev_shape,getFunctionSpace());
1202            ev.typeMatchRight(*this);
1203            m_data->nonsymmetric(ev.m_data.get());
1204            return ev;
1205         }
1206         else if (getDataPointRank()==4) {
1207            if(!(s[0] == s[2] && s[1] == s[3]))
1208               throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1209            DataArrayView::ShapeType ev_shape;
1210            ev_shape.push_back(s[0]);
1211            ev_shape.push_back(s[1]);
1212            ev_shape.push_back(s[2]);
1213            ev_shape.push_back(s[3]);
1214            Data ev(0.,ev_shape,getFunctionSpace());
1215            ev.typeMatchRight(*this);
1216            m_data->nonsymmetric(ev.m_data.get());
1217            return ev;
1218         }
1219         else {
1220            throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 or 4 object.");
1221         }
1222    }
1223    
1224    Data
1225    Data::trace(int axis_offset) const
1226    {
1227         DataArrayView::ShapeType s=getDataPointShape();
1228         if (getDataPointRank()==2) {
1229            DataArrayView::ShapeType ev_shape;
1230            Data ev(0.,ev_shape,getFunctionSpace());
1231            ev.typeMatchRight(*this);
1232            m_data->trace(ev.m_data.get(), axis_offset);
1233            return ev;
1234         }
1235         if (getDataPointRank()==3) {
1236            DataArrayView::ShapeType ev_shape;
1237            if (axis_offset==0) {
1238              int s2=s[2];
1239              ev_shape.push_back(s2);
1240            }
1241            else if (axis_offset==1) {
1242              int s0=s[0];
1243              ev_shape.push_back(s0);
1244            }
1245            Data ev(0.,ev_shape,getFunctionSpace());
1246            ev.typeMatchRight(*this);
1247            m_data->trace(ev.m_data.get(), axis_offset);
1248            return ev;
1249         }
1250         if (getDataPointRank()==4) {
1251            DataArrayView::ShapeType ev_shape;
1252            if (axis_offset==0) {
1253              ev_shape.push_back(s[2]);
1254              ev_shape.push_back(s[3]);
1255            }
1256            else if (axis_offset==1) {
1257              ev_shape.push_back(s[0]);
1258              ev_shape.push_back(s[3]);
1259            }
1260        else if (axis_offset==2) {
1261          ev_shape.push_back(s[0]);
1262          ev_shape.push_back(s[1]);
1263        }
1264            Data ev(0.,ev_shape,getFunctionSpace());
1265            ev.typeMatchRight(*this);
1266        m_data->trace(ev.m_data.get(), axis_offset);
1267            return ev;
1268         }
1269         else {
1270            throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1271         }
1272    }
1273    
1274    Data
1275    Data::transpose(int axis_offset) const
1276    {
1277         DataArrayView::ShapeType s=getDataPointShape();
1278         DataArrayView::ShapeType ev_shape;
1279         // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1280         // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1281         int rank=getDataPointRank();
1282         if (axis_offset<0 || axis_offset>rank) {
1283            throw DataException("Error - Data::transpose must have 0 <= axis_offset <= rank=" + rank);
1284         }
1285         for (int i=0; i<rank; i++) {
1286           int index = (axis_offset+i)%rank;
1287           ev_shape.push_back(s[index]); // Append to new shape
1288         }
1289         Data ev(0.,ev_shape,getFunctionSpace());
1290         ev.typeMatchRight(*this);
1291         m_data->transpose(ev.m_data.get(), axis_offset);
1292         return ev;
1293  }  }
1294    
1295  Data  Data
1296  Data::transpose(int axis) const  Data::eigenvalues() const
1297    {
1298         // check input
1299         DataArrayView::ShapeType s=getDataPointShape();
1300         if (getDataPointRank()!=2)
1301            throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");
1302         if(s[0] != s[1])
1303            throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");
1304         // create return
1305         DataArrayView::ShapeType ev_shape(1,s[0]);
1306         Data ev(0.,ev_shape,getFunctionSpace());
1307         ev.typeMatchRight(*this);
1308         m_data->eigenvalues(ev.m_data.get());
1309         return ev;
1310    }
1311    
1312    const boost::python::tuple
1313    Data::eigenvalues_and_eigenvectors(const double tol) const
1314  {  {
1315  #if defined DOPROF       DataArrayView::ShapeType s=getDataPointShape();
1316    profData->reduction2++;       if (getDataPointRank()!=2)
1317  #endif          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
1318    // not implemented       if(s[0] != s[1])
1319    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.");
1320    return Data();       // create return
1321         DataArrayView::ShapeType ev_shape(1,s[0]);
1322         Data ev(0.,ev_shape,getFunctionSpace());
1323         ev.typeMatchRight(*this);
1324         DataArrayView::ShapeType V_shape(2,s[0]);
1325         Data V(0.,V_shape,getFunctionSpace());
1326         V.typeMatchRight(*this);
1327         m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
1328         return make_tuple(boost::python::object(ev),boost::python::object(V));
1329  }  }
1330    
1331  const boost::python::tuple  const boost::python::tuple
1332  Data::mindp() const  Data::minGlobalDataPoint() const
1333  {  {
1334    // 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
1335    // 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
1336    // surrounding function    // surrounding function
1337    
   int SampleNo;  
1338    int DataPointNo;    int DataPointNo;
1339      int ProcNo;
1340    calc_mindp(SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1341      return make_tuple(ProcNo,DataPointNo);
   return make_tuple(SampleNo,DataPointNo);  
1342  }  }
1343    
1344  void  void
1345  Data::calc_mindp(int& SampleNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1346                   int& DataPointNo) const                          int& DataPointNo) const
1347  {  {
1348    int i,j;    int i,j;
1349    int lowi=0,lowj=0;    int lowi=0,lowj=0;
# Line 1248  Data::calc_mindp(int& SampleNo, Line 1379  Data::calc_mindp(int& SampleNo,
1379      }      }
1380    }    }
1381    
1382    SampleNo = lowi;  #ifdef PASO_MPI
1383    DataPointNo = lowj;      // determine the processor on which the minimum occurs
1384        next = temp.getDataPoint(lowi,lowj)();
1385        int lowProc = 0;
1386        double *globalMins = new double[get_MPISize()+1];
1387        int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1388    
1389        if( get_MPIRank()==0 ){
1390            next = globalMins[lowProc];
1391            for( i=1; i<get_MPISize(); i++ )
1392                if( next>globalMins[i] ){
1393                    lowProc = i;
1394                    next = globalMins[i];
1395                }
1396        }
1397        MPI_Bcast( &lowProc, 1, MPI_DOUBLE, 0, get_MPIComm() );
1398    
1399        delete [] globalMins;
1400        ProcNo = lowProc;
1401    #else
1402        ProcNo = 0;
1403    #endif
1404      DataPointNo = lowj + lowi * numDPPSample;
1405  }  }
1406    
1407  void  void
# Line 1273  Data::saveVTK(std::string fileName) cons Line 1425  Data::saveVTK(std::string fileName) cons
1425  Data&  Data&
1426  Data::operator+=(const Data& right)  Data::operator+=(const Data& right)
1427  {  {
1428  #if defined DOPROF    if (isProtected()) {
1429    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1430  #endif    }
1431    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1432    return (*this);    return (*this);
1433  }  }
# Line 1283  Data::operator+=(const Data& right) Line 1435  Data::operator+=(const Data& right)
1435  Data&  Data&
1436  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
1437  {  {
1438  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1439    profData->binary++;    binaryOp(tmp,plus<double>());
1440  #endif    return (*this);
1441    binaryOp(right,plus<double>());  }
1442    Data&
1443    Data::operator=(const Data& other)
1444    {
1445      copy(other);
1446    return (*this);    return (*this);
1447  }  }
1448    
1449  Data&  Data&
1450  Data::operator-=(const Data& right)  Data::operator-=(const Data& right)
1451  {  {
1452  #if defined DOPROF    if (isProtected()) {
1453    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1454  #endif    }
1455    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1456    return (*this);    return (*this);
1457  }  }
# Line 1303  Data::operator-=(const Data& right) Line 1459  Data::operator-=(const Data& right)
1459  Data&  Data&
1460  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
1461  {  {
1462  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1463    profData->binary++;    binaryOp(tmp,minus<double>());
 #endif  
   binaryOp(right,minus<double>());  
1464    return (*this);    return (*this);
1465  }  }
1466    
1467  Data&  Data&
1468  Data::operator*=(const Data& right)  Data::operator*=(const Data& right)
1469  {  {
1470  #if defined DOPROF    if (isProtected()) {
1471    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1472  #endif    }
1473    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1474    return (*this);    return (*this);
1475  }  }
# Line 1323  Data::operator*=(const Data& right) Line 1477  Data::operator*=(const Data& right)
1477  Data&  Data&
1478  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1479  {  {
1480  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1481    profData->binary++;    binaryOp(tmp,multiplies<double>());
 #endif  
   binaryOp(right,multiplies<double>());  
1482    return (*this);    return (*this);
1483  }  }
1484    
1485  Data&  Data&
1486  Data::operator/=(const Data& right)  Data::operator/=(const Data& right)
1487  {  {
1488  #if defined DOPROF    if (isProtected()) {
1489    profData->binary++;          throw DataException("Error - attempt to update protected Data object.");
1490  #endif    }
1491    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1492    return (*this);    return (*this);
1493  }  }
# Line 1343  Data::operator/=(const Data& right) Line 1495  Data::operator/=(const Data& right)
1495  Data&  Data&
1496  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
1497  {  {
1498  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1499    profData->binary++;    binaryOp(tmp,divides<double>());
 #endif  
   binaryOp(right,divides<double>());  
1500    return (*this);    return (*this);
1501  }  }
1502    
1503  Data  Data
1504    Data::rpowO(const boost::python::object& left) const
1505    {
1506      Data left_d(left,*this);
1507      return left_d.powD(*this);
1508    }
1509    
1510    Data
1511  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1512  {  {
1513  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1514    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1515  }  }
1516    
1517  Data  Data
1518  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1519  {  {
1520  #if defined DOPROF    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   profData->binary++;  
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1521  }  }
1522    
1523  //  //
# Line 1379  Data::powD(const Data& right) const Line 1525  Data::powD(const Data& right) const
1525  Data  Data
1526  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1527  {  {
1528    Data result;    return C_TensorBinaryOperation(left, right, plus<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1529  }  }
1530    
1531  //  //
# Line 1392  escript::operator+(const Data& left, con Line 1533  escript::operator+(const Data& left, con
1533  Data  Data
1534  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
1535  {  {
1536    Data result;    return C_TensorBinaryOperation(left, right, minus<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
1537  }  }
1538    
1539  //  //
# Line 1405  escript::operator-(const Data& left, con Line 1541  escript::operator-(const Data& left, con
1541  Data  Data
1542  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
1543  {  {
1544    Data result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
1545  }  }
1546    
1547  //  //
# Line 1418  escript::operator*(const Data& left, con Line 1549  escript::operator*(const Data& left, con
1549  Data  Data
1550  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
1551  {  {
1552    Data result;    return C_TensorBinaryOperation(left, right, divides<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
1553  }  }
1554    
1555  //  //
# Line 1431  escript::operator/(const Data& left, con Line 1557  escript::operator/(const Data& left, con
1557  Data  Data
1558  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
1559  {  {
1560    //    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;  
1561  }  }
1562    
1563  //  //
# Line 1447  escript::operator+(const Data& left, con Line 1565  escript::operator+(const Data& left, con
1565  Data  Data
1566  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
1567  {  {
1568    //    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;  
1569  }  }
1570    
1571  //  //
# Line 1463  escript::operator-(const Data& left, con Line 1573  escript::operator-(const Data& left, con
1573  Data  Data
1574  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
1575  {  {
1576    //    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;  
1577  }  }
1578    
1579  //  //
# Line 1479  escript::operator*(const Data& left, con Line 1581  escript::operator*(const Data& left, con
1581  Data  Data
1582  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
1583  {  {
1584    //    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;  
1585  }  }
1586    
1587  //  //
# Line 1495  escript::operator/(const Data& left, con Line 1589  escript::operator/(const Data& left, con
1589  Data  Data
1590  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
1591  {  {
1592    //    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;  
1593  }  }
1594    
1595  //  //
# Line 1508  escript::operator+(const boost::python:: Line 1597  escript::operator+(const boost::python::
1597  Data  Data
1598  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
1599  {  {
1600    //    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;  
1601  }  }
1602    
1603  //  //
# Line 1521  escript::operator-(const boost::python:: Line 1605  escript::operator-(const boost::python::
1605  Data  Data
1606  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
1607  {  {
1608    //    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;  
1609  }  }
1610    
1611  //  //
# Line 1534  escript::operator*(const boost::python:: Line 1613  escript::operator*(const boost::python::
1613  Data  Data
1614  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
1615  {  {
1616    //    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;  
1617  }  }
1618    
1619  //  //
# Line 1587  escript::operator/(const boost::python:: Line 1661  escript::operator/(const boost::python::
1661  //  return ret;  //  return ret;
1662  //}  //}
1663    
1664    /* TODO */
1665    /* global reduction */
1666  Data  Data
1667  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1668  {  {
1669    const DataArrayView& view=getPointDataView();    const DataArrayView& view=getPointDataView();
1670    
# Line 1601  Data::getItem(const boost::python::objec Line 1677  Data::getItem(const boost::python::objec
1677    return getSlice(slice_region);    return getSlice(slice_region);
1678  }  }
1679    
1680    /* TODO */
1681    /* global reduction */
1682  Data  Data
1683  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataArrayView::RegionType& region) const
1684  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1685    return Data(*this,region);    return Data(*this,region);
1686  }  }
1687    
1688    /* TODO */
1689    /* global reduction */
1690  void  void
1691  Data::setItemO(const boost::python::object& key,  Data::setItemO(const boost::python::object& key,
1692                 const boost::python::object& value)                 const boost::python::object& value)
# Line 1639  void Line 1716  void
1716  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1717                 const DataArrayView::RegionType& region)                 const DataArrayView::RegionType& region)
1718  {  {
1719  #if defined DOPROF    if (isProtected()) {
1720    profData->slicing++;          throw DataException("Error - attempt to update protected Data object.");
1721  #endif    }
1722    Data tempValue(value);    Data tempValue(value);
1723    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
1724    typeMatchRight(tempValue);    typeMatchRight(tempValue);
# Line 1677  Data::typeMatchRight(const Data& right) Line 1754  Data::typeMatchRight(const Data& right)
1754  }  }
1755    
1756  void  void
1757    Data::setTaggedValueByName(std::string name,
1758                               const boost::python::object& value)
1759    {
1760         if (getFunctionSpace().getDomain().isValidTagName(name)) {
1761            int tagKey=getFunctionSpace().getDomain().getTag(name);
1762            setTaggedValue(tagKey,value);
1763         }
1764    }
1765    void
1766  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
1767                       const boost::python::object& value)                       const boost::python::object& value)
1768  {  {
1769      if (isProtected()) {
1770            throw DataException("Error - attempt to update protected Data object.");
1771      }
1772    //    //
1773    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1774    tag();    if (isConstant()) tag();
1775    
1776    if (!isTagged()) {    numeric::array asNumArray(value);
1777      throw DataException("Error - DataTagged conversion failed!!");  
1778    
1779      // extract the shape of the numarray
1780      DataArrayView::ShapeType tempShape;
1781      for (int i=0; i < asNumArray.getrank(); i++) {
1782        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
1783    }    }
1784    
1785    //    // get the space for the data vector
1786    // Construct DataArray from boost::python::object input value    int len = DataArrayView::noValues(tempShape);
1787    DataArray valueDataArray(value);    DataVector temp_data(len, 0.0, len);
1788      DataArrayView temp_dataView(temp_data, tempShape);
1789      temp_dataView.copy(asNumArray);
1790    
1791    //    //
1792    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1793    m_data->setTaggedValue(tagKey,valueDataArray.getView());    m_data->setTaggedValue(tagKey,temp_dataView);
1794  }  }
1795    
1796  void  void
1797  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
1798                              const DataArrayView& value)                              const DataArrayView& value)
1799  {  {
1800      if (isProtected()) {
1801            throw DataException("Error - attempt to update protected Data object.");
1802      }
1803    //    //
1804    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1805    tag();    if (isConstant()) tag();
1806    
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
1807    //    //
1808    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1809    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,value);
# Line 1717  Data::setTaggedValueFromCPP(int tagKey, Line 1812  Data::setTaggedValueFromCPP(int tagKey,
1812  int  int
1813  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
1814  {  {
1815    return m_data->getTagNumber(dpno);    return getFunctionSpace().getTagFromDataPointNo(dpno);
 }  
   
 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);  
           }  
         }  
       }  
     }  
   }  
   
1816  }  }
1817    
1818  void  void
# Line 1836  Data::archiveData(const std::string file Line 1854  Data::archiveData(const std::string file
1854    int dataPointSize = getDataPointSize();    int dataPointSize = getDataPointSize();
1855    int dataLength = getLength();    int dataLength = getLength();
1856    DataArrayView::ShapeType dataPointShape = getDataPointShape();    DataArrayView::ShapeType dataPointShape = getDataPointShape();
1857    int referenceNumbers[noSamples];    vector<int> referenceNumbers(noSamples);
1858    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1859      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);
1860    }    }
1861    int tagNumbers[noSamples];    vector<int> tagNumbers(noSamples);
1862    if (isTagged()) {    if (isTagged()) {
1863      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
1864        tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);        tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);
# Line 1994  Data::extractData(const std::string file Line 2012  Data::extractData(const std::string file
2012        dataPointShape.push_back(flatShape[dim]);        dataPointShape.push_back(flatShape[dim]);
2013      }      }
2014    }    }
2015    int referenceNumbers[noSamples];    vector<int> referenceNumbers(noSamples);
2016    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2017      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));
2018    }    }
2019    int tagNumbers[noSamples];    vector<int> tagNumbers(noSamples);
2020    if (dataType==2) {    if (dataType==2) {
2021      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2022        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 2065  Data::extractData(const std::string file
2065      throw DataException("extractData Error: incompatible FunctionSpace");      throw DataException("extractData Error: incompatible FunctionSpace");
2066    }    }
2067    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {
2068      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {      if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {
2069        throw DataException("extractData Error: incompatible FunctionSpace");        throw DataException("extractData Error: incompatible FunctionSpace");
2070      }      }
2071    }    }
# Line 2137  ostream& escript::operator<<(ostream& o, Line 2155  ostream& escript::operator<<(ostream& o,
2155    o << data.toString();    o << data.toString();
2156    return o;    return o;
2157  }  }
2158    
2159    Data
2160    escript::C_GeneralTensorProduct(Data& arg_0,
2161                         Data& arg_1,
2162                         int axis_offset,
2163                         int transpose)
2164    {
2165      // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2166      // SM is the product of the last axis_offset entries in arg_0.getShape().
2167    
2168      // Interpolate if necessary and find an appropriate function space
2169      Data arg_0_Z, arg_1_Z;
2170      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2171        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2172          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2173          arg_1_Z = Data(arg_1);
2174        }
2175        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2176          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2177          arg_0_Z =Data(arg_0);
2178        }
2179        else {
2180          throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
2181        }
2182      } else {
2183          arg_0_Z = Data(arg_0);
2184          arg_1_Z = Data(arg_1);
2185      }
2186      // Get rank and shape of inputs
2187      int rank0 = arg_0_Z.getDataPointRank();
2188      int rank1 = arg_1_Z.getDataPointRank();
2189      DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();
2190      DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();
2191    
2192      // Prepare for the loops of the product and verify compatibility of shapes
2193      int start0=0, start1=0;
2194      if (transpose == 0)       {}
2195      else if (transpose == 1)  { start0 = axis_offset; }
2196      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2197      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2198    
2199      // Adjust the shapes for transpose
2200      DataArrayView::ShapeType tmpShape0;
2201      DataArrayView::ShapeType tmpShape1;
2202      for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }
2203      for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }
2204    
2205    #if 0
2206      // For debugging: show shape after transpose
2207      char tmp[100];
2208      std::string shapeStr;
2209      shapeStr = "(";
2210      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2211      shapeStr += ")";
2212      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2213      shapeStr = "(";
2214      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2215      shapeStr += ")";
2216      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2217    #endif
2218    
2219      // Prepare for the loops of the product
2220      int SL=1, SM=1, SR=1;
2221      for (int i=0; i<rank0-axis_offset; i++)   {
2222        SL *= tmpShape0[i];
2223      }
2224      for (int i=rank0-axis_offset; i<rank0; i++)   {
2225        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2226          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2227        }
2228        SM *= tmpShape0[i];
2229      }
2230      for (int i=axis_offset; i<rank1; i++)     {
2231        SR *= tmpShape1[i];
2232      }
2233    
2234      // Define the shape of the output
2235      DataArrayView::ShapeType shape2;
2236      for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z
2237      for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z
2238    
2239      // Declare output Data object
2240      Data res;
2241    
2242      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2243        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2244        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);
2245        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);
2246        double *ptr_2 = &((res.getPointDataView().getData())[0]);
2247        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2248      }
2249      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2250    
2251        // Prepare the DataConstant input
2252        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2253        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2254    
2255        // Borrow DataTagged input from Data object
2256        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2257        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2258    
2259        // Prepare a DataTagged output 2
2260        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2261        res.tag();
2262        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2263        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2264    
2265        // Prepare offset into DataConstant
2266        int offset_0 = tmp_0->getPointOffset(0,0);
2267        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2268        // Get the views
2269        DataArrayView view_1 = tmp_1->getDefaultValue();
2270        DataArrayView view_2 = tmp_2->getDefaultValue();
2271        // Get the pointers to the actual data
2272        double *ptr_1 = &((view_1.getData())[0]);
2273        double *ptr_2 = &((view_2.getData())[0]);
2274        // Compute an MVP for the default
2275        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2276        // Compute an MVP for each tag
2277        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2278        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2279        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2280          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2281          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2282          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2283          double *ptr_1 = &view_1.getData(0);
2284          double *ptr_2 = &view_2.getData(0);
2285          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2286        }
2287    
2288      }
2289      else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2290    
2291        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2292        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2293        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2294        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2295        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2296        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2297        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2298        int sampleNo_1,dataPointNo_1;
2299        int numSamples_1 = arg_1_Z.getNumSamples();
2300        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2301        int offset_0 = tmp_0->getPointOffset(0,0);
2302        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2303        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2304          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2305            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2306            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2307            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2308            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2309            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2310            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2311          }
2312        }
2313    
2314      }
2315      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2316    
2317        // Borrow DataTagged input from Data object
2318        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2319        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2320    
2321        // Prepare the DataConstant input
2322        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2323        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2324    
2325        // Prepare a DataTagged output 2
2326        res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2327        res.tag();
2328        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2329        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2330    
2331        // Prepare offset into DataConstant
2332        int offset_1 = tmp_1->getPointOffset(0,0);
2333        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2334        // Get the views
2335        DataArrayView view_0 = tmp_0->getDefaultValue();
2336        DataArrayView view_2 = tmp_2->getDefaultValue();
2337        // Get the pointers to the actual data
2338        double *ptr_0 = &((view_0.getData())[0]);
2339        double *ptr_2 = &((view_2.getData())[0]);
2340        // Compute an MVP for the default
2341        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2342        // Compute an MVP for each tag
2343        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2344        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2345        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2346          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2347          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2348          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2349          double *ptr_0 = &view_0.getData(0);
2350          double *ptr_2 = &view_2.getData(0);
2351          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2352        }
2353    
2354      }
2355      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2356    
2357        // Borrow DataTagged input from Data object
2358        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2359        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2360    
2361        // Borrow DataTagged input from Data object
2362        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2363        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2364    
2365        // Prepare a DataTagged output 2
2366        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2367        res.tag();  // DataTagged output
2368        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2369        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2370    
2371        // Get the views
2372        DataArrayView view_0 = tmp_0->getDefaultValue();
2373        DataArrayView view_1 = tmp_1->getDefaultValue();
2374        DataArrayView view_2 = tmp_2->getDefaultValue();
2375        // Get the pointers to the actual data
2376        double *ptr_0 = &((view_0.getData())[0]);
2377        double *ptr_1 = &((view_1.getData())[0]);
2378        double *ptr_2 = &((view_2.getData())[0]);
2379        // Compute an MVP for the default
2380        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2381        // Merge the tags
2382        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2383        const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2384        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2385        for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2386          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape
2387        }
2388        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2389          tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2390        }
2391        // Compute an MVP for each tag
2392        const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2393        for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2394          DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2395          DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2396          DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2397          double *ptr_0 = &view_0.getData(0);
2398          double *ptr_1 = &view_1.getData(0);
2399          double *ptr_2 = &view_2.getData(0);
2400          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2401        }
2402    
2403      }
2404      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2405    
2406        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2407        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2408        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2409        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2410        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2411        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2412        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2413        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2414        int sampleNo_0,dataPointNo_0;
2415        int numSamples_0 = arg_0_Z.getNumSamples();
2416        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2417        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2418        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2419          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2420          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2421          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2422            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2423            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2424            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2425            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2426            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2427          }
2428        }
2429    
2430      }
2431      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2432    
2433        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2434        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2435        DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2436        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2437        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2438        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2439        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2440        int sampleNo_0,dataPointNo_0;
2441        int numSamples_0 = arg_0_Z.getNumSamples();
2442        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2443        int offset_1 = tmp_1->getPointOffset(0,0);
2444        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2445        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2446          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2447            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2448            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2449            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2450            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2451            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2452            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2453          }
2454        }
2455    
2456    
2457      }
2458      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2459    
2460        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2461        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2462        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2463        DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2464        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2465        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2466        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2467        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2468        int sampleNo_0,dataPointNo_0;
2469        int numSamples_0 = arg_0_Z.getNumSamples();
2470        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2471        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2472        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2473          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2474          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2475          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2476            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2477            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2478            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2479            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2480            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2481          }
2482        }
2483    
2484      }
2485      else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2486    
2487        // After finding a common function space above the two inputs have the same numSamples and num DPPS
2488        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2489        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2490        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2491        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2492        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2493        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2494        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2495        int sampleNo_0,dataPointNo_0;
2496        int numSamples_0 = arg_0_Z.getNumSamples();
2497        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2498        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2499        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2500          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2501            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2502            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2503            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2504            double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);
2505            double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);
2506            double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);
2507            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2508          }
2509        }
2510    
2511      }
2512      else {
2513        throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
2514      }
2515    
2516      return res;
2517    }
2518    
2519    DataAbstract*
2520    Data::borrowData() const
2521    {
2522      return m_data.get();
2523    }
2524    
2525    
2526    std::string
2527    Data::toString() const
2528    {
2529        static const DataArrayView::ValueType::size_type TOO_MANY_POINTS=80;
2530        if (getNumDataPoints()*getDataPointSize()>TOO_MANY_POINTS)
2531        {
2532        stringstream temp;
2533        temp << "Summary: inf="<< inf() << " sup=" << sup() << " data points=" << getNumDataPoints();
2534        return  temp.str();
2535        }
2536        return m_data->toString();
2537    }
2538    
2539    
2540    /* Member functions specific to the MPI implementation */
2541    
2542    void
2543    Data::print()
2544    {
2545      int i,j;
2546    
2547      printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2548      for( i=0; i<getNumSamples(); i++ )
2549      {
2550        printf( "[%6d]", i );
2551        for( j=0; j<getNumDataPointsPerSample(); j++ )
2552          printf( "\t%10.7g", (getSampleData(i))[j] );
2553        printf( "\n" );
2554      }
2555    }
2556    void
2557    Data::dump(const std::string fileName) const
2558    {
2559      try
2560         {
2561            return m_data->dump(fileName);
2562         }
2563         catch (exception& e)
2564         {
2565            cout << e.what() << endl;
2566         }
2567    }
2568    
2569    int
2570    Data::get_MPISize() const
2571    {
2572        int size;
2573    #ifdef PASO_MPI
2574        int error;
2575        error = MPI_Comm_size( get_MPIComm(), &size );
2576    #else
2577        size = 1;
2578    #endif
2579        return size;
2580    }
2581    
2582    int
2583    Data::get_MPIRank() const
2584    {
2585        int rank;
2586    #ifdef PASO_MPI
2587        int error;
2588        error = MPI_Comm_rank( get_MPIComm(), &rank );
2589    #else
2590        rank = 0;
2591    #endif
2592        return rank;
2593    }
2594    
2595    MPI_Comm
2596    Data::get_MPIComm() const
2597    {
2598    #ifdef PASO_MPI
2599        return MPI_COMM_WORLD;
2600    #else
2601        return -1;
2602    #endif
2603    }
2604    
2605    

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

  ViewVC Help
Powered by ViewVC 1.1.26