/[escript]/trunk/escript/src/Data.cpp
ViewVC logotype

Diff of /trunk/escript/src/Data.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 790 by bcumming, Wed Jul 26 23:12:34 2006 UTC revision 2105 by jfenwick, Fri Nov 28 01:52:12 2008 UTC
# Line 1  Line 1 
 // $Id$  
1    
2  /*  /*******************************************************
3   ************************************************************  *
4   *          Copyright 2006 by ACcESS MNRF                   *  * Copyright (c) 2003-2008 by University of Queensland
5   *                                                          *  * Earth Systems Science Computational Center (ESSCC)
6   *              http://www.access.edu.au                    *  * http://www.uq.edu.au/esscc
7   *       Primary Business: Queensland, Australia            *  *
8   *  Licensed under the Open Software License version 3.0    *  * Primary Business: Queensland, Australia
9   *     http://www.opensource.org/licenses/osl-3.0.php       *  * Licensed under the Open Software License version 3.0
10   *                                                          *  * http://www.opensource.org/licenses/osl-3.0.php
11   ************************************************************  *
12  */  *******************************************************/
13    
14    
15  #include "Data.h"  #include "Data.h"
16    
17  #include "DataExpanded.h"  #include "DataExpanded.h"
18  #include "DataConstant.h"  #include "DataConstant.h"
19  #include "DataTagged.h"  #include "DataTagged.h"
20  #include "DataEmpty.h"  #include "DataEmpty.h"
21  #include "DataArray.h"  #include "DataLazy.h"
 #include "DataArrayView.h"  
 #include "DataProf.h"  
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
25    #include "FunctionSpaceException.h"
26    #include "EscriptParams.h"
27    
28    extern "C" {
29    #include "esysUtils/blocktimer.h"
30    }
31    
32  #include <fstream>  #include <fstream>
33  #include <algorithm>  #include <algorithm>
34  #include <vector>  #include <vector>
35  #include <functional>  #include <functional>
36    #include <sstream>  // so we can throw messages about ranks
37    
38  #include <boost/python/dict.hpp>  #include <boost/python/dict.hpp>
39  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
# Line 38  using namespace boost::python; Line 44  using namespace boost::python;
44  using namespace boost;  using namespace boost;
45  using namespace escript;  using namespace escript;
46    
47  #if defined DOPROF  // ensure the current object is not a DataLazy
48  //  // The idea was that we could add an optional warning whenever a resolve is forced
49  // global table of profiling data for all Data objects  #define FORCERESOLVE if (isLazy()) {resolve();}
 DataProf dataProfTable;  
 #endif  
50    
51  Data::Data()  Data::Data()
52  {  {
53    //    //
54    // Default data is type DataEmpty    // Default data is type DataEmpty
55    DataAbstract* temp=new DataEmpty();    DataAbstract* temp=new DataEmpty();
56    shared_ptr<DataAbstract> temp_data(temp);    m_data=temp->getPtr();
   m_data=temp_data;  
57    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
58  }  }
59    
60  Data::Data(double value,  Data::Data(double value,
# Line 63  Data::Data(double value, Line 62  Data::Data(double value,
62             const FunctionSpace& what,             const FunctionSpace& what,
63             bool expanded)             bool expanded)
64  {  {
65    DataArrayView::ShapeType dataPointShape;    DataTypes::ShapeType dataPointShape;
66    for (int i = 0; i < shape.attr("__len__")(); ++i) {    for (int i = 0; i < shape.attr("__len__")(); ++i) {
67      dataPointShape.push_back(extract<const int>(shape[i]));      dataPointShape.push_back(extract<const int>(shape[i]));
68    }    }
69    DataArray temp(dataPointShape,value);  
70    initialise(temp.getView(),what,expanded);    int len = DataTypes::noValues(dataPointShape);
71      DataVector temp_data(len,value,len);
72      initialise(temp_data, dataPointShape, what, expanded);
73    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
74  }  }
75    
76  Data::Data(double value,  Data::Data(double value,
77         const DataArrayView::ShapeType& dataPointShape,         const DataTypes::ShapeType& dataPointShape,
78         const FunctionSpace& what,         const FunctionSpace& what,
79             bool expanded)             bool expanded)
80  {  {
81    DataArray temp(dataPointShape,value);    int len = DataTypes::noValues(dataPointShape);
82    pair<int,int> dataShape=what.getDataShape();  
83    initialise(temp.getView(),what,expanded);    DataVector temp_data(len,value,len);
84    //   DataArrayView temp_dataView(temp_data, dataPointShape);
85    
86    //   initialise(temp_dataView, what, expanded);
87      initialise(temp_data, dataPointShape, what, expanded);
88    
89    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
90  }  }
91    
92  Data::Data(const Data& inData)  Data::Data(const Data& inData)
93  {  {
94    m_data=inData.m_data;    m_data=inData.m_data;
95    m_protected=inData.isProtected();    m_protected=inData.isProtected();
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
96  }  }
97    
98    
99  Data::Data(const Data& inData,  Data::Data(const Data& inData,
100             const DataArrayView::RegionType& region)             const DataTypes::RegionType& region)
101  {  {
102      DataAbstract_ptr dat=inData.m_data;
103      if (inData.isLazy())
104      {
105        dat=inData.m_data->resolve();
106      }
107      else
108      {
109        dat=inData.m_data;
110      }
111    //    //
112    // Create Data which is a slice of another Data    // Create Data which is a slice of another Data
113    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = dat->getSlice(region);
114    shared_ptr<DataAbstract> temp_data(tmp);    m_data=DataAbstract_ptr(tmp);
   m_data=temp_data;  
115    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
116  }  }
117    
118  Data::Data(const Data& inData,  Data::Data(const Data& inData,
119             const FunctionSpace& functionspace)             const FunctionSpace& functionspace)
120  {  {
121  #if defined DOPROF    if (inData.isEmpty())
122    // create entry in global profiling table for this object    {
123    profData = dataProfTable.newData();      throw DataException("Error - will not interpolate for instances of DataEmpty.");
124  #endif    }
125    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
126      m_data=inData.m_data;      m_data=inData.m_data;
127    } else {    }
128      #if defined DOPROF    else
129      profData->interpolate++;    {
130      #endif  
131      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);      if (inData.isConstant()) {  // for a constant function, we just need to use the new function space
132      // Note: Must use a reference or pointer to a derived object        if (!inData.probeInterpolation(functionspace))
133      // in order to get polymorphic behaviour. Shouldn't really        {           // Even though this is constant, we still need to check whether interpolation is allowed
134      // be able to create an instance of AbstractDomain but that was done      throw FunctionSpaceException("Cannot interpolate across to the domain of the specified FunctionSpace. (DataConstant)");
135      // as a boost:python work around which may no longer be required.        }
136      const AbstractDomain& inDataDomain=inData.getDomain();        // if the data is not lazy, this will just be a cast to DataReady
137      if  (inDataDomain==functionspace.getDomain()) {        DataReady_ptr dr=inData.m_data->resolve();
138        inDataDomain.interpolateOnDomain(tmp,inData);        DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),dr->getVector());  
139          m_data=DataAbstract_ptr(dc);
140      } else {      } else {
141        inDataDomain.interpolateACross(tmp,inData);        Data tmp(0,inData.getDataPointShape(),functionspace,true);
142          // Note: Must use a reference or pointer to a derived object
143          // in order to get polymorphic behaviour. Shouldn't really
144          // be able to create an instance of AbstractDomain but that was done
145          // as a boost:python work around which may no longer be required.
146          /*const AbstractDomain& inDataDomain=inData.getDomain();*/
147          const_Domain_ptr inDataDomain=inData.getDomain();
148          if  (inDataDomain==functionspace.getDomain()) {
149            inDataDomain->interpolateOnDomain(tmp,inData);
150          } else {
151            inDataDomain->interpolateACross(tmp,inData);
152          }
153          m_data=tmp.m_data;
154      }      }
     m_data=tmp.m_data;  
155    }    }
156    m_protected=false;    m_protected=false;
157  }  }
158    
159  Data::Data(const DataTagged::TagListType& tagKeys,  Data::Data(DataAbstract* underlyingdata)
            const DataTagged::ValueListType & values,  
            const DataArrayView& defaultValue,  
            const FunctionSpace& what,  
            bool expanded)  
