Want to define some value on interior nodes

Hello everyone

If I marked all interior nodes with point 1 using syntax

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)

u = TrialFunction(V)
v = TestFunction(V)
NodesMarker = MeshFunction('size_t', mesh, 0)
NodesMarker.set_all(0) 

a = inner(grad(u), grad(v)) * dx 
L = inner(f, v) * dx 

Now I want to assign u = 10 on all interior nodes(i.e. to point 0). How I can do this thing.
There is any way to define in weak formulation or we can do in any other way.

You can assign this directly to your function using vertex_to_dof_map

from dolfin import *
import numpy as np

mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)

class MarkedArea(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] < 0.5
NodesMarker = MeshFunction('size_t', mesh, 0)
NodesMarker.set_all(0) 
MarkedArea().mark(NodesMarker, 1) 

v_to_d = vertex_to_dof_map(V)
dofs = v_to_d[np.flatnonzero(NodesMarker.array()==1)]
values = np.full_like(dofs, 10, dtype=np.float64)
uh = Function(V)
uh.vector().vec().setValuesLocal(dofs, values) 
uh.vector().update_ghost_values()
print(assemble(uh*dx)) 
File("u.pvd") << uh
from dolfin import *
import numpy as np

# define mesh
N = 1
mesh = UnitSquareMesh(N, N)

V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
R = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
me = MixedElement([V, R])
W = FunctionSpace(mesh, me)

(u, lmda) = TrialFunctions(W)
(v, tau) = TestFunctions(W)
g = Constant(0)

# Here i marked these two set of nodes with some condition
ActiveNodes = NodesMarker.where_equal(1)
InActiveNodes = NodesMarker.where_equal(2)

a = inner(grad(u), grad(v)) * dx - lmda * v * dx 
L = inner(f, v) * dx 


Now i have to impose u = g on ActiveNodes and lmda = 0 on InActiveNodes. I have to impose in such a way so that i can change set of active nodes and Inactive nodes in a iterative process.
Please suggest something. Hope to hear from you soon. THere is any way to do these things via weak formulation.
shall we impose something in a weak formulation using dS(1) but it doesn’t work. suggest something in such a way.

You could impose this weakly using Nitsche’s method for weak enforcement of boundary condition, i.e.
create a g where you assign the values on the boundary, then integrate over your boundary ds with this value. The variational formulation for Nitsche’s method has been shown for instance: Weak imposition of Dirichlet conditions for the Poisson problem — FEniCSx tutorial

1 Like

Sir, i have to impose u=g on interior nodes not on boundary nodes…dS is used for interior edges

What we can use in our weak formulation in order to impose u=g on interior nodes.

Wouldn’t his mean that your problem can be split into two sections:
u=\begin{cases} u=g \text{ on } \Omega_0\\ -\Delta u = f \text{ on } \Omega_1\\ u=g \text{ on } \partial\Omega_0\cap\partial\Omega_1 \end{cases}
Given that you mark all cells that satisfies being in \Omega_0 with 0, all cells in \Omega_1 with one, you can write:

while some_condition:
  # Update cell marker based on some criterion
  dx = Measure("dx", domain=mesh, subdomain_data=cell_marker)
  a = inner(u, v)*dx(0) + inner(grad(u), grad(v))*dx(1)
  L = inner(g, v)*dx(0) + inner(f, v)*dx(1)
  solve(a==L, uh)
1 Like

sir, you didn’t get my points May be there is a case in which two nodes of a cell belongs to Ω0 and one node in Ω1.
so, i have to impose condition on a node.
Here i foam two points 1 and 2, some nodes are in point 1 and some nodes in point 2 i.e. these are discrete points.
On the nodes which are in point 1, I have to impose u = g there.
Tell me if i will do like that given below, Is it works?

from dolfin import *
import numpy as np

# define mesh
N = 1
mesh = UnitSquareMesh(N, N)

V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
R = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
me = MixedElement([V, R])
W = FunctionSpace(mesh, me)

(u, lmda) = TrialFunctions(W)
(v, tau) = TestFunctions(W)
g = Constant(0)

# Here i marked these two set of nodes with some condition
ActiveNodes = NodesMarker.where_equal(1)
InActiveNodes = NodesMarker.where_equal(2)

a = inner(grad(u), grad(v)) * dx - lmda * v * dx 
L = inner(f, v) * dx 
v_to_d = vertex_to_dof_map(W)
dofs1 = v_to_d[np.flatnonzero(ActiveNodes)]
values1 = np.full_like(dofs1, g2 , dtype=np.float64)
W.sub(0).vector().vec().setValuesLocal(dofs1, values1) 
W.sub(0).vector().update_ghost_values()
dofs2 = v_to_d[np.flatnonzero(InActiveNodes)]
values2 = np.full_like(dofs2, Constant(0) , dtype=np.float64)
W.sub(1).vector().vec().setValuesLocal(dofs2, values2) 
W.sub(1).vector().update_ghost_values()
solve(a == L, w,bc)

How about something along the lines of:

from IPython import embed
from dolfin import *
import numpy as np

# define mesh
N = 23
mesh = UnitSquareMesh(N, N)

V = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
R = FiniteElement("Real", mesh.ufl_cell(), 0)
me = MixedElement([V, R])
W = FunctionSpace(mesh, me)

(u, lmda) = TrialFunctions(W)
(v, tau) = TestFunctions(W)
g = Constant(-0.01)
x = SpatialCoordinate(mesh)
f = sin(x[0])+3*cos(x[1])


class MarkedArea(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] < 0.5


a = inner(grad(u), grad(v)) * dx - lmda * v * dx + u*tau*dx
L = inner(f, v) * dx
bc = DirichletBC(W.sub(0), g, MarkedArea(), method="pointwise")
w = Function(W)

solve(a == L, w, bc)

u, r = w.split(deepcopy=True)
File("w.pvd").write(u)