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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 122 - (hide annotations)
Thu Jun 9 05:38:05 2005 UTC (14 years, 4 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataExpanded.cpp
File size: 12693 byte(s)
Merge of development branch back to main trunk on 2005-06-09

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 jgs 117 // extract the shape of the python numarray
39     for (int i=0; i<value.getrank(); i++) {
40 jgs 102 tempShape.push_back(extract<int>(value.getshape()[i]));
41 jgs 82 }
42 jgs 117 //
43     // initialise the data array for this object
44 jgs 102 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
45     //
46     // copy the given value to every data point
47     copy(value);
48     }
49 jgs 82
50 jgs 102 DataExpanded::DataExpanded(const DataExpanded& other)
51     : DataAbstract(other.getFunctionSpace()),
52     m_data(other.m_data)
53 jgs 117 {
54 jgs 102 //
55     // create the view for the data
56     DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());
57     setPointDataView(temp);
58     }
59 jgs 82
60 jgs 102 DataExpanded::DataExpanded(const DataConstant& other)
61     : DataAbstract(other.getFunctionSpace())
62 jgs 117 {
63     //
64     // initialise the data array for this object
65 jgs 102 initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
66     //
67 jgs 117 // DataConstant only has one value, copy this to every data point
68 jgs 102 copy(other.getPointDataView());
69     }
70 jgs 82
71 jgs 102 DataExpanded::DataExpanded(const DataTagged& other)
72     : DataAbstract(other.getFunctionSpace())
73 jgs 117 {
74     //
75 jgs 119 // initialise the data array for this object
76 jgs 102 initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
77 jgs 117 //
78     // for each data point in this object, extract and copy the corresponding data
79     // value from the given DataTagged object
80 jgs 102 int i,j;
81     DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
82     DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
83 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
84 jgs 117 for (i=0;i<numRows;i++) {
85     for (j=0;j<numCols;j++) {
86 jgs 102 try {
87 jgs 122 getPointDataView().copy(getPointOffset(i,j),
88     other.getPointDataView(),
89     other.getPointOffset(i,j));
90 jgs 82 }
91 jgs 102 catch (std::exception& e) {
92     cout << e.what() << endl;
93     }
94 jgs 82 }
95     }
96 jgs 102 }
97 jgs 82
98 jgs 102 DataExpanded::DataExpanded(const DataExpanded& other,
99     const DataArrayView::RegionType& region)
100     : DataAbstract(other.getFunctionSpace())
101     {
102     //
103     // get the shape of the slice
104     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
105     //
106     // initialise this Data object to the shape of the slice
107     initialise(shape,other.getNumSamples(),other.getNumDPPSample());
108     //
109 jgs 117 // copy the data
110 jgs 108 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
111 jgs 102 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
112     DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
113     int i,j;
114 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
115 jgs 117 for (i=0;i<numRows;i++) {
116     for (j=0;j<numCols;j++) {
117 jgs 102 try {
118 jgs 122 getPointDataView().copySlice(getPointOffset(i,j),
119     other.getPointDataView(),
120     other.getPointOffset(i,j),
121     region_loop_range);
122 jgs 82 }
123 jgs 102 catch (std::exception& e) {
124     cout << e.what() << endl;
125     }
126 jgs 82 }
127     }
128 jgs 102 }
129 jgs 82
130 jgs 102 DataExpanded::DataExpanded(const DataArrayView& value,
131     const FunctionSpace& what)
132     : DataAbstract(what)
133     {
134 jgs 117 //
135     // get the shape of the given data value
136 jgs 102 DataArrayView::ShapeType tempShape=value.getShape();
137 jgs 117 //
138     // initialise this Data object to the shape of the given data value
139 jgs 102 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
140 jgs 117 //
141     // copy the given value to every data point
142 jgs 102 copy(value);
143     }
144 jgs 82
145 jgs 119 DataExpanded::DataExpanded(const FunctionSpace& what,
146     const DataArrayView::ShapeType &shape,
147     const DataArrayView::ValueType &data)
148     : DataAbstract(what)
149     {
150     //
151     // copy the data in the correct format
152     m_data.getData()=data;
153     //
154     // create the view of the data
155     DataArrayView tempView(m_data.getData(),shape);
156     setPointDataView(tempView);
157     }
158    
159 jgs 102 DataExpanded::~DataExpanded()
160     {
161     }
162 jgs 82
163 jgs 102 void
164     DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
165     {
166     if (getPointDataView().getRank()!=0) {
167     stringstream temp;
168     temp << "Error - Can only reshape Data with data points of rank 0. "
169     << "This Data has data points with rank: "
170     << getPointDataView().getRank();
171     throw DataException(temp.str());
172 jgs 82 }
173 jgs 117 //
174     // create the new DataBlocks2D data array, and a corresponding DataArrayView
175     DataBlocks2D newData(getNumSamples(),getNumDPPSample(),DataArrayView::noValues(shape));
176 jgs 102 DataArrayView newView(newData.getData(),shape);
177     //
178     // Copy the original data to every value for the new shape
179     int i,j;
180     int nRows=m_data.getNumRows();
181     int nCols=m_data.getNumCols();
182 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
183 jgs 117 for (i=0;i<nRows;i++) {
184     for (j=0;j<nCols;j++) {
185 jgs 102 // NOTE: An exception may be thown from this call if
186     // DOASSERT is on which of course will play
187     // havoc with the omp threads. Run single threaded
188     // if using DOASSERT.
189 jgs 117 newView.copy(newData.index(i,j),m_data(i,j));
190 jgs 82 }
191     }
192 jgs 117 // swap the new data array for the original
193 jgs 102 m_data.Swap(newData);
194 jgs 117 // set the corresponding DataArrayView
195 jgs 102 DataArrayView temp(m_data.getData(),shape);
196     setPointDataView(temp);
197     }
198 jgs 82
199 jgs 102 DataAbstract*
200     DataExpanded::getSlice(const DataArrayView::RegionType& region) const
201     {
202     return new DataExpanded(*this,region);
203     }
204    
205     void
206     DataExpanded::setSlice(const DataAbstract* value,
207     const DataArrayView::RegionType& region)
208     {
209     const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
210     if (tempDataExp==0) {
211     throw DataException("Programming error - casting to DataExpanded.");
212     }
213 jgs 108 //
214 jgs 117 // get shape of slice
215 jgs 108 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
216     DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
217     //
218 jgs 117 // check shape
219 jgs 102 if (getPointDataView().getRank()!=region.size()) {
220     throw DataException("Error - Invalid slice region.");
221     }
222 jgs 108 if (tempDataExp->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
223 jgs 102 throw DataException (value->getPointDataView().createShapeErrorMessage(
224 jgs 108 "Error - Couldn't copy slice due to shape mismatch.",shape));
225 jgs 102 }
226     //
227 jgs 117 // copy the data from the slice into this object
228 jgs 102 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
229     DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
230     int i, j;
231 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
232 jgs 102 for (i=0;i<numRows;i++) {
233     for (j=0;j<numCols;j++) {
234 jgs 122 getPointDataView().copySliceFrom(getPointOffset(i,j),
235     tempDataExp->getPointDataView(),
236     tempDataExp->getPointOffset(i,j),
237     region_loop_range);
238 jgs 82 }
239     }
240 jgs 102 }
241 jgs 82
242 jgs 102 void
243     DataExpanded::copy(const DataArrayView& value)
244     {
245     //
246 jgs 117 // copy a single value to every data point in this object
247 jgs 102 int nRows=m_data.getNumRows();
248     int nCols=m_data.getNumCols();
249 jgs 117 int i,j;
250 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
251 jgs 117 for (i=0;i<nRows;i++) {
252     for (j=0;j<nCols;j++) {
253 jgs 102 // NOTE: An exception may be thown from this call if
254     // DOASSERT is on which of course will play
255     // havoc with the omp threads. Run single threaded
256     // if using DOASSERT.
257     getPointDataView().copy(m_data.index(i,j),value);
258 jgs 82 }
259     }
260 jgs 102 }
261 jgs 82
262 jgs 102 void
263     DataExpanded::copy(const boost::python::numeric::array& value)
264     {
265     //
266 jgs 117 // first convert the numarray into a DataArray object
267 jgs 102 DataArray temp(value);
268     //
269 jgs 117 // check the input shape matches this shape
270 jgs 102 if (!getPointDataView().checkShape(temp.getView().getShape())) {
271     throw DataException(getPointDataView().createShapeErrorMessage(
272     "Error - (DataExpanded) Cannot copy due to shape mismatch.",
273     temp.getView().getShape()));
274 jgs 82 }
275 jgs 102 //
276 jgs 117 // now copy over the data
277 jgs 102 copy(temp.getView());
278     }
279 jgs 82
280 jgs 102 void
281     DataExpanded::initialise(const DataArrayView::ShapeType& shape,
282     int noSamples,
283     int noDataPointsPerSample)
284     {
285     //
286 jgs 117 // resize data array to the required size
287 jgs 102 m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));
288     //
289 jgs 117 // create the data view of the data array
290 jgs 102 DataArrayView temp(m_data.getData(),shape);
291     setPointDataView(temp);
292     }
293 jgs 82
294 jgs 102 string
295     DataExpanded::toString() const
296     {
297     stringstream temp;
298     //
299     // create a temporary view as the offset will be changed
300     DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
301 jgs 117 for (int i=0;i<m_data.getNumRows();i++) {
302     for (int j=0;j<m_data.getNumCols();j++) {
303 jgs 102 tempView.setOffset(m_data.index(i,j));
304     stringstream suffix;
305     suffix << "(" << i << "," << j << ")";
306     temp << tempView.toString(suffix.str());
307     if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
308     temp << endl;
309 jgs 82 }
310     }
311     }
312 jgs 102 return temp.str();
313     }
314 jgs 82
315 jgs 102 DataArrayView::ValueType::size_type
316     DataExpanded::getPointOffset(int sampleNo,
317     int dataPointNo) const
318     {
319     return m_data.index(sampleNo,dataPointNo);
320     }
321 jgs 82
322 jgs 102 DataArrayView
323     DataExpanded::getDataPoint(int sampleNo,
324     int dataPointNo)
325     {
326     DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo));
327     return temp;
328     }
329 jgs 82
330 jgs 102 DataArrayView::ValueType::size_type
331     DataExpanded::getLength() const
332     {
333 jgs 117 return m_data.size();
334 jgs 102 }
335 jgs 82
336 jgs 110 void
337     DataExpanded::setRefValue(int ref,
338     const DataArray& value)
339     {
340     //
341 jgs 113 // Get the number of samples and data-points per sample.
342 jgs 110 int numSamples = getNumSamples();
343     int numDPPSample = getNumDPPSample();
344    
345     //
346 jgs 113 // Determine the sample number which corresponds to this reference number.
347 jgs 110 int sampleNo = -1;
348     int tempRef = -1;
349     for (int n=0; n<numSamples; n++) {
350     tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
351     if (tempRef == ref) {
352     sampleNo = n;
353     break;
354     }
355     }
356     if (sampleNo == -1) {
357     throw DataException("DataExpanded::setRefValue error: invalid ref number supplied.");
358     }
359    
360 jgs 113 for (int n=0; n<numDPPSample; n++) {
361     //
362     // Get each data-point in the sample in turn.
363     DataArrayView pointView = getDataPoint(sampleNo, n);
364     //
365     // Assign the values in the DataArray to this data-point.
366     pointView.copy(value.getView());
367     }
368 jgs 110 }
369    
370     void
371     DataExpanded::getRefValue(int ref,
372     DataArray& value)
373     {
374     //
375 jgs 113 // Get the number of samples and data-points per sample.
376 jgs 110 int numSamples = getNumSamples();
377     int numDPPSample = getNumDPPSample();
378    
379     //
380 jgs 113 // Determine the sample number which corresponds to this reference number
381 jgs 110 int sampleNo = -1;
382     int tempRef = -1;
383     for (int n=0; n<numSamples; n++) {
384     tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
385     if (tempRef == ref) {
386     sampleNo = n;
387     break;
388     }
389     }
390     if (sampleNo == -1) {
391     throw DataException("DataExpanded::getRefValue error: invalid ref number supplied.");
392     }
393    
394 jgs 113 //
395     // Get the first data-point associated with this sample number.
396 jgs 110 DataArrayView pointView = getDataPoint(sampleNo, 0);
397    
398     //
399     // Load the values from this data-point into the DataArray
400     value.getView().copy(pointView);
401     }
402    
403 jgs 82 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26