libMesh::PetscNonlinearSolver< T > Class Template Reference

#include <petsc_nonlinear_solver.h>

Inheritance diagram for libMesh::PetscNonlinearSolver< T >:

List of all members.

Public Types

typedef NonlinearImplicitSystem sys_type

Public Member Functions

 PetscNonlinearSolver (sys_type &system)
 ~PetscNonlinearSolver ()
virtual void clear ()
virtual void init ()
SNES snes ()
virtual std::pair< unsigned
int, Real
solve (SparseMatrix< T > &, NumericVector< T > &, NumericVector< T > &, const double, const unsigned int)
virtual void print_converged_reason ()
SNESConvergedReason get_converged_reason ()
virtual int get_total_linear_iterations ()
virtual unsigned get_current_nonlinear_iteration_number () const
void set_current_nonlinear_iteration_number (unsigned num)
bool initialized () const
const sys_typesystem () const
sys_typesystem ()
void attach_preconditioner (Preconditioner< T > *preconditioner)

Static Public Member Functions

static AutoPtr
< NonlinearSolver< T > > 
build (sys_type &s, const SolverPackage solver_package=libMesh::default_solver_package())
static std::string get_info ()
static void print_info (std::ostream &out=libMesh::out)
static unsigned int n_objects ()
static void enable_print_counter_info ()
static void disable_print_counter_info ()

Public Attributes

SNES _snes
SNESConvergedReason _reason
int _n_linear_iterations
unsigned _current_nonlinear_iteration_number
void(* residual )(const NumericVector< Number > &X, NumericVector< Number > &R, sys_type &S)
NonlinearImplicitSystem::ComputeResidualresidual_object
void(* jacobian )(const NumericVector< Number > &X, SparseMatrix< Number > &J, sys_type &S)
NonlinearImplicitSystem::ComputeJacobianjacobian_object
void(* matvec )(const NumericVector< Number > &X, NumericVector< Number > *R, SparseMatrix< Number > *J, sys_type &S)
NonlinearImplicitSystem::ComputeResidualandJacobianresidual_and_jacobian_object
void(* bounds )(NumericVector< Number > &XL, NumericVector< Number > &XU, sys_type &S)
NonlinearImplicitSystem::ComputeBoundsbounds_object
void(* nullspace )(std::vector< NumericVector< Number > * > &sp, sys_type &S)
NonlinearImplicitSystem::ComputeVectorSubspacenullspace_object
void(* nearnullspace )(std::vector< NumericVector< Number > * > &sp, sys_type &S)
NonlinearImplicitSystem::ComputeVectorSubspacenearnullspace_object
void(* user_presolve )(sys_type &S)
unsigned int max_nonlinear_iterations
unsigned int max_function_evaluations
Real absolute_residual_tolerance
Real relative_residual_tolerance
Real absolute_step_tolerance
Real relative_step_tolerance
unsigned int max_linear_iterations
Real initial_linear_tolerance
Real minimum_linear_tolerance
bool converged

Protected Types

typedef std::map< std::string,
std::pair< unsigned int,
unsigned int > > 
Counts

Protected Member Functions

void increment_constructor_count (const std::string &name)
void increment_destructor_count (const std::string &name)

Protected Attributes

sys_type_system
bool _is_initialized
Preconditioner< T > * _preconditioner

Static Protected Attributes

static Counts _counts
static Threads::atomic
< unsigned int > 
_n_objects
static Threads::spin_mutex _mutex
static bool _enable_print_counter = true

Private Member Functions

void build_mat_null_space (NonlinearImplicitSystem::ComputeVectorSubspace *computeSubspaceObject, void(*)(std::vector< NumericVector< Number > * > &, sys_type &), MatNullSpace *)

Detailed Description

template<typename T>
class libMesh::PetscNonlinearSolver< T >

This class provides an interface to PETSc iterative solvers that is compatible with the libMesh NonlinearSolver<>

Author:
Benjamin Kirk, 2002-2007

Definition at line 59 of file petsc_nonlinear_solver.h.


Member Typedef Documentation

typedef std::map<std::string, std::pair<unsigned int, unsigned int> > libMesh::ReferenceCounter::Counts [protected, inherited]

Data structure to log the information. The log is identified by the class name.

Definition at line 113 of file reference_counter.h.

template<typename T >
typedef NonlinearImplicitSystem libMesh::PetscNonlinearSolver< T >::sys_type

The type of system

Reimplemented from libMesh::NonlinearSolver< T >.

Reimplemented in libMesh::PetscDMNonlinearSolver< T >.

Definition at line 65 of file petsc_nonlinear_solver.h.


Constructor & Destructor Documentation

template<typename T >
libMesh::PetscNonlinearSolver< T >::PetscNonlinearSolver ( sys_type system  )  [inline, explicit]

Constructor. Initializes Petsc data structures

Definition at line 242 of file petsc_nonlinear_solver.C.

