/[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 2179 by caltinay, Thu Dec 18 00:23:55 2008 UTC
# Line 1  Line 1 
 // $Id$  
1    
2  /*  /*******************************************************
3   ************************************************************  *
4   *          Copyright 2006 by ACcESS MNRF                   *  * Copyright (c) 2003-2008 by University of Queensland
5   *                                                          *  * Earth Systems Science Computational Center (ESSCC)
6   *              http://www.access.edu.au                    *  * http://www.uq.edu.au/esscc
7   *       Primary Business: Queensland, Australia            *  *
8   *  Licensed under the Open Software License version 3.0    *  * Primary Business: Queensland, Australia
9   *     http://www.opensource.org/licenses/osl-3.0.php       *  * Licensed under the Open Software License version 3.0
10   *                                                          *  * http://www.opensource.org/licenses/osl-3.0.php
11   ************************************************************  *
12  */  *******************************************************/
13    
14    
15  #include "Data.h"  #include "Data.h"
16    
17  #include "DataExpanded.h"  #include "DataExpanded.h"
18  #include "DataConstant.h"  #include "DataConstant.h"
19  #include "DataTagged.h"  #include "DataTagged.h"
20  #include "DataEmpty.h"  #include "DataEmpty.h"
21  #include "DataArray.h"  #include "DataLazy.h"
 #include "DataArrayView.h"  
 #include "DataProf.h"  
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
25    #include "FunctionSpaceException.h"
26    #include "EscriptParams.h"
27    
28    extern "C" {
29    #include "esysUtils/blocktimer.h"
30    }
31    
32  #include <fstream>  #include <fstream>
33  #include <algorithm>  #include <algorithm>
34  #include <vector>  #include <vector>
35  #include <functional>  #include <functional>
36    #include <sstream>  // so we can throw messages about ranks
37    
38  #include <boost/python/dict.hpp>  #include <boost/python/dict.hpp>
39  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
# Line 38  using namespace boost::python; Line 44  using namespace boost::python;
44  using namespace boost;  using namespace boost;
45  using namespace escript;  using namespace escript;
46    
47  #if defined DOPROF  // ensure the current object is not a DataLazy
48  //  // The idea was that we could add an optional warning whenever a resolve is forced
49  // global table of profiling data for all Data objects  #define FORCERESOLVE if (isLazy()) {resolve();}
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;
   m_protected=false;  
   if (expanded) {  
     expand();  
   }  
 #if defined DOPROF  
   // create entry in global profiling table for this object  
   profData = dataProfTable.newData();  
 #endif  
195  }  }
196    
197    Data::Data(DataAbstract_ptr underlyingdata)
198    {
199        m_data=underlyingdata;
200        m_protected=false;
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      // Now we iterate over the elements
545      DataVector& self=getReadyPtr()->getVector();
546      const DataVector& ovec=other2.getReadyPtr()->getVector();
547      const DataVector& mvec=mask2.getReadyPtr()->getVector();
548      if ((self.size()!=ovec.size()) || (self.size()!=mvec.size()))
549      {
550        throw DataException("Error - size mismatch in arguments to copyWithMask.");
551      }
552      size_t num_points=self.size();
553    
554    mask1 = mask.wherePositive();    // OPENMP 3.0 allows unsigned loop vars.
555    mask2.copy(mask1);  #if defined(_OPENMP) && (_OPENMP < 200805)
556      long i;
557    #else
558      size_t i;
559    #endif
560      #pragma omp parallel for private(i) schedule(static)
561      for (i=0;i<num_points;++i)
562      {
563        if (mvec[i]>0)
564        {
565           self[i]=ovec[i];
566        }
567      }
568    }
569    
   mask1 *= other;  
   mask2 *= *this;  
   mask2 = *this - mask2;  
570    
   *this = mask1 + mask2;  
 }  
571    
572  bool  bool
573  Data::isExpanded() const  Data::isExpanded() const
# Line 350  Data::isTagged() const Line 583  Data::isTagged() const
583    return (temp!=0);    return (temp!=0);
584  }  }
585    
 /* TODO */  
 /* global reduction -- the local data being empty does not imply that it is empty on other processers*/  
586  bool  bool
587  Data::isEmpty() const  Data::isEmpty() const
588  {  {
# Line 366  Data::isConstant() const Line 597  Data::isConstant() const
597    return (temp!=0);    return (temp!=0);
598  }  }
599    
600    bool
601    Data::isLazy() const
602    {
603      return m_data->isLazy();
604    }
605    
606    // at the moment this is synonymous with !isLazy() but that could change
607    bool
608    Data::isReady() const
609    {
610      return (dynamic_cast<DataReady*>(m_data.get())!=0);
611    }
612    
613    
614  void  void
615  Data::setProtection()  Data::setProtection()
616  {  {
617     m_protected=true;     m_protected=true;
618  }  }
619    
620  bool  bool
621  Data::isProtected() const  Data::isProtected() const
622  {  {
623     return m_protected;     return m_protected;
624  }  }
625    
# Line 386  Data::expand() Line 631  Data::expand()
631    if (isConstant()) {    if (isConstant()) {
632      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
633      DataAbstract* temp=new DataExpanded(*tempDataConst);      DataAbstract* temp=new DataExpanded(*tempDataConst);
634      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
635      m_data=temp_data;  //     m_data=temp_data;
636        m_data=temp->getPtr();
637    } else if (isTagged()) {    } else if (isTagged()) {
638      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
639      DataAbstract* temp=new DataExpanded(*tempDataTag);      DataAbstract* temp=new DataExpanded(*tempDataTag);
640      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
641      m_data=temp_data;  //     m_data=temp_data;
642        m_data=temp->getPtr();
643    } else if (isExpanded()) {    } else if (isExpanded()) {
644      //      //
645      // do nothing      // do nothing
646    } else if (isEmpty()) {    } else if (isEmpty()) {
647      throw DataException("Error - Expansion of DataEmpty not possible.");      throw DataException("Error - Expansion of DataEmpty not possible.");
648      } else if (isLazy()) {
649        resolve();
650        expand();       // resolve might not give us expanded data
651    } else {    } else {
652      throw DataException("Error - Expansion not implemented for this Data type.");      throw DataException("Error - Expansion not implemented for this Data type.");
653    }    }
# Line 409  Data::tag() Line 659  Data::tag()
659    if (isConstant()) {    if (isConstant()) {
660      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
661      DataAbstract* temp=new DataTagged(*tempDataConst);      DataAbstract* temp=new DataTagged(*tempDataConst);
662      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
663      m_data=temp_data;  //     m_data=temp_data;
664        m_data=temp->getPtr();
665    } else if (isTagged()) {    } else if (isTagged()) {
666      // do nothing      // do nothing
667    } else if (isExpanded()) {    } else if (isExpanded()) {
668      throw DataException("Error - Creating tag data from DataExpanded not possible.");      throw DataException("Error - Creating tag data from DataExpanded not possible.");
669    } else if (isEmpty()) {    } else if (isEmpty()) {
670      throw DataException("Error - Creating tag data from DataEmpty not possible.");      throw DataException("Error - Creating tag data from DataEmpty not possible.");
671      } else if (isLazy()) {
672         DataAbstract_ptr res=m_data->resolve();
673         if (m_data->isExpanded())
674         {
675        throw DataException("Error - data would resolve to DataExpanded, tagging is not possible.");
676         }
677         m_data=res;    
678         tag();
679    } else {    } else {
680      throw DataException("Error - Tagging not implemented for this Data type.");      throw DataException("Error - Tagging not implemented for this Data type.");
681    }    }
682  }  }
683    
684  void  void
685  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::resolve()
686    {
687      if (isLazy())
688      {
689         m_data=m_data->resolve();
690      }
691    }
692    
693    
694    Data
695    Data::oneOver() const
696  {  {
697    m_data->reshapeDataPoint(shape);    MAKELAZYOP(RECIP)
698      return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
699  }  }
700    
701  Data  Data
702  Data::wherePositive() const  Data::wherePositive() const
703  {  {
704  #if defined DOPROF    MAKELAZYOP(GZ)
705    profData->where++;    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
 #endif  
   return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));  
706  }  }
707    
708  Data  Data
709  Data::whereNegative() const  Data::whereNegative() const
710  {  {
711  #if defined DOPROF    MAKELAZYOP(LZ)
712    profData->where++;    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
 #endif  
   return escript::unaryOp(*this,bind2nd(less<double>(),0.0));  
713  }  }
714    
715  Data  Data
716  Data::whereNonNegative() const  Data::whereNonNegative() const
717  {  {
718  #if defined DOPROF    MAKELAZYOP(GEZ)
719    profData->where++;    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
 #endif  
   return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));  
720  }  }
721    
722  Data  Data
723  Data::whereNonPositive() const  Data::whereNonPositive() const
724  {  {
725  #if defined DOPROF    MAKELAZYOP(LEZ)
726    profData->where++;    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
 #endif  
   return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));  
