/[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 775 by ksteube, Mon Jul 10 04:00:08 2006 UTC revision 1312 by ksteube, Mon Sep 24 06:18:44 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 154  DataExpanded::~DataExpanded() Line 159  DataExpanded::~DataExpanded()
159  {  {
160  }  }
161    
 void  
 DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)  
 {  
   if (getPointDataView().getRank()!=0) {  
     stringstream temp;  
     temp << "Error - Can only reshape Data with data points of rank 0. "  
          << "This Data has data points with rank: "  
          << getPointDataView().getRank();  
     throw DataException(temp.str());  
   }  
   //  
   // create the new DataBlocks2D data array, and a corresponding DataArrayView  
   DataBlocks2D newData(getNumSamples(),getNumDPPSample(),DataArrayView::noValues(shape));  
   DataArrayView newView(newData.getData(),shape);  
   //  
   // Copy the original data to every value for the new shape  
   int i,j;  
   int nRows=m_data.getNumRows();  
   int nCols=m_data.getNumCols();  
   #pragma omp parallel for private(i,j) schedule(static)  
   for (i=0;i<nRows;i++) {  
     for (j=0;j<nCols;j++) {  
       // NOTE: An exception may be thown from this call if  
       // DOASSERT is on which of course will play  
       // havoc with the omp threads. Run single threaded  
       // if using DOASSERT.  
       newView.copy(newData.index(i,j),m_data(i,j));  
     }  
   }  
   // swap the new data array for the original  
   m_data.Swap(newData);  
   // set the corresponding DataArrayView  
   DataArrayView temp(m_data.getData(),shape);  
   setPointDataView(temp);  
 }  
   