00242                                                                   :
00243     NonlinearSolver<T>(system_in),
00244     _reason(SNES_CONVERGED_ITERATING/*==0*/), // Arbitrary initial value...
00245     _n_linear_iterations(0),
00246     _current_nonlinear_iteration_number(0)
00247 {
00248 }

template<typename T >
libMesh::PetscNonlinearSolver< T >::~PetscNonlinearSolver (  )  [inline]

Destructor.

Definition at line 253 of file petsc_nonlinear_solver.C.

References libMesh::PetscNonlinearSolver< T >::clear().

00254 {
00255   this->clear ();
00256 }


Member Function Documentation

template<typename T>
void libMesh::NonlinearSolver< T >::attach_preconditioner ( Preconditioner< T > *  preconditioner  )  [inline, inherited]

Attaches a Preconditioner object to be used during the linear solves.

Definition at line 91 of file nonlinear_solver.C.

References libMesh::NonlinearSolver< T >::_is_initialized, libMesh::NonlinearSolver< T >::_preconditioner, and libMesh::err.

00092 {
00093   if(this->_is_initialized)
00094   {
00095     libMesh::err << "Preconditioner must be attached before the solver is initialized!"<<std::endl;
00096     libmesh_error();
00097   }
00098 
00099   _preconditioner = preconditioner;
00100 }

template<typename T >
AutoPtr< NonlinearSolver< T > > libMesh::NonlinearSolver< T >::build ( sys_type s,
const SolverPackage  solver_package = libMesh::default_solver_package() 
) [inline, static, inherited]

Builds a NonlinearSolver using the nonlinear solver package specified by solver_package

Definition at line 38 of file nonlinear_solver.C.

References libMesh::err, libMesh::on_command_line(), libMeshEnums::PETSC_SOLVERS, libMesh::AutoPtr< Tp >::reset(), and libMesh::TRILINOS_SOLVERS.

00039 {
00040   AutoPtr<NonlinearSolver<T> > ap;
00041 
00042   // Build the appropriate solver
00043   switch (solver_package)
00044     {
00045 
00046 #ifdef LIBMESH_HAVE_PETSC
00047     case PETSC_SOLVERS:
00048 #if PETSC_VERSION_LESS_THAN(3,3,0)
00049       ap.reset(new PetscNonlinearSolver<T>(s));
00050       break;
00051 #else
00052       if (libMesh::on_command_line ("--use-petsc-dm")){
00053         ap.reset(new PetscDMNonlinearSolver<T>(s));
00054       }
00055       else {
00056         ap.reset(new PetscNonlinearSolver<T>(s));
00057       }
00058       break;
00059 #endif
00060 #endif // LIBMESH_HAVE_PETSC
00061 
00062 #ifdef LIBMESH_HAVE_NOX
00063     case TRILINOS_SOLVERS:
00064       ap.reset(new NoxNonlinearSolver<T>(s));
00065       break;
00066 #endif
00067 
00068     default:
00069       libMesh::err << "ERROR:  Unrecognized solver package: "
00070                     << solver_package
00071                     << std::endl;
00072       libmesh_error();
00073     }
00074 
00075   return ap;
00076 }

template<typename T >
void libMesh::PetscNonlinearSolver< T >::build_mat_null_space ( NonlinearImplicitSystem::ComputeVectorSubspace computeSubspaceObject,
void(*)(std::vector< NumericVector< Number > * > &, sys_type &)  computeSubspace,
MatNullSpace *  msp 
) [inline, private]

Definition at line 347 of file petsc_nonlinear_solver.C.

References libMesh::COMM_WORLD, libMesh::NonlinearSolver< T >::system(), and libMesh::PetscVector< T >::vec().

Referenced by libMesh::PetscNonlinearSolver< T >::solve().

00350 {
00351   PetscErrorCode ierr;
00352   std::vector<NumericVector<Number>* > sp;
00353   if (computeSubspaceObject)
00354     (*computeSubspaceObject)(sp, this->system());
00355   else
00356     (*computeSubspace)(sp, this->system());
00357 
00358   *msp = PETSC_NULL;
00359   if (sp.size())
00360     {
00361       Vec *modes;
00362       PetscScalar *dots;
00363       PetscInt nmodes = sp.size();
00364 
00365       ierr = PetscMalloc2(nmodes,Vec,&modes,nmodes,PetscScalar,&dots);
00366       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00367 
00368       for (PetscInt i=0; i<nmodes; ++i)
00369         {
00370           PetscVector<T>* pv = libmesh_cast_ptr<PetscVector<T>*>(sp[i]);
00371           Vec v = pv->vec();
00372 
00373           ierr = VecDuplicate(v, modes+i);
00374           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00375 
00376           ierr = VecCopy(v,modes[i]);
00377           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00378         }
00379 
00380       // Normalize.
00381       ierr = VecNormalize(modes[0],PETSC_NULL);
00382       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00383 
00384       for (PetscInt i=1; i<nmodes; i++)
00385         {
00386           // Orthonormalize vec[i] against vec[0:i-1]
00387           ierr = VecMDot(modes[i],i,modes,dots);
00388           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00389 
00390           for (PetscInt j=0; j<i; j++)
00391             dots[j] *= -1.;
00392 
00393           ierr = VecMAXPY(modes[i],i,dots,modes);
00394           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00395 
00396           ierr = VecNormalize(modes[i],PETSC_NULL);
00397           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00398         }
00399 
00400       ierr = MatNullSpaceCreate(libMesh::COMM_WORLD, PETSC_FALSE, nmodes, modes, msp);
00401       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00402 
00403       for (PetscInt i=0; i<nmodes; ++i)
00404         {
00405           ierr = VecDestroy(modes+i);
00406           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00407         }
00408 
00409       ierr = PetscFree2(modes,dots);
00410       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00411     }
00412 }

