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

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

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

revision 1358 by gross, Wed Dec 5 03:41:06 2007 UTC revision 1872 by jfenwick, Mon Oct 13 00:18:55 2008 UTC
# Line 1  Line 1 
1    
 /* $Id$ */  
   
2  /*******************************************************  /*******************************************************
3   *  *
4   *           Copyright 2003-2007 by ACceSS MNRF  * Copyright (c) 2003-2008 by University of Queensland
5   *       Copyright 2007 by University of Queensland  * Earth Systems Science Computational Center (ESSCC)
6   *  * http://www.uq.edu.au/esscc
7   *                http://esscc.uq.edu.au  *
8   *        Primary Business: Queensland, Australia  * Primary Business: Queensland, Australia
9   *  Licensed under the Open Software License version 3.0  * Licensed under the Open Software License version 3.0
10   *     http://www.opensource.org/licenses/osl-3.0.php  * http://www.opensource.org/licenses/osl-3.0.php
11   *  *
12   *******************************************************/  *******************************************************/
13    
14    
15    #include "Data.h"
16  #include "DataExpanded.h"  #include "DataExpanded.h"
17  #include "DataException.h"  #include "DataException.h"
18  #include "DataConstant.h"  #include "DataConstant.h"
# Line 20  Line 20 
20  #ifdef USE_NETCDF  #ifdef USE_NETCDF
21  #include <netcdfcpp.h>  #include <netcdfcpp.h>
22  #endif  #endif
23    #ifdef PASO_MPI
24    #include <mpi.h>
25    #endif
26    
27  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
28    #include "DataMaths.h"
29    
30  using namespace std;  using namespace std;
31  using namespace boost::python;  using namespace boost::python;
32  using namespace boost;  using namespace boost;
33    using namespace escript::DataTypes;
34    
35  namespace escript {  namespace escript {
36    
37  DataExpanded::DataExpanded(const boost::python::numeric::array& value,  DataExpanded::DataExpanded(const boost::python::numeric::array& value,
38                             const FunctionSpace& what)                             const FunctionSpace& what)
39    : DataAbstract(what)    : DataAbstract(what,DataTypes::shapeFromNumArray(value))
40  {  {
   DataArrayView::ShapeType tempShape;  
   //  
   // extract the shape of the python numarray  
   for (int i=0; i<value.getrank(); i++) {  
     tempShape.push_back(extract<int>(value.getshape()[i]));  
   }  
41    //    //
42    // initialise the data array for this object    // initialise the data array for this object
43    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());    initialise(what.getNumSamples(),what.getNumDPPSample());
44    //    //
45    // copy the given value to every data point    // copy the given value to every data point
46    copy(value);    copy(value);
47  }  }
48    
49  DataExpanded::DataExpanded(const DataExpanded& other)  DataExpanded::DataExpanded(const DataExpanded& other)
50    : DataAbstract(other.getFunctionSpace()),    : DataAbstract(other.getFunctionSpace(), other.getShape()),
51    m_data(other.m_data)    m_data(other.m_data)
52  {  {
   //  
   // create the view for the data  
   DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());  
   setPointDataView(temp);  
53  }  }
54    
55  DataExpanded::DataExpanded(const DataConstant& other)  DataExpanded::DataExpanded(const DataConstant& other)
56    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace(), other.getShape())
57  {  {
58    //    //
59    // initialise the data array for this object    // initialise the data array for this object
60    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());    initialise(other.getNumSamples(),other.getNumDPPSample());
61    //    //
62    // DataConstant only has one value, copy this to every data point    // DataConstant only has one value, copy this to every data point
63    copy(other.getPointDataView());    copy(other);
64  }  }
65    
66  DataExpanded::DataExpanded(const DataTagged& other)  DataExpanded::DataExpanded(const DataTagged& other)
67    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace(), other.getShape())
68  {  {
69    //    //
70    // initialise the data array for this object    // initialise the data array for this object
71    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());    initialise(other.getNumSamples(),other.getNumDPPSample());
72    //    //
73    // for each data point in this object, extract and copy the corresponding data    // for each data point in this object, extract and copy the corresponding data
74    // value from the given DataTagged object    // value from the given DataTagged object
75    int i,j;    int i,j;
76    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataTypes::ValueType::size_type numRows=m_data.getNumRows();
77    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataTypes::ValueType::size_type numCols=m_data.getNumCols();
78    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
79    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
80      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
81        try {        try {
82          getPointDataView().copy(getPointOffset(i,j),             DataTypes::copyPoint(getVector(), getPointOffset(i,j), getNoValues(),
83                                  other.getPointDataView(),                                  other.getVector(),
84                                  other.getPointOffset(i,j));                                  other.getPointOffset(i,j));
85        }        }
86        catch (std::exception& e) {        catch (std::exception& e) {
# Line 96  DataExpanded::DataExpanded(const DataTag Line 91  DataExpanded::DataExpanded(const DataTag
91  }  }
92    
93  DataExpanded::DataExpanded(const DataExpanded& other,  DataExpanded::DataExpanded(const DataExpanded& other,
94                             const DataArrayView::RegionType& region)                             const DataTypes::RegionType& region)
95    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))
96  {  {
97    //    //
98    // get the shape of the slice    // get the shape of the slice
99    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));  //   DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
100    //    //
101    // initialise this Data object to the shape of the slice    // initialise this Data object to the shape of the slice
102    initialise(shape,other.getNumSamples(),other.getNumDPPSample());    initialise(other.getNumSamples(),other.getNumDPPSample());
103    //    //
104    // copy the data    // copy the data
105    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
106    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataTypes::ValueType::size_type numRows=m_data.getNumRows();
107    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataTypes::ValueType::size_type numCols=m_data.getNumCols();
108    int i,j;    int i,j;
109    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
110    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
111      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
112        try {        try {
113          getPointDataView().copySlice(getPointOffset(i,j),  //         getPointDataView().copySlice(getPointOffset(i,j),
114                                       other.getPointDataView(),  //                                      other.getPointDataView(),
115    //                                      other.getPointOffset(i,j),
116    //                                      region_loop_range);
117            DataTypes::copySlice(getVector(),getShape(),getPointOffset(i,j),
118                                         other.getVector(),
119                         other.getShape(),
120                                       other.getPointOffset(i,j),                                       other.getPointOffset(i,j),
121                                       region_loop_range);                                       region_loop_range);
122        }        }
# Line 127  DataExpanded::DataExpanded(const DataExp Line 127  DataExpanded::DataExpanded(const DataExp
127    }    }
128  }  }
129    
130  DataExpanded::DataExpanded(const DataArrayView& value,  // DataExpanded::DataExpanded(const DataArrayView& value,
131                             const FunctionSpace& what)  //                            const FunctionSpace& what)
132    : DataAbstract(what)  //   : DataAbstract(what)
133  {  // {
134    //  //   //
135    // get the shape of the given data value  //   // get the shape of the given data value
136    DataArrayView::ShapeType tempShape=value.getShape();  //   DataTypes::ShapeType tempShape=value.getShape();
137    //  //   //
138    // initialise this Data object to the shape of the given data value  //   // initialise this Data object to the shape of the given data value
139    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());  //   initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
140    //  //   //
141    // copy the given value to every data point  //   // copy the given value to every data point
142    copy(value);  //   copy(value);
143  }  // }
144    
145  DataExpanded::DataExpanded(const FunctionSpace& what,  DataExpanded::DataExpanded(const FunctionSpace& what,
146                             const DataArrayView::ShapeType &shape,                             const DataTypes::ShapeType &shape,
147                             const DataArrayView::ValueType &data)                             const DataTypes::ValueType &data)
148    : DataAbstract(what)    : DataAbstract(what,shape)
149  {  {
150    //    EsysAssert(data.size()%getNoValues()==0,
151    // create the view of the data                   "DataExpanded Constructor - size of supplied data is not a multiple of shape size.");
152    initialise(shape,what.getNumSamples(),what.getNumDPPSample());  
153    //    if (data.size()==getNoValues())
154    // copy the data in the correct format    {
155    m_data.getData()=data;       ValueType& vec=m_data.getData();
156         //
157         // create the view of the data
158         initialise(what.getNumSamples(),what.getNumDPPSample());
159         // now we copy this value to all elements
160         for (int i=0;i<getLength();)
161         {
162        for (int j=0;j<getNoValues();++j,++i)
163        {
164            vec[i]=data[j];
165        }
166         }
167      }
168      else
169      {
170         //
171         // copy the data in the correct format
172         m_data.getData()=data;
173      }
174    
175    
176  }  }
177    
178  DataExpanded::~DataExpanded()  DataExpanded::~DataExpanded()
# Line 160  DataExpanded::~DataExpanded() Line 180  DataExpanded::~DataExpanded()
180  }  }
181    
182  DataAbstract*  DataAbstract*
183  DataExpanded::getSlice(const DataArrayView::RegionType& region) const  DataExpanded::deepCopy()
184    {
185      return new DataExpanded(*this);
186    }
187    
188    
189    DataAbstract*
190    DataExpanded::getSlice(const DataTypes::RegionType& region) const
191  {  {
192    return new DataExpanded(*this,region);    return new DataExpanded(*this,region);
193  }  }
194    
195  void  void
196  DataExpanded::setSlice(const DataAbstract* value,  DataExpanded::setSlice(const DataAbstract* value,
197                         const DataArrayView::RegionType& region)                         const DataTypes::RegionType& region)
198  {  {
199    const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);    const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
200    if (tempDataExp==0) {    if (tempDataExp==0) {
# Line 175  DataExpanded::setSlice(const DataAbstrac Line 202  DataExpanded::setSlice(const DataAbstrac
202    }    }
203    //    //
204    // get shape of slice    // get shape of slice
205    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
206    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
207    //    //
208    // check shape    // check shape
209    if (getPointDataView().getRank()!=region.size()) {    if (getRank()!=region.size()) {
210      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
211    }    }
212    if (tempDataExp->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {    if (tempDataExp->getRank()>0 && !DataTypes::checkShape(value->getShape(), shape)) {
213      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (DataTypes::createShapeErrorMessage(
214          "Error - Couldn't copy slice due to shape mismatch.",shape));          "Error - Couldn't copy slice due to shape mismatch.",shape, value->getShape()));
215    }    }
216    //    //
217    // copy the data from the slice into this object    // copy the data from the slice into this object
218    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataTypes::ValueType::size_type numRows=m_data.getNumRows();
219    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataTypes::ValueType::size_type numCols=m_data.getNumCols();
220    int i, j;    int i, j;
221      ValueType& vec=getVector();
222      const ShapeType& mshape=getShape();
223      const ValueType& tVec=tempDataExp->getVector();
224      const ShapeType& tShape=tempDataExp->getShape();
225    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
226    for (i=0;i<numRows;i++) {    for (i=0;i<numRows;i++) {
227      for (j=0;j<numCols;j++) {      for (j=0;j<numCols;j++) {
228        getPointDataView().copySliceFrom(getPointOffset(i,j),  /*      getPointDataView().copySliceFrom(getPointOffset(i,j),
229                                         tempDataExp->getPointDataView(),                                         tempDataExp->getPointDataView(),
230                                         tempDataExp->getPointOffset(i,j),                                         tempDataExp->getPointOffset(i,j),
231                                           region_loop_range);*/
232            DataTypes::copySliceFrom(vec,mshape,getPointOffset(i,j),
233                                           tVec,
234                           tShape,
235                                           tempDataExp->getPointOffset(i,j),
236                                         region_loop_range);                                         region_loop_range);
237    
238      }      }
239    }    }
240  }  }
241    
242  void  void
243  DataExpanded::copy(const DataArrayView& value)  DataExpanded::copy(const DataConstant& value)
244  {  {
245      EsysAssert((checkShape(getShape(), value.getShape())),
246                     createShapeErrorMessage("Error - Couldn't copy due to shape mismatch.",value.getShape(),getShape()));
247    
248    //    //
249    // copy a single value to every data point in this object    // copy a single value to every data point in this object
250    int nRows=m_data.getNumRows();    int nRows=m_data.getNumRows();
# Line 217  DataExpanded::copy(const DataArrayView& Line 257  DataExpanded::copy(const DataArrayView&
257        // DOASSERT is on which of course will play        // DOASSERT is on which of course will play
258        // havoc with the omp threads. Run single threaded        // havoc with the omp threads. Run single threaded
259        // if using DOASSERT.        // if using DOASSERT.
260        getPointDataView().copy(getPointOffset(i,j),value);        //getPointDataView().copy(getPointOffset(i,j),value);
261          DataTypes::copyPoint(getVector(), getPointOffset(i,j), getNoValues(), value.getVector(), 0);
262      }      }
263    }    }
264  }  }
265    
266    
267    // void
268    // DataExpanded::copy(const DataArrayView& value)
269    // {
270    //   //
271    //   // copy a single value to every data point in this object
272    //   int nRows=m_data.getNumRows();
273    //   int nCols=m_data.getNumCols();
274    //   int i,j;
275    //   #pragma omp parallel for private(i,j) schedule(static)
276    //   for (i=0;i<nRows;i++) {
277    //     for (j=0;j<nCols;j++) {
278    //       // NOTE: An exception may be thown from this call if
279    //       // DOASSERT is on which of course will play
280    //       // havoc with the omp threads. Run single threaded
281    //       // if using DOASSERT.
282    //       getPointDataView().copy(getPointOffset(i,j),value);
283    //     }
284    //   }
285    // }
286    
287  void  void
288  DataExpanded::copy(const boost::python::numeric::array& value)  DataExpanded::copy(const boost::python::numeric::array& value)
289  {  {
290    
291    // extract the shape of the numarray    // extract the shape of the numarray
292    DataArrayView::ShapeType tempShape;    DataTypes::ShapeType tempShape;
293    for (int i=0; i < value.getrank(); i++) {    for (int i=0; i < value.getrank(); i++) {
294      tempShape.push_back(extract<int>(value.getshape()[i]));      tempShape.push_back(extract<int>(value.getshape()[i]));
295    }    }
296    
297    // get the space for the data vector    // get the space for the data vector
298    int len = DataArrayView::noValues(tempShape);  //   int len = DataTypes::noValues(tempShape);
299    DataVector temp_data(len, 0.0, len);  //   DataVector temp_data(len, 0.0, len);
300    DataArrayView temp_dataView(temp_data, tempShape);  //   DataArrayView temp_dataView(temp_data, tempShape);
301    temp_dataView.copy(value);  //   temp_dataView.copy(value);
302    
303    //    //
304    // check the input shape matches this shape    // check the input shape matches this shape
305    if (!getPointDataView().checkShape(temp_dataView.getShape())) {    if (!DataTypes::checkShape(getShape(),tempShape)) {
306      throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException(DataTypes::createShapeErrorMessage(
307                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",
308                          temp_dataView.getShape()));                          tempShape,getShape()));
309    }    }
310    //    //
311    // now copy over the data    // now copy over the data
312    copy(temp_dataView);    //copy(temp_dataView);
313      getVector().copyFromNumArray(value);
314  }  }
315    
316    
317  void  void
318  DataExpanded::initialise(const DataArrayView::ShapeType& shape,  DataExpanded::initialise(int noSamples,
                          int noSamples,  
319                           int noDataPointsPerSample)                           int noDataPointsPerSample)
320  {  {
321      if (noSamples==0)     //retain the default empty object
322      {
323         return;
324      }
325    //    //
326    // resize data array to the required size    // resize data array to the required size
327    m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));    m_data.resize(noSamples,noDataPointsPerSample,getNoValues());
   //  
   // create the data view of the data array  
   DataArrayView temp(m_data.getData(),shape);  
   setPointDataView(temp);  
