#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass extarticle
\begin_preamble
\newcommand{\pd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\fdo}[1]{\mathcal{D}_{#1}}
\newcommand{\sdo}[1]{D_{#1}}
\newcommand{\tdo}[1]{D_{#1}}
\newcommand{\fd}[2]{\fdo{#1} #2}
\newcommand{\sd}[2]{\sdo{#1} #2}
\newcommand{\td}[2]{\tdo{#1} #2}
\newcommand{\pdd}[2]{\frac{\partial^2 #1}{\partial #2^2}}
\newcommand{\pddd}[2]{\frac{\partial^3 #1}{\partial #2^3}}
\newcommand{\pmix}[3]{\frac{\partial^2 #1}{\partial #2 \partial #3}}
\newcommand{\pmixx}[4]{\frac{\partial^3 #1}{\partial #2 \partial #3 \partial #4}}
\newcommand{\funcd}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\spaced}[2]{{\cal D}^{#1} #2}
\newcommand{\totald}[2]{{D}^{#1} #2}

\newcommand{\abs}[1]{\left\vert{#1}\right\vert}
\newcommand{\od}[2]{\frac{d #1}{d #2}}
\newcommand{\odd}[2]{\frac{d^2 #1}{d #2^2}}
\newcommand{\R}{\mathbb{R}}

\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}
\definecolor{lightgray}{RGB}{240,240,240}
\definecolor{bluegray}{RGB}{180,180,200}
\lstset{basicstyle={\sffamily\footnotesize},language={C++},tabsize=2,showstringspaces=false,frame=shadowbox,breaklines=true,mathescape=true,commentstyle={\itshape\textcolor{red}},lineskip=1pt,aboveskip=\bigskipamount,belowskip=\bigskipamount, rulesepcolor=\color{bluegray}}
\end_preamble
\use_default_options true
\begin_modules
theorems-ams
theorems-ams-extended
\end_modules
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman palatino
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100

\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize 12
\spacing single
\use_hyperref false
\papersize default
\use_geometry true
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 0
\branch answers
\selected 1
\filename_suffix 0
\color #5555ff
\end_branch
\index Index
\shortcut idx
\color #008000
\end_index
\leftmargin 0.9in
\topmargin 0.75in
\rightmargin 0.9in
\bottommargin 0.75in
\secnumdepth 3
\tocdepth 3
\paragraph_separation skip
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header

\begin_body

\begin_layout Title
Nonlinear examples
\end_layout

\begin_layout Standard
\begin_inset CommandInset toc
LatexCommand tableofcontents

\end_inset


\end_layout

\begin_layout Section
Bratu's equation
\end_layout

\begin_layout Standard
Bratu's problem is to solve the nonlinear equation 
\begin_inset Formula 
\begin{equation}
-\nabla^{2}u=\lambda e^{u}+f\left(x\right)\;\;\;\mbox{in }\Omega\label{eq:quadraticPoisson}
\end{equation}

\end_inset

with boundary conditions such as
\begin_inset Formula 
\[
u=0\;\;\;\mbox{on }\partial\Omega.
\]

\end_inset

In one dimension with 
\begin_inset Formula $f=0$
\end_inset

, the equation can be solved in closed form by an energy method.
 Otherwise, it is not generally tractable in closed form.
 However, we can always construct a solution by the method of manufactured
 solutions (MMS).
 In this set of examples, we'll produce an exactly solvable problem by the
 MMS, then proceed to solve it using a number of different methods.
\end_layout

\begin_layout Subsection
Manufacturing a problem to match a solution 
\end_layout

\begin_layout Standard
The MMS is very simple.
 Define a problem domain 
\begin_inset Formula $\Omega$
\end_inset

, and pick any convenient function 
\begin_inset Formula $\hat{u}$
\end_inset

 defined on that domain.
 Suppose 
\begin_inset Formula $R$
\end_inset

 is the operator appearing in the equation under consideration; in the case
 of the Bratu problem, 
\begin_inset Formula $R\left(u\right)=-\nabla^{2}u-\lambda e^{u}$
\end_inset

.
 Construct the equation
\begin_inset Formula 
\[
R\left(u\right)=R\left(\hat{u}\right)
\]

\end_inset

and boundary conditions of a form appropriate to the problem of interest.
 For example, construct Robin boundary conditions as
\begin_inset Formula 
\[
\alpha u+\beta\pd{u}{n}=\alpha\hat{u}+\beta\pd{\hat{u}}{n}.
\]

\end_inset

More generally, if the boundary conditions are nonlinear (such as radiative
 boundary conditions in heat transfer), construct manufactured boundary
 conditions
\begin_inset Formula 
\[
B\left(u\right)=B\left(\hat{u}\right)
\]

\end_inset

where 
\begin_inset Formula $B$
\end_inset

 is the nonlinear operator appearing in the BCs.
 
\end_layout

\begin_layout Standard
By construction, 
\begin_inset Formula $u=\hat{u}$
\end_inset

 is an exact solution to the manufactured boundary value problem.
 Note that the solution may not be unique.
\end_layout

\begin_layout Subsubsection
A manufactured Bratu's problem in 1D
\end_layout

\begin_layout Standard
We'll work on 
\begin_inset Formula $\Omega=\left[0,1\right]$
\end_inset

 with homogeneous Dirichlet boundary conditions.
 The function 
\begin_inset Formula $\hat{u}\left(x\right)=\sin\left(\pi x\right)$
\end_inset

 obeys the boundary conditions.
 Compute
\begin_inset Formula 
\[
R\left(\hat{u}\right)=-\hat{u}''-\lambda e^{\hat{u}}
\]

\end_inset


\begin_inset Formula 
\[
=\pi^{2}\hat{u}-\lambda e^{\hat{u}}
\]

\end_inset

and then we've found a problem for which our solution is exact:
\begin_inset Formula 
\[
-u''-\lambda e^{u}-\pi^{2}\sin\left(\pi x\right)+\lambda e^{\sin\left(\pi x\right)}=0
\]

\end_inset


\begin_inset Formula 
\[
u\left(0\right)=u\left(1\right)=0.
\]

\end_inset


\end_layout

\begin_layout Subsection
Weak form
\end_layout

\begin_layout Standard
Derivation of the Galerkin weak form proceeds as usual, resulting in
\begin_inset Formula 
\[
\int_{0}^{1}v'u'-v\lambda e^{u}-vR\left(\hat{u}\right)\, dx=0\;\;\;\forall v\in H_{0}^{1}.
\]

\end_inset


\end_layout

\begin_layout Standard
We'll look at two different nonlinear solvers applied to the Bratu problem:
 fixed-point iteration and Newton's method.
 
\end_layout

\begin_layout Section
Solution by fixed-point iteration
\end_layout

\begin_layout Standard
The simplest approach to solving Bratu's problem is fixed-point iteration
 on the sequence of linear problems
\end_layout

\begin_layout Standard
\begin_inset Formula 
\[
-\nabla^{2}u_{n}-\lambda e^{u_{n-1}}=R\left(\hat{u}\right)\;\;\;\mbox{in }\Omega
\]

\end_inset


\begin_inset Formula 
\[
u_{n}=0\;\;\;\mbox{on }\Gamma
\]

\end_inset

in which we have replaced the nonlinear term 
\begin_inset Formula $\lambda e^{u}$
\end_inset

 by its value at the previous iterate, 
\begin_inset Formula $\lambda e^{u_{n-1}}$
\end_inset

.
 We iterate until
\begin_inset Formula 
\[
\left\Vert u_{n}-u_{n-1}\right\Vert \le\epsilon
\]

\end_inset

for some specified tolerance 
\begin_inset Formula $\epsilon$
\end_inset

 and norm 
\begin_inset Formula $\left\Vert \cdot\right\Vert $
\end_inset

.
 The new features of this problem are
\end_layout

\begin_layout Enumerate
We need a way to represent the previous solution 
\begin_inset Formula $u_{n-1}$
\end_inset

 as a symbolic object.
 There is an object type, 
\family typewriter
DiscreteFunction
\family default

\begin_inset Index idx
status open

\begin_layout Plain Layout
DiscreteFunction
\end_layout

\end_inset

, designed for exactly this purpose.
 
\end_layout

\begin_layout Enumerate
We won't store all the previous solutions: we only need one, which we'll
 call 
\family typewriter
uPrev
\family default
.
 After each step, we'll write the solution into 
\family typewriter
uPrev
\family default
, and 
\end_layout

\begin_layout Enumerate
In order to check convergence, we need to specify how to compute a norm.
 
\end_layout

\begin_layout Standard
The program for this algorithm can be found in the source file 
\series bold
FixedPointBratu1D.cpp
\series default
.
\end_layout

\begin_layout Standard
The lagged function
\family typewriter
 
\begin_inset Formula $u_{n-1}$
\end_inset


\family default
 is represented as a discrete function 
\family typewriter
uPrev
\family default
.
 A discrete function is based on a 
\family typewriter
DiscreteSpace
\begin_inset Index idx
status open

\begin_layout Plain Layout

\family typewriter
DiscreteSpace
\end_layout

\end_inset


\family default
 object, which encapsulates a mesh, one or more basis families, and a specificat
ion of the linear algebra representation to be used.
 
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

DiscreteSpace discSpace(mesh, basis, vecType);
\end_layout

\begin_layout Plain Layout

Expr uPrev = new DiscreteFunction(discSpace, 0.5);     
\end_layout

\begin_layout Plain Layout

Expr uCur = copyDiscreteFunction(uPrev);
\end_layout

\end_inset

The discrete function has been initialized to the constant value 
\begin_inset Formula $0.5$
\end_inset

.
 
\end_layout

\begin_layout Standard
With the discrete function ready, we can write the weak form and the linear
 problem.
 Notice the use of the lagged function 
\family typewriter
uPrev
\family default
 in the nonlinear term.
 
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

Expr eqn = Integral(interior, 
\end_layout

\begin_layout Plain Layout

	(grad*v)*(grad*v) - v*lambda*exp(uPrev) - v*R), quad);
\end_layout

\begin_layout Plain Layout

Expr bc = EssentialBC(bdry, v*u, quad);
\end_layout

\begin_layout Plain Layout

	
\end_layout

\begin_layout Plain Layout

LinearProblem prob(mesh, eqn, bc, v, u, vecType);
\end_layout

\end_inset

The square of the norm 
\begin_inset Formula $\left\Vert u_{n}-u_{n-1}\right\Vert $
\end_inset

 can be written in terms of a 
\family typewriter
Functional
\family default
.
 Note the use of the unknown function 
\family typewriter
u 
\family default
in the definition of the functional.
 A 
\family typewriter
FunctionalEvaluator
\family default
 object is created to evaluate the functional at the point 
\family typewriter
u=uCur.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

Expr normSqExpr = Integral(interior, pow(u-uPrev, 2.0), quad2);     
\end_layout

\begin_layout Plain Layout

Functional normSqFunc(mesh, normSqExpr, vecType);     
\end_layout

\begin_layout Plain Layout

FunctionalEvaluator normSqEval = normSqFunc.evaluator(u, uCur);
\end_layout

\end_inset


\end_layout

\begin_layout Standard
We now write the fixed-point iteration loop, which involves the norm check
 and the updating of the solution vector.
 
\end_layout

\begin_layout Standard
\begin_inset listings
lstparams "breaklines=true"
inline false
status open

\begin_layout Plain Layout

Out::root() << "Fixed-point iteration" << endl;     
\end_layout

\begin_layout Plain Layout

int maxIters = 20;     
\end_layout

\begin_layout Plain Layout

Expr soln ;     
\end_layout

\begin_layout Plain Layout

bool converged = false;
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

for (int i=0; i<maxIters; i++) {       
\end_layout

\begin_layout Plain Layout

	/* solve for the next iterate, to be written into uCur */       
\end_layout

\begin_layout Plain Layout

	prob.solve(linSolver, uCur);     
\end_layout

\begin_layout Plain Layout

	/* evaluate the norm of (uCur-uPrev) using 
\end_layout

\begin_layout Plain Layout

	 * the FunctionalEvaluator defined above */
\end_layout

\begin_layout Plain Layout

	double deltaU = sqrt(normSqEval.evaluate());       
\end_layout

\begin_layout Plain Layout

	Out::root() << "Iter=" << setw(3) << i << " ||Delta u||=" << setw(20) 
                  
\end_layout

\begin_layout Plain Layout

				<< deltaU << endl;    
\end_layout

\begin_layout Plain Layout

	/* check for convergence */  
\end_layout

\begin_layout Plain Layout

	if (deltaU < convTol) {         
\end_layout

\begin_layout Plain Layout

		soln = uCur;         
\end_layout

\begin_layout Plain Layout

		converged = true;         
\end_layout

\begin_layout Plain Layout

		break;       
\end_layout

\begin_layout Plain Layout

	}       
\end_layout

\begin_layout Plain Layout

	/* get the vector from the current discrete function */       
\end_layout

\begin_layout Plain Layout

	Vector<double> uVec = getDiscreteFunctionVector(uCur);
\end_layout

\begin_layout Plain Layout

	/* copy the vector into the previous discrete function */      
\end_layout

\begin_layout Plain Layout

	setDiscreteFunctionVector(uPrev, uVec);     
\end_layout

\begin_layout Plain Layout

}      
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

TEUCHOS_TEST_FOR_EXCEPTION(!converged, std::runtime_error, 
\end_layout

\begin_layout Plain Layout

	"Fixed point iteration did not converge after "        << maxIters << "
 iterations");     
\end_layout

\end_inset

If the algorithm has converged, the expression 
\family typewriter
soln
\family default
 now contains the approximate solution.
 It can be written to a file, or used in postprocessing calculations.
 
\end_layout

\begin_layout Section
Solution by Newton's method
\end_layout

\begin_layout Standard
When it works, fixed-point iteration converges linearly.
 Newton's method can converge quadratically.
\end_layout

\begin_layout Standard
In Newton's method for solving a nonlinear equation 
\begin_inset Formula $F(u)=0$
\end_inset

, we linearize the problem about an estimated solution 
\begin_inset Formula $u_{n-1}$
\end_inset

,
\begin_inset Formula 
\[
F(u_{n-1})+\left.\pd{F}{u}\right|_{u_{n-1}}\left(u_{n}-u_{n-1}\right)=0.
\]

\end_inset

This linear equation is solved for 
\begin_inset Formula $w=\left(u_{n}-u_{n-1}\right)$
\end_inset

, and then 
\begin_inset Formula $u_{n}=u_{n-1}+w$
\end_inset

 is the next estimate for the solution.
 Given certain conditions on the Jacobian 
\begin_inset Formula $\pd{F}{u}$
\end_inset

 and an initial guess sufficiently close to the solution, the algorithm
 will converge quadratically.
 Without a good initial guess, the method can converge slowly or not at
 all.
 High-quality nonlinear solvers will have a method for improving global
 convergence.
 One class of globalization methods, the 
\emph on
line search
\emph default
 methods, limit the size of the step, 
\emph on
i.e.
\emph default
, they update the solution estimate by
\begin_inset Formula 
\[
u_{n}=u_{n-1}+\alpha w
\]

\end_inset

for some 
\begin_inset Formula $\alpha\in(0,1]$
\end_inset

 chosen to ensure improvement in the solution.
 Refer to a text on nonlinear solvers (
\emph on
e.g.

\emph default
 Dennis and Schnabel, or Kelley) for information on globalization methods.
 
\end_layout

\begin_layout Standard
Our example problem is to solve
\begin_inset Formula 
\[
F(u)=-\nabla^{2}u-\lambda e^{u}-R\left(\hat{u}\right)=0.
\]

\end_inset

The derivative 
\begin_inset Formula $\pd{F}{u}$
\end_inset

 is the Frechet derivative, computed implicitly through the Gateaux differential
\begin_inset Formula 
\[
d_{w}F=\pd{F}{u}w.
\]

\end_inset

Note that the Gateaux differential is exactly what appears in the equation
 for the Newton step, so we can write the linearized problem as
\begin_inset Formula 
\[
F(u_{n-1})+d_{w}F(u_{n-1})=0.
\]

\end_inset

The Gateaux differential is defined by
\begin_inset Formula 
\[
d_{w}F(u_{n-1})=\lim_{\epsilon\to0}\frac{F(u_{n-1}+\epsilon w)-F(u_{n-1})}{\epsilon}
\]

\end_inset

from which the usual rules of calculus can be derived.
 For our example problem, we find
\end_layout

\begin_layout Standard
\begin_inset Formula 
\[
d_{w}F(u_{n-1})=-\lambda e^{u_{n-1}}w-\nabla^{2}w.
\]

\end_inset

Therefore the linearized equation for the Newton step 
\begin_inset Formula $w$
\end_inset

 is
\begin_inset Formula 
\[
\left[-\nabla^{2}u_{n-1}-\lambda e^{u_{n-1}}\right]+\left[-\nabla^{2}w-\lambda e^{u_{n-1}}w\right]-R\left(\hat{u}\right)=0.
\]

\end_inset

The linearized boundary conditions are 
\begin_inset Formula 
\[
u_{n-1}+w=0.
\]

\end_inset

While we can do linearization by hand, it is difficult and error-prone for
 complicated problems.
 Sundance has a built-in automated differentiation capability so that linearized
 equations can be derived automatically from a symbolic specification of
 the nonlinear equations.
 We will show examples of Newton's method with hand-coded linearized equations
 and with automated linearization.
\end_layout

\begin_layout Subsection
Newton's method with hand-coded derivatives
\end_layout

\begin_layout Standard
This example is found in the source file 
\series bold
HandLinearizedNewtonBratu1D.cpp
\series default
.
 
\end_layout

\begin_layout Standard
We set up a 
\family typewriter
LinearProblem
\family default
 object for the linearized equations.
 The unknown function is the step 
\begin_inset Formula $w$
\end_inset

.
 The previous iterate 
\begin_inset Formula $u_{n}$
\end_inset

 is represented by a 
\family typewriter
DiscreteFunction
\family default
.
 We do Newton iteration without line search.
 The convergence test simply uses the norm of the step vector
\emph on
 
\begin_inset Formula $\mathbf{w}$
\end_inset

, 
\emph default
which is
\emph on
 not
\emph default
 the same as the norm of the discrete function 
\begin_inset Formula $w$
\end_inset

 that uses that vector, but the difference is unimportant unless the mesh
 is significantly non-uniform.
 The method is considered converged when 
\begin_inset Formula $\left\Vert \mathbf{w}\right\Vert _{2}$
\end_inset

 goes below a specified tolerance 
\family typewriter
convTol
\family default
.
 
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

DiscreteSpace discSpace(mesh, basis, vecType);     
\end_layout

\begin_layout Plain Layout

Expr uPrev = new DiscreteFunction(discSpace, 0.5);     
\end_layout

\begin_layout Plain Layout

Expr stepVal = copyDiscreteFunction(uPrev);
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

Expr eqn = Integral(interior, (grad*v)*(grad*w) + (grad*v)*(grad*uPrev)
          
\end_layout

\begin_layout Plain Layout

- 	v*lambda*exp(uPrev)*(1.0+w) - v*R, quad4);
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

Expr h = new CellDiameterExpr();     
\end_layout

\begin_layout Plain Layout

Expr bc = EssentialBC(left+right, v*(uPrev+w)/h, quad2); 
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

LinearProblem prob(mesh, eqn, bc, v, w, vecType);
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

LinearSolver<double> linSolver = LinearSolverBuilder::createSolver("amesos.xml");
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

Out::root() << "Newton iteration" << endl;     
\end_layout

\begin_layout Plain Layout

int maxIters = 20;     
\end_layout

\begin_layout Plain Layout

Expr soln ;     
\end_layout

\begin_layout Plain Layout

bool converged = false;
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

for (int i=0; i<maxIters; i++) {
\end_layout

\begin_layout Plain Layout

    /* solve for the Newton step */       
\end_layout

\begin_layout Plain Layout

	prob.solve(linSolver, stepVal);    
\end_layout

\begin_layout Plain Layout

	/* check the norm of the step vector */  
\end_layout

\begin_layout Plain Layout

	Vector<double> stepVec = getDiscreteFunctionVector(stepVal);       
\end_layout

\begin_layout Plain Layout

	double deltaU = stepVec.norm2();       
\end_layout

\begin_layout Plain Layout

	Out::root() << "Iter=" << setw(3) << i << " ||Delta u||=" << setw(20) <<
 deltaU << endl;  
\end_layout

\begin_layout Plain Layout

	/* update the solution */
\end_layout

\begin_layout Plain Layout

	addVecToDiscreteFunction(uPrev, stepVec);       
\end_layout

\begin_layout Plain Layout

	/* if converged, break */
\end_layout

\begin_layout Plain Layout

	if (deltaU < convTol) {         
\end_layout

\begin_layout Plain Layout

		soln = uPrev;         
\end_layout

\begin_layout Plain Layout

		converged = true;         
\end_layout

\begin_layout Plain Layout

		break;       
\end_layout

\begin_layout Plain Layout

	}     
\end_layout

\begin_layout Plain Layout

}      
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

TEUCHOS_TEST_FOR_EXCEPTION(!converged, std::runtime_error,        
\end_layout

\begin_layout Plain Layout

	"Newton iteration did not converge after " << maxIters << " iterations");
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Newton's method is done.
 The remainder of the code is for output and postprocessing.
\end_layout

\begin_layout Subsection
Newton's method with automated derivatives
\begin_inset CommandInset label
LatexCommand label
name "par:Newton's-method-with"

\end_inset


\end_layout

\begin_layout Standard
One of the most useful features of Sundance is its built-in automatic differenti
ation capability.
 You can write a nonlinear PDE as a Sundance 
\family typewriter
Expr
\family default
, and Sundance will do the Newton linearization for you.
 
\end_layout

\begin_layout Standard
We start by writing the fully nonlinear weak form for the unknown 
\begin_inset Formula $u$
\end_inset

,
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

Expr eqn = Integral(interior, (grad*u)*(grad*v) - v*lambda*exp(u) - v*R,
 quad);
\end_layout

\end_inset

Notice that the nonlinear part is not 
\begin_inset Quotes eld
\end_inset

lagged.
\begin_inset Quotes erd
\end_inset

 Write boundary conditions as well,
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

Expr bc = EssentialBC(bdry, v*u/h, quad); 
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Now we use these to construct a 
\family typewriter
NonlinearProblem
\family default
 object, 
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

NonlinearProblem prob(mesh, eqn, bc, v, u, uPrev, vecType);
\end_layout

\end_inset

As in the previous examples, 
\family typewriter
uPrev
\family default
 is a discrete function containing the initial guess.
 Unlike previous examples, 
\family typewriter
uPrev
\family default
 does not appear in the weak form; it is given as an argument to the nonlinear
 problem constructor.
\end_layout

\begin_layout Standard
Now we can write the Newton loop.
 We obtain the discrete Jacobian 
\begin_inset Formula $J$
\end_inset

 as a 
\family typewriter
LinearOperator
\family default
 and the discrete residual 
\begin_inset Formula $F$
\end_inset

 as a Playa 
\family typewriter
Vector
\family default
 through a member function of the 
\family typewriter
NonlinearProblem
\family default
.
 We then solve the equation 
\begin_inset Formula $Jw=-F(u_{n-1})$
\end_inset

 for the step vector 
\begin_inset Formula $w$
\end_inset

.
 The convergence check and update to 
\family typewriter
uPrev
\family default
 are done as before.
 
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

Out::root() << "Newton iteration" << endl;     
\end_layout

\begin_layout Plain Layout

int maxIters = 20;     
\end_layout

\begin_layout Plain Layout

Expr soln ;     
\end_layout

\begin_layout Plain Layout

bool converged = false;
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

LinearOperator<double> J = prob.allocateJacobian();     
\end_layout

\begin_layout Plain Layout

Vector<double> residVec = J.range().createMember();     
\end_layout

\begin_layout Plain Layout

Vector<double> stepVec;
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

for (int i=0; i<maxIters; i++) {
\end_layout

\begin_layout Plain Layout

	prob.setInitialGuess(uPrev);       
\end_layout

\begin_layout Plain Layout

	prob.computeJacobianAndFunction(J, residVec);              
\end_layout

\begin_layout Plain Layout

	linSolver.solve(J, -1.0*residVec, stepVec);              
\end_layout

\begin_layout Plain Layout

	double deltaU = stepVec.norm2();       
\end_layout

\begin_layout Plain Layout

	Out::root() << "Iter=" << setw(3) << i << " ||Delta u||=" << setw(20) <<
 deltaU << endl;  
\end_layout

\begin_layout Plain Layout

    addVecToDiscreteFunction(uPrev, stepVec);       
\end_layout

\begin_layout Plain Layout

	if (deltaU < convTol) {         
\end_layout

\begin_layout Plain Layout

		soln = uPrev;         
\end_layout

\begin_layout Plain Layout

		converged = true;         
\end_layout

\begin_layout Plain Layout

		break;       
\end_layout

\begin_layout Plain Layout

	}     
\end_layout

\begin_layout Plain Layout

}      
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

TEUCHOS_TEST_FOR_EXCEPTION(!converged, std::runtime_error,        
\end_layout

\begin_layout Plain Layout

	"Newton iteration did not converge after " << maxIters << " iterations");
\end_layout

\end_inset


\end_layout

\begin_layout Subsection
Black-box Newton-Armijo method with automated derivatives
\end_layout

\begin_layout Standard
Finally, we show the most streamlined method for setting up and solving
 a nonlinear problem, found in the source file 
\series bold
FullyAutomatedNewtonBratu1D.cpp.
 
\series default
The example uses a packaged nonlinear solver.
 Here's the meat.
 Simply write the nonlinear equation in boundary conditions in weak form,
 provide a discrete function for the initial guess, create a nonlinear problem,
 and hand of to a Playa 
\family typewriter
NonlinearSolver
\family default
 for solution.
 Playa currently supports the NOX nonlinear solvers as well as its own native
 nonlinear solvers.
 In this example Playa's Newton-Armijo solver is used, with parameters set
 in the file 
\family typewriter
playa-newton-amesos.xml.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

DiscreteSpace discSpace(mesh, basis, vecType);     
\end_layout

\begin_layout Plain Layout

Expr uPrev = new DiscreteFunction(discSpace, 0.5);
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

Expr eqn = Integral(interior, (grad*v)*(grad*u) - v*lambda*exp(u) - v*R,
 quad4);
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

Expr h = new CellDiameterExpr();     
\end_layout

\begin_layout Plain Layout

Expr bc = EssentialBC(left+right, v*u/h, quad2); 
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

NonlinearProblem prob(mesh, eqn, bc, v, u, uPrev, vecType);
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

NonlinearSolver<double> solver = NonlinearSolverBuilder::createSolver("playa-new
ton-amesos.xml");
\end_layout

\begin_layout Plain Layout

    
\end_layout

\begin_layout Plain Layout

Out::root() << "Newton solve" << endl;
\end_layout

\begin_layout Plain Layout

SolverState<double> state = prob.solve(solver);          
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

TEUCHOS_TEST_FOR_EXCEPTION(state.finalState() != SolveConverged,       std::runti
me_error,       
\end_layout

\begin_layout Plain Layout

	"Nonlinear solve failed to converge: message=" << state.finalMsg());
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Unless your problem needs special handling, the fully automated approach
 is usually the most efficient and robust against user error.
\end_layout

\begin_layout Section
Continuation on a parameter
\end_layout

\begin_layout Standard
Bratu's problem becomes more strongly nonlinear as the parameter 
\begin_inset Formula $\left|\lambda\right|$
\end_inset

 increases.
 A reasonable strategy in such problems, called 
\emph on
continuation
\emph default
, is to begin with a parameter setting for which the problem is exactly
 or nearly linear, then solve a sequence of problems in which the parameter
 is adjusted systematically for each new solve.
 This can be helpful in problems where the nonlinear solver has a small
 radius of convergence.
 
\end_layout

\begin_layout Standard
Source code is in 
\series bold
ContinuationBratu1D.cpp
\series default
.
\end_layout

\begin_layout Standard
The key is to write the adjustable parameter as a 
\family typewriter
Parameter
\family default
 object rather than a double.
 The parameter value can be modified externally by calling the 
\family typewriter
setParameterValue
\family default
 member function (as shown in the loop below) and the change in the parameter
 is automatically reflected in the shallow copies of the parameter stored
 within a problem's expressions.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

Expr lambda = new Sundance::Parameter(0.0);
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The continuation loop is shown below; in each iteration, a new value of
 
\begin_inset Formula $\lambda$
\end_inset

 is chosen, and then a nonlinear solve is carried out at that value.
 The result (stored in the discrete function 
\family typewriter
uPrev
\family default
) is then used as the initial guess for the next continuation step.
 
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

for (int n=0; n<nSteps; n++) {
\end_layout

\begin_layout Plain Layout

	double lambdaVal = n*lambdaMax/(nSteps-1.0);       
\end_layout

\begin_layout Plain Layout

	/* update the value of the parameter */       
\end_layout

\begin_layout Plain Layout

	lambda.setParameterValue(lambdaVal);       
\end_layout

\begin_layout Plain Layout

	Out::root() << "continuation step n=" << n  << " of " << nSteps << ", lambda="
 << lambdaVal << endl;
\end_layout

\begin_layout Plain Layout

	SolverState<double> state = prob.solve(solver);            
\end_layout

\begin_layout Plain Layout

	TEUCHOS_TEST_FOR_EXCEPTION(state.finalState() != SolveConverged,       
  std::runtime_error,         
\end_layout

\begin_layout Plain Layout

		"Nonlinear solve failed to converge: message=" << state.finalMsg());
\end_layout

\begin_layout Plain Layout

	Expr soln = uPrev; 
\end_layout

\begin_layout Plain Layout

	/* === Solution output code omitted === */   
\end_layout

\begin_layout Plain Layout

} 
\end_layout

\end_inset


\end_layout

\begin_layout Standard
This example shows only the simplest continuation method: increasing the
 parameter in uniform steps.
 More sophisticated strategies are possible.
 
\end_layout

\begin_layout Section
Exercises
\end_layout

\begin_layout Enumerate
Write a solver for Bratu's equation with no forcing (
\begin_inset Formula $f=0$
\end_inset

 ) on the unit square with boundary conditions 
\begin_inset Formula $u=0$
\end_inset

.
 Use continuation to explore the behavior as 
\begin_inset Formula $\lambda$
\end_inset

 is increased from zero.
 Over what range of parameters are you able to find a solution?
\end_layout

\begin_layout Enumerate
The continuation example above was built around the fully-automated Newton's
 method.
 However, continuation strategies could be used with any of the other approaches
 considered here, such as fixed-point methods or a hand-coded Newton's method.
 Modify one of those examples to implement a continuation strategy.
 
\end_layout

\begin_layout Enumerate
Consider the steady-state radiation diffusion equation,
\begin_inset Formula 
\[
\nabla^{2}\left(u^{4}\right)=0
\]

\end_inset

on the unit square with boundary conditions
\begin_inset Formula 
\[
u=\left(1+\sin\left(\pi x\right)\right)^{1/4}\;\;\;\;\mbox{along the line }y=1
\]

\end_inset


\begin_inset Formula 
\[
u=1\;\;\mbox{elsewhere on }\Gamma.
\]

\end_inset


\end_layout

\begin_deeper
\begin_layout Enumerate
Derive a weak form of the problem
\end_layout

\begin_layout Enumerate
Derive a linearized weak form for the Newton step 
\begin_inset Formula $w=u_{n}-u_{n-1}$
\end_inset

.
\end_layout

\begin_layout Enumerate
Suppose you were to use an initial guess 
\begin_inset Formula $u_{0}=0$
\end_inset

 in Newton's method.
 What would happen, and why? 
\end_layout

\begin_layout Enumerate
Write a program to set up and solve this problem using Newton's method.
\end_layout

\end_deeper
\end_body
\end_document