727  }  }
728    
729  Data  Data
730  Data::whereZero(double tol) const  Data::whereZero(double tol) const
731  {  {
732  #if defined DOPROF  //   Data dataAbs=abs();
733    profData->where++;  //   return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
734  #endif     MAKELAZYOPOFF(EZ,tol)
735    Data dataAbs=abs();     return C_TensorUnaryOperation(*this, bind2nd(AbsLTE(),tol));
736    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));  
737  }  }
738    
739  Data  Data
740  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
741  {  {
742  #if defined DOPROF  //   Data dataAbs=abs();
743    profData->where++;  //   return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
744  #endif    MAKELAZYOPOFF(NEZ,tol)
745    Data dataAbs=abs();    return C_TensorUnaryOperation(*this, bind2nd(AbsGT(),tol));
746    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));  
747  }  }
748    
749  Data  Data
750  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
751  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
752    return Data(*this,functionspace);    return Data(*this,functionspace);
753  }  }
754    
755  bool  bool
756  Data::probeInterpolation(const FunctionSpace& functionspace) const  Data::probeInterpolation(const FunctionSpace& functionspace) const
757  {  {
758    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());  
     }  
   }  
759  }  }
760    
761  Data  Data
762  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
763  {  {
764  #if defined DOPROF    if (isEmpty())
765    profData->grad++;    {
766  #endif      throw DataException("Error - operation not permitted on instances of DataEmpty.");
767      }
768      double blocktimer_start = blocktimer_time();
769    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
770      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
771    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
772    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
773    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
774    getDomain().setToGradient(out,*this);    getDomain()->setToGradient(out,*this);
775      blocktimer_increment("grad()", blocktimer_start);
776    return out;    return out;
777  }  }
778    
779  Data  Data
780  Data::grad() const  Data::grad() const
781  {  {
782    return gradOn(escript::function(getDomain()));    if (isEmpty())
783      {
784        throw DataException("Error - operation not permitted on instances of DataEmpty.");
785      }
786      return gradOn(escript::function(*getDomain()));
787  }  }
788    
789  int  int
790  Data::getDataPointSize() const  Data::getDataPointSize() const
791  {  {
792    return getPointDataView().noValues();    return m_data->getNoValues();
793  }  }
794    
795  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
796  Data::getLength() const  Data::getLength() const
797  {  {
798    return m_data->getLength();    return m_data->getLength();
799  }  }
800    
 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);  
 }  
   
801  const  const
802  boost::python::numeric::array  boost::python::numeric::array
803  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
804  {  {
805    //    int i, j, k, l;
806    // determine the total number of data points  
807    int numSamples = getNumSamples();    FORCERESOLVE;
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
808    
809    //    //
810    // determine the rank and shape of each data point    // determine the rank and shape of each data point
811    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
812    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
813    
814    //    //
815    // create the numeric array to be returned    // create the numeric array to be returned
816    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
817    
818    //    //
819    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
820    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
821    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
822    // 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]);  
   }  
823    
824    //    //
825    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
826      if (arrayRank==0) {
827        numArray.resize(1);
828      }
829    if (arrayRank==1) {    if (arrayRank==1) {
830      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
831    }    }
# Line 623  Data::convertToNumArray() Line 838  Data::convertToNumArray()
838    if (arrayRank==4) {    if (arrayRank==4) {
839      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
840    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
841    
842    //    if (getNumDataPointsPerSample()>0) {
843    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
844    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
845    int dataPoint = 0;         //
846    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
847      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
848        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
849        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);  
               }  
             }  
           }  
         }  
       }  
       dataPoint++;  
     }  
   }  
850    
851           //
852           // Check a valid data point number has been supplied
853           if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
854               throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
855           }
856           // TODO: global error handling
857           // create a view of the data if it is stored locally
858    //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
859           DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
860    
861    
862           switch( dataPointRank ){
863                case 0 :
864                    numArray[0] = getDataAtOffset(offset);
865                    break;
866                case 1 :
867                    for( i=0; i<dataPointShape[0]; i++ )
868                        numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
869                    break;
870                case 2 :
871                    for( i=0; i<dataPointShape[0]; i++ )
872                        for( j=0; j<dataPointShape[1]; j++)
873                            numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
874                    break;
875                case 3 :
876                    for( i=0; i<dataPointShape[0]; i++ )
877                        for( j=0; j<dataPointShape[1]; j++ )
878                            for( k=0; k<dataPointShape[2]; k++)
879                                numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
880                    break;
881                case 4 :
882                    for( i=0; i<dataPointShape[0]; i++ )
883                        for( j=0; j<dataPointShape[1]; j++ )
884                            for( k=0; k<dataPointShape[2]; k++ )
885                                for( l=0; l<dataPointShape[3]; l++)
886                                    numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
887                    break;
888        }
889      }
890    //    //
891    // return the loaded array    // return the array
892    return numArray;    return numArray;
893  }  }
894    
895  const  void
896  boost::python::numeric::array  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
 Data::convertToNumArrayFromSampleNo(int sampleNo)  
897  {  {
898    //      // this will throw if the value cannot be represented
899    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
900    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
901      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();  
902    
903    void
904    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
905    {
906      if (isProtected()) {
907            throw DataException("Error - attempt to update protected Data object.");
908      }
909      FORCERESOLVE;
910    //    //
911    // create the numeric array to be returned    // check rank
912    boost::python::numeric::array numArray(0.0);    if (static_cast<unsigned int>(num_array.getrank())<getDataPointRank())
913          throw DataException("Rank of numarray does not match Data object rank");
914    
915    //    //
916    // the rank of the returned numeric array will be the rank of    // check shape of num_array
917    // the data points, plus one. Where the rank of the array is n,    for (unsigned int i=0; i<getDataPointRank(); i++) {
918    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
919    // 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]);  
920    }    }
   
921    //    //
922    // resize the numeric array to the shape just calculated    // make sure data is expanded:
923    if (arrayRank==1) {    //
924      numArray.resize(arrayShape[0]);    if (!isExpanded()) {
925    }      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]);  
926    }    }
927    if (arrayRank==5) {    if (getNumDataPointsPerSample()>0) {
928      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);         int sampleNo = dataPointNo/getNumDataPointsPerSample();
929           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
930           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
931      } else {
932           m_data->copyToDataPoint(-1, 0,num_array);
933    }    }
934    }
935    
936    //  void
937    // loop through each data point in turn, loading the values for that data point  Data::setValueOfDataPoint(int dataPointNo, const double value)
938    // into the numeric array.  {
939    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    if (isProtected()) {
940      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);  
             }  
           }  
         }  
       }  
     }  
941    }    }
   
942    //    //
943    // return the loaded array    // make sure data is expanded:
944    return numArray;    FORCERESOLVE;
945      if (!isExpanded()) {
946        expand();
947      }
948      if (getNumDataPointsPerSample()>0) {
949           int sampleNo = dataPointNo/getNumDataPointsPerSample();
950           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
951           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
952      } else {
953           m_data->copyToDataPoint(-1, 0,value);
954      }
955  }  }
956    
957  const  const
958  boost::python::numeric::array  boost::python::numeric::array
959  Data::convertToNumArrayFromDPNo(int procNo,  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
                                 int sampleNo,  
                                                                 int dataPointNo)  
   
960  {  {
961      size_t length=0;    size_t length=0;
962      int i, j, k, l, pos;    int i, j, k, l, pos;
963      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.");  
   }  
   
964    //    //
965    // determine the rank and shape of each data point    // determine the rank and shape of each data point
966    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
967    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
968    
969    //    //
970    // create the numeric array to be returned    // create the numeric array to be returned
# Line 815  Data::convertToNumArrayFromDPNo(int proc Line 974  Data::convertToNumArrayFromDPNo(int proc
974    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
975    // as that of the data point    // as that of the data point
976    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
977    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
978    
979    //    //
980    // 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 994  Data::convertToNumArrayFromDPNo(int proc
994      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
995    }    }
996    
997      // added for the MPI communication    // added for the MPI communication
998      length=1;    length=1;
999      for( i=0; i<arrayRank; i++ )    for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
1000          length *= arrayShape[i];    double *tmpData = new double[length];
     double *tmpData = new double[length];  
1001    
1002    //    //
1003    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
1004    
1005      // updated for the MPI case      // updated for the MPI case
1006      if( get_MPIRank()==procNo ){      if( get_MPIRank()==procNo ){
1007                 if (getNumDataPointsPerSample()>0) {
1008                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
1009                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
1010                    //
1011                    // Check a valid sample number has been supplied
1012                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
1013                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
1014                    }
1015    
1016                    //
1017                    // Check a valid data point number has been supplied
1018                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
1019                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
1020                    }
1021                    // TODO: global error handling
1022          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
1023          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
1024                    DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
1025    
1026          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
1027          pos=0;          pos=0;
1028          switch( dataPointRank ){          switch( dataPointRank ){
1029              case 0 :              case 0 :
1030                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
1031                  break;                  break;
1032              case 1 :                      case 1 :
1033                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1034                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
1035                  break;                  break;
1036              case 2 :                      case 2 :
1037                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1038                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
1039                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
1040                  break;                  break;
1041              case 3 :                      case 3 :
1042                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1043                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
1044                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
1045                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
1046                  break;                  break;
1047              case 4 :              case 4 :
1048                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
1049                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
1050                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
1051                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
1052                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
1053                  break;                  break;
1054          }          }
1055                }
1056      }      }
1057  #ifdef PASO_MPI          #ifdef PASO_MPI
1058          // broadcast the data to all other processes          // broadcast the data to all other processes
1059          MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
1060  #endif          #endif
1061    
1062      // unpack the data      // unpack the data
1063      switch( dataPointRank ){      switch( dataPointRank ){
1064          case 0 :          case 0 :
1065              numArray[i]=tmpData[0];              numArray[0]=tmpData[0];
1066              break;              break;
1067          case 1 :                  case 1 :
1068              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1069                  numArray[i]=tmpData[i];                  numArray[i]=tmpData[i];
1070              break;              break;
1071          case 2 :                  case 2 :
1072              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1073                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1074                      tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
1075              break;              break;
1076          case 3 :                  case 3 :
1077              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1078                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1079                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
1080                          tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];                          numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
1081              break;              break;
1082          case 4 :          case 4 :
1083              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1084                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1085                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
1086                          for( l=0; l<dataPointShape[3]; l++ )                          for( l=0; l<dataPointShape[3]; l++ )
1087                              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]))];
1088              break;              break;
1089      }      }
1090    
1091      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);  
           }  
         }  
       }  
     }  
   }  
 */  
   
