/[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 790 by bcumming, Wed Jul 26 23:12:34 2006 UTC revision 1799 by jfenwick, Wed Sep 17 06:33:18 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 38  using namespace boost::python; Line 42  using namespace boost::python;
42  using namespace boost;  using namespace boost;
43  using namespace escript;  using namespace escript;
44    
 #if defined DOPROF  
 //  
 // global table of profiling data for all Data objects  
 DataProf dataProfTable;  
 #endif  
   
45  Data::Data()  Data::Data()
46  {  {
47    //    //
# Line 52  Data::Data() Line 50  Data::Data()
50    shared_ptr<DataAbstract> temp_data(temp);    shared_ptr<DataAbstract> temp_data(temp);
51    m_data=temp_data;    m_data=temp_data;
52    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
53  }  }
54    
55  Data::Data(double value,  Data::Data(double value,
# Line 63  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;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
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;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
89  }  }
90    
91  Data::Data(const Data& inData)  Data::Data(const Data& inData)
92  {  {
93    m_data=inData.m_data;    m_data=inData.m_data;
94    m_protected=inData.isProtected();    m_protected=inData.isProtected();
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
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 110  Data::Data(const Data& inData, Line 104  Data::Data(const Data& inData,
104    shared_ptr<DataAbstract> temp_data(tmp);    shared_ptr<DataAbstract> temp_data(tmp);
105    m_data=temp_data;    m_data=temp_data;
106    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
107  }  }
108    
109  Data::Data(const Data& inData,  Data::Data(const Data& inData,
110             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
111  {  {
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
112    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
113      m_data=inData.m_data;      m_data=inData.m_data;
114      } else if (inData.isConstant()) { // for a constant function, we just need to use the new function space
115        if (!inData.probeInterpolation(functionspace))
116        {           // Even though this is constant, we still need to check whether interpolation is allowed
117        throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
118        }
119        DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),inData.m_data->getVector());  
120        m_data=shared_ptr<DataAbstract>(dc);
121    } else {    } else {
122      #if defined DOPROF      Data tmp(0,inData.getDataPointShape(),functionspace,true);
     profData->interpolate++;  
     #endif  
     Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);  
123      // Note: Must use a reference or pointer to a derived object      // Note: Must use a reference or pointer to a derived object
124      // in order to get polymorphic behaviour. Shouldn't really      // in order to get polymorphic behaviour. Shouldn't really
125      // 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 145  Data::Data(const Data& inData, Line 135  Data::Data(const Data& inData,
135    m_protected=false;    m_protected=false;
136  }  }
137    
138  Data::Data(const DataTagged::TagListType& tagKeys,  // Data::Data(const DataTagged::TagListType& tagKeys,
139             const DataTagged::ValueListType & values,  //            const DataTagged::ValueListType & values,
140             const DataArrayView& defaultValue,  //            const DataArrayView& defaultValue,
141             const FunctionSpace& what,  //            const FunctionSpace& what,
142             bool expanded)  //            bool expanded)
143    // {
144    //   DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
145    //   shared_ptr<DataAbstract> temp_data(temp);
146    //   m_data=temp_data;
147    //   m_protected=false;
148    //   if (expanded) {
149    //     expand();
150    //   }
151    // }
152    
153    
154    
155    Data::Data(DataAbstract* underlyingdata)
156  {  {
157    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);      m_data=shared_ptr<DataAbstract>(underlyingdata);
158    shared_ptr<DataAbstract> temp_data(temp);      m_protected=false;
   m_data=temp_data;  
   m_protected=false;  
   if (expanded) {  
     expand();  
   }  
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
159  }  }
160    
161  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 170  Data::Data(const numeric::array& value, Line 164  Data::Data(const numeric::array& value,
164  {  {
165    initialise(value,what,expanded);    initialise(value,what,expanded);
166    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
167  }  }
168    /*
169  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
170         const FunctionSpace& what,         const FunctionSpace& what,
171             bool expanded)             bool expanded)
172  {  {
173    initialise(value,what,expanded);    initialise(value,what,expanded);
174    m_protected=false;    m_protected=false;
175  #if defined DOPROF  }*/
176    // create entry in global profiling table for this object  
177    profData = dataProfTable.newData();  Data::Data(const DataTypes::ValueType& value,
178  #endif           const DataTypes::ShapeType& shape,
179                     const FunctionSpace& what,
180                     bool expanded)
181    {
182       initialise(value,shape,what,expanded);
183       m_protected=false;
184  }  }
185    
186    
187  Data::Data(const object& value,  Data::Data(const object& value,
188         const FunctionSpace& what,         const FunctionSpace& what,
189             bool expanded)             bool expanded)
# Line 195  Data::Data(const object& value, Line 191  Data::Data(const object& value,
191    numeric::array asNumArray(value);    numeric::array asNumArray(value);
192    initialise(asNumArray,what,expanded);    initialise(asNumArray,what,expanded);
193    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
194  }  }
195    
196    
197  Data::Data(const object& value,  Data::Data(const object& value,
198             const Data& other)             const Data& other)
199  {  {
200      numeric::array asNumArray(value);
201    
202      // extract the shape of the numarray
203      DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
204    // /*  for (int i=0; i < asNumArray.getrank(); i++) {
205    //     tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
206    //   }*/
207    //   // get the space for the data vector
208    //   int len = DataTypes::noValues(tempShape);
209    //   DataVector temp_data(len, 0.0, len);
210    // /*  DataArrayView temp_dataView(temp_data, tempShape);
211    //   temp_dataView.copy(asNumArray);*/
212    //   temp_data.copyFromNumArray(asNumArray);
213    
214    //    //
215    // Create DataConstant using the given value and all other parameters    // Create DataConstant using the given value and all other parameters
216    // copied from other. If value is a rank 0 object this Data    // copied from other. If value is a rank 0 object this Data
217    // will assume the point data shape of other.    // will assume the point data shape of other.
218    DataArray temp(value);  
219    if (temp.getView().getRank()==0) {    if (DataTypes::getRank(tempShape)/*temp_dataView.getRank()*/==0) {
220      //  
221      // Create a DataArray with the scalar value for all elements  
222      DataArray temp2(other.getPointDataView().getShape(),temp.getView()());      // get the space for the data vector
223      initialise(temp2.getView(),other.getFunctionSpace(),false);      int len1 = DataTypes::noValues(tempShape);
224        DataVector temp_data(len1, 0.0, len1);
225        temp_data.copyFromNumArray(asNumArray);
226    
227        int len = DataTypes::noValues(other.getDataPointShape());
228    
229        DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
230        //DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
231    //     initialise(temp2_dataView, other.getFunctionSpace(), false);
232    
233        DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
234        boost::shared_ptr<DataAbstract> sp(t);
235        m_data=sp;
236    
237    
238    } else {    } else {
239      //      //
240      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
241      initialise(temp.getView(),other.getFunctionSpace(),false);  //     initialise(temp_dataView, other.getFunctionSpace(), false);
242        DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
243        boost::shared_ptr<DataAbstract> sp(t);
244        m_data=sp;
245    }    }
246    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
247  }  }
248    
249  Data::~Data()  Data::~Data()
# Line 231  Data::~Data() Line 251  Data::~Data()
251    
252  }  }
253    
254    
255    
256    void
257    Data::initialise(const boost::python::numeric::array& value,
258                     const FunctionSpace& what,
259                     bool expanded)
260    {
261      //
262      // Construct a Data object of the appropriate type.
263      // Construct the object first as there seems to be a bug which causes
264      // undefined behaviour if an exception is thrown during construction
265      // within the shared_ptr constructor.
266      if (expanded) {
267        DataAbstract* temp=new DataExpanded(value, what);
268        boost::shared_ptr<DataAbstract> temp_data(temp);
269        m_data=temp_data;
270      } else {
271        DataAbstract* temp=new DataConstant(value, what);
272        boost::shared_ptr<DataAbstract> temp_data(temp);
273        m_data=temp_data;
274      }
275    }
276    
277    
278    void
279    Data::initialise(const DataTypes::ValueType& value,
280             const DataTypes::ShapeType& shape,
281                     const FunctionSpace& what,
282                     bool expanded)
283    {
284      //
285      // Construct a Data object of the appropriate type.
286      // Construct the object first as there seems to be a bug which causes
287      // undefined behaviour if an exception is thrown during construction
288      // within the shared_ptr constructor.
289      if (expanded) {
290        DataAbstract* temp=new DataExpanded(what, shape, value);
291        boost::shared_ptr<DataAbstract> temp_data(temp);
292        m_data=temp_data;
293      } else {
294        DataAbstract* temp=new DataConstant(what, shape, value);
295        boost::shared_ptr<DataAbstract> temp_data(temp);
296        m_data=temp_data;
297      }
298    }
299    
300    
301    // void
302    // Data::CompareDebug(const Data& rd)
303    // {
304    //  using namespace std;
305    //  bool mismatch=false;
306    //  std::cout << "Comparing left and right" << endl;
307    //  const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
308    //  const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
309    //  
310    //  if (left==0)
311    //  {
312    //      cout << "left arg is not a DataTagged\n";
313    //      return;
314    //  }
315    //  
316    //  if (right==0)
317    //  {
318    //      cout << "right arg is not a DataTagged\n";
319    //      return;
320    //  }
321    //  cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
322    //  cout << "Shapes ";
323    //  if (left->getShape()==right->getShape())
324    //  {
325    //      cout << "ok\n";
326    //  }
327    //  else
328    //  {
329    //      cout << "Problem: shapes do not match\n";
330    //      mismatch=true;
331    //  }
332    //  int lim=left->getVector().size();
333    //  if (right->getVector().size()) lim=right->getVector().size();
334    //  for (int i=0;i<lim;++i)
335    //  {
336    //      if (left->getVector()[i]!=right->getVector()[i])
337    //      {
338    //          cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
339    //          mismatch=true;
340    //      }
341    //  }
342    //
343    //  // still need to check the tag map
344    //  // also need to watch what is happening to function spaces, are they copied or what?
345    //
346    //  const DataTagged::DataMapType& mapleft=left->getTagLookup();
347    //  const DataTagged::DataMapType& mapright=right->getTagLookup();
348    //
349    //  if (mapleft.size()!=mapright.size())
350    //  {
351    //      cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
352    //      mismatch=true;
353    //      cout << "Left map\n";
354    //      DataTagged::DataMapType::const_iterator i,j;
355    //      for (i=mapleft.begin();i!=mapleft.end();++i) {
356    //          cout << "(" << i->first << "=>" << i->second << ")\n";
357    //      }
358    //      cout << "Right map\n";
359    //      for (i=mapright.begin();i!=mapright.end();++i) {
360    //          cout << "(" << i->first << "=>" << i->second << ")\n";
361    //      }
362    //      cout << "End map\n";
363    //
364    //  }
365    //
366    //  DataTagged::DataMapType::const_iterator i,j;
367    //  for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
368    //     if ((i->first!=j->first) || (i->second!=j->second))
369    //     {
370    //      cout << "(" << i->first << "=>" << i->second << ")";
371    //      cout << ":(" << j->first << "=>" << j->second << ") ";
372    //      mismatch=true;
373    //            }
374    //  }
375    //  if (mismatch)
376    //  {
377    //      cout << "#Mismatch\n";
378    //  }
379    // }
380    
381  escriptDataC  escriptDataC
382  Data::getDataC()  Data::getDataC()
383  {  {
# Line 250  Data::getDataC() const Line 397  Data::getDataC() const
397  const boost::python::tuple  const boost::python::tuple
398  Data::getShapeTuple() const  Data::getShapeTuple() const
399  {  {
400    const DataArrayView::ShapeType& shape=getDataPointShape();    const DataTypes::ShapeType& shape=getDataPointShape();
401    switch(getDataPointRank()) {    switch(getDataPointRank()) {
402       case 0:       case 0:
403          return make_tuple();          return make_tuple();
# Line 267  Data::getShapeTuple() const Line 414  Data::getShapeTuple() const
414    }    }
415  }  }
416    
417    
418    // The different name is needed because boost has trouble with overloaded functions.
419    // It can't work out what type the function is based soley on its name.
420    // There are ways to fix this involving creating function pointer variables for each form
421    // but there doesn't seem to be a need given that the methods have the same name from the python point of view
422    Data*
423    Data::copySelf()
424    {
425       DataAbstract* temp=m_data->deepCopy();
426       return new Data(temp);
427    }
428    
429  void  void
430  Data::copy(const Data& other)  Data::copy(const Data& other)
431  {  {
432    //    DataAbstract* temp=other.m_data->deepCopy();
433    // Perform a deep copy    shared_ptr<DataAbstract> temp_data(temp);
434    {    m_data=temp_data;
435      DataExpanded* temp=dynamic_cast<DataExpanded*>(other.m_data.get());  }
436      if (temp!=0) {  
437        //  
438        // Construct a DataExpanded copy  void
439        DataAbstract* newData=new DataExpanded(*temp);  Data::setToZero()
440        shared_ptr<DataAbstract> temp_data(newData);  {
       m_data=temp_data;  
       return;  
     }  
   }  
441    {    {
442      DataTagged* temp=dynamic_cast<DataTagged*>(other.m_data.get());      DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
443      if (temp!=0) {      if (temp!=0) {
444        //         temp->setToZero();
445        // Construct a DataTagged copy         return;
       DataAbstract* newData=new DataTagged(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
446      }      }
447    }    }
448    {    {
449      DataConstant* temp=dynamic_cast<DataConstant*>(other.m_data.get());      DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
450      if (temp!=0) {      if (temp!=0) {
451        //        temp->setToZero();
       // Construct a DataConstant copy  
       DataAbstract* newData=new DataConstant(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
452        return;        return;
453      }      }
454    }    }
455    {    {
456      DataEmpty* temp=dynamic_cast<DataEmpty*>(other.m_data.get());      DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
457      if (temp!=0) {      if (temp!=0) {
458        //        temp->setToZero();
       // Construct a DataEmpty copy  
       DataAbstract* newData=new DataEmpty();  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
459        return;        return;
460      }      }
461    }    }
462    throw DataException("Error - Copy not implemented for this Data type.");    throw DataException("Error - Data can not be set to zero.");
463  }  }
464    
465  void  void
# Line 350  Data::isTagged() const Line 493  Data::isTagged() const
493    return (temp!=0);    return (temp!=0);
494  }  }
495    
 /* TODO */  
 /* global reduction -- the local data being empty does not imply that it is empty on other processers*/  
496  bool  bool
497  Data::isEmpty() const  Data::isEmpty() const
498  {  {
# Line 367  Data::isConstant() const Line 508  Data::isConstant() const
508  }  }
509    
510  void  void
511  Data::setProtection()  Data::setProtection()
512  {  {
513     m_protected=true;     m_protected=true;
514  }  }
515    
516  bool  bool
517  Data::isProtected() const  Data::isProtected() const
518  {  {
519     return m_protected;     return m_protected;
520  }  }
521    
# Line 422  Data::tag() Line 563  Data::tag()
563    }    }
564  }  }
565    
566  void  Data
567  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::oneOver() const
568  {  {
569    m_data->reshapeDataPoint(shape);    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
570  }  }
571    
572  Data  Data
573  Data::wherePositive() const  Data::wherePositive() const
574  {  {
575  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));  
576  }  }
577    
578  Data  Data
579  Data::whereNegative() const  Data::whereNegative() const
580  {  {
581  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(less<double>(),0.0));  
582  }  }
583    
584  Data  Data
585  Data::whereNonNegative() const  Data::whereNonNegative() const
586  {  {
587  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));  
588  }  }
589    
590  Data  Data
591  Data::whereNonPositive() const  Data::whereNonPositive() const
592  {  {
593  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));  
594  }  }
595    
596  Data  Data
597  Data::whereZero(double tol) const  Data::whereZero(double tol) const
598  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
599    Data dataAbs=abs();    Data dataAbs=abs();
600    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
601  }  }
602    
603  Data  Data
604  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
605  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
606    Data dataAbs=abs();    Data dataAbs=abs();
607    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
608  }  }
609    
610  Data  Data
611  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
612  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
613    return Data(*this,functionspace);    return Data(*this,functionspace);
614  }  }
615    
# Line 511  Data::probeInterpolation(const FunctionS Line 631  Data::probeInterpolation(const FunctionS
631  Data  Data
632  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
633  {  {
634  #if defined DOPROF    double blocktimer_start = blocktimer_time();
   profData->grad++;  
 #endif  
635    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
636      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
637    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
638    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
639    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
640    getDomain().setToGradient(out,*this);    getDomain().setToGradient(out,*this);
641      blocktimer_increment("grad()", blocktimer_start);
642    return out;    return out;
643  }  }
644    
# Line 532  Data::grad() const Line 651  Data::grad() const
651  int  int
652  Data::getDataPointSize() const  Data::getDataPointSize() const
653  {  {
654    return getPointDataView().noValues();    return m_data->getNoValues();
655  }  }
656    
657  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
658  Data::getLength() const  Data::getLength() const
659  {  {
660    return m_data->getLength();    return m_data->getLength();
661  }  }
662    
663  const DataArrayView::ShapeType&  // const DataTypes::ShapeType&
664  Data::getDataPointShape() const  // Data::getDataPointShape() const
665  {  // {
666    return getPointDataView().getShape();  //   return getPointDataView().getShape();
667  }  // }
668    
 void  
 Data::fillFromNumArray(const boost::python::numeric::array num_array)  
 {  
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
   //  
   // check rank  
   if (num_array.getrank()<getDataPointRank())  
       throw DataException("Rank of numarray does not match Data object rank");  
669    
   //  
   // check shape of num_array  
   for (int i=0; i<getDataPointRank(); i++) {  
     if (extract<int>(num_array.getshape()[i+1])!=getDataPointShape()[i])  
        throw DataException("Shape of numarray does not match Data object rank");  
   }  
   
   //  
   // make sure data is expanded:  
   if (!isExpanded()) {  
     expand();  
   }  
670    
   //  
   // and copy over  
   m_data->copyAll(num_array);  
 }  
