This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Expressions for AdaptiveLinearVariationalSolver

0 votes

Hi!
In the demo case ´demo_auto-adaptive_poisson.py´ the expressions use
the keyword degree=1:

g = Expression("sin(5*x[0])", degree=1)

Why do I have to include this keyword?
Without degree=1 the refinement continuous forever for me.

Second question:
I want to use a spline function (or any function) to give the values in my expression so I want something like

class MyExpression(Expression):
def __init__(self, spline_function):
    self.spline_function = spline_function   
    return None
def eval(self,values, x):
    values[0] = self.spline_function(x[0])
    return None

How can I include degree=1 in MyExpression so that I can use it in variational forms for
AdaptiveLinearVariationalSolver?
I suppose that I have initiate the base class in some way but I don't know how.

asked Oct 7, 2014 by Stefan_Jakobsson FEniCS Novice (810 points)

1 Answer

+2 votes
 
Best answer

Hi, the following snippet shows how to set the degree with classes derived from Expression.

from dolfin import *

class Foo(Expression):
    def eval(self, values, x):
        values[0] = sin(x[0])

mesh = UnitTriangleMesh()

# We want to compute integral sin(x) over triangle [0, 0]-[1, 0]-[0, 1]
exact = 1 - sin(1)

print 'w/out degre ->', abs(exact - assemble(Foo()*dx(domain=mesh)))
for deg in range(1, 6):
    print deg, '->', abs(exact - assemble(Foo(degree=deg)*dx(domain=mesh)))

Degree helps the form compiler decide what the suitable quadrature is for a given form.

answered Oct 8, 2014 by MiroK FEniCS Expert (80,920 points)
selected Oct 8, 2014 by Stefan_Jakobsson

Thanks!
Now the adaptation works.

...