/[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 1092 by gross, Fri Apr 13 03:39:49 2007 UTC revision 1803 by jfenwick, Wed Sep 24 06:20:29 2008 UTC
# Line 1  Line 1 
 // $Id$  
1    
2  /*  /* $Id$ */
3   ************************************************************  
4   *          Copyright 2006 by ACcESS MNRF                   *  /*******************************************************
5   *                                                          *   *
6   *              http://www.access.edu.au                    *   *           Copyright 2003-2007 by ACceSS MNRF
7   *       Primary Business: Queensland, Australia            *   *       Copyright 2007 by University of Queensland
8   *  Licensed under the Open Software License version 3.0    *   *
9   *     http://www.opensource.org/licenses/osl-3.0.php       *   *                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 "DataExpanded.h"  #include "DataExpanded.h"
19  #include "DataConstant.h"  #include "DataConstant.h"
20  #include "DataTagged.h"  #include "DataTagged.h"
21  #include "DataEmpty.h"  #include "DataEmpty.h"
 #include "DataArray.h"  
 #include "DataArrayView.h"  
 #include "DataProf.h"  
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
25    #include "FunctionSpaceException.h"
26    
27    extern "C" {
28    #include "escript/blocktimer.h"
29    }
30    
31  #include <fstream>  #include <fstream>
32  #include <algorithm>  #include <algorithm>
# Line 53  Data::Data(double value, Line 57  Data::Data(double value,
57             const FunctionSpace& what,             const FunctionSpace& what,
58             bool expanded)             bool expanded)
59  {  {
60    DataArrayView::ShapeType dataPointShape;    DataTypes::ShapeType dataPointShape;
61    for (int i = 0; i < shape.attr("__len__")(); ++i) {    for (int i = 0; i < shape.attr("__len__")(); ++i) {
62      dataPointShape.push_back(extract<const int>(shape[i]));      dataPointShape.push_back(extract<const int>(shape[i]));
63    }    }
64    DataArray temp(dataPointShape,value);  
65    initialise(temp.getView(),what,expanded);    int len = DataTypes::noValues(dataPointShape);
66      DataVector temp_data(len,value,len);
67    //   DataArrayView temp_dataView(temp_data, dataPointShape);
68    
69    //   initialise(temp_dataView, what, expanded);
70      initialise(temp_data, dataPointShape, what, expanded);
71    
72    m_protected=false;    m_protected=false;
73  }  }
74    
75  Data::Data(double value,  Data::Data(double value,
76         const DataArrayView::ShapeType& dataPointShape,         const DataTypes::ShapeType& dataPointShape,
77         const FunctionSpace& what,         const FunctionSpace& what,
78             bool expanded)             bool expanded)
79  {  {
80    DataArray temp(dataPointShape,value);    int len = DataTypes::noValues(dataPointShape);
81    pair<int,int> dataShape=what.getDataShape();  
82    initialise(temp.getView(),what,expanded);    DataVector temp_data(len,value,len);
83    //   DataArrayView temp_dataView(temp_data, dataPointShape);
84    
85    //   initialise(temp_dataView, what, expanded);
86      initialise(temp_data, dataPointShape, what, expanded);
87    
88    m_protected=false;    m_protected=false;
89  }  }
90    
# Line 79  Data::Data(const Data& inData) Line 94  Data::Data(const Data& inData)
94    m_protected=inData.isProtected();    m_protected=inData.isProtected();
95  }  }
96    
97    
98  Data::Data(const Data& inData,  Data::Data(const Data& inData,
99             const DataArrayView::RegionType& region)             const DataTypes::RegionType& region)
100  {  {
101    //    //
102    // Create Data which is a slice of another Data    // Create Data which is a slice of another Data
# Line 93  Data::Data(const Data& inData, Line 109  Data::Data(const Data& inData,
109  Data::Data(const Data& inData,  Data::Data(const Data& inData,
110             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
111  {  {
112      if (inData.isEmpty())
113      {
114        throw DataException("Error - will not interpolate for instances of DataEmpty.");
115      }
116    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
117      m_data=inData.m_data;      m_data=inData.m_data;
118      } else if (inData.isConstant()) { // for a constant function, we just need to use the new function space
119        if (!inData.probeInterpolation(functionspace))
120        {           // Even though this is constant, we still need to check whether interpolation is allowed
121        throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
122        }
123        DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),inData.m_data->getVector());  
124        m_data=shared_ptr<DataAbstract>(dc);
125    } else {    } else {
126      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);      Data tmp(0,inData.getDataPointShape(),functionspace,true);
127      // Note: Must use a reference or pointer to a derived object      // Note: Must use a reference or pointer to a derived object
128      // in order to get polymorphic behaviour. Shouldn't really      // in order to get polymorphic behaviour. Shouldn't really
129      // be able to create an instance of AbstractDomain but that was done      // be able to create an instance of AbstractDomain but that was done
# Line 112  Data::Data(const Data& inData, Line 139  Data::Data(const Data& inData,
139    m_protected=false;    m_protected=false;
140  }  }
141    
142  Data::Data(const DataTagged::TagListType& tagKeys,  // Data::Data(const DataTagged::TagListType& tagKeys,
143             const DataTagged::ValueListType & values,  //            const DataTagged::ValueListType & values,
144             const DataArrayView& defaultValue,  //            const DataArrayView& defaultValue,
145             const FunctionSpace& what,  //            const FunctionSpace& what,
146             bool expanded)  //            bool expanded)
147    // {
148    //   DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
149    //   shared_ptr<DataAbstract> temp_data(temp);
150    //   m_data=temp_data;
151    //   m_protected=false;
152    //   if (expanded) {
153    //     expand();
154    //   }
155    // }
156    
157    
158    
159    Data::Data(DataAbstract* underlyingdata)
160  {  {
161    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);      m_data=shared_ptr<DataAbstract>(underlyingdata);
162    shared_ptr<DataAbstract> temp_data(temp);      m_protected=false;
   m_data=temp_data;  
   m_protected=false;  
   if (expanded) {  
     expand();  
   }  
163  }  }
164    
165  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 134  Data::Data(const numeric::array& value, Line 169  Data::Data(const numeric::array& value,
169    initialise(value,what,expanded);    initialise(value,what,expanded);
170    m_protected=false;    m_protected=false;
171  }  }
172    /*
173  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
174         const FunctionSpace& what,         const FunctionSpace& what,
175             bool expanded)             bool expanded)
176  {  {
177    initialise(value,what,expanded);    initialise(value,what,expanded);
178    m_protected=false;    m_protected=false;
179    }*/
180    
181    Data::Data(const DataTypes::ValueType& value,
182             const DataTypes::ShapeType& shape,
183                     const FunctionSpace& what,
184                     bool expanded)
185    {
186       initialise(value,shape,what,expanded);
187       m_protected=false;
188  }  }
189    
190    
191  Data::Data(const object& value,  Data::Data(const object& value,
192         const FunctionSpace& what,         const FunctionSpace& what,
193             bool expanded)             bool expanded)
# Line 152  Data::Data(const object& value, Line 197  Data::Data(const object& value,
197    m_protected=false;    m_protected=false;
198  }  }
199    
200    
201  Data::Data(const object& value,  Data::Data(const object& value,
202             const Data& other)             const Data& other)
203  {  {
204      numeric::array asNumArray(value);
205    
206      // extract the shape of the numarray
207      DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
208    // /*  for (int i=0; i < asNumArray.getrank(); i++) {
209    //     tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
210    //   }*/
211    //   // get the space for the data vector
212    //   int len = DataTypes::noValues(tempShape);
213    //   DataVector temp_data(len, 0.0, len);
214    // /*  DataArrayView temp_dataView(temp_data, tempShape);
215    //   temp_dataView.copy(asNumArray);*/
216    //   temp_data.copyFromNumArray(asNumArray);
217    
218    //    //
219    // Create DataConstant using the given value and all other parameters    // Create DataConstant using the given value and all other parameters
220    // copied from other. If value is a rank 0 object this Data    // copied from other. If value is a rank 0 object this Data
221    // will assume the point data shape of other.    // will assume the point data shape of other.
222    DataArray temp(value);  
223    if (temp.getView().getRank()==0) {    if (DataTypes::getRank(tempShape)/*temp_dataView.getRank()*/==0) {
224      //  
225      // Create a DataArray with the scalar value for all elements  
226      DataArray temp2(other.getPointDataView().getShape(),temp.getView()());      // get the space for the data vector
227      initialise(temp2.getView(),other.getFunctionSpace(),false);      int len1 = DataTypes::noValues(tempShape);
228        DataVector temp_data(len1, 0.0, len1);
229        temp_data.copyFromNumArray(asNumArray);
230    
231        int len = DataTypes::noValues(other.getDataPointShape());
232    
233        DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
234        //DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
235    //     initialise(temp2_dataView, other.getFunctionSpace(), false);
236    
237        DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
238        boost::shared_ptr<DataAbstract> sp(t);
239        m_data=sp;
240    
241    
242    } else {    } else {
243      //      //
244      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
245      initialise(temp.getView(),other.getFunctionSpace(),false);  //     initialise(temp_dataView, other.getFunctionSpace(), false);
246        DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
247        boost::shared_ptr<DataAbstract> sp(t);
248        m_data=sp;
249    }    }
250    m_protected=false;    m_protected=false;
251  }  }
# Line 178  Data::~Data() Line 255  Data::~Data()
255    
256  }  }
257    
258    
259    
260    void
261    Data::initialise(const boost::python::numeric::array& value,
262                     const FunctionSpace& what,
263                     bool expanded)
264    {
265      //
266      // Construct a Data object of the appropriate type.
267      // Construct the object first as there seems to be a bug which causes
268      // undefined behaviour if an exception is thrown during construction
269      // within the shared_ptr constructor.
270      if (expanded) {
271        DataAbstract* temp=new DataExpanded(value, what);
272        boost::shared_ptr<DataAbstract> temp_data(temp);
273        m_data=temp_data;
274      } else {
275        DataAbstract* temp=new DataConstant(value, what);
276        boost::shared_ptr<DataAbstract> temp_data(temp);
277        m_data=temp_data;
278      }
279    }
280    
281    
282    void
283    Data::initialise(const DataTypes::ValueType& value,
284             const DataTypes::ShapeType& shape,
285                     const FunctionSpace& what,
286                     bool expanded)
287    {
288      //
289      // Construct a Data object of the appropriate type.
290      // Construct the object first as there seems to be a bug which causes
291      // undefined behaviour if an exception is thrown during construction
292      // within the shared_ptr constructor.
293      if (expanded) {
294        DataAbstract* temp=new DataExpanded(what, shape, value);
295        boost::shared_ptr<DataAbstract> temp_data(temp);
296        m_data=temp_data;
297      } else {
298        DataAbstract* temp=new DataConstant(what, shape, value);
299        boost::shared_ptr<DataAbstract> temp_data(temp);
300        m_data=temp_data;
301      }
302    }
303    
304    
305    // void
306    // Data::CompareDebug(const Data& rd)
307    // {
308    //  using namespace std;
309    //  bool mismatch=false;
310    //  std::cout << "Comparing left and right" << endl;
311    //  const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
312    //  const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
313    //  
314    //  if (left==0)
315    //  {
316    //      cout << "left arg is not a DataTagged\n";
317    //      return;
318    //  }
319    //  
320    //  if (right==0)
321    //  {
322    //      cout << "right arg is not a DataTagged\n";
323    //      return;
324    //  }
325    //  cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
326    //  cout << "Shapes ";
327    //  if (left->getShape()==right->getShape())
328    //  {
329    //      cout << "ok\n";
330    //  }
331    //  else
332    //  {
333    //      cout << "Problem: shapes do not match\n";
334    //      mismatch=true;
335    //  }
336    //  int lim=left->getVector().size();
337    //  if (right->getVector().size()) lim=right->getVector().size();
338    //  for (int i=0;i<lim;++i)
339    //  {
340    //      if (left->getVector()[i]!=right->getVector()[i])
341    //      {
342    //          cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
343    //          mismatch=true;
344    //      }
345    //  }
346    //
347    //  // still need to check the tag map
348    //  // also need to watch what is happening to function spaces, are they copied or what?
349    //
350    //  const DataTagged::DataMapType& mapleft=left->getTagLookup();
351    //  const DataTagged::DataMapType& mapright=right->getTagLookup();
352    //
353    //  if (mapleft.size()!=mapright.size())
354    //  {
355    //      cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
356    //      mismatch=true;
357    //      cout << "Left map\n";
358    //      DataTagged::DataMapType::const_iterator i,j;
359    //      for (i=mapleft.begin();i!=mapleft.end();++i) {
360    //          cout << "(" << i->first << "=>" << i->second << ")\n";
361    //      }
362    //      cout << "Right map\n";
363    //      for (i=mapright.begin();i!=mapright.end();++i) {
364    //          cout << "(" << i->first << "=>" << i->second << ")\n";
365    //      }
366    //      cout << "End map\n";
367    //
368    //  }
369    //
370    //  DataTagged::DataMapType::const_iterator i,j;
371    //  for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
372    //     if ((i->first!=j->first) || (i->second!=j->second))
373    //     {
374    //      cout << "(" << i->first << "=>" << i->second << ")";
375    //      cout << ":(" << j->first << "=>" << j->second << ") ";
376    //      mismatch=true;
377    //            }
378    //  }
379    //  if (mismatch)
380    //  {
381    //      cout << "#Mismatch\n";
382    //  }
383    // }
384    
385  escriptDataC  escriptDataC
386  Data::getDataC()  Data::getDataC()
387  {  {
# Line 197  Data::getDataC() const Line 401  Data::getDataC() const
401  const boost::python::tuple  const boost::python::tuple
402  Data::getShapeTuple() const  Data::getShapeTuple() const
403  {  {
404    const DataArrayView::ShapeType& shape=getDataPointShape();    const DataTypes::ShapeType& shape=getDataPointShape();
405    switch(getDataPointRank()) {    switch(getDataPointRank()) {
406       case 0:       case 0:
407          return make_tuple();          return make_tuple();
# Line 214  Data::getShapeTuple() const Line 418  Data::getShapeTuple() const
418    }    }
419  }  }
420    
421    
422    // The different name is needed because boost has trouble with overloaded functions.
423    // It can't work out what type the function is based soley on its name.
424    // There are ways to fix this involving creating function pointer variables for each form
425    // but there doesn't seem to be a need given that the methods have the same name from the python point of view
426    Data*
427    Data::copySelf()
428    {
429       DataAbstract* temp=m_data->deepCopy();
430       return new Data(temp);
431    }
432    
433  void  void
434  Data::copy(const Data& other)  Data::copy(const Data& other)
435  {  {
436    //    DataAbstract* temp=other.m_data->deepCopy();
437    // Perform a deep copy    shared_ptr<DataAbstract> temp_data(temp);
438      m_data=temp_data;
439    }
440    
441    
442    void
443    Data::setToZero()
444    {
445      if (isEmpty())
446    {    {
447      DataExpanded* temp=dynamic_cast<DataExpanded*>(other.m_data.get());       throw DataException("Error - Operations not permitted on instances of DataEmpty.");
     if (temp!=0) {  
       //  
       // Construct a DataExpanded copy  
       DataAbstract* newData=new DataExpanded(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
448    }    }
449    {    {
450      DataTagged* temp=dynamic_cast<DataTagged*>(other.m_data.get());      DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
451      if (temp!=0) {      if (temp!=0) {
452        //         temp->setToZero();
453        // Construct a DataTagged copy         return;
       DataAbstract* newData=new DataTagged(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
454      }      }
455    }    }
456    {    {
457      DataConstant* temp=dynamic_cast<DataConstant*>(other.m_data.get());      DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
458      if (temp!=0) {      if (temp!=0) {
459        //        temp->setToZero();
       // Construct a DataConstant copy  
       DataAbstract* newData=new DataConstant(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
460        return;        return;
461      }      }
462    }    }
463    {    {
464      DataEmpty* temp=dynamic_cast<DataEmpty*>(other.m_data.get());      DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
465      if (temp!=0) {      if (temp!=0) {
466        //        temp->setToZero();
       // Construct a DataEmpty copy  
       DataAbstract* newData=new DataEmpty();  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
467        return;        return;
468      }      }
469    }    }
470    throw DataException("Error - Copy not implemented for this Data type.");    throw DataException("Error - Data can not be set to zero.");
471  }  }
472    
473  void  void
474  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
475                     const Data& mask)                     const Data& mask)
476  {  {
477      if (other.isEmpty() || mask.isEmpty())
478      {
479        throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");
480      }
481    Data mask1;    Data mask1;
482    Data mask2;    Data mask2;
483    
# Line 312  Data::isConstant() const Line 520  Data::isConstant() const
520  }  }
521    
522  void  void
523  Data::setProtection()  Data::setProtection()
524  {  {
525     m_protected=true;     m_protected=true;
526  }  }
527    
528  bool  bool
529  Data::isProtected() const  Data::isProtected() const
530  {  {
531     return m_protected;     return m_protected;
532  }  }
533    
# Line 370  Data::tag() Line 578  Data::tag()
578  Data  Data
579  Data::oneOver() const  Data::oneOver() const
580  {  {
581    return escript::unaryOp(*this,bind1st(divides<double>(),1.));    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
582  }  }
583    
584  Data  Data
585  Data::wherePositive() const  Data::wherePositive() const
586  {  {
587    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
588  }  }
589    
590  Data  Data
591  Data::whereNegative() const  Data::whereNegative() const
592  {  {
593    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
594  }  }
595    
596  Data  Data
597  Data::whereNonNegative() const  Data::whereNonNegative() const
598  {  {
599    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
600  }  }
601    
602  Data  Data
603  Data::whereNonPositive() const  Data::whereNonPositive() const
604  {  {
605    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
606  }  }
607    
608  Data  Data
609  Data::whereZero(double tol) const  Data::whereZero(double tol) const
610  {  {
611    Data dataAbs=abs();    Data dataAbs=abs();
612    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
613  }  }
614    
615  Data  Data
616  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
617  {  {
618    Data dataAbs=abs();    Data dataAbs=abs();
619    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
620  }  }
621    
622  Data  Data
# Line 435  Data::probeInterpolation(const FunctionS Line 643  Data::probeInterpolation(const FunctionS
643  Data  Data
644  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
645  {  {
646      if (isEmpty())
647      {
648        throw DataException("Error - operation not permitted on instances of DataEmpty.");
649      }
650      double blocktimer_start = blocktimer_time();
651    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
652      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
653    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
654    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
655    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
656    getDomain().setToGradient(out,*this);    getDomain().setToGradient(out,*this);
657      blocktimer_increment("grad()", blocktimer_start);
658    return out;    return out;
659  }  }
660    
661  Data  Data
662  Data::grad() const  Data::grad() const
663  {  {
664      if (isEmpty())
665      {
666        throw DataException("Error - operation not permitted on instances of DataEmpty.");
667      }
668    return gradOn(escript::function(getDomain()));    return gradOn(escript::function(getDomain()));
669  }  }
670    
671  int  int
672  Data::getDataPointSize() const  Data::getDataPointSize() const
673  {  {
674    return getPointDataView().noValues();    return m_data->getNoValues();
675  }  }
676    
677  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
678  Data::getLength() const  Data::getLength() const
679  {  {
680    return m_data->getLength();    return m_data->getLength();
681  }  }
682    
683  const DataArrayView::ShapeType&  const
 Data::getDataPointShape() const  
 {  
   return getPointDataView().getShape();  
 }  
   
   
   
 const  
684  boost::python::numeric::array  boost::python::numeric::array
685  Data:: getValueOfDataPoint(int dataPointNo)  Data:: getValueOfDataPoint(int dataPointNo)
686  {  {
687    size_t length=0;    size_t length=0;
688    int i, j, k, l;    int i, j, k, l;
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();
692    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
693    
694    //    //
695    // create the numeric array to be returned    // create the numeric array to be returned
# Line 489  Data:: getValueOfDataPoint(int dataPoint Line 699  Data:: getValueOfDataPoint(int dataPoint
699    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
700    // as that of the data point    // as that of the data point
701    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
702    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
703    
704    //    //
705    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 517  Data:: getValueOfDataPoint(int dataPoint Line 727  Data:: getValueOfDataPoint(int dataPoint
727         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
728             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
729         }         }
730                  
731         //         //
732         // Check a valid data point number has been supplied         // Check a valid data point number has been supplied
733         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
# Line 525  Data:: getValueOfDataPoint(int dataPoint Line 735  Data:: getValueOfDataPoint(int dataPoint
735         }         }
736         // TODO: global error handling         // TODO: global error handling
737         // create a view of the data if it is stored locally         // create a view of the data if it is stored locally
738         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);  //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
739                   DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
740    
741    
742         switch( dataPointRank ){         switch( dataPointRank ){
743              case 0 :              case 0 :
744                  numArray[0] = dataPointView();                  numArray[0] = getDataAtOffset(offset);
745                  break;                  break;
746              case 1 :                      case 1 :
747                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
748                      numArray[i]=dataPointView(i);                      numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
749                  break;                  break;
750              case 2 :                      case 2 :
751                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
752                      for( j=0; j<dataPointShape[1]; j++)                      for( j=0; j<dataPointShape[1]; j++)
753                          numArray[make_tuple(i,j)]=dataPointView(i,j);                          numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
754                  break;                  break;
755              case 3 :                      case 3 :
756                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
757                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
758                          for( k=0; k<dataPointShape[2]; k++)                          for( k=0; k<dataPointShape[2]; k++)
759                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);                              numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
760                  break;                  break;
761              case 4 :              case 4 :
762                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
763                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
764                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
765                              for( l=0; l<dataPointShape[3]; l++)                              for( l=0; l<dataPointShape[3]; l++)
766                                  numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);                                  numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
767                  break;                  break;
768      }      }
769    }    }
# Line 560  Data:: getValueOfDataPoint(int dataPoint Line 772  Data:: getValueOfDataPoint(int dataPoint
772    return numArray;    return numArray;
773    
774  }  }
775    
776  void  void
777  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
778  {  {
779      // this will throw if the value cannot be represented      // this will throw if the value cannot be represented
780      boost::python::numeric::array num_array(py_object);      boost::python::numeric::array num_array(py_object);
# Line 578  Data::setValueOfDataPointToArray(int dat Line 791  Data::setValueOfDataPointToArray(int dat
791    }    }
792    //    //
793    // check rank    // check rank
794    if (num_array.getrank()<getDataPointRank())    if (num_array.getrank()<getDataPointRank())
795        throw DataException("Rank of numarray does not match Data object rank");        throw DataException("Rank of numarray does not match Data object rank");
796    
797    //    //
# Line 621  Data::setValueOfDataPoint(int dataPointN Line 834  Data::setValueOfDataPoint(int dataPointN
834    }    }
835  }  }
836    
837  const  const
838  boost::python::numeric::array  boost::python::numeric::array
839  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
840  {  {
841    size_t length=0;    size_t length=0;
842    int i, j, k, l, pos;    int i, j, k, l, pos;
843    //    //
844    // determine the rank and shape of each data point    // determine the rank and shape of each data point
845    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
846    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
847    
848    //    //
849    // create the numeric array to be returned    // create the numeric array to be returned
# Line 640  Data::getValueOfGlobalDataPoint(int proc Line 853  Data::getValueOfGlobalDataPoint(int proc
853    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
854    // as that of the data point    // as that of the data point
855    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
856    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
857    
858    //    //
859    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 678  Data::getValueOfGlobalDataPoint(int proc Line 891  Data::getValueOfGlobalDataPoint(int proc
891                  if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {                  if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
892                    throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");                    throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
893                  }                  }
894                  
895                  //                  //
896                  // Check a valid data point number has been supplied                  // Check a valid data point number has been supplied
897                  if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {                  if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
# Line 686  Data::getValueOfGlobalDataPoint(int proc Line 899  Data::getValueOfGlobalDataPoint(int proc
899                  }                  }
900                  // TODO: global error handling                  // TODO: global error handling
901          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
902          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
903                    DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
904    
905          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
906          pos=0;          pos=0;
907          switch( dataPointRank ){          switch( dataPointRank ){
908              case 0 :              case 0 :
909                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
910                  break;                  break;
911              case 1 :                      case 1 :
912                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
913                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
914                  break;                  break;
915              case 2 :                      case 2 :
916                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
917                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
918                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
919                  break;                  break;
920              case 3 :                      case 3 :
921                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
922                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
923                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
924                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
925                  break;                  break;
926              case 4 :              case 4 :
927                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
928                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
929                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
930                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
931                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
932                  break;                  break;
933          }          }
934              }              }
935      }      }
936          #ifdef PASO_MPI          #ifdef PASO_MPI
937          // broadcast the data to all other processes          // broadcast the data to all other processes
938      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
939          #endif          #endif
# Line 729  Data::getValueOfGlobalDataPoint(int proc Line 943  Data::getValueOfGlobalDataPoint(int proc
943          case 0 :          case 0 :
944              numArray[0]=tmpData[0];              numArray[0]=tmpData[0];
945              break;              break;
946          case 1 :                  case 1 :
947              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
948                  numArray[i]=tmpData[i];                  numArray[i]=tmpData[i];
949              break;              break;
950          case 2 :                  case 2 :
951              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
952                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
953                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
954              break;              break;
955          case 3 :                  case 3 :
956              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
957                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
958                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
# Line 753  Data::getValueOfGlobalDataPoint(int proc Line 967  Data::getValueOfGlobalDataPoint(int proc
967              break;              break;
968      }      }
969    
970      delete [] tmpData;        delete [] tmpData;
971    //    //
972    // return the loaded array    // return the loaded array
973    return numArray;    return numArray;
# Line 766  Data::integrate() const Line 980  Data::integrate() const
980  {  {
981    int index;    int index;
982    int rank = getDataPointRank();    int rank = getDataPointRank();
983    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
984      int dataPointSize = getDataPointSize();
985    
986    //    //
987    // calculate the integral values    // calculate the integral values
988    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
989      vector<double> integrals_local(dataPointSize);
990    #ifdef PASO_MPI
991      AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals_local,*this);
992      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
993      double *tmp = new double[dataPointSize];
994      double *tmp_local = new double[dataPointSize];
995      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
996      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
997      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
998      delete[] tmp;
999      delete[] tmp_local;
1000    #else
1001    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);
1002    #endif
1003    
1004    //    //
1005    // create the numeric array to be returned    // create the numeric array to be returned
# Line 832  Data::integrate() const Line 1059  Data::integrate() const
1059  Data  Data
1060  Data::sin() const  Data::sin() const
1061  {  {
1062    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1063  }  }
1064    
1065  Data  Data
1066  Data::cos() const  Data::cos() const
1067  {  {
1068    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1069  }  }
1070    
1071  Data  Data
1072  Data::tan() const  Data::tan() const
1073  {  {
1074    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1075  }  }
1076    
1077  Data  Data
1078  Data::asin() const  Data::asin() const
1079  {  {
1080    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1081  }  }
1082    
1083  Data  Data
1084  Data::acos() const  Data::acos() const
1085  {  {
1086    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1087  }  }
1088    
1089    
1090  Data  Data
1091  Data::atan() const  Data::atan() const
1092  {  {
1093    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1094  }  }
1095    
1096  Data  Data
1097  Data::sinh() const  Data::sinh() const
1098  {  {
1099    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1100    
1101  }  }
1102    
1103  Data  Data
1104  Data::cosh() const  Data::cosh() const
1105  {  {
1106    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1107  }  }
1108    
1109  Data  Data
1110  Data::tanh() const  Data::tanh() const
1111  {  {
1112    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1113  }  }
1114    
1115    
# Line 891  Data::erf() const Line 1119  Data::erf() const
1119  #ifdef _WIN32  #ifdef _WIN32
1120    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1121  #else  #else
1122    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);    return C_TensorUnaryOperation(*this, ::erf);
1123  #endif  #endif
1124  }  }
1125    
# Line 899  Data Line 1127  Data
1127  Data::asinh() const  Data::asinh() const
1128  {  {
1129  #ifdef _WIN32  #ifdef _WIN32
1130    return escript::unaryOp(*this,escript::asinh_substitute);    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1131  #else  #else
1132    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);    return C_TensorUnaryOperation(*this, ::asinh);
1133  #endif  #endif
1134  }  }
1135    
# Line 909  Data Line 1137  Data
1137  Data::acosh() const  Data::acosh() const
1138  {  {
1139  #ifdef _WIN32  #ifdef _WIN32
1140    return escript::unaryOp(*this,escript::acosh_substitute);    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1141  #else  #else
1142    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);    return C_TensorUnaryOperation(*this, ::acosh);
1143  #endif  #endif
1144  }  }
1145    
# Line 919  Data Line 1147  Data
1147  Data::atanh() const  Data::atanh() const
1148  {  {
1149  #ifdef _WIN32  #ifdef _WIN32
1150    return escript::unaryOp(*this,escript::atanh_substitute);    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1151  #else  #else
1152    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);    return C_TensorUnaryOperation(*this, ::atanh);
1153  #endif  #endif
1154  }  }
1155    
1156  Data  Data
1157  Data::log10() const  Data::log10() const
1158  {  {
1159    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1160  }  }
1161    
1162  Data  Data
1163  Data::log() const  Data::log() const
1164  {  {
1165    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1166  }  }
1167    
1168  Data  Data
1169  Data::sign() const  Data::sign() const
1170  {  {
1171    return escript::unaryOp(*this,escript::fsign);    return C_TensorUnaryOperation(*this, escript::fsign);
1172  }  }
1173    
1174  Data  Data
1175  Data::abs() const  Data::abs() const
1176  {  {
1177    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1178  }  }
1179    
1180  Data  Data
1181  Data::neg() const  Data::neg() const
1182  {  {
1183    return escript::unaryOp(*this,negate<double>());    return C_TensorUnaryOperation(*this, negate<double>());
1184  }  }
1185    
1186  Data  Data
# Line 967  Data::pos() const Line 1195  Data::pos() const
1195  Data  Data
1196  Data::exp() const  Data::exp() const
1197  {  {
1198    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1199  }  }
1200    
1201  Data  Data
1202  Data::sqrt() const  Data::sqrt() const
1203  {  {
1204    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1205  }  }
1206    
1207  double  double
1208  Data::Lsup() const  Data::Lsup() const
1209  {  {
1210    double localValue, globalValue;    double localValue;
1211    //    //
1212    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1213    
1214    AbsMax abs_max_func;    AbsMax abs_max_func;
1215    localValue = algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1216  #ifdef PASO_MPI  #ifdef PASO_MPI
1217      double globalValue;
1218    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1219    return globalValue;    return globalValue;
1220  #else  #else
# Line 994  Data::Lsup() const Line 1223  Data::Lsup() const
1223  }  }
1224    
1225  double  double
 Data::Linf() const  
 {  
   double localValue, globalValue;  
   //  
   // set the initial absolute minimum value to max double  
   AbsMin abs_min_func;  
   localValue = algorithm(abs_min_func,numeric_limits<double>::max());  
   
 #ifdef PASO_MPI  
   MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );  
   return globalValue;  
 #else  
   return localValue;  
 #endif  
 }  
   
 double  