160  {  {
161    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);  //  m_data=shared_ptr<DataAbstract>(underlyingdata);
162    shared_ptr<DataAbstract> temp_data(temp);      m_data=underlyingdata->getPtr();
163    m_data=temp_data;      m_protected=false;
   m_protected=false;  
   if (expanded) {  
     expand();  
   }  
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
164  }  }
165    
166    Data::Data(DataAbstract_ptr underlyingdata)
167    {
168        m_data=underlyingdata;
169        m_protected=false;
170    }
171    
172    
173  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
174         const FunctionSpace& what,         const FunctionSpace& what,
175             bool expanded)             bool expanded)
176  {  {
177    initialise(value,what,expanded);    initialise(value,what,expanded);
178    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
179  }  }
180    
181  Data::Data(const DataArrayView& value,  Data::Data(const DataTypes::ValueType& value,
182         const FunctionSpace& what,           const DataTypes::ShapeType& shape,
183             bool expanded)                   const FunctionSpace& what,
184                     bool expanded)
185  {  {
186    initialise(value,what,expanded);     initialise(value,shape,what,expanded);
187    m_protected=false;     m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
188  }  }
189    
190    
191  Data::Data(const object& value,  Data::Data(const object& value,
192         const FunctionSpace& what,         const FunctionSpace& what,
193             bool expanded)             bool expanded)
# Line 195  Data::Data(const object& value, Line 195  Data::Data(const object& value,
195    numeric::array asNumArray(value);    numeric::array asNumArray(value);
196    initialise(asNumArray,what,expanded);    initialise(asNumArray,what,expanded);
197    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
198  }  }
199    
200    
201  Data::Data(const object& value,  Data::Data(const object& value,
202             const Data& other)             const Data& other)
203  {  {
204    //    numeric::array asNumArray(value);
205    // Create DataConstant using the given value and all other parameters  
206    // copied from other. If value is a rank 0 object this Data    // extract the shape of the numarray
207    // will assume the point data shape of other.    DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
208    DataArray temp(value);    if (DataTypes::getRank(tempShape)==0) {
209    if (temp.getView().getRank()==0) {  
210      //  
211      // Create a DataArray with the scalar value for all elements      // get the space for the data vector
212      DataArray temp2(other.getPointDataView().getShape(),temp.getView()());      int len1 = DataTypes::noValues(tempShape);
213      initialise(temp2.getView(),other.getFunctionSpace(),false);      DataVector temp_data(len1, 0.0, len1);
214        temp_data.copyFromNumArray(asNumArray,1);
215    
216        int len = DataTypes::noValues(other.getDataPointShape());
217    
218        DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
219        DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
220        m_data=DataAbstract_ptr(t);
221    
222    } else {    } else {
223      //      //
224      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
225      initialise(temp.getView(),other.getFunctionSpace(),false);      DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
226    //     boost::shared_ptr<DataAbstract> sp(t);
227    //     m_data=sp;
228        m_data=DataAbstract_ptr(t);
229    }    }
230    m_protected=false;    m_protected=false;
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
231  }  }
232    
233  Data::~Data()  Data::~Data()
# Line 231  Data::~Data() Line 235  Data::~Data()
235    
236  }  }
237    
238    
239    
240    void
241    Data::initialise(const boost::python::numeric::array& value,
242                     const FunctionSpace& what,
243                     bool expanded)
244    {
245      //
246      // Construct a Data object of the appropriate type.
247      // Construct the object first as there seems to be a bug which causes
248      // undefined behaviour if an exception is thrown during construction
249      // within the shared_ptr constructor.
250      if (expanded) {
251        DataAbstract* temp=new DataExpanded(value, what);
252    //     boost::shared_ptr<DataAbstract> temp_data(temp);
253    //     m_data=temp_data;
254        m_data=temp->getPtr();
255      } else {
256        DataAbstract* temp=new DataConstant(value, what);
257    //     boost::shared_ptr<DataAbstract> temp_data(temp);
258    //     m_data=temp_data;
259        m_data=temp->getPtr();
260      }
261    }
262    
263    
264    void
265    Data::initialise(const DataTypes::ValueType& value,
266             const DataTypes::ShapeType& shape,
267                     const FunctionSpace& what,
268                     bool expanded)
269    {
270      //
271      // Construct a Data object of the appropriate type.
272      // Construct the object first as there seems to be a bug which causes
273      // undefined behaviour if an exception is thrown during construction
274      // within the shared_ptr constructor.
275      if (expanded) {
276        DataAbstract* temp=new DataExpanded(what, shape, value);
277    //     boost::shared_ptr<DataAbstract> temp_data(temp);
278    //     m_data=temp_data;
279        m_data=temp->getPtr();
280      } else {
281        DataAbstract* temp=new DataConstant(what, shape, value);
282    //     boost::shared_ptr<DataAbstract> temp_data(temp);
283    //     m_data=temp_data;
284        m_data=temp->getPtr();
285      }
286    }
287    
288    
289    // void
290    // Data::CompareDebug(const Data& rd)
291    // {
292    //  using namespace std;
293    //  bool mismatch=false;
294    //  std::cout << "Comparing left and right" << endl;
295    //  const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
296    //  const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
297    //  
298    //  if (left==0)
299    //  {
300    //      cout << "left arg is not a DataTagged\n";
301    //      return;
302    //  }
303    //  
304    //  if (right==0)
305    //  {
306    //      cout << "right arg is not a DataTagged\n";
307    //      return;
308    //  }
309    //  cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
310    //  cout << "Shapes ";
311    //  if (left->getShape()==right->getShape())
312    //  {
313    //      cout << "ok\n";
314    //  }
315    //  else
316    //  {
317    //      cout << "Problem: shapes do not match\n";
318    //      mismatch=true;
319    //  }
320    //  int lim=left->getVector().size();
321    //  if (right->getVector().size()) lim=right->getVector().size();
322    //  for (int i=0;i<lim;++i)
323    //  {
324    //      if (left->getVector()[i]!=right->getVector()[i])
325    //      {
326    //          cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
327    //          mismatch=true;
328    //      }
329    //  }
330    //
331    //  // still need to check the tag map
332    //  // also need to watch what is happening to function spaces, are they copied or what?
333    //
334    //  const DataTagged::DataMapType& mapleft=left->getTagLookup();
335    //  const DataTagged::DataMapType& mapright=right->getTagLookup();
336    //
337    //  if (mapleft.size()!=mapright.size())
338    //  {
339    //      cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
340    //      mismatch=true;
341    //      cout << "Left map\n";
342    //      DataTagged::DataMapType::const_iterator i,j;
343    //      for (i=mapleft.begin();i!=mapleft.end();++i) {
344    //          cout << "(" << i->first << "=>" << i->second << ")\n";
345    //      }
346    //      cout << "Right map\n";
347    //      for (i=mapright.begin();i!=mapright.end();++i) {
348    //          cout << "(" << i->first << "=>" << i->second << ")\n";
349    //      }
350    //      cout << "End map\n";
351    //
352    //  }
353    //
354    //  DataTagged::DataMapType::const_iterator i,j;
355    //  for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
356    //     if ((i->first!=j->first) || (i->second!=j->second))
357    //     {
358    //      cout << "(" << i->first << "=>" << i->second << ")";
359    //      cout << ":(" << j->first << "=>" << j->second << ") ";
360    //      mismatch=true;
361    //            }
362    //  }
363    //  if (mismatch)
364    //  {
365    //      cout << "#Mismatch\n";
366    //  }
367    // }
368    
369  escriptDataC  escriptDataC
370  Data::getDataC()  Data::getDataC()
371  {  {
# Line 250  Data::getDataC() const Line 385  Data::getDataC() const
385  const boost::python::tuple  const boost::python::tuple
386  Data::getShapeTuple() const  Data::getShapeTuple() const
387  {  {
388    const DataArrayView::ShapeType& shape=getDataPointShape();    const DataTypes::ShapeType& shape=getDataPointShape();
389    switch(getDataPointRank()) {    switch(getDataPointRank()) {
390       case 0:       case 0:
391          return make_tuple();          return make_tuple();
# Line 267  Data::getShapeTuple() const Line 402  Data::getShapeTuple() const
402    }    }
403  }  }
404    
405    
406    // The different name is needed because boost has trouble with overloaded functions.
407    // It can't work out what type the function is based soley on its name.
408    // There are ways to fix this involving creating function pointer variables for each form
409    // but there doesn't seem to be a need given that the methods have the same name from the python point of view
410    Data
411    Data::copySelf()
412    {
413       DataAbstract* temp=m_data->deepCopy();
414       return Data(temp);
415    }
416    
417  void  void
418  Data::copy(const Data& other)  Data::copy(const Data& other)
419  {  {
420    //    DataAbstract* temp=other.m_data->deepCopy();
421    // Perform a deep copy    DataAbstract_ptr p=temp->getPtr();
422    {    m_data=p;
423      DataExpanded* temp=dynamic_cast<DataExpanded*>(other.m_data.get());  }
424      if (temp!=0) {  
425        //  
426        // Construct a DataExpanded copy  Data
427        DataAbstract* newData=new DataExpanded(*temp);  Data::delay()
428        shared_ptr<DataAbstract> temp_data(newData);  {
429        m_data=temp_data;    DataLazy* dl=new DataLazy(m_data);
430        return;    return Data(dl);
431      }  }
432    }  
433    {  void
434      DataTagged* temp=dynamic_cast<DataTagged*>(other.m_data.get());  Data::delaySelf()
435      if (temp!=0) {  {
436        //    if (!isLazy())
       // Construct a DataTagged copy  
       DataAbstract* newData=new DataTagged(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
   }  
437    {    {
438      DataConstant* temp=dynamic_cast<DataConstant*>(other.m_data.get());      m_data=(new DataLazy(m_data))->getPtr();
     if (temp!=0) {  
       //  
       // Construct a DataConstant copy  
       DataAbstract* newData=new DataConstant(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
439    }    }
440    }
441    
442    void
443    Data::setToZero()
444    {
445      if (isEmpty())
446    {    {
447      DataEmpty* temp=dynamic_cast<DataEmpty*>(other.m_data.get());       throw DataException("Error - Operations not permitted on instances of DataEmpty.");
     if (temp!=0) {  
       //  
       // Construct a DataEmpty copy  
       DataAbstract* newData=new DataEmpty();  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
     }  
448    }    }
449    throw DataException("Error - Copy not implemented for this Data type.");    m_data->setToZero();
450  }  }
451    
452  void  void
453  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
454                     const Data& mask)                     const Data& mask)
455  {  {
456    Data mask1;    // 1. Interpolate if required so all Datas use the same FS as this
457    Data mask2;    // 2. Tag or Expand so that all Data's are the same type
458      // 3. Iterate over the data vectors copying values where mask is >0
459      if (other.isEmpty() || mask.isEmpty())
460      {
461        throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");
462      }
463      Data other2(other);
464      Data mask2(mask);
465      other2.resolve();
466      mask2.resolve();
467      this->resolve();
468      FunctionSpace myFS=getFunctionSpace();
469      FunctionSpace oFS=other2.getFunctionSpace();
470      FunctionSpace mFS=mask2.getFunctionSpace();
471      if (oFS!=myFS)
472      {
473         if (other2.probeInterpolation(myFS))
474         {
475        other2=other2.interpolate(myFS);
476         }
477         else
478         {
479        throw DataException("Error - copyWithMask: other FunctionSpace is not compatible with this one.");
480         }
481      }
482      if (mFS!=myFS)
483      {
484         if (mask2.probeInterpolation(myFS))
485         {
486        mask2=mask2.interpolate(myFS);
487         }
488         else
489         {
490        throw DataException("Error - copyWithMask: mask FunctionSpace is not compatible with this one.");
491         }
492      }
493                // Ensure that all args have the same type
494      if (this->isExpanded() || mask2.isExpanded() || other2.isExpanded())
495      {
496        this->expand();
497        other2.expand();
498        mask2.expand();
499      }
500      else if (this->isTagged() || mask2.isTagged() || other2.isTagged())
501      {
502        this->tag();
503        other2.tag();
504        mask2.tag();
505      }
506      else if (this->isConstant() && mask2.isConstant() && other2.isConstant())
507      {
508      }
509      else
510      {
511        throw DataException("Error - Unknown DataAbstract passed to copyWithMask.");
512      }
513      // Now we iterate over the elements
514      DataVector& self=getReadyPtr()->getVector();
515      const DataVector& ovec=other2.getReadyPtr()->getVector();
516      const DataVector& mvec=mask2.getReadyPtr()->getVector();
517      if ((self.size()!=ovec.size()) || (self.size()!=mvec.size()))
518      {
519        throw DataException("Error - size mismatch in arguments to copyWithMask.");
520      }
521      size_t num_points=self.size();
522    
523    mask1 = mask.wherePositive();    // OPENMP 3.0 allows unsigned loop vars.
524    mask2.copy(mask1);  #if defined(_OPENMP) && (_OPENMP < 200805)
525      long i;
526    #else
527      size_t i;
528    #endif
529      #pragma omp parallel for private(i) schedule(static)
530      for (i=0;i<num_points;++i)
531      {
532        if (mvec[i]>0)
533        {
534           self[i]=ovec[i];
535        }
536      }
537    }
538    
   mask1 *= other;  
   mask2 *= *this;  
   mask2 = *this - mask2;  
539    
   *this = mask1 + mask2;  
 }  
540    
541  bool  bool
542  Data::isExpanded() const  Data::isExpanded() const
# Line 350  Data::isTagged() const Line 552  Data::isTagged() const
552    return (temp!=0);    return (temp!=0);
553  }  }
554    
 /* TODO */  
 /* global reduction -- the local data being empty does not imply that it is empty on other processers*/  
555  bool  bool
556  Data::isEmpty() const  Data::isEmpty() const
557  {  {
# Line 366  Data::isConstant() const Line 566  Data::isConstant() const
566    return (temp!=0);    return (temp!=0);
567  }  }
568    
569    bool
570    Data::isLazy() const
571    {
572      return m_data->isLazy();
573    }
574    
575    // at the moment this is synonymous with !isLazy() but that could change
576    bool
577    Data::isReady() const
578    {
579      return (dynamic_cast<DataReady*>(m_data.get())!=0);
580    }
581    
582    
583  void  void
584  Data::setProtection()  Data::setProtection()
585  {  {
586     m_protected=true;     m_protected=true;
587  }  }
588    
589  bool  bool
590  Data::isProtected() const  Data::isProtected() const
591  {  {
592     return m_protected;     return m_protected;
593  }  }
594    
# Line 386  Data::expand() Line 600  Data::expand()
600    if (isConstant()) {    if (isConstant()) {
601      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
602      DataAbstract* temp=new DataExpanded(*tempDataConst);      DataAbstract* temp=new DataExpanded(*tempDataConst);
603      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
604      m_data=temp_data;  //     m_data=temp_data;
605        m_data=temp->getPtr();
606    } else if (isTagged()) {    } else if (isTagged()) {
607      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
608      DataAbstract* temp=new DataExpanded(*tempDataTag);      DataAbstract* temp=new DataExpanded(*tempDataTag);
609      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
610      m_data=temp_data;  //     m_data=temp_data;
611        m_data=temp->getPtr();
612    } else if (isExpanded()) {    } else if (isExpanded()) {
613      //      //
614      // do nothing      // do nothing
615    } else if (isEmpty()) {    } else if (isEmpty()) {
616      throw DataException("Error - Expansion of DataEmpty not possible.");      throw DataException("Error - Expansion of DataEmpty not possible.");
617      } else if (isLazy()) {
618        resolve();
619        expand();       // resolve might not give us expanded data
620    } else {    } else {
621      throw DataException("Error - Expansion not implemented for this Data type.");      throw DataException("Error - Expansion not implemented for this Data type.");
622    }    }
# Line 409  Data::tag() Line 628  Data::tag()
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 DataTagged(*tempDataConst);      DataAbstract* temp=new DataTagged(*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      // do nothing      // do nothing
636    } else if (isExpanded()) {    } else if (isExpanded()) {
637      throw DataException("Error - Creating tag data from DataExpanded not possible.");      throw DataException("Error - Creating tag data from DataExpanded not possible.");
638    } else if (isEmpty()) {    } else if (isEmpty()) {
639      throw DataException("Error - Creating tag data from DataEmpty not possible.");      throw DataException("Error - Creating tag data from DataEmpty not possible.");
640      } else if (isLazy()) {
641         DataAbstract_ptr res=m_data->resolve();
642         if (m_data->isExpanded())
643         {
644        throw DataException("Error - data would resolve to DataExpanded, tagging is not possible.");
645         }
646         m_data=res;    
647         tag();
648    } else {    } else {
649      throw DataException("Error - Tagging not implemented for this Data type.");      throw DataException("Error - Tagging not implemented for this Data type.");
650    }    }
651  }  }
652    
653  void  void
654  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::resolve()
655  {  {
656    m_data->reshapeDataPoint(shape);    if (isLazy())
657      {
658         m_data=m_data->resolve();
659      }
660    }
661    
662    
663    Data
664    Data::oneOver() const
665    {
666      if (isLazy())
667      {
668        DataLazy* c=new DataLazy(borrowDataPtr(),RECIP);
669        return Data(c);
670      }
671      return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
672  }  }
673    
674  Data  Data
675  Data::wherePositive() const  Data::wherePositive() const
676  {  {
677  #if defined DOPROF    if (isLazy())
678    profData->where++;    {
679  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),GZ);
680    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));      return Data(c);
681      }
682      return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
683  }  }
684    
685  Data  Data
686  Data::whereNegative() const  Data::whereNegative() const
687  {  {
688  #if defined DOPROF    if (isLazy())
689    profData->where++;    {
690  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),LZ);
691    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));      return Data(c);
692      }
693      return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
694  }  }
695    
696  Data  Data
697  Data::whereNonNegative() const  Data::whereNonNegative() const
698  {  {
699  #if defined DOPROF    if (isLazy())
700    profData->where++;    {
701  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),GEZ);
702    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));      return Data(c);
703      }
704      return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
705  }  }
706    
707  Data  Data
708  Data::whereNonPositive() const  Data::whereNonPositive() const
709  {  {
710  #if defined DOPROF    if (isLazy())
711    profData->where++;    {
712  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),LEZ);
713    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));      return Data(c);
714      }
715      return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
716  }  }
717    
718  Data  Data
719  Data::whereZero(double tol) const  Data::whereZero(double tol) const
720  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
721    Data dataAbs=abs();    Data dataAbs=abs();
722    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
723  }  }
724    
725  Data  Data
726  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
727  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
728    Data dataAbs=abs();    Data dataAbs=abs();
729    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
730  }  }
731    
732  Data  Data
733  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
734  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
735    return Data(*this,functionspace);    return Data(*this,functionspace);
736  }  }
737    
738  bool  bool
739  Data::probeInterpolation(const FunctionSpace& functionspace) const  Data::probeInterpolation(const FunctionSpace& functionspace) const
740  {  {
741    if (getFunctionSpace()==functionspace) {    return getFunctionSpace().probeInterpolation(functionspace);
742      return true;  //   if (getFunctionSpace()==functionspace) {
743    } else {  //     return true;
744      const AbstractDomain& domain=getDomain();  //   } else {
745      if  (domain==functionspace.getDomain()) {  //     const_Domain_ptr domain=getDomain();
746        return domain.probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());  //     if  (*domain==*functionspace.getDomain()) {
747      } else {  //       return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
748        return domain.probeInterpolationACross(getFunctionSpace().getTypeCode(),functionspace.getDomain(),functionspace.getTypeCode());  //     } else {
749      }  //       return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());
750    }  //     }
751    //   }
752  }  }
753    
754  Data  Data
755  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
756  {  {
757  #if defined DOPROF    if (isEmpty())
758    profData->grad++;    {
759  #endif      throw DataException("Error - operation not permitted on instances of DataEmpty.");
760      }
761      double blocktimer_start = blocktimer_time();
762    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
763      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
764    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
765    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
766    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
767    getDomain().setToGradient(out,*this);    getDomain()->setToGradient(out,*this);
768      blocktimer_increment("grad()", blocktimer_start);
769    return out;    return out;
770  }  }
771    
772  Data  Data
773  Data::grad() const  Data::grad() const
774  {  {
775    return gradOn(escript::function(getDomain()));    if (isEmpty())
776      {
777        throw DataException("Error - operation not permitted on instances of DataEmpty.");
778      }
779      return gradOn(escript::function(*getDomain()));
780  }  }
781    
782  int  int
783  Data::getDataPointSize() const  Data::getDataPointSize() const
784  {  {
785    return getPointDataView().noValues();    return m_data->getNoValues();
786  }  }
787    
788  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
789  Data::getLength() const  Data::getLength() const
790  {  {
791    return m_data->getLength();    return m_data->getLength();
792  }  }
793    
 const DataArrayView::ShapeType&  
 Data::getDataPointShape() const  
 {  
   return getPointDataView().getShape();  
 }  
   
 void  
 Data::fillFromNumArray(const boost::python::numeric::array num_array)  
 {  
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
   //  
   // check rank  
   if (num_array.getrank()<getDataPointRank())  
       throw DataException("Rank of numarray does not match Data object rank");  
   
   //  
   // check shape of num_array  
   for (int i=0; i<getDataPointRank(); i++) {  
     if (extract<int>(num_array.getshape()[i+1])!=getDataPointShape()[i])  
        throw DataException("Shape of numarray does not match Data object rank");  
   }  
   
   //  
   // make sure data is expanded:  
   if (!isExpanded()) {  
     expand();  
   }  
   
   //  
   // and copy over  
   m_data->copyAll(num_array);  
 }  
   
