/[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 967 by gross, Tue Feb 13 09:40:12 2007 UTC
# Line 15  Line 15 
15  #include "DataException.h"  #include "DataException.h"
16  #include "DataConstant.h"  #include "DataConstant.h"
17  #include "DataTagged.h"  #include "DataTagged.h"
18    #include <netcdfcpp.h>
19    
20  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
21    
# Line 261  DataExpanded::toString() const Line 262  DataExpanded::toString() const
262      for (int j=0;j<m_data.getNumCols();j++) {      for (int j=0;j<m_data.getNumCols();j++) {
263        tempView.setOffset(m_data.index(i,j));        tempView.setOffset(m_data.index(i,j));
264        stringstream suffix;        stringstream suffix;
265        suffix << "( id: " << i << ", ref: " << fs.getReferenceNoFromSampleNo(i) << ", pnt: " << j << ")";        suffix << "( id: " << i << ", ref: " << fs.getReferenceIDOfSample(i) << ", pnt: " << j << ")";
266        temp << tempView.toString(suffix.str());        temp << tempView.toString(suffix.str());
267        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {        if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
268          temp << endl;          temp << endl;
# Line 292  DataExpanded::getLength() const Line 293  DataExpanded::getLength() const
293    return m_data.size();    return m_data.size();
294  }  }
295    
 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);  
 }  
   
