MOOCHO/Thyra Adapter Software  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AbstractLinAlgPack_ThyraAccessors.cpp
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
6 // Copyright (2003) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
44 // ////////////////////////////////////////////////////
45 // ThyraAccessors.cpp
46 
47 #include "AbstractLinAlgPack_ThyraAccessors.hpp"
48 #include "AbstractLinAlgPack_VectorMutableThyra.hpp"
49 #include "Teuchos_dyn_cast.hpp"
50 
51 void AbstractLinAlgPack::get_thyra_vector(
52  const VectorSpaceThyra &thyra_vec_spc
53  ,const Vector &vec
55  )
56 {
57 #ifdef TEUCHOS_DEBUG
58  TEUCHOS_TEST_FOR_EXCEPTION( thyra_vec==NULL, std::invalid_argument, "Error!" );
59 #endif
60  const VectorMutableThyra *vmthyra_vec = dynamic_cast<const VectorMutableThyra*>(&vec);
61  if(vmthyra_vec) {
62  // We can just grap the const smart pointer to the underlying object
63  *thyra_vec = vmthyra_vec->thyra_vec();
64  }
65  else if(vec.space().is_in_core()) {
66  // We need to create a temporary copy and then copy the explicit elements
68  _thyra_vec = ::Thyra::createMember(thyra_vec_spc.thyra_vec_spc());
69  // Get explicit views of the elements
70  RTOpPack::SubVector vec_sv;
71  vec.get_sub_vector( Range1D(), &vec_sv );
73  _thyra_vec->acquireDetachedView( convert(Range1D()), &_thyra_vec_sv );
74 #ifdef TEUCHOS_DEBUG
75  TEUCHOS_TEST_FOR_EXCEPTION( vec_sv.subDim() != _thyra_vec_sv.subDim(), std::logic_error, "Error!" );
76 #endif
77  // Copy the elements
78  for( int i = 0; i < vec_sv.subDim(); ++i )
79  _thyra_vec_sv[i] = vec_sv[i];
80  // Free/commit the explicit views
81  vec.free_sub_vector( &vec_sv );
82  _thyra_vec->commitDetachedView( &_thyra_vec_sv );
83  // Set the output smart pointer
84  *thyra_vec = _thyra_vec;
85  }
86  else {
88  true, std::logic_error
89  ,"AbstractLinAlgPack::get_thyra_vector(...): Error, the vector of concrete type \'"
90  << typeName(vec) << "\' is not an incore vector."
91  );
92  }
93 }
94 
95 void AbstractLinAlgPack::free_thyra_vector(
96  const VectorSpaceThyra &thyra_vec_spc
97  ,const Vector &vec
99  )
100 {
101 #ifdef TEUCHOS_DEBUG
102  TEUCHOS_TEST_FOR_EXCEPTION( thyra_vec==NULL, std::invalid_argument, "Error!" );
103 #endif
104  *thyra_vec = Teuchos::null; // This works in both cases above!
105 }
106 
107 void AbstractLinAlgPack::get_thyra_vector(
108  const VectorSpaceThyra &thyra_vec_spc
109  ,VectorMutable *vec
111  )
112 {
113 #ifdef TEUCHOS_DEBUG
114  TEUCHOS_TEST_FOR_EXCEPTION( vec==NULL || thyra_vec==NULL, std::invalid_argument, "Error!" );
115 #endif
116  VectorMutableThyra *vmthyra_vec = dynamic_cast<VectorMutableThyra*>(vec);
117  if(vmthyra_vec) {
118  // We can just directly grap the Thyra vector
119  *thyra_vec = vmthyra_vec->set_uninitialized();
120  }
121  else if(thyra_vec_spc.is_in_core()) {
122  // We need to create a temporary copy and then copy the explicit elements
124  _thyra_vec = ::Thyra::createMember(thyra_vec_spc.thyra_vec_spc());
125  // Get explicit views of the elements
126  RTOpPack::SubVector vec_sv;
127  vec->get_sub_vector( Range1D(), &vec_sv );
129  _thyra_vec->acquireDetachedView( convert(Range1D()), &_thyra_vec_sv );
130 #ifdef TEUCHOS_DEBUG
131  TEUCHOS_TEST_FOR_EXCEPTION( vec_sv.subDim() != _thyra_vec_sv.subDim(), std::logic_error, "Error!" );
132 #endif
133  // Copy the elements
134  for( int i = 0; i < vec_sv.subDim(); ++i )
135  _thyra_vec_sv[i] = vec_sv[i];
136  // Free/commit the explicit views
137  vec->free_sub_vector( &vec_sv );
138  _thyra_vec->commitDetachedView( &_thyra_vec_sv );
139  // Set the output smart pointer
140  *thyra_vec = _thyra_vec;
141  }
142  else {
144  true, std::logic_error
145  ,"AbstractLinAlgPack::get_thyra_vector(...): Error, the vector of concrete type \'"
146  << typeName(vec) << "\' is not an incore vector."
147  );
148  }
149 }
150 
151 void AbstractLinAlgPack::commit_thyra_vector(
152  const VectorSpaceThyra &thyra_vec_spc
153  ,VectorMutable *vec
155  )
156 {
157 #ifdef TEUCHOS_DEBUG
158  TEUCHOS_TEST_FOR_EXCEPTION( vec==NULL || thyra_vec_in==NULL, std::invalid_argument, "Error!" );
159 #endif
160  Teuchos::RCP<Thyra::VectorBase<value_type> > &thyra_vec = *thyra_vec_in;
161  VectorMutableThyra *vmthyra_vec = dynamic_cast<VectorMutableThyra*>(vec);
162  if(vmthyra_vec) {
163  // We can just directly reset the Thyra vector
164  vmthyra_vec->initialize(thyra_vec);
165  }
166  else if(thyra_vec_spc.is_in_core()) {
167  // We need to copy back the temporary
168  // Get explicit views of the elements
170  thyra_vec->acquireDetachedView( convert(Range1D()), &thyra_vec_sv );
171  RTOpPack::MutableSubVector vec_sv;
172  vec->get_sub_vector( Range1D(), &vec_sv );
173 #ifdef TEUCHOS_DEBUG
174  TEUCHOS_TEST_FOR_EXCEPTION( vec_sv.subDim() != thyra_vec_sv.subDim(), std::logic_error, "Error!" );
175 #endif
176  // Copy the elements
177  for( int i = 0; i < vec_sv.subDim(); ++i )
178  vec_sv[i] = thyra_vec_sv[i];
179  // Free/commit the explicit views
180  thyra_vec->releaseDetachedView( &thyra_vec_sv );
181  vec->commit_sub_vector( &vec_sv );
182  // Set the output smart pointer
183  thyra_vec = Teuchos::null;
184  }
185  else {
186  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "Should never get here?." );
187  }
188 }
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
std::string typeName(const T &t)
Teuchos::Range1D Range1D