www.mooseframework.org
Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes | List of all members
PetscContactLineSearch Class Reference

Petsc implementation of the contact line search (based on the Petsc LineSearchShell) More...

#include <PetscContactLineSearch.h>

Inheritance diagram for PetscContactLineSearch:
[legend]

Public Types

typedef DataFileName DataFileParameterType
 

Public Member Functions

 PetscContactLineSearch (const InputParameters &parameters)
 
virtual void lineSearch () override
 
void printContactInfo (const std::set< dof_id_type > &contact_set)
 Method for printing the contact information. More...
 
void insertSet (const std::set< dof_id_type > &mech_set)
 Unionize sets from different constraints. More...
 
virtual void reset ()
 Reset the line search data. More...
 
void zeroIts ()
 
size_t nlIts () const
 
virtual void timestepSetup ()
 
virtual void customSetup (const ExecFlagType &)
 
virtual void initialSetup ()
 
virtual bool enabled () const
 
std::shared_ptr< MooseObjectgetSharedPtr ()
 
std::shared_ptr< const MooseObjectgetSharedPtr () const
 
MooseAppgetMooseApp () const
 
const std::string & type () const
 
virtual const std::string & name () const
 
std::string typeAndName () const
 
std::string errorPrefix (const std::string &error_type) const
 
void callMooseError (std::string msg, const bool with_prefix) const
 
MooseObjectParameterName uniqueParameterName (const std::string &parameter_name) const
 
const InputParametersparameters () const
 
MooseObjectName uniqueName () const
 
const T & getParam (const std::string &name) const
 
std::vector< std::pair< T1, T2 > > getParam (const std::string &param1, const std::string &param2) const
 
const T & getRenamedParam (const std::string &old_name, const std::string &new_name) const
 
getCheckedPointerParam (const std::string &name, const std::string &error_string="") const
 
bool isParamValid (const std::string &name) const
 
bool isParamSetByUser (const std::string &nm) const
 
void paramError (const std::string &param, Args... args) const
 
void paramWarning (const std::string &param, Args... args) const
 
void paramInfo (const std::string &param, Args... args) const
 
void connectControllableParams (const std::string &parameter, const std::string &object_type, const std::string &object_name, const std::string &object_parameter) const
 
void mooseError (Args &&... args) const
 
void mooseErrorNonPrefixed (Args &&... args) const
 
void mooseDocumentedError (const std::string &repo_name, const unsigned int issue_num, Args &&... args) const
 
void mooseWarning (Args &&... args) const
 
void mooseWarningNonPrefixed (Args &&... args) const
 
void mooseDeprecated (Args &&... args) const
 
void mooseInfo (Args &&... args) const
 
std::string getDataFileName (const std::string &param) const
 
std::string getDataFileNameByName (const std::string &name, const std::string *param=nullptr) const
 
const Parallel::Communicator & comm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static InputParameters validParams ()
 

Public Attributes

const ConsoleStream _console
 

Protected Attributes

PetscNonlinearSolver< Real > * _solver
 
std::set< dof_id_type_current_contact_state
 The current contact set. More...
 
std::set< dof_id_type_old_contact_state
 The old contact set. More...
 
Real _user_ksp_rtol
 the linear tolerance set by the user in the input file More...
 
bool _user_ksp_rtol_set
 Whether the user linear tolerance has been set yet in this object. More...
 
Real _contact_lambda
 The multiplier of the newton step. More...
 
unsigned _allowed_lambda_cuts
 How many times the linsearch is allowed to cut lambda. More...
 
Real _contact_ltol
 What the linear tolerance should be while the contact state is changing. More...
 
bool _affect_ltol
 Whether to modify the linear tolerance. More...
 
FEProblem_fe_problem
 
size_t _nl_its
 
const bool & _enabled
 
MooseApp_app
 
const std::string _type
 
const std::string _name
 
const InputParameters_pars
 
Factory_factory
 
ActionFactory_action_factory
 
const Parallel::Communicator & _communicator
 

Detailed Description

Petsc implementation of the contact line search (based on the Petsc LineSearchShell)

Definition at line 28 of file PetscContactLineSearch.h.

