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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (show annotations)
Thu Sep 25 23:11:13 2008 UTC (10 years, 3 months ago) by ksteube
File size: 12331 byte(s)
Copyright updated in all files

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26