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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1312 - (hide annotations)
Mon Sep 24 06:18:44 2007 UTC (11 years, 9 months ago) by ksteube
Original Path: trunk/escript/src/DataConstant.cpp
File size: 11483 byte(s)
The MPI branch is hereby closed. All future work should be in trunk.

Previously in revision 1295 I merged the latest changes to trunk into trunk-mpi-branch.
In this revision I copied all files from trunk-mpi-branch over the corresponding
trunk files. I did not use 'svn merge', it was a copy.

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     using namespace std;
27    
28     namespace escript {
29    
30 jgs 102 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 jgs 119 //
39     // create the view of the data
40 jgs 102 DataArrayView tempView(m_data,temp.getView().getShape());
41     setPointDataView(tempView);
42     }
43 jgs 82
44 jgs 102 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 jgs 119 //
52     // create the view of the data
53 jgs 102 DataArrayView tempView(m_data,value.getShape());
54     setPointDataView(tempView);
55     }
56 jgs 82
57 jgs 102 DataConstant::DataConstant(const DataConstant& other)
58     : DataAbstract(other.getFunctionSpace())
59 gross 1141 { //
60 jgs 102 // copy the data in the correct format
61     m_data=other.m_data;
62 jgs 119 //
63     // create the view of the data
64 jgs 102 DataArrayView tempView(m_data,other.getPointDataView().getShape());
65     setPointDataView(tempView);
66     }
67 jgs 82
68 jgs 102 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 jgs 151 int len = DataArrayView::noValues(shape);
78     m_data.resize(len,0.,len);
79 jgs 102 //
80     // create a view of the data with the correct shape
81     DataArrayView tempView(m_data,shape);
82 jgs 108 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
83 jgs 119 //
84     // load the view with the data from the slice
85 jgs 108 tempView.copySlice(other.getPointDataView(),region_loop_range);
86 jgs 119 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 jgs 102 //
95 jgs 119 // 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 jgs 102 setPointDataView(tempView);
101     }
102 jgs 82
103 jgs 102 string
104     DataConstant::toString() const
105     {
106     return getPointDataView().toString("");
107     }
108 jgs 82
109 jgs 102 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 jgs 122 //
116     // Whatever the coord's always return the same value as this is constant data.
117 jgs 102 return 0;
118     }
119 jgs 82
120 jgs 102 DataArrayView::ValueType::size_type
121     DataConstant::getLength() const
122     {
123     return m_data.size();
124     }
125 jgs 82
126 jgs 102 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 jgs 82
137 jgs 102 DataAbstract*
138     DataConstant::getSlice(const DataArrayView::RegionType& region) const
139     {
140     return new DataConstant(*this,region);
141     }
142 jgs 82
143 jgs 102 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 jgs 82 }
151 jgs 108 //
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 woo409 757 if (tempDataConst->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {
160 jgs 108 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 jgs 102 }
166 jgs 82
167 jgs 123 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 gross 580 void
182 ksteube 775 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 gross 800 DataConstant::trace(DataAbstract* ev, int axis_offset)
207 ksteube 775 {
208     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
209     if (temp_ev==0) {
210 gross 800 throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");
211 ksteube 775 }
212     DataArrayView& thisView=getPointDataView();
213     DataArrayView& evView=ev->getPointDataView();
214 gross 800 DataArrayView::trace(thisView,0,evView,0,axis_offset);
215 ksteube 775 }
216    
217     void
218 gross 804 DataConstant::swapaxes(DataAbstract* ev, int axis0, int axis1)
219 gross 800 {
220     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
221     if (temp_ev==0) {
222 gross 804 throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");
223 gross 800 }
224     DataArrayView& thisView=getPointDataView();
225     DataArrayView& evView=ev->getPointDataView();
226 gross 804 DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
227 gross 800 }
228    
229     void
230 ksteube 775 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 gross 580 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 gross 1141 DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,0,tol);
268 gross 580 }
269    
270 gross 950 void
271 gross 1118 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 gross 950 DataConstant::dump(const std::string fileName) const
279     {
280     #ifdef PASO_MPI
281 ksteube 1312 throw DataException("Error - DataConstant:: dump is not implemented for MPI yet.");
282 gross 950 #endif
283 gross 1149 #ifdef USE_NETCDF
284 gross 950 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 gross 1141 const double* d_ptr=&(m_data[0]);
291 gross 950 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 gross 1141 if (!dataFile.add_att("type_id",0) )
301 gross 950 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 gross 580
307 gross 950 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 gross 1141
334 gross 950 if (! ( var = dataFile.add_var("data", ncDouble, ndims, ncdims)) )
335     throw DataException("Error - DataConstant:: appending variable to netCDF file failed.");
336 gross 1141 if (! (var->put(d_ptr,dims)) )
337     throw DataException("Error - DataConstant:: copy data to netCDF buffer failed.");
338 gross 1023 #else
339     throw DataException("Error - DataConstant:: dump is not configured with netCDF. Please contact your installation manager.");
340     #endif
341 gross 950 }
342    
343 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