Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SGModelEvaluator_Interlaced.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 
12 #include <algorithm>
13 #include "Teuchos_Assert.hpp"
14 #include "EpetraExt_BlockUtility.h"
15 #include "EpetraExt_BlockMultiVector.h"
20 #include "Epetra_LocalMap.h"
21 #include "Epetra_Export.h"
22 #include "Epetra_Import.h"
23 
26  const Teuchos::RCP<const Stokhos::OrthogPolyBasis<int,double> >& sg_basis_,
27  const Teuchos::RCP<const Stokhos::Quadrature<int,double> >& sg_quad_,
29  const Teuchos::RCP<const Stokhos::ParallelData>& sg_parallel_data_,
31  bool scaleOP_)
32  : me(me_),
33  sg_basis(sg_basis_),
34  sg_quad(sg_quad_),
35  sg_exp(sg_exp_),
36  params(params_),
37  num_sg_blocks(sg_basis->size()),
38  num_W_blocks(sg_basis->size()),
39  num_p_blocks(sg_basis->size()),
40  supports_x(false),
41  x_map(me->get_x_map()),
42  f_map(me->get_f_map()),
43  sg_parallel_data(sg_parallel_data_),
44  sg_comm(sg_parallel_data->getMultiComm()),
45  epetraCijk(sg_parallel_data->getEpetraCijk()),
46  serialCijk(),
47  num_p(0),
48  num_p_sg(0),
49  sg_p_index_map(),
50  sg_p_map(),
51  sg_p_names(),
52  num_g(0),
53  num_g_sg(0),
54  sg_g_index_map(),
55  sg_g_map(),
56  x_dot_sg_blocks(),
57  x_sg_blocks(),
58  f_sg_blocks(),
59  W_sg_blocks(),
60  dgdx_dot_sg_blocks(),
61  dgdx_sg_blocks(),
62  sg_x_init(),
63  sg_p_init(),
64  eval_W_with_f(false),
65  scaleOP(scaleOP_)
66 {
67  if (x_map != Teuchos::null)
68  supports_x = true;
69 
72  static_cast<int>(num_sg_blocks), 0, *(sg_parallel_data->getStochasticComm())));
75  stoch_row_map = epetraCijk->getStochasticRowMap();
76 
77  if (supports_x) {
78 
79  // Create interlace SG x and f maps
80  interlace_x_map = buildInterlaceMap(*x_map,*stoch_row_map);
82 
83  interlace_f_map = buildInterlaceMap(*f_map,*stoch_row_map);
85 
86  // Create importer/exporter from/to overlapped distribution
91 
92  // now we create the underlying Epetra block vectors
93  // that will be used by the model evaluator to construct
94  // the solution of the deterministic problem.
96 
97  // Create vector blocks
101 
102  // Create default sg_x_init
104  if (sg_x_init->myGID(0))
105  (*sg_x_init)[0] = *(me->get_x_init());
106 
107  // Preconditioner needs an x: This is interlaced
109 
110 
111  // setup storage for W, these are blocked in Stokhos
112  // format
114 
115  // Determine W expansion type
116  std::string W_expansion_type =
117  params->get("Jacobian Expansion Type", "Full");
118  if (W_expansion_type == "Linear")
120  else
122 
123  Teuchos::RCP<Epetra_BlockMap> W_overlap_map =
125  static_cast<int>(num_W_blocks), 0,
126  *(sg_parallel_data->getStochasticComm())));
127  W_sg_blocks =
129  sg_basis, W_overlap_map, x_map, f_map, interlace_f_map,
130  sg_comm));
131  for (unsigned int i=0; i<num_W_blocks; i++)
132  W_sg_blocks->setCoeffPtr(i, me->create_W()); // allocate a bunch of matrices
133 
134  eval_W_with_f = params->get("Evaluate W with F", false);
135  }
136 
137  // Parameters -- The idea here is to add new parameter vectors
138  // for the stochastic Galerkin components of the parameters
139 
140  InArgs me_inargs = me->createInArgs();
141  OutArgs me_outargs = me->createOutArgs();
142  num_p = me_inargs.Np();
143 
144  // Get the p_sg's supported and build index map
145  for (int i=0; i<num_p; i++) {
146  if (me_inargs.supports(IN_ARG_p_sg, i))
148  }
150 
151  sg_p_map.resize(num_p_sg);
152  sg_p_names.resize(num_p_sg);
153  sg_p_init.resize(num_p_sg);
154 
155  // Determine parameter expansion type
156  std::string p_expansion_type =
157  params->get("Parameter Expansion Type", "Full");
158  if (p_expansion_type == "Linear")
160  else
162 
163  // Create parameter maps, names, and initial values
166  static_cast<int>(num_p_blocks), 0,
167  *(sg_parallel_data->getStochasticComm())));
168  for (int i=0; i<num_p_sg; i++) {
169  Teuchos::RCP<const Epetra_Map> p_map = me->get_p_map(sg_p_index_map[i]);
170  sg_p_map[i] =
171  Teuchos::rcp(EpetraExt::BlockUtility::GenerateBlockMap(
172  *p_map, *overlapped_stoch_p_map, *sg_comm));
173 
175  me->get_p_names(sg_p_index_map[i]);
176  if (p_names != Teuchos::null) {
177  sg_p_names[i] =
179  for (int j=0; j<p_names->size(); j++) {
180  std::stringstream ss;
181  ss << (*p_names)[j] << " -- SG Coefficient " << i;
182  (*sg_p_names[i])[j] = ss.str();
183  }
184  }
185 
186  // Create default sg_p_init
188  sg_p_init[i]->init(0.0);
189  }
190 
191  // Responses -- The idea here is to add new parameter vectors
192  // for the stochastic Galerkin components of the respones
193 
194  // Get number of SG parameters model supports derivatives w.r.t.
195  num_g = me_outargs.Ng();
196 
197  // Get the g_sg's supported and build index map
198  for (int i=0; i<num_g; i++) {
199  if (me_outargs.supports(OUT_ARG_g_sg, i))
201  }
203 
204  sg_g_map.resize(num_g_sg);
206  dgdx_sg_blocks.resize(num_g_sg);
207 
208  // Create response maps
209  for (int i=0; i<num_g_sg; i++) {
210  Teuchos::RCP<const Epetra_Map> g_map = me->get_g_map(sg_g_index_map[i]);
211  sg_g_map[i] =
212  Teuchos::rcp(EpetraExt::BlockUtility::GenerateBlockMap(
213  *g_map, *overlapped_stoch_row_map, *sg_comm));
214 
215  // Create dg/dxdot, dg/dx SG blocks
216  if (supports_x) {
217  dgdx_dot_sg_blocks[i] =
220  dgdx_sg_blocks[i] =
223  }
224  }
225 
226  // We don't support parallel for dgdx yet, so build a new EpetraCijk
227  if (supports_x) {
228  serialCijk =
230  epetraCijk->getCijk(),
231  sg_comm,
233  }
234 
235 }
236 
237 // Overridden from EpetraExt::ModelEvaluator
238 
241 {
242  return interlace_x_map;
243 }
244 
247 {
248  return interlace_f_map;
249 }
250 
253 {
254  TEUCHOS_TEST_FOR_EXCEPTION(l < 0 || l >= num_p + num_p_sg, std::logic_error,
255  "Error! Invalid p map index " << l);
256  if (l < num_p)
257  return me->get_p_map(l);
258  else
259  return sg_p_map[l-num_p];
260 
261  return Teuchos::null;
262 }
263 
266 {
267  TEUCHOS_TEST_FOR_EXCEPTION(l < 0 || l >= num_g_sg, std::logic_error,
268  "Error! Invalid g map index " << l);
269  return sg_g_map[l];
270 }
271 
274 {
275  TEUCHOS_TEST_FOR_EXCEPTION(l < 0 || l >= num_p + num_p_sg, std::logic_error,
276  "Error! Invalid p map index " << l);
277  if (l < num_p)
278  return me->get_p_names(l);
279  else
280  return sg_p_names[l-num_p];
281 
282  return Teuchos::null;
283 }
284 
287 {
288  return sg_x_init->getBlockVector();
289 }
290 
293 {
294  TEUCHOS_TEST_FOR_EXCEPTION(l < 0 || l >= num_p + num_p_sg, std::logic_error,
295  "Error! Invalid p map index " << l);
296  if (l < num_p)
297  return me->get_p_init(l);
298  else
299  return sg_p_init[l-num_p]->getBlockVector();
300 
301  return Teuchos::null;
302 }
303 
306 {
307  if (supports_x) {
309  Teuchos::rcp(&(params->sublist("SG Operator")), false);
311  W_crs = Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(me->create_W(), true);
313  Teuchos::rcp(&(W_crs->Graph()), false);
314 
315  my_W = Teuchos::rcp(new Stokhos::InterlacedOperator(sg_comm, sg_basis, epetraCijk, W_graph,sgOpParams));
316  my_W->setupOperator(W_sg_blocks);
317 
318  return my_W;
319  }
320 
321  return Teuchos::null;
322 }
323 
324 EpetraExt::ModelEvaluator::InArgs
326 {
327  InArgsSetup inArgs;
328  InArgs me_inargs = me->createInArgs();
329 
330  inArgs.setModelEvalDescription(this->description());
331  inArgs.set_Np(num_p + num_p_sg);
332  inArgs.setSupports(IN_ARG_x_dot, me_inargs.supports(IN_ARG_x_dot_sg));
333  inArgs.setSupports(IN_ARG_x, me_inargs.supports(IN_ARG_x_sg));
334  inArgs.setSupports(IN_ARG_t, me_inargs.supports(IN_ARG_t));
335  inArgs.setSupports(IN_ARG_alpha, me_inargs.supports(IN_ARG_alpha));
336  inArgs.setSupports(IN_ARG_beta, me_inargs.supports(IN_ARG_beta));
337  inArgs.setSupports(IN_ARG_sg_basis, me_inargs.supports(IN_ARG_sg_basis));
338  inArgs.setSupports(IN_ARG_sg_quadrature,
339  me_inargs.supports(IN_ARG_sg_quadrature));
340  inArgs.setSupports(IN_ARG_sg_expansion,
341  me_inargs.supports(IN_ARG_sg_expansion));
342 
343  return inArgs;
344 }
345 
346 EpetraExt::ModelEvaluator::OutArgs
348 {
349  OutArgsSetup outArgs;
350  OutArgs me_outargs = me->createOutArgs();
351 
352  outArgs.setModelEvalDescription(this->description());
353  outArgs.set_Np_Ng(num_p+num_p_sg, num_g_sg);
354  outArgs.setSupports(OUT_ARG_f, me_outargs.supports(OUT_ARG_f_sg));
355  outArgs.setSupports(OUT_ARG_W, me_outargs.supports(OUT_ARG_W_sg));
356  outArgs.setSupports(OUT_ARG_WPrec, false);
357  for (int j=0; j<num_p; j++)
358  outArgs.setSupports(OUT_ARG_DfDp, j,
359  me_outargs.supports(OUT_ARG_DfDp_sg, j));
360  for (int i=0; i<num_g_sg; i++) {
361  int ii = sg_g_index_map[i];
362 // if (!me_outargs.supports(OUT_ARG_DgDx_dot_sg, ii).none())
363 // outArgs.setSupports(OUT_ARG_DgDx_dot, i, DERIV_LINEAR_OP);
364 // if (!me_outargs.supports(OUT_ARG_DgDx_sg, i).none())
365 // outArgs.setSupports(OUT_ARG_DgDx, i, DERIV_LINEAR_OP);
366  for (int j=0; j<num_p; j++)
367  outArgs.setSupports(OUT_ARG_DgDp, i, j,
368  me_outargs.supports(OUT_ARG_DgDp_sg, ii, j));
369  }
370 
371  // We do not support derivatives w.r.t. the new SG parameters, so their
372  // support defaults to none.
373 
374  return outArgs;
375 }
376 
377 void
379  const OutArgs& outArgs) const
380 {
381  // Get the input arguments
383  if (inArgs.supports(IN_ARG_x)) {
384  x = inArgs.get_x();
385  if (x != Teuchos::null)
386  *my_x = *x;
387  }
389  if (inArgs.supports(IN_ARG_x_dot))
390  x_dot = inArgs.get_x_dot();
391 
392  // Get the output arguments
393  EpetraExt::ModelEvaluator::Evaluation<Epetra_Vector> f_out;
394  if (outArgs.supports(OUT_ARG_f))
395  f_out = outArgs.get_f();
397  if (outArgs.supports(OUT_ARG_W))
398  W_out = outArgs.get_W();
399 
400  // Create underlying inargs
401  InArgs me_inargs = me->createInArgs();
402  if (x != Teuchos::null) {
403  Teuchos::RCP<Epetra_Vector> overlapped_x
404  = Teuchos::rcp(new Epetra_Vector(*interlace_overlapped_x_map));
405  overlapped_x->Import(*x,*interlace_overlapped_x_importer,Insert);
406 
407  // x_sg_blocks->getBlockVector()->Import(*x, *interlace_overlapped_x_importer,
408  // Insert);
409 
410  copyToPolyOrthogVector(*overlapped_x,*x_sg_blocks);
411  me_inargs.set_x_sg(x_sg_blocks);
412  }
413  if (x_dot != Teuchos::null) {
414  Teuchos::RCP<Epetra_Vector> overlapped_x_dot
415  = Teuchos::rcp(new Epetra_Vector(*interlace_overlapped_x_map));
416  overlapped_x_dot->Import(*x_dot,*interlace_overlapped_x_importer,Insert);
417 
418  // x_dot_sg_blocks->getBlockVector()->Import(*x_dot, *interlace_overlapped_x_importer,
419  // Insert);
420 
421  copyToPolyOrthogVector(*overlapped_x_dot,*x_dot_sg_blocks);
422  me_inargs.set_x_dot_sg(x_dot_sg_blocks);
423  }
424  if (me_inargs.supports(IN_ARG_alpha))
425  me_inargs.set_alpha(inArgs.get_alpha());
426  if (me_inargs.supports(IN_ARG_beta))
427  me_inargs.set_beta(inArgs.get_beta());
428  if (me_inargs.supports(IN_ARG_t))
429  me_inargs.set_t(inArgs.get_t());
430  if (me_inargs.supports(IN_ARG_sg_basis)) {
431  if (inArgs.get_sg_basis() != Teuchos::null)
432  me_inargs.set_sg_basis(inArgs.get_sg_basis());
433  else
434  me_inargs.set_sg_basis(sg_basis);
435  }
436  if (me_inargs.supports(IN_ARG_sg_quadrature)) {
437  if (inArgs.get_sg_quadrature() != Teuchos::null)
438  me_inargs.set_sg_quadrature(inArgs.get_sg_quadrature());
439  else
440  me_inargs.set_sg_quadrature(sg_quad);
441  }
442  if (me_inargs.supports(IN_ARG_sg_expansion)) {
443  if (inArgs.get_sg_expansion() != Teuchos::null)
444  me_inargs.set_sg_expansion(inArgs.get_sg_expansion());
445  else
446  me_inargs.set_sg_expansion(sg_exp);
447  }
448 
449  // Pass parameters
450  for (int i=0; i<num_p; i++)
451  me_inargs.set_p(i, inArgs.get_p(i));
452  for (int i=0; i<num_p_sg; i++) {
453  Teuchos::RCP<const Epetra_Vector> p = inArgs.get_p(i+num_p);
454 
455  // We always need to pass in the SG parameters, so just use
456  // the initial parameters if it is NULL
457  if (p == Teuchos::null)
458  p = sg_p_init[i]->getBlockVector();
459 
460  // Convert block p to SG polynomial
462  create_p_sg(sg_p_index_map[i], View, p.get());
463  me_inargs.set_p_sg(sg_p_index_map[i], p_sg);
464  }
465 
466  // Create underlying outargs
467  OutArgs me_outargs = me->createOutArgs();
468 
469  // f
470  if (f_out != Teuchos::null) {
471  me_outargs.set_f_sg(f_sg_blocks);
472  if (eval_W_with_f)
473  me_outargs.set_W_sg(W_sg_blocks);
474  }
475 
476  // W
477  if (W_out != Teuchos::null && !eval_W_with_f )
478  me_outargs.set_W_sg(W_sg_blocks);
479 
480  // df/dp -- deterministic p
481  for (int i=0; i<outArgs.Np(); i++) {
482  if (!outArgs.supports(OUT_ARG_DfDp, i).none()) {
483  Derivative dfdp = outArgs.get_DfDp(i);
484  if (dfdp.getMultiVector() != Teuchos::null) {
486  if (dfdp.getMultiVectorOrientation() == DERIV_MV_BY_COL)
487  dfdp_sg =
489  sg_basis, overlapped_stoch_row_map,
490  me->get_f_map(), interlace_overlapped_f_map, sg_comm,
491  me->get_p_map(i)->NumMyElements()));
492  else if (dfdp.getMultiVectorOrientation() == DERIV_TRANS_MV_BY_ROW)
493  dfdp_sg =
495  sg_basis, overlapped_stoch_row_map,
496  me->get_p_map(i), sg_comm,
497  me->get_f_map()->NumMyElements()));
498  me_outargs.set_DfDp_sg(i,
499  SGDerivative(dfdp_sg,
500  dfdp.getMultiVectorOrientation()));
501  }
502  TEUCHOS_TEST_FOR_EXCEPTION(dfdp.getLinearOp() != Teuchos::null, std::logic_error,
503  "Error! Stokhos::SGModelEvaluator_Interlaced::evalModel " <<
504  "cannot handle operator form of df/dp!");
505  }
506  }
507 
508  // Responses (g, dg/dx, dg/dp, ...)
509  for (int i=0; i<num_g_sg; i++) {
510  int ii = sg_g_index_map[i];
511 
512  // g
513  Teuchos::RCP<Epetra_Vector> g = outArgs.get_g(i);
514  if (g != Teuchos::null) {
516  create_g_sg(sg_g_index_map[i], View, g.get());
517  me_outargs.set_g_sg(i, g_sg);
518  }
519 
520  // dg/dxdot
521  if (outArgs.supports(OUT_ARG_DgDx_dot, i).supports(DERIV_LINEAR_OP)) {
522  Derivative dgdx_dot = outArgs.get_DgDx_dot(i);
523  if (dgdx_dot.getLinearOp() != Teuchos::null) {
525  Teuchos::rcp_dynamic_cast<Stokhos::SGOperator>(
526  dgdx_dot.getLinearOp(), true);
528  op->getSGPolynomial();
529  if (me_outargs.supports(OUT_ARG_DgDx, ii).supports(DERIV_LINEAR_OP))
530  me_outargs.set_DgDx_dot_sg(ii, sg_blocks);
531  else {
532  for (unsigned int k=0; k<num_sg_blocks; k++) {
534  Teuchos::rcp_dynamic_cast<Stokhos::EpetraMultiVectorOperator>(
535  sg_blocks->getCoeffPtr(k), true)->getMultiVector();
536  dgdx_dot_sg_blocks[i]->setCoeffPtr(k, mv);
537  }
538  if (me_outargs.supports(OUT_ARG_DgDx_dot_sg, ii).supports(DERIV_MV_BY_COL))
539  me_outargs.set_DgDx_dot_sg(ii, SGDerivative(dgdx_dot_sg_blocks[i],
540  DERIV_MV_BY_COL));
541  else
542  me_outargs.set_DgDx_dot_sg(ii, SGDerivative(dgdx_dot_sg_blocks[i],
543  DERIV_TRANS_MV_BY_ROW));
544  }
545  }
546  TEUCHOS_TEST_FOR_EXCEPTION(dgdx_dot.getLinearOp() == Teuchos::null &&
547  dgdx_dot.isEmpty() == false,
548  std::logic_error,
549  "Error! Stokhos::SGModelEvaluator_Interlaced::evalModel: " <<
550  "Operator form of dg/dxdot is required!");
551  }
552 
553  // dg/dx
554  if (outArgs.supports(OUT_ARG_DgDx, i).supports(DERIV_LINEAR_OP)) {
555  Derivative dgdx = outArgs.get_DgDx(i);
556  if (dgdx.getLinearOp() != Teuchos::null) {
558  Teuchos::rcp_dynamic_cast<Stokhos::SGOperator>(
559  dgdx.getLinearOp(), true);
561  op->getSGPolynomial();
562  if (me_outargs.supports(OUT_ARG_DgDx, ii).supports(DERIV_LINEAR_OP))
563  me_outargs.set_DgDx_sg(i, sg_blocks);
564  else {
565  for (unsigned int k=0; k<num_sg_blocks; k++) {
567  Teuchos::rcp_dynamic_cast<Stokhos::EpetraMultiVectorOperator>(
568  sg_blocks->getCoeffPtr(k), true)->getMultiVector();
569  dgdx_sg_blocks[i]->setCoeffPtr(k, mv);
570  }
571  if (me_outargs.supports(OUT_ARG_DgDx_sg, ii).supports(DERIV_MV_BY_COL))
572  me_outargs.set_DgDx_sg(ii, SGDerivative(dgdx_sg_blocks[i],
573  DERIV_MV_BY_COL));
574  else
575  me_outargs.set_DgDx_sg(ii, SGDerivative(dgdx_sg_blocks[i],
576  DERIV_TRANS_MV_BY_ROW));
577  }
578  }
579  TEUCHOS_TEST_FOR_EXCEPTION(dgdx.getLinearOp() == Teuchos::null &&
580  dgdx.isEmpty() == false,
581  std::logic_error,
582  "Error! Stokhos::SGModelEvaluator_Interlaced::evalModel: " <<
583  "Operator form of dg/dxdot is required!");
584  }
585 
586  // dg/dp -- deterministic p
587  // Rembember, no derivatives w.r.t. sg parameters
588  for (int j=0; j<num_p; j++) {
589  if (!outArgs.supports(OUT_ARG_DgDp, i, j).none()) {
590  Derivative dgdp = outArgs.get_DgDp(i,j);
591  if (dgdp.getMultiVector() != Teuchos::null) {
593  if (dgdp.getMultiVectorOrientation() == DERIV_MV_BY_COL)
594  dgdp_sg =
596  sg_basis, overlapped_stoch_row_map,
597  me->get_g_map(ii), sg_g_map[i], sg_comm,
598  View, *(dgdp.getMultiVector())));
599  else if (dgdp.getMultiVectorOrientation() == DERIV_TRANS_MV_BY_ROW) {
601  Teuchos::rcp(&(dgdp.getMultiVector()->Map()),false);
602  dgdp_sg =
604  sg_basis, overlapped_stoch_row_map,
605  me->get_p_map(j), product_map, sg_comm,
606  View, *(dgdp.getMultiVector())));
607  }
608  me_outargs.set_DgDp_sg(ii, j,
609  SGDerivative(dgdp_sg,
610  dgdp.getMultiVectorOrientation()));
611  }
612  TEUCHOS_TEST_FOR_EXCEPTION(dgdp.getLinearOp() != Teuchos::null,
613  std::logic_error,
614  "Error! Stokhos::SGModelEvaluator_Interlaced::evalModel " <<
615  "cannot handle operator form of dg/dp!");
616  }
617  }
618 
619  }
620 
621  // Compute the functions
622  me->evalModel(me_inargs, me_outargs);
623 
624  // Copy block SG components for W
625  if ((W_out != Teuchos::null || (eval_W_with_f && f_out != Teuchos::null)) ) {
626 
628  if (W_out != Teuchos::null)
629  W = W_out;
630  else
631  W = my_W;
633  Teuchos::rcp_dynamic_cast<Stokhos::SGOperator>(W, true);
634  W_sg->setupOperator(W_sg_blocks);
635  }
636 
637  // f
638  if (f_out!=Teuchos::null){
639  if (!scaleOP)
640  for (int i=0; i<sg_basis->size(); i++)
641  (*f_sg_blocks)[i].Scale(sg_basis->norm_squared(i));
642 
643  Teuchos::RCP<Epetra_Vector> overlapped_f
644  = Teuchos::rcp(new Epetra_Vector(*interlace_overlapped_f_map));
645  copyToInterlacedVector(*f_sg_blocks,*overlapped_f);
646  f_out->Export(*overlapped_f,*interlace_overlapped_f_exporter,Insert);
647  }
648 
649  // df/dp -- determinsistic p
650  for (int i=0; i<num_p; i++) {
651  if (!outArgs.supports(OUT_ARG_DfDp, i).none()) {
652  Derivative dfdp = outArgs.get_DfDp(i);
653  SGDerivative dfdp_sg = me_outargs.get_DfDp_sg(i);
654  if (dfdp.getMultiVector() != Teuchos::null) {
655  dfdp.getMultiVector()->Export(
656  *(dfdp_sg.getMultiVector()->getBlockMultiVector()),
657  *interlace_overlapped_f_exporter, Insert);
658  }
659  }
660  }
661 }
662 
663 void
665  const Stokhos::EpetraVectorOrthogPoly& x_sg_in)
666 {
667  *sg_x_init = x_sg_in;
668 }
669 
672 {
673  return sg_x_init;
674 }
675 
676 void
678  int i, const Stokhos::EpetraVectorOrthogPoly& p_sg_in)
679 {
680  *sg_p_init[i] = p_sg_in;
681 }
682 
685 {
686  return sg_p_init[l];
687 }
688 
691 {
692  return sg_p_index_map;
693 }
694 
697 {
698  return sg_g_index_map;
699 }
700 
703 {
705  for (int i=0; i<num_g; i++)
706  base_maps[i] = me->get_g_map(i);
707  return base_maps;
708  }
709 
712 {
713  return overlapped_stoch_row_map;
714 }
715 
718 {
719  return interlace_overlapped_x_map;
720 }
721 
724 {
725  return interlace_overlapped_x_importer;
726 }
727 
730  const Epetra_Vector* v) const
731 {
733  if (v == NULL)
735  sg_basis, stoch_row_map, x_map, get_x_map(), sg_comm));
736  else
738  sg_basis, stoch_row_map, x_map, get_x_map(), sg_comm,
739  CV, *v));
740  return sg_x;
741 }
742 
745  const Epetra_Vector* v) const
746 {
748  if (v == NULL)
750  sg_basis, overlapped_stoch_row_map, x_map,
751  get_x_sg_overlap_map(), sg_comm));
752  else
754  sg_basis, overlapped_stoch_row_map, x_map,
755  get_x_sg_overlap_map(), sg_comm, CV, *v));
756  return sg_x;
757 }
758 
761  const Epetra_MultiVector* v) const
762 {
764  if (v == NULL)
766  sg_basis, stoch_row_map, x_map, get_x_map(), sg_comm,
767  num_vecs));
768  else
770  sg_basis, stoch_row_map, x_map, get_x_map(), sg_comm,
771  CV, *v));
772  return sg_x;
773 }
774 
777  int num_vecs,
779  const Epetra_MultiVector* v) const
780 {
782  if (v == NULL)
784  sg_basis, overlapped_stoch_row_map, x_map,
785  get_x_sg_overlap_map(), sg_comm, num_vecs));
786  else
788  sg_basis, overlapped_stoch_row_map, x_map,
789  get_x_sg_overlap_map(), sg_comm, CV, *v));
790  return sg_x;
791 }
792 
795  const Epetra_Vector* v) const
796 {
798  Teuchos::Array<int>::const_iterator it = std::find(sg_p_index_map.begin(),
799  sg_p_index_map.end(),
800  l);
801  TEUCHOS_TEST_FOR_EXCEPTION(it == sg_p_index_map.end(), std::logic_error,
802  "Error! Invalid p map index " << l);
803  int ll = it - sg_p_index_map.begin();
804  if (v == NULL)
806  sg_basis, overlapped_stoch_p_map, me->get_p_map(l),
807  sg_p_map[ll], sg_comm));
808  else
810  sg_basis, overlapped_stoch_p_map, me->get_p_map(l),
811  sg_p_map[ll], sg_comm, CV, *v));
812  return sg_p;
813 }
814 
817  const Epetra_Vector* v) const
818 {
820  if (v == NULL)
822  sg_basis, stoch_row_map, f_map, interlace_f_map, sg_comm));
823  else
825  sg_basis, stoch_row_map, f_map, interlace_f_map, sg_comm,
826  CV, *v));
827  return sg_f;
828 }
829 
832  const Epetra_Vector* v) const
833 {
835  if (v == NULL)
837  sg_basis, overlapped_stoch_row_map, f_map,
838  interlace_overlapped_f_map, sg_comm));
839  else
841  sg_basis, overlapped_stoch_row_map, f_map,
842  interlace_overlapped_f_map, sg_comm, CV, *v));
843  return sg_f;
844 }
845 
848  int num_vecs,
850  const Epetra_MultiVector* v) const
851 {
853  if (v == NULL)
855  sg_basis, stoch_row_map, f_map, interlace_f_map, sg_comm,
856  num_vecs));
857  else
859  sg_basis, stoch_row_map, f_map, interlace_f_map, sg_comm,
860  CV, *v));
861  return sg_f;
862 }
863 
866  int num_vecs,
868  const Epetra_MultiVector* v) const
869 {
871  if (v == NULL)
873  sg_basis, overlapped_stoch_row_map, f_map,
874  interlace_overlapped_f_map, sg_comm, num_vecs));
875  else
877  sg_basis, overlapped_stoch_row_map, f_map,
878  interlace_overlapped_f_map, sg_comm, CV, *v));
879  return sg_f;
880 }
881 
884  const Epetra_Vector* v) const
885 {
887  Teuchos::Array<int>::const_iterator it = std::find(sg_g_index_map.begin(),
888  sg_g_index_map.end(),
889  l);
890  TEUCHOS_TEST_FOR_EXCEPTION(it == sg_g_index_map.end(), std::logic_error,
891  "Error! Invalid g map index " << l);
892  int ll = it - sg_g_index_map.begin();
893  if (v == NULL)
895  sg_basis, overlapped_stoch_row_map,
896  me->get_g_map(l),
897  sg_g_map[ll], sg_comm));
898  else
900  sg_basis, overlapped_stoch_row_map,
901  me->get_g_map(l),
902  sg_g_map[ll], sg_comm, CV, *v));
903  return sg_g;
904 }
905 
909  const Epetra_MultiVector* v) const
910 {
912  Teuchos::Array<int>::const_iterator it = std::find(sg_g_index_map.begin(),
913  sg_g_index_map.end(),
914  l);
915  TEUCHOS_TEST_FOR_EXCEPTION(it == sg_g_index_map.end(), std::logic_error,
916  "Error! Invalid g map index " << l);
917  int ll = it - sg_g_index_map.begin();
918  if (v == NULL)
920  sg_basis, overlapped_stoch_row_map,
921  me->get_g_map(l),
922  sg_g_map[ll], sg_comm, num_vecs));
923  else
925  sg_basis, overlapped_stoch_row_map,
926  me->get_g_map(l),
927  sg_g_map[ll], sg_comm, CV, *v));
928  return sg_g;
929 }
930 
933 {
934  int stochaUnks = stocha_map.NumMyElements();
935  int determUnks = determ_map.NumMyElements();
936 
937  // these must be equal for the "adapt" model evaluator
938  TEUCHOS_ASSERT(stocha_map.NumGlobalElements()==stochaUnks);
939 
940  // build interlaced unknowns
941  std::vector<int> interlacedUnks(stochaUnks*determUnks);
942  std::size_t i=0;
943  for(int d=0;d<determUnks;d++)
944  for(int s=0;s<stochaUnks;s++,i++)
945  interlacedUnks[i] = determ_map.GID(d)*stochaUnks+s;
946 
947  return Teuchos::rcp(new Epetra_Map(-1,interlacedUnks.size(),&interlacedUnks[0],0,determ_map.Comm()));
948 }
949 
951  Epetra_Vector & x)
952 {
953  std::size_t numBlocks = x_sg.size();
955 
956  // loop over all blocks
957  for(std::size_t blk=0;blk<numBlocks;blk++) {
958  const Epetra_Vector & v = *bv_x->GetBlock(blk);
959 
960  for(int dof=0;dof<v.MyLength();dof++)
961  x[dof*numBlocks+blk] = v[dof];
962  }
963 }
964 
967 {
968  std::size_t numBlocks = x_sg.size();
970 
971  // loop over all blocks
972  for(std::size_t blk=0;blk<numBlocks;blk++) {
973  Epetra_Vector & v = *bv_x->GetBlock(blk);
974 
975  for(int dof=0;dof<v.MyLength();dof++)
976  v[dof] = x[dof*numBlocks+blk];
977  }
978 }
int NumGlobalElements() const
Teuchos::RCP< const Epetra_Map > get_x_map() const
Return solution vector map.
Teuchos::RCP< const Epetra_Map > get_g_map(int l) const
Return response map.
virtual void setupOperator(const Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > &poly)=0
Setup operator.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > x_sg_blocks
x stochastic Galerkin components
A container class storing an orthogonal polynomial whose coefficients are vectors, operators, or in general any type that would have an expensive copy constructor.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_x_sg_overlap(Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using x map and overlap sg map.
static Teuchos::RCP< Epetra_Map > buildInterlaceMap(const Epetra_BlockMap &determ_map, const Epetra_BlockMap &stocha_map)
Teuchos::RCP< const Epetra_Map > x_map
Underlying unknown map.
bool supports_x
Whether we support x (and thus f and W)
bool myGID(int i) const
Return whether global index i resides on this processor.
Teuchos::RCP< const Stokhos::EpetraSparse3Tensor > serialCijk
Serial Epetra Cijk for dgdx*.
Teuchos::RCP< const Epetra_Map > interlace_x_map
Block SG unknown map.
unsigned int num_sg_blocks
Number of stochastic blocks.
T & get(ParameterList &l, const std::string &name)
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > x_dot_sg_blocks
x_dot stochastic Galerkin components
Teuchos::RCP< const Epetra_Vector > get_x_init() const
Return initial solution.
Teuchos::RCP< EpetraExt::BlockVector > getBlockVector()
Get block vector.
int num_g_sg
Number of stochastic response vectors.
Teuchos::RCP< Epetra_Import > interlace_overlapped_x_importer
Importer from SG to SG-overlapped maps.
Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > create_x_mv_sg_overlap(int num_vecs, Epetra_DataAccess CV=Copy, const Epetra_MultiVector *v=NULL) const
Create vector orthog poly using x map and overlap sg map.
int num_g
Number of response vectors of underlying model evaluator.
Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > W_sg_blocks
W stochastic Galerkin components.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::Array< Teuchos::RCP< const Epetra_Map > > get_g_sg_base_maps() const
Get base maps of SG responses.
Teuchos::RCP< const Epetra_Map > interlace_overlapped_f_map
Block SG overlapped residual map.
Teuchos::RCP< const Stokhos::EpetraVectorOrthogPoly > get_p_sg_init(int l) const
Return initial SG parameters.
SGModelEvaluator_Interlaced(const Teuchos::RCP< EpetraExt::ModelEvaluator > &me, const Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > &sg_basis, const Teuchos::RCP< const Stokhos::Quadrature< int, double > > &sg_quad, const Teuchos::RCP< Stokhos::OrthogPolyExpansion< int, double > > &sg_exp, const Teuchos::RCP< const Stokhos::ParallelData > &sg_parallel_data, const Teuchos::RCP< Teuchos::ParameterList > &params, bool scaleOP=true)
T * get() const
Teuchos::RCP< const Stokhos::EpetraSparse3Tensor > epetraCijk
Epetra Cijk.
Teuchos::Array< int > get_g_sg_map_indices() const
Get indices of SG responses.
Teuchos::RCP< const Epetra_BlockMap > get_overlap_stochastic_map() const
Return overlap stochastic map.
virtual ordinal_type dimension() const =0
Return dimension of basis.
Teuchos::RCP< const Epetra_Vector > get_p_init(int l) const
Return initial parameters.
Teuchos::RCP< const Stokhos::OrthogPolyBasis< int, double > > sg_basis
Stochastic Galerkin basis.
Teuchos::Array< Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > > dgdx_sg_blocks
dg/dx stochastic Galerkin components
Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > create_x_mv_sg(int num_vecs, Epetra_DataAccess CV=Copy, const Epetra_MultiVector *v=NULL) const
Create vector orthog poly using x map and owned sg map.
Teuchos::Array< int > get_p_sg_map_indices() const
Get indices of SG parameters.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_g_sg(int l, Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using g map.
Teuchos::RCP< const Epetra_BlockMap > get_x_sg_overlap_map() const
Return x sg overlap map.
Abstract base class for orthogonal polynomial-based expansions.
Teuchos::RCP< const Epetra_Map > get_f_map() const
Return residual vector map.
Teuchos::RCP< const Epetra_Map > get_p_map(int l) const
Return parameter vector map.
int NumMyElements() const
static void copyToPolyOrthogVector(const Epetra_Vector &x, Stokhos::EpetraVectorOrthogPoly &x_sg)
void set_x_sg_init(const Stokhos::EpetraVectorOrthogPoly &x_sg_in)
Set initial solution polynomial.
An adaptor that supplies the operator interface to a multi-vector.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
An abstract class to represent a generic stochastic Galerkin operator as an Epetra_Operator.
Teuchos::RCP< const Epetra_BlockMap > overlapped_stoch_row_map
Overlapped map for stochastic blocks (local map)
int num_p
Number of parameter vectors of underlying model evaluator.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_f_sg_overlap(Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using f map and overlap sg map.
A container class storing an orthogonal polynomial whose coefficients are vectors, operators, or in general any type that would have an expensive copy constructor.
int GID(int LID) const
Teuchos::Array< Teuchos::RCP< Teuchos::Array< std::string > > > sg_p_names
SG coefficient parameter names.
Teuchos::RCP< const EpetraExt::MultiComm > sg_comm
Parallel SG communicator.
virtual Teuchos::RCP< Stokhos::EpetraOperatorOrthogPoly > getSGPolynomial()=0
Get SG polynomial.
Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > create_f_mv_sg(int num_vecs, Epetra_DataAccess CV=Copy, const Epetra_MultiVector *v=NULL) const
Create multi-vector orthog poly using f map and owned sg map.
Teuchos::RCP< const Stokhos::ParallelData > sg_parallel_data
Parallel SG data.
Teuchos::RCP< Epetra_Operator > create_W() const
Create W = alpha*M + beta*J matrix.
Teuchos::Array< Teuchos::RCP< const Epetra_Map > > sg_g_map
Block SG response map.
Teuchos::Array< Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > > dgdx_dot_sg_blocks
dg/dxdot stochastic Galerkin components
Teuchos::RCP< const Epetra_BlockMap > stoch_row_map
Map for stochastic blocks.
Teuchos::RCP< Epetra_Vector > my_x
x pointer for evaluating preconditioner
Teuchos::RCP< const Epetra_Map > interlace_overlapped_x_map
Block SG overlapped unknown map.
Teuchos::RCP< const Epetra_Import > get_x_sg_importer() const
Return x sg importer.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > sg_x_init
SG initial x.
unsigned int num_p_blocks
Number of p stochastic blocks (may be smaller than num_sg_blocks)
const Epetra_Comm & Comm() const
Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > create_g_mv_sg(int l, int num_vecs, Epetra_DataAccess CV=Copy, const Epetra_MultiVector *v=NULL) const
Create multi-vector orthog poly using g map.
void set_p_sg_init(int i, const Stokhos::EpetraVectorOrthogPoly &p_sg_in)
Set initial parameter polynomial.
iterator end()
std::vector< T >::const_iterator const_iterator
Teuchos::RCP< Epetra_Export > interlace_overlapped_f_exporter
Exporter from SG-overlapped to SG maps.
Teuchos::RCP< const Epetra_Map > interlace_f_map
Block SG residual map.
void evalModel(const InArgs &inArgs, const OutArgs &outArgs) const
Evaluate model on InArgs.
An Epetra operator representing the block stochastic Galerkin operator generated by fully assembling ...
bool eval_W_with_f
Whether to always evaluate W with f.
void push_back(const value_type &x)
Teuchos::RCP< const Epetra_BlockMap > overlapped_stoch_p_map
Overlapped map for p stochastic blocks (local map)
unsigned int num_W_blocks
Number of W stochastic blocks (may be smaller than num_sg_blocks)
Teuchos::RCP< Teuchos::ParameterList > params
Algorithmic parameters.
size_type size() const
Teuchos::RCP< Stokhos::EpetraMultiVectorOrthogPoly > create_f_mv_sg_overlap(int num_vecs, Epetra_DataAccess CV=Copy, const Epetra_MultiVector *v=NULL) const
Create multi-vector orthog poly using f map and overlap sg map.
Teuchos::RCP< const Stokhos::EpetraVectorOrthogPoly > get_x_sg_init() const
Return initial SG x.
Teuchos::Array< Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > > sg_p_init
SG initial p.
Teuchos::RCP< const Epetra_Map > f_map
Underlying residual map.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_f_sg(Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using f map and owned sg map.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_p_sg(int l, Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using p map.
const Epetra_CrsGraph & Graph() const
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > create_x_sg(Epetra_DataAccess CV=Copy, const Epetra_Vector *v=NULL) const
Create vector orthog poly using x map and owned sg map.
Epetra_DataAccess
Teuchos::RCP< EpetraExt::ModelEvaluator > me
Underlying model evaluator.
iterator begin()
ordinal_type size() const
Return size.
Teuchos::RCP< Stokhos::EpetraVectorOrthogPoly > f_sg_blocks
f stochastic Galerkin components
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Return array of parameter names.
int num_p_sg
Number of stochastic parameter vectors.
ScalarType g(const Teuchos::Array< ScalarType > &x, const ScalarType &y)
Teuchos::Array< Teuchos::RCP< const Epetra_Map > > sg_p_map
Block SG parameter map.
Teuchos::Array< int > sg_p_index_map
Index map between block-p and p_sg maps.
A container class storing an orthogonal polynomial whose coefficients are vectors, operators, or in general any type that would have an expensive copy constructor.
static void copyToInterlacedVector(const Stokhos::EpetraVectorOrthogPoly &x_sg, Epetra_Vector &x)
Teuchos::Array< int > sg_g_index_map
Index map between block-g and g_sg maps.