/[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 1388 by trankine, Fri Jan 11 07:45:58 2008 UTC revision 1796 by jfenwick, Wed Sep 17 01:45:46 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    DataArrayView::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 = DataArrayView::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      m_data.copyFromNumArray(value);
51    //    //
   // copy the data in the correct format  
   m_data=temp_data;  
   //  
   // create the view of the data  
   DataArrayView tempView(m_data,temp_dataView.getShape());  
   setPointDataView(tempView);  
 }  
52    
 DataConstant::DataConstant(const DataArrayView& value,  
                            const FunctionSpace& what)  
   : DataAbstract(what)  
 {  
   //  
53    // copy the data in the correct format    // copy the data in the correct format
54    m_data=value.getData();  //   m_data=temp_data;
55    //    //
56    // create the view of the data    // create the view of the data
57    DataArrayView tempView(m_data,value.getShape());  //   DataArrayView tempView(m_data,temp_dataView.getShape());
58    setPointDataView(tempView);  //   setPointDataView(tempView);
59  }  }
60    
61    // DataConstant::DataConstant(const DataArrayView& value,
62    //                            const FunctionSpace& what)
63    //   : DataAbstract(what)
64    // {
65    //   //
66    //   // copy the data in the correct format
67    //   m_data=value.getData();
68    //   //
69    //   // create the view of the data
70    //   DataArrayView tempView(m_data,value.getShape());
71    //   setPointDataView(tempView);
72    // }
73    
74  DataConstant::DataConstant(const DataConstant& other)  DataConstant::DataConstant(const DataConstant& other)
75    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace(),other.getShape())
76  {  //  {  //
77    // copy the data in the correct format    // copy the data in the correct format
78    m_data=other.m_data;    m_data=other.m_data;
79    //    //
80    // create the view of the data  //   // create the view of the data
81    DataArrayView tempView(m_data,other.getPointDataView().getShape());  //   DataArrayView tempView(m_data,other.getPointDataView().getShape());
82    setPointDataView(tempView);  //   setPointDataView(tempView);
83  }  }
84    
85  DataConstant::DataConstant(const DataConstant& other,  DataConstant::DataConstant(const DataConstant& other,
86                             const DataArrayView::RegionType& region)                             const DataTypes::RegionType& region)
87    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))
88  {  {
89    //    //
90    // get the shape of the slice to copy from    // get the shape of the slice to copy from
91    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));  //   DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
92    //    //
93    // allocate space for this new DataConstant's data    // allocate space for this new DataConstant's data
94    int len = DataArrayView::noValues(shape);    int len = getNoValues();
95    m_data.resize(len,0.,len);    m_data.resize(len,0.,len);
96    //    //
97    // create a view of the data with the correct shape    // create a view of the data with the correct shape
98    DataArrayView tempView(m_data,shape);  //   DataArrayView tempView(m_data,shape);
99    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
100    //    //
101    // load the view with the data from the slice    // load the view with the data from the slice
102    tempView.copySlice(other.getPointDataView(),region_loop_range);  //   tempView.copySlice(other.getPointDataView(),region_loop_range);
103    setPointDataView(tempView);    DataTypes::copySlice(m_data,getShape(),0,other.getVector(),other.getShape(),0,region_loop_range);
104    //   setPointDataView(tempView);
105  }  }
106    
107  DataConstant::DataConstant(const FunctionSpace& what,  DataConstant::DataConstant(const FunctionSpace& what,
108                             const DataArrayView::ShapeType &shape,                             const DataTypes::ShapeType &shape,
109                             const DataArrayView::ValueType &data)                             const DataTypes::ValueType &data)
110    : DataAbstract(what)    : DataAbstract(what,shape)
111  {  {
112    //    //
113    // copy the data in the correct format    // copy the data in the correct format
114    m_data=data;    m_data=data;
115    //    //
116    // create the view of the data    // create the view of the data
117    DataArrayView tempView(m_data,shape);  //   DataArrayView tempView(m_data,shape);
118    setPointDataView(tempView);  //   setPointDataView(tempView);
119  }  }
120    
121  string  string
122  DataConstant::toString() const  DataConstant::toString() const
123  {  {
124    return getPointDataView().toString("");    return DataTypes::pointToString(m_data,getShape(),0,"");
125  }  }
126    
127  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
128  DataConstant::getPointOffset(int sampleNo,  DataConstant::getPointOffset(int sampleNo,
129                               int dataPointNo) const                               int dataPointNo) const
130  {  {
# Line 131  DataConstant::getPointOffset(int sampleN Line 135  DataConstant::getPointOffset(int sampleN
135    return 0;    return 0;
136  }  }
137    
138  DataArrayView::ValueType::size_type  DataTypes::ValueType::size_type
139  DataConstant::getLength() const  DataConstant::getLength() const
140  {  {
141    return m_data.size();    return m_data.size();
142  }  }
143    
144  DataArrayView  // DataArrayView
145  DataConstant::getDataPoint(int sampleNo,  // DataConstant::getDataPoint(int sampleNo,
146                             int dataPointNo)  //                            int dataPointNo)
147  {  // {
148    EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),  //   EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
149               "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);  //              "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
150    //  //   //
151    // 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.
152    return getPointDataView();  //   return getPointDataView();
153  }  // }
154    
155  DataAbstract*  DataAbstract*
156  DataConstant::getSlice(const DataArrayView::RegionType& region) const  DataConstant::getSlice(const DataTypes::RegionType& region) const
157  {  {
158    return new DataConstant(*this,region);    return new DataConstant(*this,region);
159  }  }
160    
161  void  void
162  DataConstant::setSlice(const DataAbstract* value,  DataConstant::setSlice(const DataAbstract* value,
163                         const DataArrayView::RegionType& region)                         const DataTypes::RegionType& region)
164  {  {
165    const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);    const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
166    if (tempDataConst==0) {    if (tempDataConst==0) {
167      throw DataException("Programming error - casting to DataConstant.");      throw DataException("Programming error - casting to DataConstant.");
168    }    }
169    //    //
170    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
171    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
172    //    //
173    // check shape:    // check shape:
174    if (getPointDataView().getRank()!=region.size()) {    if (getRank()!=region.size()) {
175      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
176    }    }
177    if (tempDataConst->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {    if (getRank()>0 && !DataTypes::checkShape(value->getShape(),shape)) {
178      throw DataException (value->getPointDataView().createShapeErrorMessage(      throw DataException (DataTypes::createShapeErrorMessage(
179                  "Error - Couldn't copy slice due to shape mismatch.",shape));                  "Error - Couldn't copy slice due to shape mismatch.",shape,value->getShape()));
180    }    }
181    //    //   getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
182    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);    DataTypes::copySliceFrom(m_data,getShape(),0,tempDataConst->getVector(), tempDataConst->getShape(),0,region_loop_range);
183  }  }
184    
 int  
 DataConstant::archiveData(ofstream& archiveFile,  
                           const DataArrayView::ValueType::size_type noValues) const  
 {  
   return(m_data.archiveData(archiveFile, noValues));  
 }  