Constructor & Destructor Documentation

◆ PetscContactLineSearch()

PetscContactLineSearch::PetscContactLineSearch ( const InputParameters parameters)

Definition at line 25 of file PetscContactLineSearch.C.

27 {
28  _solver = dynamic_cast<PetscNonlinearSolver<Real> *>(
29  _fe_problem.getNonlinearSystem(/*nl_sys_num=*/0).nonlinearSolver());
30  if (!_solver)
31  mooseError(
32  "This line search operates only with Petsc, so Petsc must be your nonlinear solver.");
33 }
FEProblem & _fe_problem
ContactLineSearchBase(const InputParameters &parameters)
virtual NonlinearSolver< Number > * nonlinearSolver() override
void mooseError(Args &&... args) const
const InputParameters & parameters() const
PetscNonlinearSolver< Real > * _solver
virtual NonlinearSystem & getNonlinearSystem(const unsigned int nl_sys_num) override

Member Function Documentation

◆ insertSet()

void ContactLineSearchBase::insertSet ( const std::set< dof_id_type > &  mech_set)
inherited

Unionize sets from different constraints.

Definition at line 53 of file ContactLineSearchBase.C.

Referenced by MechanicalContactConstraint::residualEnd().

54 {
55  if (_current_contact_state.empty())
56  _current_contact_state = mech_set;
57  else
58  for (auto & node : mech_set)
59  _current_contact_state.insert(node);
60 }
std::set< dof_id_type > _current_contact_state
The current contact set.

◆ lineSearch()

void PetscContactLineSearch::lineSearch ( )
overridevirtual

Reimplemented from LineSearch.

Definition at line 36 of file PetscContactLineSearch.C.

