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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (hide annotations)
Tue Apr 12 04:45:05 2005 UTC (14 years, 6 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataConstant.cpp
File size: 5735 byte(s)
*** empty log message ***

1 jgs 102 //$Id$
2 jgs 82 /*
3     ******************************************************************************
4     * *
5     * COPYRIGHT ACcESS 2004 - All Rights Reserved *
6     * *
7     * This software is the property of ACcESS. No part of this code *
8     * may be copied in any form or by any means without the expressed written *
9     * consent of ACcESS. Copying, use or modification of this software *
10     * by any unauthorised person is illegal unless that person has a software *
11     * license agreement with ACcESS. *
12     * *
13     ******************************************************************************
14     */
15    
16     #include "escript/Data/DataConstant.h"
17     #include "escript/Data/DataException.h"
18     #include "esysUtils/EsysAssert.h"
19    
20     #include <iostream>
21     #include <boost/python/extract.hpp>
22    
23     using namespace std;
24    
25     namespace escript {
26    
27 jgs 102 DataConstant::DataConstant(const boost::python::numeric::array& value,
28     const FunctionSpace& what)
29     : DataAbstract(what)
30     {
31     DataArray temp(value);
32     //
33     // copy the data in the correct format
34     m_data=temp.getData();
35 jgs 119 //
36     // create the view of the data
37 jgs 102 DataArrayView tempView(m_data,temp.getView().getShape());
38     setPointDataView(tempView);
39     }
40 jgs 82
41 jgs 102 DataConstant::DataConstant(const DataArrayView& value,
42     const FunctionSpace& what)
43     : DataAbstract(what)
44     {
45     //
46     // copy the data in the correct format
47     m_data=value.getData();
48 jgs 119 //
49     // create the view of the data
50 jgs 102 DataArrayView tempView(m_data,value.getShape());
51     setPointDataView(tempView);
52     }
53 jgs 82
54 jgs 102 DataConstant::DataConstant(const DataConstant& other)
55     : DataAbstract(other.getFunctionSpace())
56     {
57     //
58     // copy the data in the correct format
59     m_data=other.m_data;
60 jgs 119 //
61     // create the view of the data
62 jgs 102 DataArrayView tempView(m_data,other.getPointDataView().getShape());
63     setPointDataView(tempView);
64     }
65 jgs 82
66 jgs 102 DataConstant::DataConstant(const DataConstant& other,
67     const DataArrayView::RegionType& region)
68     : DataAbstract(other.getFunctionSpace())
69     {
70     //
71     // get the shape of the slice to copy from
72     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
73     //
74     // allocate space for this new DataConstant's data
75     m_data.resize(DataArrayView::noValues(shape));
76     //
77     // create a view of the data with the correct shape
78     DataArrayView tempView(m_data,shape);
79 jgs 108 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
80 jgs 119 //
81     // load the view with the data from the slice
82 jgs 108 tempView.copySlice(other.getPointDataView(),region_loop_range);
83 jgs 119 setPointDataView(tempView);
84     }
85    
86     DataConstant::DataConstant(const FunctionSpace& what,
87     const DataArrayView::ShapeType &shape,
88     const DataArrayView::ValueType &data)
89     : DataAbstract(what)
90     {
91 jgs 102 //
92 jgs 119 // copy the data in the correct format
93     m_data=data;
94     //
95     // create the view of the data
96     DataArrayView tempView(m_data,shape);
97 jgs 102 setPointDataView(tempView);
98     }
99 jgs 82
100 jgs 102 string
101     DataConstant::toString() const
102     {
103     return getPointDataView().toString("");
104     }
105 jgs 82
106 jgs 102 DataArrayView::ValueType::size_type
107     DataConstant::getPointOffset(int sampleNo,
108     int dataPointNo) const
109     {
110     EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
111     "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
112     return 0;
113     }
114 jgs 82
115 jgs 102 DataArrayView::ValueType::size_type
116     DataConstant::getLength() const
117     {
118     return m_data.size();
119     }
120 jgs 82
121 jgs 102 DataArrayView
122     DataConstant::getDataPoint(int sampleNo,
123     int dataPointNo)
124     {
125     EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
126     "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
127     //
128     // Whatever the coord's always return the same value as this is constant data.
129     return getPointDataView();
130     }
131 jgs 82
132 jgs 102 DataAbstract*
133     DataConstant::getSlice(const DataArrayView::RegionType& region) const
134     {
135     return new DataConstant(*this,region);
136     }
137 jgs 82
138 jgs 102 void
139     DataConstant::setSlice(const DataAbstract* value,
140     const DataArrayView::RegionType& region)
141     {
142     const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
143     if (tempDataConst==0) {
144     throw DataException("Programming error - casting to DataConstant.");
145 jgs 82 }
146 jgs 108 //
147     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
148     DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
149     //
150     // check shape:
151     if (getPointDataView().getRank()!=region.size()) {
152     throw DataException("Error - Invalid slice region.");
153     }
154     if (tempDataConst->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
155     throw DataException (value->getPointDataView().createShapeErrorMessage(
156     "Error - Couldn't copy slice due to shape mismatch.",shape));
157     }
158     //
159     getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
160 jgs 102 }
161 jgs 82
162 jgs 102 void
163     DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)
164     {
165     if (getPointDataView().getRank()!=0) {
166     stringstream temp;
167     temp << "Error - Can only reshape Data with data points of rank 0. "
168     << "This Data has data points with rank: " << getPointDataView().getRank();
169     throw DataException(temp.str());
170 jgs 82 }
171 jgs 102 m_data.resize(DataArrayView::noValues(shape),getPointDataView()());
172     DataArrayView newView(m_data,shape);
173     setPointDataView(newView);
174     }
175 jgs 82
176     } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26