185    
 int  
 DataConstant::extractData(ifstream& archiveFile,  
                           const DataArrayView::ValueType::size_type noValues)  
 {  
   return(m_data.extractData(archiveFile, noValues));  
 }  
186    
187  void  void
188  DataConstant::symmetric(DataAbstract* ev)  DataConstant::symmetric(DataAbstract* ev)
# Line 199  DataConstant::symmetric(DataAbstract* ev Line 191  DataConstant::symmetric(DataAbstract* ev
191    if (temp_ev==0) {    if (temp_ev==0) {
192      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).");
193    }    }
194    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
195    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
196    DataArrayView::symmetric(thisView,0,evView,0);    DataMaths::symmetric(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
197  }  }
198    
199  void  void
# Line 211  DataConstant::nonsymmetric(DataAbstract* Line 203  DataConstant::nonsymmetric(DataAbstract*
203    if (temp_ev==0) {    if (temp_ev==0) {
204      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).");
205    }    }
206    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
207    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
208    DataArrayView::nonsymmetric(thisView,0,evView,0);    DataMaths::nonsymmetric(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
209  }  }
210    
211  void  void
# Line 223  DataConstant::trace(DataAbstract* ev, in Line 215  DataConstant::trace(DataAbstract* ev, in
215    if (temp_ev==0) {    if (temp_ev==0) {
216      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).");
217    }    }
218    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
219    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
220    DataArrayView::trace(thisView,0,evView,0,axis_offset);    ValueType& evVec=temp_ev->getVector();
221      const ShapeType& evShape=temp_ev->getShape();
222      DataMaths::trace(m_data,getShape(),0,evVec,evShape,0,axis_offset);
223  }  }
224    
225  void  void
# Line 235  DataConstant::swapaxes(DataAbstract* ev, Line 229  DataConstant::swapaxes(DataAbstract* ev,
229    if (temp_ev==0) {    if (temp_ev==0) {
230      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).");
231    }    }
232    DataArrayView& thisView=getPointDataView();  //   DataArrayView& thisView=getPointDataView();
233    DataArrayView& evView=ev->getPointDataView();  //   DataArrayView& evView=ev->getPointDataView();
234    DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);    DataMaths::swapaxes(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0,axis0,axis1);
235  }  }
236    
237  void  void
# Line 247  DataConstant::transpose(DataAbstract* ev Line 241  DataConstant::transpose(DataAbstract* ev
241    if (temp_ev==0) {    if (temp_ev==0) {
242      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).");
243    }    }
244    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
245    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
246    DataArrayView::transpose(thisView,0,evView,0,axis_offset);    DataMaths::transpose(m_data, getShape(),0, temp_ev->getVector(),temp_ev->getShape(),0,axis_offset);
247  }  }
248    
249  void  void
# Line 259  DataConstant::eigenvalues(DataAbstract* Line 253  DataConstant::eigenvalues(DataAbstract*
253    if (temp_ev==0) {    if (temp_ev==0) {
254      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).");
255    }    }
256    DataArrayView& thisView=getPointDataView();  /*  DataArrayView& thisView=getPointDataView();
257    DataArrayView& evView=ev->getPointDataView();    DataArrayView& evView=ev->getPointDataView();*/
258    DataArrayView::eigenvalues(thisView,0,evView,0);    DataMaths::eigenvalues(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
259  }  }
260  void  void
261  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 268  DataConstant::eigenvalues_and_eigenvecto
268    if (temp_V==0) {    if (temp_V==0) {
269      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).");
270    }    }
271    DataArrayView thisView=getPointDataView();  //   DataArrayView thisView=getPointDataView();
272    DataArrayView evView=ev->getPointDataView();  //   DataArrayView evView=ev->getPointDataView();
273    DataArrayView VView=V->getPointDataView();  //   DataArrayView VView=V->getPointDataView();
274    
275    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);
276  }  }
277    
278  void  void
279  DataConstant::setToZero()  DataConstant::setToZero()
280  {  {
281      DataArrayView::ValueType::size_type n=m_data.size();      DataTypes::ValueType::size_type n=m_data.size();
282      for (int i=0; i<n ;++i) m_data[i]=0.;      for (int i=0; i<n ;++i) m_data[i]=0.;
283  }  }
284    
# Line 295  DataConstant::dump(const std::string fil Line 289  DataConstant::dump(const std::string fil
289     throw DataException("Error - DataConstant:: dump is not implemented for MPI yet.");     throw DataException("Error - DataConstant:: dump is not implemented for MPI yet.");
290     #endif     #endif
291     #ifdef USE_NETCDF     #ifdef USE_NETCDF
292     const NcDim* ncdims[DataArrayView::maxRank];     const NcDim* ncdims[DataTypes::maxRank];
293     NcVar* var;     NcVar* var;
294     int rank = getPointDataView().getRank();     int rank = getRank();
295     int type=  getFunctionSpace().getTypeCode();     int type=  getFunctionSpace().getTypeCode();
296     int ndims =0;     int ndims =0;
297     long dims[DataArrayView::maxRank];     long dims[DataTypes::maxRank];
298     const double* d_ptr=&(m_data[0]);     const double* d_ptr=&(m_data[0]);
299     DataArrayView::ShapeType shape = getPointDataView().getShape();     DataTypes::ShapeType shape = getShape();
300    
301     // netCDF error handler     // netCDF error handler
302     NcError err(NcError::verbose_nonfatal);     NcError err(NcError::verbose_nonfatal);

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

  ViewVC Help
Powered by ViewVC 1.1.26