template<typename T >
void libMesh::PetscNonlinearSolver< T >::clear (  )  [inline, virtual]

Release all memory and clear data structures.

Reimplemented from libMesh::NonlinearSolver< T >.

Definition at line 261 of file petsc_nonlinear_solver.C.

References libMesh::PetscNonlinearSolver< T >::_current_nonlinear_iteration_number, libMesh::NonlinearSolver< T >::_is_initialized, libMesh::PetscNonlinearSolver< T >::_snes, libMesh::COMM_WORLD, and libMesh::NonlinearSolver< T >::initialized().

Referenced by libMesh::PetscNonlinearSolver< T >::solve(), libMesh::PetscDMNonlinearSolver< T >::solve(), libMesh::PetscDMNonlinearSolver< T >::~PetscDMNonlinearSolver(), and libMesh::PetscNonlinearSolver< T >::~PetscNonlinearSolver().

00262 {
00263   if (this->initialized())
00264     {
00265       this->_is_initialized = false;
00266 
00267       int ierr=0;
00268 
00269       ierr = LibMeshSNESDestroy(&_snes);
00270              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00271 
00272       // Reset the nonlinear iteration counter.  This information is only relevant
00273       // *during* the solve().  After the solve is completed it should return to
00274       // the default value of 0.
00275       _current_nonlinear_iteration_number = 0;
00276     }
00277 }

void libMesh::ReferenceCounter::disable_print_counter_info (  )  [static, inherited]

Definition at line 106 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

00107 {
00108   _enable_print_counter = false;
00109   return;
00110 }

void libMesh::ReferenceCounter::enable_print_counter_info (  )  [static, inherited]

Methods to enable/disable the reference counter output from print_info()

Definition at line 100 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

00101 {
00102   _enable_print_counter = true;
00103   return;
00104 }

template<typename T >
SNESConvergedReason libMesh::PetscNonlinearSolver< T >::get_converged_reason (  )  [inline]

Returns the currently-available (or most recently obtained, if the SNES object has been destroyed) convergence reason. Refer to PETSc docs for the meaning of different SNESConvergedReasons.

Definition at line 567 of file petsc_nonlinear_solver.C.

References libMesh::PetscNonlinearSolver< T >::_reason, libMesh::PetscNonlinearSolver< T >::_snes, libMesh::COMM_WORLD, and libMesh::NonlinearSolver< T >::initialized().

Referenced by libMesh::PetscNonlinearSolver< T >::print_converged_reason().

00568 {
00569   int ierr=0;
00570 
00571   if (this->initialized())
00572     {
00573       ierr = SNESGetConvergedReason(_snes, &_reason);
00574       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00575     }
00576 
00577   return _reason;
00578 }

template<typename T >
virtual unsigned libMesh::PetscNonlinearSolver< T >::get_current_nonlinear_iteration_number (  )  const [inline, virtual]

If called *during* the solve(), for example by the user-specified residual or Jacobian function, returns the current nonlinear iteration number.

Implements libMesh::NonlinearSolver< T >.

Definition at line 126 of file petsc_nonlinear_solver.h.

References libMesh::PetscNonlinearSolver< T >::_current_nonlinear_iteration_number.

std::string libMesh::ReferenceCounter::get_info (  )  [static, inherited]

Gets a string containing the reference information.

Definition at line 47 of file reference_counter.C.

References libMesh::ReferenceCounter::_counts, and libMesh::Quality::name().

Referenced by libMesh::ReferenceCounter::print_info().

00048 {
00049 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
00050 
00051   std::ostringstream oss;
00052 
00053   oss << '\n'
00054       << " ---------------------------------------------------------------------------- \n"
00055       << "| Reference count information                                                |\n"
00056       << " ---------------------------------------------------------------------------- \n";
00057 
00058   for (Counts::iterator it = _counts.begin();
00059        it != _counts.end(); ++it)
00060     {
00061       const std::string name(it->first);
00062       const unsigned int creations    = it->second.first;
00063       const unsigned int destructions = it->second.second;
00064 
00065       oss << "| " << name << " reference count information:\n"
00066           << "|  Creations:    " << creations    << '\n'
00067           << "|  Destructions: " << destructions << '\n';
00068     }
00069 
00070   oss << " ---------------------------------------------------------------------------- \n";
00071 
00072   return oss.str();
00073 
00074 #else
00075 
00076   return "";
00077 
00078 #endif
00079 }

