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

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

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

revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC revision 113 by jgs, Mon Feb 28 07:06:33 2005 UTC
# Line 1  Line 1 
1    // $Id$
2  /*  /*
3   ******************************************************************************   ******************************************************************************
4   *                                                                            *   *                                                                            *
# Line 28  using namespace boost; Line 29  using namespace boost;
29    
30  namespace escript {  namespace escript {
31    
32    DataExpanded::DataExpanded(const boost::python::numeric::array& value, const FunctionSpace& what) : DataAbstract(what)  DataExpanded::DataExpanded(const boost::python::numeric::array& value,
33    {                             const FunctionSpace& what)
34      //cout << "Calling DataExpanded constructor 1." << endl;    : DataAbstract(what)
35      DataArrayView::ShapeType tempShape;  {
36      //    DataArrayView::ShapeType tempShape;
37      // extract the shape from the python array    //
38      for (int i=0; i<value.getrank(); ++i) {    // extract the shape from the python array
39        cout << extract<int>(value.getshape()[i]) << endl;    for (int i=0; i<value.getrank(); ++i) {
40        tempShape.push_back(extract<int>(value.getshape()[i]));      //cout << extract<int>(value.getshape()[i]) << endl;
41      }      tempShape.push_back(extract<int>(value.getshape()[i]));
42      initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());    }
43      //    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
44      // copy the given value to every data point    //
45      copy(value);    // copy the given value to every data point
46    }    copy(value);
47    }
48    DataExpanded::DataExpanded(const DataExpanded& other) : DataAbstract(other.getFunctionSpace()), m_data(other.m_data)  
49    {      DataExpanded::DataExpanded(const DataExpanded& other)
50      //cout << "Calling DataExpanded copy constructor." << endl;    : DataAbstract(other.getFunctionSpace()),
51      //    m_data(other.m_data)
52      // create the view for the data  {    
53      DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());    //
54      setPointDataView(temp);    // create the view for the data
55    }    DataArrayView temp(m_data.getData(),other.getPointDataView().getShape());
56      setPointDataView(temp);
57    DataExpanded::DataExpanded(const DataConstant& other) : DataAbstract(other.getFunctionSpace())  }
58    {      
59      //cout << "Creating DataExpanded from DataConstant." << endl;  DataExpanded::DataExpanded(const DataConstant& other)
60      initialise(other.getPointDataView().getShape(),other.getNumSamples(), other.getNumDPPSample());    : DataAbstract(other.getFunctionSpace())
61      //  {    
62      // DataConstant only has one value    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
63      copy(other.getPointDataView());    //
64    }    // DataConstant only has one value
65      copy(other.getPointDataView());
66    DataExpanded::DataExpanded(const DataTagged& other) : DataAbstract(other.getFunctionSpace())  }
67    {      
68      //cout << "Creating DataExpanded from DataTagged." << endl;  DataExpanded::DataExpanded(const DataTagged& other)
69      initialise(other.getPointDataView().getShape(),other.getNumSamples(), other.getNumDPPSample());    : DataAbstract(other.getFunctionSpace())
70      int i,j;  {    
71      DataArrayView::ValueType::size_type numRows=m_data.getNumRows();    initialise(other.getPointDataView().getShape(),other.getNumSamples(),other.getNumDPPSample());
72      DataArrayView::ValueType::size_type numCols=m_data.getNumCols();    int i,j;
73      DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
74      DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
75  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
76      for (i=0;i<numRows;++i) {    for (i=0;i<numRows;++i) {
77        for (j=0;j<numCols;++j) {      for (j=0;j<numCols;++j) {
78      try {        try {
79        getPointDataView().copy(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j));          getPointDataView().copy(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j));
     }  
     catch (std::exception& e) {  
       cout << e.what() << endl;  
     }  
80        }        }
81      }        catch (std::exception& e) {
82    }          cout << e.what() << endl;
   
   DataExpanded::DataExpanded(const DataExpanded& other, const DataArrayView::RegionType& region) : DataAbstract(other.getFunctionSpace())  
   {  
     //cout << "Creating DataExpanded by slicing from another DataExpanded." << endl;  
     //  
     // get the shape of the slice  
     DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));  
     initialise(shape,other.getNumSamples(),other.getNumDPPSample());  
     //  
     // copy the data  
     DataArrayView::ValueType::size_type numRows=m_data.getNumRows();  
     DataArrayView::ValueType::size_type numCols=m_data.getNumCols();  
     int i,j;  
 #pragma omp parallel for private(i,j) schedule(static)  
     for (i=0;i<numRows;++i) {  
       for (j=0;j<numCols;++j) {  
     try {  
       getPointDataView().copySlice(getPointOffset(i,j), other.getPointDataView(), other.getPointOffset(i,j), region);  
     }  
     catch (std::exception& e) {  
       cout << e.what() << endl;  
     }  
83        }        }
84      }      }
85    }    }
86    }
87    
88    DataExpanded::DataExpanded(const DataArrayView& value,const FunctionSpace& what): DataAbstract(what)  DataExpanded::DataExpanded(const DataExpanded& other,
89    {                             const DataArrayView::RegionType& region)
90      //cout << "Calling DataExpanded constructor 2." << endl;    : DataAbstract(other.getFunctionSpace())
91      DataArrayView::ShapeType tempShape=value.getShape();  {
92      initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());    //
93      copy(value);    // get the shape of the slice
94    }    DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
95      //
96    DataExpanded::~DataExpanded()    // initialise this Data object to the shape of the slice
97    {    initialise(shape,other.getNumSamples(),other.getNumDPPSample());
98      //cout << "Destructing DataExpanded." << endl;    //
99    }    DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
100      //
101    DataAbstract* DataExpanded::getSlice(const DataArrayView::RegionType& region) const    // copy the data
102    {    DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
103      return new DataExpanded(*this,region);    DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
104    }    int i,j;
   
   void DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)  
   {  
     //  
     // reshape a rank zero data point  
     if (getPointDataView().getRank()!=0) {  
       stringstream temp;  
       temp << "Error - Can only reshape Data with data points of rank 0. "  
        << "This Data has data points with rank: "  
        << getPointDataView().getRank();  
       throw DataException(temp.str());  
     }  
     DataBlocks2D newData(getNumSamples(),getNumDPPSample(), DataArrayView::noValues(shape));  
     DataArrayView newView(newData.getData(),shape);  
     //  
     // Copy the original data to every value for the new shape  
     int i,j;  
     int nRows=m_data.getNumRows();  
     int nCols=m_data.getNumCols();  
     #pragma omp parallel for private(i,j) schedule(static)  
     for (i=0;i<nRows;++i) {  
       for (j=0;j<nCols;++j) {  
     //  
     // Copy the data into the specified offset  
     // NOTE: An exception may be thown from this call if  
     // DOASSERT is on which of course will play  
     // havoc with the omp threads. Run single threaded  
     // if using DOASSERT.  
     newView.copy(newData.index(i,j),m_data.getData()[m_data.index(i,j)]);  
       }  
     }  
     m_data.Swap(newData);  
     DataArrayView temp(m_data.getData(),shape);  
     setPointDataView(temp);  
   }  
   
   void DataExpanded::setSlice(const DataAbstract* value, const DataArrayView::RegionType& region)  
   {  
     const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);  
     if (tempDataExp==0) {  
       throw DataException("Programming error - casting to DataExpanded.");  
     }  
     if (getPointDataView().getRank()!=region.size()) {  
       throw DataException("Error - Invalid slice region.");  
     }  
     if (!value->getPointDataView().checkShape(DataArrayView::getResultSliceShape(region))) {  
       throw DataException (value->getPointDataView().createShapeErrorMessage(  
                 "Error - Couldn't copy slice due to shape mismatch.",DataArrayView::getResultSliceShape(region)));  
     }  
     //  
     // copy the data  
     DataArrayView::ValueType::size_type numRows=m_data.getNumRows();  
     DataArrayView::ValueType::size_type numCols=m_data.getNumCols();  
     int i,j;  
105  #pragma omp parallel for private(i,j) schedule(static)  #pragma omp parallel for private(i,j) schedule(static)
106      for (i=0;i<numRows;++i) {    for (i=0;i<numRows;++i) {
107        for (j=0;j<numCols;++j) {      for (j=0;j<numCols;++j) {
108      getPointDataView().copySliceFrom(getPointOffset(i,j), tempDataExp->getPointDataView(), tempDataExp->getPointOffset(i,j), region);        try {
109            getPointDataView().copySlice(getPointOffset(i,j),other.getPointDataView(),other.getPointOffset(i,j),region_loop_range);
110        }        }
111      }        catch (std::exception& e) {
112    }          cout << e.what() << endl;
   
   void DataExpanded::copy(const DataArrayView& value)  
   {  
     //  
     // Copy a single value to every data point  
     int i,j;  
     int nRows=m_data.getNumRows();  
     int nCols=m_data.getNumCols();  
     #pragma omp parallel for private(i,j) schedule(static)  
     for (i=0;i<nRows;++i) {  
       for (j=0;j<nCols;++j) {  
     //  
     // Copy the data into the specified offset  
     // NOTE: An exception may be thown from this call if  
     // DOASSERT is on which of course will play  
     // havoc with the omp threads. Run single threaded  
     // if using DOASSERT.  
     getPointDataView().copy(m_data.index(i,j),value);  
113        }        }
114      }      }
115    }    }
116    }
117    
118    void DataExpanded::copy(const boost::python::numeric::array& value)  DataExpanded::DataExpanded(const DataArrayView& value,
119    {                             const FunctionSpace& what)
120      //    : DataAbstract(what)
121      // first convert the numarray into a DataArrayView format  {
122      DataArray temp(value);    DataArrayView::ShapeType tempShape=value.getShape();
123      //    initialise(tempShape,what.getNumSamples(),what.getNumDPPSample());
124      // check the input shape matches this shape, this will throw an exception    copy(value);
125      if (!getPointDataView().checkShape(temp.getView().getShape())) {  }
126        throw DataException(getPointDataView().createShapeErrorMessage(  
127           "Error - (DataExpanded) Cannot copy due to shape mismatch.",  DataExpanded::~DataExpanded()
128           temp.getView().getShape()));  {
129    }
130    
131    void
132    DataExpanded::reshapeDataPoint(const DataArrayView::ShapeType& shape)
133    {
134      //
135      // reshape a rank zero data point
136      if (getPointDataView().getRank()!=0) {
137        stringstream temp;
138        temp << "Error - Can only reshape Data with data points of rank 0. "
139             << "This Data has data points with rank: "
140             << getPointDataView().getRank();
141        throw DataException(temp.str());
142      }
143      DataBlocks2D newData(getNumSamples(),getNumDPPSample(), DataArrayView::noValues(shape));
144      DataArrayView newView(newData.getData(),shape);
145      //
146      // Copy the original data to every value for the new shape
147      int i,j;
148      int nRows=m_data.getNumRows();
149      int nCols=m_data.getNumCols();
150    #pragma omp parallel for private(i,j) schedule(static)
151      for (i=0;i<nRows;++i) {
152        for (j=0;j<nCols;++j) {
153          //
154          // Copy the data into the specified offset
155          // NOTE: An exception may be thown from this call if
156          // DOASSERT is on which of course will play
157          // havoc with the omp threads. Run single threaded
158          // if using DOASSERT.
159          newView.copy(newData.index(i,j),m_data.getData()[m_data.index(i,j)]);
160        }
161      }
162      m_data.Swap(newData);
163      DataArrayView temp(m_data.getData(),shape);
164      setPointDataView(temp);
165    }
166    
167    DataAbstract*
168    DataExpanded::getSlice(const DataArrayView::RegionType& region) const
169    {
170      return new DataExpanded(*this,region);
171    }
172    
173    void
174    DataExpanded::setSlice(const DataAbstract* value,
175                           const DataArrayView::RegionType& region)
176    {
177      const DataExpanded* tempDataExp=dynamic_cast<const DataExpanded*>(value);
178      if (tempDataExp==0) {
179        throw DataException("Programming error - casting to DataExpanded.");
180      }
181      //
182      DataArrayView::ShapeType shape(DataArrayView::getResultSliceShape(region));
183      DataArrayView::RegionLoopRangeType region_loop_range=getSliceRegionLoopRange(region);
184      //
185      // check shape:
186      if (getPointDataView().getRank()!=region.size()) {
187        throw DataException("Error - Invalid slice region.");
188      }
189      if (tempDataExp->getPointDataView().getRank()>0 and !value->getPointDataView().checkShape(shape)) {
190        throw DataException (value->getPointDataView().createShapeErrorMessage(
191            "Error - Couldn't copy slice due to shape mismatch.",shape));
192      }
193      //
194      // copy the data
195      DataArrayView::ValueType::size_type numRows=m_data.getNumRows();
196      DataArrayView::ValueType::size_type numCols=m_data.getNumCols();
197      int i, j;
198    #pragma omp parallel for private(i,j) schedule(static)
199      for (i=0;i<numRows;i++) {
200        for (j=0;j<numCols;j++) {
201          getPointDataView().copySliceFrom(getPointOffset(i,j),tempDataExp->getPointDataView(),tempDataExp->getPointOffset(i,j),region_loop_range);
202      }      }
     //  
     // now copy over the entire data structure  
     copy(temp.getView());  
203    }    }
204    }
205    void DataExpanded::initialise(const DataArrayView::ShapeType& shape, int noSamples, int noDataPointsPerSample)  
206    {  void
207      //  DataExpanded::copy(const DataArrayView& value)
208      // resize data to the required size  {
209      //cout << "noDataPointsPerSample=" << noDataPointsPerSample    //
210      //     << " noSamples=" << noSamples    // Copy a single value to every data point
211      //     << " noValuesPerDataPoint=" << DataArrayView::noValues(shape) << endl;    int nRows=m_data.getNumRows();
212      m_data.resize(noSamples,noDataPointsPerSample, DataArrayView::noValues(shape));    int nCols=m_data.getNumCols();
213      //    int i, j;
214      // create a point data viewer of the data  #pragma omp parallel for private(i,j) schedule(static)
215      DataArrayView temp(m_data.getData(),shape);    for (i=0;i<nRows;++i) {
216      setPointDataView(temp);      for (j=0;j<nCols;++j) {
217      //cout << "leaving initialise." << endl;        //
218    }        // Copy the data into the specified offset
219          // NOTE: An exception may be thown from this call if
220    string DataExpanded::toString() const        // DOASSERT is on which of course will play
221    {        // havoc with the omp threads. Run single threaded
222      stringstream temp;        // if using DOASSERT.
223      //        getPointDataView().copy(m_data.index(i,j),value);
224      // create a temporary view as the offset will be changed      }
225      DataArrayView tempView(getPointDataView().getData(), getPointDataView().getShape(), getPointDataView().getOffset());    }
226      for (int i=0;i<m_data.getNumRows();++i) {  }
227        for (int j=0;j<m_data.getNumCols();++j) {  
228          tempView.setOffset(m_data.index(i,j));  void
229      stringstream suffix;  DataExpanded::copy(const boost::python::numeric::array& value)
230      suffix << "(" << i << "," << j << ")";  {
231      temp << tempView.toString(suffix.str());    //
232      if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {    // first convert the numarray into a DataArrayView format
233        temp << endl;    DataArray temp(value);
234      }    //
235      // check the input shape matches this shape, this will throw an exception
236      if (!getPointDataView().checkShape(temp.getView().getShape())) {
237        throw DataException(getPointDataView().createShapeErrorMessage(
238                            "Error - (DataExpanded) Cannot copy due to shape mismatch.",
239                            temp.getView().getShape()));
240      }
241      //
242      // now copy over the entire data structure
243      copy(temp.getView());
244    }
245    
246    void
247    DataExpanded::initialise(const DataArrayView::ShapeType& shape,
248                             int noSamples,
249                             int noDataPointsPerSample)
250    {
251      //
252      // resize data to the required size
253      m_data.resize(noSamples,noDataPointsPerSample,DataArrayView::noValues(shape));
254      //
255      // create a point data viewer of the data
256      DataArrayView temp(m_data.getData(),shape);
257      setPointDataView(temp);
258    }
259    
260    string
261    DataExpanded::toString() const
262    {
263      stringstream temp;
264      //
265      // create a temporary view as the offset will be changed
266      DataArrayView tempView(getPointDataView().getData(),getPointDataView().getShape(),getPointDataView().getOffset());
267      for (int i=0;i<m_data.getNumRows();++i) {
268        for (int j=0;j<m_data.getNumCols();++j) {
269          tempView.setOffset(m_data.index(i,j));
270          stringstream suffix;
271          suffix << "(" << i << "," << j << ")";
272          temp << tempView.toString(suffix.str());
273          if (!(i==(m_data.getNumRows()-1) && j==(m_data.getNumCols()-1))) {
274            temp << endl;
275        }        }
276      }      }
     return temp.str();  
   }  
   
   DataArrayView::ValueType::size_type DataExpanded::getPointOffset(int sampleNo, int dataPointNo) const  
   {  
     return m_data.index(sampleNo,dataPointNo);  
277    }    }
278      return temp.str();
279    }
280    
281    DataArrayView DataExpanded::getDataPoint(int sampleNo, int dataPointNo)  DataArrayView::ValueType::size_type
282    {  DataExpanded::getPointOffset(int sampleNo,
283      DataArrayView temp(m_data.getData(), getPointDataView().getShape(), m_data.index(sampleNo,dataPointNo));                               int dataPointNo) const
284      return temp;  {
285    }    return m_data.index(sampleNo,dataPointNo);
286    }
287    DataArrayView::ValueType::size_type DataExpanded::getLength() const  
288    {  DataArrayView
289      return m_data.getData().size();  DataExpanded::getDataPoint(int sampleNo,
290    }                             int dataPointNo)
291    {
292      DataArrayView temp(m_data.getData(),getPointDataView().getShape(),m_data.index(sampleNo,dataPointNo));
293      return temp;
294    }
295    
296    DataArrayView::ValueType::size_type
297    DataExpanded::getLength() const
298    {
299      return m_data.getData().size();
300    }
301    
302    void
303    DataExpanded::setRefValue(int ref,
304                              const DataArray& value)
305    {
306      //
307      // Get the number of samples and data-points per sample.
308      int numSamples = getNumSamples();
309      int numDPPSample = getNumDPPSample();
310    
311      //
312      // Determine the sample number which corresponds to this reference number.
313      int sampleNo = -1;
314      int tempRef = -1;
315      for (int n=0; n<numSamples; n++) {
316        tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
317        if (tempRef == ref) {
318          sampleNo = n;
319          break;
320        }
321      }
322      if (sampleNo == -1) {
323        throw DataException("DataExpanded::setRefValue error: invalid ref number supplied.");
324      }
325    
326      for (int n=0; n<numDPPSample; n++) {
327        //
328        // Get each data-point in the sample in turn.
329        DataArrayView pointView = getDataPoint(sampleNo, n);
330    
331        //
332        // Assign the values in the DataArray to this data-point.
333        pointView.copy(value.getView());
334      }
335    }
336    
337    void
338    DataExpanded::getRefValue(int ref,
339                              DataArray& value)
340    {
341      //
342      // Get the number of samples and data-points per sample.
343      int numSamples = getNumSamples();
344      int numDPPSample = getNumDPPSample();
345    
346      //
347      // Determine the sample number which corresponds to this reference number
348      int sampleNo = -1;
349      int tempRef = -1;
350      for (int n=0; n<numSamples; n++) {
351        tempRef = getFunctionSpace().getReferenceNoFromSampleNo(n);
352        if (tempRef == ref) {
353          sampleNo = n;
354          break;
355        }
356      }
357      if (sampleNo == -1) {
358        throw DataException("DataExpanded::getRefValue error: invalid ref number supplied.");
359      }
360    
361      //
362      // Get the first data-point associated with this sample number.
363      DataArrayView pointView = getDataPoint(sampleNo, 0);
364    
365      //
366      // Load the values from this data-point into the DataArray
367      value.getView().copy(pointView);
368    }
369    
370  }  // end of namespace  }  // end of namespace

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

  ViewVC Help
Powered by ViewVC 1.1.26