/[escript]/branches/arrayview_from_1695_trunk/escript/src/DataConstant.cpp
ViewVC logotype

Contents of /branches/arrayview_from_1695_trunk/escript/src/DataConstant.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1747 - (show annotations)
Wed Sep 3 04:56:50 2008 UTC (11 years, 2 months ago) by jfenwick
File size: 12611 byte(s)
Branch commit.

Current status, this version compiles as passes run_tests (haven't tried 
all_tests), provided that the following files are moved out of the way:

DataAlgorithmAdapterTestCase.h 
DataTaggedTestCase.cpp 
DataTestCase.cpp 
DataTaggedTestCase.h

(They are not run by the unit tests at the moment).

I will fix those next.



1
2 /* $Id$ */
3
4 /*******************************************************
5 *
6 * Copyright 2003-2007 by ACceSS MNRF
7 * Copyright 2007 by University of Queensland
8 *
9 * http://esscc.uq.edu.au
10 * Primary Business: Queensland, Australia
11 * Licensed under the Open Software License version 3.0
12 * http://www.opensource.org/licenses/osl-3.0.php
13 *
14 *******************************************************/
15
16 #include "DataConstant.h"
17 #include "DataException.h"
18 #include "esysUtils/EsysAssert.h"
19
20 #include <iostream>
21 #include <boost/python/extract.hpp>
22 #ifdef USE_NETCDF
23 #include <netcdfcpp.h>
24 #endif
25
26 #include <boost/python/extract.hpp>
27 #include "DataMaths.h"
28
29 using namespace std;
30 using namespace boost::python;
31
32 namespace escript {
33
34 DataConstant::DataConstant(const boost::python::numeric::array& value,
35 const FunctionSpace& what)
36 : DataAbstract(what,DataTypes::shapeFromNumArray(value))
37 {
38 // // extract the shape of the numarray
39 // DataTypes::ShapeType tempShape;
40 // for (int i=0; i < value.getrank(); i++) {
41 // tempShape.push_back(extract<int>(value.getshape()[i]));
42 // }
43
44 // get the space for the data vector
45 int len = getNoValues();
46 DataVector temp_data(len, 0.0, len);
47 // DataArrayView temp_dataView(temp_data, tempShape);
48 // temp_dataView.copy(value);
49
50 //
51 // copy the data in the correct format
52 m_data=temp_data;
53 //
54 // create the view of the data
55 // DataArrayView tempView(m_data,temp_dataView.getShape());
56 // setPointDataView(tempView);
57 }
58
59 // DataConstant::DataConstant(const DataArrayView& value,
60 // const FunctionSpace& what)
61 // : DataAbstract(what)
62 // {
63 // //
64 // // copy the data in the correct format
65 // m_data=value.getData();
66 // //
67 // // create the view of the data
68 // DataArrayView tempView(m_data,value.getShape());
69 // setPointDataView(tempView);
70 // }
71
72 DataConstant::DataConstant(const DataConstant& other)
73 : DataAbstract(other.getFunctionSpace(),other.getShape())
74 { //
75 // copy the data in the correct format
76 m_data=other.m_data;
77 //
78 // // create the view of the data
79 // DataArrayView tempView(m_data,other.getPointDataView().getShape());
80 // setPointDataView(tempView);
81 }
82
83 DataConstant::DataConstant(const DataConstant& other,
84 const DataTypes::RegionType& region)
85 : DataAbstract(other.getFunctionSpace(),DataTypes::getResultSliceShape(region))
86 {
87 //
88 // get the shape of the slice to copy from
89 // DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
90 //
91 // allocate space for this new DataConstant's data
92 int len = getNoValues();
93 m_data.resize(len,0.,len);
94 //
95 // create a view of the data with the correct shape
96 // DataArrayView tempView(m_data,shape);
97 DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
98 //
99 // load the view with the data from the slice
100 // tempView.copySlice(other.getPointDataView(),region_loop_range);
101 DataTypes::copySlice(m_data,getShape(),0,other.getVector(),other.getShape(),0,region_loop_range);
102 // setPointDataView(tempView);
103 }
104
105 DataConstant::DataConstant(const FunctionSpace& what,
106 const DataTypes::ShapeType &shape,
107 const DataTypes::ValueType &data)
108 : DataAbstract(what,shape)
109 {
110 //
111 // copy the data in the correct format
112 m_data=data;
113 //
114 // create the view of the data
115 // DataArrayView tempView(m_data,shape);
116 // setPointDataView(tempView);
117 }
118
119 string
120 DataConstant::toString() const
121 {
122 return DataTypes::pointToString(m_data,getShape(),0,"");
123 }
124
125 DataTypes::ValueType::size_type
126 DataConstant::getPointOffset(int sampleNo,
127 int dataPointNo) const
128 {
129 EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
130 "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
131 //
132 // Whatever the coord's always return the same value as this is constant data.
133 return 0;
134 }
135
136 DataTypes::ValueType::size_type
137 DataConstant::getLength() const
138 {
139 return m_data.size();
140 }
141
142 // DataArrayView
143 // DataConstant::getDataPoint(int sampleNo,
144 // int dataPointNo)
145 // {
146 // EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
147 // "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
148 // //
149 // // Whatever the coord's always return the same value as this is constant data.
150 // return getPointDataView();
151 // }
152
153 DataAbstract*
154 DataConstant::getSlice(const DataTypes::RegionType& region) const
155 {
156 return new DataConstant(*this,region);
157 }
158
159 void
160 DataConstant::setSlice(const DataAbstract* value,
161 const DataTypes::RegionType& region)
162 {
163 const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
164 if (tempDataConst==0) {
165 throw DataException("Programming error - casting to DataConstant.");
166 }
167 //
168 DataTypes::ShapeType shape(DataTypes::getResultSliceShape(region));
169 DataTypes::RegionLoopRangeType region_loop_range=DataTypes::getSliceRegionLoopRange(region);
170 //
171 // check shape:
172 if (getRank()!=region.size()) {
173 throw DataException("Error - Invalid slice region.");
174 }
175 if (getRank()>0 && !DataTypes::checkShape(getShape(),value->getShape())) {
176 throw DataException (DataTypes::createShapeErrorMessage(
177 "Error - Couldn't copy slice due to shape mismatch.",shape,value->getShape()));
178 }
179 // getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
180 DataTypes::copySliceFrom(m_data,getShape(),0,tempDataConst->getVector(), tempDataConst->getShape(),0,region_loop_range);
181 }
182
183 int
184 DataConstant::archiveData(ofstream& archiveFile,
185 const DataTypes::ValueType::size_type noValues) const
186 {
187 return(m_data.archiveData(archiveFile, noValues));
188 }
189
190 int
191 DataConstant::extractData(ifstream& archiveFile,
192 const DataTypes::ValueType::size_type noValues)
193 {
194 return(m_data.extractData(archiveFile, noValues));
195 }
196
197 void
198 DataConstant::symmetric(DataAbstract* ev)
199 {
200 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
201 if (temp_ev==0) {
202 throw DataException("Error - DataConstant::symmetric: casting to DataConstant failed (propably a programming error).");
203 }
204 /* DataArrayView& thisView=getPointDataView();
205 DataArrayView& evView=ev->getPointDataView();*/
206 DataMaths::symmetric(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
207 }
208
209 void
210 DataConstant::nonsymmetric(DataAbstract* ev)
211 {
212 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
213 if (temp_ev==0) {
214 throw DataException("Error - DataConstant::nonsymmetric: casting to DataConstant failed (propably a programming error).");
215 }
216 /* DataArrayView& thisView=getPointDataView();
217 DataArrayView& evView=ev->getPointDataView();*/
218 DataMaths::nonsymmetric(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
219 }
220
221 void
222 DataConstant::trace(DataAbstract* ev, int axis_offset)
223 {
224 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
225 if (temp_ev==0) {
226 throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");
227 }
228 /* DataArrayView& thisView=getPointDataView();
229 DataArrayView& evView=ev->getPointDataView();*/
230 ValueType& evVec=temp_ev->getVector();
231 const ShapeType& evShape=temp_ev->getShape();
232 DataMaths::trace(m_data,getShape(),0,evVec,evShape,0,axis_offset);
233 }
234
235 void
236 DataConstant::swapaxes(DataAbstract* ev, int axis0, int axis1)
237 {
238 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
239 if (temp_ev==0) {
240 throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");
241 }
242 // DataArrayView& thisView=getPointDataView();
243 // DataArrayView& evView=ev->getPointDataView();
244 DataMaths::swapaxes(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0,axis0,axis1);
245 }
246
247 void
248 DataConstant::transpose(DataAbstract* ev, int axis_offset)
249 {
250 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
251 if (temp_ev==0) {
252 throw DataException("Error - DataConstant::transpose: casting to DataConstant failed (propably a programming error).");
253 }
254 /* DataArrayView& thisView=getPointDataView();
255 DataArrayView& evView=ev->getPointDataView();*/
256 DataMaths::transpose(m_data, getShape(),0, temp_ev->getVector(),temp_ev->getShape(),0,axis_offset);
257 }
258
259 void
260 DataConstant::eigenvalues(DataAbstract* ev)
261 {
262 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
263 if (temp_ev==0) {
264 throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");
265 }
266 /* DataArrayView& thisView=getPointDataView();
267 DataArrayView& evView=ev->getPointDataView();*/
268 DataMaths::eigenvalues(m_data,getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0);
269 }
270 void
271 DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
272 {
273 DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
274 if (temp_ev==0) {
275 throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
276 }
277 DataConstant* temp_V=dynamic_cast<DataConstant*>(V);
278 if (temp_V==0) {
279 throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
280 }
281 // DataArrayView thisView=getPointDataView();
282 // DataArrayView evView=ev->getPointDataView();
283 // DataArrayView VView=V->getPointDataView();
284
285 DataMaths::eigenvalues_and_eigenvectors(m_data, getShape(),0,temp_ev->getVector(), temp_ev->getShape(),0,temp_V->getVector(), temp_V->getShape(),0,tol);
286 }
287
288 void
289 DataConstant::setToZero()
290 {
291 DataTypes::ValueType::size_type n=m_data.size();
292 for (int i=0; i<n ;++i) m_data[i]=0.;
293 }
294
295 void
296 DataConstant::dump(const std::string fileName) const
297 {
298 #ifdef PASO_MPI
299 throw DataException("Error - DataConstant:: dump is not implemented for MPI yet.");
300 #endif
301 #ifdef USE_NETCDF
302 const NcDim* ncdims[DataTypes::maxRank];
303 NcVar* var;
304 int rank = getRank();
305 int type= getFunctionSpace().getTypeCode();
306 int ndims =0;
307 long dims[DataTypes::maxRank];
308 const double* d_ptr=&(m_data[0]);
309 DataTypes::ShapeType shape = getShape();
310
311 // netCDF error handler
312 NcError err(NcError::verbose_nonfatal);
313 // Create the file.
314 NcFile dataFile(fileName.c_str(), NcFile::Replace);
315 // check if writing was successful
316 if (!dataFile.is_valid())
317 throw DataException("Error - DataConstant:: opening of netCDF file for output failed.");
318 if (!dataFile.add_att("type_id",0) )
319 throw DataException("Error - DataConstant:: appending data type to netCDF file failed.");
320 if (!dataFile.add_att("rank",rank) )
321 throw DataException("Error - DataConstant:: appending rank attribute to netCDF file failed.");
322 if (!dataFile.add_att("function_space_type",type))
323 throw DataException("Error - DataConstant:: appending function space attribute to netCDF file failed.");
324
325 if (rank == 0) {
326 if( ! (ncdims[0] = dataFile.add_dim("l", 1)) )
327 throw DataException("Error - DataConstant:: appending ncdimsion 0 to netCDF file failed.");
328 dims[0]=1,
329 ndims=1;
330 } else {
331 ndims=rank;
332 dims[0]=shape[0];
333 if (! (ncdims[0] = dataFile.add_dim("d0",shape[0])) )
334 throw DataException("Error - DataConstant:: appending ncdimsion 0 to netCDF file failed.");
335 if ( rank >1 ) {
336 dims[1]=shape[1];
337 if (! (ncdims[1] = dataFile.add_dim("d1",shape[1])) )
338 throw DataException("Error - DataConstant:: appending ncdimsion 1 to netCDF file failed.");
339 }
340 if ( rank >2 ) {
341 dims[2]=shape[2];
342 if (! (ncdims[2] = dataFile.add_dim("d2", shape[2])) )
343 throw DataException("Error - DataConstant:: appending ncdimsion 2 to netCDF file failed.");
344 }
345 if ( rank >3 ) {
346 dims[3]=shape[3];
347 if (! (ncdims[3] = dataFile.add_dim("d3", shape[3])) )
348 throw DataException("Error - DataConstant:: appending ncdimsion 3 to netCDF file failed.");
349 }
350 }
351
352 if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
353 throw DataException("Error - DataConstant:: appending variable to netCDF file failed.");
354 if (! (var->put(d_ptr,dims)) )
355 throw DataException("Error - DataConstant:: copy data to netCDF buffer failed.");
356 #else
357 throw DataException("Error - DataConstant:: dump is not configured with netCDF. Please contact your installation manager.");
358 #endif
359 }
360
361 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26