/[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 1118 by gross, Tue Apr 24 08:55:04 2007 UTC revision 1878 by gross, Tue Oct 14 03:39:13 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 43  Data::Data() Line 46  Data::Data()
46    //    //
47    // Default data is type DataEmpty    // Default data is type DataEmpty
48    DataAbstract* temp=new DataEmpty();    DataAbstract* temp=new DataEmpty();
49    shared_ptr<DataAbstract> temp_data(temp);    m_data=temp->getPtr();
   m_data=temp_data;  
50    m_protected=false;    m_protected=false;
51  }  }
52    
# Line 53  Data::Data(double value, Line 55  Data::Data(double value,
55             const FunctionSpace& what,             const FunctionSpace& what,
56             bool expanded)             bool expanded)
57  {  {
58    DataArrayView::ShapeType dataPointShape;    DataTypes::ShapeType dataPointShape;
59    for (int i = 0; i < shape.attr("__len__")(); ++i) {    for (int i = 0; i < shape.attr("__len__")(); ++i) {
60      dataPointShape.push_back(extract<const int>(shape[i]));      dataPointShape.push_back(extract<const int>(shape[i]));
61    }    }
62    DataArray temp(dataPointShape,value);  
63    initialise(temp.getView(),what,expanded);    int len = DataTypes::noValues(dataPointShape);
64      DataVector temp_data(len,value,len);
65      initialise(temp_data, dataPointShape, what, expanded);
66    m_protected=false;    m_protected=false;
67  }  }
68    
69  Data::Data(double value,  Data::Data(double value,
70         const DataArrayView::ShapeType& dataPointShape,         const DataTypes::ShapeType& dataPointShape,
71         const FunctionSpace& what,         const FunctionSpace& what,
72             bool expanded)             bool expanded)
73  {  {
74    DataArray temp(dataPointShape,value);    int len = DataTypes::noValues(dataPointShape);
75    pair<int,int> dataShape=what.getDataShape();  
76    initialise(temp.getView(),what,expanded);    DataVector temp_data(len,value,len);
77    //   DataArrayView temp_dataView(temp_data, dataPointShape);
78    
79    //   initialise(temp_dataView, what, expanded);
80      initialise(temp_data, dataPointShape, what, expanded);
81    
82    m_protected=false;    m_protected=false;
83  }  }
84    
# Line 79  Data::Data(const Data& inData) Line 88  Data::Data(const Data& inData)
88    m_protected=inData.isProtected();    m_protected=inData.isProtected();
89  }  }
90    
91    
92  Data::Data(const Data& inData,  Data::Data(const Data& inData,
93             const DataArrayView::RegionType& region)             const DataTypes::RegionType& region)
94  {  {
95    //    //
96    // Create Data which is a slice of another Data    // Create Data which is a slice of another Data
97    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = inData.m_data->getSlice(region);
98    shared_ptr<DataAbstract> temp_data(tmp);    m_data=DataAbstract_ptr(tmp);
   m_data=temp_data;  
99    m_protected=false;    m_protected=false;
100  }  }
101    
102  Data::Data(const Data& inData,  Data::Data(const Data& inData,
103             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
104  {  {
105      if (inData.isEmpty())
106      {
107        throw DataException("Error - will not interpolate for instances of DataEmpty.");
108      }
109    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
110      m_data=inData.m_data;      m_data=inData.m_data;
111      } else if (inData.isConstant()) { // for a constant function, we just need to use the new function space
112        if (!inData.probeInterpolation(functionspace))
113        {           // Even though this is constant, we still need to check whether interpolation is allowed
114        throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
115        }
116        DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),inData.m_data->getVector());  
117        m_data=DataAbstract_ptr(dc);
118    } else {    } else {
119      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);      Data tmp(0,inData.getDataPointShape(),functionspace,true);
120      // Note: Must use a reference or pointer to a derived object      // Note: Must use a reference or pointer to a derived object
121      // in order to get polymorphic behaviour. Shouldn't really      // in order to get polymorphic behaviour. Shouldn't really
122      // be able to create an instance of AbstractDomain but that was done      // be able to create an instance of AbstractDomain but that was done
123      // as a boost:python work around which may no longer be required.      // as a boost:python work around which may no longer be required.
124      const AbstractDomain& inDataDomain=inData.getDomain();      /*const AbstractDomain& inDataDomain=inData.getDomain();*/
125        const_Domain_ptr inDataDomain=inData.getDomain();
126      if  (inDataDomain==functionspace.getDomain()) {      if  (inDataDomain==functionspace.getDomain()) {
127        inDataDomain.interpolateOnDomain(tmp,inData);        inDataDomain->interpolateOnDomain(tmp,inData);
128      } else {      } else {
129        inDataDomain.interpolateACross(tmp,inData);        inDataDomain->interpolateACross(tmp,inData);
130      }      }
131      m_data=tmp.m_data;      m_data=tmp.m_data;
132    }    }
133    m_protected=false;    m_protected=false;
134  }  }
135    
136  Data::Data(const DataTagged::TagListType& tagKeys,  Data::Data(DataAbstract* underlyingdata)
            const DataTagged::ValueListType & values,  
            const DataArrayView& defaultValue,  
            const FunctionSpace& what,  
            bool expanded)  
