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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 854 - (hide annotations)
Thu Sep 21 05:29:42 2006 UTC (13 years, 1 month ago) by gross
File size: 8655 byte(s)
Some modifications to the binary operations +,-,*/, pow. 
The code is a bit simpler now and more efficient has there is
no reseising required now. the resizing method has been removed as
it is very, very inefficient. Even serial code should be faster now.
It is now forbidden to do an inplace update of scalar data object with an object 
of rank >0 as this is very slow (and does not make much sense). 


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    
21     using namespace std;
22    
23     namespace escript {
24    
25 jgs 102 DataConstant::DataConstant(const boost::python::numeric::array& value,
26     const FunctionSpace& what)
27     : DataAbstract(what)
28     {
29     DataArray temp(value);
30     //
31     // copy the data in the correct format
32     m_data=temp.getData();
33 jgs 119 //
34     // create the view of the data
35 jgs 102 DataArrayView tempView(m_data,temp.getView().getShape());
36     setPointDataView(tempView);
37     }
38 jgs 82
39 jgs 102 DataConstant::DataConstant(const DataArrayView& value,
40     const FunctionSpace& what)
41     : DataAbstract(what)
42     {
43     //
44     // copy the data in the correct format
45     m_data=value.getData();
46 jgs 119 //
47     // create the view of the data
48 jgs 102 DataArrayView tempView(m_data,value.getShape());
49     setPointDataView(tempView);
50     }
51 jgs 82
52 jgs 102 DataConstant::DataConstant(const DataConstant& other)
53     : DataAbstract(other.getFunctionSpace())
54     {
55     //
56     // copy the data in the correct format
57     m_data=other.m_data;
58 jgs 119 //
59     // create the view of the data
60 jgs 102 DataArrayView tempView(m_data,other.getPointDataView().getShape());
61     setPointDataView(tempView);
62     }
63 jgs 82
64 jgs 102 DataConstant::DataConstant(const DataConstant& other,
65     const DataArrayView::RegionType& region)
66     : DataAbstract(other.getFunctionSpace())
67     {
68     //
69     // get the shape of the slice to copy from
70     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
71     //
72     // allocate space for this new DataConstant's data
73 jgs 151 int len = DataArrayView::noValues(shape);
74     m_data.resize(len,0.,len);
75 jgs 102 //
76     // create a view of the data with the correct shape
77     DataArrayView tempView(m_data,shape);
78 jgs 108 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
79 jgs 119 //
80     // load the view with the data from the slice
81 jgs 108 tempView.copySlice(other.getPointDataView(),region_loop_range);
82 jgs 119 setPointDataView(tempView);
83     }
84    
85     DataConstant::DataConstant(const FunctionSpace& what,
86     const DataArrayView::ShapeType &shape,
87     const DataArrayView::ValueType &data)
88     : DataAbstract(what)
89     {
90 jgs 102 //
91 jgs 119 // copy the data in the correct format
92     m_data=data;
93     //
94     // create the view of the data
95     DataArrayView tempView(m_data,shape);
96 jgs 102 setPointDataView(tempView);
97     }
98 jgs 82
99 jgs 102 string
100     DataConstant::toString() const
101     {
102     return getPointDataView().toString("");
103     }
104 jgs 82
105 jgs 102 DataArrayView::ValueType::size_type
106     DataConstant::getPointOffset(int sampleNo,
107     int dataPointNo) const
108     {
109     EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
110     "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
111 jgs 122 //
112     // Whatever the coord's always return the same value as this is constant data.
113 jgs 102 return 0;
114     }
115 jgs 82
116 jgs 102 DataArrayView::ValueType::size_type
117     DataConstant::getLength() const
118     {
119     return m_data.size();
120     }
121 jgs 82
122 jgs 102 DataArrayView
123     DataConstant::getDataPoint(int sampleNo,
124     int dataPointNo)
125     {
126     EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
127     "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
128     //
129     // Whatever the coord's always return the same value as this is constant data.
130     return getPointDataView();
131     }
132 jgs 82
133 jgs 102 DataAbstract*
134     DataConstant::getSlice(const DataArrayView::RegionType& region) const
135     {
136     return new DataConstant(*this,region);
137     }
138 jgs 82
139 jgs 102 void
140     DataConstant::setSlice(const DataAbstract* value,
141     const DataArrayView::RegionType& region)
142     {
143     const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
144     if (tempDataConst==0) {
145     throw DataException("Programming error - casting to DataConstant.");
146 jgs 82 }
147 jgs 108 //
148     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
149     DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
150     //
151     // check shape:
152     if (getPointDataView().getRank()!=region.size()) {
153     throw DataException("Error - Invalid slice region.");
154     }
155 woo409 757 if (tempDataConst->getPointDataView().getRank()>0 && !value->getPointDataView().checkShape(shape)) {
156 jgs 108 throw DataException (value->getPointDataView().createShapeErrorMessage(
157     "Error - Couldn't copy slice due to shape mismatch.",shape));
158     }
159     //
160     getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
161 jgs 102 }
162 jgs 82
163 jgs 123 int
164     DataConstant::archiveData(ofstream& archiveFile,
165     const DataArrayView::ValueType::size_type noValues) const
166     {
167     return(m_data.archiveData(archiveFile, noValues));
168     }
169    
170     int
171     DataConstant::extractData(ifstream& archiveFile,
172     const DataArrayView::ValueType::size_type noValues)
173     {
174     return(m_data.extractData(archiveFile, noValues));
175     }
176    
177 gross 580 void
178 ksteube 775 DataConstant::symmetric(DataAbstract* ev)
179     {
180     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
181     if (temp_ev==0) {
182     throw DataException("Error - DataConstant::symmetric: casting to DataConstant failed (propably a programming error).");
183     }
184     DataArrayView& thisView=getPointDataView();
185     DataArrayView& evView=ev->getPointDataView();
186     DataArrayView::symmetric(thisView,0,evView,0);
187     }
188    
189     void
190     DataConstant::nonsymmetric(DataAbstract* ev)
191     {
192     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
193     if (temp_ev==0) {
194     throw DataException("Error - DataConstant::nonsymmetric: casting to DataConstant failed (propably a programming error).");
195     }
196     DataArrayView& thisView=getPointDataView();
197     DataArrayView& evView=ev->getPointDataView();
198     DataArrayView::nonsymmetric(thisView,0,evView,0);
199     }
200    
201     void
202 gross 800 DataConstant::trace(DataAbstract* ev, int axis_offset)
203 ksteube 775 {
204     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
205     if (temp_ev==0) {
206 gross 800 throw DataException("Error - DataConstant::trace: casting to DataConstant failed (propably a programming error).");
207 ksteube 775 }
208     DataArrayView& thisView=getPointDataView();
209     DataArrayView& evView=ev->getPointDataView();
210 gross 800 DataArrayView::trace(thisView,0,evView,0,axis_offset);
211 ksteube 775 }
212    
213     void
214 gross 804 DataConstant::swapaxes(DataAbstract* ev, int axis0, int axis1)
215 gross 800 {
216     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
217     if (temp_ev==0) {
218 gross 804 throw DataException("Error - DataConstant::swapaxes: casting to DataConstant failed (propably a programming error).");
219 gross 800 }
220     DataArrayView& thisView=getPointDataView();
221     DataArrayView& evView=ev->getPointDataView();
222 gross 804 DataArrayView::swapaxes(thisView,0,evView,0,axis0,axis1);
223 gross 800 }
224    
225     void
226 ksteube 775 DataConstant::transpose(DataAbstract* ev, int axis_offset)
227     {
228     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
229     if (temp_ev==0) {
230     throw DataException("Error - DataConstant::transpose: casting to DataConstant failed (propably a programming error).");
231     }
232     DataArrayView& thisView=getPointDataView();
233     DataArrayView& evView=ev->getPointDataView();
234     DataArrayView::transpose(thisView,0,evView,0,axis_offset);
235     }
236    
237     void
238 gross 580 DataConstant::eigenvalues(DataAbstract* ev)
239     {
240     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
241     if (temp_ev==0) {
242     throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");
243     }
244     DataArrayView& thisView=getPointDataView();
245     DataArrayView& evView=ev->getPointDataView();
246     DataArrayView::eigenvalues(thisView,0,evView,0);
247     }
248     void
249     DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
250     {
251     DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
252     if (temp_ev==0) {
253     throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
254     }
255     DataConstant* temp_V=dynamic_cast<DataConstant*>(V);
256     if (temp_V==0) {
257     throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
258     }
259     DataArrayView thisView=getPointDataView();
260     DataArrayView evView=ev->getPointDataView();
261     DataArrayView VView=V->getPointDataView();
262    
263     DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,tol);
264     }
265    
266    
267 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