671    
672  const  const
673  boost::python::numeric::array  boost::python::numeric::array
674  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
675  {  {
676    //    size_t length=0;
677    // determine the total number of data points    int i, j, k, l;
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
   
678    //    //
679    // determine the rank and shape of each data point    // determine the rank and shape of each data point
680    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
681    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
682    
683    //    //
684    // create the numeric array to be returned    // create the numeric array to be returned
685    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
686    
687    //    //
688    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
689    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
690    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
691    // data points, whilst the first dimension will be equal to the    const DataTypes::ShapeType& arrayShape = dataPointShape;
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPoints);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
   }  
692    
693    //    //
694    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
695      if (arrayRank==0) {
696        numArray.resize(1);
697      }
698    if (arrayRank==1) {    if (arrayRank==1) {
699      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
700    }    }
# Line 623  Data::convertToNumArray() Line 707  Data::convertToNumArray()
707    if (arrayRank==4) {    if (arrayRank==4) {
708      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
709    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
710    
711    //    if (getNumDataPointsPerSample()>0) {
712    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
713    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
714    int dataPoint = 0;         //
715    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
716      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
717        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
718        if (dataPointRank==0) {         }
719          numArray[dataPoint]=dataPointView();  
720        }         //
721        if (dataPointRank==1) {         // Check a valid data point number has been supplied
722          for (int i=0; i<dataPointShape[0]; i++) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
723            numArray[dataPoint][i]=dataPointView(i);             throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
724          }         }
725        }         // TODO: global error handling
726        if (dataPointRank==2) {         // create a view of the data if it is stored locally
727          for (int i=0; i<dataPointShape[0]; i++) {  //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
728            for (int j=0; j<dataPointShape[1]; j++) {         DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
729              numArray[dataPoint][i][j] = dataPointView(i,j);  
           }  
         }  
       }  
       if (dataPointRank==3) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               numArray[dataPoint][i][j][k]=dataPointView(i,j,k);  
             }  
           }  
         }  
       }  
       if (dataPointRank==4) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               for (int l=0; l<dataPointShape[3]; l++) {  
                 numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);  
               }  
             }  
           }  
         }  
       }  
       dataPoint++;  
     }  
   }  
