/[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 856 by gross, Tue Sep 26 01:00:36 2006 UTC revision 1358 by gross, Wed Dec 5 03:41:06 2007 UTC
# Line 1  Line 1 
1  // $Id$  
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 "DataExpanded.h"  #include "DataExpanded.h"
17  #include "DataException.h"  #include "DataException.h"
18  #include "DataConstant.h"  #include "DataConstant.h"
19  #include "DataTagged.h"  #include "DataTagged.h"
20    #ifdef USE_NETCDF
21    #include <netcdfcpp.h>
22    #endif
23    
24  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
25    
# Line 155  DataExpanded::~DataExpanded() Line 160  DataExpanded::~DataExpanded()
160  }  }
161    
162  DataAbstract*  DataAbstract*
163  DataExpanded::getSlice(const DataArrayView::RegionType& region) const  DataExpanded::getSlice(const DataArrayView::RegionType& region) const
164  {  {
165    return new DataExpanded(*this,region);    return new DataExpanded(*this,region);
166  }  }
167    
168  void  void
169  DataExpanded::setSlice(const DataAbstract* value,  DataExpanded::setSlice(const DataAbstract* value,
170                         const DataArrayView::RegionType& region)                         const DataArrayView::RegionType& region)
171  {  {
172    const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);    const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
173    if (tempDataExp==0) {    if (tempDataExp==0) {
# Line 198  DataExpanded::setSlice(const DataAbstrac Line 203  DataExpanded::setSlice(const DataAbstrac
203  }  }
204    
205  void  void
206  DataExpanded::copy(const DataArrayView& value)  DataExpanded::copy(const DataArrayView& value)
207  {  {
208    //    //
209    // copy a single value to every data point in this object    // copy a single value to every data point in this object
# Line 208  DataExpanded::copy(const DataArrayView& Line 213  DataExpanded::copy(const DataArrayView&
213    #pragma omp parallel for private(i,j) schedule(static)    #pragma omp parallel for private(i,j) schedule(static)
214    for (i=0;i<nRows;i++) {    for (i=0;i<nRows;i++) {
215      for (j=0;j<nCols;j++) {      for (j=0;j<nCols;j++) {
216        // NOTE: An exception may be thown from this call if        // NOTE: An exception may be thown from this call if
217        // DOASSERT is on which of course will play        // DOASSERT is on which of course will play
218        // havoc with the omp threads. Run single threaded        // havoc with the omp threads. Run single threaded
219        // if using DOASSERT.        // if using DOASSERT.
220        getPointDataView().copy(m_data.index(i,j),value);        getPointDataView().copy(getPointOffset(i,j),value);
221      }      }
222    }    }
223  }  }
224    
225  void  void
226  DataExpanded::copy(const boost::python::numeric::array& value)  DataExpanded::copy(const boost::python::numeric::array& value)
227  {  {
228    //  
229    // first convert the numarray into a DataArray object    // extract the shape of the numarray
230    DataArray temp(value);    DataArrayView::ShapeType tempShape;
231      for (int i=0; i < value.getrank(); i++) {
232        tempShape.push_back(extract<int>(value.getshape()[i]));
233      }
234    
235      // get the space for the data vector
236      int len = DataArrayView::noValues(tempShape);
237      DataVector temp_data(len, 0.0, len);
238      DataArrayView temp_dataView(temp_data, tempShape);
239      temp_dataView.copy(value);
240    
241    //    //
242    // check the input shape matches this shape    // check the input shape matches this shape
243    if (!getPointDataView().checkShape(temp.getView().getShape())) {    if (!getPointDataView().checkShape(temp_dataView.getShape())) {
244      throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException(getPointDataView().createShapeErrorMessage(
245                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",
246                          temp.getView().getShape()));                          temp_dataView.getShape()));
247    }    }
248    //    //
249    // now copy over the data    // now copy over the data
250    copy(temp.getView());    copy(temp_dataView);
251  }  }
252    
253  void  void
# Line 259  DataExpanded::toString() const Line 274  DataExpanded::toString() const
274    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
275    for (int i=0;i<m_data.getNumRows();i++) {    for (int i=0;i<m_data.getNumRows();i++) {
276      for (int j=0;j<m_data.getNumCols();j++) {      for (int j=0;j<m_data.getNumCols();j++) {
277        tempView.setOffset(m_data.index(i,j));        tempView.setOffset(getPointOffset(i,j));
278        stringstream suffix;        stringstream suffix;
279        suffix << "( id: " << i << ", ref: " << fs.getReferenceNoFromSampleNo(i) << ", pnt: " << j << ")";        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";
280        temp << tempView.toString(suffix.str());        temp << tempView.toString(suffix.str());
281        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
282          temp << endl;          temp << endl;
# Line 282  DataArrayView Line 297  DataArrayView
297  DataExpanded::getDataPoint(int sampleNo,  DataExpanded::getDataPoint(int sampleNo,
298                             int dataPointNo)                             int dataPointNo)
299  {  {
300    DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo));    DataArrayView temp(m_data.getData(),getPointDataView().getShape(),getPointOffset(sampleNo,dataPointNo));
301    return temp;    return temp;
302  }  }
303    
# Line 292  DataExpanded::getLength() const Line 307  DataExpanded::getLength() const
307    return m_data.size();    return m_data.size();
308  }  }
309    
 void  
 DataExpanded::setRefValue(int ref,  
                           const DataArray& value)  
 {  
   //  
   // Get the number of samples and data-points per sample.  
   int numSamples = getNumSamples();  
   int numDPPSample = getNumDPPSample();  
   
   //  
   // Determine the sample number which corresponds to this reference number.  
   int sampleNo = -1;  
   int tempRef = -1;  
   for (int n=0; n<numSamples; n++) {  
     tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);  
     if (tempRef == ref) {  
       sampleNo = n;  
       break;  
     }  
   }  
   if (sampleNo == -1) {  
     throw DataException("DataExpanded::setRefValue error: invalid ref number supplied.");  
   }  
   
   for (int n=0; n<numDPPSample; n++) {  
     //  
     // Get *each* data-point in the sample in turn.  
     DataArrayView pointView = getDataPoint(sampleNo, n);  
     //  
     // Assign the values in the DataArray to this data-point.  
     pointView.copy(value.getView());  
   }  
 }  
   
 void  
 DataExpanded::getRefValue(int ref,  
                           DataArray& value)  
 {  
   //  
   // Get the number of samples and data-points per sample.  
   int numSamples = getNumSamples();  
   int numDPPSample = getNumDPPSample();  
   
   //  
   // Determine the sample number which corresponds to this reference number  
   int sampleNo = -1;  
   int tempRef = -1;  
   for (int n=0; n<numSamples; n++) {  
     tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);  
     if (tempRef == ref) {  
       sampleNo = n;  
       break;  
     }  
   }  
   if (sampleNo == -1) {  
     throw DataException("DataExpanded::getRefValue error: invalid ref number supplied.");  
   }  
   
   //  
   // Get the *first* data-point associated with this sample number.  
   DataArrayView pointView = getDataPoint(sampleNo, 0);  
   
   //  
   // Load the values from this data-point into the DataArray  
   value.getView().copy(pointView);  
 }  
   
