PetscDiffSolver Class Reference

#include <petsc_diff_solver.h>

Inheritance diagram for PetscDiffSolver:

List of all members.

Public Types

typedef DiffSolver Parent
enum  SolveResult {
  INVALID_SOLVE_RESULT = 0, CONVERGED_NO_REASON = 1, CONVERGED_ABSOLUTE_RESIDUAL = 2, CONVERGED_RELATIVE_RESIDUAL = 4,
  CONVERGED_ABSOLUTE_STEP = 8, CONVERGED_RELATIVE_STEP = 16, DIVERGED_NO_REASON = 32, DIVERGED_MAX_NONLINEAR_ITERATIONS = 64,
  DIVERGED_BACKTRACKING_FAILURE = 128
}
typedef DifferentiableSystem sys_type

Public Member Functions

 PetscDiffSolver (sys_type &system)
virtual ~PetscDiffSolver ()
virtual void reinit ()
void init ()
void clear ()
virtual unsigned int solve ()
unsigned int total_outer_iterations ()
unsigned int total_inner_iterations ()
unsigned int solve_result ()
const sys_typesystem () const
sys_typesystem ()

Static Public Member Functions

static AutoPtr< DiffSolverbuild (sys_type &s)
static std::string get_info ()
static void print_info ()
static unsigned int n_objects ()

Public Attributes

unsigned int max_linear_iterations
unsigned int max_nonlinear_iterations
bool quiet
bool continue_after_max_iterations
bool continue_after_backtrack_failure
Real absolute_residual_tolerance
Real relative_residual_tolerance
Real absolute_step_tolerance
Real relative_step_tolerance
Real initial_linear_tolerance
Real minimum_linear_tolerance

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

SNES _snes
Real max_solution_norm
Real max_residual_norm
unsigned int _outer_iterations
unsigned int _inner_iterations
sys_type_system
unsigned int _solve_result

Static Protected Attributes

static Counts _counts
static Threads::atomic
< unsigned int > 
_n_objects
static Threads::spin_mutex _mutex


Detailed Description

This class defines a solver which uses a PETSc SNES context to handle a DifferentiableSystem

This class is part of the new DifferentiableSystem framework, which is still experimental. Users of this framework should beware of bugs and future API changes.

Author:
Roy H. Stogner 2008

Definition at line 52 of file petsc_diff_solver.h.


Member Typedef Documentation

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

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

Definition at line 105 of file reference_counter.h.

Definition at line 66 of file petsc_diff_solver.h.

The type of system

Definition at line 59 of file diff_solver.h.


Member Enumeration Documentation

enum DiffSolver::SolveResult [inherited]

