|
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 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 00043 #include "Teuchos_ConfigDefs.hpp" 00044 #include "Teuchos_Handle.hpp" 00045 #include "Teuchos_Handleable.hpp" 00046 #include "Teuchos_GlobalMPISession.hpp" 00047 #include "Teuchos_MPIContainerComm.hpp" 00048 #include "Teuchos_ErrorPolling.hpp" 00049 #include "Teuchos_StrUtils.hpp" 00050 #include "Teuchos_Version.hpp" 00051 00052 00053 using namespace Teuchos; 00054 using std::string; 00055 00056 //#ifndef DOXYGEN_SHOULD_SKIP_THIS 00057 00058 class VecSpaceBase; 00059 00060 class VecBase : public Handleable<VecBase> 00061 { 00062 public: 00063 VecBase(){;} 00064 ~VecBase(){;} 00065 00066 virtual RCP<const VecSpaceBase> space() const = 0 ; 00067 00068 virtual void add(const VecBase* other, 00069 RCP<VecBase>& result) const = 0 ; 00070 00071 00072 virtual double dot(const VecBase* other) const = 0 ; 00073 00074 virtual void scale(const double& a) = 0 ; 00075 00076 virtual RCP<VecBase> copy() const = 0 ; 00077 00078 virtual void print(std::ostream& os) const = 0 ; 00079 00080 virtual void setElement(int i, const double& x) = 0 ; 00081 00082 virtual const double& getElement(int i) const = 0 ; 00083 00084 virtual int dim() const = 0 ; 00085 }; 00086 00087 00088 class VecSpaceBase : public ConstHandleable<VecSpaceBase> 00089 { 00090 public: 00091 VecSpaceBase(){;} 00092 ~VecSpaceBase(){;} 00093 00094 virtual RCP<VecBase> create(const RCP<const VecSpaceBase>& s) const = 0; 00095 00096 }; 00097 00098 00099 00100 class VecSpaceA : public VecSpaceBase 00101 { 00102 public: 00103 VecSpaceA(int n) : n_(n) {;} 00104 virtual RCP<VecBase> create(const RCP<const VecSpaceBase>& s) const ; 00105 TEUCHOS_GET_CONST_RCP(VecSpaceBase) 00106 private: 00107 int n_; 00108 }; 00109 00110 class VecA : public VecBase 00111 { 00112 public: 00113 VecA(int n, const RCP<const VecSpaceBase>& sp) : x_(n), 00114 sp_(sp) 00115 {for (unsigned int i=0; i<x_.size(); i++) x_[i] = 0.0;} 00116 00117 RCP<const VecSpaceBase> space() const {return sp_;} 00118 00119 void add(const VecBase* other, 00120 RCP<VecBase>& result) const 00121 { 00122 const VecA* va = dynamic_cast<const VecA*>(other); 00123 VecA* vr = dynamic_cast<VecA*>(result.get()); 00124 TEUCHOS_TEST_FOR_EXCEPT(va==0); 00125 TEUCHOS_TEST_FOR_EXCEPT(vr==0); 00126 for (unsigned int i=0; i<x_.size(); i++) 00127 { 00128 vr->x_[i] = x_[i] + va->x_[i]; 00129 } 00130 } 00131 00132 double dot(const VecBase* other) const 00133 { 00134 double rtn = 0.0; 00135 const VecA* va = dynamic_cast<const VecA*>(other); 00136 TEUCHOS_TEST_FOR_EXCEPT(va==0); 00137 for (unsigned int i=0; i<x_.size(); i++) 00138 { 00139 rtn += x_[i] * va->x_[i]; 00140 } 00141 return rtn; 00142 } 00143 00144 void scale(const double& a) 00145 { 00146 for (unsigned int i=0; i<x_.size(); i++) 00147 { 00148 x_[i] *= a; 00149 } 00150 } 00151 00152 RCP<VecBase> copy() const 00153 { 00154 RCP<VecBase> rtn = space()->create(space()); 00155 VecA* va = dynamic_cast<VecA*>(rtn.get()); 00156 TEUCHOS_TEST_FOR_EXCEPT(va==0); 00157 for (unsigned int i=0; i<x_.size(); i++) 00158 { 00159 va->x_[i] = x_[i]; 00160 } 00161 return rtn; 00162 } 00163 00164 void print(std::ostream& os) const 00165 { 00166 for (unsigned int i=0; i<x_.size(); i++) 00167 { 00168 os << i << " " << x_[i] << std::endl; 00169 } 00170 } 00171 00172 void setElement(int i, const double& x) 00173 {x_[i] = x;} 00174 00175 const double& getElement(int i) const {return x_[i];} 00176 00177 int dim() const {return x_.size();} 00178 00179 TEUCHOS_GET_RCP(VecBase) 00180 private: 00181 Array<double> x_; 00182 RCP<const VecSpaceBase> sp_; 00183 }; 00184 00185 00186 RCP<VecBase> VecSpaceA::create(const RCP<const VecSpaceBase>& s) const 00187 { 00188 return rcp(new VecA(n_, s)); 00189 } 00190 00191 class Vector; 00192 00193 00194 00195 class ConstVector : public virtual ConstHandle<VecBase> 00196 { 00197 public: 00198 TEUCHOS_CONST_HANDLE_CTORS(ConstVector, VecBase) 00199 00200 00201 RCP<const VecSpaceBase> space() const {return constPtr()->space();} 00202 00203 double operator*(const ConstVector& other) const ; 00204 00205 void add(const ConstVector& other, Vector& result) const ; 00206 00207 void copyInto(Vector& x) const ; 00208 00209 int dim() const {return constPtr()->dim();} 00210 00211 const double& getElement(int i) const {return constPtr()->getElement(i);} 00212 }; 00213 00214 class Vector : public ConstVector, 00215 public Handle<VecBase> 00216 { 00217 public: 00218 TEUCHOS_HANDLE_CTORS(Vector, VecBase) 00219 00220 void scale(const double& a) {ptr()->scale(a);} 00221 00222 void setElement(int i, const double& x) {ptr()->setElement(i, x);} 00223 00224 }; 00225 00226 00227 Vector copy(const ConstVector& x) 00228 { 00229 Vector rtn; 00230 x.copyInto(rtn); 00231 return rtn; 00232 } 00233 00234 Vector operator+(const ConstVector& a, const ConstVector& b) 00235 { 00236 Vector result; 00237 a.add(b, result); 00238 return result; 00239 } 00240 00241 Vector operator*(const ConstVector& x, const double& a) 00242 { 00243 Vector result = copy(x); 00244 result.scale(a); 00245 return result; 00246 } 00247 00248 Vector operator*(const double& a, const ConstVector& x) 00249 { 00250 return x*a; 00251 } 00252 00253 00254 void ConstVector::add(const ConstVector& other, Vector& result) const 00255 { 00256 result = space()->create(space()); 00257 RCP<VecBase> tmp = result.ptr(); 00258 constPtr()->add(other.constPtr().get(), tmp); 00259 } 00260 00261 void ConstVector::copyInto(Vector& result) const 00262 { 00263 result = constPtr()->copy(); 00264 } 00265 00266 00267 00268 00269 class VectorSpace : public ConstHandle<VecSpaceBase> 00270 { 00271 public: 00272 TEUCHOS_CONST_HANDLE_CTORS(VectorSpace, VecSpaceBase) 00273 00274 Vector create() const {return constPtr()->create(constPtr());} 00275 }; 00276 00277 00278 std::ostream& operator<<(std::ostream& os, const ConstVector& v) 00279 { 00280 v.constPtr()->print(os); 00281 return os; 00282 } 00283 00284 //#endif 00285 00286 /* Test of Teuchos generic handle classes */ 00287 00288 int main(int argc, char** argv) 00289 { 00290 int state = 0; 00291 std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl; 00292 00293 Teuchos::GlobalMPISession mpiSession(&argc, &argv); 00294 00295 try 00296 { 00297 VectorSpace space = new VecSpaceA(3); 00298 Vector x = space.create(); 00299 Vector y = space.create(); 00300 00301 00302 for (int i=0; i<x.dim(); i++) 00303 { 00304 x.setElement(i, i); 00305 y.setElement(i, i+2.0); 00306 } 00307 Vector z = copy(x); 00308 00309 std::cout << "x = " << x << std::endl; 00310 std::cout << "y = " << y << std::endl; 00311 std::cout << "z = " << z << std::endl; 00312 00313 std::cout << "mess = " << 2.0*(x+y+3.0*z) << std::endl; 00314 00315 Vector a = 2.0*(x+y+3.0*z); 00316 std::cout << "a=" << std::endl; 00317 double err = 0.0; 00318 for (int i=0; i<a.dim(); i++) 00319 { 00320 std::cout << i << " " << a.getElement(i) << std::endl; 00321 double x_i = x.getElement(i); 00322 double y_i = y.getElement(i); 00323 double z_i = z.getElement(i); 00324 double t = 2.0*(x_i + y_i + 3.0*z_i); 00325 err += std::fabs(t - a.getElement(i)); 00326 } 00327 00328 VectorSpace s2 = new VecSpaceA(5); 00329 VecBase* vb = new VecA(5, s2.constPtr()); 00330 Vector b = vb; 00331 00332 std::cout << "b = " << b << std::endl; 00333 00334 if (err > 1.0e-12) state = 1; 00335 } 00336 catch(std::exception& e) 00337 { 00338 std::cerr << e.what() << std::endl; 00339 state = 1; 00340 } 00341 00342 if (state != 0) 00343 { 00344 std::cout << "TEST FAILED" << std::endl; 00345 return -1; 00346 } 00347 00348 00349 std::cout << "TEST PASSED" << std::endl; 00350 return state; 00351 }
1.7.6.1