template<typename T >
int libMesh::PetscNonlinearSolver< T >::get_total_linear_iterations (  )  [inline, virtual]

Get the total number of linear iterations done in the last solve

Implements libMesh::NonlinearSolver< T >.

Definition at line 581 of file petsc_nonlinear_solver.C.

References libMesh::PetscNonlinearSolver< T >::_n_linear_iterations.

00582 {
00583   return _n_linear_iterations;
00584 }

void libMesh::ReferenceCounter::increment_constructor_count ( const std::string &  name  )  [inline, protected, inherited]

Increments the construction counter. Should be called in the constructor of any derived class that will be reference counted.

Definition at line 163 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::ReferenceCountedObject().

00164 {
00165   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00166   std::pair<unsigned int, unsigned int>& p = _counts[name];
00167 
00168   p.first++;
00169 }

void libMesh::ReferenceCounter::increment_destructor_count ( const std::string &  name  )  [inline, protected, inherited]

Increments the destruction counter. Should be called in the destructor of any derived class that will be reference counted.

Definition at line 176 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::~ReferenceCountedObject().

00177 {
00178   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00179   std::pair<unsigned int, unsigned int>& p = _counts[name];
00180 
00181   p.second++;
00182 }

template<typename T >
void libMesh::PetscNonlinearSolver< T >::init (  )  [inline, virtual]

Initialize data structures if not done so already.

Implements libMesh::NonlinearSolver< T >.

Reimplemented in libMesh::PetscDMNonlinearSolver< T >.

Definition at line 282 of file petsc_nonlinear_solver.C.

References libMesh::__libmesh_petsc_preconditioner_apply(), libMesh::__libmesh_petsc_preconditioner_setup(), libMesh::__libmesh_petsc_snes_monitor(), libMesh::NonlinearSolver< T >::_is_initialized, libMesh::NonlinearSolver< T >::_preconditioner, libMesh::PetscNonlinearSolver< T >::_snes, libMesh::COMM_WORLD, and libMesh::NonlinearSolver< T >::initialized().

Referenced by libMesh::PetscNonlinearSolver< T >::snes(), and libMesh::PetscNonlinearSolver< T >::solve().

00283 {
00284   // Initialize the data structures if not done so already.
00285   if (!this->initialized())
00286     {
00287       this->_is_initialized = true;
00288 
00289       int ierr=0;
00290 
00291 #if PETSC_VERSION_LESS_THAN(2,1,2)
00292       // At least until Petsc 2.1.1, the SNESCreate had a different calling syntax.
00293       // The second argument was of type SNESProblemType, and could have a value of
00294       // either SNES_NONLINEAR_EQUATIONS or SNES_UNCONSTRAINED_MINIMIZATION.
00295       ierr = SNESCreate(libMesh::COMM_WORLD, SNES_NONLINEAR_EQUATIONS, &_snes);
00296              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00297 
00298 #else
00299 
00300       ierr = SNESCreate(libMesh::COMM_WORLD,&_snes);
00301              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00302 
00303 #endif
00304 
00305 
00306 #if PETSC_VERSION_LESS_THAN(2,3,3)
00307       ierr = SNESSetMonitor (_snes, __libmesh_petsc_snes_monitor,
00308                              this, PETSC_NULL);
00309 #else
00310       // API name change in PETSc 2.3.3
00311       ierr = SNESMonitorSet (_snes, __libmesh_petsc_snes_monitor,
00312                              this, PETSC_NULL);
00313 #endif
00314       CHKERRABORT(libMesh::COMM_WORLD,ierr);
00315 
00316 #if PETSC_VERSION_LESS_THAN(3,1,0)
00317       // Cannot call SNESSetOptions before SNESSetFunction when using
00318       // any matrix free options with PETSc 3.1.0+
00319       ierr = SNESSetFromOptions(_snes);
00320              CHKERRABORT(libMesh::COMM_WORLD,ierr);
00321 #endif
00322 
00323       if(this->_preconditioner)
00324       {
00325         KSP ksp;
00326         ierr = SNESGetKSP (_snes, &ksp);
00327                CHKERRABORT(libMesh::COMM_WORLD,ierr);
00328         PC pc;
00329         ierr = KSPGetPC(ksp,&pc);
00330                CHKERRABORT(libMesh::COMM_WORLD,ierr);
00331 
00332         this->_preconditioner->init();
00333 
00334         PCSetType(pc, PCSHELL);
00335         PCShellSetContext(pc,(void*)this->_preconditioner);
00336 
00337         //Re-Use the shell functions from petsc_linear_solver
00338         PCShellSetSetUp(pc,__libmesh_petsc_preconditioner_setup);
00339         PCShellSetApply(pc,__libmesh_petsc_preconditioner_apply);
00340       }
00341     }
00342 }

template<typename T>
bool libMesh::NonlinearSolver< T >::initialized (  )  const [inline, inherited]
Returns:
true if the data structures are initialized, false otherwise.