1092    //    //
1093    // return the loaded array    // return the loaded array
1094    return numArray;    return numArray;
1095  }  }
1096    
1097    
1098    boost::python::numeric::array
1099    Data::integrate_const() const
1100    {
1101      if (isLazy())
1102      {
1103        throw DataException("Error - cannot integrate for constant lazy data.");
1104      }
1105      return integrateWorker();
1106    }
1107    
1108    boost::python::numeric::array
1109    Data::integrate()
1110    {
1111      if (isLazy())
1112      {
1113        expand();
1114      }
1115      return integrateWorker();
1116    }
1117    
1118    
1119    
1120  boost::python::numeric::array  boost::python::numeric::array
1121  Data::integrate() const  Data::integrateWorker() const
1122  {  {
1123    int index;    int index;
1124    int rank = getDataPointRank();    int rank = getDataPointRank();
1125    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
1126      int dataPointSize = getDataPointSize();
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
1127    
1128    //    //
1129    // calculate the integral values    // calculate the integral values
1130    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
1131    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    vector<double> integrals_local(dataPointSize);
1132      const AbstractContinuousDomain* dom=dynamic_cast<const AbstractContinuousDomain*>(getDomain().get());
1133      if (dom==0)
1134      {            
1135        throw DataException("Can not integrate over non-continuous domains.");
1136      }
1137    #ifdef PASO_MPI
1138      dom->setToIntegrals(integrals_local,*this);
1139      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
1140      double *tmp = new double[dataPointSize];
1141      double *tmp_local = new double[dataPointSize];
1142      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
1143      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
1144      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
1145      delete[] tmp;
1146      delete[] tmp_local;
1147    #else
1148      dom->setToIntegrals(integrals,*this);
1149    #endif
1150    
1151    //    //
1152    // create the numeric array to be returned    // create the numeric array to be returned
# Line 1031  Data::integrate() const Line 1206  Data::integrate() const
1206  Data  Data
1207  Data::sin() const  Data::sin() const
1208  {  {
1209  #if defined DOPROF    MAKELAZYOP(SIN)
1210    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);  
1211  }  }
1212    
1213  Data  Data
1214  Data::cos() const  Data::cos() const
1215  {  {
1216  #if defined DOPROF    MAKELAZYOP(COS)
1217    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);  
1218  }  }
1219    
1220  Data  Data
1221  Data::tan() const  Data::tan() const
1222  {  {
1223  #if defined DOPROF    MAKELAZYOP(TAN)
1224    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);  
1225  }  }
1226    
1227  Data  Data
1228  Data::asin() const  Data::asin() const
1229  {  {
1230  #if defined DOPROF    MAKELAZYOP(ASIN)
1231    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);  
1232  }  }
1233    
1234  Data  Data
1235  Data::acos() const  Data::acos() const
1236  {  {
1237  #if defined DOPROF    MAKELAZYOP(ACOS)
1238    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);  
1239  }  }
1240    
1241    
1242  Data  Data
1243  Data::atan() const  Data::atan() const
1244  {  {
1245  #if defined DOPROF    MAKELAZYOP(ATAN)
1246    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);  
1247  }  }
1248    
1249  Data  Data
1250  Data::sinh() const  Data::sinh() const
1251  {  {
1252  #if defined DOPROF    MAKELAZYOP(SINH)
1253    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);  
1254  }  }
1255    
1256  Data  Data
1257  Data::cosh() const  Data::cosh() const
1258  {  {
1259  #if defined DOPROF    MAKELAZYOP(COSH)
1260    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);  
1261  }  }
1262    
1263  Data  Data
1264  Data::tanh() const  Data::tanh() const
1265  {  {
1266  #if defined DOPROF    MAKELAZYOP(TANH)
1267    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1268    }
1269    
1270    
1271    Data
1272    Data::erf() const
1273    {
1274    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1275      throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1276    #else
1277      MAKELAZYOP(ERF)
1278      return C_TensorUnaryOperation(*this, ::erf);
1279  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);  
1280  }  }
1281    
1282  Data  Data
1283  Data::asinh() const  Data::asinh() const
1284  {  {
1285  #if defined DOPROF    MAKELAZYOP(ASINH)
1286    profData->unary++;  #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1287      return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1288    #else
1289      return C_TensorUnaryOperation(*this, ::asinh);
1290  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);  
1291  }  }
1292    
1293  Data  Data
1294  Data::acosh() const  Data::acosh() const
1295  {  {
1296  #if defined DOPROF    MAKELAZYOP(ACOSH)
1297    profData->unary++;  #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1298      return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1299    #else
1300      return C_TensorUnaryOperation(*this, ::acosh);
1301  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);  
1302  }  }
1303    
1304  Data  Data
1305  Data::atanh() const  Data::atanh() const
1306  {  {
1307  #if defined DOPROF    MAKELAZYOP(ATANH)
1308    profData->unary++;  #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1309      return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1310    #else
1311      return C_TensorUnaryOperation(*this, ::atanh);
1312  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);  
1313  }  }
1314    
1315  Data  Data
1316  Data::log10() const  Data::log10() const
1317  {  {
1318  #if defined DOPROF    MAKELAZYOP(LOG10)
1319    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);  
1320  }  }
1321    
1322  Data  Data
1323  Data::log() const  Data::log() const
1324  {  {
1325  #if defined DOPROF    MAKELAZYOP(LOG)
1326    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);  
1327  }  }
1328    
1329  Data  Data
1330  Data::sign() const  Data::sign() const
1331  {  {
1332  #if defined DOPROF    MAKELAZYOP(SIGN)
1333    profData->unary++;    return C_TensorUnaryOperation(*this, escript::fsign);
 #endif  
   return escript::unaryOp(*this,escript::fsign);  
1334  }  }
1335    
1336  Data  Data
1337  Data::abs() const  Data::abs() const
1338  {  {
1339  #if defined DOPROF    MAKELAZYOP(ABS)
1340    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);  
1341  }  }
1342    
1343  Data  Data
1344  Data::neg() const  Data::neg() const
1345  {  {
1346  #if defined DOPROF    MAKELAZYOP(NEG)
1347    profData->unary++;    return C_TensorUnaryOperation(*this, negate<double>());
 #endif  
   return escript::unaryOp(*this,negate<double>());  
1348  }  }
1349    
1350  Data  Data
1351  Data::pos() const  Data::pos() const
1352  {  {
1353  #if defined DOPROF      // not doing lazy check here is deliberate.
1354    profData->unary++;      // since a deep copy of lazy data should be cheap, I'll just let it happen now
 #endif  
1355    Data result;    Data result;
1356    // perform a deep copy    // perform a deep copy
1357    result.copy(*this);    result.copy(*this);
# Line 1196  Data::pos() const Line 1361  Data::pos() const
1361  Data  Data
1362  Data::exp() const  Data::exp() const
1363  {  {
1364  #if defined DOPROF    MAKELAZYOP(EXP)
1365    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);  
1366  }  }
1367    
1368  Data  Data
1369  Data::sqrt() const  Data::sqrt() const
1370  {  {
1371  #if defined DOPROF    MAKELAZYOP(SQRT)
1372    profData->unary++;    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);  
1373  }  }
1374    
1375  double  double
1376  Data::Lsup() const  Data::Lsup_const() const
1377  {  {
1378    double localValue, globalValue;     if (isLazy())
1379  #if defined DOPROF     {
1380    profData->reduction1++;      throw DataException("Error - cannot compute Lsup for constant lazy data.");
1381  #endif     }
1382    //     return LsupWorker();
1383    // set the initial absolute maximum value to zero  }
1384    
1385    AbsMax abs_max_func;  double
1386    localValue = algorithm(abs_max_func,0);  Data::Lsup()
1387  #ifdef PASO_MPI  {
1388    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );     if (isLazy())
1389    return globalValue;     {
1390  #else      resolve();
1391    return localValue;     }
1392  #endif     return LsupWorker();
1393  }  }
1394    
1395  double  double
1396  Data::Linf() const  Data::sup_const() const
1397  {  {
1398    double localValue, globalValue;     if (isLazy())
1399  #if defined DOPROF     {
1400    profData->reduction1++;      throw DataException("Error - cannot compute sup for constant lazy data.");
1401  #endif     }
1402       return supWorker();
1403    }
1404    
1405    double
1406    Data::sup()
1407    {
1408       if (isLazy())
1409       {
1410        resolve();
1411       }
1412       return supWorker();
1413    }
1414    
1415    double
1416    Data::inf_const() const
1417    {
1418       if (isLazy())
1419       {
1420        throw DataException("Error - cannot compute inf for constant lazy data.");
1421       }
1422       return infWorker();
1423    }
1424    
1425    double
1426    Data::inf()
1427    {
1428       if (isLazy())
1429       {
1430        resolve();
1431       }
1432       return infWorker();
1433    }
1434    
1435    double
1436    Data::LsupWorker() const
1437    {
1438      double localValue;
1439    //    //
1440    // 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());  
1441    
1442      AbsMax abs_max_func;
1443      localValue = algorithm(abs_max_func,0);
1444  #ifdef PASO_MPI  #ifdef PASO_MPI
1445    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    double globalValue;
1446      MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1447    return globalValue;    return globalValue;
1448  #else  #else
1449    return localValue;    return localValue;
# Line 1252  Data::Linf() const Line 1451  Data::Linf() const
1451  }  }
1452    
1453  double  double
1454  Data::sup() const  Data::supWorker() const
1455  {  {
1456    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1457    //    //
1458    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1459    FMax fmax_func;    FMax fmax_func;
1460    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1461  #ifdef PASO_MPI  #ifdef PASO_MPI
1462      double globalValue;
1463    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1464    return globalValue;    return globalValue;
1465  #else  #else
# Line 1271  Data::sup() const Line 1468  Data::sup() const
1468  }  }
1469    
1470  double  double
1471  Data::inf() const  Data::infWorker() const
1472  {  {
1473    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1474    //    //
1475    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1476    FMin fmin_func;    FMin fmin_func;
1477    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1478  #ifdef PASO_MPI  #ifdef PASO_MPI
1479      double globalValue;
1480    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1481    return globalValue;    return globalValue;
1482  #else  #else
# Line 1294  Data::inf() const Line 1489  Data::inf() const
1489  Data  Data
1490  Data::maxval() const  Data::maxval() const
1491  {  {
1492  #if defined DOPROF    if (isLazy())
1493    profData->reduction2++;    {
1494  #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1495        temp.resolve();
1496        return temp.maxval();
1497      }
1498    //    //
1499    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1500    FMax fmax_func;    FMax fmax_func;
# Line 1306  Data::maxval() const Line 1504  Data::maxval() const
1504  Data  Data
1505  Data::minval() const  Data::minval() const
1506  {  {
1507  #if defined DOPROF    if (isLazy())
1508    profData->reduction2++;    {
1509  #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1510        temp.resolve();
1511        return temp.minval();
1512      }
1513    //    //
1514    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1515    FMin fmin_func;    FMin fmin_func;
# Line 1316  Data::minval() const Line 1517  Data::minval() const
1517  }  }
1518    
1519  Data  Data
1520  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1521  {  {
1522  #if defined DOPROF       if (isLazy())
1523    profData->reduction2++;       {
1524  #endif      Data temp(*this);
1525    Trace trace_func;      temp.resolve();
1526    return dp_algorithm(trace_func,0);      return temp.swapaxes(axis0,axis1);
1527         }
1528         int axis0_tmp,axis1_tmp;
1529         DataTypes::ShapeType s=getDataPointShape();
1530         DataTypes::ShapeType ev_shape;
1531         // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1532         // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1533         int rank=getDataPointRank();
1534         if (rank<2) {
1535            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1536         }
1537         if (axis0<0 || axis0>rank-1) {
1538            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1539         }
1540         if (axis1<0 || axis1>rank-1) {
1541             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1542         }
1543         if (axis0 == axis1) {
1544             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1545         }
1546         if (axis0 > axis1) {
1547             axis0_tmp=axis1;
1548             axis1_tmp=axis0;
1549         } else {
1550             axis0_tmp=axis0;
1551             axis1_tmp=axis1;
1552         }
1553         for (int i=0; i<rank; i++) {
1554           if (i == axis0_tmp) {
1555              ev_shape.push_back(s[axis1_tmp]);
1556           } else if (i == axis1_tmp) {
1557              ev_shape.push_back(s[axis0_tmp]);
1558           } else {
1559              ev_shape.push_back(s[i]);
1560           }
1561         }
1562         Data ev(0.,ev_shape,getFunctionSpace());
1563         ev.typeMatchRight(*this);
1564         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1565         return ev;
1566    
1567  }  }
1568    
1569  Data  Data
1570  Data::symmetric() const  Data::symmetric() const
1571  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1572       // check input       // check input
1573       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1574       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1575          if(s[0] != s[1])          if(s[0] != s[1])
1576             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.");
1577       }       }
1578       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
# Line 1344  Data::symmetric() const Line 1582  Data::symmetric() const
1582       else {       else {
1583          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.");
1584       }       }
1585         MAKELAZYOP(SYM)
1586       Data ev(0.,getDataPointShape(),getFunctionSpace());       Data ev(0.,getDataPointShape(),getFunctionSpace());
1587       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1588       m_data->symmetric(ev.m_data.get());       m_data->symmetric(ev.m_data.get());
# Line 1353  Data::symmetric() const Line 1592  Data::symmetric() const
1592  Data  Data
1593  Data::nonsymmetric() const  Data::nonsymmetric() const
1594  {  {
1595       #if defined DOPROF       MAKELAZYOP(NSYM)
         profData->unary++;  
      #endif  
1596       // check input       // check input
1597       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1598       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1599          if(s[0] != s[1])          if(s[0] != s[1])
1600             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.");
1601          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1602          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1603          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1604          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1372  Data::nonsymmetric() const Line 1609  Data::nonsymmetric() const
1609       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1610          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1611             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.");
1612          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1613          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1614          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1615          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1388  Data::nonsymmetric() const Line 1625  Data::nonsymmetric() const
1625  }  }
1626    
1627  Data  Data
1628  Data::matrixtrace(int axis_offset) const  Data::trace(int axis_offset) const
1629  {  {    
1630       #if defined DOPROF       MAKELAZYOPOFF(TRACE,axis_offset)
1631          profData->unary++;       DataTypes::ShapeType s=getDataPointShape();
      #endif  
      DataArrayView::ShapeType s=getDataPointShape();  
1632       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1633          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1634          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1635          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1636          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1637          return ev;          return ev;
1638       }       }
1639       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1640          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1641          if (axis_offset==0) {          if (axis_offset==0) {
1642            int s2=s[2];            int s2=s[2];
1643            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1413  Data::matrixtrace(int axis_offset) const Line 1648  Data::matrixtrace(int axis_offset) const
1648          }          }
1649          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1650          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1651          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1652          return ev;          return ev;
1653       }       }
1654       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1655          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1656          if (axis_offset==0) {          if (axis_offset==0) {
1657            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1658            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1432  Data::matrixtrace(int axis_offset) const Line 1667  Data::matrixtrace(int axis_offset) const
1667      }      }
1668          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1669          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1670      m_data->matrixtrace(ev.m_data.get(), axis_offset);      m_data->trace(ev.m_data.get(), axis_offset);
1671          return ev;          return ev;
1672       }       }
1673       else {       else {
1674          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.");
1675       }       }
1676  }  }
1677    
1678  Data  Data
1679  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1680  {  {    
1681  #if defined DOPROF       MAKELAZYOPOFF(TRANS,axis_offset)
1682       profData->reduction2++;       DataTypes::ShapeType s=getDataPointShape();
1683  #endif       DataTypes::ShapeType ev_shape;
      DataArrayView::ShapeType s=getDataPointShape();  
      DataArrayView::ShapeType ev_shape;  
1684       // 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]
1685       // 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)
1686       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1467  Data::transpose(int axis_offset) const Line 1700  Data::transpose(int axis_offset) const
1700  Data  Data
1701  Data::eigenvalues() const  Data::eigenvalues() const
1702  {  {
1703       #if defined DOPROF       if (isLazy())
1704          profData->unary++;       {
1705       #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1706        temp.resolve();
1707        return temp.eigenvalues();
1708         }
1709       // check input       // check input
1710       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1711       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1712          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.");
1713       if(s[0] != s[1])       if(s[0] != s[1])
1714          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.");
1715       // create return       // create return
1716       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1717       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1718       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1719       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1487  Data::eigenvalues() const Line 1723  Data::eigenvalues() const
1723  const boost::python::tuple  const boost::python::tuple
1724  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1725  {  {
1726       #if defined DOPROF       if (isLazy())
1727          profData->unary++;       {
1728       #endif      Data temp(*this);   // to get around the fact that you can't resolve a const Data
1729       DataArrayView::ShapeType s=getDataPointShape();      temp.resolve();
1730       if (getDataPointRank()!=2)      return temp.eigenvalues_and_eigenvectors(tol);
1731         }
1732         DataTypes::ShapeType s=getDataPointShape();
1733         if (getDataPointRank()!=2)
1734          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.");
1735       if(s[0] != s[1])       if(s[0] != s[1])
1736          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.");
1737       // create return       // create return
1738       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1739       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1740       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1741       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1742       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1743       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1744       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 1746  Data::eigenvalues_and_eigenvectors(const
1746  }  }
1747    
1748  const boost::python::tuple  const boost::python::tuple
1749  Data::mindp() const  Data::minGlobalDataPoint() const
1750  {  {
1751    // 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
1752    // 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
1753    // surrounding function    // surrounding function
1754    
   int SampleNo;  
1755    int DataPointNo;    int DataPointNo;
1756      int ProcNo;    int ProcNo;
1757    calc_mindp(ProcNo,SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1758    return make_tuple(ProcNo,SampleNo,DataPointNo);    return make_tuple(ProcNo,DataPointNo);
1759  }  }
1760    
1761  void  void
1762  Data::calc_mindp(   int& ProcNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1763                  int& SampleNo,                          int& DataPointNo) const
         int& DataPointNo) const  
1764  {  {
1765      if (isLazy())
1766      {
1767        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1768        temp.resolve();
1769        return temp.calc_minGlobalDataPoint(ProcNo,DataPointNo);
1770      }
1771    int i,j;    int i,j;
1772    int lowi=0,lowj=0;    int lowi=0,lowj=0;
1773    double min=numeric_limits<double>::max();    double min=numeric_limits<double>::max();
# Line 1535  Data::calc_mindp(  int& ProcNo, Line 1778  Data::calc_mindp(  int& ProcNo,
1778    int numDPPSample=temp.getNumDataPointsPerSample();    int numDPPSample=temp.getNumDataPointsPerSample();
1779    
1780    double next,local_min;    double next,local_min;
1781    int local_lowi,local_lowj;    int local_lowi=0,local_lowj=0;    
1782    
1783    #pragma omp parallel private(next,local_min,local_lowi,local_lowj)    #pragma omp parallel firstprivate(local_lowi,local_lowj) private(next,local_min)
1784    {    {
1785      local_min=min;      local_min=min;
1786      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1787      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1788        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1789          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1790          if (next<local_min) {          if (next<local_min) {
1791            local_min=next;            local_min=next;
1792            local_lowi=i;            local_lowi=i;
# Line 1561  Data::calc_mindp(  int& ProcNo, Line 1804  Data::calc_mindp(  int& ProcNo,
1804    
1805  #ifdef PASO_MPI  #ifdef PASO_MPI
1806      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1807      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1808      int lowProc = 0;      int lowProc = 0;
1809      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1810      int error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );      int error;
1811            error = MPI_Gather ( &next, 1, MPI_DOUBLE, globalMins, 1, MPI_DOUBLE, 0, get_MPIComm() );
1812    
1813      if( get_MPIRank()==0 ){      if( get_MPIRank()==0 ){
1814          next = globalMins[lowProc];          next = globalMins[lowProc];
1815          for( i=1; i<get_MPISize(); i++ )          for( i=1; i<get_MPISize(); i++ )
# Line 1581  Data::calc_mindp(  int& ProcNo, Line 1825  Data::calc_mindp(  int& ProcNo,
1825  #else  #else
1826      ProcNo = 0;      ProcNo = 0;
1827  #endif  #endif
1828    SampleNo = lowi;    DataPointNo = lowj + lowi * numDPPSample;
   DataPointNo = lowj;  
1829  }  }
1830    
1831  void  void
1832  Data::saveDX(std::string fileName) const  Data::saveDX(std::string fileName) const
1833  {  {
1834      if (isEmpty())
1835      {
1836        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1837      }
1838      if (isLazy())
1839      {
1840         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1841         temp.resolve();
1842         temp.saveDX(fileName);
1843         return;
1844      }
1845    boost::python::dict args;    boost::python::dict args;
1846    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1847    getDomain().saveDX(fileName,args);    getDomain()->saveDX(fileName,args);
1848    return;    return;
1849  }  }
1850    
1851  void  void
1852  Data::saveVTK(std::string fileName) const  Data::saveVTK(std::string fileName) const
1853  {  {
1854      if (isEmpty())
1855      {
1856        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1857      }
1858      if (isLazy())
1859      {
1860         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1861         temp.resolve();
1862         temp.saveVTK(fileName);
1863         return;
1864      }
1865    boost::python::dict args;    boost::python::dict args;
1866    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1867    getDomain().saveVTK(fileName,args);    getDomain()->saveVTK(fileName,args);
1868    return;    return;
1869  }  }
1870    
# Line 1609  Data::operator+=(const Data& right) Line 1874  Data::operator+=(const Data& right)
1874    if (isProtected()) {    if (isProtected()) {
1875          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1876    }    }
1877  #if defined DOPROF    MAKELAZYBINSELF(right,ADD)    // for lazy + is equivalent to +=
   profData->binary++;  
 #endif  
1878    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1879    return (*this);    return (*this);
1880  }  }
# Line 1622  Data::operator+=(const boost::python::ob Line 1885  Data::operator+=(const boost::python::ob
1885    if (isProtected()) {    if (isProtected()) {
1886          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1887    }    }
1888  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1889    profData->binary++;    MAKELAZYBINSELF(tmp,ADD)
1890  #endif    binaryOp(tmp,plus<double>());
1891    binaryOp(right,plus<double>());    return (*this);
1892    }
1893    
1894    // Hmmm, operator= makes a deep copy but the copy constructor does not?
1895    Data&
1896    Data::operator=(const Data& other)
1897    {
1898      copy(other);
1899    return (*this);    return (*this);
1900  }  }
1901    
# Line 1635  Data::operator-=(const Data& right) Line 1905  Data::operator-=(const Data& right)
1905    if (isProtected()) {    if (isProtected()) {
1906          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1907    }    }
1908  #if defined DOPROF    MAKELAZYBINSELF(right,SUB)
   profData->binary++;  
 #endif  
1909    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1910    return (*this);    return (*this);
1911  }  }
# Line 1648  Data::operator-=(const boost::python::ob Line 1916  Data::operator-=(const boost::python::ob
1916    if (isProtected()) {    if (isProtected()) {
1917          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1918    }    }
1919  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1920    profData->binary++;    MAKELAZYBINSELF(tmp,SUB)
1921  #endif    binaryOp(tmp,minus<double>());
   binaryOp(right,minus<double>());  
1922    return (*this);    return (*this);
1923  }  }
1924    
# Line 1661  Data::operator*=(const Data& right) Line 1928  Data::operator*=(const Data& right)
1928    if (isProtected()) {    if (isProtected()) {
1929          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1930    }    }
1931  #if defined DOPROF    MAKELAZYBINSELF(right,MUL)
   profData->binary++;  
 #endif  
1932    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1933    return (*this);    return (*this);
1934  }  }
1935    
1936  Data&  Data&
1937  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1938  {  {  
1939    if (isProtected()) {    if (isProtected()) {
1940          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1941    }    }
1942  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1943    profData->binary++;    MAKELAZYBINSELF(tmp,MUL)
1944  #endif    binaryOp(tmp,multiplies<double>());
   binaryOp(right,multiplies<double>());  
1945    return (*this);    return (*this);
1946  }  }
1947    
# Line 1687  Data::operator/=(const Data& right) Line 1951  Data::operator/=(const Data& right)
1951    if (isProtected()) {    if (isProtected()) {
1952          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1953    }    }
1954  #if defined DOPROF    MAKELAZYBINSELF(right,DIV)
   profData->binary++;  
 #endif  
