Subject: Re: [Ufl] [Question #164419]: How to compute a pointwise vector product within an ufl-file

From: Martin Sandve Alnæs<[email-masked]>
Date: Mon, 11 Jul 2011 15:01:28 -0000

Question #164419 on UFL changed:
https://answers.launchpad.net/ufl/+question/164419

    Status: Open => Answered

Martin Sandve Alnæs proposed the following answer:
On 11 July 2011 15:25, Norman Lang <[email-masked]> 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-masked]
> Unsubscribe : https://launchpad.net/~ufl
> More help   : https://help.launchpad.net/ListHelp
>

-- 
You received this question notification because you are a member of UFL
Team, which is an answer contact for UFL.

---

From: Martin Sandve Alnæs<[email-masked]>
Date: Mon, 11 Jul 2011 17:11:19 -0000

Status: Open => Answered

Hi Norman,

On 11 July 2011 17:51, Norman Lang <[email-masked]> wrote:
>    Status: Answered => Open
> Norman Lang is still having a problem:
> 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.
>
> 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
>
> --