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

Declaring expression using another expression or python function

+1 vote

I have a complicated vector analytic expression as a source and I would like to write some of its components separately. How can I declare and expression using one or more expressions? Could I use a python function/s to do it?

asked Apr 19, 2017 by alexmm FEniCS User (4,240 points)

1 Answer

+1 vote
 
Best answer

It is a very vague question, but if you manage to define two expressions

f = CustomExpression1(args)
g = CustomExpression2(args)

then putting everyting together is simply done with

F = as_vector([f, g])

Best regards

answered Apr 19, 2017 by nabarnaf FEniCS User (2,940 points)
selected Apr 20, 2017 by alexmm

Yes it was, let me be more specific:

I would like the following functionality:

someExpression = Expression('funct(x[0] + 20.0) + x[0] + 40.0', degree=1)

Where funct is either a function as shown or just another expression, written outside the line either as an expression or a python function or a c++ function.

For this you would need to subclass Expression so that is has your desired function as follows:

    class CustomExpression(Expression):
        def __init__(self, func, element):
            self.func = func
            self.element = element
        def eval(self, value,x):
            value[0] = func(x) + etc

and then create the expression with your new argument

    F = CustomExpression(func, element)

Best regards!

...