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

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

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

revision 1033 by phornby, Wed Mar 14 06:32:09 2007 UTC revision 1034 by gross, Wed Mar 14 23:49:20 2007 UTC
# Line 550  Data::getDataPointShape() const Line 550  Data::getDataPointShape() const
550    
551    
552    
 void  
 Data::fillFromNumArray(const boost::python::numeric::array num_array)  
 {  
   if (isProtected()) {  
         throw DataException("Error - attempt to update protected Data object.");  
   }  
   //  
   // check rank  
   if (num_array.getrank()<getDataPointRank())  
       throw DataException("Rank of numarray does not match Data object rank");  
   
   //  
   // check shape of num_array  
   for (int i=0; i<getDataPointRank(); i++) {  
     if (extract<int>(num_array.getshape()[i+1])!=getDataPointShape()[i])  
        throw DataException("Shape of numarray does not match Data object rank");  
   }  
   
   //  
   // make sure data is expanded:  
   if (!isExpanded()) {  
     expand();  
   }  
   
   //  
   // and copy over  
   m_data->copyAll(num_array);  
 }  
   
 const  
 boost::python::numeric::array  
 Data::convertToNumArray()  
 {  
   //  
   // determine the total number of data points  
   int numSamples = getNumSamples();  
   int numDataPointsPerSample = getNumDataPointsPerSample();  
   int numDataPoints = numSamples * numDataPointsPerSample;  
   
   //  
   // determine the rank and shape of each data point  
   int dataPointRank = getDataPointRank();  
   DataArrayView::ShapeType dataPointShape = getDataPointShape();  
   
   //  
   // create the numeric array to be returned  
   boost::python::numeric::array numArray(0.0);  
   
   //  
   // the rank of the returned numeric array will be the rank of  
   // the data points, plus one. Where the rank of the array is n,  
   // the last n-1 dimensions will be equal to the shape of the  
   // data points, whilst the first dimension will be equal to the  
   // total number of data points. Thus the array will consist of  
   // a serial vector of the data points.  
   int arrayRank = dataPointRank + 1;  
   DataArrayView::ShapeType arrayShape;  
   arrayShape.push_back(numDataPoints);  
   for (int d=0; d<dataPointRank; d++) {  
      arrayShape.push_back(dataPointShape[d]);  
   }  
   
   //  
   // resize the numeric array to the shape just calculated  
   if (arrayRank==1) {  
     numArray.resize(arrayShape[0]);  
   }  
   if (arrayRank==2) {  
     numArray.resize(arrayShape[0],arrayShape[1]);  
   }  
   if (arrayRank==3) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);  
   }  
   if (arrayRank==4) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);  
   }  
   if (arrayRank==5) {  
     numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);  
   }  
   
   //  
   // loop through each data point in turn, loading the values for that data point  
   // into the numeric array.  
   int dataPoint = 0;  
   for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {  
     for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {  
       DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);  
       if (dataPointRank==0) {  
         numArray[dataPoint]=dataPointView();  
       }  
       if (dataPointRank==1) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           numArray[make_tuple(dataPoint,i)]=dataPointView(i);  
         }  
       }  
       if (dataPointRank==2) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             numArray[make_tuple(dataPoint,i,j)] = dataPointView(i,j);  
           }  
         }  
       }  
       if (dataPointRank==3) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               numArray[make_tuple(dataPoint,i,j,k)]=dataPointView(i,j,k);  
             }  
           }  
         }  
       }  
       if (dataPointRank==4) {  
         for (int i=0; i<dataPointShape[0]; i++) {  
           for (int j=0; j<dataPointShape[1]; j++) {  
             for (int k=0; k<dataPointShape[2]; k++) {  
               for (int l=0; l<dataPointShape[3]; l++) {  
                 numArray[make_tuple(dataPoint,i,j,k,l)]=dataPointView(i,j,k,l);  
               }  
             }  
           }  
         }  
       }  
       dataPoint++;  
     }  
   }  
   
   //  
   // return the loaded array  
   return numArray;  
 }  
   
   
553  const  const
554  boost::python::numeric::array  boost::python::numeric::array
555  Data:: getValueOfDataPoint(int dataPointNo)  Data:: getValueOfDataPoint(int dataPointNo)
# Line 773  Data:: getValueOfDataPoint(int dataPoint Line 641  Data:: getValueOfDataPoint(int dataPoint
641    
642  }  }
643  void  void
644  Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array num_array)  Data::setValueOfDataPointToPyObject(int dataPointNo, const boost::python::object& py_object)
645    {
646        // this will throw if the value cannot be represented
647        boost::python::numeric::array num_array(py_object);
648        setValueOfDataPointToArray(dataPointNo,num_array);
649    
650    
651    }
652    
653    void
654    Data::setValueOfDataPointToArray(int dataPointNo, const boost::python::numeric::array& num_array)
655  {  {
656    if (isProtected()) {    if (isProtected()) {
657          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");

Legend:
Removed from v.1033  
changed lines
  Added in v.1034

  ViewVC Help
Powered by ViewVC 1.1.26