Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
epetra/SimpleME.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 
10 #include "SimpleME.hpp"
11 #include "Teuchos_Assert.hpp"
12 #include "Stokhos_Epetra.hpp"
13 
15 {
16  // Solution vector map
17  x_map = Teuchos::rcp(new Epetra_Map(2, 0, *comm));
18 
19  // Overlapped solution vector map
20  x_overlapped_map = Teuchos::rcp(new Epetra_LocalMap(2, 0, *comm));
21 
22  // Importer
24 
25  // Initial guess, initialized to 1.5
27  x_init->PutScalar(1.5);
28 
29  // Overlapped solution vector
31 
32  // Parameter vector map
33  p_map = Teuchos::rcp(new Epetra_LocalMap(1, 0, *comm));
34 
35  // Initial parameters
37  (*p_init)[0] = 2.0;
38 
39  // Parameter names
41  (*p_names)[0] = "alpha";
42 
43  // Jacobian graph (dense 2x2 matrix)
45  int indices[2];
46  indices[0] = 0; indices[1] = 1;
47  if (x_map->MyGID(0))
48  graph->InsertGlobalIndices(0, 2, indices);
49  if (x_map->MyGID(1))
50  graph->InsertGlobalIndices(1, 2, indices);
53 }
54 
55 // Overridden from EpetraExt::ModelEvaluator
56 
59 {
60  return x_map;
61 }
62 
65 {
66  return x_map;
67 }
68 
70 SimpleME::get_p_map(int l) const
71 {
73  std::logic_error,
74  std::endl <<
75  "Error! SimpleME::get_p_map(): " <<
76  "Invalid parameter index l = " << l << std::endl);
77 
78  return p_map;
79 }
80 
83 {
85  std::logic_error,
86  std::endl <<
87  "Error! SimpleME::get_p_names(): " <<
88  "Invalid parameter index l = " << l << std::endl);
89 
90  return p_names;
91 }
92 
95 {
96  return x_init;
97 }
98 
101 {
103  std::logic_error,
104  std::endl <<
105  "Error! SimpleME::get_p_init(): " <<
106  "Invalid parameter index l = " << l << std::endl);
107 
108  return p_init;
109 }
110 
113 {
116  A->FillComplete();
117  A->OptimizeStorage();
118  return A;
119 }
120 
121 EpetraExt::ModelEvaluator::InArgs
123 {
124  InArgsSetup inArgs;
125  inArgs.setModelEvalDescription("Simple Model Evaluator");
126 
127  // Deterministic InArgs
128  inArgs.setSupports(IN_ARG_x,true);
129  inArgs.set_Np(1); // 1 parameter vector
130 
131  // Stochastic InArgs
132  inArgs.setSupports(IN_ARG_x_sg,true);
133  inArgs.setSupports(IN_ARG_p_sg, 0, true); // 1 SG parameter vector
134  inArgs.setSupports(IN_ARG_sg_basis,true);
135  inArgs.setSupports(IN_ARG_sg_quadrature,true);
136  inArgs.setSupports(IN_ARG_sg_expansion,true);
137 
138  return inArgs;
139 }
140 
141 EpetraExt::ModelEvaluator::OutArgs
143 {
144  OutArgsSetup outArgs;
145  outArgs.setModelEvalDescription("Simple Model Evaluator");
146 
147  // Deterministic OutArgs
148  outArgs.set_Np_Ng(1, 0);
149  outArgs.setSupports(OUT_ARG_f,true);
150  outArgs.setSupports(OUT_ARG_W,true);
151 
152  // Stochastic OutArgs
153  outArgs.setSupports(OUT_ARG_f_sg,true);
154  outArgs.setSupports(OUT_ARG_W_sg,true);
155 
156  return outArgs;
157 }
158 
159 void
160 SimpleME::evalModel(const InArgs& inArgs, const OutArgs& outArgs) const
161 {
162  //
163  // Determinisic calculation
164  //
165 
166  // Solution vector
167  Teuchos::RCP<const Epetra_Vector> x = inArgs.get_x();
168  if (x != Teuchos::null)
169  x_overlapped->Import(*x, *importer, Insert);
170  double x0 = (*x_overlapped)[0];
171  double x1 = (*x_overlapped)[1];
172 
173  // Parameters
174  Teuchos::RCP<const Epetra_Vector> p = inArgs.get_p(0);
175  if (p == Teuchos::null)
176  p = p_init;
177  double a = (*p)[0];
178 
179  // Residual
180  // f = | a*a - x0 |
181  // | x1*x1 - x0 |
182  // where a = p[0].
183  Teuchos::RCP<Epetra_Vector> f = outArgs.get_f();
184  if (f != Teuchos::null) {
185  int row;
186  double v;
187  if (x_map->MyGID(0)) {
188  row = 0;
189  v = a*a - x0;
190  f->ReplaceGlobalValues(1, &v, &row);
191  }
192  if (x_map->MyGID(1)) {
193  row = 1;
194  v = x1*x1 - x0;
195  f->ReplaceGlobalValues(1, &v, &row);
196  }
197  }
198 
199  // Jacobian
200  // J = | -1 0 |
201  // | -1 2*x1 |
202  Teuchos::RCP<Epetra_Operator> W = outArgs.get_W();
203  if (W != Teuchos::null) {
205  Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W, true);
206  int indices[2] = { 0, 1 };
207  double values[2];
208  if (x_map->MyGID(0)) {
209  values[0] = -1.0; values[1] = 0.0;
210  jac->ReplaceGlobalValues(0, 2, values, indices);
211  }
212  if (x_map->MyGID(1)) {
213  values[0] = -1.0; values[1] = 2.0*x1;
214  jac->ReplaceGlobalValues(1, 2, values, indices);
215  }
216  }
217 
218  //
219  // Stochastic Galerkin calculation
220  //
221 
222  // Get stochastic expansion data
224  inArgs.get_sg_basis();
226  inArgs.get_sg_expansion();
227 
228  // Stochastic solution vector
229  InArgs::sg_const_vector_t x_sg = inArgs.get_x_sg();
230  Stokhos::OrthogPolyApprox<int,double> x0_sg(basis), x1_sg(basis);
231  if (x_sg != Teuchos::null) {
232  for (int i=0; i<basis->size(); i++) {
233  x_overlapped->Import((*x_sg)[i], *importer, Insert);
234  x0_sg[i] = (*x_overlapped)[0];
235  x1_sg[i] = (*x_overlapped)[1];
236  }
237  }
238 
239  // Stochastic parameters
240  InArgs::sg_const_vector_t p_sg = inArgs.get_p_sg(0);
242  if (p_sg != Teuchos::null) {
243  for (int i=0; i<basis->size(); i++) {
244  a_sg[i] = (*p_sg)[i][0];
245  }
246  }
247 
248  // Stochastic residual
249  // f[i] = | <a*a - x0, psi_i>/<psi_i^2> |
250  // | <x1*x1 - x0, psi_i>/<psi_i^2> |
251  OutArgs::sg_vector_t f_sg = outArgs.get_f_sg();
252  Stokhos::OrthogPolyApprox<int,double> tmp0_sg(basis), tmp1_sg(basis);
253  if (f_sg != Teuchos::null) {
254  int row;
255  if (x_map->MyGID(0)) {
256  row = 0;
257  expn->times(tmp0_sg, a_sg, a_sg);
258  expn->minus(tmp1_sg, tmp0_sg, x0_sg);
259  for (int i=0; i<basis->size(); i++)
260  (*f_sg)[i].ReplaceGlobalValues(1, &tmp1_sg[i], &row);
261  }
262  if (x_map->MyGID(1)) {
263  row = 1;
264  expn->times(tmp0_sg, x1_sg, x1_sg);
265  expn->minus(tmp1_sg, tmp0_sg, x0_sg);
266  for (int i=0; i<basis->size(); i++)
267  (*f_sg)[i].ReplaceGlobalValues(1, &tmp1_sg[i], &row);
268  }
269  }
270 
271  // Stochastic Jacobian
272  // J[0] = | -1 0 |, J[i] = | 0 0 |, i > 0
273  // | -1 2*x0[0] | | 0 2*x0[i] |
274  OutArgs::sg_operator_t W_sg = outArgs.get_W_sg();
275  if (W_sg != Teuchos::null) {
276  for (int i=0; i<basis->size(); i++) {
278  Teuchos::rcp_dynamic_cast<Epetra_CrsMatrix>(W_sg->getCoeffPtr(i), true);
279  int indices[2] = { 0, 1 };
280  double values[2];
281  if (x_map->MyGID(0)) {
282  if (i == 0)
283  values[0] = -1.0;
284  else
285  values[0] = 0.0;
286  values[1] = 0.0;
287  jac->ReplaceGlobalValues(0, 2, values, indices);
288  }
289  if (x_map->MyGID(1)) {
290  if (i == 0)
291  values[0] = -1.0;
292  else
293  values[0] = 0.0;
294  values[1] = 2.0*x1_sg[i];
295  jac->ReplaceGlobalValues(1, 2, values, indices);
296  }
297  }
298  }
299 }
Teuchos::RCP< const Epetra_Vector > get_x_init() const
Return initial solution.
Teuchos::RCP< Epetra_Vector > x_init
Initial guess.
Teuchos::RCP< const Epetra_Map > get_f_map() const
Return residual vector map.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
SimpleME(const Teuchos::RCP< const Epetra_Comm > &comm)
Constructor.
Teuchos::RCP< const Epetra_Map > get_x_map() const
Return solution vector map.
Teuchos::RCP< Epetra_Vector > p_init
Initial parameters.
InArgs createInArgs() const
Create InArgs.
Teuchos::RCP< Epetra_Vector > x_overlapped
Overlapped solution vector.
Teuchos::RCP< Epetra_Map > x_map
Solution vector map.
int InsertGlobalIndices(int_type GlobalRow, int NumIndices, int_type *Indices)
int FillComplete(bool OptimizeDataStorage=true)
int ReplaceGlobalValues(int NumEntries, const double *Values, const int *Indices)
void evalModel(const InArgs &inArgs, const OutArgs &outArgs) const
Evaluate model on InArgs.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< Epetra_Map > p_map
Parameter vector map.
bool MyGID(int GID_in) const
Teuchos::RCP< Teuchos::Array< std::string > > p_names
Parameter names.
Teuchos::RCP< Epetra_CrsGraph > graph
Jacobian graph.
ScalarType f(const Teuchos::Array< ScalarType > &x, double a, double b)
Teuchos::RCP< const Epetra_Map > get_p_map(int l) const
Return parameter vector map.
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Return array of parameter names.
Teuchos::RCP< Epetra_Import > importer
Importer to overlapped distribution.
virtual int ReplaceGlobalValues(int GlobalRow, int NumEntries, const double *Values, const int *Indices)
OutArgs createOutArgs() const
Create OutArgs.
Teuchos::RCP< Epetra_Operator > create_W() const
Create W = alpha*M + beta*J matrix.
Teuchos::RCP< Epetra_Map > x_overlapped_map
Overlapped solution vector map.
Teuchos::RCP< const Epetra_Vector > get_p_init(int l) const
Return initial parameters.