changing robin boundary in form

Asked by Chaffra Affouda

I have the following form with a robin boundary which depends on the Constants bias and resistance coefficients.

F = inner(mun*n*grad(un), grad(vn))*dx + (un-bias)/resistance*vn*ds(1)
J = derivative(F, un, dn)
F = Form(F) #compile the forms
J = Form(J)

To solve I do something like

solver = NewtonSolver()
problem = NonlinearProblem(F,J, exterior_facet_domains=exterior_facet_domains) #assemble

 solver.solve(problem,un.vector())

My problem is that I'd like to solve this for several biases (changing the bias coefficeint inside the forms F, and J). For DirichletBC it is easy because I can create new DirichletBC's and pass them to the NonlinearProblem class without having to rebuild my forms (F, J). Is there a way to have a similar solution for a robin (or neumann for that matter) boundary, maybe using ufl replace?

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Chaffra Affouda
Solved:
Last query:
Last reply:
Revision history for this message
Johan Hake (johan-hake) said :
#1

You could try an expression which depends on some paramater. For us to help
you please proivde a runnable example.

Johan
On Nov 20, 2012 4:41 PM, "Chaffra Affouda" <
<email address hidden>> wrote:

> Question #214759 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214759
>
> Description changed to:
> I have the following form with a robin boundary which depends on the
> Constants bias and resistance coefficients.
>
> F = inner(mun*n*grad(un), grad(vn))*dx + (un-bias)/resistance*vn*ds(1)
> J = derivative(F, un, dn)
> F = Form(F) #compile the forms
> J = Form(J)
>
> To solve I do something like
>
> solver = NewtonSolver()
> problem = NonlinearProblem(F,J,
> exterior_facet_domains=exterior_facet_domains) #assemble
>
> solver.solve(problem,un.vector())
>
> My problem is that I'd like to solve this for several biases (changing
> the bias coefficeint inside the forms F, and J). For DirichletBC it is
> easy because I can create new DirichletBC's and pass them to the
> NonlinearProblem class without having to rebuild my forms (F, J). Is
> there a way to have a similar solution for a robin (or neumann for that
> matter) boundary, maybe using ufl replace?
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#2

I would just keep the form fixed and copy the result into other functions
after each solve. There are other ways, but that is the cleanest I think.

Martin
Den 21. nov. 2012 06:51 skrev "Johan Hake" <
<email address hidden>> følgende:

> Question #214759 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214759
>
> Status: Open => Answered
>
> Johan Hake proposed the following answer:
> You could try an expression which depends on some paramater. For us to help
> you please proivde a runnable example.
>
> Johan
> On Nov 20, 2012 4:41 PM, "Chaffra Affouda" <
> <email address hidden>> wrote:
>
> > Question #214759 on DOLFIN changed:
> > https://answers.launchpad.net/dolfin/+question/214759
> >
> > Description changed to:
> > I have the following form with a robin boundary which depends on the
> > Constants bias and resistance coefficients.
> >
> > F = inner(mun*n*grad(un), grad(vn))*dx + (un-bias)/resistance*vn*ds(1)
> > J = derivative(F, un, dn)
> > F = Form(F) #compile the forms
> > J = Form(J)
> >
> > To solve I do something like
> >
> > solver = NewtonSolver()
> > problem = NonlinearProblem(F,J,
> > exterior_facet_domains=exterior_facet_domains) #assemble
> >
> > solver.solve(problem,un.vector())
> >
> > My problem is that I'd like to solve this for several biases (changing
> > the bias coefficeint inside the forms F, and J). For DirichletBC it is
> > easy because I can create new DirichletBC's and pass them to the
> > NonlinearProblem class without having to rebuild my forms (F, J). Is
> > there a way to have a similar solution for a robin (or neumann for that
> > matter) boundary, maybe using ufl replace?
> >
> > --
> > You received this question notification because you are a member of
> > DOLFIN Team, which is an answer contact for DOLFIN.
> >
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>

Revision history for this message
Martin Sandve Alnæs (martinal) said :
#3

Sorry: Or rather copy new values into the coefficient function vector
before each solve.

Martin
Den 21. nov. 2012 07:30 skrev "Martin Sandve Alnæs" <email address hidden>
følgende:

> I would just keep the form fixed and copy the result into other functions
> after each solve. There are other ways, but that is the cleanest I think.
>
> Martin
> Den 21. nov. 2012 06:51 skrev "Johan Hake" <
> <email address hidden>> følgende:
>
>> Question #214759 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/214759
>>
>> Status: Open => Answered
>>
>> Johan Hake proposed the following answer:
>> You could try an expression which depends on some paramater. For us to
>> help
>> you please proivde a runnable example.
>>
>> Johan
>> On Nov 20, 2012 4:41 PM, "Chaffra Affouda" <
>> <email address hidden>> wrote:
>>
>> > Question #214759 on DOLFIN changed:
>> > https://answers.launchpad.net/dolfin/+question/214759
>> >
>> > Description changed to:
>> > I have the following form with a robin boundary which depends on the
>> > Constants bias and resistance coefficients.
>> >
>> > F = inner(mun*n*grad(un), grad(vn))*dx + (un-bias)/resistance*vn*ds(1)
>> > J = derivative(F, un, dn)
>> > F = Form(F) #compile the forms
>> > J = Form(J)
>> >
>> > To solve I do something like
>> >
>> > solver = NewtonSolver()
>> > problem = NonlinearProblem(F,J,
>> > exterior_facet_domains=exterior_facet_domains) #assemble
>> >
>> > solver.solve(problem,un.vector())
>> >
>> > My problem is that I'd like to solve this for several biases (changing
>> > the bias coefficeint inside the forms F, and J). For DirichletBC it is
>> > easy because I can create new DirichletBC's and pass them to the
>> > NonlinearProblem class without having to rebuild my forms (F, J). Is
>> > there a way to have a similar solution for a robin (or neumann for that
>> > matter) boundary, maybe using ufl replace?
>> >
>> > --
>> > You received this question notification because you are a member of
>> > DOLFIN Team, which is an answer contact for DOLFIN.
>> >
>>
>> --
>> You received this question notification because you are a member of
>> DOLFIN Team, which is an answer contact for DOLFIN.
>>
>

Revision history for this message
Chaffra Affouda (chaffra) said :
#4

Ok so is there an easy way to change a Constant? I do not really want to project the constant in order to change its coefficients.

Revision history for this message
Johan Hake (johan-hake) said :
#5

On 11/21/2012 04:36 PM, Chaffra Affouda wrote:
> Question #214759 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214759
>
> Status: Answered => Open
>
> Chaffra Affouda is still having a problem:
> Ok so is there an easy way to change a Constant? I do not really want
> to project the constant in order to change its coefficients.

You do not change a Constant. That is the sole purpose of using a Constant.

If you really mean a finite element Function you need to change its dofs
to update it. If it really just is a scalar you can use an Expression:

  bias = Expression("value", value = 1.0, element=FiniteElement("R",
mesh.ufl_cell(), 0))

  F = your form using bias
  for bias_value in bias_values:
     bias.value = bias_value
     # solve your problem

Johan

Revision history for this message
Marie Rognes (meg-simula) said :
#6

On 11/21/2012 06:41 PM, Johan Hake wrote:
> Question #214759 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214759
>
> Status: Open => Answered
>
> Johan Hake proposed the following answer:
> On 11/21/2012 04:36 PM, Chaffra Affouda wrote:
>> Question #214759 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/214759
>>
>> Status: Answered => Open
>>
>> Chaffra Affouda is still having a problem:
>> Ok so is there an easy way to change a Constant? I do not really want
>> to project the constant in order to change its coefficients.
> You do not change a Constant. That is the sole purpose of using a
> Constant.

Just FYI: you can change a Constant via assignment:

   a = Constant(1.0)
   print float(b)
   a.assign(2.0)
   print float(a)

--
Marie

> If you really mean a finite element Function you need to change its dofs
> to update it. If it really just is a scalar you can use an Expression:
>
> bias = Expression("value", value = 1.0, element=FiniteElement("R",
> mesh.ufl_cell(), 0))
>
> F = your form using bias
> for bias_value in bias_values:
> bias.value = bias_value
> # solve your problem
>
> Johan
>

Revision history for this message
Johan Hake (johan-hake) said :
#7

On 11/21/2012 06:45 PM, Marie Rognes wrote:
> Question #214759 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/214759
>
> Marie Rognes proposed the following answer:
> On 11/21/2012 06:41 PM, Johan Hake wrote:
>> Question #214759 on DOLFIN changed:
>> https://answers.launchpad.net/dolfin/+question/214759
>>
>> Status: Open => Answered
>>
>> Johan Hake proposed the following answer:
>> On 11/21/2012 04:36 PM, Chaffra Affouda wrote:
>>> Question #214759 on DOLFIN changed:
>>> https://answers.launchpad.net/dolfin/+question/214759
>>>
>>> Status: Answered => Open
>>>
>>> Chaffra Affouda is still having a problem:
>>> Ok so is there an easy way to change a Constant? I do not really want
>>> to project the constant in order to change its coefficients.
>> You do not change a Constant. That is the sole purpose of using a
>> Constant.
>
> Just FYI: you can change a Constant via assignment:
>
> a = Constant(1.0)
> print float(b)
> a.assign(2.0)
> print float(a)

Nice!

I did not know :P

Johan

> --
> Marie
>
>> If you really mean a finite element Function you need to change its dofs
>> to update it. If it really just is a scalar you can use an Expression:
>>
>> bias = Expression("value", value = 1.0, element=FiniteElement("R",
>> mesh.ufl_cell(), 0))
>>
>> F = your form using bias
>> for bias_value in bias_values:
>> bias.value = bias_value
>> # solve your problem
>>
>> Johan
>>
>

Revision history for this message
Chaffra Affouda (chaffra) said :
#8

great thanks!