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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 155 - (hide annotations)
Wed Nov 9 02:02:19 2005 UTC (14 years ago) by jgs
Original Path: trunk/escript/src/Data/DataConstant.cpp
File size: 6250 byte(s)
move all directories from trunk/esys2 into trunk and remove esys2

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 jgs 151 int len = DataArrayView::noValues(shape);
76     m_data.resize(len,0.,len);
77 jgs 102 //
78     // create a view of the data with the correct shape
79     DataArrayView tempView(m_data,shape);
80 jgs 108 DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
81 jgs 119 //
82     // load the view with the data from the slice
83 jgs 108 tempView.copySlice(other.getPointDataView(),region_loop_range);
84 jgs 119 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 jgs 102 //
93 jgs 119 // 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 jgs 102 setPointDataView(tempView);
99     }
100 jgs 82
101 jgs 102 string
102     DataConstant::toString() const
103     {
104     return getPointDataView().toString("");
105     }
106 jgs 82
107 jgs 102 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 jgs 122 //
114     // Whatever the coord's always return the same value as this is constant data.
115 jgs 102 return 0;
116     }
117 jgs 82
118 jgs 102 DataArrayView::ValueType::size_type
119     DataConstant::getLength() const
120     {
121     return m_data.size();
122     }
123 jgs 82
124 jgs 102 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 jgs 82
135 jgs 102 DataAbstract*
136     DataConstant::getSlice(const DataArrayView::RegionType& region) const
137     {
138     return new DataConstant(*this,region);
139     }
140 jgs 82
141 jgs 102 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 jgs 82 }
149 jgs 108 //
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 jgs 102 }
164 jgs 82
165 jgs 102 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 jgs 82 }
174 jgs 151 int len = DataArrayView::noValues(shape);
175     m_data.resize(len,getPointDataView()(),len);
176 jgs 102 DataArrayView newView(m_data,shape);
177     setPointDataView(newView);
178     }
179 jgs 82
180 jgs 123 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 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