/[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 82 by jgs, Tue Oct 26 06:53:54 2004 UTC trunk/escript/src/Data/DataConstant.cpp revision 468 by jgs, Wed Jan 25 06:50:39 2006 UTC
# Line 1  Line 1 
1    //$Id$
2  /*  /*
3   ******************************************************************************   ******************************************************************************
4   *                                                                            *   *                                                                            *
# Line 14  Line 15 
15    
16  #include "escript/Data/DataConstant.h"  #include "escript/Data/DataConstant.h"
17  #include "escript/Data/DataException.h"  #include "escript/Data/DataException.h"
18  #include "esysUtils/EsysAssert.h"  #include "EsysAssert.h"
19    
20  #include <iostream>  #include <iostream>
21  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
22    
   
23  using namespace std;  using namespace std;
24    
25  namespace escript {  namespace escript {
26    
27    DataConstant::DataConstant(const boost::python::numeric::array& value, const FunctionSpace& what): DataAbstract(what)  DataConstant::DataConstant(const boost::python::numeric::array& value,
28    {                             const FunctionSpace& what)
29      //cout << "Calling DataConstant constructor 1." << endl;    : DataAbstract(what)
30      DataArray temp(value);  {
31      //    DataArray temp(value);
32      // copy the data in the correct format    //
33      m_data=temp.getData();    // copy the data in the correct format
34      DataArrayView tempView(m_data,temp.getView().getShape());    m_data=temp.getData();
35      //    //
36      // copy the view of the data    // create the view of the data
37      setPointDataView(tempView);    DataArrayView tempView(m_data,temp.getView().getShape());
38    }    setPointDataView(tempView);
39    }
40    DataConstant::DataConstant(const DataArrayView& value, const FunctionSpace& what): DataAbstract(what)  
41    {  DataConstant::DataConstant(const DataArrayView& value,
42      //cout << "Calling DataConstant constructor 2." << endl;                             const FunctionSpace& what)
43      //    : DataAbstract(what)
44      // copy the data in the correct format  {
45      m_data=value.getData();    //
46      DataArrayView tempView(m_data,value.getShape());    // copy the data in the correct format
47      //    m_data=value.getData();
48      // copy the view of the data    //
49      setPointDataView(tempView);    // create the view of the data
50    }    DataArrayView tempView(m_data,value.getShape());
51      setPointDataView(tempView);
52    DataConstant::DataConstant(const DataConstant& other): DataAbstract(other.getFunctionSpace()), m_data(other.m_data)  }
53    {  
54      //cout << "Calling DataConstant copy constructor." << endl;  DataConstant::DataConstant(const DataConstant& other)
55      //    : DataAbstract(other.getFunctionSpace())
56      DataArrayView tempView(m_data,other.getPointDataView().getShape());  {
57      //    //
58      // copy the view of the data    // copy the data in the correct format
59      setPointDataView(tempView);    m_data=other.m_data;
60    }    //
61      // create the view of the data
62    DataConstant::DataConstant(const DataConstant& other, const DataArrayView::RegionType& region): DataAbstract(other.getFunctionSpace())    DataArrayView tempView(m_data,other.getPointDataView().getShape());
63    {    setPointDataView(tempView);
64      //cout << "Calling DataConstant slice constructor." << endl;  }
65      //  
66      //  DataConstant::DataConstant(const DataConstant& other,
67      // get the shape of the slice                             const DataArrayView::RegionType& region)
68      DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    : DataAbstract(other.getFunctionSpace())
69      //  {
70      // allocate space for this DataConstant    //
71      m_data.resize(DataArrayView::noValues(shape));    // get the shape of the slice to copy from
72      DataArrayView tempView(m_data,shape);    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
73      tempView.copySlice(other.getPointDataView(),region);    //
74      //    // allocate space for this new DataConstant's data
75      // copy the view of the data    int len = DataArrayView::noValues(shape);
76      setPointDataView(tempView);    m_data.resize(len,0.,len);
77    }    //
78      // create a view of the data with the correct shape
79    string DataConstant::toString() const    DataArrayView tempView(m_data,shape);
80    {    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
81      return getPointDataView().toString("");    //
82    }    // load the view with the data from the slice
83      tempView.copySlice(other.getPointDataView(),region_loop_range);
84    DataArrayView::ValueType::size_type DataConstant::getPointOffset(int sampleNo, int dataPointNo) const    setPointDataView(tempView);
85    {  }
86      EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),  
87             "Invalid index, sampleNo: " << sampleNo  DataConstant::DataConstant(const FunctionSpace& what,
88             << " dataPointNo: " << dataPointNo);                             const DataArrayView::ShapeType &shape,
89      return 0;                             const DataArrayView::ValueType &data)
90    }    : DataAbstract(what)
91    {
92    DataArrayView::ValueType::size_type DataConstant::getLength() const    //
93    {    // copy the data in the correct format
94      return m_data.size();    m_data=data;
95    }    //
96      // create the view of the data
97    DataArrayView DataConstant::getDataPoint(int sampleNo, int dataPointNo)    DataArrayView tempView(m_data,shape);
98    {    setPointDataView(tempView);
99      EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),  }
100             "Invalid index, sampleNo: " << sampleNo  
101             << " dataPointNo: " << dataPointNo);  string
102      //  DataConstant::toString() const
103      // Whatever the coord's always return the same value  {
104      return getPointDataView();    return getPointDataView().toString("");
105    }  }
106    
107    DataArrayView::ValueType::size_type
108    DataConstant::getPointOffset(int sampleNo,
109                                 int dataPointNo) const
110    {
111      EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
112                  "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
113      //
114      // Whatever the coord's always return the same value as this is constant data.
115      return 0;
116    }
117    
118    DataArrayView::ValueType::size_type
119    DataConstant::getLength() const
120    {
121      return m_data.size();
122    }
123    
124    DataArrayView
125    DataConstant::getDataPoint(int sampleNo,
126                               int dataPointNo)
127    {
128      EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
129                 "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
130      //
131      // Whatever the coord's always return the same value as this is constant data.
132      return getPointDataView();
133    }
134        
135    DataAbstract* DataConstant::getSlice(const DataArrayView::RegionType& region) const  DataAbstract*
136    {  DataConstant::getSlice(const DataArrayView::RegionType& region) const
137      return new DataConstant(*this,region);  {
138    }    return new DataConstant(*this,region);
139    }
140    void DataConstant::setSlice(const DataAbstract* value, const DataArrayView::RegionType& region)  
141    {  void
142      const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);  DataConstant::setSlice(const DataAbstract* value,
143      if (tempDataConst==0)                         const DataArrayView::RegionType& region)
144      {  {
145        throw DataException("Programming error - casting to DataConstant.");    const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
146      }    if (tempDataConst==0) {
147      getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region);      throw DataException("Programming error - casting to DataConstant.");
148    }    }
149      //
150    void DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
151    {    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
152      //    //
153      // reshape a rank zero data point    // check shape:
154      if (getPointDataView().getRank()!=0) {    if (getPointDataView().getRank()!=region.size()) {
155        stringstream temp;      throw DataException("Error - Invalid slice region.");
156        temp << "Error - Can only reshape Data with data points of rank 0. "    }
157         << "This Data has data points with rank: "    if (tempDataConst->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
158         << getPointDataView().getRank();      throw DataException (value->getPointDataView().createShapeErrorMessage(
159        throw DataException(temp.str());                  "Error - Couldn't copy slice due to shape mismatch.",shape));
160      }    }
161      m_data.resize(DataArrayView::noValues(shape),getPointDataView()());    //
162      DataArrayView newView(m_data,shape);    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
163      setPointDataView(newView);  }
164    }  
165    void
166    DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)
167    {
168      if (getPointDataView().getRank()!=0) {
169        stringstream temp;
170        temp << "Error - Can only reshape Data with data points of rank 0. "
171             << "This Data has data points with rank: " << getPointDataView().getRank();
172        throw DataException(temp.str());
173      }
174      int len = DataArrayView::noValues(shape);
175      m_data.resize(len,getPointDataView()(),len);
176      DataArrayView newView(m_data,shape);
177      setPointDataView(newView);
178    }
179    
180    int
181    DataConstant::archiveData(ofstream& archiveFile,
182                              const DataArrayView::ValueType::size_type noValues) const
183    {
184      return(m_data.archiveData(archiveFile, noValues));
185    }
186    
187    int
188    DataConstant::extractData(ifstream& archiveFile,
189                              const DataArrayView::ValueType::size_type noValues)
190    {
191      return(m_data.extractData(archiveFile, noValues));
192    }
193    
194  }  // end of namespace  }  // end of namespace
   
   
   
   

Legend:
Removed from v.82  
changed lines
  Added in v.468

  ViewVC Help
Powered by ViewVC 1.1.26