/[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 1953 by phornby, Thu Oct 30 07:01:11 2008 UTC revision 2049 by phornby, Mon Nov 17 08:54:33 2008 UTC
# Line 18  Line 18 
18  #include "DataConstant.h"  #include "DataConstant.h"
19  #include "DataTagged.h"  #include "DataTagged.h"
20  #include "DataEmpty.h"  #include "DataEmpty.h"
21    #include "DataLazy.h"
22  #include "FunctionSpaceFactory.h"  #include "FunctionSpaceFactory.h"
23  #include "AbstractContinuousDomain.h"  #include "AbstractContinuousDomain.h"
24  #include "UnaryFuncs.h"  #include "UnaryFuncs.h"
# Line 42  using namespace boost::python; Line 43  using namespace boost::python;
43  using namespace boost;  using namespace boost;
44  using namespace escript;  using namespace escript;
45    
46    // ensure the current object is not a DataLazy
47    // The idea was that we could add an optional warning whenever a resolve is forced
48    #define FORCERESOLVE if (isLazy()) {resolve();}
49    
50  Data::Data()  Data::Data()
51  {  {
52    //    //
# Line 93  Data::Data(const Data& inData) Line 98  Data::Data(const Data& inData)
98  Data::Data(const Data& inData,  Data::Data(const Data& inData,
99             const DataTypes::RegionType& region)             const DataTypes::RegionType& region)
100  {  {
101      DataAbstract_ptr dat=inData.m_data;
102      if (inData.isLazy())
103      {
104        dat=inData.m_data->resolve();
105      }
106      else
107      {
108        dat=inData.m_data;
109      }
110    //    //
111    // Create Data which is a slice of another Data    // Create Data which is a slice of another Data
112    DataAbstract* tmp = inData.m_data->getSlice(region);    DataAbstract* tmp = dat->getSlice(region);
113    m_data=DataAbstract_ptr(tmp);    m_data=DataAbstract_ptr(tmp);
114    m_protected=false;    m_protected=false;
115  }  }
# Line 109  Data::Data(const Data& inData, Line 123  Data::Data(const Data& inData,
123    }    }
124    if (inData.getFunctionSpace()==functionspace) {    if (inData.getFunctionSpace()==functionspace) {
125      m_data=inData.m_data;      m_data=inData.m_data;
126    } else if (inData.isConstant()) { // for a constant function, we just need to use the new function space    }
127      if (!inData.probeInterpolation(functionspace))    else
128      {           // Even though this is constant, we still need to check whether interpolation is allowed    {
129    
130        if (inData.isConstant()) {  // for a constant function, we just need to use the new function space
131          if (!inData.probeInterpolation(functionspace))
132          {           // Even though this is constant, we still need to check whether interpolation is allowed
133      throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");      throw FunctionSpaceException("Call to probeInterpolation returned false for DataConstant.");
134      }        }
135      DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),inData.m_data->getVector());          // if the data is not lazy, this will just be a cast to DataReady
136      m_data=DataAbstract_ptr(dc);        DataReady_ptr dr=inData.m_data->resolve();
137    } else {        DataConstant* dc=new DataConstant(functionspace,inData.m_data->getShape(),dr->getVector());  
138      Data tmp(0,inData.getDataPointShape(),functionspace,true);        m_data=DataAbstract_ptr(dc);
     // Note: Must use a reference or pointer to a derived object  
     // in order to get polymorphic behaviour. Shouldn't really  
     // be able to create an instance of AbstractDomain but that was done  
     // as a boost:python work around which may no longer be required.  
     /*const AbstractDomain& inDataDomain=inData.getDomain();*/  
     const_Domain_ptr inDataDomain=inData.getDomain();  
     if  (inDataDomain==functionspace.getDomain()) {  
       inDataDomain->interpolateOnDomain(tmp,inData);  
139      } else {      } else {
140        inDataDomain->interpolateACross(tmp,inData);        Data tmp(0,inData.getDataPointShape(),functionspace,true);
141          // Note: Must use a reference or pointer to a derived object
142          // in order to get polymorphic behaviour. Shouldn't really
143          // be able to create an instance of AbstractDomain but that was done
144          // as a boost:python work around which may no longer be required.
145          /*const AbstractDomain& inDataDomain=inData.getDomain();*/
146          const_Domain_ptr inDataDomain=inData.getDomain();
147          if  (inDataDomain==functionspace.getDomain()) {
148            inDataDomain->interpolateOnDomain(tmp,inData);
149          } else {
150            inDataDomain->interpolateACross(tmp,inData);
151          }
152          m_data=tmp.m_data;
153      }      }
     m_data=tmp.m_data;  
154    }    }
155    m_protected=false;    m_protected=false;
156  }  }
# Line 141  Data::Data(DataAbstract* underlyingdata) Line 162  Data::Data(DataAbstract* underlyingdata)
162      m_protected=false;      m_protected=false;
163  }  }
164    
165    Data::Data(DataAbstract_ptr underlyingdata)
166    {
167        m_data=underlyingdata;
168        m_protected=false;
169    }
170    
171    
172  Data::Data(const numeric::array& value,  Data::Data(const numeric::array& value,
173         const FunctionSpace& what,         const FunctionSpace& what,
174             bool expanded)             bool expanded)
# Line 423  Data::copy(const Data& other) Line 451  Data::copy(const Data& other)
451  }  }
452    
453    
454    Data
455    Data::delay()
456    {
457      DataLazy* dl=new DataLazy(m_data);
458      return Data(dl);
459    }
460    
461    void
462    Data::delaySelf()
463    {
464      if (!isLazy())
465      {
466        m_data=(new DataLazy(m_data))->getPtr();
467      }
468    }
469    
470  void  void
471  Data::setToZero()  Data::setToZero()
472  {  {
# Line 430  Data::setToZero() Line 474  Data::setToZero()
474    {    {
475       throw DataException("Error - Operations not permitted on instances of DataEmpty.");       throw DataException("Error - Operations not permitted on instances of DataEmpty.");
476    }    }
477    {    m_data->setToZero();
     DataExpanded* temp=dynamic_cast<DataExpanded*>(m_data.get());  
     if (temp!=0) {  
        temp->setToZero();  
        return;  
     }  
   }  
   {  
     DataTagged* temp=dynamic_cast<DataTagged*>(m_data.get());  
     if (temp!=0) {  
       temp->setToZero();  
       return;  
     }  
   }  
   {  
     DataConstant* temp=dynamic_cast<DataConstant*>(m_data.get());  
     if (temp!=0) {  
       temp->setToZero();  
       return;  
     }  
   }  
   throw DataException("Error - Data can not be set to zero.");  
478  }  }
479    
 // void  
 // Data::copyWithMask(const Data& other,  
 //                    const Data& mask)  
 // {  
 //   if (other.isEmpty() || mask.isEmpty())  
 //   {  
 //  throw DataException("Error - copyWithMask not permitted using instances of DataEmpty.");  
 //   }  
 //   Data mask1;  
 //   Data mask2;  
 //   mask1 = mask.wherePositive();  
 //  
 //   mask2.copy(mask1);  
 //   mask1 *= other;  
 //  
 //   mask2 *= *this;  
 //   mask2 = *this - mask2;  
 //   *this = mask1 + mask2;  
 // }  
   
