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

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

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

revision 790 by bcumming, Wed Jul 26 23:12:34 2006 UTC revision 1946 by jfenwick, Wed Oct 29 05:48:53 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      if (other.isEmpty() || mask.isEmpty())
485      {
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    
545    mask1 = mask.wherePositive();    // OPENMP 3.0 allows unsigned loop vars.
546    mask2.copy(mask1);  #if defined(_OPENMP) && (_OPENMP < 200805)
547      long i;
548    #else
549      size_t i;
550    #endif
551      #pragma omp parallel for private(i) schedule(static)
552      for (i=0;i<num_points;++i)
553      {
554        if (mvec[i]>0)
555        {
556           self[i]=ovec[i];
557        }
558      }
559    }
560    
   mask1 *= other;  
   mask2 *= *this;  
   mask2 = *this - mask2;  
561    
   *this = mask1 + mask2;  
 }  
562    
563  bool  bool
564  Data::isExpanded() const  Data::isExpanded() const
# Line 350  Data::isTagged() const Line 574  Data::isTagged() const
574    return (temp!=0);    return (temp!=0);
575  }  }
576    
 /* TODO */  
 /* global reduction -- the local data being empty does not imply that it is empty on other processers*/  
577  bool  bool
578  Data::isEmpty() const  Data::isEmpty() const
579  {  {
# Line 367  Data::isConstant() const Line 589  Data::isConstant() const
589  }  }
590    
591  void  void
592  Data::setProtection()  Data::setProtection()
593  {  {
594     m_protected=true;     m_protected=true;
595  }  }
596    
597  bool  bool
598  Data::isProtected() const  Data::isProtected() const
599  {  {
600     return m_protected;     return m_protected;
601  }  }
602    
# Line 386  Data::expand() Line 608  Data::expand()
608    if (isConstant()) {    if (isConstant()) {
609      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
610      DataAbstract* temp=new DataExpanded(*tempDataConst);      DataAbstract* temp=new DataExpanded(*tempDataConst);
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 (isTagged()) {    } else if (isTagged()) {
615      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());      DataTagged* tempDataTag=dynamic_cast<DataTagged*>(m_data.get());
616      DataAbstract* temp=new DataExpanded(*tempDataTag);      DataAbstract* temp=new DataExpanded(*tempDataTag);
617      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
618      m_data=temp_data;  //     m_data=temp_data;
619        m_data=temp->getPtr();
620    } else if (isExpanded()) {    } else if (isExpanded()) {
621      //      //
622      // do nothing      // do nothing
# Line 409  Data::tag() Line 633  Data::tag()
633    if (isConstant()) {    if (isConstant()) {
634      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());      DataConstant* tempDataConst=dynamic_cast<DataConstant*>(m_data.get());
635      DataAbstract* temp=new DataTagged(*tempDataConst);      DataAbstract* temp=new DataTagged(*tempDataConst);
636      shared_ptr<DataAbstract> temp_data(temp);  //     shared_ptr<DataAbstract> temp_data(temp);
637      m_data=temp_data;  //     m_data=temp_data;
638        m_data=temp->getPtr();
639    } else if (isTagged()) {    } else if (isTagged()) {
640      // do nothing      // do nothing
641    } else if (isExpanded()) {    } else if (isExpanded()) {
# Line 422  Data::tag() Line 647  Data::tag()
647    }    }
648  }  }
649    
650  void  Data
651  Data::reshapeDataPoint(const DataArrayView::ShapeType& shape)  Data::oneOver() const
652  {  {
653    m_data->reshapeDataPoint(shape);    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
654  }  }
655    
656  Data  Data
657  Data::wherePositive() const  Data::wherePositive() const
658  {  {
659  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));  
660  }  }
661    
662  Data  Data
663  Data::whereNegative() const  Data::whereNegative() const
664  {  {
665  #if defined DOPROF    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
   profData->where++;  
 #endif  
   return escript::unaryOp(*this,bind2nd(less<double>(),0.0));  
666  }  }
667    
668  Data  Data
669  Data::whereNonNegative() const  Data::whereNonNegative() const
670  {  {
671  #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));  
672  }  }
673    
674  Data  Data
675  Data::whereNonPositive() const  Data::whereNonPositive() const
676  {  {
677  #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));  
678  }  }
679    
680  Data  Data
681  Data::whereZero(double tol) const  Data::whereZero(double tol) const
682  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
683    Data dataAbs=abs();    Data dataAbs=abs();
684    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
685  }  }
686    
687  Data  Data
688  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
689  {  {
 #if defined DOPROF  
   profData->where++;  
 #endif  
690    Data dataAbs=abs();    Data dataAbs=abs();
691    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
692  }  }
693    
694  Data  Data
695  Data::interpolate(const FunctionSpace& functionspace) const  Data::interpolate(const FunctionSpace& functionspace) const
696  {  {
 #if defined DOPROF  
   profData->interpolate++;  
 #endif  
697    return Data(*this,functionspace);    return Data(*this,functionspace);
698  }  }
699    
# Line 499  Data::probeInterpolation(const FunctionS Line 703  Data::probeInterpolation(const FunctionS
703    if (getFunctionSpace()==functionspace) {    if (getFunctionSpace()==functionspace) {
704      return true;      return true;
705    } else {    } else {
706      const AbstractDomain& domain=getDomain();      const_Domain_ptr domain=getDomain();
707      if  (domain==functionspace.getDomain()) {      if  (*domain==*functionspace.getDomain()) {
708        return domain.probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());        return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
709      } else {      } else {
710        return domain.probeInterpolationACross(getFunctionSpace().getTypeCode(),functionspace.getDomain(),functionspace.getTypeCode());        return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());
711      }      }
712    }    }
713  }  }
# Line 511  Data::probeInterpolation(const FunctionS Line 715  Data::probeInterpolation(const FunctionS
715  Data  Data
716  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
717  {  {
718  #if defined DOPROF    if (isEmpty())
719    profData->grad++;    {
720  #endif      throw DataException("Error - operation not permitted on instances of DataEmpty.");
721      }
722      double blocktimer_start = blocktimer_time();
723    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
724      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
725    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
726    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
727    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
728    getDomain().setToGradient(out,*this);    getDomain()->setToGradient(out,*this);
729      blocktimer_increment("grad()", blocktimer_start);
730    return out;    return out;
731  }  }
732    
733  Data  Data
734  Data::grad() const  Data::grad() const
735  {  {
736    return gradOn(escript::function(getDomain()));    if (isEmpty())
737      {
738        throw DataException("Error - operation not permitted on instances of DataEmpty.");
739      }
740      return gradOn(escript::function(*getDomain()));
741  }  }
742    
743  int  int
744  Data::getDataPointSize() const  Data::getDataPointSize() const
745  {  {
746    return getPointDataView().noValues();    return m_data->getNoValues();
747  }  }
748    
749  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
750  Data::getLength() const  Data::getLength() const
751  {  {
752    return m_data->getLength();    return m_data->getLength();
753  }  }
754    
 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);  
 }  
   
755  const  const
756  boost::python::numeric::array  boost::python::numeric::array
757  Data::convertToNumArray()  Data:: getValueOfDataPoint(int dataPointNo)
758  {  {
759    //    int i, j, k, l;
   // determine the total number of data points  
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
   
760    //    //
761    // determine the rank and shape of each data point    // determine the rank and shape of each data point
762    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
763    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
764    
765    //    //
766    // create the numeric array to be returned    // create the numeric array to be returned
767    boost::python::numeric::array numArray(0.0);    boost::python::numeric::array numArray(0.0);
768    
769    //    //
770    // the rank of the returned numeric array will be the rank of    // the shape of the returned numeric array will be the same
771    // the data points, plus one. Where the rank of the array is n,    // as that of the data point
772    // the last n-1 dimensions will be equal to the shape of the    int arrayRank = dataPointRank;
773    // 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]);  
   }  
774    
775    //    //
776    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
777      if (arrayRank==0) {
778        numArray.resize(1);
779      }
780    if (arrayRank==1) {    if (arrayRank==1) {
781      numArray.resize(arrayShape[0]);      numArray.resize(arrayShape[0]);
782    }    }
# Line 623  Data::convertToNumArray() Line 789  Data::convertToNumArray()
789    if (arrayRank==4) {    if (arrayRank==4) {
790      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
791    }    }
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
792    
793    //    if (getNumDataPointsPerSample()>0) {
794    // loop through each data point in turn, loading the values for that data point         int sampleNo = dataPointNo/getNumDataPointsPerSample();
795    // into the numeric array.         int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
796    int dataPoint = 0;         //
797    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {         // Check a valid sample number has been supplied
798      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
799        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
800        if (dataPointRank==0) {         }
801          numArray[dataPoint]=dataPointView();  
802        }         //
803        if (dataPointRank==1) {         // Check a valid data point number has been supplied
804          for (int i=0; i<dataPointShape[0]; i++) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
805            numArray[dataPoint][i]=dataPointView(i);             throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
806          }         }
807        }         // TODO: global error handling
808        if (dataPointRank==2) {         // create a view of the data if it is stored locally
809          for (int i=0; i<dataPointShape[0]; i++) {  //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
810            for (int j=0; j<dataPointShape[1]; j++) {         DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
             numArray[dataPoint][i][j] = dataPointView(i,j);  
           }  
         }  
       }  
       if (dataPointRank==3) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               numArray[dataPoint][i][j][k]=dataPointView(i,j,k);  
             }  
           }  
         }  
       }  
       if (dataPointRank==4) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               for (int l=0; l<dataPointShape[3]; l++) {  
                 numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);  
               }  
             }  
           }  
         }  
       }  
       dataPoint++;  
     }  
   }  
