Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
N_NLS_NOX_SharedSystem.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // Copyright Notice
3 //
4 // Copyright 2002 Sandia Corporation. Under the terms
5 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
6 // Government retains certain rights in this software.
7 //
8 // Xyce(TM) Parallel Electrical Simulator
9 // Copyright (C) 2002-2014 Sandia Corporation
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //-----------------------------------------------------------------------------
24 
25 //-------------------------------------------------------------------------
26 // Filename : $RCSfile: N_NLS_NOX_SharedSystem.C,v $
27 //
28 // Purpose : Interface to Xyce vectors for NOX.
29 //
30 // Special Notes :
31 //
32 // Creator : Tammy Kolda, NLS, 8950
33 //
34 // Creation Date : 01/31/02
35 //
36 // Revision Information:
37 // ---------------------
38 //
39 // Revision Number: $Revision: 1.36 $
40 //
41 // Revision Date : $Date: 2014/05/08 16:40:44 $
42 //
43 // Current Owner : $Author: erkeite $
44 //-------------------------------------------------------------------------
45 
46 #include <Xyce_config.h>
47 
48 
49 // ---------- Standard Includes ----------
50 
51 // ---------- Xyce Includes ----------
52 #include <N_UTL_Misc.h>
53 
54 #include "N_NLS_NOX_SharedSystem.h"
55 #include "N_NLS_NOX_Interface.h"
56 #include "N_LAS_Vector.h"
57 #include "N_LAS_Matrix.h"
58 #include "N_LAS_System.h"
59 #include "N_LAS_Builder.h"
60 #include "N_ERH_ErrorMgr.h"
61 #include "Epetra_CrsMatrix.h"
62 #include "Ifpack_IlukGraph.h"
63 #include "Ifpack_CrsRiluk.h"
64 
65 // ---------- NOX Includes ----------
66 
67 // ---------- Namespaces ---------------
68 using namespace N_NLS_NOX;
69 
70 //-----------------------------------------------------------------------------
71 // Function : N_NLS_NOX::SharedSystem::SharedSystem
72 // Purpose : Constructor. Creates a shared system containing
73 // the soln vector, the previous solution vector,
74 // the RHS vector, the Newton vector, the Jacobian
75 // matrix, and a reference to the interface used to
76 // call the evaluation functions.
77 // Special Notes :
78 // Scope : public
79 // Creator :
80 // Creation Date :
81 //-----------------------------------------------------------------------------
82 SharedSystem::SharedSystem(N_LAS_Vector& soln,
83  N_LAS_Vector& f,
84  N_LAS_Matrix& jacobian,
85  N_LAS_Vector& newton,
86  N_LAS_Vector& gradient,
87  N_LAS_System& lasSys,
88  N_NLS_NOX::Interface& interface) :
89 
90  xyceSolnPtr_(0),
91  xyceFPtr_(0),
92  xyceJacobianPtr_(0),
93  xyceNewtonPtr_(0),
94  xyceGradientPtr_(0),
95  xyceLasSysPtr_(0),
96  xyceInterfacePtr_(0),
97  matrixFreeFlag_(false),
98  ownerOfJacobian_(0),
99  ownerOfStateVectors_(0),
100  ifpackGraphPtr_(0),
101  ifpackPreconditionerPtr_(0)
102 {
103  reset(soln, f, jacobian, newton, gradient, lasSys, interface);
104 }
105 
106 //-----------------------------------------------------------------------------
107 // Function : N_NLS_NOX::SharedSystem::~SharedSystem
108 // Purpose : Destructor.
109 // Special Notes :
110 // Scope : public
111 // Creator :
112 // Creation Date :
113 //-----------------------------------------------------------------------------
115 {
117  delete xyceSolnPtr_;
118  delete xyceFPtr_;
119  delete xyceNewtonPtr_;
120  delete xyceGradientPtr_;
121 }
122 
123 //-----------------------------------------------------------------------------
124 // Function : N_NLS_NOX::SharedSystem::reset
125 // Purpose : reset the Xyce fill objects - pointers may have changed!
126 // Special Notes :
127 // Scope : public
128 // Creator :
129 // Creation Date :
130 //-----------------------------------------------------------------------------
131 void SharedSystem::reset(N_LAS_Vector& x,
132  N_LAS_Vector& f,
133  N_LAS_Matrix& jacobian,
134  N_LAS_Vector& newton,
135  N_LAS_Vector& gradient,
136  N_LAS_System& lasSys,
137  N_NLS_NOX::Interface& interface)
138 {
139  // Clear out old views
140  delete xyceSolnPtr_;
141  delete xyceFPtr_;
142  delete xyceNewtonPtr_;
143  delete xyceGradientPtr_;
144 
145  xyceJacobianPtr_ = &jacobian;
146  xyceLasSysPtr_ = &lasSys;
147  xyceInterfacePtr_ = &interface;
149 
150  // Create views of the data used for fills in xyce
151  xyceSolnPtr_ = new N_NLS_NOX::Vector(x, lasSys);
152  xyceFPtr_ = new N_NLS_NOX::Vector(f, lasSys);
153  xyceNewtonPtr_ = new N_NLS_NOX::Vector(newton, lasSys);
154  xyceGradientPtr_ = new N_NLS_NOX::Vector(gradient, lasSys);
155 
156  // Wipe the preconditioner clean
158 }
159 
160 //-----------------------------------------------------------------------------
161 // Function : N_NLS_NOX::SharedSystem::computeF
162 // Purpose : Compute the F corresponding to the current
163 // primary solution vector. Makes the primary
164 // solution vector owner in to the owner of the F.
165 // Special Notes :
166 // Scope : public
167 // Creator :
168 // Creation Date :
169 //-----------------------------------------------------------------------------
170 bool SharedSystem::computeF(const Vector& solution, Vector& F,
171  const Group* grp)
172 {
173  ownerOfStateVectors_ = grp;
174 
175 #ifdef Xyce_NOX_USE_VECTOR_COPY
176  *xyceSolnPtr_ = solution;
177  bool status = xyceInterfacePtr_->computeF();
178 #else
179  bool status = xyceInterfacePtr_->computeF(F, solution);
180 #endif
181 
182  if (status == false) {
183  const string message = "Error: N_NLS_NOX::SharedSystem::computeF() - compute F failed!";
184  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
185  }
186 
187 #ifdef Xyce_NOX_USE_VECTOR_COPY
188  F = *xyceFPtr_;
189 #endif
190  return status;
191 }
192 
193 //-----------------------------------------------------------------------------
194 // Function : N_NLS_NOX::SharedSystem::computeJacobian
195 // Purpose : Compute the Jacobian corresponding to the current
196 // primary solution vector.
197 // Special Notes :
198 // Scope : public
199 // Creator :
200 // Creation Date :
201 //-----------------------------------------------------------------------------
203 {
204  ownerOfJacobian_ = grp;
205 
206 #ifdef Xyce_NOX_USE_VECTOR_COPY
207  *xyceSolnPtr_ = grp->getX();
208 #endif
209 
210  if (!areStateVectors(grp)) {
211 #ifdef Xyce_VERBOSE_NOX
212  if (1) { //RPP: Need to add priting utilities to group ctor
213  cout << "Warning: N_NLS_NOX::SharedSystem::computeJacobian() - State "
214  << "Vectors are not valid wrt solution!" << endl;
215  cout << "Calling computeResidual to fix this!" << endl;
216  }
217 #endif
218  // RPP: This line is not needed since we now call the group
219  //ownerOfStateVectors_ = grp;
220 
221  NOX::Abstract::Group::ReturnType status = grp->computeF();
222 
223  if (status != NOX::Abstract::Group::Ok) {
224  const string message = "N_NLS_NOX::SharedSystem::computeJacobian() - compute F failed!";
225  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
226  }
227 
228  }
229 
230 #ifdef Xyce_NOX_USE_VECTOR_COPY
231  bool status = xyceInterfacePtr_->computeJacobian();
232 #else
233  bool status = xyceInterfacePtr_->computeJacobian
234  (dynamic_cast<const N_NLS_NOX::Vector &> (grp->getX()));
235 #endif
236 
237  if (status == false) {
238  const string message = "N_NLS_NOX::SharedSystem::computeJacobian() - compute Jac failed!";
239  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
240  }
241 
242  return status;
243 }
244 
245 //-----------------------------------------------------------------------------
246 // Function : N_NLS_NOX::SharedSystem::computeNewton
247 // Purpose : Compute the Newton corresponding to the current
248 // primary solution vector.
249 // Special Notes :
250 // Scope : public
251 // Creator :
252 // Creation Date :
253 //-----------------------------------------------------------------------------
255  Teuchos::ParameterList& params)
256 {
257  *xyceFPtr_ = F;
258  // Zero out the Newton vector
259  xyceNewtonPtr_->scale(0.0);
260  bool status = xyceInterfacePtr_->computeNewton(params);
261  Newton = *xyceNewtonPtr_;
262 
263  return status;
264 }
265 
266 //-----------------------------------------------------------------------------
267 // Function : N_NLS_NOX::SharedSystem::computeGradient
268 // Purpose : Compute the Gradient corresponding to the current
269 // primary solution vector.
270 // Special Notes :
271 // Scope : public
272 // Creator :
273 // Creation Date :
274 //-----------------------------------------------------------------------------
275 bool SharedSystem::computeGradient(const Vector& F, Vector& Gradient)
276 {
277  *xyceFPtr_ = F;
278  xyceGradientPtr_->scale(0.0);
279  bool status = xyceInterfacePtr_->computeGradient();
280  Gradient = *xyceGradientPtr_;
281  return status;
282 }
283 
284 //-----------------------------------------------------------------------------
285 // Function : N_NLS_NOX::SharedSystem::applyJacobian
286 // Purpose :
287 // Special Notes :
288 // Scope : public
289 // Creator :
290 // Creation Date :
291 //-----------------------------------------------------------------------------
292 bool SharedSystem::applyJacobian(const Vector& input, Vector& result) const
293 {
294  if (!matrixFreeFlag_) {
295  bool NoTranspose = false;
296  xyceJacobianPtr_->matvec(NoTranspose, input.getNativeVectorRef(), result.getNativeVectorRef());
297  } else {
298  // tscoffe/tmei HB 07/29/08
299 #ifndef Xyce_NOX_USE_VECTOR_COPY
300  const string message = "N_NLS_NOX::SharedSystem::applyJacobian() - ERROR, Xyce_NOX_USE_VECTOR_COPY required";
301  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
302 #endif
303  bool status = xyceInterfacePtr_->applyJacobian(input.getNativeVectorRef(), result.getNativeVectorRef());
304  if (status == false) {
305  const string message = "N_NLS_NOX::SharedSystem::applyJacobian() - apply Jac failed!";
306  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
307  }
308  }
309  return true;
310 }
311 
312 //-----------------------------------------------------------------------------
313 // Function : N_NLS_NOX::SharedSystem::applyJacobianTranspose
314 // Purpose :
315 // Special Notes :
316 // Scope : public
317 // Creator :
318 // Creation Date :
319 //-----------------------------------------------------------------------------
320 bool SharedSystem::applyJacobianTranspose(const Vector& input, Vector& result) const
321 {
322  if (!matrixFreeFlag_) {
323  bool Transpose = true;
324  xyceJacobianPtr_->matvec(Transpose, input.getNativeVectorRef(), result.getNativeVectorRef());
325  } else {
326  const string message = "N_NLS_NOX::SharedSystem::applyJacobianTranspose() - Not Supported for Matrix Free Loads!";
327  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
328  }
329  return true;
330 }
331 
332 //-----------------------------------------------------------------------------
333 // Function : N_NLS_NOX::SharedSystem::computePreconditioner
334 // Purpose :
335 // Special Notes :
336 // Scope : public
337 // Creator :
338 // Creation Date :
339 //-----------------------------------------------------------------------------
341 {
342  Epetra_CrsMatrix* crs = dynamic_cast<Epetra_CrsMatrix*>
343  (&(xyceJacobianPtr_->epetraObj()));
344 
345  if (crs == 0) {
346  cout << "N_NLS_NOX::SharedSystem::computePreconditioner() - "
347  << "Dynamic cast to CRS Matrix failed!" << endl;
348  }
349 
351  ifpackGraphPtr_ = new Ifpack_IlukGraph(crs->Graph(),
352  1,
353  0);
354  ifpackGraphPtr_->ConstructFilledGraph();
355  ifpackPreconditionerPtr_ = new Ifpack_CrsRiluk(*ifpackGraphPtr_);
356  ifpackPreconditionerPtr_->InitValues(*crs);
357  ifpackPreconditionerPtr_->Factor();
358  return true;
359 }
360 
361 //-----------------------------------------------------------------------------
362 // Function : N_NLS_NOX::SharedSystem::deletePreconditioner
363 // Purpose :
364 // Special Notes :
365 // Scope : public
366 // Creator :
367 // Creation Date :
368 //-----------------------------------------------------------------------------
370 {
372  delete ifpackGraphPtr_;
374  ifpackGraphPtr_ = 0;
375  return true;
376 }
377 
378 //-----------------------------------------------------------------------------
379 // Function : N_NLS_NOX::SharedSystem::applyRightPreconditioning
380 // Purpose :
381 // Special Notes :
382 // Scope : public
383 // Creator :
384 // Creation Date :
385 //-----------------------------------------------------------------------------
387  Teuchos::ParameterList& params,
388  const Vector& input,
389  Vector& result)
390 {
391  if (ifpackPreconditionerPtr_ == 0) {
392  const string message = "N_NLS_NOX::SharedSystem::applyRightPreconditioning - Preconditioner is 0!";
393  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
394  }
395 
396  if (useTranspose)
397  ifpackPreconditionerPtr_->SetUseTranspose(useTranspose);
398 
399  N_LAS_Vector& nonConstInput =
400  const_cast<N_LAS_Vector&>(input.getNativeVectorRef_());
401  Epetra_MultiVector& epVecInput =
402  const_cast<Epetra_MultiVector&>(nonConstInput.epetraObj());
403 
404  N_LAS_Vector& nonConstResult =
405  const_cast<N_LAS_Vector&>(result.getNativeVectorRef_());
406  Epetra_MultiVector& epVecResult =
407  const_cast<Epetra_MultiVector&>(nonConstResult.epetraObj());
408 
409  int errorCode = ifpackPreconditionerPtr_->
410  ApplyInverse(epVecInput, epVecResult);
411 
412  // Unset the transpose call
413  if (useTranspose)
414  ifpackPreconditionerPtr_->SetUseTranspose(false);
415 
416  return true;
417 }
418 
419 //-----------------------------------------------------------------------------
420 // Function : N_NLS_NOX::SharedSystem::getSolutionVector
421 // Purpose :
422 // Special Notes :
423 // Scope : public
424 // Creator :
425 // Creation Date :
426 //-----------------------------------------------------------------------------
428 {
429  return *xyceSolnPtr_;
430 }
431 
432 //-----------------------------------------------------------------------------
433 // Function : N_NLS_NOX::SharedSystem::getJacobian
434 // Purpose :
435 // Special Notes :
436 // Scope : public
437 // Creator :
438 // Creation Date :
439 //-----------------------------------------------------------------------------
440 const N_LAS_Matrix& SharedSystem::getJacobian() const
441 {
442  return *xyceJacobianPtr_;
443 }
444 
445 //-----------------------------------------------------------------------------
446 // Function : N_NLS_NOX::SharedSystem::getJacobian
447 // Purpose :
448 // Special Notes :
449 // Scope : public
450 // Creator :
451 // Creation Date :
452 //-----------------------------------------------------------------------------
453 N_LAS_Matrix& SharedSystem::getJacobian(const Group* grp)
454 {
455  ownerOfJacobian_ = grp;
456  return *xyceJacobianPtr_;
457 }
458 
459 //-----------------------------------------------------------------------------
460 // Function : N_NLS_NOX::SharedSystem::getStateVectors
461 // Purpose :
462 // Special Notes :
463 // Scope : public
464 // Creator :
465 // Creation Date :
466 //-----------------------------------------------------------------------------
468 {
469  ownerOfStateVectors_ = grp;
470 }
471 
472 //-----------------------------------------------------------------------------
473 // Function : N_NLS_NOX::SharedSystem::getLasSystem
474 // Purpose :
475 // Special Notes :
476 // Scope : public
477 // Creator :
478 // Creation Date :
479 //-----------------------------------------------------------------------------
481 {
482  return xyceLasSysPtr_;
483 }
484 
485 //-----------------------------------------------------------------------------
486 // Function : N_NLS_NOX::SharedSystem::cloneSolutionVector
487 // Purpose :
488 // Special Notes :
489 // Scope : public
490 // Creator :
491 // Creation Date :
492 //-----------------------------------------------------------------------------
494 {
495  N_NLS_NOX::Vector* tmpVectorPtr = 0;
496  tmpVectorPtr =
497  dynamic_cast<N_NLS_NOX::Vector*>(xyceSolnPtr_->clone(NOX::DeepCopy).release().get());
498 
499  if (tmpVectorPtr == 0) {
500  const string message =
501  "N_NLS_NOX::SharedSystem::cloneSolutionVector() - dynamic cast/ memory allocation failure!";
502  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
503  }
504 
505  return (tmpVectorPtr);
506 }
507 
508 //-----------------------------------------------------------------------------
509 // Function : N_NLS_NOX::SharedSystem:: getNewtonVector
510 // Purpose :
511 // Special Notes :
512 // Scope : public
513 // Creator :
514 // Creation Date :
515 //-----------------------------------------------------------------------------
517 {
518  return *xyceNewtonPtr_;
519 }
520 
521 #ifdef Xyce_DEBUG_NONLINEAR
522 //-----------------------------------------------------------------------------
523 // Function : N_NLS_NOX::SharedSystem::debugOutput1
524 // Purpose :
525 // Special Notes :
526 // Scope : public
527 // Creator :
528 // Creation Date :
529 //-----------------------------------------------------------------------------
530 void SharedSystem::debugOutput1
531  (N_LAS_Matrix & jacobian, N_LAS_Vector & rhs)
532 {
533  xyceInterfacePtr_->debugOutput1(jacobian, rhs);
534 }
535 
536 //-----------------------------------------------------------------------------
537 // Function : N_NLS_NOX::SharedSystem::debugOutput3
538 // Purpose :
539 // Special Notes :
540 // Scope : public
541 // Creator :
542 // Creation Date :
543 //-----------------------------------------------------------------------------
544 void SharedSystem::debugOutput3
545  (N_LAS_Vector & dxVector, N_LAS_Vector & xVector)
546 {
547  xyceInterfacePtr_->debugOutput3(dxVector, xVector);
548 }
549 #endif
550