/[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 1349 by matt, Wed Nov 21 06:38:08 2007 UTC revision 2037 by jfenwick, Thu Nov 13 06:17:12 2008 UTC
# Line 1  Line 1 
1    
 /* $Id$ */  
   
2  /*******************************************************  /*******************************************************
3   *  *
4   *           Copyright 2003-2007 by ACceSS MNRF  * Copyright (c) 2003-2008 by University of Queensland
5   *       Copyright 2007 by University of Queensland  * Earth Systems Science Computational Center (ESSCC)
6   *  * http://www.uq.edu.au/esscc
7   *                http://esscc.uq.edu.au  *
8   *        Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
9   *  Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
10   *     http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
11   *  *
12   *******************************************************/  *******************************************************/
13    
14    
15  #include "Data.h"  #include "Data.h"
16    
# Line 19  Line 18 
18  #include "DataConstant.h"  #include "DataConstant.h"
19  #include "DataTagged.h"  #include "DataTagged.h"
20  #include "DataEmpty.h"  #include "DataEmpty.h"
21  #include "DataArrayView.h"  #include "DataLazy.h"
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
25    #include "FunctionSpaceException.h"
26    #include "EscriptParams.h"
27    
28  extern "C" {  extern "C" {
29  #include "escript/blocktimer.h"  #include "escript/blocktimer.h"
30  }  }
# Line 41  using namespace boost::python; Line 43  using namespace boost::python;
43  using namespace boost;  using namespace boost;
44  using namespace escript;  using namespace escript;
45    
46    // ensure the current object is not a DataLazy
47    // The idea was that we could add an optional warning whenever a resolve is forced
48    #define FORCERESOLVE if (isLazy()) {resolve();}
49    
50  Data::Data()  Data::Data()
51  {  {
52    //    //
53    // Default data is type DataEmpty    // Default data is type DataEmpty
54    DataAbstract* temp=new DataEmpty();    DataAbstract* temp=new DataEmpty();
55    shared_ptr<DataAbstract> temp_data(temp);    m_data=temp->getPtr();
   m_data=temp_data;  
56    m_protected=false;    m_protected=false;
57  }  }
58    
# Line 56  Data::Data(double value, Line 61  Data::Data(double value,
61             const FunctionSpace& what,             const FunctionSpace& what,
62             bool expanded)             bool expanded)
63  {  {
64    DataArrayView::ShapeType dataPointShape;    DataTypes::ShapeType dataPointShape;
65    for (int i = 0; i < shape.attr("__len__")(); ++i) {    for (int i = 0; i < shape.attr("__len__")(); ++i) {
66      dataPointShape.push_back(extract<const int>(shape[i]));      dataPointShape.push_back(extract<const int>(shape[i]));
67    }    }
68    
69    int len = DataArrayView::noValues(dataPointShape);    int len = DataTypes::noValues(dataPointShape);
70    DataVector temp_data(len,value,len);    DataVector temp_data(len,value,len);
71    DataArrayView temp_dataView(temp_data, dataPointShape);    initialise(temp_data, dataPointShape, what, expanded);
   
   initialise(temp_dataView, what, expanded);  
   
72    m_protected=false;    m_protected=false;
73  }  }
74    
75  Data::Data(double value,  Data::Data(double value,
76         const DataArrayView::ShapeType& dataPointShape,         const DataTypes::ShapeType& dataPointShape,
77         const FunctionSpace& what,         const FunctionSpace& what,
78             bool expanded)             bool expanded)
79  {  {
80    int len = DataArrayView::noValues(dataPointShape);    int len = DataTypes::noValues(dataPointShape);
81    
82    DataVector temp_data(len,value,len);    DataVector temp_data(len,value,len);
83    DataArrayView temp_dataView(temp_data, dataPointShape);  //   DataArrayView temp_dataView(temp_data, dataPointShape);
84    
85    initialise(temp_dataView, what, expanded);  //   initialise(temp_dataView, what, expanded);
86      initialise(temp_data, dataPointShape, what, expanded);
87    
88    m_protected=false;    m_protected=false;
89  }  }
# Line 91  Data::Data(const Data& inData) Line 94  Data::Data(const Data& inData)
94    m_protected=inData.isProtected();    m_protected=inData.isProtected();
95  }  }
96    
97    
98  Data::Data(const Data& inData,  Data::Data(const Data& inData,
99             const DataArrayView::RegionType& region)             const DataTypes::RegionType& region)
100  {  {
101      DataAbstract_ptr dat=inData.m_data;
102      if (inData.isLazy())
103      {
104        dat=inData.m_data->resolve();
105      }
106      else
107      {
108        dat=inData.m_data;
109      }
110    //    //
111    // Create Data which is a slice of another Data    // Create Data which is a slice of another Data
112    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = dat->getSlice(region);
113    shared_ptr<DataAbstract> temp_data(tmp);    m_data=DataAbstract_ptr(tmp);
   m_data=temp_data;  
114    m_protected=false;    m_protected=false;
115  }  }
116    
117  Data::Data(const Data& inData,  Data::Data(const Data& inData,
118             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
119  {  {
120      if (inData.isEmpty())
121      {
122        throw DataException("Error - will not interpolate for instances of DataEmpty.");
123      }
124    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
125      m_data=inData.m_data;      m_data=inData.m_data;
126    } else {    }
127      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);    else
128      // Note: Must use a reference or pointer to a derived object    {
129      // in order to get polymorphic behaviour. Shouldn't really  
130      // be able to create an instance of AbstractDomain but that was done      if (inData.isConstant()) {  // for a constant function, we just need to use the new function space
131      // as a boost:python work around which may no longer be required.        if (!inData.probeInterpolation(functionspace))
132      const AbstractDomain& inDataDomain=inData.getDomain();        {           // Even though this is constant, we still need to check whether interpolation is allowed
133      if  (inDataDomain==functionspace.getDomain()) {      throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
134        inDataDomain.interpolateOnDomain(tmp,inData);        }
135          // if the data is not lazy, this will just be a cast to DataReady
136          DataReady_ptr dr=inData.m_data->resolve();
137          DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),dr->getVector());  
138          m_data=DataAbstract_ptr(dc);
139      } else {      } else {
140        inDataDomain.interpolateACross(tmp,inData);        Data tmp(0,inData.getDataPointShape(),functionspace,true);
141          // Note: Must use a reference or pointer to a derived object
142          // in order to get polymorphic behaviour. Shouldn't really
143          // be able to create an instance of AbstractDomain but that was done
144          // as a boost:python work around which may no longer be required.
145          /*const AbstractDomain& inDataDomain=inData.getDomain();*/
146          const_Domain_ptr inDataDomain=inData.getDomain();
147          if  (inDataDomain==functionspace.getDomain()) {
148            inDataDomain->interpolateOnDomain(tmp,inData);
149          } else {
150            inDataDomain->interpolateACross(tmp,inData);
151          }
152          m_data=tmp.m_data;
153      }      }
     m_data=tmp.m_data;  
154    }    }
155    m_protected=false;    m_protected=false;
156  }  }
157    
158  Data::Data(const DataTagged::TagListType& tagKeys,  Data::Data(DataAbstract* underlyingdata)
            const DataTagged::ValueListType & values,  
            const DataArrayView& defaultValue,  
            const FunctionSpace& what,  
            bool expanded)  