811    
812    
813           switch( dataPointRank ){
814                case 0 :
815                    numArray[0] = getDataAtOffset(offset);
816                    break;
817                case 1 :
818                    for( i=0; i<dataPointShape[0]; i++ )
819                        numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
820                    break;
821                case 2 :
822                    for( i=0; i<dataPointShape[0]; i++ )
823                        for( j=0; j<dataPointShape[1]; j++)
824                            numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
825                    break;
826                case 3 :
827                    for( i=0; i<dataPointShape[0]; i++ )
828                        for( j=0; j<dataPointShape[1]; j++ )
829                            for( k=0; k<dataPointShape[2]; k++)
830                                numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
831                    break;
832                case 4 :
833                    for( i=0; i<dataPointShape[0]; i++ )
834                        for( j=0; j<dataPointShape[1]; j++ )
835                            for( k=0; k<dataPointShape[2]; k++ )
836                                for( l=0; l<dataPointShape[3]; l++)
837                                    numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
838                    break;
839        }
840      }
841    //    //
842    // return the loaded array    // return the array
843    return numArray;    return numArray;
844    
845  }  }
846    
847  const  void
848  boost::python::numeric::array  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
 Data::convertToNumArrayFromSampleNo(int sampleNo)  
849  {  {
850    //      // this will throw if the value cannot be represented
851    // Check a valid sample number has been supplied      boost::python::numeric::array num_array(py_object);
852    if (sampleNo >= getNumSamples()) {      setValueOfDataPointToArray(dataPointNo,num_array);
853      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();  
854    
855    void
856    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
857    {
858      if (isProtected()) {
859            throw DataException("Error - attempt to update protected Data object.");
860      }
861    //    //
862    // create the numeric array to be returned    // check rank
863    boost::python::numeric::array numArray(0.0);    if (num_array.getrank()<getDataPointRank())
864          throw DataException("Rank of numarray does not match Data object rank");
865    
866    //    //
867    // the rank of the returned numeric array will be the rank of    // check shape of num_array
868    // the data points, plus one. Where the rank of the array is n,    for (int i=0; i<getDataPointRank(); i++) {
869    // the last n-1 dimensions will be equal to the shape of the      if (extract<int>(num_array.getshape()[i])!=getDataPointShape()[i])
870    // 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]);  
871    }    }
   
872    //    //
873    // resize the numeric array to the shape just calculated    // make sure data is expanded:
874    if (arrayRank==1) {    //
875      numArray.resize(arrayShape[0]);    if (!isExpanded()) {
876    }      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]);  
877    }    }
878    if (arrayRank==5) {    if (getNumDataPointsPerSample()>0) {
879      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);         int sampleNo = dataPointNo/getNumDataPointsPerSample();
880           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
881           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,num_array);
882      } else {
883           m_data->copyToDataPoint(-1, 0,num_array);
884    }    }
885    }
886    
887    //  void
888    // loop through each data point in turn, loading the values for that data point  Data::setValueOfDataPoint(int dataPointNo, const double value)
889    // into the numeric array.  {
890    for (int dataPoint = 0; dataPoint < numDataPointsPerSample; dataPoint++) {    if (isProtected()) {
891      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);  
             }  
           }  
         }  
       }  
     }  
892    }    }
   
893    //    //
894    // return the loaded array    // make sure data is expanded:
895    return numArray;    if (!isExpanded()) {
896        expand();
897      }
898      if (getNumDataPointsPerSample()>0) {
899           int sampleNo = dataPointNo/getNumDataPointsPerSample();
900           int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
901           m_data->copyToDataPoint(sampleNo, dataPointNoInSample,value);
902      } else {
903           m_data->copyToDataPoint(-1, 0,value);
904      }
905  }  }
906    
907  const  const
908  boost::python::numeric::array  boost::python::numeric::array
909  Data::convertToNumArrayFromDPNo(int procNo,  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
                                 int sampleNo,  
                                                                 int dataPointNo)  
   
910  {  {
911      size_t length=0;    size_t length=0;
912      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.");  
   }  
   
913    //    //
914    // determine the rank and shape of each data point    // determine the rank and shape of each data point
915    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
916    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
917    
918    //    //
919    // create the numeric array to be returned    // create the numeric array to be returned
# Line 815  Data::convertToNumArrayFromDPNo(int proc Line 923  Data::convertToNumArrayFromDPNo(int proc
923    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
924    // as that of the data point    // as that of the data point
925    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
926    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
927    
928    //    //
929    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 835  Data::convertToNumArrayFromDPNo(int proc Line 943  Data::convertToNumArrayFromDPNo(int proc
943      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);      numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
944    }    }
945    
946      // added for the MPI communication    // added for the MPI communication
947      length=1;    length=1;
948      for( i=0; i<arrayRank; i++ )    for( i=0; i<arrayRank; i++ ) length *= arrayShape[i];
949          length *= arrayShape[i];    double *tmpData = new double[length];
     double *tmpData = new double[length];  
950    
951    //    //
952    // load the values for the data point into the numeric array.    // load the values for the data point into the numeric array.
953    
954      // updated for the MPI case      // updated for the MPI case
955      if( get_MPIRank()==procNo ){      if( get_MPIRank()==procNo ){
956                 if (getNumDataPointsPerSample()>0) {
957                    int sampleNo = dataPointNo/getNumDataPointsPerSample();
958                    int dataPointNoInSample = dataPointNo - sampleNo * getNumDataPointsPerSample();
959                    //
960                    // Check a valid sample number has been supplied
961                    if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
962                      throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
963                    }
964    
965                    //
966                    // Check a valid data point number has been supplied
967                    if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
968                      throw DataException("Error - Data::convertToNumArray: invalid dataPointNoInSample.");
969                    }
970                    // TODO: global error handling
971          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
972          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
973                    DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
974    
975          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
976          pos=0;          pos=0;
977          switch( dataPointRank ){          switch( dataPointRank ){
978              case 0 :              case 0 :
979                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
980                  break;                  break;
981              case 1 :                      case 1 :
982                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
983                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
984                  break;                  break;
985              case 2 :                      case 2 :
986                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
987                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
988                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
989                  break;                  break;
990              case 3 :                      case 3 :
991                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
992                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
993                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
994                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
995                  break;                  break;
996              case 4 :              case 4 :
997                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
998                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
999                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
1000                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
1001                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
1002                  break;                  break;
1003          }          }
1004                }
1005      }      }
1006  #ifdef PASO_MPI          #ifdef PASO_MPI
1007          // broadcast the data to all other processes          // broadcast the data to all other processes
1008          MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
1009  #endif          #endif
1010    
1011      // unpack the data      // unpack the data
1012      switch( dataPointRank ){      switch( dataPointRank ){
1013          case 0 :          case 0 :
1014              numArray[i]=tmpData[0];              numArray[0]=tmpData[0];
1015              break;              break;
1016          case 1 :                  case 1 :
1017              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1018                  numArray[i]=tmpData[i];                  numArray[i]=tmpData[i];
1019              break;              break;
1020          case 2 :                  case 2 :
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                      tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
1024              break;              break;
1025          case 3 :                  case 3 :
1026              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1027                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1028                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
1029                          tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];                          numArray[make_tuple(i,j,k)]=tmpData[i+dataPointShape[0]*(j*+k*dataPointShape[1])];
1030              break;              break;
1031          case 4 :          case 4 :
1032              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
1033                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
1034                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
1035                          for( l=0; l<dataPointShape[3]; l++ )                          for( l=0; l<dataPointShape[3]; l++ )
1036                              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]))];
1037              break;              break;
1038      }      }
1039    
1040      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);  
           }  
         }  
       }  
     }  
   }  
 */  
   
