UFL

How to compute a pointwise vector product within an ufl-file

Asked by Norman Lang

i am really new in working with fenics and the packages around. So i have got a question about ufl. I want to generate an input Matrix for an optimal control problem.
So i am writing down my variational form and want to get the expression

                                    L = c*inner(1,v)*ds(1)

in my matrix (c is only an constant). For that i am assembling L to an Vector B via

                          assemble(B, L, NULL, & sub_domains, NULL);

Now i want to pointwise multiply this vector with another vector, called z.
I am writing my main programm in C++ and the pointwise multiplying doesn't make any problem.

My question is now. How could i get the pointwise multiplication into the ufl-file, such that, i don't have to do the multiplication after assembling my vector.

I tried to use the Coefficient class in ufl and later on i tried it with the Function class via

elem=FiniteElement("Lagrange",tetrahedron,1)

z = Coefficient(elem) or z = Function(elem).

With the Coefficient class the multiplication

                        L = z*c*inner(1,v)*ds(1)

does not create an error but the result isn't correct when i'm comparing it with the pointwise operation

                z.*B, where B is the assembled Vector from above

in MATLAB. So i'm sure i'm not computing a pointwise multiplication of two vectors.
Could you please help me with this problem. It would really help me.

Regards,

Norman

Question information

Language:
English Edit question
Status:
Solved
For:
UFL Edit question
Assignee:
No assignee Edit question
Solved by:
Norman Lang
Solved:
Last query:
Last reply:
Revision history for this message
Martin Sandve Alnæs (martinal) said :
#1

On 11 July 2011 15:25, Norman Lang <email address hidden> wrote:
> New question #164419 on UFL:
> https://answers.launchpad.net/ufl/+question/164419
>
>
>
> i am really new in working with fenics and the packages around. So i have got a question about ufl. I want to generate an input Matrix for an optimal control problem.
> So i am writing down my variational form and want to get the expression
>
>                                    L = c*inner(1,v)*ds(1)

inner(1, v) does not make any sense to me.
You need to specify more clearly what you're
doing with a small but complete code example.
If v is a scalar, it is the same as just v.
If v is not a scalar, it will trigger an error.

> in my matrix (c is only an constant). For that i am assembling L to an Vector B via
>
>                          assemble(B, L, NULL, & sub_domains, NULL);
>
> Now i want to pointwise multiply this vector with another vector, called z.
> I am writing my main programm in C++ and the pointwise multiplying doesn't make any problem.
>
> My question is now. How could i get the pointwise multiplication into the ufl-file, such that, i don't have to do the multiplication after assembling my vector.

Unless you can write what you want into the PDE, you cannot express it with UFL.

UFL is a language for variational forms, not for linear algebra
operations on the assembled global vectors and matrices.

> I tried to use the Coefficient class in ufl and later on i tried it with the Function class via
>
> elem=FiniteElement("Lagrange",tetrahedron,1)
>
> z = Coefficient(elem) or z = Function(elem).
>
> With the Coefficient class the multiplication
>
>                        L = z*c*inner(1,v)*ds(1)
>
> does not create an error but the result isn't correct when i'm comparing it with the pointwise operation
>
>                z.*B,        where B is the assembled Vector from above
>
> in MATLAB. So i'm sure i'm not computing a pointwise multiplication of two vectors.
> Could you please help me with this problem. It would really help me.
>
> Regards,
>
> Norman
>
>
> --
> You received this question notification because you are a member of UFL
> Team, which is an answer contact for UFL.
>
> _______________________________________________
> Mailing list: https://launchpad.net/~ufl
> Post to     : <email address hidden>
> Unsubscribe : https://launchpad.net/~ufl
> More help   : https://help.launchpad.net/ListHelp
>

Revision history for this message
Norman Lang (norman-lang) said :
#2

elem=FiniteElement("Lagrange",tetrahedron,1)
v = TestFunction(element)

The expression inner(1, v)*ds(1) should do the same like v*ds(1) i guess. With that expression i want to generate a vector of length n, where n is the number of grid nodes (because of use of Lagrange, 1) and the vector only has some constant entries on node places of the boundary one (ds(1)).
Later on this should be an input vector of an optimal control problem.

Now i want to create a vector which depends on my spatial coordinate z. More precise i want to generate a vector of the form

                              B(i)=exp(-50*z(i))*v(i)*ds(1).

The entries of vector should vary over the hight of my working piece.
In my C++ code i could realize that via a pointwise multiplication.

But i want to get that expression into my variational form to generate the right hand side of my system of linear equations which i want to solve with FEniCS.
Because of your answer

'Unless you can write what you want into the PDE, you cannot express it with UFL.

UFL is a language for variational forms, not for linear algebra
operations on the assembled global vectors and matrices.'

i think it isn't really possible. So i would be happy if some has got an other approach for this problem.

I hope one can understand my problem now and i want to thank for the previous answer.

Regards,

Norman

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

Hi Norman,

On 11 July 2011 17:51, Norman Lang <email address hidden> wrote:
> Question #164419 on UFL changed:
> https://answers.launchpad.net/ufl/+question/164419
>
>    Status: Answered => Open
>
> Norman Lang is still having a problem:
> elem=FiniteElement("Lagrange",tetrahedron,1)
> v = TestFunction(element)
>
> The expression inner(1, v)*ds(1)  should do the same like v*ds(1) i guess. With that expression i want to generate a vector of length n, where n is the number of grid nodes (because of use of Lagrange, 1) and the vector only has some constant entries on node places of the boundary one (ds(1)).
> Later on this should be an input vector of an optimal control problem.
>
> Now i want to create a vector which depends on my spatial coordinate z.
> More precise i want to generate a vector of the form
>
>                              B(i)=exp(-50*z(i))*v(i)*ds(1).

This is ambiguous. Can you write it in (pseudo-)LaTeX?
I interpret your line above as roughly:

V := span{ v_i(x) }
z \in V
z(x) := \sum_i z_i v_i(x)
B_i = exp(-50 z_i) ( \int_{d\Omega} v_i(x) dx )

Is that right? Then no, you can't express that in UFL.

> The entries of vector should vary over the hight of my working piece.
> In my C++ code i could realize that via a pointwise multiplication.
>
> But i want to get that expression into my variational form to generate the right hand side of my system of linear equations which i want to solve with FEniCS.

The variational forms you define in UFL will always be integrated, i.e.
expression*ds(1) means "the integral of (expression) over boundary 1".

> Because of your answer
>
> 'Unless you can write what you want into the PDE, you cannot express it
> with UFL.
>
> UFL is a language for variational forms, not for linear algebra
> operations on the assembled global vectors and matrices.'
>
> i think it isn't really possible. So i would be happy if some has got an
> other approach for this problem.

I think your original approach of doing pointwise multiplication
at the linear algebra level is what you have to do.
Unless you can formulate your problem differently.
I don't understand where your equations come from,
so I can't help any more than that.

Martin

> I hope one can understand my problem now and i want to thank for the previous answer.
>
> Regards,
>
> Norman
>
> --
> You received this question notification because you are a member of UFL
> Team, which is an answer contact for UFL.
>
> _______________________________________________
> Mailing list: https://launchpad.net/~ufl
> Post to     : <email address hidden>
> Unsubscribe : https://launchpad.net/~ufl
> More help   : https://help.launchpad.net/ListHelp
>

Revision history for this message
Norman Lang (norman-lang) said :
#4

I think your help was quite good allthough my explanation wasn't so good. I think i have to search for an other way then writing the pointwise product into the ufl file. Maybe the way has to be computing that stuff on algerbaic level.
At the end i want to thank you for your help.

Norman