1955    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1956    return (*this);    return (*this);
1957  }  }
# Line 1700  Data::operator/=(const boost::python::ob Line 1962  Data::operator/=(const boost::python::ob
1962    if (isProtected()) {    if (isProtected()) {
1963          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1964    }    }
1965  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1966    profData->binary++;    MAKELAZYBINSELF(tmp,DIV)
1967  #endif    binaryOp(tmp,divides<double>());
   binaryOp(right,divides<double>());  
1968    return (*this);    return (*this);
1969  }  }
1970    
1971  Data  Data
1972  Data::rpowO(const boost::python::object& left) const  Data::rpowO(const boost::python::object& left) const
1973  {  {
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1974    Data left_d(left,*this);    Data left_d(left,*this);
1975    return left_d.powD(*this);    return left_d.powD(*this);
1976  }  }
# Line 1723  Data::rpowO(const boost::python::object& Line 1978  Data::rpowO(const boost::python::object&
1978  Data  Data
1979  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1980  {  {
1981  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1982    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1983  }  }
1984    
1985  Data  Data
1986  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1987  {  {
1988  #if defined DOPROF    MAKELAZYBIN(right,POW)
1989    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;  
1990  }  }
1991    
   
1992  //  //
1993  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1994  Data  Data
1995  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1996  {  {
1997    Data result;    MAKELAZYBIN2(left,right,ADD)
1998    //    return C_TensorBinaryOperation(left, right, plus<double>());
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1999  }  }
2000    
2001  //  //
# Line 1763  escript::operator+(const Data& left, con Line 2003  escript::operator+(const Data& left, con
2003  Data  Data
2004  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
2005  {  {
2006    Data result;    MAKELAZYBIN2(left,right,SUB)
2007    //    return C_TensorBinaryOperation(left, right, minus<double>());
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
2008  }  }
2009    
2010  //  //
# Line 1776  escript::operator-(const Data& left, con Line 2012  escript::operator-(const Data& left, con
2012  Data  Data
2013  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
2014  {  {
2015    Data result;    MAKELAZYBIN2(left,right,MUL)
2016    //    return C_TensorBinaryOperation(left, right, multiplies<double>());
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
2017  }  }
2018    
2019  //  //
# Line 1789  escript::operator*(const Data& left, con Line 2021  escript::operator*(const Data& left, con
2021  Data  Data
2022  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
2023  {  {
2024    Data result;    MAKELAZYBIN2(left,right,DIV)
2025    //    return C_TensorBinaryOperation(left, right, divides<double>());
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
2026  }  }
2027    
2028  //  //
# Line 1802  escript::operator/(const Data& left, con Line 2030  escript::operator/(const Data& left, con
2030  Data  Data
2031  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
2032  {  {
2033    //    Data tmp(right,left.getFunctionSpace(),false);
2034    // Convert to DataArray format if possible    MAKELAZYBIN2(left,tmp,ADD)
2035    DataArray temp(right);    return left+tmp;
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
2036  }  }
2037    
2038  //  //
# Line 1818  escript::operator+(const Data& left, con Line 2040  escript::operator+(const Data& left, con
2040  Data  Data
2041  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
2042  {  {
2043    //    Data tmp(right,left.getFunctionSpace(),false);
2044    // Convert to DataArray format if possible    MAKELAZYBIN2(left,tmp,SUB)
2045    DataArray temp(right);    return left-tmp;
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
2046  }  }
2047    
2048  //  //
# Line 1834  escript::operator-(const Data& left, con Line 2050  escript::operator-(const Data& left, con
2050  Data  Data
2051  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
2052  {  {
2053    //    Data tmp(right,left.getFunctionSpace(),false);
2054    // Convert to DataArray format if possible    MAKELAZYBIN2(left,tmp,MUL)
2055    DataArray temp(right);    return left*tmp;
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
2056  }  }
2057    
2058  //  //
# Line 1850  escript::operator*(const Data& left, con Line 2060  escript::operator*(const Data& left, con
2060  Data  Data
2061  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
2062  {  {
2063    //    Data tmp(right,left.getFunctionSpace(),false);
2064    // Convert to DataArray format if possible    MAKELAZYBIN2(left,tmp,DIV)
2065    DataArray temp(right);    return left/tmp;
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
2066  }  }
2067    
2068  //  //
# Line 1866  escript::operator/(const Data& left, con Line 2070  escript::operator/(const Data& left, con
2070  Data  Data
2071  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
2072  {  {
2073    //    Data tmp(left,right.getFunctionSpace(),false);
2074    // Construct the result using the given value and the other parameters    MAKELAZYBIN2(tmp,right,ADD)
2075    // from right    return tmp+right;
   Data result(left,right);  
   result+=right;  
   return result;  
2076  }  }
2077    
2078  //  //
# Line 1879  escript::operator+(const boost::python:: Line 2080  escript::operator+(const boost::python::
2080  Data  Data
2081  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
2082  {  {
2083    //    Data tmp(left,right.getFunctionSpace(),false);
2084    // Construct the result using the given value and the other parameters    MAKELAZYBIN2(tmp,right,SUB)
2085    // from right    return tmp-right;
   Data result(left,right);  
   result-=right;  
   return result;  
2086  }  }
2087    
2088  //  //
# Line 1892  escript::operator-(const boost::python:: Line 2090  escript::operator-(const boost::python::
2090  Data  Data
2091  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
2092  {  {
2093    //    Data tmp(left,right.getFunctionSpace(),false);
2094    // Construct the result using the given value and the other parameters    MAKELAZYBIN2(tmp,right,MUL)
2095    // from right    return tmp*right;
   Data result(left,right);  
   result*=right;  
   return result;  
2096  }  }
2097    
2098  //  //
# Line 1905  escript::operator*(const boost::python:: Line 2100  escript::operator*(const boost::python::
2100  Data  Data
2101  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
2102  {  {
2103    //    Data tmp(left,right.getFunctionSpace(),false);
2104    // Construct the result using the given value and the other parameters    MAKELAZYBIN2(tmp,right,DIV)
2105    // from right    return tmp/right;
   Data result(left,right);  
   result/=right;  
   return result;  
2106  }  }
2107    
 //  
 //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;  
 //}  
