test/Handle/cxx_main.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 // @HEADER
00030 
00031 
00032 #include "Teuchos_ConfigDefs.hpp"
00033 #include "Teuchos_Handle.hpp"
00034 #include "Teuchos_Handleable.hpp"
00035 #include "Teuchos_GlobalMPISession.hpp"
00036 #include "Teuchos_MPIContainerComm.hpp"
00037 #include "Teuchos_ErrorPolling.hpp"
00038 #include "Teuchos_StrUtils.hpp"
00039 #include "Teuchos_Version.hpp"
00040 
00041 
00042 using namespace Teuchos;
00043 using std::string;
00044 
00045 //#ifndef DOXYGEN_SHOULD_SKIP_THIS
00046 
00047 class VecSpaceBase;
00048 
00049 class VecBase : public Handleable<VecBase>
00050 {
00051 public:
00052   VecBase(){;}
00053   ~VecBase(){;}
00054 
00055   virtual RCP<const VecSpaceBase> space() const = 0 ;
00056 
00057   virtual void add(const VecBase* other, 
00058                    RCP<VecBase>& result) const = 0 ;
00059 
00060 
00061   virtual double dot(const VecBase* other) const = 0 ;
00062 
00063   virtual void scale(const double& a) = 0 ;
00064 
00065   virtual RCP<VecBase> copy() const = 0 ;
00066 
00067   virtual void print(std::ostream& os) const = 0 ;
00068 
00069   virtual void setElement(int i, const double& x) = 0 ;
00070 
00071   virtual const double& getElement(int i) const = 0 ;
00072 
00073   virtual int dim() const = 0 ;
00074 };
00075 
00076 
00077 class VecSpaceBase : public ConstHandleable<VecSpaceBase>
00078 {
00079 public:
00080   VecSpaceBase(){;}
00081   ~VecSpaceBase(){;}
00082 
00083   virtual RCP<VecBase> create(const RCP<const VecSpaceBase>& s) const = 0;
00084 
00085 };
00086 
00087 
00088 
00089 class VecSpaceA : public VecSpaceBase
00090 {
00091 public:
00092   VecSpaceA(int n) : n_(n) {;}
00093   virtual RCP<VecBase> create(const RCP<const VecSpaceBase>& s) const ;
00094   TEUCHOS_GET_CONST_RCP(VecSpaceBase)
00095 private:
00096   int n_;
00097 };
00098 
00099 class VecA : public VecBase
00100 {
00101 public:
00102   VecA(int n, const RCP<const VecSpaceBase>& sp) : x_(n),
00103                                                            sp_(sp)
00104   {for (unsigned int i=0; i<x_.size(); i++) x_[i] = 0.0;}
00105 
00106   RCP<const VecSpaceBase> space() const {return sp_;}
00107   
00108   void add(const VecBase* other, 
00109            RCP<VecBase>& result) const 
00110   {
00111     const VecA* va = dynamic_cast<const VecA*>(other);
00112     VecA* vr = dynamic_cast<VecA*>(result.get());
00113     TEST_FOR_EXCEPT(va==0);
00114     TEST_FOR_EXCEPT(vr==0);
00115     for (unsigned int i=0; i<x_.size(); i++) 
00116       {
00117         vr->x_[i] = x_[i] + va->x_[i];
00118       }
00119   }
00120 
00121   double dot(const VecBase* other) const 
00122   {
00123     double rtn = 0.0;
00124     const VecA* va = dynamic_cast<const VecA*>(other);
00125     TEST_FOR_EXCEPT(va==0);
00126     for (unsigned int i=0; i<x_.size(); i++) 
00127       {
00128         rtn += x_[i] * va->x_[i];
00129       }
00130     return rtn;
00131   }
00132 
00133   void scale(const double& a) 
00134   {
00135     for (unsigned int i=0; i<x_.size(); i++) 
00136       {
00137         x_[i] *= a;
00138       }
00139   }
00140 
00141   RCP<VecBase> copy() const
00142   {
00143     RCP<VecBase> rtn = space()->create(space());
00144     VecA* va = dynamic_cast<VecA*>(rtn.get());
00145     TEST_FOR_EXCEPT(va==0);
00146     for (unsigned int i=0; i<x_.size(); i++) 
00147       {
00148         va->x_[i] = x_[i];
00149       }
00150     return rtn;
00151   }
00152 
00153   void print(std::ostream& os) const 
00154   {
00155     for (unsigned int i=0; i<x_.size(); i++) 
00156       {
00157         os << i << " " << x_[i] << std::endl;
00158       }
00159   }
00160 
00161   void setElement(int i, const double& x) 
00162   {x_[i] = x;}
00163 
00164   const double& getElement(int i) const {return x_[i];}
00165 
00166   int dim() const {return x_.size();}
00167 
00168   TEUCHOS_GET_RCP(VecBase)
00169 private:
00170   Array<double> x_;
00171   RCP<const VecSpaceBase> sp_;
00172 };
00173 
00174 
00175 RCP<VecBase> VecSpaceA::create(const RCP<const VecSpaceBase>& s) const
00176 {
00177   return rcp(new VecA(n_, s));
00178 }
00179 
00180 class Vector;
00181 
00182 
00183 
00184 class ConstVector : public virtual ConstHandle<VecBase>
00185 {
00186 public:
00187   TEUCHOS_CONST_HANDLE_CTORS(ConstVector, VecBase)
00188   
00189 
00190   RCP<const VecSpaceBase> space() const {return constPtr()->space();}
00191 
00192   double operator*(const ConstVector& other) const ;  
00193 
00194   void add(const ConstVector& other, Vector& result) const ;
00195 
00196   void copyInto(Vector& x) const ;
00197 
00198   int dim() const {return constPtr()->dim();}
00199 
00200   const double& getElement(int i) const {return constPtr()->getElement(i);}
00201 };
00202 
00203 class Vector : public ConstVector,
00204                public Handle<VecBase>
00205 {
00206 public:
00207   TEUCHOS_HANDLE_CTORS(Vector, VecBase)
00208 
00209   void scale(const double& a) {ptr()->scale(a);}
00210 
00211   void setElement(int i, const double& x) {ptr()->setElement(i,  x);}
00212 
00213 };
00214 
00215 
00216 Vector copy(const ConstVector& x)
00217 {
00218   Vector rtn;
00219   x.copyInto(rtn);
00220   return rtn;
00221 }
00222 
00223 Vector operator+(const ConstVector& a, const ConstVector& b)
00224 {
00225   Vector result;
00226   a.add(b, result);
00227   return result;
00228 }
00229 
00230 Vector operator*(const ConstVector& x, const double& a) 
00231 {
00232   Vector result = copy(x);
00233   result.scale(a);
00234   return result;
00235 }
00236 
00237 Vector operator*(const double& a, const ConstVector& x) 
00238 {
00239   return x*a;
00240 }
00241 
00242 
00243 void ConstVector::add(const ConstVector& other, Vector& result) const 
00244 {
00245   result = space()->create(space());
00246   RCP<VecBase> tmp = result.ptr();
00247   constPtr()->add(other.constPtr().get(), tmp);
00248 }
00249 
00250 void ConstVector::copyInto(Vector& result) const 
00251 {
00252   result = constPtr()->copy();
00253 }
00254 
00255 
00256 
00257 
00258 class VectorSpace : public ConstHandle<VecSpaceBase>
00259 {
00260 public:
00261   TEUCHOS_CONST_HANDLE_CTORS(VectorSpace, VecSpaceBase)
00262   
00263   Vector create() const {return constPtr()->create(constPtr());}
00264 };
00265 
00266 
00267 std::ostream& operator<<(std::ostream& os, const ConstVector& v)
00268 {
00269   v.constPtr()->print(os);
00270   return os;
00271 }
00272 
00273 //#endif
00274 
00275 /* Test of Teuchos generic handle classes */
00276 
00277 int main(int argc, char** argv)
00278 {
00279   int state = 0;
00280   std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00281 
00282   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00283 
00284   try
00285     {
00286       VectorSpace space = new VecSpaceA(3);
00287       Vector x = space.create();
00288       Vector y = space.create();
00289 
00290       
00291       for (int i=0; i<x.dim(); i++)
00292         {
00293           x.setElement(i, i);
00294           y.setElement(i, i+2.0);
00295         }
00296       Vector z = copy(x);
00297 
00298       std::cout << "x = " << x << std::endl;
00299       std::cout << "y = " << y << std::endl;
00300       std::cout << "z = " << z << std::endl;
00301 
00302       std::cout << "mess = " << 2.0*(x+y+3.0*z) << std::endl;
00303 
00304       Vector a = 2.0*(x+y+3.0*z);
00305       std::cout << "a=" << std::endl;
00306       double err = 0.0;
00307       for (int i=0; i<a.dim(); i++)
00308         {
00309           std::cout << i << " " << a.getElement(i) << std::endl;
00310           double x_i = x.getElement(i);
00311           double y_i = y.getElement(i);
00312           double z_i = z.getElement(i);
00313           double t = 2.0*(x_i + y_i + 3.0*z_i);
00314           err += std::fabs(t - a.getElement(i));
00315         }
00316       
00317       VectorSpace s2 = new VecSpaceA(5);
00318       VecBase* vb = new VecA(5, s2.constPtr());
00319       Vector b = vb;
00320 
00321       std::cout << "b = " << b << std::endl;
00322 
00323       if (err > 1.0e-12) state = 1;
00324     }
00325   catch(std::exception& e)
00326     {
00327       std::cerr << e.what() << std::endl;
00328       state = 1;
00329     }
00330 
00331   if (state != 0)
00332     {
00333       std::cout << "TEST FAILED" << std::endl;
00334       return -1;
00335     }
00336 
00337   
00338   std::cout << "TEST PASSED" << std::endl;
00339   return state;
00340 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Tue Oct 20 10:13:59 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1