This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Accessing coordinates of a mesh and "DG" function space

+1 vote

I often need to access the mesh coordinates of a function, so that I can manually set their values. The script below, for example, sets func(x,y,z) = x+y+z, where func is a vector on a mesh.

My question is, is it possible to implement a similar method, only instead of a function space with "Lagrange" 1, it has "DG" 0.

from dolfin import *
import numpy as np

mesh = BoxMesh(-1,-1,-1,1,1,1,20,20,20)
V = FunctionSpace(mesh, "Lagrange", 1)
func = Function(V)

n = V.dim()
d = mesh.geometry().dim()

coor = V.dofmap().tabulate_all_coordinates(mesh)
coor.resize((n,d))

vertex_values = np.zeros(mesh.num_vertices())
for vertex in vertices(mesh):
    x = vertex.x(0)
    y = vertex.x(1)
    z = vertex.x(2)

    vertex_values[vertex.index()] = x+y+z

func.vector()[:] = vertex_values[dof_to_vertex_map(V)]

plot(func,interactive=True)
asked Nov 17, 2014 by sixtysymbols FEniCS User (2,280 points)

1 Answer

+2 votes
 
Best answer

You've already created and reshaped the coor-object, which gives you the coordinates of all the degrees of freedom. Just loop over that instead:

for i in xrange(len(coor)):
     x = coor[i,0]
     y = coor[i,1]
     z = coor[i,2]

     func.vector()[i] = x+y+z
answered Nov 17, 2014 by Øyvind Evju FEniCS Expert (17,700 points)
selected Nov 17, 2014 by sixtysymbols
...