137  {  {
138    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);  //  m_data=shared_ptr<DataAbstract>(underlyingdata);
139    shared_ptr<DataAbstract> temp_data(temp);      m_data=underlyingdata->getPtr();
140    m_data=temp_data;      m_protected=false;
   m_protected=false;  
   if (expanded) {  
     expand();  
   }  
141  }  }
142    
143  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 134  Data::Data(const numeric::array& value, Line 147  Data::Data(const numeric::array& value,
147    initialise(value,what,expanded);    initialise(value,what,expanded);
148    m_protected=false;    m_protected=false;
149  }  }
150    /*
151  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
152         const FunctionSpace& what,         const FunctionSpace& what,
153             bool expanded)             bool expanded)
154  {  {
155    initialise(value,what,expanded);    initialise(value,what,expanded);
156    m_protected=false;    m_protected=false;
157    }*/
158    
159    Data::Data(const DataTypes::ValueType& value,
160             const DataTypes::ShapeType& shape,
161                     const FunctionSpace& what,
162                     bool expanded)
163    {
164       initialise(value,shape,what,expanded);
165       m_protected=false;
166  }  }
167    
168    
169  Data::Data(const object& value,  Data::Data(const object& value,
170         const FunctionSpace& what,         const FunctionSpace& what,
171             bool expanded)             bool expanded)
# Line 152  Data::Data(const object& value, Line 175  Data::Data(const object& value,
175    m_protected=false;    m_protected=false;
176  }  }
177    
178    
179  Data::Data(const object& value,  Data::Data(const object& value,
180             const Data& other)             const Data& other)
181  {  {
182      numeric::array asNumArray(value);
183    
184      // extract the shape of the numarray
185      DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
186    // /*  for (int i=0; i < asNumArray.getrank(); i++) {
187    //     tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
188    //   }*/
189    //   // get the space for the data vector
190    //   int len = DataTypes::noValues(tempShape);
191    //   DataVector temp_data(len, 0.0, len);
192    // /*  DataArrayView temp_dataView(temp_data, tempShape);
193    //   temp_dataView.copy(asNumArray);*/
194    //   temp_data.copyFromNumArray(asNumArray);
195    
196    //    //
197    // Create DataConstant using the given value and all other parameters    // Create DataConstant using the given value and all other parameters
198    // copied from other. If value is a rank 0 object this Data    // copied from other. If value is a rank 0 object this Data
199    // will assume the point data shape of other.    // will assume the point data shape of other.
200    DataArray temp(value);  
201    if (temp.getView().getRank()==0) {    if (DataTypes::getRank(tempShape)/*temp_dataView.getRank()*/==0) {
202      //  
203      // Create a DataArray with the scalar value for all elements  
204      DataArray temp2(other.getPointDataView().getShape(),temp.getView()());      // get the space for the data vector
205      initialise(temp2.getView(),other.getFunctionSpace(),false);      int len1 = DataTypes::noValues(tempShape);
206        DataVector temp_data(len1, 0.0, len1);
207        temp_data.copyFromNumArray(asNumArray);
208    
209        int len = DataTypes::noValues(other.getDataPointShape());
210    
211        DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
212        //DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
213    //     initialise(temp2_dataView, other.getFunctionSpace(), false);
214    
215        DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
216    //     boost::shared_ptr<DataAbstract> sp(t);
217    //     m_data=sp;
218        m_data=DataAbstract_ptr(t);
219    
220    } else {    } else {
221      //      //
222      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
223      initialise(temp.getView(),other.getFunctionSpace(),false);  //     initialise(temp_dataView, other.getFunctionSpace(), false);
224        DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
225    //     boost::shared_ptr<DataAbstract> sp(t);
226    //     m_data=sp;
227        m_data=DataAbstract_ptr(t);
228    }    }
229    m_protected=false;    m_protected=false;
230  }  }
# Line 178  Data::~Data() Line 234  Data::~Data()
234    
235  }  }
236    
237    
238    
239    void
240    Data::initialise(const boost::python::numeric::array& value,
241                     const FunctionSpace& what,
242                     bool expanded)
243    {
244      //
245      // Construct a Data object of the appropriate type.
246      // Construct the object first as there seems to be a bug which causes
247      // undefined behaviour if an exception is thrown during construction
248      // within the shared_ptr constructor.
249      if (expanded) {
250        DataAbstract* temp=new DataExpanded(value, what);
251    //     boost::shared_ptr<DataAbstract> temp_data(temp);
252    //     m_data=temp_data;
253        m_data=temp->getPtr();
254      } else {
255        DataAbstract* temp=new DataConstant(value, what);
256    //     boost::shared_ptr<DataAbstract> temp_data(temp);
257    //     m_data=temp_data;
258        m_data=temp->getPtr();
259      }
260    }
261    
262    
263    void
264    Data::initialise(const DataTypes::ValueType& value,
265             const DataTypes::ShapeType& shape,
266                     const FunctionSpace& what,
267                     bool expanded)
268    {
269      //
270      // Construct a Data object of the appropriate type.
271      // Construct the object first as there seems to be a bug which causes
272      // undefined behaviour if an exception is thrown during construction
273      // within the shared_ptr constructor.
274      if (expanded) {
275        DataAbstract* temp=new DataExpanded(what, shape, value);
276    //     boost::shared_ptr<DataAbstract> temp_data(temp);
277    //     m_data=temp_data;
278        m_data=temp->getPtr();
279      } else {
280        DataAbstract* temp=new DataConstant(what, shape, value);
281    //     boost::shared_ptr<DataAbstract> temp_data(temp);
282    //     m_data=temp_data;
283        m_data=temp->getPtr();
284      }
285    }
286    
287    
288    // void
289    // Data::CompareDebug(const Data& rd)
290    // {
291    //  using namespace std;
292    //  bool mismatch=false;
293    //  std::cout << "Comparing left and right" << endl;
294    //  const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
295    //  const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
296    //  
297    //  if (left==0)
298    //  {
299    //      cout << "left arg is not a DataTagged\n";
300    //      return;
301    //  }
302    //  
303    //  if (right==0)
304    //  {
305    //      cout << "right arg is not a DataTagged\n";
306    //      return;
307    //  }
308    //  cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
309    //  cout << "Shapes ";
310    //  if (left->getShape()==right->getShape())
311    //  {
312    //      cout << "ok\n";
313    //  }
314    //  else
315    //  {
316    //      cout << "Problem: shapes do not match\n";
317    //      mismatch=true;
318    //  }
319    //  int lim=left->getVector().size();
320    //  if (right->getVector().size()) lim=right->getVector().size();
321    //  for (int i=0;i<lim;++i)
322    //  {
323    //      if (left->getVector()[i]!=right->getVector()[i])
324    //      {
325    //          cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
326    //          mismatch=true;
327    //      }
328    //  }
329    //
330    //  // still need to check the tag map
331    //  // also need to watch what is happening to function spaces, are they copied or what?
332    //
333    //  const DataTagged::DataMapType& mapleft=left->getTagLookup();
334    //  const DataTagged::DataMapType& mapright=right->getTagLookup();
335    //
336    //  if (mapleft.size()!=mapright.size())
337    //  {
338    //      cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
339    //      mismatch=true;
340    //      cout << "Left map\n";
341    //      DataTagged::DataMapType::const_iterator i,j;
342    //      for (i=mapleft.begin();i!=mapleft.end();++i) {
343    //          cout << "(" << i->first << "=>" << i->second << ")\n";
344    //      }
345    //      cout << "Right map\n";
346    //      for (i=mapright.begin();i!=mapright.end();++i) {
347    //          cout << "(" << i->first << "=>" << i->second << ")\n";
348    //      }
349    //      cout << "End map\n";
350    //
351    //  }
352    //
353    //  DataTagged::DataMapType::const_iterator i,j;
354    //  for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
355    //     if ((i->first!=j->first) || (i->second!=j->second))
356    //     {
357    //      cout << "(" << i->first << "=>" << i->second << ")";
358    //      cout << ":(" << j->first << "=>" << j->second << ") ";
359    //      mismatch=true;
360    //            }
361    //  }
362    //  if (mismatch)
363    //  {
364    //      cout << "#Mismatch\n";
365    //  }
366    // }
367    
368  escriptDataC  escriptDataC
369  Data::getDataC()  Data::getDataC()
370  {  {
# Line 197  Data::getDataC() const Line 384  Data::getDataC() const
384  const boost::python::tuple  const boost::python::tuple
385  Data::getShapeTuple() const  Data::getShapeTuple() const
386  {  {
387    const DataArrayView::ShapeType& shape=getDataPointShape();    const DataTypes::ShapeType& shape=getDataPointShape();
388    switch(getDataPointRank()) {    switch(getDataPointRank()) {
389       case 0:       case 0:
390          return make_tuple();          return make_tuple();
# Line 213  Data::getShapeTuple() const Line 400  Data::getShapeTuple() const
400          throw DataException("Error - illegal Data rank.");          throw DataException("Error - illegal Data rank.");
401    }    }
402  }  }
403    
404    
405    // The different name is needed because boost has trouble with overloaded functions.
406    // It can't work out what type the function is based soley on its name.
407    // There are ways to fix this involving creating function pointer variables for each form
408    // but there doesn't seem to be a need given that the methods have the same name from the python point of view
409    Data*
410    Data::copySelf()
411    {
412       DataAbstract* temp=m_data->deepCopy();
413       return new Data(temp);
414    }
415    
416  void  void
417  Data::copy(const Data& other)  Data::copy(const Data& other)
418  {  {
419    //    DataAbstract* temp=other.m_data->deepCopy();
420    // Perform a deep copy    DataAbstract_ptr p=temp->getPtr();
421    {    m_data=p;
     DataExpanded* temp=dynamic_cast<DataExpanded*>(other.m_data.get());  
     if (temp!=0) {  
       //  
       // Construct a DataExpanded copy  
       DataAbstract* newData=new DataExpanded(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
   }  
   {  
     DataTagged* temp=dynamic_cast<DataTagged*>(other.m_data.get());  
     if (temp!=0) {  
       //  
       // Construct a DataTagged copy  
       DataAbstract* newData=new DataTagged(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
   }  
   {  
     DataConstant* temp=dynamic_cast<DataConstant*>(other.m_data.get());  
     if (temp!=0) {  
       //  
       // Construct a DataConstant copy  
       DataAbstract* newData=new DataConstant(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
   }  
   {  
     DataEmpty* temp=dynamic_cast<DataEmpty*>(other.m_data.get());  
     if (temp!=0) {  
       //  
       // Construct a DataEmpty copy  
       DataAbstract* newData=new DataEmpty();  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
   }  
   throw DataException("Error - Copy not implemented for this Data type.");  
422  }  }
423    
424    
425  void  void
426  Data::setToZero()  Data::setToZero()
427  {  {
428      if (isEmpty())
429      {
430         throw DataException("Error - Operations not permitted on instances of DataEmpty.");
431      }
432    {    {
433      DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());      DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
434      if (temp!=0) {      if (temp!=0) {
# Line 293  Data::setToZero() Line 453  Data::setToZero()
453    throw DataException("Error - Data can not be set to zero.");    throw DataException("Error - Data can not be set to zero.");
454  }  }
455    
456    // void
457    // Data::copyWithMask(const Data& other,
458    //                    const Data& mask)
459    // {
460    //   if (other.isEmpty() || mask.isEmpty())
461    //   {
462    //  throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");
463    //   }
464    //   Data mask1;
465    //   Data mask2;
466    //   mask1 = mask.wherePositive();
467    //
468    //   mask2.copy(mask1);
469    //   mask1 *= other;
470    //
471    //   mask2 *= *this;
472    //   mask2 = *this - mask2;
473    //   *this = mask1 + mask2;
474    // }
475    
476  void  void
477  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
478                     const Data& mask)                     const Data& mask)
479  {  {
480    Data mask1;    // 1. Interpolate if required so all Datas use the same FS as this
481    Data mask2;    // 2. Tag or Expand so that all Data's are the same type
482      // 3. Iterate over the data vectors copying values where mask is >0
483    mask1 = mask.wherePositive();    if (other.isEmpty() || mask.isEmpty())
484    mask2.copy(mask1);    {
485        throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");
486      }
487      Data other2(other);
488      Data mask2(mask);
489      FunctionSpace myFS=getFunctionSpace();
490      FunctionSpace oFS=other2.getFunctionSpace();
491      FunctionSpace mFS=mask2.getFunctionSpace();
492      if (oFS!=myFS)
493      {
494         if (other2.probeInterpolation(myFS))
495         {
496        other2=other2.interpolate(myFS);
497         }
498         else
499         {
500        throw DataException("Error - copyWithMask: other FunctionSpace is not compatible with this one.");
501         }
502      }
503      if (mFS!=myFS)
504      {
505         if (mask2.probeInterpolation(myFS))
506         {
507        mask2=mask2.interpolate(myFS);
508         }
509         else
510         {
511        throw DataException("Error - copyWithMask: mask FunctionSpace is not compatible with this one.");
512         }
513      }
514                // Ensure that all args have the same type
515      if (this->isExpanded() || mask2.isExpanded() || other2.isExpanded())
516      {
517        this->expand();
518        other2.expand();
519        mask2.expand();
520      }
521      else if (this->isTagged() || mask2.isTagged() || other2.isTagged())
522      {
523        this->tag();
524        other2.tag();
525        mask2.tag();
526      }
527      else if (this->isConstant() && mask2.isConstant() && other2.isConstant())
528      {
529      }
530      else
531      {
532        throw DataException("Error - Unknown DataAbstract passed to copyWithMask.");
533      }
534      // Now we iterate over the elements
535      DataVector& self=m_data->getVector();
536      const DataVector& ovec=other2.m_data->getVector();
537      const DataVector& mvec=mask2.m_data->getVector();
538      if ((self.size()!=ovec.size()) || (self.size()!=mvec.size()))
539      {
540        throw DataException("Error - size mismatch in arguments to copyWithMask.");
541      }
542      size_t num_points=self.size();
543      long i;
544      #pragma omp parallel for private(i) schedule(static)
545      for (i=0;i<num_points;++i)
546      {
547        if (mvec[i]>0)
548        {
549           self[i]=ovec[i];
550        }
551      }
552    }
553    
   mask1 *= other;  
   mask2 *= *this;  
   mask2 = *this - mask2;  
554    
   *this = mask1 + mask2;  
 }  
555    
556  bool  bool
557  Data::isExpanded() const  Data::isExpanded() const
# Line 339  Data::isConstant() const Line 582  Data::isConstant() const
582  }  }
583    
584  void  void
585  Data::setProtection()  Data::setProtection()
586  {  {
587     m_protected=true;     m_protected=true;
588  }  }
589    
590  bool  bool
591  Data::isProtected() const  Data::isProtected() const
592  {  {
593     return m_protected;     return m_protected;
594  }  }
595    
# Line 358  Data::expand() Line 601  Data::expand()
601    if (isConstant()) {    if (isConstant()) {
602      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
603      DataAbstract* temp=new DataExpanded(*tempDataConst);      DataAbstract* temp=new DataExpanded(*tempDataConst);
604      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
605      m_data=temp_data;  //     m_data=temp_data;
606        m_data=temp->getPtr();
607    } else if (isTagged()) {    } else if (isTagged()) {
608      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
609      DataAbstract* temp=new DataExpanded(*tempDataTag);      DataAbstract* temp=new DataExpanded(*tempDataTag);
610      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
611      m_data=temp_data;  //     m_data=temp_data;
612        m_data=temp->getPtr();
613    } else if (isExpanded()) {    } else if (isExpanded()) {
614      //      //
615      // do nothing      // do nothing
# Line 381  Data::tag() Line 626  Data::tag()
626    if (isConstant()) {    if (isConstant()) {
627      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
628      DataAbstract* temp=new DataTagged(*tempDataConst);      DataAbstract* temp=new DataTagged(*tempDataConst);
629      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
630      m_data=temp_data;  //     m_data=temp_data;
631        m_data=temp->getPtr();
632    } else if (isTagged()) {    } else if (isTagged()) {
633      // do nothing      // do nothing
634    } else if (isExpanded()) {    } else if (isExpanded()) {
# Line 397  Data::tag() Line 643  Data::tag()
643  Data  Data
644  Data::oneOver() const  Data::oneOver() const
645  {  {
646    return escript::unaryOp(*this,bind1st(divides<double>(),1.));    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
647  }  }
648    
649  Data  Data
650  Data::wherePositive() const  Data::wherePositive() const
651  {  {
652    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
653  }  }
654    
655  Data  Data
656  Data::whereNegative() const  Data::whereNegative() const
657  {  {
658    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
659  }  }
660    
661  Data  Data
662  Data::whereNonNegative() const  Data::whereNonNegative() const
663  {  {
664    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
665  }  }
666    
667  Data  Data
668  Data::whereNonPositive() const  Data::whereNonPositive() const
669  {  {
670    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
671  }  }
672    
673  Data  Data
674  Data::whereZero(double tol) const  Data::whereZero(double tol) const
675  {  {
676    Data dataAbs=abs();    Data dataAbs=abs();
677    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
678  }  }
679    
680  Data  Data
681  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
682  {  {
683    Data dataAbs=abs();    Data dataAbs=abs();
684    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
685  }  }
686    
687  Data  Data
# Line 450  Data::probeInterpolation(const FunctionS Line 696  Data::probeInterpolation(const FunctionS
696    if (getFunctionSpace()==functionspace) {    if (getFunctionSpace()==functionspace) {
697      return true;      return true;
698    } else {    } else {
699      const AbstractDomain& domain=getDomain();      const_Domain_ptr domain=getDomain();
700      if  (domain==functionspace.getDomain()) {      if  (*domain==*functionspace.getDomain()) {
701        return domain.probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());        return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
702      } else {      } else {
703        return domain.probeInterpolationACross(getFunctionSpace().getTypeCode(),functionspace.getDomain(),functionspace.getTypeCode());        return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());
704      }      }
705    }    }
706  }  }
# Line 462  Data::probeInterpolation(const FunctionS Line 708  Data::probeInterpolation(const FunctionS
708  Data  Data
709  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
710  {  {
711      if (isEmpty())
712      {
713        throw DataException("Error - operation not permitted on instances of DataEmpty.");
714      }
715      double blocktimer_start = blocktimer_time();
716    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
717      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
718    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
719    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
720    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
721    getDomain().setToGradient(out,*this);    getDomain()->setToGradient(out,*this);
722      blocktimer_increment("grad()", blocktimer_start);
723    return out;    return out;
724  }  }
725    
726  Data  Data
727  Data::grad() const  Data::grad() const
728  {  {
729    return gradOn(escript::function(getDomain()));    if (isEmpty())
730      {
731        throw DataException("Error - operation not permitted on instances of DataEmpty.");
732      }
733      return gradOn(escript::function(*getDomain()));
734  }  }
735    
736  int  int
737  Data::getDataPointSize() const  Data::getDataPointSize() const
738  {  {
739    return getPointDataView().noValues();    return m_data->getNoValues();
740  }  }
741    
742  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
743  Data::getLength() const  Data::getLength() const
744  {  {
745    return m_data->getLength();    return m_data->getLength();
746  }  }
747    
748  const DataArrayView::ShapeType&  const
 Data::getDataPointShape() const  
 {  
   return getPointDataView().getShape();  
 }  
   
   
   
 const  
749  boost::python::numeric::array  boost::python::numeric::array
750  Data:: getValueOfDataPoint(int dataPointNo)  Data:: getValueOfDataPoint(int dataPointNo)
751  {  {
752    size_t length=0;    size_t length=0;
753    int i, j, k, l;    int i, j, k, l;
754    //    //
755    // determine the rank and shape of each data point    // determine the rank and shape of each data point
756    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
757    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
758    
759    //    //
760    // create the numeric array to be returned    // create the numeric array to be returned
# Line 516  Data:: getValueOfDataPoint(int dataPoint Line 764  Data:: getValueOfDataPoint(int dataPoint
764    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
765    // as that of the data point    // as that of the data point
766    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
767    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
768    
769    //    //
770    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 544  Data:: getValueOfDataPoint(int dataPoint Line 792  Data:: getValueOfDataPoint(int dataPoint
792         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
793             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
794         }         }
795                  
796         //         //
797         // Check a valid data point number has been supplied         // Check a valid data point number has been supplied
798         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
# Line 552  Data:: getValueOfDataPoint(int dataPoint Line 800  Data:: getValueOfDataPoint(int dataPoint
800         }         }
801         // TODO: global error handling         // TODO: global error handling
802         // create a view of the data if it is stored locally         // create a view of the data if it is stored locally
803         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);  //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
804                   DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
805    
806    
807         switch( dataPointRank ){         switch( dataPointRank ){
808              case 0 :              case 0 :
809                  numArray[0] = dataPointView();                  numArray[0] = getDataAtOffset(offset);
810                  break;                  break;
811              case 1 :                      case 1 :
812                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
813                      numArray[i]=dataPointView(i);                      numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
814                  break;                  break;
815              case 2 :                      case 2 :
816                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
817                      for( j=0; j<dataPointShape[1]; j++)                      for( j=0; j<dataPointShape[1]; j++)
818                          numArray[make_tuple(i,j)]=dataPointView(i,j);                          numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
819                  break;                  break;
820              case 3 :                      case 3 :
821                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
822                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
823                          for( k=0; k<dataPointShape[2]; k++)                          for( k=0; k<dataPointShape[2]; k++)
824                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);                              numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
825                  break;                  break;
826              case 4 :              case 4 :
827                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
828                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
829                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
830                              for( l=0; l<dataPointShape[3]; l++)                              for( l=0; l<dataPointShape[3]; l++)
831                                  numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);                                  numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
832                  break;                  break;
833      }      }
834    }    }
# Line 587  Data:: getValueOfDataPoint(int dataPoint Line 837  Data:: getValueOfDataPoint(int dataPoint
837    return numArray;    return numArray;
838    
839  }  }
840    
841  void  void
842  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
843  {  {
844      // this will throw if the value cannot be represented      // this will throw if the value cannot be represented
845      boost::python::numeric::array num_array(py_object);      boost::python::numeric::array num_array(py_object);
846      setValueOfDataPointToArray(dataPointNo,num_array);      setValueOfDataPointToArray(dataPointNo,num_array);
   
   
847  }  }
848    
849  void  void
# Line 605  Data::setValueOfDataPointToArray(int dat Line 854  Data::setValueOfDataPointToArray(int dat
854    }    }
855    //    //
856    // check rank    // check rank
857    if (num_array.getrank()<getDataPointRank())    if (num_array.getrank()<getDataPointRank())
858        throw DataException("Rank of numarray does not match Data object rank");        throw DataException("Rank of numarray does not match Data object rank");
859    
860    //    //
# Line 616  Data::setValueOfDataPointToArray(int dat Line 865  Data::setValueOfDataPointToArray(int dat
865    }    }
866    //    //
867    // make sure data is expanded:    // make sure data is expanded:
868      //
869    if (!isExpanded()) {    if (!isExpanded()) {
870      expand();      expand();
871    }    }
# Line 648  Data::setValueOfDataPoint(int dataPointN Line 898  Data::setValueOfDataPoint(int dataPointN
898    }    }
899  }  }
900    
901  const  const
902  boost::python::numeric::array  boost::python::numeric::array
903  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
904  {  {
905    size_t length=0;    size_t length=0;
906    int i, j, k, l, pos;    int i, j, k, l, pos;
907    //    //
908    // determine the rank and shape of each data point    // determine the rank and shape of each data point
909    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
910    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
911    
912    //    //
913    // create the numeric array to be returned    // create the numeric array to be returned
# Line 667  Data::getValueOfGlobalDataPoint(int proc Line 917  Data::getValueOfGlobalDataPoint(int proc
917    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
918    // as that of the data point    // as that of the data point
919    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
920    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
921    
922    //    //
923    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 705  Data::getValueOfGlobalDataPoint(int proc Line 955  Data::getValueOfGlobalDataPoint(int proc
955                  if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {                  if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
956                    throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");                    throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
957                  }                  }
958                  
959                  //                  //
960                  // Check a valid data point number has been supplied                  // Check a valid data point number has been supplied
961                  if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {                  if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
# Line 713  Data::getValueOfGlobalDataPoint(int proc Line 963  Data::getValueOfGlobalDataPoint(int proc
963                  }                  }
964                  // TODO: global error handling                  // TODO: global error handling
965          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
966          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
967                    DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
968    
969          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
970          pos=0;          pos=0;
971          switch( dataPointRank ){          switch( dataPointRank ){
972              case 0 :              case 0 :
973                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
974                  break;                  break;
975              case 1 :                      case 1 :
976                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
977                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
978                  break;                  break;
979              case 2 :                      case 2 :
980                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
981                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
982                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
983                  break;                  break;
984              case 3 :                      case 3 :
985                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
986                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
987                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
988                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
989                  break;                  break;
990              case 4 :              case 4 :
991                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
992                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
993                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
994                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
995                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
996                  break;                  break;
997          }          }
998              }              }
999      }      }
1000          #ifdef PASO_MPI          #ifdef PASO_MPI
1001          // broadcast the data to all other processes          // broadcast the data to all other processes
1002      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
1003          #endif          #endif
# Line 756  Data::getValueOfGlobalDataPoint(int proc Line 1007  Data::getValueOfGlobalDataPoint(int proc
1007          case 0 :          case 0 :
1008              numArray[0]=tmpData[0];              numArray[0]=tmpData[0];
1009              break;              break;
1010          case 1 :                  case 1 :
1011              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1012                  numArray[i]=tmpData[i];                  numArray[i]=tmpData[i];
1013              break;              break;
1014          case 2 :                  case 2 :
1015              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1016                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1017                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
1018              break;              break;
1019          case 3 :                  case 3 :
1020              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1021                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1022                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
# Line 780  Data::getValueOfGlobalDataPoint(int proc Line 1031  Data::getValueOfGlobalDataPoint(int proc
1031              break;              break;
1032      }      }
1033    
1034      delete [] tmpData;        delete [] tmpData;
1035    //    //
1036    // return the loaded array    // return the loaded array
1037    return numArray;    return numArray;
# Line 793  Data::integrate() const Line 1044  Data::integrate() const
1044  {  {
1045    int index;    int index;
1046    int rank = getDataPointRank();    int rank = getDataPointRank();
1047    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
1048      int dataPointSize = getDataPointSize();
1049    
1050    //    //
1051    // calculate the integral values    // calculate the integral values
1052    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
1053    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    vector<double> integrals_local(dataPointSize);
1054    #ifdef PASO_MPI
1055      AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals_local,*this);
1056      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
1057      double *tmp = new double[dataPointSize];
1058      double *tmp_local = new double[dataPointSize];
1059      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
1060      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
1061      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
1062      delete[] tmp;
1063      delete[] tmp_local;
1064    #else
1065      AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals,*this);
1066    #endif
1067    
1068    //    //
1069    // create the numeric array to be returned    // create the numeric array to be returned
# Line 859  Data::integrate() const Line 1123  Data::integrate() const
1123  Data  Data
1124  Data::sin() const  Data::sin() const
1125  {  {
1126    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1127  }  }
1128    
1129  Data  Data
1130  Data::cos() const  Data::cos() const
1131  {  {
1132    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1133  }  }
1134    
1135  Data  Data
1136  Data::tan() const  Data::tan() const
1137  {  {
1138    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1139  }  }
1140    
1141  Data  Data
1142  Data::asin() const  Data::asin() const
1143  {  {
1144    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1145  }  }
1146    
1147  Data  Data
1148  Data::acos() const  Data::acos() const
1149  {  {
1150    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1151  }  }
1152    
1153    
1154  Data  Data
1155  Data::atan() const  Data::atan() const
1156  {  {
1157    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1158  }  }
1159    
1160  Data  Data
1161  Data::sinh() const  Data::sinh() const
1162  {  {
1163    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1164    
1165  }  }
1166    
1167  Data  Data
1168  Data::cosh() const  Data::cosh() const
1169  {  {
1170    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1171  }  }
1172    
1173  Data  Data
1174  Data::tanh() const  Data::tanh() const
1175  {  {
1176    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1177  }  }
1178    
1179    
# Line 918  Data::erf() const Line 1183  Data::erf() const
1183  #ifdef _WIN32  #ifdef _WIN32
1184    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1185  #else  #else
1186    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);    return C_TensorUnaryOperation(*this, ::erf);
1187  #endif  #endif
1188  }  }
1189    
# Line 926  Data Line 1191  Data
1191  Data::asinh() const  Data::asinh() const
1192  {  {
1193  #ifdef _WIN32  #ifdef _WIN32
1194    return escript::unaryOp(*this,escript::asinh_substitute);    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1195  #else  #else
1196    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);    return C_TensorUnaryOperation(*this, ::asinh);
1197  #endif  #endif
1198  }  }
1199    
# Line 936  Data Line 1201  Data
1201  Data::acosh() const  Data::acosh() const
1202  {  {
1203  #ifdef _WIN32  #ifdef _WIN32
1204    return escript::unaryOp(*this,escript::acosh_substitute);    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1205  #else  #else
1206    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);    return C_TensorUnaryOperation(*this, ::acosh);
1207  #endif  #endif
1208  }  }
1209    
# Line 946  Data Line 1211  Data
1211  Data::atanh() const  Data::atanh() const
1212  {  {
1213  #ifdef _WIN32  #ifdef _WIN32
1214    return escript::unaryOp(*this,escript::atanh_substitute);    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1215  #else  #else
1216    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);    return C_TensorUnaryOperation(*this, ::atanh);
1217  #endif  #endif
1218  }  }
1219    
1220  Data  Data
1221  Data::log10() const  Data::log10() const
1222  {  {
1223    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1224  }  }
1225    
1226  Data  Data
1227  Data::log() const  Data::log() const
1228  {  {
1229    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1230  }  }
1231    
1232  Data  Data
1233  Data::sign() const  Data::sign() const
1234  {  {
1235    return escript::unaryOp(*this,escript::fsign);    return C_TensorUnaryOperation(*this, escript::fsign);
1236  }  }
1237    
1238  Data  Data
1239  Data::abs() const  Data::abs() const
1240  {  {
1241    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1242  }  }
1243    
1244  Data  Data
1245  Data::neg() const  Data::neg() const
1246  {  {
1247    return escript::unaryOp(*this,negate<double>());    return C_TensorUnaryOperation(*this, negate<double>());
1248  }  }
1249    
1250  Data  Data
# Line 994  Data::pos() const Line 1259  Data::pos() const
1259  Data  Data
1260  Data::exp() const  Data::exp() const
1261  {  {
1262    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1263  }  }
1264    
1265  Data  Data
1266  Data::sqrt() const  Data::sqrt() const
1267  {  {
1268    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1269  }  }
1270    
1271  double  double
1272  Data::Lsup() const  Data::Lsup() const
1273  {  {
1274    double localValue, globalValue;    double localValue;
1275    //    //
1276    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1277    
1278    AbsMax abs_max_func;    AbsMax abs_max_func;
1279    localValue = algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1280  #ifdef PASO_MPI  #ifdef PASO_MPI
1281      double globalValue;
1282    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1283    return globalValue;    return globalValue;
1284  #else  #else
# Line 1021  Data::Lsup() const Line 1287  Data::Lsup() const
1287  }  }
1288    
1289  double  double
 Data::Linf() const  
 {  
   double localValue, globalValue;  
   //  
   // set the initial absolute minimum value to max double  
   AbsMin abs_min_func;  
   localValue = algorithm(abs_min_func,numeric_limits<double>::max());  
   
 #ifdef PASO_MPI  
   MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );  
   return globalValue;  
 #else  
   return localValue;  
 #endif  
 }  
   
 double  