480  void  void
481  Data::copyWithMask(const Data& other,  Data::copyWithMask(const Data& other,
482                     const Data& mask)                     const Data& mask)
# Line 487  Data::copyWithMask(const Data& other, Line 490  Data::copyWithMask(const Data& other,
490    }    }
491    Data other2(other);    Data other2(other);
492    Data mask2(mask);    Data mask2(mask);
493      other2.resolve();
494      mask2.resolve();
495      this->resolve();
496    FunctionSpace myFS=getFunctionSpace();    FunctionSpace myFS=getFunctionSpace();
497    FunctionSpace oFS=other2.getFunctionSpace();    FunctionSpace oFS=other2.getFunctionSpace();
498    FunctionSpace mFS=mask2.getFunctionSpace();    FunctionSpace mFS=mask2.getFunctionSpace();
# Line 533  Data::copyWithMask(const Data& other, Line 539  Data::copyWithMask(const Data& other,
539      throw DataException("Error - Unknown DataAbstract passed to copyWithMask.");      throw DataException("Error - Unknown DataAbstract passed to copyWithMask.");
540    }    }
541    // Now we iterate over the elements    // Now we iterate over the elements
542    DataVector& self=m_data->getVector();    DataVector& self=getReadyPtr()->getVector();
543    const DataVector& ovec=other2.m_data->getVector();    const DataVector& ovec=other2.getReadyPtr()->getVector();
544    const DataVector& mvec=mask2.m_data->getVector();    const DataVector& mvec=mask2.getReadyPtr()->getVector();
545    if ((self.size()!=ovec.size()) || (self.size()!=mvec.size()))    if ((self.size()!=ovec.size()) || (self.size()!=mvec.size()))
546    {    {
547      throw DataException("Error - size mismatch in arguments to copyWithMask.");      throw DataException("Error - size mismatch in arguments to copyWithMask.");
# Line 588  Data::isConstant() const Line 594  Data::isConstant() const
594    return (temp!=0);    return (temp!=0);
595  }  }
596    
597    bool
598    Data::isLazy() const
599    {
600      return m_data->isLazy();
601    }
602    
603    // at the moment this is synonymous with !isLazy() but that could change
604    bool
605    Data::isReady() const
606    {
607      return (dynamic_cast<DataReady*>(m_data.get())!=0);
608    }
609    
610    
611  void  void
612  Data::setProtection()  Data::setProtection()
613  {  {
# Line 622  Data::expand() Line 642  Data::expand()
642      // do nothing      // do nothing
643    } else if (isEmpty()) {    } else if (isEmpty()) {
644      throw DataException("Error - Expansion of DataEmpty not possible.");      throw DataException("Error - Expansion of DataEmpty not possible.");
645      } else if (isLazy()) {
646        resolve();
647        expand();       // resolve might not give us expanded data
648    } else {    } else {
649      throw DataException("Error - Expansion not implemented for this Data type.");      throw DataException("Error - Expansion not implemented for this Data type.");
650    }    }
# Line 642  Data::tag() Line 665  Data::tag()
665      throw DataException("Error - Creating tag data from DataExpanded not possible.");      throw DataException("Error - Creating tag data from DataExpanded not possible.");
666    } else if (isEmpty()) {    } else if (isEmpty()) {
667      throw DataException("Error - Creating tag data from DataEmpty not possible.");      throw DataException("Error - Creating tag data from DataEmpty not possible.");
668      } else if (isLazy()) {
669         DataAbstract_ptr res=m_data->resolve();
670         if (m_data->isExpanded())
671         {
672        throw DataException("Error - data would resolve to DataExpanded, tagging is not possible.");
673         }
674         m_data=res;    
675         tag();
676    } else {    } else {
677      throw DataException("Error - Tagging not implemented for this Data type.");      throw DataException("Error - Tagging not implemented for this Data type.");
678    }    }
679  }  }
680    
681    void
682    Data::resolve()
683    {
684      if (isLazy())
685      {
686         m_data=m_data->resolve();
687      }
688    }
689    
690    
691  Data  Data
692  Data::oneOver() const  Data::oneOver() const
693  {  {
694      if (isLazy())
695      {
696        DataLazy* c=new DataLazy(borrowDataPtr(),RECIP);
697        return Data(c);
698      }
699    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));    return C_TensorUnaryOperation(*this, bind1st(divides<double>(),1.));
700  }  }
701    
702  Data  Data
703  Data::wherePositive() const  Data::wherePositive() const
704  {  {
705      if (isLazy())
706      {
707        DataLazy* c=new DataLazy(borrowDataPtr(),GZ);
708        return Data(c);
709      }
710    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater<double>(),0.0));
711  }  }
712    
713  Data  Data
714  Data::whereNegative() const  Data::whereNegative() const
715  {  {
716      if (isLazy())
717      {
718        DataLazy* c=new DataLazy(borrowDataPtr(),LZ);
719        return Data(c);
720      }
721    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less<double>(),0.0));
722  }  }
723    
724  Data  Data
725  Data::whereNonNegative() const  Data::whereNonNegative() const
726  {  {
727      if (isLazy())
728      {
729        DataLazy* c=new DataLazy(borrowDataPtr(),GEZ);
730        return Data(c);
731      }
732    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(greater_equal<double>(),0.0));
733  }  }
734    
735  Data  Data
736  Data::whereNonPositive() const  Data::whereNonPositive() const
737  {  {
738      if (isLazy())
739      {
740        DataLazy* c=new DataLazy(borrowDataPtr(),LEZ);
741        return Data(c);
742      }
743    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));    return C_TensorUnaryOperation(*this, bind2nd(less_equal<double>(),0.0));
744  }  }
745    
# Line 700  Data::interpolate(const FunctionSpace& f Line 766  Data::interpolate(const FunctionSpace& f
766  bool  bool
767  Data::probeInterpolation(const FunctionSpace& functionspace) const  Data::probeInterpolation(const FunctionSpace& functionspace) const
768  {  {
769    if (getFunctionSpace()==functionspace) {    return getFunctionSpace().probeInterpolation(functionspace);
770      return true;  //   if (getFunctionSpace()==functionspace) {
771    } else {  //     return true;
772      const_Domain_ptr domain=getDomain();  //   } else {
773      if  (*domain==*functionspace.getDomain()) {  //     const_Domain_ptr domain=getDomain();
774        return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());  //     if  (*domain==*functionspace.getDomain()) {
775      } else {  //       return domain->probeInterpolationOnDomain(getFunctionSpace().getTypeCode(),functionspace.getTypeCode());
776        return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());  //     } else {
777      }  //       return domain->probeInterpolationACross(getFunctionSpace().getTypeCode(),*(functionspace.getDomain()),functionspace.getTypeCode());
778    }  //     }
779    //   }
780  }  }
781    
782  Data  Data
# Line 757  boost::python::numeric::array Line 824  boost::python::numeric::array
824  Data:: getValueOfDataPoint(int dataPointNo)  Data:: getValueOfDataPoint(int dataPointNo)
825  {  {
826    int i, j, k, l;    int i, j, k, l;
827    
828      FORCERESOLVE;
829    
830    //    //
831    // determine the rank and shape of each data point    // determine the rank and shape of each data point
832    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 858  Data::setValueOfDataPointToArray(int dat Line 928  Data::setValueOfDataPointToArray(int dat
928    if (isProtected()) {    if (isProtected()) {
929          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
930    }    }
931      FORCERESOLVE;
932    //    //
933    // check rank    // check rank
934    if (num_array.getrank()<getDataPointRank())    if (static_cast<unsigned int>(num_array.getrank())<getDataPointRank())
935        throw DataException("Rank of numarray does not match Data object rank");        throw DataException("Rank of numarray does not match Data object rank");
936    
937    //    //
# Line 892  Data::setValueOfDataPoint(int dataPointN Line 963  Data::setValueOfDataPoint(int dataPointN
963    }    }
964    //    //
965    // make sure data is expanded:    // make sure data is expanded:
966      FORCERESOLVE;
967    if (!isExpanded()) {    if (!isExpanded()) {
968      expand();      expand();
969    }    }
# Line 910  Data::getValueOfGlobalDataPoint(int proc Line 982  Data::getValueOfGlobalDataPoint(int proc
982  {  {
983    size_t length=0;    size_t length=0;
984    int i, j, k, l, pos;    int i, j, k, l, pos;
985      FORCERESOLVE;
986    //    //
987    // determine the rank and shape of each data point    // determine the rank and shape of each data point
988    int dataPointRank = getDataPointRank();    int dataPointRank = getDataPointRank();
# Line 1044  Data::getValueOfGlobalDataPoint(int proc Line 1117  Data::getValueOfGlobalDataPoint(int proc
1117  }  }
1118    
1119    
1120    boost::python::numeric::array
1121    Data::integrate_const() const
1122    {
1123      if (isLazy())
1124      {
1125        throw DataException("Error - cannot integrate for constant lazy data.");
1126      }
1127      return integrateWorker();
1128    }
1129    
1130    boost::python::numeric::array
1131    Data::integrate()
1132    {
1133      if (isLazy())
1134      {
1135        expand();
1136      }
1137      return integrateWorker();
1138    }
1139    
1140    
1141    
1142  boost::python::numeric::array  boost::python::numeric::array
1143  Data::integrate() const  Data::integrateWorker() const
1144  {  {
1145    int index;    int index;
1146    int rank = getDataPointRank();    int rank = getDataPointRank();
# Line 1129  Data::integrate() const Line 1223  Data::integrate() const
1223  Data  Data
1224  Data::sin() const  Data::sin() const
1225  {  {
1226      if (isLazy())
1227      {
1228        DataLazy* c=new DataLazy(borrowDataPtr(),SIN);
1229        return Data(c);
1230      }
1231    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sin);
1232  }  }
1233    
1234  Data  Data
1235  Data::cos() const  Data::cos() const
1236  {  {
1237      if (isLazy())
1238      {
1239        DataLazy* c=new DataLazy(borrowDataPtr(),COS);
1240        return Data(c);
1241      }
1242    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::cos);
1243  }  }
1244    
1245  Data  Data
1246  Data::tan() const  Data::tan() const
1247  {  {
1248      if (isLazy())
1249      {
1250        DataLazy* c=new DataLazy(borrowDataPtr(),TAN);
1251        return Data(c);
1252      }
1253    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::tan);
1254  }  }
1255    
1256  Data  Data
1257  Data::asin() const  Data::asin() const
1258  {  {
1259      if (isLazy())
1260      {
1261        DataLazy* c=new DataLazy(borrowDataPtr(),ASIN);
1262        return Data(c);
1263      }
1264    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::asin);
1265  }  }
1266    
1267  Data  Data
1268  Data::acos() const  Data::acos() const
1269  {  {
1270      if (isLazy())
1271      {
1272        DataLazy* c=new DataLazy(borrowDataPtr(),ACOS);
1273        return Data(c);
1274      }
1275    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::acos);
1276  }  }
1277    
# Line 1160  Data::acos() const Line 1279  Data::acos() const
1279  Data  Data
1280  Data::atan() const  Data::atan() const
1281  {  {
1282      if (isLazy())
1283      {
1284        DataLazy* c=new DataLazy(borrowDataPtr(),ATAN);
1285        return Data(c);
1286      }
1287    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::atan);
1288  }  }
1289    
1290  Data  Data
1291  Data::sinh() const  Data::sinh() const
1292  {  {
1293      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);    if (isLazy())
1294      {
1295        DataLazy* c=new DataLazy(borrowDataPtr(),SINH);
1296        return Data(c);
1297      }
1298      return C_TensorUnaryOperation<double (*)(double)>(*this, ::sinh);
1299  }  }
1300    
1301  Data  Data
1302  Data::cosh() const  Data::cosh() const
1303  {  {
1304      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);    if (isLazy())
1305      {
1306        DataLazy* c=new DataLazy(borrowDataPtr(),COSH);
1307        return Data(c);
1308      }
1309      return C_TensorUnaryOperation<double (*)(double)>(*this, ::cosh);
1310  }  }
1311    
1312  Data  Data
1313  Data::tanh() const  Data::tanh() const
1314  {  {
1315      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);    if (isLazy())
1316      {
1317        DataLazy* c=new DataLazy(borrowDataPtr(),TANH);
1318        return Data(c);
1319      }
1320      return C_TensorUnaryOperation<double (*)(double)>(*this, ::tanh);
1321  }  }
1322    
1323    
1324  Data  Data
1325  Data::erf() const  Data::erf() const
1326  {  {
1327  #ifdef _WIN32  #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1328    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");    throw DataException("Error - Data:: erf function is not supported on _WIN32 platforms.");
1329  #else  #else
1330      if (isLazy())
1331      {
1332        DataLazy* c=new DataLazy(borrowDataPtr(),ERF);
1333        return Data(c);
1334      }
1335    return C_TensorUnaryOperation(*this, ::erf);    return C_TensorUnaryOperation(*this, ::erf);
1336  #endif  #endif
1337  }  }
# Line 1196  Data::erf() const Line 1339  Data::erf() const
1339  Data  Data
1340  Data::asinh() const  Data::asinh() const
1341  {  {
1342  #ifdef _WIN32    if (isLazy())
1343      {
1344        DataLazy* c=new DataLazy(borrowDataPtr(),ASINH);
1345        return Data(c);
1346      }
1347    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1348    return C_TensorUnaryOperation(*this, escript::asinh_substitute);    return C_TensorUnaryOperation(*this, escript::asinh_substitute);
1349  #else  #else
1350    return C_TensorUnaryOperation(*this, ::asinh);    return C_TensorUnaryOperation(*this, ::asinh);
# Line 1206  Data::asinh() const Line 1354  Data::asinh() const
1354  Data  Data
1355  Data::acosh() const  Data::acosh() const
1356  {  {
1357  #ifdef _WIN32    if (isLazy())
1358      {
1359        DataLazy* c=new DataLazy(borrowDataPtr(),ACOSH);
1360        return Data(c);
1361      }
1362    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1363    return C_TensorUnaryOperation(*this, escript::acosh_substitute);    return C_TensorUnaryOperation(*this, escript::acosh_substitute);
1364  #else  #else
1365    return C_TensorUnaryOperation(*this, ::acosh);    return C_TensorUnaryOperation(*this, ::acosh);
# Line 1216  Data::acosh() const Line 1369  Data::acosh() const
1369  Data  Data
1370  Data::atanh() const  Data::atanh() const
1371  {  {
1372  #ifdef _WIN32    if (isLazy())
1373      {
1374        DataLazy* c=new DataLazy(borrowDataPtr(),ATANH);
1375        return Data(c);
1376      }
1377    #if defined (_WIN32) && !defined(__INTEL_COMPILER)
1378    return C_TensorUnaryOperation(*this, escript::atanh_substitute);    return C_TensorUnaryOperation(*this, escript::atanh_substitute);
1379  #else  #else
1380    return C_TensorUnaryOperation(*this, ::atanh);    return C_TensorUnaryOperation(*this, ::atanh);
# Line 1225  Data::atanh() const Line 1383  Data::atanh() const
1383    
1384  Data  Data
1385  Data::log10() const  Data::log10() const
1386  {  {  if (isLazy())
1387      {
1388        DataLazy* c=new DataLazy(borrowDataPtr(),LOG10);
1389        return Data(c);
1390      }
1391    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log10);
1392  }  }
1393    
1394  Data  Data
1395  Data::log() const  Data::log() const
1396  {  {
1397      if (isLazy())
1398      {
1399        DataLazy* c=new DataLazy(borrowDataPtr(),LOG);
1400        return Data(c);
1401      }
1402    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::log);
1403  }  }
1404    
1405  Data  Data
1406  Data::sign() const  Data::sign() const
1407  {  {
1408      if (isLazy())
1409      {
1410        DataLazy* c=new DataLazy(borrowDataPtr(),SIGN);
1411        return Data(c);
1412      }
1413    return C_TensorUnaryOperation(*this, escript::fsign);    return C_TensorUnaryOperation(*this, escript::fsign);
1414  }  }
1415    
1416  Data  Data
1417  Data::abs() const  Data::abs() const
1418  {  {
1419      if (isLazy())
1420      {
1421        DataLazy* c=new DataLazy(borrowDataPtr(),ABS);
1422        return Data(c);
1423      }
1424    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::fabs);
1425  }  }
1426    
1427  Data  Data
1428  Data::neg() const  Data::neg() const
1429  {  {
1430      if (isLazy())
1431      {
1432        DataLazy* c=new DataLazy(borrowDataPtr(),NEG);
1433        return Data(c);
1434      }
1435    return C_TensorUnaryOperation(*this, negate<double>());    return C_TensorUnaryOperation(*this, negate<double>());
1436  }  }
1437    
1438  Data  Data
1439  Data::pos() const  Data::pos() const
1440  {  {
1441        // not doing lazy check here is deliberate.
1442        // since a deep copy of lazy data should be cheap, I'll just let it happen now
1443    Data result;    Data result;
1444    // perform a deep copy    // perform a deep copy
1445    result.copy(*this);    result.copy(*this);
# Line 1264  Data::pos() const Line 1448  Data::pos() const
1448    
1449  Data  Data
1450  Data::exp() const  Data::exp() const
1451  {  {  
1452      if (isLazy())
1453      {
1454        DataLazy* c=new DataLazy(borrowDataPtr(),EXP);
1455        return Data(c);
1456      }
1457    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::exp);
1458  }  }
1459    
1460  Data  Data
1461  Data::sqrt() const  Data::sqrt() const
1462  {  {
1463      if (isLazy())
1464      {
1465        DataLazy* c=new DataLazy(borrowDataPtr(),SQRT);
1466        return Data(c);
1467      }
1468    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);    return C_TensorUnaryOperation<double (*)(double)>(*this, ::sqrt);
1469  }  }
1470    
1471  double  double
1472  Data::Lsup() const  Data::Lsup_const() const
1473    {
1474       if (isLazy())
1475       {
1476        throw DataException("Error - cannot compute Lsup for constant lazy data.");
1477       }
1478       return LsupWorker();
1479    }
1480    
1481    double
1482    Data::Lsup()
1483    {
1484       if (isLazy())
1485       {
1486        expand();
1487       }
1488       return LsupWorker();
1489    }
1490    
1491    double
1492    Data::sup_const() const
1493    {
1494       if (isLazy())
1495       {
1496        throw DataException("Error - cannot compute sup for constant lazy data.");
1497       }
1498       return supWorker();
1499    }
1500    
1501    double
1502    Data::sup()
1503    {
1504       if (isLazy())
1505       {
1506        expand();
1507       }
1508       return supWorker();
1509    }
1510    
1511    double
1512    Data::inf_const() const
1513    {
1514       if (isLazy())
1515       {
1516        throw DataException("Error - cannot compute inf for constant lazy data.");
1517       }
1518       return infWorker();
1519    }
1520    
1521    double
1522    Data::inf()
1523    {
1524       if (isLazy())
1525       {
1526        expand();
1527       }
1528       return infWorker();
1529    }
1530    
1531    double
1532    Data::LsupWorker() const
1533  {  {
1534    double localValue;    double localValue;
1535    //    //
# Line 1293  Data::Lsup() const Line 1547  Data::Lsup() const
1547  }  }
1548    
1549  double  double
1550  Data::sup() const  Data::supWorker() const
1551  {  {
1552    double localValue;    double localValue;
1553    //    //
# Line 1310  Data::sup() const Line 1564  Data::sup() const
1564  }  }
1565    
1566  double  double
1567  Data::inf() const  Data::infWorker() const
1568  {  {
1569    double localValue;    double localValue;
1570    //    //
# Line 1331  Data::inf() const Line 1585  Data::inf() const
1585  Data  Data
1586  Data::maxval() const  Data::maxval() const
1587  {  {
1588      if (isLazy())
1589      {
1590        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1591        temp.resolve();
1592        return temp.maxval();
1593      }
1594    //    //
1595    // set the initial maximum value to min possible double    // set the initial maximum value to min possible double
1596    FMax fmax_func;    FMax fmax_func;
# Line 1340  Data::maxval() const Line 1600  Data::maxval() const
1600  Data  Data
1601  Data::minval() const  Data::minval() const
1602  {  {
1603      if (isLazy())
1604      {
1605        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1606        temp.resolve();
1607        return temp.minval();
1608      }
1609    //    //
1610    // set the initial minimum value to max possible double    // set the initial minimum value to max possible double
1611    FMin fmin_func;    FMin fmin_func;
# Line 1406  Data::symmetric() const Line 1672  Data::symmetric() const
1672       else {       else {
1673          throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");          throw DataException("Error - Data::symmetric can only be calculated for rank 2 or 4 object.");
1674       }       }
1675         if (isLazy())
1676         {
1677        DataLazy* c=new DataLazy(borrowDataPtr(),SYM);
1678        return Data(c);
1679         }
1680       Data ev(0.,getDataPointShape(),getFunctionSpace());       Data ev(0.,getDataPointShape(),getFunctionSpace());
1681       ev.typeMatchRight(*this);       ev.typeMatchRight(*this);
1682       m_data->symmetric(ev.m_data.get());       m_data->symmetric(ev.m_data.get());
# Line 1415  Data::symmetric() const Line 1686  Data::symmetric() const
1686  Data  Data
1687  Data::nonsymmetric() const  Data::nonsymmetric() const
1688  {  {
1689         if (isLazy())
1690         {
1691        DataLazy* c=new DataLazy(borrowDataPtr(),NSYM);
1692        return Data(c);
1693         }
1694       // check input       // check input
1695       DataTypes::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1696       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
# Line 1446  Data::nonsymmetric() const Line 1722  Data::nonsymmetric() const
1722       }       }
1723  }  }
1724    
1725    
1726    // Doing a lazy version of this would require some thought.
1727    // First it needs a parameter (which DataLazy doesn't support at the moment).
1728    // (secondly although it does not apply to trace) we can't handle operations which return
1729    // multiple results (like eigenvectors_values) or return values of different shapes to their input
1730    // (like eigenvalues).
1731  Data  Data
1732  Data::trace(int axis_offset) const  Data::trace(int axis_offset) const
1733  {  {
1734         if (isLazy())
1735         {
1736        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1737        temp.resolve();
1738        return temp.trace(axis_offset);
1739         }
1740       DataTypes::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1741       if (getDataPointRank()==2) {       if (getDataPointRank()==2) {
1742          DataTypes::ShapeType ev_shape;          DataTypes::ShapeType ev_shape;
# Line 1498  Data::trace(int axis_offset) const Line 1786  Data::trace(int axis_offset) const
1786    
1787  Data  Data
1788  Data::transpose(int axis_offset) const  Data::transpose(int axis_offset) const
1789  {  {    
1790         if (isLazy())
1791         {
1792        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1793        temp.resolve();
1794        return temp.transpose(axis_offset);
1795         }
1796       DataTypes::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1797       DataTypes::ShapeType ev_shape;       DataTypes::ShapeType ev_shape;
1798       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]       // Here's the equivalent of python s_out=s[axis_offset:]+s[:axis_offset]
# Line 1520  Data::transpose(int axis_offset) const Line 1814  Data::transpose(int axis_offset) const
1814  Data  Data
1815  Data::eigenvalues() const  Data::eigenvalues() const
1816  {  {
1817         if (isLazy())
1818         {
1819        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1820        temp.resolve();
1821        return temp.eigenvalues();
1822         }
1823       // check input       // check input
1824       DataTypes::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1825       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
# Line 1537  Data::eigenvalues() const Line 1837  Data::eigenvalues() const
1837  const boost::python::tuple  const boost::python::tuple
1838  Data::eigenvalues_and_eigenvectors(const double tol) const  Data::eigenvalues_and_eigenvectors(const double tol) const
1839  {  {
1840         if (isLazy())
1841         {
1842        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1843        temp.resolve();
1844        return temp.eigenvalues_and_eigenvectors(tol);
1845         }
1846       DataTypes::ShapeType s=getDataPointShape();       DataTypes::ShapeType s=getDataPointShape();
1847       if (getDataPointRank()!=2)       if (getDataPointRank()!=2)
1848          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");          throw DataException("Error - Data::eigenvalues and eigenvectors can only be calculated for rank 2 object.");
# Line 1570  void Line 1876  void
1876  Data::calc_minGlobalDataPoint(int& ProcNo,  Data::calc_minGlobalDataPoint(int& ProcNo,
1877                          int& DataPointNo) const                          int& DataPointNo) const
1878  {  {
1879      if (isLazy())
1880      {
1881        Data temp(*this);   // to get around the fact that you can't resolve a const Data
1882        temp.resolve();
1883        return temp.calc_minGlobalDataPoint(ProcNo,DataPointNo);
1884      }
1885    int i,j;    int i,j;
1886    int lowi=0,lowj=0;    int lowi=0,lowj=0;
1887    double min=numeric_limits<double>::max();    double min=numeric_limits<double>::max();
# Line 1636  Data::saveDX(std::string fileName) const Line 1948  Data::saveDX(std::string fileName) const
1948    {    {
1949      throw DataException("Error - Operations not permitted on instances of DataEmpty.");      throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1950    }    }
1951      if (isLazy())
1952      {
1953         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1954         temp.resolve();
1955         temp.saveDX(fileName);
1956         return;
1957      }
1958    boost::python::dict args;    boost::python::dict args;
1959    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1960    getDomain()->saveDX(fileName,args);    getDomain()->saveDX(fileName,args);
# Line 1649  Data::saveVTK(std::string fileName) cons Line 1968  Data::saveVTK(std::string fileName) cons
1968    {    {
1969      throw DataException("Error - Operations not permitted on instances of DataEmpty.");      throw DataException("Error - Operations not permitted on instances of DataEmpty.");
1970    }    }
1971      if (isLazy())
1972      {
1973         Data temp(*this);  // to get around the fact that you can't resolve a const Data
1974         temp.resolve();
1975         temp.saveVTK(fileName);
1976         return;
1977      }
1978    boost::python::dict args;    boost::python::dict args;
1979    args["data"]=boost::python::object(this);    args["data"]=boost::python::object(this);
1980    getDomain()->saveVTK(fileName,args);    getDomain()->saveVTK(fileName,args);
# Line 1661  Data::operator+=(const Data& right) Line 1987  Data::operator+=(const Data& right)
1987    if (isProtected()) {    if (isProtected()) {
1988          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
1989    }    }
1990    binaryOp(right,plus<double>());    if (isLazy() || right.isLazy())
1991    return (*this);    {
1992        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),ADD); // for lazy + is equivalent to +=
1993            m_data=c->getPtr();
1994        return (*this);
1995      }
1996      else
1997      {
1998        binaryOp(right,plus<double>());
1999        return (*this);
2000      }
2001  }  }
2002    
2003  Data&  Data&
2004  Data::operator+=(const boost::python::object& right)  Data::operator+=(const boost::python::object& right)
2005  {  {
2006      if (isProtected()) {
2007            throw DataException("Error - attempt to update protected Data object.");
2008      }
2009    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2010    binaryOp(tmp,plus<double>());    if (isLazy())
2011    return (*this);    {
2012        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),ADD);   // for lazy + is equivalent to +=
2013            m_data=c->getPtr();
2014        return (*this);
2015      }
2016      else
2017      {
2018        binaryOp(tmp,plus<double>());
2019        return (*this);
2020      }
2021  }  }
2022    
2023    // Hmmm, operator= makes a deep copy but the copy constructor does not?
2024  Data&  Data&
2025  Data::operator=(const Data& other)  Data::operator=(const Data& other)
2026  {  {
# Line 1685  Data::operator-=(const Data& right) Line 2034  Data::operator-=(const Data& right)
2034    if (isProtected()) {    if (isProtected()) {
2035          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2036    }    }
2037    binaryOp(right,minus<double>());    if (isLazy() || right.isLazy())
2038    return (*this);    {
2039        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),SUB); // for lazy - is equivalent to -=
2040            m_data=c->getPtr();
2041        return (*this);
2042      }
2043      else
2044      {
2045        binaryOp(right,minus<double>());
2046        return (*this);
2047      }
2048  }  }
2049    
2050  Data&  Data&
2051  Data::operator-=(const boost::python::object& right)  Data::operator-=(const boost::python::object& right)
2052  {  {
2053      if (isProtected()) {
2054            throw DataException("Error - attempt to update protected Data object.");
2055      }
2056    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2057    binaryOp(tmp,minus<double>());    if (isLazy())
2058    return (*this);    {
2059        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),SUB);   // for lazy - is equivalent to -=
2060            m_data=c->getPtr();
2061        return (*this);
2062      }
2063      else
2064      {
2065        binaryOp(tmp,minus<double>());
2066        return (*this);
2067      }
2068  }  }
2069    
2070  Data&  Data&
# Line 1703  Data::operator*=(const Data& right) Line 2073  Data::operator*=(const Data& right)
2073    if (isProtected()) {    if (isProtected()) {
2074          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2075    }    }
2076    binaryOp(right,multiplies<double>());    if (isLazy() || right.isLazy())
2077    return (*this);    {
2078        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),MUL); // for lazy * is equivalent to *=
2079            m_data=c->getPtr();
2080        return (*this);
2081      }
2082      else
2083      {
2084        binaryOp(right,multiplies<double>());
2085        return (*this);
2086      }
2087  }  }
2088    
2089  Data&  Data&
2090  Data::operator*=(const boost::python::object& right)  Data::operator*=(const boost::python::object& right)
2091  {  {  
2092      if (isProtected()) {
2093            throw DataException("Error - attempt to update protected Data object.");
2094      }
2095    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2096    binaryOp(tmp,multiplies<double>());    if (isLazy())
2097    return (*this);    {
2098        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),MUL);   // for lazy * is equivalent to *=
2099            m_data=c->getPtr();
2100        return (*this);
2101      }
2102      else
2103      {
2104        binaryOp(tmp,multiplies<double>());
2105        return (*this);
2106      }
2107  }  }
2108    
2109  Data&  Data&
# Line 1721  Data::operator/=(const Data& right) Line 2112  Data::operator/=(const Data& right)
2112    if (isProtected()) {    if (isProtected()) {
2113          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2114    }    }
2115    binaryOp(right,divides<double>());    if (isLazy() || right.isLazy())
2116    return (*this);    {
2117        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),DIV); // for lazy / is equivalent to /=
2118            m_data=c->getPtr();
2119        return (*this);
2120      }
2121      else
2122      {
2123        binaryOp(right,divides<double>());
2124        return (*this);
2125      }
2126  }  }
2127    
2128  Data&  Data&
2129  Data::operator/=(const boost::python::object& right)  Data::operator/=(const boost::python::object& right)
2130  {  {
2131      if (isProtected()) {
2132            throw DataException("Error - attempt to update protected Data object.");
2133      }
2134    Data tmp(right,getFunctionSpace(),false);    Data tmp(right,getFunctionSpace(),false);
2135    binaryOp(tmp,divides<double>());    if (isLazy())
2136    return (*this);    {
2137        DataLazy* c=new DataLazy(m_data,tmp.borrowDataPtr(),DIV);   // for lazy / is equivalent to /=
2138            m_data=c->getPtr();
2139        return (*this);
2140      }
2141      else
2142      {
2143        binaryOp(tmp,divides<double>());
2144        return (*this);
2145      }
2146  }  }
2147    
2148  Data  Data
# Line 1750  Data::powO(const boost::python::object& Line 2162  Data::powO(const boost::python::object&
2162  Data  Data
2163  Data::powD(const Data& right) const  Data::powD(const Data& right) const
2164  {  {
2165      if (isLazy() || right.isLazy())
2166      {
2167        DataLazy* c=new DataLazy(m_data,right.borrowDataPtr(),POW);
2168        return Data(c);
2169      }
2170    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);    return C_TensorBinaryOperation<double (*)(double, double)>(*this, right, ::pow);
2171  }  }
2172    
# Line 1758  Data::powD(const Data& right) const Line 2175  Data::powD(const Data& right) const
2175  Data  Data
2176  escript::operator+(const Data& left, const Data& right)  escript::operator+(const Data& left, const Data& right)
2177  {  {
2178      if (left.isLazy() || right.isLazy())
2179      {
2180        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),ADD);
2181        return Data(c);
2182      }
2183    return C_TensorBinaryOperation(left, right, plus<double>());    return C_TensorBinaryOperation(left, right, plus<double>());
2184  }  }
2185    
# Line 1766  escript::operator+(const Data& left, con Line 2188  escript::operator+(const Data& left, con
2188  Data  Data
2189  escript::operator-(const Data& left, const Data& right)  escript::operator-(const Data& left, const Data& right)
2190  {  {
2191      if (left.isLazy() || right.isLazy())
2192      {
2193        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),SUB);
2194        return Data(c);
2195      }
2196    return C_TensorBinaryOperation(left, right, minus<double>());    return C_TensorBinaryOperation(left, right, minus<double>());
2197  }  }
2198    
# Line 1774  escript::operator-(const Data& left, con Line 2201  escript::operator-(const Data& left, con
2201  Data  Data
2202  escript::operator*(const Data& left, const Data& right)  escript::operator*(const Data& left, const Data& right)
2203  {  {
2204      if (left.isLazy() || right.isLazy())
2205      {
2206        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),MUL);
2207        return Data(c);
2208      }
2209    return C_TensorBinaryOperation(left, right, multiplies<double>());    return C_TensorBinaryOperation(left, right, multiplies<double>());
2210  }  }
2211    
# Line 1782  escript::operator*(const Data& left, con Line 2214  escript::operator*(const Data& left, con
2214  Data  Data
2215  escript::operator/(const Data& left, const Data& right)  escript::operator/(const Data& left, const Data& right)
2216  {  {
2217      if (left.isLazy() || right.isLazy())
2218      {
2219        DataLazy* c=new DataLazy(left.borrowDataPtr(),right.borrowDataPtr(),DIV);
2220        return Data(c);
2221      }
2222    return C_TensorBinaryOperation(left, right, divides<double>());    return C_TensorBinaryOperation(left, right, divides<double>());
2223  }  }
2224    
# Line 1790  escript::operator/(const Data& left, con Line 2227  escript::operator/(const Data& left, con
2227  Data  Data
2228  escript::operator+(const Data& left, const boost::python::object& right)  escript::operator+(const Data& left, const boost::python::object& right)
2229  {  {
2230      if (left.isLazy())
2231      {
2232        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),ADD);
2233        return Data(c);
2234      }
2235    return left+Data(right,left.getFunctionSpace(),false);    return left+Data(right,left.getFunctionSpace(),false);
2236  }  }
2237    
# Line 1798  escript::operator+(const Data& left, con Line 2240  escript::operator+(const Data& left, con
2240  Data  Data
2241  escript::operator-(const Data& left, const boost::python::object& right)  escript::operator-(const Data& left, const boost::python::object& right)
2242  {  {
2243      if (left.isLazy())
2244      {
2245        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),SUB);
2246        return Data(c);
2247      }
2248    return left-Data(right,left.getFunctionSpace(),false);    return left-Data(right,left.getFunctionSpace(),false);
2249  }  }
2250    
# Line 1806  escript::operator-(const Data& left, con Line 2253  escript::operator-(const Data& left, con
2253  Data  Data
2254  escript::operator*(const Data& left, const boost::python::object& right)  escript::operator*(const Data& left, const boost::python::object& right)
2255  {  {
2256      if (left.isLazy())
2257      {
2258        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),MUL);
2259        return Data(c);
2260      }
2261    return left*Data(right,left.getFunctionSpace(),false);    return left*Data(right,left.getFunctionSpace(),false);
2262  }  }
2263    
# Line 1814  escript::operator*(const Data& left, con Line 2266  escript::operator*(const Data& left, con
2266  Data  Data
2267  escript::operator/(const Data& left, const boost::python::object& right)  escript::operator/(const Data& left, const boost::python::object& right)
2268  {  {
2269      if (left.isLazy())
2270      {
2271        DataLazy* c=new DataLazy(left.borrowDataPtr(),Data(right,left.getFunctionSpace(),false).borrowDataPtr(),DIV);
2272        return Data(c);
2273      }
2274    return left/Data(right,left.getFunctionSpace(),false);    return left/Data(right,left.getFunctionSpace(),false);
2275  }  }
2276    
# Line 1822  escript::operator/(const Data& left, con Line 2279  escript::operator/(const Data& left, con
2279  Data  Data
2280  escript::operator+(const boost::python::object& left, const Data& right)  escript::operator+(const boost::python::object& left, const Data& right)
2281  {  {
2282      if (right.isLazy())
2283      {
2284        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),ADD);
2285        return Data(c);
2286      }
2287    return Data(left,right.getFunctionSpace(),false)+right;    return Data(left,right.getFunctionSpace(),false)+right;
2288  }  }
2289    
# Line 1830  escript::operator+(const boost::python:: Line 2292  escript::operator+(const boost::python::
2292  Data  Data
2293  escript::operator-(const boost::python::object& left, const Data& right)  escript::operator-(const boost::python::object& left, const Data& right)
2294  {  {
2295      if (right.isLazy())
2296      {
2297        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),SUB);
2298        return Data(c);
2299      }
2300    return Data(left,right.getFunctionSpace(),false)-right;    return Data(left,right.getFunctionSpace(),false)-right;
2301  }  }
2302    
# Line 1838  escript::operator-(const boost::python:: Line 2305  escript::operator-(const boost::python::
2305  Data  Data
2306  escript::operator*(const boost::python::object& left, const Data& right)  escript::operator*(const boost::python::object& left, const Data& right)
2307  {  {
2308      if (right.isLazy())
2309      {
2310        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),MUL);
2311        return Data(c);
2312      }
2313    return Data(left,right.getFunctionSpace(),false)*right;    return Data(left,right.getFunctionSpace(),false)*right;
2314  }  }
2315    
# Line 1846  escript::operator*(const boost::python:: Line 2318  escript::operator*(const boost::python::
2318  Data  Data
2319  escript::operator/(const boost::python::object& left, const Data& right)  escript::operator/(const boost::python::object& left, const Data& right)
2320  {  {
2321      if (right.isLazy())
2322      {
2323        DataLazy* c=new DataLazy(Data(left,right.getFunctionSpace(),false).borrowDataPtr(),right.borrowDataPtr(),DIV);
2324        return Data(c);
2325      }
2326    return Data(left,right.getFunctionSpace(),false)/right;    return Data(left,right.getFunctionSpace(),false)/right;
2327  }  }
2328    
 //  
 //bool escript::operator==(const Data& left, const Data& right)  
 //{  
 //  /*  
 //  NB: this operator does very little at this point, and isn't to  
 //  be relied on. Requires further implementation.  
 //  */  
 //  
 //  bool ret;  
 //  
 //  if (left.isEmpty()) {  
 //    if(!right.isEmpty()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 //  }  
 //  
 //  if (left.isConstant()) {  
 //    if(!right.isConstant()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 // }  
 //  
 //  if (left.isTagged()) {  
 //   if(!right.isTagged()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 //  }  
 //  
 //  if (left.isExpanded()) {  
 //    if(!right.isExpanded()) {  
 //      ret = false;  
 //    } else {  
 //      ret = true;  
 //    }  
 //  }  
 //  
 //  return ret;  
 //}  
