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

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

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

revision 102 by jgs, Wed Dec 15 07:08:39 2004 UTC revision 117 by jgs, Fri Apr 1 05:48:57 2005 UTC
# Line 444  Data::getDataPointShape() const Line 444  Data::getDataPointShape() const
444  }  }
445    
446  boost::python::numeric::array  boost::python::numeric::array
447    Data::convertToNumArray()
448    {
449      //
450      // determine the current number of data points
451      int numSamples = getNumSamples();
452      int numDataPointsPerSample = getNumDataPointsPerSample();
453      int numDataPoints = numSamples * numDataPointsPerSample;
454    
455      //
456      // determine the rank and shape of each data point
457      int dataPointRank = getDataPointRank();
458      DataArrayView::ShapeType dataPointShape = getDataPointShape();
459    
460      //
461      // create the numeric array to be returned
462      boost::python::numeric::array numArray(0.0);
463    
464      //
465      // the rank of the returned numeric array will be the rank of
466      // the data points, plus one. Where the rank of the array is n,
467      // the last n-1 dimensions will be equal to the shape of the
468      // data points, whilst the first dimension will be equal to the
469      // total number of data points. Thus the array will consist of
470      // a serial vector of the data points.
471      int arrayRank = dataPointRank + 1;
472      DataArrayView::ShapeType arrayShape;
473      arrayShape.push_back(numDataPoints);
474      for (int d=0; d<dataPointRank; d++) {
475         arrayShape.push_back(dataPointShape[d]);
476      }
477    
478      //
479      // resize the numeric array to the shape just calculated
480      if (arrayRank==1) {
481        numArray.resize(arrayShape[0]);
482      }
483      if (arrayRank==2) {
484        numArray.resize(arrayShape[0],arrayShape[1]);
485      }
486      if (arrayRank==3) {
487        numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2]);
488      }
489      if (arrayRank==4) {
490        numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3]);
491      }
492      if (arrayRank==5) {
493        numArray.resize(arrayShape[0],arrayShape[1],arrayShape[2],arrayShape[3],arrayShape[4]);
494      }
495    
496      //
497      // loop through each data point in turn, loading the values for that data point
498      // into the numeric array.
499      int dataPoint = 0;
500      for (int sampleNo = 0; sampleNo < numSamples; sampleNo++) {
501        for (int dataPointNo = 0; dataPointNo < numDataPointsPerSample; dataPointNo++) {
502    
503          DataArrayView dataPointView = getDataPoint(sampleNo, dataPointNo);
504    
505          if (dataPointRank==0) {
506            numArray[dataPoint]=dataPointView();
507          }
508    
509          if (dataPointRank==1) {
510            for (int i=0; i<dataPointShape[0]; i++) {
511              numArray[dataPoint][i]=dataPointView(i);
512            }
513          }
514    
515          if (dataPointRank==2) {
516            for (int i=0; i<dataPointShape[0]; i++) {
517              for (int j=0; j<dataPointShape[1]; j++) {
518                numArray[dataPoint][i][j] = dataPointView(i,j);
519              }
520            }
521          }
522    
523          if (dataPointRank==3) {
524            for (int i=0; i<dataPointShape[0]; i++) {
525              for (int j=0; j<dataPointShape[1]; j++) {
526                for (int k=0; k<dataPointShape[2]; k++) {
527                  numArray[dataPoint][i][j][k]=dataPointView(i,j,k);
528                }
529              }
530            }
531          }
532    
533          if (dataPointRank==4) {
534            for (int i=0; i<dataPointShape[0]; i++) {
535              for (int j=0; j<dataPointShape[1]; j++) {
536                for (int k=0; k<dataPointShape[2]; k++) {
537                  for (int l=0; l<dataPointShape[3]; l++) {
538                    numArray[dataPoint][i][j][k][l]=dataPointView(i,j,k,l);
539                  }
540                }
541              }
542            }
543          }
544    
545          dataPoint++;
546    
547        }
548      }
549    
550      //
551      // return the loaded array
552      return numArray;
553    }
554    
555    boost::python::numeric::array
556  Data::integrate() const  Data::integrate() const
557  {  {
558    int index;    int index;
# Line 460  Data::integrate() const Line 569  Data::integrate() const
569    // and load the array with the integral values    // and load the array with the integral values
570    boost::python::numeric::array bp_array(1.0);    boost::python::numeric::array bp_array(1.0);
571    if (rank==0) {    if (rank==0) {
572        bp_array.resize(1);
573      index = 0;      index = 0;
574      bp_array[0] = integrals[index];      bp_array[0] = integrals[index];
575    }    }
# Line 539  Data::ln() const Line 649  Data::ln() const
649    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::log);
650  }  }
651    
652    Data
653    Data::sign() const
654    {
655      return escript::unaryOp(*this,escript::fsign);
656    }
657    
658    Data
659    Data::abs() const
660    {
661      return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);
662    }
663    
664    Data
665    Data::neg() const
666    {
667      return escript::unaryOp(*this,negate<double>());
668    }
669    
670    Data
671    Data::pos() const
672    {
673      return (*this);
674    }
675    
676    Data
677    Data::exp() const
678    {
679      return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);
680    }
681    
682    Data
683    Data::sqrt() const
684    {
685      return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);
686    }
687    
688  double  double
689  Data::Lsup() const  Data::Lsup() const
690  {  {
# Line 548  Data::Lsup() const Line 694  Data::Lsup() const
694  }  }
695    
696  double  double
697    Data::Linf() const
698    {
699      //
700      // set the initial absolute minimum value to max double
701      return algorithm(DataAlgorithmAdapter<AbsMin>(numeric_limits<double>::max()));
702    }
703    
704    double
705  Data::sup() const  Data::sup() const
706  {  {
707    //    //
708    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
709    return algorithm(DataAlgorithmAdapter<FMax>(numeric_limits<double>::min()));    return algorithm(DataAlgorithmAdapter<FMax>(numeric_limits<double>::max()*-1));
710  }  }
711    
712  double  double
# Line 566  Data::inf() const Line 720  Data::inf() const
720  Data  Data
721  Data::maxval() const  Data::maxval() const
722  {  {
723    // not implemented - will use dp_algorithm    //
724    return (*this);    // set the initial maximum value to min possible double
725      return dp_algorithm(DataAlgorithmAdapter<FMax>(numeric_limits<double>::max()*-1));
726  }  }
727    
728  Data  Data
729  Data::minval() const  Data::minval() const
730  {  {
731    // not implemented - will use dp_algorithm    //
732    return (*this);    // set the initial minimum value to max possible double
733      return dp_algorithm(DataAlgorithmAdapter<FMin>(numeric_limits<double>::max()));
734  }  }
735    
736  Data  Data
737  Data::length() const  Data::length() const
738  {  {
739    // not implemented - will use dp_algorithm    return dp_algorithm(DataAlgorithmAdapter<Length>(0));
   return (*this);  
740  }  }
741    
742  Data  Data
743  Data::trace() const  Data::trace() const
744  {  {
745    // not implemented - will use dp_algorithm    return dp_algorithm(DataAlgorithmAdapter<Trace>(0));
   return (*this);  
746  }  }
747    
748  Data  Data
749  Data::transpose(int axis) const  Data::transpose(int axis) const
750  {  {
751    // not implemented    // not implemented
752    return (*this);    throw DataException("Error - Data::transpose not implemented yet.");
753  }    return Data();
   
 Data  
 Data::sign() const  
 {  
   return escript::unaryOp(*this,escript::fsign);  
754  }  }
755    
756  Data  void
757  Data::abs() const  Data::saveDX(std::string fileName) const
 {  
   return escript::unaryOp(*this,(Data::UnaryDFunPtr)::fabs);  
 }  
   
 Data  
 Data::neg() const  
 {  
   return escript::unaryOp(*this,negate<double>());  
 }  
   
 Data  
 Data::pos() const  
 {  
   return (*this);  
 }  
   
 Data  
 Data::exp() const  
