Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_MPModelEvaluatorAdapter.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 
14 #include "Teuchos_Assert.hpp"
15 #include "Epetra_Map.h"
16 
20  me(me_)
21 {
22 }
23 
24 // Overridden from EpetraExt::ModelEvaluator
25 
28 get_x_map() const
29 {
30  return me->get_x_map();
31 }
32 
35 get_f_map() const
36 {
37  return me->get_f_map();
38 }
39 
42 get_p_map(int l) const
43 {
44  return me->get_p_map(l);
45 }
46 
49 get_g_map(int l) const
50 {
51  return me->get_g_map(l);
52 }
53 
56 get_p_names(int l) const
57 {
58  return me->get_p_names(l);
59 }
60 
63 get_x_init() const
64 {
65  return me->get_x_init();
66 }
67 
70 get_p_init(int l) const
71 {
72  return me->get_p_init(l);
73 }
74 
77 create_W() const
78 {
79  return me->create_W();
80 }
81 
82 EpetraExt::ModelEvaluator::InArgs
84 createInArgs() const
85 {
86  InArgsSetup inArgs;
87  InArgs me_inargs = me->createInArgs();
88 
89  inArgs.setModelEvalDescription(this->description());
90  inArgs.set_Np(me_inargs.Np());
91  inArgs.setSupports(IN_ARG_x_dot, me_inargs.supports(IN_ARG_x_dot));
92  inArgs.setSupports(IN_ARG_x, me_inargs.supports(IN_ARG_x));
93  inArgs.setSupports(IN_ARG_t, me_inargs.supports(IN_ARG_t));
94  inArgs.setSupports(IN_ARG_alpha, me_inargs.supports(IN_ARG_alpha));
95  inArgs.setSupports(IN_ARG_beta, me_inargs.supports(IN_ARG_beta));
96 
97  for (int i=0; i<me_inargs.Np(); i++)
98  inArgs.setSupports(IN_ARG_p_mp, i, true);
99  inArgs.setSupports(IN_ARG_x_mp, me_inargs.supports(IN_ARG_x));
100  inArgs.setSupports(IN_ARG_x_dot_mp, me_inargs.supports(IN_ARG_x_dot));
101 
102  return inArgs;
103 }
104 
105 EpetraExt::ModelEvaluator::OutArgs
108 {
109  OutArgsSetup outArgs;
110  OutArgs me_outargs = me->createOutArgs();
111 
112  outArgs.setModelEvalDescription(this->description());
113  outArgs.set_Np_Ng(me_outargs.Np(), me_outargs.Ng());
114  outArgs.setSupports(OUT_ARG_f, me_outargs.supports(OUT_ARG_f));
115  outArgs.setSupports(OUT_ARG_W, me_outargs.supports(OUT_ARG_W));
116  for (int j=0; j<me_outargs.Np(); j++)
117  outArgs.setSupports(OUT_ARG_DfDp, j,
118  me_outargs.supports(OUT_ARG_DfDp, j));
119  for (int i=0; i<me_outargs.Ng(); i++) {
120  outArgs.setSupports(OUT_ARG_DgDx, i,
121  me_outargs.supports(OUT_ARG_DgDx, i));
122  outArgs.setSupports(OUT_ARG_DgDx_dot, i,
123  me_outargs.supports(OUT_ARG_DgDx_dot, i));
124  for (int j=0; j<me_outargs.Np(); j++)
125  outArgs.setSupports(OUT_ARG_DgDp, i, j,
126  me_outargs.supports(OUT_ARG_DgDp, i, j));
127  }
128 
129  outArgs.setSupports(OUT_ARG_f_mp, me_outargs.supports(OUT_ARG_f));
130  if (me_outargs.supports(OUT_ARG_W)) {
131  outArgs.set_W_properties(me_outargs.get_W_properties());
132  outArgs.setSupports(OUT_ARG_W_mp, true);
133  }
134  for (int j=0; j<me_outargs.Np(); j++)
135  outArgs.setSupports(OUT_ARG_DfDp_mp, j,
136  me_outargs.supports(OUT_ARG_DfDp, j));
137  for (int i=0; i<me_outargs.Ng(); i++) {
138  outArgs.setSupports(OUT_ARG_g_mp, i, true);
139  outArgs.setSupports(OUT_ARG_DgDx_mp, i,
140  me_outargs.supports(OUT_ARG_DgDx, i));
141  outArgs.setSupports(OUT_ARG_DgDx_dot_mp, i,
142  me_outargs.supports(OUT_ARG_DgDx_dot, i));
143  for (int j=0; j<me_outargs.Np(); j++)
144  outArgs.setSupports(OUT_ARG_DgDp_mp, i, j,
145  me_outargs.supports(OUT_ARG_DgDp, i, j));
146  }
147 
148  return outArgs;
149 }
150 
151 void
153 evalModel(const InArgs& inArgs, const OutArgs& outArgs) const
154 {
155  // Create underlying inargs
156  InArgs me_inargs = me->createInArgs();
157  if (me_inargs.supports(IN_ARG_x))
158  me_inargs.set_x(inArgs.get_x());
159  if (me_inargs.supports(IN_ARG_x_dot))
160  me_inargs.set_x_dot(inArgs.get_x_dot());
161  if (me_inargs.supports(IN_ARG_alpha))
162  me_inargs.set_alpha(inArgs.get_alpha());
163  if (me_inargs.supports(IN_ARG_beta))
164  me_inargs.set_beta(inArgs.get_beta());
165  if (me_inargs.supports(IN_ARG_t))
166  me_inargs.set_t(inArgs.get_t());
167  for (int i=0; i<inArgs.Np(); i++)
168  me_inargs.set_p(i, inArgs.get_p(i));
169 
170  // Create underlying outargs
171  OutArgs me_outargs = me->createOutArgs();
172  if (me_outargs.supports(OUT_ARG_f))
173  me_outargs.set_f(outArgs.get_f());
174  if (me_outargs.supports(OUT_ARG_W))
175  me_outargs.set_W(outArgs.get_W());
176  for (int j=0; j<outArgs.Np(); j++)
177  if (!outArgs.supports(OUT_ARG_DfDp, j).none())
178  me_outargs.set_DfDp(j, outArgs.get_DfDp(j));
179  for (int i=0; i<outArgs.Ng(); i++) {
180  me_outargs.set_g(i, outArgs.get_g(i));
181  if (!outArgs.supports(OUT_ARG_DgDx, i).none())
182  me_outargs.set_DgDx(i, outArgs.get_DgDx(i));
183  if (!outArgs.supports(OUT_ARG_DgDx_dot, i).none())
184  me_outargs.set_DgDx(i, outArgs.get_DgDx_dot(i));
185  for (int j=0; j<outArgs.Np(); j++)
186  if (!outArgs.supports(OUT_ARG_DgDp, i, j).none())
187  me_outargs.set_DgDp(i, j, outArgs.get_DgDp(i,j));
188  }
189 
190  mp_const_vector_t x_mp;
191  mp_const_vector_t x_dot_mp;
192  Teuchos::Array<mp_const_vector_t> p_mp(inArgs.Np());
193  mp_vector_t f_mp;
194  mp_operator_t W_mp;
195  Teuchos::Array<MPDerivative> dfdp_mp(outArgs.Np());
196  Teuchos::Array<mp_vector_t> g_mp(outArgs.Ng());
197  Teuchos::Array<MPDerivative> dgdx_mp(outArgs.Ng());
198  Teuchos::Array<MPDerivative> dgdx_dot_mp(outArgs.Ng());
199  Teuchos::Array< Teuchos::Array<MPDerivative> > dgdp_mp(outArgs.Ng());
200  int num_mp = 0;
201 
202  if (inArgs.supports(IN_ARG_x_mp)) {
203  x_mp = inArgs.get_x_mp();
204  if (x_mp != Teuchos::null) {
205  num_mp = x_mp->size();
206  }
207  }
208  if (inArgs.supports(IN_ARG_x_dot_mp)) {
209  x_dot_mp = inArgs.get_x_dot_mp();
210  if (x_dot_mp != Teuchos::null) {
211  num_mp = x_dot_mp->size();
212  }
213  }
214  for (int i=0; i<inArgs.Np(); i++) {
215  p_mp[i] = inArgs.get_p_mp(i);
216  if (p_mp[i] != Teuchos::null) {
217  num_mp = p_mp[i]->size();
218  }
219  }
220  if (outArgs.supports(OUT_ARG_f_mp)) {
221  f_mp = outArgs.get_f_mp();
222  if (f_mp != Teuchos::null)
223  f_mp->init(0.0);
224  }
225  if (outArgs.supports(OUT_ARG_W_mp)) {
226  W_mp = outArgs.get_W_mp();
227  if (W_mp != Teuchos::null)
228  W_mp->init(0.0);
229  }
230  for (int i=0; i<inArgs.Np(); i++) {
231  if (!outArgs.supports(OUT_ARG_DfDp_mp, i).none()) {
232  dfdp_mp[i] = outArgs.get_DfDp_mp(i);
233  if (dfdp_mp[i].getMultiVector() != Teuchos::null)
234  dfdp_mp[i].getMultiVector()->init(0.0);
235  else if (dfdp_mp[i].getLinearOp() != Teuchos::null)
236  dfdp_mp[i].getLinearOp()->init(0.0);
237  }
238  }
239 
240  for (int i=0; i<outArgs.Ng(); i++) {
241  g_mp[i] = outArgs.get_g_mp(i);
242  if (g_mp[i] != Teuchos::null)
243  g_mp[i]->init(0.0);
244 
245  if (!outArgs.supports(OUT_ARG_DgDx_mp, i).none()) {
246  dgdx_mp[i] = outArgs.get_DgDx_mp(i);
247  if (dgdx_mp[i].getMultiVector() != Teuchos::null)
248  dgdx_mp[i].getMultiVector()->init(0.0);
249  else if (dgdx_mp[i].getLinearOp() != Teuchos::null)
250  dgdx_mp[i].getLinearOp()->init(0.0);
251  }
252 
253  if (!outArgs.supports(OUT_ARG_DgDx_dot_mp, i).none()) {
254  dgdx_dot_mp[i] = outArgs.get_DgDx_dot_mp(i);
255  if (dgdx_dot_mp[i].getMultiVector() != Teuchos::null)
256  dgdx_dot_mp[i].getMultiVector()->init(0.0);
257  else if (dgdx_dot_mp[i].getLinearOp() != Teuchos::null)
258  dgdx_dot_mp[i].getLinearOp()->init(0.0);
259  }
260 
261  dgdp_mp[i].resize(outArgs.Np());
262  for (int j=0; j<outArgs.Np(); j++) {
263  if (!outArgs.supports(OUT_ARG_DgDp_mp, i, j).none()) {
264  dgdp_mp[i][j] = outArgs.get_DgDp_mp(i,j);
265  if (dgdp_mp[i][j].getMultiVector() != Teuchos::null)
266  dgdp_mp[i][j].getMultiVector()->init(0.0);
267  else if (dgdp_mp[i][j].getLinearOp() != Teuchos::null)
268  dgdp_mp[i][j].getLinearOp()->init(0.0);
269  }
270  }
271  }
272 
273  for (int qp=0; qp<num_mp; qp++) {
274 
275  // Set InArgs
276  if (x_mp != Teuchos::null)
277  me_inargs.set_x(x_mp->getCoeffPtr(qp));
278  if (x_dot_mp != Teuchos::null)
279  me_inargs.set_x_dot(x_dot_mp->getCoeffPtr(qp));
280  for (int i=0; i<inArgs.Np(); i++)
281  if (p_mp[i] != Teuchos::null)
282  me_inargs.set_p(i, p_mp[i]->getCoeffPtr(qp));
283 
284  // Set OutArgs
285  if (f_mp != Teuchos::null)
286  me_outargs.set_f(f_mp->getCoeffPtr(qp));
287  if (W_mp != Teuchos::null)
288  me_outargs.set_W(W_mp->getCoeffPtr(qp));
289 
290  for (int i=0; i<inArgs.Np(); i++) {
291  if (!dfdp_mp[i].isEmpty()) {
292  if (dfdp_mp[i].getMultiVector() != Teuchos::null) {
293  Derivative deriv(dfdp_mp[i].getMultiVector()->getCoeffPtr(qp),
294  dfdp_mp[i].getMultiVectorOrientation());
295  me_outargs.set_DfDp(i, deriv);
296  }
297  else if (dfdp_mp[i].getLinearOp() != Teuchos::null) {
298  Derivative deriv(dfdp_mp[i].getLinearOp()->getCoeffPtr(qp));
299  me_outargs.set_DfDp(i, deriv);
300  }
301  }
302  }
303 
304  for (int i=0; i<outArgs.Ng(); i++) {
305  if (g_mp[i] != Teuchos::null)
306  me_outargs.set_g(i, g_mp[i]->getCoeffPtr(qp));
307  if (!dgdx_dot_mp[i].isEmpty()) {
308  if (dgdx_dot_mp[i].getMultiVector() != Teuchos::null) {
309  Derivative deriv(dgdx_dot_mp[i].getMultiVector()->getCoeffPtr(qp),
310  dgdx_dot_mp[i].getMultiVectorOrientation());
311  me_outargs.set_DgDx_dot(i, deriv);
312  }
313  else if (dgdx_dot_mp[i].getLinearOp() != Teuchos::null) {
314  Derivative deriv(dgdx_dot_mp[i].getLinearOp()->getCoeffPtr(qp));
315  me_outargs.set_DgDx_dot(i, deriv);
316  }
317  }
318  if (!dgdx_mp[i].isEmpty()) {
319  if (dgdx_mp[i].getMultiVector() != Teuchos::null) {
320  Derivative deriv(dgdx_mp[i].getMultiVector()->getCoeffPtr(qp),
321  dgdx_mp[i].getMultiVectorOrientation());
322  me_outargs.set_DgDx(i, deriv);
323  }
324  else if (dgdx_mp[i].getLinearOp() != Teuchos::null) {
325  Derivative deriv(dgdx_mp[i].getLinearOp()->getCoeffPtr(qp));
326  me_outargs.set_DgDx(i, deriv);
327  }
328  }
329  for (int j=0; j<outArgs.Np(); j++) {
330  if (!dgdp_mp[i][j].isEmpty()) {
331  if (dgdp_mp[i][j].getMultiVector() != Teuchos::null) {
332  Derivative deriv(dgdp_mp[i][j].getMultiVector()->getCoeffPtr(qp),
333  dgdp_mp[i][j].getMultiVectorOrientation());
334  me_outargs.set_DgDp(i, j, deriv);
335  }
336  else if (dgdp_mp[i][j].getLinearOp() != Teuchos::null) {
337  Derivative deriv(dgdp_mp[i][j].getLinearOp()->getCoeffPtr(qp));
338  me_outargs.set_DgDp(i, j, deriv);
339  }
340  }
341  }
342  }
343 
344  // Evaluate model
345  me->evalModel(me_inargs, me_outargs);
346 
347  }
348 
349  // Evaluate single-point model
350  if (num_mp == 0)
351  me->evalModel(me_inargs, me_outargs);
352 }
Teuchos::RCP< const Epetra_Map > get_g_map(int l) const
Return observation vector map.
MPModelEvaluatorAdapter(const Teuchos::RCP< EpetraExt::ModelEvaluator > &me)
Teuchos::RCP< const Epetra_Map > get_f_map() const
Return residual vector map.
void evalModel(const InArgs &inArgs, const OutArgs &outArgs) const
Evaluate model on InArgs.
Teuchos::RCP< const Epetra_Vector > get_x_init() const
Return initial solution.
Teuchos::RCP< const Epetra_Map > get_x_map() const
Return solution vector map.
Teuchos::RCP< const Epetra_Map > get_p_map(int l) const
Return parameter vector map.
void resize(size_type new_size, const value_type &x=value_type())
Teuchos::RCP< const Epetra_Vector > get_p_init(int l) const
Return initial parameters.
Teuchos::RCP< const Teuchos::Array< std::string > > get_p_names(int l) const
Return array of parameter names.
size_type size() const
Teuchos::RCP< Epetra_Operator > create_W() const
Create W = alpha*M + beta*J matrix.
OutArgs createOutArgs() const
Create OutArgs.