Enumeration return type for the solve() function. Multiple SolveResults may be combined (OR'd) in the single return. To test which ones are present, just AND the return value with any of the SolveResult flags defined below.

Enumerator:
INVALID_SOLVE_RESULT  A default or invalid solve result. This usually means no solve has occurred yet.
CONVERGED_NO_REASON  The solver converged but no particular reason is specified.
CONVERGED_ABSOLUTE_RESIDUAL  The DiffSolver achieved the desired absolute residual tolerance.
CONVERGED_RELATIVE_RESIDUAL  The DiffSolver achieved the desired relative residual tolerance.
CONVERGED_ABSOLUTE_STEP  The DiffSolver achieved the desired absolute step size tolerance.
CONVERGED_RELATIVE_STEP  The DiffSolver achieved the desired relative step size tolerance.
DIVERGED_NO_REASON  The DiffSolver diverged but no particular reason is specified.
DIVERGED_MAX_NONLINEAR_ITERATIONS  The DiffSolver reached the maximum allowed number of nonlinear iterations before satisfying any convergence tests.
DIVERGED_BACKTRACKING_FAILURE  The DiffSolver failed to find a descent direction by backtracking (See newton_solver.C)

Definition at line 196 of file diff_solver.h.

00196                    {
00201     INVALID_SOLVE_RESULT = 0,
00202     
00207     CONVERGED_NO_REASON = 1,
00208 
00213     CONVERGED_ABSOLUTE_RESIDUAL = 2,
00214 
00219     CONVERGED_RELATIVE_RESIDUAL = 4,
00220 
00225     CONVERGED_ABSOLUTE_STEP = 8,
00226 
00231     CONVERGED_RELATIVE_STEP = 16,
00232 
00237     DIVERGED_NO_REASON = 32,
00238 
00244     DIVERGED_MAX_NONLINEAR_ITERATIONS = 64,
00245 
00250     DIVERGED_BACKTRACKING_FAILURE = 128
00251   };


Constructor & Destructor Documentation

PetscDiffSolver::PetscDiffSolver ( sys_type system  ) 

Constructor. Requires a reference to the system to be solved.

Definition at line 156 of file petsc_diff_solver.C.

00157   : Parent(s)
00158 {
00159 }

PetscDiffSolver::~PetscDiffSolver (  )  [virtual]

Destructor.

Definition at line 200 of file petsc_diff_solver.C.

00201 {
00202 }


Member Function Documentation

AutoPtr< DiffSolver > DiffSolver::build ( sys_type s  )  [static, inherited]

Factory. Requires a reference to the system to be solved. Returns a NewtonSolver by default

Definition at line 47 of file diff_solver.C.

Referenced by TimeSolver::init().

00048 {
00049   return AutoPtr<DiffSolver>(new NewtonSolver(s));
00050 }

void PetscDiffSolver::clear (  ) 

The clear function. solve() calls this to destroy a used SNES context.

Definition at line 206 of file petsc_diff_solver.C.

References _snes, and libMesh::COMM_WORLD.

Referenced by solve().

00207 {
00208   START_LOG("clear()", "PetscDiffSolver");
00209 
00210   int ierr=0;
00211 
00212   ierr = SNESDestroy(_snes);
00213   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00214 
00215   STOP_LOG("clear()", "PetscDiffSolver");
00216 }

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

Gets a string containing the reference information.

Definition at line 45 of file reference_counter.C.

References ReferenceCounter::_counts, and QuadratureRules::name().

Referenced by ReferenceCounter::print_info().

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

void 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 149 of file reference_counter.h.

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

Referenced by ReferenceCountedObject< SparseMatrix< T > >::ReferenceCountedObject().

00150 {
00151   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00152   std::pair<unsigned int, unsigned int>& p = _counts[name];
00153 
00154   p.first++;
00155 }

void 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 167 of file reference_counter.h.

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

Referenced by ReferenceCountedObject< SparseMatrix< T > >::~ReferenceCountedObject().

00168 {
00169   Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
00170   std::pair<unsigned int, unsigned int>& p = _counts[name];
00171 
00172   p.second++;
00173 }

void PetscDiffSolver::init (  )  [virtual]

The initialization function. solve() calls this to create a new SNES context.

Reimplemented from DiffSolver.

Definition at line 162 of file petsc_diff_solver.C.

References __libmesh_petsc_diff_solver_monitor(), _snes, libMesh::COMM_WORLD, and DiffSolver::init().

Referenced by solve().

00163 {
00164   START_LOG("init()", "PetscDiffSolver");
00165 
00166   Parent::init();
00167 
00168   int ierr=0;
00169 
00170 #if PETSC_VERSION_LESS_THAN(2,1,2)
00171   // At least until Petsc 2.1.1, the SNESCreate had a different
00172   // calling syntax.  The second argument was of type SNESProblemType,
00173   // and could have a value of either SNES_NONLINEAR_EQUATIONS or
00174   // SNES_UNCONSTRAINED_MINIMIZATION.
00175   ierr = SNESCreate(libMesh::COMM_WORLD, SNES_NONLINEAR_EQUATIONS, &_snes);
00176   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00177 #else
00178   ierr = SNESCreate(libMesh::COMM_WORLD,&_snes);
00179   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00180 #endif
00181 
00182 #if PETSC_VERSION_LESS_THAN(2,3,3)
00183   ierr = SNESSetMonitor (_snes, __libmesh_petsc_diff_solver_monitor,
00184                          this, PETSC_NULL);
00185 #else
00186   // API name change in PETSc 2.3.3
00187   ierr = SNESMonitorSet (_snes, __libmesh_petsc_diff_solver_monitor,
00188                          this, PETSC_NULL);
00189 #endif
00190   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00191 
00192   ierr = SNESSetFromOptions(_snes);
00193   CHKERRABORT(libMesh::COMM_WORLD,ierr);
00194 
00195   STOP_LOG("init()", "PetscDiffSolver");
00196 }

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

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

Definition at line 76 of file reference_counter.h.

References ReferenceCounter::_n_objects.

00077   { return _n_objects; }

void ReferenceCounter::print_info (  )  [static, inherited]

Prints the reference information to std::cout.

Definition at line 83 of file reference_counter.C.

References ReferenceCounter::get_info().

00084 {
00085 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
00086   
00087   std::cout << ReferenceCounter::get_info();
00088   
00089 #endif
00090 }

void PetscDiffSolver::reinit (  )  [virtual]

The reinitialization function. This method is used after changes in the mesh.

Reimplemented from DiffSolver.

Definition at line 220 of file petsc_diff_solver.C.

References DiffSolver::reinit().

00221 {
00222   Parent::reinit();
00223 }

unsigned int PetscDiffSolver::solve (  )  [virtual]

This method performs a solve. What occurs in this method will depend on the PETSc SNES settings. See the PETSc documentation for more details.

Implements DiffSolver.

Definition at line 227 of file petsc_diff_solver.C.

References __libmesh_petsc_diff_solver_jacobian(), __libmesh_petsc_diff_solver_residual(), DiffSolver::_outer_iterations, _snes, DiffSolver::_system, clear(), PetscMatrix< T >::close(), PetscVector< T >::close(), libMesh::COMM_WORLD, DiffSolver::CONVERGED_RELATIVE_RESIDUAL, DofMap::enforce_constraints_exactly(), System::get_dof_map(), init(), PetscMatrix< T >::mat(), ImplicitSystem::matrix, ExplicitSystem::rhs, System::solution, and PetscVector< T >::vec().

00228 {
00229   this->init();
00230 
00231   START_LOG("solve()", "PetscDiffSolver");
00232 
00233   PetscVector<Number> &x =
00234     *(libmesh_cast_ptr<PetscVector<Number>*>(_system.solution.get()));
00235   PetscMatrix<Number> &jac =
00236     *(libmesh_cast_ptr<PetscMatrix<Number>*>(_system.matrix));
00237   PetscVector<Number> &r =
00238     *(libmesh_cast_ptr<PetscVector<Number>*>(_system.rhs));
00239 
00240   x.close();
00241   r.close();
00242   jac.close();
00243 
00244 #ifdef LIBMESH_ENABLE_AMR
00245   _system.get_dof_map().enforce_constraints_exactly(_system);
00246 #endif
00247 
00248   int ierr = 0;
00249 
00250   ierr = SNESSetFunction (_snes, r.vec(),
00251                           __libmesh_petsc_diff_solver_residual, this);
00252     CHKERRABORT(libMesh::COMM_WORLD,ierr);
00253 
00254   ierr = SNESSetJacobian (_snes, jac.mat(), jac.mat(),
00255                           __libmesh_petsc_diff_solver_jacobian, this);
00256     CHKERRABORT(libMesh::COMM_WORLD,ierr);
00257 
00258 # if PETSC_VERSION_LESS_THAN(2,2,0)
00259 
00260   ierr = SNESSolve (_snes, x.vec(), &_outer_iterations);
00261          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00262 
00263 // 2.2.x style
00264 #elif PETSC_VERSION_LESS_THAN(2,3,0)
00265 
00266   ierr = SNESSolve (_snes, x.vec());
00267          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00268 
00269 // 2.3.x & newer style
00270 #else
00271 
00272   ierr = SNESSolve (_snes, PETSC_NULL, x.vec());
00273          CHKERRABORT(libMesh::COMM_WORLD,ierr);
00274 
00275 #endif
00276 
00277   STOP_LOG("solve()", "PetscDiffSolver");
00278 
00279   this->clear();
00280 
00281   // FIXME - We'll worry about getting the solve result right later...
00282   
00283   return DiffSolver::CONVERGED_RELATIVE_RESIDUAL;
00284 }

unsigned int DiffSolver::solve_result (  )  [inline, inherited]

Returns:
the value of the SolveResult from the last solve.

Definition at line 112 of file diff_solver.h.

References DiffSolver::_solve_result.

00112 { return _solve_result; }

sys_type& DiffSolver::system (  )  [inline, inherited]

Returns:
a writeable reference to the system we are solving.

Definition at line 122 of file diff_solver.h.

References DiffSolver::_system.

00122 { return _system; }

const sys_type& DiffSolver::system (  )  const [inline, inherited]

Returns:
a constant reference to the system we are solving.

Definition at line 117 of file diff_solver.h.

References DiffSolver::_system.

Referenced by __libmesh_petsc_diff_solver_jacobian(), and __libmesh_petsc_diff_solver_residual().

00117 { return _system; }

unsigned int DiffSolver::total_inner_iterations (  )  [inline, inherited]

Returns:
the number of "inner" (e.g. Krylov) iterations required by the last solve.

Definition at line 107 of file diff_solver.h.

References DiffSolver::_inner_iterations.

00107 { return _inner_iterations; }

unsigned int DiffSolver::total_outer_iterations (  )  [inline, inherited]

Returns:
the number of "outer" (e.g. quasi-Newton) iterations required by the last solve.

Definition at line 101 of file diff_solver.h.

References DiffSolver::_outer_iterations.

00101 { return _outer_iterations; }


Member Data Documentation

unsigned int DiffSolver::_inner_iterations [protected, inherited]

The number of inner iterations used by the last solve

Definition at line 276 of file diff_solver.h.

Referenced by NewtonSolver::solve(), and DiffSolver::total_inner_iterations().

Threads::spin_mutex ReferenceCounter::_mutex [static, protected, inherited]

Mutual exclusion object to enable thread-safe reference counting.

Definition at line 123 of file reference_counter.h.

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

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

Definition at line 118 of file reference_counter.h.

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

unsigned int DiffSolver::_outer_iterations [protected, inherited]

The number of outer iterations used by the last solve

Definition at line 271 of file diff_solver.h.

Referenced by NewtonSolver::line_search(), solve(), NewtonSolver::solve(), and DiffSolver::total_outer_iterations().

SNES PetscDiffSolver::_snes [protected]

Nonlinear solver context

Definition at line 98 of file petsc_diff_solver.h.

Referenced by clear(), init(), and solve().

unsigned int DiffSolver::_solve_result [protected, inherited]

Initialized to zero. solve_result is typically set internally in the solve() function before it returns. When non-zero, solve_result tells the result of the latest solve. See enum definition for description.

Definition at line 289 of file diff_solver.h.

Referenced by NewtonSolver::line_search(), NewtonSolver::solve(), DiffSolver::solve_result(), and NewtonSolver::test_convergence().

sys_type& DiffSolver::_system [protected, inherited]

A reference to the system we are solving.

Definition at line 281 of file diff_solver.h.

Referenced by NewtonSolver::line_search(), solve(), NewtonSolver::solve(), and DiffSolver::system().

The DiffSolver 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 165 of file diff_solver.h.

Referenced by NewtonSolver::print_convergence(), and NewtonSolver::test_convergence().

The DiffSolver 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.

Definition at line 177 of file diff_solver.h.

Referenced by NewtonSolver::print_convergence(), and NewtonSolver::test_convergence().

Defaults to false, telling the DiffSolver to throw a libmesh_error() when the backtracking scheme fails to find a descent direction.

Definition at line 154 of file diff_solver.h.

Referenced by NewtonSolver::line_search().

Defaults to true, telling the DiffSolver to continue rather than exit when a solve has reached its maximum number of nonlinear iterations.

Definition at line 148 of file diff_solver.h.

Referenced by NewtonSolver::solve().

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

Definition at line 184 of file diff_solver.h.

Referenced by NewtonSolver::solve().

unsigned int DiffSolver::max_linear_iterations [inherited]

Each linear solver step should exit after max_linear_iterations is exceeded.

Definition at line 128 of file diff_solver.h.

Referenced by ContinuationSystem::continuation_solve(), NewtonSolver::solve(), and ContinuationSystem::solve_tangent().

unsigned int DiffSolver::max_nonlinear_iterations [inherited]

The DiffSolver should exit in failure if max_nonlinear_iterations is exceeded and continue_after_max_iterations is false, or should end the nonlinear solve if max_nonlinear_iterations is exceeded and continue_after_max_iterations is true.

Definition at line 136 of file diff_solver.h.

Referenced by ContinuationSystem::continuation_solve(), NewtonSolver::solve(), and ContinuationSystem::update_solution().

Real DiffSolver::max_residual_norm [protected, inherited]

The largest nonlinear residual which the DiffSolver has yet seen will be stored here, to be used for stopping criteria based on relative_residual_tolerance

Definition at line 266 of file diff_solver.h.

Referenced by DiffSolver::init(), NewtonSolver::print_convergence(), DiffSolver::reinit(), NewtonSolver::solve(), and NewtonSolver::test_convergence().

Real DiffSolver::max_solution_norm [protected, inherited]

The largest solution norm which the DiffSolver has yet seen will be stored here, to be used for stopping criteria based on relative_step_tolerance

Definition at line 259 of file diff_solver.h.

Referenced by DiffSolver::init(), NewtonSolver::print_convergence(), DiffSolver::reinit(), NewtonSolver::solve(), and NewtonSolver::test_convergence().

The tolerance for linear solves is kept above this minimum

Definition at line 189 of file diff_solver.h.

Referenced by NewtonSolver::solve().

bool DiffSolver::quiet [inherited]

The DiffSolver should not print anything to std::cout unless quiet is set to false

Definition at line 142 of file diff_solver.h.

Referenced by NewtonSolver::line_search(), NewtonSolver::print_convergence(), and NewtonSolver::solve().


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

Site Created By: libMesh Developers
Last modified: November 25 2009 03:44:44.

Hosted By:
SourceForge.net Logo