2108    
2109  /* TODO */  /* TODO */
2110  /* global reduction */  /* global reduction */
2111  Data  Data
2112  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
2113  {  {
   const DataArrayView& view=getPointDataView();  
2114    
2115    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2116    
2117    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
2118      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
2119    }    }
2120    
# Line 1977  Data::getItem(const boost::python::objec Line 2124  Data::getItem(const boost::python::objec
2124  /* TODO */  /* TODO */
2125  /* global reduction */  /* global reduction */
2126  Data  Data
2127  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
2128  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
2129    return Data(*this,region);    return Data(*this,region);
2130  }  }
2131    
# Line 1995  Data::setItemO(const boost::python::obje Line 2139  Data::setItemO(const boost::python::obje
2139    setItemD(key,tempData);    setItemD(key,tempData);
2140  }  }
2141    
 /* TODO */  
 /* global reduction */  
2142  void  void
2143  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
2144                 const Data& value)                 const Data& value)
2145  {  {
2146    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
2147    
2148    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
2149    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
2150      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
2151    }    }
2152    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 2014  Data::setItemD(const boost::python::obje Line 2156  Data::setItemD(const boost::python::obje
2156    }    }
2157  }  }
2158    
 /* TODO */  
 /* global reduction */  
2159  void  void
2160  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
2161                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
2162  {  {
2163    if (isProtected()) {    if (isProtected()) {
2164          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2165    }    }
2166  #if defined DOPROF    FORCERESOLVE;
2167    profData->slicing++;  /*  if (isLazy())
2168  #endif    {
2169        throw DataException("Error - setSlice not permitted on lazy data.");
2170      }*/
2171    Data tempValue(value);    Data tempValue(value);
2172    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
2173    typeMatchRight(tempValue);    typeMatchRight(tempValue);
2174    m_data->setSlice(tempValue.m_data.get(),region);    getReady()->setSlice(tempValue.m_data.get(),region);
2175  }  }
2176    
2177  void  void
2178  Data::typeMatchLeft(Data& right) const  Data::typeMatchLeft(Data& right) const
2179  {  {
2180      if (right.isLazy() && !isLazy())
2181      {
2182        right.resolve();
2183      }
2184    if (isExpanded()){    if (isExpanded()){
2185      right.expand();      right.expand();
2186    } else if (isTagged()) {    } else if (isTagged()) {
# Line 2047  Data::typeMatchLeft(Data& right) const Line 2193  Data::typeMatchLeft(Data& right) const
2193  void  void
2194  Data::typeMatchRight(const Data& right)  Data::typeMatchRight(const Data& right)
2195  {  {
2196      if (isLazy() && !right.isLazy())
2197      {
2198        resolve();
2199      }
2200    if (isTagged()) {    if (isTagged()) {
2201      if (right.isExpanded()) {      if (right.isExpanded()) {
2202        expand();        expand();
# Line 2060  Data::typeMatchRight(const Data& right) Line 2210  Data::typeMatchRight(const Data& right)
2210    }    }
2211  }  }
2212    
2213  /* TODO */  void
2214  /* global reduction */  Data::setTaggedValueByName(std::string name,
2215                               const boost::python::object& value)
2216    {
2217         if (getFunctionSpace().getDomain()->isValidTagName(name)) {
2218        FORCERESOLVE;
2219            int tagKey=getFunctionSpace().getDomain()->getTag(name);
2220            setTaggedValue(tagKey,value);
2221         }
2222    }
2223  void  void
2224  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
2225                       const boost::python::object& value)                       const boost::python::object& value)
# Line 2071  Data::setTaggedValue(int tagKey, Line 2229  Data::setTaggedValue(int tagKey,
2229    }    }
2230    //    //
2231    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2232    tag();    FORCERESOLVE;
2233      if (isConstant()) tag();
2234      numeric::array asNumArray(value);
2235    
2236    if (!isTagged()) {    // extract the shape of the numarray
2237      throw DataException("Error - DataTagged conversion failed!!");    DataTypes::ShapeType tempShape;
2238      for (int i=0; i < asNumArray.getrank(); i++) {
2239        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2240    }    }
2241    
2242    //    DataVector temp_data2;
2243    // Construct DataArray from boost::python::object input value    temp_data2.copyFromNumArray(asNumArray,1);
   DataArray valueDataArray(value);  
2244    
2245    //    m_data->setTaggedValue(tagKey,tempShape, temp_data2);
   // Call DataAbstract::setTaggedValue  
   m_data->setTaggedValue(tagKey,valueDataArray.getView());  
2246  }  }
2247    
2248  /* TODO */  
 /* global reduction */  
