# How to apply particular bc on imported mesh?

**URL:** <https://fenicsproject.discourse.group/t/how-to-apply-particular-bc-on-imported-mesh/6889>\
**Category:** Uncategorized\
**Created:** [October 23, 2021, 6:39am UTC](https://fenicsproject.discourse.group/t/how-to-apply-particular-bc-on-imported-mesh/6889 "2021-10-23T06:39:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkenko](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/rkenko/32/1969_2.png) [@rkenko](https://fenicsproject.discourse.group/u/rkenko)\
**Post date:** [October 23, 2021, 6:39am UTC](https://fenicsproject.discourse.group/t/how-to-apply-particular-bc-on-imported-mesh/6889/1 "2021-10-23T06:39:46Z")

</div>

HI,

Please i would like to know how to apply pointwise bc and a gaussian load over a subdomain on a generated mesh with gmsh.

Here is a code which reproduce my error:

```auto

from dolfin import *
from mshr import *
from math import *
import numpy as np
import sympy
import ufl

# mechanical parameters
E, nu = Constant(210e9), Constant(0.3)
mu, lmbda = E/2/(1+nu), E*nu/(1+nu)/(1-2*nu)
def sigma(v):
	return lmbda*tr(eps(v))*Identity(2)+2.0*mu*eps(v)
def eps(v):
	return sym(grad(v))

# mesh import and principal parameters
mesh = Mesh("gmsh_cube.xml")
mm = 10^(-3)
L = 20*mm
H = 20*mm
mean_mu = 0.5
standard_deviation = 1/np.sqrt(2*pi)
rho_g = 4400*9.81

# subdomains
class upper_set(SubDomain):
	def inside(self, x, on_boundary):
		return (x[0]>L/2-5*standard_deviation) and (x[0]<L/2+5*standard_deviation)
class lower_set_left(SubDomain):
	def inside(self, x, on_boundary):
		return near(x[0],0.,DOLFIN_EPS) and near(x[1],0.,DOLFIN_EPS)
class lower_set_right(SubDomain):
        def inside(self, x, on_boundary):
                return near(x[0],L,DOLFIN_EPS) and near(x[1],0.,DOLFIN_EPS)
boundary_top, boundary_left, boundary_right = upper_set(), lower_set_left, lower_set_right
subdomains = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
subdomains.set_all(0)
boundary_top.mark(subdomains, 1)
ds=Measure('ds', domain=mesh, subdomain_data=subdomains)

# variational problem and bc
f = Constant((0,-rho_g))
g = Expression(("0","(1/(sqrt(2*pi*standard_deviation)))*exp(- pow((x[0]-mean_mu),2)/(2*pow(standard_deviation,2)))"), degree=0, mean_mu=mean_mu, standard_deviation=standard_deviation)

V=VectorFunctionSpace(mesh,'CG',degree=2)
u = Function(V, name="Displacement")
du = TrialFunction(V)
u_ = TestFunction(V)
a = inner(sigma(du),eps(u_))*dx
l = inner(f,u_)*dx+inner(g,u_)*ds(1)

bc_left = DirichletBC(V, Constant((0.,0.)), boundary_left, method="pointwise")
bc_right = DirichletBC(V.sub(1), Constant(0.), boundary_right, method="pointwise")
bc_u = [bc_left, bc_right, boundary_top]

solve(a==l,u,bc_u)

```

and here is the gmsh code :

```auto
//cube 20x20

mm = 10^(-3);
mesh_size = 2*mm;

L = 20*mm;
H = 20*mm;

Point(1) = {0, 0, 0, mesh_size};
Point(2) = {L, 0, 0, mesh_size};
Point(3) = {L, H, 0, mesh_size};
Point(4) = {0, H, 0, mesh_size};

Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};

Line Loop(5) = {1, 2, 3, 4};

Plane Surface(6) = {5};

Physical Surface(7) = {6};

```

Here is how i proceed from the gmsh file : 1. name the gmsh code as “gmsh\_cube.geo” , 2. use the commands “gmsh -2 -format msh22 gmsh\_cube.geo” and “dolfin-convert gmsh\_cube.msh gmsh\_cube.xml” to mesh then to convert it on xml 3. run the fenics code “python code.py”

Thank you in advance for your time

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 23, 2021, 9:07pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-particular-bc-on-imported-mesh/6889/3 "2021-10-23T21:07:43Z")

</div>

> [@rkenko](#):
>
> `boundary_top, boundary_left, boundary_right = upper_set(), lower_set_left, lower_set_right`

Your first issue is the code quoted above, as it has to be changed to:

```python
boundary_top, boundary_left, boundary_right = upper_set(), lower_set_left(), lower_set_right()

```

> [@rkenko](#):
>
> `bc_u = [bc_left, bc_right, boundary_top]`

The second issue is that you are trying to send `boundary_top` into the solver as a DirichletBC, when it is a `SubDomain`.

---

<div class="post-metadata">

**Author:** ![rkenko](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/rkenko/32/1969_2.png) [@rkenko](https://fenicsproject.discourse.group/u/rkenko)\
**Post date:** [October 23, 2021, 9:39pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-particular-bc-on-imported-mesh/6889/4 "2021-10-23T21:39:38Z")

</div>

Hi, thank you very much for your answer. The first issue is a typo when i tried to make a minimal code to represent my problem, i forgot to write the “()”.

Please could you tell me 1. how to correctly apply my particular “g” load over my subdomain then ? it will be correct if i just write “bc\_u = [bc\_left, bc\_right]” and 2. are “bc\_left” and “bc\_right” correctly defined without taking into account the “()” that i forgot ?

Thank you for you time

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 23, 2021, 10:29pm UTC](https://fenicsproject.discourse.group/t/how-to-apply-particular-bc-on-imported-mesh/6889/5 "2021-10-23T22:29:48Z")

</div>

As the top boundary condition is imposed through a Neumann condition,

> [@rkenko](#):
>
> `l = inner(f,u_)*dx+inner(g,u_)*ds(1)`

you only need

```python
bc_u = [bc_left, bc_right]

```

with the corrections (adding `()` as described above).  
Please note that you could use GMSH to mark boundaries, as described in [Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio - #181 by rkailash](https://fenicsproject.discourse.group/t/transitioning-from-mesh-xml-to-mesh-xdmf-from-dolfin-convert-to-meshio/412/181)
