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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (show annotations)
Thu Jan 27 06:21:59 2005 UTC (14 years, 9 months ago) by jgs
Original Path: trunk/esys2/escript/src/Data/DataConstant.cpp
File size: 5402 byte(s)
*** empty log message ***

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 "esysUtils/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 DataArrayView tempView(m_data,temp.getView().getShape());
36 //
37 // copy the view of the data
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 DataArrayView tempView(m_data,value.getShape());
49 //
50 // copy the view of the data
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 DataArrayView tempView(m_data,other.getPointDataView().getShape());
61 //
62 // copy the view of the data
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 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 // copy the data from the slice to the temp view
80 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
81 tempView.copySlice(other.getPointDataView(),region_loop_range);
82 //
83 // store the temp view of the data in this object
84 setPointDataView(tempView);
85 }
86
87 string
88 DataConstant::toString() const
89 {
90 return getPointDataView().toString("");
91 }
92
93 DataArrayView::ValueType::size_type
94 DataConstant::getPointOffset(int sampleNo,
95 int dataPointNo) const
96 {
97 EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
98 "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
99 return 0;
100 }
101
102 DataArrayView::ValueType::size_type
103 DataConstant::getLength() const
104 {
105 return m_data.size();
106 }
107
108 DataArrayView
109 DataConstant::getDataPoint(int sampleNo,
110 int dataPointNo)
111 {
112 EsysAssert((validSamplePointNo(dataPointNo) && validSampleNo(sampleNo)),
113 "Invalid index, sampleNo: " << sampleNo << " dataPointNo: " << dataPointNo);
114 //
115 // Whatever the coord's always return the same value as this is constant data.
116 return getPointDataView();
117 }
118
119 DataAbstract*
120 DataConstant::getSlice(const DataArrayView::RegionType& region) const
121 {
122 return new DataConstant(*this,region);
123 }
124
125 void
126 DataConstant::setSlice(const DataAbstract* value,
127 const DataArrayView::RegionType& region)
128 {
129 const DataConstant* tempDataConst=dynamic_cast<const DataConstant*>(value);
130 if (tempDataConst==0) {
131 throw DataException("Programming error - casting to DataConstant.");
132 }
133 //
134 DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
135 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
136 //
137 // check shape:
138 if (getPointDataView().getRank()!=region.size()) {
139 throw DataException("Error - Invalid slice region.");
140 }
141 if (tempDataConst->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
142 throw DataException (value->getPointDataView().createShapeErrorMessage(
143 "Error - Couldn't copy slice due to shape mismatch.",shape));
144 }
145 //
146 getPointDataView().copySliceFrom(tempDataConst->getPointDataView(),region_loop_range);
147 }
148
149 void
150 DataConstant::reshapeDataPoint(const DataArrayView::ShapeType& shape)
151 {
152 if (getPointDataView().getRank()!=0) {
153 stringstream temp;
154 temp << "Error - Can only reshape Data with data points of rank 0. "
155 << "This Data has data points with rank: " << getPointDataView().getRank();
156 throw DataException(temp.str());
157 }
158 m_data.resize(DataArrayView::noValues(shape),getPointDataView()());
159 DataArrayView newView(m_data,shape);
160 setPointDataView(newView);
161 }
162
163 } // end of namespace

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26