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

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

Parent Directory Parent Directory | Revision Log Revision Log


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