EpetraExt  Development
EpetraExt_MultiPointModelEvaluator.cpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // EpetraExt: Epetra Extended - Linear Algebra Services Package
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
45 #include "Epetra_Map.h"
46 #include "Teuchos_as.hpp"
47 
49  Teuchos::RefCountPtr<EpetraExt::ModelEvaluator> underlyingME_,
50  const Teuchos::RefCountPtr<EpetraExt::MultiComm> &globalComm_,
51  const std::vector<Epetra_Vector*> initGuessVec_,
52  Teuchos::RefCountPtr<std::vector< Teuchos::RefCountPtr<Epetra_Vector> > > q_vec_,
53  Teuchos::RefCountPtr<std::vector< Teuchos::RefCountPtr<Epetra_Vector> > > matching_vec_
54  ) :
55  underlyingME(underlyingME_),
56  globalComm(globalComm_),
57  underlyingNg(0),
58  timeStepsOnTimeDomain(globalComm_->NumTimeStepsOnDomain()),
59  numTimeDomains(globalComm_->NumSubDomains()),
60  timeDomain(globalComm_->SubDomainRank()),
61  rowStencil(0),
62  rowIndex(0),
63  q_vec(q_vec_),
64  matching_vec(matching_vec_)
65 {
66  using Teuchos::as;
67  if (globalComm->MyPID()==0) {
68  cout << "----------MultiPoint Partition Info------------"
69  << "\n\tNumProcs = " << globalComm->NumProc()
70  << "\n\tSpatial Decomposition = " << globalComm->SubDomainComm().NumProc()
71  << "\n\tNumber of Domains = " << numTimeDomains
72  << "\n\tSteps on Domain 0 = " << timeStepsOnTimeDomain
73  << "\n\tTotal Number of Steps = " << globalComm->NumTimeSteps();
74  cout << "\n-----------------------------------------------" << endl;
75  }
76 
77  // Construct global block matrix graph from split W and stencil,
78  // which is just diagonal in this case
79 
80  split_W = Teuchos::rcp_dynamic_cast<Epetra_RowMatrix>(underlyingME->create_W());
81 
82  rowStencil = new std::vector< std::vector<int> >(timeStepsOnTimeDomain);
83  rowIndex = new std::vector<int>;
84  for (int i=0; i < timeStepsOnTimeDomain; i++) {
85  (*rowStencil)[i].push_back(0);
86  (*rowIndex).push_back(i + globalComm->FirstTimeStepOnDomain());
87  }
88 
89  block_W = Teuchos::rcp(new EpetraExt::BlockCrsMatrix(*split_W,
90  *rowStencil, *rowIndex, *globalComm));
91 
92  // Test for g vector
93  EpetraExt::ModelEvaluator::OutArgs underlyingOutArgs = underlyingME->createOutArgs();
94 
95  underlyingNg = underlyingOutArgs.Ng();
96  if (underlyingNg) {
97  if (underlyingOutArgs.supports(OUT_ARG_DgDp,0,0).supports(DERIV_TRANS_MV_BY_ROW))
98  orientation_DgDp = DERIV_TRANS_MV_BY_ROW;
99  else
100  orientation_DgDp = DERIV_MV_BY_COL;
101  }
102 
103  // This code assumes 2 parameter vectors, 1 for opt, second for MultiPoint states
104  TEUCHOS_TEST_FOR_EXCEPT(underlyingOutArgs.Np()!=2);
105 
106  // temporary quantities
107  const Epetra_Map& split_map = split_W->RowMatrixRowMap();
108  num_p0 = underlyingME_->get_p_map(0)->NumMyElements();
109  if (underlyingNg) num_g0 = underlyingME_->get_g_map(0)->NumMyElements();
110  else num_g0 = 0;
111  num_dg0dp0 = num_g0 * num_p0;
112 
113  // Construct global solution vector, residual vector -- local storage
114  block_x = new EpetraExt::BlockVector(split_map, block_W->RowMap());
115  block_f = new EpetraExt::BlockVector(*block_x);
116  block_DfDp = new EpetraExt::BlockMultiVector(split_map, block_W->RowMap(), num_p0);
117  if (underlyingNg)
118  block_DgDx = new EpetraExt::BlockMultiVector(split_map, block_W->RowMap(), num_g0);
119 
120  // Allocate local storage of epetra vectors
121  split_x = Teuchos::rcp(new Epetra_Vector(split_map));
122  split_f = Teuchos::rcp(new Epetra_Vector(split_map));
123  split_DfDp = Teuchos::rcp(new Epetra_MultiVector(split_map, num_p0));
124  if (underlyingNg)
125  split_DgDx = Teuchos::rcp(new Epetra_MultiVector(split_map, num_g0));
126  if (underlyingNg) {
127  if(orientation_DgDp == DERIV_TRANS_MV_BY_ROW)
128  split_DgDp = Teuchos::rcp(new Epetra_MultiVector(*(underlyingME_->get_p_map(0)), num_g0));
129  else
130  split_DgDp = Teuchos::rcp(new Epetra_MultiVector(*(underlyingME_->get_g_map(0)), num_p0));
131  }
132  if (underlyingNg)
133  split_g = Teuchos::rcp(new Epetra_Vector(*(underlyingME_->get_g_map(0))));
134 
135  // Packaging required for getting multivectors back as Derivatives
136  derivMV_DfDp = new EpetraExt::ModelEvaluator::DerivativeMultiVector(split_DfDp);
137  deriv_DfDp = new EpetraExt::ModelEvaluator::Derivative(*derivMV_DfDp);
138  if (underlyingNg) {
140  deriv_DgDx = new EpetraExt::ModelEvaluator::Derivative(*derivMV_DgDx);
141  derivMV_DgDp = new EpetraExt::ModelEvaluator::DerivativeMultiVector(split_DgDp, orientation_DgDp);
142  deriv_DgDp = new EpetraExt::ModelEvaluator::Derivative(*derivMV_DgDp);
143  }
144 
145  // For 4D, we will need the overlap vector and importer between them
146  // Overlap not needed for MultiPoint -- no overlap between blocks
147  /* solutionOverlap = new EpetraExt::BlockVector(split_W->RowMatrixRowMap(),
148  block_W->ColMap());
149  overlapImporter = new Epetra_Import(solutionOverlap->Map(), block_x->Map());
150  */
151 
152  // Load initial guess into block solution vector
153  solution_init = Teuchos::rcp(new EpetraExt::BlockVector(*block_x));
154  for (int i=0; i < timeStepsOnTimeDomain; i++)
155  solution_init->LoadBlockValues(*(initGuessVec_[i]), (*rowIndex)[i]);
156 
157 
158  //Prepare logic for matching problem
159  if (Teuchos::is_null(matching_vec)) matchingProblem = false;
160  else matchingProblem = true;
161 
162  if (matchingProblem) {
163  TEUCHOS_TEST_FOR_EXCEPT(as<int>(matching_vec->size())!=timeStepsOnTimeDomain);
164  TEUCHOS_TEST_FOR_EXCEPT(!(*matching_vec)[0]->Map().SameAs(*(underlyingME_->get_g_map(0))));
165  TEUCHOS_TEST_FOR_EXCEPT(num_g0 != 1); //This restriction may be lifted later
166  }
167 }
168 
170 {
171  delete block_x;
172  delete block_f;
173  delete block_DfDp;
174  if (underlyingNg) delete block_DgDx;
175  delete rowStencil;
176  delete rowIndex;
177 
178  delete derivMV_DfDp;
179  delete deriv_DfDp;
180  if (underlyingNg) {
181  delete derivMV_DgDx;
182  delete deriv_DgDx;
183  delete derivMV_DgDp;
184  delete deriv_DgDp;
185  }
186 }
187 
188 Teuchos::RefCountPtr<const Epetra_Map> EpetraExt::MultiPointModelEvaluator::get_x_map() const
189 {
190  return Teuchos::rcp(&(block_W->OperatorDomainMap()), false);
191 }
192 
193 Teuchos::RefCountPtr<const Epetra_Map> EpetraExt::MultiPointModelEvaluator::get_f_map() const
194 {
195  return get_x_map();
196 }
197 
198 Teuchos::RefCountPtr<const Epetra_Map> EpetraExt::MultiPointModelEvaluator::get_p_map(int l) const
199 {
200  return underlyingME->get_p_map(l);
201 }
202 
203 Teuchos::RefCountPtr<const Epetra_Map> EpetraExt::MultiPointModelEvaluator::get_g_map(int j) const
204 {
205  return underlyingME->get_g_map(j);
206 }
207 
208 Teuchos::RefCountPtr<const Epetra_Vector> EpetraExt::MultiPointModelEvaluator::get_x_init() const
209 {
210  return solution_init;
211 }
212 
213 Teuchos::RefCountPtr<const Epetra_Vector> EpetraExt::MultiPointModelEvaluator::get_p_init(int l) const
214 {
215  return underlyingME->get_p_init(l);
216 }
217 
218 Teuchos::RefCountPtr<Epetra_Operator> EpetraExt::MultiPointModelEvaluator::create_W() const
219 {
220  return block_W;
221 }
222 
224 {
225  //return underlyingME->createInArgs();
226  InArgsSetup inArgs;
227  inArgs.setModelEvalDescription(this->description());
228  inArgs.set_Np(1);
229  inArgs.setSupports(IN_ARG_x,true);
230  return inArgs;
231 }
232 
234 {
235  //return underlyingME->createOutArgs();
236  OutArgsSetup outArgs;
237  outArgs.setModelEvalDescription(this->description());
238  outArgs.set_Np_Ng(1, underlyingNg);
239  outArgs.setSupports(OUT_ARG_f,true);
240  outArgs.setSupports(OUT_ARG_W,true);
241  outArgs.set_W_properties(
245  ,true // supportsAdjoint
246  )
247  );
249  outArgs.set_DfDp_properties(
253  ,true // supportsAdjoint
254  )
255  );
256 
257  if (underlyingNg) {
259  outArgs.set_DgDx_properties(
263  ,true // supportsAdjoint
264  )
265  );
266  outArgs.setSupports(OUT_ARG_DgDp,0,0, orientation_DgDp);
267  outArgs.set_DgDp_properties(
271  ,true // supportsAdjoint
272  )
273  );
274  }
275  return outArgs;
276 }
277 
279  const OutArgs& outArgs ) const
280 {
281 
282  EpetraExt::ModelEvaluator::InArgs underlyingInArgs = underlyingME->createInArgs();
283  EpetraExt::ModelEvaluator::OutArgs underlyingOutArgs = underlyingME->createOutArgs();
284 
285  //temp code for multipoint param q vec
286 /*
287  Teuchos::RefCountPtr<Epetra_Vector> q =
288  Teuchos::rcp(new Epetra_Vector(*(underlyingME->get_p_map(1))));
289 */
290 
291  // Parse InArgs
292  Teuchos::RefCountPtr<const Epetra_Vector> p_in = inArgs.get_p(0);
293  if (p_in.get()) underlyingInArgs.set_p(0, p_in);
294 
295  Teuchos::RefCountPtr<const Epetra_Vector> x_in = inArgs.get_x();
296  block_x->Epetra_Vector::operator=(*x_in); //copy into block vector
297 
298  // Parse OutArgs
299  Teuchos::RefCountPtr<Epetra_Vector> f_out = outArgs.get_f();
300 
301  Teuchos::RefCountPtr<Epetra_Operator> W_out = outArgs.get_W();
302  Teuchos::RefCountPtr<EpetraExt::BlockCrsMatrix> W_block =
303  Teuchos::rcp_dynamic_cast<EpetraExt::BlockCrsMatrix>(W_out);
304 
305  Teuchos::RefCountPtr<Epetra_Vector> g_out;
306  if (underlyingNg) g_out = outArgs.get_g(0);
307  if (g_out.get()) g_out->PutScalar(0.0);
308 
309  EpetraExt::ModelEvaluator::Derivative DfDp_out = outArgs.get_DfDp(0);
310 
313  if (underlyingNg) {
314  DgDx_out = outArgs.get_DgDx(0);
315  DgDp_out = outArgs.get_DgDp(0,0);
316  if (!DgDx_out.isEmpty()) DgDx_out.getMultiVector()->PutScalar(0.0);
317  if (!DgDp_out.isEmpty()) DgDp_out.getMultiVector()->PutScalar(0.0);
318  }
319 
320  // For mathcingProblems, g is needed to calc DgDx DgDp, so ask for
321  // g even if it isn't requested.
322  bool need_g = g_out.get();
323  if (matchingProblem)
324  if ( !DgDx_out.isEmpty() || !DgDp_out.isEmpty() ) need_g = true;
325 
326 
327  // Begin loop over Points (steps) owned on this proc
328  for (int i=0; i < timeStepsOnTimeDomain; i++) {
329 
330  // Set MultiPoint parameter vector
331  underlyingInArgs.set_p(1, (*q_vec)[i]);
332 
333  // Set InArgs
334  block_x->ExtractBlockValues(*split_x, (*rowIndex)[i]);
335  underlyingInArgs.set_x(split_x);
336 
337  // Set OutArgs
338  if (f_out.get()) underlyingOutArgs.set_f(split_f);
339 
340  if (need_g) underlyingOutArgs.set_g(0, split_g);
341 
342  if (W_out.get()) underlyingOutArgs.set_W(split_W);
343 
344  if (!DfDp_out.isEmpty()) underlyingOutArgs.set_DfDp(0, *deriv_DfDp);
345 
346  if (!DgDx_out.isEmpty()) underlyingOutArgs.set_DgDx(0, *deriv_DgDx);
347 
348  if (!DgDp_out.isEmpty()) underlyingOutArgs.set_DgDp(0, 0, *deriv_DgDp);
349 
350  //********Eval Model ********/
351  underlyingME->evalModel(underlyingInArgs, underlyingOutArgs);
352  //********Eval Model ********/
353 
354  // If matchingProblem, modify all g-related quantitites G = 0.5*(g-g*)^2 / g*^2
355  if (matchingProblem) {
356  if (need_g) {
357  double diff = (*split_g)[0] - (*(*matching_vec)[i])[0];
358  double nrmlz = fabs((*(*matching_vec)[i])[0]) + 1.0e-6;
359  (*split_g)[0] = 0.5 * diff * diff/(nrmlz*nrmlz);
360  if (!DgDx_out.isEmpty()) split_DgDx->Scale(diff/(nrmlz*nrmlz));
361  if (!DgDp_out.isEmpty()) split_DgDp->Scale(diff/(nrmlz*nrmlz));
362  }
363  }
364 
365  // Repackage block components into global block matrx/vector/multivector
366  if (f_out.get()) block_f->LoadBlockValues(*split_f, (*rowIndex)[i]);
367  if (W_out.get()) W_block->LoadBlock(*split_W, i, 0);
368  // note: split_DfDp points inside deriv_DfDp
369  if (!DfDp_out.isEmpty()) block_DfDp->LoadBlockValues(*split_DfDp, (*rowIndex)[i]);
370  if (!DgDx_out.isEmpty()) block_DgDx->LoadBlockValues(*split_DgDx, (*rowIndex)[i]);
371 
372  // Assemble multiple steps on this domain into g and dgdp(0) vectors
373  if (g_out.get()) g_out->Update(1.0, *split_g, 1.0);
374 
375  if (!DgDp_out.isEmpty())
376  DgDp_out.getMultiVector()->Update(1.0, *split_DgDp, 1.0);
377 
378  } // End loop over multiPoint steps on this domain/cluster
379 
380  //Copy block vectors into *_out vectors of same size
381  if (f_out.get()) f_out->operator=(*block_f);
382  if (!DfDp_out.isEmpty())
383  DfDp_out.getMultiVector()->operator=(*block_DfDp);
384  if (!DgDx_out.isEmpty())
385  DgDx_out.getMultiVector()->operator=(*block_DgDx);
386 
387  //Sum together obj fn contributions from differnt Domains (clusters).
388  if (numTimeDomains > 1) {
389  double factorToZeroOutCopies = 0.0;
390  if (globalComm->SubDomainComm().MyPID()==0) factorToZeroOutCopies = 1.0;
391  if (g_out.get()) {
392  (*g_out).Scale(factorToZeroOutCopies);
393  double* vPtr = &((*g_out)[0]);
394  Epetra_Vector tmp = *(g_out.get());
395  globalComm->SumAll( &(tmp[0]), vPtr, num_g0);
396  }
397  if (!DgDp_out.isEmpty()) {
398  DgDp_out.getMultiVector()->Scale(factorToZeroOutCopies);
399  double* mvPtr = (*DgDp_out.getMultiVector())[0];
400  Epetra_MultiVector tmp = *(DgDp_out.getMultiVector());
401  globalComm->SumAll(tmp[0], mvPtr, num_dg0dp0);
402  }
403  }
404 }
bool supports(EOutArgsMembers arg) const
void set_W(const Teuchos::RefCountPtr< Epetra_Operator > &W)
void set_W_properties(const DerivativeProperties &properties)
Evaluation< Epetra_Vector > get_g(int j) const
Get g(j) where 0 <= j && j < this->Ng().
Teuchos::RefCountPtr< const Epetra_Map > get_x_map() const
void set_DgDx(int j, const Derivative &DgDx_j)
Teuchos::RefCountPtr< const Epetra_Map > get_f_map() const
void setSupports(EOutArgsMembers arg, bool supports=true)
Teuchos::RefCountPtr< Epetra_MultiVector > getMultiVector() const
void set_DfDp(int l, const Derivative &DfDp_l)
void evalModel(const InArgs &inArgs, const OutArgs &outArgs) const
void set_f(const Evaluation< Epetra_Vector > &f)
void setSupports(EInArgsMembers arg, bool supports=true)
Simple aggregate class that stores a derivative object as a general linear operator or as a multi-vec...
Teuchos::RefCountPtr< Epetra_Operator > create_W() const
int ExtractBlockValues(Epetra_Vector &BaseVec, int BlockRow) const
Extract a single block from a Block Vector: block row is global, not a stencil value.
Simple aggregate class for a derivative object represented as a column-wise multi-vector or its trans...
Teuchos::RefCountPtr< const Epetra_Vector > get_p(int l) const
void set_DgDp_properties(int j, int l, const DerivativeProperties &properties)
Teuchos::RefCountPtr< const Epetra_Map > get_g_map(int j) const
.
Teuchos::RefCountPtr< const Epetra_Vector > get_x_init() const
void set_DfDp_properties(int l, const DerivativeProperties &properties)
void setModelEvalDescription(const std::string &modelEvalDescription)
Teuchos::RefCountPtr< const Epetra_Vector > get_x() const
Set solution vector Taylor polynomial.
Teuchos::RefCountPtr< const Epetra_Map > get_p_map(int l) const
.
Derivative get_DgDp(int j, int l) const
void set_x(const Teuchos::RefCountPtr< const Epetra_Vector > &x)
void setModelEvalDescription(const std::string &modelEvalDescription)
void set_DgDp(int j, int l, const Derivative &DgDp_j_l)
Teuchos::RefCountPtr< Epetra_Operator > get_W() const
MultiPointModelEvaluator(Teuchos::RefCountPtr< EpetraExt::ModelEvaluator > underlyingME_, const Teuchos::RefCountPtr< EpetraExt::MultiComm > &globalComm_, const std::vector< Epetra_Vector * > initGuessVec, Teuchos::RefCountPtr< std::vector< Teuchos::RefCountPtr< Epetra_Vector > > > qvec, Teuchos::RefCountPtr< std::vector< Teuchos::RefCountPtr< Epetra_Vector > > > matching_vec=Teuchos::null)
int LoadBlockValues(const Epetra_Vector &BaseVec, int BlockRow)
Load a single block into a Block Vector: block row is global, not a stencil value.
void set_p(int l, const Teuchos::RefCountPtr< const Epetra_Vector > &p_l)
void set_DgDx_properties(int j, const DerivativeProperties &properties)
Evaluation< Epetra_Vector > get_f() const
int LoadBlockValues(const Epetra_MultiVector &BaseVec, int BlockRow)
Load a single block into a Block Vector: block row is global, not a stencil value.
void set_g(int j, const Evaluation< Epetra_Vector > &g_j)
Set g(j) where 0 <= j && j < this->Ng().
Teuchos::RefCountPtr< const Epetra_Vector > get_p_init(int l) const