730    
731           switch( dataPointRank ){
732                case 0 :
733                    numArray[0] = getDataAtOffset(offset);
734                    break;
735                case 1 :
736                    for( i=0; i<dataPointShape[0]; i++ )
737                        numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
738                    break;
739                case 2 :
740                    for( i=0; i<dataPointShape[0]; i++ )
741                        for( j=0; j<dataPointShape[1]; j++)
742                            numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
743                    break;
744                case 3 :
745                    for( i=0; i<dataPointShape[0]; i++ )
746                        for( j=0; j<dataPointShape[1]; j++ )
747                            for( k=0; k<dataPointShape[2]; k++)
748                                numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
749                    break;
750                case 4 :
751                    for( i=0; i<dataPointShape[0]; i++ )
752                        for( j=0; j<dataPointShape[1]; j++ )
753                            for( k=0; k<dataPointShape[2]; k++ )
754                                for( l=0; l<dataPointShape[3]; l++)
755                                    numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
756                    break;
757        }
758      }
759    //    //
760    // return the loaded array    // return the array
761    return numArray;    return numArray;
762    
763  }  }
764    
765  const  void
766  boost::python::numeric::array  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
 Data::convertToNumArrayFromSampleNo(int sampleNo)  
767  {  {
768    //      // this will throw if the value cannot be represented
769    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
770    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
771    
   //  
   // determine the number of data points per sample  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
772    
773    //  }
   // determine the rank and shape of each data point  
   int dataPointRank = getDataPointRank();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
774    
775    void
776    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
777    {
778      if (isProtected()) {
779            throw DataException("Error - attempt to update protected Data object.");
780      }
781    //    //
782    // create the numeric array to be returned    // check rank
783    boost::python::numeric::array numArray(0.0);    if (num_array.getrank()<getDataPointRank())
784          throw DataException("Rank of numarray does not match Data object rank");
785    
786    //    //
787    // the rank of the returned numeric array will be the rank of    // check shape of num_array
788    // the data points, plus one. Where the rank of the array is n,    for (int i=0; i<getDataPointRank(); i++) {
789    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
790    // data points, whilst the first dimension will be equal to the         throw DataException("Shape of numarray does not match Data object rank");
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPointsPerSample);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
791    }    }
   
792    //    //
793    // resize the numeric array to the shape just calculated    // make sure data is expanded:
794    if (arrayRank==1) {    if (!isExpanded()) {
795      numArray.resize(arrayShape[0]);      expand();
   }  
   if (arrayRank==2) {  
     numArray.resize(arrayShape[0],arrayShape[1]);  
   }  
   if (arrayRank==3) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);  
   }  
   if (arrayRank==4) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);  
796    }    }
797    if (arrayRank==5) {    if (getNumDataPointsPerSample()>0) {
798      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);         int sampleNo = dataPointNo/getNumDataPointsPerSample();
799           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
800           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
801      } else {
802           m_data->copyToDataPoint(-1, 0,num_array);
803    }    }
804    }
805    
806    //  void
807    // loop through each data point in turn, loading the values for that data point  Data::setValueOfDataPoint(int dataPointNo, const double value)
808    // into the numeric array.  {
809    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    if (isProtected()) {
810      DataArrayView dataPointView = getDataPoint(sampleNo, dataPoint);          throw DataException("Error - attempt to update protected Data object.");
     if (dataPointRank==0) {  
       numArray[dataPoint]=dataPointView();  
     }  
     if (dataPointRank==1) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         numArray[dataPoint][i]=dataPointView(i);  
       }  
     }  
     if (dataPointRank==2) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           numArray[dataPoint][i][j] = dataPointView(i,j);  
         }  
       }  
     }  
     if (dataPointRank==3) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             numArray[dataPoint][i][j][k]=dataPointView(i,j,k);  
           }  
         }  
       }  
     }  
     if (dataPointRank==4) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             for (int l=0; l<dataPointShape[3]; l++) {  
               numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);  
             }  
           }  
         }  
       }  
     }  
811    }    }
   
812    //    //
813    // return the loaded array    // make sure data is expanded:
814    return numArray;    if (!isExpanded()) {
815        expand();
816      }
817      if (getNumDataPointsPerSample()>0) {
818           int sampleNo = dataPointNo/getNumDataPointsPerSample();
819           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
820           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
821      } else {
822           m_data->copyToDataPoint(-1, 0,value);
823      }
824  }  }
825    
826  const  const
827  boost::python::numeric::array  boost::python::numeric::array
828  Data::convertToNumArrayFromDPNo(int procNo,  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
                                 int sampleNo,  
                                                                 int dataPointNo)  
   
829  {  {
830      size_t length=0;    size_t length=0;
831      int i, j, k, l, pos;    int i, j, k, l, pos;
   
   //  
   // Check a valid sample number has been supplied  
   if (sampleNo >= getNumSamples()) {  
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
   
   //  
   // Check a valid data point number has been supplied  
   if (dataPointNo >= getNumDataPointsPerSample()) {  
     throw DataException("Error - Data::convertToNumArray: invalid dataPointNo.");  
   }  
   
832    //    //
833    // determine the rank and shape of each data point    // determine the rank and shape of each data point
834    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
835    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
836    
837    //    //
838    // create the numeric array to be returned    // create the numeric array to be returned
# Line 815  Data::convertToNumArrayFromDPNo(int proc Line 842  Data::convertToNumArrayFromDPNo(int proc
842    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
843    // as that of the data point    // as that of the data point
844    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
845    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
846    
847    //    //
848    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 835  Data::convertToNumArrayFromDPNo(int proc Line 862  Data::convertToNumArrayFromDPNo(int proc
862      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
863    }    }
864    
865      // added for the MPI communication    // added for the MPI communication
866      length=1;    length=1;
867      for( i=0; i<arrayRank; i++ )    for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
868          length *= arrayShape[i];    double *tmpData = new double[length];
     double *tmpData = new double[length];  
869    
870    //    //
871    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
872    
873      // updated for the MPI case      // updated for the MPI case
874      if( get_MPIRank()==procNo ){      if( get_MPIRank()==procNo ){
875                 if (getNumDataPointsPerSample()>0) {
876                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
877                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
878                    //
879                    // Check a valid sample number has been supplied
880                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
881                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
882                    }
883    
884                    //
885                    // Check a valid data point number has been supplied
886                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
887                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
888                    }
889                    // TODO: global error handling
890          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
891          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
892                    DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
893    
894          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
895          pos=0;          pos=0;
896          switch( dataPointRank ){          switch( dataPointRank ){
897              case 0 :              case 0 :
898                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
899                  break;                  break;
900              case 1 :                      case 1 :
901                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
902                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
903                  break;                  break;
904              case 2 :                      case 2 :
905                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
906                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
907                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
908                  break;                  break;
909              case 3 :                      case 3 :
910                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
911                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
912                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
913                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
914                  break;                  break;
915              case 4 :              case 4 :
916                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
917                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
918                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
919                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
920                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
921                  break;                  break;
922          }          }
923                }
924      }      }
925  #ifdef PASO_MPI          #ifdef PASO_MPI
926          // broadcast the data to all other processes          // broadcast the data to all other processes
927          MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
928  #endif          #endif
929    
930      // unpack the data      // unpack the data
931      switch( dataPointRank ){      switch( dataPointRank ){
932          case 0 :          case 0 :
933              numArray[i]=tmpData[0];              numArray[0]=tmpData[0];
934              break;              break;
935          case 1 :                  case 1 :
936              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
937                  numArray[i]=tmpData[i];                  numArray[i]=tmpData[i];
938              break;              break;
939          case 2 :                  case 2 :
940              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
941                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
942                      tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
943              break;              break;
944          case 3 :                  case 3 :
945              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
946                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
947                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
948                          tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];                          numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
949              break;              break;
950          case 4 :          case 4 :
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                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
954                          for( l=0; l<dataPointShape[3]; l++ )                          for( l=0; l<dataPointShape[3]; l++ )
955                              tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];                                  numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
956              break;              break;
957      }      }
958    
959      delete [] tmpData;        delete [] tmpData;
 /*  
   if (dataPointRank==0) {  
     numArray[0]=dataPointView();  
   }  
   if (dataPointRank==1) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       numArray[i]=dataPointView(i);  
     }  
   }  
   if (dataPointRank==2) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         numArray[i][j] = dataPointView(i,j);  
       }  
     }  
   }  
   if (dataPointRank==3) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           numArray[i][j][k]=dataPointView(i,j,k);  
         }  
       }  
     }  
   }  
   if (dataPointRank==4) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           for (int l=0; l<dataPointShape[3]; l++) {  
             numArray[i][j][k][l]=dataPointView(i,j,k,l);  
           }  
         }  
       }  
     }  
   }  
 */  
   