37 {
38  PetscBool changed_y = PETSC_FALSE, changed_w = PETSC_FALSE;
39  PetscErrorCode ierr;
40  Vec X, F, Y, W, G, W1;
41  SNESLineSearch line_search;
42  PetscReal fnorm, xnorm, ynorm, gnorm;
43  PetscBool domainerror;
44  PetscReal ksp_rtol, ksp_abstol, ksp_dtol;
45  PetscInt ksp_maxits;
46  KSP ksp;
47  SNES snes = _solver->snes();
48 
49  ierr = SNESGetLineSearch(snes, &line_search);
50  LIBMESH_CHKERR(ierr);
51  ierr = SNESLineSearchGetVecs(line_search, &X, &F, &Y, &W, &G);
52  LIBMESH_CHKERR(ierr);
53  ierr = SNESLineSearchGetNorms(line_search, &xnorm, &fnorm, &ynorm);
54  LIBMESH_CHKERR(ierr);
55  ierr = SNESLineSearchGetSNES(line_search, &snes);
56  LIBMESH_CHKERR(ierr);
57  ierr = SNESLineSearchSetReason(line_search, SNES_LINESEARCH_SUCCEEDED);
58  LIBMESH_CHKERR(ierr);
59  ierr = SNESGetKSP(snes, &ksp);
60  LIBMESH_CHKERR(ierr);
61  ierr = KSPGetTolerances(ksp, &ksp_rtol, &ksp_abstol, &ksp_dtol, &ksp_maxits);
62  LIBMESH_CHKERR(ierr);
63  ierr = VecDuplicate(W, &W1);
64  LIBMESH_CHKERR(ierr);
65 
66  if (!_user_ksp_rtol_set)
67  {
68  _user_ksp_rtol = ksp_rtol;
69  _user_ksp_rtol_set = true;
70  }
71 
72  ++_nl_its;
73 
74  /* precheck */
75  ierr = SNESLineSearchPreCheck(line_search, X, Y, &changed_y);
76  LIBMESH_CHKERR(ierr);
77 
78  /* temporary update */
79  _contact_lambda = 1.;
80  ierr = VecWAXPY(W, -_contact_lambda, Y, X);
81  LIBMESH_CHKERR(ierr);
82 
83  /* compute residual to determine whether contact state has changed since the last non-linear
84  * residual evaluation */
85  _current_contact_state.clear();
86  ierr = SNESComputeFunction(snes, W, F);
87  LIBMESH_CHKERR(ierr);
88  ierr = SNESGetFunctionDomainError(snes, &domainerror);
89  LIBMESH_CHKERR(ierr);
90  if (domainerror)
91  {
92  ierr = SNESLineSearchSetReason(line_search, SNES_LINESEARCH_FAILED_DOMAIN);
93  LIBMESH_CHKERR(ierr);
94  }
95  ierr = VecNorm(F, NORM_2, &fnorm);
96  LIBMESH_CHKERR(ierr);
97  std::set<dof_id_type> contact_state_stored = _current_contact_state;
98  _current_contact_state.clear();
99  printContactInfo(contact_state_stored);
100 
101  if (_affect_ltol)
102  {
103  if (contact_state_stored != _old_contact_state)
104  {
105  KSPSetTolerances(ksp, _contact_ltol, ksp_abstol, ksp_dtol, ksp_maxits);
106  _console << "Contact set changed since previous non-linear iteration!" << std::endl;
107  }
108  else
109  KSPSetTolerances(ksp, _user_ksp_rtol, ksp_abstol, ksp_dtol, ksp_maxits);
110  }
111 
112  size_t ls_its = 0;
113  while (ls_its < _allowed_lambda_cuts)
114  {
115  _contact_lambda *= 0.5;
116  /* update */
117  ierr = VecWAXPY(W1, -_contact_lambda, Y, X);
118  LIBMESH_CHKERR(ierr);
119 
120  ierr = SNESComputeFunction(snes, W1, G);
121  LIBMESH_CHKERR(ierr);
122  ierr = SNESGetFunctionDomainError(snes, &domainerror);
123  LIBMESH_CHKERR(ierr);
124  if (domainerror)
125  {
126  ierr = SNESLineSearchSetReason(line_search, SNES_LINESEARCH_FAILED_DOMAIN);
127  LIBMESH_CHKERR(ierr);
128  }
129  ierr = VecNorm(G, NORM_2, &gnorm);
130  LIBMESH_CHKERR(ierr);
131  if (gnorm < fnorm)
132  {
133  VecCopy(G, F);
134  LIBMESH_CHKERR(ierr);
135  VecCopy(W1, W);
136  LIBMESH_CHKERR(ierr);
137  fnorm = gnorm;
138  contact_state_stored.swap(_current_contact_state);
139  _current_contact_state.clear();
140  printContactInfo(contact_state_stored);
141  ++ls_its;
142  }
143  else
144  break;
145  }
146 
147  ierr = VecScale(Y, _contact_lambda);
148  LIBMESH_CHKERR(ierr);
149  /* postcheck */
150  ierr = SNESLineSearchPostCheck(line_search, X, Y, W, &changed_y, &changed_w);
151  LIBMESH_CHKERR(ierr);
152 
153  if (changed_y)
154  {
155  ierr = VecWAXPY(W, -1., Y, X);
156  LIBMESH_CHKERR(ierr);
157  }
158 
159  if (changed_w || changed_y)
160  {
161  ierr = SNESComputeFunction(snes, W, F);
162  LIBMESH_CHKERR(ierr);
163  ierr = SNESGetFunctionDomainError(snes, &domainerror);
164  LIBMESH_CHKERR(ierr);
165  if (domainerror)
166  {
167  ierr = SNESLineSearchSetReason(line_search, SNES_LINESEARCH_FAILED_DOMAIN);
168  LIBMESH_CHKERR(ierr);
169  }
170  contact_state_stored.swap(_current_contact_state);
171  _current_contact_state.clear();
172  printContactInfo(contact_state_stored);
173  }
174 
175  /* copy the solution over */
176  ierr = VecCopy(W, X);
177  LIBMESH_CHKERR(ierr);
178 
179  ierr = SNESLineSearchComputeNorms(line_search);
180  LIBMESH_CHKERR(ierr);
181 
182  ierr = VecDestroy(&W1);
183  LIBMESH_CHKERR(ierr);
184 
185  _old_contact_state = std::move(contact_state_stored);
186 }
bool _affect_ltol
Whether to modify the linear tolerance.
std::set< dof_id_type > _old_contact_state
The old contact set.
Real _contact_ltol
What the linear tolerance should be while the contact state is changing.
std::set< dof_id_type > _current_contact_state
The current contact set.
ierr
static const std::string F
Definition: NS.h:150
static const std::string G
Definition: NS.h:151
Real _contact_lambda
The multiplier of the newton step.
unsigned _allowed_lambda_cuts
How many times the linsearch is allowed to cut lambda.
void printContactInfo(const std::set< dof_id_type > &contact_set)
Method for printing the contact information.
const ConsoleStream _console
Real _user_ksp_rtol
the linear tolerance set by the user in the input file
PetscNonlinearSolver< Real > * _solver
size_t _nl_its
bool _user_ksp_rtol_set
Whether the user linear tolerance has been set yet in this object.

