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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/escript/src/Data/DataConstant.cpp revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC trunk/escript/src/DataConstant.cpp revision 682 by robwdcock, Mon Mar 27 02:43:09 2006 UTC
# Line 1  Line 1 
1    //$Id$
2  /*  /*
3   ******************************************************************************   ************************************************************
4   *                                                                            *   *          Copyright 2006 by ACcESS MNRF                   *
5   *       COPYRIGHT  ACcESS 2004 -  All Rights Reserved                        *   *                                                          *
6   *                                                                            *   *              http://www.access.edu.au                    *
7   * This software is the property of ACcESS. No part of this code              *   *       Primary Business: Queensland, Australia            *
8   * may be copied in any form or by any means without the expressed written    *   *  Licensed under the Open Software License version 3.0    *
9   * consent of ACcESS.  Copying, use or modification of this software          *   *     http://www.opensource.org/licenses/osl-3.0.php       *
10   * by any unauthorised person is illegal unless that person has a software    *   *                                                          *
11   * license agreement with ACcESS.                                             *   ************************************************************
  *                                                                            *  
  ******************************************************************************  
12  */  */
13    
14  #include "escript/Data/DataConstant.h"  #include "DataConstant.h"
15  #include "escript/Data/DataException.h"  #include "DataException.h"
16  #include "esysUtils/EsysAssert.h"  #include "esysUtils/EsysAssert.h"
17    
18  #include <iostream>  #include <iostream>
19  #include <boost/python/extract.hpp>  #include <boost/python/extract.hpp>
20    
   