Definition at line 84 of file nonlinear_solver.h.

Referenced by libMesh::PetscNonlinearSolver< T >::clear(), libMesh::PetscNonlinearSolver< T >::get_converged_reason(), libMesh::NoxNonlinearSolver< T >::init(), and libMesh::PetscNonlinearSolver< T >::init().

00084 { return _is_initialized; }

static unsigned int libMesh::ReferenceCounter::n_objects (  )  [inline, static, inherited]

Prints the number of outstanding (created, but not yet destroyed) objects.

Definition at line 79 of file reference_counter.h.

References libMesh::ReferenceCounter::_n_objects.

00080   { return _n_objects; }

template<typename T >
void libMesh::PetscNonlinearSolver< T >::print_converged_reason (  )  [inline, virtual]

Prints a useful message about why the latest nonlinear solve con(di)verged.

Reimplemented from libMesh::NonlinearSolver< T >.

Definition at line 557 of file petsc_nonlinear_solver.C.

References libMesh::PetscNonlinearSolver< T >::get_converged_reason(), and libMesh::out.

00558 {
00559 
00560   libMesh::out << "Nonlinear solver convergence/divergence reason: "
00561                << SNESConvergedReasons[this->get_converged_reason()] << std::endl;
00562 }

void libMesh::ReferenceCounter::print_info ( std::ostream &  out = libMesh::out  )  [static, inherited]

Prints the reference information, by default to libMesh::out.

Definition at line 88 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter, and libMesh::ReferenceCounter::get_info().

00089 {
00090   if( _enable_print_counter ) out_stream << ReferenceCounter::get_info();
00091 }

template<typename T >
void libMesh::PetscNonlinearSolver< T >::set_current_nonlinear_iteration_number ( unsigned  num  )  [inline]

This public setter is necessary since the value is computed in the __libmesh_petsc_snes_residual()/jacobian() function and must be stored somehow.

Definition at line 133 of file petsc_nonlinear_solver.h.

References libMesh::PetscNonlinearSolver< T >::_current_nonlinear_iteration_number.

template<typename T >
SNES libMesh::PetscNonlinearSolver< T >::snes (  )  [inline]

Returns the raw PETSc snes context pointer.

Definition at line 91 of file petsc_nonlinear_solver.h.

References libMesh::PetscNonlinearSolver< T >::_snes, and libMesh::PetscNonlinearSolver< T >::init().

00091 { this->init(); return _snes; }

template<typename T >
std::pair< unsigned int, Real > libMesh::PetscNonlinearSolver< T >::solve ( SparseMatrix< T > &  jac_in,
NumericVector< T > &  x_in,
NumericVector< T > &  r_in,
const   double,
const unsigned  int 
) [inline, virtual]

Call the Petsc solver. It calls the method below, using the same matrix for the system and preconditioner matrices.

Implements libMesh::NonlinearSolver< T >.

Reimplemented in libMesh::PetscDMNonlinearSolver< T >.

Definition at line 417 of file petsc_nonlinear_solver.C.

References libMesh::__libmesh_petsc_snes_jacobian(), libMesh::__libmesh_petsc_snes_residual(), libMesh::PetscNonlinearSolver< T >::_n_linear_iterations, libMesh::NonlinearSolver< T >::_preconditioner, libMesh::PetscNonlinearSolver< T >::_reason, libMesh::PetscNonlinearSolver< T >::_snes, libMesh::NonlinearSolver< T >::absolute_residual_tolerance, libMesh::PetscNonlinearSolver< T >::build_mat_null_space(), libMesh::PetscNonlinearSolver< T >::clear(), libMesh::COMM_WORLD, libMesh::NonlinearSolver< T >::converged, libMesh::PetscNonlinearSolver< T >::init(), libMesh::NonlinearSolver< T >::initial_linear_tolerance, libMesh::NonlinearSolver< T >::jacobian, libMesh::NonlinearSolver< T >::jacobian_object, libMesh::PetscMatrix< T >::mat(), libMesh::NonlinearSolver< T >::max_function_evaluations, libMesh::NonlinearSolver< T >::max_linear_iterations, libMesh::NonlinearSolver< T >::max_nonlinear_iterations, libMesh::NonlinearSolver< T >::nearnullspace, libMesh::NonlinearSolver< T >::nearnullspace_object, libMesh::NonlinearSolver< T >::nullspace, libMesh::NonlinearSolver< T >::nullspace_object, libMesh::Real, libMesh::NonlinearSolver< T >::relative_residual_tolerance, libMesh::NonlinearSolver< T >::relative_step_tolerance, libMesh::NonlinearSolver< T >::residual_and_jacobian_object, libMesh::NonlinearSolver< T >::system(), and libMesh::NonlinearSolver< T >::user_presolve.