758  {  {
759    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::exp);    getDomain().saveDX(fileName,*this);
760      return;
761  }  }
762    
763  Data  void
764  Data::sqrt() const  Data::saveVTK(std::string fileName) const
765  {  {
766    return escript::unaryOp(*this,(Data::UnaryDFunPtr)::sqrt);    getDomain().saveVTK(fileName,*this);
767      return;
768  }  }
769    
770  Data&  Data&
# Line 959  Data::setItemD(const boost::python::obje Line 1092  Data::setItemD(const boost::python::obje
1092    if (slice_region.size()!=view.getRank()) {    if (slice_region.size()!=view.getRank()) {
1093      throw DataException("Error - slice size does not match Data rank.");      throw DataException("Error - slice size does not match Data rank.");
1094    }    }
1095    setSlice(value,slice_region);    if (getFunctionSpace()!=value.getFunctionSpace()) {
1096         setSlice(Data(value,getFunctionSpace()),slice_region);
1097      } else {
1098         setSlice(value,slice_region);
1099      }
1100  }  }
1101    
1102  void  void
# Line 1021  Data::setTaggedValue(int tagKey, Line 1158  Data::setTaggedValue(int tagKey,
1158    m_data->setTaggedValue(tagKey,valueDataArray.getView());    m_data->setTaggedValue(tagKey,valueDataArray.getView());
1159  }  }
1160    
1161    void
1162    Data::setRefValue(int ref,
1163                      const boost::python::numeric::array& value)
1164    {
1165      //
1166      // Construct DataArray from boost::python::object input value
1167      DataArray valueDataArray(value);
1168    
1169      //
1170      // Call DataAbstract::setRefValue
1171      m_data->setRefValue(ref,valueDataArray);
1172    }
1173    
1174    void
1175    Data::getRefValue(int ref,
1176                      boost::python::numeric::array& value)
1177    {
1178      //
1179      // Construct DataArray for boost::python::object return value
1180      DataArray valueDataArray(value);
1181    
1182      //
1183      // Load DataArray with values from data-points specified by ref
1184      m_data->getRefValue(ref,valueDataArray);
1185    
1186      //
1187      // Load values from valueDataArray into return numarray
1188    
1189      // extract the shape of the numarray
1190      int rank = value.getrank();
1191      DataArrayView::ShapeType shape;
1192      for (int i=0; i < rank; i++) {
1193        shape.push_back(extract<int>(value.getshape()[i]));
1194      }
1195    
1196      // and load the numarray with the data from the DataArray
1197      DataArrayView valueView = valueDataArray.getView();
1198    
1199      if (rank==0) {
1200        throw DataException("Data::getRefValue error: only rank 1 data handled for now.");
1201      }
1202      if (rank==1) {
1203        for (int i=0; i < shape[0]; i++) {
1204          value[i] = valueView(i);
1205        }
1206      }
1207      if (rank==2) {
1208        throw DataException("Data::getRefValue error: only rank 1 data handled for now.");
1209      }
1210      if (rank==3) {
1211        throw DataException("Data::getRefValue error: only rank 1 data handled for now.");
1212      }
1213      if (rank==4) {
1214        throw DataException("Data::getRefValue error: only rank 1 data handled for now.");
1215      }
1216    
1217    }
1218    
1219  /*  /*
1220  Note: this version removed for now. Not needed, and breaks escript.cpp  Note: this version removed for now. Not needed, and breaks escript.cpp
1221  void  void

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

  ViewVC Help
Powered by ViewVC 1.1.26