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.35 $
40 //
41 // Revision Date : $Date: 2014/02/24 23:49:25 $
42 //
43 // Current Owner : $Author: tvrusso $
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 :
73 // Special Notes :
74 // Scope : public
75 // Creator :
76 // Creation Date :
77 //-----------------------------------------------------------------------------
78 SharedSystem::SharedSystem(N_LAS_Vector& soln,
79  N_LAS_Vector& f,
80  N_LAS_Matrix& jacobian,
81  N_LAS_Vector& newton,
82  N_LAS_Vector& gradient,
83  N_LAS_System& lasSys,
84  N_NLS_NOX::Interface& interface) :
85 
86  xyceSolnPtr_(0),
87  xyceFPtr_(0),
88  xyceJacobianPtr_(0),
89  xyceNewtonPtr_(0),
90  xyceGradientPtr_(0),
91  xyceLasSysPtr_(0),
92  xyceInterfacePtr_(0),
93  matrixFreeFlag_(false),
94  ownerOfJacobian_(0),
95  ownerOfStateVectors_(0),
96  ifpackGraphPtr_(0),
97  ifpackPreconditionerPtr_(0)
98 {
99  reset(soln, f, jacobian, newton, gradient, lasSys, interface);
100 }
101 
102 //-----------------------------------------------------------------------------
103 // Function : N_NLS_NOX::SharedSystem::~SharedSystem
104 // Purpose :
105 // Special Notes :
106 // Scope : public
107 // Creator :
108 // Creation Date :
109 //-----------------------------------------------------------------------------
111 {
113  delete xyceSolnPtr_;
114  delete xyceFPtr_;
115  delete xyceNewtonPtr_;
116  delete xyceGradientPtr_;
117 }
118 
119 //-----------------------------------------------------------------------------
120 // Function : N_NLS_NOX::SharedSystem::reset
121 // Purpose :
122 // Special Notes :
123 // Scope : public
124 // Creator :
125 // Creation Date :
126 //-----------------------------------------------------------------------------
127 void SharedSystem::reset(N_LAS_Vector& x,
128  N_LAS_Vector& f,
129  N_LAS_Matrix& jacobian,
130  N_LAS_Vector& newton,
131  N_LAS_Vector& gradient,
132  N_LAS_System& lasSys,
133  N_NLS_NOX::Interface& interface)
134 {
135  // Clear out old views
136  delete xyceSolnPtr_;
137  delete xyceFPtr_;
138  delete xyceNewtonPtr_;
139  delete xyceGradientPtr_;
140 
141  xyceJacobianPtr_ = &jacobian;
142  xyceLasSysPtr_ = &lasSys;
143  xyceInterfacePtr_ = &interface;
145 
146  // Create views of the data used for fills in xyce
147  xyceSolnPtr_ = new N_NLS_NOX::Vector(x, lasSys);
148  xyceFPtr_ = new N_NLS_NOX::Vector(f, lasSys);
149  xyceNewtonPtr_ = new N_NLS_NOX::Vector(newton, lasSys);
150  xyceGradientPtr_ = new N_NLS_NOX::Vector(gradient, lasSys);
151 
152  // Wipe the preconditioner clean
154 }
155 
156 //-----------------------------------------------------------------------------
157 // Function : N_NLS_NOX::SharedSystem::computeF
158 // Purpose :
159 // Special Notes :
160 // Scope : public
161 // Creator :
162 // Creation Date :
163 //-----------------------------------------------------------------------------
164 bool SharedSystem::computeF(const Vector& solution, Vector& F,
165  const Group* grp)
166 {
167  ownerOfStateVectors_ = grp;
168 
169 #ifdef Xyce_NOX_USE_VECTOR_COPY
170  *xyceSolnPtr_ = solution;
171  bool status = xyceInterfacePtr_->computeF();
172 #else
173  bool status = xyceInterfacePtr_->computeF(F, solution);
174 #endif
175 
176  if (status == false) {
177  const string message = "Error: N_NLS_NOX::SharedSystem::computeF() - compute F failed!";
178  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
179  }
180 
181 #ifdef Xyce_NOX_USE_VECTOR_COPY
182  F = *xyceFPtr_;
183 #endif
184  return status;
185 }
186 
187 //-----------------------------------------------------------------------------
188 // Function : N_NLS_NOX::SharedSystem::computeJacobian
189 // Purpose :
190 // Special Notes :
191 // Scope : public
192 // Creator :
193 // Creation Date :
194 //-----------------------------------------------------------------------------
196 {
197  ownerOfJacobian_ = grp;
198 
199 #ifdef Xyce_NOX_USE_VECTOR_COPY
200  *xyceSolnPtr_ = grp->getX();
201 #endif
202 
203  if (!areStateVectors(grp)) {
204 #ifdef Xyce_VERBOSE_NOX
205  if (1) { //RPP: Need to add priting utilities to group ctor
206  cout << "Warning: N_NLS_NOX::SharedSystem::computeJacobian() - State "
207  << "Vectors are not valid wrt solution!" << endl;
208  cout << "Calling computeResidual to fix this!" << endl;
209  }
210 #endif
211  // RPP: This line is not needed since we now call the group
212  //ownerOfStateVectors_ = grp;
213 
214  NOX::Abstract::Group::ReturnType status = grp->computeF();
215 
216  if (status != NOX::Abstract::Group::Ok) {
217  const string message = "N_NLS_NOX::SharedSystem::computeJacobian() - compute F failed!";
218  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
219  }
220 
221  }
222 
223 #ifdef Xyce_NOX_USE_VECTOR_COPY
224  bool status = xyceInterfacePtr_->computeJacobian();
225 #else
226  bool status = xyceInterfacePtr_->computeJacobian
227  (dynamic_cast<const N_NLS_NOX::Vector &> (grp->getX()));
228 #endif
229 
230  if (status == false) {
231  const string message = "N_NLS_NOX::SharedSystem::computeJacobian() - compute Jac failed!";
232  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
233  }
234 
235  return status;
236 }
237 
238 //-----------------------------------------------------------------------------
239 // Function : N_NLS_NOX::SharedSystem::computeNewton
240 // Purpose :
241 // Special Notes :
242 // Scope : public
243 // Creator :
244 // Creation Date :
245 //-----------------------------------------------------------------------------
247  Teuchos::ParameterList& params)
248 {
249  *xyceFPtr_ = F;
250  // Zero out the Newton vector
251  xyceNewtonPtr_->scale(0.0);
252  bool status = xyceInterfacePtr_->computeNewton(params);
253  Newton = *xyceNewtonPtr_;
254 
255  return status;
256 }
257 
258 //-----------------------------------------------------------------------------
259 // Function : N_NLS_NOX::SharedSystem::computeGradient
260 // Purpose :
261 // Special Notes :
262 // Scope : public
263 // Creator :
264 // Creation Date :
265 //-----------------------------------------------------------------------------
266 bool SharedSystem::computeGradient(const Vector& F, Vector& Gradient)
267 {
268  *xyceFPtr_ = F;
269  xyceGradientPtr_->scale(0.0);
270  bool status = xyceInterfacePtr_->computeGradient();
271  Gradient = *xyceGradientPtr_;
272  return status;
273 }
274 
275 //-----------------------------------------------------------------------------
276 // Function : N_NLS_NOX::SharedSystem::applyJacobian
277 // Purpose :
278 // Special Notes :
279 // Scope : public
280 // Creator :
281 // Creation Date :
282 //-----------------------------------------------------------------------------
283 bool SharedSystem::applyJacobian(const Vector& input, Vector& result) const
284 {
285  if (!matrixFreeFlag_) {
286  bool NoTranspose = false;
287  xyceJacobianPtr_->matvec(NoTranspose, input.getNativeVectorRef(), result.getNativeVectorRef());
288  } else {
289  // tscoffe/tmei HB 07/29/08
290 #ifndef Xyce_NOX_USE_VECTOR_COPY
291  const string message = "N_NLS_NOX::SharedSystem::applyJacobian() - ERROR, Xyce_NOX_USE_VECTOR_COPY required";
292  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
293 #endif
294  bool status = xyceInterfacePtr_->applyJacobian(input.getNativeVectorRef(), result.getNativeVectorRef());
295  if (status == false) {
296  const string message = "N_NLS_NOX::SharedSystem::applyJacobian() - apply Jac failed!";
297  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
298  }
299  }
300  return true;
301 }
302 
303 //-----------------------------------------------------------------------------
304 // Function : N_NLS_NOX::SharedSystem::applyJacobianTranspose
305 // Purpose :
306 // Special Notes :
307 // Scope : public
308 // Creator :
309 // Creation Date :
310 //-----------------------------------------------------------------------------
311 bool SharedSystem::applyJacobianTranspose(const Vector& input, Vector& result) const
312 {
313  if (!matrixFreeFlag_) {
314  bool Transpose = true;
315  xyceJacobianPtr_->matvec(Transpose, input.getNativeVectorRef(), result.getNativeVectorRef());
316  } else {
317  const string message = "N_NLS_NOX::SharedSystem::applyJacobianTranspose() - Not Supported for Matrix Free Loads!";
318  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
319  }
320  return true;
321 }
322 
323 //-----------------------------------------------------------------------------
324 // Function : N_NLS_NOX::SharedSystem::computePreconditioner
325 // Purpose :
326 // Special Notes :
327 // Scope : public
328 // Creator :
329 // Creation Date :
330 //-----------------------------------------------------------------------------
332 {
333  Epetra_CrsMatrix* crs = dynamic_cast<Epetra_CrsMatrix*>
334  (&(xyceJacobianPtr_->epetraObj()));
335 
336  if (crs == 0) {
337  cout << "N_NLS_NOX::SharedSystem::computePreconditioner() - "
338  << "Dynamic cast to CRS Matrix failed!" << endl;
339  }
340 
342  ifpackGraphPtr_ = new Ifpack_IlukGraph(crs->Graph(),
343  1,
344  0);
345  ifpackGraphPtr_->ConstructFilledGraph();
346  ifpackPreconditionerPtr_ = new Ifpack_CrsRiluk(*ifpackGraphPtr_);
347  ifpackPreconditionerPtr_->InitValues(*crs);
348  ifpackPreconditionerPtr_->Factor();
349  return true;
350 }
351 
352 //-----------------------------------------------------------------------------
353 // Function : N_NLS_NOX::SharedSystem::deletePreconditioner
354 // Purpose :
355 // Special Notes :
356 // Scope : public
357 // Creator :
358 // Creation Date :
359 //-----------------------------------------------------------------------------
361 {
363  delete ifpackGraphPtr_;
365  ifpackGraphPtr_ = 0;
366  return true;
367 }
368 
369 //-----------------------------------------------------------------------------
370 // Function : N_NLS_NOX::SharedSystem::applyRightPreconditioning
371 // Purpose :
372 // Special Notes :
373 // Scope : public
374 // Creator :
375 // Creation Date :
376 //-----------------------------------------------------------------------------
378  Teuchos::ParameterList& params,
379  const Vector& input,
380  Vector& result)
381 {
382  if (ifpackPreconditionerPtr_ == 0) {
383  const string message = "N_NLS_NOX::SharedSystem::applyRightPreconditioning - Preconditioner is 0!";
384  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
385  }
386 
387  if (useTranspose)
388  ifpackPreconditionerPtr_->SetUseTranspose(useTranspose);
389 
390  N_LAS_Vector& nonConstInput =
391  const_cast<N_LAS_Vector&>(input.getNativeVectorRef_());
392  Epetra_MultiVector& epVecInput =
393  const_cast<Epetra_MultiVector&>(nonConstInput.epetraObj());
394 
395  N_LAS_Vector& nonConstResult =
396  const_cast<N_LAS_Vector&>(result.getNativeVectorRef_());
397  Epetra_MultiVector& epVecResult =
398  const_cast<Epetra_MultiVector&>(nonConstResult.epetraObj());
399 
400  int errorCode = ifpackPreconditionerPtr_->
401  ApplyInverse(epVecInput, epVecResult);
402 
403  // Unset the transpose call
404  if (useTranspose)
405  ifpackPreconditionerPtr_->SetUseTranspose(false);
406 
407  return true;
408 }
409 
410 //-----------------------------------------------------------------------------
411 // Function : N_NLS_NOX::SharedSystem::getSolutionVector
412 // Purpose :
413 // Special Notes :
414 // Scope : public
415 // Creator :
416 // Creation Date :
417 //-----------------------------------------------------------------------------
419 {
420  return *xyceSolnPtr_;
421 }
422 
423 //-----------------------------------------------------------------------------
424 // Function : N_NLS_NOX::SharedSystem::getJacobian
425 // Purpose :
426 // Special Notes :
427 // Scope : public
428 // Creator :
429 // Creation Date :
430 //-----------------------------------------------------------------------------
431 const N_LAS_Matrix& SharedSystem::getJacobian() const
432 {
433  return *xyceJacobianPtr_;
434 }
435 
436 //-----------------------------------------------------------------------------
437 // Function : N_NLS_NOX::SharedSystem::getJacobian
438 // Purpose :
439 // Special Notes :
440 // Scope : public
441 // Creator :
442 // Creation Date :
443 //-----------------------------------------------------------------------------
444 N_LAS_Matrix& SharedSystem::getJacobian(const Group* grp)
445 {
446  ownerOfJacobian_ = grp;
447  return *xyceJacobianPtr_;
448 }
449 
450 //-----------------------------------------------------------------------------
451 // Function : N_NLS_NOX::SharedSystem::getStateVectors
452 // Purpose :
453 // Special Notes :
454 // Scope : public
455 // Creator :
456 // Creation Date :
457 //-----------------------------------------------------------------------------
459 {
460  ownerOfStateVectors_ = grp;
461 }
462 
463 //-----------------------------------------------------------------------------
464 // Function : N_NLS_NOX::SharedSystem::getLasSystem
465 // Purpose :
466 // Special Notes :
467 // Scope : public
468 // Creator :
469 // Creation Date :
470 //-----------------------------------------------------------------------------
472 {
473  return xyceLasSysPtr_;
474 }
475 
476 //-----------------------------------------------------------------------------
477 // Function : N_NLS_NOX::SharedSystem::cloneSolutionVector
478 // Purpose :
479 // Special Notes :
480 // Scope : public
481 // Creator :
482 // Creation Date :
483 //-----------------------------------------------------------------------------
485 {
486  N_NLS_NOX::Vector* tmpVectorPtr = 0;
487  tmpVectorPtr =
488  dynamic_cast<N_NLS_NOX::Vector*>(xyceSolnPtr_->clone(NOX::DeepCopy).release().get());
489 
490  if (tmpVectorPtr == 0) {
491  const string message =
492  "N_NLS_NOX::SharedSystem::cloneSolutionVector() - dynamic cast/ memory allocation failure!";
493  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL, message);
494  }
495 
496  return (tmpVectorPtr);
497 }
498 
499 //-----------------------------------------------------------------------------
500 // Function : N_NLS_NOX::SharedSystem:: getNewtonVector
501 // Purpose :
502 // Special Notes :
503 // Scope : public
504 // Creator :
505 // Creation Date :
506 //-----------------------------------------------------------------------------
508 {
509  return *xyceNewtonPtr_;
510 }
511 
512 #ifdef Xyce_DEBUG_NONLINEAR
513 //-----------------------------------------------------------------------------
514 // Function : N_NLS_NOX::SharedSystem::debugOutput1
515 // Purpose :
516 // Special Notes :
517 // Scope : public
518 // Creator :
519 // Creation Date :
520 //-----------------------------------------------------------------------------
521 void SharedSystem::debugOutput1
522  (N_LAS_Matrix & jacobian, N_LAS_Vector & rhs)
523 {
524  xyceInterfacePtr_->debugOutput1(jacobian, rhs);
525 }
526 
527 //-----------------------------------------------------------------------------
528 // Function : N_NLS_NOX::SharedSystem::debugOutput3
529 // Purpose :
530 // Special Notes :
531 // Scope : public
532 // Creator :
533 // Creation Date :
534 //-----------------------------------------------------------------------------
535 void SharedSystem::debugOutput3
536  (N_LAS_Vector & dxVector, N_LAS_Vector & xVector)
537 {
538  xyceInterfacePtr_->debugOutput3(dxVector, xVector);
539 }
540 #endif
541