1290  Data::sup() const  Data::sup() const
1291  {  {
1292    double localValue, globalValue;    double localValue;
1293    //    //
1294    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1295    FMax fmax_func;    FMax fmax_func;
1296    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1297  #ifdef PASO_MPI  #ifdef PASO_MPI
1298      double globalValue;
1299    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1300    return globalValue;    return globalValue;
1301  #else  #else
# Line 1056  Data::sup() const Line 1306  Data::sup() const
1306  double  double
1307  Data::inf() const  Data::inf() const
1308  {  {
1309    double localValue, globalValue;    double localValue;
1310    //    //
1311    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1312    FMin fmin_func;    FMin fmin_func;
1313    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1314  #ifdef PASO_MPI  #ifdef PASO_MPI
1315      double globalValue;
1316    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1317    return globalValue;    return globalValue;
1318  #else  #else
# Line 1093  Data Line 1344  Data
1344  Data::swapaxes(const int axis0, const int axis1) const  Data::swapaxes(const int axis0, const int axis1) const
1345  {  {
1346       int axis0_tmp,axis1_tmp;       int axis0_tmp,axis1_tmp;
1347       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1348       DataArrayView::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1349       // 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]
1350       // 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)
1351       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1119  Data::swapaxes(const int axis0, const in Line 1370  Data::swapaxes(const int axis0, const in
1370       }       }
1371       for (int i=0; i<rank; i++) {       for (int i=0; i<rank; i++) {
1372         if (i == axis0_tmp) {         if (i == axis0_tmp) {
1373            ev_shape.push_back(s[axis1_tmp]);            ev_shape.push_back(s[axis1_tmp]);
1374         } else if (i == axis1_tmp) {         } else if (i == axis1_tmp) {
1375            ev_shape.push_back(s[axis0_tmp]);            ev_shape.push_back(s[axis0_tmp]);
1376         } else {         } else {
1377            ev_shape.push_back(s[i]);            ev_shape.push_back(s[i]);
1378         }         }
1379       }       }
1380       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
# Line 1137  Data Line 1388  Data
1388  Data::symmetric() const  Data::symmetric() const
1389  {  {
1390       // check input       // check input
1391       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1392       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1393          if(s[0] != s[1])          if(s[0] != s[1])
1394             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.");
1395       }       }
1396       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
# Line 1159  Data Line 1410  Data
1410  Data::nonsymmetric() const  Data::nonsymmetric() const
1411  {  {
1412       // check input       // check input
1413       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1414       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1415          if(s[0] != s[1])          if(s[0] != s[1])
1416             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.");
1417          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1418          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1419          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1420          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1174  Data::nonsymmetric() const Line 1425  Data::nonsymmetric() const
1425       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1426          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1427             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.");
1428          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1429          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1430          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1431          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1192  Data::nonsymmetric() const Line 1443  Data::nonsymmetric() const
1443  Data  Data
1444  Data::trace(int axis_offset) const  Data::trace(int axis_offset) const
1445  {  {
1446       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1447       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1448          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1449          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1450          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1451          m_data->trace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1452          return ev;          return ev;
1453       }       }
1454       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1455          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1456          if (axis_offset==0) {          if (axis_offset==0) {
1457            int s2=s[2];            int s2=s[2];
1458            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1216  Data::trace(int axis_offset) const Line 1467  Data::trace(int axis_offset) const
1467          return ev;          return ev;
1468       }       }
1469       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1470          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1471          if (axis_offset==0) {          if (axis_offset==0) {
1472            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1473            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1242  Data::trace(int axis_offset) const Line 1493  Data::trace(int axis_offset) const
1493  Data  Data
1494  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1495  {  {
1496       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1497       DataArrayView::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1498       // 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]
1499       // 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)
1500       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1264  Data Line 1515  Data
1515  Data::eigenvalues() const  Data::eigenvalues() const
1516  {  {
1517       // check input       // check input
1518       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1519       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1520          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.");
1521       if(s[0] != s[1])       if(s[0] != s[1])
1522          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.");
1523       // create return       // create return
1524       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1525       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1526       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1527       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1280  Data::eigenvalues() const Line 1531  Data::eigenvalues() const
1531  const boost::python::tuple  const boost::python::tuple
1532  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1533  {  {
1534       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1535       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1536          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.");
1537       if(s[0] != s[1])       if(s[0] != s[1])
1538          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.");
1539       // create return       // create return
1540       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1541       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1542       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1543       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1544       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1545       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1546       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 1331  Data::calc_minGlobalDataPoint(int& ProcN Line 1582  Data::calc_minGlobalDataPoint(int& ProcN
1582      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1583      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1584        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1585          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1586          if (next<local_min) {          if (next<local_min) {
1587            local_min=next;            local_min=next;
1588            local_lowi=i;            local_lowi=i;
# Line 1349  Data::calc_minGlobalDataPoint(int& ProcN Line 1600  Data::calc_minGlobalDataPoint(int& ProcN
1600    
1601  #ifdef PASO_MPI  #ifdef PASO_MPI
1602      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1603      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1604      int lowProc = 0;      int lowProc = 0;
1605      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1606      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() );
1607        
1608      if( get_MPIRank()==0 ){      if( get_MPIRank()==0 ){
1609          next = globalMins[lowProc];          next = globalMins[lowProc];
1610          for( i=1; i<get_MPISize(); i++ )          for( i=1; i<get_MPISize(); i++ )
# Line 1375  Data::calc_minGlobalDataPoint(int& ProcN Line 1626  Data::calc_minGlobalDataPoint(int& ProcN
1626  void  void
1627  Data::saveDX(std::string fileName) const  Data::saveDX(std::string fileName) const
1628  {  {
1629      if (isEmpty())
1630      {
1631        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1632      }
1633    boost::python::dict args;    boost::python::dict args;
1634    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1635    getDomain().saveDX(fileName,args);    getDomain()->saveDX(fileName,args);
1636    return;    return;
1637  }  }
1638    
1639  void  void
1640  Data::saveVTK(std::string fileName) const  Data::saveVTK(std::string fileName) const
1641  {  {
1642      if (isEmpty())
1643      {
1644        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1645      }
1646    boost::python::dict args;    boost::python::dict args;
1647    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1648    getDomain().saveVTK(fileName,args);    getDomain()->saveVTK(fileName,args);
1649    return;    return;
1650  }  }
1651    
# Line 1407  Data::operator+=(const boost::python::ob Line 1666  Data::operator+=(const boost::python::ob
1666    binaryOp(tmp,plus<double>());    binaryOp(tmp,plus<double>());
1667    return (*this);    return (*this);
1668  }  }
1669    Data&
1670    Data::operator=(const Data& other)
1671    {
1672      copy(other);
1673      return (*this);
1674    }
1675    
1676  Data&  Data&
1677  Data::operator-=(const Data& right)  Data::operator-=(const Data& right)
# Line 1479  Data::powO(const boost::python::object& Line 1744  Data::powO(const boost::python::object&
1744  Data  Data
1745  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1746  {  {
1747    Data result;    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   if (getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result.binaryOp(*this,escript::rpow);  
   } else {  
      result.copy(*this);  
      result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   }  
   return result;  
1748  }  }
1749    
   
1750  //  //
1751  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1752  Data  Data
1753  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1754  {  {
1755    Data result;    return C_TensorBinaryOperation(left, right, plus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result+=left;  
   } else {  
      result.copy(left);  
      result+=right;  
   }  
   return result;  
1756  }  }
1757    
1758  //  //
# Line 1514  escript::operator+(const Data& left, con Line 1760  escript::operator+(const Data& left, con
1760  Data  Data
1761  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
1762  {  {
1763    Data result;    return C_TensorBinaryOperation(left, right, minus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.neg();  
      result+=left;  
   } else {  
      result.copy(left);  
      result-=right;  
   }  
   return result;  
1764  }  }
1765    
1766  //  //
# Line 1532  escript::operator-(const Data& left, con Line 1768  escript::operator-(const Data& left, con
1768  Data  Data
1769  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
1770  {  {
1771    Data result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result*=left;  
   } else {  
      result.copy(left);  
      result*=right;  
   }  
   return result;  
1772  }  }
1773    
1774  //  //
# Line 1550  escript::operator*(const Data& left, con Line 1776  escript::operator*(const Data& left, con
1776  Data  Data
1777  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
1778  {  {
1779    Data result;    return C_TensorBinaryOperation(left, right, divides<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.oneOver();  
      result*=left;  
   } else {  
      result.copy(left);  
      result/=right;  
   }  
   return result;  
1780  }  }
1781    
1782  //  //
# Line 1675  escript::operator/(const boost::python:: Line 1891  escript::operator/(const boost::python::
1891  /* TODO */  /* TODO */
1892  /* global reduction */  /* global reduction */
1893  Data  Data
1894  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1895  {  {
1896    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1897    
1898    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1899    
1900    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1901      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1902    }    }
1903    
# Line 1691  Data::getItem(const boost::python::objec Line 1907  Data::getItem(const boost::python::objec
1907  /* TODO */  /* TODO */
1908  /* global reduction */  /* global reduction */
1909  Data  Data
1910  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
1911  {  {
1912    return Data(*this,region);    return Data(*this,region);
1913  }  }
# Line 1706  Data::setItemO(const boost::python::obje Line 1922  Data::setItemO(const boost::python::obje
1922    setItemD(key,tempData);    setItemD(key,tempData);
1923  }  }
1924    
 /* TODO */  
 /* global reduction */  
1925  void  void
1926  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
1927                 const Data& value)                 const Data& value)
1928  {  {
1929    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1930    
1931    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1932    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1933      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1934    }    }
1935    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 1725  Data::setItemD(const boost::python::obje Line 1939  Data::setItemD(const boost::python::obje
1939    }    }
1940  }  }
1941    
 /* TODO */  
 /* global reduction */  
1942  void  void
1943  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1944                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
1945  {  {
1946    if (isProtected()) {    if (isProtected()) {
1947          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
# Line 1770  Data::typeMatchRight(const Data& right) Line 1982  Data::typeMatchRight(const Data& right)
1982    
1983  void  void
1984  Data::setTaggedValueByName(std::string name,  Data::setTaggedValueByName(std::string name,
1985                             const boost::python::object& value)                             const boost::python::object& value)
1986  {  {
1987       if (getFunctionSpace().getDomain().isValidTagName(name)) {       if (getFunctionSpace().getDomain()->isValidTagName(name)) {
1988          int tagKey=getFunctionSpace().getDomain().getTag(name);          int tagKey=getFunctionSpace().getDomain()->getTag(name);
1989          setTaggedValue(tagKey,value);          setTaggedValue(tagKey,value);
1990       }       }
1991  }  }
# Line 1786  Data::setTaggedValue(int tagKey, Line 1998  Data::setTaggedValue(int tagKey,
1998    }    }
1999    //    //
2000    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2001    tag();    if (isConstant()) tag();
2002    
2003    if (!isTagged()) {    numeric::array asNumArray(value);
2004      throw DataException("Error - DataTagged conversion failed!!");  
2005    
2006      // extract the shape of the numarray
2007      DataTypes::ShapeType tempShape;
2008      for (int i=0; i < asNumArray.getrank(); i++) {
2009        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2010    }    }
2011    
2012    //    // get the space for the data vector
2013    // Construct DataArray from boost::python::object input value  //   int len = DataTypes::noValues(tempShape);
2014    DataArray valueDataArray(value);  //   DataVector temp_data(len, 0.0, len);
2015    //   DataArrayView temp_dataView(temp_data, tempShape);
2016    //   temp_dataView.copy(asNumArray);
2017    
2018      DataVector temp_data2;
2019      temp_data2.copyFromNumArray(asNumArray);
2020    
2021    //    //
2022    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2023    m_data->setTaggedValue(tagKey,valueDataArray.getView());    //m_data->setTaggedValue(tagKey,temp_dataView);
2024    
2025        m_data->setTaggedValue(tagKey,tempShape, temp_data2);
2026  }  }
2027    
2028    
2029  void  void
2030  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
2031                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
2032                                const DataTypes::ValueType& value,
2033                    int dataOffset)
2034  {  {
2035    if (isProtected()) {    if (isProtected()) {
2036          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2037    }    }
2038    //    //
2039    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2040    tag();    if (isConstant()) tag();
2041    
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
2042    //    //
2043    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2044    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
2045  }  }
2046    
2047  int  int
2048  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
2049  {  {
2050    return m_data->getTagNumber(dpno);    if (isEmpty())
2051  }    {
2052        throw DataException("Error - operation not permitted on instances of DataEmpty.");
 void  
 Data::archiveData(const std::string fileName)  
 {  
   cout << "Archiving Data object to: " << fileName << endl;  
   
   //  
   // Determine type of this Data object  
   int dataType = -1;  
   
   if (isEmpty()) {  
     dataType = 0;  
     cout << "\tdataType: DataEmpty" << endl;  
   }  
   if (isConstant()) {  
     dataType = 1;  
     cout << "\tdataType: DataConstant" << endl;  
   }  
   if (isTagged()) {  
     dataType = 2;  
     cout << "\tdataType: DataTagged" << endl;  
   }  
   if (isExpanded()) {  
     dataType = 3;  
     cout << "\tdataType: DataExpanded" << endl;  
   }  
   
   if (dataType == -1) {  
     throw DataException("archiveData Error: undefined dataType");  
   }  
   
   //  
   // Collect data items common to all Data types  
   int noSamples = getNumSamples();  
   int noDPPSample = getNumDataPointsPerSample();  
   int functionSpaceType = getFunctionSpace().getTypeCode();  
   int dataPointRank = getDataPointRank();  
   int dataPointSize = getDataPointSize();  
   int dataLength = getLength();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
   vector<int> referenceNumbers(noSamples);  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);  
   }  
   vector<int> tagNumbers(noSamples);  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);  
     }  
   }  
   
   cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;  
   cout << "\tfunctionSpaceType: " << functionSpaceType << endl;  
   cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;  
   
   //  
   // Flatten Shape to an array of integers suitable for writing to file  
   int flatShape[4] = {0,0,0,0};  
   cout << "\tshape: < ";  
   for (int dim=0; dim<dataPointRank; dim++) {  
     flatShape[dim] = dataPointShape[dim];  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
   
   //  
   // Open archive file  
   ofstream archiveFile;  
   archiveFile.open(fileName.data(), ios::out);  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem opening archive file");  
   }  
   
   //  
   // Write common data items to archive file  
   archiveFile.write(reinterpret_cast<char *>(&dataType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.write(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.write(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.write(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing to archive file");  
   }  
   
   //  
   // Archive underlying data values for each Data type  
   int noValues;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       noValues = 0;  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       break;  
     case 1:  
       // DataConstant  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 2:  
       // DataTagged  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing data to archive file");  
   }  
   
   //  
   // Close archive file  
   archiveFile.close();  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem closing archive file");  
2053    }    }
2054      return getFunctionSpace().getTagFromDataPointNo(dpno);
2055  }  }
2056    
 void  
 Data::extractData(const std::string fileName,  
                   const FunctionSpace& fspace)  
 {  
   //  
   // Can only extract Data to an object which is initially DataEmpty  
   if (!isEmpty()) {  
     throw DataException("extractData Error: can only extract to DataEmpty object");  
   }  
   
   cout << "Extracting Data object from: " << fileName << endl;  
   
   int dataType;  
   int noSamples;  
   int noDPPSample;  
   int functionSpaceType;  
   int dataPointRank;  
   int dataPointSize;  
   int dataLength;  
   DataArrayView::ShapeType dataPointShape;  
   int flatShape[4];  
   
   //  
   // Open the archive file  
   ifstream archiveFile;  
   archiveFile.open(fileName.data(), ios::in);  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem opening archive file");  
   }  
   
   //  
   // Read common data items from archive file  
   archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
     if (flatShape[dim]>0) {  
       dataPointShape.push_back(flatShape[dim]);  
     }  
   }  
   vector<int> referenceNumbers(noSamples);  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   vector<int> tagNumbers(noSamples);  
   if (dataType==2) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
   }  
   
   //  
   // Verify the values just read from the archive file  
   switch (dataType) {  
     case 0:  
       cout << "\tdataType: DataEmpty" << endl;  
       break;  
     case 1:  
       cout << "\tdataType: DataConstant" << endl;  
       break;  
     case 2:  
       cout << "\tdataType: DataTagged" << endl;  
       break;  
     case 3:  
       cout << "\tdataType: DataExpanded" << endl;  
       break;  
     default:  
       throw DataException("extractData Error: undefined dataType read from archive file");  
       break;  
   }  
   
   cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;  
   cout << "\tfunctionSpaceType: " << functionSpaceType << endl;  
   cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;  
   cout << "\tshape: < ";  
   for (int dim = 0; dim < dataPointRank; dim++) {  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
   
   //  
   // Verify that supplied FunctionSpace object is compatible with this Data object.  
   if ( (fspace.getTypeCode()!=functionSpaceType) ||  
        (fspace.getNumSamples()!=noSamples) ||  
        (fspace.getNumDPPSample()!=noDPPSample)  
      ) {  
     throw DataException("extractData Error: incompatible FunctionSpace");  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {  
       throw DataException("extractData Error: incompatible FunctionSpace");  
     }  
   }  
   if (dataType==2) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {  
         throw DataException("extractData Error: incompatible FunctionSpace");  
       }  
     }  
   }  
   
   //  
   // Construct a DataVector to hold underlying data values  
   DataVector dataVec(dataLength);  
   
   //  
   // Load this DataVector with the appropriate values  
   int noValues;  
   archiveFile.read(reinterpret_cast<char *>(&noValues),sizeof(int));  
   cout << "\tnoValues: " << noValues << endl;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       if (noValues != 0) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 1:  
       // DataConstant  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 2:  
       // DataTagged  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
   }  
   
   //  
   // Close archive file  
   archiveFile.close();  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem closing archive file");  
   }  
   
   //  
   // Construct an appropriate Data object  
   DataAbstract* tempData;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       tempData=new DataEmpty();  
       break;  
     case 1:  
       // DataConstant  
       tempData=new DataConstant(fspace,dataPointShape,dataVec);  
       break;  
     case 2:  
       // DataTagged  
       tempData=new DataTagged(fspace,dataPointShape,tagNumbers,dataVec);  
       break;  
     case 3:  
       // DataExpanded  
       tempData=new DataExpanded(fspace,dataPointShape,dataVec);  
       break;  
   }  
   shared_ptr<DataAbstract> temp_data(tempData);  
   m_data=temp_data;  
 }  
2057    
2058  ostream& escript::operator<<(ostream& o, const Data& data)  ostream& escript::operator<<(ostream& o, const Data& data)
2059  {  {
# Line 2177  escript::C_GeneralTensorProduct(Data& ar Line 2070  escript::C_GeneralTensorProduct(Data& ar
2070    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2071    // SM is the product of the last axis_offset entries in arg_0.getShape().    // SM is the product of the last axis_offset entries in arg_0.getShape().
2072    
   
2073    // Interpolate if necessary and find an appropriate function space    // Interpolate if necessary and find an appropriate function space
2074    Data arg_0_Z, arg_1_Z;    Data arg_0_Z, arg_1_Z;
2075    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
# Line 2199  escript::C_GeneralTensorProduct(Data& ar Line 2091  escript::C_GeneralTensorProduct(Data& ar
2091    // Get rank and shape of inputs    // Get rank and shape of inputs
2092    int rank0 = arg_0_Z.getDataPointRank();    int rank0 = arg_0_Z.getDataPointRank();
2093    int rank1 = arg_1_Z.getDataPointRank();    int rank1 = arg_1_Z.getDataPointRank();
2094    DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2095    DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2096    
2097    // Prepare for the loops of the product and verify compatibility of shapes    // Prepare for the loops of the product and verify compatibility of shapes
2098    int start0=0, start1=0;    int start0=0, start1=0;
# Line 2209  escript::C_GeneralTensorProduct(Data& ar Line 2101  escript::C_GeneralTensorProduct(Data& ar
2101    else if (transpose == 2)  { start1 = rank1-axis_offset; }    else if (transpose == 2)  { start1 = rank1-axis_offset; }
2102    else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }    else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2103    
2104    
2105    // Adjust the shapes for transpose    // Adjust the shapes for transpose
2106    DataArrayView::ShapeType tmpShape0;    DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2107    DataArrayView::ShapeType tmpShape1;    DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2108    for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }    for (int i=0; i<rank0; i++)   { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2109    for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }    for (int i=0; i<rank1; i++)   { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2110    
2111  #if 0  #if 0
2112    // For debugging: show shape after transpose    // For debugging: show shape after transpose
# Line 2244  escript::C_GeneralTensorProduct(Data& ar Line 2137  escript::C_GeneralTensorProduct(Data& ar
2137      SR *= tmpShape1[i];      SR *= tmpShape1[i];
2138    }    }
2139    
2140    // Define the shape of the output    // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2141    DataArrayView::ShapeType shape2;    DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2142    for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z    {         // block to limit the scope of out_index
2143    for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z       int out_index=0;
2144         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2145         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2146      }
2147    
2148    // Declare output Data object    // Declare output Data object
2149    Data res;    Data res;
2150    
2151    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2152      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2153      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2154      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2155      double *ptr_2 = &((res.getPointDataView().getData())[0]);      double *ptr_2 = &(res.getDataAtOffset(0));
2156      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2157    }    }
2158    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
# Line 2277  escript::C_GeneralTensorProduct(Data& ar Line 2173  escript::C_GeneralTensorProduct(Data& ar
2173    
2174      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2175      int offset_0 = tmp_0->getPointOffset(0,0);      int offset_0 = tmp_0->getPointOffset(0,0);
2176      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2177      // Get the views      // Get the views
2178      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2179      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2180      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2181      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2182      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2183    
2184        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2185        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2186    
2187    
2188      // Compute an MVP for the default      // Compute an MVP for the default
2189      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2190      // Compute an MVP for each tag      // Compute an MVP for each tag
2191      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2192      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2193      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2194        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2195        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2196        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2197        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2198        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2199    
2200          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2201          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2202        
2203        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2204      }      }
2205    
# Line 2317  escript::C_GeneralTensorProduct(Data& ar Line 2222  escript::C_GeneralTensorProduct(Data& ar
2222        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2223          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2224          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2225          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2226          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2227          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2228          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2229        }        }
2230      }      }
# Line 2343  escript::C_GeneralTensorProduct(Data& ar Line 2248  escript::C_GeneralTensorProduct(Data& ar
2248    
2249      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2250      int offset_1 = tmp_1->getPointOffset(0,0);      int offset_1 = tmp_1->getPointOffset(0,0);
2251      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2252      // Get the views      // Get the views
2253      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2254      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2255      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2256      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2257      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2258    
2259        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2260        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2261    
2262      // Compute an MVP for the default      // Compute an MVP for the default
2263      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2264      // Compute an MVP for each tag      // Compute an MVP for each tag
2265      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2266      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2267      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2268        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2269        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2270        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2271        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2272        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2273    
2274          tmp_2->addTag(i->first);
2275          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2276          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2277        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2278      }      }
2279    
# Line 2381  escript::C_GeneralTensorProduct(Data& ar Line 2294  escript::C_GeneralTensorProduct(Data& ar
2294      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2295      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2296    
2297      // Get the views  //     // Get the views
2298      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2299      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2300      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2301      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2302      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2303      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2304      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2305    
2306        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2307        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2308        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2309    
2310    
2311      // Compute an MVP for the default      // Compute an MVP for the default
2312      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2313      // Merge the tags      // Merge the tags
# Line 2396  escript::C_GeneralTensorProduct(Data& ar Line 2315  escript::C_GeneralTensorProduct(Data& ar
2315      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2316      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2317      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2318        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape        tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2319      }      }
2320      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2321        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2322      }      }
2323      // Compute an MVP for each tag      // Compute an MVP for each tag
2324      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2325      for (i=lookup_2.begin();i!=lookup_2.end();i++) {      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2326        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2327        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2328        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2329        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2330        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2331        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2332    
2333          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2334          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2335          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2336    
2337        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2338      }      }
2339    
# Line 2430  escript::C_GeneralTensorProduct(Data& ar Line 2354  escript::C_GeneralTensorProduct(Data& ar
2354      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2355      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2356        int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0        int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2357        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2358        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2359          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2360          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2361          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2362          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2363          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2364        }        }
2365      }      }
# Line 2459  escript::C_GeneralTensorProduct(Data& ar Line 2383  escript::C_GeneralTensorProduct(Data& ar
2383        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2384          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2385          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2386          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2387          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2388          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2389          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2390        }        }
2391      }      }
# Line 2484  escript::C_GeneralTensorProduct(Data& ar Line 2408  escript::C_GeneralTensorProduct(Data& ar
2408      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2409      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2410        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2411        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);        double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2412        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2413          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2414          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2415          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2416          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2417          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2418        }        }
2419      }      }
# Line 2514  escript::C_GeneralTensorProduct(Data& ar Line 2438  escript::C_GeneralTensorProduct(Data& ar
2438          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2439          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2440          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2441          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2442          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2443          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2444          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2445        }        }
2446      }      }
# Line 2535  Data::borrowData() const Line 2459  Data::borrowData() const
2459    return m_data.get();    return m_data.get();
2460  }  }
2461    
2462    
2463    std::string
2464    Data::toString() const
2465    {
2466        static const DataTypes::ValueType::size_type TOO_MANY_POINTS=8000;
2467        if (getNumDataPoints()*getDataPointSize()>TOO_MANY_POINTS)
2468        {
2469        stringstream temp;
2470        temp << "Summary: inf="<< inf() << " sup=" << sup() << " data points=" << getNumDataPoints();
2471        return  temp.str();
2472        }
2473        return m_data->toString();
2474    }
2475    
2476    
2477    
2478    DataTypes::ValueType::const_reference
2479    Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2480    {
2481        return m_data->getDataAtOffset(i);
2482    }
2483    
2484    
2485    DataTypes::ValueType::reference
2486    Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2487    {
2488        return m_data->getDataAtOffset(i);
2489    }
2490    
2491    DataTypes::ValueType::const_reference
2492    Data::getDataPoint(int sampleNo, int dataPointNo) const
2493    {
2494        return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
2495    }
2496    
2497    
2498    DataTypes::ValueType::reference
2499    Data::getDataPoint(int sampleNo, int dataPointNo)
2500    {
2501        return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
2502    }
2503    
2504    
2505  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2506    
2507  void  void
2508  Data::print()  Data::print()
2509  {  {
2510    int i,j;    int i,j;
2511      
2512    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2513    for( i=0; i<getNumSamples(); i++ )    for( i=0; i<getNumSamples(); i++ )
2514    {    {
# Line 2551  Data::print() Line 2518  Data::print()
2518      printf( "\n" );      printf( "\n" );
2519    }    }
2520  }  }
2521    void
2522    Data::dump(const std::string fileName) const
2523    {
2524      try
2525         {
2526            return m_data->dump(fileName);
2527         }
2528         catch (exception& e)
2529         {
2530            cout << e.what() << endl;
2531         }
2532    }
2533    
2534  int  int
2535  Data::get_MPISize() const  Data::get_MPISize() const
2536  {  {
2537      int error, size;      int size;
2538  #ifdef PASO_MPI  #ifdef PASO_MPI
2539        int error;
2540      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
2541  #else  #else
2542      size = 1;      size = 1;
# Line 2567  Data::get_MPISize() const Line 2547  Data::get_MPISize() const
2547  int  int
2548  Data::get_MPIRank() const  Data::get_MPIRank() const
2549  {  {
2550      int error, rank;      int rank;
2551  #ifdef PASO_MPI  #ifdef PASO_MPI
2552        int error;
2553      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
2554  #else  #else
2555      rank = 0;      rank = 0;
# Line 2578  Data::get_MPIRank() const Line 2559  Data::get_MPIRank() const
2559    
2560  MPI_Comm  MPI_Comm
2561  Data::get_MPIComm() const  Data::get_MPIComm() const
2562  {  {
2563  #ifdef PASO_MPI  #ifdef PASO_MPI
2564      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
2565  #else  #else
# Line 2586  Data::get_MPIComm() const Line 2567  Data::get_MPIComm() const
2567  #endif  #endif
2568  }  }
2569    
2570    

Legend:
Removed from v.1118  
changed lines
  Added in v.1878

  ViewVC Help
Powered by ViewVC 1.1.26