Solving nonlinear problem directly

Asked by Gustav Kettil

I'm trying to solve u'^2-u=0. u(0)=1, u(2)=4 with the built in solver but it does not converge. I have tried to use u_.vector()[:] = 1.0 as was suggested in another question but I'm not sure I put it in the right place. What to do?

from dolfin import*
mesh =Interval(50,0,2)
V = FunctionSpace(mesh, 'Lagrange', 1)

#boundary condition init.
u0=Constant(1)
def u0_boundary(x):
   return x[0]<DOLFIN_EPS

bc0 = DirichletBC(V, u0, u0_boundary)

u2=Constant(4)
def u2_boundary(x):
   return abs(x[0]-2)<DOLFIN_EPS

bc2 = DirichletBC(V, u2, u2_boundary)

bcs=[bc0, bc2]

# Define variational problem
u = TrialFunction(V)

v = TestFunction(V)
u_ = Function(V) # the most recently computed solution

F = inner(grad(u)*grad(u)-u, v)*dx
F = action(F, u_)
J = derivative(F, u_, u)

u_.vector()[:] = 1.0

problem = NonlinearVariationalProblem(F, u_, bc2, J)
solver = NonlinearVariationalSolver(problem)
solver.solve()

plot(u_,title="u")

interactive()

Question information

Language:
English Edit question
Status:
Answered
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Martin Sandve Alnæs (martinal) said :
#1

On 15 March 2012 22:06, Gustav Kettil
<email address hidden> wrote:
> New question #190805 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/190805
>
> I'm trying to solve u'^2-u=0. u(0)=1, u(2)=4 with the built in solver but it does not converge. I have tried to use u_.vector()[:] = 1.0 as was suggested in another question but I'm not sure I put it in the right place. What to do?
>
> from dolfin import*
> mesh =Interval(50,0,2)
> V = FunctionSpace(mesh, 'Lagrange', 1)
>
> #boundary condition init.
> u0=Constant(1)
> def u0_boundary(x):
>   return x[0]<DOLFIN_EPS
>
> bc0 = DirichletBC(V, u0, u0_boundary)
>
> u2=Constant(4)
> def u2_boundary(x):
>   return abs(x[0]-2)<DOLFIN_EPS
>
> bc2 = DirichletBC(V, u2, u2_boundary)
>
> bcs=[bc0, bc2]
>
> # Define variational problem
> u  = TrialFunction(V)
>
> v  = TestFunction(V)
> u_ = Function(V)      # the most recently computed solution
>
> F  = inner(grad(u)*grad(u)-u, v)*dx
> F  = action(F, u_)

You must define the nonlinear residual form in terms of the Function:

F  = inner(grad(u_)*grad(u_)-u_, v)*dx

The rest looks fine at a glance.

Martin

> J  = derivative(F, u_, u)
>
> u_.vector()[:] = 1.0
>
> problem = NonlinearVariationalProblem(F, u_, bc2, J)
> solver  = NonlinearVariationalSolver(problem)
> solver.solve()
>
> plot(u_,title="u")
>
> interactive()
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.

Can you help with this problem?

Provide an answer of your own, or ask Gustav Kettil for more information if necessary.

To post a message you must log in.