296  int  int
297  DataExpanded::archiveData(ofstream& archiveFile,  DataExpanded::archiveData(ofstream& archiveFile,
298                            const DataArrayView::ValueType::size_type noValues) const                            const DataArrayView::ValueType::size_type noValues) const
# Line 373  DataExpanded::extractData(ifstream& arch Line 307  DataExpanded::extractData(ifstream& arch
307    return(m_data.extractData(archiveFile, noValues));    return(m_data.extractData(archiveFile, noValues));
308  }  }
309    
310    void
311    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const double value) {
312      //
313      // Get the number of samples and data-points per sample.
314      int numSamples = getNumSamples();
315      int numDataPointsPerSample = getNumDPPSample();
316      int dataPointRank = getPointDataView().getRank();
317      ShapeType dataPointShape = getPointDataView().getShape();
318      if (numSamples*numDataPointsPerSample > 0) {
319         //TODO: global error handling
320         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
321              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
322         }
323         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
324               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
325         }
326         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
327         if (dataPointRank==0) {
328             dataPointView()=value;
329         } else if (dataPointRank==1) {
330            for (int i=0; i<dataPointShape[0]; i++) {
331                dataPointView(i)=value;
332            }
333         } else if (dataPointRank==2) {
334            for (int i=0; i<dataPointShape[0]; i++) {
335               for (int j=0; j<dataPointShape[1]; j++) {
336                  dataPointView(i,j)=value;
337               }
338            }
339         } else if (dataPointRank==3) {
340            for (int i=0; i<dataPointShape[0]; i++) {
341               for (int j=0; j<dataPointShape[1]; j++) {
342                  for (int k=0; k<dataPointShape[2]; k++) {
343                     dataPointView(i,j,k)=value;
344                  }
345               }
346            }
347         } else if (dataPointRank==4) {
348             for (int i=0; i<dataPointShape[0]; i++) {
349               for (int j=0; j<dataPointShape[1]; j++) {
350                 for (int k=0; k<dataPointShape[2]; k++) {
351                   for (int l=0; l<dataPointShape[3]; l++) {
352                      dataPointView(i,j,k,l)=value;
353                   }
354                 }
355               }
356             }
357         }
358      }
359    }
360    void
361    DataExpanded::copyToDataPoint(const int sampleNo, const int dataPointNo, const boost::python::numeric::array& value) {
362      //
363      // Get the number of samples and data-points per sample.
364      int numSamples = getNumSamples();
365      int numDataPointsPerSample = getNumDPPSample();
366      int dataPointRank = getPointDataView().getRank();
367      ShapeType dataPointShape = getPointDataView().getShape();
368      //
369      // check rank:
370      if (value.getrank()!=dataPointRank)
371           throw DataException("Rank of numarray does not match Data object rank");
372      if (numSamples*numDataPointsPerSample > 0) {
373         //TODO: global error handling
374         if ((sampleNo >= numSamples) || (sampleNo < 0 )) {
375              throw DataException("Error - DataExpanded::copyDataPoint invalid sampleNo.");
376         }
377         if ((dataPointNo >= numDataPointsPerSample) || (dataPointNo < 0)) {
378               throw DataException("Error - DataExpanded::copyDataPoint invalid dataPointNoInSample.");
379         }
380         DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
381         if (dataPointRank==0) {
382             dataPointView()=extract<double>(value[0]);
383         } else if (dataPointRank==1) {
384            for (int i=0; i<dataPointShape[0]; i++) {
385                dataPointView(i)=extract<double>(value[i]);
386            }
387         } else if (dataPointRank==2) {
388            for (int i=0; i<dataPointShape[0]; i++) {
389               for (int j=0; j<dataPointShape[1]; j++) {
390                  dataPointView(i,j)=extract<double>(value[i][j]);
391               }
392            }
393         } else if (dataPointRank==3) {
394            for (int i=0; i<dataPointShape[0]; i++) {
395               for (int j=0; j<dataPointShape[1]; j++) {
396                  for (int k=0; k<dataPointShape[2]; k++) {
397                     dataPointView(i,j,k)=extract<double>(value[i][j][k]);
398                  }
399               }
400            }
401         } else if (dataPointRank==4) {
402             for (int i=0; i<dataPointShape[0]; i++) {
403               for (int j=0; j<dataPointShape[1]; j++) {
404                 for (int k=0; k<dataPointShape[2]; k++) {
405                   for (int l=0; l<dataPointShape[3]; l++) {
406                      dataPointView(i,j,k,l)=extract<double>(value[i][j][k][l]);
407                   }
408                 }
409               }
410             }
411         }
412      }
413    }
414  void  void
415  DataExpanded::copyAll(const boost::python::numeric::array& value) {  DataExpanded::copyAll(const boost::python::numeric::array& value) {
416    //    //
# Line 577  DataExpanded::eigenvalues_and_eigenvecto Line 615  DataExpanded::eigenvalues_and_eigenvecto
615    }    }
616  }  }
617    
618    void
619    DataExpanded::dump(const std::string fileName) const
620    {
621       #ifdef PASO_MPI
622       throw DataException("Error - DataExpanded:: dump is not implemented for MPI yet.");
623       #endif
624       const int ldims=2+DataArrayView::maxRank;
625       const NcDim* ncdims[ldims];
626       NcVar *var, *ids;
627       int rank = getPointDataView().getRank();
628       int type=  getFunctionSpace().getTypeCode();
629       int ndims =0;
630       long dims[ldims];
631       DataArrayView::ShapeType shape = getPointDataView().getShape();
632    
633       // netCDF error handler
634       NcError err(NcError::verbose_nonfatal);
635       // Create the file.
636       NcFile dataFile(fileName.c_str(), NcFile::Replace);
637       // check if writing was successful
638       if (!dataFile.is_valid())
639            throw DataException("Error - DataExpanded:: opening of netCDF file for output failed.");
640       if (!dataFile.add_att("type","expanded") )
641            throw DataException("Error - DataExpanded:: appending data type to netCDF file failed.");
642       if (!dataFile.add_att("rank",rank) )
643            throw DataException("Error - DataExpanded:: appending rank attribute to netCDF file failed.");
644       if (!dataFile.add_att("function_space_type",type))
645            throw DataException("Error - DataExpanded:: appending function space attribute to netCDF file failed.");
646       ndims=rank+2;
647       if ( rank >0 ) {
648           dims[0]=shape[0];
649           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
650                throw DataException("Error - DataExpanded:: appending ncdimsion 0 to netCDF file failed.");
651       }
652       if ( rank >1 ) {
653           dims[1]=shape[1];
654           if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
655                throw DataException("Error - DataExpanded:: appending ncdimsion 1 to netCDF file failed.");
656       }
657       if ( rank >2 ) {
658           dims[2]=shape[2];
659           if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
660                throw DataException("Error - DataExpanded:: appending ncdimsion 2 to netCDF file failed.");
661       }
662       if ( rank >3 ) {
663           dims[3]=shape[3];
664           if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
665                throw DataException("Error - DataExpanded:: appending ncdimsion 3 to netCDF file failed.");
666       }
667       dims[rank]=getFunctionSpace().getNumDataPointsPerSample();
668       if (! (ncdims[rank] = dataFile.add_dim("num_data_points_per_sample", dims[rank])) )
669                throw DataException("Error - DataExpanded:: appending num_data_points_per_sample to netCDF file failed.");
670       dims[rank+1]=getFunctionSpace().getNumSamples();
671       if (! (ncdims[rank+1] = dataFile.add_dim("num_samples", dims[rank+1])) )
672                throw DataException("Error - DataExpanded:: appending num_sample to netCDF file failed.");
673    
674       if (! ( ids = dataFile.add_var("id", ncInt, ncdims[rank+1])) )
675            throw DataException("Error - DataExpanded:: appending reference id to netCDF file failed.");
676       const int* ids_p=getFunctionSpace().borrowSampleReferenceIDs();
677       if (! (ids->put(ids_p,dims[rank+1])) )
678            throw DataException("Error - DataExpanded:: copy reference id  to netCDF buffer failed.");
679    
680       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
681            throw DataException("Error - DataExpanded:: appending variable to netCDF file failed.");
682       if (! (var->put(&m_data[0],dims)) )
683            throw DataException("Error - DataExpanded:: copy data to netCDF buffer failed.");
684    }
685    
686    
687  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26