65 virtual B& operator=(
const B&) = 0;
66 virtual void alpha(
double) = 0;
67 virtual double alpha()
const = 0;
72 B& operator=(
const B& b) {
73 const D* d =
dynamic_cast<const D*
>(&b);
75 throw std::logic_error(
"D::operator=(...) : b is not of type D");
79 void alpha(
double alpha) {
82 double alpha()
const {
99 bool success =
true, result;
101 if(out) *out << std::boolalpha;
104 *out<<
"\n\n******************************\n"
105 <<
"*** Testing AlgorithmState ***\n"
106 <<
"******************************\n"
107 <<
"\nWarning: this interface is weakly typed and mistakes"
108 <<
"can be very bad\n";
110 typedef double alpha_k_t;
111 typedef std::vector<double> x_k_t;
117 if(out) *out <<
"\n*** Create state object ...\n";
122 if(out) *out <<
"\n*** Set three types of iteration quantity access objects.\n";
124 if(out) *out <<
"set IterQuantityAccessContiguous<double>(2,\"alpha\")\n";
134 if(out) *out <<
"set IterQuantityAccessContiguous<std::vector<double> >(2,\"x\")\n";
144 if(out) *out <<
"set IterQuantityAccessDerivedToBase<B,D>(1,\"V\")\n";
158 if(out) *out <<
"\nTry to add \"x\" (should throw execption) : ";
173 if(out) *out <<
"Caught a AlgorithmState::AlreadyExists execption : " << expt.what() <<
" : true\n" << endl;
179 *out <<
"\n*** dump iteration quantitys\n";
184 if(out) *out <<
"\nstate.k() == 0 : " << result << endl;
188 if(out) *out <<
"\n*** Set iteration quantities for the kth iteration\n";
190 if(out) *out <<
"\nSet alpha_k(0) = 5.0 then get alpha_k(0) == 5.0 : ";
191 alpha_t *alpha =
dynamic_cast<alpha_t*
>( &state.
iter_quant(
"alpha") );
192 alpha->set_k(0) = 5.0;
193 alpha =
dynamic_cast<alpha_t*
>( &state.
iter_quant(
"alpha") );
195 if(out) *out << result << endl;
197 if(out) *out <<
"\nSet x_k(0)[0] = 2.0 then get x_k(0)[0] == 2.0 : ";
198 x_t *x =
dynamic_cast<x_t*
>( &state.
iter_quant(
"x") );
199 x->set_k(0).resize(1);
200 x->set_k(0)[0] = 2.0;
201 x =
dynamic_cast<x_t*
>( &state.
iter_quant(
"x") );
203 if(out) *out << result << endl;
205 if(out) *out <<
"\nSet V_k(0).alpha() = 3.0 then get V_k(0).alpha() == 3.0 : ";
206 V_t *V =
dynamic_cast<V_t*
>( &state.
iter_quant(
"V") );
207 V->set_k(0).alpha(3.0);
208 V =
dynamic_cast<V_t*
>( &state.
iter_quant(
"V") );
210 if(out) *out << result << endl;
214 if(out) *out <<
"\n*** Use an id to get at an iteration quantity\n";
216 if(out) *out <<
"\nid for \"x\" is : ";
218 if(out) *out << x_id << endl;
220 if(out) *out <<
"\nAccess \"x\" using id and x_k(0)[0] == 2.0 : ";
221 x =
dynamic_cast<x_t*
>( &state.
iter_quant(x_id) );
223 if(out) *out << result << endl;
227 if(out) *out <<
"\n*** Use a nonexistant name or id to access iteration quantity\n";
229 if(out) *out <<
"id for \"X\" is DOES_NOT_EXIST : ";
232 if(out) *out << result << endl;
234 if(out) *out <<
"\nAccess nonexistant iteration quantity by name \"X\" throws a AlgorithmState::DoesNotExist exception : ";
238 if(out) *out <<
false << endl;
241 if(out) *out <<
true << endl;
246 if(out) *out <<
"\nUse of a nonexistant id = 100 throws a AlgorithmState::DoesNotExist exception : ";
250 if(out) *out <<
false << endl;
253 if(out) *out <<
true << endl;
258 if(out) *out <<
"\n*** Update iteration quantities k+1 = k then check\n";
260 if(out) *out <<
"alpha_k(+1) = alpha_k(0)...\n";
261 alpha =
dynamic_cast<alpha_t*
>( &state.
iter_quant(
"alpha") );
263 alpha_k_t &alpha_k = alpha->get_k(0);
264 alpha->set_k(+1) = alpha_k;
267 if(out) *out <<
"x_k(+1) = x_k(0)...\n";
268 x =
dynamic_cast<x_t*
>( &state.
iter_quant(
"x") );
270 x_k_t &x_k = x->get_k(0);
274 if(out) *out <<
"V_k(+1) = V_k(0)...\n";
275 V =
dynamic_cast<V_t*
>( &state.
iter_quant(
"V") );
277 V_k_t &V_k = V->get_k(0);
281 if(out) *out <<
"shift reference from k to k+1...\n";
284 if(out) *out <<
"\nalpha_k(-1) == 5.0 : ";
285 alpha =
dynamic_cast<alpha_t*
>( &state.
iter_quant(
"alpha") );
287 if(out) *out << result << endl;
288 if(out) *out <<
"alpha_k(0) == 5.0 : ";
290 if(out) *out << result << endl;
292 if(out) *out <<
"\nx_k(-1)[0] == 2.0 : ";
293 x =
dynamic_cast<x_t*
>( &state.
iter_quant(
"x") );
295 if(out) *out << result << endl;
296 if(out) *out <<
"x_k(0)[0] == 2.0 : ";
298 if(out) *out << result << endl;
300 if(out) *out <<
"\nV_k(0).alpha() == 3.0 : ";
301 V =
dynamic_cast<V_t*
>( &state.
iter_quant(
"V") );
303 if(out) *out << result << endl;
307 if(out) *out <<
"\n*** Erasing iteration quantity \"x\" then trying to access it throws"
308 " a AlgorithmState::DoesNotExist exception : ";
312 if(out) *out <<
false << endl;
316 if(out) *out <<
true << endl;
323 *out <<
"\n*** Congradulations, all of the tests for AlgorihtmState"
324 " returned the expected results\n";
327 *out <<
"\n*** Oops, at least one of the above tests for AlgorihtmState"
328 " did not return the expected results ***\n";
335 catch(
const std::exception& excpt) {
336 if(out) *out <<
"\nCaught a std::exception: " <<
typeName(excpt) <<
" : " << excpt.what() << endl;
339 if(out) *out <<
"\nCaught an unknown exception\n";
342 if(out) *out <<
"\n*** Oops, If you read this some function throw an unexpected exception and the tests have failed!\n";
std::string typeName(const T &t)
virtual void erase_iter_quant(const std::string &iq_name)
Removes the iteration quantity with name iq_name.
Thrown if name or id does not exist.
virtual void dump_iter_quant(std::ostream &out) const
iteration quantity information dumping.
virtual iq_id_type set_iter_quant(const std::string &iq_name, const IQ_ptr &iq)
Inserts the iteration quantity through a RCP<...> object.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual IterQuantity & iter_quant(const std::string &iq_name)
Iteration quantity encapsulation object access with via iq_name.
Abstacts a set of iteration quantities for an iterative algorithm.
virtual iq_id_type get_iter_quant_id(const std::string &iq_name) const
Return the iteration quantity id (iq_id) for the iteration quantity.
Iteration Quanities subclass for contiguous iterations.
bool update_success(bool result_check, bool *success)
Helper function for updating a flag for if an operation returned false.
bool TestAlgorithmState(std::ostream *out)
Thrown if name already exists.
virtual void next_iteration(bool incr_k=true)
iteration quantity forwarding.