2329    
2330  /* TODO */  /* TODO */
2331  /* global reduction */  /* global reduction */
# Line 1951  Data::setSlice(const Data& value, Line 2384  Data::setSlice(const Data& value,
2384    if (isProtected()) {    if (isProtected()) {
2385          throw DataException("Error - attempt to update protected Data object.");          throw DataException("Error - attempt to update protected Data object.");
2386    }    }
2387      FORCERESOLVE;
2388    /*  if (isLazy())
2389      {
2390        throw DataException("Error - setSlice not permitted on lazy data.");
2391      }*/
2392    Data tempValue(value);    Data tempValue(value);
2393    typeMatchLeft(tempValue);    typeMatchLeft(tempValue);
2394    typeMatchRight(tempValue);    typeMatchRight(tempValue);
2395    m_data->setSlice(tempValue.m_data.get(),region);    getReady()->setSlice(tempValue.m_data.get(),region);
2396  }  }
2397    
2398  void  void
2399  Data::typeMatchLeft(Data& right) const  Data::typeMatchLeft(Data& right) const
2400  {  {
2401      if (right.isLazy() && !isLazy())
2402      {
2403        right.resolve();
2404      }
2405    if (isExpanded()){    if (isExpanded()){
2406      right.expand();      right.expand();
2407    } else if (isTagged()) {    } else if (isTagged()) {
# Line 1972  Data::typeMatchLeft(Data& right) const Line 2414  Data::typeMatchLeft(Data& right) const
2414  void  void
2415  Data::typeMatchRight(const Data& right)  Data::typeMatchRight(const Data& right)
2416  {  {
2417      if (isLazy() && !right.isLazy())
2418      {
2419        resolve();
2420      }
2421    if (isTagged()) {    if (isTagged()) {
2422      if (right.isExpanded()) {      if (right.isExpanded()) {
2423        expand();        expand();
# Line 1990  Data::setTaggedValueByName(std::string n Line 2436  Data::setTaggedValueByName(std::string n
2436                             const boost::python::object& value)                             const boost::python::object& value)
2437  {  {
2438       if (getFunctionSpace().getDomain()->isValidTagName(name)) {       if (getFunctionSpace().getDomain()->isValidTagName(name)) {
2439        FORCERESOLVE;
2440          int tagKey=getFunctionSpace().getDomain()->getTag(name);          int tagKey=getFunctionSpace().getDomain()->getTag(name);
2441          setTaggedValue(tagKey,value);          setTaggedValue(tagKey,value);
2442       }       }
# Line 2003  Data::setTaggedValue(int tagKey, Line 2450  Data::setTaggedValue(int tagKey,
2450    }    }
2451    //    //
2452    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2453      FORCERESOLVE;
2454    if (isConstant()) tag();    if (isConstant()) tag();
   
2455    numeric::array asNumArray(value);    numeric::array asNumArray(value);
2456    
   
2457    // extract the shape of the numarray    // extract the shape of the numarray
2458    DataTypes::ShapeType tempShape;    DataTypes::ShapeType tempShape;
2459    for (int i=0; i < asNumArray.getrank(); i++) {    for (int i=0; i < asNumArray.getrank(); i++) {
2460      tempShape.push_back(extract<int>(asNumArray.getshape()[i]));      tempShape.push_back(extract<int>(asNumArray.getshape()[i]));
2461    }    }
2462    
   // get the space for the data vector  
 //   int len = DataTypes::noValues(tempShape);  
 //   DataVector temp_data(len, 0.0, len);  
 //   DataArrayView temp_dataView(temp_data, tempShape);  
 //   temp_dataView.copy(asNumArray);  
   
2463    DataVector temp_data2;    DataVector temp_data2;
2464    temp_data2.copyFromNumArray(asNumArray);    temp_data2.copyFromNumArray(asNumArray);
2465    
2466    //    m_data->setTaggedValue(tagKey,tempShape, temp_data2);
   // Call DataAbstract::setTaggedValue  
   //m_data->setTaggedValue(tagKey,temp_dataView);  
   
     m_data->setTaggedValue(tagKey,tempShape, temp_data2);  
2467  }  }
2468    
2469    
# Line 2042  Data::setTaggedValueFromCPP(int tagKey, Line 2478  Data::setTaggedValueFromCPP(int tagKey,
2478    }    }
2479    //    //
2480    // Ensure underlying data object is of type DataTagged    // Ensure underlying data object is of type DataTagged
2481      FORCERESOLVE;
2482    if (isConstant()) tag();    if (isConstant()) tag();
   
2483    //    //
2484    // Call DataAbstract::setTaggedValue    // Call DataAbstract::setTaggedValue
2485    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);    m_data->setTaggedValue(tagKey,pointshape, value, dataOffset);
# Line 2075  escript::C_GeneralTensorProduct(Data& ar Line 2511  escript::C_GeneralTensorProduct(Data& ar
2511    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)    // General tensor product: res(SL x SR) = arg_0(SL x SM) * arg_1(SM x SR)
2512    // SM is the product of the last axis_offset entries in arg_0.getShape().    // SM is the product of the last axis_offset entries in arg_0.getShape().
2513    
2514      // deal with any lazy data
2515      if (arg_0.isLazy()) {arg_0.resolve();}
2516      if (arg_1.isLazy()) {arg_1.resolve();}
2517    
2518    // Interpolate if necessary and find an appropriate function space    // Interpolate if necessary and find an appropriate function space
2519    Data arg_0_Z, arg_1_Z;    Data arg_0_Z, arg_1_Z;
2520    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {    if (arg_0.getFunctionSpace()!=arg_1.getFunctionSpace()) {
# Line 2464  Data::borrowData() const Line 2904  Data::borrowData() const
2904    return m_data.get();    return m_data.get();
2905  }  }
2906    
2907    // Not all that happy about returning a non-const from a const
2908    DataAbstract_ptr
2909    Data::borrowDataPtr() const
2910    {
2911      return m_data;
2912    }
2913    
2914    // Not all that happy about returning a non-const from a const
2915    DataReady_ptr
2916    Data::borrowReadyPtr() const
2917    {
2918       DataReady_ptr dr=dynamic_pointer_cast<DataReady>(m_data);
2919       EsysAssert((dr!=0), "Error - casting to DataReady.");
2920       return dr;
2921    }
2922    
2923  std::string  std::string
2924  Data::toString() const  Data::toString() const
# Line 2472  Data::toString() const Line 2927  Data::toString() const
2927      getNumDataPoints()*getDataPointSize()>escriptParams.getInt("TOO_MANY_LINES"))      getNumDataPoints()*getDataPointSize()>escriptParams.getInt("TOO_MANY_LINES"))
2928      {      {
2929      stringstream temp;      stringstream temp;
2930      temp << "Summary: inf="<< inf() << " sup=" << sup() << " data points=" << getNumDataPoints();      temp << "Summary: inf="<< inf_const() << " sup=" << sup_const() << " data points=" << getNumDataPoints();
2931      return  temp.str();      return  temp.str();
2932      }      }
2933      return m_data->toString();      return m_data->toString();
# Line 2483  Data::toString() const Line 2938  Data::toString() const
2938  DataTypes::ValueType::const_reference  DataTypes::ValueType::const_reference
2939  Data::getDataAtOffset(DataTypes::ValueType::size_type i) const  Data::getDataAtOffset(DataTypes::ValueType::size_type i) const
2940  {  {
2941      return m_data->getDataAtOffset(i);      if (isLazy())
2942        {
2943        throw DataException("Programmer error - getDataAtOffset not permitted on lazy data (object is const which prevents resolving).");
2944        }
2945        return getReady()->getDataAtOffset(i);
2946  }  }
2947    
2948    
2949  DataTypes::ValueType::reference  DataTypes::ValueType::reference
2950  Data::getDataAtOffset(DataTypes::ValueType::size_type i)  Data::getDataAtOffset(DataTypes::ValueType::size_type i)
2951  {  {
2952      return m_data->getDataAtOffset(i);  //     if (isLazy())
2953    //     {
2954    //  throw DataException("getDataAtOffset not permitted on lazy data.");
2955    //     }
2956        FORCERESOLVE;
2957        return getReady()->getDataAtOffset(i);
2958  }  }
2959    
2960  DataTypes::ValueType::const_reference  DataTypes::ValueType::const_reference
2961  Data::getDataPoint(int sampleNo, int dataPointNo) const  Data::getDataPoint(int sampleNo, int dataPointNo) const
2962  {  {
2963      return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));    if (!isReady())
2964      {
2965        throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data (object is const which prevents resolving).");
2966      }
2967      else
2968      {
2969        const DataReady* dr=getReady();
2970        return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2971      }
2972  }  }
2973    
2974    
2975  DataTypes::ValueType::reference  DataTypes::ValueType::reference
2976  Data::getDataPoint(int sampleNo, int dataPointNo)  Data::getDataPoint(int sampleNo, int dataPointNo)
2977  {  {
2978      return m_data->getDataAtOffset(m_data->getPointOffset(sampleNo, dataPointNo));    FORCERESOLVE;
2979      if (!isReady())
2980      {
2981        throw DataException("Programmer error - getDataPoint() not permitted on Lazy Data.");
2982      }
2983      else
2984      {
2985        DataReady* dr=getReady();
2986        return dr->getDataAtOffset(dr->getPointOffset(sampleNo, dataPointNo));
2987      }
2988  }  }
2989    
2990    
# Line 2527  void Line 3008  void
3008  Data::dump(const std::string fileName) const  Data::dump(const std::string fileName) const
3009  {  {
3010    try    try
3011       {    {
3012          return m_data->dump(fileName);      if (isLazy())
3013       }      {
3014       catch (exception& e)        Data temp(*this); // this is to get a non-const object which we can resolve
3015       {        temp.resolve();
3016          temp.dump(fileName);
3017        }
3018        else
3019        {
3020              return m_data->dump(fileName);
3021        }
3022      }
3023      catch (exception& e)
3024      {
3025          cout << e.what() << endl;          cout << e.what() << endl;
3026       }    }
3027  }  }
3028    
3029  int  int

Legend:
Removed from v.1953  
changed lines
  Added in v.2049

  ViewVC Help
Powered by ViewVC 1.1.26