328  }  }
329    
330  string  string
# Line 269  DataExpanded::toString() const Line 332  DataExpanded::toString() const
332  {  {
333    stringstream temp;    stringstream temp;
334    FunctionSpace fs=getFunctionSpace();    FunctionSpace fs=getFunctionSpace();
335    //  
336    // create a temporary view as the offset will be changed    int offset=0;
   DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());  
337    for (int i=0;i<m_data.getNumRows();i++) {    for (int i=0;i<m_data.getNumRows();i++) {
338      for (int j=0;j<m_data.getNumCols();j++) {      for (int j=0;j<m_data.getNumCols();j++) {
339        tempView.setOffset(getPointOffset(i,j));        offset=getPointOffset(i,j);
340        stringstream suffix;        stringstream suffix;
341        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";
342        temp << tempView.toString(suffix.str());        temp << DataTypes::pointToString(getVector(),getShape(),offset,suffix.str());
343        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
344          temp << endl;          temp << endl;
345        }        }
346      }      }
347    }    }
348      string result=temp.str();
349      if (result.empty())
350      {
351        return "(data contains no samples)\n";
352      }
353    return temp.str();    return temp.str();
354  }  }
355    
356  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
357  DataExpanded::getPointOffset(int sampleNo,  DataExpanded::getPointOffset(int sampleNo,
358                               int dataPointNo) const                               int dataPointNo) const
359  {  {
360    return m_data.index(sampleNo,dataPointNo);    return m_data.index(sampleNo,dataPointNo);
361  }  }
362    
363  DataArrayView  DataTypes::ValueType::size_type
 DataExpanded::getDataPoint(int sampleNo,  
                            int dataPointNo)  
 {  
   DataArrayView temp(m_data.getData(),getPointDataView().getShape(),getPointOffset(sampleNo,dataPointNo));  
   return temp;  
 }  
   
 DataArrayView::ValueType::size_type  
