/[escript]/trunk-mpi-branch/escript/src/DataConstant.cpp
ViewVC logotype

Contents of /trunk-mpi-branch/escript/src/DataConstant.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1306 - (show annotations)
Tue Sep 18 05:51:09 2007 UTC (11 years, 11 months ago) by ksteube
File size: 11483 byte(s)
New Copyright in each .c .h .cpp and .py file

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26