159  {  {
160    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);  //  m_data=shared_ptr<DataAbstract>(underlyingdata);
161    shared_ptr<DataAbstract> temp_data(temp);      m_data=underlyingdata->getPtr();
162    m_data=temp_data;      m_protected=false;
163    m_protected=false;  }
164    if (expanded) {  
165      expand();  Data::Data(DataAbstract_ptr underlyingdata)
166    }  {
167        m_data=underlyingdata;
168        m_protected=false;
169  }  }
170    
171    
172  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
173         const FunctionSpace& what,         const FunctionSpace& what,
174             bool expanded)             bool expanded)
# Line 146  Data::Data(const numeric::array& value, Line 176  Data::Data(const numeric::array& value,
176    initialise(value,what,expanded);    initialise(value,what,expanded);
177    m_protected=false;    m_protected=false;
178  }  }
179    /*
180  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
181         const FunctionSpace& what,         const FunctionSpace& what,
182             bool expanded)             bool expanded)
183  {  {
184    initialise(value,what,expanded);    initialise(value,what,expanded);
185    m_protected=false;    m_protected=false;
186    }*/
187    
188    Data::Data(const DataTypes::ValueType& value,
189             const DataTypes::ShapeType& shape,
190                     const FunctionSpace& what,
191                     bool expanded)
192    {
193       initialise(value,shape,what,expanded);
194       m_protected=false;
195  }  }
196    
197    
198  Data::Data(const object& value,  Data::Data(const object& value,
199         const FunctionSpace& what,         const FunctionSpace& what,
200             bool expanded)             bool expanded)
# Line 168  Data::Data(const object& value, Line 208  Data::Data(const object& value,
208  Data::Data(const object& value,  Data::Data(const object& value,
209             const Data& other)             const Data& other)
210  {  {
   
211    numeric::array asNumArray(value);    numeric::array asNumArray(value);
212    
   
213    // extract the shape of the numarray    // extract the shape of the numarray
214    DataArrayView::ShapeType tempShape;    DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
215    for (int i=0; i < asNumArray.getrank(); i++) {  // /*  for (int i=0; i < asNumArray.getrank(); i++) {
216      tempShape.push_back(extract<int>(asNumArray.getshape()[i]));  //     tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
217    }  //   }*/
218    // get the space for the data vector  //   // get the space for the data vector
219    int len = DataArrayView::noValues(tempShape);  //   int len = DataTypes::noValues(tempShape);
220    DataVector temp_data(len, 0.0, len);  //   DataVector temp_data(len, 0.0, len);
221    DataArrayView temp_dataView(temp_data, tempShape);  // /*  DataArrayView temp_dataView(temp_data, tempShape);
222    temp_dataView.copy(asNumArray);  //   temp_dataView.copy(asNumArray);*/
223    //   temp_data.copyFromNumArray(asNumArray);
224    
225    //    //
226    // Create DataConstant using the given value and all other parameters    // Create DataConstant using the given value and all other parameters
227    // copied from other. If value is a rank 0 object this Data    // copied from other. If value is a rank 0 object this Data
228    // will assume the point data shape of other.    // will assume the point data shape of other.
229    
230    if (temp_dataView.getRank()==0) {    if (DataTypes::getRank(tempShape)/*temp_dataView.getRank()*/==0) {
231      int len = DataArrayView::noValues(other.getPointDataView().getShape());  
232    
233        // get the space for the data vector
234        int len1 = DataTypes::noValues(tempShape);
235        DataVector temp_data(len1, 0.0, len1);
236        temp_data.copyFromNumArray(asNumArray);
237    
238        int len = DataTypes::noValues(other.getDataPointShape());
239    
240      DataVector temp2_data(len, temp_dataView(), len);      DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
241      DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());      //DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
242      initialise(temp2_dataView, other.getFunctionSpace(), false);  //     initialise(temp2_dataView, other.getFunctionSpace(), false);
243    
244        DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
245    //     boost::shared_ptr<DataAbstract> sp(t);
246    //     m_data=sp;
247        m_data=DataAbstract_ptr(t);
248    
249    } else {    } else {
250      //      //
251      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
252      initialise(temp_dataView, other.getFunctionSpace(), false);  //     initialise(temp_dataView, other.getFunctionSpace(), false);
253        DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
254    //     boost::shared_ptr<DataAbstract> sp(t);
255    //     m_data=sp;
256        m_data=DataAbstract_ptr(t);
257    }    }
258    m_protected=false;    m_protected=false;
259  }  }
# Line 208  Data::~Data() Line 263  Data::~Data()
263    
264  }  }
265    
266    
267    
268    void
269    Data::initialise(const boost::python::numeric::array& value,
270                     const FunctionSpace& what,
271                     bool expanded)
272    {
273      //
274      // Construct a Data object of the appropriate type.
275      // Construct the object first as there seems to be a bug which causes
276      // undefined behaviour if an exception is thrown during construction
277      // within the shared_ptr constructor.
278      if (expanded) {
279        DataAbstract* temp=new DataExpanded(value, what);
280    //     boost::shared_ptr<DataAbstract> temp_data(temp);
281    //     m_data=temp_data;
282        m_data=temp->getPtr();
283      } else {
284        DataAbstract* temp=new DataConstant(value, what);
285    //     boost::shared_ptr<DataAbstract> temp_data(temp);
286    //     m_data=temp_data;
287        m_data=temp->getPtr();
288      }
289    }
290    
291    
292    void
293    Data::initialise(const DataTypes::ValueType& value,
294             const DataTypes::ShapeType& shape,
295                     const FunctionSpace& what,
296                     bool expanded)
297    {
298      //
299      // Construct a Data object of the appropriate type.
300      // Construct the object first as there seems to be a bug which causes
301      // undefined behaviour if an exception is thrown during construction
302      // within the shared_ptr constructor.
303      if (expanded) {
304        DataAbstract* temp=new DataExpanded(what, shape, value);
305    //     boost::shared_ptr<DataAbstract> temp_data(temp);
306    //     m_data=temp_data;
307        m_data=temp->getPtr();
308      } else {
309        DataAbstract* temp=new DataConstant(what, shape, value);
310    //     boost::shared_ptr<DataAbstract> temp_data(temp);
311    //     m_data=temp_data;
312        m_data=temp->getPtr();
313      }
314    }
315    
316    
317    // void
318    // Data::CompareDebug(const Data& rd)
319    // {
320    //  using namespace std;
321    //  bool mismatch=false;
322    //  std::cout << "Comparing left and right" << endl;
323    //  const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
324    //  const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
325    //  
326    //  if (left==0)
327    //  {
328    //      cout << "left arg is not a DataTagged\n";
329    //      return;
330    //  }
331    //  
332    //  if (right==0)
333    //  {
334    //      cout << "right arg is not a DataTagged\n";
335    //      return;
336    //  }
337    //  cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
338    //  cout << "Shapes ";
339    //  if (left->getShape()==right->getShape())
340    //  {
341    //      cout << "ok\n";
342    //  }
343    //  else
344    //  {
345    //      cout << "Problem: shapes do not match\n";
346    //      mismatch=true;
347    //  }
348    //  int lim=left->getVector().size();
349    //  if (right->getVector().size()) lim=right->getVector().size();
350    //  for (int i=0;i<lim;++i)
351    //  {
352    //      if (left->getVector()[i]!=right->getVector()[i])
353    //      {
354    //          cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
355    //          mismatch=true;
356    //      }
357    //  }
358    //
359    //  // still need to check the tag map
360    //  // also need to watch what is happening to function spaces, are they copied or what?
361    //
362    //  const DataTagged::DataMapType& mapleft=left->getTagLookup();
363    //  const DataTagged::DataMapType& mapright=right->getTagLookup();
364    //
365    //  if (mapleft.size()!=mapright.size())
366    //  {
367    //      cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
368    //      mismatch=true;
369    //      cout << "Left map\n";
370    //      DataTagged::DataMapType::const_iterator i,j;
371    //      for (i=mapleft.begin();i!=mapleft.end();++i) {
372    //          cout << "(" << i->first << "=>" << i->second << ")\n";
373    //      }
374    //      cout << "Right map\n";
375    //      for (i=mapright.begin();i!=mapright.end();++i) {
376    //          cout << "(" << i->first << "=>" << i->second << ")\n";
377    //      }
378    //      cout << "End map\n";
379    //
380    //  }
381    //
382    //  DataTagged::DataMapType::const_iterator i,j;
383    //  for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
384    //     if ((i->first!=j->first) || (i->second!=j->second))
385    //     {
386    //      cout << "(" << i->first << "=>" << i->second << ")";
387    //      cout << ":(" << j->first << "=>" << j->second << ") ";
388    //      mismatch=true;
389    //            }
390    //  }
391    //  if (mismatch)
392    //  {
393    //      cout << "#Mismatch\n";
394    //  }
395    // }
396    
397  escriptDataC  escriptDataC
398  Data::getDataC()  Data::getDataC()
399  {  {
# Line 227  Data::getDataC() const Line 413  Data::getDataC() const
413  const boost::python::tuple  const boost::python::tuple
414  Data::getShapeTuple() const  Data::getShapeTuple() const
415  {  {
416    const DataArrayView::ShapeType& shape=getDataPointShape();    const DataTypes::ShapeType& shape=getDataPointShape();
417    switch(getDataPointRank()) {    switch(getDataPointRank()) {
418       case 0:       case 0:
419          return make_tuple();          return make_tuple();
# Line 243  Data::getShapeTuple() const Line 429  Data::getShapeTuple() const
429          throw DataException("Error - illegal Data rank.");          throw DataException("Error - illegal Data rank.");
430    }    }
431  }  }
432    
433    
434    // The different name is needed because boost has trouble with overloaded functions.
435    // It can't work out what type the function is based soley on its name.
436    // There are ways to fix this involving creating function pointer variables for each form
437    // but there doesn't seem to be a need given that the methods have the same name from the python point of view
438    Data*
439    Data::copySelf()
440    {
441       DataAbstract* temp=m_data->deepCopy();
442       return new Data(temp);
443    }
444    
445  void  void
446  Data::copy(const Data& other)  Data::copy(const Data& other)
447  {  {
448    //    DataAbstract* temp=other.m_data->deepCopy();
449    // Perform a deep copy    DataAbstract_ptr p=temp->getPtr();
450    {    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.");  
451  }  }
452    
453    
454    Data
455    Data::delay()
456    {
457      DataLazy* dl=new DataLazy(m_data);
458      return Data(dl);
459    }
460    
461  void  void
462  Data::setToZero()  Data::delaySelf()
463  {  {
464      if (!isLazy())
465    {    {
466      DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());      m_data=(new DataLazy(m_data))->getPtr();
     if (temp!=0) {  
        temp->setToZero();  
        return;  
     }  
   }  
   {  
     DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());  
     if (temp!=0) {  
       temp->setToZero();  
       return;  
     }  
467    }    }
468    }
469    
470    void
471    Data::setToZero()
472    {
473      if (isEmpty())
474    {    {
475      DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());       throw DataException("Error - Operations not permitted on instances of DataEmpty.");
     if (temp!=0) {  
       temp->setToZero();  
       return;  
     }  
476    }    }
477    throw DataException("Error - Data can not be set to zero.");    m_data->setToZero();
478  }  }
479    
480  void  void
481  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
482                     const Data& mask)                     const Data& mask)
483  {  {
484    Data mask1;    // 1. Interpolate if required so all Datas use the same FS as this
485    Data mask2;    // 2. Tag or Expand so that all Data's are the same type
486      // 3. Iterate over the data vectors copying values where mask is >0
487      if (other.isEmpty() || mask.isEmpty())
488      {
489        throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");
490      }
491      Data other2(other);
492      Data mask2(mask);
493      other2.resolve();
494      mask2.resolve();
495      this->resolve();
496      FunctionSpace myFS=getFunctionSpace();
497      FunctionSpace oFS=other2.getFunctionSpace();
498      FunctionSpace mFS=mask2.getFunctionSpace();
499      if (oFS!=myFS)
500      {
501         if (other2.probeInterpolation(myFS))
502         {
503        other2=other2.interpolate(myFS);
504         }
505         else
506         {
507        throw DataException("Error - copyWithMask: other FunctionSpace is not compatible with this one.");
508         }
509      }
510      if (mFS!=myFS)
511      {
512         if (mask2.probeInterpolation(myFS))
513         {
514        mask2=mask2.interpolate(myFS);
515         }
516         else
517         {
518        throw DataException("Error - copyWithMask: mask FunctionSpace is not compatible with this one.");
519         }
520      }
521                // Ensure that all args have the same type
522      if (this->isExpanded() || mask2.isExpanded() || other2.isExpanded())
523      {
524        this->expand();
525        other2.expand();
526        mask2.expand();
527      }
528      else if (this->isTagged() || mask2.isTagged() || other2.isTagged())
529      {
530        this->tag();
531        other2.tag();
532        mask2.tag();
533      }
534      else if (this->isConstant() && mask2.isConstant() && other2.isConstant())
535      {
536      }
537      else
538      {
539        throw DataException("Error - Unknown DataAbstract passed to copyWithMask.");
540      }
541      // Now we iterate over the elements
542      DataVector& self=getReadyPtr()->getVector();
543      const DataVector& ovec=other2.getReadyPtr()->getVector();
544      const DataVector& mvec=mask2.getReadyPtr()->getVector();
545      if ((self.size()!=ovec.size()) || (self.size()!=mvec.size()))
546      {
547        throw DataException("Error - size mismatch in arguments to copyWithMask.");
548      }
549      size_t num_points=self.size();
550    
551    mask1 = mask.wherePositive();    // OPENMP 3.0 allows unsigned loop vars.
552    mask2.copy(mask1);  #if defined(_OPENMP) && (_OPENMP < 200805)
553      long i;
554    #else
555      size_t i;
556    #endif
557      #pragma omp parallel for private(i) schedule(static)
558      for (i=0;i<num_points;++i)
559      {
560        if (mvec[i]>0)
561        {
562           self[i]=ovec[i];
563        }
564      }
565    }
566    
   mask1 *= other;  
   mask2 *= *this;  
   mask2 = *this - mask2;  
567    
   *this = mask1 + mask2;  
 }  