794  const  const
795  boost::python::numeric::array  boost::python::numeric::array
796  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
797  {  {
798    //    int i, j, k, l;
799    // determine the total number of data points  
800    int numSamples = getNumSamples();    FORCERESOLVE;
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
801    
802    //    //
803    // determine the rank and shape of each data point    // determine the rank and shape of each data point
804    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
805    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
806    
807    //    //
808    // create the numeric array to be returned    // create the numeric array to be returned
809    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
810    
811    //    //
812    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
813    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
814    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
815    // data points, whilst the first dimension will be equal to the    const DataTypes::ShapeType& arrayShape = dataPointShape;
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPoints);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
   }  
816    
817    //    //
818    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
819      if (arrayRank==0) {
820        numArray.resize(1);
821      }
822    if (arrayRank==1) {    if (arrayRank==1) {
823      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
824    }    }
# Line 623  Data::convertToNumArray() Line 831  Data::convertToNumArray()
831    if (arrayRank==4) {    if (arrayRank==4) {
832      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
833    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
834    
835    //    if (getNumDataPointsPerSample()>0) {
836    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
837    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
838    int dataPoint = 0;         //
839    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
840      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
841        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
842        if (dataPointRank==0) {         }
843          numArray[dataPoint]=dataPointView();  
844        }         //
845        if (dataPointRank==1) {         // Check a valid data point number has been supplied
846          for (int i=0; i<dataPointShape[0]; i++) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
847            numArray[dataPoint][i]=dataPointView(i);             throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
848          }         }
849        }         // TODO: global error handling
850        if (dataPointRank==2) {         // create a view of the data if it is stored locally
851          for (int i=0; i<dataPointShape[0]; i++) {  //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
852            for (int j=0; j<dataPointShape[1]; j++) {         DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
853              numArray[dataPoint][i][j] = dataPointView(i,j);  
           }  
         }  
       }  
       if (dataPointRank==3) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               numArray[dataPoint][i][j][k]=dataPointView(i,j,k);  
             }  
           }  
         }  
       }  
       if (dataPointRank==4) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               for (int l=0; l<dataPointShape[3]; l++) {  
                 numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);  
               }  
             }  
           }  
         }  
       }  
       dataPoint++;  
     }  
   }  
854    
855           switch( dataPointRank ){
856                case 0 :
857                    numArray[0] = getDataAtOffset(offset);
858                    break;
859                case 1 :
860                    for( i=0; i<dataPointShape[0]; i++ )
861                        numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
862                    break;
863                case 2 :
864                    for( i=0; i<dataPointShape[0]; i++ )
865                        for( j=0; j<dataPointShape[1]; j++)
866                            numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
867                    break;
868                case 3 :
869                    for( i=0; i<dataPointShape[0]; i++ )
870                        for( j=0; j<dataPointShape[1]; j++ )
871                            for( k=0; k<dataPointShape[2]; k++)
872                                numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
873                    break;
874                case 4 :
875                    for( i=0; i<dataPointShape[0]; i++ )
876                        for( j=0; j<dataPointShape[1]; j++ )
877                            for( k=0; k<dataPointShape[2]; k++ )
878                                for( l=0; l<dataPointShape[3]; l++)
879                                    numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
880                    break;
881        }
882      }
883    //    //
884    // return the loaded array    // return the array
885    return numArray;    return numArray;
886    
887  }  }
888    
889  const  void
890  boost::python::numeric::array  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
 Data::convertToNumArrayFromSampleNo(int sampleNo)  
891  {  {
892    //      // this will throw if the value cannot be represented
893    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
894    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
895      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  }
   }  
   
   //  
   // determine the number of data points per sample  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   
   //  
   // determine the rank and shape of each data point  
   int dataPointRank = getDataPointRank();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
896    
897    void
898    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
899    {
900      if (isProtected()) {
901            throw DataException("Error - attempt to update protected Data object.");
902      }
903      FORCERESOLVE;
904    //    //
905    // create the numeric array to be returned    // check rank
906    boost::python::numeric::array numArray(0.0);    if (static_cast<unsigned int>(num_array.getrank())<getDataPointRank())
907          throw DataException("Rank of numarray does not match Data object rank");
908    
909    //    //
910    // the rank of the returned numeric array will be the rank of    // check shape of num_array
911    // the data points, plus one. Where the rank of the array is n,    for (unsigned int i=0; i<getDataPointRank(); i++) {
912    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
913    // data points, whilst the first dimension will be equal to the         throw DataException("Shape of numarray does not match Data object rank");
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPointsPerSample);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
914    }    }
   
915    //    //
916    // resize the numeric array to the shape just calculated    // make sure data is expanded:
917    if (arrayRank==1) {    //
918      numArray.resize(arrayShape[0]);    if (!isExpanded()) {
919    }      expand();
   if (arrayRank==2) {  
     numArray.resize(arrayShape[0],arrayShape[1]);  
   }  
   if (arrayRank==3) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);  
   }  
   if (arrayRank==4) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);  
920    }    }
921    if (arrayRank==5) {    if (getNumDataPointsPerSample()>0) {
922      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);         int sampleNo = dataPointNo/getNumDataPointsPerSample();
923           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
924           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
925      } else {
926           m_data->copyToDataPoint(-1, 0,num_array);
927    }    }
928    }
929    
930    //  void
931    // loop through each data point in turn, loading the values for that data point  Data::setValueOfDataPoint(int dataPointNo, const double value)
932    // into the numeric array.  {
933    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    if (isProtected()) {
934      DataArrayView dataPointView = getDataPoint(sampleNo, dataPoint);          throw DataException("Error - attempt to update protected Data object.");
     if (dataPointRank==0) {  
       numArray[dataPoint]=dataPointView();  
     }  
     if (dataPointRank==1) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         numArray[dataPoint][i]=dataPointView(i);  
       }  
     }  
     if (dataPointRank==2) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           numArray[dataPoint][i][j] = dataPointView(i,j);  
         }  
       }  
     }  
     if (dataPointRank==3) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             numArray[dataPoint][i][j][k]=dataPointView(i,j,k);  
           }  
         }  
       }  
     }  
     if (dataPointRank==4) {  
       for (int i=0; i<dataPointShape[0]; i++) {  
         for (int j=0; j<dataPointShape[1]; j++) {  
           for (int k=0; k<dataPointShape[2]; k++) {  
             for (int l=0; l<dataPointShape[3]; l++) {  
               numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);  
             }  
           }  
         }  
       }  
     }  
935    }    }
   
936    //    //
937    // return the loaded array    // make sure data is expanded:
938    return numArray;    FORCERESOLVE;
939      if (!isExpanded()) {
940        expand();
941      }
942      if (getNumDataPointsPerSample()>0) {
943           int sampleNo = dataPointNo/getNumDataPointsPerSample();
944           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
945           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
946      } else {
947           m_data->copyToDataPoint(-1, 0,value);
948      }
949  }  }
950    
951  const  const
952  boost::python::numeric::array  boost::python::numeric::array
953  Data::convertToNumArrayFromDPNo(int procNo,  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
                                 int sampleNo,  
                                                                 int dataPointNo)  
   
