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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 468 - (show annotations)
Wed Jan 25 06:50:39 2006 UTC (13 years, 9 months ago) by jgs
Original Path: trunk/escript/src/Data/DataConstant.cpp
File size: 6240 byte(s)
reorganised esysUtils to remove inc directory
1 //$Id$
2 /*
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 "EsysAssert.h"
19
20 #include <iostream>
21 #include <boost/python/extract.hpp>
22
23 using namespace std;
24
25 namespace escript {
26
27 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 //
36 // create the view of the data
37 DataArrayView tempView(m_data,temp.getView().getShape());
38 setPointDataView(tempView);
39 }
40
41 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 //
49 // create the view of the data
50 DataArrayView tempView(m_data,value.getShape());
51 setPointDataView(tempView);
52 }
53
54 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 //
61 // create the view of the data
62 DataArrayView tempView(m_data,other.getPointDataView().getShape());
63 setPointDataView(tempView);
64 }
65
66 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 int len = DataArrayView::noValues(shape);
76 m_data.resize(len,0.,len);
77 //
78 // create a view of the data with the correct shape
79 DataArrayView tempView(m_data,shape);
80 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
81 //
82 // load the view with the data from the slice
83 tempView.copySlice(other.getPointDataView(),region_loop_range);
84 setPointDataView(tempView);
85 }
86
87 DataConstant::DataConstant(const FunctionSpace& what,
88 const DataArrayView::ShapeType &shape,
89 const DataArrayView::ValueType &data)
90 : DataAbstract(what)
91 {
92 //
93 // copy the data in the correct format
94 m_data=data;
95 //
96 // create the view of the data
97 DataArrayView tempView(m_data,shape);
98 setPointDataView(tempView);
99 }
100
101 string
102 DataConstant::toString() const
103 {
104 return getPointDataView().toString("");
105 }
106
107 DataArrayView::ValueType::size_type
108 DataConstant::getPointOffset(int sampleNo,
109 int dataPointNo) const
110 {
111 EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
112 "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
113 //
114 // Whatever the coord's always return the same value as this is constant data.
115 return 0;
116 }
117
118 DataArrayView::ValueType::size_type
119 DataConstant::getLength() const
120 {
121 return m_data.size();
122 }
123
124 DataArrayView
125 DataConstant::getDataPoint(int sampleNo,
126 int dataPointNo)
127 {
128 EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
129 "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
130 //
131 // Whatever the coord's always return the same value as this is constant data.
132 return getPointDataView();
133 }
134
135 DataAbstract*
136 DataConstant::getSlice(const DataArrayView::RegionType& region) const
137 {
138 return new DataConstant(*this,region);
139 }
140
141 void
142 DataConstant::setSlice(const DataAbstract* value,
143 const DataArrayView::RegionType& region)
144 {
145 const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
146 if (tempDataConst==0) {
147 throw DataException("Programming error - casting to DataConstant.");
148 }
149 //
150 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
151 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
152 //
153 // check shape:
154 if (getPointDataView().getRank()!=region.size()) {
155 throw DataException("Error - Invalid slice region.");
156 }
157 if (tempDataConst->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
158 throw DataException (value->getPointDataView().createShapeErrorMessage(
159 "Error - Couldn't copy slice due to shape mismatch.",shape));
160 }
161 //
162 getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
163 }
164
165 void
166 DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)
167 {
168 if (getPointDataView().getRank()!=0) {
169 stringstream temp;
170 temp << "Error - Can only reshape Data with data points of rank 0. "
171 << "This Data has data points with rank: " << getPointDataView().getRank();
172 throw DataException(temp.str());
173 }
174 int len = DataArrayView::noValues(shape);
175 m_data.resize(len,getPointDataView()(),len);
176 DataArrayView newView(m_data,shape);
177 setPointDataView(newView);
178 }
179
180 int
181 DataConstant::archiveData(ofstream& archiveFile,
182 const DataArrayView::ValueType::size_type noValues) const
183 {
184 return(m_data.archiveData(archiveFile, noValues));
185 }
186
187 int
188 DataConstant::extractData(ifstream& archiveFile,
189 const DataArrayView::ValueType::size_type noValues)
190 {
191 return(m_data.extractData(archiveFile, noValues));
192 }
193
194 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26