◆ printContactInfo()

void ContactLineSearchBase::printContactInfo ( const std::set< dof_id_type > &  contact_set)
inherited

Method for printing the contact information.

Definition at line 44 of file ContactLineSearchBase.C.

Referenced by lineSearch().

45 {
46  if (!contact_set.empty())
47  _console << contact_set.size() << " nodes in contact" << std::endl;
48  else
49  _console << "No nodes in contact" << std::endl;
50 }
const ConsoleStream _console

◆ reset()

void ContactLineSearchBase::reset ( )
virtualinherited

Reset the line search data.

Definition at line 63 of file ContactLineSearchBase.C.

Referenced by MechanicalContactConstraint::timestepSetup().

64 {
65  _current_contact_state.clear();
66  zeroIts();
67 }
std::set< dof_id_type > _current_contact_state
The current contact set.
void zeroIts()

◆ validParams()

InputParameters PetscContactLineSearch::validParams ( )
static

Definition at line 20 of file PetscContactLineSearch.C.

21 {
23 }
static InputParameters validParams()

Member Data Documentation

◆ _affect_ltol

bool ContactLineSearchBase::_affect_ltol
protectedinherited

Whether to modify the linear tolerance.

Definition at line 77 of file ContactLineSearchBase.h.

Referenced by lineSearch().

◆ _allowed_lambda_cuts

unsigned ContactLineSearchBase::_allowed_lambda_cuts
protectedinherited

How many times the linsearch is allowed to cut lambda.

Definition at line 71 of file ContactLineSearchBase.h.

Referenced by lineSearch().

◆ _contact_lambda

Real ContactLineSearchBase::_contact_lambda
protectedinherited

The multiplier of the newton step.

Definition at line 68 of file ContactLineSearchBase.h.

Referenced by lineSearch().

◆ _contact_ltol

Real ContactLineSearchBase::_contact_ltol
protectedinherited

What the linear tolerance should be while the contact state is changing.

Definition at line 74 of file ContactLineSearchBase.h.

Referenced by lineSearch().

◆ _current_contact_state

std::set<dof_id_type> ContactLineSearchBase::_current_contact_state
protectedinherited

The current contact set.

Definition at line 58 of file ContactLineSearchBase.h.

Referenced by ContactLineSearchBase::insertSet(), lineSearch(), and ContactLineSearchBase::reset().

◆ _old_contact_state

std::set<dof_id_type> ContactLineSearchBase::_old_contact_state
protectedinherited

The old contact set.

Definition at line 60 of file ContactLineSearchBase.h.

Referenced by lineSearch().

◆ _solver

PetscNonlinearSolver<Real>* PetscContactLineSearch::_solver
protected

Definition at line 38 of file PetscContactLineSearch.h.

Referenced by lineSearch(), and PetscContactLineSearch().

◆ _user_ksp_rtol

Real ContactLineSearchBase::_user_ksp_rtol
protectedinherited

the linear tolerance set by the user in the input file

Definition at line 63 of file ContactLineSearchBase.h.

Referenced by lineSearch().

◆ _user_ksp_rtol_set

bool ContactLineSearchBase::_user_ksp_rtol_set
protectedinherited

Whether the user linear tolerance has been set yet in this object.

Definition at line 65 of file ContactLineSearchBase.h.

Referenced by lineSearch().


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