364  DataExpanded::getLength() const  DataExpanded::getLength() const
365  {  {
366    return m_data.size();    return m_data.size();
367  }  }
368    
 int  
 DataExpanded::archiveData(ofstream& archiveFile,  
                           const DataArrayView::ValueType::size_type noValues) const  
 {  
   return(m_data.archiveData(archiveFile, noValues));  
 }  
369    
 int  
 DataExpanded::extractData(ifstream& archiveFile,  
                           const DataArrayView::ValueType::size_type noValues)  
 {  
   return(m_data.extractData(archiveFile, noValues));  
 }  
370    
371  void  void
372  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {  DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {
# Line 327  DataExpanded::copyToDataPoint(const int Line 374  DataExpanded::copyToDataPoint(const int
374    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
375    int numSamples = getNumSamples();    int numSamples = getNumSamples();
376    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
377    int dataPointRank = getPointDataView().getRank();    int dataPointRank = getRank();
378    ShapeType dataPointShape = getPointDataView().getShape();    ShapeType dataPointShape = getShape();
379    if (numSamples*numDataPointsPerSample > 0) {    if (numSamples*numDataPointsPerSample > 0) {
380       //TODO: global error handling       //TODO: global error handling
381       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {       if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
# Line 337  DataExpanded::copyToDataPoint(const int Line 384  DataExpanded::copyToDataPoint(const int
384       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
385             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
386       }       }
387       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);       ValueType::size_type offset = getPointOffset(sampleNo, dataPointNo);
388         ValueType& vec=getVector();
389       if (dataPointRank==0) {       if (dataPointRank==0) {
390           dataPointView()=value;           vec[offset]=value;
391       } else if (dataPointRank==1) {       } else if (dataPointRank==1) {
392          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
393              dataPointView(i)=value;              vec[offset+i]=value;
394          }          }
395       } else if (dataPointRank==2) {       } else if (dataPointRank==2) {
396          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
397             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
398                dataPointView(i,j)=value;                vec[offset+getRelIndex(dataPointShape,i,j)]=value;
399             }             }
400          }          }
401       } else if (dataPointRank==3) {       } else if (dataPointRank==3) {
402          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<dataPointShape[0]; i++) {
403             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
404                for (int k=0; k<dataPointShape[2]; k++) {                for (int k=0; k<dataPointShape[2]; k++) {
405                   dataPointView(i,j,k)=value;                   vec[offset+getRelIndex(dataPointShape,i,j,k)]=value;
406                }                }
407             }             }
408          }          }
# Line 363  DataExpanded::copyToDataPoint(const int Line 411  DataExpanded::copyToDataPoint(const int
411             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
412               for (int k=0; k<dataPointShape[2]; k++) {               for (int k=0; k<dataPointShape[2]; k++) {
413                 for (int l=0; l<dataPointShape[3]; l++) {                 for (int l=0; l<dataPointShape[3]; l++) {
414                    dataPointView(i,j,k,l)=value;                    vec[offset+getRelIndex(dataPointShape,i,j,k,l)]=value;
415                 }                 }
416               }               }
417             }             }
# Line 377  DataExpanded::copyToDataPoint(const int Line 425  DataExpanded::copyToDataPoint(const int
425    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
426    int numSamples = getNumSamples();    int numSamples = getNumSamples();
427    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
428    int dataPointRank = getPointDataView().getRank();    int dataPointRank = getRank();
429    ShapeType dataPointShape = getPointDataView().getShape();    const ShapeType& shape = getShape();
430    //    //
431    // check rank:    // check rank:
432    if (value.getrank()!=dataPointRank)    if (value.getrank()!=dataPointRank)
# Line 391  DataExpanded::copyToDataPoint(const int Line 439  DataExpanded::copyToDataPoint(const int
439       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {       if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
440             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");             throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
441       }       }
442       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);       ValueType& vec=getVector();
443       if (dataPointRank==0) {       if (dataPointRank==0) {
444           dataPointView()=extract<double>(value[0]);           vec[0]=extract<double>(value[0]);
445       } else if (dataPointRank==1) {       } else if (dataPointRank==1) {
446          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<shape[0]; i++) {
447              dataPointView(i)=extract<double>(value[i]);              vec[i]=extract<double>(value[i]);
448          }          }
449       } else if (dataPointRank==2) {       } else if (dataPointRank==2) {
450          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<shape[0]; i++) {
451             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<shape[1]; j++) {
452                dataPointView(i,j)=extract<double>(value[i][j]);                vec[getRelIndex(shape,i,j)]=extract<double>(value[i][j]);
453             }             }
454          }          }
455       } else if (dataPointRank==3) {       } else if (dataPointRank==3) {
456          for (int i=0; i<dataPointShape[0]; i++) {          for (int i=0; i<shape[0]; i++) {
457             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<shape[1]; j++) {
458                for (int k=0; k<dataPointShape[2]; k++) {                for (int k=0; k<shape[2]; k++) {
459                   dataPointView(i,j,k)=extract<double>(value[i][j][k]);                   vec[getRelIndex(shape,i,j,k)]=extract<double>(value[i][j][k]);
460                }                }
461             }             }
462          }          }
463       } else if (dataPointRank==4) {       } else if (dataPointRank==4) {
464           for (int i=0; i<dataPointShape[0]; i++) {           for (int i=0; i<shape[0]; i++) {
465             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<shape[1]; j++) {
466               for (int k=0; k<dataPointShape[2]; k++) {               for (int k=0; k<shape[2]; k++) {
467                 for (int l=0; l<dataPointShape[3]; l++) {                 for (int l=0; l<shape[3]; l++) {
468                    dataPointView(i,j,k,l)=extract<double>(value[i][j][k][l]);                    vec[getRelIndex(shape,i,j,k,l)]=extract<double>(value[i][j][k][l]);
469                 }                 }
470               }               }
471             }             }
# Line 431  DataExpanded::copyAll(const boost::pytho Line 479  DataExpanded::copyAll(const boost::pytho
479    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
480    int numSamples = getNumSamples();    int numSamples = getNumSamples();
481    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
482    int dataPointRank = getPointDataView().getRank();    int dataPointRank = getRank();
483    ShapeType dataPointShape = getPointDataView().getShape();    const ShapeType& dataPointShape = getShape();
484    //    //
485    // check rank:    // check rank:
486    if (value.getrank()!=dataPointRank+1)    if (value.getrank()!=dataPointRank+1)
# Line 440  DataExpanded::copyAll(const boost::pytho Line 488  DataExpanded::copyAll(const boost::pytho
488    if (value.getshape()[0]!=numSamples*numDataPointsPerSample)    if (value.getshape()[0]!=numSamples*numDataPointsPerSample)
489         throw DataException("leading dimension of numarray is too small");         throw DataException("leading dimension of numarray is too small");
490    //    //
491      ValueType& vec=getVector();
492    int dataPoint = 0;    int dataPoint = 0;
493    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {
494      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
495        DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);        ValueType::size_type offset=getPointOffset(sampleNo, dataPointNo);
496        if (dataPointRank==0) {        if (dataPointRank==0) {
497           dataPointView()=extract<double>(value[dataPoint]);           vec[offset]=extract<double>(value[dataPoint]);
498        } else if (dataPointRank==1) {        } else if (dataPointRank==1) {
499           for (int i=0; i<dataPointShape[0]; i++) {           for (int i=0; i<dataPointShape[0]; i++) {
500              dataPointView(i)=extract<double>(value[dataPoint][i]);              vec[offset+i]=extract<double>(value[dataPoint][i]);
501           }           }
502        } else if (dataPointRank==2) {        } else if (dataPointRank==2) {
503           for (int i=0; i<dataPointShape[0]; i++) {           for (int i=0; i<dataPointShape[0]; i++) {
504             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
505               dataPointView(i,j)=extract<double>(value[dataPoint][i][j]);           vec[offset+getRelIndex(dataPointShape,i,j)]=extract<double>(value[dataPoint][i][j]);
506             }             }
507           }           }
508         } else if (dataPointRank==3) {         } else if (dataPointRank==3) {
509           for (int i=0; i<dataPointShape[0]; i++) {           for (int i=0; i<dataPointShape[0]; i++) {
510             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
511               for (int k=0; k<dataPointShape[2]; k++) {               for (int k=0; k<dataPointShape[2]; k++) {
512                   dataPointView(i,j,k)=extract<double>(value[dataPoint][i][j][k]);           vec[offset+getRelIndex(dataPointShape,i,j,k)]=extract<double>(value[dataPoint][i][j][k]);
513               }               }
514             }             }
515           }           }
# Line 469  DataExpanded::copyAll(const boost::pytho Line 518  DataExpanded::copyAll(const boost::pytho
518             for (int j=0; j<dataPointShape[1]; j++) {             for (int j=0; j<dataPointShape[1]; j++) {
519               for (int k=0; k<dataPointShape[2]; k++) {               for (int k=0; k<dataPointShape[2]; k++) {
520                 for (int l=0; l<dataPointShape[3]; l++) {                 for (int l=0; l<dataPointShape[3]; l++) {
521                   dataPointView(i,j,k,l)=extract<double>(value[dataPoint][i][j][k][l]);                   vec[offset+getRelIndex(dataPointShape,i,j,k,l)]=extract<double>(value[dataPoint][i][j][k][l]);
522                 }                 }
523               }               }
524             }             }
# Line 489  DataExpanded::symmetric(DataAbstract* ev Line 538  DataExpanded::symmetric(DataAbstract* ev
538    if (temp_ev==0) {    if (temp_ev==0) {
539      throw DataException("Error - DataExpanded::symmetric: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::symmetric: casting to DataExpanded failed (propably a programming error).");
540    }    }
541    DataArrayView& thisView=getPointDataView();    ValueType& vec=getVector();
542    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
543      ValueType& evVec=temp_ev->getVector();
544      const ShapeType& evShape=temp_ev->getShape();
545    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
546    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
547      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
548           DataArrayView::symmetric(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::symmetric(vec,shape,getPointOffset(sampleNo,dataPointNo),
549                                      evView,ev->getPointOffset(sampleNo,dataPointNo));                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo));
550      }      }
551    }    }
552  }  }
# Line 509  DataExpanded::nonsymmetric(DataAbstract* Line 560  DataExpanded::nonsymmetric(DataAbstract*
560    if (temp_ev==0) {    if (temp_ev==0) {
561      throw DataException("Error - DataExpanded::nonsymmetric: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::nonsymmetric: casting to DataExpanded failed (propably a programming error).");
562    }    }
563    DataArrayView& thisView=getPointDataView();    ValueType& vec=getVector();
564    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
565      ValueType& evVec=temp_ev->getVector();
566      const ShapeType& evShape=temp_ev->getShape();
567    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
568    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
569      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
570           DataArrayView::nonsymmetric(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::nonsymmetric(vec,shape,getPointOffset(sampleNo,dataPointNo),
571                                      evView,ev->getPointOffset(sampleNo,dataPointNo));                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo));
572      }      }
573    }    }
574  }  }
# Line 529  DataExpanded::trace(DataAbstract* ev, in Line 582  DataExpanded::trace(DataAbstract* ev, in
582    if (temp_ev==0) {    if (temp_ev==0) {
583      throw DataException("Error - DataExpanded::trace: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::trace: casting to DataExpanded failed (propably a programming error).");
584    }    }
585    DataArrayView& thisView=getPointDataView();    ValueType& vec=getVector();
586    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
587      ValueType& evVec=temp_ev->getVector();
588      const ShapeType& evShape=temp_ev->getShape();
589    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
590    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
591      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
592           DataArrayView::trace(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::trace(vec,shape,getPointOffset(sampleNo,dataPointNo),
593                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);
594      }      }
595    }    }
596  }  }
# Line 550  DataExpanded::transpose(DataAbstract* ev Line 605  DataExpanded::transpose(DataAbstract* ev
605    if (temp_ev==0) {    if (temp_ev==0) {
606      throw DataException("Error - DataExpanded::transpose: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::transpose: casting to DataExpanded failed (propably a programming error).");
607    }    }
608    DataArrayView& thisView=getPointDataView();    ValueType& vec=getVector();
609    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
610      ValueType& evVec=temp_ev->getVector();
611      const ShapeType& evShape=temp_ev->getShape();
612    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
613    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
614      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
615           DataArrayView::transpose(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::transpose(vec,shape,getPointOffset(sampleNo,dataPointNo),
616                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);
617      }      }
618    }    }
619  }  }
# Line 571  DataExpanded::swapaxes(DataAbstract* ev, Line 628  DataExpanded::swapaxes(DataAbstract* ev,
628    if (temp_ev==0) {    if (temp_ev==0) {
629      throw DataException("Error - DataExpanded::swapaxes: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::swapaxes: casting to DataExpanded failed (propably a programming error).");
630    }    }
631    DataArrayView& thisView=getPointDataView();    ValueType& vec=getVector();
632    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
633      ValueType& evVec=temp_ev->getVector();
634      const ShapeType& evShape=temp_ev->getShape();
635    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
636    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
637      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
638           DataArrayView::swapaxes(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::swapaxes(vec,shape,getPointOffset(sampleNo,dataPointNo),
639                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis0,axis1);                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),axis0,axis1);
640      }      }
641    }    }
642  }  }
# Line 591  DataExpanded::eigenvalues(DataAbstract* Line 650  DataExpanded::eigenvalues(DataAbstract*
650    if (temp_ev==0) {    if (temp_ev==0) {
651      throw DataException("Error - DataExpanded::eigenvalues: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::eigenvalues: casting to DataExpanded failed (propably a programming error).");
652    }    }
653    DataArrayView& thisView=getPointDataView();    ValueType& vec=getVector();
654    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
655      ValueType& evVec=temp_ev->getVector();
656      const ShapeType& evShape=temp_ev->getShape();
657    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
658    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
659      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
660           DataArrayView::eigenvalues(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::eigenvalues(vec,shape,getPointOffset(sampleNo,dataPointNo),
661                                      evView,ev->getPointOffset(sampleNo,dataPointNo));                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo));
662      }      }
663    }    }
664  }  }
# Line 615  DataExpanded::eigenvalues_and_eigenvecto Line 676  DataExpanded::eigenvalues_and_eigenvecto
676    if (temp_V==0) {    if (temp_V==0) {
677      throw DataException("Error - DataExpanded::eigenvalues_and_eigenvectors: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::eigenvalues_and_eigenvectors: casting to DataExpanded failed (propably a programming error).");
678    }    }
679    DataArrayView& thisView=getPointDataView();    ValueType& vec=getVector();
680    DataArrayView& evView=ev->getPointDataView();    const ShapeType& shape=getShape();
681    DataArrayView& VView=V->getPointDataView();    ValueType& evVec=temp_ev->getVector();
682      const ShapeType& evShape=temp_ev->getShape();
683      ValueType& VVec=temp_V->getVector();
684      const ShapeType& VShape=temp_V->getShape();
685    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
686    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
687      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
688           DataArrayView::eigenvalues_and_eigenvectors(thisView,getPointOffset(sampleNo,dataPointNo),           DataMaths::eigenvalues_and_eigenvectors(vec,shape,getPointOffset(sampleNo,dataPointNo),
689                                                       evView,ev->getPointOffset(sampleNo,dataPointNo),                                      evVec,evShape,ev->getPointOffset(sampleNo,dataPointNo),
690                                                       VView,V->getPointOffset(sampleNo,dataPointNo),                                      VVec, VShape,V->getPointOffset(sampleNo,dataPointNo),tol);
                                                      tol);  