568    
569  bool  bool
570  Data::isExpanded() const  Data::isExpanded() const
# Line 368  Data::isConstant() const Line 594  Data::isConstant() const
594    return (temp!=0);    return (temp!=0);
595  }  }
596    
597    bool
598    Data::isLazy() const
599    {
600      return m_data->isLazy();
601    }
602    
603    // at the moment this is synonymous with !isLazy() but that could change
604    bool
605    Data::isReady() const
606    {
607      return (dynamic_cast<DataReady*>(m_data.get())!=0);
608    }
609    
610    
611  void  void
612  Data::setProtection()  Data::setProtection()
613  {  {
# Line 388  Data::expand() Line 628  Data::expand()
628    if (isConstant()) {    if (isConstant()) {
629      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
630      DataAbstract* temp=new DataExpanded(*tempDataConst);      DataAbstract* temp=new DataExpanded(*tempDataConst);
631      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
632      m_data=temp_data;  //     m_data=temp_data;
633        m_data=temp->getPtr();
634    } else if (isTagged()) {    } else if (isTagged()) {
635      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
636      DataAbstract* temp=new DataExpanded(*tempDataTag);      DataAbstract* temp=new DataExpanded(*tempDataTag);
637      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
638      m_data=temp_data;  //     m_data=temp_data;
639        m_data=temp->getPtr();
640    } else if (isExpanded()) {    } else if (isExpanded()) {
641      //      //
642      // do nothing      // do nothing
643    } else if (isEmpty()) {    } else if (isEmpty()) {
644      throw DataException("Error - Expansion of DataEmpty not possible.");      throw DataException("Error - Expansion of DataEmpty not possible.");
645      } else if (isLazy()) {
646        resolve();
647        expand();       // resolve might not give us expanded data
648    } else {    } else {
649      throw DataException("Error - Expansion not implemented for this Data type.");      throw DataException("Error - Expansion not implemented for this Data type.");
650    }    }
# Line 411  Data::tag() Line 656  Data::tag()
656    if (isConstant()) {    if (isConstant()) {
657      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
658      DataAbstract* temp=new DataTagged(*tempDataConst);      DataAbstract* temp=new DataTagged(*tempDataConst);
659      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
660      m_data=temp_data;  //     m_data=temp_data;
661        m_data=temp->getPtr();
662    } else if (isTagged()) {    } else if (isTagged()) {
663      // do nothing      // do nothing
664    } else if (isExpanded()) {    } else if (isExpanded()) {
665      throw DataException("Error - Creating tag data from DataExpanded not possible.");      throw DataException("Error - Creating tag data from DataExpanded not possible.");
666    } else if (isEmpty()) {    } else if (isEmpty()) {
667      throw DataException("Error - Creating tag data from DataEmpty not possible.");      throw DataException("Error - Creating tag data from DataEmpty not possible.");
668      } else if (isLazy()) {
669         DataAbstract_ptr res=m_data->resolve();
670         if (m_data->isExpanded())
671         {
672        throw DataException("Error - data would resolve to DataExpanded, tagging is not possible.");
673         }
674         m_data=res;    
675         tag();
676    } else {    } else {
677      throw DataException("Error - Tagging not implemented for this Data type.");      throw DataException("Error - Tagging not implemented for this Data type.");
678    }    }
679  }  }
680    
681    void
682    Data::resolve()
683    {
684      if (isLazy())
685      {
686         m_data=m_data->resolve();
687      }
688    }
689    
690    
691  Data  Data
692  Data::oneOver() const  Data::oneOver() const
693  {  {
694      if (isLazy())
695      {
696        DataLazy* c=new DataLazy(borrowDataPtr(),RECIP);
697        return Data(c);
698      }
699    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
700  }  }
701    
702  Data  Data
703  Data::wherePositive() const  Data::wherePositive() const
704  {  {
705      if (isLazy())
706      {
707        DataLazy* c=new DataLazy(borrowDataPtr(),GZ);
708        return Data(c);
709      }
710    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
711  }  }
712    
713  Data  Data
714  Data::whereNegative() const  Data::whereNegative() const
715  {  {
716      if (isLazy())
717      {
718        DataLazy* c=new DataLazy(borrowDataPtr(),LZ);
719        return Data(c);
720      }
721    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
722  }  }
723    
724  Data  Data
725  Data::whereNonNegative() const  Data::whereNonNegative() const
726  {  {
727      if (isLazy())
728      {
729        DataLazy* c=new DataLazy(borrowDataPtr(),GEZ);
730        return Data(c);
731      }
732    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
733  }  }
734    
735  Data  Data
736  Data::whereNonPositive() const  Data::whereNonPositive() const
737  {  {
738      if (isLazy())
739      {
740        DataLazy* c=new DataLazy(borrowDataPtr(),LEZ);
741        return Data(c);
742      }
743    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
744  }  }
745    
# Line 477  Data::interpolate(const FunctionSpace& f Line 766  Data::interpolate(const FunctionSpace& f
766  bool  bool
767  Data::probeInterpolation(const FunctionSpace& functionspace) const  Data::probeInterpolation(const FunctionSpace& functionspace) const
768  {  {
769    if (getFunctionSpace()==functionspace) {    return getFunctionSpace().probeInterpolation(functionspace);
770      return true;  //   if (getFunctionSpace()==functionspace) {
771    } else {  //     return true;
772      const AbstractDomain& domain=getDomain();  //   } else {
773      if  (domain==functionspace.getDomain()) {  //     const_Domain_ptr domain=getDomain();
774        return domain.probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());  //     if  (*domain==*functionspace.getDomain()) {
775      } else {  //       return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
776        return domain.probeInterpolationACross(getFunctionSpace().getTypeCode(),functionspace.getDomain(),functionspace.getTypeCode());  //     } else {
777      }  //       return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());
778    }  //     }
779    //   }
780  }  }
781    
782  Data  Data
783  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
784  {  {
785      if (isEmpty())
786      {
787        throw DataException("Error - operation not permitted on instances of DataEmpty.");
788      }
789    double blocktimer_start = blocktimer_time();    double blocktimer_start = blocktimer_time();
790    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
791      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
792    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
793    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
794    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
795    getDomain().setToGradient(out,*this);    getDomain()->setToGradient(out,*this);
796    blocktimer_increment("grad()", blocktimer_start);    blocktimer_increment("grad()", blocktimer_start);
797    return out;    return out;
798  }  }
# Line 506  Data::gradOn(const FunctionSpace& functi Line 800  Data::gradOn(const FunctionSpace& functi
800  Data  Data
801  Data::grad() const  Data::grad() const
802  {  {
803    return gradOn(escript::function(getDomain()));    if (isEmpty())
804      {
805        throw DataException("Error - operation not permitted on instances of DataEmpty.");
806      }
807      return gradOn(escript::function(*getDomain()));
808  }  }
809    
810  int  int
811  Data::getDataPointSize() const  Data::getDataPointSize() const
812  {  {
813    return getPointDataView().noValues();    return m_data->getNoValues();
814  }  }
815    
816  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
817  Data::getLength() const  Data::getLength() const
818  {  {
819    return m_data->getLength();    return m_data->getLength();
820  }  }
821    
 const DataArrayView::ShapeType&  
 Data::getDataPointShape() const  
 {  
   return getPointDataView().getShape();  
 }  
   
   
   
822  const  const
823  boost::python::numeric::array  boost::python::numeric::array
824  Data:: getValueOfDataPoint(int dataPointNo)  Data:: getValueOfDataPoint(int dataPointNo)
825  {  {
   size_t length=0;  
826    int i, j, k, l;    int i, j, k, l;
827    
828      FORCERESOLVE;
829    
830    //    //
831    // determine the rank and shape of each data point    // determine the rank and shape of each data point
832    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
833    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
834    
835    //    //
836    // create the numeric array to be returned    // create the numeric array to be returned
# Line 548  Data:: getValueOfDataPoint(int dataPoint Line 840  Data:: getValueOfDataPoint(int dataPoint
840    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
841    // as that of the data point    // as that of the data point
842    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
843    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
844    
845    //    //
846    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 584  Data:: getValueOfDataPoint(int dataPoint Line 876  Data:: getValueOfDataPoint(int dataPoint
876         }         }
877         // TODO: global error handling         // TODO: global error handling
878         // create a view of the data if it is stored locally         // create a view of the data if it is stored locally
879         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);  //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
880           DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
881    
882    
883         switch( dataPointRank ){         switch( dataPointRank ){
884              case 0 :              case 0 :
885                  numArray[0] = dataPointView();                  numArray[0] = getDataAtOffset(offset);
886                  break;                  break;
887              case 1 :              case 1 :
888                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
889                      numArray[i]=dataPointView(i);                      numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
890                  break;                  break;
891              case 2 :              case 2 :
892                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
893                      for( j=0; j<dataPointShape[1]; j++)                      for( j=0; j<dataPointShape[1]; j++)
894                          numArray[make_tuple(i,j)]=dataPointView(i,j);                          numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
895                  break;                  break;
896              case 3 :              case 3 :
897                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
898                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
899                          for( k=0; k<dataPointShape[2]; k++)                          for( k=0; k<dataPointShape[2]; k++)
900                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);                              numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
901                  break;                  break;
902              case 4 :              case 4 :
903                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
904                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
905                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
906                              for( l=0; l<dataPointShape[3]; l++)                              for( l=0; l<dataPointShape[3]; l++)
907                                  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));
908                  break;                  break;
909      }      }
910    }    }
# Line 619  Data:: getValueOfDataPoint(int dataPoint Line 913  Data:: getValueOfDataPoint(int dataPoint
913    return numArray;    return numArray;
914    
915  }  }
916    
917  void  void
918  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
919  {  {
920      // this will throw if the value cannot be represented      // this will throw if the value cannot be represented
921      boost::python::numeric::array num_array(py_object);      boost::python::numeric::array num_array(py_object);
922      setValueOfDataPointToArray(dataPointNo,num_array);      setValueOfDataPointToArray(dataPointNo,num_array);
   
   
923  }  }
924    
925  void  void
# Line 635  Data::setValueOfDataPointToArray(int dat Line 928  Data::setValueOfDataPointToArray(int dat
928    if (isProtected()) {    if (isProtected()) {
929          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
930    }    }
931      FORCERESOLVE;
932    //    //
933    // check rank    // check rank
934    if (num_array.getrank()<getDataPointRank())    if (static_cast<unsigned int>(num_array.getrank())<getDataPointRank())
935        throw DataException("Rank of numarray does not match Data object rank");        throw DataException("Rank of numarray does not match Data object rank");
936    
937    //    //
938    // check shape of num_array    // check shape of num_array
939    for (int i=0; i<getDataPointRank(); i++) {    for (unsigned int i=0; i<getDataPointRank(); i++) {
940      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
941         throw DataException("Shape of numarray does not match Data object rank");         throw DataException("Shape of numarray does not match Data object rank");
942    }    }
943    //    //
944    // make sure data is expanded:    // make sure data is expanded:
945      //
946    if (!isExpanded()) {    if (!isExpanded()) {
947      expand();      expand();
948    }    }
# Line 668  Data::setValueOfDataPoint(int dataPointN Line 963  Data::setValueOfDataPoint(int dataPointN
963    }    }
964    //    //
965    // make sure data is expanded:    // make sure data is expanded:
966      FORCERESOLVE;
967    if (!isExpanded()) {    if (!isExpanded()) {
968      expand();      expand();
969    }    }
# Line 686  Data::getValueOfGlobalDataPoint(int proc Line 982  Data::getValueOfGlobalDataPoint(int proc
982  {  {
983    size_t length=0;    size_t length=0;
984    int i, j, k, l, pos;    int i, j, k, l, pos;
985      FORCERESOLVE;
986    //    //
987    // determine the rank and shape of each data point    // determine the rank and shape of each data point
988    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
989    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
990    
991    //    //
992    // create the numeric array to be returned    // create the numeric array to be returned
# Line 699  Data::getValueOfGlobalDataPoint(int proc Line 996  Data::getValueOfGlobalDataPoint(int proc
996    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
997    // as that of the data point    // as that of the data point
998    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
999    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
1000    
1001    //    //
1002    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 745  Data::getValueOfGlobalDataPoint(int proc Line 1042  Data::getValueOfGlobalDataPoint(int proc
1042                  }                  }
1043                  // TODO: global error handling                  // TODO: global error handling
1044          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
1045          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
1046            DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
1047    
1048          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
1049          pos=0;          pos=0;
1050          switch( dataPointRank ){          switch( dataPointRank ){
1051              case 0 :              case 0 :
1052                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
1053                  break;                  break;
1054              case 1 :              case 1 :
1055                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1056                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
1057                  break;                  break;
1058              case 2 :              case 2 :
1059                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1060                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
1061                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
1062                  break;                  break;
1063              case 3 :              case 3 :
1064                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1065                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
1066                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
1067                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
1068                  break;                  break;
1069              case 4 :              case 4 :
1070                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1071                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
1072                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
1073                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
1074                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
1075                  break;                  break;
1076          }          }
1077              }              }
# Line 819  Data::getValueOfGlobalDataPoint(int proc Line 1117  Data::getValueOfGlobalDataPoint(int proc
1117  }  }
1118    
1119    
1120    boost::python::numeric::array
1121    Data::integrate_const() const
1122    {
1123      if (isLazy())
1124      {
1125        throw DataException("Error - cannot integrate for constant lazy data.");
1126      }
1127      return integrateWorker();
1128    }
1129    
1130  boost::python::numeric::array  boost::python::numeric::array
1131  Data::integrate() const  Data::integrate()
1132    {
1133      if (isLazy())
1134      {
1135        expand();
1136      }
1137      return integrateWorker();
1138    }
1139    
1140    
1141    
1142    boost::python::numeric::array
1143    Data::integrateWorker() const
1144  {  {
1145    int index;    int index;
1146    int rank = getDataPointRank();    int rank = getDataPointRank();
1147    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
1148    int dataPointSize = getDataPointSize();    int dataPointSize = getDataPointSize();
1149    
1150    //    //
# Line 833  Data::integrate() const Line 1152  Data::integrate() const
1152    vector<double> integrals(dataPointSize);    vector<double> integrals(dataPointSize);
1153    vector<double> integrals_local(dataPointSize);    vector<double> integrals_local(dataPointSize);
1154  #ifdef PASO_MPI  #ifdef PASO_MPI
1155    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals_local,*this);    AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals_local,*this);
1156    // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory    // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
1157    double *tmp = new double[dataPointSize];    double *tmp = new double[dataPointSize];
1158    double *tmp_local = new double[dataPointSize];    double *tmp_local = new double[dataPointSize];
# Line 843  Data::integrate() const Line 1162  Data::integrate() const
1162    delete[] tmp;    delete[] tmp;
1163    delete[] tmp_local;    delete[] tmp_local;
1164  #else  #else
1165    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals,*this);
1166  #endif  #endif
1167    
1168    //    //
# Line 904  Data::integrate() const Line 1223  Data::integrate() const
1223  Data  Data
1224  Data::sin() const  Data::sin() const
1225  {  {
1226      if (isLazy())
1227      {
1228        DataLazy* c=new DataLazy(borrowDataPtr(),SIN);
1229        return Data(c);
1230      }
1231    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1232  }  }
1233    
1234  Data  Data
1235  Data::cos() const  Data::cos() const
1236  {  {
1237      if (isLazy())
1238      {
1239        DataLazy* c=new DataLazy(borrowDataPtr(),COS);
1240        return Data(c);
1241      }
1242    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1243  }  }
1244    
1245  Data  Data
1246  Data::tan() const  Data::tan() const
1247  {  {
1248      if (isLazy())
1249      {
1250        DataLazy* c=new DataLazy(borrowDataPtr(),TAN);
1251        return Data(c);
1252      }
1253    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1254  }  }
1255    
1256  Data  Data
1257  Data::asin() const  Data::asin() const
1258  {  {
1259    return C_TensorUnaryOperation(*this, ::asin);    if (isLazy())
1260      {
1261        DataLazy* c=new DataLazy(borrowDataPtr(),ASIN);
1262        return Data(c);
1263      }
1264      return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1265  }  }
1266    
1267  Data  Data
1268  Data::acos() const  Data::acos() const
1269  {  {
1270    return C_TensorUnaryOperation(*this, ::acos);    if (isLazy())
1271      {
1272        DataLazy* c=new DataLazy(borrowDataPtr(),ACOS);
1273        return Data(c);
1274      }
1275      return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1276  }  }
1277    
1278    
1279  Data  Data
1280  Data::atan() const  Data::atan() const
1281  {  {
1282    return C_TensorUnaryOperation(*this, ::atan);    if (isLazy())
1283      {
1284        DataLazy* c=new DataLazy(borrowDataPtr(),ATAN);
1285        return Data(c);
1286      }
1287      return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1288  }  }
1289    
1290  Data  Data
1291  Data::sinh() const  Data::sinh() const
1292  {  {
1293      return C_TensorUnaryOperation(*this, ::sinh);    if (isLazy())
1294      {
1295        DataLazy* c=new DataLazy(borrowDataPtr(),SINH);
1296        return Data(c);
1297      }
1298      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1299  }  }
1300    
1301  Data  Data
1302  Data::cosh() const  Data::cosh() const
1303  {  {
1304      return C_TensorUnaryOperation(*this, ::cosh);    if (isLazy())
1305      {
1306        DataLazy* c=new DataLazy(borrowDataPtr(),COSH);
1307        return Data(c);
1308      }
1309      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1310  }  }
1311    
1312  Data  Data
1313  Data::tanh() const  Data::tanh() const
1314  {  {
1315      return C_TensorUnaryOperation(*this, ::tanh);    if (isLazy())
1316      {
1317        DataLazy* c=new DataLazy(borrowDataPtr(),TANH);
1318        return Data(c);
1319      }
1320      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1321  }  }
1322    
1323    
# Line 964  Data::erf() const Line 1327  Data::erf() const
1327  #ifdef _WIN32  #ifdef _WIN32
1328    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1329  #else  #else
1330      if (isLazy())
1331      {
1332        DataLazy* c=new DataLazy(borrowDataPtr(),ERF);
1333        return Data(c);
1334      }
1335    return C_TensorUnaryOperation(*this, ::erf);    return C_TensorUnaryOperation(*this, ::erf);
1336  #endif  #endif
1337  }  }
# Line 971  Data::erf() const Line 1339  Data::erf() const
1339  Data  Data
1340  Data::asinh() const  Data::asinh() const
1341  {  {
1342      if (isLazy())
1343      {
1344        DataLazy* c=new DataLazy(borrowDataPtr(),ASINH);
1345        return Data(c);
1346      }
1347  #ifdef _WIN32  #ifdef _WIN32
1348    return C_TensorUnaryOperation(*this, escript::asinh_substitute);    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1349  #else  #else
# Line 981  Data::asinh() const Line 1354  Data::asinh() const
1354  Data  Data
1355  Data::acosh() const  Data::acosh() const
1356  {  {
1357      if (isLazy())
1358      {
1359        DataLazy* c=new DataLazy(borrowDataPtr(),ACOSH);
1360        return Data(c);
1361      }
1362  #ifdef _WIN32  #ifdef _WIN32
1363    return C_TensorUnaryOperation(*this, escript::acosh_substitute);    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1364  #else  #else
# Line 991  Data::acosh() const Line 1369  Data::acosh() const
1369  Data  Data
1370  Data::atanh() const  Data::atanh() const
1371  {  {
1372      if (isLazy())
1373      {
1374        DataLazy* c=new DataLazy(borrowDataPtr(),ATANH);
1375        return Data(c);
1376      }
1377  #ifdef _WIN32  #ifdef _WIN32
1378    return C_TensorUnaryOperation(*this, escript::atanh_substitute);    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1379  #else  #else
# Line 1000  Data::atanh() const Line 1383  Data::atanh() const
1383    
1384  Data  Data
1385  Data::log10() const  Data::log10() const
1386  {  {  if (isLazy())
1387    return C_TensorUnaryOperation(*this, ::log10);    {
1388        DataLazy* c=new DataLazy(borrowDataPtr(),LOG10);
1389        return Data(c);
1390      }
1391      return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1392  }  }
1393    
1394  Data  Data
1395  Data::log() const  Data::log() const
1396  {  {
1397    return C_TensorUnaryOperation(*this, ::log);    if (isLazy())
1398      {
1399        DataLazy* c=new DataLazy(borrowDataPtr(),LOG);
1400        return Data(c);
1401      }
1402      return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1403  }  }
1404    
1405  Data  Data
1406  Data::sign() const  Data::sign() const
1407  {  {
1408      if (isLazy())
1409      {
1410        DataLazy* c=new DataLazy(borrowDataPtr(),SIGN);
1411        return Data(c);
1412      }
1413    return C_TensorUnaryOperation(*this, escript::fsign);    return C_TensorUnaryOperation(*this, escript::fsign);
1414  }  }
1415    
1416  Data  Data
1417  Data::abs() const  Data::abs() const
1418  {  {
1419    return C_TensorUnaryOperation(*this, ::fabs);    if (isLazy())
1420      {
1421        DataLazy* c=new DataLazy(borrowDataPtr(),ABS);
1422        return Data(c);
1423      }
1424      return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1425  }  }
1426    
1427  Data  Data
1428  Data::neg() const  Data::neg() const
1429  {  {
1430      if (isLazy())
1431      {
1432        DataLazy* c=new DataLazy(borrowDataPtr(),NEG);
1433        return Data(c);
1434      }
1435    return C_TensorUnaryOperation(*this, negate<double>());    return C_TensorUnaryOperation(*this, negate<double>());
1436  }  }
1437    
1438  Data  Data
1439  Data::pos() const  Data::pos() const
1440  {  {
1441        // not doing lazy check here is deliberate.
1442        // since a deep copy of lazy data should be cheap, I'll just let it happen now
1443    Data result;    Data result;
1444    // perform a deep copy    // perform a deep copy
1445    result.copy(*this);    result.copy(*this);
# Line 1039  Data::pos() const Line 1448  Data::pos() const
1448    
1449  Data  Data
1450  Data::exp() const  Data::exp() const
1451  {  {  
1452    return C_TensorUnaryOperation(*this, ::exp);    if (isLazy())
1453      {
1454        DataLazy* c=new DataLazy(borrowDataPtr(),EXP);
1455        return Data(c);
1456      }
1457      return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1458  }  }
1459    
1460  Data  Data
1461  Data::sqrt() const  Data::sqrt() const
1462  {  {
1463    return C_TensorUnaryOperation(*this, ::sqrt);    if (isLazy())
1464      {
1465        DataLazy* c=new DataLazy(borrowDataPtr(),SQRT);
1466        return Data(c);
1467      }
1468      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1469    }
1470    
1471    double
1472    Data::Lsup_const() const
1473    {
1474       if (isLazy())
1475       {
1476        throw DataException("Error - cannot compute Lsup for constant lazy data.");
1477       }
1478       return LsupWorker();
1479    }
1480    
1481    double
1482    Data::Lsup()
1483    {
1484       if (isLazy())
1485       {
1486        expand();
1487       }
1488       return LsupWorker();
1489    }
1490    
1491    double
1492    Data::sup_const() const
1493    {
1494       if (isLazy())
1495       {
1496        throw DataException("Error - cannot compute sup for constant lazy data.");
1497       }
1498       return supWorker();
1499    }
1500    
1501    double
1502    Data::sup()
1503    {
1504       if (isLazy())
1505       {
1506        expand();
1507       }
1508       return supWorker();
1509    }
1510    
1511    double
1512    Data::inf_const() const
1513    {
1514       if (isLazy())
1515       {
1516        throw DataException("Error - cannot compute inf for constant lazy data.");
1517       }
1518       return infWorker();
1519  }  }
1520    
1521  double  double
1522  Data::Lsup() const  Data::inf()
1523  {  {
1524    double localValue, globalValue;     if (isLazy())
1525       {
1526        expand();
1527       }
1528       return infWorker();
1529    }
1530    
1531    double
1532    Data::LsupWorker() const
1533    {
1534      double localValue;
1535    //    //
1536    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1537    
1538    AbsMax abs_max_func;    AbsMax abs_max_func;
1539    localValue = algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1540  #ifdef PASO_MPI  #ifdef PASO_MPI
1541      double globalValue;
1542    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1543    return globalValue;    return globalValue;
1544  #else  #else
# Line 1067  Data::Lsup() const Line 1547  Data::Lsup() const
1547  }  }
1548    
1549  double  double
1550  Data::sup() const  Data::supWorker() const
1551  {  {
1552    double localValue, globalValue;    double localValue;
1553    //    //
1554    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1555    FMax fmax_func;    FMax fmax_func;
1556    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1557  #ifdef PASO_MPI  #ifdef PASO_MPI
1558      double globalValue;
1559    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1560    return globalValue;    return globalValue;
1561  #else  #else
# Line 1083  Data::sup() const Line 1564  Data::sup() const
1564  }  }
1565    
1566  double  double
1567  Data::inf() const  Data::infWorker() const
1568  {  {
1569    double localValue, globalValue;    double localValue;
1570    //    //
1571    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1572    FMin fmin_func;    FMin fmin_func;
1573    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1574  #ifdef PASO_MPI  #ifdef PASO_MPI
1575      double globalValue;
1576    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1577    return globalValue;    return globalValue;
1578  #else  #else
# Line 1103  Data::inf() const Line 1585  Data::inf() const
1585  Data  Data
1586  Data::maxval() const  Data::maxval() const
1587  {  {
1588      if (isLazy())
1589      {
1590        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1591        temp.resolve();
1592        return temp.maxval();
1593      }
1594    //    //
1595    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1596    FMax fmax_func;    FMax fmax_func;
# Line 1112  Data::maxval() const Line 1600  Data::maxval() const
1600  Data  Data
1601  Data::minval() const  Data::minval() const
1602  {  {
1603      if (isLazy())
1604      {
1605        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1606        temp.resolve();
1607        return temp.minval();
1608      }
1609    //    //
1610    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1611    FMin fmin_func;    FMin fmin_func;
# Line 1122  Data Line 1616  Data
1616  Data::swapaxes(const int axis0, const int axis1) const  Data::swapaxes(const int axis0, const int axis1) const
1617  {  {
1618       int axis0_tmp,axis1_tmp;       int axis0_tmp,axis1_tmp;
1619       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1620       DataArrayView::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1621       // 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]
1622       // 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)
1623       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1166  Data Line 1660  Data
1660  Data::symmetric() const  Data::symmetric() const
1661  {  {
1662       // check input       // check input
1663       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1664       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1665          if(s[0] != s[1])          if(s[0] != s[1])
1666             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.");
# Line 1178  Data::symmetric() const Line 1672  Data::symmetric() const
1672       else {       else {
1673          throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");          throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1674       }       }
1675         if (isLazy())
1676         {
1677        DataLazy* c=new DataLazy(borrowDataPtr(),SYM);
1678        return Data(c);
1679         }
1680       Data ev(0.,getDataPointShape(),getFunctionSpace());       Data ev(0.,getDataPointShape(),getFunctionSpace());
1681       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1682       m_data->symmetric(ev.m_data.get());       m_data->symmetric(ev.m_data.get());
# Line 1187  Data::symmetric() const Line 1686  Data::symmetric() const
1686  Data  Data
1687  Data::nonsymmetric() const  Data::nonsymmetric() const
1688  {  {
1689         if (isLazy())
1690         {
1691        DataLazy* c=new DataLazy(borrowDataPtr(),NSYM);
1692        return Data(c);
1693         }
1694       // check input       // check input
1695       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1696       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1697          if(s[0] != s[1])          if(s[0] != s[1])
1698             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.");
1699          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1700          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1701          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1702          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1203  Data::nonsymmetric() const Line 1707  Data::nonsymmetric() const
1707       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1708          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1709             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.");
1710          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1711          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1712          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1713          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1218  Data::nonsymmetric() const Line 1722  Data::nonsymmetric() const
1722       }       }
1723  }  }
1724    
1725    
1726    // Doing a lazy version of this would require some thought.
1727    // First it needs a parameter (which DataLazy doesn't support at the moment).
1728    // (secondly although it does not apply to trace) we can't handle operations which return
1729    // multiple results (like eigenvectors_values) or return values of different shapes to their input
1730    // (like eigenvalues).
1731  Data  Data
1732  Data::trace(int axis_offset) const  Data::trace(int axis_offset) const
1733  {  {
1734       DataArrayView::ShapeType s=getDataPointShape();       if (isLazy())
1735         {
1736        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1737        temp.resolve();
1738        return temp.trace(axis_offset);
1739         }
1740         DataTypes::ShapeType s=getDataPointShape();
1741       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1742          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1743          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1744          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1745          m_data->trace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1746          return ev;          return ev;
1747       }       }
1748       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1749          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1750          if (axis_offset==0) {          if (axis_offset==0) {
1751            int s2=s[2];            int s2=s[2];
1752            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1245  Data::trace(int axis_offset) const Line 1761  Data::trace(int axis_offset) const
1761          return ev;          return ev;
1762       }       }
1763       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1764          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1765          if (axis_offset==0) {          if (axis_offset==0) {
1766            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1767            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1270  Data::trace(int axis_offset) const Line 1786  Data::trace(int axis_offset) const
1786    
1787  Data  Data
1788  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1789  {  {    
1790       DataArrayView::ShapeType s=getDataPointShape();       if (isLazy())
1791       DataArrayView::ShapeType ev_shape;       {
1792        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1793        temp.resolve();
1794        return temp.transpose(axis_offset);
1795         }
1796         DataTypes::ShapeType s=getDataPointShape();
1797         DataTypes::ShapeType ev_shape;
1798       // 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]
1799       // 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)
1800       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1292  Data::transpose(int axis_offset) const Line 1814  Data::transpose(int axis_offset) const
1814  Data  Data
1815  Data::eigenvalues() const  Data::eigenvalues() const
1816  {  {
1817         if (isLazy())
1818         {
1819        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1820        temp.resolve();
1821        return temp.eigenvalues();
1822         }
1823       // check input       // check input
1824       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1825       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1826          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.");
1827       if(s[0] != s[1])       if(s[0] != s[1])
1828          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.");
1829       // create return       // create return
1830       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1831       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1832       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1833       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1309  Data::eigenvalues() const Line 1837  Data::eigenvalues() const
1837  const boost::python::tuple  const boost::python::tuple
1838  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1839  {  {
1840       DataArrayView::ShapeType s=getDataPointShape();       if (isLazy())
1841         {
1842        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1843        temp.resolve();
1844        return temp.eigenvalues_and_eigenvectors(tol);
1845         }
1846         DataTypes::ShapeType s=getDataPointShape();
1847       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1848          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.");
1849       if(s[0] != s[1])       if(s[0] != s[1])
1850          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.");
1851       // create return       // create return
1852       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1853       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1854       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1855       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1856       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1857       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1858       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 1342  void Line 1876  void
1876  Data::calc_minGlobalDataPoint(int& ProcNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1877                          int& DataPointNo) const                          int& DataPointNo) const
1878  {  {
1879      if (isLazy())
1880      {
1881        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1882        temp.resolve();
1883        return temp.calc_minGlobalDataPoint(ProcNo,DataPointNo);
1884      }
1885    int i,j;    int i,j;
1886    int lowi=0,lowj=0;    int lowi=0,lowj=0;
1887    double min=numeric_limits<double>::max();    double min=numeric_limits<double>::max();
# Line 1352  Data::calc_minGlobalDataPoint(int& ProcN Line 1892  Data::calc_minGlobalDataPoint(int& ProcN
1892    int numDPPSample=temp.getNumDataPointsPerSample();    int numDPPSample=temp.getNumDataPointsPerSample();
1893    
1894    double next,local_min;    double next,local_min;
1895    int local_lowi,local_lowj;    int local_lowi=0,local_lowj=0;    
1896    
1897    #pragma omp parallel private(next,local_min,local_lowi,local_lowj)    #pragma omp parallel private(next,local_min,local_lowi,local_lowj)
1898    {    {
# Line 1360  Data::calc_minGlobalDataPoint(int& ProcN Line 1900  Data::calc_minGlobalDataPoint(int& ProcN
1900      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1901      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1902        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1903          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1904          if (next<local_min) {          if (next<local_min) {
1905            local_min=next;            local_min=next;
1906            local_lowi=i;            local_lowi=i;
# Line 1378  Data::calc_minGlobalDataPoint(int& ProcN Line 1918  Data::calc_minGlobalDataPoint(int& ProcN
1918    
1919  #ifdef PASO_MPI  #ifdef PASO_MPI
1920      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1921      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1922      int lowProc = 0;      int lowProc = 0;
1923      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1924      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() );
# Line 1404  Data::calc_minGlobalDataPoint(int& ProcN Line 1944  Data::calc_minGlobalDataPoint(int& ProcN
1944  void  void
1945  Data::saveDX(std::string fileName) const  Data::saveDX(std::string fileName) const
1946  {  {
1947      if (isEmpty())
1948      {
1949        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1950      }
1951      if (isLazy())
1952      {
1953         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1954         temp.resolve();
1955         temp.saveDX(fileName);
1956         return;
1957      }
1958    boost::python::dict args;    boost::python::dict args;
1959    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1960    getDomain().saveDX(fileName,args);    getDomain()->saveDX(fileName,args);
1961    return;    return;
1962  }  }
1963    
1964  void  void
1965  Data::saveVTK(std::string fileName) const  Data::saveVTK(std::string fileName) const
1966  {  {
1967      if (isEmpty())
1968      {
1969        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1970      }
1971      if (isLazy())
1972      {
1973         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1974         temp.resolve();
1975         temp.saveVTK(fileName);
1976         return;
1977      }
1978    boost::python::dict args;    boost::python::dict args;
1979    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1980    getDomain().saveVTK(fileName,args);    getDomain()->saveVTK(fileName,args);
1981    return;    return;
1982  }  }
1983    
# Line 1425  Data::operator+=(const Data& right) Line 1987  Data::operator+=(const Data& right)
1987    if (isProtected()) {    if (isProtected()) {
1988          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1989    }    }
1990    binaryOp(right,plus<double>());    if (isLazy() || right.isLazy())
1991    return (*this);    {
1992        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),ADD); // for lazy + is equivalent to +=
1993            m_data=c->getPtr();
1994        return (*this);
1995      }
1996      else
1997      {
1998        binaryOp(right,plus<double>());
1999        return (*this);
2000      }
2001  }  }
2002    
2003  Data&  Data&
2004  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
2005  {  {
2006      if (isProtected()) {
2007            throw DataException("Error - attempt to update protected Data object.");
2008      }
2009    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2010    binaryOp(tmp,plus<double>());    if (isLazy())
2011    return (*this);    {
2012        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),ADD);   // for lazy + is equivalent to +=
2013            m_data=c->getPtr();
2014        return (*this);
2015      }
2016      else
2017      {
2018        binaryOp(tmp,plus<double>());
2019        return (*this);
2020      }
2021  }  }
2022    
2023    // Hmmm, operator= makes a deep copy but the copy constructor does not?
2024  Data&  Data&
2025  Data::operator=(const Data& other)  Data::operator=(const Data& other)
2026  {  {
# Line 1449  Data::operator-=(const Data& right) Line 2034  Data::operator-=(const Data& right)
2034    if (isProtected()) {    if (isProtected()) {
2035          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2036    }    }
2037    binaryOp(right,minus<double>());    if (isLazy() || right.isLazy())
2038    return (*this);    {
2039        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),SUB); // for lazy - is equivalent to -=
2040            m_data=c->getPtr();
2041        return (*this);
2042      }
2043      else
2044      {
2045        binaryOp(right,minus<double>());
2046        return (*this);
2047      }
2048  }  }
2049    
2050  Data&  Data&
2051  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
2052  {  {
2053      if (isProtected()) {
2054            throw DataException("Error - attempt to update protected Data object.");
2055      }
2056    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2057    binaryOp(tmp,minus<double>());    if (isLazy())
2058    return (*this);    {
2059        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),SUB);   // for lazy - is equivalent to -=
2060            m_data=c->getPtr();
2061        return (*this);
2062      }
2063      else
2064      {
2065        binaryOp(tmp,minus<double>());
2066        return (*this);
2067      }
2068  }  }
2069    
2070  Data&  Data&
# Line 1467  Data::operator*=(const Data& right) Line 2073  Data::operator*=(const Data& right)
2073    if (isProtected()) {    if (isProtected()) {
2074          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2075    }    }
2076    binaryOp(right,multiplies<double>());    if (isLazy() || right.isLazy())
2077    return (*this);    {
2078        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),MUL); // for lazy * is equivalent to *=
2079            m_data=c->getPtr();
2080        return (*this);
2081      }
2082      else
2083      {
2084        binaryOp(right,multiplies<double>());
2085        return (*this);
2086      }
2087  }  }
2088    
2089  Data&  Data&
2090  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
2091  {  {  
2092      if (isProtected()) {
2093            throw DataException("Error - attempt to update protected Data object.");
2094      }
2095    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2096    binaryOp(tmp,multiplies<double>());    if (isLazy())
2097    return (*this);    {
2098        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),MUL);   // for lazy * is equivalent to *=
2099            m_data=c->getPtr();
2100        return (*this);
2101      }
2102      else
2103      {
2104        binaryOp(tmp,multiplies<double>());
2105        return (*this);
2106      }
2107  }  }
2108    
2109  Data&  Data&
# Line 1485  Data::operator/=(const Data& right) Line 2112  Data::operator/=(const Data& right)
2112    if (isProtected()) {    if (isProtected()) {
2113          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2114    }    }
2115    binaryOp(right,divides<double>());    if (isLazy() || right.isLazy())
2116    return (*this);    {
2117        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),DIV); // for lazy / is equivalent to /=
2118            m_data=c->getPtr();
2119        return (*this);
2120      }
2121      else
2122      {
2123        binaryOp(right,divides<double>());
2124        return (*this);
2125      }
2126  }  }
2127    
2128  Data&  Data&
2129  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
2130  {  {
2131      if (isProtected()) {
2132            throw DataException("Error - attempt to update protected Data object.");
2133      }
2134    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2135    binaryOp(tmp,divides<double>());    if (isLazy())
2136    return (*this);    {
2137        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),DIV);   // for lazy / is equivalent to /=
2138            m_data=c->getPtr();
2139        return (*this);
2140      }
2141      else
2142      {
2143        binaryOp(tmp,divides<double>());
2144        return (*this);
2145      }
2146  }  }
2147    
2148  Data  Data
# Line 1514  Data::powO(const boost::python::object& Line 2162  Data::powO(const boost::python::object&
2162  Data  Data
2163  Data::powD(const Data& right) const  Data::powD(const Data& right) const
2164  {  {
2165    return C_TensorBinaryOperation(*this, right, ::pow);    if (isLazy() || right.isLazy())
2166      {
2167        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),POW);
2168        return Data(c);
2169      }
2170      return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
2171  }  }
2172    
2173  //  //
# Line 1522  Data::powD(const Data& right) const Line 2175  Data::powD(const Data& right) const
2175  Data  Data
2176  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
2177  {  {
2178      if (left.isLazy() || right.isLazy())
2179      {
2180        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),ADD);
2181        return Data(c);
2182      }
2183    return C_TensorBinaryOperation(left, right, plus<double>());    return C_TensorBinaryOperation(left, right, plus<double>());
2184  }  }
2185    
# Line 1530  escript::operator+(const Data& left, con Line 2188  escript::operator+(const Data& left, con
2188  Data  Data
2189  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
2190  {  {
2191      if (left.isLazy() || right.isLazy())
2192      {
2193        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),SUB);
2194        return Data(c);
2195      }
2196    return C_TensorBinaryOperation(left, right, minus<double>());    return C_TensorBinaryOperation(left, right, minus<double>());
2197  }  }
2198    
# Line 1538  escript::operator-(const Data& left, con Line 2201  escript::operator-(const Data& left, con
2201  Data  Data
2202  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
2203  {  {
2204      if (left.isLazy() || right.isLazy())
2205      {
2206        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),MUL);
2207        return Data(c);
2208      }
2209    return C_TensorBinaryOperation(left, right, multiplies<double>());    return C_TensorBinaryOperation(left, right, multiplies<double>());
2210  }  }
2211    
# Line 1546  escript::operator*(const Data& left, con Line 2214  escript::operator*(const Data& left, con
2214  Data  Data
2215  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
2216  {  {
2217      if (left.isLazy() || right.isLazy())
2218      {
2219        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),DIV);
2220        return Data(c);
2221      }
2222    return C_TensorBinaryOperation(left, right, divides<double>());    return C_TensorBinaryOperation(left, right, divides<double>());
2223  }  }
2224    
# Line 1554  escript::operator/(const Data& left, con Line 2227  escript::operator/(const Data& left, con
2227  Data  Data
2228  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
2229  {  {
2230      if (left.isLazy())
2231      {
2232        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),ADD);
2233        return Data(c);
2234      }
2235    return left+Data(right,left.getFunctionSpace(),false);    return left+Data(right,left.getFunctionSpace(),false);
2236  }  }
2237    
# Line 1562  escript::operator+(const Data& left, con Line 2240  escript::operator+(const Data& left, con
2240  Data  Data
2241  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
2242  {  {
2243      if (left.isLazy())
2244      {
2245        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),SUB);
2246        return Data(c);
2247      }
2248    return left-Data(right,left.getFunctionSpace(),false);    return left-Data(right,left.getFunctionSpace(),false);
2249  }  }
2250    
# Line 1570  escript::operator-(const Data& left, con Line 2253  escript::operator-(const Data& left, con
2253  Data  Data
2254  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
2255  {  {
2256      if (left.isLazy())
2257      {
2258        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),MUL);
2259        return Data(c);
2260      }
2261    return left*Data(right,left.getFunctionSpace(),false);    return left*Data(right,left.getFunctionSpace(),false);
2262  }  }
2263    
# Line 1578  escript::operator*(const Data& left, con Line 2266  escript::operator*(const Data& left, con
2266  Data  Data
2267  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
2268  {  {
2269      if (left.isLazy())
2270      {
2271        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),DIV);
2272        return Data(c);
2273      }
2274    return left/Data(right,left.getFunctionSpace(),false);    return left/Data(right,left.getFunctionSpace(),false);
2275  }  }
2276    
# Line 1586  escript::operator/(const Data& left, con Line 2279  escript::operator/(const Data& left, con
2279  Data  Data
2280  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
2281  {  {
2282      if (right.isLazy())
2283      {
2284        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),ADD);
2285        return Data(c);
2286      }
2287    return Data(left,right.getFunctionSpace(),false)+right;    return Data(left,right.getFunctionSpace(),false)+right;
2288  }  }
2289    
# Line 1594  escript::operator+(const boost::python:: Line 2292  escript::operator+(const boost::python::
2292  Data  Data
2293  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
2294  {  {
2295      if (right.isLazy())
2296      {
2297        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),SUB);
2298        return Data(c);
2299      }
2300    return Data(left,right.getFunctionSpace(),false)-right;    return Data(left,right.getFunctionSpace(),false)-right;
2301  }  }
2302    
# Line 1602  escript::operator-(const boost::python:: Line 2305  escript::operator-(const boost::python::
2305  Data  Data
2306  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
2307  {  {
2308      if (right.isLazy())
2309      {
2310        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),MUL);
2311        return Data(c);
2312      }
2313    return Data(left,right.getFunctionSpace(),false)*right;    return Data(left,right.getFunctionSpace(),false)*right;
2314  }  }
2315    
# Line 1610  escript::operator*(const boost::python:: Line 2318  escript::operator*(const boost::python::
2318  Data  Data
2319  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
2320  {  {
2321      if (right.isLazy())
2322      {
2323        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),DIV);
2324        return Data(c);
2325      }
2326    return Data(left,right.getFunctionSpace(),false)/right;    return Data(left,right.getFunctionSpace(),false)/right;
2327  }  }
2328    
 //  
 //bool escript::operator==(const Data& left, const Data& right)  
 //{  
 //  /*  
 //  NB: this operator does very little at this point, and isn't to  
 //  be relied on. Requires further implementation.  
 //  */  
 //  
 //  bool ret;  
 //  
 //  if (left.isEmpty()) {  
 //    if(!right.isEmpty()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 //  }  
 //  
 //  if (left.isConstant()) {  
 //    if(!right.isConstant()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 // }  
 //  
 //  if (left.isTagged()) {  
 //   if(!right.isTagged()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 //  }  
 //  
 //  if (left.isExpanded()) {  
 //    if(!right.isExpanded()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 //  }  
 //  
 //  return ret;  
 //}  