960    //    //
961    // return the loaded array    // return the loaded array
962    return numArray;    return numArray;
963  }  }
964    
965    
966    
967  boost::python::numeric::array  boost::python::numeric::array
968  Data::integrate() const  Data::integrate() const
969  {  {
970    int index;    int index;
971    int rank = getDataPointRank();    int rank = getDataPointRank();
972    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
973      int dataPointSize = getDataPointSize();
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
974    
975    //    //
976    // calculate the integral values    // calculate the integral values
977    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
978      vector<double> integrals_local(dataPointSize);
979    #ifdef PASO_MPI
980      AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals_local,*this);
981      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
982      double *tmp = new double[dataPointSize];
983      double *tmp_local = new double[dataPointSize];
984      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
985      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
986      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
987      delete[] tmp;
988      delete[] tmp_local;
989    #else
990    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);
991    #endif
992    
993    //    //
994    // create the numeric array to be returned    // create the numeric array to be returned
# Line 1031  Data::integrate() const Line 1048  Data::integrate() const
1048  Data  Data
1049  Data::sin() const  Data::sin() const
1050  {  {
1051  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);  
1052  }  }
1053    
1054  Data  Data
1055  Data::cos() const  Data::cos() const
1056  {  {
1057  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);  
1058  }  }
1059    
1060  Data  Data
1061  Data::tan() const  Data::tan() const
1062  {  {
1063  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);  
1064  }  }
1065    
1066  Data  Data
1067  Data::asin() const  Data::asin() const
1068  {  {
1069  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);  
1070  }  }
1071    
1072  Data  Data
1073  Data::acos() const  Data::acos() const
1074  {  {
1075  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);  
1076  }  }
1077    
1078    
1079  Data  Data
1080  Data::atan() const  Data::atan() const
1081  {  {
1082  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);  
1083  }  }
1084    
1085  Data  Data
1086  Data::sinh() const  Data::sinh() const
1087  {  {
1088  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1089    profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);  
1090  }  }
1091    
1092  Data  Data
1093  Data::cosh() const  Data::cosh() const
1094  {  {
1095  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);  
1096  }  }
1097    
1098  Data  Data
1099  Data::tanh() const  Data::tanh() const
1100  {  {
1101  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1102    profData->unary++;  }
1103    
1104    
1105    Data
1106    Data::erf() const
1107    {
1108    #ifdef _WIN32
1109      throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1110    #else
1111      return C_TensorUnaryOperation(*this, ::erf);
1112  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);  
1113  }  }
1114    
1115  Data  Data
1116  Data::asinh() const  Data::asinh() const
1117  {  {
1118  #if defined DOPROF  #ifdef _WIN32
1119    profData->unary++;    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1120    #else
1121      return C_TensorUnaryOperation(*this, ::asinh);
1122  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);  
1123  }  }
1124    
1125  Data  Data
1126  Data::acosh() const  Data::acosh() const
1127  {  {
1128  #if defined DOPROF  #ifdef _WIN32
1129    profData->unary++;    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1130    #else
1131      return C_TensorUnaryOperation(*this, ::acosh);
1132  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);  
1133  }  }
1134    
1135  Data  Data
1136  Data::atanh() const  Data::atanh() const
1137  {  {
1138  #if defined DOPROF  #ifdef _WIN32
1139    profData->unary++;    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1140    #else
1141      return C_TensorUnaryOperation(*this, ::atanh);
1142  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);  
1143  }  }
1144    
1145  Data  Data
1146  Data::log10() const  Data::log10() const
1147  {  {
1148  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);  
1149  }  }
1150    
1151  Data  Data
1152  Data::log() const  Data::log() const
1153  {  {
1154  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);  
1155  }  }
1156    
1157  Data  Data
1158  Data::sign() const  Data::sign() const
1159  {  {
1160  #if defined DOPROF    return C_TensorUnaryOperation(*this, escript::fsign);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,escript::fsign);  
1161  }  }
1162    
1163  Data  Data
1164  Data::abs() const  Data::abs() const
1165  {  {
1166  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);  
1167  }  }
1168    
1169  Data  Data
1170  Data::neg() const  Data::neg() const
1171  {  {
1172  #if defined DOPROF    return C_TensorUnaryOperation(*this, negate<double>());
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,negate<double>());  
1173  }  }
1174    
1175  Data  Data
1176  Data::pos() const  Data::pos() const
1177  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1178    Data result;    Data result;
1179    // perform a deep copy    // perform a deep copy
1180    result.copy(*this);    result.copy(*this);
# Line 1196  Data::pos() const Line 1184  Data::pos() const
1184  Data  Data
1185  Data::exp() const  Data::exp() const
1186  {  {
1187  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);  
1188  }  }
1189    
1190  Data  Data
1191  Data::sqrt() const  Data::sqrt() const
1192  {  {
1193  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);  
1194  }  }
1195    
1196  double  double
1197  Data::Lsup() const  Data::Lsup() const
1198  {  {
1199    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1200    //    //
1201    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1202    
1203    AbsMax abs_max_func;    AbsMax abs_max_func;
1204    localValue = algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1205  #ifdef PASO_MPI  #ifdef PASO_MPI
1206      double globalValue;
1207    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1208    return globalValue;    return globalValue;
1209  #else  #else
# Line 1232  Data::Lsup() const Line 1212  Data::Lsup() const
1212  }  }
1213    
1214  double  double
 Data::Linf() const  
 {  
   double localValue, globalValue;  
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
   //  
   // 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  
1215  Data::sup() const  Data::sup() const
1216  {  {
1217    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1218    //    //
1219    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1220    FMax fmax_func;    FMax fmax_func;
1221    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1222  #ifdef PASO_MPI  #ifdef PASO_MPI
1223      double globalValue;
1224    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1225    return globalValue;    return globalValue;
1226  #else  #else
# Line 1273  Data::sup() const Line 1231  Data::sup() const
1231  double  double
1232  Data::inf() const  Data::inf() const
1233  {  {
1234    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1235    //    //
1236    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1237    FMin fmin_func;    FMin fmin_func;
1238    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1239  #ifdef PASO_MPI  #ifdef PASO_MPI
1240      double globalValue;
1241    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1242    return globalValue;    return globalValue;
1243  #else  #else
# Line 1294  Data::inf() const Line 1250  Data::inf() const
1250  Data  Data
1251  Data::maxval() const  Data::maxval() const
1252  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1253    //    //
1254    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1255    FMax fmax_func;    FMax fmax_func;
# Line 1306  Data::maxval() const Line 1259  Data::maxval() const
1259  Data  Data
1260  Data::minval() const  Data::minval() const
1261  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1262    //    //
1263    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1264    FMin fmin_func;    FMin fmin_func;
# Line 1316  Data::minval() const Line 1266  Data::minval() const
1266  }  }
1267    
1268  Data  Data
1269  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1270  {  {
1271  #if defined DOPROF       int axis0_tmp,axis1_tmp;
1272    profData->reduction2++;       DataTypes::ShapeType s=getDataPointShape();
1273  #endif       DataTypes::ShapeType ev_shape;
1274    Trace trace_func;       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1275    return dp_algorithm(trace_func,0);       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1276         int rank=getDataPointRank();
1277         if (rank<2) {
1278            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1279         }
1280         if (axis0<0 || axis0>rank-1) {
1281            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1282         }
1283         if (axis1<0 || axis1>rank-1) {
1284             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1285         }
1286         if (axis0 == axis1) {
1287             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1288         }
1289         if (axis0 > axis1) {
1290             axis0_tmp=axis1;
1291             axis1_tmp=axis0;
1292         } else {
1293             axis0_tmp=axis0;
1294             axis1_tmp=axis1;
1295         }
1296         for (int i=0; i<rank; i++) {
1297           if (i == axis0_tmp) {
1298              ev_shape.push_back(s[axis1_tmp]);
1299           } else if (i == axis1_tmp) {
1300              ev_shape.push_back(s[axis0_tmp]);
1301           } else {
1302              ev_shape.push_back(s[i]);
1303           }
1304         }
1305         Data ev(0.,ev_shape,getFunctionSpace());
1306         ev.typeMatchRight(*this);
1307         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1308         return ev;
1309    
1310  }  }
1311    
1312  Data  Data
1313  Data::symmetric() const  Data::symmetric() const
1314  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1315       // check input       // check input
1316       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1317       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1318          if(s[0] != s[1])          if(s[0] != s[1])
1319             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.");
1320       }       }
1321       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
# Line 1353  Data::symmetric() const Line 1334  Data::symmetric() const
1334  Data  Data
1335  Data::nonsymmetric() const  Data::nonsymmetric() const
1336  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1337       // check input       // check input
1338       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1339       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1340          if(s[0] != s[1])          if(s[0] != s[1])
1341             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.");
1342          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1343          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1344          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1345          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1372  Data::nonsymmetric() const Line 1350  Data::nonsymmetric() const
1350       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1351          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1352             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.");
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          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1388  Data::nonsymmetric() const Line 1366  Data::nonsymmetric() const
1366  }  }
1367    
1368  Data  Data
1369  Data::matrixtrace(int axis_offset) const  Data::trace(int axis_offset) const
1370  {  {
1371       #if defined DOPROF       DataTypes::ShapeType s=getDataPointShape();
         profData->unary++;  
      #endif  
      DataArrayView::ShapeType s=getDataPointShape();  
1372       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1373          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1374          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1375          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1376          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1377          return ev;          return ev;
1378       }       }
1379       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1380          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1381          if (axis_offset==0) {          if (axis_offset==0) {
1382            int s2=s[2];            int s2=s[2];
1383            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1413  Data::matrixtrace(int axis_offset) const Line 1388  Data::matrixtrace(int axis_offset) const
1388          }          }
1389          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1390          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1391          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1392          return ev;          return ev;
1393       }       }
1394       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1395          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1396          if (axis_offset==0) {          if (axis_offset==0) {
1397            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1398            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1432  Data::matrixtrace(int axis_offset) const Line 1407  Data::matrixtrace(int axis_offset) const
1407      }      }
1408          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1409          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1410      m_data->matrixtrace(ev.m_data.get(), axis_offset);      m_data->trace(ev.m_data.get(), axis_offset);
1411          return ev;          return ev;
1412       }       }
1413       else {       else {
1414          throw DataException("Error - Data::matrixtrace can only be calculated for rank 2, 3 or 4 object.");          throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1415       }       }
1416  }  }
1417    
1418  Data  Data
1419  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1420  {  {
1421  #if defined DOPROF       DataTypes::ShapeType s=getDataPointShape();
1422       profData->reduction2++;       DataTypes::ShapeType ev_shape;
 #endif  
      DataArrayView::ShapeType s=getDataPointShape();  
      DataArrayView::ShapeType ev_shape;  
1423       // 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]
1424       // 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)
1425       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1467  Data::transpose(int axis_offset) const Line 1439  Data::transpose(int axis_offset) const
1439  Data  Data
1440  Data::eigenvalues() const  Data::eigenvalues() const
1441  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1442       // check input       // check input
1443       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1444       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1445          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.");
1446       if(s[0] != s[1])       if(s[0] != s[1])
1447          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.");
1448       // create return       // create return
1449       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1450       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1451       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1452       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1487  Data::eigenvalues() const Line 1456  Data::eigenvalues() const
1456  const boost::python::tuple  const boost::python::tuple
1457  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1458  {  {
1459       #if defined DOPROF       DataTypes::ShapeType s=getDataPointShape();
1460          profData->unary++;       if (getDataPointRank()!=2)
      #endif  
      DataArrayView::ShapeType s=getDataPointShape();  
      if (getDataPointRank()!=2)  
1461          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.");
1462       if(s[0] != s[1])       if(s[0] != s[1])
1463          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.");
1464       // create return       // create return
1465       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1466       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1467       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1468       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1469       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1470       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1471       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 1507  Data::eigenvalues_and_eigenvectors(const Line 1473  Data::eigenvalues_and_eigenvectors(const
1473  }  }
1474    
1475  const boost::python::tuple  const boost::python::tuple
1476  Data::mindp() const  Data::minGlobalDataPoint() const
1477  {  {
1478    // NB: calc_mindp had to be split off from mindp as boost::make_tuple causes an    // NB: calc_minGlobalDataPoint( had to be split off from minGlobalDataPoint( as boost::make_tuple causes an
1479    // abort (for unknown reasons) if there are openmp directives with it in the    // abort (for unknown reasons) if there are openmp directives with it in the
1480    // surrounding function    // surrounding function
1481    
   int SampleNo;  
1482    int DataPointNo;    int DataPointNo;
1483      int ProcNo;    int ProcNo;
1484    calc_mindp(ProcNo,SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1485    return make_tuple(ProcNo,SampleNo,DataPointNo);    return make_tuple(ProcNo,DataPointNo);
1486  }  }
1487    
1488  void  void
1489  Data::calc_mindp(   int& ProcNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1490                  int& SampleNo,                          int& DataPointNo) const
         int& DataPointNo) const  
1491  {  {
1492    int i,j;    int i,j;
1493    int lowi=0,lowj=0;    int lowi=0,lowj=0;
# Line 1543  Data::calc_mindp(  int& ProcNo, Line 1507  Data::calc_mindp(  int& ProcNo,
1507      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1508      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1509        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1510          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1511          if (next<local_min) {          if (next<local_min) {
1512            local_min=next;            local_min=next;
1513            local_lowi=i;            local_lowi=i;
# Line 1561  Data::calc_mindp(  int& ProcNo, Line 1525  Data::calc_mindp(  int& ProcNo,
1525    
1526  #ifdef PASO_MPI  #ifdef PASO_MPI
1527      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1528      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1529      int lowProc = 0;      int lowProc = 0;
1530      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1531      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() );
1532        
1533      if( get_MPIRank()==0 ){      if( get_MPIRank()==0 ){
1534          next = globalMins[lowProc];          next = globalMins[lowProc];
1535          for( i=1; i<get_MPISize(); i++ )          for( i=1; i<get_MPISize(); i++ )
# Line 1581  Data::calc_mindp(  int& ProcNo, Line 1545  Data::calc_mindp(  int& ProcNo,
1545  #else  #else
1546      ProcNo = 0;      ProcNo = 0;
1547  #endif  #endif
1548    SampleNo = lowi;    DataPointNo = lowj + lowi * numDPPSample;
   DataPointNo = lowj;  
1549  }  }
1550    
1551  void  void
# Line 1609  Data::operator+=(const Data& right) Line 1572  Data::operator+=(const Data& right)
1572    if (isProtected()) {    if (isProtected()) {
1573          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1574    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1575    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1576    return (*this);    return (*this);
1577  }  }
# Line 1619  Data::operator+=(const Data& right) Line 1579  Data::operator+=(const Data& right)
1579  Data&  Data&
1580  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
1581  {  {
1582    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1583          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,plus<double>());
1584    }    return (*this);
1585  #if defined DOPROF  }
1586    profData->binary++;  Data&
1587  #endif  Data::operator=(const Data& other)
1588    binaryOp(right,plus<double>());  {
1589      copy(other);
1590    return (*this);    return (*this);
1591  }  }
1592    
# Line 1635  Data::operator-=(const Data& right) Line 1596  Data::operator-=(const Data& right)
1596    if (isProtected()) {    if (isProtected()) {
1597          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1598    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1599    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1600    return (*this);    return (*this);
1601  }  }
# Line 1645  Data::operator-=(const Data& right) Line 1603  Data::operator-=(const Data& right)
1603  Data&  Data&
1604  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
1605  {  {
1606    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1607          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,minus<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,minus<double>());  
1608    return (*this);    return (*this);
1609  }  }
1610    
# Line 1661  Data::operator*=(const Data& right) Line 1614  Data::operator*=(const Data& right)
1614    if (isProtected()) {    if (isProtected()) {
1615          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1616    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1617    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1618    return (*this);    return (*this);
1619  }  }
# Line 1671  Data::operator*=(const Data& right) Line 1621  Data::operator*=(const Data& right)
1621  Data&  Data&
1622  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1623  {  {
1624    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1625          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,multiplies<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,multiplies<double>());  
1626    return (*this);    return (*this);
1627  }  }
1628    
# Line 1687  Data::operator/=(const Data& right) Line 1632  Data::operator/=(const Data& right)
1632    if (isProtected()) {    if (isProtected()) {
1633          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1634    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1635    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1636    return (*this);    return (*this);
1637  }  }
# Line 1697  Data::operator/=(const Data& right) Line 1639  Data::operator/=(const Data& right)
1639  Data&  Data&
1640  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
1641  {  {
1642    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1643          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,divides<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,divides<double>());  
1644    return (*this);    return (*this);
1645  }  }
1646    
1647  Data  Data
1648  Data::rpowO(const boost::python::object& left) const  Data::rpowO(const boost::python::object& left) const
1649  {  {
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1650    Data left_d(left,*this);    Data left_d(left,*this);
1651    return left_d.powD(*this);    return left_d.powD(*this);
1652  }  }
# Line 1723  Data::rpowO(const boost::python::object& Line 1654  Data::rpowO(const boost::python::object&
1654  Data  Data
1655  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1656  {  {
1657  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1658    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1659  }  }
1660    
1661  Data  Data
1662  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1663  {  {
1664  #if defined DOPROF    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   profData->binary++;  
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1665  }  }
1666    
   
1667  //  //
1668  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1669  Data  Data
1670  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1671  {  {
1672    Data result;    return C_TensorBinaryOperation(left, right, plus<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1673  }  }
1674    
1675  //  //
# Line 1763  escript::operator+(const Data& left, con Line 1677  escript::operator+(const Data& left, con
1677  Data  Data
1678  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
1679  {  {
1680    Data result;    return C_TensorBinaryOperation(left, right, minus<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
1681  }  }
1682    
1683  //  //
# Line 1776  escript::operator-(const Data& left, con Line 1685  escript::operator-(const Data& left, con
1685  Data  Data
1686  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
1687  {  {
1688    Data result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
1689  }  }
1690    
1691  //  //
# Line 1789  escript::operator*(const Data& left, con Line 1693  escript::operator*(const Data& left, con
1693  Data  Data
1694  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
1695  {  {
1696    Data result;    return C_TensorBinaryOperation(left, right, divides<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
1697  }  }
1698    
1699  //  //
# Line 1802  escript::operator/(const Data& left, con Line 1701  escript::operator/(const Data& left, con
1701  Data  Data
1702  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
1703  {  {
1704    //    return left+Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1705  }  }
1706    
1707  //  //
# Line 1818  escript::operator+(const Data& left, con Line 1709  escript::operator+(const Data& left, con
1709  Data  Data
1710  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
1711  {  {
1712    //    return left-Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
1713  }  }
1714    
1715  //  //
# Line 1834  escript::operator-(const Data& left, con Line 1717  escript::operator-(const Data& left, con
1717  Data  Data
1718  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
1719  {  {
1720    //    return left*Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
1721  }  }
1722    
1723  //  //
# Line 1850  escript::operator*(const Data& left, con Line 1725  escript::operator*(const Data& left, con
1725  Data  Data
1726  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
1727  {  {
1728    //    return left/Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
1729  }  }
1730    
1731  //  //
# Line 1866  escript::operator/(const Data& left, con Line 1733  escript::operator/(const Data& left, con
1733  Data  Data
1734  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
1735  {  {
1736    //    return Data(left,right.getFunctionSpace(),false)+right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result+=right;  
   return result;  
1737  }  }
1738    
1739  //  //
# Line 1879  escript::operator+(const boost::python:: Line 1741  escript::operator+(const boost::python::
1741  Data  Data
1742  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
1743  {  {
1744    //    return Data(left,right.getFunctionSpace(),false)-right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result-=right;  
   return result;  
1745  }  }
1746    
1747  //  //
# Line 1892  escript::operator-(const boost::python:: Line 1749  escript::operator-(const boost::python::
1749  Data  Data
1750  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
1751  {  {
1752    //    return Data(left,right.getFunctionSpace(),false)*right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result*=right;  
   return result;  
1753  }  }
1754    
1755  //  //
# Line 1905  escript::operator*(const boost::python:: Line 1757  escript::operator*(const boost::python::
1757  Data  Data
1758  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
1759  {  {
1760    //    return Data(left,right.getFunctionSpace(),false)/right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result/=right;  
   return result;  
1761  }  }
1762    
1763  //  //
# Line 1961  escript::operator/(const boost::python:: Line 1808  escript::operator/(const boost::python::
1808  /* TODO */  /* TODO */
1809  /* global reduction */  /* global reduction */
1810  Data  Data
1811  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1812  {  {
1813    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1814    
1815    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1816    
1817    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1818      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1819    }    }
1820    
# Line 1977  Data::getItem(const boost::python::objec Line 1824  Data::getItem(const boost::python::objec
1824  /* TODO */  /* TODO */
1825  /* global reduction */  /* global reduction */
1826  Data  Data
1827  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
1828  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1829    return Data(*this,region);    return Data(*this,region);
1830  }  }
1831    
# Line 1995  Data::setItemO(const boost::python::obje Line 1839  Data::setItemO(const boost::python::obje
1839    setItemD(key,tempData);    setItemD(key,tempData);
1840  }  }
1841    
 /* TODO */  
 /* global reduction */  
1842  void  void
1843  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
1844                 const Data& value)                 const Data& value)
1845  {  {
1846    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1847    
1848    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1849    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1850      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1851    }    }
1852    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 2014  Data::setItemD(const boost::python::obje Line 1856  Data::setItemD(const boost::python::obje
1856    }    }
1857  }  }
1858    
 /* TODO */  
 /* global reduction */  
