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.155.2.1 $
43 //
44 // Revision Date : $Date: 2015/04/02 18:20:17 $
45 //
46 // Current Owner : $Author: tvrusso $
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  petraOptionBlockPtr_(0),
118  loaderPtr_(0),
119  analysisManager_(0),
120  tlnPtr_(0),
121  nonlinearParameterManager_(0),
122 
123  outMgrPtr_(0),
124  initialConditionsManager_(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 petraOptionBlockPtr_;
185  }
186 
187  if (lasSolverPtr_)
188  {
189  delete lasSolverPtr_;
190  lasSolverPtr_ = 0;
191  }
192 
193 }
194 
195 //-----------------------------------------------------------------------------
196 // Function : NonLinearSolver::setPetraOptions
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::setPetraOptions(const Util::OptionBlock & OB)
217 {
218  petraOptionBlockPtr_ = new Util::OptionBlock(OB);
219  return (petraOptionBlockPtr_ != 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::registerLoader
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  loaderPtr_ = tmp_LoaderPtr;
350  return (loaderPtr_ != 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 
363 bool NonLinearSolver::registerLinearSystem(Linear::System* tmp_LasSysPtr)
364 {
365  lasSysPtr_ = tmp_LasSysPtr;
366  return (lasSysPtr_ != 0);
367 }
368 
369 //-----------------------------------------------------------------------------
370 // Function : NonLinearSolver::registerPrecondFactory
371 // Purpose :
372 // Special Notes :
373 // Return Type : boolean
374 // Scope : public
375 // Creator : Heidi Thornquist, SNL, Electrical & Microsystem Modeling
376 // Creation Date : 11/11/08
377 //-----------------------------------------------------------------------------
378 bool NonLinearSolver::registerPrecondFactory(const Linear::PrecondFactory *tmp_LasPrecPtr)
379 {
380  lasPrecPtr_ = tmp_LasPrecPtr;
381  return lasPrecPtr_;
382 }
383 
384 //-----------------------------------------------------------------------------
385 // Function : NonLinearSolver::registerParallelMgr
386 // Purpose :
387 // Special Notes :
388 // Return Type : boolean
389 // Scope : public
390 // Creator : Eric Keiter, Sandia
391 // Creation Date : 6/8/2013
392 //-----------------------------------------------------------------------------
393 bool NonLinearSolver::registerParallelMgr(N_PDS_Manager * pdsMgrPtr)
394 {
395  pdsMgrPtr_ = pdsMgrPtr;
396  return (pdsMgrPtr_ != 0);
397 }
398 
399 //-----------------------------------------------------------------------------
400 // Function : NonLinearSolver::registerAnalysisManager
401 // Purpose :
402 // Special Notes :
403 // Return Type : boolean
404 // Scope : public
405 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
406 // Creation Date : 5/03/00
407 //-----------------------------------------------------------------------------
409 {
410  analysisManager_ = tmp_anaIntPtr;
411  return (analysisManager_ != 0);
412 }
413 
414 //-----------------------------------------------------------------------------
415 // Function : NonLinearSolver::registerOutputMgr
416 // Purpose :
417 // Special Notes :
418 // Scope : public
419 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
420 // Creation Date : 9/23/03
421 //-----------------------------------------------------------------------------
422 bool NonLinearSolver::registerOutputMgr (IO::OutputMgr * outPtr)
423 {
424  outMgrPtr_ = outPtr;
425  return (outMgrPtr_ != 0);
426 }
427 
428 //-----------------------------------------------------------------------------
429 // Function : NonLinearSolver::registerInitialConditionsManager
430 // Purpose :
431 // Special Notes :
432 // Scope : public
433 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
434 // Creation Date : 9/23/03
435 //-----------------------------------------------------------------------------
436 bool NonLinearSolver::registerInitialConditionsManager(IO::InitialConditionsManager * outPtr)
437 {
438  initialConditionsManager_ = outPtr;
439  return (initialConditionsManager_ != 0);
440 }
441 
442 //-----------------------------------------------------------------------------
443 // Function : NonLinearSolver::registerTwoLevelSolver
444 // Purpose : This function is called in the event that the two-level
445 // Newton method has been invoked.
446 // Special Notes :
447 // Scope : public
448 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
449 // Creation Date : 10/24/02
450 //-----------------------------------------------------------------------------
452  (TwoLevelNewton * tmp_tlnPtr)
453 {
454  tlnPtr_ = tmp_tlnPtr;
455  return (tlnPtr_ != 0);
456 }
457 
458 //-----------------------------------------------------------------------------
459 // Function : NonLinearSolver::registerParamMgr
460 // Purpose : This function is called in the event that the two-level
461 // Newton method has been invoked.
462 // Special Notes :
463 // Scope : public
464 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
465 // Creation Date : 10/24/02
466 //-----------------------------------------------------------------------------
468  (ParamMgr * ptr)
469 {
470  nonlinearParameterManager_ = ptr;
471  return (nonlinearParameterManager_ != 0);
472 }
473 
474 //-----------------------------------------------------------------------------
475 // Function : N_NLS_NonLinearSolver::registerTIADataStore
476 // Purpose :
477 // Special Notes :
478 // Scope : public
479 // Creator : Eric Keiter
480 // Creation Date :
481 //-----------------------------------------------------------------------------
483 {
484  dsPtr_ = ds_tmp;
485  return true;
486 }
487 
488 //-----------------------------------------------------------------------------
489 // Function : NonLinearSolver::initializeAll
490 //
491 // Purpose : Called after all register and set functions.
492 // Once the various registrations have taken place,
493 // this function sets the remaining pointers.
494 //
495 // Special Notes: This function obtains the solution, temporary solution and
496 // rhs vectors from the LAS system class.
497 //
498 // Return Type : boolean
499 // Scope : public
500 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
501 // Creation Date : 6/12/00
502 //-----------------------------------------------------------------------------
504 {
505  bool bsuccess = true;
506 
507 
508  // Check the registerLinearSystem has been successfully called.
509  if (lasSysPtr_ == 0)
510  return false;
511 
512  // get the temporaries:
513  tmpSolVectorPtrPtr_ = lasSysPtr_->getTmpSolVectorPtr();
514  bsuccess = bsuccess && (tmpSolVectorPtrPtr_ != 0);
515 
516  // get rhs vector:
517  rhsVectorPtr_ = lasSysPtr_->getRHSVector();
518  bsuccess = bsuccess && (rhsVectorPtr_ != 0);
519 
520  // get current solution vectors:
521  currSolVectorPtrPtr_ = lasSysPtr_->getCurrSolVectorPtr();
522  bsuccess = bsuccess && (currSolVectorPtrPtr_ != 0);
523 
524  // get next solution vectors:
525  nextSolVectorPtrPtr_ = lasSysPtr_->getNextSolVectorPtr();
526  bsuccess = bsuccess && (nextSolVectorPtrPtr_ != 0);
527 
528  // get jacobian:
529  jacobianMatrixPtr_ = lasSysPtr_->getJacobianMatrix();
530  bsuccess = bsuccess && (jacobianMatrixPtr_ != 0);
531 
532  Linear::Builder & bld_ = lasSysPtr_->builder();
533 
534  // create gradient vector:
535  gradVectorPtr_ = bld_.createVector();
536  bsuccess = bsuccess && (gradVectorPtr_ != 0);
537 
538  // create Newton update vector:
539  NewtonVectorPtr_ = bld_.createVector();
540  bsuccess = bsuccess && (NewtonVectorPtr_ != 0);
541 
542  // create solution weighting vector:
543  solWtVectorPtr_ = bld_.createVector();
544  bsuccess = bsuccess && (solWtVectorPtr_ != 0);
545 
546  if( !petraOptionBlockPtr_ )
547  {
548  petraOptionBlockPtr_ = new Util::OptionBlock();
549  petraOptionBlockPtr_->getParams().push_back( Util::Param( "TYPE", "DEFAULT" ) );
550  }
551 
552  Teuchos::RCP<Linear::Vector> NewtonVectorRCPtr = Teuchos::rcp(NewtonVectorPtr_, false);
553  Teuchos::RCP<Linear::Vector> rhsVectorRCPtr = Teuchos::rcp(rhsVectorPtr_, false);
554 
555  if (!matrixFreeFlag_)
556  {
557  Teuchos::RCP<Linear::Matrix> jacobianMatrixRCPtr = Teuchos::rcp(jacobianMatrixPtr_, false);
558  // Normal full matrix linear solver options
559  lasProblemRCPtr_ = rcp( new Linear::Problem( jacobianMatrixRCPtr,
560  NewtonVectorRCPtr,
561  rhsVectorRCPtr) );
562  }
563  else
564  {
565  // Matrix free harmonic balance linear solver options
566  // Create MatrixFreeLinearProblem
567  Teuchos::RCP<NonLinearSolver> NonlinearSolverRCPtr = Teuchos::rcp(this, false);
568  Teuchos::RCP<MatrixFreeEpetraOperator>
569  matFreeOp = matrixFreeEpetraOperator(
570  NonlinearSolverRCPtr,
571  NewtonVectorRCPtr,
572  rhsVectorRCPtr,
573  bld_.getSolutionMap()
574  );
575 
576  Teuchos::RCP<Epetra_Operator> epetraOperator = Teuchos::rcp_dynamic_cast<Epetra_Operator>(matFreeOp, true);
577  // Create Linear::Problem
578  lasProblemRCPtr_ = rcp( new Linear::Problem(
579  epetraOperator,
580  NewtonVectorRCPtr,
581  rhsVectorRCPtr
582  )
583  );
584  }
585 
586  lasSolverPtr_ = Linear::SolverFactory::create( *petraOptionBlockPtr_, *lasProblemRCPtr_ , commandLine_);
587 
588  // If a preconditioner factory has been provided by the analysis package,
589  // use it to generate a preconditioner for the linear solver.
590  if (lasPrecPtr_) {
591  Teuchos::RCP<Linear::Preconditioner> precond = lasPrecPtr_->create( Teuchos::rcp( lasSysPtr_, false ) );
592  lasSolverPtr_->setPreconditioner( precond );
593  }
594 
595  if (DEBUG_NONLINEAR)
596  Xyce::dout() << "size of solution vector: " << lasSysPtr_->getGlobalSolutionSize() << std::endl
597  << "size of state vector: " << lasSysPtr_->getGlobalStateSize() << std::endl
598  << "End of NonLinearSolver::initializeAll\n";
599 
600  return bsuccess;
601 }
602 
603 //-----------------------------------------------------------------------------
604 // Function : NonLinearSolver::debugOutput1_
605 // Purpose : Write Jacobian to the file matrix.(n).txt, and the residual
606 // to the file rhs.(n).txt
607 //
608 // Special Notes : These are objects that will be availabled prior to the
609 // linear solve performed at each Newton step.
610 //
611 // Scope : public
612 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
613 // Creation Date : 10/30/00
614 //-----------------------------------------------------------------------------
616  Linear::Matrix & jacobian,
617  Linear::Vector & rhs)
618 {
619  setNonlinearDumpDebugLevel(getDebugLevel());
620  int newtStep = getNumIterations();
621  int screenOutput = getScreenOutputFlag ();
622  int contStep = getContinuationStep();
623  int paramNumber = getParameterNumber ();
624 
625  if (!debugTimeFlag_ || !isActive(Diag::NONLINEAR_DUMP_MASK)) return;
626 
627  char filename1[256]; for (int ich = 0; ich < 256; ++ich) filename1[ich] = 0;
628  char filename2[256]; for (int ich = 0; ich < 256; ++ich) filename2[ich] = 0;
629 
630  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
631  {
632  sprintf(filename1, "matrix_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
633  }
634  if (isActive(Diag::NONLINEAR_DUMP_STEP))
635  {
636  sprintf(filename1, "matrix_%03d_%03d.txt", outputStepNumber_, newtStep);
637  }
638  if (isActive(Diag::NONLINEAR_DUMP))
639  {
640  sprintf(filename1, "matrix_%03d.txt", newtStep);
641  }
642 
643  jacobian.writeToFile(filename1, false, getMMFormat () );
644 
645  if (screenOutput == 1)
646  {
647  Xyce::dout() << "\n\t***** Jacobian matrix:" << std::endl;
648  jacobian.printPetraObject(Xyce::dout());
649  }
650 
651  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
652  {
653  sprintf(filename2, "rhs_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
654  }
655  if (isActive(Diag::NONLINEAR_DUMP_STEP))
656  {
657  sprintf(filename2, "rhs_%03d_%03d.txt", outputStepNumber_, newtStep);
658  }
659  else
660  {
661  sprintf(filename2, "rhs_%03d.txt", newtStep);
662  }
663 
664  if (screenOutput == 1)
665  {
666  Xyce::dout() << "\n\t***** RHS vector:" << std::endl;
667 
668  rhs.printPetraObject(Xyce::dout());
669  }
670 
671  rhs.writeToFile(filename2);
672 
673  if (DEBUG_VOLTLIM)
675 
676  debugOutputDAE ();
677 
678 }
679 
680 //-----------------------------------------------------------------------------
681 // Function : NonLinearSolver::debugOutputJDX_VOLTLIM_
682 // Purpose : Write JDX vector to output files.
683 // Special Notes : This requires a matvec.
684 // Scope : public
685 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
686 // Creation Date : 01/05/05
687 //-----------------------------------------------------------------------------
689 {
690  setNonlinearDumpDebugLevel(getDebugLevel());
691 
692  int newtStep = getNumIterations();
693  int contStep = getContinuationStep();
694  int paramNumber = getParameterNumber ();
695 
696  char filename1[256]; for (int ich = 0; ich < 256; ++ich) filename1[ich] = 0;
697  char filename2[256]; for (int ich = 0; ich < 256; ++ich) filename2[ich] = 0;
698  char filename3[256]; for (int ich = 0; ich < 256; ++ich) filename3[ich] = 0;
699 
700  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
701  {
702  sprintf(filename1, "jdxVL_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
703  sprintf(filename2, "fdxVL_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
704  sprintf(filename3, "qdxVL_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
705  }
706  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
707  {
708  sprintf(filename1, "jdxVL_%03d_%03d.txt", outputStepNumber_, newtStep);
709  sprintf(filename2, "fdxVL_%03d_%03d.txt", outputStepNumber_, newtStep);
710  sprintf(filename3, "qdxVL_%03d_%03d.txt", outputStepNumber_, newtStep);
711  }
712  else
713  {
714  sprintf(filename1, "jdxVL_%03d.txt", newtStep);
715  sprintf(filename2, "fdxVL_%03d.txt", newtStep);
716  sprintf(filename3, "qdxVL_%03d.txt", newtStep);
717  }
718 
719  bool Transpose = false; // if set to true, the matvec does the transpose.
720 
721  jdxVLVectorPtr_->putScalar(0.0);
722  fdxVLVectorPtr_->putScalar(0.0);
723  qdxVLVectorPtr_->putScalar(0.0);
724 
725  jacTestMatrixPtr_->matvec( Transpose , *dxVoltlimVectorPtr_, *jdxVLVectorPtr_);
726  dFdxTestMatrixPtr_->matvec( Transpose , *dxVoltlimVectorPtr_, *fdxVLVectorPtr_);
727  dQdxTestMatrixPtr_->matvec( Transpose , *dxVoltlimVectorPtr_, *qdxVLVectorPtr_);
728 
729  jdxVLVectorPtr_->writeToFile(filename1);
730  fdxVLVectorPtr_->writeToFile(filename2);
731  qdxVLVectorPtr_->writeToFile(filename3);
732 
733  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
734  {
735  sprintf(filename1, "jtest_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
736  sprintf(filename2, "ftest_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
737  sprintf(filename3, "qtest_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, newtStep);
738  }
739  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
740  {
741  sprintf(filename1, "jtest_%03d_%03d.txt", outputStepNumber_, newtStep);
742  sprintf(filename2, "ftest_%03d_%03d.txt", outputStepNumber_, newtStep);
743  sprintf(filename3, "qtest_%03d_%03d.txt", outputStepNumber_, newtStep);
744  }
745  else
746  {
747  sprintf(filename1, "jtest_%03d.txt", newtStep);
748  sprintf(filename2, "ftest_%03d.txt", newtStep);
749  sprintf(filename3, "qtest_%03d.txt", newtStep);
750  }
751 
752  jacTestMatrixPtr_->writeToFile(filename1);
753  dFdxTestMatrixPtr_->writeToFile(filename2);
754  dQdxTestMatrixPtr_->writeToFile(filename3);
755 
756 }
757 
758 //-----------------------------------------------------------------------------
759 // Function : NonLinearSolver::debugOutputDAE
760 // Purpose : Write DAE vectors and matrices to output files.
761 // Special Notes :
762 // Scope : public
763 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
764 // Creation Date : 01/27/03
765 //-----------------------------------------------------------------------------
767 {
768  setNonlinearDumpDebugLevel(getDebugLevel());
769 
770  int newtStep = getNumIterations();
771  int contStep = getContinuationStep();
772  int paramNumber = getParameterNumber ();
773 
774  char filename1[256]; for (int ich = 0; ich < 256; ++ich) filename1[ich] = 0;
775  char filename2[256]; for (int ich = 0; ich < 256; ++ich) filename2[ich] = 0;
776 
777  char filename4[256]; for (int ich = 0; ich < 256; ++ich) filename4[ich] = 0;
778  char filename6[256]; for (int ich = 0; ich < 256; ++ich) filename6[ich] = 0;
779  char filename7[256]; for (int ich = 0; ich < 256; ++ich) filename7[ich] = 0;
780  char filename8[256]; for (int ich = 0; ich < 256; ++ich) filename8[ich] = 0;
781  char filename9[256]; for (int ich = 0; ich < 256; ++ich) filename9[ich] = 0;
782 
783  Linear::Matrix *dQdx = lasSysPtr_->getDAEdQdxMatrix ();
784  Linear::Matrix *dFdx = lasSysPtr_->getDAEdFdxMatrix ();
785 
786  Linear::Vector *daeQ = lasSysPtr_->getDAEQVector();
787  Linear::Vector *daeF = lasSysPtr_->getDAEFVector();
788 
789  Linear::Vector *daeFlim = lasSysPtr_->getdFdxdVpVector ();
790  Linear::Vector *daeQlim = lasSysPtr_->getdQdxdVpVector ();
791 
792  //Xyce::dout() << "In debugOutputDAE" << std::endl;
793 
794  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
795  {
796  sprintf(filename1, "dQdx_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
797  sprintf(filename2, "dFdx_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
798 
799  sprintf(filename4, "daeQ_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
800  sprintf(filename6, "daeF_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
801 
802  sprintf(filename8, "daeQlim_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
803  sprintf(filename9, "daeFlim_%03d_%03d_%03d_%03d.txt" , outputStepNumber_, paramNumber, contStep, newtStep);
804  }
805  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
806  {
807  sprintf(filename1, "dQdx_%03d_%03d.txt" , outputStepNumber_, newtStep);
808  sprintf(filename2, "dFdx_%03d_%03d.txt" , outputStepNumber_, newtStep);
809 
810  sprintf(filename4, "daeQ_%03d_%03d.txt" , outputStepNumber_, newtStep);
811  sprintf(filename6, "daeF_%03d_%03d.txt" , outputStepNumber_, newtStep);
812 
813  sprintf(filename8, "daeQlim_%03d_%03d.txt" , outputStepNumber_, newtStep);
814  sprintf(filename9, "daeFlim_%03d_%03d.txt" , outputStepNumber_, newtStep);
815  }
816  else
817  {
818  sprintf(filename1, "dQdx_%03d.txt" , newtStep);
819  sprintf(filename2, "dFdx_%03d.txt" , newtStep);
820 
821  sprintf(filename4, "daeQ_%03d.txt" , newtStep);
822  sprintf(filename6, "daeF_%03d.txt" , newtStep);
823 
824  sprintf(filename8, "daeQlim_%03d.txt" , newtStep);
825  sprintf(filename9, "daeFlim_%03d.txt" , newtStep);
826  }
827 
828  // write the matrices:
829  dQdx->writeToFile (filename1, false, getMMFormat () );
830  dFdx->writeToFile (filename2, false, getMMFormat () );
831 
832  // write the vectors:
833  daeQ->writeToFile(filename4);
834  daeF->writeToFile(filename6);
835  daeQlim->writeToFile(filename8);
836  daeFlim->writeToFile(filename9);
837 }
838 
839 //-----------------------------------------------------------------------------
840 // Function : NonLinearSolver::debugOutput3_
841 // Purpose : Write out the update vector and the new solution.
842 //
843 // Special Notes : These are objects that will be availabled *after* the
844 // linear solve performed at each Newton step. That
845 // differentiates this function from debugOutput1_.
846 //
847 // Scope : public
848 // Creator : Eric Keiter, SNL, Parallel Computational Sciences
849 // Creation Date : 10/30/00
850 //-----------------------------------------------------------------------------
852  Linear::Vector & dxVector,
853  Linear::Vector & xVector)
854 {
855  setNonlinearDumpDebugLevel(getDebugLevel());
856 
857  int nlStep = getNumIterations();
858  int contStep = getContinuationStep();
859  int paramNumber = getParameterNumber ();
860 
861  if (!debugTimeFlag_ || !isActive(Diag::NONLINEAR_DUMP_MASK)) return;
862 
863  char filename[256]; for (int ich = 0; ich < 256; ++ich) filename[ich] = 0;
864 
865  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
866  {
867  sprintf(filename, "update_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, nlStep);
868  }
869  else if (isActive(Diag::NONLINEAR_DUMP_STEP))
870  {
871  sprintf(filename, "update_%03d_%03d.txt", outputStepNumber_, nlStep);
872  }
873  else
874  {
875  sprintf(filename, "update_%03d.txt", nlStep);
876  }
877  dxVector.writeToFile(filename);
878 
879  if (isActive(Diag::NONLINEAR_DUMP_PARAM_NUMBER))
880  {
881  sprintf(filename, "solution_%03d_%03d_%03d_%03d.txt", outputStepNumber_, paramNumber, contStep, nlStep);
882  }
883  if (isActive(Diag::NONLINEAR_DUMP_STEP))
884  {
885  sprintf(filename, "solution_%03d_%03d.txt", outputStepNumber_, nlStep);
886  }
887  if (isActive(Diag::NONLINEAR_DUMP))
888  {
889  sprintf(filename, "solution_%03d.txt", nlStep);
890  }
891  xVector.writeToFile(filename);
892 }
893 
894 //-----------------------------------------------------------------------------
895 // Function : NonLinearSolver::resetCountersAndTimers_
896 //
897 // Purpose : Resets all the counters and timers in this object.
898 //
899 // Scope : protected
900 // Creator : Tamara G. Kolda, SNL, CSMR (8950)
901 // Eric Keiter, SNL, Parallel Computational Sciences. (9233)
902 // Creation Date : 01/24/02
903 //-----------------------------------------------------------------------------
905 {
906  numJacobianLoads_ = 0;
908  numLinearSolves_ = 0;
910  numResidualLoads_ = 0;
912  totalLinearSolveTime_ = 0.0;
915 }
916 
917 //-----------------------------------------------------------------------------
918 // Function : NonLinearSolver::setX0_()
919 //
920 // Purpose : This should be called at the beginning of each nonlinear
921 // iteration. Copies information from nextSolVector (and
922 // related vectors that are important but hidden from the
923 // nonlinear solver) into tmpSolVector.
924 //
925 // Return Type : boolean
926 // Scope : protected
927 // Creator : Tamara G. Kolda, SNL, CSMR (8950)
928 // Eric Keiter, SNL, Parallel Computational Sciences. (9233)
929 // Creation Date : 01/24/02
930 //-----------------------------------------------------------------------------
932 {
933 
935 
936  return true;
937 }
938 
939 //-----------------------------------------------------------------------------
940 // Function : NonLinearSolver::rhs_()
941 //
942 // Purpose : Calculates the RHS corresponding to the current solution
943 // vector. More specifically, it fills in the content of
944 // RHSVectorPtr_ based on the contents of nextSolVectorPtr_.
945 //
946 // Special Notes : The rhsVectorPtr_ is really the NEGATIVE of F(x).
947 //
948 // Scope : private
949 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
950 // Mathematics Research Department
951 // Creation Date : 06/19/01
952 //-----------------------------------------------------------------------------
953 
955 {
956  Stats::StatTop _residualStat("Residual");
957  Stats::TimeBlock _residualTimer(_residualStat);
958 
959  loaderPtr_->loadRHS();
962 
963  return true;
964 }
965 
966 //-----------------------------------------------------------------------------
967 // Function : NonLinearSolver::jacobian_()
968 //
969 // Purpose : Calculates the Jacobian corresponding to the current
970 // solution vector. More specifically, it fills in the
971 // content of jacobianMatrixPtr_ based on the contents of
972 // nextSolVectorPtr_.
973 //
974 // Return Type : boolean
975 // Scope : private
976 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
977 // Mathematics Research Department
978 // Creation Date : 06/19/01
979 //-----------------------------------------------------------------------------
981 {
982  Stats::StatTop _jacobianStat("Jacobian");
983  Stats::TimeBlock _jacobianTimer(_jacobianStat);
984 
988  return true;
989 }
990 
991 //-----------------------------------------------------------------------------
992 // Function : NonLinearSolver::applyJacobian()
993 //
994 // Purpose : Applies the Jacobian corresponding to the current
995 // solution vector.
996 //
997 // Return Type : boolean
998 // Scope : private
999 // Creator : Todd Coffey, Ting Mei
1000 // Creation Date : 07/29/08
1001 //-----------------------------------------------------------------------------
1002 bool NonLinearSolver::applyJacobian(const Linear::Vector& input, Linear::Vector& result)
1003 {
1004  Stats::StatTop _jacobianStat("Jacobian");
1005  Stats::TimeBlock _jacobianTime(_jacobianStat);
1006 
1007  loaderPtr_->applyJacobian(input, result);
1010  return true;
1011 }
1012 
1013 //-----------------------------------------------------------------------------
1014 // Function : NonLinearSolver::newton_
1015 //
1016 // Purpose : Calculates the Newton direction corresponding to the
1017 // current RHS and Jacobian matrix.
1018 //
1019 // Return Type : boolean
1020 //
1021 // Scope : private
1022 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
1023 // Mathematics Research Department
1024 // Creation Date : 06/19/01
1025 //-----------------------------------------------------------------------------
1027 {
1028  int solutionStatus = lasSolverPtr_->solve( false );
1029 
1030  totalLinearSolveTime_ += lasSolverPtr_->solutionTime();
1031  ++numLinearSolves_;
1032 
1033  if( lasSolverPtr_->isIterative() )
1034  {
1035  Util::Param param( "Iterations", 0 );
1036  lasSolverPtr_->getInfo( param );
1037  totalNumLinearIters_ += param.getImmutableValue<int>();
1038 
1039  if( solutionStatus ) ++numFailedLinearSolves_;
1040  }
1041  else
1042  {
1043  Util::Param param( "Refactored", 0 );
1044  lasSolverPtr_->getInfo( param );
1045  if( param.getImmutableValue<int>() ) ++numJacobianFactorizations_;
1046  if( solutionStatus ) ++numFailedLinearSolves_;
1047  }
1048 
1049  if( solutionStatus ) return false;
1050 
1051  return true;
1052 }
1053 
1054 //-----------------------------------------------------------------------------
1055 // Function : NonLinearSolver::gradient_()
1056 // Purpose : Calculates the Gradient direction corresponding to the
1057 // current RHS and Jacobian matrix.
1058 // Computes gradient using jacobianMatrixPtr_ and
1059 // the rhsVectorPtr_. On output, gradVectorPtr_ contains
1060 // the gradient of 0.5 * ||F(x)||^2.
1061 //
1062 // Special Notes : The rhsVectorPtr_ is really the NEGATIVE of F(x).
1063 //
1064 // Return Type : boolean
1065 // Scope : private
1066 // Creator : Tamara G. Kolda, SNL, Compuational Sciences and
1067 // Mathematics Research Department
1068 // Creation Date : 06/19/01
1069 //-----------------------------------------------------------------------------
1071 {
1072  // Compute gradient = Jacobian' * RHS
1073  bool transpose = true;
1074  jacobianMatrixPtr_->matvec(transpose, *rhsVectorPtr_, *gradVectorPtr_);
1075 
1076  // We have to scale by -1 because the rhsVectorPtr_ is really the
1077  // NEGATIVE of F(X). This gives us gradient = Jacobian' * F(X).
1078  gradVectorPtr_->scale(-1.0);
1079 
1080  return true;
1081 }
1082 
1083 //-----------------------------------------------------------------------------
1084 // Function : NonLinearSolver::getCouplingMode ()
1085 // Purpose :
1086 // Special Notes :
1087 // Scope : public
1088 // Creator : Eric R. Keiter, SNL, Compuational Sciences and
1089 // Creation Date : 12/05/02
1090 //-----------------------------------------------------------------------------
1092 {
1093  return FULL_PROBLEM;
1094 }
1095 
1096 //-----------------------------------------------------------------------------
1097 // Function : NonLinearSolver::setMatrixFreeFlag
1098 // Purpose :
1099 // Special Notes :
1100 // Scope : public
1101 // Creator : Todd Coffey, Ting Mei
1102 // Creation Date : 7/29/08
1103 //-----------------------------------------------------------------------------
1104 void NonLinearSolver::setMatrixFreeFlag (bool matrixFreeFlag)
1105 {
1106  matrixFreeFlag_ = matrixFreeFlag;
1107 }
1108 
1109 //-----------------------------------------------------------------------------
1110 // Function : NonLinearSolver::getMatrixFreeFlag
1111 // Purpose :
1112 // Special Notes :
1113 // Scope : public
1114 // Creator : Todd Coffey, Ting Mei
1115 // Creation Date : 7/29/08
1116 //-----------------------------------------------------------------------------
1118 {
1119  return matrixFreeFlag_;
1120 }
1121 
1122 //-----------------------------------------------------------------------------
1123 // Function : NonLinearSolver::setDebugFlags
1124 // Purpose :
1125 // Special Notes :
1126 // Scope : private
1127 // Creator : Eric R. Keiter, SNL, Compuational Sciences
1128 // Creation Date : 09/23/01
1129 //-----------------------------------------------------------------------------
1130 void
1132  int output_step_number,
1133  double time)
1134 {
1135  outputStepNumber_ = output_step_number;
1136 
1137  debugTimeFlag_ =
1138  (time >= getDebugMinTime() && time <= getDebugMaxTime())
1139  && (output_step_number >= getDebugMinTimeStep() && output_step_number <= getDebugMaxTimeStep());
1140 
1141  if (tlnPtr_ != 0)
1143  else
1144  contStep_ = 0;
1145 }
1146 
1147 } // namespace Nonlinear
1148 } // namespace Xyce
virtual bool getScreenOutputFlag() const =0
void debugOutput3(Linear::Vector &dxVector, Linear::Vector &xVector)
bool registerLoader(Loader::NonlinearEquationLoader *ptr)
virtual void setMatrixFreeFlag(bool matrixFreeFlag)
virtual bool setTwoLevelLocaOptions(const Util::OptionBlock &OB)
virtual bool setTwoLevelOptions(const Util::OptionBlock &OB)
virtual bool setNodeSetOptions(const Util::OptionBlock &OB)
bool registerTIADataStore(TimeIntg::DataStore *tiaDSPtr)
bool registerParallelMgr(N_PDS_Manager *pdsMgrPtr)
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 int getNumIterations() const =0
virtual double getDebugMaxTime() const =0
virtual bool setICOptions(const Util::OptionBlock &OB)
virtual bool setPetraOptions(const Util::OptionBlock &OB)
NonLinearSolver(const IO::CmdParse &cp)
virtual int getParameterNumber() const =0
virtual int getDebugLevel() const =0
void setDebugFlags(int output_step_number, double time)
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)
Analysis::AnalysisManager * analysisManager_
virtual bool getMMFormat() const =0
virtual int getContinuationStep() const =0
AnalysisManager & analysisManager_
Definition: N_ANP_AC.C:965
bool registerLinearSystem(Linear::System *ptr)
virtual int getDebugMaxTimeStep() const =0
IO::InitialConditionsManager * initialConditionsManager_
RCP< MatrixFreeEpetraOperator > matrixFreeEpetraOperator(RCP< NonLinearSolver > nonlinearSolver, RCP< Linear::Vector > solVector, RCP< Linear::Vector > rhsVector, RCP< const Epetra_Map > solutionMap)
virtual double getDebugMinTime() const =0
bool registerAnalysisManager(Analysis::AnalysisManager *tmp_anaIntPtr)
virtual int getDebugMinTimeStep() const =0
Loader::NonlinearEquationLoader * loaderPtr_
bool registerOutputMgr(IO::OutputMgr *outPtr)
bool registerRHSVector(Linear::Vector *ptr)
virtual bool setDCOPRestartOptions(const Util::OptionBlock &OB)
virtual bool setTwoLevelTranOptions(const Util::OptionBlock &OB)
virtual bool setLocaOptions(const Util::OptionBlock &OB)