/[escript]/branches/RW_WIN32/escript/src/Data/DataExpanded.cpp
ViewVC logotype

Annotation of /branches/RW_WIN32/escript/src/Data/DataExpanded.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 160 - (hide annotations)
Thu Nov 10 05:09:25 2005 UTC (14 years ago) by jgs
Original Path: trunk/escript/src/Data/DataExpanded.cpp
File size: 15173 byte(s)
fixed a broken constructor - simply didn't work

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 jgs 160 // create the view of the data
152     initialise(shape,what.getNumSamples(),what.getNumDPPSample());
153     //
154 jgs 119 // copy the data in the correct format
155     m_data.getData()=data;
156     }
157    
158 jgs 102 DataExpanded::~DataExpanded()
159     {
160     }
161 jgs 82
162 jgs 102 void
163     DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
164     {
165     if (getPointDataView().getRank()!=0) {
166     stringstream temp;
167     temp << "Error - Can only reshape Data with data points of rank 0. "
168     << "This Data has data points with rank: "
169     << getPointDataView().getRank();
170     throw DataException(temp.str());
171 jgs 82 }
172 jgs 117 //
173     // create the new DataBlocks2D data array, and a corresponding DataArrayView
174     DataBlocks2D newData(getNumSamples(),getNumDPPSample(),DataArrayView::noValues(shape));
175 jgs 102 DataArrayView newView(newData.getData(),shape);
176     //
177     // Copy the original data to every value for the new shape
178     int i,j;
179     int nRows=m_data.getNumRows();
180     int nCols=m_data.getNumCols();
181 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
182 jgs 117 for (i=0;i<nRows;i++) {
183     for (j=0;j<nCols;j++) {
184 jgs 102 // NOTE: An exception may be thown from this call if
185     // DOASSERT is on which of course will play
186     // havoc with the omp threads. Run single threaded
187     // if using DOASSERT.
188 jgs 117 newView.copy(newData.index(i,j),m_data(i,j));
189 jgs 82 }
190     }
191 jgs 117 // swap the new data array for the original
192 jgs 102 m_data.Swap(newData);
193 jgs 117 // set the corresponding DataArrayView
194 jgs 102 DataArrayView temp(m_data.getData(),shape);
195     setPointDataView(temp);
196     }
197 jgs 82
198 jgs 102 DataAbstract*
199     DataExpanded::getSlice(const DataArrayView::RegionType& region) const
200     {
201     return new DataExpanded(*this,region);
202     }
203    
204     void
205     DataExpanded::setSlice(const DataAbstract* value,
206     const DataArrayView::RegionType& region)
207     {
208     const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
209     if (tempDataExp==0) {
210     throw DataException("Programming error - casting to DataExpanded.");
211     }
212 jgs 108 //
213 jgs 117 // get shape of slice
214 jgs 108 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
215     DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
216     //
217 jgs 117 // check shape
218 jgs 102 if (getPointDataView().getRank()!=region.size()) {
219     throw DataException("Error - Invalid slice region.");
220     }
221 jgs 108 if (tempDataExp->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
222 jgs 102 throw DataException (value->getPointDataView().createShapeErrorMessage(
223 jgs 108 "Error - Couldn't copy slice due to shape mismatch.",shape));
224 jgs 102 }
225     //
226 jgs 117 // copy the data from the slice into this object
227 jgs 102 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
228     DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
229     int i, j;
230 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
231 jgs 102 for (i=0;i<numRows;i++) {
232     for (j=0;j<numCols;j++) {
233 jgs 122 getPointDataView().copySliceFrom(getPointOffset(i,j),
234     tempDataExp->getPointDataView(),
235     tempDataExp->getPointOffset(i,j),
236     region_loop_range);
237 jgs 82 }
238     }
239 jgs 102 }
240 jgs 82
241 jgs 102 void
242     DataExpanded::copy(const DataArrayView& value)
243     {
244     //
245 jgs 117 // copy a single value to every data point in this object
246 jgs 102 int nRows=m_data.getNumRows();
247     int nCols=m_data.getNumCols();
248 jgs 117 int i,j;
249 jgs 122 #pragma omp parallel for private(i,j) schedule(static)
250 jgs 117 for (i=0;i<nRows;i++) {
251     for (j=0;j<nCols;j++) {
252 jgs 102 // NOTE: An exception may be thown from this call if
253     // DOASSERT is on which of course will play
254     // havoc with the omp threads. Run single threaded
255     // if using DOASSERT.
256     getPointDataView().copy(m_data.index(i,j),value);
257 jgs 82 }
258     }
259 jgs 102 }
260 jgs 82
261 jgs 102 void
262     DataExpanded::copy(const boost::python::numeric::array& value)
263     {
264     //
265 jgs 117 // first convert the numarray into a DataArray object
266 jgs 102 DataArray temp(value);
267     //
268 jgs 117 // check the input shape matches this shape
269 jgs 102 if (!getPointDataView().checkShape(temp.getView().getShape())) {
270     throw DataException(getPointDataView().createShapeErrorMessage(
271     "Error - (DataExpanded) Cannot copy due to shape mismatch.",
272     temp.getView().getShape()));
273 jgs 82 }
274 jgs 102 //
275 jgs 117 // now copy over the data
276 jgs 102 copy(temp.getView());
277     }
278 jgs 82
279 jgs 102 void
280     DataExpanded::initialise(const DataArrayView::ShapeType& shape,
281     int noSamples,
282     int noDataPointsPerSample)
283     {
284     //
285 jgs 117 // resize data array to the required size
286 jgs 102 m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));
287     //
288 jgs 117 // create the data view of the data array
289 jgs 102 DataArrayView temp(m_data.getData(),shape);
290     setPointDataView(temp);
291     }
292 jgs 82
293 jgs 102 string
294     DataExpanded::toString() const
295     {
296     stringstream temp;
297     //
298     // create a temporary view as the offset will be changed
299     DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
300 jgs 117 for (int i=0;i<m_data.getNumRows();i++) {
301     for (int j=0;j<m_data.getNumCols();j++) {
302 jgs 102 tempView.setOffset(m_data.index(i,j));
303     stringstream suffix;
304     suffix << "(" << i << "," << j << ")";
305     temp << tempView.toString(suffix.str());
306     if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
307     temp << endl;
308 jgs 82 }
309     }
310     }
311 jgs 102 return temp.str();
312     }
313 jgs 82
314 jgs 102 DataArrayView::ValueType::size_type
315     DataExpanded::getPointOffset(int sampleNo,
316     int dataPointNo) const
317     {
318     return m_data.index(sampleNo,dataPointNo);
319     }
320 jgs 82
321 jgs 102 DataArrayView
322     DataExpanded::getDataPoint(int sampleNo,
323     int dataPointNo)
324     {
325     DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo));
326     return temp;
327     }
328 jgs 82
329 jgs 102 DataArrayView::ValueType::size_type
330     DataExpanded::getLength() const
331     {
332 jgs 117 return m_data.size();
333 jgs 102 }
334 jgs 82
335 jgs 110 void
336     DataExpanded::setRefValue(int ref,
337     const DataArray& value)
338     {
339     //
340 jgs 113 // Get the number of samples and data-points per sample.
341 jgs 110 int numSamples = getNumSamples();
342     int numDPPSample = getNumDPPSample();
343    
344     //
345 jgs 113 // Determine the sample number which corresponds to this reference number.
346 jgs 110 int sampleNo = -1;
347     int tempRef = -1;
348     for (int n=0; n<numSamples; n++) {
349     tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
350     if (tempRef == ref) {
351     sampleNo = n;
352     break;
353     }
354     }
355     if (sampleNo == -1) {
356     throw DataException("DataExpanded::setRefValue error: invalid ref number supplied.");
357     }
358    
359 jgs 113 for (int n=0; n<numDPPSample; n++) {
360     //
361 jgs 126 // Get *each* data-point in the sample in turn.
362 jgs 113 DataArrayView pointView = getDataPoint(sampleNo, n);
363     //
364     // Assign the values in the DataArray to this data-point.
365     pointView.copy(value.getView());
366     }
367 jgs 110 }
368    
369     void
370     DataExpanded::getRefValue(int ref,
371     DataArray& value)
372     {
373     //
374 jgs 113 // Get the number of samples and data-points per sample.
375 jgs 110 int numSamples = getNumSamples();
376     int numDPPSample = getNumDPPSample();
377    
378     //
379 jgs 113 // Determine the sample number which corresponds to this reference number
380 jgs 110 int sampleNo = -1;
381     int tempRef = -1;
382     for (int n=0; n<numSamples; n++) {
383     tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
384     if (tempRef == ref) {
385     sampleNo = n;
386     break;
387     }
388     }
389     if (sampleNo == -1) {
390     throw DataException("DataExpanded::getRefValue error: invalid ref number supplied.");
391     }
392    
393 jgs 113 //
394 jgs 126 // Get the *first* data-point associated with this sample number.
395 jgs 110 DataArrayView pointView = getDataPoint(sampleNo, 0);
396    
397     //
398     // Load the values from this data-point into the DataArray
399     value.getView().copy(pointView);
400     }
401    
402 jgs 123 int
403     DataExpanded::archiveData(ofstream& archiveFile,
404     const DataArrayView::ValueType::size_type noValues) const
405     {
406     return(m_data.archiveData(archiveFile, noValues));
407     }
408    
409     int
410     DataExpanded::extractData(ifstream& archiveFile,
411     const DataArrayView::ValueType::size_type noValues)
412     {
413     return(m_data.extractData(archiveFile, noValues));
414     }
415    
416 jgs 126 void
417     DataExpanded::copyAll(const boost::python::numeric::array& value) {
418     //
419     // Get the number of samples and data-points per sample.
420     int numSamples = getNumSamples();
421     int numDataPointsPerSample = getNumDPPSample();
422     int dataPointRank = getPointDataView().getRank();
423     ShapeType dataPointShape = getPointDataView().getShape();
424     //
425     // check rank:
426     if (value.getrank()!=dataPointRank+1)
427     throw DataException("Rank of numarray does not match Data object rank");
428     if (value.getshape()[0]!=numSamples*numDataPointsPerSample)
429     throw DataException("leading dimension of numarray is too small");
430     //
431     int dataPoint = 0;
432     for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {
433     for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
434     DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
435     if (dataPointRank==0) {
436     dataPointView()=extract<double>(value[dataPoint]);
437     } else if (dataPointRank==1) {
438     for (int i=0; i<dataPointShape[0]; i++) {
439     dataPointView(i)=extract<double>(value[dataPoint][i]);
440     }
441     } else if (dataPointRank==2) {
442     for (int i=0; i<dataPointShape[0]; i++) {
443     for (int j=0; j<dataPointShape[1]; j++) {
444     dataPointView(i,j)=extract<double>(value[dataPoint][i][j]);
445     }
446     }
447     } else if (dataPointRank==3) {
448     for (int i=0; i<dataPointShape[0]; i++) {
449     for (int j=0; j<dataPointShape[1]; j++) {
450     for (int k=0; k<dataPointShape[2]; k++) {
451     dataPointView(i,j,k)=extract<double>(value[dataPoint][i][j][k]);
452     }
453     }
454     }
455     } else if (dataPointRank==4) {
456     for (int i=0; i<dataPointShape[0]; i++) {
457     for (int j=0; j<dataPointShape[1]; j++) {
458     for (int k=0; k<dataPointShape[2]; k++) {
459     for (int l=0; l<dataPointShape[3]; l++) {
460     dataPointView(i,j,k,l)=extract<double>(value[dataPoint][i][j][k][l]);
461     }
462     }
463     }
464     }
465     }
466     dataPoint++;
467     }
468     }
469     }
470 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