/[escript]/branches/arrayview_from_1695_trunk/escript/src/DataConstant.cpp
ViewVC logotype

Diff of /branches/arrayview_from_1695_trunk/escript/src/DataConstant.cpp

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

revision 1746 by jfenwick, Mon Aug 25 05:38:57 2008 UTC revision 1747 by jfenwick, Wed Sep 3 04:56:50 2008 UTC
# Line 24  Line 24 
24  #endif  #endif
25    
26  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
27    #include "DataMaths.h"
28    
29  using namespace std;  using namespace std;
30  using namespace boost::python;  using namespace boost::python;
# Line 32  namespace escript { Line 33  namespace escript {
33    
34  DataConstant::DataConstant(const boost::python::numeric::array& value,  DataConstant::DataConstant(const boost::python::numeric::array& value,
35                             const FunctionSpace& what)                             const FunctionSpace& what)
36    : DataAbstract(what)    : DataAbstract(what,DataTypes::shapeFromNumArray(value))
37  {  {
38    // extract the shape of the numarray  //   // extract the shape of the numarray
39    DataTypes::ShapeType tempShape;  //   DataTypes::ShapeType tempShape;
40    for (int i=0; i < value.getrank(); i++) {  //   for (int i=0; i < value.getrank(); i++) {
41      tempShape.push_back(extract<int>(value.getshape()[i]));  //     tempShape.push_back(extract<int>(value.getshape()[i]));
42    }  //   }
43    
44    // get the space for the data vector    // get the space for the data vector
45    int len = DataTypes::noValues(tempShape);    int len = getNoValues();
46    DataVector temp_data(len, 0.0, len);    DataVector temp_data(len, 0.0, len);
47    DataArrayView temp_dataView(temp_data, tempShape);  //   DataArrayView temp_dataView(temp_data, tempShape);
48    temp_dataView.copy(value);  //   temp_dataView.copy(value);
49    
50    //    //
51    // copy the data in the correct format    // copy the data in the correct format
52    m_data=temp_data;    m_data=temp_data;
53    //    //
54    // create the view of the data    // create the view of the data
55    DataArrayView tempView(m_data,temp_dataView.getShape());  //   DataArrayView tempView(m_data,temp_dataView.getShape());
56    setPointDataView(tempView);  //   setPointDataView(tempView);
57  }  }
58    
59  DataConstant::DataConstant(const DataArrayView& value,  // DataConstant::DataConstant(const DataArrayView& value,
60                             const FunctionSpace& what)  //                            const FunctionSpace& what)
61    : DataAbstract(what)  //   : DataAbstract(what)
62  {  // {
63    //  //   //
64    // copy the data in the correct format  //   // copy the data in the correct format
65    m_data=value.getData();  //   m_data=value.getData();
66    //  //   //
67    // create the view of the data  //   // create the view of the data
68    DataArrayView tempView(m_data,value.getShape());  //   DataArrayView tempView(m_data,value.getShape());
69    setPointDataView(tempView);  //   setPointDataView(tempView);
70  }  // }
71    
72  DataConstant::DataConstant(const DataConstant& other)  DataConstant::DataConstant(const DataConstant& other)
73    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace(),other.getShape())
74  {  //  {  //
75    // copy the data in the correct format    // copy the data in the correct format
76    m_data=other.m_data;    m_data=other.m_data;
77    //    //
78    // create the view of the data  //   // create the view of the data
79    DataArrayView tempView(m_data,other.getPointDataView().getShape());  //   DataArrayView tempView(m_data,other.getPointDataView().getShape());
80    setPointDataView(tempView);  //   setPointDataView(tempView);
81  }  }
82    
83  DataConstant::DataConstant(const DataConstant& other,  DataConstant::DataConstant(const DataConstant& other,
84                             const DataTypes::RegionType& region)                             const DataTypes::RegionType& region)
85    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))
86  {  {
87    //    //
88    // get the shape of the slice to copy from    // get the shape of the slice to copy from
89    DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));  //   DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
90    //    //
91    // allocate space for this new DataConstant's data    // allocate space for this new DataConstant's data
92    int len = DataTypes::noValues(shape);    int len = getNoValues();
93    m_data.resize(len,0.,len);    m_data.resize(len,0.,len);
94    //    //
95    // create a view of the data with the correct shape    // create a view of the data with the correct shape
96    DataArrayView tempView(m_data,shape);  //   DataArrayView tempView(m_data,shape);
97    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
98    //    //
99    // load the view with the data from the slice    // load the view with the data from the slice
100    tempView.copySlice(other.getPointDataView(),region_loop_range);  //   tempView.copySlice(other.getPointDataView(),region_loop_range);
101    setPointDataView(tempView);    DataTypes::copySlice(m_data,getShape(),0,other.getVector(),other.getShape(),0,region_loop_range);
102    //   setPointDataView(tempView);
103  }  }
104    
105  DataConstant::DataConstant(const FunctionSpace& what,  DataConstant::DataConstant(const FunctionSpace& what,
106                             const DataTypes::ShapeType &shape,                             const DataTypes::ShapeType &shape,
107                             const DataTypes::ValueType &data)                             const DataTypes::ValueType &data)
108    : DataAbstract(what)    : DataAbstract(what,shape)
109  {  {
110    //    //
111    // copy the data in the correct format    // copy the data in the correct format
112    m_data=data;    m_data=data;
113    //    //
114    // create the view of the data    // create the view of the data
115    DataArrayView tempView(m_data,shape);  //   DataArrayView tempView(m_data,shape);
116    setPointDataView(tempView);  //   setPointDataView(tempView);
117  }  }
118    
119  string  string
120  DataConstant::toString() const  DataConstant::toString() const
121  {  {
122    return getPointDataView().toString("");    return DataTypes::pointToString(m_data,getShape(),0,"");
123  }  }
124    
125  DataTypes::ValueType::size_type  DataTypes::ValueType::size_type
# Line 137  DataConstant::getLength() const Line 139  DataConstant::getLength() const
139    return m_data.size();    return m_data.size();
140  }  }
141    
142  DataArrayView  // DataArrayView
143  DataConstant::getDataPoint(int sampleNo,  // DataConstant::getDataPoint(int sampleNo,
144                             int dataPointNo)  //                            int dataPointNo)
145  {  // {
146    EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),  //   EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
147               "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);  //              "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
148    //  //   //
149    // 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.
150    return getPointDataView();  //   return getPointDataView();
151  }  // }
152    
153  DataAbstract*  DataAbstract*
154  DataConstant::getSlice(const DataTypes::RegionType& region) const  DataConstant::getSlice(const DataTypes::RegionType& region) const
# Line 167  DataConstant::setSlice(const DataAbstrac Line 169  DataConstant::setSlice(const DataAbstrac
169    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
170    //    //
171    // check shape:    // check shape:
172    if (getPointDataView().getRank()!=region.size()) {    if (getRank()!=region.size()) {
173      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
174    }    }
175    if (tempDataConst->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {    if (getRank()>0 && !DataTypes::checkShape(getShape(),value->getShape())) {
176      throw DataException (DataTypes::createShapeErrorMessage(      throw DataException (DataTypes::createShapeErrorMessage(
177                  "Error - Couldn't copy slice due to shape mismatch.",shape,value->getShape()));                  "Error - Couldn't copy slice due to shape mismatch.",shape,value->getShape()));
178    }    }
179    //    //   getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
180    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);    DataTypes::copySliceFrom(m_data,getShape(),0,tempDataConst->getVector(), tempDataConst->getShape(),0,region_loop_range);
181  }  }
182    
183  int  int
# Line 199  DataConstant::symmetric(DataAbstract* ev Line 201  DataConstant::symmetric(DataAbstract* ev
201    if (temp_ev==0) {    if (temp_ev==0) {
202      throw DataException("Error - DataConstant::symmetric: casting to DataConstant failed (propably a programming error).");      throw DataException("Error - DataConstant::symmetric: casting to DataConstant failed (propably a programming error).");
203    }    }
204    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
205    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
206    DataArrayView::symmetric(thisView,0,evView,0);    DataMaths::symmetric(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
207  }  }
208    
209  void  void
# Line 211  DataConstant::nonsymmetric(DataAbstract* Line 213  DataConstant::nonsymmetric(DataAbstract*
213    if (temp_ev==0) {    if (temp_ev==0) {
214      throw DataException("Error - DataConstant::nonsymmetric: casting to DataConstant failed (propably a programming error).");      throw DataException("Error - DataConstant::nonsymmetric: casting to DataConstant failed (propably a programming error).");
215    }    }
216    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
217    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
218    DataArrayView::nonsymmetric(thisView,0,evView,0);    DataMaths::nonsymmetric(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
219  }  }
220    
221  void  void
# Line 223  DataConstant::trace(DataAbstract* ev, in Line 225  DataConstant::trace(DataAbstract* ev, in
225    if (temp_ev==0) {    if (temp_ev==0) {
226      throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");      throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");
227    }    }
228    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
229    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
230    DataArrayView::trace(thisView,0,evView,0,axis_offset);    ValueType& evVec=temp_ev->getVector();
231      const ShapeType& evShape=temp_ev->getShape();
232      DataMaths::trace(m_data,getShape(),0,evVec,evShape,0,axis_offset);
233  }  }
234    
235  void  void
# Line 235  DataConstant::swapaxes(DataAbstract* ev, Line 239  DataConstant::swapaxes(DataAbstract* ev,
239    if (temp_ev==0) {    if (temp_ev==0) {
240      throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");      throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");
241    }    }
242    DataArrayView& thisView=getPointDataView();  //   DataArrayView& thisView=getPointDataView();
243    DataArrayView& evView=ev->getPointDataView();  //   DataArrayView& evView=ev->getPointDataView();
244    DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);    DataMaths::swapaxes(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0,axis0,axis1);
245  }  }
246    
247  void  void
# Line 247  DataConstant::transpose(DataAbstract* ev Line 251  DataConstant::transpose(DataAbstract* ev
251    if (temp_ev==0) {    if (temp_ev==0) {
252      throw DataException("Error - DataConstant::transpose: casting to DataConstant failed (propably a programming error).");      throw DataException("Error - DataConstant::transpose: casting to DataConstant failed (propably a programming error).");
253    }    }
254    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
255    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
256    DataArrayView::transpose(thisView,0,evView,0,axis_offset);    DataMaths::transpose(m_data, getShape(),0, temp_ev->getVector(),temp_ev->getShape(),0,axis_offset);
257  }  }
258    
259  void  void
# Line 259  DataConstant::eigenvalues(DataAbstract* Line 263  DataConstant::eigenvalues(DataAbstract*
263    if (temp_ev==0) {    if (temp_ev==0) {
264      throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");      throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");
265    }    }
266    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
267    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
268    DataArrayView::eigenvalues(thisView,0,evView,0);    DataMaths::eigenvalues(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
269  }  }
270  void  void
271  DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)  DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
# Line 274  DataConstant::eigenvalues_and_eigenvecto Line 278  DataConstant::eigenvalues_and_eigenvecto
278    if (temp_V==0) {    if (temp_V==0) {
279      throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");      throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
280    }    }
281    DataArrayView thisView=getPointDataView();  //   DataArrayView thisView=getPointDataView();
282    DataArrayView evView=ev->getPointDataView();  //   DataArrayView evView=ev->getPointDataView();
283    DataArrayView VView=V->getPointDataView();  //   DataArrayView VView=V->getPointDataView();
284    
285    DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);    DataMaths::eigenvalues_and_eigenvectors(m_data, getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0,temp_V->getVector(), temp_V->getShape(),0,tol);
286  }  }
287    
288  void  void
# Line 297  DataConstant::dump(const std::string fil Line 301  DataConstant::dump(const std::string fil
301     #ifdef USE_NETCDF     #ifdef USE_NETCDF
302     const NcDim* ncdims[DataTypes::maxRank];     const NcDim* ncdims[DataTypes::maxRank];
303     NcVar* var;     NcVar* var;
304     int rank = getPointDataView().getRank();     int rank = getRank();
305     int type=  getFunctionSpace().getTypeCode();     int type=  getFunctionSpace().getTypeCode();
306     int ndims =0;     int ndims =0;
307     long dims[DataTypes::maxRank];     long dims[DataTypes::maxRank];
308     const double* d_ptr=&(m_data[0]);     const double* d_ptr=&(m_data[0]);
309     DataTypes::ShapeType shape = getPointDataView().getShape();     DataTypes::ShapeType shape = getShape();
310    
311     // netCDF error handler     // netCDF error handler
312     NcError err(NcError::verbose_nonfatal);     NcError err(NcError::verbose_nonfatal);

Legend:
Removed from v.1746  
changed lines
  Added in v.1747

  ViewVC Help
Powered by ViewVC 1.1.26