162  DataAbstract*  DataAbstract*
163  DataExpanded::getSlice(const DataArrayView::RegionType& region) const  DataExpanded::getSlice(const DataArrayView::RegionType& region) const
164  {  {
# Line 289  string Line 258  string
258  DataExpanded::toString() const  DataExpanded::toString() const
259  {  {
260    stringstream temp;    stringstream temp;
261      FunctionSpace fs=getFunctionSpace();
262    //    //
263    // create a temporary view as the offset will be changed    // create a temporary view as the offset will be changed
264    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
# Line 296  DataExpanded::toString() const Line 266  DataExpanded::toString() const
266      for (int j=0;j<m_data.getNumCols();j++) {      for (int j=0;j<m_data.getNumCols();j++) {
267        tempView.setOffset(m_data.index(i,j));        tempView.setOffset(m_data.index(i,j));
268        stringstream suffix;        stringstream suffix;
269        suffix << "(" << i << "," << j << ")";        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";
270        temp << tempView.toString(suffix.str());        temp << tempView.toString(suffix.str());
271        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
272          temp << endl;          temp << endl;
# Line 327  DataExpanded::getLength() const Line 297  DataExpanded::getLength() const
297    return m_data.size();    return m_data.size();
298  }  }
299    
 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);  
 }  
   
300  int  int
301  DataExpanded::archiveData(ofstream& archiveFile,  DataExpanded::archiveData(ofstream& archiveFile,
302                            const DataArrayView::ValueType::size_type noValues) const                            const DataArrayView::ValueType::size_type noValues) const
# Line 408  DataExpanded::extractData(ifstream& arch Line 311  DataExpanded::extractData(ifstream& arch
311    return(m_data.extractData(archiveFile, noValues));    return(m_data.extractData(archiveFile, noValues));
312  }  }
313    
314    void
315    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {
316      //
317      // Get the number of samples and data-points per sample.
318      int numSamples = getNumSamples();
319      int numDataPointsPerSample = getNumDPPSample();
320      int dataPointRank = getPointDataView().getRank();
321      ShapeType dataPointShape = getPointDataView().getShape();
322      if (numSamples*numDataPointsPerSample > 0) {
323         //TODO: global error handling
324         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
325              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
326         }
327         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
328               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
329         }
330         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
331         if (dataPointRank==0) {
332             dataPointView()=value;
333         } else if (dataPointRank==1) {
334            for (int i=0; i<dataPointShape[0]; i++) {
335                dataPointView(i)=value;
336            }
337         } else if (dataPointRank==2) {
338            for (int i=0; i<dataPointShape[0]; i++) {
339               for (int j=0; j<dataPointShape[1]; j++) {
340                  dataPointView(i,j)=value;
341               }
342            }
343         } else if (dataPointRank==3) {
344            for (int i=0; i<dataPointShape[0]; i++) {
345               for (int j=0; j<dataPointShape[1]; j++) {
346                  for (int k=0; k<dataPointShape[2]; k++) {
347                     dataPointView(i,j,k)=value;
348                  }
349               }
350            }
351         } else if (dataPointRank==4) {
352             for (int i=0; i<dataPointShape[0]; i++) {
353               for (int j=0; j<dataPointShape[1]; j++) {
354                 for (int k=0; k<dataPointShape[2]; k++) {
355                   for (int l=0; l<dataPointShape[3]; l++) {
356                      dataPointView(i,j,k,l)=value;
357                   }
358                 }
359               }
360             }
361         }
362      }
363    }
364    void
365    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const boost::python::numeric::array& value) {
366      //
367      // Get the number of samples and data-points per sample.
368      int numSamples = getNumSamples();
369      int numDataPointsPerSample = getNumDPPSample();
370      int dataPointRank = getPointDataView().getRank();
371      ShapeType dataPointShape = getPointDataView().getShape();
372      //
373      // check rank:
374      if (value.getrank()!=dataPointRank)
375           throw DataException("Rank of numarray does not match Data object rank");
376      if (numSamples*numDataPointsPerSample > 0) {
377         //TODO: global error handling
378         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
379              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
380         }
381         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
382               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
383         }
384         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
385         if (dataPointRank==0) {
386             dataPointView()=extract<double>(value[0]);
387         } else if (dataPointRank==1) {
388            for (int i=0; i<dataPointShape[0]; i++) {
389                dataPointView(i)=extract<double>(value[i]);
390            }
391         } else if (dataPointRank==2) {
392            for (int i=0; i<dataPointShape[0]; i++) {
393               for (int j=0; j<dataPointShape[1]; j++) {
394                  dataPointView(i,j)=extract<double>(value[i][j]);
395               }
396            }
397         } else if (dataPointRank==3) {
398            for (int i=0; i<dataPointShape[0]; i++) {
399               for (int j=0; j<dataPointShape[1]; j++) {
400                  for (int k=0; k<dataPointShape[2]; k++) {
401                     dataPointView(i,j,k)=extract<double>(value[i][j][k]);
402                  }
403               }
404            }
405         } else if (dataPointRank==4) {
406             for (int i=0; i<dataPointShape[0]; i++) {
407               for (int j=0; j<dataPointShape[1]; j++) {
408                 for (int k=0; k<dataPointShape[2]; k++) {
409                   for (int l=0; l<dataPointShape[3]; l++) {
410                      dataPointView(i,j,k,l)=extract<double>(value[i][j][k][l]);
411                   }
412                 }
413               }
414             }
415         }
416      }
417    }
418  void  void
419  DataExpanded::copyAll(const boost::python::numeric::array& value) {  DataExpanded::copyAll(const boost::python::numeric::array& value) {
420    //    //
# Line 503  DataExpanded::nonsymmetric(DataAbstract* Line 510  DataExpanded::nonsymmetric(DataAbstract*
510    }    }
511  }  }
512  void  void
513  DataExpanded::matrixtrace(DataAbstract* ev, int axis_offset)  DataExpanded::trace(DataAbstract* ev, int axis_offset)
514  {  {
515    int sampleNo,dataPointNo;    int sampleNo,dataPointNo;
516    int numSamples = getNumSamples();    int numSamples = getNumSamples();
517    int numDataPointsPerSample = getNumDPPSample();    int numDataPointsPerSample = getNumDPPSample();
518    DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);    DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
519    if (temp_ev==0) {    if (temp_ev==0) {
520      throw DataException("Error - DataExpanded::matrixtrace: casting to DataExpanded failed (propably a programming error).");      throw DataException("Error - DataExpanded::trace: casting to DataExpanded failed (propably a programming error).");
521    }    }
522    DataArrayView& thisView=getPointDataView();    DataArrayView& thisView=getPointDataView();
523    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();
524    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)    #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
525    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {    for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
526      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {      for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
527           DataArrayView::matrixtrace(thisView,getPointOffset(sampleNo,dataPointNo),           DataArrayView::trace(thisView,getPointOffset(sampleNo,dataPointNo),
528                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);                                      evView,ev->getPointOffset(sampleNo,dataPointNo),axis_offset);
529      }      }
530    }    }
531  }  }
532    
533  void  void
534  DataExpanded::transpose(DataAbstract* ev, int axis_offset)  DataExpanded::transpose(DataAbstract* ev, int axis_offset)
535  {  {
# Line 542  DataExpanded::transpose(DataAbstract* ev Line 550  DataExpanded::transpose(DataAbstract* ev
550      }      }
551    }    }
552  }  }
553    
554    void
555    DataExpanded::swapaxes(DataAbstract* ev, int axis0, int axis1)
556    {
557      int sampleNo,dataPointNo;
558      int numSamples = getNumSamples();
559      int numDataPointsPerSample = getNumDPPSample();
560      DataExpanded* temp_ev=dynamic_cast<DataExpanded*>(ev);
561      if (temp_ev==0) {
562        throw DataException("Error - DataExpanded::swapaxes: casting to DataExpanded failed (propably a programming error).");
563      }
564      DataArrayView& thisView=getPointDataView();
565      DataArrayView& evView=ev->getPointDataView();
566      #pragma omp parallel for private(sampleNo,dataPointNo) schedule(static)
567      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
568        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
569             DataArrayView::swapaxes(thisView,getPointOffset(sampleNo,dataPointNo),
570                                        evView,ev->getPointOffset(sampleNo,dataPointNo),axis0,axis1);
571        }
572      }
573    }
574  void  void
575  DataExpanded::eigenvalues(DataAbstract* ev)  DataExpanded::eigenvalues(DataAbstract* ev)
576  {  {
# Line 590  DataExpanded::eigenvalues_and_eigenvecto Line 619  DataExpanded::eigenvalues_and_eigenvecto
619    }    }
620  }  }
621    
622    void
623    DataExpanded::setToZero(){
624      int numSamples = getNumSamples();
625      int numDataPointsPerSample = getNumDPPSample();
626      DataArrayView& thisView=getPointDataView();
627      DataArrayView::ValueType::size_type n = thisView.noValues();
628      double* p;
629      int  sampleNo,dataPointNo, i;
630      #pragma omp parallel for private(sampleNo,dataPointNo,p,i) schedule(static)
631      for (sampleNo = 0; sampleNo < numSamples; sampleNo++) {
632        for (dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
633            p=&(m_data[getPointOffset(sampleNo,dataPointNo)]);
634            for (int i=0; i<n ;++i) p[i]=0.;
635        }
636      }
637    }
638    
639    
640    void
641    DataExpanded::dump(const std::string fileName) const
642    {
643       #ifdef PASO_MPI
644       throw DataException("Error - DataExpanded:: dump is not implemented for MPI yet.");
645       #endif
646       #ifdef USE_NETCDF
647       const int ldims=2+DataArrayView::maxRank;
648       const NcDim* ncdims[ldims];
649       NcVar *var, *ids;
650       int rank = getPointDataView().getRank();
651       int type=  getFunctionSpace().getTypeCode();
652       int ndims =0;
653       long dims[ldims];
654       const double* d_ptr=&(m_data[0]);
655       DataArrayView::ShapeType shape = getPointDataView().getShape();
656    
657       // netCDF error handler
658       NcError err(NcError::verbose_nonfatal);
659       // Create the file.
660       NcFile dataFile(fileName.c_str(), NcFile::Replace);
661       // check if writing was successful
662       if (!dataFile.is_valid())
663            throw DataException("Error - DataExpanded:: opening of netCDF file for output failed.");
664       if (!dataFile.add_att("type_id",2) )
665            throw DataException("Error - DataExpanded:: appending data type to netCDF file failed.");
666       if (!dataFile.add_att("rank",rank) )
667            throw DataException("Error - DataExpanded:: appending rank attribute to netCDF file failed.");
668       if (!dataFile.add_att("function_space_type",type))
669            throw DataException("Error - DataExpanded:: appending function space attribute to netCDF file failed.");
670       ndims=rank+2;
671       if ( rank >0 ) {
672           dims[0]=shape[0];
673           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
674                throw DataException("Error - DataExpanded:: appending ncdimsion 0 to netCDF file failed.");
675       }
676       if ( rank >1 ) {
677           dims[1]=shape[1];
678           if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
679                throw DataException("Error - DataExpanded:: appending ncdimsion 1 to netCDF file failed.");
680       }
681       if ( rank >2 ) {
682           dims[2]=shape[2];
683           if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
684                throw DataException("Error - DataExpanded:: appending ncdimsion 2 to netCDF file failed.");
685       }
686       if ( rank >3 ) {
687           dims[3]=shape[3];
688           if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
689                throw DataException("Error - DataExpanded:: appending ncdimsion 3 to netCDF file failed.");
690       }
691       dims[rank]=getFunctionSpace().getNumDataPointsPerSample();
692       if (! (ncdims[rank] = dataFile.add_dim("num_data_points_per_sample", dims[rank])) )
693                throw DataException("Error - DataExpanded:: appending num_data_points_per_sample to netCDF file failed.");
694       dims[rank+1]=getFunctionSpace().getNumSamples();
695       if (! (ncdims[rank+1] = dataFile.add_dim("num_samples", dims[rank+1])) )
696                throw DataException("Error - DataExpanded:: appending num_sample to netCDF file failed.");
697    
698       if (! ( ids = dataFile.add_var("id", ncInt, ncdims[rank+1])) )
699            throw DataException("Error - DataExpanded:: appending reference id to netCDF file failed.");
700       const int* ids_p=getFunctionSpace().borrowSampleReferenceIDs();
701       if (! (ids->put(ids_p,dims[rank+1])) )
702            throw DataException("Error - DataExpanded:: copy reference id  to netCDF buffer failed.");
703    
704       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
705            throw DataException("Error - DataExpanded:: appending variable to netCDF file failed.");
706       if (! (var->put(d_ptr,dims)) )
707            throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");
708       #else
709       throw DataException("Error - DataExpanded:: dump is not configured with netCDF. Please contact your installation manager.");
710       #endif
711    }
712  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.775  
changed lines
  Added in v.1312

  ViewVC Help
Powered by ViewVC 1.1.26