/[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 97 - (show annotations)
Tue Dec 14 05:39:33 2004 UTC (14 years, 11 months ago) by jgs
File size: 9499 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 // copy the data
100 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
101 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
102 int i,j;
103 #pragma omp parallel for private(i,j) schedule(static)
104 for (i=0;i<numRows;++i) {
105 for (j=0;j<numCols;++j) {
106 try {
107 getPointDataView().copySlice(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j),region);
108 }
109 catch (std::exception& e) {
110 cout << e.what() << endl;
111 }
112 }
113 }
114 }
115
116 DataExpanded::DataExpanded(const DataArrayView& value,
117 const FunctionSpace& what)
118 : DataAbstract(what)
119 {
120 DataArrayView::ShapeType tempShape=value.getShape();
121 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
122 copy(value);
123 }
124
125 DataExpanded::~DataExpanded()
126 {
127 }
128
129 void
130 DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
131 {
132 //
133 // reshape a rank zero data point
134 if (getPointDataView().getRank()!=0) {
135 stringstream temp;
136 temp << "Error - Can only reshape Data with data points of rank 0. "
137 << "This Data has data points with rank: "
138 << getPointDataView().getRank();
139 throw DataException(temp.str());
140 }
141 DataBlocks2D newData(getNumSamples(),getNumDPPSample(), DataArrayView::noValues(shape));
142 DataArrayView newView(newData.getData(),shape);
143 //
144 // Copy the original data to every value for the new shape
145 int i,j;
146 int nRows=m_data.getNumRows();
147 int nCols=m_data.getNumCols();
148 #pragma omp parallel for private(i,j) schedule(static)
149 for (i=0;i<nRows;++i) {
150 for (j=0;j<nCols;++j) {
151 //
152 // Copy the data into the specified offset
153 // NOTE: An exception may be thown from this call if
154 // DOASSERT is on which of course will play
155 // havoc with the omp threads. Run single threaded
156 // if using DOASSERT.
157 newView.copy(newData.index(i,j),m_data.getData()[m_data.index(i,j)]);
158 }
159 }
160 m_data.Swap(newData);
161 DataArrayView temp(m_data.getData(),shape);
162 setPointDataView(temp);
163 }
164
165 DataAbstract*
166 DataExpanded::getSlice(const DataArrayView::RegionType& region) const
167 {
168 return new DataExpanded(*this,region);
169 }
170
171 void
172 DataExpanded::setSlice(const DataAbstract* value,
173 const DataArrayView::RegionType& region)
174 {
175 const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
176 if (tempDataExp==0) {
177 throw DataException("Programming error - casting to DataExpanded.");
178 }
179 if (getPointDataView().getRank()!=region.size()) {
180 throw DataException("Error - Invalid slice region.");
181 }
182 if (!value->getPointDataView().checkShape(DataArrayView::getResultSliceShape(region))) {
183 throw DataException (value->getPointDataView().createShapeErrorMessage(
184 "Error - Couldn't copy slice due to shape mismatch.",DataArrayView::getResultSliceShape(region)));
185 }
186 //
187 // copy the data
188 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
189 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
190 int i, j;
191 #pragma omp parallel for private(i,j) schedule(static)
192 for (i=0;i<numRows;i++) {
193 for (j=0;j<numCols;j++) {
194 getPointDataView().copySliceFrom(getPointOffset(i,j),tempDataExp->getPointDataView(),tempDataExp->getPointOffset(i,j),region);
195 }
196 }
197 }
198
199 void
200 DataExpanded::copy(const DataArrayView& value)
201 {
202 //
203 // Copy a single value to every data point
204 int nRows=m_data.getNumRows();
205 int nCols=m_data.getNumCols();
206 int i, j;
207 #pragma omp parallel for private(i,j) schedule(static)
208 for (i=0;i<nRows;++i) {
209 for (j=0;j<nCols;++j) {
210 //
211 // Copy the data into the specified offset
212 // NOTE: An exception may be thown from this call if
213 // DOASSERT is on which of course will play
214 // havoc with the omp threads. Run single threaded
215 // if using DOASSERT.
216 getPointDataView().copy(m_data.index(i,j),value);
217 }
218 }
219 }
220
221 void
222 DataExpanded::copy(const boost::python::numeric::array& value)
223 {
224 //
225 // first convert the numarray into a DataArrayView format
226 DataArray temp(value);
227 //
228 // check the input shape matches this shape, this will throw an exception
229 if (!getPointDataView().checkShape(temp.getView().getShape())) {
230 throw DataException(getPointDataView().createShapeErrorMessage(
231 "Error - (DataExpanded) Cannot copy due to shape mismatch.",
232 temp.getView().getShape()));
233 }
234 //
235 // now copy over the entire data structure
236 copy(temp.getView());
237 }
238
239 void
240 DataExpanded::initialise(const DataArrayView::ShapeType& shape,
241 int noSamples,
242 int noDataPointsPerSample)
243 {
244 //
245 // resize data to the required size
246 m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));
247 //
248 // create a point data viewer of the data
249 DataArrayView temp(m_data.getData(),shape);
250 setPointDataView(temp);
251 }
252
253 string
254 DataExpanded::toString() const
255 {
256 stringstream temp;
257 //
258 // create a temporary view as the offset will be changed
259 DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
260 for (int i=0;i<m_data.getNumRows();++i) {
261 for (int j=0;j<m_data.getNumCols();++j) {
262 tempView.setOffset(m_data.index(i,j));
263 stringstream suffix;
264 suffix << "(" << i << "," << j << ")";
265 temp << tempView.toString(suffix.str());
266 if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
267 temp << endl;
268 }
269 }
270 }
271 return temp.str();
272 }
273
274 DataArrayView::ValueType::size_type
275 DataExpanded::getPointOffset(int sampleNo,
276 int dataPointNo) const
277 {
278 return m_data.index(sampleNo,dataPointNo);
279 }
280
281 DataArrayView
282 DataExpanded::getDataPoint(int sampleNo,
283 int dataPointNo)
284 {
285 DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo));
286 return temp;
287 }
288
289 DataArrayView::ValueType::size_type
290 DataExpanded::getLength() const
291 {
292 return m_data.getData().size();
293 }
294
295 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26