Apply DirichletBC from known vector

Hi,

i want to know if there is the possibility to assign the DirichletBC on a boundary starting from the known displacement value of each point belonging to that boundary.

Thanks in advance

Could you be a bit more specific, and maybe sketch out a minimal example of what you would like to achieve? Is this vector a dolfin.Function?

Thanks for the reply, here is a minimal code

from dolfin import *
import numpy as np
import mshr as mr
import ufl as uf

E, nu = 70000., 0.2
L, H = 100., 10.
cell_size = 1.
nel = int(L / cell_size)

geom = mr.Rectangle(Point(0, 0), Point(L, H))
mesh = mr.generate_mesh(geom, nel)
ndim = mesh.topology().dim()

class half(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] < L/2.
h = half()

class Left(SubDomain):
    def inside(self, x, on_boundary):
        return near(x[0], 0.)
left = Left()

class Right(SubDomain):
    def inside(self, x, on_boundary):
        return near(x[0], L)
right = Right()

class Mid(SubDomain):
    def inside(self, x, on_boundary):
        return between(x[0], (0.49*L, 0.5*L))
mid = Mid()

boundaries = MeshFunction('size_t', mesh, 1, 0)
domains = MeshFunction('size_t', mesh, 2, 0)
h.mark(domains, 1)
left.mark(boundaries, 1)
right.mark(boundaries, 2)

# --- Funzioni spazio 1
V = VectorElement("Lagrange", mesh.ufl_cell(), 1)
V_u = FunctionSpace(mesh, V)
v2d_u1 = vertex_to_dof_map(V_u)

u1 = Function(V_u)
du = TrialFunction(V_u)
v = TestFunction(V_u)

bc_l = DirichletBC(V_u, Constant((0., 0.)), boundaries, 1)
bc_r = DirichletBC(V_u.sub(0), Constant(0.1), boundaries, 2)
bcu = [bc_l, bc_r]

mu, lamb = E / (2. * (1. + nu)), E * nu / (1. - nu ** 2)
def eps(u):
    return sym(grad(u))
def sigma(u):
    return 2. * mu * (eps(u)) + lamb * tr(eps(u)) * Identity(ndim)

En = 0.5 * inner(sigma(u1), eps(u1))*dx
E_u = derivative(En, u1, v)
E_du = uf.replace(E_u, {u1: du})

problem_u = LinearVariationalProblem(lhs(E_du), rhs(E_du), u1, bcu)
solver_u = LinearVariationalSolver(problem_u)
solver_u.solve()

File('u1.pvd') << (u1, 0)

mesh2 = SubMesh(mesh, domains, 1)
points2 = MeshFunction('size_t', mesh2, 0, 0)
boundaries2 = MeshFunction('size_t', mesh2, 1, 0)
domains2 = MeshFunction('size_t', mesh2, 2, 0)
dx2 = Measure('dx', domain=mesh2, subdomain_data=domains2)

left.mark(boundaries2, 1)
mid.mark(boundaries2, 2)
mid.mark(points2, 1)

V_u2 = FunctionSpace(mesh2, V)
v2d_u2 = vertex_to_dof_map(V_u2)

u2 = Function(V_u2)
du2 = TrialFunction(V_u2)
v2 = TestFunction(V_u2)

vert_on_bound = np.concatenate(np.argwhere(points2.array() == 1))
vmap = mesh2.data().array('parent_vertex_indices', 0)
vmap_bound = vmap[vert_on_bound]

vvv1 = np.zeros(2*len(vmap_bound))
vvv2 = np.zeros(2*len(vert_on_bound))

vvv1[0:len(vvv1):2] = (2*vmap_bound)
vvv1[1:len(vvv1):2] = (2*vmap_bound+1)
vvv1 = list(map(int, vvv1))
vvv2[0:len(vvv2):2] = (2*vert_on_bound)
vvv2[1:len(vvv2):2] = (2*vert_on_bound+1)
vvv2 = list(map(int, vvv2))

u2.vector()[v2d_u2[vvv2]] = u1.vector()[v2d_u1[vvv1]]

File('u2.pvd') << (u2, 0)

First i create mesh1 and solve a traction elasticity problem on it. After i create a submesh which is half of the mesh1 and copy the solution for the points in the right edge of the submesh on the function u2. How can i use those function values as a boundary condition on the submesh for an elasticity problem (therefore obtaining the same displacement results between mesh2 and the corresponding portion of mesh1)?

I had met the same problem but I have the experience to construct a function from a numpy matrix. Here is my code:

class ExpressionPhi(UserExpression):
    def __init__(self, phi, *args, **kwargs):
        self.data=phi
        super().__init__(*args, **kwargs)
    
    def value_shape(self):
        return ()
    
    def eval(self, values, x):
        i = int((x[0]+DOLFIN_EPS)*128)
        j = int((x[1]+DOLFIN_EPS)*128)
        i = 127 if i > 127 else i
        j = 127 if j > 127 else j
        values[0] = self.data[i][j]

It can be used to apply Dirichlet boundary conditions.

1 Like

Could you provide a MWE on how to use your approach? I’ve been trying to fit your solution to my problem (which is similar to Rareform), but I still cant figure out how to do it properly. Thanks in advance!