1041    //    //
1042    // return the loaded array    // return the loaded array
1043    return numArray;    return numArray;
1044  }  }
1045    
1046    
1047    
1048  boost::python::numeric::array  boost::python::numeric::array
1049  Data::integrate() const  Data::integrate() const
1050  {  {
1051    int index;    int index;
1052    int rank = getDataPointRank();    int rank = getDataPointRank();
1053    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
1054      int dataPointSize = getDataPointSize();
 #if defined DOPROF  
   profData->integrate++;  
 #endif  
1055    
1056    //    //
1057    // calculate the integral values    // calculate the integral values
1058    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
1059    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    vector<double> integrals_local(dataPointSize);
1060    #ifdef PASO_MPI
1061      AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals_local,*this);
1062      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
1063      double *tmp = new double[dataPointSize];
1064      double *tmp_local = new double[dataPointSize];
1065      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
1066      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
1067      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
1068      delete[] tmp;
1069      delete[] tmp_local;
1070    #else
1071      AbstractContinuousDomain::asAbstractContinuousDomain(*getDomain()).setToIntegrals(integrals,*this);
1072    #endif
1073    
1074    //    //
1075    // create the numeric array to be returned    // create the numeric array to be returned
# Line 1031  Data::integrate() const Line 1129  Data::integrate() const
1129  Data  Data
1130  Data::sin() const  Data::sin() const
1131  {  {
1132  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);  
1133  }  }
1134    
1135  Data  Data
1136  Data::cos() const  Data::cos() const
1137  {  {
1138  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);  
1139  }  }
1140    
1141  Data  Data
1142  Data::tan() const  Data::tan() const
1143  {  {
1144  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);  
1145  }  }
1146    
1147  Data  Data
1148  Data::asin() const  Data::asin() const
1149  {  {
1150  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);  
1151  }  }
1152    
1153  Data  Data
1154  Data::acos() const  Data::acos() const
1155  {  {
1156  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);  
1157  }  }
1158    
1159    
1160  Data  Data
1161  Data::atan() const  Data::atan() const
1162  {  {
1163  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);  
1164  }  }
1165    
1166  Data  Data
1167  Data::sinh() const  Data::sinh() const
1168  {  {
1169  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1170    profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);  
1171  }  }
1172    
1173  Data  Data
1174  Data::cosh() const  Data::cosh() const
1175  {  {
1176  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);  
1177  }  }
1178    
1179  Data  Data
1180  Data::tanh() const  Data::tanh() const
1181  {  {
1182  #if defined DOPROF      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1183    profData->unary++;  }
1184    
1185    
1186    Data
1187    Data::erf() const
1188    {
1189    #ifdef _WIN32
1190      throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1191    #else
1192      return C_TensorUnaryOperation(*this, ::erf);
1193  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);  
1194  }  }
1195    
1196  Data  Data
1197  Data::asinh() const  Data::asinh() const
1198  {  {
1199  #if defined DOPROF  #ifdef _WIN32
1200    profData->unary++;    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1201    #else
1202      return C_TensorUnaryOperation(*this, ::asinh);
1203  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);  
1204  }  }
1205    
1206  Data  Data
1207  Data::acosh() const  Data::acosh() const
1208  {  {
1209  #if defined DOPROF  #ifdef _WIN32
1210    profData->unary++;    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1211    #else
1212      return C_TensorUnaryOperation(*this, ::acosh);
1213  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);  
1214  }  }
1215    
1216  Data  Data
1217  Data::atanh() const  Data::atanh() const
1218  {  {
1219  #if defined DOPROF  #ifdef _WIN32
1220    profData->unary++;    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1221    #else
1222      return C_TensorUnaryOperation(*this, ::atanh);
1223  #endif  #endif
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);  
1224  }  }
1225    
1226  Data  Data
1227  Data::log10() const  Data::log10() const
1228  {  {
1229  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);  
1230  }  }
1231    
1232  Data  Data
1233  Data::log() const  Data::log() const
1234  {  {
1235  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);  
1236  }  }
1237    
1238  Data  Data
1239  Data::sign() const  Data::sign() const
1240  {  {
1241  #if defined DOPROF    return C_TensorUnaryOperation(*this, escript::fsign);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,escript::fsign);  
1242  }  }
1243    
1244  Data  Data
1245  Data::abs() const  Data::abs() const
1246  {  {
1247  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);  
1248  }  }
1249    
1250  Data  Data
1251  Data::neg() const  Data::neg() const
1252  {  {
1253  #if defined DOPROF    return C_TensorUnaryOperation(*this, negate<double>());
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,negate<double>());  
1254  }  }
1255    
1256  Data  Data
1257  Data::pos() const  Data::pos() const
1258  {  {
 #if defined DOPROF  
   profData->unary++;  
 #endif  
1259    Data result;    Data result;
1260    // perform a deep copy    // perform a deep copy
1261    result.copy(*this);    result.copy(*this);
# Line 1196  Data::pos() const Line 1265  Data::pos() const
1265  Data  Data
1266  Data::exp() const  Data::exp() const
1267  {  {
1268  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);  
1269  }  }
1270    
1271  Data  Data
1272  Data::sqrt() const  Data::sqrt() const
1273  {  {
1274  #if defined DOPROF    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
   profData->unary++;  
 #endif  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);  
1275  }  }
1276    
1277  double  double
1278  Data::Lsup() const  Data::Lsup() const
1279  {  {
1280    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1281    //    //
1282    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1283    
1284    AbsMax abs_max_func;    AbsMax abs_max_func;
1285    localValue = algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1286  #ifdef PASO_MPI  #ifdef PASO_MPI
1287      double globalValue;
1288    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1289    return globalValue;    return globalValue;
1290  #else  #else
# Line 1232  Data::Lsup() const Line 1293  Data::Lsup() const
1293  }  }
1294    
1295  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  
1296  Data::sup() const  Data::sup() const
1297  {  {
1298    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1299    //    //
1300    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1301    FMax fmax_func;    FMax fmax_func;
1302    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1303  #ifdef PASO_MPI  #ifdef PASO_MPI
1304      double globalValue;
1305    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1306    return globalValue;    return globalValue;
1307  #else  #else
# Line 1273  Data::sup() const Line 1312  Data::sup() const
1312  double  double
1313  Data::inf() const  Data::inf() const
1314  {  {
1315    double localValue, globalValue;    double localValue;
 #if defined DOPROF  
   profData->reduction1++;  
 #endif  
1316    //    //
1317    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1318    FMin fmin_func;    FMin fmin_func;
1319    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1320  #ifdef PASO_MPI  #ifdef PASO_MPI
1321      double globalValue;
1322    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1323    return globalValue;    return globalValue;
1324  #else  #else
# Line 1294  Data::inf() const Line 1331  Data::inf() const
1331  Data  Data
1332  Data::maxval() const  Data::maxval() const
1333  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1334    //    //
1335    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1336    FMax fmax_func;    FMax fmax_func;
# Line 1306  Data::maxval() const Line 1340  Data::maxval() const
1340  Data  Data
1341  Data::minval() const  Data::minval() const
1342  {  {
 #if defined DOPROF  
   profData->reduction2++;  
 #endif  
1343    //    //
1344    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1345    FMin fmin_func;    FMin fmin_func;
# Line 1316  Data::minval() const Line 1347  Data::minval() const
1347  }  }
1348    
1349  Data  Data
1350  Data::trace() const  Data::swapaxes(const int axis0, const int axis1) const
1351  {  {
1352  #if defined DOPROF       int axis0_tmp,axis1_tmp;
1353    profData->reduction2++;       DataTypes::ShapeType s=getDataPointShape();
1354  #endif       DataTypes::ShapeType ev_shape;
1355    Trace trace_func;       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
1356    return dp_algorithm(trace_func,0);       // which goes thru all shape vector elements starting with axis_offset (at index=rank wrap around to 0)
1357         int rank=getDataPointRank();
1358         if (rank<2) {
1359            throw DataException("Error - Data::swapaxes argument must have at least rank 2.");
1360         }
1361         if (axis0<0 || axis0>rank-1) {
1362            throw DataException("Error - Data::swapaxes: axis0 must be between 0 and rank-1=" + rank-1);
1363         }
1364         if (axis1<0 || axis1>rank-1) {
1365             throw DataException("Error - Data::swapaxes: axis1 must be between 0 and rank-1=" + rank-1);
1366         }
1367         if (axis0 == axis1) {
1368             throw DataException("Error - Data::swapaxes: axis indices must be different.");
1369         }
1370         if (axis0 > axis1) {
1371             axis0_tmp=axis1;
1372             axis1_tmp=axis0;
1373         } else {
1374             axis0_tmp=axis0;
1375             axis1_tmp=axis1;
1376         }
1377         for (int i=0; i<rank; i++) {
1378           if (i == axis0_tmp) {
1379              ev_shape.push_back(s[axis1_tmp]);
1380           } else if (i == axis1_tmp) {
1381              ev_shape.push_back(s[axis0_tmp]);
1382           } else {
1383              ev_shape.push_back(s[i]);
1384           }
1385         }
1386         Data ev(0.,ev_shape,getFunctionSpace());
1387         ev.typeMatchRight(*this);
1388         m_data->swapaxes(ev.m_data.get(), axis0_tmp, axis1_tmp);
1389         return ev;
1390    
1391  }  }
1392    
1393  Data  Data
1394  Data::symmetric() const  Data::symmetric() const
1395  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1396       // check input       // check input
1397       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1398       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1399          if(s[0] != s[1])          if(s[0] != s[1])
1400             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.");
1401       }       }
1402       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
# Line 1353  Data::symmetric() const Line 1415  Data::symmetric() const
1415  Data  Data
1416  Data::nonsymmetric() const  Data::nonsymmetric() const
1417  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1418       // check input       // check input
1419       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1420       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1421          if(s[0] != s[1])          if(s[0] != s[1])
1422             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.");
1423          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1424          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1425          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1426          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1372  Data::nonsymmetric() const Line 1431  Data::nonsymmetric() const
1431       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1432          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1433             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.");
1434          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1435          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1436          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1437          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1388  Data::nonsymmetric() const Line 1447  Data::nonsymmetric() const
1447  }  }
1448    
1449  Data  Data
1450  Data::matrixtrace(int axis_offset) const  Data::trace(int axis_offset) const
1451  {  {
1452       #if defined DOPROF       DataTypes::ShapeType s=getDataPointShape();
         profData->unary++;  
      #endif  
      DataArrayView::ShapeType s=getDataPointShape();  
1453       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1454          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1455          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1456          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1457          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1458          return ev;          return ev;
1459       }       }
1460       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1461          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1462          if (axis_offset==0) {          if (axis_offset==0) {
1463            int s2=s[2];            int s2=s[2];
1464            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1413  Data::matrixtrace(int axis_offset) const Line 1469  Data::matrixtrace(int axis_offset) const
1469          }          }
1470          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1471          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1472          m_data->matrixtrace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1473          return ev;          return ev;
1474       }       }
1475       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1476          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1477          if (axis_offset==0) {          if (axis_offset==0) {
1478            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1479            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1432  Data::matrixtrace(int axis_offset) const Line 1488  Data::matrixtrace(int axis_offset) const
1488      }      }
1489          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1490          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1491      m_data->matrixtrace(ev.m_data.get(), axis_offset);      m_data->trace(ev.m_data.get(), axis_offset);
1492          return ev;          return ev;
1493       }       }
1494       else {       else {
1495          throw DataException("Error - Data::matrixtrace can only be calculated for rank 2, 3 or 4 object.");          throw DataException("Error - Data::trace can only be calculated for rank 2, 3 or 4 object.");
1496       }       }
1497  }  }
1498    
1499  Data  Data
1500  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1501  {  {
1502  #if defined DOPROF       DataTypes::ShapeType s=getDataPointShape();
1503       profData->reduction2++;       DataTypes::ShapeType ev_shape;
 #endif  
      DataArrayView::ShapeType s=getDataPointShape();  
      DataArrayView::ShapeType ev_shape;  
1504       // 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]
1505       // 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)
1506       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1467  Data::transpose(int axis_offset) const Line 1520  Data::transpose(int axis_offset) const
1520  Data  Data
1521  Data::eigenvalues() const  Data::eigenvalues() const
1522  {  {
      #if defined DOPROF  
         profData->unary++;  
      #endif  
1523       // check input       // check input
1524       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1525       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1526          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.");
1527       if(s[0] != s[1])       if(s[0] != s[1])
1528          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.");
1529       // create return       // create return
1530       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1531       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1532       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1533       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1487  Data::eigenvalues() const Line 1537  Data::eigenvalues() const
1537  const boost::python::tuple  const boost::python::tuple
1538  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1539  {  {
1540       #if defined DOPROF       DataTypes::ShapeType s=getDataPointShape();
1541          profData->unary++;       if (getDataPointRank()!=2)
      #endif  
      DataArrayView::ShapeType s=getDataPointShape();  
      if (getDataPointRank()!=2)  
1542          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.");
1543       if(s[0] != s[1])       if(s[0] != s[1])
1544          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.");
1545       // create return       // create return
1546       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1547       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1548       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1549       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1550       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1551       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1552       m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);       m_data->eigenvalues_and_eigenvectors(ev.m_data.get(),V.m_data.get(),tol);
# Line 1507  Data::eigenvalues_and_eigenvectors(const Line 1554  Data::eigenvalues_and_eigenvectors(const
1554  }  }
1555    
1556  const boost::python::tuple  const boost::python::tuple
1557  Data::mindp() const  Data::minGlobalDataPoint() const
1558  {  {
1559    // 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
1560    // 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
1561    // surrounding function    // surrounding function
1562    
   int SampleNo;  
1563    int DataPointNo;    int DataPointNo;
1564      int ProcNo;    int ProcNo;
1565    calc_mindp(ProcNo,SampleNo,DataPointNo);    calc_minGlobalDataPoint(ProcNo,DataPointNo);
1566    return make_tuple(ProcNo,SampleNo,DataPointNo);    return make_tuple(ProcNo,DataPointNo);
1567  }  }
1568    
1569  void  void
1570  Data::calc_mindp(   int& ProcNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1571                  int& SampleNo,                          int& DataPointNo) const
         int& DataPointNo) const  
1572  {  {
1573    int i,j;    int i,j;
1574    int lowi=0,lowj=0;    int lowi=0,lowj=0;
# Line 1535  Data::calc_mindp(  int& ProcNo, Line 1580  Data::calc_mindp(  int& ProcNo,
1580    int numDPPSample=temp.getNumDataPointsPerSample();    int numDPPSample=temp.getNumDataPointsPerSample();
1581    
1582    double next,local_min;    double next,local_min;
1583    int local_lowi,local_lowj;    int local_lowi=0,local_lowj=0;    
1584    
1585    #pragma omp parallel private(next,local_min,local_lowi,local_lowj)    #pragma omp parallel private(next,local_min,local_lowi,local_lowj)
1586    {    {
# Line 1543  Data::calc_mindp(  int& ProcNo, Line 1588  Data::calc_mindp(  int& ProcNo,
1588      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1589      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1590        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1591          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1592          if (next<local_min) {          if (next<local_min) {
1593            local_min=next;            local_min=next;
1594            local_lowi=i;            local_lowi=i;
# Line 1561  Data::calc_mindp(  int& ProcNo, Line 1606  Data::calc_mindp(  int& ProcNo,
1606    
1607  #ifdef PASO_MPI  #ifdef PASO_MPI
1608      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1609      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1610      int lowProc = 0;      int lowProc = 0;
1611      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1612      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() );
1613        
1614      if( get_MPIRank()==0 ){      if( get_MPIRank()==0 ){
1615          next = globalMins[lowProc];          next = globalMins[lowProc];
1616          for( i=1; i<get_MPISize(); i++ )          for( i=1; i<get_MPISize(); i++ )
# Line 1581  Data::calc_mindp(  int& ProcNo, Line 1626  Data::calc_mindp(  int& ProcNo,
1626  #else  #else
1627      ProcNo = 0;      ProcNo = 0;
1628  #endif  #endif
1629    SampleNo = lowi;    DataPointNo = lowj + lowi * numDPPSample;
   DataPointNo = lowj;  
1630  }  }
1631    
1632  void  void
1633  Data::saveDX(std::string fileName) const  Data::saveDX(std::string fileName) const
1634  {  {
1635      if (isEmpty())
1636      {
1637        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1638      }
1639    boost::python::dict args;    boost::python::dict args;
1640    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1641    getDomain().saveDX(fileName,args);    getDomain()->saveDX(fileName,args);
1642    return;    return;
1643  }  }
1644    
1645  void  void
1646  Data::saveVTK(std::string fileName) const  Data::saveVTK(std::string fileName) const
1647  {  {
1648      if (isEmpty())
1649      {
1650        throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1651      }
1652    boost::python::dict args;    boost::python::dict args;
1653    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1654    getDomain().saveVTK(fileName,args);    getDomain()->saveVTK(fileName,args);
1655    return;    return;
1656  }  }
1657    
# Line 1609  Data::operator+=(const Data& right) Line 1661  Data::operator+=(const Data& right)
1661    if (isProtected()) {    if (isProtected()) {
1662          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1663    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1664    binaryOp(right,plus<double>());    binaryOp(right,plus<double>());
1665    return (*this);    return (*this);
1666  }  }
# Line 1619  Data::operator+=(const Data& right) Line 1668  Data::operator+=(const Data& right)
1668  Data&  Data&
1669  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
1670  {  {
1671    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1672          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,plus<double>());
1673    }    return (*this);
1674  #if defined DOPROF  }
1675    profData->binary++;  Data&
1676  #endif  Data::operator=(const Data& other)
1677    binaryOp(right,plus<double>());  {
1678      copy(other);
1679    return (*this);    return (*this);
1680  }  }
1681    
# Line 1635  Data::operator-=(const Data& right) Line 1685  Data::operator-=(const Data& right)
1685    if (isProtected()) {    if (isProtected()) {
1686          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1687    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1688    binaryOp(right,minus<double>());    binaryOp(right,minus<double>());
1689    return (*this);    return (*this);
1690  }  }
# Line 1645  Data::operator-=(const Data& right) Line 1692  Data::operator-=(const Data& right)
1692  Data&  Data&
1693  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
1694  {  {
1695    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1696          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,minus<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,minus<double>());  
1697    return (*this);    return (*this);
1698  }  }
1699    
# Line 1661  Data::operator*=(const Data& right) Line 1703  Data::operator*=(const Data& right)
1703    if (isProtected()) {    if (isProtected()) {
1704          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1705    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1706    binaryOp(right,multiplies<double>());    binaryOp(right,multiplies<double>());
1707    return (*this);    return (*this);
1708  }  }
# Line 1671  Data::operator*=(const Data& right) Line 1710  Data::operator*=(const Data& right)
1710  Data&  Data&
1711  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
1712  {  {
1713    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1714          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,multiplies<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,multiplies<double>());  
1715    return (*this);    return (*this);
1716  }  }
1717    
# Line 1687  Data::operator/=(const Data& right) Line 1721  Data::operator/=(const Data& right)
1721    if (isProtected()) {    if (isProtected()) {
1722          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1723    }    }
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1724    binaryOp(right,divides<double>());    binaryOp(right,divides<double>());
1725    return (*this);    return (*this);
1726  }  }
# Line 1697  Data::operator/=(const Data& right) Line 1728  Data::operator/=(const Data& right)
1728  Data&  Data&
1729  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
1730  {  {
1731    if (isProtected()) {    Data tmp(right,getFunctionSpace(),false);
1732          throw DataException("Error - attempt to update protected Data object.");    binaryOp(tmp,divides<double>());
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
   binaryOp(right,divides<double>());  
1733    return (*this);    return (*this);
1734  }  }
1735    
1736  Data  Data
1737  Data::rpowO(const boost::python::object& left) const  Data::rpowO(const boost::python::object& left) const
1738  {  {
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
 #if defined DOPROF  
   profData->binary++;  
 #endif  
1739    Data left_d(left,*this);    Data left_d(left,*this);
1740    return left_d.powD(*this);    return left_d.powD(*this);
1741  }  }
# Line 1723  Data::rpowO(const boost::python::object& Line 1743  Data::rpowO(const boost::python::object&
1743  Data  Data
1744  Data::powO(const boost::python::object& right) const  Data::powO(const boost::python::object& right) const
1745  {  {
1746  #if defined DOPROF    Data tmp(right,getFunctionSpace(),false);
1747    profData->binary++;    return powD(tmp);
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1748  }  }
1749    
1750  Data  Data
1751  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1752  {  {
1753  #if defined DOPROF    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   profData->binary++;  
 #endif  
   Data result;  
   result.copy(*this);  
   result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   return result;  
1754  }  }
1755    
   
1756  //  //
1757  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1758  Data  Data
1759  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1760  {  {
1761    Data result;    return C_TensorBinaryOperation(left, right, plus<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1762  }  }
1763    
1764  //  //
# Line 1763  escript::operator+(const Data& left, con Line 1766  escript::operator+(const Data& left, con
1766  Data  Data
1767  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
1768  {  {
1769    Data result;    return C_TensorBinaryOperation(left, right, minus<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
1770  }  }
1771    
1772  //  //
# Line 1776  escript::operator-(const Data& left, con Line 1774  escript::operator-(const Data& left, con
1774  Data  Data
1775  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
1776  {  {
1777    Data result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
1778  }  }
1779    
1780  //  //
# Line 1789  escript::operator*(const Data& left, con Line 1782  escript::operator*(const Data& left, con
1782  Data  Data
1783  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
1784  {  {
1785    Data result;    return C_TensorBinaryOperation(left, right, divides<double>());
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
1786  }  }
1787    
1788  //  //
# Line 1802  escript::operator/(const Data& left, con Line 1790  escript::operator/(const Data& left, con
1790  Data  Data
1791  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
1792  {  {
1793    //    return left+Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result+=right;  
   return result;  
1794  }  }
1795    
1796  //  //
# Line 1818  escript::operator+(const Data& left, con Line 1798  escript::operator+(const Data& left, con
1798  Data  Data
1799  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
1800  {  {
1801    //    return left-Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result-=right;  
   return result;  
1802  }  }
1803    
1804  //  //
# Line 1834  escript::operator-(const Data& left, con Line 1806  escript::operator-(const Data& left, con
1806  Data  Data
1807  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
1808  {  {
1809    //    return left*Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result*=right;  
   return result;  
1810  }  }
1811    
1812  //  //
# Line 1850  escript::operator*(const Data& left, con Line 1814  escript::operator*(const Data& left, con
1814  Data  Data
1815  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
1816  {  {
1817    //    return left/Data(right,left.getFunctionSpace(),false);
   // Convert to DataArray format if possible  
   DataArray temp(right);  
   Data result;  
   //  
   // perform a deep copy  
   result.copy(left);  
   result/=right;  
   return result;  
1818  }  }
1819    
1820  //  //
# Line 1866  escript::operator/(const Data& left, con Line 1822  escript::operator/(const Data& left, con
1822  Data  Data
1823  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
1824  {  {
1825    //    return Data(left,right.getFunctionSpace(),false)+right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result+=right;  
   return result;  
1826  }  }
1827    
1828  //  //
# Line 1879  escript::operator+(const boost::python:: Line 1830  escript::operator+(const boost::python::
1830  Data  Data
1831  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
1832  {  {
1833    //    return Data(left,right.getFunctionSpace(),false)-right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result-=right;  
   return result;  
1834  }  }
1835    
1836  //  //
# Line 1892  escript::operator-(const boost::python:: Line 1838  escript::operator-(const boost::python::
1838  Data  Data
1839  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
1840  {  {
1841    //    return Data(left,right.getFunctionSpace(),false)*right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result*=right;  
   return result;  
1842  }  }
1843    
1844  //  //
# Line 1905  escript::operator*(const boost::python:: Line 1846  escript::operator*(const boost::python::
1846  Data  Data
1847  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
1848  {  {
1849    //    return Data(left,right.getFunctionSpace(),false)/right;
   // Construct the result using the given value and the other parameters  
   // from right  
   Data result(left,right);  
   result/=right;  
   return result;  
1850  }  }
1851    
1852  //  //
# Line 1961  escript::operator/(const boost::python:: Line 1897  escript::operator/(const boost::python::
1897  /* TODO */  /* TODO */
1898  /* global reduction */  /* global reduction */
1899  Data  Data
1900  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1901  {  {
   const DataArrayView& view=getPointDataView();  
1902    
1903    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1904    
1905    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1906      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1907    }    }
1908    
# Line 1977  Data::getItem(const boost::python::objec Line 1912  Data::getItem(const boost::python::objec
1912  /* TODO */  /* TODO */
1913  /* global reduction */  /* global reduction */
1914  Data  Data
1915  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
1916  {  {
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1917    return Data(*this,region);    return Data(*this,region);
1918  }  }
1919    
# Line 1995  Data::setItemO(const boost::python::obje Line 1927  Data::setItemO(const boost::python::obje
1927    setItemD(key,tempData);    setItemD(key,tempData);
1928  }  }
1929    
 /* TODO */  
 /* global reduction */  
1930  void  void
1931  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
1932                 const Data& value)                 const Data& value)
1933  {  {
1934    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1935    
1936    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1937    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1938      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1939    }    }
1940    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 2014  Data::setItemD(const boost::python::obje Line 1944  Data::setItemD(const boost::python::obje
1944    }    }
1945  }  }
1946    
 /* TODO */  
 /* global reduction */  
