/[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 1092 by gross, Fri Apr 13 03:39:49 2007 UTC revision 1799 by jfenwick, Wed Sep 17 06:33:18 2008 UTC
# Line 1  Line 1 
 // $Id$  
1    
2  /*  /* $Id$ */
3   ************************************************************  
4   *          Copyright 2006 by ACcESS MNRF                   *  /*******************************************************
5   *                                                          *   *
6   *              http://www.access.edu.au                    *   *           Copyright 2003-2007 by ACceSS MNRF
7   *       Primary Business: Queensland, Australia            *   *       Copyright 2007 by University of Queensland
8   *  Licensed under the Open Software License version 3.0    *   *
9   *     http://www.opensource.org/licenses/osl-3.0.php       *   *                http://esscc.uq.edu.au
10   *                                                          *   *        Primary Business: Queensland, Australia
11   ************************************************************   *  Licensed under the Open Software License version 3.0
12  */   *     http://www.opensource.org/licenses/osl-3.0.php
13     *
14     *******************************************************/
15    
16  #include "Data.h"  #include "Data.h"
17    
18  #include "DataExpanded.h"  #include "DataExpanded.h"
19  #include "DataConstant.h"  #include "DataConstant.h"
20  #include "DataTagged.h"  #include "DataTagged.h"
21  #include "DataEmpty.h"  #include "DataEmpty.h"
 #include "DataArray.h"  
 #include "DataArrayView.h"  
 #include "DataProf.h"  
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
25    #include "FunctionSpaceException.h"
26    
27    extern "C" {
28    #include "escript/blocktimer.h"
29    }
30    
31  #include <fstream>  #include <fstream>
32  #include <algorithm>  #include <algorithm>
# Line 53  Data::Data(double value, Line 57  Data::Data(double value,
57             const FunctionSpace& what,             const FunctionSpace& what,
58             bool expanded)             bool expanded)
59  {  {
60    DataArrayView::ShapeType dataPointShape;    DataTypes::ShapeType dataPointShape;
61    for (int i = 0; i < shape.attr("__len__")(); ++i) {    for (int i = 0; i < shape.attr("__len__")(); ++i) {
62      dataPointShape.push_back(extract<const int>(shape[i]));      dataPointShape.push_back(extract<const int>(shape[i]));
63    }    }
64    DataArray temp(dataPointShape,value);  
65    initialise(temp.getView(),what,expanded);    int len = DataTypes::noValues(dataPointShape);
66      DataVector temp_data(len,value,len);
67    //   DataArrayView temp_dataView(temp_data, dataPointShape);
68    
69    //   initialise(temp_dataView, what, expanded);
70      initialise(temp_data, dataPointShape, what, expanded);
71    
72    m_protected=false;    m_protected=false;
73  }  }
74    
75  Data::Data(double value,  Data::Data(double value,
76         const DataArrayView::ShapeType& dataPointShape,         const DataTypes::ShapeType& dataPointShape,
77         const FunctionSpace& what,         const FunctionSpace& what,
78             bool expanded)             bool expanded)
79  {  {
80    DataArray temp(dataPointShape,value);    int len = DataTypes::noValues(dataPointShape);
81    pair<int,int> dataShape=what.getDataShape();  
82    initialise(temp.getView(),what,expanded);    DataVector temp_data(len,value,len);
83    //   DataArrayView temp_dataView(temp_data, dataPointShape);
84    
85    //   initialise(temp_dataView, what, expanded);
86      initialise(temp_data, dataPointShape, what, expanded);
87    
88    m_protected=false;    m_protected=false;
89  }  }
90    
# Line 79  Data::Data(const Data& inData) Line 94  Data::Data(const Data& inData)
94    m_protected=inData.isProtected();    m_protected=inData.isProtected();
95  }  }
96    
97    
98  Data::Data(const Data& inData,  Data::Data(const Data& inData,
99             const DataArrayView::RegionType& region)             const DataTypes::RegionType& region)
100  {  {
101    //    //
102    // Create Data which is a slice of another Data    // Create Data which is a slice of another Data
# Line 95  Data::Data(const Data& inData, Line 111  Data::Data(const Data& inData,
111  {  {
112    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
113      m_data=inData.m_data;      m_data=inData.m_data;
114      } else if (inData.isConstant()) { // for a constant function, we just need to use the new function space
115        if (!inData.probeInterpolation(functionspace))
116        {           // Even though this is constant, we still need to check whether interpolation is allowed
117        throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
118        }
119        DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),inData.m_data->getVector());  
120        m_data=shared_ptr<DataAbstract>(dc);
121    } else {    } else {
122      Data tmp(0,inData.getPointDataView().getShape(),functionspace,true);      Data tmp(0,inData.getDataPointShape(),functionspace,true);
123      // Note: Must use a reference or pointer to a derived object      // Note: Must use a reference or pointer to a derived object
124      // in order to get polymorphic behaviour. Shouldn't really      // in order to get polymorphic behaviour. Shouldn't really
125      // be able to create an instance of AbstractDomain but that was done      // be able to create an instance of AbstractDomain but that was done
# Line 112  Data::Data(const Data& inData, Line 135  Data::Data(const Data& inData,
135    m_protected=false;    m_protected=false;
136  }  }
137    
138  Data::Data(const DataTagged::TagListType& tagKeys,  // Data::Data(const DataTagged::TagListType& tagKeys,
139             const DataTagged::ValueListType & values,  //            const DataTagged::ValueListType & values,
140             const DataArrayView& defaultValue,  //            const DataArrayView& defaultValue,
141             const FunctionSpace& what,  //            const FunctionSpace& what,
142             bool expanded)  //            bool expanded)
143    // {
144    //   DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);
145    //   shared_ptr<DataAbstract> temp_data(temp);
146    //   m_data=temp_data;
147    //   m_protected=false;
148    //   if (expanded) {
149    //     expand();
150    //   }
151    // }
152    
153    
154    
155    Data::Data(DataAbstract* underlyingdata)
156  {  {
157    DataAbstract* temp=new DataTagged(tagKeys,values,defaultValue,what);      m_data=shared_ptr<DataAbstract>(underlyingdata);
158    shared_ptr<DataAbstract> temp_data(temp);      m_protected=false;
   m_data=temp_data;  
   m_protected=false;  
   if (expanded) {  
     expand();  
   }  
159  }  }
160    
161  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
# Line 134  Data::Data(const numeric::array& value, Line 165  Data::Data(const numeric::array& value,
165    initialise(value,what,expanded);    initialise(value,what,expanded);
166    m_protected=false;    m_protected=false;
167  }  }
168    /*
169  Data::Data(const DataArrayView& value,  Data::Data(const DataArrayView& value,
170         const FunctionSpace& what,         const FunctionSpace& what,
171             bool expanded)             bool expanded)
172  {  {
173    initialise(value,what,expanded);    initialise(value,what,expanded);
174    m_protected=false;    m_protected=false;
175    }*/
176    
177    Data::Data(const DataTypes::ValueType& value,
178             const DataTypes::ShapeType& shape,
179                     const FunctionSpace& what,
180                     bool expanded)
181    {
182       initialise(value,shape,what,expanded);
183       m_protected=false;
184  }  }
185    
186    
187  Data::Data(const object& value,  Data::Data(const object& value,
188         const FunctionSpace& what,         const FunctionSpace& what,
189             bool expanded)             bool expanded)
# Line 152  Data::Data(const object& value, Line 193  Data::Data(const object& value,
193    m_protected=false;    m_protected=false;
194  }  }
195    
196    
197  Data::Data(const object& value,  Data::Data(const object& value,
198             const Data& other)             const Data& other)
199  {  {
200      numeric::array asNumArray(value);
201    
202      // extract the shape of the numarray
203      DataTypes::ShapeType tempShape=DataTypes::shapeFromNumArray(asNumArray);
204    // /*  for (int i=0; i < asNumArray.getrank(); i++) {
205    //     tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
206    //   }*/
207    //   // get the space for the data vector
208    //   int len = DataTypes::noValues(tempShape);
209    //   DataVector temp_data(len, 0.0, len);
210    // /*  DataArrayView temp_dataView(temp_data, tempShape);
211    //   temp_dataView.copy(asNumArray);*/
212    //   temp_data.copyFromNumArray(asNumArray);
213    
214    //    //
215    // Create DataConstant using the given value and all other parameters    // Create DataConstant using the given value and all other parameters
216    // copied from other. If value is a rank 0 object this Data    // copied from other. If value is a rank 0 object this Data
217    // will assume the point data shape of other.    // will assume the point data shape of other.
218    DataArray temp(value);  
219    if (temp.getView().getRank()==0) {    if (DataTypes::getRank(tempShape)/*temp_dataView.getRank()*/==0) {
220      //  
221      // Create a DataArray with the scalar value for all elements  
222      DataArray temp2(other.getPointDataView().getShape(),temp.getView()());      // get the space for the data vector
223      initialise(temp2.getView(),other.getFunctionSpace(),false);      int len1 = DataTypes::noValues(tempShape);
224        DataVector temp_data(len1, 0.0, len1);
225        temp_data.copyFromNumArray(asNumArray);
226    
227        int len = DataTypes::noValues(other.getDataPointShape());
228    
229        DataVector temp2_data(len, temp_data[0]/*temp_dataView()*/, len);
230        //DataArrayView temp2_dataView(temp2_data, other.getPointDataView().getShape());
231    //     initialise(temp2_dataView, other.getFunctionSpace(), false);
232    
233        DataConstant* t=new DataConstant(other.getFunctionSpace(),other.getDataPointShape(),temp2_data);
234        boost::shared_ptr<DataAbstract> sp(t);
235        m_data=sp;
236    
237    
238    } else {    } else {
239      //      //
240      // Create a DataConstant with the same sample shape as other      // Create a DataConstant with the same sample shape as other
241      initialise(temp.getView(),other.getFunctionSpace(),false);  //     initialise(temp_dataView, other.getFunctionSpace(), false);
242        DataConstant* t=new DataConstant(asNumArray,other.getFunctionSpace());
243        boost::shared_ptr<DataAbstract> sp(t);
244        m_data=sp;
245    }    }
246    m_protected=false;    m_protected=false;
247  }  }
# Line 178  Data::~Data() Line 251  Data::~Data()
251    
252  }  }
253    
254    
255    
256    void
257    Data::initialise(const boost::python::numeric::array& value,
258                     const FunctionSpace& what,
259                     bool expanded)
260    {
261      //
262      // Construct a Data object of the appropriate type.
263      // Construct the object first as there seems to be a bug which causes
264      // undefined behaviour if an exception is thrown during construction
265      // within the shared_ptr constructor.
266      if (expanded) {
267        DataAbstract* temp=new DataExpanded(value, what);
268        boost::shared_ptr<DataAbstract> temp_data(temp);
269        m_data=temp_data;
270      } else {
271        DataAbstract* temp=new DataConstant(value, what);
272        boost::shared_ptr<DataAbstract> temp_data(temp);
273        m_data=temp_data;
274      }
275    }
276    
277    
278    void
279    Data::initialise(const DataTypes::ValueType& value,
280             const DataTypes::ShapeType& shape,
281                     const FunctionSpace& what,
282                     bool expanded)
283    {
284      //
285      // Construct a Data object of the appropriate type.
286      // Construct the object first as there seems to be a bug which causes
287      // undefined behaviour if an exception is thrown during construction
288      // within the shared_ptr constructor.
289      if (expanded) {
290        DataAbstract* temp=new DataExpanded(what, shape, value);
291        boost::shared_ptr<DataAbstract> temp_data(temp);
292        m_data=temp_data;
293      } else {
294        DataAbstract* temp=new DataConstant(what, shape, value);
295        boost::shared_ptr<DataAbstract> temp_data(temp);
296        m_data=temp_data;
297      }
298    }
299    
300    
301    // void
302    // Data::CompareDebug(const Data& rd)
303    // {
304    //  using namespace std;
305    //  bool mismatch=false;
306    //  std::cout << "Comparing left and right" << endl;
307    //  const DataTagged* left=dynamic_cast<DataTagged*>(m_data.get());
308    //  const DataTagged* right=dynamic_cast<DataTagged*>(rd.m_data.get());
309    //  
310    //  if (left==0)
311    //  {
312    //      cout << "left arg is not a DataTagged\n";
313    //      return;
314    //  }
315    //  
316    //  if (right==0)
317    //  {
318    //      cout << "right arg is not a DataTagged\n";
319    //      return;
320    //  }
321    //  cout << "Num elements=" << left->getVector().size() << ":" << right->getVector().size() << std::endl;
322    //  cout << "Shapes ";
323    //  if (left->getShape()==right->getShape())
324    //  {
325    //      cout << "ok\n";
326    //  }
327    //  else
328    //  {
329    //      cout << "Problem: shapes do not match\n";
330    //      mismatch=true;
331    //  }
332    //  int lim=left->getVector().size();
333    //  if (right->getVector().size()) lim=right->getVector().size();
334    //  for (int i=0;i<lim;++i)
335    //  {
336    //      if (left->getVector()[i]!=right->getVector()[i])
337    //      {
338    //          cout << "[" << i << "] value mismatch " << left->getVector()[i] << ":" << right->getVector()[i] << endl;
339    //          mismatch=true;
340    //      }
341    //  }
342    //
343    //  // still need to check the tag map
344    //  // also need to watch what is happening to function spaces, are they copied or what?
345    //
346    //  const DataTagged::DataMapType& mapleft=left->getTagLookup();
347    //  const DataTagged::DataMapType& mapright=right->getTagLookup();
348    //
349    //  if (mapleft.size()!=mapright.size())
350    //  {
351    //      cout << "Maps are different sizes " << mapleft.size() << ":" << mapright.size() << endl;
352    //      mismatch=true;
353    //      cout << "Left map\n";
354    //      DataTagged::DataMapType::const_iterator i,j;
355    //      for (i=mapleft.begin();i!=mapleft.end();++i) {
356    //          cout << "(" << i->first << "=>" << i->second << ")\n";
357    //      }
358    //      cout << "Right map\n";
359    //      for (i=mapright.begin();i!=mapright.end();++i) {
360    //          cout << "(" << i->first << "=>" << i->second << ")\n";
361    //      }
362    //      cout << "End map\n";
363    //
364    //  }
365    //
366    //  DataTagged::DataMapType::const_iterator i,j;
367    //  for (i=mapleft.begin(),j=mapright.begin();i!=mapleft.end() && j!=mapright.end();++i,++j) {
368    //     if ((i->first!=j->first) || (i->second!=j->second))
369    //     {
370    //      cout << "(" << i->first << "=>" << i->second << ")";
371    //      cout << ":(" << j->first << "=>" << j->second << ") ";
372    //      mismatch=true;
373    //            }
374    //  }
375    //  if (mismatch)
376    //  {
377    //      cout << "#Mismatch\n";
378    //  }
379    // }
380    
381  escriptDataC  escriptDataC
382  Data::getDataC()  Data::getDataC()
383  {  {
# Line 197  Data::getDataC() const Line 397  Data::getDataC() const
397  const boost::python::tuple  const boost::python::tuple
398  Data::getShapeTuple() const  Data::getShapeTuple() const
399  {  {
400    const DataArrayView::ShapeType& shape=getDataPointShape();    const DataTypes::ShapeType& shape=getDataPointShape();
401    switch(getDataPointRank()) {    switch(getDataPointRank()) {
402       case 0:       case 0:
403          return make_tuple();          return make_tuple();
# Line 214  Data::getShapeTuple() const Line 414  Data::getShapeTuple() const
414    }    }
415  }  }
416    
417    
418    // The different name is needed because boost has trouble with overloaded functions.
419    // It can't work out what type the function is based soley on its name.
420    // There are ways to fix this involving creating function pointer variables for each form
421    // but there doesn't seem to be a need given that the methods have the same name from the python point of view
422    Data*
423    Data::copySelf()
424    {
425       DataAbstract* temp=m_data->deepCopy();
426       return new Data(temp);
427    }
428    
429  void  void
430  Data::copy(const Data& other)  Data::copy(const Data& other)
431  {  {
432    //    DataAbstract* temp=other.m_data->deepCopy();
433    // Perform a deep copy    shared_ptr<DataAbstract> temp_data(temp);
434    {    m_data=temp_data;
435      DataExpanded* temp=dynamic_cast<DataExpanded*>(other.m_data.get());  }
436      if (temp!=0) {  
437        //  
438        // Construct a DataExpanded copy  void
439        DataAbstract* newData=new DataExpanded(*temp);  Data::setToZero()
440        shared_ptr<DataAbstract> temp_data(newData);  {
       m_data=temp_data;  
       return;  
     }  
   }  
441    {    {
442      DataTagged* temp=dynamic_cast<DataTagged*>(other.m_data.get());      DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());
443      if (temp!=0) {      if (temp!=0) {
444        //         temp->setToZero();
445        // Construct a DataTagged copy         return;
       DataAbstract* newData=new DataTagged(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
       return;  
446      }      }
447    }    }
448    {    {
449      DataConstant* temp=dynamic_cast<DataConstant*>(other.m_data.get());      DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());
450      if (temp!=0) {      if (temp!=0) {
451        //        temp->setToZero();
       // Construct a DataConstant copy  
       DataAbstract* newData=new DataConstant(*temp);  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
452        return;        return;
453      }      }
454    }    }
455    {    {
456      DataEmpty* temp=dynamic_cast<DataEmpty*>(other.m_data.get());      DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());
457      if (temp!=0) {      if (temp!=0) {
458        //        temp->setToZero();
       // Construct a DataEmpty copy  
       DataAbstract* newData=new DataEmpty();  
       shared_ptr<DataAbstract> temp_data(newData);  
       m_data=temp_data;  
459        return;        return;
460      }      }
461    }    }
462    throw DataException("Error - Copy not implemented for this Data type.");    throw DataException("Error - Data can not be set to zero.");
463  }  }
464    
465  void  void
# Line 312  Data::isConstant() const Line 508  Data::isConstant() const
508  }  }
509    
510  void  void
511  Data::setProtection()  Data::setProtection()
512  {  {
513     m_protected=true;     m_protected=true;
514  }  }
515    
516  bool  bool
517  Data::isProtected() const  Data::isProtected() const
518  {  {
519     return m_protected;     return m_protected;
520  }  }
521    
# Line 370  Data::tag() Line 566  Data::tag()
566  Data  Data
567  Data::oneOver() const  Data::oneOver() const
568  {  {
569    return escript::unaryOp(*this,bind1st(divides<double>(),1.));    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
570  }  }
571    
572  Data  Data
573  Data::wherePositive() const  Data::wherePositive() const
574  {  {
575    return escript::unaryOp(*this,bind2nd(greater<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
576  }  }
577    
578  Data  Data
579  Data::whereNegative() const  Data::whereNegative() const
580  {  {
581    return escript::unaryOp(*this,bind2nd(less<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
582  }  }
583    
584  Data  Data
585  Data::whereNonNegative() const  Data::whereNonNegative() const
586  {  {
587    return escript::unaryOp(*this,bind2nd(greater_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
588  }  }
589    
590  Data  Data
591  Data::whereNonPositive() const  Data::whereNonPositive() const
592  {  {
593    return escript::unaryOp(*this,bind2nd(less_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
594  }  }
595    
596  Data  Data
597  Data::whereZero(double tol) const  Data::whereZero(double tol) const
598  {  {
599    Data dataAbs=abs();    Data dataAbs=abs();
600    return escript::unaryOp(dataAbs,bind2nd(less_equal<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(less_equal<double>(),tol));
601  }  }
602    
603  Data  Data
604  Data::whereNonZero(double tol) const  Data::whereNonZero(double tol) const
605  {  {
606    Data dataAbs=abs();    Data dataAbs=abs();
607    return escript::unaryOp(dataAbs,bind2nd(greater<double>(),tol));    return C_TensorUnaryOperation(dataAbs, bind2nd(greater<double>(),tol));
608  }  }
609    
610  Data  Data
# Line 435  Data::probeInterpolation(const FunctionS Line 631  Data::probeInterpolation(const FunctionS
631  Data  Data
632  Data::gradOn(const FunctionSpace& functionspace) const  Data::gradOn(const FunctionSpace& functionspace) const
633  {  {
634      double blocktimer_start = blocktimer_time();
635    if (functionspace.getDomain()!=getDomain())    if (functionspace.getDomain()!=getDomain())
636      throw DataException("Error - gradient cannot be calculated on different domains.");      throw DataException("Error - gradient cannot be calculated on different domains.");
637    DataArrayView::ShapeType grad_shape=getPointDataView().getShape();    DataTypes::ShapeType grad_shape=getDataPointShape();
638    grad_shape.push_back(functionspace.getDim());    grad_shape.push_back(functionspace.getDim());
639    Data out(0.0,grad_shape,functionspace,true);    Data out(0.0,grad_shape,functionspace,true);
640    getDomain().setToGradient(out,*this);    getDomain().setToGradient(out,*this);
641      blocktimer_increment("grad()", blocktimer_start);
642    return out;    return out;
643  }  }
644    
# Line 453  Data::grad() const Line 651  Data::grad() const
651  int  int
652  Data::getDataPointSize() const  Data::getDataPointSize() const
653  {  {
654    return getPointDataView().noValues();    return m_data->getNoValues();
655  }  }
656    
657  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
658  Data::getLength() const  Data::getLength() const
659  {  {
660    return m_data->getLength();    return m_data->getLength();
661  }  }
662    
663  const DataArrayView::ShapeType&  // const DataTypes::ShapeType&
664  Data::getDataPointShape() const  // Data::getDataPointShape() const
665  {  // {
666    return getPointDataView().getShape();  //   return getPointDataView().getShape();
667  }  // }
668    
669    
670    
671  const  
672    const
673  boost::python::numeric::array  boost::python::numeric::array
674  Data:: getValueOfDataPoint(int dataPointNo)  Data:: getValueOfDataPoint(int dataPointNo)
675  {  {
676    size_t length=0;    size_t length=0;
677    int i, j, k, l;    int i, j, k, l;
678    //    //
679    // determine the rank and shape of each data point    // determine the rank and shape of each data point
680    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
681    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
682    
683    //    //
684    // create the numeric array to be returned    // create the numeric array to be returned
# Line 489  Data:: getValueOfDataPoint(int dataPoint Line 688  Data:: getValueOfDataPoint(int dataPoint
688    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
689    // as that of the data point    // as that of the data point
690    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
691    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
692    
693    //    //
694    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 517  Data:: getValueOfDataPoint(int dataPoint Line 716  Data:: getValueOfDataPoint(int dataPoint
716         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {         if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
717             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");             throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
718         }         }
719                  
720         //         //
721         // Check a valid data point number has been supplied         // Check a valid data point number has been supplied
722         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {         if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
# Line 525  Data:: getValueOfDataPoint(int dataPoint Line 724  Data:: getValueOfDataPoint(int dataPoint
724         }         }
725         // TODO: global error handling         // TODO: global error handling
726         // create a view of the data if it is stored locally         // create a view of the data if it is stored locally
727         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);  //       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
728                   DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
729    
730    
731         switch( dataPointRank ){         switch( dataPointRank ){
732              case 0 :              case 0 :
733                  numArray[0] = dataPointView();                  numArray[0] = getDataAtOffset(offset);
734                  break;                  break;
735              case 1 :                      case 1 :
736                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
737                      numArray[i]=dataPointView(i);                      numArray[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
738                  break;                  break;
739              case 2 :                      case 2 :
740                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
741                      for( j=0; j<dataPointShape[1]; j++)                      for( j=0; j<dataPointShape[1]; j++)
742                          numArray[make_tuple(i,j)]=dataPointView(i,j);                          numArray[make_tuple(i,j)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
743                  break;                  break;
744              case 3 :                      case 3 :
745                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
746                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
747                          for( k=0; k<dataPointShape[2]; k++)                          for( k=0; k<dataPointShape[2]; k++)
748                              numArray[make_tuple(i,j,k)]=dataPointView(i,j,k);                              numArray[make_tuple(i,j,k)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
749                  break;                  break;
750              case 4 :              case 4 :
751                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
752                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
753                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
754                              for( l=0; l<dataPointShape[3]; l++)                              for( l=0; l<dataPointShape[3]; l++)
755                                  numArray[make_tuple(i,j,k,l)]=dataPointView(i,j,k,l);                                  numArray[make_tuple(i,j,k,l)]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
756                  break;                  break;
757      }      }
758    }    }
# Line 560  Data:: getValueOfDataPoint(int dataPoint Line 761  Data:: getValueOfDataPoint(int dataPoint
761    return numArray;    return numArray;
762    
763  }  }
764    
765  void  void
766  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
767  {  {
768      // this will throw if the value cannot be represented      // this will throw if the value cannot be represented
769      boost::python::numeric::array num_array(py_object);      boost::python::numeric::array num_array(py_object);
# Line 578  Data::setValueOfDataPointToArray(int dat Line 780  Data::setValueOfDataPointToArray(int dat
780    }    }
781    //    //
782    // check rank    // check rank
783    if (num_array.getrank()<getDataPointRank())    if (num_array.getrank()<getDataPointRank())
784        throw DataException("Rank of numarray does not match Data object rank");        throw DataException("Rank of numarray does not match Data object rank");
785    
786    //    //
# Line 621  Data::setValueOfDataPoint(int dataPointN Line 823  Data::setValueOfDataPoint(int dataPointN
823    }    }
824  }  }
825    
826  const  const
827  boost::python::numeric::array  boost::python::numeric::array
828  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)  Data::getValueOfGlobalDataPoint(int procNo, int dataPointNo)
829  {  {
830    size_t length=0;    size_t length=0;
831    int i, j, k, l, pos;    int i, j, k, l, pos;
832    //    //
833    // determine the rank and shape of each data point    // determine the rank and shape of each data point
834    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
835    DataArrayView::ShapeType dataPointShape = getDataPointShape();    const DataTypes::ShapeType& dataPointShape = getDataPointShape();
836    
837    //    //
838    // create the numeric array to be returned    // create the numeric array to be returned
# Line 640  Data::getValueOfGlobalDataPoint(int proc Line 842  Data::getValueOfGlobalDataPoint(int proc
842    // the shape of the returned numeric array will be the same    // the shape of the returned numeric array will be the same
843    // as that of the data point    // as that of the data point
844    int arrayRank = dataPointRank;    int arrayRank = dataPointRank;
845    DataArrayView::ShapeType arrayShape = dataPointShape;    const DataTypes::ShapeType& arrayShape = dataPointShape;
846    
847    //    //
848    // resize the numeric array to the shape just calculated    // resize the numeric array to the shape just calculated
# Line 678  Data::getValueOfGlobalDataPoint(int proc Line 880  Data::getValueOfGlobalDataPoint(int proc
880                  if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {                  if ((sampleNo >= getNumSamples()) || (sampleNo < 0 )) {
881                    throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");                    throw DataException("Error - Data::convertToNumArray: invalid sampleNo.");
882                  }                  }
883                  
884                  //                  //
885                  // Check a valid data point number has been supplied                  // Check a valid data point number has been supplied
886                  if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {                  if ((dataPointNoInSample >= getNumDataPointsPerSample()) || (dataPointNoInSample < 0)) {
# Line 686  Data::getValueOfGlobalDataPoint(int proc Line 888  Data::getValueOfGlobalDataPoint(int proc
888                  }                  }
889                  // TODO: global error handling                  // TODO: global error handling
890          // create a view of the data if it is stored locally          // create a view of the data if it is stored locally
891          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);          //DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNoInSample);
892                    DataTypes::ValueType::size_type offset=getDataOffset(sampleNo, dataPointNoInSample);
893    
894          // pack the data from the view into tmpData for MPI communication          // pack the data from the view into tmpData for MPI communication
895          pos=0;          pos=0;
896          switch( dataPointRank ){          switch( dataPointRank ){
897              case 0 :              case 0 :
898                  tmpData[0] = dataPointView();                  tmpData[0] = getDataAtOffset(offset);
899                  break;                  break;
900              case 1 :                      case 1 :
901                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
902                      tmpData[i]=dataPointView(i);                      tmpData[i]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i));
903                  break;                  break;
904              case 2 :                      case 2 :
905                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
906                      for( j=0; j<dataPointShape[1]; j++, pos++ )                      for( j=0; j<dataPointShape[1]; j++, pos++ )
907                          tmpData[pos]=dataPointView(i,j);                          tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j));
908                  break;                  break;
909              case 3 :                      case 3 :
910                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
911                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
912                          for( k=0; k<dataPointShape[2]; k++, pos++ )                          for( k=0; k<dataPointShape[2]; k++, pos++ )
913                              tmpData[pos]=dataPointView(i,j,k);                              tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k));
914                  break;                  break;
915              case 4 :              case 4 :
916                  for( i=0; i<dataPointShape[0]; i++ )                  for( i=0; i<dataPointShape[0]; i++ )
917                      for( j=0; j<dataPointShape[1]; j++ )                      for( j=0; j<dataPointShape[1]; j++ )
918                          for( k=0; k<dataPointShape[2]; k++ )                          for( k=0; k<dataPointShape[2]; k++ )
919                              for( l=0; l<dataPointShape[3]; l++, pos++ )                              for( l=0; l<dataPointShape[3]; l++, pos++ )
920                                  tmpData[pos]=dataPointView(i,j,k,l);                                  tmpData[pos]=getDataAtOffset(offset+DataTypes::getRelIndex(dataPointShape, i,j,k,l));
921                  break;                  break;
922          }          }
923              }              }
924      }      }
925          #ifdef PASO_MPI          #ifdef PASO_MPI
926          // broadcast the data to all other processes          // broadcast the data to all other processes
927      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );      MPI_Bcast( tmpData, length, MPI_DOUBLE, procNo, get_MPIComm() );
928          #endif          #endif
# Line 729  Data::getValueOfGlobalDataPoint(int proc Line 932  Data::getValueOfGlobalDataPoint(int proc
932          case 0 :          case 0 :
933              numArray[0]=tmpData[0];              numArray[0]=tmpData[0];
934              break;              break;
935          case 1 :                  case 1 :
936              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
937                  numArray[i]=tmpData[i];                  numArray[i]=tmpData[i];
938              break;              break;
939          case 2 :                  case 2 :
940              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
941                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
942                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];                     numArray[make_tuple(i,j)]=tmpData[i+j*dataPointShape[0]];
943              break;              break;
944          case 3 :                  case 3 :
945              for( i=0; i<dataPointShape[0]; i++ )              for( i=0; i<dataPointShape[0]; i++ )
946                  for( j=0; j<dataPointShape[1]; j++ )                  for( j=0; j<dataPointShape[1]; j++ )
947                      for( k=0; k<dataPointShape[2]; k++ )                      for( k=0; k<dataPointShape[2]; k++ )
# Line 753  Data::getValueOfGlobalDataPoint(int proc Line 956  Data::getValueOfGlobalDataPoint(int proc
956              break;              break;
957      }      }
958    
959      delete [] tmpData;        delete [] tmpData;
960    //    //
961    // return the loaded array    // return the loaded array
962    return numArray;    return numArray;
# Line 766  Data::integrate() const Line 969  Data::integrate() const
969  {  {
970    int index;    int index;
971    int rank = getDataPointRank();    int rank = getDataPointRank();
972    DataArrayView::ShapeType shape = getDataPointShape();    DataTypes::ShapeType shape = getDataPointShape();
973      int dataPointSize = getDataPointSize();
974    
975    //    //
976    // calculate the integral values    // calculate the integral values
977    vector<double> integrals(getDataPointSize());    vector<double> integrals(dataPointSize);
978      vector<double> integrals_local(dataPointSize);
979    #ifdef PASO_MPI
980      AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals_local,*this);
981      // Global sum: use an array instead of a vector because elements of array are guaranteed to be contiguous in memory
982      double *tmp = new double[dataPointSize];
983      double *tmp_local = new double[dataPointSize];
984      for (int i=0; i<dataPointSize; i++) { tmp_local[i] = integrals_local[i]; }
985      MPI_Allreduce( &tmp_local[0], &tmp[0], dataPointSize, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD );
986      for (int i=0; i<dataPointSize; i++) { integrals[i] = tmp[i]; }
987      delete[] tmp;
988      delete[] tmp_local;
989    #else
990    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);    AbstractContinuousDomain::asAbstractContinuousDomain(getDomain()).setToIntegrals(integrals,*this);
991    #endif
992    
993    //    //
994    // create the numeric array to be returned    // create the numeric array to be returned
# Line 832  Data::integrate() const Line 1048  Data::integrate() const
1048  Data  Data
1049  Data::sin() const  Data::sin() const
1050  {  {
1051    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1052  }  }
1053    
1054  Data  Data
1055  Data::cos() const  Data::cos() const
1056  {  {
1057    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1058  }  }
1059    
1060  Data  Data
1061  Data::tan() const  Data::tan() const
1062  {  {
1063    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1064  }  }
1065    
1066  Data  Data
1067  Data::asin() const  Data::asin() const
1068  {  {
1069    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1070  }  }
1071    
1072  Data  Data
1073  Data::acos() const  Data::acos() const
1074  {  {
1075    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1076  }  }
1077    
1078    
1079  Data  Data
1080  Data::atan() const  Data::atan() const
1081  {  {
1082    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1083  }  }
1084    
1085  Data  Data
1086  Data::sinh() const  Data::sinh() const
1087  {  {
1088    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sinh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1089    
1090  }  }
1091    
1092  Data  Data
1093  Data::cosh() const  Data::cosh() const
1094  {  {
1095    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::cosh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1096  }  }
1097    
1098  Data  Data
1099  Data::tanh() const  Data::tanh() const
1100  {  {
1101    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::tanh);      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1102  }  }
1103    
1104    
# Line 891  Data::erf() const Line 1108  Data::erf() const
1108  #ifdef _WIN32  #ifdef _WIN32
1109    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1110  #else  #else
1111    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::erf);    return C_TensorUnaryOperation(*this, ::erf);
1112  #endif  #endif
1113  }  }
1114    
# Line 899  Data Line 1116  Data
1116  Data::asinh() const  Data::asinh() const
1117  {  {
1118  #ifdef _WIN32  #ifdef _WIN32
1119    return escript::unaryOp(*this,escript::asinh_substitute);    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1120  #else  #else
1121    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::asinh);    return C_TensorUnaryOperation(*this, ::asinh);
1122  #endif  #endif
1123  }  }
1124    
# Line 909  Data Line 1126  Data
1126  Data::acosh() const  Data::acosh() const
1127  {  {
1128  #ifdef _WIN32  #ifdef _WIN32
1129    return escript::unaryOp(*this,escript::acosh_substitute);    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1130  #else  #else
1131    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::acosh);    return C_TensorUnaryOperation(*this, ::acosh);
1132  #endif  #endif
1133  }  }
1134    
# Line 919  Data Line 1136  Data
1136  Data::atanh() const  Data::atanh() const
1137  {  {
1138  #ifdef _WIN32  #ifdef _WIN32
1139    return escript::unaryOp(*this,escript::atanh_substitute);    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1140  #else  #else
1141    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::atanh);    return C_TensorUnaryOperation(*this, ::atanh);
1142  #endif  #endif
1143  }  }
1144    
1145  Data  Data
1146  Data::log10() const  Data::log10() const
1147  {  {
1148    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log10);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1149  }  }
1150    
1151  Data  Data
1152  Data::log() const  Data::log() const
1153  {  {
1154    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1155  }  }
1156    
1157  Data  Data
1158  Data::sign() const  Data::sign() const
1159  {  {
1160    return escript::unaryOp(*this,escript::fsign);    return C_TensorUnaryOperation(*this, escript::fsign);
1161  }  }
1162    
1163  Data  Data
1164  Data::abs() const  Data::abs() const
1165  {  {
1166    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1167  }  }
1168    
1169  Data  Data
1170  Data::neg() const  Data::neg() const
1171  {  {
1172    return escript::unaryOp(*this,negate<double>());    return C_TensorUnaryOperation(*this, negate<double>());
1173  }  }
1174    
1175  Data  Data
# Line 967  Data::pos() const Line 1184  Data::pos() const
1184  Data  Data
1185  Data::exp() const  Data::exp() const
1186  {  {
1187    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1188  }  }
1189    
1190  Data  Data
1191  Data::sqrt() const  Data::sqrt() const
1192  {  {
1193    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1194  }  }
1195    
1196  double  double
1197  Data::Lsup() const  Data::Lsup() const
1198  {  {
1199    double localValue, globalValue;    double localValue;
1200    //    //
1201    // set the initial absolute maximum value to zero    // set the initial absolute maximum value to zero
1202    
1203    AbsMax abs_max_func;    AbsMax abs_max_func;
1204    localValue = algorithm(abs_max_func,0);    localValue = algorithm(abs_max_func,0);
1205  #ifdef PASO_MPI  #ifdef PASO_MPI
1206      double globalValue;
1207    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1208    return globalValue;    return globalValue;
1209  #else  #else
# Line 994  Data::Lsup() const Line 1212  Data::Lsup() const
1212  }  }
1213    
1214  double  double
 Data::Linf() const  
 {  
   double localValue, globalValue;  
   //  
   // 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  
1215  Data::sup() const  Data::sup() const
1216  {  {
1217    double localValue, globalValue;    double localValue;
1218    //    //
1219    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1220    FMax fmax_func;    FMax fmax_func;
1221    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);    localValue = algorithm(fmax_func,numeric_limits<double>::max()*-1);
1222  #ifdef PASO_MPI  #ifdef PASO_MPI
1223      double globalValue;
1224    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD );
1225    return globalValue;    return globalValue;
1226  #else  #else
# Line 1029  Data::sup() const Line 1231  Data::sup() const
1231  double  double
1232  Data::inf() const  Data::inf() const
1233  {  {
1234    double localValue, globalValue;    double localValue;
1235    //    //
1236    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1237    FMin fmin_func;    FMin fmin_func;
1238    localValue = algorithm(fmin_func,numeric_limits<double>::max());    localValue = algorithm(fmin_func,numeric_limits<double>::max());
1239  #ifdef PASO_MPI  #ifdef PASO_MPI
1240      double globalValue;
1241    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );    MPI_Allreduce( &localValue, &globalValue, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD );
1242    return globalValue;    return globalValue;
1243  #else  #else
# Line 1066  Data Line 1269  Data
1269  Data::swapaxes(const int axis0, const int axis1) const  Data::swapaxes(const int axis0, const int axis1) const
1270  {  {
1271       int axis0_tmp,axis1_tmp;       int axis0_tmp,axis1_tmp;
1272       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1273       DataArrayView::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1274       // 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]
1275       // 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)
1276       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1092  Data::swapaxes(const int axis0, const in Line 1295  Data::swapaxes(const int axis0, const in
1295       }       }
1296       for (int i=0; i<rank; i++) {       for (int i=0; i<rank; i++) {
1297         if (i == axis0_tmp) {         if (i == axis0_tmp) {
1298            ev_shape.push_back(s[axis1_tmp]);            ev_shape.push_back(s[axis1_tmp]);
1299         } else if (i == axis1_tmp) {         } else if (i == axis1_tmp) {
1300            ev_shape.push_back(s[axis0_tmp]);            ev_shape.push_back(s[axis0_tmp]);
1301         } else {         } else {
1302            ev_shape.push_back(s[i]);            ev_shape.push_back(s[i]);
1303         }         }
1304       }       }
1305       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
# Line 1110  Data Line 1313  Data
1313  Data::symmetric() const  Data::symmetric() const
1314  {  {
1315       // check input       // check input
1316       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1317       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1318          if(s[0] != s[1])          if(s[0] != s[1])
1319             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.");
1320       }       }
1321       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
# Line 1132  Data Line 1335  Data
1335  Data::nonsymmetric() const  Data::nonsymmetric() const
1336  {  {
1337       // check input       // check input
1338       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1339       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1340          if(s[0] != s[1])          if(s[0] != s[1])
1341             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.");
1342          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1343          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1344          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1345          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
# Line 1147  Data::nonsymmetric() const Line 1350  Data::nonsymmetric() const
1350       else if (getDataPointRank()==4) {       else if (getDataPointRank()==4) {
1351          if(!(s[0] == s[2] && s[1] == s[3]))          if(!(s[0] == s[2] && s[1] == s[3]))
1352             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.");
1353          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1354          ev_shape.push_back(s[0]);          ev_shape.push_back(s[0]);
1355          ev_shape.push_back(s[1]);          ev_shape.push_back(s[1]);
1356          ev_shape.push_back(s[2]);          ev_shape.push_back(s[2]);
# Line 1165  Data::nonsymmetric() const Line 1368  Data::nonsymmetric() const
1368  Data  Data
1369  Data::trace(int axis_offset) const  Data::trace(int axis_offset) const
1370  {  {
1371       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1372       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1373          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1374          Data ev(0.,ev_shape,getFunctionSpace());          Data ev(0.,ev_shape,getFunctionSpace());
1375          ev.typeMatchRight(*this);          ev.typeMatchRight(*this);
1376          m_data->trace(ev.m_data.get(), axis_offset);          m_data->trace(ev.m_data.get(), axis_offset);
1377          return ev;          return ev;
1378       }       }
1379       if (getDataPointRank()==3) {       if (getDataPointRank()==3) {
1380          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1381          if (axis_offset==0) {          if (axis_offset==0) {
1382            int s2=s[2];            int s2=s[2];
1383            ev_shape.push_back(s2);            ev_shape.push_back(s2);
# Line 1189  Data::trace(int axis_offset) const Line 1392  Data::trace(int axis_offset) const
1392          return ev;          return ev;
1393       }       }
1394       if (getDataPointRank()==4) {       if (getDataPointRank()==4) {
1395          DataArrayView::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
1396          if (axis_offset==0) {          if (axis_offset==0) {
1397            ev_shape.push_back(s[2]);            ev_shape.push_back(s[2]);
1398            ev_shape.push_back(s[3]);            ev_shape.push_back(s[3]);
# Line 1215  Data::trace(int axis_offset) const Line 1418  Data::trace(int axis_offset) const
1418  Data  Data
1419  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1420  {  {
1421       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1422       DataArrayView::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1423       // 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]
1424       // 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)
1425       int rank=getDataPointRank();       int rank=getDataPointRank();
# Line 1237  Data Line 1440  Data
1440  Data::eigenvalues() const  Data::eigenvalues() const
1441  {  {
1442       // check input       // check input
1443       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1444       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1445          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.");
1446       if(s[0] != s[1])       if(s[0] != s[1])
1447          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.");
1448       // create return       // create return
1449       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1450       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1451       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1452       m_data->eigenvalues(ev.m_data.get());       m_data->eigenvalues(ev.m_data.get());
# Line 1253  Data::eigenvalues() const Line 1456  Data::eigenvalues() const
1456  const boost::python::tuple  const boost::python::tuple
1457  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1458  {  {
1459       DataArrayView::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1460       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1461          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.");
1462       if(s[0] != s[1])       if(s[0] != s[1])
1463          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.");
1464       // create return       // create return
1465       DataArrayView::ShapeType ev_shape(1,s[0]);       DataTypes::ShapeType ev_shape(1,s[0]);
1466       Data ev(0.,ev_shape,getFunctionSpace());       Data ev(0.,ev_shape,getFunctionSpace());
1467       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1468       DataArrayView::ShapeType V_shape(2,s[0]);       DataTypes::ShapeType V_shape(2,s[0]);
1469       Data V(0.,V_shape,getFunctionSpace());       Data V(0.,V_shape,getFunctionSpace());
1470       V.typeMatchRight(*this);       V.typeMatchRight(*this);
1471       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 1304  Data::calc_minGlobalDataPoint(int& ProcN Line 1507  Data::calc_minGlobalDataPoint(int& ProcN
1507      #pragma omp for private(i,j) schedule(static)      #pragma omp for private(i,j) schedule(static)
1508      for (i=0; i<numSamples; i++) {      for (i=0; i<numSamples; i++) {
1509        for (j=0; j<numDPPSample; j++) {        for (j=0; j<numDPPSample; j++) {
1510          next=temp.getDataPoint(i,j)();          next=temp.getDataAtOffset(temp.getDataOffset(i,j));
1511          if (next<local_min) {          if (next<local_min) {
1512            local_min=next;            local_min=next;
1513            local_lowi=i;            local_lowi=i;
# Line 1322  Data::calc_minGlobalDataPoint(int& ProcN Line 1525  Data::calc_minGlobalDataPoint(int& ProcN
1525    
1526  #ifdef PASO_MPI  #ifdef PASO_MPI
1527      // determine the processor on which the minimum occurs      // determine the processor on which the minimum occurs
1528      next = temp.getDataPoint(lowi,lowj)();      next = temp.getDataPoint(lowi,lowj);
1529      int lowProc = 0;      int lowProc = 0;
1530      double *globalMins = new double[get_MPISize()+1];      double *globalMins = new double[get_MPISize()+1];
1531      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() );
1532        
1533      if( get_MPIRank()==0 ){      if( get_MPIRank()==0 ){
1534          next = globalMins[lowProc];          next = globalMins[lowProc];
1535          for( i=1; i<get_MPISize(); i++ )          for( i=1; i<get_MPISize(); i++ )
# Line 1380  Data::operator+=(const boost::python::ob Line 1583  Data::operator+=(const boost::python::ob
1583    binaryOp(tmp,plus<double>());    binaryOp(tmp,plus<double>());
1584    return (*this);    return (*this);
1585  }  }
1586    Data&
1587    Data::operator=(const Data& other)
1588    {
1589      copy(other);
1590      return (*this);
1591    }
1592    
1593  Data&  Data&
1594  Data::operator-=(const Data& right)  Data::operator-=(const Data& right)
# Line 1452  Data::powO(const boost::python::object& Line 1661  Data::powO(const boost::python::object&
1661  Data  Data
1662  Data::powD(const Data& right) const  Data::powD(const Data& right) const
1663  {  {
1664    Data result;    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
   if (getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result.binaryOp(*this,escript::rpow);  
   } else {  
      result.copy(*this);  
      result.binaryOp(right,(Data::BinaryDFunPtr)::pow);  
   }  
   return result;  
1665  }  }
1666    
   
1667  //  //
1668  // NOTE: It is essential to specify the namespace this operator belongs to  // NOTE: It is essential to specify the namespace this operator belongs to
1669  Data  Data
1670  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
1671  {  {
1672    Data result;    return C_TensorBinaryOperation(left, right, plus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result+=left;  
   } else {  
      result.copy(left);  
      result+=right;  
   }  
   return result;  
1673  }  }
1674    
1675  //  //
# Line 1487  escript::operator+(const Data& left, con Line 1677  escript::operator+(const Data& left, con
1677  Data  Data
1678  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
1679  {  {
1680    Data result;    return C_TensorBinaryOperation(left, right, minus<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.neg();  
      result+=left;  
   } else {  
      result.copy(left);  
      result-=right;  
   }  
   return result;  
1681  }  }
1682    
1683  //  //
# Line 1505  escript::operator-(const Data& left, con Line 1685  escript::operator-(const Data& left, con
1685  Data  Data
1686  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
1687  {  {
1688    Data result;    return C_TensorBinaryOperation(left, right, multiplies<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result.copy(right);  
      result*=left;  
   } else {  
      result.copy(left);  
      result*=right;  
   }  
   return result;  
1689  }  }
1690    
1691  //  //
# Line 1523  escript::operator*(const Data& left, con Line 1693  escript::operator*(const Data& left, con
1693  Data  Data
1694  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
1695  {  {
1696    Data result;    return C_TensorBinaryOperation(left, right, divides<double>());
   //  
   // perform a deep copy  
   if (left.getDataPointRank()<right.getDataPointRank()) {  
      result=right.oneOver();  
      result*=left;  
   } else {  
      result.copy(left);  
      result/=right;  
   }  
   return result;  
1697  }  }
1698    
1699  //  //
# Line 1648  escript::operator/(const boost::python:: Line 1808  escript::operator/(const boost::python::
1808  /* TODO */  /* TODO */
1809  /* global reduction */  /* global reduction */
1810  Data  Data
1811  Data::getItem(const boost::python::object& key) const  Data::getItem(const boost::python::object& key) const
1812  {  {
1813    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1814    
1815    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1816    
1817    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1818      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1819    }    }
1820    
# Line 1664  Data::getItem(const boost::python::objec Line 1824  Data::getItem(const boost::python::objec
1824  /* TODO */  /* TODO */
1825  /* global reduction */  /* global reduction */
1826  Data  Data
1827  Data::getSlice(const DataArrayView::RegionType& region) const  Data::getSlice(const DataTypes::RegionType& region) const
1828  {  {
1829    return Data(*this,region);    return Data(*this,region);
1830  }  }
# Line 1679  Data::setItemO(const boost::python::obje Line 1839  Data::setItemO(const boost::python::obje
1839    setItemD(key,tempData);    setItemD(key,tempData);
1840  }  }
1841    
 /* TODO */  
 /* global reduction */  
1842  void  void
1843  Data::setItemD(const boost::python::object& key,  Data::setItemD(const boost::python::object& key,
1844                 const Data& value)                 const Data& value)
1845  {  {
1846    const DataArrayView& view=getPointDataView();  //  const DataArrayView& view=getPointDataView();
1847    
1848    DataArrayView::RegionType slice_region=view.getSliceRegion(key);    DataTypes::RegionType slice_region=DataTypes::getSliceRegion(getDataPointShape(),key);
1849    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=getDataPointRank()) {
1850      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1851    }    }
1852    if (getFunctionSpace()!=value.getFunctionSpace()) {    if (getFunctionSpace()!=value.getFunctionSpace()) {
# Line 1698  Data::setItemD(const boost::python::obje Line 1856  Data::setItemD(const boost::python::obje
1856    }    }
1857  }  }
1858    
 /* TODO */  
 /* global reduction */  
1859  void  void
1860  Data::setSlice(const Data& value,  Data::setSlice(const Data& value,
1861                 const DataArrayView::RegionType& region)                 const DataTypes::RegionType& region)
1862  {  {
1863    if (isProtected()) {    if (isProtected()) {
1864          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
# Line 1743  Data::typeMatchRight(const Data& right) Line 1899  Data::typeMatchRight(const Data& right)
1899    
1900  void  void
1901  Data::setTaggedValueByName(std::string name,  Data::setTaggedValueByName(std::string name,
1902                             const boost::python::object& value)                             const boost::python::object& value)
1903  {  {
1904       if (getFunctionSpace().getDomain().isValidTagName(name)) {       if (getFunctionSpace().getDomain().isValidTagName(name)) {
1905          int tagKey=getFunctionSpace().getDomain().getTag(name);          int tagKey=getFunctionSpace().getDomain().getTag(name);
# Line 1759  Data::setTaggedValue(int tagKey, Line 1915  Data::setTaggedValue(int tagKey,
1915    }    }
1916    //    //
1917    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1918    tag();    if (isConstant()) tag();
1919    
1920      numeric::array asNumArray(value);
1921    
1922    
1923    if (!isTagged()) {    // extract the shape of the numarray
1924      throw DataException("Error - DataTagged conversion failed!!");    DataTypes::ShapeType tempShape;
1925      for (int i=0; i < asNumArray.getrank(); i++) {
1926        tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
1927    }    }
1928    
1929    //    // get the space for the data vector
1930    // Construct DataArray from boost::python::object input value  //   int len = DataTypes::noValues(tempShape);
1931    DataArray valueDataArray(value);  //   DataVector temp_data(len, 0.0, len);
1932    //   DataArrayView temp_dataView(temp_data, tempShape);
1933    //   temp_dataView.copy(asNumArray);
1934    
1935      DataVector temp_data2;
1936      temp_data2.copyFromNumArray(asNumArray);
1937    
1938    //    //
1939    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1940    m_data->setTaggedValue(tagKey,valueDataArray.getView());    //m_data->setTaggedValue(tagKey,temp_dataView);
1941    
1942        m_data->setTaggedValue(tagKey,tempShape, temp_data2);
1943  }  }
1944    
1945    // void
1946    // Data::setTaggedValueFromCPP(int tagKey,
1947    //                             const DataArrayView& value)
1948    // {
1949    //   if (isProtected()) {
1950    //         throw DataException("Error - attempt to update protected Data object.");
1951    //   }
1952    //   //
1953    //   // Ensure underlying data object is of type DataTagged
1954    //   if (isConstant()) tag();
1955    //
1956    //   //
1957    //   // Call DataAbstract::setTaggedValue
1958    //   m_data->setTaggedValue(tagKey,value);
1959    // }
1960    
1961  void  void
1962  Data::setTaggedValueFromCPP(int tagKey,  Data::setTaggedValueFromCPP(int tagKey,
1963                              const DataArrayView& value)                  const DataTypes::ShapeType& pointshape,
1964                                const DataTypes::ValueType& value,
1965                    int dataOffset)
1966  {  {
1967    if (isProtected()) {    if (isProtected()) {
1968          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1969    }    }
1970    //    //
1971    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
1972    tag();    if (isConstant()) tag();
1973    
   if (!isTagged()) {  
     throw DataException("Error - DataTagged conversion failed!!");  
   }  
                                                                                                                 
1974    //    //
1975    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
1976    m_data->setTaggedValue(tagKey,value);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
1977  }  }
1978    
1979  int  int
1980  Data::getTagNumber(int dpno)  Data::getTagNumber(int dpno)
1981  {  {
1982    return m_data->getTagNumber(dpno);    return getFunctionSpace().getTagFromDataPointNo(dpno);
1983  }  }
1984    
 void  
 Data::archiveData(const std::string fileName)  
 {  
   cout << "Archiving Data object to: " << fileName << endl;  
   
   //  
   // Determine type of this Data object  
   int dataType = -1;  
   
   if (isEmpty()) {  
     dataType = 0;  
     cout << "\tdataType: DataEmpty" << endl;  
   }  
   if (isConstant()) {  
     dataType = 1;  
     cout << "\tdataType: DataConstant" << endl;  
   }  
   if (isTagged()) {  
     dataType = 2;  
     cout << "\tdataType: DataTagged" << endl;  
   }  
   if (isExpanded()) {  
     dataType = 3;  
     cout << "\tdataType: DataExpanded" << endl;  
   }  
   
   if (dataType == -1) {  
     throw DataException("archiveData Error: undefined dataType");  
   }  
   
   //  
   // Collect data items common to all Data types  
   int noSamples = getNumSamples();  
   int noDPPSample = getNumDataPointsPerSample();  
   int functionSpaceType = getFunctionSpace().getTypeCode();  
   int dataPointRank = getDataPointRank();  
   int dataPointSize = getDataPointSize();  
   int dataLength = getLength();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
   vector<int> referenceNumbers(noSamples);  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     referenceNumbers[sampleNo] = getFunctionSpace().getReferenceIDOfSample(sampleNo);  
   }  
   vector<int> tagNumbers(noSamples);  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       tagNumbers[sampleNo] = getFunctionSpace().getTagFromSampleNo(sampleNo);  
     }  
   }  
   
   cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;  
   cout << "\tfunctionSpaceType: " << functionSpaceType << endl;  
   cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;  
   
   //  
   // Flatten Shape to an array of integers suitable for writing to file  
   int flatShape[4] = {0,0,0,0};  
   cout << "\tshape: < ";  
   for (int dim=0; dim<dataPointRank; dim++) {  
     flatShape[dim] = dataPointShape[dim];  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
   
   //  
   // Open archive file  
   ofstream archiveFile;  
   archiveFile.open(fileName.data(), ios::out);  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem opening archive file");  
   }  
   
   //  
   // Write common data items to archive file  
   archiveFile.write(reinterpret_cast<char *>(&dataType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.write(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.write(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.write(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   if (isTagged()) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.write(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing to archive file");  
   }  
   
   //  
   // Archive underlying data values for each Data type  
   int noValues;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       noValues = 0;  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       break;  
     case 1:  
       // DataConstant  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 2:  
       // DataTagged  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       noValues = m_data->getLength();  
       archiveFile.write(reinterpret_cast<char *>(&noValues),sizeof(int));  
       cout << "\tnoValues: " << noValues << endl;  
       if (m_data->archiveData(archiveFile,noValues)) {  
         throw DataException("archiveData Error: problem writing data to archive file");  
       }  
       break;  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem writing data to archive file");  
   }  
   
   //  
   // Close archive file  
   archiveFile.close();  
   
   if (!archiveFile.good()) {  
     throw DataException("archiveData Error: problem closing archive file");  
   }  
   
 }  
   
 void  
 Data::extractData(const std::string fileName,  
                   const FunctionSpace& fspace)  
 {  
   //  
   // Can only extract Data to an object which is initially DataEmpty  
   if (!isEmpty()) {  
     throw DataException("extractData Error: can only extract to DataEmpty object");  
   }  
   
   cout << "Extracting Data object from: " << fileName << endl;  
   
   int dataType;  
   int noSamples;  
   int noDPPSample;  
   int functionSpaceType;  
   int dataPointRank;  
   int dataPointSize;  
   int dataLength;  
   DataArrayView::ShapeType dataPointShape;  
   int flatShape[4];  
   
   //  
   // Open the archive file  
   ifstream archiveFile;  
   archiveFile.open(fileName.data(), ios::in);  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem opening archive file");  
   }  
   
   //  
   // Read common data items from archive file  
   archiveFile.read(reinterpret_cast<char *>(&dataType),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&noSamples),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&noDPPSample),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&functionSpaceType),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataPointRank),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataPointSize),sizeof(int));  
   archiveFile.read(reinterpret_cast<char *>(&dataLength),sizeof(int));  
   for (int dim = 0; dim < 4; dim++) {  
     archiveFile.read(reinterpret_cast<char *>(&flatShape[dim]),sizeof(int));  
     if (flatShape[dim]>0) {  
       dataPointShape.push_back(flatShape[dim]);  
     }  
   }  
   vector<int> referenceNumbers(noSamples);  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     archiveFile.read(reinterpret_cast<char *>(&referenceNumbers[sampleNo]),sizeof(int));  
   }  
   vector<int> tagNumbers(noSamples);  
   if (dataType==2) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       archiveFile.read(reinterpret_cast<char *>(&tagNumbers[sampleNo]),sizeof(int));  
     }  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
   }  
   
   //  
   // Verify the values just read from the archive file  
   switch (dataType) {  
     case 0:  
       cout << "\tdataType: DataEmpty" << endl;  
       break;  
     case 1:  
       cout << "\tdataType: DataConstant" << endl;  
       break;  
     case 2:  
       cout << "\tdataType: DataTagged" << endl;  
       break;  
     case 3:  
       cout << "\tdataType: DataExpanded" << endl;  
       break;  
     default:  
       throw DataException("extractData Error: undefined dataType read from archive file");  
       break;  
   }  
   
   cout << "\tnoSamples: " << noSamples << " noDPPSample: " << noDPPSample << endl;  
   cout << "\tfunctionSpaceType: " << functionSpaceType << endl;  
   cout << "\trank: " << dataPointRank << " size: " << dataPointSize << " length: " << dataLength << endl;  
   cout << "\tshape: < ";  
   for (int dim = 0; dim < dataPointRank; dim++) {  
     cout << dataPointShape[dim] << " ";  
   }  
   cout << ">" << endl;  
   
   //  
   // Verify that supplied FunctionSpace object is compatible with this Data object.  
   if ( (fspace.getTypeCode()!=functionSpaceType) ||  
        (fspace.getNumSamples()!=noSamples) ||  
        (fspace.getNumDPPSample()!=noDPPSample)  
      ) {  
     throw DataException("extractData Error: incompatible FunctionSpace");  
   }  
   for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
     if (referenceNumbers[sampleNo] != fspace.getReferenceIDOfSample(sampleNo)) {  
       throw DataException("extractData Error: incompatible FunctionSpace");  
     }  
   }  
   if (dataType==2) {  
     for (int sampleNo=0; sampleNo<noSamples; sampleNo++) {  
       if (tagNumbers[sampleNo] != fspace.getTagFromSampleNo(sampleNo)) {  
         throw DataException("extractData Error: incompatible FunctionSpace");  
       }  
     }  
   }  
   
   //  
   // Construct a DataVector to hold underlying data values  
   DataVector dataVec(dataLength);  
   
   //  
   // Load this DataVector with the appropriate values  
   int noValues;  
   archiveFile.read(reinterpret_cast<char *>(&noValues),sizeof(int));  
   cout << "\tnoValues: " << noValues << endl;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       if (noValues != 0) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 1:  
       // DataConstant  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 2:  
       // DataTagged  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
     case 3:  
       // DataExpanded  
       if (dataVec.extractData(archiveFile,noValues)) {  
         throw DataException("extractData Error: problem reading data from archive file");  
       }  
       break;  
   }  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem reading from archive file");  
   }  
   
   //  
   // Close archive file  
   archiveFile.close();  
   
   if (!archiveFile.good()) {  
     throw DataException("extractData Error: problem closing archive file");  
   }  
   
   //  
   // Construct an appropriate Data object  
   DataAbstract* tempData;  
   switch (dataType) {  
     case 0:  
       // DataEmpty  
       tempData=new DataEmpty();  
       break;  
     case 1:  
       // DataConstant  
       tempData=new DataConstant(fspace,dataPointShape,dataVec);  
       break;  
     case 2:  
       // DataTagged  
       tempData=new DataTagged(fspace,dataPointShape,tagNumbers,dataVec);  
       break;  
     case 3:  
       // DataExpanded  
       tempData=new DataExpanded(fspace,dataPointShape,dataVec);  
       break;  
   }  
   shared_ptr<DataAbstract> temp_data(tempData);  
   m_data=temp_data;  
 }  
1985    
1986  ostream& escript::operator<<(ostream& o, const Data& data)  ostream& escript::operator<<(ostream& o, const Data& data)
1987  {  {
# Line 2150  escript::C_GeneralTensorProduct(Data& ar Line 1998  escript::C_GeneralTensorProduct(Data& ar
1998    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
1999    // SM is the product of the last axis_offset entries in arg_0.getShape().    // SM is the product of the last axis_offset entries in arg_0.getShape().
2000    
   
2001    // Interpolate if necessary and find an appropriate function space    // Interpolate if necessary and find an appropriate function space
2002    Data arg_0_Z, arg_1_Z;    Data arg_0_Z, arg_1_Z;
2003    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
# Line 2172  escript::C_GeneralTensorProduct(Data& ar Line 2019  escript::C_GeneralTensorProduct(Data& ar
2019    // Get rank and shape of inputs    // Get rank and shape of inputs
2020    int rank0 = arg_0_Z.getDataPointRank();    int rank0 = arg_0_Z.getDataPointRank();
2021    int rank1 = arg_1_Z.getDataPointRank();    int rank1 = arg_1_Z.getDataPointRank();
2022    DataArrayView::ShapeType shape0 = arg_0_Z.getDataPointShape();    const DataTypes::ShapeType& shape0 = arg_0_Z.getDataPointShape();
2023    DataArrayView::ShapeType shape1 = arg_1_Z.getDataPointShape();    const DataTypes::ShapeType& shape1 = arg_1_Z.getDataPointShape();
2024    
2025    // Prepare for the loops of the product and verify compatibility of shapes    // Prepare for the loops of the product and verify compatibility of shapes
2026    int start0=0, start1=0;    int start0=0, start1=0;
# Line 2182  escript::C_GeneralTensorProduct(Data& ar Line 2029  escript::C_GeneralTensorProduct(Data& ar
2029    else if (transpose == 2)  { start1 = rank1-axis_offset; }    else if (transpose == 2)  { start1 = rank1-axis_offset; }
2030    else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }    else              { throw DataException("C_GeneralTensorProduct: Error - transpose should be 0, 1 or 2"); }
2031    
2032    
2033    // Adjust the shapes for transpose    // Adjust the shapes for transpose
2034    DataArrayView::ShapeType tmpShape0;    DataTypes::ShapeType tmpShape0(rank0);    // pre-sizing the vectors rather
2035    DataArrayView::ShapeType tmpShape1;    DataTypes::ShapeType tmpShape1(rank1);    // than using push_back
2036    for (int i=0; i<rank0; i++)   { tmpShape0.push_back( shape0[(i+start0)%rank0] ); }    for (int i=0; i<rank0; i++)   { tmpShape0[i]=shape0[(i+start0)%rank0]; }
2037    for (int i=0; i<rank1; i++)   { tmpShape1.push_back( shape1[(i+start1)%rank1] ); }    for (int i=0; i<rank1; i++)   { tmpShape1[i]=shape1[(i+start1)%rank1]; }
2038    
2039  #if 0  #if 0
2040    // For debugging: show shape after transpose    // For debugging: show shape after transpose
# Line 2217  escript::C_GeneralTensorProduct(Data& ar Line 2065  escript::C_GeneralTensorProduct(Data& ar
2065      SR *= tmpShape1[i];      SR *= tmpShape1[i];
2066    }    }
2067    
2068    // Define the shape of the output    // Define the shape of the output (rank of shape is the sum of the loop ranges below)
2069    DataArrayView::ShapeType shape2;    DataTypes::ShapeType shape2(rank0+rank1-2*axis_offset);  
2070    for (int i=0; i<rank0-axis_offset; i++) { shape2.push_back(tmpShape0[i]); } // First part of arg_0_Z    {         // block to limit the scope of out_index
2071    for (int i=axis_offset; i<rank1; i++)   { shape2.push_back(tmpShape1[i]); } // Last part of arg_1_Z       int out_index=0;
2072         for (int i=0; i<rank0-axis_offset; i++, ++out_index) { shape2[out_index]=tmpShape0[i]; } // First part of arg_0_Z
2073         for (int i=axis_offset; i<rank1; i++, ++out_index)   { shape2[out_index]=tmpShape1[i]; } // Last part of arg_1_Z
2074      }
2075    
2076    // Declare output Data object    // Declare output Data object
2077    Data res;    Data res;
2078    
2079    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {    if      (arg_0_Z.isConstant()   && arg_1_Z.isConstant()) {
2080      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output      res = Data(0.0, shape2, arg_1_Z.getFunctionSpace());    // DataConstant output
2081      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(0));
2082      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[0]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(0));
2083      double *ptr_2 = &((res.getPointDataView().getData())[0]);      double *ptr_2 = &(res.getDataAtOffset(0));
2084      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2085    }    }
2086    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {    else if (arg_0_Z.isConstant()   && arg_1_Z.isTagged()) {
# Line 2250  escript::C_GeneralTensorProduct(Data& ar Line 2101  escript::C_GeneralTensorProduct(Data& ar
2101    
2102      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2103      int offset_0 = tmp_0->getPointOffset(0,0);      int offset_0 = tmp_0->getPointOffset(0,0);
2104      double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);      double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2105      // Get the views      // Get the views
2106      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2107      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2108      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2109      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2110      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2111    
2112        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2113        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2114    
2115    
2116      // Compute an MVP for the default      // Compute an MVP for the default
2117      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2118      // Compute an MVP for each tag      // Compute an MVP for each tag
2119      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2120      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2121      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2122        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2123        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2124        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2125        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2126        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2127    
2128          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2129          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2130        
2131        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2132      }      }
2133    
# Line 2290  escript::C_GeneralTensorProduct(Data& ar Line 2150  escript::C_GeneralTensorProduct(Data& ar
2150        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {        for (dataPointNo_1 = 0; dataPointNo_1 < numDataPointsPerSample_1; dataPointNo_1++) {
2151          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_1 = tmp_1->getPointOffset(sampleNo_1,dataPointNo_1);
2152          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);          int offset_2 = tmp_2->getPointOffset(sampleNo_1,dataPointNo_1);
2153          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2154          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2155          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2156          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2157        }        }
2158      }      }
# Line 2316  escript::C_GeneralTensorProduct(Data& ar Line 2176  escript::C_GeneralTensorProduct(Data& ar
2176    
2177      // Prepare offset into DataConstant      // Prepare offset into DataConstant
2178      int offset_1 = tmp_1->getPointOffset(0,0);      int offset_1 = tmp_1->getPointOffset(0,0);
2179      double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);      double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2180      // Get the views      // Get the views
2181      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2182      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2183      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2184      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2185      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2186    
2187        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2188        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2189    
2190      // Compute an MVP for the default      // Compute an MVP for the default
2191      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2192      // Compute an MVP for each tag      // Compute an MVP for each tag
2193      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2194      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory      DataTagged::DataMapType::const_iterator i; // i->first is a tag, i->second is an offset into memory
2195      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2196        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());  //      tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());
2197        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2198        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2199        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2200        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2201    
2202          tmp_2->addTag(i->first);
2203          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2204          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2205        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2206      }      }
2207    
# Line 2354  escript::C_GeneralTensorProduct(Data& ar Line 2222  escript::C_GeneralTensorProduct(Data& ar
2222      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());      DataTagged* tmp_2=dynamic_cast<DataTagged*>(res.borrowData());
2223      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }      if (tmp_2==0) { throw DataException("GTP Programming error - casting to DataTagged."); }
2224    
2225      // Get the views  //     // Get the views
2226      DataArrayView view_0 = tmp_0->getDefaultValue();  //     DataArrayView view_0 = tmp_0->getDefaultValue();
2227      DataArrayView view_1 = tmp_1->getDefaultValue();  //     DataArrayView view_1 = tmp_1->getDefaultValue();
2228      DataArrayView view_2 = tmp_2->getDefaultValue();  //     DataArrayView view_2 = tmp_2->getDefaultValue();
2229      // Get the pointers to the actual data  //     // Get the pointers to the actual data
2230      double *ptr_0 = &((view_0.getData())[0]);  //     double *ptr_0 = &((view_0.getData())[0]);
2231      double *ptr_1 = &((view_1.getData())[0]);  //     double *ptr_1 = &((view_1.getData())[0]);
2232      double *ptr_2 = &((view_2.getData())[0]);  //     double *ptr_2 = &((view_2.getData())[0]);
2233    
2234        double *ptr_0 = &(tmp_0->getDefaultValue(0));
2235        double *ptr_1 = &(tmp_1->getDefaultValue(0));
2236        double *ptr_2 = &(tmp_2->getDefaultValue(0));
2237    
2238    
2239      // Compute an MVP for the default      // Compute an MVP for the default
2240      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);      matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2241      // Merge the tags      // Merge the tags
# Line 2369  escript::C_GeneralTensorProduct(Data& ar Line 2243  escript::C_GeneralTensorProduct(Data& ar
2243      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();      const DataTagged::DataMapType& lookup_0=tmp_0->getTagLookup();
2244      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();      const DataTagged::DataMapType& lookup_1=tmp_1->getTagLookup();
2245      for (i=lookup_0.begin();i!=lookup_0.end();i++) {      for (i=lookup_0.begin();i!=lookup_0.end();i++) {
2246        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue()); // use tmp_2 to get correct shape        tmp_2->addTag(i->first); // use tmp_2 to get correct shape
2247      }      }
2248      for (i=lookup_1.begin();i!=lookup_1.end();i++) {      for (i=lookup_1.begin();i!=lookup_1.end();i++) {
2249        tmp_2->addTaggedValue(i->first,tmp_2->getDefaultValue());        tmp_2->addTag(i->first);
2250      }      }
2251      // Compute an MVP for each tag      // Compute an MVP for each tag
2252      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();      const DataTagged::DataMapType& lookup_2=tmp_2->getTagLookup();
2253      for (i=lookup_2.begin();i!=lookup_2.end();i++) {      for (i=lookup_2.begin();i!=lookup_2.end();i++) {
2254        DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);  //       DataArrayView view_0 = tmp_0->getDataPointByTag(i->first);
2255        DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);  //       DataArrayView view_1 = tmp_1->getDataPointByTag(i->first);
2256        DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);  //       DataArrayView view_2 = tmp_2->getDataPointByTag(i->first);
2257        double *ptr_0 = &view_0.getData(0);  //       double *ptr_0 = &view_0.getData(0);
2258        double *ptr_1 = &view_1.getData(0);  //       double *ptr_1 = &view_1.getData(0);
2259        double *ptr_2 = &view_2.getData(0);  //       double *ptr_2 = &view_2.getData(0);
2260    
2261          double *ptr_0 = &(tmp_0->getDataByTag(i->first,0));
2262          double *ptr_1 = &(tmp_1->getDataByTag(i->first,0));
2263          double *ptr_2 = &(tmp_2->getDataByTag(i->first,0));
2264    
2265        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);        matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2266      }      }
2267    
# Line 2403  escript::C_GeneralTensorProduct(Data& ar Line 2282  escript::C_GeneralTensorProduct(Data& ar
2282      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2283      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2284        int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0        int offset_0 = tmp_0->getPointOffset(sampleNo_0,0); // They're all the same, so just use #0
2285        double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);        double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2286        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2287          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2288          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2289          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2290          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2291          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2292        }        }
2293      }      }
# Line 2432  escript::C_GeneralTensorProduct(Data& ar Line 2311  escript::C_GeneralTensorProduct(Data& ar
2311        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2312          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2313          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2314          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2315          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2316          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2317          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2318        }        }
2319      }      }
# Line 2457  escript::C_GeneralTensorProduct(Data& ar Line 2336  escript::C_GeneralTensorProduct(Data& ar
2336      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)      #pragma omp parallel for private(sampleNo_0,dataPointNo_0) schedule(static)
2337      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {      for (sampleNo_0 = 0; sampleNo_0 < numSamples_0; sampleNo_0++) {
2338        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);        int offset_1 = tmp_1->getPointOffset(sampleNo_0,0);
2339        double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);        double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2340        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {        for (dataPointNo_0 = 0; dataPointNo_0 < numDataPointsPerSample_0; dataPointNo_0++) {
2341          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2342          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2343          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2344          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2345          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2346        }        }
2347      }      }
# Line 2487  escript::C_GeneralTensorProduct(Data& ar Line 2366  escript::C_GeneralTensorProduct(Data& ar
2366          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_0 = tmp_0->getPointOffset(sampleNo_0,dataPointNo_0);
2367          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_1 = tmp_1->getPointOffset(sampleNo_0,dataPointNo_0);
2368          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);          int offset_2 = tmp_2->getPointOffset(sampleNo_0,dataPointNo_0);
2369          double *ptr_0 = &((arg_0_Z.getPointDataView().getData())[offset_0]);          double *ptr_0 = &(arg_0_Z.getDataAtOffset(offset_0));
2370          double *ptr_1 = &((arg_1_Z.getPointDataView().getData())[offset_1]);          double *ptr_1 = &(arg_1_Z.getDataAtOffset(offset_1));
2371          double *ptr_2 = &((res.getPointDataView().getData())[offset_2]);          double *ptr_2 = &(res.getDataAtOffset(offset_2));
2372          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);          matrix_matrix_product(SL, SM, SR, ptr_0, ptr_1, ptr_2, transpose);
2373        }        }
2374      }      }
# Line 2508  Data::borrowData() const Line 2387  Data::borrowData() const
2387    return m_data.get();    return m_data.get();
2388  }  }
2389    
2390    
2391    std::string
2392    Data::toString() const
2393    {
2394        static const DataTypes::ValueType::size_type TOO_MANY_POINTS=80;
2395        if (getNumDataPoints()*getDataPointSize()>TOO_MANY_POINTS)
2396        {
2397        stringstream temp;
2398        temp << "Summary: inf="<< inf() << " sup=" << sup() << " data points=" << getNumDataPoints();
2399        return  temp.str();
2400        }
2401        return m_data->toString();
2402    }
2403    
2404    
2405    
2406    DataTypes::ValueType::const_reference
2407    Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2408    {
2409        return m_data->getDataAtOffset(i);
2410    }
2411    
2412    
2413    DataTypes::ValueType::reference
2414    Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2415    {
2416        return m_data->getDataAtOffset(i);
2417    }
2418    
2419    DataTypes::ValueType::const_reference
2420    Data::getDataPoint(int sampleNo, int dataPointNo) const
2421    {
2422        return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
2423    }
2424    
2425    
2426    DataTypes::ValueType::reference
2427    Data::getDataPoint(int sampleNo, int dataPointNo)
2428    {
2429        return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));
2430    }
2431    
2432    
2433  /* Member functions specific to the MPI implementation */  /* Member functions specific to the MPI implementation */
2434    
2435  void  void
2436  Data::print()  Data::print()
2437  {  {
2438    int i,j;    int i,j;
2439      
2440    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );    printf( "Data is %dX%d\n", getNumSamples(), getNumDataPointsPerSample() );
2441    for( i=0; i<getNumSamples(); i++ )    for( i=0; i<getNumSamples(); i++ )
2442    {    {
# Line 2524  Data::print() Line 2446  Data::print()
2446      printf( "\n" );      printf( "\n" );
2447    }    }
2448  }  }
2449    void
2450    Data::dump(const std::string fileName) const
2451    {
2452      try
2453         {
2454            return m_data->dump(fileName);
2455         }
2456         catch (exception& e)
2457         {
2458            cout << e.what() << endl;
2459         }
2460    }
2461    
2462  int  int
2463  Data::get_MPISize() const  Data::get_MPISize() const
2464  {  {
2465      int error, size;      int size;
2466  #ifdef PASO_MPI  #ifdef PASO_MPI
2467        int error;
2468      error = MPI_Comm_size( get_MPIComm(), &size );      error = MPI_Comm_size( get_MPIComm(), &size );
2469  #else  #else
2470      size = 1;      size = 1;
# Line 2540  Data::get_MPISize() const Line 2475  Data::get_MPISize() const
2475  int  int
2476  Data::get_MPIRank() const  Data::get_MPIRank() const
2477  {  {
2478      int error, rank;      int rank;
2479  #ifdef PASO_MPI  #ifdef PASO_MPI
2480        int error;
2481      error = MPI_Comm_rank( get_MPIComm(), &rank );      error = MPI_Comm_rank( get_MPIComm(), &rank );
2482  #else  #else
2483      rank = 0;      rank = 0;
# Line 2551  Data::get_MPIRank() const Line 2487  Data::get_MPIRank() const
2487    
2488  MPI_Comm  MPI_Comm
2489  Data::get_MPIComm() const  Data::get_MPIComm() const
2490  {  {
2491  #ifdef PASO_MPI  #ifdef PASO_MPI
2492      return MPI_COMM_WORLD;      return MPI_COMM_WORLD;
2493  #else  #else
# Line 2559  Data::get_MPIComm() const Line 2495  Data::get_MPIComm() const
2495  #endif  #endif
2496  }  }
2497    
2498    

Legend:
Removed from v.1092  
changed lines
  Added in v.1799

  ViewVC Help
Powered by ViewVC 1.1.26