jorgensd / dolfinx-tutorial

A reimplementation of the Springer book: https://github.com/hplgit/fenics-tutorial/, covering new topics as well as transitioning from dolfin to dolfinx
https://jorgensd.github.io/dolfinx-tutorial/
103 stars 60 forks source link

How to create boundary condition in FEniCSx #106

Closed wnstss closed 1 year ago

wnstss commented 1 year ago

Hello, every one,

I would like to update my code from FEniCS to FEniCSx. But I met some problems on setting the boundary condtions. The following codes were writen using FEniCS.


def boundary(x, on_boundary): return on_boundary

def boundary_Left(x, on_boundary): return on_boundary and near(x[0],0)

def boundary_Right(x, on_boundary): return on_boundary and near(x[0],1)

bc_L = DirichletBC(V, Constant(10), boundary_Left) bc_R = DirichletBC(V, Constant(40), boundary_Right) bcs =[ bc_L, bc_R]


I would like to rewriten these codes using dolfinx as follows: dofs_L = locate_dofs_geometrical(V, lambda x: np.isclose(x[0], 0)) u_L = Function(V) u_L.interpolate(lambda x: PETSc.ScalarType(10)) bc_L = dirichletbc(u_L, dofs_L)

dofs_R = locate_dofs_geometrical(V, lambda x: np.isclose(x[0], 1)) u_R = Function(V) u_R.interpolate(lambda x: PETSc.ScalarType(40)) bc_R = dirichletbc(u_R, dofs_R) bcs = [bc_R, bc_L]

It seems there are some problems in "u_L.interpolate(lambda x: PETSc.ScalarType(10))".

Could any one give me some help to revise the codes.

Best regards,

Lei

jorgensd commented 1 year ago

the function you use inside interpolate expects an input that is an (3, N) array of coordinates, and returns an array of length N. Thus you would need to change your code to u_L.interpolate(lambda x: numpy.full(x.shape[1], PETScScalarType(10.)))

jorgensd commented 1 year ago

For further questions regarding the FEniCSx software (and not the tutorial itself), I would recommend you to use the Discourse forum: https://fenicsproject.discourse.group/

wnstss commented 1 year ago

Dear Jorgensd Thank you very much for your quick reply. I will use the forum latter.

Best regards

Lei