### How to define an array of Constants in UFL

281

views

0

Hello all,

I am trying to solve a problem with Dolfin C++ interface, I need to define a variational form which includes a term as

$\sum_{i=1}^nk_nvds\left(i\right)$∑

The parameter $k_n$

anyone knows how I can do this?

I am trying to solve a problem with Dolfin C++ interface, I need to define a variational form which includes a term as

$\sum_{i=1}^nk_nvds\left(i\right)$∑

_{i=1}^{n}`k`_{n}`v``d``s`(`i`)The parameter $k_n$

`k`_{n}are some known constant need to be defined by user, but n is not known. I need to define a variational form like```
for i in range(n):
list.append(k[i]*v*ds(i))
a = sum(list)
```

anyone knows how I can do this?

Community: FEniCS Project

### 2 Answers

0

Use the numpy sum function after converting the list to an array.

```
for i in range(n):
list.append(k[i]*v*ds(i))
```

list = np.array(list)
a = np.sum(list)

I don't have my computer with me to test, but can you not rename the constant with c.rename('%i', % i)?

written
11 months ago by
pf4d

Yes, init with "name" arg:
https://fenicsproject.org/olddocs/dolfin/1.3.0/python/programmers-reference/functions/constant/Constant.html

written
11 months ago by
pf4d

I am not so sure I understand completely. You can not do it since you don't know which name belongs to e1_list or e2_list from C++. As shown in my second example with n = 4, FFC will produce 8 constants named as w0, w1, w2, ..., w8, then I am not so sure how to know which belongs to e1_list from C++.

written
11 months ago by
sin2cos

Also, if you know n at runtime, and you know how you built the constants, you know how to access the vector components too...

written
11 months ago by
pf4d

Yes, you are right. But if we have two vectors, then you will have all constant named sequentially as w0 w1 w2 ..., from which you cannot distinguish the constants of the two lists.

written
11 months ago by
sin2cos

I am not a UFL expert, and was only responding to the python code you posted. It is trivial to achieve what you need with the python interface. Perhaps you can convert your C++ code to a dolfin jit-compiled extension.

written
11 months ago by
pf4d

The code posted is actually UFL code. I am still thinking about this problems. I will post my final solution here later on. It seems there is no any way to achieve what I want within UFL. A careful separation of variational form might be helpful.

written
11 months ago by
sin2cos

0

Hi,

you might be interested in considering the following MWE for adding (multiple) ufl-forms (and yes, it really is this easy ;) )

Hope this helps.

you might be interested in considering the following MWE for adding (multiple) ufl-forms (and yes, it really is this easy ;) )

```
from dolfin import *
mesh = UnitSquareMesh(2,2)
V = FunctionSpace(mesh,'CG',1)
u = TrialFunction(V)
v = TestFunction(V)
uh = Function(V)
a = u*v*dx
f = Constant(0)*v*dx
# List of coefficients
k_list = [1.0, 2.0, 3.0]
# Add forms to f, based on k_list
for k in k_list:
f += Constant(k)*v*dx
# Solve and plot to check result
solve(a == f, uh)
plot(uh)
interactive()
```

Hope this helps.

Please login to add an answer/comment or follow this question.

then I can give es1, es2, etc. a value in C++ code. But this can be done only if I know in advance that there are 4 constants, but if I don't know how many constants I have in advance, and it will be decided later in C++ code, it looks difficult to achieve this. You can also do something like

But after compilation with FFC, the names of constants will be given automatically by FFC as w0, w1, w3 ..., then you can not distinguish the constants corresponding to the e1_list from these of e2_list. I guess there is no an automatic solution for this. You need to define the constants one by one as given in the first example here.