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

Contents of /trunk/esys2/escript/src/Data/DataExpanded.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (show annotations)
Mon Feb 28 07:06:33 2005 UTC (14 years, 1 month ago) by jgs
File size: 11563 byte(s)
*** empty log message ***

1 // $Id$
2 /*
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 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 }
43 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
44 //
45 // copy the given value to every data point
46 copy(value);
47 }
48
49 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
59 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
68 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 #pragma omp parallel for private(i,j) schedule(static)
76 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 }
81 catch (std::exception& e) {
82 cout << e.what() << endl;
83 }
84 }
85 }
86 }
87
88 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 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
100 //
101 // 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 #pragma omp parallel for private(i,j) schedule(static)
106 for (i=0;i<numRows;++i) {
107 for (j=0;j<numCols;++j) {
108 try {
109 getPointDataView().copySlice(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j),region_loop_range);
110 }
111 catch (std::exception& e) {
112 cout << e.what() << endl;
113 }
114 }
115 }
116 }
117
118 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
127 DataExpanded::~DataExpanded()
128 {
129 }
130
131 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 }
143 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 }
161 }
162 m_data.Swap(newData);
163 DataArrayView temp(m_data.getData(),shape);
164 setPointDataView(temp);
165 }
166
167 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 //
182 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
183 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
184 //
185 // check shape:
186 if (getPointDataView().getRank()!=region.size()) {
187 throw DataException("Error - Invalid slice region.");
188 }
189 if (tempDataExp->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
190 throw DataException (value->getPointDataView().createShapeErrorMessage(
191 "Error - Couldn't copy slice due to shape mismatch.",shape));
192 }
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 #pragma omp parallel for private(i,j) schedule(static)
199 for (i=0;i<numRows;i++) {
200 for (j=0;j<numCols;j++) {
201 getPointDataView().copySliceFrom(getPointOffset(i,j),tempDataExp->getPointDataView(),tempDataExp->getPointOffset(i,j),region_loop_range);
202 }
203 }
204 }
205
206 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 }
225 }
226 }
227
228 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 }
241 //
242 // now copy over the entire data structure
243 copy(temp.getView());
244 }
245
246 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
260 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 }
276 }
277 }
278 return temp.str();
279 }
280
281 DataArrayView::ValueType::size_type
282 DataExpanded::getPointOffset(int sampleNo,
283 int dataPointNo) const
284 {
285 return m_data.index(sampleNo,dataPointNo);
286 }
287
288 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
296 DataArrayView::ValueType::size_type
297 DataExpanded::getLength() const
298 {
299 return m_data.getData().size();
300 }
301
302 void
303 DataExpanded::setRefValue(int ref,
304 const DataArray& value)
305 {
306 //
307 // Get the number of samples and data-points per sample.
308 int numSamples = getNumSamples();
309 int numDPPSample = getNumDPPSample();
310
311 //
312 // Determine the sample number which corresponds to this reference number.
313 int sampleNo = -1;
314 int tempRef = -1;
315 for (int n=0; n<numSamples; n++) {
316 tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
317 if (tempRef == ref) {
318 sampleNo = n;
319 break;
320 }
321 }
322 if (sampleNo == -1) {
323 throw DataException("DataExpanded::setRefValue error: invalid ref number supplied.");
324 }
325
326 for (int n=0; n<numDPPSample; n++) {
327 //
328 // Get each data-point in the sample in turn.
329 DataArrayView pointView = getDataPoint(sampleNo, n);
330
331 //
332 // Assign the values in the DataArray to this data-point.
333 pointView.copy(value.getView());
334 }
335 }
336
337 void
338 DataExpanded::getRefValue(int ref,
339 DataArray& value)
340 {
341 //
342 // Get the number of samples and data-points per sample.
343 int numSamples = getNumSamples();
344 int numDPPSample = getNumDPPSample();
345
346 //
347 // Determine the sample number which corresponds to this reference number
348 int sampleNo = -1;
349 int tempRef = -1;
350 for (int n=0; n<numSamples; n++) {
351 tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
352 if (tempRef == ref) {
353 sampleNo = n;
354 break;
355 }
356 }
357 if (sampleNo == -1) {
358 throw DataException("DataExpanded::getRefValue error: invalid ref number supplied.");
359 }
360
361 //
362 // Get the first data-point associated with this sample number.
363 DataArrayView pointView = getDataPoint(sampleNo, 0);
364
365 //
366 // Load the values from this data-point into the DataArray
367 value.getView().copy(pointView);
368 }
369
370 } // end of namespace

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26