954  {  {
955      size_t length=0;    size_t length=0;
956      int i, j, k, l, pos;    int i, j, k, l, pos;
957      FORCERESOLVE;
   //  
   // Check a valid sample number has been supplied  
   if (sampleNo >= getNumSamples()) {  
     throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");  
   }  
   
   //  
   // Check a valid data point number has been supplied  
   if (dataPointNo >= getNumDataPointsPerSample()) {  
     throw DataException("Error - Data::convertToNumArray: invalid dataPointNo.");  
   }  
   
958    //    //
959    // determine the rank and shape of each data point    // determine the rank and shape of each data point
960    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
961    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
962    
963    //    //
964    // create the numeric array to be returned    // create the numeric array to be returned
# Line 815  Data::convertToNumArrayFromDPNo(int proc Line 968  Data::convertToNumArrayFromDPNo(int proc
968    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
969    // as that of the data point    // as that of the data point
970    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
971    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
972    
973    //    //
974    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 835  Data::convertToNumArrayFromDPNo(int proc Line 988  Data::convertToNumArrayFromDPNo(int proc
988      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
989    }    }
990    
991      // added for the MPI communication    // added for the MPI communication
992      length=1;    length=1;
993      for( i=0; i<arrayRank; i++ )    for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
994          length *= arrayShape[i];    double *tmpData = new double[length];
     double *tmpData = new double[length];  
995    
996    //    //
997    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
998    
999      // updated for the MPI case      // updated for the MPI case
1000      if( get_MPIRank()==procNo ){      if( get_MPIRank()==procNo ){
1001                 if (getNumDataPointsPerSample()>0) {
1002                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
1003                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
1004                    //
1005                    // Check a valid sample number has been supplied
1006                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
1007                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
1008                    }
1009    
1010                    //
1011                    // Check a valid data point number has been supplied
1012                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
1013                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
1014                    }
1015                    // TODO: global error handling
1016          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
1017          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
1018                    DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
1019    
1020          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
1021          pos=0;          pos=0;
1022          switch( dataPointRank ){          switch( dataPointRank ){
1023              case 0 :              case 0 :
1024                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
1025                  break;                  break;
1026              case 1 :                      case 1 :
1027                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1028                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
1029                  break;                  break;
1030              case 2 :                      case 2 :
1031                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1032                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
1033                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
1034                  break;                  break;
1035              case 3 :                      case 3 :
1036                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1037                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
1038                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
1039                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
1040                  break;                  break;
1041              case 4 :              case 4 :
1042                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1043                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
1044                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
1045                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
1046                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
1047                  break;                  break;
1048          }          }
1049                }
1050      }      }
1051  #ifdef PASO_MPI          #ifdef PASO_MPI
1052          // broadcast the data to all other processes          // broadcast the data to all other processes
1053          MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
1054  #endif          #endif
1055    
1056      // unpack the data      // unpack the data
1057      switch( dataPointRank ){      switch( dataPointRank ){
1058          case 0 :          case 0 :
1059              numArray[i]=tmpData[0];              numArray[0]=tmpData[0];
1060              break;              break;
1061          case 1 :                  case 1 :
1062              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1063                  numArray[i]=tmpData[i];                  numArray[i]=tmpData[i];
1064              break;              break;
1065          case 2 :                  case 2 :
1066              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1067                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1068                      tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
1069              break;              break;
1070          case 3 :                  case 3 :
1071              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1072                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1073                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
1074                          tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];                          numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
1075              break;              break;
1076          case 4 :          case 4 :
1077              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1078                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1079                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
1080                          for( l=0; l<dataPointShape[3]; l++ )                          for( l=0; l<dataPointShape[3]; l++ )
1081                              tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];                                  numArray[make_tuple(i,j,k,l)]=tmpData[i+dataPointShape[0]*(j*+dataPointShape[1]*(k+l*dataPointShape[2]))];
1082              break;              break;
1083      }      }
1084    
1085      delete [] tmpData;        delete [] tmpData;
 /*  
   if (dataPointRank==0) {  
     numArray[0]=dataPointView();  
   }  
   if (dataPointRank==1) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       numArray[i]=dataPointView(i);  
     }  
   }  
   if (dataPointRank==2) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         numArray[i][j] = dataPointView(i,j);  
       }  
     }  
   }  
   if (dataPointRank==3) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           numArray[i][j][k]=dataPointView(i,j,k);  
         }  
       }  
     }  
   }  
   if (dataPointRank==4) {  
     for (int i=0; i<dataPointShape[0]; i++) {  
       for (int j=0; j<dataPointShape[1]; j++) {  
         for (int k=0; k<dataPointShape[2]; k++) {  
           for (int l=0; l<dataPointShape[3]; l++) {  
             numArray[i][j][k][l]=dataPointView(i,j,k,l);  
           }  
         }  
       }  
     }  
   }  
 */  
   