2249  void  void
2250  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
2251                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
2252                                const DataTypes::ValueType& value,
2253                    int dataOffset)
2254  {  {
2255    if (isProtected()) {    if (isProtected()) {
2256          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2257    }    }
2258    //    //
2259    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2260    tag();    FORCERESOLVE;
2261      if (isConstant()) tag();
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
2262    //    //
2263    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2264    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
2265  }  }
2266    
 /* TODO */  
 /* global reduction */  
2267  int  int
2268  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
2269  {  {
2270    return m_data->getTagNumber(dpno);    if (isEmpty())
2271  }    {
2272        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.");  
2273    }    }
2274    //    return getFunctionSpace().getTagFromDataPointNo(dpno);
   // Construct DataArray from boost::python::object input value  
   DataArray valueDataArray(value);  
   
   //  
   // Call DataAbstract::setRefValue  
   m_data->setRefValue(ref,valueDataArray);  
2275  }  }
2276    
 /* TODO */  
 /* global reduction */  
 void  
 Data::getRefValue(int ref,  
                   boost::python::numeric::array& value)  
 {  
   //  
   // Construct DataArray for boost::python::object return value  
   DataArray valueDataArray(value);  
2277    
2278    //  ostream& escript::operator<<(ostream& o, const Data& data)
2279    // Load DataArray with values from data-points specified by ref  {
2280    m_data->getRefValue(ref,valueDataArray);    o << data.toString();
2281      return o;
2282    //  }
   // Load values from valueDataArray into return numarray  
2283    
2284    // extract the shape of the numarray  Data
2285    int rank = value.getrank();  escript::C_GeneralTensorProduct(Data& arg_0,
2286    DataArrayView::ShapeType shape;                       Data& arg_1,
2287    for (int i=0; i < rank; i++) {                       int axis_offset,
2288      shape.push_back(extract<int>(value.getshape()[i]));                       int transpose)
2289    {
2290      // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2291      // SM is the product of the last axis_offset entries in arg_0.getShape().
2292    
2293      // deal with any lazy data
2294    //   if (arg_0.isLazy()) {arg_0.resolve();}
2295    //   if (arg_1.isLazy()) {arg_1.resolve();}
2296      if (arg_0.isLazy() || arg_1.isLazy())
2297      {
2298        DataLazy* c=new DataLazy(arg_0.borrowDataPtr(), arg_1.borrowDataPtr(), PROD, axis_offset,transpose);
2299        return Data(c);
2300    }    }
2301    
2302    // and load the numarray with the data from the DataArray    // Interpolate if necessary and find an appropriate function space
2303    DataArrayView valueView = valueDataArray.getView();    Data arg_0_Z, arg_1_Z;
2304      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2305    if (rank==0) {      if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2306        boost::python::numeric::array temp_numArray(valueView());        arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2307        value = temp_numArray;        arg_1_Z = Data(arg_1);
2308    }      }
2309    if (rank==1) {      else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2310      for (int i=0; i < shape[0]; i++) {        arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2311        value[i] = valueView(i);        arg_0_Z =Data(arg_0);
2312      }      }
2313    }      else {
2314    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);  
       }  
