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

Legend:
Removed from v.854  
changed lines
  Added in v.1897

  ViewVC Help
Powered by ViewVC 1.1.26