How to define a function with computed nodal values?

Hi,
In my work, during solving the problem, I need to compute some quantity in each node of the mesh (by loop on the nodes of the mesh) and then consider a piecewise linear function with computed nodal’s value. How can I do that by FEinCS.
nx = ny = 8
mesh = UnitSquareMesh(nx, ny, “right/left”)
mesh_points = mesh.coordinates()
x = SpatialCoordinate(mesh)
V = FiniteElement(“CG”, mesh.ufl_cell(), 1)
g = Function(V)
i = 0
nvertices = len(g.vector().array())
gnodal = [0 for xn in range(nvertices)]

for x1,y1 in mesh_points:
gnodal[i] = f(x1,y1) # f(x1,y1) means some computation on x1 and y1
i = i+1

How to define the function g of V-space in which to assign the value of nodes by vector gnodal?

Thank you in advance.

The following code should solve your problem:

from dolfin import *
import matplotlib.pyplot as plt

def f(coordinate):
    return coordinate[0]*coordinate[1]

mesh = UnitSquareMesh(10,10)

V = FunctionSpace(mesh, 'CG', 1)

g = Function(V)

Coords = V.tabulate_dof_coordinates()

for i in range(V.dim()):
    g.vector()[i] = f(Coords[i])
    
plot(g)
plt.show()  

Thank you for your help.