2315      }      }
2316      } else {
2317          arg_0_Z = Data(arg_0);
2318          arg_1_Z = Data(arg_1);
2319    }    }
2320    if (rank==3) {    // Get rank and shape of inputs
2321      for (int i=0; i < shape[0]; i++) {    int rank0 = arg_0_Z.getDataPointRank();
2322        for (int j=0; j < shape[1]; j++) {    int rank1 = arg_1_Z.getDataPointRank();
2323          for (int k=0; k < shape[2]; k++) {    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2324            value[i][j][k] = valueView(i,j,k);    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2325          }  
2326        }    // Prepare for the loops of the product and verify compatibility of shapes
2327      int start0=0, start1=0;
2328      if (transpose == 0)       {}
2329      else if (transpose == 1)  { start0 = axis_offset; }
2330      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2331      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2332    
2333    
2334      // Adjust the shapes for transpose
2335      DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2336      DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2337      for (int i=0; i<rank0; i++)   { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2338      for (int i=0; i<rank1; i++)   { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2339    
2340    #if 0
2341      // For debugging: show shape after transpose
2342      char tmp[100];
2343      std::string shapeStr;
2344      shapeStr = "(";
2345      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2346      shapeStr += ")";
2347      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2348      shapeStr = "(";
2349      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2350      shapeStr += ")";
2351      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2352    #endif
2353    
2354      // Prepare for the loops of the product
2355      int SL=1, SM=1, SR=1;
2356      for (int i=0; i<rank0-axis_offset; i++)   {
2357        SL *= tmpShape0[i];
2358      }
2359      for (int i=rank0-axis_offset; i<rank0; i++)   {
2360        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2361          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2362        }
2363        SM *= tmpShape0[i];
2364      }
2365      for (int i=axis_offset; i<rank1; i++)     {
2366        SR *= tmpShape1[i];
2367      }
2368    
2369      // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2370      DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2371      {         // block to limit the scope of out_index
2372         int out_index=0;
2373         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2374         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2375      }
2376    
2377      if (shape2.size()>ESCRIPT_MAX_DATA_RANK)
2378      {
2379         ostringstream os;
2380         os << "C_GeneralTensorProduct: Error - Attempt to create a rank " << shape2.size() << " object. The maximum rank is " << ESCRIPT_MAX_DATA_RANK << ".";
2381         throw DataException(os.str());
2382      }
2383    
2384      // Declare output Data object
2385      Data res;
2386    
2387      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2388        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2389        double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2390        double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2391        double *ptr_2 = &(res.getDataAtOffset(0));
2392        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2393      }
2394      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2395    
2396        // Prepare the DataConstant input
2397        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2398        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2399    
2400        // Borrow DataTagged input from Data object
2401        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2402        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2403    
2404        // Prepare a DataTagged output 2
2405        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2406        res.tag();
2407        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2408        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2409    
2410        // Prepare offset into DataConstant
2411        int offset_0 = tmp_0->getPointOffset(0,0);
2412        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2413        // Get the views
2414    //     DataArrayView view_1 = tmp_1->getDefaultValue();
2415    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2416    //     // Get the pointers to the actual data
2417    //     double *ptr_1 = &((view_1.getData())[0]);
2418    //     double *ptr_2 = &((view_2.getData())[0]);
2419    
2420        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2421        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2422    
2423    
2424        // Compute an MVP for the default
2425        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2426        // Compute an MVP for each tag
2427        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2428        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2429        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2430          tmp_2->addTag(i->first);
2431    //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2432    //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2433    //       double *ptr_1 = &view_1.getData(0);
2434    //       double *ptr_2 = &view_2.getData(0);
2435    
2436          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2437          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2438        
2439          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2440      }      }
2441    
2442    }    }
2443    if (rank==4) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2444      for (int i=0; i < shape[0]; i++) {  
2445        for (int j=0; j < shape[1]; j++) {      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2446          for (int k=0; k < shape[2]; k++) {      DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2447            for (int l=0; l < shape[3]; l++) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2448              value[i][j][k][l] = valueView(i,j,k,l);      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2449            }      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2450          }      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2451        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2452        int sampleNo_1,dataPointNo_1;
2453        int numSamples_1 = arg_1_Z.getNumSamples();
2454        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2455        int offset_0 = tmp_0->getPointOffset(0,0);
2456        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2457        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2458          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2459            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2460            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2461            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2462            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2463            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2464            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2465        }        }
2466      }      }
2467    
2468    }    }
2469      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2470    
2471  }      // Borrow DataTagged input from Data object
2472        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2473        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2474    
2475  void      // Prepare the DataConstant input
2476  Data::archiveData(const std::string fileName)      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2477  {      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
   cout << "Archiving Data object to: " << fileName << endl;  
2478    
2479    //      // Prepare a DataTagged output 2
2480    // Determine type of this Data object      res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2481    int dataType = -1;      res.tag();
2482        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2483        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2484    
2485    if (isEmpty()) {      // Prepare offset into DataConstant
2486      dataType = 0;      int offset_1 = tmp_1->getPointOffset(0,0);
2487      cout << "\tdataType: DataEmpty" << endl;      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2488    }      // Get the views
2489    if (isConstant()) {  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2490      dataType = 1;  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2491      cout << "\tdataType: DataConstant" << endl;  //     // Get the pointers to the actual data
2492    }  //     double *ptr_0 = &((view_0.getData())[0]);
2493    if (isTagged()) {  //     double *ptr_2 = &((view_2.getData())[0]);
     dataType = 2;  
     cout << "\tdataType: DataTagged" << endl;  
   }  
   if (isExpanded()) {  
     dataType = 3;  
     cout << "\tdataType: DataExpanded" << endl;  
   }  
2494    
2495    if (dataType == -1) {      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2496      throw DataException("archiveData Error: undefined dataType");      double *ptr_2 = &(tmp_2->getDefaultValue(0));
   }  
2497    
2498    //      // Compute an MVP for the default
2499    // Collect data items common to all Data types      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2500    int noSamples = getNumSamples();      // Compute an MVP for each tag
2501    int noDPPSample = getNumDataPointsPerSample();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2502    int functionSpaceType = getFunctionSpace().getTypeCode();      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2503    int dataPointRank = getDataPointRank();      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2504    int dataPointSize = getDataPointSize();  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2505    int dataLength = getLength();  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2506    DataArrayView::ShapeType dataPointShape = getDataPointShape();  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2507    vector<int> referenceNumbers(noSamples);  //       double *ptr_0 = &view_0.getData(0);
2508    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  //       double *ptr_2 = &view_2.getData(0);
2509      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);  
2510    }        tmp_2->addTag(i->first);
2511    vector<int> tagNumbers(noSamples);        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2512    if (isTagged()) {        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2513      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);  
2514      }      }
2515    
2516    }    }
2517      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2518    
2519    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;      // Borrow DataTagged input from Data object
2520    cout << "\tfunctionSpaceType: " << functionSpaceType << endl;      DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2521    cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2522    
2523    //      // Borrow DataTagged input from Data object
2524    // Flatten Shape to an array of integers suitable for writing to file      DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2525    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;  
2526    
2527    //      // Prepare a DataTagged output 2
2528    // Open archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2529    ofstream archiveFile;      res.tag();  // DataTagged output
2530    archiveFile.open(fileName.data(), ios::out);      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2531        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2532    
2533    if (!archiveFile.good()) {  //     // Get the views
2534      throw DataException("archiveData Error: problem opening archive file");  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2535    }  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2536    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2537    //     // Get the pointers to the actual data
2538    //     double *ptr_0 = &((view_0.getData())[0]);
2539    //     double *ptr_1 = &((view_1.getData())[0]);
2540    //     double *ptr_2 = &((view_2.getData())[0]);
2541    
2542    //      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2543    // Write common data items to archive file      double *ptr_1 = &(tmp_1->getDefaultValue(0));
2544    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));  
     }  
   }  
2545    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing to archive file");  
   }  