1947  void  void
1948  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1949                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
1950  {  {
1951    if (isProtected()) {    if (isProtected()) {
1952          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1953    }    }
 #if defined DOPROF  
   profData->slicing++;  
 #endif  
1954    Data tempValue(value);    Data tempValue(value);
1955    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
1956    typeMatchRight(tempValue);    typeMatchRight(tempValue);
# Line 2060  Data::typeMatchRight(const Data& right) Line 1985  Data::typeMatchRight(const Data& right)
1985    }    }
1986  }  }
1987    
1988  /* TODO */  void
1989  /* global reduction */  Data::setTaggedValueByName(std::string name,
1990                               const boost::python::object& value)
1991    {
1992         if (getFunctionSpace().getDomain()->isValidTagName(name)) {
1993            int tagKey=getFunctionSpace().getDomain()->getTag(name);
1994            setTaggedValue(tagKey,value);
1995         }
1996    }
1997  void  void
1998  Data::setTaggedValue(int tagKey,  Data::setTaggedValue(int tagKey,
1999                       const boost::python::object& value)                       const boost::python::object& value)
# Line 2071  Data::setTaggedValue(int tagKey, Line 2003  Data::setTaggedValue(int tagKey,
2003    }    }
2004    //    //
2005    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2006    tag();    if (isConstant()) tag();
2007    
2008      numeric::array asNumArray(value);
2009    
2010    
2011    if (!isTagged()) {    // extract the shape of the numarray
2012      throw DataException("Error - DataTagged conversion failed!!");    DataTypes::ShapeType tempShape;
2013      for (int i=0; i < asNumArray.getrank(); i++) {
2014        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2015    }    }
2016    
2017    //    // get the space for the data vector
2018    // Construct DataArray from boost::python::object input value  //   int len = DataTypes::noValues(tempShape);
2019    DataArray valueDataArray(value);  //   DataVector temp_data(len, 0.0, len);
2020    //   DataArrayView temp_dataView(temp_data, tempShape);
2021    //   temp_dataView.copy(asNumArray);
2022    
2023      DataVector temp_data2;
2024      temp_data2.copyFromNumArray(asNumArray);
2025    
2026    //    //
2027    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2028    m_data->setTaggedValue(tagKey,valueDataArray.getView());    //m_data->setTaggedValue(tagKey,temp_dataView);
2029    
2030        m_data->setTaggedValue(tagKey,tempShape, temp_data2);
2031  }  }
2032    
2033  /* TODO */  
 /* global reduction */  
