AbstractLinAlgPack_ThyraAccessors.cpp

00001 // ////////////////////////////////////////////////////
00002 // ThyraAccessors.cpp
00003 
00004 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
00005 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
00006 #include "Teuchos_dyn_cast.hpp"
00007 
00008 void AbstractLinAlgPack::get_thyra_vector(
00009   const VectorSpaceThyra                                         &thyra_vec_spc
00010   ,const Vector                                                  &vec
00011   ,Teuchos::RefCountPtr<const Thyra::VectorBase<value_type> >    *thyra_vec
00012   )
00013 {
00014 #ifdef TEUCHOS_DEBUG
00015     TEST_FOR_EXCEPTION( thyra_vec==NULL, std::invalid_argument, "Error!" );
00016 #endif
00017   const VectorMutableThyra *vmthyra_vec = dynamic_cast<const VectorMutableThyra*>(&vec);
00018   if(vmthyra_vec) {
00019     // We can just grap the const smart pointer to the underlying object 
00020     *thyra_vec = vmthyra_vec->thyra_vec();
00021   }
00022   else if(vec.space().is_in_core()) {
00023     // We need to create a temporary copy and then copy the explicit elements
00024     Teuchos::RefCountPtr<Thyra::VectorBase<value_type> >
00025       _thyra_vec = ::Thyra::createMember(thyra_vec_spc.thyra_vec_spc());
00026     // Get explicit views of the elements
00027     RTOpPack::SubVector vec_sv;
00028     vec.get_sub_vector( Range1D(), &vec_sv );
00029     RTOpPack::SubVectorView<value_type> _thyra_vec_sv;
00030     _thyra_vec->acquireDetachedView( convert(Range1D()), &_thyra_vec_sv );
00031 #ifdef TEUCHOS_DEBUG
00032     TEST_FOR_EXCEPTION( vec_sv.subDim() != _thyra_vec_sv.subDim(), std::logic_error, "Error!" );
00033 #endif
00034     // Copy the elements
00035     for( int i = 0; i < vec_sv.subDim(); ++i )
00036       _thyra_vec_sv[i] = vec_sv[i];
00037     // Free/commit the explicit views
00038     vec.free_sub_vector( &vec_sv );
00039     _thyra_vec->commitDetachedView( &_thyra_vec_sv );
00040     // Set the output smart pointer
00041     *thyra_vec = _thyra_vec;
00042   }
00043   else {
00044     TEST_FOR_EXCEPTION(
00045       true, std::logic_error
00046       ,"AbstractLinAlgPack::get_thyra_vector(...): Error, the vector of concrete type \'"
00047       << typeid(vec).name() << "\' is not an incore vector."
00048       );
00049   }
00050 }
00051 
00052 void AbstractLinAlgPack::free_thyra_vector(
00053   const VectorSpaceThyra                                         &thyra_vec_spc
00054   ,const Vector                                                  &vec
00055   ,Teuchos::RefCountPtr<const Thyra::VectorBase<value_type> >    *thyra_vec
00056   )
00057 {
00058 #ifdef TEUCHOS_DEBUG
00059     TEST_FOR_EXCEPTION( thyra_vec==NULL, std::invalid_argument, "Error!" );
00060 #endif
00061   *thyra_vec = Teuchos::null;  // This works in both cases above!
00062 }
00063 
00064 void AbstractLinAlgPack::get_thyra_vector(
00065   const VectorSpaceThyra                                         &thyra_vec_spc
00066   ,VectorMutable                                                 *vec
00067   ,Teuchos::RefCountPtr<Thyra::VectorBase<value_type> >          *thyra_vec
00068   )
00069 { 
00070 #ifdef TEUCHOS_DEBUG
00071     TEST_FOR_EXCEPTION( vec==NULL || thyra_vec==NULL, std::invalid_argument, "Error!" );
00072 #endif
00073   VectorMutableThyra *vmthyra_vec = dynamic_cast<VectorMutableThyra*>(vec);
00074   if(vmthyra_vec) {
00075     // We can just directly grap the Thyra vector
00076     *thyra_vec = vmthyra_vec->set_uninitialized();
00077   }
00078   else if(thyra_vec_spc.is_in_core()) {
00079     // We need to create a temporary copy and then copy the explicit elements
00080     Teuchos::RefCountPtr<Thyra::VectorBase<value_type> >
00081       _thyra_vec = ::Thyra::createMember(thyra_vec_spc.thyra_vec_spc());
00082     // Get explicit views of the elements
00083     RTOpPack::SubVector vec_sv;
00084     vec->get_sub_vector( Range1D(), &vec_sv );
00085     RTOpPack::SubVectorView<value_type> _thyra_vec_sv;
00086     _thyra_vec->acquireDetachedView( convert(Range1D()), &_thyra_vec_sv );
00087 #ifdef TEUCHOS_DEBUG
00088     TEST_FOR_EXCEPTION( vec_sv.subDim() != _thyra_vec_sv.subDim(), std::logic_error, "Error!" );
00089 #endif
00090     // Copy the elements
00091     for( int i = 0; i < vec_sv.subDim(); ++i )
00092       _thyra_vec_sv[i] = vec_sv[i];
00093     // Free/commit the explicit views
00094     vec->free_sub_vector( &vec_sv );
00095     _thyra_vec->commitDetachedView( &_thyra_vec_sv );
00096     // Set the output smart pointer
00097     *thyra_vec = _thyra_vec;
00098   }
00099   else {
00100     TEST_FOR_EXCEPTION(
00101       true, std::logic_error
00102       ,"AbstractLinAlgPack::get_thyra_vector(...): Error, the vector of concrete type \'"
00103       << typeid(vec).name() << "\' is not an incore vector."
00104       );
00105   }
00106 }
00107 
00108 void AbstractLinAlgPack::commit_thyra_vector(
00109   const VectorSpaceThyra                                         &thyra_vec_spc
00110   ,VectorMutable                                                 *vec
00111   ,Teuchos::RefCountPtr<Thyra::VectorBase<value_type> >          *thyra_vec_in
00112   )
00113 {
00114 #ifdef TEUCHOS_DEBUG
00115     TEST_FOR_EXCEPTION( vec==NULL || thyra_vec_in==NULL, std::invalid_argument, "Error!" );
00116 #endif
00117   Teuchos::RefCountPtr<Thyra::VectorBase<value_type> >  &thyra_vec = *thyra_vec_in;
00118   VectorMutableThyra *vmthyra_vec = dynamic_cast<VectorMutableThyra*>(vec);
00119   if(vmthyra_vec) {
00120     // We can just directly reset the Thyra vector
00121     vmthyra_vec->initialize(thyra_vec);
00122   }
00123   else if(thyra_vec_spc.is_in_core()) {
00124     // We need to copy back the temporary
00125     // Get explicit views of the elements
00126     RTOpPack::ConstSubVectorView<value_type> thyra_vec_sv;
00127     thyra_vec->acquireDetachedView( convert(Range1D()), &thyra_vec_sv );
00128     RTOpPack::MutableSubVector vec_sv;
00129     vec->get_sub_vector( Range1D(), &vec_sv );
00130 #ifdef TEUCHOS_DEBUG
00131     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       vec_sv[i] = thyra_vec_sv[i];
00136     // Free/commit the explicit views
00137     thyra_vec->releaseDetachedView( &thyra_vec_sv );
00138     vec->commit_sub_vector( &vec_sv );
00139     // Set the output smart pointer
00140     thyra_vec = Teuchos::null;
00141   }
00142   else {
00143     TEST_FOR_EXCEPTION( true, std::logic_error, "Should never get here?." );
00144   }
00145 }

Generated on Thu Sep 18 12:34:37 2008 for MOOCHO/Thyra Adapter Software by doxygen 1.3.9.1