2329    
2330  /* TODO */  /* TODO */
2331  /* global reduction */  /* global reduction */
2332  Data  Data
2333  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
2334  {  {
   const DataArrayView& view=getPointDataView();  
2335    
2336    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2337    
2338    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
2339      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
2340    }    }
2341    
# Line 1677  Data::getItem(const boost::python::objec Line 2345  Data::getItem(const boost::python::objec
2345  /* TODO */  /* TODO */
2346  /* global reduction */  /* global reduction */
2347  Data  Data
2348  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
2349  {  {
2350    return Data(*this,region);    return Data(*this,region);
2351  }  }
# Line 1696  void Line 2364  void
2364  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
2365                 const Data& value)                 const Data& value)
2366  {  {
2367    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
2368    
2369    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2370    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
2371      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
2372    }    }
2373    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 1711  Data::setItemD(const boost::python::obje Line 2379  Data::setItemD(const boost::python::obje
2379    
2380  void  void
2381  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
2382                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
2383  {  {
2384    if (isProtected()) {    if (isProtected()) {
2385          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2386    }    }
2387      FORCERESOLVE;
2388    /*  if (isLazy())
2389      {
2390        throw DataException("Error - setSlice not permitted on lazy data.");
2391      }*/
2392    Data tempValue(value);    Data tempValue(value);
2393    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
2394    typeMatchRight(tempValue);    typeMatchRight(tempValue);
2395    m_data->setSlice(tempValue.m_data.get(),region);    getReady()->setSlice(tempValue.m_data.get(),region);
2396  }  }
2397    
2398  void  void
2399  Data::typeMatchLeft(Data& right) const  Data::typeMatchLeft(Data& right) const
2400  {  {
2401      if (right.isLazy() && !isLazy())
2402      {
2403        right.resolve();
2404      }
2405    if (isExpanded()){    if (isExpanded()){
2406      right.expand();      right.expand();
2407    } else if (isTagged()) {    } else if (isTagged()) {
# Line 1737  Data::typeMatchLeft(Data& right) const Line 2414  Data::typeMatchLeft(Data& right) const
2414  void  void
2415  Data::typeMatchRight(const Data& right)  Data::typeMatchRight(const Data& right)
2416  {  {
2417      if (isLazy() && !right.isLazy())
2418      {
2419        resolve();
2420      }
2421    if (isTagged()) {    if (isTagged()) {
2422      if (right.isExpanded()) {      if (right.isExpanded()) {
2423        expand();        expand();
# Line 1754  void Line 2435  void
2435  Data::setTaggedValueByName(std::string name,  Data::setTaggedValueByName(std::string name,
2436                             const boost::python::object& value)                             const boost::python::object& value)
2437  {  {
2438       if (getFunctionSpace().getDomain().isValidTagName(name)) {       if (getFunctionSpace().getDomain()->isValidTagName(name)) {
2439          int tagKey=getFunctionSpace().getDomain().getTag(name);      FORCERESOLVE;
2440            int tagKey=getFunctionSpace().getDomain()->getTag(name);
2441          setTaggedValue(tagKey,value);          setTaggedValue(tagKey,value);
2442       }       }
2443  }  }
# Line 1768  Data::setTaggedValue(int tagKey, Line 2450  Data::setTaggedValue(int tagKey,
2450    }    }
2451    //    //
2452    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2453    tag();    FORCERESOLVE;
2454      if (isConstant()) tag();
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
   
2455    numeric::array asNumArray(value);    numeric::array asNumArray(value);
2456    
   
2457    // extract the shape of the numarray    // extract the shape of the numarray
2458    DataArrayView::ShapeType tempShape;    DataTypes::ShapeType tempShape;
2459    for (int i=0; i < asNumArray.getrank(); i++) {    for (int i=0; i < asNumArray.getrank(); i++) {
2460      tempShape.push_back(extract<int>(asNumArray.getshape()[i]));      tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2461    }    }
2462    
2463    // get the space for the data vector    DataVector temp_data2;
2464    int len = DataArrayView::noValues(tempShape);    temp_data2.copyFromNumArray(asNumArray);
   DataVector temp_data(len, 0.0, len);  
   DataArrayView temp_dataView(temp_data, tempShape);  
   temp_dataView.copy(asNumArray);  
2465    
2466    //    m_data->setTaggedValue(tagKey,tempShape, temp_data2);
   // Call DataAbstract::setTaggedValue  
   m_data->setTaggedValue(tagKey,temp_dataView);  
2467  }  }
2468    
2469    
2470  void  void
2471  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
2472                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
2473                                const DataTypes::ValueType& value,
2474                    int dataOffset)
2475  {  {
2476    if (isProtected()) {    if (isProtected()) {
2477          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2478    }    }
2479    //    //
2480    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2481    tag();    FORCERESOLVE;
2482      if (isConstant()) tag();
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
   
2483    //    //
2484    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2485    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
2486  }  }
2487    
2488  int  int
2489  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
2490  {  {
2491    return m_data->getTagNumber(dpno);    if (isEmpty())
2492  }    {
2493        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");  
2494    }    }
2495      return getFunctionSpace().getTagFromDataPointNo(dpno);
2496  }  }
2497    
 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;  
 }  
2498    
2499  ostream& escript::operator<<(ostream& o, const Data& data)  ostream& escript::operator<<(ostream& o, const Data& data)
2500  {  {
# Line 2170  escript::C_GeneralTensorProduct(Data& ar Line 2511  escript::C_GeneralTensorProduct(Data& ar
2511    // 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)
2512    // 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().
2513    
2514      // deal with any lazy data
2515      if (arg_0.isLazy()) {arg_0.resolve();}
2516      if (arg_1.isLazy()) {arg_1.resolve();}
2517    
2518    // Interpolate if necessary and find an appropriate function space    // Interpolate if necessary and find an appropriate function space
2519    Data arg_0_Z, arg_1_Z;    Data arg_0_Z, arg_1_Z;
2520    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
# Line 2191  escript::C_GeneralTensorProduct(Data& ar Line 2536  escript::C_GeneralTensorProduct(Data& ar
2536    // Get rank and shape of inputs    // Get rank and shape of inputs
2537    int rank0 = arg_0_Z.getDataPointRank();    int rank0 = arg_0_Z.getDataPointRank();
2538    int rank1 = arg_1_Z.getDataPointRank();    int rank1 = arg_1_Z.getDataPointRank();
2539    DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2540    DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2541    
2542    // Prepare for the loops of the product and verify compatibility of shapes    // Prepare for the loops of the product and verify compatibility of shapes
2543    int start0=0, start1=0;    int start0=0, start1=0;
# Line 2201  escript::C_GeneralTensorProduct(Data& ar Line 2546  escript::C_GeneralTensorProduct(Data& ar
2546    else if (transpose == 2)  { start1 = rank1-axis_offset; }    else if (transpose == 2)  { start1 = rank1-axis_offset; }
2547    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"); }
2548    
2549    
2550    // Adjust the shapes for transpose    // Adjust the shapes for transpose
2551    DataArrayView::ShapeType tmpShape0;    DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2552    DataArrayView::ShapeType tmpShape1;    DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2553    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]; }
2554    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]; }
2555    
2556  #if 0  #if 0
2557    // For debugging: show shape after transpose    // For debugging: show shape after transpose
# Line 2236  escript::C_GeneralTensorProduct(Data& ar Line 2582  escript::C_GeneralTensorProduct(Data& ar
2582      SR *= tmpShape1[i];      SR *= tmpShape1[i];
2583    }    }
2584    
2585    // Define the shape of the output    // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2586    DataArrayView::ShapeType shape2;    DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2587    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
2588    for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z       int out_index=0;
2589         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2590         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2591      }
2592    
2593    // Declare output Data object    // Declare output Data object
2594    Data res;    Data res;
2595    
2596    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2597      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2598      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2599      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2600      double *ptr_2 = &((res.getPointDataView().getData())[0]);      double *ptr_2 = &(res.getDataAtOffset(0));
2601      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);
2602    }    }
2603    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
# Line 2269  escript::C_GeneralTensorProduct(Data& ar Line 2618  escript::C_GeneralTensorProduct(Data& ar
2618    
2619      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2620      int offset_0 = tmp_0->getPointOffset(0,0);      int offset_0 = tmp_0->getPointOffset(0,0);
2621      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2622      // Get the views      // Get the views
2623      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2624      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2625      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2626      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2627      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2628    
2629        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2630        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2631    
2632    
2633      // Compute an MVP for the default      // Compute an MVP for the default
2634      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);
2635      // Compute an MVP for each tag      // Compute an MVP for each tag
2636      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2637      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
2638      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2639        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2640        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2641        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2642        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2643        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2644    
2645          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2646          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2647        
2648        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);
2649      }      }
2650    
# Line 2309  escript::C_GeneralTensorProduct(Data& ar Line 2667  escript::C_GeneralTensorProduct(Data& ar
2667        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2668          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2669          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2670          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2671          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2672          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2673          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);
2674        }        }
2675      }      }
# Line 2335  escript::C_GeneralTensorProduct(Data& ar Line 2693  escript::C_GeneralTensorProduct(Data& ar
2693    
2694      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2695      int offset_1 = tmp_1->getPointOffset(0,0);      int offset_1 = tmp_1->getPointOffset(0,0);
2696      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2697      // Get the views      // Get the views
2698      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2699      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2700      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2701      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2702      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2703    
2704        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2705        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2706    
2707      // Compute an MVP for the default      // Compute an MVP for the default
2708      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);
2709      // Compute an MVP for each tag      // Compute an MVP for each tag
2710      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2711      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
2712      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2713        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2714        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2715        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2716        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2717        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2718    
2719          tmp_2->addTag(i->first);
2720          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2721          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2722        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);
2723      }      }
2724    
# Line 2373  escript::C_GeneralTensorProduct(Data& ar Line 2739  escript::C_GeneralTensorProduct(Data& ar
2739      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2740      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2741    
2742      // Get the views  //     // Get the views
2743      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2744      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2745      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2746      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2747      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2748      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2749      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2750    
2751        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2752        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2753        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2754    
2755    
2756      // Compute an MVP for the default      // Compute an MVP for the default
2757      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);
2758      // Merge the tags      // Merge the tags
# Line 2388  escript::C_GeneralTensorProduct(Data& ar Line 2760  escript::C_GeneralTensorProduct(Data& ar
2760      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2761      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2762      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2763        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
2764      }      }
2765      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2766        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2767      }      }
2768      // Compute an MVP for each tag      // Compute an MVP for each tag
2769      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2770      for (i=lookup_2.begin();i!=lookup_2.end();i++) {      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2771        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2772        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2773        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2774        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2775        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2776        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2777    
2778          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2779          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2780          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2781    
2782        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);
2783      }      }
2784    
# Line 2422  escript::C_GeneralTensorProduct(Data& ar Line 2799  escript::C_GeneralTensorProduct(Data& ar
2799      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2800      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2801        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
2802        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2803        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2804          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2805          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2806          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2807          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2808          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);
2809        }        }
2810      }      }
# Line 2451  escript::C_GeneralTensorProduct(Data& ar Line 2828  escript::C_GeneralTensorProduct(Data& ar
2828        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2829          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2830          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2831          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2832          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2833          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2834          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);
2835        }        }
2836      }      }
# Line 2476  escript::C_GeneralTensorProduct(Data& ar Line 2853  escript::C_GeneralTensorProduct(Data& ar
2853      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2854      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2855        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2856        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);        double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2857        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2858          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2859          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2860          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2861          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2862          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);
2863        }        }
2864      }      }
# Line 2506  escript::C_GeneralTensorProduct(Data& ar Line 2883  escript::C_GeneralTensorProduct(Data& ar
2883          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2884          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2885          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2886          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2887          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2888          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2889          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);
2890        }        }
2891      }      }
# Line 2527  Data::borrowData() const Line 2904  Data::borrowData() const
2904    return m_data.get();    return m_data.get();
2905  }  }
2906    
2907    // Not all that happy about returning a non-const from a const
2908    DataAbstract_ptr
2909    Data::borrowDataPtr() const
2910    {
2911      return m_data;
2912    }
2913    
2914    // Not all that happy about returning a non-const from a const
2915    DataReady_ptr
2916    Data::borrowReadyPtr() const
2917    {
2918       DataReady_ptr dr=dynamic_pointer_cast<DataReady>(m_data);
2919       EsysAssert((dr!=0), "Error - casting to DataReady.");
2920       return dr;
2921    }
2922    
2923    std::string
2924    Data::toString() const
2925    {
2926        if (!m_data->isEmpty() &&
2927        getNumDataPoints()*getDataPointSize()>escriptParams.getInt("TOO_MANY_LINES"))
2928        {
2929        stringstream temp;
2930        temp << "Summary: inf="<< inf_const() << " sup=" << sup_const() << " data points=" << getNumDataPoints();
2931        return  temp.str();
2932        }
2933        return m_data->toString();
2934    }
2935    
2936    
2937    
2938    DataTypes::ValueType::const_reference
2939    Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2940    {
2941        if (isLazy())
2942        {
2943        throw DataException("Programmer error - getDataAtOffset not permitted on lazy data (object is const which prevents resolving).");
2944        }
2945        return getReady()->getDataAtOffset(i);
2946    }
2947    
2948    
2949    DataTypes::ValueType::reference
2950    Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2951    {
2952    //     if (isLazy())
2953    //     {
2954    //  throw DataException("getDataAtOffset not permitted on lazy data.");
2955    //     }
2956        FORCERESOLVE;
2957        return getReady()->getDataAtOffset(i);
2958    }
2959    
2960    DataTypes::ValueType::const_reference
2961    Data::getDataPoint(int sampleNo, int dataPointNo) const
2962    {
2963      if (!isReady())
2964      {
2965        throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data (object is const which prevents resolving).");
2966      }
2967      else
2968      {
2969        const DataReady* dr=getReady();
2970        return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2971      }
2972    }
2973    
2974    
2975    DataTypes::ValueType::reference
2976    Data::getDataPoint(int sampleNo, int dataPointNo)
2977    {
2978      FORCERESOLVE;
2979      if (!isReady())
2980      {
2981        throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2982      }
2983      else
2984      {
2985        DataReady* dr=getReady();
2986        return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2987      }
2988    }
2989    
2990    
2991  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2992    
2993  void  void
# Line 2547  void Line 3008  void
3008  Data::dump(const std::string fileName) const  Data::dump(const std::string fileName) const
3009  {  {
3010    try    try
3011       {    {
3012          return m_data->dump(fileName);      if (isLazy())
3013       }      {
3014       catch (exception& e)        Data temp(*this); // this is to get a non-const object which we can resolve
3015       {        temp.resolve();
3016          temp.dump(fileName);
3017        }
3018        else
3019        {
3020              return m_data->dump(fileName);
3021        }
3022      }
3023      catch (exception& e)
3024      {
3025          cout << e.what() << endl;          cout << e.what() << endl;
3026       }    }
3027  }  }
3028    
3029  int  int
3030  Data::get_MPISize() const  Data::get_MPISize() const
3031  {  {
3032      int error, size;      int size;
3033  #ifdef PASO_MPI  #ifdef PASO_MPI
3034        int error;
3035      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
3036  #else  #else
3037      size = 1;      size = 1;
# Line 2571  Data::get_MPISize() const Line 3042  Data::get_MPISize() const
3042  int  int
3043  Data::get_MPIRank() const  Data::get_MPIRank() const
3044  {  {
3045      int error, rank;      int rank;
3046  #ifdef PASO_MPI  #ifdef PASO_MPI
3047        int error;
3048      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
3049  #else  #else
3050      rank = 0;      rank = 0;
# Line 2590  Data::get_MPIComm() const Line 3062  Data::get_MPIComm() const
3062  #endif  #endif
3063  }  }
3064    
3065    

Legend:
Removed from v.1349  
changed lines
  Added in v.2037

  ViewVC Help
Powered by ViewVC 1.1.26