1086    //    //
1087    // return the loaded array    // return the loaded array
1088    return numArray;    return numArray;
1089  }  }
1090    
1091    
1092    boost::python::numeric::array
1093    Data::integrate_const() const
1094    {
1095      if (isLazy())
1096      {
1097        throw DataException("Error - cannot integrate for constant lazy data.");
1098      }
1099      return integrateWorker();
1100    }
1101    
1102    boost::python::numeric::array
1103    Data::integrate()
1104    {
1105      if (isLazy())
1106      {
1107        expand();
1108      }
1109      return integrateWorker();
1110    }
1111    
1112    
1113    
1114  boost::python::numeric::array  boost::python::numeric::array
1115  Data::integrate() const  Data::integrateWorker() const
1116  {  {
1117    int index;    int index;
1118    int rank = getDataPointRank();    int rank = getDataPointRank();
1119    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
1120      int dataPointSize = getDataPointSize();
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
1121    
1122    //    //
1123    // calculate the integral values    // calculate the integral values
1124    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
1125    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    vector<double> integrals_local(dataPointSize);
1126      const AbstractContinuousDomain* dom=dynamic_cast<const AbstractContinuousDomain*>(getDomain().get());
1127      if (dom==0)
1128      {            
1129        throw DataException("Can not integrate over non-continuous domains.");
1130      }
1131    #ifdef PASO_MPI
1132      dom->setToIntegrals(integrals_local,*this);
1133      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
1134      double *tmp = new double[dataPointSize];
1135      double *tmp_local = new double[dataPointSize];
1136      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
1137      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
1138      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
1139      delete[] tmp;
1140      delete[] tmp_local;
1141    #else
1142      dom->setToIntegrals(integrals,*this);
1143    #endif
1144    
1145    //    //
1146    // create the numeric array to be returned    // create the numeric array to be returned
# Line 1031  Data::integrate() const Line 1200  Data::integrate() const
1200  Data  Data
1201  Data::sin() const  Data::sin() const
1202  {  {
1203  #if defined DOPROF    if (isLazy())
1204    profData->unary++;    {
1205  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),SIN);
1206    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);      return Data(c);
1207      }
1208      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1209  }  }
1210    
1211  Data  Data
1212  Data::cos() const  Data::cos() const
1213  {  {
1214  #if defined DOPROF    if (isLazy())
1215    profData->unary++;    {
1216  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),COS);
1217    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);      return Data(c);
1218      }
1219      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1220  }  }
1221    
1222  Data  Data
1223  Data::tan() const  Data::tan() const
1224  {  {
1225  #if defined DOPROF    if (isLazy())
1226    profData->unary++;    {
1227  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),TAN);
1228    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);      return Data(c);
1229      }
1230      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1231  }  }
1232    
1233  Data  Data
1234  Data::asin() const  Data::asin() const
1235  {  {
1236  #if defined DOPROF    if (isLazy())
1237    profData->unary++;    {
1238  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),ASIN);
1239    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);      return Data(c);
1240      }
1241      return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1242  }  }
1243    
1244  Data  Data
1245  Data::acos() const  Data::acos() const
1246  {  {
1247  #if defined DOPROF    if (isLazy())
1248    profData->unary++;    {
1249  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),ACOS);
1250    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);      return Data(c);
1251      }
1252      return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1253  }  }
1254    
1255    
1256  Data  Data
1257  Data::atan() const  Data::atan() const
1258  {  {
1259  #if defined DOPROF    if (isLazy())
1260    profData->unary++;    {
1261  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),ATAN);
1262    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);      return Data(c);
1263      }
1264      return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1265  }  }
1266    
1267  Data  Data
1268  Data::sinh() const  Data::sinh() const
1269  {  {
1270  #if defined DOPROF    if (isLazy())
1271    profData->unary++;    {
1272  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),SINH);
1273    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);      return Data(c);
1274      }
1275      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1276  }  }
1277    
1278  Data  Data
1279  Data::cosh() const  Data::cosh() const
1280  {  {
1281  #if defined DOPROF    if (isLazy())
1282    profData->unary++;    {
1283  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),COSH);
1284    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);      return Data(c);
1285      }
1286      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1287  }  }
1288    
1289  Data  Data
1290  Data::tanh() const  Data::tanh() const
1291  {  {
1292  #if defined DOPROF    if (isLazy())
1293    profData->unary++;    {
1294        DataLazy* c=new DataLazy(borrowDataPtr(),TANH);
1295        return Data(c);
1296      }
1297      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1298    }
1299    
1300    
1301    Data
1302    Data::erf() const
1303    {
1304    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1305      throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1306    #else
1307      if (isLazy())
1308      {
1309        DataLazy* c=new DataLazy(borrowDataPtr(),ERF);
1310        return Data(c);
1311      }
1312      return C_TensorUnaryOperation(*this, ::erf);
1313  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);  
1314  }  }
1315    
1316  Data  Data
1317  Data::asinh() const  Data::asinh() const
1318  {  {
1319  #if defined DOPROF    if (isLazy())
1320    profData->unary++;    {
1321        DataLazy* c=new DataLazy(borrowDataPtr(),ASINH);
1322        return Data(c);
1323      }
1324    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1325      return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1326    #else
1327      return C_TensorUnaryOperation(*this, ::asinh);
1328  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);  
1329  }  }
1330    
1331  Data  Data
1332  Data::acosh() const  Data::acosh() const
1333  {  {
1334  #if defined DOPROF    if (isLazy())
1335    profData->unary++;    {
1336        DataLazy* c=new DataLazy(borrowDataPtr(),ACOSH);
1337        return Data(c);
1338      }
1339    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1340      return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1341    #else
1342      return C_TensorUnaryOperation(*this, ::acosh);
1343  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);  
1344  }  }
1345    
1346  Data  Data
1347  Data::atanh() const  Data::atanh() const
1348  {  {
1349  #if defined DOPROF    if (isLazy())
1350    profData->unary++;    {
1351        DataLazy* c=new DataLazy(borrowDataPtr(),ATANH);
1352        return Data(c);
1353      }
1354    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1355      return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1356    #else
1357      return C_TensorUnaryOperation(*this, ::atanh);
1358  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);  
1359  }  }
1360    
1361  Data  Data
1362  Data::log10() const  Data::log10() const
1363  {  {  if (isLazy())
1364  #if defined DOPROF    {
1365    profData->unary++;      DataLazy* c=new DataLazy(borrowDataPtr(),LOG10);
1366  #endif      return Data(c);
1367    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    }
1368      return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1369  }  }
1370    
1371  Data  Data
1372  Data::log() const  Data::log() const
1373  {  {
1374  #if defined DOPROF    if (isLazy())
1375    profData->unary++;    {
1376  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),LOG);
1377    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);      return Data(c);
1378      }
1379      return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1380  }  }
1381    
1382  Data  Data
1383  Data::sign() const  Data::sign() const
1384  {  {
1385  #if defined DOPROF    if (isLazy())
1386    profData->unary++;    {
1387  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),SIGN);
1388    return escript::unaryOp(*this,escript::fsign);      return Data(c);
1389      }
1390      return C_TensorUnaryOperation(*this, escript::fsign);
1391  }  }
1392    
1393  Data  Data
1394  Data::abs() const  Data::abs() const
1395  {  {
1396  #if defined DOPROF    if (isLazy())
1397    profData->unary++;    {
1398  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),ABS);
1399    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);      return Data(c);
1400      }
1401      return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1402  }  }
1403    
1404  Data  Data
1405  Data::neg() const  Data::neg() const
1406  {  {
1407  #if defined DOPROF    if (isLazy())
1408    profData->unary++;    {
1409  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),NEG);
1410    return escript::unaryOp(*this,negate<double>());      return Data(c);
1411      }
1412      return C_TensorUnaryOperation(*this, negate<double>());
1413  }  }
1414    
1415  Data  Data
1416  Data::pos() const  Data::pos() const
1417  {  {
1418  #if defined DOPROF      // not doing lazy check here is deliberate.
1419    profData->unary++;      // since a deep copy of lazy data should be cheap, I'll just let it happen now
 #endif  
1420    Data result;    Data result;
1421    // perform a deep copy    // perform a deep copy
1422    result.copy(*this);    result.copy(*this);
# Line 1195  Data::pos() const Line 1425  Data::pos() const
1425    
1426  Data  Data
1427  Data::exp() const  Data::exp() const
1428  {  {  
1429  #if defined DOPROF    if (isLazy())
1430    profData->unary++;    {
1431  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),EXP);
1432    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);      return Data(c);
1433      }
1434      return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1435  }  }
1436    
1437  Data  Data
1438  Data::sqrt() const  Data::sqrt() const
1439  {  {
1440  #if defined DOPROF    if (isLazy())
1441    profData->unary++;    {
1442  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),SQRT);
1443    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);      return Data(c);
1444      }
1445      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1446  }  }
1447    
1448  double  double
1449  Data::Lsup() const  Data::Lsup_const() const
1450  {  {
1451    double localValue, globalValue;     if (isLazy())
1452  #if defined DOPROF     {
1453    profData->reduction1++;      throw DataException("Error - cannot compute Lsup for constant lazy data.");
1454  #endif     }
1455    //     return LsupWorker();
1456    // set the initial absolute maximum value to zero  }
1457    
1458    AbsMax abs_max_func;  double
1459    localValue = algorithm(abs_max_func,0);  Data::Lsup()
1460  #ifdef PASO_MPI  {
1461    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );     if (isLazy())
1462    return globalValue;     {
1463  #else      resolve();
1464    return localValue;     }
1465  #endif     return LsupWorker();
1466  }  }
1467    
1468  double  double
1469  Data::Linf() const  Data::sup_const() const
1470  {  {
1471    double localValue, globalValue;     if (isLazy())
1472  #if defined DOPROF     {
1473    profData->reduction1++;      throw DataException("Error - cannot compute sup for constant lazy data.");
1474  #endif     }
1475       return supWorker();
1476    }
1477    
1478    double
1479    Data::sup()
1480    {
1481       if (isLazy())
1482       {
1483        resolve();
1484       }
1485       return supWorker();
1486    }
1487    
1488    double
1489    Data::inf_const() const
1490    {
1491       if (isLazy())
1492       {
1493        throw DataException("Error - cannot compute inf for constant lazy data.");
1494       }
1495       return infWorker();
1496    }
1497    
1498    double
1499    Data::inf()
1500    {
1501       if (isLazy())
1502       {
1503        resolve();
1504       }
1505       return infWorker();
1506    }
1507    
1508    double
1509    Data::LsupWorker() const
1510    {
1511      double localValue;
1512    //    //
1513    // set the initial absolute minimum value to max double    // set the initial absolute maximum value to zero
   AbsMin abs_min_func;  
   localValue = algorithm(abs_min_func,numeric_limits<double>::max());  
1514    
1515      AbsMax abs_max_func;
1516      localValue = algorithm(abs_max_func,0);
1517  #ifdef PASO_MPI  #ifdef PASO_MPI
1518    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    double globalValue;
1519      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1520    return globalValue;    return globalValue;
1521  #else  #else
1522    return localValue;    return localValue;
# Line 1252  Data::Linf() const Line 1524  Data::Linf() const
1524  }  }
1525    
1526  double  double
1527  Data::sup() const  Data::supWorker() const
1528  {  {
1529    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1530    //    //
1531    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1532    FMax fmax_func;    FMax fmax_func;
1533    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1534  #ifdef PASO_MPI  #ifdef PASO_MPI
1535      double globalValue;
1536    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1537    return globalValue;    return globalValue;
1538  #else  #else
# Line 1271  Data::sup() const Line 1541  Data::sup() const
1541  }  }
1542    
1543  double  double
1544  Data::inf() const  Data::infWorker() const
1545  {  {
1546    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1547    //    //
1548    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1549    FMin fmin_func;    FMin fmin_func;
1550    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1551  #ifdef PASO_MPI  #ifdef PASO_MPI
1552      double globalValue;
1553    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1554    return globalValue;    return globalValue;
1555  #else  #else
# Line 1294  Data::inf() const Line 1562  Data::inf() const
1562  Data  Data
1563  Data::maxval() const  Data::maxval() const
1564  {  {
1565  #if defined DOPROF    if (isLazy())
1566    profData->reduction2++;    {
1567  #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1568        temp.resolve();
1569        return temp.maxval();
1570      }
1571    //    //
1572    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1573    FMax fmax_func;    FMax fmax_func;
# Line 1306  Data::maxval() const Line 1577  Data::maxval() const
1577  Data  Data
1578  Data::minval() const  Data::minval() const
1579  {  {
1580  #if defined DOPROF    if (isLazy())
1581    profData->reduction2++;    {
1582  #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1583        temp.resolve();
1584        return temp.minval();
1585      }
1586    //    //
1587    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1588    FMin fmin_func;    FMin fmin_func;
# Line 1316  Data::minval() const Line 1590  Data::minval() const
1590  }  }
1591    
1592  Data  Data
1593  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1594  {  {
1595  #if defined DOPROF       if (isLazy())
1596    profData->reduction2++;       {
1597  #endif      Data temp(*this);
1598    Trace trace_func;      temp.resolve();
1599    return dp_algorithm(trace_func,0);      return temp.swapaxes(axis0,axis1);
1600         }
1601         int axis0_tmp,axis1_tmp;
1602         DataTypes::ShapeType s=getDataPointShape();
1603         DataTypes::ShapeType ev_shape;
1604         // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1605         // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1606         int rank=getDataPointRank();
1607         if (rank<2) {
1608            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1609         }
1610         if (axis0<0 || axis0>rank-1) {
1611            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1612         }
1613         if (axis1<0 || axis1>rank-1) {
1614             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1615         }
1616         if (axis0 == axis1) {
1617             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1618         }
1619         if (axis0 > axis1) {
1620             axis0_tmp=axis1;
1621             axis1_tmp=axis0;
1622         } else {
1623             axis0_tmp=axis0;
1624             axis1_tmp=axis1;
1625         }
1626         for (int i=0; i<rank; i++) {
1627           if (i == axis0_tmp) {
1628              ev_shape.push_back(s[axis1_tmp]);
1629           } else if (i == axis1_tmp) {
1630              ev_shape.push_back(s[axis0_tmp]);
1631           } else {
1632              ev_shape.push_back(s[i]);
1633           }
1634         }
1635         Data ev(0.,ev_shape,getFunctionSpace());
1636         ev.typeMatchRight(*this);
1637         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1638         return ev;
1639    
1640  }  }
1641    
1642  Data  Data
1643  Data::symmetric() const  Data::symmetric() const
1644  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1645       // check input       // check input
1646       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1647       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1648          if(s[0] != s[1])          if(s[0] != s[1])
1649             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.");
1650       }       }
1651       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
# Line 1344  Data::symmetric() const Line 1655  Data::symmetric() const
1655       else {       else {
1656          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.");
1657       }       }
1658         if (isLazy())
1659         {
1660        DataLazy* c=new DataLazy(borrowDataPtr(),SYM);
1661        return Data(c);
1662         }
1663       Data ev(0.,getDataPointShape(),getFunctionSpace());       Data ev(0.,getDataPointShape(),getFunctionSpace());
1664       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1665       m_data->symmetric(ev.m_data.get());       m_data->symmetric(ev.m_data.get());
# Line 1353  Data::symmetric() const Line 1669  Data::symmetric() const
1669  Data  Data
1670  Data::nonsymmetric() const  Data::nonsymmetric() const
1671  {  {
1672       #if defined DOPROF       if (isLazy())
1673          profData->unary++;       {
1674       #endif      DataLazy* c=new DataLazy(borrowDataPtr(),NSYM);
1675        return Data(c);
1676         }
1677       // check input       // check input
1678       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1679       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1680          if(s[0] != s[1])          if(s[0] != s[1])
1681             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.");
1682          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1683          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1684          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1685          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1372  Data::nonsymmetric() const Line 1690  Data::nonsymmetric() const
1690       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1691          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1692             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.");
1693          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1694          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1695          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1696          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1388  Data::nonsymmetric() const Line 1706  Data::nonsymmetric() const
1706  }  }
1707    
1708  Data  Data
1709  Data::matrixtrace(int axis_offset) const  Data::trace(int axis_offset) const
1710  {  {    
1711       #if defined DOPROF       if (isLazy())
1712          profData->unary++;       {
1713       #endif      DataLazy* c=new DataLazy(borrowDataPtr(),TRACE,axis_offset);
1714       DataArrayView::ShapeType s=getDataPointShape();      return Data(c);
1715         }
1716         DataTypes::ShapeType s=getDataPointShape();
1717       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1718          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1719          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1720          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1721          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1722          return ev;          return ev;
1723       }       }
1724       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1725          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1726          if (axis_offset==0) {          if (axis_offset==0) {
1727            int s2=s[2];            int s2=s[2];
1728            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1413  Data::matrixtrace(int axis_offset) const Line 1733  Data::matrixtrace(int axis_offset) const
1733          }          }
1734          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1735          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1736          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1737          return ev;          return ev;
1738       }       }
1739       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1740          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1741          if (axis_offset==0) {          if (axis_offset==0) {
1742            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1743            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1432  Data::matrixtrace(int axis_offset) const Line 1752  Data::matrixtrace(int axis_offset) const
1752      }      }
1753          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1754          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1755      m_data->matrixtrace(ev.m_data.get(), axis_offset);      m_data->trace(ev.m_data.get(), axis_offset);
1756          return ev;          return ev;
1757       }       }
1758       else {       else {
1759          throw DataException("Error - Data::matrixtrace can only be calculated for rank 2, 3 or 4 object.");          throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1760       }       }
1761  }  }
1762    
1763  Data  Data
1764  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1765  {  {    
1766  #if defined DOPROF       if (isLazy())
1767       profData->reduction2++;       {
1768  #endif      DataLazy* c=new DataLazy(borrowDataPtr(),TRANS,axis_offset);
1769       DataArrayView::ShapeType s=getDataPointShape();      return Data(c);
1770       DataArrayView::ShapeType ev_shape;       }
1771         DataTypes::ShapeType s=getDataPointShape();
1772         DataTypes::ShapeType ev_shape;
1773       // 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]
1774       // 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)
1775       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1467  Data::transpose(int axis_offset) const Line 1789  Data::transpose(int axis_offset) const
1789  Data  Data
1790  Data::eigenvalues() const  Data::eigenvalues() const
1791  {  {
1792       #if defined DOPROF       if (isLazy())
1793          profData->unary++;       {
1794       #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1795        temp.resolve();
1796        return temp.eigenvalues();
1797         }
1798       // check input       // check input
1799       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1800       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1801          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.");
1802       if(s[0] != s[1])       if(s[0] != s[1])
1803          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.");
1804       // create return       // create return
1805       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1806       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1807       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1808       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1487  Data::eigenvalues() const Line 1812  Data::eigenvalues() const
1812  const boost::python::tuple  const boost::python::tuple
1813  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1814  {  {
1815       #if defined DOPROF       if (isLazy())
1816          profData->unary++;       {
1817       #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1818       DataArrayView::ShapeType s=getDataPointShape();      temp.resolve();
1819       if (getDataPointRank()!=2)      return temp.eigenvalues_and_eigenvectors(tol);
1820         }
1821         DataTypes::ShapeType s=getDataPointShape();
1822         if (getDataPointRank()!=2)
1823          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.");
1824       if(s[0] != s[1])       if(s[0] != s[1])
1825          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.");
1826       // create return       // create return
1827       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1828       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1829       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1830       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1831       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1832       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1833       m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);       m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
# Line 1507  Data::eigenvalues_and_eigenvectors(const Line 1835  Data::eigenvalues_and_eigenvectors(const
1835  }  }
1836    
1837  const boost::python::tuple  const boost::python::tuple
1838  Data::mindp() const  Data::minGlobalDataPoint() const
1839  {  {
1840    // NB: calc_mindp had to be split off from mindp as boost::make_tuple causes an    // NB: calc_minGlobalDataPoint( had to be split off from minGlobalDataPoint( as boost::make_tuple causes an
1841    // abort (for unknown reasons) if there are openmp directives with it in the    // abort (for unknown reasons) if there are openmp directives with it in the
1842    // surrounding function    // surrounding function
1843    
   int SampleNo;  
1844    int DataPointNo;    int DataPointNo;
1845      int ProcNo;    int ProcNo;
1846    calc_mindp(ProcNo,SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1847    return make_tuple(ProcNo,SampleNo,DataPointNo);    return make_tuple(ProcNo,DataPointNo);
1848  }  }
1849    
1850  void  void
1851  Data::calc_mindp(   int& ProcNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1852                  int& SampleNo,                          int& DataPointNo) const
         int& DataPointNo) const  
1853  {  {
1854      if (isLazy())
1855      {
1856        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1857        temp.resolve();
1858        return temp.calc_minGlobalDataPoint(ProcNo,DataPointNo);
1859      }
1860    int i,j;    int i,j;
1861    int lowi=0,lowj=0;    int lowi=0,lowj=0;
1862    double min=numeric_limits<double>::max();    double min=numeric_limits<double>::max();
# Line 1535  Data::calc_mindp(  int& ProcNo, Line 1867  Data::calc_mindp(  int& ProcNo,
1867    int numDPPSample=temp.getNumDataPointsPerSample();    int numDPPSample=temp.getNumDataPointsPerSample();
1868    
1869    double next,local_min;    double next,local_min;
1870    int local_lowi,local_lowj;    int local_lowi=0,local_lowj=0;    
1871    
1872    #pragma omp parallel private(next,local_min,local_lowi,local_lowj)    #pragma omp parallel firstprivate(local_lowi,local_lowj) private(next,local_min)
1873    {    {
1874      local_min=min;      local_min=min;
1875      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1876      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1877        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1878          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1879          if (next<local_min) {          if (next<local_min) {
1880            local_min=next;            local_min=next;
1881            local_lowi=i;            local_lowi=i;
# Line 1561  Data::calc_mindp(  int& ProcNo, Line 1893  Data::calc_mindp(  int& ProcNo,
1893    
1894  #ifdef PASO_MPI  #ifdef PASO_MPI
1895      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1896      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1897      int lowProc = 0;      int lowProc = 0;
1898      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1899      int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );      int error;
1900            error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1901    
1902      if( get_MPIRank()==0 ){      if( get_MPIRank()==0 ){
1903          next = globalMins[lowProc];          next = globalMins[lowProc];
1904          for( i=1; i<get_MPISize(); i++ )          for( i=1; i<get_MPISize(); i++ )
# Line 1581  Data::calc_mindp(  int& ProcNo, Line 1914  Data::calc_mindp(  int& ProcNo,
1914  #else  #else
1915      ProcNo = 0;      ProcNo = 0;
1916  #endif  #endif
1917    SampleNo = lowi;    DataPointNo = lowj + lowi * numDPPSample;
   DataPointNo = lowj;  
1918  }  }
1919    
1920  void  void
1921  Data::saveDX(std::string fileName) const  Data::saveDX(std::string fileName) const
1922  {  {
1923      if (isEmpty())
1924      {
1925        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1926      }
1927      if (isLazy())
1928      {
1929         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1930         temp.resolve();
1931         temp.saveDX(fileName);
1932         return;
1933      }
1934    boost::python::dict args;    boost::python::dict args;
1935    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1936    getDomain().saveDX(fileName,args);    getDomain()->saveDX(fileName,args);
1937    return;    return;
1938  }  }
1939    
1940  void  void
1941  Data::saveVTK(std::string fileName) const  Data::saveVTK(std::string fileName) const
1942  {  {
1943      if (isEmpty())
1944      {
1945        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1946      }
1947      if (isLazy())
1948      {
1949         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1950         temp.resolve();
1951         temp.saveVTK(fileName);
1952         return;
1953      }
1954    boost::python::dict args;    boost::python::dict args;
1955    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1956    getDomain().saveVTK(fileName,args);    getDomain()->saveVTK(fileName,args);
1957    return;    return;
1958  }  }
1959    
# Line 1609  Data::operator+=(const Data& right) Line 1963  Data::operator+=(const Data& right)
1963    if (isProtected()) {    if (isProtected()) {
1964          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1965    }    }
1966  #if defined DOPROF    if (isLazy() || right.isLazy())
1967    profData->binary++;    {
1968  #endif      DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),ADD); // for lazy + is equivalent to +=
1969    binaryOp(right,plus<double>());          m_data=c->getPtr();
1970    return (*this);      return (*this);
1971      }
1972      else
1973      {
1974        binaryOp(right,plus<double>());
1975        return (*this);
1976      }
1977  }  }
1978    
1979  Data&  Data&
# Line 1622  Data::operator+=(const boost::python::ob Line 1982  Data::operator+=(const boost::python::ob
1982    if (isProtected()) {    if (isProtected()) {
1983          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1984    }    }
1985  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1986    profData->binary++;    if (isLazy())
1987  #endif    {
1988    binaryOp(right,plus<double>());      DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),ADD);   // for lazy + is equivalent to +=
1989            m_data=c->getPtr();
1990        return (*this);
1991      }
1992      else
1993      {
1994        binaryOp(tmp,plus<double>());
1995        return (*this);
1996      }
1997    }
1998    
1999    // Hmmm, operator= makes a deep copy but the copy constructor does not?
2000    Data&
2001    Data::operator=(const Data& other)
2002    {
2003      copy(other);
2004    return (*this);    return (*this);
2005  }  }
2006    
# Line 1635  Data::operator-=(const Data& right) Line 2010  Data::operator-=(const Data& right)
2010    if (isProtected()) {    if (isProtected()) {
2011          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2012    }    }
2013  #if defined DOPROF    if (isLazy() || right.isLazy())
2014    profData->binary++;    {
2015  #endif      DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),SUB); // for lazy - is equivalent to -=
2016    binaryOp(right,minus<double>());          m_data=c->getPtr();
2017    return (*this);      return (*this);
2018      }
2019      else
2020      {
2021        binaryOp(right,minus<double>());
2022        return (*this);
2023      }
2024  }  }
2025    
2026  Data&  Data&
# Line 1648  Data::operator-=(const boost::python::ob Line 2029  Data::operator-=(const boost::python::ob
2029    if (isProtected()) {    if (isProtected()) {
2030          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2031    }    }
2032  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
2033    profData->binary++;    if (isLazy())
2034  #endif    {
2035    binaryOp(right,minus<double>());      DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),SUB);   // for lazy - is equivalent to -=
2036    return (*this);          m_data=c->getPtr();
2037        return (*this);
2038      }
2039      else
2040      {
2041        binaryOp(tmp,minus<double>());
2042        return (*this);
2043      }
2044  }  }
2045    
2046  Data&  Data&
# Line 1661  Data::operator*=(const Data& right) Line 2049  Data::operator*=(const Data& right)
2049    if (isProtected()) {    if (isProtected()) {
2050          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2051    }    }
2052  #if defined DOPROF    if (isLazy() || right.isLazy())
2053    profData->binary++;    {
2054  #endif      DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),MUL); // for lazy * is equivalent to *=
2055    binaryOp(right,multiplies<double>());          m_data=c->getPtr();
2056    return (*this);      return (*this);
2057      }
2058      else
2059      {
2060        binaryOp(right,multiplies<double>());
2061        return (*this);
2062      }
2063  }  }
2064    
2065  Data&  Data&
2066  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
2067  {  {  
2068    if (isProtected()) {    if (isProtected()) {
2069          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2070    }    }
2071  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
2072    profData->binary++;    if (isLazy())
2073  #endif    {
2074    binaryOp(right,multiplies<double>());      DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),MUL);   // for lazy * is equivalent to *=
2075    return (*this);          m_data=c->getPtr();
2076        return (*this);
2077      }
2078      else
2079      {
2080        binaryOp(tmp,multiplies<double>());
2081        return (*this);
2082      }
2083  }  }
2084    
2085  Data&  Data&
# Line 1687  Data::operator/=(const Data& right) Line 2088  Data::operator/=(const Data& right)
2088    if (isProtected()) {    if (isProtected()) {
2089          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2090    }    }
2091  #if defined DOPROF    if (isLazy() || right.isLazy())
2092    profData->binary++;    {
2093  #endif      DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),DIV); // for lazy / is equivalent to /=
2094    binaryOp(right,divides<double>());          m_data=c->getPtr();
2095    return (*this);      return (*this);
2096      }
2097      else
2098      {
2099        binaryOp(right,divides<double>());
2100        return (*this);
2101      }
2102  }  }
2103    
2104  Data&  Data&
# Line 1700  Data::operator/=(const boost::python::ob Line 2107  Data::operator/=(const boost::python::ob
2107    if (isProtected()) {    if (isProtected()) {
2108          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2109    }    }
2110  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
2111    profData->binary++;    if (isLazy())
2112  #endif    {
2113    binaryOp(right,divides<double>());      DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),DIV);   // for lazy / is equivalent to /=
2114    return (*this);          m_data=c->getPtr();
2115        return (*this);
2116      }
2117      else
2118      {
2119        binaryOp(tmp,divides<double>());
2120        return (*this);
2121      }
2122  }  }
2123    
2124  Data  Data
2125  Data::rpowO(const boost::python::object& left) const  Data::rpowO(const boost::python::object& left) const
2126  {  {
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
2127    Data left_d(left,*this);    Data left_d(left,*this);
2128    return left_d.powD(*this);    return left_d.powD(*this);
2129  }  }
# Line 1723  Data::rpowO(const boost::python::object& Line 2131  Data::rpowO(const boost::python::object&
2131  Data  Data
2132  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
2133  {  {
2134  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
2135    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
2136  }  }
2137    
2138  Data  Data
2139  Data::powD(const Data& right) const  Data::powD(const Data& right) const
2140  {  {
2141  #if defined DOPROF    if (isLazy() || right.isLazy())
2142    profData->binary++;    {
2143  #endif      DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),POW);
2144    Data result;      return Data(c);
2145    result.copy(*this);    }
2146    result.binaryOp(right,(Data::BinaryDFunPtr)::pow);    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   return result;  
2147  }  }
2148    
   
