EpetraExt  Development
EpetraExt_ModelEvaluatorScalingTools.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 
42 
44 #include "Teuchos_implicit_cast.hpp"
45 #include "Epetra_RowMatrix.h"
46 
47 //
48 // Here in the implementation of scaling we write all scaling routines to
49 // specifically and individually handle every input and output object and we
50 // transfer all objects from one [In,Out]Arg container to another to make sure
51 // that that object has been specifically addressed. We could just use the
52 // [In,Out]Args::setArgs(...) function to copy all arguments be default but
53 // then that would setup subtle errors where a new quality could be silently
54 // ignored and not be scaled correctly. Therefore, we feel it is better to
55 // have to deal with *every* input and output object specifically with respect
56 // to scaling in order to avoid overlooking scaling. In this way, if a new
57 // input or output object is added to [In,Out]Args but the code in this file
58 // is not updated, then that object will not be passed through and some type
59 // of error will be generated right away. We feel this is the best behavior
60 // and it justifies having every scaling-related function take both an input
61 // and an output [In,Out]Args object and transferring the objects specifically.
62 //
63 
64 namespace {
65 
66 
67 const std::string fwdScalingVecName = "fwdScalingVec";
68 
69 
70 // Assert that the input scaling vectors have been set up correctly
71 void assertModelVarScalings(
72  const EpetraExt::ModelEvaluator::InArgs &varScalings
73  )
74 {
75  typedef EpetraExt::ModelEvaluator EME;
76  TEUCHOS_TEST_FOR_EXCEPTION(
77  (varScalings.supports(EME::IN_ARG_x) && varScalings.supports(EME::IN_ARG_x_dot))
78  && (varScalings.get_x() != varScalings.get_x_dot()),
79  std::logic_error,
80  "Error, if scaling for x is given and x_dot is supported, then\n"
81  "the scaling for x_dot must also be set and must be the same scaling\n"
82  "as is used for x!"
83  );
84 }
85 
86 
87 
88 // Scale a single vector using a templated policy object to take care of what
89 // vector gets used.
90 template<class InArgsVectorGetterSetter>
91 void scaleModelVar(
92  InArgsVectorGetterSetter vecGetterSetter, // Templated policy object!
93  const EpetraExt::ModelEvaluator::InArgs &origVars,
94  const EpetraExt::ModelEvaluator::InArgs &varScalings,
96  Teuchos::FancyOStream *out,
97  Teuchos::EVerbosityLevel verbLevel
98  )
99 {
100 
101  using Teuchos::null;
102  using Teuchos::rcp;
103  using Teuchos::RCP;
104  using Teuchos::Ptr;
105  using Teuchos::rcp_const_cast;
106 
107 
108 #ifdef TEUCHOS_DEBUG
109  TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
110 #endif
111 
112  RCP<const Epetra_Vector>
113  orig_vec = vecGetterSetter.getVector(origVars);
114  if ( !is_null(orig_vec) ) {
115  RCP<const Epetra_Vector>
116  inv_s_vec = vecGetterSetter.getVector(varScalings);
117  if ( !is_null(inv_s_vec) ) {
118  RCP<Epetra_Vector>
119  scaled_vec = rcp_const_cast<Epetra_Vector>(
120  vecGetterSetter.getVector(*scaledVars) );
121  if ( is_null(scaled_vec) )
122  scaled_vec = rcp(new Epetra_Vector(orig_vec->Map()));
123  // See if there is a "hidden" forward scaling vector to use
124  Ptr<const RCP<const Epetra_Vector> > fwd_s_vec =
125  Teuchos::getOptionalEmbeddedObj<Epetra_Vector, RCP<const Epetra_Vector> >(
126  inv_s_vec);
127 /*
128  Teuchos::get_optional_extra_data<const RCP<const Epetra_Vector> >(
129  inv_s_vec, fwdScalingVecName );
130 */
131  if ( !is_null(fwd_s_vec) ) {
132  // Use the "hidden" forward scaling vector and multiply
133  scaled_vec->Multiply( 1.0, **fwd_s_vec, *orig_vec, 0.0 );
134  }
135  else {
136  // Just use the inverse scaling vector and divide
138  *orig_vec, *inv_s_vec, &*scaled_vec );
139  }
140  vecGetterSetter.setVector( scaled_vec, scaledVars );
141  }
142  else {
143  vecGetterSetter.setVector( orig_vec, scaledVars );
144  }
145  }
146  else {
147  vecGetterSetter.setVector( null, scaledVars );
148  }
149 
150 }
151 
152 
153 // Scale variable bounds for a single vector using a templated policy object
154 // to take care of what vector gets used.
155 template<class InArgsVectorGetterSetter>
156 void scaleModelBound(
157  InArgsVectorGetterSetter vecGetterSetter, // Templated policy object!
158  const EpetraExt::ModelEvaluator::InArgs &origLowerBounds,
159  const EpetraExt::ModelEvaluator::InArgs &origUpperBounds,
160  const double infBnd,
161  const EpetraExt::ModelEvaluator::InArgs &varScalings,
162  EpetraExt::ModelEvaluator::InArgs *scaledLowerBounds,
163  EpetraExt::ModelEvaluator::InArgs *scaledUpperBounds,
164  Teuchos::FancyOStream *out,
165  Teuchos::EVerbosityLevel verbLevel
166  )
167 {
168 
169  using Teuchos::null;
170  using Teuchos::rcp;
171  using Teuchos::RCP;
172  using Teuchos::rcp_const_cast;
173 
174 #ifdef TEUCHOS_DEBUG
175  TEUCHOS_TEST_FOR_EXCEPT(!scaledLowerBounds);
176  TEUCHOS_TEST_FOR_EXCEPT(!scaledUpperBounds);
177 #endif
178 
179  RCP<const Epetra_Vector>
180  orig_lower_vec = vecGetterSetter.getVector(origLowerBounds);
181  if ( !is_null(orig_lower_vec) ) {
182  RCP<const Epetra_Vector>
183  inv_s_vec = vecGetterSetter.getVector(varScalings);
184  if ( !is_null(inv_s_vec) ) {
185  TEUCHOS_TEST_FOR_EXCEPT("Can't handle scaling bounds yet!");
186  }
187  else {
188  vecGetterSetter.setVector( orig_lower_vec, scaledLowerBounds );
189  }
190  }
191  else {
192  vecGetterSetter.setVector( null, scaledLowerBounds );
193  }
194 
195  RCP<const Epetra_Vector>
196  orig_upper_vec = vecGetterSetter.getVector(origUpperBounds);
197  if ( !is_null(orig_upper_vec) ) {
198  RCP<const Epetra_Vector>
199  inv_s_vec = vecGetterSetter.getVector(varScalings);
200  if ( !is_null(inv_s_vec) ) {
201  TEUCHOS_TEST_FOR_EXCEPT("Can't handle scaling bounds yet!");
202  }
203  else {
204  vecGetterSetter.setVector( orig_upper_vec, scaledUpperBounds );
205  }
206  }
207  else {
208  vecGetterSetter.setVector( null, scaledUpperBounds );
209  }
210 
211 }
212 
213 
214 // Unscale a single vector using a templated policy object to take care of
215 // what vector gets used.
216 template<class InArgsVectorGetterSetter>
217 void unscaleModelVar(
218  InArgsVectorGetterSetter vecGetterSetter, // Templated policy object!
219  const EpetraExt::ModelEvaluator::InArgs &scaledVars,
220  const EpetraExt::ModelEvaluator::InArgs &varScalings,
222  Teuchos::FancyOStream *out,
223  Teuchos::EVerbosityLevel verbLevel
224  )
225 {
226 
227  using Teuchos::null;
228  using Teuchos::rcp;
229  using Teuchos::RCP;
230  using Teuchos::rcp_const_cast;
231  using Teuchos::includesVerbLevel;
232 
233 
234 #ifdef TEUCHOS_DEBUG
235  TEUCHOS_TEST_FOR_EXCEPT(!origVars);
236 #endif
237 
238  RCP<const Epetra_Vector>
239  scaled_vec = vecGetterSetter.getVector(scaledVars);
240  if ( !is_null(scaled_vec) ) {
241  RCP<const Epetra_Vector>
242  inv_s_vec = vecGetterSetter.getVector(varScalings);
243  if ( !is_null(inv_s_vec) ) {
244  RCP<Epetra_Vector>
245  orig_vec = rcp_const_cast<Epetra_Vector>(
246  vecGetterSetter.getVector(*origVars) );
247  if ( is_null(orig_vec) )
248  orig_vec = rcp(new Epetra_Vector(scaled_vec->Map()));
250  *scaled_vec, *inv_s_vec, &*orig_vec );
251  if (out && includesVerbLevel(verbLevel,Teuchos::VERB_HIGH)) {
252  *out << "\nUnscaled vector "<<vecGetterSetter.getName()<<":\n";
253  Teuchos::OSTab tab(*out);
254  orig_vec->Print(*out);
255  }
256  vecGetterSetter.setVector( orig_vec, origVars );
257  }
258  else {
259  vecGetterSetter.setVector( scaled_vec, origVars );
260  }
261  }
262  else {
263  vecGetterSetter.setVector( null, origVars );
264  }
265 
266 }
267 
268 
269 // Scale a single vector using a templated policy object to take care of what
270 // vector gets used.
271 template<class OutArgsVectorGetterSetter>
272 void scaleModelFunc(
273  OutArgsVectorGetterSetter vecGetterSetter, // Templated policy object!
274  const EpetraExt::ModelEvaluator::OutArgs &origFuncs,
275  const EpetraExt::ModelEvaluator::OutArgs &funcScalings,
277  Teuchos::FancyOStream *out,
278  Teuchos::EVerbosityLevel verbLevel
279  )
280 {
281  TEUCHOS_TEST_FOR_EXCEPT(0==scaledFuncs);
282  Teuchos::RCP<Epetra_Vector>
283  func = vecGetterSetter.getVector(origFuncs);
284  if (!is_null(func) ) {
285  Teuchos::RCP<const Epetra_Vector>
286  funcScaling = vecGetterSetter.getVector(funcScalings);
287  if (!is_null(funcScaling) ) {
288  EpetraExt::scaleModelFuncGivenForwardScaling( *funcScaling, &*func );
289  }
290  }
291  vecGetterSetter.setVector( func, scaledFuncs );
292 }
293 
294 
295 } // namespace
296 
297 
299  const ModelEvaluator &model,
300  ModelEvaluator::InArgs *nominalValues
301  )
302 {
303 
304  using Teuchos::implicit_cast;
305  typedef ModelEvaluator EME;
306 
307 #ifdef TEUCHOS_DEBUG
308  TEUCHOS_TEST_FOR_EXCEPT(!nominalValues);
309 #endif
310 
311  *nominalValues = model.createInArgs();
312 
313  if(nominalValues->supports(EME::IN_ARG_x)) {
314  nominalValues->set_x(model.get_x_init());
315  }
316 
317  if(nominalValues->supports(EME::IN_ARG_x_dot)) {
318  nominalValues->set_x_dot(model.get_x_dot_init());
319  }
320 
321  for( int l = 0; l < nominalValues->Np(); ++l ) {
322  nominalValues->set_p( l, model.get_p_init(l) );
323  }
324 
325  if(nominalValues->supports(EME::IN_ARG_t)) {
326  nominalValues->set_t(model.get_t_init());
327  }
328 
329 }
330 
331 
333  const ModelEvaluator &model,
334  ModelEvaluator::InArgs *lowerBounds,
335  ModelEvaluator::InArgs *upperBounds
336  )
337 {
338 
339  using Teuchos::implicit_cast;
340  typedef ModelEvaluator EME;
341 
342 #ifdef TEUCHOS_DEBUG
343  TEUCHOS_TEST_FOR_EXCEPT(!lowerBounds);
344  TEUCHOS_TEST_FOR_EXCEPT(!upperBounds);
345 #endif
346 
347  *lowerBounds = model.createInArgs();
348  *upperBounds = model.createInArgs();
349 
350  if(lowerBounds->supports(EME::IN_ARG_x)) {
351  lowerBounds->set_x(model.get_x_lower_bounds());
352  upperBounds->set_x(model.get_x_upper_bounds());
353  }
354 
355  for( int l = 0; l < lowerBounds->Np(); ++l ) {
356  lowerBounds->set_p( l, model.get_p_lower_bounds(l) );
357  upperBounds->set_p( l, model.get_p_upper_bounds(l) );
358  }
359 
360  if(lowerBounds->supports(EME::IN_ARG_t)) {
361  lowerBounds->set_t(model.get_t_lower_bound());
362  upperBounds->set_t(model.get_t_upper_bound());
363  }
364 
365 }
366 
367 
369  const ModelEvaluator::InArgs &origVars,
370  const ModelEvaluator::InArgs &varScalings,
371  ModelEvaluator::InArgs *scaledVars,
372  Teuchos::FancyOStream *out,
373  Teuchos::EVerbosityLevel verbLevel
374  )
375 {
376  typedef ModelEvaluator EME;
377 
378 #ifdef TEUCHOS_DEBUG
379  TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
380  assertModelVarScalings(varScalings);
381 #endif
382 
383  if (origVars.supports(EME::IN_ARG_x)) {
384  scaleModelVar( InArgsGetterSetter_x(), origVars, varScalings, scaledVars,
385  out, verbLevel );
386  }
387 
388  if (origVars.supports(EME::IN_ARG_x_dot)) {
389  scaleModelVar( InArgsGetterSetter_x_dot(), origVars, varScalings, scaledVars,
390  out, verbLevel );
391  }
392 
393  const int Np = origVars.Np();
394  for ( int l = 0; l < Np; ++l ) {
395  scaleModelVar( InArgsGetterSetter_p(l), origVars, varScalings, scaledVars,
396  out, verbLevel );
397  }
398 
399  if (origVars.supports(EME::IN_ARG_x_poly)) {
400  TEUCHOS_TEST_FOR_EXCEPTION(
401  !is_null(varScalings.get_x()), std::logic_error,
402  "Error, can't hanlde scaling of x_poly yet!"
403  );
404  scaledVars->set_x_poly(origVars.get_x_poly());
405  }
406 
407  if (origVars.supports(EME::IN_ARG_x_dot_poly)) {
408  TEUCHOS_TEST_FOR_EXCEPTION(
409  !is_null(varScalings.get_x()), std::logic_error,
410  "Error, can't hanlde scaling of x_dot_poly yet!"
411  );
412  scaledVars->set_x_dot_poly(origVars.get_x_dot_poly());
413  }
414 
415  if (origVars.supports(EME::IN_ARG_t)) {
416  TEUCHOS_TEST_FOR_EXCEPTION(
417  varScalings.get_t() > 0.0, std::logic_error,
418  "Error, can't hanlde scaling of t yet!"
419  );
420  scaledVars->set_t(origVars.get_t());
421  }
422 
423  if (origVars.supports(EME::IN_ARG_alpha)) {
424  TEUCHOS_TEST_FOR_EXCEPTION(
425  varScalings.get_alpha() > 0.0, std::logic_error,
426  "Error, can't hanlde scaling of alpha yet!"
427  );
428  scaledVars->set_alpha(origVars.get_alpha());
429  }
430 
431  if (origVars.supports(EME::IN_ARG_beta)) {
432  TEUCHOS_TEST_FOR_EXCEPTION(
433  varScalings.get_beta() > 0.0, std::logic_error,
434  "Error, can't hanlde scaling of beta yet!"
435  );
436  scaledVars->set_beta(origVars.get_beta());
437  }
438 
439  // ToDo: Support other input arguments?
440 
441 }
442 
443 
445  const ModelEvaluator::InArgs &origLowerBounds,
446  const ModelEvaluator::InArgs &origUpperBounds,
447  const double infBnd,
448  const ModelEvaluator::InArgs &varScalings,
449  ModelEvaluator::InArgs *scaledLowerBounds,
450  ModelEvaluator::InArgs *scaledUpperBounds,
451  Teuchos::FancyOStream *out,
452  Teuchos::EVerbosityLevel verbLevel
453  )
454 {
455 
456  typedef ModelEvaluator EME;
457 
458 #ifdef TEUCHOS_DEBUG
459  TEUCHOS_TEST_FOR_EXCEPT(!scaledLowerBounds);
460  TEUCHOS_TEST_FOR_EXCEPT(!scaledUpperBounds);
461  assertModelVarScalings(varScalings);
462 #endif
463 
464  if (origLowerBounds.supports(EME::IN_ARG_x)) {
465  scaleModelBound(
466  InArgsGetterSetter_x(), origLowerBounds, origUpperBounds, infBnd,
467  varScalings, scaledLowerBounds, scaledUpperBounds,
468  out, verbLevel );
469  }
470 
471  if (origLowerBounds.supports(EME::IN_ARG_x_dot)) {
472  scaleModelBound(
473  InArgsGetterSetter_x_dot(), origLowerBounds, origUpperBounds, infBnd,
474  varScalings, scaledLowerBounds, scaledUpperBounds,
475  out, verbLevel );
476  }
477 
478  const int Np = origLowerBounds.Np();
479  for ( int l = 0; l < Np; ++l ) {
480  scaleModelBound(
481  InArgsGetterSetter_p(l), origLowerBounds, origUpperBounds, infBnd,
482  varScalings, scaledLowerBounds, scaledUpperBounds,
483  out, verbLevel );
484  }
485 
486  // ToDo: Support other input arguments?
487 
488 }
489 
490 
492  const ModelEvaluator::InArgs &scaledVars,
493  const ModelEvaluator::InArgs &varScalings,
494  ModelEvaluator::InArgs *origVars,
495  Teuchos::FancyOStream *out,
496  Teuchos::EVerbosityLevel verbLevel
497  )
498 {
499 
500  using Teuchos::RCP;
501  using Teuchos::includesVerbLevel;
502  typedef ModelEvaluator EME;
503 
504 #ifdef TEUCHOS_DEBUG
505  TEUCHOS_TEST_FOR_EXCEPT(!origVars);
506  assertModelVarScalings(varScalings);
507 #endif
508 
509  // Print scaling vectors
510 
511  if (out && includesVerbLevel(verbLevel,Teuchos::VERB_HIGH)) {
512  RCP<const Epetra_Vector> inv_s_x;
513  if ( scaledVars.supports(EME::IN_ARG_x) &&
514  !is_null(inv_s_x=varScalings.get_x()) )
515  {
516  *out << "\nState inverse scaling vector inv_s_x:\n";
517  Teuchos::OSTab tab(*out);
518  inv_s_x->Print(*out);
519  }
520  }
521 
522  // Scal the input varaibles
523 
524  if (scaledVars.supports(EME::IN_ARG_x_dot)) {
525  unscaleModelVar( InArgsGetterSetter_x_dot(), scaledVars, varScalings, origVars,
526  out, verbLevel );
527  }
528 
529  if (scaledVars.supports(EME::IN_ARG_x)) {
530  unscaleModelVar( InArgsGetterSetter_x(), scaledVars, varScalings, origVars,
531  out, verbLevel );
532  }
533 
534  const int Np = scaledVars.Np();
535  for ( int l = 0; l < Np; ++l ) {
536  unscaleModelVar( InArgsGetterSetter_p(l), scaledVars, varScalings, origVars,
537  out, verbLevel );
538  }
539 
540  if (scaledVars.supports(EME::IN_ARG_x_dot_poly)) {
541  TEUCHOS_TEST_FOR_EXCEPTION(
542  !is_null(varScalings.get_x()), std::logic_error,
543  "Error, can't hanlde unscaling of x_dot_poly yet!"
544  );
545  origVars->set_x_dot_poly(scaledVars.get_x_dot_poly());
546  }
547 
548  if (scaledVars.supports(EME::IN_ARG_x_poly)) {
549  TEUCHOS_TEST_FOR_EXCEPTION(
550  !is_null(varScalings.get_x()), std::logic_error,
551  "Error, can't hanlde unscaling of x_poly yet!"
552  );
553  origVars->set_x_poly(scaledVars.get_x_poly());
554  }
555 
556  if (scaledVars.supports(EME::IN_ARG_t)) {
557  TEUCHOS_TEST_FOR_EXCEPTION(
558  varScalings.get_t() > 0.0, std::logic_error,
559  "Error, can't hanlde unscaling of t yet!"
560  );
561  origVars->set_t(scaledVars.get_t());
562  }
563 
564  if (scaledVars.supports(EME::IN_ARG_alpha)) {
565  TEUCHOS_TEST_FOR_EXCEPTION(
566  varScalings.get_alpha() > 0.0, std::logic_error,
567  "Error, can't hanlde unscaling of alpha yet!"
568  );
569  origVars->set_alpha(scaledVars.get_alpha());
570  }
571 
572  if (scaledVars.supports(EME::IN_ARG_beta)) {
573  TEUCHOS_TEST_FOR_EXCEPTION(
574  varScalings.get_beta() > 0.0, std::logic_error,
575  "Error, can't hanlde unscaling of beta yet!"
576  );
577  origVars->set_beta(scaledVars.get_beta());
578  }
579 
580 }
581 
582 
584  const ModelEvaluator::OutArgs &origFuncs,
585  const ModelEvaluator::InArgs &varScalings,
586  const ModelEvaluator::OutArgs &funcScalings,
587  ModelEvaluator::OutArgs *scaledFuncs,
588  bool *allFuncsWhereScaled,
589  Teuchos::FancyOStream *out,
590  Teuchos::EVerbosityLevel verbLevel
591  )
592 {
593 
594  using Teuchos::RCP;
595  typedef ModelEvaluator EME;
596 
597  TEUCHOS_TEST_FOR_EXCEPT(0==allFuncsWhereScaled);
598 
599  *allFuncsWhereScaled = true;
600 
601  const int Np = origFuncs.Np();
602  const int Ng = origFuncs.Ng();
603 
604  // f
605  if ( origFuncs.supports(EME::OUT_ARG_f) && !is_null(origFuncs.get_f()) ) {
606  scaleModelFunc( OutArgsGetterSetter_f(), origFuncs, funcScalings, scaledFuncs,
607  out, verbLevel );
608  }
609 
610  // f_poly
611  if (
612  origFuncs.supports(EME::OUT_ARG_f_poly)
613  && !is_null(origFuncs.get_f_poly())
614  )
615  {
616  TEUCHOS_TEST_FOR_EXCEPTION(
617  !is_null(funcScalings.get_f()), std::logic_error,
618  "Error, we can't handle scaling of f_poly yet!"
619  );
620  scaledFuncs->set_f_poly(origFuncs.get_f_poly());
621  }
622 
623  // g(j)
624  for ( int j = 0; j < Ng; ++j ) {
625  scaleModelFunc( OutArgsGetterSetter_g(j), origFuncs, funcScalings, scaledFuncs,
626  out, verbLevel );
627  }
628 
629  // W
630  RCP<Epetra_Operator> W;
631  if ( origFuncs.supports(EME::OUT_ARG_W) && !is_null(W=origFuncs.get_W()) ) {
632  bool didScaling = false;
634  varScalings.get_x().get(), funcScalings.get_f().get(),
635  &*W, &didScaling
636  );
637  if(didScaling)
638  scaledFuncs->set_W(W);
639  else
640  *allFuncsWhereScaled = false;
641  }
642 
643  // DfDp(l)
644  for ( int l = 0; l < Np; ++l ) {
645  EME::Derivative orig_DfDp_l;
646  if (
647  !origFuncs.supports(EME::OUT_ARG_DfDp,l).none()
648  && !(orig_DfDp_l=origFuncs.get_DfDp(l)).isEmpty()
649  )
650  {
651  EME::Derivative scaled_DfDp_l;
652  bool didScaling = false;
654  orig_DfDp_l, varScalings.get_p(l).get(), funcScalings.get_f().get(),
655  &scaled_DfDp_l, &didScaling
656  );
657  if(didScaling)
658  scaledFuncs->set_DfDp(l,scaled_DfDp_l);
659  else
660  *allFuncsWhereScaled = false;
661  }
662 
663  }
664 
665  // DgDx_dot(j), DgDx(j), and DgDp(j,l)
666  for ( int j = 0; j < Ng; ++j ) {
667 
668  EME::Derivative orig_DgDx_dot_j;
669  if (
670  !origFuncs.supports(EME::OUT_ARG_DgDx_dot,j).none()
671  && !(orig_DgDx_dot_j=origFuncs.get_DgDx_dot(j)).isEmpty()
672  )
673  {
674  EME::Derivative scaled_DgDx_dot_j;
675  bool didScaling = false;
677  orig_DgDx_dot_j, varScalings.get_x().get(), funcScalings.get_g(j).get(),
678  &scaled_DgDx_dot_j, &didScaling
679  );
680  if(didScaling)
681  scaledFuncs->set_DgDx_dot(j,scaled_DgDx_dot_j);
682  else
683  *allFuncsWhereScaled = false;
684  }
685 
686  EME::Derivative orig_DgDx_j;
687  if (
688  !origFuncs.supports(EME::OUT_ARG_DgDx,j).none()
689  && !(orig_DgDx_j=origFuncs.get_DgDx(j)).isEmpty()
690  )
691  {
692  EME::Derivative scaled_DgDx_j;
693  bool didScaling = false;
695  orig_DgDx_j, varScalings.get_x().get(), funcScalings.get_g(j).get(),
696  &scaled_DgDx_j, &didScaling
697  );
698  if(didScaling)
699  scaledFuncs->set_DgDx(j,scaled_DgDx_j);
700  else
701  *allFuncsWhereScaled = false;
702  }
703 
704  for ( int l = 0; l < Np; ++l ) {
705  EME::Derivative orig_DgDp_j_l;
706  if (
707  !origFuncs.supports(EME::OUT_ARG_DgDp,j,l).none()
708  && !(orig_DgDp_j_l=origFuncs.get_DgDp(j,l)).isEmpty()
709  )
710  {
711  EME::Derivative scaled_DgDp_j_l;
712  bool didScaling = false;
714  orig_DgDp_j_l, varScalings.get_p(l).get(), funcScalings.get_g(j).get(),
715  &scaled_DgDp_j_l, &didScaling
716  );
717  if(didScaling)
718  scaledFuncs->set_DgDp(j,l,scaled_DgDp_j_l);
719  else
720  *allFuncsWhereScaled = false;
721  }
722  }
723  }
724 
725 }
726 
727 
728 Teuchos::RCP<const Epetra_Vector>
730  Teuchos::RCP<const Epetra_Vector> const& scalingVector
731  )
732 {
733  Teuchos::RCP<Epetra_Vector> invScalingVector =
734  Teuchos::rcpWithEmbeddedObj(
735  new Epetra_Vector(scalingVector->Map()),
736  scalingVector
737  );
738  invScalingVector->Reciprocal(*scalingVector);
739  return invScalingVector;
740  // Above, we embedd the forward scaling vector. This is done in order to
741  // achieve the exact same numerics as before this refactoring and to improve
742  // runtime speed and accruacy.
743 }
744 
745 
747  const Epetra_Vector &origVars,
748  const Epetra_Vector &invVarScaling,
749  Epetra_Vector *scaledVars
750  )
751 {
752 #ifdef TEUCHOS_DEBUG
753  TEUCHOS_TEST_FOR_EXCEPT(!scaledVars);
754  TEUCHOS_TEST_FOR_EXCEPT(!origVars.Map().SameAs(invVarScaling.Map()));
755  TEUCHOS_TEST_FOR_EXCEPT(!origVars.Map().SameAs(scaledVars->Map()));
756 #endif
757  const int localDim = origVars.Map().NumMyElements();
758  for ( int i = 0; i < localDim; ++i )
759  (*scaledVars)[i] = origVars[i] / invVarScaling[i];
760 }
761 
762 
764  const Epetra_Vector &origLowerBounds,
765  const Epetra_Vector &origUpperBounds,
766  const double infBnd,
767  const Epetra_Vector &invVarScaling,
768  Epetra_Vector *scaledLowerBounds,
769  Epetra_Vector *scaledUpperBounds
770  )
771 {
772  TEUCHOS_TEST_FOR_EXCEPT("ToDo: Implement!");
773 }
774 
775 
777  const Epetra_Vector &origVars,
778  const Epetra_Vector &invVarScaling,
779  Epetra_Vector *scaledVars
780  )
781 {
782  TEUCHOS_TEST_FOR_EXCEPT(0==scaledVars);
783  scaledVars->Multiply( 1.0, invVarScaling, origVars, 0.0 );
784 }
785 
786 
788  const Epetra_Vector &fwdFuncScaling,
789  Epetra_Vector *funcs
790  )
791 {
792  TEUCHOS_TEST_FOR_EXCEPT(0==funcs);
793  funcs->Multiply( 1.0, fwdFuncScaling, *funcs, 0.0 );
794  // Note: Above is what Epetra_LinearProblem does to scale the RHS and LHS
795  // vectors so this type of argument aliasing must be okay in Epetra!
796 }
797 
798 
800  const Epetra_Vector *invVarScaling,
801  const Epetra_Vector *fwdFuncScaling,
802  Epetra_Operator *funcDerivOp,
803  bool *didScaling
804  )
805 {
806  TEUCHOS_TEST_FOR_EXCEPT(0==funcDerivOp);
807  TEUCHOS_TEST_FOR_EXCEPT(0==didScaling);
808  *didScaling = false; // Assume not scaled to start
809  Epetra_RowMatrix *funcDerivRowMatrix
810  = dynamic_cast<Epetra_RowMatrix*>(funcDerivOp);
811  if (funcDerivRowMatrix) {
812  if (fwdFuncScaling)
813  funcDerivRowMatrix->LeftScale(*fwdFuncScaling);
814  if (invVarScaling)
815  funcDerivRowMatrix->RightScale(*invVarScaling);
816  *didScaling = true;
817  // Note that above I do the func scaling before the var scaling since that
818  // is the same order it was done for W in Thyra::EpetraModelEvaluator
819  }
820 }
821 
822 
824  const ModelEvaluator::Derivative &origFuncDeriv,
825  const Epetra_Vector *invVarScaling,
826  const Epetra_Vector *fwdFuncScaling,
827  ModelEvaluator::Derivative *scaledFuncDeriv,
828  bool *didScaling
829  )
830 {
831  using Teuchos::RCP;
832  typedef ModelEvaluator EME;
833  TEUCHOS_TEST_FOR_EXCEPT(0==scaledFuncDeriv);
834  TEUCHOS_TEST_FOR_EXCEPT(0==didScaling);
835  *didScaling = false;
836  const RCP<Epetra_MultiVector>
837  funcDerivMv = origFuncDeriv.getMultiVector();
838  const EME::EDerivativeMultiVectorOrientation
839  funcDerivMv_orientation = origFuncDeriv.getMultiVectorOrientation();
840  if(!is_null(funcDerivMv)) {
841  if ( funcDerivMv_orientation == EME::DERIV_MV_BY_COL )
842  {
843  if (fwdFuncScaling) {
844  funcDerivMv->Multiply(1.0, *fwdFuncScaling, *funcDerivMv, 0.0);
845  }
846  if (invVarScaling) {
847  TEUCHOS_TEST_FOR_EXCEPT("ToDo: Scale rows!");
848  //funcDerivMv->Multiply(1.0, *funcDerivMv, *invVarScaling, 0.0);
849  }
850  }
851  else if ( funcDerivMv_orientation == EME::DERIV_TRANS_MV_BY_ROW )
852  {
853  if (invVarScaling) {
854  funcDerivMv->Multiply(1.0, *invVarScaling, *funcDerivMv, 0.0);
855  }
856  if (fwdFuncScaling) {
857  TEUCHOS_TEST_FOR_EXCEPT("ToDo: Scale rows!");
858  //funcDerivMv->Multiply(1.0, *funcDerivMv, *fwdFuncScaling, 0.0);
859  }
860  }
861  else {
862  TEUCHOS_TEST_FOR_EXCEPT("Should not get here!");
863  }
864  *scaledFuncDeriv = EME::Derivative(funcDerivMv,funcDerivMv_orientation);
865  *didScaling = true;
866  }
867  else {
868  RCP<Epetra_Operator>
869  funcDerivOp = origFuncDeriv.getLinearOp();
870  TEUCHOS_TEST_FOR_EXCEPT(is_null(funcDerivOp));
871  scaleModelFuncFirstDerivOp( invVarScaling, fwdFuncScaling,
872  &*funcDerivOp, didScaling );
873  if (didScaling)
874  *scaledFuncDeriv = EME::Derivative(funcDerivOp);
875  }
876 }
Class that gets and sets p(l) in an InArgs object.
void unscaleModelVarsGivenInverseScaling(const Epetra_Vector &origVars, const Epetra_Vector &invVarScaling, Epetra_Vector *scaledVars)
Unscale a vector given its inverse scaling vector.
bool supports(EOutArgsMembers arg) const
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_lower_bounds() const
void set_x_dot_poly(const Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > &x_dot_poly)
Set time derivative vector Taylor polynomial.
void set_W(const Teuchos::RefCountPtr< Epetra_Operator > &W)
virtual InArgs createInArgs() const =0
void scaleModelFuncFirstDeriv(const ModelEvaluator::Derivative &origFuncDeriv, const Epetra_Vector *invVarScaling, const Epetra_Vector *fwdFuncScaling, ModelEvaluator::Derivative *scaledFuncDeriv, bool *didScaling)
Scale (in place) an output first-order function derivative object given its function and variable sca...
Evaluation< Epetra_Vector > get_g(int j) const
Get g(j) where 0 <= j && j < this->Ng().
void set_DgDx(int j, const Derivative &DgDx_j)
Teuchos::RefCountPtr< const Epetra_Vector > get_x_dot() const
void unscaleModelVars(const ModelEvaluator::InArgs &scaledVars, const ModelEvaluator::InArgs &varScalings, ModelEvaluator::InArgs *origVars, Teuchos::FancyOStream *out=0, Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_LOW)
Unscale the scaled variables.
Teuchos::RefCountPtr< Epetra_MultiVector > getMultiVector() const
Teuchos::RefCountPtr< Teuchos::Polynomial< Epetra_Vector > > get_f_poly() const
Get residual vector Taylor polynomial.
void scaleModelVarBoundsGivenInverseScaling(const Epetra_Vector &origLowerBounds, const Epetra_Vector &origUpperBounds, const double infBnd, const Epetra_Vector &invVarScaling, Epetra_Vector *scaledLowerBounds, Epetra_Vector *scaledUpperBounds)
Scale the model variable bounds.
virtual double get_t_upper_bound() const
void set_DfDp(int l, const Derivative &DfDp_l)
Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > get_x_poly() const
Get solution vector Taylor polynomial.
Simple aggregate class that stores a derivative object as a general linear operator or as a multi-vec...
Class that gets and sets x_dot in an InArgs object.
void set_x_dot(const Teuchos::RefCountPtr< const Epetra_Vector > &x_dot)
void set_DgDx_dot(int j, const Derivative &DgDx_dot_j)
Teuchos::RefCountPtr< const Epetra_Vector > get_p(int l) const
void scaleModelFuncFirstDerivOp(const Epetra_Vector *invVarScaling, const Epetra_Vector *fwdFuncScaling, Epetra_Operator *funcDerivOp, bool *didScaling)
Scale (in place) an output first-order function derivative object represented as an Epetra_Operator g...
void scaleModelFuncs(const ModelEvaluator::OutArgs &origFuncs, const ModelEvaluator::InArgs &varScalings, const ModelEvaluator::OutArgs &funcScalings, ModelEvaluator::OutArgs *scaledFuncs, bool *allFuncsWhereScaled, Teuchos::FancyOStream *out=0, Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_LOW)
Scale the output functions and their derivative objects.
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_p_init(int l) const
Teuchos::RefCountPtr< const Epetra_Vector > get_x() const
Set solution vector Taylor polynomial.
void scaleModelFuncGivenForwardScaling(const Epetra_Vector &fwdFuncScaling, Epetra_Vector *funcs)
Scale (in place) an output function vector given its forward scaling vector.
void scaleModelVars(const ModelEvaluator::InArgs &origVars, const ModelEvaluator::InArgs &varScalings, ModelEvaluator::InArgs *scaledVars, Teuchos::FancyOStream *out=0, Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_LOW)
Scale the original unscaled variables into the scaled variables.
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_p_upper_bounds(int l) const
Teuchos::RefCountPtr< const Epetra_Vector > createInverseModelScalingVector(Teuchos::RefCountPtr< const Epetra_Vector > const &scalingVector)
Create an inverse scaling vector.
Derivative get_DgDp(int j, int l) const
Class that gets and sets f in an OutArgs object.
bool supports(EInArgsMembers arg) const
void set_x(const Teuchos::RefCountPtr< const Epetra_Vector > &x)
void set_f_poly(const Teuchos::RefCountPtr< Teuchos::Polynomial< Epetra_Vector > > &f_poly)
Set residual vector Taylor polynomial.
Class that gets and sets g(j) in an OutArgs object.
void set_DgDp(int j, int l, const Derivative &DgDp_j_l)
Teuchos::RefCountPtr< Epetra_Operator > get_W() const
EDerivativeMultiVectorOrientation getMultiVectorOrientation() const
void scaleModelVarsGivenInverseScaling(const Epetra_Vector &origVars, const Epetra_Vector &invVarScaling, Epetra_Vector *scaledVars)
Scale a vector given its inverse scaling vector.
void gatherModelBounds(const ModelEvaluator &model, ModelEvaluator::InArgs *lowerBounds, ModelEvaluator::InArgs *upperBounds)
Gather the lower and upper bounds from a model evaluator.
virtual double get_t_lower_bound() const
Class that gets and sets x in an InArgs object.
void scaleModelBounds(const ModelEvaluator::InArgs &origLowerBounds, const ModelEvaluator::InArgs &origUpperBounds, const double infBnd, const ModelEvaluator::InArgs &varScalings, ModelEvaluator::InArgs *scaledLowerBounds, ModelEvaluator::InArgs *scaledUpperBounds, Teuchos::FancyOStream *out=0, Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_LOW)
Scale the lower and upper model variable bounds.
virtual double get_t_init() const
Teuchos::RefCountPtr< Epetra_Operator > getLinearOp() const
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_p_lower_bounds(int l) const
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_dot_init() const
void set_p(int l, const Teuchos::RefCountPtr< const Epetra_Vector > &p_l)
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_upper_bounds() const
Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > get_x_dot_poly() const
Get time derivative vector Taylor polynomial.
virtual Teuchos::RefCountPtr< const Epetra_Vector > get_x_init() const
Evaluation< Epetra_Vector > get_f() const
void gatherModelNominalValues(const ModelEvaluator &model, ModelEvaluator::InArgs *nominalValues)
Gather the nominal values from a model evaluator.
Base interface for evaluating a stateless "model".
void set_x_poly(const Teuchos::RefCountPtr< const Teuchos::Polynomial< Epetra_Vector > > &x_poly)