00422 {
00423   START_LOG("solve()", "PetscNonlinearSolver");
00424   this->init ();
00425 
00426   // Make sure the data passed in are really of Petsc types
00427   PetscMatrix<T>* jac = libmesh_cast_ptr<PetscMatrix<T>*>(&jac_in);
00428   PetscVector<T>* x   = libmesh_cast_ptr<PetscVector<T>*>(&x_in);
00429   PetscVector<T>* r   = libmesh_cast_ptr<PetscVector<T>*>(&r_in);
00430 
00431   int ierr=0;
00432   int n_iterations =0;
00433   // Should actually be a PetscReal, but I don't know which version of PETSc first introduced PetscReal
00434   Real final_residual_norm=0.;
00435 
00436   ierr = SNESSetFunction (_snes, r->vec(), __libmesh_petsc_snes_residual, this);
00437          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00438 
00439    // Only set the jacobian function if we've been provided with something to call.
00440    // This allows a user to set their own jacobian function if they want to
00441    if (this->jacobian || this->jacobian_object || this->residual_and_jacobian_object)
00442    {
00443      ierr = SNESSetJacobian (_snes, jac->mat(), jac->mat(), __libmesh_petsc_snes_jacobian, this);
00444      CHKERRABORT(libMesh::COMM_WORLD,ierr);
00445    }
00446 #if !PETSC_VERSION_LESS_THAN(3,3,0)
00447    // Only set the nullspace if we have a way of computing it and the result is non-empty.
00448    if (this->nullspace || this->nullspace_object)
00449    {
00450      MatNullSpace msp;
00451      this->build_mat_null_space(this->nullspace_object, this->nullspace, &msp);
00452      if (msp)
00453        {
00454          ierr = MatSetNullSpace(jac->mat(), msp);
00455          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00456 
00457          ierr = MatNullSpaceDestroy(&msp);
00458          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00459        }
00460    }
00461 
00462    // Only set the nearnullspace if we have a way of computing it and the result is non-empty.
00463    if (this->nearnullspace || this->nearnullspace_object)
00464    {
00465      MatNullSpace msp = PETSC_NULL;
00466      this->build_mat_null_space(this->nearnullspace_object, this->nearnullspace, &msp);
00467 
00468      if(msp) {
00469        ierr = MatSetNearNullSpace(jac->mat(), msp);
00470        CHKERRABORT(libMesh::COMM_WORLD,ierr);
00471 
00472        ierr = MatNullSpaceDestroy(&msp);
00473        CHKERRABORT(libMesh::COMM_WORLD,ierr);
00474      }
00475    }
00476 #endif
00477    // Have the Krylov subspace method use our good initial guess rather than 0
00478    KSP ksp;
00479    ierr = SNESGetKSP (_snes, &ksp);
00480           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00481 
00482   // Set the tolerances for the iterative solver.  Use the user-supplied
00483   // tolerance for the relative residual & leave the others at default values
00484   ierr = KSPSetTolerances (ksp, this->initial_linear_tolerance, PETSC_DEFAULT,
00485                            PETSC_DEFAULT, this->max_linear_iterations);
00486          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00487 
00488   // Set the tolerances for the non-linear solver.
00489   ierr = SNESSetTolerances(_snes, this->absolute_residual_tolerance, this->relative_residual_tolerance,
00490                            this->relative_step_tolerance, this->max_nonlinear_iterations, this->max_function_evaluations);
00491          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00492 
00493   //Pull in command-line options
00494   KSPSetFromOptions(ksp);
00495   SNESSetFromOptions(_snes);
00496 
00497   if (this->user_presolve)
00498     this->user_presolve(this->system());
00499 
00500   //Set the preconditioning matrix
00501   if(this->_preconditioner)
00502   {
00503     this->_preconditioner->set_matrix(jac_in);
00504     this->_preconditioner->init();
00505   }
00506 
00507 //    ierr = KSPSetInitialGuessNonzero (ksp, PETSC_TRUE);
00508 //           CHKERRABORT(libMesh::COMM_WORLD,ierr);
00509 
00510 // Older versions (at least up to 2.1.5) of SNESSolve took 3 arguments,
00511 // the last one being a pointer to an int to hold the number of iterations required.
00512 # if PETSC_VERSION_LESS_THAN(2,2,0)
00513 
00514  ierr = SNESSolve (_snes, x->vec(), &n_iterations);
00515         CHKERRABORT(libMesh::COMM_WORLD,ierr);
00516 
00517 // 2.2.x style
00518 #elif PETSC_VERSION_LESS_THAN(2,3,0)
00519 
00520  ierr = SNESSolve (_snes, x->vec());
00521         CHKERRABORT(libMesh::COMM_WORLD,ierr);
00522 
00523 // 2.3.x & newer style
00524 #else
00525 
00526   ierr = SNESSolve (_snes, PETSC_NULL, x->vec());
00527          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00528 
00529   ierr = SNESGetIterationNumber(_snes,&n_iterations);
00530          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00531 
00532   ierr = SNESGetLinearSolveIterations(_snes, &_n_linear_iterations);
00533          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00534 
00535   ierr = SNESGetFunctionNorm(_snes,&final_residual_norm);
00536          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00537 
00538 #endif
00539 
00540   // Get and store the reason for convergence
00541   SNESGetConvergedReason(_snes, &_reason);
00542 
00543   //Based on Petsc 2.3.3 documentation all diverged reasons are negative
00544   this->converged = (_reason >= 0);
00545 
00546   this->clear();
00547 
00548   STOP_LOG("solve()", "PetscNonlinearSolver");
00549 
00550   // return the # of its. and the final residual norm.
00551   return std::make_pair(n_iterations, final_residual_norm);
00552 }