2149  //  //
2150  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
2151  Data  Data
2152  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
2153  {  {
2154    Data result;    if (left.isLazy() || right.isLazy())
2155    //    {
2156    // perform a deep copy      DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),ADD);
2157    result.copy(left);      return Data(c);
2158    result+=right;    }
2159    return result;    return C_TensorBinaryOperation(left, right, plus<double>());
2160  }  }
2161    
2162  //  //
# Line 1763  escript::operator+(const Data& left, con Line 2164  escript::operator+(const Data& left, con
2164  Data  Data
2165  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
2166  {  {
2167    Data result;    if (left.isLazy() || right.isLazy())
2168    //    {
2169    // perform a deep copy      DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),SUB);
2170    result.copy(left);      return Data(c);
2171    result-=right;    }
2172    return result;    return C_TensorBinaryOperation(left, right, minus<double>());
2173  }  }
2174    
2175  //  //
# Line 1776  escript::operator-(const Data& left, con Line 2177  escript::operator-(const Data& left, con
2177  Data  Data
2178  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
2179  {  {
2180    Data result;    if (left.isLazy() || right.isLazy())
2181    //    {
2182    // perform a deep copy      DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),MUL);
2183    result.copy(left);      return Data(c);
2184    result*=right;    }
2185    return result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
2186  }  }
2187    
2188  //  //
# Line 1789  escript::operator*(const Data& left, con Line 2190  escript::operator*(const Data& left, con
2190  Data  Data
2191  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
2192  {  {
2193    Data result;    if (left.isLazy() || right.isLazy())
2194    //    {
2195    // perform a deep copy      DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),DIV);
2196    result.copy(left);      return Data(c);
2197    result/=right;    }
2198    return result;    return C_TensorBinaryOperation(left, right, divides<double>());
2199  }  }
2200    
2201  //  //
# Line 1802  escript::operator/(const Data& left, con Line 2203  escript::operator/(const Data& left, con
2203  Data  Data
2204  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
2205  {  {
2206    //    if (left.isLazy())
2207    // Convert to DataArray format if possible    {
2208    DataArray temp(right);      DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),ADD);
2209    Data result;      return Data(c);
2210    //    }
2211    // perform a deep copy    return left+Data(right,left.getFunctionSpace(),false);
   result.copy(left);  
   result+=right;  
   return result;  
2212  }  }
2213    
2214  //  //
# Line 1818  escript::operator+(const Data& left, con Line 2216  escript::operator+(const Data& left, con
2216  Data  Data
2217  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
2218  {  {
2219    //    if (left.isLazy())
2220    // Convert to DataArray format if possible    {
2221    DataArray temp(right);      DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),SUB);
2222    Data result;      return Data(c);
2223    //    }
2224    // perform a deep copy    return left-Data(right,left.getFunctionSpace(),false);
   result.copy(left);  
   result-=right;  
   return result;  
2225  }  }
2226    
2227  //  //
# Line 1834  escript::operator-(const Data& left, con Line 2229  escript::operator-(const Data& left, con
2229  Data  Data
2230  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
2231  {  {
2232    //    if (left.isLazy())
2233    // Convert to DataArray format if possible    {
2234    DataArray temp(right);      DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),MUL);
2235    Data result;      return Data(c);
2236    //    }
2237    // perform a deep copy    return left*Data(right,left.getFunctionSpace(),false);
   result.copy(left);  
   result*=right;  
   return result;  
2238  }  }
2239    
2240  //  //
# Line 1850  escript::operator*(const Data& left, con Line 2242  escript::operator*(const Data& left, con
2242  Data  Data
2243  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
2244  {  {
2245    //    if (left.isLazy())
2246    // Convert to DataArray format if possible    {
2247    DataArray temp(right);      DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),DIV);
2248    Data result;      return Data(c);
2249    //    }
2250    // perform a deep copy    return left/Data(right,left.getFunctionSpace(),false);
   result.copy(left);  
   result/=right;  
   return result;  
2251  }  }
2252    
2253  //  //
# Line 1866  escript::operator/(const Data& left, con Line 2255  escript::operator/(const Data& left, con
2255  Data  Data
2256  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
2257  {  {
2258    //    if (right.isLazy())
2259    // Construct the result using the given value and the other parameters    {
2260    // from right      DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),ADD);
2261    Data result(left,right);      return Data(c);
2262    result+=right;    }
2263    return result;    return Data(left,right.getFunctionSpace(),false)+right;
2264  }  }
2265    
2266  //  //
# Line 1879  escript::operator+(const boost::python:: Line 2268  escript::operator+(const boost::python::
2268  Data  Data
2269  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
2270  {  {
2271    //    if (right.isLazy())
2272    // Construct the result using the given value and the other parameters    {
2273    // from right      DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),SUB);
2274    Data result(left,right);      return Data(c);
2275    result-=right;    }
2276    return result;    return Data(left,right.getFunctionSpace(),false)-right;
2277  }  }
2278    
2279  //  //
# Line 1892  escript::operator-(const boost::python:: Line 2281  escript::operator-(const boost::python::
2281  Data  Data
2282  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
2283  {  {
2284    //    if (right.isLazy())
2285    // Construct the result using the given value and the other parameters    {
2286    // from right      DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),MUL);
2287    Data result(left,right);      return Data(c);
2288    result*=right;    }
2289    return result;    return Data(left,right.getFunctionSpace(),false)*right;
2290  }  }
2291    
2292  //  //
# Line 1905  escript::operator*(const boost::python:: Line 2294  escript::operator*(const boost::python::
2294  Data  Data
2295  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
2296  {  {
2297    //    if (right.isLazy())
2298    // Construct the result using the given value and the other parameters    {
2299    // from right      DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),DIV);
2300    Data result(left,right);      return Data(c);
2301    result/=right;    }
2302    return result;    return Data(left,right.getFunctionSpace(),false)/right;
2303  }  }
2304    
 //  
 //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;  
 //}  
2305    
2306  /* TODO */  /* TODO */
2307  /* global reduction */  /* global reduction */
2308  Data  Data
2309  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
2310  {  {
   const DataArrayView& view=getPointDataView();  
2311    
2312    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2313    
2314    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
2315      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
2316    }    }
2317    
# Line 1977  Data::getItem(const boost::python::objec Line 2321  Data::getItem(const boost::python::objec
2321  /* TODO */  /* TODO */
2322  /* global reduction */  /* global reduction */
2323  Data  Data
2324  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
2325  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
2326    return Data(*this,region);    return Data(*this,region);
2327  }  }
2328    
# Line 1995  Data::setItemO(const boost::python::obje Line 2336  Data::setItemO(const boost::python::obje
2336    setItemD(key,tempData);    setItemD(key,tempData);
2337  }  }
2338    
 /* TODO */  
 /* global reduction */  
