Assign values to the node in vectorFunctionSpace

Hi, I want to assign values to specific node for one component (vectorFunctionSpace). Please find the attached MWC. current code is not able to assign for vector.

from dolfin import *
import numpy as np
mesh = UnitSquareMesh(10,10)
V = VectorFunctionSpace(mesh, "CG", 1)

w = Function(V)

x = V.tabulate_dof_coordinates()

w_array = w.vector().get_local()


for i in range(mesh.num_vertices()):
    if x[i][0]==0 and x[i][1]<=0.5:
        w_array[i][0] = 1.0

w.sub(0).vector().set_local(w_array)

p=plot(w[0])
plt.colorbar(p)
plt.show()`

You can use a function assigner:

from dolfin import *
import numpy as np
import matplotlib.pyplot as plt
mesh = UnitSquareMesh(10, 10)
V = VectorFunctionSpace(mesh, "CG", 1)

w = Function(V)

x = V.tabulate_dof_coordinates()

w_array = w.vector().get_local()

V0 = V.sub(0).collapse()
x0 = V0.tabulate_dof_coordinates()
w0 = Function(V0)
w0_array = w0.vector().get_local()
for i in range(x0.shape[0]):
    if x0[i, 0] == 0 and x0[i, 1] <= 0.5:
        w0_array[i] = 1.0

w0.vector().set_local(w0_array)

fa = FunctionAssigner(V.sub(0), V0)
fa.assign(w.sub(0), w0)


p = plot(w)
plt.colorbar(p)
plt.savefig("vector-plot.png")
2 Likes

Thank you for your help. It is working fine now.