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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1724 - (hide annotations)
Mon Aug 25 05:38:57 2008 UTC (11 years ago) by jfenwick
File size: 11831 byte(s)
Branch commit

Moved createShapeErrorMessage() into DataTypes.h
Modified functions in DataAlgorithm.h to use non-DataArrayView accesses.

Added getVector() to each of DataTagged, DataConstant, DataExpanded - This method returns 
the underlying DataVector by reference/constant reference. Note that this method does not 
exist in DataAbstract so (at the momement) in order to pull the data from something you 
need to know what you are looking at. (Lower level access is still possible via double* 
though).

DataTagged now has a getOffsetForTag method and a getDefaultOffset method.

DataMaths.h - A new file containing the reductionOps etc from DataArrayView (but without 
requiring DAV).
This file requires significant commenting improvements.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26