1859  void  void
1860  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1861                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
1862  {  {
1863    if (isProtected()) {    if (isProtected()) {
1864          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1865    }    }
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1866    Data tempValue(value);    Data tempValue(value);
1867    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
1868    typeMatchRight(tempValue);    typeMatchRight(tempValue);
# Line 2060  Data::typeMatchRight(const Data& right) Line 1897  Data::typeMatchRight(const Data& right)
1897    }    }
1898  }  }
1899    
1900  /* TODO */  void
1901  /* global reduction */  Data::setTaggedValueByName(std::string name,
1902                               const boost::python::object& value)
1903    {
1904         if (getFunctionSpace().getDomain().isValidTagName(name)) {
1905            int tagKey=getFunctionSpace().getDomain().getTag(name);
1906            setTaggedValue(tagKey,value);
1907         }
1908    }
1909  void  void
1910  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
1911                       const boost::python::object& value)                       const boost::python::object& value)
# Line 2071  Data::setTaggedValue(int tagKey, Line 1915  Data::setTaggedValue(int tagKey,
1915    }    }
1916    //    //
1917    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1918    tag();    if (isConstant()) tag();
1919    
1920    if (!isTagged()) {    numeric::array asNumArray(value);
1921      throw DataException("Error - DataTagged conversion failed!!");  
1922    
1923      // extract the shape of the numarray
1924      DataTypes::ShapeType tempShape;
1925      for (int i=0; i < asNumArray.getrank(); i++) {
1926        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
1927    }    }
1928    
1929    //    // get the space for the data vector
1930    // Construct DataArray from boost::python::object input value  //   int len = DataTypes::noValues(tempShape);
1931    DataArray valueDataArray(value);  //   DataVector temp_data(len, 0.0, len);
1932    //   DataArrayView temp_dataView(temp_data, tempShape);
1933    //   temp_dataView.copy(asNumArray);
1934    
1935      DataVector temp_data2;
1936      temp_data2.copyFromNumArray(asNumArray);
1937    
1938    //    //
1939    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1940    m_data->setTaggedValue(tagKey,valueDataArray.getView());    //m_data->setTaggedValue(tagKey,temp_dataView);
1941    
1942        m_data->setTaggedValue(tagKey,tempShape, temp_data2);
1943  }  }
1944    
1945  /* TODO */  // void
1946  /* global reduction */  // Data::setTaggedValueFromCPP(int tagKey,
1947    //                             const DataArrayView& value)
1948    // {
1949    //   if (isProtected()) {
1950    //         throw DataException("Error - attempt to update protected Data object.");
1951    //   }
1952    //   //
1953    //   // Ensure underlying data object is of type DataTagged
1954    //   if (isConstant()) tag();
1955    //
1956    //   //
1957    //   // Call DataAbstract::setTaggedValue
1958    //   m_data->setTaggedValue(tagKey,value);
1959    // }
1960    
1961  void  void
1962  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
1963                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
1964                                const DataTypes::ValueType& value,
1965                    int dataOffset)
1966  {  {
1967    if (isProtected()) {    if (isProtected()) {
1968          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1969    }    }
1970    //    //
1971    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1972    tag();    if (isConstant()) tag();
1973    
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
1974    //    //
1975    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1976    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
1977  }  }
1978    
 /* TODO */  
 /* global reduction */  