21  using namespace std;  using namespace std;
22    
23  namespace escript {  namespace escript {
24    
25    DataConstant::DataConstant(const boost::python::numeric::array& value, const FunctionSpace& what): DataAbstract(what)  DataConstant::DataConstant(const boost::python::numeric::array& value,
26    {                             const FunctionSpace& what)
27      //cout << "Calling DataConstant constructor 1." << endl;    : DataAbstract(what)
28      DataArray temp(value);  {
29      //    DataArray temp(value);
30      // copy the data in the correct format    //
31      m_data=temp.getData();    // copy the data in the correct format
32      DataArrayView tempView(m_data,temp.getView().getShape());    m_data=temp.getData();
33      //    //
34      // copy the view of the data    // create the view of the data
35      setPointDataView(tempView);    DataArrayView tempView(m_data,temp.getView().getShape());
36    }    setPointDataView(tempView);
37    }
38    DataConstant::DataConstant(const DataArrayView& value, const FunctionSpace& what): DataAbstract(what)  
39    {  DataConstant::DataConstant(const DataArrayView& value,
40      //cout << "Calling DataConstant constructor 2." << endl;                             const FunctionSpace& what)
41      //    : DataAbstract(what)
42      // copy the data in the correct format  {
43      m_data=value.getData();    //
44      DataArrayView tempView(m_data,value.getShape());    // copy the data in the correct format
45      //    m_data=value.getData();
46      // copy the view of the data    //
47      setPointDataView(tempView);    // create the view of the data
48    }    DataArrayView tempView(m_data,value.getShape());
49      setPointDataView(tempView);
50    DataConstant::DataConstant(const DataConstant& other): DataAbstract(other.getFunctionSpace()), m_data(other.m_data)  }
51    {  
52      //cout << "Calling DataConstant copy constructor." << endl;  DataConstant::DataConstant(const DataConstant& other)
53      //    : DataAbstract(other.getFunctionSpace())
54      DataArrayView tempView(m_data,other.getPointDataView().getShape());  {
55      //    //
56      // copy the view of the data    // copy the data in the correct format
57      setPointDataView(tempView);    m_data=other.m_data;
58    }    //
59      // create the view of the data
60    DataConstant::DataConstant(const DataConstant& other, const DataArrayView::RegionType& region): DataAbstract(other.getFunctionSpace())    DataArrayView tempView(m_data,other.getPointDataView().getShape());
61    {    setPointDataView(tempView);
62      //cout << "Calling DataConstant slice constructor." << endl;  }
63      //  
64      //  DataConstant::DataConstant(const DataConstant& other,
65      // get the shape of the slice                             const DataArrayView::RegionType& region)
66      DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));    : DataAbstract(other.getFunctionSpace())
67      //  {
68      // allocate space for this DataConstant    //
69      m_data.resize(DataArrayView::noValues(shape));    // get the shape of the slice to copy from
70      DataArrayView tempView(m_data,shape);    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
71      tempView.copySlice(other.getPointDataView(),region);    //
72      //    // allocate space for this new DataConstant's data
73      // copy the view of the data    int len = DataArrayView::noValues(shape);
74      setPointDataView(tempView);    m_data.resize(len,0.,len);
75    }    //
76      // create a view of the data with the correct shape
77    string DataConstant::toString() const    DataArrayView tempView(m_data,shape);
78    {    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
79      return getPointDataView().toString("");    //
80    }    // load the view with the data from the slice
81      tempView.copySlice(other.getPointDataView(),region_loop_range);
82    DataArrayView::ValueType::size_type DataConstant::getPointOffset(int sampleNo, int dataPointNo) const    setPointDataView(tempView);
83    {  }
84      EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),  
85             "Invalid index, sampleNo: " << sampleNo  DataConstant::DataConstant(const FunctionSpace& what,
86             << " dataPointNo: " << dataPointNo);                             const DataArrayView::ShapeType &shape,
87      return 0;                             const DataArrayView::ValueType &data)
88    }    : DataAbstract(what)
89    {
90    DataArrayView::ValueType::size_type DataConstant::getLength() const    //
91    {    // copy the data in the correct format
92      return m_data.size();    m_data=data;
93    }    //
94      // create the view of the data
95    DataArrayView DataConstant::getDataPoint(int sampleNo, int dataPointNo)    DataArrayView tempView(m_data,shape);
96    {    setPointDataView(tempView);
97      EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),  }
98             "Invalid index, sampleNo: " << sampleNo  
99             << " dataPointNo: " << dataPointNo);  string
100      //  DataConstant::toString() const
101      // Whatever the coord's always return the same value  {
102      return getPointDataView();    return getPointDataView().toString("");
103    }  }
104    
105    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      //
112      // Whatever the coord's always return the same value as this is constant data.
113      return 0;
114    }
115    
116    DataArrayView::ValueType::size_type
117    DataConstant::getLength() const
118    {
119      return m_data.size();
120    }
121    
122    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        
133    DataAbstract* DataConstant::getSlice(const DataArrayView::RegionType& region) const  DataAbstract*
134    {  DataConstant::getSlice(const DataArrayView::RegionType& region) const
135      return new DataConstant(*this,region);  {
136    }    return new DataConstant(*this,region);
137    }
138    void DataConstant::setSlice(const DataAbstract* value, const DataArrayView::RegionType& region)  
139    {  void
140      const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);  DataConstant::setSlice(const DataAbstract* value,
141      if (tempDataConst==0)                         const DataArrayView::RegionType& region)
142      {  {
143        throw DataException("Programming error - casting to DataConstant.");    const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
144      }    if (tempDataConst==0) {
145      getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region);      throw DataException("Programming error - casting to DataConstant.");
146    }    }
147      //
148    void DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
149    {    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
150      //    //
151      // reshape a rank zero data point    // check shape:
152      if (getPointDataView().getRank()!=0) {    if (getPointDataView().getRank()!=region.size()) {
153        stringstream temp;      throw DataException("Error - Invalid slice region.");
154        temp << "Error - Can only reshape Data with data points of rank 0. "    }
155         << "This Data has data points with rank: "    if (tempDataConst->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
156         << getPointDataView().getRank();      throw DataException (value->getPointDataView().createShapeErrorMessage(
157        throw DataException(temp.str());                  "Error - Couldn't copy slice due to shape mismatch.",shape));
158      }    }
159      m_data.resize(DataArrayView::noValues(shape),getPointDataView()());    //
160      DataArrayView newView(m_data,shape);    getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
161      setPointDataView(newView);  }
162    }  
163    void
164  }  // end of namespace  DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)
165    {
166      if (getPointDataView().getRank()!=0) {
167        stringstream temp;
168        temp << "Error - Can only reshape Data with data points of rank 0. "
169             << "This Data has data points with rank: " << getPointDataView().getRank();
170        throw DataException(temp.str());
171      }
172      int len = DataArrayView::noValues(shape);
173      m_data.resize(len,getPointDataView()(),len);
174      DataArrayView newView(m_data,shape);
175      setPointDataView(newView);
176    }
177    
178    int
179    DataConstant::archiveData(ofstream& archiveFile,
180                              const DataArrayView::ValueType::size_type noValues) const
181    {
182      return(m_data.archiveData(archiveFile, noValues));
183    }
184    
185    int
186    DataConstant::extractData(ifstream& archiveFile,
187                              const DataArrayView::ValueType::size_type noValues)
188    {
189      return(m_data.extractData(archiveFile, noValues));
190    }
191    
192    void
193    DataConstant::eigenvalues(DataAbstract* ev)
194    {
195      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
196      if (temp_ev==0) {
197        throw DataException("Error - DataConstant::eigenvalues: casting to DataConstant failed (propably a programming error).");
198      }
199      DataArrayView& thisView=getPointDataView();
200      DataArrayView& evView=ev->getPointDataView();
201      DataArrayView::eigenvalues(thisView,0,evView,0);
202    }
203    void
204    DataConstant::eigenvalues_and_eigenvectors(DataAbstract* ev,DataAbstract* V,const double tol)
205    {
206      DataConstant* temp_ev=dynamic_cast<DataConstant*>(ev);
207      if (temp_ev==0) {
208        throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
209      }
210      DataConstant* temp_V=dynamic_cast<DataConstant*>(V);
211      if (temp_V==0) {
212        throw DataException("Error - DataConstant::eigenvalues_and_eigenvectors: casting to DataConstant failed (propably a programming error).");
213      }
214      DataArrayView thisView=getPointDataView();
215      DataArrayView evView=ev->getPointDataView();
216      DataArrayView VView=V->getPointDataView();
217    
218      DataArrayView::eigenvalues_and_eigenvectors(thisView,0,evView,0,VView,tol);
219    }
220    
221    
222    }  // end of namespace

Legend:
Removed from v.100  
changed lines
  Added in v.682

  ViewVC Help
Powered by ViewVC 1.1.26