template<typename T>
sys_type& libMesh::NonlinearSolver< T >::system (  )  [inline, inherited]
Returns:
a writeable reference to the system we are solving.

Definition at line 222 of file nonlinear_solver.h.

00222 { return _system; }


Member Data Documentation

bool libMesh::ReferenceCounter::_enable_print_counter = true [static, protected, inherited]

Flag to control whether reference count information is printed when print_info is called.

Definition at line 137 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::disable_print_counter_info(), libMesh::ReferenceCounter::enable_print_counter_info(), and libMesh::ReferenceCounter::print_info().

template<typename T>
bool libMesh::NonlinearSolver< T >::_is_initialized [protected, inherited]

Mutual exclusion object to enable thread-safe reference counting.

Definition at line 131 of file reference_counter.h.

template<typename T >
int libMesh::PetscNonlinearSolver< T >::_n_linear_iterations

Stores the total number of linear iterations from the last solve.

Definition at line 152 of file petsc_nonlinear_solver.h.

Referenced by libMesh::PetscNonlinearSolver< T >::get_total_linear_iterations(), libMesh::PetscNonlinearSolver< T >::solve(), and libMesh::PetscDMNonlinearSolver< T >::solve().

Threads::atomic< unsigned int > libMesh::ReferenceCounter::_n_objects [static, protected, inherited]

The number of objects. Print the reference count information when the number returns to 0.

Definition at line 126 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::n_objects(), libMesh::ReferenceCounter::ReferenceCounter(), and libMesh::ReferenceCounter::~ReferenceCounter().

template<typename T >
SNESConvergedReason libMesh::PetscNonlinearSolver< T >::_reason

Store the reason for SNES convergence/divergence for use even after the _snes has been cleared. Note that print_converged_reason() will always *try* to get the current reason with SNESGetConvergedReason(), but if the SNES object has already been cleared, it will fall back on this stored value. Note that this value is therefore necessarily *not* cleared by the clear() function.

Definition at line 147 of file petsc_nonlinear_solver.h.

Referenced by libMesh::PetscNonlinearSolver< T >::get_converged_reason(), libMesh::PetscNonlinearSolver< T >::solve(), and libMesh::PetscDMNonlinearSolver< T >::solve().

template<typename T>
sys_type& libMesh::NonlinearSolver< T >::_system [protected, inherited]

A reference to the system we are solving.

Definition at line 293 of file nonlinear_solver.h.

Referenced by libMesh::NonlinearSolver< Number >::system().

template<typename T>
Real libMesh::NonlinearSolver< T >::absolute_residual_tolerance [inherited]

The NonlinearSolver should exit after the residual is reduced to either less than absolute_residual_tolerance or less than relative_residual_tolerance times the initial residual.

Users should increase any of these tolerances that they want to use for a stopping condition.

Definition at line 249 of file nonlinear_solver.h.

Referenced by libMesh::PetscDMNonlinearSolver< T >::init(), libMesh::NoxNonlinearSolver< T >::solve(), and libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
Real libMesh::NonlinearSolver< T >::absolute_step_tolerance [inherited]

The NonlinearSolver should exit after the full nonlinear step norm is reduced to either less than absolute_step_tolerance or less than relative_step_tolerance times the largest nonlinear solution which has been seen so far.

Users should increase any of these tolerances that they want to use for a stopping condition.

Note that not all NonlinearSolvers support relative_step_tolerance!

Definition at line 263 of file nonlinear_solver.h.

Referenced by libMesh::PetscDMNonlinearSolver< T >::init(), and libMesh::NoxNonlinearSolver< T >::solve().

template<typename T>
void(* libMesh::NonlinearSolver< T >::bounds)(NumericVector< Number > &XL, NumericVector< Number > &XU, sys_type &S) [inherited]

Function that computes the lower and upper bounds XL and XU on the solution of the nonlinear system.

Object that computes the bounds vectors $ XL $ and $ XU $.

Definition at line 179 of file nonlinear_solver.h.

template<typename T>
bool libMesh::NonlinearSolver< T >::converged [inherited]

After a call to solve this will reflect whether or not the nonlinear solve was successful.

Definition at line 287 of file nonlinear_solver.h.

Referenced by libMesh::NoxNonlinearSolver< T >::solve(), libMesh::PetscNonlinearSolver< T >::solve(), and libMesh::PetscDMNonlinearSolver< T >::solve().

template<typename T>
Real libMesh::NonlinearSolver< T >::initial_linear_tolerance [inherited]

