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

Apply a punctual source function from a list of nodes.

0 votes

Dear Sirs,

I have my mesh defined in .*xml. I have my list of nodes from mesh in *.xml.
I am looking for apply a source in this list of nodes.

For exemplo my mesh is in :
https://www.dropbox.com/s/5qvrqew8cnfmj6a/Square3.xml?dl=0

The problem is how to apply a source term in a list of nodes from mesh.

For exemplo this list of nodes:
DATA=[4,5,6,17,18]

Unfortunately I did this simple code, but the list of nodes does not is iqual from array:

from dolfin import*
import sys, math, numpy

mesh = Mesh("Square3.xml")
V = FunctionSpace(mesh, "CG", 1)
B = Function(V)

a=[4,5,6,17,18]
for i in a:
B.vector()[i]=1
plot(B)

Kind regards,
Sr. Costa

asked Jan 26, 2016 by LeoCosta FEniCS User (1,190 points)

1 Answer

+2 votes

Hi, here are two ways to do it

from dolfin import *
from random import sample

mesh = UnitIntervalMesh(20)
n_vertices = mesh.num_vertices()
# Get all vertices in random order
data = sample(range(n_vertices), n_vertices)
# The values are such that we make function f=x
values = mesh.coordinates().reshape((n_vertices, ))[data]

V = FunctionSpace(mesh, 'CG', 1)
f = Function(V).vector()

# First approach.
v2d = vertex_to_dof_map(V) 
for vertex, value in zip(data, values):
    dof = v2d[vertex]
    f[dof] = value

# Compare to f0=x
f0 = interpolate(Expression('x[0]'), V).vector()
f0.axpy(-1, f)
print f0.norm('linf')

# Second approach. We will use it to zero the vector by prescribing 
# negative values
gdim = mesh.geometry().dim()
for vertex, value in zip(data, values):
    v = Vertex(mesh, vertex)
    x = Point(*[v.x(i) for i in range(gdim)])
    p = PointSource(V, x, -value)
    p.apply(f)

# Compare to 0
print f.norm('linf')
answered Jan 27, 2016 by MiroK FEniCS Expert (80,920 points)
...