1979  int  int
1980  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
1981  {  {
1982    return m_data->getTagNumber(dpno);    return getFunctionSpace().getTagFromDataPointNo(dpno);
1983  }  }
1984    
 /* TODO */  
 /* global reduction */  
 void  
 Data::setRefValue(int ref,  
                   const boost::python::numeric::array& value)  
 {  
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
   //  
   // Construct DataArray from boost::python::object input value  
   DataArray valueDataArray(value);  
   
   //  
   // Call DataAbstract::setRefValue  
   m_data->setRefValue(ref,valueDataArray);  
 }  
1985    
1986  /* TODO */  ostream& escript::operator<<(ostream& o, const Data& data)
 /* global reduction */  
 void  
 Data::getRefValue(int ref,  
                   boost::python::numeric::array& value)  
1987  {  {
1988    //    o << data.toString();
1989    // Construct DataArray for boost::python::object return value    return o;
1990    DataArray valueDataArray(value);  }
   
   //  
   // Load DataArray with values from data-points specified by ref  
   m_data->getRefValue(ref,valueDataArray);  
   
   //  
   // Load values from valueDataArray into return numarray  
   
   // extract the shape of the numarray  
   int rank = value.getrank();  
   DataArrayView::ShapeType shape;  
   for (int i=0; i < rank; i++) {  
     shape.push_back(extract<int>(value.getshape()[i]));  
   }  
   
   // and load the numarray with the data from the DataArray  
   DataArrayView valueView = valueDataArray.getView();  
1991    
1992    if (rank==0) {  Data
1993        boost::python::numeric::array temp_numArray(valueView());  escript::C_GeneralTensorProduct(Data& arg_0,
1994        value = temp_numArray;                       Data& arg_1,
1995    }                       int axis_offset,
1996    if (rank==1) {                       int transpose)
1997      for (int i=0; i < shape[0]; i++) {  {
1998        value[i] = valueView(i);    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
1999      // SM is the product of the last axis_offset entries in arg_0.getShape().
2000    
2001      // Interpolate if necessary and find an appropriate function space
2002      Data arg_0_Z, arg_1_Z;
2003      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2004        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2005          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2006          arg_1_Z = Data(arg_1);
2007        }
2008        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2009          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2010          arg_0_Z =Data(arg_0);
2011      }      }
2012    }      else {
2013    if (rank==2) {        throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         value[i][j] = valueView(i,j);  
       }  
2014      }      }
2015      } else {
2016          arg_0_Z = Data(arg_0);
2017          arg_1_Z = Data(arg_1);
2018    }    }
2019    if (rank==3) {    // Get rank and shape of inputs
2020      for (int i=0; i < shape[0]; i++) {    int rank0 = arg_0_Z.getDataPointRank();
2021        for (int j=0; j < shape[1]; j++) {    int rank1 = arg_1_Z.getDataPointRank();
2022          for (int k=0; k < shape[2]; k++) {    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2023            value[i][j][k] = valueView(i,j,k);    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2024          }  
2025        }    // Prepare for the loops of the product and verify compatibility of shapes
2026      int start0=0, start1=0;
2027      if (transpose == 0)       {}
2028      else if (transpose == 1)  { start0 = axis_offset; }
2029      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2030      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2031    
2032    
2033      // Adjust the shapes for transpose
2034      DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2035      DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2036      for (int i=0; i<rank0; i++)   { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2037      for (int i=0; i<rank1; i++)   { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2038    
2039    #if 0
2040      // For debugging: show shape after transpose
2041      char tmp[100];
2042      std::string shapeStr;
2043      shapeStr = "(";
2044      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2045      shapeStr += ")";
2046      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2047      shapeStr = "(";
2048      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2049      shapeStr += ")";
2050      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2051    #endif
2052    
2053      // Prepare for the loops of the product
2054      int SL=1, SM=1, SR=1;
2055      for (int i=0; i<rank0-axis_offset; i++)   {
2056        SL *= tmpShape0[i];
2057      }
2058      for (int i=rank0-axis_offset; i<rank0; i++)   {
2059        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2060          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2061        }
2062        SM *= tmpShape0[i];
2063      }
2064      for (int i=axis_offset; i<rank1; i++)     {
2065        SR *= tmpShape1[i];
2066      }
2067    
2068      // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2069      DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2070      {         // block to limit the scope of out_index
2071         int out_index=0;
2072         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2073         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2074      }
2075    
2076      // Declare output Data object
2077      Data res;
2078    
2079      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2080        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2081        double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2082        double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2083        double *ptr_2 = &(res.getDataAtOffset(0));
2084        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2085      }
2086      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2087    
2088        // Prepare the DataConstant input
2089        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2090        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2091    
2092        // Borrow DataTagged input from Data object
2093        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2094        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2095    
2096        // Prepare a DataTagged output 2
2097        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2098        res.tag();
2099        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2100        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2101    
2102        // Prepare offset into DataConstant
2103        int offset_0 = tmp_0->getPointOffset(0,0);
2104        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2105        // Get the views
2106    //     DataArrayView view_1 = tmp_1->getDefaultValue();
2107    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2108    //     // Get the pointers to the actual data
2109    //     double *ptr_1 = &((view_1.getData())[0]);
2110    //     double *ptr_2 = &((view_2.getData())[0]);
2111    
2112        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2113        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2114    
2115    
2116        // Compute an MVP for the default
2117        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2118        // Compute an MVP for each tag
2119        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2120        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2121        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2122          tmp_2->addTag(i->first);
2123    //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2124    //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2125    //       double *ptr_1 = &view_1.getData(0);
2126    //       double *ptr_2 = &view_2.getData(0);
2127    
2128          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2129          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2130        
2131          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2132      }      }
2133    
2134    }    }
2135    if (rank==4) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2136      for (int i=0; i < shape[0]; i++) {  
2137        for (int j=0; j < shape[1]; j++) {      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2138          for (int k=0; k < shape[2]; k++) {      DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2139            for (int l=0; l < shape[3]; l++) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2140              value[i][j][k][l] = valueView(i,j,k,l);      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2141            }      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2142          }      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2143        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2144        int sampleNo_1,dataPointNo_1;
2145        int numSamples_1 = arg_1_Z.getNumSamples();
2146        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2147        int offset_0 = tmp_0->getPointOffset(0,0);
2148        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2149        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2150          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2151            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2152            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2153            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2154            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2155            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2156            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2157        }        }
2158      }      }
2159    
2160    }    }
2161      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2162    
2163  }      // Borrow DataTagged input from Data object
2164        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2165        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2166    
2167  void      // Prepare the DataConstant input
2168  Data::archiveData(const std::string fileName)      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2169  {      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
   cout << "Archiving Data object to: " << fileName << endl;  
2170    
2171    //      // Prepare a DataTagged output 2
2172    // Determine type of this Data object      res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2173    int dataType = -1;      res.tag();
2174        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2175        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2176    
2177    if (isEmpty()) {      // Prepare offset into DataConstant
2178      dataType = 0;      int offset_1 = tmp_1->getPointOffset(0,0);
2179      cout << "\tdataType: DataEmpty" << endl;      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2180    }      // Get the views
2181    if (isConstant()) {  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2182      dataType = 1;  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2183      cout << "\tdataType: DataConstant" << endl;  //     // Get the pointers to the actual data
2184    }  //     double *ptr_0 = &((view_0.getData())[0]);
2185    if (isTagged()) {  //     double *ptr_2 = &((view_2.getData())[0]);
     dataType = 2;  
     cout << "\tdataType: DataTagged" << endl;  
   }  
   if (isExpanded()) {  
     dataType = 3;  
     cout << "\tdataType: DataExpanded" << endl;  
   }  
2186    
2187    if (dataType == -1) {      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2188      throw DataException("archiveData Error: undefined dataType");      double *ptr_2 = &(tmp_2->getDefaultValue(0));
   }  
2189    
2190    //      // Compute an MVP for the default
2191    // Collect data items common to all Data types      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2192    int noSamples = getNumSamples();      // Compute an MVP for each tag
2193    int noDPPSample = getNumDataPointsPerSample();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2194    int functionSpaceType = getFunctionSpace().getTypeCode();      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2195    int dataPointRank = getDataPointRank();      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2196    int dataPointSize = getDataPointSize();  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2197    int dataLength = getLength();  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2198    DataArrayView::ShapeType dataPointShape = getDataPointShape();  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2199    vector<int> referenceNumbers(noSamples);  //       double *ptr_0 = &view_0.getData(0);
2200    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  //       double *ptr_2 = &view_2.getData(0);
2201      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);  
2202    }        tmp_2->addTag(i->first);
2203    vector<int> tagNumbers(noSamples);        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2204    if (isTagged()) {        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2205      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
       tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);  
