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

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

  ViewVC Help
Powered by ViewVC 1.1.26