1 |
jgs |
102 |
// $Id$ |
2 |
jgs |
82 |
/* |
3 |
|
|
****************************************************************************** |
4 |
|
|
* * |
5 |
|
|
* COPYRIGHT ACcESS 2004 - All Rights Reserved * |
6 |
|
|
* * |
7 |
|
|
* This software is the property of ACcESS. No part of this code * |
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 * |
10 |
|
|
* by any unauthorised person is illegal unless that person has a software * |
11 |
|
|
* license agreement with ACcESS. * |
12 |
|
|
* * |
13 |
|
|
****************************************************************************** |
14 |
|
|
*/ |
15 |
|
|
|
16 |
|
|
#include "escript/Data/DataException.h" |
17 |
|
|
#include "escript/Data/DataExpanded.h" |
18 |
|
|
#include "escript/Data/DataConstant.h" |
19 |
|
|
#include "escript/Data/DataTagged.h" |
20 |
|
|
#include "escript/Data/DataArrayView.h" |
21 |
|
|
|
22 |
|
|
#include <boost/python/extract.hpp> |
23 |
|
|
|
24 |
|
|
#include <iostream> |
25 |
|
|
|
26 |
|
|
using namespace std; |
27 |
|
|
using namespace boost::python; |
28 |
|
|
using namespace boost; |
29 |
|
|
|
30 |
|
|
namespace escript { |
31 |
|
|
|
32 |
jgs |
102 |
DataExpanded::DataExpanded(const boost::python::numeric::array& value, |
33 |
|
|
const FunctionSpace& what) |
34 |
|
|
: DataAbstract(what) |
35 |
|
|
{ |
36 |
|
|
DataArrayView::ShapeType tempShape; |
37 |
|
|
// |
38 |
|
|
// extract the shape from the python array |
39 |
|
|
for (int i=0; i<value.getrank(); ++i) { |
40 |
|
|
//cout << extract<int>(value.getshape()[i]) << endl; |
41 |
|
|
tempShape.push_back(extract<int>(value.getshape()[i])); |
42 |
jgs |
82 |
} |
43 |
jgs |
102 |
initialise(tempShape,what.getNumSamples(),what.getNumDPPSample()); |
44 |
|
|
// |
45 |
|
|
// copy the given value to every data point |
46 |
|
|
copy(value); |
47 |
|
|
} |
48 |
jgs |
82 |
|
49 |
jgs |
102 |
DataExpanded::DataExpanded(const DataExpanded& other) |
50 |
|
|
: DataAbstract(other.getFunctionSpace()), |
51 |
|
|
m_data(other.m_data) |
52 |
|
|
{ |
53 |
|
|
// |
54 |
|
|
// create the view for the data |
55 |
|
|
DataArrayView temp(m_data.getData(),other.getPointDataView().getShape()); |
56 |
|
|
setPointDataView(temp); |
57 |
|
|
} |
58 |
jgs |
82 |
|
59 |
jgs |
102 |
DataExpanded::DataExpanded(const DataConstant& other) |
60 |
|
|
: DataAbstract(other.getFunctionSpace()) |
61 |
|
|
{ |
62 |
|
|
initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample()); |
63 |
|
|
// |
64 |
|
|
// DataConstant only has one value |
65 |
|
|
copy(other.getPointDataView()); |
66 |
|
|
} |
67 |
jgs |
82 |
|
68 |
jgs |
102 |
DataExpanded::DataExpanded(const DataTagged& other) |
69 |
|
|
: DataAbstract(other.getFunctionSpace()) |
70 |
|
|
{ |
71 |
|
|
initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample()); |
72 |
|
|
int i,j; |
73 |
|
|
DataArrayView::ValueType::size_type numRows=m_data.getNumRows(); |
74 |
|
|
DataArrayView::ValueType::size_type numCols=m_data.getNumCols(); |
75 |
jgs |
82 |
#pragma omp parallel for private(i,j) schedule(static) |
76 |
jgs |
102 |
for (i=0;i<numRows;++i) { |
77 |
|
|
for (j=0;j<numCols;++j) { |
78 |
|
|
try { |
79 |
|
|
getPointDataView().copy(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j)); |
80 |
jgs |
82 |
} |
81 |
jgs |
102 |
catch (std::exception& e) { |
82 |
|
|
cout << e.what() << endl; |
83 |
|
|
} |
84 |
jgs |
82 |
} |
85 |
|
|
} |
86 |
jgs |
102 |
} |
87 |
jgs |
82 |
|
88 |
jgs |
102 |
DataExpanded::DataExpanded(const DataExpanded& other, |
89 |
|
|
const DataArrayView::RegionType& region) |
90 |
|
|
: DataAbstract(other.getFunctionSpace()) |
91 |
|
|
{ |
92 |
|
|
// |
93 |
|
|
// get the shape of the slice |
94 |
|
|
DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region)); |
95 |
|
|
// |
96 |
|
|
// initialise this Data object to the shape of the slice |
97 |
|
|
initialise(shape,other.getNumSamples(),other.getNumDPPSample()); |
98 |
|
|
// |
99 |
jgs |
108 |
DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region); |
100 |
|
|
// |
101 |
jgs |
102 |
// copy the data |
102 |
|
|
DataArrayView::ValueType::size_type numRows=m_data.getNumRows(); |
103 |
|
|
DataArrayView::ValueType::size_type numCols=m_data.getNumCols(); |
104 |
|
|
int i,j; |
105 |
jgs |
82 |
#pragma omp parallel for private(i,j) schedule(static) |
106 |
jgs |
102 |
for (i=0;i<numRows;++i) { |
107 |
|
|
for (j=0;j<numCols;++j) { |
108 |
|
|
try { |
109 |
jgs |
108 |
getPointDataView().copySlice(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j),region_loop_range); |
110 |
jgs |
82 |
} |
111 |
jgs |
102 |
catch (std::exception& e) { |
112 |
|
|
cout << e.what() << endl; |
113 |
|
|
} |
114 |
jgs |
82 |
} |
115 |
|
|
} |
116 |
jgs |
102 |
} |
117 |
jgs |
82 |
|
118 |
jgs |
102 |
DataExpanded::DataExpanded(const DataArrayView& value, |
119 |
|
|
const FunctionSpace& what) |
120 |
|
|
: DataAbstract(what) |
121 |
|
|
{ |
122 |
|
|
DataArrayView::ShapeType tempShape=value.getShape(); |
123 |
|
|
initialise(tempShape,what.getNumSamples(),what.getNumDPPSample()); |
124 |
|
|
copy(value); |
125 |
|
|
} |
126 |
jgs |
82 |
|
127 |
jgs |
102 |
DataExpanded::~DataExpanded() |
128 |
|
|
{ |
129 |
|
|
} |
130 |
jgs |
82 |
|
131 |
jgs |
102 |
void |
132 |
|
|
DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape) |
133 |
|
|
{ |
134 |
|
|
// |
135 |
|
|
// reshape a rank zero data point |
136 |
|
|
if (getPointDataView().getRank()!=0) { |
137 |
|
|
stringstream temp; |
138 |
|
|
temp << "Error - Can only reshape Data with data points of rank 0. " |
139 |
|
|
<< "This Data has data points with rank: " |
140 |
|
|
<< getPointDataView().getRank(); |
141 |
|
|
throw DataException(temp.str()); |
142 |
jgs |
82 |
} |
143 |
jgs |
102 |
DataBlocks2D newData(getNumSamples(),getNumDPPSample(), DataArrayView::noValues(shape)); |
144 |
|
|
DataArrayView newView(newData.getData(),shape); |
145 |
|
|
// |
146 |
|
|
// Copy the original data to every value for the new shape |
147 |
|
|
int i,j; |
148 |
|
|
int nRows=m_data.getNumRows(); |
149 |
|
|
int nCols=m_data.getNumCols(); |
150 |
|
|
#pragma omp parallel for private(i,j) schedule(static) |
151 |
|
|
for (i=0;i<nRows;++i) { |
152 |
|
|
for (j=0;j<nCols;++j) { |
153 |
|
|
// |
154 |
|
|
// Copy the data into the specified offset |
155 |
|
|
// NOTE: An exception may be thown from this call if |
156 |
|
|
// DOASSERT is on which of course will play |
157 |
|
|
// havoc with the omp threads. Run single threaded |
158 |
|
|
// if using DOASSERT. |
159 |
|
|
newView.copy(newData.index(i,j),m_data.getData()[m_data.index(i,j)]); |
160 |
jgs |
82 |
} |
161 |
|
|
} |
162 |
jgs |
102 |
m_data.Swap(newData); |
163 |
|
|
DataArrayView temp(m_data.getData(),shape); |
164 |
|
|
setPointDataView(temp); |
165 |
|
|
} |
166 |
jgs |
82 |
|
167 |
jgs |
102 |
DataAbstract* |
168 |
|
|
DataExpanded::getSlice(const DataArrayView::RegionType& region) const |
169 |
|
|
{ |
170 |
|
|
return new DataExpanded(*this,region); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
void |
174 |
|
|
DataExpanded::setSlice(const DataAbstract* value, |
175 |
|
|
const DataArrayView::RegionType& region) |
176 |
|
|
{ |
177 |
|
|
const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value); |
178 |
|
|
if (tempDataExp==0) { |
179 |
|
|
throw DataException("Programming error - casting to DataExpanded."); |
180 |
|
|
} |
181 |
jgs |
108 |
// |
182 |
|
|
DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region)); |
183 |
|
|
DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region); |
184 |
|
|
// |
185 |
|
|
// check shape: |
186 |
jgs |
102 |
if (getPointDataView().getRank()!=region.size()) { |
187 |
|
|
throw DataException("Error - Invalid slice region."); |
188 |
|
|
} |
189 |
jgs |
108 |
if (tempDataExp->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) { |
190 |
jgs |
102 |
throw DataException (value->getPointDataView().createShapeErrorMessage( |
191 |
jgs |
108 |
"Error - Couldn't copy slice due to shape mismatch.",shape)); |
192 |
jgs |
102 |
} |
193 |
|
|
// |
194 |
|
|
// copy the data |
195 |
|
|
DataArrayView::ValueType::size_type numRows=m_data.getNumRows(); |
196 |
|
|
DataArrayView::ValueType::size_type numCols=m_data.getNumCols(); |
197 |
|
|
int i, j; |
198 |
jgs |
82 |
#pragma omp parallel for private(i,j) schedule(static) |
199 |
jgs |
102 |
for (i=0;i<numRows;i++) { |
200 |
|
|
for (j=0;j<numCols;j++) { |
201 |
jgs |
108 |
getPointDataView().copySliceFrom(getPointOffset(i,j),tempDataExp->getPointDataView(),tempDataExp->getPointOffset(i,j),region_loop_range); |
202 |
jgs |
82 |
} |
203 |
|
|
} |
204 |
jgs |
102 |
} |
205 |
jgs |
82 |
|
206 |
jgs |
102 |
void |
207 |
|
|
DataExpanded::copy(const DataArrayView& value) |
208 |
|
|
{ |
209 |
|
|
// |
210 |
|
|
// Copy a single value to every data point |
211 |
|
|
int nRows=m_data.getNumRows(); |
212 |
|
|
int nCols=m_data.getNumCols(); |
213 |
|
|
int i, j; |
214 |
|
|
#pragma omp parallel for private(i,j) schedule(static) |
215 |
|
|
for (i=0;i<nRows;++i) { |
216 |
|
|
for (j=0;j<nCols;++j) { |
217 |
|
|
// |
218 |
|
|
// Copy the data into the specified offset |
219 |
|
|
// NOTE: An exception may be thown from this call if |
220 |
|
|
// DOASSERT is on which of course will play |
221 |
|
|
// havoc with the omp threads. Run single threaded |
222 |
|
|
// if using DOASSERT. |
223 |
|
|
getPointDataView().copy(m_data.index(i,j),value); |
224 |
jgs |
82 |
} |
225 |
|
|
} |
226 |
jgs |
102 |
} |
227 |
jgs |
82 |
|
228 |
jgs |
102 |
void |
229 |
|
|
DataExpanded::copy(const boost::python::numeric::array& value) |
230 |
|
|
{ |
231 |
|
|
// |
232 |
|
|
// first convert the numarray into a DataArrayView format |
233 |
|
|
DataArray temp(value); |
234 |
|
|
// |
235 |
|
|
// check the input shape matches this shape, this will throw an exception |
236 |
|
|
if (!getPointDataView().checkShape(temp.getView().getShape())) { |
237 |
|
|
throw DataException(getPointDataView().createShapeErrorMessage( |
238 |
|
|
"Error - (DataExpanded) Cannot copy due to shape mismatch.", |
239 |
|
|
temp.getView().getShape())); |
240 |
jgs |
82 |
} |
241 |
jgs |
102 |
// |
242 |
|
|
// now copy over the entire data structure |
243 |
|
|
copy(temp.getView()); |
244 |
|
|
} |
245 |
jgs |
82 |
|
246 |
jgs |
102 |
void |
247 |
|
|
DataExpanded::initialise(const DataArrayView::ShapeType& shape, |
248 |
|
|
int noSamples, |
249 |
|
|
int noDataPointsPerSample) |
250 |
|
|
{ |
251 |
|
|
// |
252 |
|
|
// resize data to the required size |
253 |
|
|
m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape)); |
254 |
|
|
// |
255 |
|
|
// create a point data viewer of the data |
256 |
|
|
DataArrayView temp(m_data.getData(),shape); |
257 |
|
|
setPointDataView(temp); |
258 |
|
|
} |
259 |
jgs |
82 |
|
260 |
jgs |
102 |
string |
261 |
|
|
DataExpanded::toString() const |
262 |
|
|
{ |
263 |
|
|
stringstream temp; |
264 |
|
|
// |
265 |
|
|
// create a temporary view as the offset will be changed |
266 |
|
|
DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset()); |
267 |
|
|
for (int i=0;i<m_data.getNumRows();++i) { |
268 |
|
|
for (int j=0;j<m_data.getNumCols();++j) { |
269 |
|
|
tempView.setOffset(m_data.index(i,j)); |
270 |
|
|
stringstream suffix; |
271 |
|
|
suffix << "(" << i << "," << j << ")"; |
272 |
|
|
temp << tempView.toString(suffix.str()); |
273 |
|
|
if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) { |
274 |
|
|
temp << endl; |
275 |
jgs |
82 |
} |
276 |
|
|
} |
277 |
|
|
} |
278 |
jgs |
102 |
return temp.str(); |
279 |
|
|
} |
280 |
jgs |
82 |
|
281 |
jgs |
102 |
DataArrayView::ValueType::size_type |
282 |
|
|
DataExpanded::getPointOffset(int sampleNo, |
283 |
|
|
int dataPointNo) const |
284 |
|
|
{ |
285 |
|
|
return m_data.index(sampleNo,dataPointNo); |
286 |
|
|
} |
287 |
jgs |
82 |
|
288 |
jgs |
102 |
DataArrayView |
289 |
|
|
DataExpanded::getDataPoint(int sampleNo, |
290 |
|
|
int dataPointNo) |
291 |
|
|
{ |
292 |
|
|
DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo)); |
293 |
|
|
return temp; |
294 |
|
|
} |
295 |
jgs |
82 |
|
296 |
jgs |
102 |
DataArrayView::ValueType::size_type |
297 |
|
|
DataExpanded::getLength() const |
298 |
|
|
{ |
299 |
|
|
return m_data.getData().size(); |
300 |
|
|
} |
301 |
jgs |
82 |
|
302 |
|
|
} // end of namespace |