linear stretching of a function

Asked by Bento

Hi,

I am working on a problem, where I solve a function f on some one-dimensional mesh and then have to scale it to a different one-dimensional mesh, where I also want to turn the function around (so something like f(-x)). How do I achieve this?
I tried the following (which maybe makes the question more understandable):
take my function f on meshA of length LengthA
define an Expression that returns f(LengthA - LengthA/LengthB*x)
define a VariationalProblem that returns the stretched and mirrored function. Here is the code that I am using:

from dolfin import *

LengthA = 476.0
LengthB = 391.0
meshA = Interval(100, 0, LengthA)
meshB = Interval(100, 0, LengthB)
VA = FunctionSpace(meshA, "CG", 1)
VB = FunctionSpace(meshB, "CG", 1)
f = interpolate(Constant(0.0), VA)

class foo(Expression):
 def eval(self, values, x):
  values[0] = f(LengthA - LengthA/LengthB*x[0])

bar = foo()
T = TrialFunction(VB)
v = TestFunction(VB)
u = VariationalProblem(T*v*dx, bar*v*dx).solve()

But this throws an error: Unable to evaluate function at given point (not inside domain)...
The reason is a rounding error: on the rightmost point of meshB, we cannot evaluate f, because python returns
LengthA - LengthA/LengthB*LengthB returns something like -5.684e-14
which is outside the domain of f, as it is negative.

What is a good way to get around this problem? I have tried to take abs of the value inside f, but I would prefer to avoid this, because I am afraid this might reduce accuracy (in my computation I have to switch many times between the two meshes). And in similar situation I also left the domain on the other side. So abs does not seem to be a good solution.

thank you!

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Bento
Solved:
Last query:
Last reply:
Revision history for this message
Marie Rognes (meg-simula) said :
#1

On 06/28/11 15:01, Bento wrote:
> New question #163015 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/163015
>
> Hi,
>
> I am working on a problem, where I solve a function f on some one-dimensional mesh and then have to scale it to a different one-dimensional mesh, where I also want to turn the function around (so something like f(-x)). How do I achieve this?
> I tried the following (which maybe makes the question more understandable):
> take my function f on meshA of length LengthA
> define an Expression that returns f(LengthA - LengthA/LengthB*x)
> define a VariationalProblem that returns the stretched and mirrored function. Here is the code that I am using:
>
> from dolfin import *
>
> LengthA = 476.0
> LengthB = 391.0
> meshA = Interval(100, 0, LengthA)
> meshB = Interval(100, 0, LengthB)
> VA = FunctionSpace(meshA, "CG", 1)
> VB = FunctionSpace(meshB, "CG", 1)
> f = interpolate(Constant(0.0), VA)
>
> class foo(Expression):
> def eval(self, values, x):
> values[0] = f(LengthA - LengthA/LengthB*x[0])
>
> bar = foo()
> T = TrialFunction(VB)
> v = TestFunction(VB)
> u = VariationalProblem(T*v*dx, bar*v*dx).solve()
>
> But this throws an error: Unable to evaluate function at given point (not inside domain)...
> The reason is a rounding error: on the rightmost point of meshB, we cannot evaluate f, because python returns
> LengthA - LengthA/LengthB*LengthB returns something like -5.684e-14
> which is outside the domain of f, as it is negative.
>
> What is a good way to get around this problem? I have tried to take abs of the value inside f, but I would prefer to avoid this, because I am afraid this might reduce accuracy (in my computation I have to switch many times between the two meshes). And in similar situation I also left the domain on the other side. So abs does not seem to be a good solution.
>

Try setting the parameter "allow_extrapolation" initially:

     parameters["allow_extrapolation"] = True

--
Marie

Revision history for this message
Bento (tbarmeier-deactivatedaccount) said :
#2

Hi Marie,

Thank you. I was already thinking about this (and it indeed solves the problem). But I was not sure how much iinaccuracy this might introduce. I need to switch around maybe tenthousands of times, so I want to keep this mesh-switching as error-free as possible.

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

On 06/28/11 16:06, Bento wrote:
> Question #163015 on DOLFIN changed:
> https://answers.launchpad.net/dolfin/+question/163015
>
> Status: Answered => Open
>
> Bento is still having a problem:
> Hi Marie,
>
> Thank you. I was already thinking about this (and it indeed solves the
> problem). But I was not sure how much iinaccuracy this might introduce.
> I need to switch around maybe tenthousands of times, so I want to keep
> this mesh-switching as error-free as possible.
>

Allowing extrapolation enables function evaluation at a point that is
considered "outside the mesh" by evaluating the function at the closest
point "within the mesh" instead. I imagine that this actually is what
you want to evaluate.

--
Marie

Revision history for this message
Bento (tbarmeier-deactivatedaccount) said :
#4

If it works like that, then it does what I want. I assumed it does some regression.
Thank you!