691      }      }
692    }    }
693  }  }
694    
695  void  void
696  DataExpanded::setToZero(){  DataExpanded::setToZero(){
697    // TODO: Surely there is a more efficient way to do this????
698    // Why is there no memset here? Parallel issues?
699    int numSamples = getNumSamples();    int numSamples = getNumSamples();
700    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
701    DataArrayView& thisView=getPointDataView();    DataTypes::ValueType::size_type n = getNoValues();
   DataArrayView::ValueType::size_type n = thisView.noValues();  
702    double* p;    double* p;
703    int  sampleNo,dataPointNo, i;    int  sampleNo,dataPointNo, i;
704    #pragma omp parallel for private(sampleNo,dataPointNo,p,i) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo,p,i) schedule(static)
705    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
706      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
707          p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);          p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);
708          for (int i=0; i<n ;++i) p[i]=0.;          for (i=0; i<n ;++i) p[i]=0.;
709      }      }
710    }    }
711  }  }
712    
713    /* Append MPI rank to file name if multiple MPI processes */
714    char *Escript_MPI_appendRankToFileName(const char *fileName, int mpi_size, int mpi_rank) {
715      /* Make plenty of room for the mpi_rank number and terminating '\0' */
716      char *newFileName = (char *)malloc(strlen(fileName)+20);
717      strncpy(newFileName, fileName, strlen(fileName)+1);
718      if (mpi_size>1) sprintf(newFileName+strlen(newFileName), ".%04d", mpi_rank);
719      return(newFileName);
720    }
721    
722  void  void
723  DataExpanded::dump(const std::string fileName) const  DataExpanded::dump(const std::string fileName) const
724  {  {
    #ifdef PASO_MPI  
    throw DataException("Error - DataExpanded:: dump is not implemented for MPI yet.");  
    #endif  
725     #ifdef USE_NETCDF     #ifdef USE_NETCDF
726     const int ldims=2+DataArrayView::maxRank;     const int ldims=2+DataTypes::maxRank;
727     const NcDim* ncdims[ldims];     const NcDim* ncdims[ldims];
728     NcVar *var, *ids;     NcVar *var, *ids;
729     int rank = getPointDataView().getRank();     int rank = getRank();
730     int type=  getFunctionSpace().getTypeCode();     int type=  getFunctionSpace().getTypeCode();
731     int ndims =0;     int ndims =0;
732     long dims[ldims];     long dims[ldims];
733     const double* d_ptr=&(m_data[0]);     const double* d_ptr=&(m_data[0]);
734     DataArrayView::ShapeType shape = getPointDataView().getShape();     const DataTypes::ShapeType& shape = getShape();
735       int mpi_iam=getFunctionSpace().getDomain()->getMPIRank();
736       int mpi_num=getFunctionSpace().getDomain()->getMPISize();
737    #ifdef PASO_MPI
738       MPI_Status status;
739    #endif
740    
741    #ifdef PASO_MPI
742       /* Serialize NetCDF I/O */
743       if (mpi_iam>0) MPI_Recv(&ndims, 0, MPI_INT, mpi_iam-1, 81801, MPI_COMM_WORLD, &status);
744    #endif
745    
746     // netCDF error handler     // netCDF error handler
747     NcError err(NcError::verbose_nonfatal);     NcError err(NcError::verbose_nonfatal);
748     // Create the file.     // Create the file.
749     NcFile dataFile(fileName.c_str(), NcFile::Replace);     char *newFileName = Escript_MPI_appendRankToFileName(fileName.c_str(), mpi_num, mpi_iam);
750       NcFile dataFile(newFileName, NcFile::Replace);
751     // check if writing was successful     // check if writing was successful
752     if (!dataFile.is_valid())     if (!dataFile.is_valid())
753          throw DataException("Error - DataExpanded:: opening of netCDF file for output failed.");          throw DataException("Error - DataExpanded:: opening of netCDF file for output failed.");
# Line 681  DataExpanded::dump(const std::string fil Line 761  DataExpanded::dump(const std::string fil
761     if ( rank >0 ) {     if ( rank >0 ) {
762         dims[0]=shape[0];         dims[0]=shape[0];
763         if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )         if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
764              throw DataException("Error - DataExpanded:: appending ncdimsion 0 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 0 to netCDF file failed.");
765     }     }
766     if ( rank >1 ) {     if ( rank >1 ) {
767         dims[1]=shape[1];         dims[1]=shape[1];
768         if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )         if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
769              throw DataException("Error - DataExpanded:: appending ncdimsion 1 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 1 to netCDF file failed.");
770     }     }
771     if ( rank >2 ) {     if ( rank >2 ) {
772         dims[2]=shape[2];         dims[2]=shape[2];
773         if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )         if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
774              throw DataException("Error - DataExpanded:: appending ncdimsion 2 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 2 to netCDF file failed.");
775     }     }
776     if ( rank >3 ) {     if ( rank >3 ) {
777         dims[3]=shape[3];         dims[3]=shape[3];
778         if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )         if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
779              throw DataException("Error - DataExpanded:: appending ncdimsion 3 to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending ncdimension 3 to netCDF file failed.");
780     }     }
781     dims[rank]=getFunctionSpace().getNumDataPointsPerSample();     dims[rank]=getFunctionSpace().getNumDataPointsPerSample();
782     if (! (ncdims[rank] = dataFile.add_dim("num_data_points_per_sample", dims[rank])) )     if (! (ncdims[rank] = dataFile.add_dim("num_data_points_per_sample", dims[rank])) )
# Line 705  DataExpanded::dump(const std::string fil Line 785  DataExpanded::dump(const std::string fil
785     if (! (ncdims[rank+1] = dataFile.add_dim("num_samples", dims[rank+1])) )     if (! (ncdims[rank+1] = dataFile.add_dim("num_samples", dims[rank+1])) )
786              throw DataException("Error - DataExpanded:: appending num_sample to netCDF file failed.");              throw DataException("Error - DataExpanded:: appending num_sample to netCDF file failed.");
787    
788     if (! ( ids = dataFile.add_var("id", ncInt, ncdims[rank+1])) )     if (getFunctionSpace().getNumSamples()>0)
789       {
790    
791         if (! ( ids = dataFile.add_var("id", ncInt, ncdims[rank+1])) )
792          throw DataException("Error - DataExpanded:: appending reference id to netCDF file failed.");          throw DataException("Error - DataExpanded:: appending reference id to netCDF file failed.");
793     const int* ids_p=getFunctionSpace().borrowSampleReferenceIDs();       const int* ids_p=getFunctionSpace().borrowSampleReferenceIDs();
794     if (! (ids->put(ids_p,dims[rank+1])) )       if (! (ids->put(ids_p,dims[rank+1])) )
795          throw DataException("Error - DataExpanded:: copy reference id  to netCDF buffer failed.");          throw DataException("Error - DataExpanded:: copy reference id  to netCDF buffer failed.");
796         if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
    if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )  
