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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (hide annotations)
Tue Apr 12 04:45:05 2005 UTC (14 years, 8 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataExpanded.cpp
File size: 12383 byte(s)
*** empty log message ***

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