2339  void  void
2340  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
2341                 const Data& value)                 const Data& value)
2342  {  {
2343    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
2344    
2345    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2346    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
2347      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
2348    }    }
2349    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 2014  Data::setItemD(const boost::python::obje Line 2353  Data::setItemD(const boost::python::obje
2353    }    }
2354  }  }
2355    
 /* TODO */  
 /* global reduction */  
2356  void  void
2357  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
2358                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
2359  {  {
2360    if (isProtected()) {    if (isProtected()) {
2361          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2362    }    }
2363  #if defined DOPROF    FORCERESOLVE;
2364    profData->slicing++;  /*  if (isLazy())
2365  #endif    {
2366        throw DataException("Error - setSlice not permitted on lazy data.");
2367      }*/
2368    Data tempValue(value);    Data tempValue(value);
2369    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
2370    typeMatchRight(tempValue);    typeMatchRight(tempValue);
2371    m_data->setSlice(tempValue.m_data.get(),region);    getReady()->setSlice(tempValue.m_data.get(),region);
2372  }  }
2373    
2374  void  void
2375  Data::typeMatchLeft(Data& right) const  Data::typeMatchLeft(Data& right) const
2376  {  {
2377      if (right.isLazy() && !isLazy())
2378      {
2379        right.resolve();
2380      }
2381    if (isExpanded()){    if (isExpanded()){
2382      right.expand();      right.expand();
2383    } else if (isTagged()) {    } else if (isTagged()) {
# Line 2047  Data::typeMatchLeft(Data& right) const Line 2390  Data::typeMatchLeft(Data& right) const
2390  void  void
2391  Data::typeMatchRight(const Data& right)  Data::typeMatchRight(const Data& right)
2392  {  {
2393      if (isLazy() && !right.isLazy())
2394      {
2395        resolve();
2396      }
2397    if (isTagged()) {    if (isTagged()) {
2398      if (right.isExpanded()) {      if (right.isExpanded()) {
2399        expand();        expand();
# Line 2060  Data::typeMatchRight(const Data& right) Line 2407  Data::typeMatchRight(const Data& right)
2407    }    }
2408  }  }
2409    
2410  /* TODO */  void
2411  /* global reduction */  Data::setTaggedValueByName(std::string name,
2412                               const boost::python::object& value)
2413    {
2414         if (getFunctionSpace().getDomain()->isValidTagName(name)) {
2415        FORCERESOLVE;
2416            int tagKey=getFunctionSpace().getDomain()->getTag(name);
2417            setTaggedValue(tagKey,value);
2418         }
2419    }
2420  void  void
2421  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
2422                       const boost::python::object& value)                       const boost::python::object& value)
# Line 2071  Data::setTaggedValue(int tagKey, Line 2426  Data::setTaggedValue(int tagKey,
2426    }    }
2427    //    //
2428    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2429    tag();    FORCERESOLVE;
2430      if (isConstant()) tag();
2431      numeric::array asNumArray(value);
2432    
2433    if (!isTagged()) {    // extract the shape of the numarray
2434      throw DataException("Error - DataTagged conversion failed!!");    DataTypes::ShapeType tempShape;
2435      for (int i=0; i < asNumArray.getrank(); i++) {
2436        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2437    }    }
2438    
2439    //    DataVector temp_data2;
2440    // Construct DataArray from boost::python::object input value    temp_data2.copyFromNumArray(asNumArray,1);
   DataArray valueDataArray(value);  
2441    
2442    //    m_data->setTaggedValue(tagKey,tempShape, temp_data2);
   // Call DataAbstract::setTaggedValue  
   m_data->setTaggedValue(tagKey,valueDataArray.getView());  
2443  }  }
2444    
2445  /* TODO */  
 /* global reduction */  
2446  void  void
2447  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
2448                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
2449                                const DataTypes::ValueType& value,
2450                    int dataOffset)
2451  {  {
2452    if (isProtected()) {    if (isProtected()) {
2453          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2454    }    }
2455    //    //
2456    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2457    tag();    FORCERESOLVE;
2458      if (isConstant()) tag();
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
2459    //    //
2460    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2461    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
2462  }  }
2463    
 /* TODO */  
 /* global reduction */  
2464  int  int
2465  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
2466  {  {
2467    return m_data->getTagNumber(dpno);    if (isEmpty())
2468  }    {
2469        throw DataException("Error - operation not permitted on instances of DataEmpty.");
 /* TODO */  
 /* global reduction */  
 void  
 Data::setRefValue(int ref,  
                   const boost::python::numeric::array& value)  
 {  
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
2470    }    }
2471    //    return getFunctionSpace().getTagFromDataPointNo(dpno);
   // Construct DataArray from boost::python::object input value  
   DataArray valueDataArray(value);  
   
   //  
   // Call DataAbstract::setRefValue  
   m_data->setRefValue(ref,valueDataArray);  
2472  }  }
2473    
 /* TODO */  
 /* global reduction */  
 void  
 Data::getRefValue(int ref,  
                   boost::python::numeric::array& value)  
 {  
   //  
   // Construct DataArray for boost::python::object return value  
   DataArray valueDataArray(value);  
   
   //  
   // Load DataArray with values from data-points specified by ref  
   m_data->getRefValue(ref,valueDataArray);  
2474    
2475    //  ostream& escript::operator<<(ostream& o, const Data& data)
2476    // Load values from valueDataArray into return numarray  {
2477      o << data.toString();
2478      return o;
2479    }
2480    
2481    // extract the shape of the numarray  Data
2482    int rank = value.getrank();  escript::C_GeneralTensorProduct(Data& arg_0,
2483    DataArrayView::ShapeType shape;                       Data& arg_1,
2484    for (int i=0; i < rank; i++) {                       int axis_offset,
2485      shape.push_back(extract<int>(value.getshape()[i]));                       int transpose)
2486    {
2487      // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2488      // SM is the product of the last axis_offset entries in arg_0.getShape().
2489    
2490      // deal with any lazy data
2491    //   if (arg_0.isLazy()) {arg_0.resolve();}
2492    //   if (arg_1.isLazy()) {arg_1.resolve();}
2493      if (arg_0.isLazy() || arg_1.isLazy())
2494      {
2495        DataLazy* c=new DataLazy(arg_0.borrowDataPtr(), arg_1.borrowDataPtr(), PROD, axis_offset,transpose);
2496        return Data(c);
2497    }    }
2498    
2499    // and load the numarray with the data from the DataArray    // Interpolate if necessary and find an appropriate function space
2500    DataArrayView valueView = valueDataArray.getView();    Data arg_0_Z, arg_1_Z;
2501      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2502    if (rank==0) {      if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2503        boost::python::numeric::array temp_numArray(valueView());        arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2504        value = temp_numArray;        arg_1_Z = Data(arg_1);
2505    }      }
2506    if (rank==1) {      else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2507      for (int i=0; i < shape[0]; i++) {        arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2508        value[i] = valueView(i);        arg_0_Z =Data(arg_0);
2509      }      }
2510    }      else {
2511    if (rank==2) {        throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         value[i][j] = valueView(i,j);  
       }  
2512      }      }
2513      } else {
2514          arg_0_Z = Data(arg_0);
2515          arg_1_Z = Data(arg_1);
2516    }    }
2517    if (rank==3) {    // Get rank and shape of inputs
2518      for (int i=0; i < shape[0]; i++) {    int rank0 = arg_0_Z.getDataPointRank();
2519        for (int j=0; j < shape[1]; j++) {    int rank1 = arg_1_Z.getDataPointRank();
2520          for (int k=0; k < shape[2]; k++) {    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2521            value[i][j][k] = valueView(i,j,k);    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2522          }  
2523        }    // Prepare for the loops of the product and verify compatibility of shapes
2524      int start0=0, start1=0;
2525      if (transpose == 0)       {}
2526      else if (transpose == 1)  { start0 = axis_offset; }
2527      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2528      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2529    
2530    
2531      // Adjust the shapes for transpose
2532      DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2533      DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2534      for (int i=0; i<rank0; i++)   { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2535      for (int i=0; i<rank1; i++)   { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2536    
2537    #if 0
2538      // For debugging: show shape after transpose
2539      char tmp[100];
2540      std::string shapeStr;
2541      shapeStr = "(";
2542      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2543      shapeStr += ")";
2544      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2545      shapeStr = "(";
2546      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2547      shapeStr += ")";
2548      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2549    #endif
2550    
2551      // Prepare for the loops of the product
2552      int SL=1, SM=1, SR=1;
2553      for (int i=0; i<rank0-axis_offset; i++)   {
2554        SL *= tmpShape0[i];
2555      }
2556      for (int i=rank0-axis_offset; i<rank0; i++)   {
2557        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2558          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2559        }
2560        SM *= tmpShape0[i];
2561      }
2562      for (int i=axis_offset; i<rank1; i++)     {
2563        SR *= tmpShape1[i];
2564      }
2565    
2566      // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2567      DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2568      {         // block to limit the scope of out_index
2569         int out_index=0;
2570         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2571         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2572      }
2573    
2574      if (shape2.size()>ESCRIPT_MAX_DATA_RANK)
2575      {
2576         ostringstream os;
2577         os << "C_GeneralTensorProduct: Error - Attempt to create a rank " << shape2.size() << " object. The maximum rank is " << ESCRIPT_MAX_DATA_RANK << ".";
2578         throw DataException(os.str());
2579      }
2580    
2581      // Declare output Data object
2582      Data res;
2583    
2584      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2585        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2586        double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2587        double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2588        double *ptr_2 = &(res.getDataAtOffset(0));
2589        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2590      }
2591      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2592    
2593        // Prepare the DataConstant input
2594        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2595        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2596    
2597        // Borrow DataTagged input from Data object
2598        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2599        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2600    
2601        // Prepare a DataTagged output 2
2602        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2603        res.tag();
2604        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2605        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2606    
2607        // Prepare offset into DataConstant
2608        int offset_0 = tmp_0->getPointOffset(0,0);
2609        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2610        // Get the views
2611    //     DataArrayView view_1 = tmp_1->getDefaultValue();
2612    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2613    //     // Get the pointers to the actual data
2614    //     double *ptr_1 = &((view_1.getData())[0]);
2615    //     double *ptr_2 = &((view_2.getData())[0]);
2616    
2617        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2618        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2619    
2620    
2621        // Compute an MVP for the default
2622        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2623        // Compute an MVP for each tag
2624        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2625        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2626        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2627          tmp_2->addTag(i->first);
2628    //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2629    //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2630    //       double *ptr_1 = &view_1.getData(0);
2631    //       double *ptr_2 = &view_2.getData(0);
2632    
2633          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2634          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2635        
2636          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2637      }      }
2638    
2639    }    }
2640    if (rank==4) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2641      for (int i=0; i < shape[0]; i++) {  
2642        for (int j=0; j < shape[1]; j++) {      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2643          for (int k=0; k < shape[2]; k++) {      DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2644            for (int l=0; l < shape[3]; l++) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2645              value[i][j][k][l] = valueView(i,j,k,l);      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2646            }      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2647          }      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2648        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2649        int sampleNo_1,dataPointNo_1;
2650        int numSamples_1 = arg_1_Z.getNumSamples();
2651        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2652        int offset_0 = tmp_0->getPointOffset(0,0);
2653        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2654        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2655          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2656            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2657            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2658            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2659            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2660            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2661            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2662        }        }
2663      }      }
2664    
2665    }    }
2666      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2667    
2668  }      // Borrow DataTagged input from Data object
2669        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2670        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2671    
2672  void      // Prepare the DataConstant input
2673  Data::archiveData(const std::string fileName)      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2674  {      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
   cout << "Archiving Data object to: " << fileName << endl;  
2675    
2676    //      // Prepare a DataTagged output 2
2677    // Determine type of this Data object      res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2678    int dataType = -1;      res.tag();
2679        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2680        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2681    
2682    if (isEmpty()) {      // Prepare offset into DataConstant
2683      dataType = 0;      int offset_1 = tmp_1->getPointOffset(0,0);
2684      cout << "\tdataType: DataEmpty" << endl;      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2685    }      // Get the views
2686    if (isConstant()) {  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2687      dataType = 1;  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2688      cout << "\tdataType: DataConstant" << endl;  //     // Get the pointers to the actual data
2689    }  //     double *ptr_0 = &((view_0.getData())[0]);
2690    if (isTagged()) {  //     double *ptr_2 = &((view_2.getData())[0]);
     dataType = 2;  
     cout << "\tdataType: DataTagged" << endl;  
   }  
   if (isExpanded()) {  
     dataType = 3;  
     cout << "\tdataType: DataExpanded" << endl;  
   }  
2691    
2692    if (dataType == -1) {      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2693      throw DataException("archiveData Error: undefined dataType");      double *ptr_2 = &(tmp_2->getDefaultValue(0));
   }  
2694    
2695    //      // Compute an MVP for the default
2696    // Collect data items common to all Data types      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2697    int noSamples = getNumSamples();      // Compute an MVP for each tag
2698    int noDPPSample = getNumDataPointsPerSample();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2699    int functionSpaceType = getFunctionSpace().getTypeCode();      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2700    int dataPointRank = getDataPointRank();      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2701    int dataPointSize = getDataPointSize();  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2702    int dataLength = getLength();  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2703    DataArrayView::ShapeType dataPointShape = getDataPointShape();  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2704    vector<int> referenceNumbers(noSamples);  //       double *ptr_0 = &view_0.getData(0);
2705    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  //       double *ptr_2 = &view_2.getData(0);
2706      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);  
2707    }        tmp_2->addTag(i->first);
2708    vector<int> tagNumbers(noSamples);        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2709    if (isTagged()) {        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2710      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
       tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);  
