RTOp Package Browser (Single Doxygen Collection) Version of the Day
RTOpPack_TOpRandomize.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // RTOp: Interfaces and Support Software for Vector Reduction Transformation
00005 //       Operations
00006 //                Copyright (2006) Sandia Corporation
00007 // 
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //  
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00026 // 
00027 // ***********************************************************************
00028 // @HEADER
00029 
00030 #ifndef RTOPPACK_TOP_RANDOMIZE_HPP
00031 #define RTOPPACK_TOP_RANDOMIZE_HPP
00032 
00033 #include "RTOpPack_RTOpTHelpers.hpp"
00034 
00035 
00036 namespace RTOpPack {
00037 
00038 
00051 template<class Scalar>
00052 class TOpRandomize : public RTOpT<Scalar> {
00053 public:
00054   using RTOpT<Scalar>::apply_op;
00056   static void set_static_seed( const unsigned int static_seed )
00057     { static_seed_ = static_seed; }
00059   static unsigned int get_static_seed() { return static_seed_; }
00061   TOpRandomize(
00062     const Scalar& l = -ScalarTraits<Scalar>::one(),
00063     const Scalar& u = +ScalarTraits<Scalar>::one()
00064     )
00065     {
00066       this->setOpNameBase("TOpRandomize");
00067       set_bounds(l, u);
00068       set_seed(static_seed_);
00069       ++static_seed_; // By default we will just increment the seed!
00070     }
00072   void set_bounds( const Scalar& l, const Scalar& u )
00073     { l_ = l; u_ = u; }
00075   void set_seed( const unsigned int seed ) { seed_ = seed; }
00077   unsigned int get_seed() const { return seed_; }
00081   void apply_op_impl(
00082     const ArrayView<const ConstSubVectorView<Scalar> > &sub_vecs,
00083     const ArrayView<const SubVectorView<Scalar> > &targ_sub_vecs,
00084     const Ptr<ReductTarget> &reduct_obj_inout
00085     ) const
00086     {
00087       typedef typename Teuchos::ArrayRCP<const Scalar>::iterator const_iter_t;
00088       typedef typename Teuchos::ArrayRCP<Scalar>::iterator iter_t;
00089 
00090 #ifdef TEUCHOS_DEBUG
00091       validate_apply_op<Scalar>(*this, 0, 1, false,
00092         sub_vecs, targ_sub_vecs, reduct_obj_inout.getConst());
00093 #endif
00094       
00095       const index_type subDim = targ_sub_vecs[0].subDim();
00096       const index_type globalOffset =  targ_sub_vecs[0].globalOffset();
00097 
00098       iter_t z0_val = targ_sub_vecs[0].values().begin();
00099       const ptrdiff_t z0_s = targ_sub_vecs[0].stride();
00100 
00101       // Linear coefficients for translating from [-1,+1] to [l,b]
00102       const Scalar a = Scalar(0.5)*(u_ - l_);
00103       const Scalar b = Scalar(0.5)*(u_ + l_);
00104       for( index_type i = 0; i < subDim; ++i, z0_val += z0_s )
00105       {
00106         Teuchos::ScalarTraits<Scalar>::seedrandom(seed_+globalOffset+i);
00107         *z0_val = a * Teuchos::ScalarTraits<Scalar>::random() + b;
00108         // Above should be in the range [l,b]
00109       }
00110     }
00112 private:
00113   static unsigned int static_seed_;
00114   unsigned int seed_;
00115   Scalar l_;
00116   Scalar u_;
00117 };
00118 
00119 
00120 template<class Scalar>
00121 unsigned int TOpRandomize<Scalar>::static_seed_ = 0;
00122 
00123 
00124 } // namespace RTOpPack
00125 
00126 
00127 #endif // RTOPPACK_TOP_RANDOMIZE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines