MOOCHO (Single Doxygen Collection) Version of the Day
AbstractLinAlgPack_ThyraAccessors.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 // 
00005 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00006 //                  Copyright (2003) 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 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00039 // 
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 // ////////////////////////////////////////////////////
00045 // ThyraAccessors.cpp
00046 
00047 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
00048 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
00049 #include "Teuchos_dyn_cast.hpp"
00050 
00051 void AbstractLinAlgPack::get_thyra_vector(
00052   const VectorSpaceThyra                                         &thyra_vec_spc
00053   ,const Vector                                                  &vec
00054   ,Teuchos::RCP<const Thyra::VectorBase<value_type> >    *thyra_vec
00055   )
00056 {
00057 #ifdef TEUCHOS_DEBUG
00058     TEUCHOS_TEST_FOR_EXCEPTION( thyra_vec==NULL, std::invalid_argument, "Error!" );
00059 #endif
00060   const VectorMutableThyra *vmthyra_vec = dynamic_cast<const VectorMutableThyra*>(&vec);
00061   if(vmthyra_vec) {
00062     // We can just grap the const smart pointer to the underlying object 
00063     *thyra_vec = vmthyra_vec->thyra_vec();
00064   }
00065   else if(vec.space().is_in_core()) {
00066     // We need to create a temporary copy and then copy the explicit elements
00067     Teuchos::RCP<Thyra::VectorBase<value_type> >
00068       _thyra_vec = ::Thyra::createMember(thyra_vec_spc.thyra_vec_spc());
00069     // Get explicit views of the elements
00070     RTOpPack::SubVector vec_sv;
00071     vec.get_sub_vector( Range1D(), &vec_sv );
00072     RTOpPack::SubVectorView<value_type> _thyra_vec_sv;
00073     _thyra_vec->acquireDetachedView( convert(Range1D()), &_thyra_vec_sv );
00074 #ifdef TEUCHOS_DEBUG
00075     TEUCHOS_TEST_FOR_EXCEPTION( vec_sv.subDim() != _thyra_vec_sv.subDim(), std::logic_error, "Error!" );
00076 #endif
00077     // Copy the elements
00078     for( int i = 0; i < vec_sv.subDim(); ++i )
00079       _thyra_vec_sv[i] = vec_sv[i];
00080     // Free/commit the explicit views
00081     vec.free_sub_vector( &vec_sv );
00082     _thyra_vec->commitDetachedView( &_thyra_vec_sv );
00083     // Set the output smart pointer
00084     *thyra_vec = _thyra_vec;
00085   }
00086   else {
00087     TEUCHOS_TEST_FOR_EXCEPTION(
00088       true, std::logic_error
00089       ,"AbstractLinAlgPack::get_thyra_vector(...): Error, the vector of concrete type \'"
00090       << typeName(vec) << "\' is not an incore vector."
00091       );
00092   }
00093 }
00094 
00095 void AbstractLinAlgPack::free_thyra_vector(
00096   const VectorSpaceThyra                                         &thyra_vec_spc
00097   ,const Vector                                                  &vec
00098   ,Teuchos::RCP<const Thyra::VectorBase<value_type> >    *thyra_vec
00099   )
00100 {
00101 #ifdef TEUCHOS_DEBUG
00102     TEUCHOS_TEST_FOR_EXCEPTION( thyra_vec==NULL, std::invalid_argument, "Error!" );
00103 #endif
00104   *thyra_vec = Teuchos::null;  // This works in both cases above!
00105 }
00106 
00107 void AbstractLinAlgPack::get_thyra_vector(
00108   const VectorSpaceThyra                                         &thyra_vec_spc
00109   ,VectorMutable                                                 *vec
00110   ,Teuchos::RCP<Thyra::VectorBase<value_type> >          *thyra_vec
00111   )
00112 { 
00113 #ifdef TEUCHOS_DEBUG
00114     TEUCHOS_TEST_FOR_EXCEPTION( vec==NULL || thyra_vec==NULL, std::invalid_argument, "Error!" );
00115 #endif
00116   VectorMutableThyra *vmthyra_vec = dynamic_cast<VectorMutableThyra*>(vec);
00117   if(vmthyra_vec) {
00118     // We can just directly grap the Thyra vector
00119     *thyra_vec = vmthyra_vec->set_uninitialized();
00120   }
00121   else if(thyra_vec_spc.is_in_core()) {
00122     // We need to create a temporary copy and then copy the explicit elements
00123     Teuchos::RCP<Thyra::VectorBase<value_type> >
00124       _thyra_vec = ::Thyra::createMember(thyra_vec_spc.thyra_vec_spc());
00125     // Get explicit views of the elements
00126     RTOpPack::SubVector vec_sv;
00127     vec->get_sub_vector( Range1D(), &vec_sv );
00128     RTOpPack::SubVectorView<value_type> _thyra_vec_sv;
00129     _thyra_vec->acquireDetachedView( convert(Range1D()), &_thyra_vec_sv );
00130 #ifdef TEUCHOS_DEBUG
00131     TEUCHOS_TEST_FOR_EXCEPTION( vec_sv.subDim() != _thyra_vec_sv.subDim(), std::logic_error, "Error!" );
00132 #endif
00133     // Copy the elements
00134     for( int i = 0; i < vec_sv.subDim(); ++i )
00135       _thyra_vec_sv[i] = vec_sv[i];
00136     // Free/commit the explicit views
00137     vec->free_sub_vector( &vec_sv );
00138     _thyra_vec->commitDetachedView( &_thyra_vec_sv );
00139     // Set the output smart pointer
00140     *thyra_vec = _thyra_vec;
00141   }
00142   else {
00143     TEUCHOS_TEST_FOR_EXCEPTION(
00144       true, std::logic_error
00145       ,"AbstractLinAlgPack::get_thyra_vector(...): Error, the vector of concrete type \'"
00146       << typeName(vec) << "\' is not an incore vector."
00147       );
00148   }
00149 }
00150 
00151 void AbstractLinAlgPack::commit_thyra_vector(
00152   const VectorSpaceThyra                                         &thyra_vec_spc
00153   ,VectorMutable                                                 *vec
00154   ,Teuchos::RCP<Thyra::VectorBase<value_type> >          *thyra_vec_in
00155   )
00156 {
00157 #ifdef TEUCHOS_DEBUG
00158     TEUCHOS_TEST_FOR_EXCEPTION( vec==NULL || thyra_vec_in==NULL, std::invalid_argument, "Error!" );
00159 #endif
00160   Teuchos::RCP<Thyra::VectorBase<value_type> >  &thyra_vec = *thyra_vec_in;
00161   VectorMutableThyra *vmthyra_vec = dynamic_cast<VectorMutableThyra*>(vec);
00162   if(vmthyra_vec) {
00163     // We can just directly reset the Thyra vector
00164     vmthyra_vec->initialize(thyra_vec);
00165   }
00166   else if(thyra_vec_spc.is_in_core()) {
00167     // We need to copy back the temporary
00168     // Get explicit views of the elements
00169     RTOpPack::ConstSubVectorView<value_type> thyra_vec_sv;
00170     thyra_vec->acquireDetachedView( convert(Range1D()), &thyra_vec_sv );
00171     RTOpPack::MutableSubVector vec_sv;
00172     vec->get_sub_vector( Range1D(), &vec_sv );
00173 #ifdef TEUCHOS_DEBUG
00174     TEUCHOS_TEST_FOR_EXCEPTION( vec_sv.subDim() != thyra_vec_sv.subDim(), std::logic_error, "Error!" );
00175 #endif
00176     // Copy the elements
00177     for( int i = 0; i < vec_sv.subDim(); ++i )
00178       vec_sv[i] = thyra_vec_sv[i];
00179     // Free/commit the explicit views
00180     thyra_vec->releaseDetachedView( &thyra_vec_sv );
00181     vec->commit_sub_vector( &vec_sv );
00182     // Set the output smart pointer
00183     thyra_vec = Teuchos::null;
00184   }
00185   else {
00186     TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "Should never get here?." );
00187   }
00188 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines