# PETScSNESSolver example?

Asked by Paul Constantine on 2013-02-05

Are there any examples of using the PETScSNESSolver? In particular, how to set the initial guess?

## Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Paul Constantine
Solved:
2013-02-06
Last query:
2013-02-06
2013-02-06
 Patrick Farrell (pefarrell) said on 2013-02-06: #1

Hi,

Take a look at test/unit/nls/python/PETScSNESSolver.py .

The initial guess is supplied in the same variable as the output -- e.g. if you call

solve(F == 0, u, ...)

the value of u before the call to solve is the initial guess, and the value of u after the solve is the solution of the nonlinear problem.

 Paul Constantine (paul-g-constantine) said on 2013-02-06: #2

Turns out the Cahn-Hilliard had most of what I was looking for -- how to set up "initial conditions" and how to set up a nonlinear problem class. Otherwise the syntax looks something like:

nlp = MyNonlinearProblem(a, L, bcs)
solver = PETScSNESSolver()
solver.parameters["maximum_iterations"]=20

# "ls" is line search, "tr" is trust region method
solver.parameters["method"]="ls"
solver.solve(nlp,u.vector())

 Garth Wells (garth-wells) said on 2013-02-06: #3

On 6 February 2013 09:56, Paul Constantine
> Question #221146 on DOLFIN changed:
>
>
> Paul Constantine confirmed that the question is solved:
> Turns out the Cahn-Hilliard had most of what I was looking for

Yes, it's the best example!

Garth

> -- how to
> set up "initial conditions" and how to set up a nonlinear problem class.
> Otherwise the syntax looks something like:
>
> nlp = MyNonlinearProblem(a, L, bcs)
> solver = PETScSNESSolver()
> solver.parameters["maximum_iterations"]=20
>
> # "ls" is line search, "tr" is trust region method
> solver.parameters["method"]="ls"
> solver.solve(nlp,u.vector())
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.

 Anders Logg (logg) said on 2013-02-06: #4

On Wed, Feb 06, 2013 at 02:55:56PM -0000, Garth Wells wrote:
> Question #221146 on DOLFIN changed:
> Garth Wells posted a new comment:
> On 6 February 2013 09:56, Paul Constantine

>> Question #221146 on DOLFIN changed:
>>
>>
>> Paul Constantine confirmed that the question is solved:
>> Turns out the Cahn-Hilliard had most of what I was looking for
> Yes, it's the best example!

So why don't you make some more! ;-)

--
Anders

>> -- how to
>> set up "initial conditions" and how to set up a nonlinear problem class.
>> Otherwise the syntax looks something like:
>>
>> nlp = MyNonlinearProblem(a, L, bcs)
>> solver = PETScSNESSolver()
>> solver.parameters["maximum_iterations"]=20
>>
>> # "ls" is line search, "tr" is trust region method
>> solver.parameters["method"]="ls"
>> solver.solve(nlp,u.vector())
>>

 Garth Wells (garth-wells) said on 2013-02-06: #5

On 6 February 2013 20:45, Anders Logg
> Question #221146 on DOLFIN changed:
>
> Anders Logg posted a new comment:
> On Wed, Feb 06, 2013 at 02:55:56PM -0000, Garth Wells wrote:
>> Question #221146 on DOLFIN changed:
>> Garth Wells posted a new comment:
>> On 6 February 2013 09:56, Paul Constantine
>
>
>>> Question #221146 on DOLFIN changed:
>>>
>>>
>>> Paul Constantine confirmed that the question is solved:
>>> Turns out the Cahn-Hilliard had most of what I was looking for
>> Yes, it's the best example!
>
> So why don't you make some more! ;-)
>

Because the C-H demo already has it all . . .

Garth

> --
> Anders
>
>
>>> -- how to
>>> set up "initial conditions" and how to set up a nonlinear problem class.
>>> Otherwise the syntax looks something like:
>>>
>>> nlp = MyNonlinearProblem(a, L, bcs)
>>> solver = PETScSNESSolver()
>>> solver.parameters["maximum_iterations"]=20