2034  void  void
2035  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
2036                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
2037                                const DataTypes::ValueType& value,
2038                    int dataOffset)
2039  {  {
2040    if (isProtected()) {    if (isProtected()) {
2041          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2042    }    }
2043    //    //
2044    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2045    tag();    if (isConstant()) tag();
2046    
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
2047    //    //
2048    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2049    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
2050  }  }
2051    
 /* TODO */  
 /* global reduction */  
2052  int  int
2053  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
2054  {  {
2055    return m_data->getTagNumber(dpno);    if (isEmpty())
2056  }    {
2057        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.");  
2058    }    }
2059    //    return getFunctionSpace().getTagFromDataPointNo(dpno);
   // Construct DataArray from boost::python::object input value  
   DataArray valueDataArray(value);  
   
   //  
   // Call DataAbstract::setRefValue  
   m_data->setRefValue(ref,valueDataArray);  
2060  }  }
2061    
 /* 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  
2062    
2063    // extract the shape of the numarray  ostream& escript::operator<<(ostream& o, const Data& data)
2064    int rank = value.getrank();  {
2065    DataArrayView::ShapeType shape;    o << data.toString();
2066    for (int i=0; i < rank; i++) {    return o;
2067      shape.push_back(extract<int>(value.getshape()[i]));  }
   }  
   
   // and load the numarray with the data from the DataArray  
   DataArrayView valueView = valueDataArray.getView();  
2068    
2069    if (rank==0) {  Data
2070        boost::python::numeric::array temp_numArray(valueView());  escript::C_GeneralTensorProduct(Data& arg_0,
2071        value = temp_numArray;                       Data& arg_1,
2072    }                       int axis_offset,
2073    if (rank==1) {                       int transpose)
2074      for (int i=0; i < shape[0]; i++) {  {
2075        value[i] = valueView(i);    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2076      // SM is the product of the last axis_offset entries in arg_0.getShape().
2077    
2078      // Interpolate if necessary and find an appropriate function space
2079      Data arg_0_Z, arg_1_Z;
2080      if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
2081        if (arg_0.probeInterpolation(arg_1.getFunctionSpace())) {
2082          arg_0_Z = arg_0.interpolate(arg_1.getFunctionSpace());
2083          arg_1_Z = Data(arg_1);
2084        }
2085        else if (arg_1.probeInterpolation(arg_0.getFunctionSpace())) {
2086          arg_1_Z=arg_1.interpolate(arg_0.getFunctionSpace());
2087          arg_0_Z =Data(arg_0);
2088      }      }
2089    }      else {
2090    if (rank==2) {        throw DataException("Error - C_GeneralTensorProduct: arguments have incompatible function spaces.");
     for (int i=0; i < shape[0]; i++) {  
       for (int j=0; j < shape[1]; j++) {  
         value[i][j] = valueView(i,j);  
       }  
2091      }      }
2092      } else {
2093          arg_0_Z = Data(arg_0);
2094          arg_1_Z = Data(arg_1);
2095    }    }
2096    if (rank==3) {    // Get rank and shape of inputs
2097      for (int i=0; i < shape[0]; i++) {    int rank0 = arg_0_Z.getDataPointRank();
2098        for (int j=0; j < shape[1]; j++) {    int rank1 = arg_1_Z.getDataPointRank();
2099          for (int k=0; k < shape[2]; k++) {    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2100            value[i][j][k] = valueView(i,j,k);    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2101          }  
2102        }    // Prepare for the loops of the product and verify compatibility of shapes
2103      int start0=0, start1=0;
2104      if (transpose == 0)       {}
2105      else if (transpose == 1)  { start0 = axis_offset; }
2106      else if (transpose == 2)  { start1 = rank1-axis_offset; }
2107      else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2108    
2109    
2110      // Adjust the shapes for transpose
2111      DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2112      DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2113      for (int i=0; i<rank0; i++)   { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2114      for (int i=0; i<rank1; i++)   { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2115    
2116    #if 0
2117      // For debugging: show shape after transpose
2118      char tmp[100];
2119      std::string shapeStr;
2120      shapeStr = "(";
2121      for (int i=0; i<rank0; i++)   { sprintf(tmp, "%d,", tmpShape0[i]); shapeStr += tmp; }
2122      shapeStr += ")";
2123      cout << "C_GeneralTensorProduct: Shape of arg0 is " << shapeStr << endl;
2124      shapeStr = "(";
2125      for (int i=0; i<rank1; i++)   { sprintf(tmp, "%d,", tmpShape1[i]); shapeStr += tmp; }
2126      shapeStr += ")";
2127      cout << "C_GeneralTensorProduct: Shape of arg1 is " << shapeStr << endl;
2128    #endif
2129    
2130      // Prepare for the loops of the product
2131      int SL=1, SM=1, SR=1;
2132      for (int i=0; i<rank0-axis_offset; i++)   {
2133        SL *= tmpShape0[i];
2134      }
2135      for (int i=rank0-axis_offset; i<rank0; i++)   {
2136        if (tmpShape0[i] != tmpShape1[i-(rank0-axis_offset)]) {
2137          throw DataException("C_GeneralTensorProduct: Error - incompatible shapes");
2138        }
2139        SM *= tmpShape0[i];
2140      }
2141      for (int i=axis_offset; i<rank1; i++)     {
2142        SR *= tmpShape1[i];
2143      }
2144    
2145      // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2146      DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2147      {         // block to limit the scope of out_index
2148         int out_index=0;
2149         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2150         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2151      }
2152    
2153      // Declare output Data object
2154      Data res;
2155    
2156      if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2157        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2158        double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2159        double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2160        double *ptr_2 = &(res.getDataAtOffset(0));
2161        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2162      }
2163      else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
2164    
2165        // Prepare the DataConstant input
2166        DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2167        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2168    
2169        // Borrow DataTagged input from Data object
2170        DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2171        if (tmp_1==0) { throw DataException("GTP_1 Programming error - casting to DataTagged."); }
2172    
2173        // Prepare a DataTagged output 2
2174        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataTagged output
2175        res.tag();
2176        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2177        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2178    
2179        // Prepare offset into DataConstant
2180        int offset_0 = tmp_0->getPointOffset(0,0);
2181        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2182        // Get the views
2183    //     DataArrayView view_1 = tmp_1->getDefaultValue();
2184    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2185    //     // Get the pointers to the actual data
2186    //     double *ptr_1 = &((view_1.getData())[0]);
2187    //     double *ptr_2 = &((view_2.getData())[0]);
2188    
2189        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2190        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2191    
2192    
2193        // Compute an MVP for the default
2194        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2195        // Compute an MVP for each tag
2196        const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2197        DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2198        for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2199          tmp_2->addTag(i->first);
2200    //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2201    //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2202    //       double *ptr_1 = &view_1.getData(0);
2203    //       double *ptr_2 = &view_2.getData(0);
2204    
2205          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2206          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2207        
2208          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2209      }      }
2210    
2211    }    }
2212    if (rank==4) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isExpanded()) {
2213      for (int i=0; i < shape[0]; i++) {  
2214        for (int j=0; j < shape[1]; j++) {      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2215          for (int k=0; k < shape[2]; k++) {      DataConstant* tmp_0=dynamic_cast<DataConstant*>(arg_0_Z.borrowData());
2216            for (int l=0; l < shape[3]; l++) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2217              value[i][j][k][l] = valueView(i,j,k,l);      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2218            }      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2219          }      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2220        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2221        int sampleNo_1,dataPointNo_1;
2222        int numSamples_1 = arg_1_Z.getNumSamples();
2223        int numDataPointsPerSample_1 = arg_1_Z.getNumDataPointsPerSample();
2224        int offset_0 = tmp_0->getPointOffset(0,0);
2225        #pragma omp parallel for private(sampleNo_1,dataPointNo_1) schedule(static)
2226        for (sampleNo_1 = 0; sampleNo_1 < numSamples_1; sampleNo_1++) {
2227          for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2228            int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2229            int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2230            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2231            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2232            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2233            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2234        }        }
2235      }      }
2236    
2237    }    }
2238      else if (arg_0_Z.isTagged()     && arg_1_Z.isConstant()) {
2239    
2240  }      // Borrow DataTagged input from Data object
2241        DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2242        if (tmp_0==0) { throw DataException("GTP_0 Programming error - casting to DataTagged."); }
2243    
2244  void      // Prepare the DataConstant input
2245  Data::archiveData(const std::string fileName)      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2246  {      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
   cout << "Archiving Data object to: " << fileName << endl;  
2247    
2248    //      // Prepare a DataTagged output 2
2249    // Determine type of this Data object      res = Data(0.0, shape2, arg_0_Z.getFunctionSpace());    // DataTagged output
2250    int dataType = -1;      res.tag();
2251        DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2252        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2253    
2254    if (isEmpty()) {      // Prepare offset into DataConstant
2255      dataType = 0;      int offset_1 = tmp_1->getPointOffset(0,0);
2256      cout << "\tdataType: DataEmpty" << endl;      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2257    }      // Get the views
2258    if (isConstant()) {  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2259      dataType = 1;  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2260      cout << "\tdataType: DataConstant" << endl;  //     // Get the pointers to the actual data
2261    }  //     double *ptr_0 = &((view_0.getData())[0]);
2262    if (isTagged()) {  //     double *ptr_2 = &((view_2.getData())[0]);
     dataType = 2;  
     cout << "\tdataType: DataTagged" << endl;  
   }  
   if (isExpanded()) {  
     dataType = 3;  
     cout << "\tdataType: DataExpanded" << endl;  
   }  
2263    
2264    if (dataType == -1) {      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2265      throw DataException("archiveData Error: undefined dataType");      double *ptr_2 = &(tmp_2->getDefaultValue(0));
   }  
2266    
2267    //      // Compute an MVP for the default
2268    // Collect data items common to all Data types      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2269    int noSamples = getNumSamples();      // Compute an MVP for each tag
2270    int noDPPSample = getNumDataPointsPerSample();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2271    int functionSpaceType = getFunctionSpace().getTypeCode();      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2272    int dataPointRank = getDataPointRank();      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2273    int dataPointSize = getDataPointSize();  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2274    int dataLength = getLength();  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2275    DataArrayView::ShapeType dataPointShape = getDataPointShape();  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2276    vector<int> referenceNumbers(noSamples);  //       double *ptr_0 = &view_0.getData(0);
2277    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  //       double *ptr_2 = &view_2.getData(0);
2278      referenceNumbers[sampleNo] = getFunctionSpace().getReferenceNoFromSampleNo(sampleNo);  
2279    }        tmp_2->addTag(i->first);
2280    vector<int> tagNumbers(noSamples);        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2281    if (isTagged()) {        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2282      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
       tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);  
2283      }      }
2284    
2285    }    }
2286      else if (arg_0_Z.isTagged()     && arg_1_Z.isTagged()) {
2287    
2288    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;      // Borrow DataTagged input from Data object
2289    cout << "\tfunctionSpaceType: " << functionSpaceType << endl;      DataTagged* tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2290    cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2291    
2292    //      // Borrow DataTagged input from Data object
2293    // Flatten Shape to an array of integers suitable for writing to file      DataTagged* tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2294    int flatShape[4] = {0,0,0,0};      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
   cout << "\tshape: < ";  
   for (int dim=0; dim<dataPointRank; dim++) {  
     flatShape[dim] = dataPointShape[dim];  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
2295    
2296    //      // Prepare a DataTagged output 2
2297    // Open archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());
2298    ofstream archiveFile;      res.tag();  // DataTagged output
2299    archiveFile.open(fileName.data(), ios::out);      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2300        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2301    
2302    if (!archiveFile.good()) {  //     // Get the views
2303      throw DataException("archiveData Error: problem opening archive file");  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2304    }  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2305    //     DataArrayView view_2 = tmp_2->getDefaultValue();
2306    //     // Get the pointers to the actual data
2307    //     double *ptr_0 = &((view_0.getData())[0]);
2308    //     double *ptr_1 = &((view_1.getData())[0]);
2309    //     double *ptr_2 = &((view_2.getData())[0]);
2310    
2311    //      double *ptr_0 = &(tmp_0->getDefaultValue(0));
2312    // Write common data items to archive file      double *ptr_1 = &(tmp_1->getDefaultValue(0));
2313    archiveFile.write(reinterpret_cast<char *>(&dataType),sizeof(int));      double *ptr_2 = &(tmp_2->getDefaultValue(0));
   archiveFile.write(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.write(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.write(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.write(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
2314    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing to archive file");  
   }  
2315    
2316    //      // Compute an MVP for the default
2317    // Archive underlying data values for each Data type      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2318    int noValues;      // Merge the tags
2319    switch (dataType) {      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2320      case 0:      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2321        // DataEmpty      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2322        noValues = 0;      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2323        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));        tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2324        cout << "\tnoValues: " << noValues << endl;      }
2325        break;      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2326      case 1:        tmp_2->addTag(i->first);
2327        // DataConstant      }
2328        noValues = m_data->getLength();      // Compute an MVP for each tag
2329        archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2330        cout << "\tnoValues: " << noValues << endl;      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2331        if (m_data->archiveData(archiveFile,noValues)) {  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2332          throw DataException("archiveData Error: problem writing data to archive file");  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2333        }  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2334        break;  //       double *ptr_0 = &view_0.getData(0);
2335      case 2:  //       double *ptr_1 = &view_1.getData(0);
2336        // DataTagged  //       double *ptr_2 = &view_2.getData(0);
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
   }  
2337    
2338    if (!archiveFile.good()) {        double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2339      throw DataException("archiveData Error: problem writing data to archive file");        double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2340    }        double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2341    
2342    //        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2343    // Close archive file      }
   archiveFile.close();  
2344    
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem closing archive file");  
2345    }    }
2346      else if (arg_0_Z.isTagged()     && arg_1_Z.isExpanded()) {
2347    
2348  }      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2349        res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2350        DataTagged*   tmp_0=dynamic_cast<DataTagged*>(arg_0_Z.borrowData());
2351        DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2352        DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2353        if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2354        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2355        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2356        int sampleNo_0,dataPointNo_0;
2357        int numSamples_0 = arg_0_Z.getNumSamples();
2358        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2359        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2360        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2361          int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2362          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2363          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2364            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2365            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2366            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2367            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2368            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2369          }
2370        }
2371    
 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");  
2372    }    }
2373      else if (arg_0_Z.isExpanded()   && arg_1_Z.isConstant()) {
2374    
2375    cout << "Extracting Data object from: " << fileName << endl;      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2376        DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2377    int dataType;      DataConstant* tmp_1=dynamic_cast<DataConstant*>(arg_1_Z.borrowData());
2378    int noSamples;      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2379    int noDPPSample;      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2380    int functionSpaceType;      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataConstant."); }
2381    int dataPointRank;      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2382    int dataPointSize;      int sampleNo_0,dataPointNo_0;
2383    int dataLength;      int numSamples_0 = arg_0_Z.getNumSamples();
2384    DataArrayView::ShapeType dataPointShape;      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2385    int flatShape[4];      int offset_1 = tmp_1->getPointOffset(0,0);
2386        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2387        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2388          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2389            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2390            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2391            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2392            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2393            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2394            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2395          }
2396        }
2397    
   //  
   // Open the archive file  
   ifstream archiveFile;  
   archiveFile.open(fileName.data(), ios::in);  
2398    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem opening archive file");  
2399    }    }
2400      else if (arg_0_Z.isExpanded()   && arg_1_Z.isTagged()) {
2401    
2402    //      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2403    // Read common data items from archive file      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2404    archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2405    archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));      DataTagged*   tmp_1=dynamic_cast<DataTagged*>(arg_1_Z.borrowData());
2406    archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2407    archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2408    archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));      if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2409    archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2410    archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));      int sampleNo_0,dataPointNo_0;
2411    for (int dim = 0; dim < 4; dim++) {      int numSamples_0 = arg_0_Z.getNumSamples();
2412      archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));      int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2413      if (flatShape[dim]>0) {      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2414        dataPointShape.push_back(flatShape[dim]);      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2415          int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2416          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2417          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2418            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2419            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2420            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2421            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2422            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2423          }
2424      }      }
2425    
2426    }    }
2427    vector<int> referenceNumbers(noSamples);    else if (arg_0_Z.isExpanded()   && arg_1_Z.isExpanded()) {
2428    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
2429      archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));      // After finding a common function space above the two inputs have the same numSamples and num DPPS
2430    }      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace(),true); // DataExpanded output
2431    vector<int> tagNumbers(noSamples);      DataExpanded* tmp_0=dynamic_cast<DataExpanded*>(arg_0_Z.borrowData());
2432    if (dataType==2) {      DataExpanded* tmp_1=dynamic_cast<DataExpanded*>(arg_1_Z.borrowData());
2433      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      DataExpanded* tmp_2=dynamic_cast<DataExpanded*>(res.borrowData());
2434        archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));      if (tmp_0==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2435        if (tmp_1==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2436        if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataExpanded."); }
2437        int sampleNo_0,dataPointNo_0;
2438        int numSamples_0 = arg_0_Z.getNumSamples();
2439        int numDataPointsPerSample_0 = arg_0_Z.getNumDataPointsPerSample();
2440        #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2441        for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2442          for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2443            int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2444            int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2445            int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2446            double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2447            double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2448            double *ptr_2 = &(res.getDataAtOffset(offset_2));
2449            matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2450          }
2451      }      }
   }  
2452    
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
2453    }    }
2454      else {
2455    //      throw DataException("Error - C_GeneralTensorProduct: unknown combination of inputs");
   // Verify the values just read from the archive file  
   switch (dataType) {  
     case 0:  
       cout << "\tdataType: DataEmpty" << endl;  
       break;  
     case 1:  
       cout << "\tdataType: DataConstant" << endl;  
       break;  
     case 2:  
       cout << "\tdataType: DataTagged" << endl;  
       break;  
     case 3:  
       cout << "\tdataType: DataExpanded" << endl;  
       break;  
     default:  
       throw DataException("extractData Error: undefined dataType read from archive file");  
       break;  
2456    }    }
2457    
2458    cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;    return res;
2459    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;  
2460    
2461    //  DataAbstract*
2462    // Verify that supplied FunctionSpace object is compatible with this Data object.  Data::borrowData() const
2463    if ( (fspace.getTypeCode()!=functionSpaceType) ||  {
2464         (fspace.getNumSamples()!=noSamples) ||    return m_data.get();
2465         (fspace.getNumDPPSample()!=noDPPSample)  }
2466       ) {  
2467      throw DataException("extractData Error: incompatible FunctionSpace");  
2468    }  std::string
2469    for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  Data::toString() const
2470      if (referenceNumbers[sampleNo] != fspace.getReferenceNoFromSampleNo(sampleNo)) {  {
2471        throw DataException("extractData Error: incompatible FunctionSpace");      if (!m_data->isEmpty() &&
2472      }      getNumDataPoints()*getDataPointSize()>escriptParams.getInt("TOO_MANY_LINES"))
2473    }      {
2474    if (dataType==2) {      stringstream temp;
2475      for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {      temp << "Summary: inf="<< inf() << " sup=" << sup() << " data points=" << getNumDataPoints();
2476        if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {      return  temp.str();
         throw DataException("extractData Error: incompatible FunctionSpace");  
       }  
2477      }      }
2478    }      return m_data->toString();
2479    }
2480    
   //  
   // Construct a DataVector to hold underlying data values  
   DataVector dataVec(dataLength);  
2481    
   //  
   // 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;  
   }  
2482    
2483    if (!archiveFile.good()) {  DataTypes::ValueType::const_reference
2484      throw DataException("extractData Error: problem reading from archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2485    }  {
2486        return m_data->getDataAtOffset(i);
2487    }
2488    
   //  
   // Close archive file  
   archiveFile.close();  
2489    
2490    if (!archiveFile.good()) {  DataTypes::ValueType::reference
2491      throw DataException("extractData Error: problem closing archive file");  Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2492    }  {
2493        return m_data->getDataAtOffset(i);
2494    }
2495    
2496    //  DataTypes::ValueType::const_reference
2497    // Construct an appropriate Data object  Data::getDataPoint(int sampleNo, int dataPointNo) const
2498    DataAbstract* tempData;  {
2499    switch (dataType) {      return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
     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;  
2500  }  }
2501    
2502  ostream& escript::operator<<(ostream& o, const Data& data)  
2503    DataTypes::ValueType::reference
2504    Data::getDataPoint(int sampleNo, int dataPointNo)
2505  {  {
2506    o << data.toString();      return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
   return o;  
2507  }  }
2508    
2509    
2510  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2511    
2512  void  void
2513  Data::print()  Data::print()
2514  {  {
2515    int i,j;    int i,j;
2516      
2517    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2518    for( i=0; i<getNumSamples(); i++ )    for( i=0; i<getNumSamples(); i++ )
2519    {    {
# Line 2557  Data::print() Line 2523  Data::print()
2523      printf( "\n" );      printf( "\n" );
2524    }    }
2525  }  }
2526    void
2527    Data::dump(const std::string fileName) const
2528    {
2529      try
2530         {
2531            return m_data->dump(fileName);
2532         }
2533         catch (exception& e)
2534         {
2535            cout << e.what() << endl;
2536         }
2537    }
2538    
2539  int  int
2540  Data::get_MPISize() const  Data::get_MPISize() const
2541  {  {
2542      int error, size;      int size;
2543  #ifdef PASO_MPI  #ifdef PASO_MPI
2544        int error;
2545      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
2546  #else  #else
2547      size = 1;      size = 1;
# Line 2573  Data::get_MPISize() const Line 2552  Data::get_MPISize() const
2552  int  int
2553  Data::get_MPIRank() const  Data::get_MPIRank() const
2554  {  {
2555      int error, rank;      int rank;
2556  #ifdef PASO_MPI  #ifdef PASO_MPI
2557        int error;
2558      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
2559  #else  #else
2560      rank = 0;      rank = 0;
# Line 2584  Data::get_MPIRank() const Line 2564  Data::get_MPIRank() const
2564    
2565  MPI_Comm  MPI_Comm
2566  Data::get_MPIComm() const  Data::get_MPIComm() const
2567  {  {
2568  #ifdef PASO_MPI  #ifdef PASO_MPI
2569      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
2570  #else  #else
# Line 2592  Data::get_MPIComm() const Line 2572  Data::get_MPIComm() const
2572  #endif  #endif
2573  }  }
2574    
2575    

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

  ViewVC Help
Powered by ViewVC 1.1.26