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

Legend:
Removed from v.82  
changed lines
  Added in v.117

  ViewVC Help
Powered by ViewVC 1.1.26