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

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

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

revision 116 by jgs, Mon Feb 28 07:06:33 2005 UTC revision 117 by jgs, Fri Apr 1 05:48:57 2005 UTC
# Line 35  DataExpanded::DataExpanded(const boost:: Line 35  DataExpanded::DataExpanded(const boost::
35  {  {
36    DataArrayView::ShapeType tempShape;    DataArrayView::ShapeType tempShape;
37    //    //
38    // extract the shape from the python array    // extract the shape of the python numarray
39    for (int i=0; i<value.getrank(); ++i) {    for (int i=0; i<value.getrank(); i++) {
     //cout << extract<int>(value.getshape()[i]) << endl;  
40      tempShape.push_back(extract<int>(value.getshape()[i]));      tempShape.push_back(extract<int>(value.getshape()[i]));
41    }    }
42      //
43      // initialise the data array for this object
44    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
45    //    //
46    // copy the given value to every data point    // copy the given value to every data point
# Line 49  DataExpanded::DataExpanded(const boost:: Line 50  DataExpanded::DataExpanded(const boost::
50  DataExpanded::DataExpanded(const DataExpanded& other)  DataExpanded::DataExpanded(const DataExpanded& other)
51    : DataAbstract(other.getFunctionSpace()),    : DataAbstract(other.getFunctionSpace()),
52    m_data(other.m_data)    m_data(other.m_data)
53  {      {
54    //    //
55    // create the view for the data    // create the view for the data
56    DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());    DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());
# Line 58  DataExpanded::DataExpanded(const DataExp Line 59  DataExpanded::DataExpanded(const DataExp
59    
60  DataExpanded::DataExpanded(const DataConstant& other)  DataExpanded::DataExpanded(const DataConstant& other)
61    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
62  {      {
63      //
64      // initialise the data array for this object
65    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
66    //    //
67    // DataConstant only has one value    // DataConstant only has one value, copy this to every data point
68    copy(other.getPointDataView());    copy(other.getPointDataView());
69  }  }
70    
71  DataExpanded::DataExpanded(const DataTagged& other)  DataExpanded::DataExpanded(const DataTagged& other)
72    : DataAbstract(other.getFunctionSpace())    : DataAbstract(other.getFunctionSpace())
73  {      {
74      //
75      // initialise the data for this object
76    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
77      //
78      // for each data point in this object, extract and copy the corresponding data
79      // value from the given DataTagged object
80    int i,j;    int i,j;
81    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
82    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
83  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
84    for (i=0;i<numRows;++i) {    for (i=0;i<numRows;i++) {
85      for (j=0;j<numCols;++j) {      for (j=0;j<numCols;j++) {
86        try {        try {
87          getPointDataView().copy(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j));          getPointDataView().copy(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j));
88        }        }
89        catch (std::exception& e) {        catch (std::exception& e) {
90          cout << e.what() << endl;          cout << e.what() << endl;
# Line 96  DataExpanded::DataExpanded(const DataExp Line 104  DataExpanded::DataExpanded(const DataExp
104    // initialise this Data object to the shape of the slice    // initialise this Data object to the shape of the slice
105    initialise(shape,other.getNumSamples(),other.getNumDPPSample());    initialise(shape,other.getNumSamples(),other.getNumDPPSample());
106    //    //
   DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);  
   //  
107    // copy the data    // copy the data
108      DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
109    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
110    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
111    int i,j;    int i,j;
112  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
113    for (i=0;i<numRows;++i) {    for (i=0;i<numRows;i++) {
114      for (j=0;j<numCols;++j) {      for (j=0;j<numCols;j++) {
115        try {        try {
116          getPointDataView().copySlice(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j),region_loop_range);          getPointDataView().copySlice(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j),region_loop_range);
117        }        }
# Line 119  DataExpanded::DataExpanded(const DataArr Line 126  DataExpanded::DataExpanded(const DataArr
126                             const FunctionSpace& what)                             const FunctionSpace& what)
127    : DataAbstract(what)    : DataAbstract(what)
128  {  {
129      //
130      // get the shape of the given data value
131    DataArrayView::ShapeType tempShape=value.getShape();    DataArrayView::ShapeType tempShape=value.getShape();
132      //
133      // initialise this Data object to the shape of the given data value
134    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
135      //
136      // copy the given value to every data point
137    copy(value);    copy(value);
138  }  }
139    
# Line 131  DataExpanded::~DataExpanded() Line 144  DataExpanded::~DataExpanded()
144  void  void
145  DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)  DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
146  {  {
   //  
   // reshape a rank zero data point  
147    if (getPointDataView().getRank()!=0) {    if (getPointDataView().getRank()!=0) {
148      stringstream temp;      stringstream temp;
149      temp << "Error - Can only reshape Data with data points of rank 0. "      temp << "Error - Can only reshape Data with data points of rank 0. "
# Line 140  DataExpanded::reshapeDataPoint(const Dat Line 151  DataExpanded::reshapeDataPoint(const Dat
151           << getPointDataView().getRank();           << getPointDataView().getRank();
152      throw DataException(temp.str());      throw DataException(temp.str());
153    }    }
154    DataBlocks2D newData(getNumSamples(),getNumDPPSample(), DataArrayView::noValues(shape));    //
155      // create the new DataBlocks2D data array, and a corresponding DataArrayView
156      DataBlocks2D newData(getNumSamples(),getNumDPPSample(),DataArrayView::noValues(shape));
157    DataArrayView newView(newData.getData(),shape);    DataArrayView newView(newData.getData(),shape);
158    //    //
159    // Copy the original data to every value for the new shape    // Copy the original data to every value for the new shape
# Line 148  DataExpanded::reshapeDataPoint(const Dat Line 161  DataExpanded::reshapeDataPoint(const Dat
161    int nRows=m_data.getNumRows();    int nRows=m_data.getNumRows();
162    int nCols=m_data.getNumCols();    int nCols=m_data.getNumCols();
163  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
164    for (i=0;i<nRows;++i) {    for (i=0;i<nRows;i++) {
165      for (j=0;j<nCols;++j) {      for (j=0;j<nCols;j++) {
       //  
       // Copy the data into the specified offset  
166        // NOTE: An exception may be thown from this call if        // NOTE: An exception may be thown from this call if
167        // DOASSERT is on which of course will play        // DOASSERT is on which of course will play
168        // havoc with the omp threads. Run single threaded        // havoc with the omp threads. Run single threaded
169        // if using DOASSERT.        // if using DOASSERT.
170        newView.copy(newData.index(i,j),m_data.getData()[m_data.index(i,j)]);        newView.copy(newData.index(i,j),m_data(i,j));
171      }      }
172    }    }
173      // swap the new data array for the original
174    m_data.Swap(newData);    m_data.Swap(newData);
175      // set the corresponding DataArrayView
176    DataArrayView temp(m_data.getData(),shape);    DataArrayView temp(m_data.getData(),shape);
177    setPointDataView(temp);    setPointDataView(temp);
178  }  }
# Line 179  DataExpanded::setSlice(const DataAbstrac Line 192  DataExpanded::setSlice(const DataAbstrac
192      throw DataException("Programming error - casting to DataExpanded.");      throw DataException("Programming error - casting to DataExpanded.");
193    }    }
194    //    //
195      // get shape of slice
196    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
197    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
198    //    //
199    // check shape:    // check shape
200    if (getPointDataView().getRank()!=region.size()) {    if (getPointDataView().getRank()!=region.size()) {
201      throw DataException("Error - Invalid slice region.");      throw DataException("Error - Invalid slice region.");
202    }    }
# Line 191  DataExpanded::setSlice(const DataAbstrac Line 205  DataExpanded::setSlice(const DataAbstrac
205          "Error - Couldn't copy slice due to shape mismatch.",shape));          "Error - Couldn't copy slice due to shape mismatch.",shape));
206    }    }
207    //    //
208    // copy the data    // copy the data from the slice into this object
209    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
210    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
211    int i, j;    int i, j;
# Line 207  void Line 221  void
221  DataExpanded::copy(const DataArrayView& value)  DataExpanded::copy(const DataArrayView& value)
222  {  {
223    //    //
224    // Copy a single value to every data point    // copy a single value to every data point in this object
225    int nRows=m_data.getNumRows();    int nRows=m_data.getNumRows();
226    int nCols=m_data.getNumCols();    int nCols=m_data.getNumCols();
227    int i, j;    int i,j;
228  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
229    for (i=0;i<nRows;++i) {    for (i=0;i<nRows;i++) {
230      for (j=0;j<nCols;++j) {      for (j=0;j<nCols;j++) {
       //  
       // Copy the data into the specified offset  
231        // NOTE: An exception may be thown from this call if        // NOTE: An exception may be thown from this call if
232        // DOASSERT is on which of course will play        // DOASSERT is on which of course will play
233        // havoc with the omp threads. Run single threaded        // havoc with the omp threads. Run single threaded
# Line 229  void Line 241  void
241  DataExpanded::copy(const boost::python::numeric::array& value)  DataExpanded::copy(const boost::python::numeric::array& value)
242  {  {
243    //    //
244    // first convert the numarray into a DataArrayView format    // first convert the numarray into a DataArray object
245    DataArray temp(value);    DataArray temp(value);
246    //    //
247    // check the input shape matches this shape, this will throw an exception    // check the input shape matches this shape
248    if (!getPointDataView().checkShape(temp.getView().getShape())) {    if (!getPointDataView().checkShape(temp.getView().getShape())) {
249      throw DataException(getPointDataView().createShapeErrorMessage(      throw DataException(getPointDataView().createShapeErrorMessage(
250                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",                          "Error - (DataExpanded) Cannot copy due to shape mismatch.",
251                          temp.getView().getShape()));                          temp.getView().getShape()));
252    }    }
253    //    //
254    // now copy over the entire data structure    // now copy over the data
255    copy(temp.getView());    copy(temp.getView());
256  }  }
257    
# Line 249  DataExpanded::initialise(const DataArray Line 261  DataExpanded::initialise(const DataArray
261                           int noDataPointsPerSample)                           int noDataPointsPerSample)
262  {  {
263    //    //
264    // resize data to the required size    // resize data array to the required size
265    m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));    m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));
266    //    //
267    // create a point data viewer of the data    // create the data view of the data array
268    DataArrayView temp(m_data.getData(),shape);    DataArrayView temp(m_data.getData(),shape);
269    setPointDataView(temp);    setPointDataView(temp);
270  }  }
# Line 264  DataExpanded::toString() const Line 276  DataExpanded::toString() const
276    //    //
277    // create a temporary view as the offset will be changed    // create a temporary view as the offset will be changed
278    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());    DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
279    for (int i=0;i<m_data.getNumRows();++i) {    for (int i=0;i<m_data.getNumRows();i++) {
280      for (int j=0;j<m_data.getNumCols();++j) {      for (int j=0;j<m_data.getNumCols();j++) {
281        tempView.setOffset(m_data.index(i,j));        tempView.setOffset(m_data.index(i,j));
282        stringstream suffix;        stringstream suffix;
283        suffix << "(" << i << "," << j << ")";        suffix << "(" << i << "," << j << ")";
# Line 296  DataExpanded::getDataPoint(int sampleNo, Line 308  DataExpanded::getDataPoint(int sampleNo,
308  DataArrayView::ValueType::size_type  DataArrayView::ValueType::size_type
309  DataExpanded::getLength() const  DataExpanded::getLength() const
310  {  {
311    return m_data.getData().size();    return m_data.size();
312  }  }
313    
314  void  void
# Line 327  DataExpanded::setRefValue(int ref, Line 339  DataExpanded::setRefValue(int ref,
339      //      //
340      // Get each data-point in the sample in turn.      // Get each data-point in the sample in turn.
341      DataArrayView pointView = getDataPoint(sampleNo, n);      DataArrayView pointView = getDataPoint(sampleNo, n);
   
342      //      //
343      // Assign the values in the DataArray to this data-point.      // Assign the values in the DataArray to this data-point.
344      pointView.copy(value.getView());      pointView.copy(value.getView());

Legend:
Removed from v.116  
changed lines
  Added in v.117

  ViewVC Help
Powered by ViewVC 1.1.26