310  int  int
311  DataExpanded::archiveData(ofstream& archiveFile,  DataExpanded::archiveData(ofstream& archiveFile,
312                            const DataArrayView::ValueType::size_type noValues) const                            const DataArrayView::ValueType::size_type noValues) const
# Line 374  DataExpanded::extractData(ifstream& arch Line 322  DataExpanded::extractData(ifstream& arch
322  }  }
323    
324  void  void
325    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {
326      //
327      // Get the number of samples and data-points per sample.
328      int numSamples = getNumSamples();
329      int numDataPointsPerSample = getNumDPPSample();
330      int dataPointRank = getPointDataView().getRank();
331      ShapeType dataPointShape = getPointDataView().getShape();
332      if (numSamples*numDataPointsPerSample > 0) {
333         //TODO: global error handling
334         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
335              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
336         }
337         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
338               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
339         }
340         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
341         if (dataPointRank==0) {
342             dataPointView()=value;
343         } else if (dataPointRank==1) {
344            for (int i=0; i<dataPointShape[0]; i++) {
345                dataPointView(i)=value;
346            }
347         } else if (dataPointRank==2) {
348            for (int i=0; i<dataPointShape[0]; i++) {
349               for (int j=0; j<dataPointShape[1]; j++) {
350                  dataPointView(i,j)=value;
351               }
352            }
353         } else if (dataPointRank==3) {
354            for (int i=0; i<dataPointShape[0]; i++) {
355               for (int j=0; j<dataPointShape[1]; j++) {
356                  for (int k=0; k<dataPointShape[2]; k++) {
357                     dataPointView(i,j,k)=value;
358                  }
359               }
360            }
361         } else if (dataPointRank==4) {
362             for (int i=0; i<dataPointShape[0]; i++) {
363               for (int j=0; j<dataPointShape[1]; j++) {
364                 for (int k=0; k<dataPointShape[2]; k++) {
365                   for (int l=0; l<dataPointShape[3]; l++) {
366                      dataPointView(i,j,k,l)=value;
367                   }
368                 }
369               }
370             }
371         }
372      }
373    }
374    void
375    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const boost::python::numeric::array& value) {
376      //
377      // Get the number of samples and data-points per sample.
378      int numSamples = getNumSamples();
379      int numDataPointsPerSample = getNumDPPSample();
380      int dataPointRank = getPointDataView().getRank();
381      ShapeType dataPointShape = getPointDataView().getShape();
382      //
383      // check rank:
384      if (value.getrank()!=dataPointRank)
385           throw DataException("Rank of numarray does not match Data object rank");
386      if (numSamples*numDataPointsPerSample > 0) {
387         //TODO: global error handling
388         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
389              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
390         }
391         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
392               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
393         }
394         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
395         if (dataPointRank==0) {
396             dataPointView()=extract<double>(value[0]);
397         } else if (dataPointRank==1) {
398            for (int i=0; i<dataPointShape[0]; i++) {
399                dataPointView(i)=extract<double>(value[i]);
400            }
401         } else if (dataPointRank==2) {
402            for (int i=0; i<dataPointShape[0]; i++) {
403               for (int j=0; j<dataPointShape[1]; j++) {
404                  dataPointView(i,j)=extract<double>(value[i][j]);
405               }
406            }
407         } else if (dataPointRank==3) {
408            for (int i=0; i<dataPointShape[0]; i++) {
409               for (int j=0; j<dataPointShape[1]; j++) {
410                  for (int k=0; k<dataPointShape[2]; k++) {
411                     dataPointView(i,j,k)=extract<double>(value[i][j][k]);
412                  }
413               }
414            }
415         } else if (dataPointRank==4) {
416             for (int i=0; i<dataPointShape[0]; i++) {
417               for (int j=0; j<dataPointShape[1]; j++) {
418                 for (int k=0; k<dataPointShape[2]; k++) {
419                   for (int l=0; l<dataPointShape[3]; l++) {
420                      dataPointView(i,j,k,l)=extract<double>(value[i][j][k][l]);
421                   }
422                 }
423               }
424             }
425         }
426      }
427    }
428    void
429  DataExpanded::copyAll(const boost::python::numeric::array& value) {  DataExpanded::copyAll(const boost::python::numeric::array& value) {
430    //    //
431    // Get the number of samples and data-points per sample.    // Get the number of samples and data-points per sample.
# Line 577  DataExpanded::eigenvalues_and_eigenvecto Line 629  DataExpanded::eigenvalues_and_eigenvecto
629    }    }
630  }  }
631    
632    void
633    DataExpanded::setToZero(){
634      int numSamples = getNumSamples();
635      int numDataPointsPerSample = getNumDPPSample();
636      DataArrayView& thisView=getPointDataView();
637      DataArrayView::ValueType::size_type n = thisView.noValues();
638      double* p;
639      int  sampleNo,dataPointNo, i;
640      #pragma omp parallel for private(sampleNo,dataPointNo,p,i) schedule(static)
641      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
642        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
643            p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);
644            for (int i=0; i<n ;++i) p[i]=0.;
645        }
646      }
647    }
648    
649    
650    void
651    DataExpanded::dump(const std::string fileName) const
652    {
653       #ifdef PASO_MPI
654       throw DataException("Error - DataExpanded:: dump is not implemented for MPI yet.");
655       #endif
656       #ifdef USE_NETCDF
657       const int ldims=2+DataArrayView::maxRank;
658       const NcDim* ncdims[ldims];
659       NcVar *var, *ids;
660       int rank = getPointDataView().getRank();
661       int type=  getFunctionSpace().getTypeCode();
662       int ndims =0;
663       long dims[ldims];
664       const double* d_ptr=&(m_data[0]);
665       DataArrayView::ShapeType shape = getPointDataView().getShape();
666    
667       // netCDF error handler
668       NcError err(NcError::verbose_nonfatal);
669       // Create the file.
670       NcFile dataFile(fileName.c_str(), NcFile::Replace);
671       // check if writing was successful
672       if (!dataFile.is_valid())
673            throw DataException("Error - DataExpanded:: opening of netCDF file for output failed.");
674       if (!dataFile.add_att("type_id",2) )
675            throw DataException("Error - DataExpanded:: appending data type to netCDF file failed.");
676       if (!dataFile.add_att("rank",rank) )
677            throw DataException("Error - DataExpanded:: appending rank attribute to netCDF file failed.");
678       if (!dataFile.add_att("function_space_type",type))
679            throw DataException("Error - DataExpanded:: appending function space attribute to netCDF file failed.");
680       ndims=rank+2;
681       if ( rank >0 ) {
682           dims[0]=shape[0];
683           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
684                throw DataException("Error - DataExpanded:: appending ncdimsion 0 to netCDF file failed.");
685       }
686       if ( rank >1 ) {
687           dims[1]=shape[1];
688           if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
689                throw DataException("Error - DataExpanded:: appending ncdimsion 1 to netCDF file failed.");
690       }
691       if ( rank >2 ) {
692           dims[2]=shape[2];
693           if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
694                throw DataException("Error - DataExpanded:: appending ncdimsion 2 to netCDF file failed.");
695       }
696       if ( rank >3 ) {
697           dims[3]=shape[3];
698           if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
699                throw DataException("Error - DataExpanded:: appending ncdimsion 3 to netCDF file failed.");
700       }
701       dims[rank]=getFunctionSpace().getNumDataPointsPerSample();
702       if (! (ncdims[rank] = dataFile.add_dim("num_data_points_per_sample", dims[rank])) )
703                throw DataException("Error - DataExpanded:: appending num_data_points_per_sample to netCDF file failed.");
704       dims[rank+1]=getFunctionSpace().getNumSamples();
705       if (! (ncdims[rank+1] = dataFile.add_dim("num_samples", dims[rank+1])) )
706                throw DataException("Error - DataExpanded:: appending num_sample to netCDF file failed.");
707    
708       if (! ( ids = dataFile.add_var("id", ncInt, ncdims[rank+1])) )
709            throw DataException("Error - DataExpanded:: appending reference id to netCDF file failed.");
710       const int* ids_p=getFunctionSpace().borrowSampleReferenceIDs();
711       if (! (ids->put(ids_p,dims[rank+1])) )
712            throw DataException("Error - DataExpanded:: copy reference id  to netCDF buffer failed.");
713    
714       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
715            throw DataException("Error - DataExpanded:: appending variable to netCDF file failed.");
716       if (! (var->put(d_ptr,dims)) )
717            throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");
718       #else
719       throw DataException("Error - DataExpanded:: dump is not configured with netCDF. Please contact your installation manager.");
720       #endif
721    }
722    
723    void
724    DataExpanded::setTaggedValue(int tagKey,
725                                 const DataArrayView& value)
726    {
727      int numSamples = getNumSamples();
728      int numDataPointsPerSample = getNumDPPSample();
729      int sampleNo,dataPointNo, i;
730      DataArrayView& thisView=getPointDataView();
731      DataArrayView::ValueType::size_type n = thisView.noValues();
732      double* p,*in=&(value.getData()[0]);
733      
734      if (value.noValues() != n) {
735        throw DataException("Error - DataExpanded::setTaggedValue: number of input values does not match number of values per data points.");
736      }
737    
738      #pragma omp parallel for private(sampleNo,dataPointNo,p,i) schedule(static)
739      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
740        if (getFunctionSpace().getTagFromSampleNo(sampleNo) == tagKey ) {
741            for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
742                p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);
743                for (int i=0; i<n ;++i) p[i]=in[i];
744            }
745        }
746      }
747    }
748    
749    
750  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26