Any required linear solves will at first be done with this tolerance; the NonlinearSolver may tighten the tolerance for later solves.

Definition at line 276 of file nonlinear_solver.h.

Referenced by libMesh::PetscDMNonlinearSolver< T >::init(), libMesh::NoxNonlinearSolver< T >::solve(), and libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
void(* libMesh::NonlinearSolver< T >::jacobian)(const NumericVector< Number > &X, SparseMatrix< Number > &J, sys_type &S) [inherited]

Object that computes the Jacobian J(X) of the nonlinear system at the input iterate X.

Definition at line 149 of file nonlinear_solver.h.

Referenced by libMesh::Problem_Interface::computeJacobian(), libMesh::Problem_Interface::computePreconditioner(), libMesh::NoxNonlinearSolver< T >::solve(), and libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
void(* libMesh::NonlinearSolver< T >::matvec)(const NumericVector< Number > &X, NumericVector< Number > *R, SparseMatrix< Number > *J, sys_type &S) [inherited]

Function that computes either the residual $ R(X) $ or the Jacobian $ J(X) $ of the nonlinear system at the input iterate $ X $. Note that either R or J could be XSNULL.

Referenced by libMesh::Problem_Interface::computeF(), libMesh::Problem_Interface::computeJacobian(), and libMesh::Problem_Interface::computePreconditioner().

template<typename T>
unsigned int libMesh::NonlinearSolver< T >::max_function_evaluations [inherited]

Maximum number of function evaluations.

Definition at line 237 of file nonlinear_solver.h.

Referenced by libMesh::PetscDMNonlinearSolver< T >::init(), and libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
unsigned int libMesh::NonlinearSolver< T >::max_linear_iterations [inherited]

Each linear solver step should exit after max_linear_iterations is exceeded.

Definition at line 270 of file nonlinear_solver.h.

Referenced by libMesh::PetscDMNonlinearSolver< T >::init(), libMesh::NoxNonlinearSolver< T >::solve(), and libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
unsigned int libMesh::NonlinearSolver< T >::max_nonlinear_iterations [inherited]
template<typename T>
Real libMesh::NonlinearSolver< T >::minimum_linear_tolerance [inherited]

The tolerance for linear solves is kept above this minimum

Definition at line 281 of file nonlinear_solver.h.

template<typename T>
void(* libMesh::NonlinearSolver< T >::nearnullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S) [inherited]

Function that computes a basis for the Jacobian's near nullspace -- the set of "low energy modes" -- that can be used for AMG coarsening, if the solver supports it (e.g., ML, PETSc's GAMG).

Referenced by libMesh::PetscNonlinearSolver< T >::solve().

A callable object that computes a basis for the Jacobian's near nullspace -- the set of "low energy modes" -- that can be used for AMG coarsening, if the solver supports it (e.g., ML, PETSc's GAMG).

Definition at line 209 of file nonlinear_solver.h.

Referenced by libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
void(* libMesh::NonlinearSolver< T >::nullspace)(std::vector< NumericVector< Number > * > &sp, sys_type &S) [inherited]

Function that computes a basis for the Jacobian's nullspace -- the kernel or the "zero energy modes" -- that can be used in solving a degenerate problem iteratively, if the solver supports it (e.g., PETSc's KSP).

Referenced by libMesh::PetscNonlinearSolver< T >::solve().

A callable object that computes a basis for the Jacobian's nullspace -- the kernel or the "zero energy modes" -- that can be used in solving a degenerate problem iteratively, if the solver supports it (e.g., PETSc's KSP).

Definition at line 195 of file nonlinear_solver.h.

Referenced by libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
Real libMesh::NonlinearSolver< T >::relative_step_tolerance [inherited]

Definition at line 264 of file nonlinear_solver.h.

Referenced by libMesh::PetscNonlinearSolver< T >::solve().

template<typename T>
void(* libMesh::NonlinearSolver< T >::residual)(const NumericVector< Number > &X, NumericVector< Number > &R, sys_type &S) [inherited]

Function that computes the residual R(X) of the nonlinear system at the input iterate X.

Referenced by libMesh::Problem_Interface::computeF().

Object that computes either the residual $ R(X) $ or the Jacobian $ J(X) $ of the nonlinear system at the input iterate $ X $. Note that either R or J could be XSNULL.

Definition at line 168 of file nonlinear_solver.h.

Referenced by libMesh::Problem_Interface::computeF(), libMesh::Problem_Interface::computeJacobian(), libMesh::Problem_Interface::computePreconditioner(), libMesh::NoxNonlinearSolver< T >::solve(), and libMesh::PetscNonlinearSolver< T >::solve().

Object that computes the residual R(X) of the nonlinear system at the input iterate X.

Definition at line 135 of file nonlinear_solver.h.

Referenced by libMesh::Problem_Interface::computeF().


The documentation for this class was generated from the following files:

Site Created By: libMesh Developers
Last modified: February 05 2013 19:55:33 UTC

Hosted By:
SourceForge.net Logo