1226  Data::sup() const  Data::sup() const
1227  {  {
1228    double localValue, globalValue;    double localValue;
1229    //    //
1230    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1231    FMax fmax_func;    FMax fmax_func;
1232    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1233  #ifdef PASO_MPI  #ifdef PASO_MPI
1234      double globalValue;
1235    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1236    return globalValue;    return globalValue;
1237  #else  #else
# Line 1029  Data::sup() const Line 1242  Data::sup() const
1242  double  double
1243  Data::inf() const  Data::inf() const
1244  {  {
1245    double localValue, globalValue;    double localValue;
1246    //    //
1247    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1248    FMin fmin_func;    FMin fmin_func;
1249    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1250  #ifdef PASO_MPI  #ifdef PASO_MPI
1251      double globalValue;
1252    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1253    return globalValue;    return globalValue;
1254  #else  #else
# Line 1066  Data Line 1280  Data
1280  Data::swapaxes(const int axis0, const int axis1) const  Data::swapaxes(const int axis0, const int axis1) const
1281  {  {
1282       int axis0_tmp,axis1_tmp;       int axis0_tmp,axis1_tmp;
1283       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1284       DataArrayView::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1285       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1286       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1287       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1092  Data::swapaxes(const int axis0, const in Line 1306  Data::swapaxes(const int axis0, const in
1306       }       }
1307       for (int i=0; i<rank; i++) {       for (int i=0; i<rank; i++) {
1308         if (i == axis0_tmp) {         if (i == axis0_tmp) {
1309            ev_shape.push_back(s[axis1_tmp]);            ev_shape.push_back(s[axis1_tmp]);
1310         } else if (i == axis1_tmp) {         } else if (i == axis1_tmp) {
1311            ev_shape.push_back(s[axis0_tmp]);            ev_shape.push_back(s[axis0_tmp]);
1312         } else {         } else {
1313            ev_shape.push_back(s[i]);            ev_shape.push_back(s[i]);
1314         }         }
1315       }       }
1316       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
# Line 1110  Data Line 1324  Data
1324  Data::symmetric() const  Data::symmetric() const
1325  {  {
1326       // check input       // check input
1327       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1328       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1329          if(s[0] != s[1])          if(s[0] != s[1])
1330             throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");             throw DataException("Error - Data::symmetric can only be calculated for rank 2 object with equal first and second dimension.");
1331       }       }
1332       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
# Line 1132  Data Line 1346  Data
1346  Data::nonsymmetric() const  Data::nonsymmetric() const
1347  {  {
1348       // check input       // check input
1349       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1350       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1351          if(s[0] != s[1])          if(s[0] != s[1])
1352             throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");             throw DataException("Error - Data::nonsymmetric can only be calculated for rank 2 object with equal first and second dimension.");
1353          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1354          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1355          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1356          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1147  Data::nonsymmetric() const Line 1361  Data::nonsymmetric() const
1361       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1362          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1363             throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");             throw DataException("Error - Data::nonsymmetric can only be calculated for rank 4 object with dim0==dim2 and dim1==dim3.");
1364          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1365          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1366          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1367          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1165  Data::nonsymmetric() const Line 1379  Data::nonsymmetric() const
1379  Data  Data
1380  Data::trace(int axis_offset) const  Data::trace(int axis_offset) const
1381  {  {
1382       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1383       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1384          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1385          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1386          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1387          m_data->trace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1388          return ev;          return ev;
1389       }       }
1390       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1391          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1392          if (axis_offset==0) {          if (axis_offset==0) {
1393            int s2=s[2];            int s2=s[2];
1394            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1189  Data::trace(int axis_offset) const Line 1403  Data::trace(int axis_offset) const
1403          return ev;          return ev;
1404       }       }
1405       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1406          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1407          if (axis_offset==0) {          if (axis_offset==0) {
1408            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1409            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1215  Data::trace(int axis_offset) const Line 1429  Data::trace(int axis_offset) const
1429  Data  Data
1430  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1431  {  {
1432       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1433       DataArrayView::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1434       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1435       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1436       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1237  Data Line 1451  Data
1451  Data::eigenvalues() const  Data::eigenvalues() const
1452  {  {
1453       // check input       // check input
1454       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1455       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1456          throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");          throw DataException("Error - Data::eigenvalues can only be calculated for rank 2 object.");
1457       if(s[0] != s[1])       if(s[0] != s[1])
1458          throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");          throw DataException("Error - Data::eigenvalues can only be calculated for object with equal first and second dimension.");
1459       // create return       // create return
1460       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1461       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1462       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1463       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1253  Data::eigenvalues() const Line 1467  Data::eigenvalues() const
1467  const boost::python::tuple  const boost::python::tuple
1468  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1469  {  {
1470       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1471       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1472          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
1473       if(s[0] != s[1])       if(s[0] != s[1])
1474          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for object with equal first and second dimension.");          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for object with equal first and second dimension.");
1475       // create return       // create return
1476       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1477       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1478       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1479       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1480       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1481       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1482       m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);       m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
# Line 1304  Data::calc_minGlobalDataPoint(int& ProcN Line 1518  Data::calc_minGlobalDataPoint(int& ProcN
1518      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1519      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1520        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1521          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1522          if (next<local_min) {          if (next<local_min) {
1523            local_min=next;            local_min=next;
1524            local_lowi=i;            local_lowi=i;
# Line 1322  Data::calc_minGlobalDataPoint(int& ProcN Line 1536  Data::calc_minGlobalDataPoint(int& ProcN
1536    
1537  #ifdef PASO_MPI  #ifdef PASO_MPI
1538      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1539      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1540      int lowProc = 0;      int lowProc = 0;
1541      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1542      int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );      int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1543        
1544      if( get_MPIRank()==0 ){      if( get_MPIRank()==0 ){
1545          next = globalMins[lowProc];          next = globalMins[lowProc];
1546          for( i=1; i<get_MPISize(); i++ )          for( i=1; i<get_MPISize(); i++ )
# Line 1348  Data::calc_minGlobalDataPoint(int& ProcN Line 1562  Data::calc_minGlobalDataPoint(int& ProcN
1562  void  void
1563  Data::saveDX(std::string fileName) const  Data::saveDX(std::string fileName) const
1564  {  {
1565      if (isEmpty())
1566      {
1567        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1568      }
1569    boost::python::dict args;    boost::python::dict args;
1570    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1571    getDomain().saveDX(fileName,args);    getDomain().saveDX(fileName,args);
# Line 1357  Data::saveDX(std::string fileName) const Line 1575  Data::saveDX(std::string fileName) const
1575  void  void
1576  Data::saveVTK(std::string fileName) const  Data::saveVTK(std::string fileName) const
1577  {  {
1578      if (isEmpty())
1579      {
1580        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1581      }
1582    boost::python::dict args;    boost::python::dict args;
1583    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1584    getDomain().saveVTK(fileName,args);    getDomain().saveVTK(fileName,args);
# Line 1380  Data::operator+=(const boost::python::ob Line 1602  Data::operator+=(const boost::python::ob
1602    binaryOp(tmp,plus<double>());    binaryOp(tmp,plus<double>());
1603    return (*this);    return (*this);
1604  }  }
1605    Data&
1606    Data::operator=(const Data& other)
1607    {
1608      copy(other);
1609      return (*this);
1610    }
1611    
1612  Data&  Data&
1613  Data::operator-=(const Data& right)  Data::operator-=(const Data& right)
# Line 1452  Data::powO(const boost::python::object& Line 1680  Data::powO(const boost::python::object&
1680  Data  Data
1681  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1682  {  {
1683    Data result;    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   if (getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result.binaryOp(*this,escript::rpow);  
   } else {  
      result.copy(*this);  
      result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   }  
   return result;  
1684  }  }
1685    
   
1686  //  //
1687  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1688  Data  Data
1689  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1690  {  {
1691    Data result;    return C_TensorBinaryOperation(left, right, plus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result+=left;  
   } else {  
      result.copy(left);  
      result+=right;  
   }  
   return result;  
1692  }  }
1693    
1694  //  //
# Line 1487  escript::operator+(const Data& left, con Line 1696  escript::operator+(const Data& left, con
1696  Data  Data
1697  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
1698  {  {
1699    Data result;    return C_TensorBinaryOperation(left, right, minus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.neg();  
      result+=left;  
   } else {  
      result.copy(left);  
      result-=right;  
   }  
   return result;  
1700  }  }
1701    
1702  //  //
# Line 1505  escript::operator-(const Data& left, con Line 1704  escript::operator-(const Data& left, con
1704  Data  Data
1705  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
1706  {  {
1707    Data result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result*=left;  
   } else {  
      result.copy(left);  
      result*=right;  
   }  
   return result;  
1708  }  }
1709    
1710  //  //
# Line 1523  escript::operator*(const Data& left, con Line 1712  escript::operator*(const Data& left, con
1712  Data  Data
1713  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
1714  {  {
1715    Data result;    return C_TensorBinaryOperation(left, right, divides<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.oneOver();  
      result*=left;  
   } else {  
      result.copy(left);  
      result/=right;  
   }  
   return result;  
1716  }  }
1717    
1718  //  //
# Line 1648  escript::operator/(const boost::python:: Line 1827  escript::operator/(const boost::python::
1827  /* TODO */  /* TODO */
1828  /* global reduction */  /* global reduction */
1829  Data  Data
1830  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1831  {  {
1832    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1833    
1834    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1835    
1836    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1837      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1838    }    }
1839    
# Line 1664  Data::getItem(const boost::python::objec Line 1843  Data::getItem(const boost::python::objec
1843  /* TODO */  /* TODO */
1844  /* global reduction */  /* global reduction */
1845  Data  Data
1846  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
1847  {  {
1848    return Data(*this,region);    return Data(*this,region);
1849  }  }
# Line 1679  Data::setItemO(const boost::python::obje Line 1858  Data::setItemO(const boost::python::obje
1858    setItemD(key,tempData);    setItemD(key,tempData);
1859  }  }
1860    
 /* TODO */  
 /* global reduction */  
1861  void  void
1862  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
1863                 const Data& value)                 const Data& value)
1864  {  {
1865    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1866    
1867    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1868    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1869      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1870    }    }
1871    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 1698  Data::setItemD(const boost::python::obje Line 1875  Data::setItemD(const boost::python::obje
1875    }    }
1876  }  }
1877    
 /* TODO */  
 /* global reduction */  
1878  void  void
1879  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1880                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
1881  {  {
1882    if (isProtected()) {    if (isProtected()) {
1883          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
# Line 1743  Data::typeMatchRight(const Data& right) Line 1918  Data::typeMatchRight(const Data& right)
1918    
1919  void  void
1920  Data::setTaggedValueByName(std::string name,  Data::setTaggedValueByName(std::string name,
1921                             const boost::python::object& value)                             const boost::python::object& value)
1922  {  {
1923       if (getFunctionSpace().getDomain().isValidTagName(name)) {       if (getFunctionSpace().getDomain().isValidTagName(name)) {
1924          int tagKey=getFunctionSpace().getDomain().getTag(name);          int tagKey=getFunctionSpace().getDomain().getTag(name);
# Line 1759  Data::setTaggedValue(int tagKey, Line 1934  Data::setTaggedValue(int tagKey,
1934    }    }
1935    //    //
1936    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1937    tag();    if (isConstant()) tag();
1938    
1939      numeric::array asNumArray(value);
1940    
1941    if (!isTagged()) {  
1942      throw DataException("Error - DataTagged conversion failed!!");    // extract the shape of the numarray
1943      DataTypes::ShapeType tempShape;
1944      for (int i=0; i < asNumArray.getrank(); i++) {
1945        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
1946    }    }
1947    
1948    //    // get the space for the data vector
1949    // Construct DataArray from boost::python::object input value  //   int len = DataTypes::noValues(tempShape);
1950    DataArray valueDataArray(value);  //   DataVector temp_data(len, 0.0, len);
1951    //   DataArrayView temp_dataView(temp_data, tempShape);
1952    //   temp_dataView.copy(asNumArray);
1953    
1954      DataVector temp_data2;
1955      temp_data2.copyFromNumArray(asNumArray);
1956    
1957    //    //
1958    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1959    m_data->setTaggedValue(tagKey,valueDataArray.getView());    //m_data->setTaggedValue(tagKey,temp_dataView);
1960    
1961        m_data->setTaggedValue(tagKey,tempShape, temp_data2);
1962  }  }
1963    
1964    // void
1965    // Data::setTaggedValueFromCPP(int tagKey,
1966    //                             const DataArrayView& value)
1967    // {
1968    //   if (isProtected()) {
1969    //         throw DataException("Error - attempt to update protected Data object.");
1970    //   }
1971    //   //
1972    //   // Ensure underlying data object is of type DataTagged
1973    //   if (isConstant()) tag();
1974    //
1975    //   //
1976    //   // Call DataAbstract::setTaggedValue
1977    //   m_data->setTaggedValue(tagKey,value);
1978    // }
1979    
1980  void  void
1981  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
1982                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
1983                                const DataTypes::ValueType& value,
1984                    int dataOffset)
1985  {  {
1986    if (isProtected()) {    if (isProtected()) {
1987          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1988    }    }
1989    //    //
1990    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1991    tag();    if (isConstant()) tag();
1992    
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
1993    //    //
1994    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1995    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
1996  }  }
1997    
1998  int  int
1999  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
2000  {  {
2001    return m_data->getTagNumber(dpno);    if (isEmpty())
2002  }    {
2003        throw DataException("Error - operation not permitted on instances of DataEmpty.");
 void  
 Data::archiveData(const std::string fileName)  
 {  
   cout << "Archiving Data object to: " << fileName << endl;  
   
   //  
   // Determine type of this Data object  
   int dataType = -1;  
   
   if (isEmpty()) {  
     dataType = 0;  
     cout << "\tdataType: DataEmpty" << endl;  
   }  
   if (isConstant()) {  
     dataType = 1;  
     cout << "\tdataType: DataConstant" << endl;  
   }  
   if (isTagged()) {  
     dataType = 2;  
     cout << "\tdataType: DataTagged" << endl;  
   }  
   if (isExpanded()) {  
     dataType = 3;  
     cout << "\tdataType: DataExpanded" << endl;  
   }  
   
   if (dataType == -1) {  
     throw DataException("archiveData Error: undefined dataType");  
   }  
   
   //  
   // Collect data items common to all Data types  
   int noSamples = getNumSamples();  
   int noDPPSample = getNumDataPointsPerSample();  
   int functionSpaceType = getFunctionSpace().getTypeCode();  
   int dataPointRank = getDataPointRank();  
   int dataPointSize = getDataPointSize();  
   int dataLength = getLength();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
   vector<int> referenceNumbers(noSamples);  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);  
   }  
   vector<int> tagNumbers(noSamples);  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);  
     }  
   }  
   
   cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;  
   cout << "\tfunctionSpaceType: " << functionSpaceType << endl;  
   cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;  
   
   //  
   // Flatten Shape to an array of integers suitable for writing to file  
   int flatShape[4] = {0,0,0,0};  
   cout << "\tshape: < ";  
   for (int dim=0; dim<dataPointRank; dim++) {  
     flatShape[dim] = dataPointShape[dim];  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
   
   //  
   // Open archive file  
   ofstream archiveFile;  
   archiveFile.open(fileName.data(), ios::out);  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem opening archive file");  
   }  
   
   //  
   // Write common data items to archive file  
   archiveFile.write(reinterpret_cast<char *>(&dataType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.write(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.write(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.write(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing to archive file");  
   }  
   
   //  
   // Archive underlying data values for each Data type  
   int noValues;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       noValues = 0;  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       break;  
     case 1:  
       // DataConstant  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 2:  
       // DataTagged  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing data to archive file");  
   }  
   
   //  
   // Close archive file  
   archiveFile.close();  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem closing archive file");  
2004    }    }
2005      return getFunctionSpace().getTagFromDataPointNo(dpno);
2006  }  }
2007    
 void  
 Data::extractData(const std::string fileName,  
                   const FunctionSpace& fspace)  
 {  
   //  
   // Can only extract Data to an object which is initially DataEmpty  
   if (!isEmpty()) {  
     throw DataException("extractData Error: can only extract to DataEmpty object");  
   }  
   
   cout << "Extracting Data object from: " << fileName << endl;  
   
   int dataType;  
   int noSamples;  
   int noDPPSample;  
   int functionSpaceType;  
   int dataPointRank;  
   int dataPointSize;  
   int dataLength;  
   DataArrayView::ShapeType dataPointShape;  
   int flatShape[4];  
   
   //  
   // Open the archive file  
   ifstream archiveFile;  
   archiveFile.open(fileName.data(), ios::in);  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem opening archive file");  
   }  
   
   //  
   // Read common data items from archive file  
   archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
     if (flatShape[dim]>0) {  
       dataPointShape.push_back(flatShape[dim]);  
     }  
   }  
   vector<int> referenceNumbers(noSamples);  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   vector<int> tagNumbers(noSamples);  
   if (dataType==2) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
   }  
   
   //  
   // Verify the values just read from the archive file  
   switch (dataType) {  
     case 0:  
       cout << "\tdataType: DataEmpty" << endl;  
       break;  
     case 1:  
       cout << "\tdataType: DataConstant" << endl;  
       break;  
     case 2:  
       cout << "\tdataType: DataTagged" << endl;  
       break;  
     case 3:  
       cout << "\tdataType: DataExpanded" << endl;  
       break;  
     default:  
       throw DataException("extractData Error: undefined dataType read from archive file");  
       break;  
   }  
   
   cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;  
   cout << "\tfunctionSpaceType: " << functionSpaceType << endl;  
   cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;  
   cout << "\tshape: < ";  
   for (int dim = 0; dim < dataPointRank; dim++) {  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
   
   //  
   // Verify that supplied FunctionSpace object is compatible with this Data object.  
   if ( (fspace.getTypeCode()!=functionSpaceType) ||  
        (fspace.getNumSamples()!=noSamples) ||  
        (fspace.getNumDPPSample()!=noDPPSample)  
      ) {  
     throw DataException("extractData Error: incompatible FunctionSpace");  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {  
       throw DataException("extractData Error: incompatible FunctionSpace");  
     }  
   }  
   if (dataType==2) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {  
         throw DataException("extractData Error: incompatible FunctionSpace");  
       }  
     }  
   }  
   
   //  
   // Construct a DataVector to hold underlying data values  
   DataVector dataVec(dataLength);  
   
   //  
   // Load this DataVector with the appropriate values  
   int noValues;  
   archiveFile.read(reinterpret_cast<char *>(&noValues),sizeof(int));  
   cout << "\tnoValues: " << noValues << endl;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       if (noValues != 0) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 1:  
       // DataConstant  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 2:  
       // DataTagged  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
   }  
   
   //  
   // Close archive file  
   archiveFile.close();  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem closing archive file");  
   }  
   
   //  
   // Construct an appropriate Data object  
   DataAbstract* tempData;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       tempData=new DataEmpty();  
       break;  
     case 1:  
       // DataConstant  
       tempData=new DataConstant(fspace,dataPointShape,dataVec);  
       break;  
     case 2:  
       // DataTagged  
       tempData=new DataTagged(fspace,dataPointShape,tagNumbers,dataVec);  
       break;  
     case 3:  
       // DataExpanded  
       tempData=new DataExpanded(fspace,dataPointShape,dataVec);  
       break;  
   }  
   shared_ptr<DataAbstract> temp_data(tempData);  
   m_data=temp_data;  
 }  
2008    
2009  ostream& escript::operator<<(ostream& o, const Data& data)  ostream& escript::operator<<(ostream& o, const Data& data)
2010  {  {
# Line 2150  escript::C_GeneralTensorProduct(Data& ar Line 2021  escript::C_GeneralTensorProduct(Data& ar
2021    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2022    // SM is the product of the last axis_offset entries in arg_0.getShape().    // SM is the product of the last axis_offset entries in arg_0.getShape().
2023    
   
2024    // Interpolate if necessary and find an appropriate function space    // Interpolate if necessary and find an appropriate function space
2025    Data arg_0_Z, arg_1_Z;    Data arg_0_Z, arg_1_Z;
2026    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
# Line 2172  escript::C_GeneralTensorProduct(Data& ar Line 2042  escript::C_GeneralTensorProduct(Data& ar
2042    // Get rank and shape of inputs    // Get rank and shape of inputs
2043    int rank0 = arg_0_Z.getDataPointRank();    int rank0 = arg_0_Z.getDataPointRank();
2044    int rank1 = arg_1_Z.getDataPointRank();    int rank1 = arg_1_Z.getDataPointRank();
2045    DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2046    DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2047    
2048    // Prepare for the loops of the product and verify compatibility of shapes    // Prepare for the loops of the product and verify compatibility of shapes
2049    int start0=0, start1=0;    int start0=0, start1=0;
# Line 2182  escript::C_GeneralTensorProduct(Data& ar Line 2052  escript::C_GeneralTensorProduct(Data& ar
2052    else if (transpose == 2)  { start1 = rank1-axis_offset; }    else if (transpose == 2)  { start1 = rank1-axis_offset; }
2053    else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }    else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2054    
2055    
2056    // Adjust the shapes for transpose    // Adjust the shapes for transpose
2057    DataArrayView::ShapeType tmpShape0;    DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2058    DataArrayView::ShapeType tmpShape1;    DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2059    for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }    for (int i=0; i<rank0; i++)   { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2060    for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }    for (int i=0; i<rank1; i++)   { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2061    
2062  #if 0  #if 0
2063    // For debugging: show shape after transpose    // For debugging: show shape after transpose
# Line 2217  escript::C_GeneralTensorProduct(Data& ar Line 2088  escript::C_GeneralTensorProduct(Data& ar
2088      SR *= tmpShape1[i];      SR *= tmpShape1[i];
2089    }    }
2090    
2091    // Define the shape of the output    // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2092    DataArrayView::ShapeType shape2;    DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2093    for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z    {         // block to limit the scope of out_index
2094    for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z       int out_index=0;
2095         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2096         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2097      }
2098    
2099    // Declare output Data object    // Declare output Data object
2100    Data res;    Data res;
2101    
2102    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2103      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2104      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2105      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2106      double *ptr_2 = &((res.getPointDataView().getData())[0]);      double *ptr_2 = &(res.getDataAtOffset(0));
2107      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2108    }    }
2109    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
# Line 2250  escript::C_GeneralTensorProduct(Data& ar Line 2124  escript::C_GeneralTensorProduct(Data& ar
2124    
2125      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2126      int offset_0 = tmp_0->getPointOffset(0,0);      int offset_0 = tmp_0->getPointOffset(0,0);
2127      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2128      // Get the views      // Get the views
2129      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2130      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2131      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2132      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2133      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2134    
2135        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2136        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2137    
2138    
2139      // Compute an MVP for the default      // Compute an MVP for the default
2140      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2141      // Compute an MVP for each tag      // Compute an MVP for each tag
2142      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2143      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2144      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2145        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2146        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2147        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2148        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2149        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2150    
2151          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2152          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2153        
2154        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2155      }      }
2156    
# Line 2290  escript::C_GeneralTensorProduct(Data& ar Line 2173  escript::C_GeneralTensorProduct(Data& ar
2173        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2174          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2175          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2176          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2177          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2178          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2179          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2180        }        }
2181      }      }
# Line 2316  escript::C_GeneralTensorProduct(Data& ar Line 2199  escript::C_GeneralTensorProduct(Data& ar
2199    
2200      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2201      int offset_1 = tmp_1->getPointOffset(0,0);      int offset_1 = tmp_1->getPointOffset(0,0);
2202      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2203      // Get the views      // Get the views
2204      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2205      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2206      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2207      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2208      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2209    
2210        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2211        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2212    
2213      // Compute an MVP for the default      // Compute an MVP for the default
2214      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2215      // Compute an MVP for each tag      // Compute an MVP for each tag
2216      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2217      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2218      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2219        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2220        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2221        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2222        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2223        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2224    
2225          tmp_2->addTag(i->first);
2226          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2227          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2228        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2229      }      }
2230    
# Line 2354  escript::C_GeneralTensorProduct(Data& ar Line 2245  escript::C_GeneralTensorProduct(Data& ar
2245      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2246      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2247    
2248      // Get the views  //     // Get the views
2249      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2250      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2251      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2252      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2253      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2254      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2255      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2256    
2257        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2258        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2259        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2260    
2261    
2262      // Compute an MVP for the default      // Compute an MVP for the default
2263      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2264      // Merge the tags      // Merge the tags
# Line 2369  escript::C_GeneralTensorProduct(Data& ar Line 2266  escript::C_GeneralTensorProduct(Data& ar
2266      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2267      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2268      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2269        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape        tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2270      }      }
2271      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2272        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2273      }      }
2274      // Compute an MVP for each tag      // Compute an MVP for each tag
2275      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2276      for (i=lookup_2.begin();i!=lookup_2.end();i++) {      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2277        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2278        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2279        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2280        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2281        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2282        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2283    
2284          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2285          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2286          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2287    
2288        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2289      }      }
2290    
# Line 2403  escript::C_GeneralTensorProduct(Data& ar Line 2305  escript::C_GeneralTensorProduct(Data& ar
2305      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2306      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2307        int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0        int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2308        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2309        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2310          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2311          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2312          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2313          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2314          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2315        }        }
2316      }      }
# Line 2432  escript::C_GeneralTensorProduct(Data& ar Line 2334  escript::C_GeneralTensorProduct(Data& ar
2334        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2335          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2336          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2337          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2338          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2339          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2340          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2341        }        }
2342      }      }
# Line 2457  escript::C_GeneralTensorProduct(Data& ar Line 2359  escript::C_GeneralTensorProduct(Data& ar
2359      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2360      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2361        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2362        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);        double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2363        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2364          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2365          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2366          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2367          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2368          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2369        }        }
2370      }      }
# Line 2487  escript::C_GeneralTensorProduct(Data& ar Line 2389  escript::C_GeneralTensorProduct(Data& ar
2389          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2390          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2391          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2392          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2393          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2394          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2395          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2396        }        }
2397      }      }
# Line 2508  Data::borrowData() const Line 2410  Data::borrowData() const
2410    return m_data.get();    return m_data.get();
2411  }  }
2412    
2413    
2414    std::string
2415    Data::toString() const
2416    {
2417        static const DataTypes::ValueType::size_type TOO_MANY_POINTS=80;
2418        if (getNumDataPoints()*getDataPointSize()>TOO_MANY_POINTS)
2419        {
2420        stringstream temp;
2421        temp << "Summary: inf="<< inf() << " sup=" << sup() << " data points=" << getNumDataPoints();
2422        return  temp.str();
2423        }
2424        return m_data->toString();
2425    }
2426    
2427    
2428    
2429    DataTypes::ValueType::const_reference
2430    Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2431    {
2432        return m_data->getDataAtOffset(i);
2433    }
2434    
2435    
2436    DataTypes::ValueType::reference
2437    Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2438    {
2439        return m_data->getDataAtOffset(i);
2440    }
2441    
2442    DataTypes::ValueType::const_reference
2443    Data::getDataPoint(int sampleNo, int dataPointNo) const
2444    {
2445        return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
2446    }
2447    
2448    
2449    DataTypes::ValueType::reference
2450    Data::getDataPoint(int sampleNo, int dataPointNo)
2451    {
2452        return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
2453    }
2454    
2455    
2456  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2457    
2458  void  void
2459  Data::print()  Data::print()
2460  {  {
2461    int i,j;    int i,j;
2462      
2463    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2464    for( i=0; i<getNumSamples(); i++ )    for( i=0; i<getNumSamples(); i++ )
2465    {    {
# Line 2524  Data::print() Line 2469  Data::print()
2469      printf( "\n" );      printf( "\n" );
2470    }    }
2471  }  }
2472    void
2473    Data::dump(const std::string fileName) const
2474    {
2475      try
2476         {
2477            return m_data->dump(fileName);
2478         }
2479         catch (exception& e)
2480         {
2481            cout << e.what() << endl;
2482         }
2483    }
2484    
2485  int  int
2486  Data::get_MPISize() const  Data::get_MPISize() const
2487  {  {
2488      int error, size;      int size;
2489  #ifdef PASO_MPI  #ifdef PASO_MPI
2490        int error;
2491      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
2492  #else  #else
2493      size = 1;      size = 1;
# Line 2540  Data::get_MPISize() const Line 2498  Data::get_MPISize() const
2498  int  int
2499  Data::get_MPIRank() const  Data::get_MPIRank() const
2500  {  {
2501      int error, rank;      int rank;
2502  #ifdef PASO_MPI  #ifdef PASO_MPI
2503        int error;
2504      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
2505  #else  #else
2506      rank = 0;      rank = 0;
# Line 2551  Data::get_MPIRank() const Line 2510  Data::get_MPIRank() const
2510    
2511  MPI_Comm  MPI_Comm
2512  Data::get_MPIComm() const  Data::get_MPIComm() const
2513  {  {
2514  #ifdef PASO_MPI  #ifdef PASO_MPI
2515      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
2516  #else  #else
# Line 2559  Data::get_MPIComm() const Line 2518  Data::get_MPIComm() const
2518  #endif  #endif
2519  }  }
2520    
2521    

Legend:
Removed from v.1092  
changed lines
  Added in v.1803

  ViewVC Help
Powered by ViewVC 1.1.26