### Vector source function from numerical nodal array

180

views

0

I am trying to solve the following weak formulation:

$\int_{\Omega}\left(\nabla u\cdot\nabla v\right)d\Omega=\int_{\Omega}\textbf{b}\cdot\nabla vd\Omega$∫

in a 3D mesh, where b is a numerical numpy array (number of nodes x 3) representing nodal values. The product inside the integral on the rhs can also be written as:

$\sum_k\int_{\Omega}b_k\cdot\frac{\partial v_n}{\partial x_k}d\Omega$∑

N being the number of nodes per element.

I have looked at other similar questions but I still cannot wrap my head around this.

How can I write this problem in a way dolfin is able to interpret?

Thank you.

$\int_{\Omega}\left(\nabla u\cdot\nabla v\right)d\Omega=\int_{\Omega}\textbf{b}\cdot\nabla vd\Omega$∫

_{Ω}(∇`u`·∇`v`)`d`Ω=∫_{Ω}**b**·∇`v``d`Ωin a 3D mesh, where b is a numerical numpy array (number of nodes x 3) representing nodal values. The product inside the integral on the rhs can also be written as:

$\sum_k\int_{\Omega}b_k\cdot\frac{\partial v_n}{\partial x_k}d\Omega$∑

_{k}∫_{Ω}`b`_{k}·∂`v`_{n}∂`x`_{k}`d`Ω for n=1,...NN being the number of nodes per element.

I have looked at other similar questions but I still cannot wrap my head around this.

How can I write this problem in a way dolfin is able to interpret?

Thank you.

Community: FEniCS Project

How is the array b ordered? with respect to the mesh coordinates? with respect to the dofs of the function space? Maybe this answer can help you (the second part): https://fenicsproject.org/qa/2715/coordinates-u_nodal_values-using-numerical-source-function?show=2721#a2721

written
4 months ago by
Miguel

Array b is ordered in this way: b[i] corresponds to nodes[i] and so on. The question you cite did not help much :(

written
4 months ago by
Semion

### 1 Answer

0

If b[i] corresponds to nodes[i] and the dofs of your function space are defined on the vertices of the mesh, consider this:

```
from dolfin import *
import numpy as np
mesh = BoxMesh(Point(0,0,0), Point(1,1,1), 2, 2, 2)
V = VectorFunctionSpace(mesh, "CG", 1)
# numpy array
b = np.zeros([mesh.num_vertices(), 3])
b[:,0] = 1.0 # non-zero component just in x-direction
# Vertex to dofmap
v2d = vertex_to_dof_map(V)
# reshape numpy array
b = b.reshape([len(v2d), 1])
# Fill function with the numpy values
B = Function(V)
B.vector()[v2d] = b
plot(B, interactive=True)
```

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