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

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

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

trunk/esys2/escript/src/Data/DataConstant.cpp revision 119 by jgs, Tue Apr 12 04:45:05 2005 UTC trunk/escript/src/DataConstant.cpp revision 1312 by ksteube, Mon Sep 24 06:18:44 2007 UTC
# Line 1  Line 1 
 //$Id$  
 /*  
  ******************************************************************************  
  *                                                                            *  
  *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *  
  *                                                                            *  
  * This software is the property of ACcESS. No part of this code              *  
  * may be copied in any form or by any means without the expressed written    *  
  * consent of ACcESS.  Copying, use or modification of this software          *  
  * by any unauthorised person is illegal unless that person has a software    *  
  * license agreement with ACcESS.                                             *  
  *                                                                            *  
  ******************************************************************************  
 */  
1    
2  #include "escript/Data/DataConstant.h"  /* $Id$ */
3  #include "escript/Data/DataException.h"  
4    /*******************************************************
5     *
6     *           Copyright 2003-2007 by ACceSS MNRF
7     *       Copyright 2007 by University of Queensland
8     *
9     *                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 "DataConstant.h"
17    #include "DataException.h"
18  #include "esysUtils/EsysAssert.h"  #include "esysUtils/EsysAssert.h"
19    
20  #include <iostream>  #include <iostream>
21  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
22    #ifdef USE_NETCDF
23    #include <netcdfcpp.h>
24    #endif
25    
26  using namespace std;  using namespace std;
27    
# Line 53  DataConstant::DataConstant(const DataArr Line 56  DataConstant::DataConstant(const DataArr
56    
57  DataConstant::DataConstant(const DataConstant& other)  DataConstant::DataConstant(const DataConstant& other)
58    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
59  {  {  //
   //  
60    // copy the data in the correct format    // copy the data in the correct format
61    m_data=other.m_data;    m_data=other.m_data;
62    //    //
# Line 72  DataConstant::DataConstant(const DataCon Line 74  DataConstant::DataConstant(const DataCon
74    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
75    //    //
76    // allocate space for this new DataConstant's data    // allocate space for this new DataConstant's data
77    m_data.resize(DataArrayView::noValues(shape));    int len = DataArrayView::noValues(shape);
78      m_data.resize(len,0.,len);
79    //    //
80    // create a view of the data with the correct shape    // create a view of the data with the correct shape
81    DataArrayView tempView(m_data,shape);    DataArrayView tempView(m_data,shape);
# Line 109  DataConstant::getPointOffset(int sampleN Line 112  DataConstant::getPointOffset(int sampleN
112  {  {
113    EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),    EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
114                "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);                "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
115      //
116      // Whatever the coord's always return the same value as this is constant data.
117    return 0;    return 0;
118  }  }
119    
# Line 151  DataConstant::setSlice(const DataAbstrac Line 156  DataConstant::setSlice(const DataAbstrac
156    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
157      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
158    }    }
159    if (tempDataConst->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {    if (tempDataConst->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {
160      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (value->getPointDataView().createShapeErrorMessage(
161                  "Error - Couldn't copy slice due to shape mismatch.",shape));                  "Error - Couldn't copy slice due to shape mismatch.",shape));
162    }    }
# Line 159  DataConstant::setSlice(const DataAbstrac Line 164  DataConstant::setSlice(const DataAbstrac
164    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
165  }  }
166    
167    int
168    DataConstant::archiveData(ofstream& archiveFile,
169                              const DataArrayView::ValueType::size_type noValues) const
170    {
171      return(m_data.archiveData(archiveFile, noValues));
172    }
173    
174    int
175    DataConstant::extractData(ifstream& archiveFile,
176                              const DataArrayView::ValueType::size_type noValues)
177    {
178      return(m_data.extractData(archiveFile, noValues));
179    }
180    
181    void
182    DataConstant::symmetric(DataAbstract* ev)
183    {
184      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
185      if (temp_ev==0) {
186        throw DataException("Error - DataConstant::symmetric: casting to DataConstant failed (propably a programming error).");
187      }
188      DataArrayView& thisView=getPointDataView();
189      DataArrayView& evView=ev->getPointDataView();
190      DataArrayView::symmetric(thisView,0,evView,0);
191    }
192    
193    void
194    DataConstant::nonsymmetric(DataAbstract* ev)
195    {
196      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
197      if (temp_ev==0) {
198        throw DataException("Error - DataConstant::nonsymmetric: casting to DataConstant failed (propably a programming error).");
199      }
200      DataArrayView& thisView=getPointDataView();
201      DataArrayView& evView=ev->getPointDataView();
202      DataArrayView::nonsymmetric(thisView,0,evView,0);
203    }
204    
205    void
206    DataConstant::trace(DataAbstract* ev, int axis_offset)
207    {
208      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
209      if (temp_ev==0) {
210        throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");
211      }
212      DataArrayView& thisView=getPointDataView();
213      DataArrayView& evView=ev->getPointDataView();
214      DataArrayView::trace(thisView,0,evView,0,axis_offset);
215    }
216    
217    void
218    DataConstant::swapaxes(DataAbstract* ev, int axis0, int axis1)
219    {
220      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
221      if (temp_ev==0) {
222        throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");
223      }
224      DataArrayView& thisView=getPointDataView();
225      DataArrayView& evView=ev->getPointDataView();
226      DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
227    }
228    
229    void
230    DataConstant::transpose(DataAbstract* ev, int axis_offset)
231    {
232      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
233      if (temp_ev==0) {
234        throw DataException("Error - DataConstant::transpose: casting to DataConstant failed (propably a programming error).");
235      }
236      DataArrayView& thisView=getPointDataView();
237      DataArrayView& evView=ev->getPointDataView();
238      DataArrayView::transpose(thisView,0,evView,0,axis_offset);
239    }
240    
241    void
242    DataConstant::eigenvalues(DataAbstract* ev)
243    {
244      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
245      if (temp_ev==0) {
246        throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");
247      }
248      DataArrayView& thisView=getPointDataView();
249      DataArrayView& evView=ev->getPointDataView();
250      DataArrayView::eigenvalues(thisView,0,evView,0);
251    }
252    void
253    DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
254    {
255      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
256      if (temp_ev==0) {
257        throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
258      }
259      DataConstant* temp_V=dynamic_cast<DataConstant*>(V);
260      if (temp_V==0) {
261        throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
262      }
263      DataArrayView thisView=getPointDataView();
264      DataArrayView evView=ev->getPointDataView();
265      DataArrayView VView=V->getPointDataView();
266    
267      DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
268    }
269    
270    void
271    DataConstant::setToZero()
272    {
273        DataArrayView::ValueType::size_type n=m_data.size();
274        for (int i=0; i<n ;++i) m_data[i]=0.;
275    }
276    
277  void  void
278  DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)  DataConstant::dump(const std::string fileName) const
279  {  {
280    if (getPointDataView().getRank()!=0) {     #ifdef PASO_MPI
281      stringstream temp;     throw DataException("Error - DataConstant:: dump is not implemented for MPI yet.");
282      temp << "Error - Can only reshape Data with data points of rank 0. "     #endif
283           << "This Data has data points with rank: " << getPointDataView().getRank();     #ifdef USE_NETCDF
284      throw DataException(temp.str());     const NcDim* ncdims[DataArrayView::maxRank];
285    }     NcVar* var;
286    m_data.resize(DataArrayView::noValues(shape),getPointDataView()());     int rank = getPointDataView().getRank();
287    DataArrayView newView(m_data,shape);     int type=  getFunctionSpace().getTypeCode();
288    setPointDataView(newView);     int ndims =0;
289       long dims[DataArrayView::maxRank];
290       const double* d_ptr=&(m_data[0]);
291       DataArrayView::ShapeType shape = getPointDataView().getShape();
292      
293       // netCDF error handler
294       NcError err(NcError::verbose_nonfatal);
295       // Create the file.
296       NcFile dataFile(fileName.c_str(), NcFile::Replace);
297       // check if writing was successful
298       if (!dataFile.is_valid())
299        throw DataException("Error - DataConstant:: opening of netCDF file for output failed.");
300       if (!dataFile.add_att("type_id",0) )
301        throw DataException("Error - DataConstant:: appending data type to netCDF file failed.");
302       if (!dataFile.add_att("rank",rank) )
303        throw DataException("Error - DataConstant:: appending rank attribute to netCDF file failed.");
304       if (!dataFile.add_att("function_space_type",type))
305        throw DataException("Error - DataConstant:: appending function space attribute to netCDF file failed.");
306    
307       if (rank == 0) {
308          if( ! (ncdims[0] = dataFile.add_dim("l", 1)) )
309            throw DataException("Error - DataConstant:: appending ncdimsion 0 to netCDF file failed.");
310          dims[0]=1,
311          ndims=1;
312       } else {
313           ndims=rank;
314           dims[0]=shape[0];
315           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
316            throw DataException("Error - DataConstant:: appending ncdimsion 0 to netCDF file failed.");
317           if ( rank >1 ) {
318               dims[1]=shape[1];
319               if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
320            throw DataException("Error - DataConstant:: appending ncdimsion 1 to netCDF file failed.");
321           }
322           if ( rank >2 ) {
323               dims[2]=shape[2];
324               if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
325            throw DataException("Error - DataConstant:: appending ncdimsion 2 to netCDF file failed.");
326           }
327           if ( rank >3 ) {
328               dims[3]=shape[3];
329               if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
330            throw DataException("Error - DataConstant:: appending ncdimsion 3 to netCDF file failed.");
331           }
332       }
333      
334       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
335        throw DataException("Error - DataConstant:: appending variable to netCDF file failed.");
336       if (! (var->put(d_ptr,dims)) )
337             throw DataException("Error - DataConstant:: copy data to netCDF buffer failed.");
338       #else
339       throw DataException("Error - DataConstant:: dump is not configured with netCDF. Please contact your installation manager.");
340       #endif
341  }  }
342    
343  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26