797          throw DataException("Error - DataExpanded:: appending variable to netCDF file failed.");          throw DataException("Error - DataExpanded:: appending variable to netCDF file failed.");
798     if (! (var->put(d_ptr,dims)) )       if (! (var->put(d_ptr,dims)) )
799          throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");          throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");
800       }
801    #ifdef PASO_MPI
802       if (mpi_iam<mpi_num-1) MPI_Send(&ndims, 0, MPI_INT, mpi_iam+1, 81801, MPI_COMM_WORLD);
803    #endif
804     #else     #else
805     throw DataException("Error - DataExpanded:: dump is not configured with netCDF. Please contact your installation manager.");     throw DataException("Error - DataExpanded:: dump is not configured with netCDF. Please contact your installation manager.");
806     #endif     #endif
807  }  }
808    
809  void  void  
810  DataExpanded::setTaggedValue(int tagKey,  DataExpanded::setTaggedValue(int tagKey,
811                               const DataArrayView& value)             const DataTypes::ShapeType& pointshape,
812                   const DataTypes::ValueType& value,
813               int dataOffset)
814  {  {
815    int numSamples = getNumSamples();    int numSamples = getNumSamples();
816    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
817    int sampleNo,dataPointNo, i;    int sampleNo,dataPointNo, i;
818    DataArrayView& thisView=getPointDataView();    DataTypes::ValueType::size_type n = getNoValues();
819    DataArrayView::ValueType::size_type n = thisView.noValues();    double* p;
820    double* p,*in=&(value.getData()[0]);    const double* in=&value[0+dataOffset];
821        
822    if (value.noValues() != n) {    if (value.size() != n) {
823      throw DataException("Error - DataExpanded::setTaggedValue: number of input values does not match number of values per data points.");      throw DataException("Error - DataExpanded::setTaggedValue: number of input values does not match number of values per data points.");
824    }    }
825    
# Line 740  DataExpanded::setTaggedValue(int tagKey, Line 828  DataExpanded::setTaggedValue(int tagKey,
828      if (getFunctionSpace().getTagFromSampleNo(sampleNo) == tagKey ) {      if (getFunctionSpace().getTagFromSampleNo(sampleNo) == tagKey ) {
829          for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {          for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
830              p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);              p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);
831              for (int i=0; i<n ;++i) p[i]=in[i];              for (i=0; i<n ;++i) p[i]=in[i];
832          }          }
833      }      }
834    }    }
835  }  }
836    
837    
838    void
839    DataExpanded::reorderByReferenceIDs(int *reference_ids)
840    {
841      int numSamples = getNumSamples();
842      DataTypes::ValueType::size_type n = getNoValues() * getNumDPPSample();
843      int sampleNo, sampleNo2,i;
844      double* p,*p2;
845      register double rtmp;
846      FunctionSpace fs=getFunctionSpace();
847    
848      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
849         const int id_in=reference_ids[sampleNo];
850         const int id=fs.getReferenceIDOfSample(sampleNo);
851         if (id!=id_in) {
852             bool matched=false;
853             for (sampleNo2 = sampleNo+1; sampleNo2 < numSamples; sampleNo2++) {
854                  if (id == reference_ids[sampleNo2]) {
855                     p=&(m_data[getPointOffset(sampleNo,0)]);
856                     p2=&(m_data[getPointOffset(sampleNo2,0)]);
857                     for (i=0; i<n ;i++) {
858                             rtmp=p[i];
859                             p[i]=p2[i];
860                             p2[i]=rtmp;
861                     }
862                     reference_ids[sampleNo]=id;
863                     reference_ids[sampleNo2]=id_in;
864                     matched=true;
865                     break;
866                  }
867             }
868             if (! matched) {
869                throw DataException("Error - DataExpanded::reorderByReferenceIDs: unable to reorder sample data by reference ids");
870             }
871         }
872       }
873    }
874    
875    DataTypes::ValueType&
876    DataExpanded::getVector()
877    {
878        return m_data.getData();
879    }
880    
881    const DataTypes::ValueType&
882    DataExpanded::getVector() const
883    {
884        return m_data.getData();
885    }
886    
887  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.1358  
changed lines
  Added in v.1872

  ViewVC Help
Powered by ViewVC 1.1.26