Fenicsx different tags in boundary

hello,
i am new in fenicsx. i want to solve a rectangular box.
first i want read a file i have done this step.
Next in mesh i have different boundaries tags. i don’t how to solve can you please send a tutorial.
I am sharing my code this is not working.
from dolfinx import MeshTags, IO
from dolfinx.cpp.mesh import CellType
from dolfinx.mesh import create_meshtags
from dolfinx.io import XDMFFile
from mpi4py import MPI

Initialize MPI communicator

comm = MPI.COMM_WORLD

Read mesh from file

with XDMFFile(comm, “ventricle_mesh.xdmf”, “r”) as infile:
mesh = infile.read_mesh()

Create MeshTags for boundary tags

with XDMFFile(comm, “ventricle_mesh.xdmf”) as infile:
boundary_tags = MeshTags(mesh, 2, infile, “name_to_read”)

Create MeshTags for facet regions

with XDMFFile(comm, “ventricle_mesh.xdmf”) as infile:
facet_tags = MeshTags(mesh, 2, infile, “name_to_read”)

There is no way anyone can help you fully resolve your issue, as you have not:

  1. formatted your code with 3x`, ie
```python
import dolfinx
# add more code here

```
  1. Added a plaintext version of your input mesh.

A tutorial can be found at
https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html?highlight=read_meshtags#convert-msh-files-to-xdmf-using-meshio
look for read_meshtags

Hi
import ufl
from dolfinx.fem import (Constant, Function, FunctionSpace,
assemble_scalar, dirichletbc, form, locate_dofs_geometrical)
from dolfinx.fem.petsc import LinearProblem
from dolfinx.mesh import create_unit_square
from mpi4py import MPI
from ufl import SpatialCoordinate, TestFunction, TrialFunction, dot, dx, ds, grad, Measure
from petsc4py.PETSc import ScalarType
from dolfinx import *
from dolfinx.io import XDMFFile
from mpi4py import MPI

Read mesh from file

infile = io.XDMFFile(MPI.COMM_WORLD, “Domain.xdmf”, “r”)

just read a mesh

msh = infile.read_mesh(name=“Grid”)

read a all domain

domain =infile.read_meshtags(msh,name=“Grid”)
infile.close()

this is for full domain

newValues = [-i for i in domain.values]
domain = mesh.meshtags(msh, domain.dim, domain.indices, newValues)
#fdim=domain.topology.create_connectivity(domain.topology.dim, domain.topology.dim-1)

msh.topology.create_connectivity(2, 0)

read a boundary file

boundaryInfile = io.XDMFFile(MPI.COMM_WORLD, “Domain_boundary.xdmf”, “r”)
boundary_msh = boundaryInfile.read_mesh(name=“Grid”)
boundaryMeshTags = boundaryInfile.read_meshtags(msh, name=“Grid”)
boundaryInfile.close()

this is for full boundary

newValues = [-i for i in boundaryMeshTags.values]
boundaryMeshTags = mesh.meshtags(msh, boundaryMeshTags.dim, boundaryMeshTags.indices, newValues)
del newValues
V=FunctionSpace(msh,(“Lagrange”,2))
boundaryTags = {“left” : 1, “right” : 2,“up” : 3 }
leftFacets = boundaryMeshTags.find(boundaryTags[“left”])
rightFacets = boundaryMeshTags.find(boundaryTags[“right”])
upFacets = boundaryMeshTags.find(boundaryTags[“up”])

downFacets =boundaryMeshTags.find(boundaryTags[“down”])

fronttFacets =boundaryMeshTags.find(boundaryTags[“front”])

backFacets =boundaryMeshTags.find(boundaryTags[“back”])

headDofs = fem.locate_dofs_topological(V=V, entity_dim=2, entities=leftFacets)
handsDofs = fem.locate_dofs_topological(V=V, entity_dim=2, entities=rightFacets)

bcs = [

fem.dirichletbc(value=ScalarType(0), dofs=headDofs, V=V),

fem.dirichletbc(value=ScalarType(0), dofs=handsDofs, V=V),
fem.dirichletbc(value=ScalarType(0), dofs=headDofs, V=V),

]

boundaryMeshTags.values[upFacets] = 1
dx = Measure(“dx”, msh, subdomain_data=domain)
ds = Measure(“ds”, msh, subdomain_data=boundaryMeshTags)
u = TrialFunction(V)
v = TestFunction(V)
x = ufl.SpatialCoordinate(msh)
f = Constant(msh, (0.0))
g = Constant(msh, (0.0))
a = dot(grad(u), grad(v)) * dx
L = dot(f, v) * dx + dot(g, v) * ds (boundaryTags[“up”])

problem = fem.petsc.LinearProblem(a, L, bcs=bcs, petsc_options={“ksp_type”: “preonly”, “pc_type”: “lu”})
uh = problem.solve()

with io.XDMFFile(msh.comm, “domain_boundary.xdmf”, “w”) as file:
file.write_mesh(msh)
file.write_function(uh)
I have applied neumann boundary condition and dirichletbc conditions I don’t know why but it is not applying can you tell what should I do
Regards
Minha

Please follow my instructions in the previous post

Thank you so much for the quick response. But this is not defined
in that tutorial. please tellme about this

dx = ufl.Measure(“dx”, mesh, subdomain_data=meshTags)
ds = ufl.Measure(“ds”, mesh, subdomain_data=boundaryMeshTags)

Please read my actual reply, Which is about the formatting of the code. You are also not explaining what is wrong with your code? Are you not getting the expected result?

Start by considering a simpler example, like:
https://jsdokken.com/dolfinx-tutorial/chapter3/neumann_dirichlet_code.html

Sorry for bed explaintiion.Yes I am not getting expected results. Like I have rectangle box in this box has 3 boundary tags I want to apply 2 tags dirchlet conditions and 1 neumann condition.
Like I have external mesh.Now I have question how I define these conditions at external mesh.
I followed your last tutorial I get some things but in all boundaries they apply dirchlet conditions. Hope so you understand me.
Sorry
Many thanks

Hi
I did follow all the link but in my external mesh file I didn’t mark a boundary tags when i created a mesh.is it possible with out boundary mark can I apply boundaries conditions In general.
Regards,
MM

https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html?highlight=read_meshtags#convert-msh-files-to-xdmf-using-meshio

If you have no boundary markers in your file, you should mark them by first

  1. locating facets on the boundary, either with dolfinx.mesh.exterior_facet_indices or dolfinx.mesh.locate_entities_boundary.
    2a. add these to a mesh tag if used with Neumann boundary conditions and ds measure.
    2b. Find dofs belonging to these facets using dolfinx.fem.locate_dofs_topological

All these functions are explained on the tutorial pages.

Hi Dokken,
Hope you are doing well. Could you please tell me i want to convert vtk to XDMF format
But i got this error could tell me what is the problem the problem in mesh or my code

meshio.write(‘Domain_boundary.xdmf’,meshio.Mesh(msh.points*1e-3,cells={‘triangle’:msh.cells_dict[‘triangle’]},cell_data={‘boundary’:[msh.cell_data_dict[‘cell_tags’][‘triangle’]]}))

KeyError: ‘cell_tags’

Well, start by checking the content of msh.cell_data_dict to see what you have tag in your mesh.

This is not a reply to my post above, so I do not know how to proceed further with this topic.