LinearVariationalSolver does not return a dolfin.Function when solving a refined problem

Asked by Simon Funke

Dear DOLFIN team.

I solve a refined problem created with adapt() using the LinearVariationalProblem class in Python.
Unfortunately, the solution of that problem is not a dolfin.Function object, but rather a dolfin.cpp.function.Function, which
is very restrictive regarding its operations (e.g. I can not multiply it with a scalar).

The following code demonstrates my problem:

====================== python code for the dolfin trunk ====================
from dolfin import *

mesh = UnitSquareMesh(2, 2)
V = FunctionSpace(mesh, "CG", 1)
u = TrialFunction(V)
v = TestFunction(V)
sol = Function(V)
s = interpolate(Constant(1.0), V)

a = u*v*dx
L = s*v*dx

problem = LinearVariationalProblem(a, L, sol)
solver = LinearVariationalSolver(problem)

solver.solve()

print isinstance(sol, dolfin.Function) # True
print isinstance(problem.solution(), dolfin.Function) # False, but that's ok,
# because we can use sol as our dolfin.Function

# But in this case we don't have a dolfin.Function, only a dolfin.cpp.function.Function:
mesh_fine = adapt(mesh)
prob_fine = adapt(problem, mesh_fine)
solver = LinearVariationalSolver(prob_fine)
solver.solve()

print isinstance(prob_fine.solution(), dolfin.Function) # False -- this needs to
# be a dolfin.Function to be usable
print isinstance(sol.child(), dolfin.Function) # False -- this needs to be a
# dolfin.Function to be usable

# For example I want to do somehthing like
print 2*prob_fine.solution()

==============================================================================

Thanks very much for your help,

Simon

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Simon Funke
Solved:
Last query:
Last reply:
Revision history for this message
Simon Funke (simon-funke) said :
#1

This a workaround that seems to work:

sol_fine = Function(prob_fine.solution().function_space(), prob_fine.solution().vector())

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

I have pushed some fixes based on your workaround. It fixes only:

  prob_fine.solution()

I was about to fix the:

  sol.child()

too, but read in:

  dolfin/swig/function/post.i

that the solution is kept a cpp.Function{Space} by purpose. Maybe Marie
can elaborate on why this was kept by purpose?

If it was to avoid copying, we should be able to fix it without copying.

Johan

On 11/29/2012 06:55 PM, Simon Funke wrote:
> New question #215572 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/215572
>
> Dear DOLFIN team.
>
> I solve a refined problem created with adapt() using the LinearVariationalProblem class in Python.
> Unfortunately, the solution of that problem is not a dolfin.Function object, but rather a dolfin.cpp.function.Function, which
> is very restrictive regarding its operations (e.g. I can not multiply it with a scalar).
>
> The following code demonstrates my problem:
>
> ====================== python code for the dolfin trunk ===================================
> from dolfin import *
>
> mesh = UnitSquareMesh(2, 2)
> V = FunctionSpace(mesh, "CG", 1)
> u = TrialFunction(V)
> v = TestFunction(V)
> sol = Function(V)
> s = interpolate(Constant(1.0), V)
>
> a = u*v*dx
> L = s*v*dx
>
> problem = LinearVariationalProblem(a, L, sol)
> solver = LinearVariationalSolver(problem)
>
> solver.solve()
>
> print isinstance(sol, dolfin.Function) # True
> print isinstance(problem.solution(), dolfin.Function) # False, but that's ok, because we can use sol as our dolfin.Function
>
> # But in this case we don't have a dolfin.Function, only a dolfin.cpp.function.Function:
> mesh_fine = adapt(mesh)
> prob_fine = adapt(problem, mesh_fine)
> solver = LinearVariationalSolver(prob_fine)
> solver.solve()
>
> print isinstance(prob_fine.solution(), dolfin.Function) # False -- this needs to be a dolfin.Function to be usable
> print isinstance(sol.child(), dolfin.Function) # False -- this needs to be a dolfin.Function to be usable
>
> # For example I want to do somehthing like
> print 2*prob_fine.solution()
>
> ==============================================================================
>
> Thanks very much for your help,
>
> Simon
>

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

Marie does definitely not remember...

Revision history for this message
Simon Funke (simon-funke) said :
#4

I think from the users perspective there is no need to copy.
However, for my purposes the current fix is sufficient - thanks very much for the effort.

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

Ok, cool.

I have some fixes for the rest in line for a push.

Johan
On Dec 1, 2012 3:41 PM, "Simon Funke" <email address hidden>
wrote:

> Question #215572 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/215572
>
> Simon Funke posted a new comment:
> I think from the users perspective there is no need to copy.
> However, for my purposes the current fix is sufficient - thanks very much
> for the effort.
>
> --
> You received this question notification because you are a member of
> DOLFIN Team, which is an answer contact for DOLFIN.
>