2206      }      }
2207    
2208    }    }
2209      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2210    
2211    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;      // Borrow DataTagged input from Data object
2212    cout << "\tfunctionSpaceType: " << functionSpaceType << endl;      DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2213    cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2214    
2215    //      // Borrow DataTagged input from Data object
2216    // Flatten Shape to an array of integers suitable for writing to file      DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2217    int flatShape[4] = {0,0,0,0};      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
   cout << "\tshape: < ";  
   for (int dim=0; dim<dataPointRank; dim++) {  
     flatShape[dim] = dataPointShape[dim];  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
2218    
2219    //      // Prepare a DataTagged output 2
2220    // Open archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2221    ofstream archiveFile;      res.tag();  // DataTagged output
2222    archiveFile.open(fileName.data(), ios::out);      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2223        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2224    
2225    if (!archiveFile.good()) {  //     // Get the views
2226      throw DataException("archiveData Error: problem opening archive file");  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2227    }  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2228    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2229    //     // Get the pointers to the actual data
2230    //     double *ptr_0 = &((view_0.getData())[0]);
2231    //     double *ptr_1 = &((view_1.getData())[0]);
2232    //     double *ptr_2 = &((view_2.getData())[0]);
2233    
2234    //      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2235    // Write common data items to archive file      double *ptr_1 = &(tmp_1->getDefaultValue(0));
2236    archiveFile.write(reinterpret_cast<char *>(&dataType),sizeof(int));      double *ptr_2 = &(tmp_2->getDefaultValue(0));
   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));  
     }  
   }  