2711      }      }
2712    
2713    }    }
2714      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2715    
2716    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;      // Borrow DataTagged input from Data object
2717    cout << "\tfunctionSpaceType: " << functionSpaceType << endl;      DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2718    cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2719    
2720    //      // Borrow DataTagged input from Data object
2721    // Flatten Shape to an array of integers suitable for writing to file      DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2722    int flatShape[4] = {0,0,0,0};      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
   cout << "\tshape: < ";  
   for (int dim=0; dim<dataPointRank; dim++) {  
     flatShape[dim] = dataPointShape[dim];  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
2723    
2724    //      // Prepare a DataTagged output 2
2725    // Open archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2726    ofstream archiveFile;      res.tag();  // DataTagged output
2727    archiveFile.open(fileName.data(), ios::out);      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2728        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2729    
2730    if (!archiveFile.good()) {  //     // Get the views
2731      throw DataException("archiveData Error: problem opening archive file");  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2732    }  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2733    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2734    //     // Get the pointers to the actual data
2735    //     double *ptr_0 = &((view_0.getData())[0]);
2736    //     double *ptr_1 = &((view_1.getData())[0]);
2737    //     double *ptr_2 = &((view_2.getData())[0]);
2738    
2739    //      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2740    // Write common data items to archive file      double *ptr_1 = &(tmp_1->getDefaultValue(0));
2741    archiveFile.write(reinterpret_cast<char *>(&dataType),sizeof(int));      double *ptr_2 = &(tmp_2->getDefaultValue(0));
   archiveFile.write(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.write(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.write(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.write(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
2742    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing to archive file");  
   }  
2743    
2744    //      // Compute an MVP for the default
2745    // Archive underlying data values for each Data type      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2746    int noValues;      // Merge the tags
2747    switch (dataType) {      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2748      case 0:      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2749        // DataEmpty      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2750        noValues = 0;      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2751        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));        tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2752        cout << "\tnoValues: " << noValues << endl;      }
2753        break;      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2754      case 1:        tmp_2->addTag(i->first);
2755        // DataConstant      }
2756        noValues = m_data->getLength();      // Compute an MVP for each tag
2757        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2758        cout << "\tnoValues: " << noValues << endl;      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2759        if (m_data->archiveData(archiveFile,noValues)) {  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2760          throw DataException("archiveData Error: problem writing data to archive file");  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2761        }  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2762        break;  //       double *ptr_0 = &view_0.getData(0);
2763      case 2:  //       double *ptr_1 = &view_1.getData(0);
2764        // DataTagged  //       double *ptr_2 = &view_2.getData(0);
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
   }  
2765    
2766    if (!archiveFile.good()) {        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2767      throw DataException("archiveData Error: problem writing data to archive file");        double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2768    }        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2769    
2770    //        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2771    // Close archive file      }
   archiveFile.close();  
2772    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem closing archive file");  
2773    }    }
2774      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2775    
2776  }      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2777        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2778        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2779        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2780        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2781        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2782        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2783        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2784        int sampleNo_0,dataPointNo_0;
2785        int numSamples_0 = arg_0_Z.getNumSamples();
2786        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2787        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2788        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2789          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2790          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2791          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2792            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2793            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2794            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2795            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2796            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2797          }
2798        }
2799    
 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");  
2800    }    }
2801      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2802    
2803    cout << "Extracting Data object from: " << fileName << endl;      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2804        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2805    int dataType;      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2806    int noSamples;      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2807    int noDPPSample;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2808    int functionSpaceType;      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2809    int dataPointRank;      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2810    int dataPointSize;      int sampleNo_0,dataPointNo_0;
2811    int dataLength;      int numSamples_0 = arg_0_Z.getNumSamples();
2812    DataArrayView::ShapeType dataPointShape;      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2813    int flatShape[4];      int offset_1 = tmp_1->getPointOffset(0,0);
2814        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2815        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2816          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2817            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2818            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2819            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2820            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2821            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2822            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2823          }
2824        }
2825    
   //  
   // Open the archive file  
   ifstream archiveFile;  
   archiveFile.open(fileName.data(), ios::in);  
2826    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem opening archive file");  
2827    }    }
2828      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2829    
2830    //      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2831    // Read common data items from archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2832    archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2833    archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));      DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2834    archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2835    archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2836    archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2837    archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2838    archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));      int sampleNo_0,dataPointNo_0;
2839    for (int dim = 0; dim < 4; dim++) {      int numSamples_0 = arg_0_Z.getNumSamples();
2840      archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2841      if (flatShape[dim]>0) {      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2842        dataPointShape.push_back(flatShape[dim]);      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2843          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2844          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2845          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2846            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2847            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2848            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2849            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2850            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2851          }
2852      }      }
2853    
2854    }    }
2855    vector<int> referenceNumbers(noSamples);    else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2856    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
2857      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2858    }      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2859    vector<int> tagNumbers(noSamples);      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2860    if (dataType==2) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2861      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2862        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2863        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2864        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2865        int sampleNo_0,dataPointNo_0;
2866        int numSamples_0 = arg_0_Z.getNumSamples();
2867        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2868        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2869        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2870          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2871            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2872            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2873            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2874            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2875            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2876            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2877            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2878          }
2879      }      }
   }  
2880    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
2881    }    }
2882      else {
2883    //      throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
   // Verify the values just read from the archive file  
   switch (dataType) {  
     case 0:  
       cout << "\tdataType: DataEmpty" << endl;  
       break;  
     case 1:  
       cout << "\tdataType: DataConstant" << endl;  
       break;  
     case 2:  
       cout << "\tdataType: DataTagged" << endl;  
       break;  
     case 3:  
       cout << "\tdataType: DataExpanded" << endl;  
       break;  
     default:  
       throw DataException("extractData Error: undefined dataType read from archive file");  
       break;  
2884    }    }
2885    
2886    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;    return res;
2887    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;  
2888    
2889    //  DataAbstract*
2890    // Verify that supplied FunctionSpace object is compatible with this Data object.  Data::borrowData() const
2891    if ( (fspace.getTypeCode()!=functionSpaceType) ||  {
2892         (fspace.getNumSamples()!=noSamples) ||    return m_data.get();
2893         (fspace.getNumDPPSample()!=noDPPSample)  }
2894       ) {  
2895      throw DataException("extractData Error: incompatible FunctionSpace");  // Not all that happy about returning a non-const from a const
2896    }  DataAbstract_ptr
2897    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  Data::borrowDataPtr() const
2898      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {  {
2899        throw DataException("extractData Error: incompatible FunctionSpace");    return m_data;
2900      }  }
2901    }  
2902    if (dataType==2) {  // Not all that happy about returning a non-const from a const
2903      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  DataReady_ptr
2904        if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {  Data::borrowReadyPtr() const
2905          throw DataException("extractData Error: incompatible FunctionSpace");  {
2906        }     DataReady_ptr dr=dynamic_pointer_cast<DataReady>(m_data);
2907       EsysAssert((dr!=0), "Error - casting to DataReady.");
2908       return dr;
2909    }
2910    
2911    std::string
2912    Data::toString() const
2913    {
2914        if (!m_data->isEmpty() &&
2915        !m_data->isLazy() &&
2916        getLength()>escriptParams.getInt("TOO_MANY_LINES"))
2917        {
2918        stringstream temp;
2919        temp << "Summary: inf="<< inf_const() << " sup=" << sup_const() << " data points=" << getNumDataPoints();
2920        return  temp.str();
2921      }      }
2922    }      return m_data->toString();
2923    }
2924    
   //  
   // Construct a DataVector to hold underlying data values  
   DataVector dataVec(dataLength);  
2925    
   //  
   // 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;  
   }  
2926    
2927    if (!archiveFile.good()) {  DataTypes::ValueType::const_reference
2928      throw DataException("extractData Error: problem reading from archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2929    }  {
2930        if (isLazy())
2931        {
2932        throw DataException("Programmer error - getDataAtOffset not permitted on lazy data (object is const which prevents resolving).");
2933        }
2934        return getReady()->getDataAtOffset(i);
2935    }
2936    
   //  
   // Close archive file  
   archiveFile.close();  
2937    
2938    if (!archiveFile.good()) {  DataTypes::ValueType::reference
2939      throw DataException("extractData Error: problem closing archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2940    }  {
2941    //     if (isLazy())
2942    //     {
2943    //  throw DataException("getDataAtOffset not permitted on lazy data.");
2944    //     }
2945        FORCERESOLVE;
2946        return getReady()->getDataAtOffset(i);
2947    }
2948    
2949    //  DataTypes::ValueType::const_reference
2950    // Construct an appropriate Data object  Data::getDataPoint(int sampleNo, int dataPointNo) const
2951    DataAbstract* tempData;  {
2952    switch (dataType) {    if (!isReady())
2953      case 0:    {
2954        // DataEmpty      throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data (object is const which prevents resolving).");
2955        tempData=new DataEmpty();    }
2956        break;    else
2957      case 1:    {
2958        // DataConstant      const DataReady* dr=getReady();
2959        tempData=new DataConstant(fspace,dataPointShape,dataVec);      return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
       break;  
     case 2:  
       // DataTagged  
       tempData=new DataTagged(fspace,dataPointShape,tagNumbers,dataVec);  
       break;  
     case 3:  
       // DataExpanded  
       tempData=new DataExpanded(fspace,dataPointShape,dataVec);  
       break;  
2960    }    }
   shared_ptr<DataAbstract> temp_data(tempData);  
   m_data=temp_data;  
2961  }  }
2962    
2963  ostream& escript::operator<<(ostream& o, const Data& data)  
2964    DataTypes::ValueType::reference
2965    Data::getDataPoint(int sampleNo, int dataPointNo)
2966  {  {
2967    o << data.toString();    FORCERESOLVE;
2968    return o;    if (!isReady())
2969      {
2970        throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2971      }
2972      else
2973      {
2974        DataReady* dr=getReady();
2975        return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2976      }
2977  }  }
2978    
2979    
2980  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2981    
2982  void  void
2983  Data::print()  Data::print()
2984  {  {
2985    int i,j;    int i,j;
2986      
2987    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2988    for( i=0; i<getNumSamples(); i++ )    for( i=0; i<getNumSamples(); i++ )
2989    {    {
# Line 2557  Data::print() Line 2993  Data::print()
2993      printf( "\n" );      printf( "\n" );
2994    }    }
2995  }  }
2996    void
2997    Data::dump(const std::string fileName) const
2998    {
2999      try
3000      {
3001        if (isLazy())
3002        {
3003          Data temp(*this); // this is to get a non-const object which we can resolve
3004          temp.resolve();
3005          temp.dump(fileName);
3006        }
3007        else
3008        {
3009              return m_data->dump(fileName);
3010        }
3011      }
3012      catch (exception& e)
3013      {
3014            cout << e.what() << endl;
3015      }
3016    }
3017    
3018  int  int
3019  Data::get_MPISize() const  Data::get_MPISize() const
3020  {  {
3021      int error, size;      int size;
3022  #ifdef PASO_MPI  #ifdef PASO_MPI
3023        int error;
3024      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
3025  #else  #else
3026      size = 1;      size = 1;
# Line 2573  Data::get_MPISize() const Line 3031  Data::get_MPISize() const
3031  int  int
3032  Data::get_MPIRank() const  Data::get_MPIRank() const
3033  {  {
3034      int error, rank;      int rank;
3035  #ifdef PASO_MPI  #ifdef PASO_MPI
3036        int error;
3037      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
3038  #else  #else
3039      rank = 0;      rank = 0;
# Line 2584  Data::get_MPIRank() const Line 3043  Data::get_MPIRank() const
3043    
3044  MPI_Comm  MPI_Comm
3045  Data::get_MPIComm() const  Data::get_MPIComm() const
3046  {  {
3047  #ifdef PASO_MPI  #ifdef PASO_MPI
3048      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
3049  #else  #else
# Line 2592  Data::get_MPIComm() const Line 3051  Data::get_MPIComm() const
3051  #endif  #endif
3052  }  }
3053    
3054    

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

  ViewVC Help
Powered by ViewVC 1.1.26