Xyce  6.1
N_NLS_NonLinearSolver.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-2015 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_NonLinearSolver.C,v $
27 //
28 // Purpose : Body file which declares an interface common to all
29 // supported nonlinear solver algorithms. The Manager class
30 // uses this interface to call a concrete algorithm.
31 //
32 // Special Notes : This is the "Strategy" class in the Strategy design
33 // pattern.
34 //
35 // Creator : Scott A. Hutchinson, SNL, Parallel Computational Sciences
36 //
37 // Creation Date : 04/28/00
38 //
39 // Revision Information:
40 // ---------------------
41 //
42 // Revision Number: $Revision: 1.162 $
43 //
44 // Revision Date : $Date: 2015/07/28 22:21:49 $
45 //
46 // Current Owner : $Author: hkthorn $
47 //-------------------------------------------------------------------------
48 
49 #include <Xyce_config.h>
50 
51 
52 // ---------- Standard Includes ----------
53 
54 // ---------- Xyce Includes ----------
55 #include <N_UTL_fwd.h>
56 
57 #include <N_ANP_AnalysisManager.h>
58 #include <N_ERH_ErrorMgr.h>
59 #include <N_IO_CmdParse.h>
60 #include <N_IO_OutputMgr.h>
61 #include <N_LAS_Builder.h>
62 #include <N_LAS_Matrix.h>
63 #include <N_LAS_PrecondFactory.h>
64 #include <N_LAS_Problem.h>
65 #include <N_LAS_Solver.h>
66 #include <N_LAS_SolverFactory.h>
67 #include <N_LAS_System.h>
68 #include <N_LAS_Vector.h>
70 #include <N_NLS_ConstraintBT.h>
71 #include <N_NLS_Manager.h>
73 #include <N_NLS_NonLinearSolver.h>
74 #include <N_NLS_TwoLevelNewton.h>
75 #include <N_PDS_Manager.h>
76 #include <N_PDS_ParComm.h>
77 #include <N_TIA_DataStore.h>
78 #include <N_UTL_Diagnostic.h>
79 #include <N_UTL_FeatureTest.h>
80 #include <N_UTL_OptionBlock.h>
81 
82 namespace Xyce {
83 namespace Nonlinear {
84 
85 //-----------------------------------------------------------------------------
86 // Function : NonLinearSolver::NonLinearSolver
87 // Purpose : Constructor
88 // Special Notes :
89 // Scope : public
90 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
91 // Creation Date : 5/01/00
92 //-----------------------------------------------------------------------------
93 NonLinearSolver::NonLinearSolver(const IO::CmdParse &cp)
94  : commandLine_(cp),
95  netlistFilename_(""),
96 
97  nextSolVectorPtrPtr_(0),
98  currSolVectorPtrPtr_(0),
99  tmpSolVectorPtrPtr_(0),
100  rhsVectorPtr_(0),
101 
102  jacTestMatrixPtr_(0),
103  dFdxTestMatrixPtr_(0),
104  dQdxTestMatrixPtr_(0),
105  dxVoltlimVectorPtr_(0),
106  jdxVLVectorPtr_(0),
107  fdxVLVectorPtr_(0),
108  qdxVLVectorPtr_(0),
109 
110  jacobianMatrixPtr_(0),
111  gradVectorPtr_(0),
112  NewtonVectorPtr_(0),
113  solWtVectorPtr_(0),
114  lasSysPtr_(0),
115  lasSolverPtr_(0),
116  lasPrecPtr_(0),
117  linsolOptionBlockPtr_(0),
118  nonlinearEquationLoader_(0),
119  analysisManager_(0),
120  tlnPtr_(0),
121  nonlinearParameterManager_(0),
122 
123  outMgrPtr_(0),
125  pdsMgrPtr_(0),
126  dsPtr_(0),
127 
128  numJacobianLoads_(0),
129  numJacobianFactorizations_(0),
130  numLinearSolves_(0),
131  numFailedLinearSolves_(0),
132  numResidualLoads_(0),
133  totalNumLinearIters_(0),
134 
135  totalLinearSolveTime_(0.0),
136  totalResidualLoadTime_(0.0),
137  totalJacobianLoadTime_(0.0),
138 
139  matrixFreeFlag_(false),
140  outputStepNumber_(0),
141  debugTimeFlag_(true),
142  contStep_(0)
143 {
145 
146  if (commandLine_.getArgumentValue("netlist") != "")
147  {
148  netlistFilename_ = commandLine_.getArgumentValue("netlist");
149  }
150 }
151 
152 
153 //-----------------------------------------------------------------------------
154 // Function : NonLinearSolver::~NonLinearSolver
155 // Purpose : Destructor
156 // Special Notes :
157 // Scope : public
158 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
159 // Creation Date : 5/01/00
160 //-----------------------------------------------------------------------------
162 {
163  if (NewtonVectorPtr_)
164  {
165  delete NewtonVectorPtr_;
166  NewtonVectorPtr_ = 0;
167  }
168 
169  if (gradVectorPtr_)
170  {
171  delete gradVectorPtr_;
172  gradVectorPtr_ = 0;
173  }
174 
175  if (solWtVectorPtr_)
176  {
177  delete solWtVectorPtr_;
178  solWtVectorPtr_ = 0;
179  }
180 
182  {
183  delete linsolOptionBlockPtr_;
185  }
186 
187  if (lasSolverPtr_)
188  {
189  delete lasSolverPtr_;
190  lasSolverPtr_ = 0;
191  }
192 
193 }
194 
195 //-----------------------------------------------------------------------------
196 // Function : NonLinearSolver::setLinsolOptions
197 //
198 // Purpose : Passes option block corresponding to "LINSOL" onto
199 // nonlinear solver.
200 //
201 // Special Notes: These options are saved to be passed onto the object from
202 // registerLinearSolver() in the initializeAll() function.
203 // This is only called if "NONLIN" is present in the
204 // circuit file.
205 //
206 // Return Type : boolean
207 //
208 // - Input Arguments -
209 //
210 // OB : Option block containing options corresponding to
211 // "LINSOL" in the netlist.
212 // Scope : public
213 // Creator : Robert Hoekstra, SNL, Parallel Computational Sciences
214 // Creation Date : 11/9/00
215 //-----------------------------------------------------------------------------
216 bool NonLinearSolver::setLinsolOptions(const Util::OptionBlock & OB)
217 {
218  linsolOptionBlockPtr_ = new Util::OptionBlock(OB);
219  return (linsolOptionBlockPtr_ != 0);
220 }
221 
222 //-----------------------------------------------------------------------------
223 // Function : NonLinearSolver::setDCOPRestartOptions
224 // Purpose :
225 // Special Notes :
226 // Scope : public
227 // Creator : Eric Keiter
228 // Creation Date : 09/17/2007
229 //-----------------------------------------------------------------------------
230 bool NonLinearSolver::setDCOPRestartOptions(const Util::OptionBlock& OB)
231 {
232  std::string msg = "DCOP restart options not supported for this solver. Use nox instead. ";
233  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
234  return true;
235 }
236 
237 //-----------------------------------------------------------------------------
238 // Function : NonLinearSolver::setICOptions
239 // Purpose :
240 // Special Notes :
241 // Scope : public
242 // Creator : Eric Keiter
243 // Creation Date : 09/17/2007
244 //-----------------------------------------------------------------------------
245 bool NonLinearSolver::setICOptions(const Util::OptionBlock& OB)
246 {
247  std::string msg = ".IC options not supported for this nonlinear solver. Use nox instead. ";
248  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
249  return true;
250 }
251 
252 //-----------------------------------------------------------------------------
253 // Function : NonLinearSolver::setNodeSetOptions
254 // Purpose :
255 // Special Notes :
256 // Scope : public
257 // Creator : Eric Keiter
258 // Creation Date : 09/25/2007
259 //-----------------------------------------------------------------------------
260 bool NonLinearSolver::setNodeSetOptions(const Util::OptionBlock& OB)
261 {
262  std::string msg = ".NODESET options not supported for this nonlinear solver. Use nox instead. ";
263  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
264  return true;
265 }
266 
267 //-----------------------------------------------------------------------------
268 // Function : NonLinearSolver::setLocaOptions
269 // Purpose :
270 // Special Notes :
271 // Scope : public
272 // Creator : Eric Keiter
273 // Creation Date :
274 //-----------------------------------------------------------------------------
275 bool NonLinearSolver::setLocaOptions (const Util::OptionBlock& OB)
276 {
277  std::string msg = "NonLinearSolver::setLocaOptions - not implemented for this solver. Use nox instead. ";
278  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
279  return true;
280 }
281 
282 //---------------------------------------------------------------------------
283 // Function : NonLinearSolver::setTwoLevelLocaOptions
284 // Purpose :
285 // Special Notes :
286 // Scope : public
287 // Creator : Eric Keiter
288 // Creation Date :
289 //-----------------------------------------------------------------------------
290 bool NonLinearSolver::setTwoLevelLocaOptions (const Util::OptionBlock& OB)
291 {
292  std::string msg = "NonLinearSolver::setTwoLevelLocaOptions - not implemented for this solver. Use nox instead.";
293  N_ERH_ErrorMgr::report(N_ERH_ErrorMgr::DEV_FATAL_0, msg);
294  return true;
295 }
296 
297 //---------------------------------------------------------------------------
298 // Function : NonLinearSolver::setTwoLevelOptions
299 // Purpose :
300 // Special Notes :
301 // Scope : public
302 // Creator : Eric Keiter
303 // Creation Date :
304 //---------------------------------------------------------------------------
305 bool NonLinearSolver::setTwoLevelOptions (const Util::OptionBlock& OB)
306 {
307  return true;
308 }
309 
310 //---------------------------------------------------------------------------
311 // Function : NonLinearSolver::setTwoLevelTranOptions
312 // Purpose :
313 // Special Notes :
314 // Scope : public
315 // Creator : Eric Keiter
316 // Creation Date :
317 //---------------------------------------------------------------------------
318 bool NonLinearSolver::setTwoLevelTranOptions (const Util::OptionBlock& OB)
319 {
320  return true;
321 }
322 
323 //-----------------------------------------------------------------------------
324 // Function : NonLinearSolver::registerRHSVector
325 // Purpose :
326 // Special Notes :
327 // Return Type : boolean
328 // Scope : public
329 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
330 // Creation Date : 5/01/00
331 //-----------------------------------------------------------------------------
332 bool NonLinearSolver::registerRHSVector(Linear::Vector* tmp_RHSVecPtr)
333 {
334  rhsVectorPtr_ = tmp_RHSVecPtr;
335  return (rhsVectorPtr_ != 0);
336 }
337 
338 //-----------------------------------------------------------------------------
339 // Function : NonLinearSolver::registerNonlinearEquationLoader
340 // Purpose :
341 // Special Notes :
342 // Return Type : boolean
343 // Scope : public
344 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
345 // Creation Date : 5/01/00
346 //-----------------------------------------------------------------------------
348 {
349  nonlinearEquationLoader_ = nonlinear_equation_loader;
350  return (nonlinearEquationLoader_ != 0);
351 }
352 
353 //-----------------------------------------------------------------------------
354 // Function : NonLinearSolver::registerLinearSystem
355 // Purpose :
356 // Special Notes :
357 // Return Type : boolean
358 // Scope : public
359 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
360 // Creation Date : 6/09/00
361 //-----------------------------------------------------------------------------
362 bool NonLinearSolver::registerLinearSystem(Linear::System* tmp_LasSysPtr)
363 {
364  lasSysPtr_ = tmp_LasSysPtr;
365  return (lasSysPtr_ != 0);
366 }
367 
368 //-----------------------------------------------------------------------------
369 // Function : NonLinearSolver::registerPrecondFactory
370 // Purpose :
371 // Special Notes :
372 // Return Type : boolean
373 // Scope : public
374 // Creator : Heidi Thornquist, SNL, Electrical & Microsystem Modeling
375 // Creation Date : 11/11/08
376 //-----------------------------------------------------------------------------
377 bool NonLinearSolver::registerPrecondFactory(const Linear::PrecondFactory *tmp_LasPrecPtr)
378 {
379  lasPrecPtr_ = tmp_LasPrecPtr;
380  return lasPrecPtr_;
381 }
382 
383 //-----------------------------------------------------------------------------
384 // Function : NonLinearSolver::registerParallelMgr
385 // Purpose :
386 // Special Notes :
387 // Return Type : boolean
388 // Scope : public
389 // Creator : Eric Keiter, Sandia
390 // Creation Date : 6/8/2013
391 //-----------------------------------------------------------------------------
392 bool NonLinearSolver::registerParallelMgr(N_PDS_Manager * pdsMgrPtr)
393 {
394  pdsMgrPtr_ = pdsMgrPtr;
395  return (pdsMgrPtr_ != 0);
396 }
397 
398 //-----------------------------------------------------------------------------
399 // Function : NonLinearSolver::registerAnalysisManager
400 // Purpose :
401 // Special Notes :
402 // Return Type : boolean
403 // Scope : public
404 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
405 // Creation Date : 5/03/00
406 //-----------------------------------------------------------------------------
408 {
409  analysisManager_ = tmp_anaIntPtr;
410  return (analysisManager_ != 0);
411 }
412 
413 //-----------------------------------------------------------------------------
414 // Function : NonLinearSolver::registerOutputMgr
415 // Purpose :
416 // Special Notes :
417 // Scope : public
418 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
419 // Creation Date : 9/23/03
420 //-----------------------------------------------------------------------------
421 bool NonLinearSolver::registerOutputMgr (IO::OutputMgr * outPtr)
422 {
423  outMgrPtr_ = outPtr;
424  return (outMgrPtr_ != 0);
425 }
426 
427 //-----------------------------------------------------------------------------
428 // Function : NonLinearSolver::registerInitialConditionsManager
429 // Purpose :
430 // Special Notes :
431 // Scope : public
432 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
433 // Creation Date : 9/23/03
434 //-----------------------------------------------------------------------------
435 bool NonLinearSolver::registerInitialConditionsManager(IO::InitialConditionsManager * outPtr)
436 {
437  initialConditionsManager_ = outPtr;
438  return (initialConditionsManager_ != 0);
439 }
440 
441 //-----------------------------------------------------------------------------
442 // Function : NonLinearSolver::registerTwoLevelSolver
443 // Purpose : This function is called in the event that the two-level
444 // Newton method has been invoked.
445 // Special Notes :
446 // Scope : public
447 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
448 // Creation Date : 10/24/02
449 //-----------------------------------------------------------------------------
451  (TwoLevelNewton * tmp_tlnPtr)
452 {
453  tlnPtr_ = tmp_tlnPtr;
454  return (tlnPtr_ != 0);
455 }
456 
457 //-----------------------------------------------------------------------------
458 // Function : NonLinearSolver::registerParamMgr
459 // Purpose : This function is called in the event that the two-level
460 // Newton method has been invoked.
461 // Special Notes :
462 // Scope : public
463 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
464 // Creation Date : 10/24/02
465 //-----------------------------------------------------------------------------
467  (ParamMgr * ptr)
468 {
469  nonlinearParameterManager_ = ptr;
470  return (nonlinearParameterManager_ != 0);
471 }
472 
473 //-----------------------------------------------------------------------------
474 // Function : NonLinearSolver::registerTIADataStore
475 // Purpose :
476 // Special Notes :
477 // Scope : public
478 // Creator : Eric Keiter
479 // Creation Date :
480 //-----------------------------------------------------------------------------
482 {
483  dsPtr_ = ds_tmp;
484  return true;
485 }
486 
487 //-----------------------------------------------------------------------------
488 // Function : NonLinearSolver::initializeAll
489 //
490 // Purpose : Called after all register and set functions.
491 // Once the various registrations have taken place,
492 // this function sets the remaining pointers.
493 //
494 // Special Notes: This function obtains the solution, temporary solution and
495 // rhs vectors from the LAS system class.
496 //
497 // Return Type : boolean
498 // Scope : public
499 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
500 // Creation Date : 6/12/00
501 //-----------------------------------------------------------------------------
503 {
504  bool bsuccess = true;
505 
506  // Check the registerLinearSystem has been successfully called.
507  if (lasSysPtr_ == 0)
508  return false;
509 
510  // get the temporaries:
511  tmpSolVectorPtrPtr_ = lasSysPtr_->getTmpSolVectorPtr();
512  bsuccess = bsuccess && (tmpSolVectorPtrPtr_ != 0);
513 
514  // get rhs vector:
515  rhsVectorPtr_ = lasSysPtr_->getRHSVector();
516  bsuccess = bsuccess && (rhsVectorPtr_ != 0);
517 
518  // get current solution vectors:
519  currSolVectorPtrPtr_ = lasSysPtr_->getCurrSolVectorPtr();
520  bsuccess = bsuccess && (currSolVectorPtrPtr_ != 0);
521 
522  // get next solution vectors:
523  nextSolVectorPtrPtr_ = lasSysPtr_->getNextSolVectorPtr();
524  bsuccess = bsuccess && (nextSolVectorPtrPtr_ != 0);
525 
526  // get jacobian:
527  jacobianMatrixPtr_ = lasSysPtr_->getJacobianMatrix();
528  bsuccess = bsuccess && (jacobianMatrixPtr_ != 0);
529 
530  Linear::Builder & bld_ = lasSysPtr_->builder();
531 
532  // create gradient vector:
533  gradVectorPtr_ = bld_.createVector();
534  bsuccess = bsuccess && (gradVectorPtr_ != 0);
535 
536  // create Newton update vector:
537  NewtonVectorPtr_ = bld_.createVector();
538  bsuccess = bsuccess && (NewtonVectorPtr_ != 0);
539 
540  // create solution weighting vector:
541  solWtVectorPtr_ = bld_.createVector();
542  bsuccess = bsuccess && (solWtVectorPtr_ != 0);
543 
544  if( !linsolOptionBlockPtr_ )
545  {
546  linsolOptionBlockPtr_ = new Util::OptionBlock();
547  linsolOptionBlockPtr_->addParam(Util::Param("TYPE", "DEFAULT"));
548  }
549 
550  Teuchos::RCP<Linear::Vector> NewtonVectorRCPtr = Teuchos::rcp(NewtonVectorPtr_, false);
551  Teuchos::RCP<Linear::Vector> rhsVectorRCPtr = Teuchos::rcp(rhsVectorPtr_, false);
552 
553  if (!matrixFreeFlag_)
554  {
555  Teuchos::RCP<Linear::Matrix> jacobianMatrixRCPtr = Teuchos::rcp(jacobianMatrixPtr_, false);
556  // Normal full matrix linear solver options
557  lasProblemRCPtr_ = rcp( new Linear::Problem( jacobianMatrixRCPtr,
558  NewtonVectorRCPtr,
559  rhsVectorRCPtr) );
560  }
561  else
562  {
563  // Matrix free harmonic balance linear solver options
564  // Create MatrixFreeLinearProblem
565  Teuchos::RCP<NonLinearSolver> NonlinearSolverRCPtr = Teuchos::rcp(this, false);
566  Teuchos::RCP<MatrixFreeEpetraOperator>
567  matFreeOp = matrixFreeEpetraOperator(
568  NonlinearSolverRCPtr,
569  NewtonVectorRCPtr,
570  rhsVectorRCPtr,
571  bld_.getSolutionMap()
572  );
573 
574  Teuchos::RCP<Epetra_Operator> epetraOperator = Teuchos::rcp_dynamic_cast<Epetra_Operator>(matFreeOp, true);
575  // Create Linear::Problem
576  lasProblemRCPtr_ = rcp( new Linear::Problem(
577  epetraOperator,
578  NewtonVectorRCPtr,
579  rhsVectorRCPtr
580  )
581  );
582  }
583 
584  lasSolverPtr_ = Linear::SolverFactory::create( *linsolOptionBlockPtr_, *lasProblemRCPtr_ , commandLine_);
585 
586  // If a preconditioner factory has been provided by the analysis package,
587  // use it to generate a preconditioner for the linear solver.
588  if (lasPrecPtr_) {
589  Teuchos::RCP<Linear::Preconditioner> precond = lasPrecPtr_->create( Teuchos::rcp( lasSysPtr_, false ) );
590  lasSolverPtr_->setPreconditioner( precond );
591  }
592 
593  if (DEBUG_NONLINEAR)
594  Xyce::dout() << "size of solution vector: " << lasSysPtr_->getGlobalSolutionSize() << std::endl
595  << "size of state vector: " << lasSysPtr_->getGlobalStateSize() << std::endl
596  << "End of NonLinearSolver::initializeAll\n";
597 
598  return bsuccess;
599 }
600 
601 //-----------------------------------------------------------------------------
602 // Function : NonLinearSolver::debugOutput1_
603 // Purpose : Write Jacobian to the file matrix.(n).txt, and the residual
604 // to the file rhs.(n).txt
605 //
606 // Special Notes : These are objects that will be availabled prior to the
607 // linear solve performed at each Newton step.
608 //
609 // Scope : public
610 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
611 // Creation Date : 10/30/00
612 //-----------------------------------------------------------------------------
614  Linear::Matrix & jacobian,
615  Linear::Vector & rhs)
616 {
617  setNonlinearDumpDebugLevel(getDebugLevel());
618  int newtStep = getNumIterations();
619  int screenOutput = getScreenOutputFlag ();
620  int contStep = getContinuationStep();
621  int paramNumber = getParameterNumber ();
622 
623  if (!debugTimeFlag_ || !isActive(Diag::NONLINEAR_DUMP_MASK)) return;
624 
625  char filename1[256]; for (int ich = 0; ich < 256; ++ich) filename1[ich] = 0;
626  char filename2[256]; for (int ich = 0; ich < 256; ++ich) filename2[ich] = 0;
627 
628  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
629  {
630  sprintf(filename1, "matrix_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
631  }
632  if (isActive(Diag::NONLINEAR_DUMP_STEP))
633  {
634  sprintf(filename1, "matrix_%03d_%03d.txt", outputStepNumber_, newtStep);
635  }
636  if (isActive(Diag::NONLINEAR_DUMP))
637  {
638  sprintf(filename1, "matrix_%03d.txt", newtStep);
639  }
640 
641  jacobian.writeToFile(filename1, false, getMMFormat () );
642 
643  if (screenOutput == 1)
644  {
645  Xyce::dout() << "\n\t***** Jacobian matrix:" << std::endl;
646  jacobian.printPetraObject(Xyce::dout());
647  }
648 
649  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
650  {
651  sprintf(filename2, "rhs_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
652  }
653  if (isActive(Diag::NONLINEAR_DUMP_STEP))
654  {
655  sprintf(filename2, "rhs_%03d_%03d.txt", outputStepNumber_, newtStep);
656  }
657  else
658  {
659  sprintf(filename2, "rhs_%03d.txt", newtStep);
660  }
661 
662  if (screenOutput == 1)
663  {
664  Xyce::dout() << "\n\t***** RHS vector:" << std::endl;
665 
666  rhs.printPetraObject(Xyce::dout());
667  }
668 
669  rhs.writeToFile(filename2);
670 
671  if (DEBUG_VOLTLIM)
673 
674  debugOutputDAE ();
675 
676 }
677 
678 //-----------------------------------------------------------------------------
679 // Function : NonLinearSolver::debugOutputJDX_VOLTLIM_
680 // Purpose : Write JDX vector to output files.
681 // Special Notes : This requires a matvec.
682 // Scope : public
683 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
684 // Creation Date : 01/05/05
685 //-----------------------------------------------------------------------------
687 {
688  setNonlinearDumpDebugLevel(getDebugLevel());
689 
690  int newtStep = getNumIterations();
691  int contStep = getContinuationStep();
692  int paramNumber = getParameterNumber ();
693 
694  char filename1[256]; for (int ich = 0; ich < 256; ++ich) filename1[ich] = 0;
695  char filename2[256]; for (int ich = 0; ich < 256; ++ich) filename2[ich] = 0;
696  char filename3[256]; for (int ich = 0; ich < 256; ++ich) filename3[ich] = 0;
697 
698  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
699  {
700  sprintf(filename1, "jdxVL_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
701  sprintf(filename2, "fdxVL_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
702  sprintf(filename3, "qdxVL_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
703  }
704  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
705  {
706  sprintf(filename1, "jdxVL_%03d_%03d.txt", outputStepNumber_, newtStep);
707  sprintf(filename2, "fdxVL_%03d_%03d.txt", outputStepNumber_, newtStep);
708  sprintf(filename3, "qdxVL_%03d_%03d.txt", outputStepNumber_, newtStep);
709  }
710  else
711  {
712  sprintf(filename1, "jdxVL_%03d.txt", newtStep);
713  sprintf(filename2, "fdxVL_%03d.txt", newtStep);
714  sprintf(filename3, "qdxVL_%03d.txt", newtStep);
715  }
716 
717  bool Transpose = false; // if set to true, the matvec does the transpose.
718 
719  jdxVLVectorPtr_->putScalar(0.0);
720  fdxVLVectorPtr_->putScalar(0.0);
721  qdxVLVectorPtr_->putScalar(0.0);
722 
723  jacTestMatrixPtr_->matvec( Transpose , *dxVoltlimVectorPtr_, *jdxVLVectorPtr_);
724  dFdxTestMatrixPtr_->matvec( Transpose , *dxVoltlimVectorPtr_, *fdxVLVectorPtr_);
725  dQdxTestMatrixPtr_->matvec( Transpose , *dxVoltlimVectorPtr_, *qdxVLVectorPtr_);
726 
727  jdxVLVectorPtr_->writeToFile(filename1);
728  fdxVLVectorPtr_->writeToFile(filename2);
729  qdxVLVectorPtr_->writeToFile(filename3);
730 
731  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
732  {
733  sprintf(filename1, "jtest_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
734  sprintf(filename2, "ftest_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
735  sprintf(filename3, "qtest_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
736  }
737  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
738  {
739  sprintf(filename1, "jtest_%03d_%03d.txt", outputStepNumber_, newtStep);
740  sprintf(filename2, "ftest_%03d_%03d.txt", outputStepNumber_, newtStep);
741  sprintf(filename3, "qtest_%03d_%03d.txt", outputStepNumber_, newtStep);
742  }
743  else
744  {
745  sprintf(filename1, "jtest_%03d.txt", newtStep);
746  sprintf(filename2, "ftest_%03d.txt", newtStep);
747  sprintf(filename3, "qtest_%03d.txt", newtStep);
748  }
749 
750  jacTestMatrixPtr_->writeToFile(filename1);
751  dFdxTestMatrixPtr_->writeToFile(filename2);
752  dQdxTestMatrixPtr_->writeToFile(filename3);
753 
754 }
755 
756 //-----------------------------------------------------------------------------
757 // Function : NonLinearSolver::debugOutputDAE
758 // Purpose : Write DAE vectors and matrices to output files.
759 // Special Notes :
760 // Scope : public
761 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
762 // Creation Date : 01/27/03
763 //-----------------------------------------------------------------------------
765 {
766  setNonlinearDumpDebugLevel(getDebugLevel());
767 
768  int newtStep = getNumIterations();
769  int contStep = getContinuationStep();
770  int paramNumber = getParameterNumber ();
771 
772  char filename1[256]; for (int ich = 0; ich < 256; ++ich) filename1[ich] = 0;
773  char filename2[256]; for (int ich = 0; ich < 256; ++ich) filename2[ich] = 0;
774 
775  char filename4[256]; for (int ich = 0; ich < 256; ++ich) filename4[ich] = 0;
776  char filename6[256]; for (int ich = 0; ich < 256; ++ich) filename6[ich] = 0;
777  char filename7[256]; for (int ich = 0; ich < 256; ++ich) filename7[ich] = 0;
778  char filename8[256]; for (int ich = 0; ich < 256; ++ich) filename8[ich] = 0;
779  char filename9[256]; for (int ich = 0; ich < 256; ++ich) filename9[ich] = 0;
780 
781  Linear::Matrix *dQdx = lasSysPtr_->getDAEdQdxMatrix ();
782  Linear::Matrix *dFdx = lasSysPtr_->getDAEdFdxMatrix ();
783 
784  Linear::Vector *daeQ = lasSysPtr_->getDAEQVector();
785  Linear::Vector *daeF = lasSysPtr_->getDAEFVector();
786 
787  Linear::Vector *daeFlim = lasSysPtr_->getdFdxdVpVector ();
788  Linear::Vector *daeQlim = lasSysPtr_->getdQdxdVpVector ();
789 
790  //Xyce::dout() << "In debugOutputDAE" << std::endl;
791 
792  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
793  {
794  sprintf(filename1, "dQdx_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
795  sprintf(filename2, "dFdx_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
796 
797  sprintf(filename4, "daeQ_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
798  sprintf(filename6, "daeF_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
799 
800  sprintf(filename8, "daeQlim_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
801  sprintf(filename9, "daeFlim_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
802  }
803  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
804  {
805  sprintf(filename1, "dQdx_%03d_%03d.txt" , outputStepNumber_, newtStep);
806  sprintf(filename2, "dFdx_%03d_%03d.txt" , outputStepNumber_, newtStep);
807 
808  sprintf(filename4, "daeQ_%03d_%03d.txt" , outputStepNumber_, newtStep);
809  sprintf(filename6, "daeF_%03d_%03d.txt" , outputStepNumber_, newtStep);
810 
811  sprintf(filename8, "daeQlim_%03d_%03d.txt" , outputStepNumber_, newtStep);
812  sprintf(filename9, "daeFlim_%03d_%03d.txt" , outputStepNumber_, newtStep);
813  }
814  else
815  {
816  sprintf(filename1, "dQdx_%03d.txt" , newtStep);
817  sprintf(filename2, "dFdx_%03d.txt" , newtStep);
818 
819  sprintf(filename4, "daeQ_%03d.txt" , newtStep);
820  sprintf(filename6, "daeF_%03d.txt" , newtStep);
821 
822  sprintf(filename8, "daeQlim_%03d.txt" , newtStep);
823  sprintf(filename9, "daeFlim_%03d.txt" , newtStep);
824  }
825 
826  // write the matrices:
827  dQdx->writeToFile (filename1, false, getMMFormat () );
828  dFdx->writeToFile (filename2, false, getMMFormat () );
829 
830  // write the vectors:
831  daeQ->writeToFile(filename4);
832  daeF->writeToFile(filename6);
833  daeQlim->writeToFile(filename8);
834  daeFlim->writeToFile(filename9);
835 }
836 
837 //-----------------------------------------------------------------------------
838 // Function : NonLinearSolver::debugOutput3_
839 // Purpose : Write out the update vector and the new solution.
840 //
841 // Special Notes : These are objects that will be availabled *after* the
842 // linear solve performed at each Newton step. That
843 // differentiates this function from debugOutput1_.
844 //
845 // Scope : public
846 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
847 // Creation Date : 10/30/00
848 //-----------------------------------------------------------------------------
850  Linear::Vector & dxVector,
851  Linear::Vector & xVector)
852 {
853  setNonlinearDumpDebugLevel(getDebugLevel());
854 
855  int nlStep = getNumIterations();
856  int contStep = getContinuationStep();
857  int paramNumber = getParameterNumber ();
858 
859  if (!debugTimeFlag_ || !isActive(Diag::NONLINEAR_DUMP_MASK)) return;
860 
861  char filename[256]; for (int ich = 0; ich < 256; ++ich) filename[ich] = 0;
862 
863  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
864  {
865  sprintf(filename, "update_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, nlStep);
866  }
867  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
868  {
869  sprintf(filename, "update_%03d_%03d.txt", outputStepNumber_, nlStep);
870  }
871  else
872  {
873  sprintf(filename, "update_%03d.txt", nlStep);
874  }
875  dxVector.writeToFile(filename);
876 
877  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
878  {
879  sprintf(filename, "solution_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, nlStep);
880  }
881  if (isActive(Diag::NONLINEAR_DUMP_STEP))
882  {
883  sprintf(filename, "solution_%03d_%03d.txt", outputStepNumber_, nlStep);
884  }
885  if (isActive(Diag::NONLINEAR_DUMP))
886  {
887  sprintf(filename, "solution_%03d.txt", nlStep);
888  }
889  xVector.writeToFile(filename);
890 }
891 
892 //-----------------------------------------------------------------------------
893 // Function : NonLinearSolver::resetCountersAndTimers_
894 //
895 // Purpose : Resets all the counters and timers in this object.
896 //
897 // Scope : protected
898 // Creator : Tamara G. Kolda, SNL, CSMR (8950)
899 // Eric Keiter, SNL, Parallel Computational Sciences. (9233)
900 // Creation Date : 01/24/02
901 //-----------------------------------------------------------------------------
903 {
904  numJacobianLoads_ = 0;
906  numLinearSolves_ = 0;
908  numResidualLoads_ = 0;
910  totalLinearSolveTime_ = 0.0;
913 }
914 
915 //-----------------------------------------------------------------------------
916 // Function : NonLinearSolver::setX0_()
917 //
918 // Purpose : This should be called at the beginning of each nonlinear
919 // iteration. Copies information from nextSolVector (and
920 // related vectors that are important but hidden from the
921 // nonlinear solver) into tmpSolVector.
922 //
923 // Return Type : boolean
924 // Scope : protected
925 // Creator : Tamara G. Kolda, SNL, CSMR (8950)
926 // Eric Keiter, SNL, Parallel Computational Sciences. (9233)
927 // Creation Date : 01/24/02
928 //-----------------------------------------------------------------------------
930 {
931 
933 
934  return true;
935 }
936 
937 //-----------------------------------------------------------------------------
938 // Function : NonLinearSolver::rhs_()
939 //
940 // Purpose : Calculates the RHS corresponding to the current solution
941 // vector. More specifically, it fills in the content of
942 // RHSVectorPtr_ based on the contents of nextSolVectorPtr_.
943 //
944 // Special Notes : The rhsVectorPtr_ is really the NEGATIVE of F(x).
945 //
946 // Scope : private
947 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
948 // Mathematics Research Department
949 // Creation Date : 06/19/01
950 //-----------------------------------------------------------------------------
951 
953 {
954  Stats::StatTop _residualStat("Residual");
955  Stats::TimeBlock _residualTimer(_residualStat);
956 
960 
961  return true;
962 }
963 
964 //-----------------------------------------------------------------------------
965 // Function : NonLinearSolver::jacobian_()
966 //
967 // Purpose : Calculates the Jacobian corresponding to the current
968 // solution vector. More specifically, it fills in the
969 // content of jacobianMatrixPtr_ based on the contents of
970 // nextSolVectorPtr_.
971 //
972 // Return Type : boolean
973 // Scope : private
974 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
975 // Mathematics Research Department
976 // Creation Date : 06/19/01
977 //-----------------------------------------------------------------------------
979 {
980  Stats::StatTop _jacobianStat("Jacobian");
981  Stats::TimeBlock _jacobianTimer(_jacobianStat);
982 
986  return true;
987 }
988 
989 //-----------------------------------------------------------------------------
990 // Function : NonLinearSolver::applyJacobian()
991 //
992 // Purpose : Applies the Jacobian corresponding to the current
993 // solution vector.
994 //
995 // Return Type : boolean
996 // Scope : private
997 // Creator : Todd Coffey, Ting Mei
998 // Creation Date : 07/29/08
999 //-----------------------------------------------------------------------------
1000 bool NonLinearSolver::applyJacobian(const Linear::Vector& input, Linear::Vector& result)
1001 {
1002  Stats::StatTop _jacobianStat("Apply Jacobian");
1003  Stats::TimeBlock _jacobianTime(_jacobianStat);
1004 
1005  nonlinearEquationLoader_->applyJacobian(input, result);
1008  return true;
1009 }
1010 
1011 //-----------------------------------------------------------------------------
1012 // Function : NonLinearSolver::newton_
1013 //
1014 // Purpose : Calculates the Newton direction corresponding to the
1015 // current RHS and Jacobian matrix.
1016 //
1017 // Return Type : boolean
1018 //
1019 // Scope : private
1020 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
1021 // Mathematics Research Department
1022 // Creation Date : 06/19/01
1023 //-----------------------------------------------------------------------------
1025 {
1026  int solutionStatus = lasSolverPtr_->solve( false );
1027 
1028  totalLinearSolveTime_ += lasSolverPtr_->solutionTime();
1029  ++numLinearSolves_;
1030 
1031  if( lasSolverPtr_->isIterative() )
1032  {
1033  Util::Param param( "Iterations", 0 );
1034  lasSolverPtr_->getInfo( param );
1035  totalNumLinearIters_ += param.getImmutableValue<int>();
1036 
1037  if( solutionStatus ) ++numFailedLinearSolves_;
1038  }
1039  else
1040  {
1041  Util::Param param( "Refactored", 0 );
1042  lasSolverPtr_->getInfo( param );
1043  if( param.getImmutableValue<int>() ) ++numJacobianFactorizations_;
1044  if( solutionStatus ) ++numFailedLinearSolves_;
1045  }
1046 
1047  if( solutionStatus ) return false;
1048 
1049  return true;
1050 }
1051 
1052 //-----------------------------------------------------------------------------
1053 // Function : NonLinearSolver::gradient_()
1054 // Purpose : Calculates the Gradient direction corresponding to the
1055 // current RHS and Jacobian matrix.
1056 // Computes gradient using jacobianMatrixPtr_ and
1057 // the rhsVectorPtr_. On output, gradVectorPtr_ contains
1058 // the gradient of 0.5 * ||F(x)||^2.
1059 //
1060 // Special Notes : The rhsVectorPtr_ is really the NEGATIVE of F(x).
1061 //
1062 // Return Type : boolean
1063 // Scope : private
1064 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
1065 // Mathematics Research Department
1066 // Creation Date : 06/19/01
1067 //-----------------------------------------------------------------------------
1069 {
1070  // Compute gradient = Jacobian' * RHS
1071  bool transpose = true;
1072  jacobianMatrixPtr_->matvec(transpose, *rhsVectorPtr_, *gradVectorPtr_);
1073 
1074  // We have to scale by -1 because the rhsVectorPtr_ is really the
1075  // NEGATIVE of F(X). This gives us gradient = Jacobian' * F(X).
1076  gradVectorPtr_->scale(-1.0);
1077 
1078  return true;
1079 }
1080 
1081 //-----------------------------------------------------------------------------
1082 // Function : NonLinearSolver::getCouplingMode ()
1083 // Purpose :
1084 // Special Notes :
1085 // Scope : public
1086 // Creator : Eric R. Keiter, SNL, Compuational Sciences and
1087 // Creation Date : 12/05/02
1088 //-----------------------------------------------------------------------------
1090 {
1091  return FULL_PROBLEM;
1092 }
1093 
1094 //-----------------------------------------------------------------------------
1095 // Function : NonLinearSolver::setDebugFlags
1096 // Purpose :
1097 // Special Notes :
1098 // Scope : private
1099 // Creator : Eric R. Keiter, SNL, Compuational Sciences
1100 // Creation Date : 09/23/01
1101 //-----------------------------------------------------------------------------
1102 void
1104  int output_step_number,
1105  double time)
1106 {
1107  outputStepNumber_ = output_step_number;
1108 
1109  debugTimeFlag_ =
1110  (time >= getDebugMinTime() && time <= getDebugMaxTime())
1111  && (output_step_number >= getDebugMinTimeStep() && output_step_number <= getDebugMaxTimeStep());
1112 
1113  if (tlnPtr_ != 0)
1115  else
1116  contStep_ = 0;
1117 }
1118 
1119 } // namespace Nonlinear
1120 } // namespace Xyce
void debugOutput3(Linear::Vector &dxVector, Linear::Vector &xVector)
virtual bool setTwoLevelLocaOptions(const Util::OptionBlock &OB)
virtual int getContinuationStep() const =0
virtual bool setTwoLevelOptions(const Util::OptionBlock &OB)
IO::InitialConditionsManager & initialConditionsManager_
Definition: N_ANP_AC.C:965
virtual bool setNodeSetOptions(const Util::OptionBlock &OB)
Pure virtual class to augment a linear system.
bool applyJacobian(const Linear::Vector &input, Linear::Vector &result)
virtual bool applyJacobian(const Linear::Vector &input, Linear::Vector &result)
virtual bool setICOptions(const Util::OptionBlock &OB)
virtual int getDebugLevel() const =0
NonLinearSolver(const IO::CmdParse &cp)
void setDebugFlags(int output_step_number, double time)
virtual bool getScreenOutputFlag() const =0
void debugOutput1(Linear::Matrix &jacobian, Linear::Vector &rhs)
bool registerTwoLevelSolver(TwoLevelNewton *ptr)
const Linear::PrecondFactory * lasPrecPtr_
bool registerInitialConditionsManager(IO::InitialConditionsManager *outPtr)
virtual TwoLevelNewtonMode getCouplingMode()
bool registerPrecondFactory(const Linear::PrecondFactory *ptr)
virtual double getDebugMinTime() const =0
virtual int getDebugMinTimeStep() const =0
Analysis::AnalysisManager * analysisManager_
AnalysisManager & analysisManager_
Definition: N_ANP_AC.C:960
Loader::NonlinearEquationLoader * nonlinearEquationLoader_
virtual bool registerTIADataStore(TimeIntg::DataStore *ptr)
virtual bool getMMFormat() const =0
virtual int getDebugMaxTimeStep() const =0
virtual bool registerLinearSystem(Linear::System *ptr)
virtual bool setLinsolOptions(const Util::OptionBlock &OB)
IO::InitialConditionsManager * initialConditionsManager_
RCP< MatrixFreeEpetraOperator > matrixFreeEpetraOperator(RCP< NonLinearSolver > nonlinearSolver, RCP< Linear::Vector > solVector, RCP< Linear::Vector > rhsVector, RCP< const Epetra_Map > solutionMap)
virtual bool registerParallelMgr(N_PDS_Manager *ptr)
virtual bool registerAnalysisManager(Analysis::AnalysisManager *tmp_anaIntPtr)
virtual int getParameterNumber() const =0
virtual double getDebugMaxTime() const =0
bool registerOutputMgr(IO::OutputMgr *outPtr)
bool registerRHSVector(Linear::Vector *ptr)
virtual bool setDCOPRestartOptions(const Util::OptionBlock &OB)
virtual int getNumIterations() const =0
virtual bool registerNonlinearEquationLoader(Loader::NonlinearEquationLoader *ptr)
virtual bool setTwoLevelTranOptions(const Util::OptionBlock &OB)
virtual bool setLocaOptions(const Util::OptionBlock &OB)