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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (show annotations)
Wed Dec 15 03:48:48 2004 UTC (14 years, 10 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataExpanded.cpp
File size: 10039 byte(s)
*** empty log message ***

1 /*
2 ******************************************************************************
3 * *
4 * COPYRIGHT ACcESS 2004 - All Rights Reserved *
5 * *
6 * This software is the property of ACcESS. No part of this code *
7 * may be copied in any form or by any means without the expressed written *
8 * consent of ACcESS. Copying, use or modification of this software *
9 * by any unauthorised person is illegal unless that person has a software *
10 * license agreement with ACcESS. *
11 * *
12 ******************************************************************************
13 */
14
15 #include "escript/Data/DataException.h"
16 #include "escript/Data/DataExpanded.h"
17 #include "escript/Data/DataConstant.h"
18 #include "escript/Data/DataTagged.h"
19 #include "escript/Data/DataArrayView.h"
20
21 #include <boost/python/extract.hpp>
22
23 #include <iostream>
24
25 using namespace std;
26 using namespace boost::python;
27 using namespace boost;
28
29 namespace escript {
30
31 DataExpanded::DataExpanded(const boost::python::numeric::array& value, const FunctionSpace& what) : DataAbstract(what)
32 {
33 //cout << "Calling DataExpanded constructor 1." << endl;
34 DataArrayView::ShapeType tempShape;
35 //
36 // extract the shape from the python array
37 for (int i=0; i<value.getrank(); ++i) {
38 cout << extract<int>(value.getshape()[i]) << endl;
39 tempShape.push_back(extract<int>(value.getshape()[i]));
40 }
41 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
42 //
43 // copy the given value to every data point
44 copy(value);
45 }
46
47 DataExpanded::DataExpanded(const DataExpanded& other) : DataAbstract(other.getFunctionSpace()), m_data(other.m_data)
48 {
49 //cout << "Calling DataExpanded copy constructor." << endl;
50 //
51 // create the view for the data
52 DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());
53 setPointDataView(temp);
54 }
55
56 DataExpanded::DataExpanded(const DataConstant& other) : DataAbstract(other.getFunctionSpace())
57 {
58 //cout << "Creating DataExpanded from DataConstant." << endl;
59 initialise(other.getPointDataView().getShape(),other.getNumSamples(), other.getNumDPPSample());
60 //
61 // DataConstant only has one value
62 copy(other.getPointDataView());
63 }
64
65 DataExpanded::DataExpanded(const DataTagged& other) : DataAbstract(other.getFunctionSpace())
66 {
67 //cout << "Creating DataExpanded from DataTagged." << endl;
68 initialise(other.getPointDataView().getShape(),other.getNumSamples(), other.getNumDPPSample());
69 int i,j;
70 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
71 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
72 #pragma omp parallel for private(i,j) schedule(static)
73 for (i=0;i<numRows;++i) {
74 for (j=0;j<numCols;++j) {
75 try {
76 getPointDataView().copy(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j));
77 }
78 catch (std::exception& e) {
79 cout << e.what() << endl;
80 }
81 }
82 }
83 }
84
85 DataExpanded::DataExpanded(const DataExpanded& other, const DataArrayView::RegionType& region) : DataAbstract(other.getFunctionSpace())
86 {
87 //cout << "Creating DataExpanded by slicing from another DataExpanded." << endl;
88 //
89 // get the shape of the slice
90 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
91 initialise(shape,other.getNumSamples(),other.getNumDPPSample());
92 //
93 // copy the data
94 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
95 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
96 int i,j;
97 #pragma omp parallel for private(i,j) schedule(static)
98 for (i=0;i<numRows;++i) {
99 for (j=0;j<numCols;++j) {
100 try {
101 getPointDataView().copySlice(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j), region);
102 }
103 catch (std::exception& e) {
104 cout << e.what() << endl;
105 }
106 }
107 }
108 }
109
110 DataExpanded::DataExpanded(const DataArrayView& value,const FunctionSpace& what): DataAbstract(what)
111 {
112 //cout << "Calling DataExpanded constructor 2." << endl;
113 DataArrayView::ShapeType tempShape=value.getShape();
114 initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
115 copy(value);
116 }
117
118 DataExpanded::~DataExpanded()
119 {
120 //cout << "Destructing DataExpanded." << endl;
121 }
122
123 DataAbstract* DataExpanded::getSlice(const DataArrayView::RegionType& region) const
124 {
125 return new DataExpanded(*this,region);
126 }
127
128 void DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
129 {
130 //
131 // reshape a rank zero data point
132 if (getPointDataView().getRank()!=0) {
133 stringstream temp;
134 temp << "Error - Can only reshape Data with data points of rank 0. "
135 << "This Data has data points with rank: "
136 << getPointDataView().getRank();
137 throw DataException(temp.str());
138 }
139 DataBlocks2D newData(getNumSamples(),getNumDPPSample(), DataArrayView::noValues(shape));
140 DataArrayView newView(newData.getData(),shape);
141 //
142 // Copy the original data to every value for the new shape
143 int i,j;
144 int nRows=m_data.getNumRows();
145 int nCols=m_data.getNumCols();
146 #pragma omp parallel for private(i,j) schedule(static)
147 for (i=0;i<nRows;++i) {
148 for (j=0;j<nCols;++j) {
149 //
150 // Copy the data into the specified offset
151 // NOTE: An exception may be thown from this call if
152 // DOASSERT is on which of course will play
153 // havoc with the omp threads. Run single threaded
154 // if using DOASSERT.
155 newView.copy(newData.index(i,j),m_data.getData()[m_data.index(i,j)]);
156 }
157 }
158 m_data.Swap(newData);
159 DataArrayView temp(m_data.getData(),shape);
160 setPointDataView(temp);
161 }
162
163 void DataExpanded::setSlice(const DataAbstract* value, const DataArrayView::RegionType& region)
164 {
165 const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
166 if (tempDataExp==0) {
167 throw DataException("Programming error - casting to DataExpanded.");
168 }
169 if (getPointDataView().getRank()!=region.size()) {
170 throw DataException("Error - Invalid slice region.");
171 }
172 if (!value->getPointDataView().checkShape(DataArrayView::getResultSliceShape(region))) {
173 throw DataException (value->getPointDataView().createShapeErrorMessage(
174 "Error - Couldn't copy slice due to shape mismatch.",DataArrayView::getResultSliceShape(region)));
175 }
176 //
177 // copy the data
178 DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
179 DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
180 int i,j;
181 #pragma omp parallel for private(i,j) schedule(static)
182 for (i=0;i<numRows;++i) {
183 for (j=0;j<numCols;++j) {
184 getPointDataView().copySliceFrom(getPointOffset(i,j), tempDataExp->getPointDataView(), tempDataExp->getPointOffset(i,j), region);
185 }
186 }
187 }
188
189 void DataExpanded::copy(const DataArrayView& value)
190 {
191 //
192 // Copy a single value to every data point
193 int i,j;
194 int nRows=m_data.getNumRows();
195 int nCols=m_data.getNumCols();
196 #pragma omp parallel for private(i,j) schedule(static)
197 for (i=0;i<nRows;++i) {
198 for (j=0;j<nCols;++j) {
199 //
200 // Copy the data into the specified offset
201 // NOTE: An exception may be thown from this call if
202 // DOASSERT is on which of course will play
203 // havoc with the omp threads. Run single threaded
204 // if using DOASSERT.
205 getPointDataView().copy(m_data.index(i,j),value);
206 }
207 }
208 }
209
210 void DataExpanded::copy(const boost::python::numeric::array& value)
211 {
212 //
213 // first convert the numarray into a DataArrayView format
214 DataArray temp(value);
215 //
216 // check the input shape matches this shape, this will throw an exception
217 if (!getPointDataView().checkShape(temp.getView().getShape())) {
218 throw DataException(getPointDataView().createShapeErrorMessage(
219 "Error - (DataExpanded) Cannot copy due to shape mismatch.",
220 temp.getView().getShape()));
221 }
222 //
223 // now copy over the entire data structure
224 copy(temp.getView());
225 }
226
227 void DataExpanded::initialise(const DataArrayView::ShapeType& shape, int noSamples, int noDataPointsPerSample)
228 {
229 //
230 // resize data to the required size
231 //cout << "noDataPointsPerSample=" << noDataPointsPerSample
232 // << " noSamples=" << noSamples
233 // << " noValuesPerDataPoint=" << DataArrayView::noValues(shape) << endl;
234 m_data.resize(noSamples,noDataPointsPerSample, DataArrayView::noValues(shape));
235 //
236 // create a point data viewer of the data
237 DataArrayView temp(m_data.getData(),shape);
238 setPointDataView(temp);
239 //cout << "leaving initialise." << endl;
240 }
241
242 string DataExpanded::toString() const
243 {
244 stringstream temp;
245 //
246 // create a temporary view as the offset will be changed
247 DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape(), getPointDataView().getOffset());
248 for (int i=0;i<m_data.getNumRows();++i) {
249 for (int j=0;j<m_data.getNumCols();++j) {
250 tempView.setOffset(m_data.index(i,j));
251 stringstream suffix;
252 suffix << "(" << i << "," << j << ")";
253 temp << tempView.toString(suffix.str());
254 if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
255 temp << endl;
256 }
257 }
258 }
259 return temp.str();
260 }
261
262 DataArrayView::ValueType::size_type DataExpanded::getPointOffset(int sampleNo, int dataPointNo) const
263 {
264 return m_data.index(sampleNo,dataPointNo);
265 }
266
267 DataArrayView DataExpanded::getDataPoint(int sampleNo, int dataPointNo)
268 {
269 DataArrayView temp(m_data.getData(), getPointDataView().getShape(), m_data.index(sampleNo,dataPointNo));
270 return temp;
271 }
272
273 DataArrayView::ValueType::size_type DataExpanded::getLength() const
274 {
275 return m_data.getData().size();
276 }
277
278 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26