|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
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 00030 #include "Teuchos_ConfigDefs.hpp" 00031 #include "Teuchos_Handle.hpp" 00032 #include "Teuchos_Handleable.hpp" 00033 #include "Teuchos_GlobalMPISession.hpp" 00034 #include "Teuchos_MPIContainerComm.hpp" 00035 #include "Teuchos_ErrorPolling.hpp" 00036 #include "Teuchos_StrUtils.hpp" 00037 #include "Teuchos_Version.hpp" 00038 00039 00040 using namespace Teuchos; 00041 using std::string; 00042 00043 //#ifndef DOXYGEN_SHOULD_SKIP_THIS 00044 00045 class VecSpaceBase; 00046 00047 class VecBase : public Handleable<VecBase> 00048 { 00049 public: 00050 VecBase(){;} 00051 ~VecBase(){;} 00052 00053 virtual RCP<const VecSpaceBase> space() const = 0 ; 00054 00055 virtual void add(const VecBase* other, 00056 RCP<VecBase>& result) const = 0 ; 00057 00058 00059 virtual double dot(const VecBase* other) const = 0 ; 00060 00061 virtual void scale(const double& a) = 0 ; 00062 00063 virtual RCP<VecBase> copy() const = 0 ; 00064 00065 virtual void print(std::ostream& os) const = 0 ; 00066 00067 virtual void setElement(int i, const double& x) = 0 ; 00068 00069 virtual const double& getElement(int i) const = 0 ; 00070 00071 virtual int dim() const = 0 ; 00072 }; 00073 00074 00075 class VecSpaceBase : public ConstHandleable<VecSpaceBase> 00076 { 00077 public: 00078 VecSpaceBase(){;} 00079 ~VecSpaceBase(){;} 00080 00081 virtual RCP<VecBase> create(const RCP<const VecSpaceBase>& s) const = 0; 00082 00083 }; 00084 00085 00086 00087 class VecSpaceA : public VecSpaceBase 00088 { 00089 public: 00090 VecSpaceA(int n) : n_(n) {;} 00091 virtual RCP<VecBase> create(const RCP<const VecSpaceBase>& s) const ; 00092 TEUCHOS_GET_CONST_RCP(VecSpaceBase) 00093 private: 00094 int n_; 00095 }; 00096 00097 class VecA : public VecBase 00098 { 00099 public: 00100 VecA(int n, const RCP<const VecSpaceBase>& sp) : x_(n), 00101 sp_(sp) 00102 {for (unsigned int i=0; i<x_.size(); i++) x_[i] = 0.0;} 00103 00104 RCP<const VecSpaceBase> space() const {return sp_;} 00105 00106 void add(const VecBase* other, 00107 RCP<VecBase>& result) const 00108 { 00109 const VecA* va = dynamic_cast<const VecA*>(other); 00110 VecA* vr = dynamic_cast<VecA*>(result.get()); 00111 TEST_FOR_EXCEPT(va==0); 00112 TEST_FOR_EXCEPT(vr==0); 00113 for (unsigned int i=0; i<x_.size(); i++) 00114 { 00115 vr->x_[i] = x_[i] + va->x_[i]; 00116 } 00117 } 00118 00119 double dot(const VecBase* other) const 00120 { 00121 double rtn = 0.0; 00122 const VecA* va = dynamic_cast<const VecA*>(other); 00123 TEST_FOR_EXCEPT(va==0); 00124 for (unsigned int i=0; i<x_.size(); i++) 00125 { 00126 rtn += x_[i] * va->x_[i]; 00127 } 00128 return rtn; 00129 } 00130 00131 void scale(const double& a) 00132 { 00133 for (unsigned int i=0; i<x_.size(); i++) 00134 { 00135 x_[i] *= a; 00136 } 00137 } 00138 00139 RCP<VecBase> copy() const 00140 { 00141 RCP<VecBase> rtn = space()->create(space()); 00142 VecA* va = dynamic_cast<VecA*>(rtn.get()); 00143 TEST_FOR_EXCEPT(va==0); 00144 for (unsigned int i=0; i<x_.size(); i++) 00145 { 00146 va->x_[i] = x_[i]; 00147 } 00148 return rtn; 00149 } 00150 00151 void print(std::ostream& os) const 00152 { 00153 for (unsigned int i=0; i<x_.size(); i++) 00154 { 00155 os << i << " " << x_[i] << std::endl; 00156 } 00157 } 00158 00159 void setElement(int i, const double& x) 00160 {x_[i] = x;} 00161 00162 const double& getElement(int i) const {return x_[i];} 00163 00164 int dim() const {return x_.size();} 00165 00166 TEUCHOS_GET_RCP(VecBase) 00167 private: 00168 Array<double> x_; 00169 RCP<const VecSpaceBase> sp_; 00170 }; 00171 00172 00173 RCP<VecBase> VecSpaceA::create(const RCP<const VecSpaceBase>& s) const 00174 { 00175 return rcp(new VecA(n_, s)); 00176 } 00177 00178 class Vector; 00179 00180 00181 00182 class ConstVector : public virtual ConstHandle<VecBase> 00183 { 00184 public: 00185 TEUCHOS_CONST_HANDLE_CTORS(ConstVector, VecBase) 00186 00187 00188 RCP<const VecSpaceBase> space() const {return constPtr()->space();} 00189 00190 double operator*(const ConstVector& other) const ; 00191 00192 void add(const ConstVector& other, Vector& result) const ; 00193 00194 void copyInto(Vector& x) const ; 00195 00196 int dim() const {return constPtr()->dim();} 00197 00198 const double& getElement(int i) const {return constPtr()->getElement(i);} 00199 }; 00200 00201 class Vector : public ConstVector, 00202 public Handle<VecBase> 00203 { 00204 public: 00205 TEUCHOS_HANDLE_CTORS(Vector, VecBase) 00206 00207 void scale(const double& a) {ptr()->scale(a);} 00208 00209 void setElement(int i, const double& x) {ptr()->setElement(i, x);} 00210 00211 }; 00212 00213 00214 Vector copy(const ConstVector& x) 00215 { 00216 Vector rtn; 00217 x.copyInto(rtn); 00218 return rtn; 00219 } 00220 00221 Vector operator+(const ConstVector& a, const ConstVector& b) 00222 { 00223 Vector result; 00224 a.add(b, result); 00225 return result; 00226 } 00227 00228 Vector operator*(const ConstVector& x, const double& a) 00229 { 00230 Vector result = copy(x); 00231 result.scale(a); 00232 return result; 00233 } 00234 00235 Vector operator*(const double& a, const ConstVector& x) 00236 { 00237 return x*a; 00238 } 00239 00240 00241 void ConstVector::add(const ConstVector& other, Vector& result) const 00242 { 00243 result = space()->create(space()); 00244 RCP<VecBase> tmp = result.ptr(); 00245 constPtr()->add(other.constPtr().get(), tmp); 00246 } 00247 00248 void ConstVector::copyInto(Vector& result) const 00249 { 00250 result = constPtr()->copy(); 00251 } 00252 00253 00254 00255 00256 class VectorSpace : public ConstHandle<VecSpaceBase> 00257 { 00258 public: 00259 TEUCHOS_CONST_HANDLE_CTORS(VectorSpace, VecSpaceBase) 00260 00261 Vector create() const {return constPtr()->create(constPtr());} 00262 }; 00263 00264 00265 std::ostream& operator<<(std::ostream& os, const ConstVector& v) 00266 { 00267 v.constPtr()->print(os); 00268 return os; 00269 } 00270 00271 //#endif 00272 00273 /* Test of Teuchos generic handle classes */ 00274 00275 int main(int argc, char** argv) 00276 { 00277 int state = 0; 00278 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00279 00280 Teuchos::GlobalMPISession mpiSession(&argc, &argv); 00281 00282 try 00283 { 00284 VectorSpace space = new VecSpaceA(3); 00285 Vector x = space.create(); 00286 Vector y = space.create(); 00287 00288 00289 for (int i=0; i<x.dim(); i++) 00290 { 00291 x.setElement(i, i); 00292 y.setElement(i, i+2.0); 00293 } 00294 Vector z = copy(x); 00295 00296 std::cout << "x = " << x << std::endl; 00297 std::cout << "y = " << y << std::endl; 00298 std::cout << "z = " << z << std::endl; 00299 00300 std::cout << "mess = " << 2.0*(x+y+3.0*z) << std::endl; 00301 00302 Vector a = 2.0*(x+y+3.0*z); 00303 std::cout << "a=" << std::endl; 00304 double err = 0.0; 00305 for (int i=0; i<a.dim(); i++) 00306 { 00307 std::cout << i << " " << a.getElement(i) << std::endl; 00308 double x_i = x.getElement(i); 00309 double y_i = y.getElement(i); 00310 double z_i = z.getElement(i); 00311 double t = 2.0*(x_i + y_i + 3.0*z_i); 00312 err += std::fabs(t - a.getElement(i)); 00313 } 00314 00315 VectorSpace s2 = new VecSpaceA(5); 00316 VecBase* vb = new VecA(5, s2.constPtr()); 00317 Vector b = vb; 00318 00319 std::cout << "b = " << b << std::endl; 00320 00321 if (err > 1.0e-12) state = 1; 00322 } 00323 catch(std::exception& e) 00324 { 00325 std::cerr << e.what() << std::endl; 00326 state = 1; 00327 } 00328 00329 if (state != 0) 00330 { 00331 std::cout << "TEST FAILED" << std::endl; 00332 return -1; 00333 } 00334 00335 00336 std::cout << "TEST PASSED" << std::endl; 00337 return state; 00338 }
1.7.4