/[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

revision 474 by jgs, Mon Jan 30 04:23:44 2006 UTC revision 1388 by trankine, Fri Jan 11 07:45:58 2008 UTC
# Line 1  Line 1 
1  //$Id$  
2  /*  /* $Id$ */
3   ******************************************************************************  
4   *                                                                            *  /*******************************************************
5   *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *   *
6   *                                                                            *   *           Copyright 2003-2007 by ACceSS MNRF
7   * This software is the property of ACcESS. No part of this code              *   *       Copyright 2007 by University of Queensland
8   * may be copied in any form or by any means without the expressed written    *   *
9   * consent of ACcESS.  Copying, use or modification of this software          *   *                http://esscc.uq.edu.au
10   * by any unauthorised person is illegal unless that person has a software    *   *        Primary Business: Queensland, Australia
11   * license agreement with ACcESS.                                             *   *  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"  #include "DataConstant.h"
17  #include "DataException.h"  #include "DataException.h"
18  #include "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    #include <boost/python/extract.hpp>
27    
28  using namespace std;  using namespace std;
29    using namespace boost::python;
30    
31  namespace escript {  namespace escript {
32    
# Line 28  DataConstant::DataConstant(const boost:: Line 34  DataConstant::DataConstant(const boost::
34                             const FunctionSpace& what)                             const FunctionSpace& what)
35    : DataAbstract(what)    : DataAbstract(what)
36  {  {
37    DataArray temp(value);    // extract the shape of the numarray
38      DataArrayView::ShapeType tempShape;
39      for (int i=0; i < value.getrank(); i++) {
40        tempShape.push_back(extract<int>(value.getshape()[i]));
41      }
42    
43      // get the space for the data vector
44      int len = DataArrayView::noValues(tempShape);
45      DataVector temp_data(len, 0.0, len);
46      DataArrayView temp_dataView(temp_data, tempShape);
47      temp_dataView.copy(value);
48    
49    //    //
50    // copy the data in the correct format    // copy the data in the correct format
51    m_data=temp.getData();    m_data=temp_data;
52    //    //
53    // create the view of the data    // create the view of the data
54    DataArrayView tempView(m_data,temp.getView().getShape());    DataArrayView tempView(m_data,temp_dataView.getShape());
55    setPointDataView(tempView);    setPointDataView(tempView);
56  }  }
57    
# Line 53  DataConstant::DataConstant(const DataArr Line 70  DataConstant::DataConstant(const DataArr
70    
71  DataConstant::DataConstant(const DataConstant& other)  DataConstant::DataConstant(const DataConstant& other)
72    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
73  {  {  //
   //  
74    // copy the data in the correct format    // copy the data in the correct format
75    m_data=other.m_data;    m_data=other.m_data;
76    //    //
# Line 131  DataConstant::getDataPoint(int sampleNo, Line 147  DataConstant::getDataPoint(int sampleNo,
147    // Whatever the coord's always return the same value as this is constant data.    // Whatever the coord's always return the same value as this is constant data.
148    return getPointDataView();    return getPointDataView();
149  }  }
150      
151  DataAbstract*  DataAbstract*
152  DataConstant::getSlice(const DataArrayView::RegionType& region) const  DataConstant::getSlice(const DataArrayView::RegionType& region) const
153  {  {
# Line 140  DataConstant::getSlice(const DataArrayVi Line 156  DataConstant::getSlice(const DataArrayVi
156    
157  void  void
158  DataConstant::setSlice(const DataAbstract* value,  DataConstant::setSlice(const DataAbstract* value,
159                         const DataArrayView::RegionType& region)                         const DataArrayView::RegionType& region)
160  {  {
161    const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);    const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
162    if (tempDataConst==0) {    if (tempDataConst==0) {
163      throw DataException("Programming error - casting to DataConstant.");      throw DataException("Programming error - casting to DataConstant.");
164    }    }
165    //    //
166    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
167    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
168    //    //
# Line 154  DataConstant::setSlice(const DataAbstrac Line 170  DataConstant::setSlice(const DataAbstrac
170    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
171      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
172    }    }
173    if (tempDataConst->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {    if (tempDataConst->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {
174      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (value->getPointDataView().createShapeErrorMessage(
175                  "Error - Couldn't copy slice due to shape mismatch.",shape));                  "Error - Couldn't copy slice due to shape mismatch.",shape));
176    }    }
# Line 162  DataConstant::setSlice(const DataAbstrac Line 178  DataConstant::setSlice(const DataAbstrac
178    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
179  }  }
180    
 void  
 DataConstant::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());  
   }  
   int len = DataArrayView::noValues(shape);  
   m_data.resize(len,getPointDataView()(),len);  
   DataArrayView newView(m_data,shape);  
   setPointDataView(newView);  
 }  
   
181  int  int
182  DataConstant::archiveData(ofstream& archiveFile,  DataConstant::archiveData(ofstream& archiveFile,
183                            const DataArrayView::ValueType::size_type noValues) const                            const DataArrayView::ValueType::size_type noValues) const
# Line 191  DataConstant::extractData(ifstream& arch Line 192  DataConstant::extractData(ifstream& arch
192    return(m_data.extractData(archiveFile, noValues));    return(m_data.extractData(archiveFile, noValues));
193  }  }
194    
195    void
196    DataConstant::symmetric(DataAbstract* ev)
197    {
198      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
199      if (temp_ev==0) {
200        throw DataException("Error - DataConstant::symmetric: casting to DataConstant failed (propably a programming error).");
201      }
202      DataArrayView& thisView=getPointDataView();
203      DataArrayView& evView=ev->getPointDataView();
204      DataArrayView::symmetric(thisView,0,evView,0);
205    }
206    
207    void
208    DataConstant::nonsymmetric(DataAbstract* ev)
209    {
210      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
211      if (temp_ev==0) {
212        throw DataException("Error - DataConstant::nonsymmetric: casting to DataConstant failed (propably a programming error).");
213      }
214      DataArrayView& thisView=getPointDataView();
215      DataArrayView& evView=ev->getPointDataView();
216      DataArrayView::nonsymmetric(thisView,0,evView,0);
217    }
218    
219    void
220    DataConstant::trace(DataAbstract* ev, int axis_offset)
221    {
222      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
223      if (temp_ev==0) {
224        throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");
225      }
226      DataArrayView& thisView=getPointDataView();
227      DataArrayView& evView=ev->getPointDataView();
228      DataArrayView::trace(thisView,0,evView,0,axis_offset);
229    }
230    
231    void
232    DataConstant::swapaxes(DataAbstract* ev, int axis0, int axis1)
233    {
234      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
235      if (temp_ev==0) {
236        throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");
237      }
238      DataArrayView& thisView=getPointDataView();
239      DataArrayView& evView=ev->getPointDataView();
240      DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
241    }
242    
243    void
244    DataConstant::transpose(DataAbstract* ev, int axis_offset)
245    {
246      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
247      if (temp_ev==0) {
248        throw DataException("Error - DataConstant::transpose: casting to DataConstant failed (propably a programming error).");
249      }
250      DataArrayView& thisView=getPointDataView();
251      DataArrayView& evView=ev->getPointDataView();
252      DataArrayView::transpose(thisView,0,evView,0,axis_offset);
253    }
254    
255    void
256    DataConstant::eigenvalues(DataAbstract* ev)
257    {
258      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
259      if (temp_ev==0) {
260        throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");
261      }
262      DataArrayView& thisView=getPointDataView();
263      DataArrayView& evView=ev->getPointDataView();
264      DataArrayView::eigenvalues(thisView,0,evView,0);
265    }
266    void
267    DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
268    {
269      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
270      if (temp_ev==0) {
271        throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
272      }
273      DataConstant* temp_V=dynamic_cast<DataConstant*>(V);
274      if (temp_V==0) {
275        throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
276      }
277      DataArrayView thisView=getPointDataView();
278      DataArrayView evView=ev->getPointDataView();
279      DataArrayView VView=V->getPointDataView();
280    
281      DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
282    }
283    
284    void
285    DataConstant::setToZero()
286    {
287        DataArrayView::ValueType::size_type n=m_data.size();
288        for (int i=0; i<n ;++i) m_data[i]=0.;
289    }
290    
291    void
292    DataConstant::dump(const std::string fileName) const
293    {
294       #ifdef PASO_MPI
295       throw DataException("Error - DataConstant:: dump is not implemented for MPI yet.");
296       #endif
297       #ifdef USE_NETCDF
298       const NcDim* ncdims[DataArrayView::maxRank];
299       NcVar* var;
300       int rank = getPointDataView().getRank();
301       int type=  getFunctionSpace().getTypeCode();
302       int ndims =0;
303       long dims[DataArrayView::maxRank];
304       const double* d_ptr=&(m_data[0]);
305       DataArrayView::ShapeType shape = getPointDataView().getShape();
306    
307       // netCDF error handler
308       NcError err(NcError::verbose_nonfatal);
309       // Create the file.
310       NcFile dataFile(fileName.c_str(), NcFile::Replace);
311       // check if writing was successful
312       if (!dataFile.is_valid())
313        throw DataException("Error - DataConstant:: opening of netCDF file for output failed.");
314       if (!dataFile.add_att("type_id",0) )
315        throw DataException("Error - DataConstant:: appending data type to netCDF file failed.");
316       if (!dataFile.add_att("rank",rank) )
317        throw DataException("Error - DataConstant:: appending rank attribute to netCDF file failed.");
318       if (!dataFile.add_att("function_space_type",type))
319        throw DataException("Error - DataConstant:: appending function space attribute to netCDF file failed.");
320    
321       if (rank == 0) {
322          if( ! (ncdims[0] = dataFile.add_dim("l", 1)) )
323            throw DataException("Error - DataConstant:: appending ncdimsion 0 to netCDF file failed.");
324          dims[0]=1,
325          ndims=1;
326       } else {
327           ndims=rank;
328           dims[0]=shape[0];
329           if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
330            throw DataException("Error - DataConstant:: appending ncdimsion 0 to netCDF file failed.");
331           if ( rank >1 ) {
332               dims[1]=shape[1];
333               if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
334            throw DataException("Error - DataConstant:: appending ncdimsion 1 to netCDF file failed.");
335           }
336           if ( rank >2 ) {
337               dims[2]=shape[2];
338               if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
339            throw DataException("Error - DataConstant:: appending ncdimsion 2 to netCDF file failed.");
340           }
341           if ( rank >3 ) {
342               dims[3]=shape[3];
343               if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
344            throw DataException("Error - DataConstant:: appending ncdimsion 3 to netCDF file failed.");
345           }
346       }
347    
348       if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
349        throw DataException("Error - DataConstant:: appending variable to netCDF file failed.");
350       if (! (var->put(d_ptr,dims)) )
351             throw DataException("Error - DataConstant:: copy data to netCDF buffer failed.");
352       #else
353       throw DataException("Error - DataConstant:: dump is not configured with netCDF. Please contact your installation manager.");
354       #endif
355    }
356    
357  }  // end of namespace  }  // end of namespace

Legend:
Removed from v.474  
changed lines
  Added in v.1388

  ViewVC Help
Powered by ViewVC 1.1.26