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

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

  ViewVC Help
Powered by ViewVC 1.1.26