2546    
2547    //      // Compute an MVP for the default
2548    // Archive underlying data values for each Data type      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2549    int noValues;      // Merge the tags
2550    switch (dataType) {      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2551      case 0:      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2552        // DataEmpty      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2553        noValues = 0;      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2554        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));        tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2555        cout << "\tnoValues: " << noValues << endl;      }
2556        break;      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2557      case 1:        tmp_2->addTag(i->first);
2558        // DataConstant      }
2559        noValues = m_data->getLength();      // Compute an MVP for each tag
2560        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2561        cout << "\tnoValues: " << noValues << endl;      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2562        if (m_data->archiveData(archiveFile,noValues)) {  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2563          throw DataException("archiveData Error: problem writing data to archive file");  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2564        }  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2565        break;  //       double *ptr_0 = &view_0.getData(0);
2566      case 2:  //       double *ptr_1 = &view_1.getData(0);
2567        // 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;  
   }  
2568    
2569    if (!archiveFile.good()) {        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2570      throw DataException("archiveData Error: problem writing data to archive file");        double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2571    }        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2572    
2573    //        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2574    // Close archive file      }
   archiveFile.close();  
2575    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem closing archive file");  
2576    }    }
2577      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2578    
2579  }      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2580        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2581        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2582        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2583        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2584        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2585        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2586        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2587        int sampleNo_0,dataPointNo_0;
2588        int numSamples_0 = arg_0_Z.getNumSamples();
2589        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2590        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2591        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2592          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2593          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2594          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2595            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2596            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2597            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2598            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2599            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2600          }
2601        }
2602    
 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");  
2603    }    }
2604      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2605    
2606    cout << "Extracting Data object from: " << fileName << endl;      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2607        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2608    int dataType;      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2609    int noSamples;      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2610    int noDPPSample;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2611    int functionSpaceType;      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2612    int dataPointRank;      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2613    int dataPointSize;      int sampleNo_0,dataPointNo_0;
2614    int dataLength;      int numSamples_0 = arg_0_Z.getNumSamples();
2615    DataArrayView::ShapeType dataPointShape;      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2616    int flatShape[4];      int offset_1 = tmp_1->getPointOffset(0,0);
2617        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2618        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2619          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2620            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2621            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2622            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2623            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2624            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2625            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2626          }
2627        }
2628    
   //  
   // Open the archive file  
   ifstream archiveFile;  
   archiveFile.open(fileName.data(), ios::in);  
2629    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem opening archive file");  
2630    }    }
2631      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2632    
2633    //      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2634    // Read common data items from archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2635    archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2636    archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));      DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2637    archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2638    archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2639    archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2640    archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2641    archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));      int sampleNo_0,dataPointNo_0;
2642    for (int dim = 0; dim < 4; dim++) {      int numSamples_0 = arg_0_Z.getNumSamples();
2643      archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2644      if (flatShape[dim]>0) {      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2645        dataPointShape.push_back(flatShape[dim]);      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2646          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2647          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2648          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2649            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2650            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2651            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2652            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2653            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2654          }
2655      }      }
2656    
2657    }    }
2658    vector<int> referenceNumbers(noSamples);    else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2659    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
2660      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
2661    }      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2662    vector<int> tagNumbers(noSamples);      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2663    if (dataType==2) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2664      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2665        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2666        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2667        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2668        int sampleNo_0,dataPointNo_0;
2669        int numSamples_0 = arg_0_Z.getNumSamples();
2670        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2671        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2672        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2673          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2674            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2675            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2676            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2677            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2678            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2679            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2680            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2681          }
2682      }      }
   }  
2683    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
2684    }    }
2685      else {
2686    //      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;  
2687    }    }
2688    
2689    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;    return res;
2690    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;  
2691    
2692    //  DataAbstract*
2693    // Verify that supplied FunctionSpace object is compatible with this Data object.  Data::borrowData() const
2694    if ( (fspace.getTypeCode()!=functionSpaceType) ||  {
2695         (fspace.getNumSamples()!=noSamples) ||    return m_data.get();
2696         (fspace.getNumDPPSample()!=noDPPSample)  }
2697       ) {  
2698      throw DataException("extractData Error: incompatible FunctionSpace");  // Not all that happy about returning a non-const from a const
2699    }  DataAbstract_ptr
2700    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  Data::borrowDataPtr() const
2701      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {  {
2702        throw DataException("extractData Error: incompatible FunctionSpace");    return m_data;
2703      }  }
2704    }  
2705    if (dataType==2) {  // Not all that happy about returning a non-const from a const
2706      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  DataReady_ptr
2707        if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {  Data::borrowReadyPtr() const
2708          throw DataException("extractData Error: incompatible FunctionSpace");  {
2709        }     DataReady_ptr dr=dynamic_pointer_cast<DataReady>(m_data);
2710       EsysAssert((dr!=0), "Error - casting to DataReady.");
2711       return dr;
2712    }
2713    
2714    std::string
2715    Data::toString() const
2716    {
2717        if (!m_data->isEmpty() &&
2718        !m_data->isLazy() &&
2719        getLength()>escriptParams.getInt("TOO_MANY_LINES"))
2720        {
2721        stringstream temp;
2722        temp << "Summary: inf="<< inf_const() << " sup=" << sup_const() << " data points=" << getNumDataPoints();
2723        return  temp.str();
2724      }      }
2725    }      return m_data->toString();
2726    }
2727    
   //  
   // Construct a DataVector to hold underlying data values  
   DataVector dataVec(dataLength);  
2728    
   //  
   // 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;  
   }  
2729    
2730    if (!archiveFile.good()) {  DataTypes::ValueType::const_reference
2731      throw DataException("extractData Error: problem reading from archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2732    }  {
2733        if (isLazy())
2734        {
2735        throw DataException("Programmer error - getDataAtOffset not permitted on lazy data (object is const which prevents resolving).");
2736        }
2737        return getReady()->getDataAtOffset(i);
2738    }
2739    
   //  
   // Close archive file  
   archiveFile.close();  
2740    
2741    if (!archiveFile.good()) {  DataTypes::ValueType::reference
2742      throw DataException("extractData Error: problem closing archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2743    }  {
2744    //     if (isLazy())
2745    //     {
2746    //  throw DataException("getDataAtOffset not permitted on lazy data.");
2747    //     }
2748        FORCERESOLVE;
2749        return getReady()->getDataAtOffset(i);
2750    }
2751    
2752    //  DataTypes::ValueType::const_reference
2753    // Construct an appropriate Data object  Data::getDataPoint(int sampleNo, int dataPointNo) const
2754    DataAbstract* tempData;  {
2755    switch (dataType) {    if (!isReady())
2756      case 0:    {
2757        // DataEmpty      throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data (object is const which prevents resolving).");
2758        tempData=new DataEmpty();    }
2759        break;    else
2760      case 1:    {
2761        // DataConstant      const DataReady* dr=getReady();
2762        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;  
2763    }    }
   shared_ptr<DataAbstract> temp_data(tempData);  
   m_data=temp_data;  
2764  }  }
2765    
2766  ostream& escript::operator<<(ostream& o, const Data& data)  
2767    DataTypes::ValueType::reference
2768    Data::getDataPoint(int sampleNo, int dataPointNo)
2769  {  {
2770    o << data.toString();    FORCERESOLVE;
2771    return o;    if (!isReady())
2772      {
2773        throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2774      }
2775      else
2776      {
2777        DataReady* dr=getReady();
2778        return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2779      }
2780  }  }
2781    
2782    
2783  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2784    
2785  void  void
2786  Data::print()  Data::print()
2787  {  {
2788    int i,j;    int i,j;
2789      
2790    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2791    for( i=0; i<getNumSamples(); i++ )    for( i=0; i<getNumSamples(); i++ )
2792    {    {
# Line 2557  Data::print() Line 2796  Data::print()
2796      printf( "\n" );      printf( "\n" );
2797    }    }
2798  }  }
2799    void
2800    Data::dump(const std::string fileName) const
2801    {
2802      try
2803      {
2804        if (isLazy())
2805        {
2806          Data temp(*this); // this is to get a non-const object which we can resolve
2807          temp.resolve();
2808          temp.dump(fileName);
2809        }
2810        else
2811        {
2812              return m_data->dump(fileName);
2813        }
2814      }
2815      catch (std::exception& e)
2816      {
2817            cout << e.what() << endl;
2818      }
2819    }
2820    
2821  int  int
2822  Data::get_MPISize() const  Data::get_MPISize() const
2823  {  {
2824      int error, size;      int size;
2825  #ifdef PASO_MPI  #ifdef PASO_MPI
2826        int error;
2827      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
2828  #else  #else
2829      size = 1;      size = 1;
# Line 2573  Data::get_MPISize() const Line 2834  Data::get_MPISize() const
2834  int  int
2835  Data::get_MPIRank() const  Data::get_MPIRank() const
2836  {  {
2837      int error, rank;      int rank;
2838  #ifdef PASO_MPI  #ifdef PASO_MPI
2839        int error;
2840      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
2841  #else  #else
2842      rank = 0;      rank = 0;
# Line 2584  Data::get_MPIRank() const Line 2846  Data::get_MPIRank() const
2846    
2847  MPI_Comm  MPI_Comm
2848  Data::get_MPIComm() const  Data::get_MPIComm() const
2849  {  {
2850  #ifdef PASO_MPI  #ifdef PASO_MPI
2851      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
2852  #else  #else
# Line 2592  Data::get_MPIComm() const Line 2854  Data::get_MPIComm() const
2854  #endif  #endif
2855  }  }
2856    
2857    

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

  ViewVC Help
Powered by ViewVC 1.1.26