2237    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing to archive file");  
   }  
2238    
2239    //      // Compute an MVP for the default
2240    // Archive underlying data values for each Data type      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2241    int noValues;      // Merge the tags
2242    switch (dataType) {      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2243      case 0:      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2244        // DataEmpty      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2245        noValues = 0;      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2246        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));        tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2247        cout << "\tnoValues: " << noValues << endl;      }
2248        break;      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2249      case 1:        tmp_2->addTag(i->first);
2250        // DataConstant      }
2251        noValues = m_data->getLength();      // Compute an MVP for each tag
2252        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2253        cout << "\tnoValues: " << noValues << endl;      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2254        if (m_data->archiveData(archiveFile,noValues)) {  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2255          throw DataException("archiveData Error: problem writing data to archive file");  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2256        }  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2257        break;  //       double *ptr_0 = &view_0.getData(0);
2258      case 2:  //       double *ptr_1 = &view_1.getData(0);
2259        // DataTagged  //       double *ptr_2 = &view_2.getData(0);
       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;  
   }  
2260    
2261    if (!archiveFile.good()) {        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2262      throw DataException("archiveData Error: problem writing data to archive file");        double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2263    }        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2264    
2265    //        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2266    // Close archive file      }
   archiveFile.close();  
2267    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem closing archive file");  
2268    }    }
2269      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2270    
2271  }      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2272        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2273        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2274        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2275        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2276        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2277        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2278        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2279        int sampleNo_0,dataPointNo_0;
2280        int numSamples_0 = arg_0_Z.getNumSamples();
2281        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2282        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2283        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2284          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2285          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2286          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2287            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2288            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2289            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2290            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2291            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2292          }
2293        }
2294    
 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");  
2295    }    }
2296      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2297    
2298    cout << "Extracting Data object from: " << fileName << endl;      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2299        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2300    int dataType;      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2301    int noSamples;      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2302    int noDPPSample;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2303    int functionSpaceType;      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2304    int dataPointRank;      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2305    int dataPointSize;      int sampleNo_0,dataPointNo_0;
2306    int dataLength;      int numSamples_0 = arg_0_Z.getNumSamples();
2307    DataArrayView::ShapeType dataPointShape;      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2308    int flatShape[4];      int offset_1 = tmp_1->getPointOffset(0,0);
2309        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2310        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2311          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2312            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2313            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2314            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2315            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2316            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2317            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2318          }
2319        }
2320    
   //  
   // Open the archive file  
   ifstream archiveFile;  
   archiveFile.open(fileName.data(), ios::in);  
2321    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem opening archive file");  
2322    }    }
2323      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2324    
2325    //      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2326    // Read common data items from archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2327    archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2328    archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));      DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2329    archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2330    archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2331    archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2332    archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2333    archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));      int sampleNo_0,dataPointNo_0;
2334    for (int dim = 0; dim < 4; dim++) {      int numSamples_0 = arg_0_Z.getNumSamples();
2335      archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2336      if (flatShape[dim]>0) {      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2337        dataPointShape.push_back(flatShape[dim]);      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2338          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2339          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2340          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2341            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2342            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2343            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2344            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2345            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2346          }
2347      }      }
2348    
2349    }    }
2350    vector<int> referenceNumbers(noSamples);    else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2351    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
2352      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2353    }      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2354    vector<int> tagNumbers(noSamples);      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2355    if (dataType==2) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2356      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2357        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2358        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2359        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2360        int sampleNo_0,dataPointNo_0;
2361        int numSamples_0 = arg_0_Z.getNumSamples();
2362        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2363        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2364        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2365          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2366            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2367            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2368            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2369            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2370            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2371            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2372            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2373          }
2374      }      }
   }  
2375    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
2376    }    }
2377      else {
2378    //      throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
   // 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;  
2379    }    }
2380    
2381    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;    return res;
2382    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;  
2383    
2384    //  DataAbstract*
2385    // Verify that supplied FunctionSpace object is compatible with this Data object.  Data::borrowData() const
2386    if ( (fspace.getTypeCode()!=functionSpaceType) ||  {
2387         (fspace.getNumSamples()!=noSamples) ||    return m_data.get();
2388         (fspace.getNumDPPSample()!=noDPPSample)  }
2389       ) {  
2390      throw DataException("extractData Error: incompatible FunctionSpace");  
2391    }  std::string
2392    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  Data::toString() const
2393      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {  {
2394        throw DataException("extractData Error: incompatible FunctionSpace");      static const DataTypes::ValueType::size_type TOO_MANY_POINTS=80;
2395      }      if (getNumDataPoints()*getDataPointSize()>TOO_MANY_POINTS)
2396    }      {
2397    if (dataType==2) {      stringstream temp;
2398      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      temp << "Summary: inf="<< inf() << " sup=" << sup() << " data points=" << getNumDataPoints();
2399        if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {      return  temp.str();
         throw DataException("extractData Error: incompatible FunctionSpace");  
       }  
2400      }      }
2401    }      return m_data->toString();
2402    }
2403    
   //  
   // Construct a DataVector to hold underlying data values  
   DataVector dataVec(dataLength);  
2404    
   //  
   // 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;  
   }  
2405    
2406    if (!archiveFile.good()) {  DataTypes::ValueType::const_reference
2407      throw DataException("extractData Error: problem reading from archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2408    }  {
2409        return m_data->getDataAtOffset(i);
2410    }
2411    
   //  
   // Close archive file  
   archiveFile.close();  
2412    
2413    if (!archiveFile.good()) {  DataTypes::ValueType::reference
2414      throw DataException("extractData Error: problem closing archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2415    }  {
2416        return m_data->getDataAtOffset(i);
2417    }
2418    
2419    //  DataTypes::ValueType::const_reference
2420    // Construct an appropriate Data object  Data::getDataPoint(int sampleNo, int dataPointNo) const
2421    DataAbstract* tempData;  {
2422    switch (dataType) {      return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
     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;  
2423  }  }
2424    
2425  ostream& escript::operator<<(ostream& o, const Data& data)  
2426    DataTypes::ValueType::reference
2427    Data::getDataPoint(int sampleNo, int dataPointNo)
2428  {  {
2429    o << data.toString();      return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
   return o;  
2430  }  }
2431    
2432    
2433  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2434    
2435  void  void
2436  Data::print()  Data::print()
2437  {  {
2438    int i,j;    int i,j;
2439      
2440    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2441    for( i=0; i<getNumSamples(); i++ )    for( i=0; i<getNumSamples(); i++ )
2442    {    {
# Line 2557  Data::print() Line 2446  Data::print()
2446      printf( "\n" );      printf( "\n" );
2447    }    }
2448  }  }
2449    void
2450    Data::dump(const std::string fileName) const
2451    {
2452      try
2453         {
2454            return m_data->dump(fileName);
2455         }
2456         catch (exception& e)
2457         {
2458            cout << e.what() << endl;
2459         }
2460    }
2461    
2462  int  int
2463  Data::get_MPISize() const  Data::get_MPISize() const
2464  {  {
2465      int error, size;      int size;
2466  #ifdef PASO_MPI  #ifdef PASO_MPI
2467        int error;
2468      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
2469  #else  #else
2470      size = 1;      size = 1;
# Line 2573  Data::get_MPISize() const Line 2475  Data::get_MPISize() const
2475  int  int
2476  Data::get_MPIRank() const  Data::get_MPIRank() const
2477  {  {
2478      int error, rank;      int rank;
2479  #ifdef PASO_MPI  #ifdef PASO_MPI
2480        int error;
2481      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
2482  #else  #else
2483      rank = 0;      rank = 0;
# Line 2584  Data::get_MPIRank() const Line 2487  Data::get_MPIRank() const
2487    
2488  MPI_Comm  MPI_Comm
2489  Data::get_MPIComm() const  Data::get_MPIComm() const
2490  {  {
2491  #ifdef PASO_MPI  #ifdef PASO_MPI
2492      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
2493  #else  #else
# Line 2592  Data::get_MPIComm() const Line 2495  Data::get_MPIComm() const
2495  #endif  #endif
2496  }  }
2497    
2498    

Legend:
Removed from v.790  
changed lines
  Added in v.1799

  ViewVC Help
Powered by ViewVC 1.1.26