Define a cylinder inside a box

hi all,

im trying to create a domain that consist a rod and inside of if i have cylinder, how can do it?
how do i create the mesh?

tried to do it like in thise mesh Navier-Stokes equations — FEniCS hands-on
but its in 2d and i need 3d and also i understood the mshr is not supported anymore, so what can change that if not mshr?

if somehow relevant, this is what i tried to do

length, width, height = 1.0, 0.2, 0.2  # Dimensions of the rod
num_elements = 40  # Number of elements along each dimension
rod = BoxMesh(Point(0, 0, 0), Point(length, width, height), num_elements, 4, 4)

#definnig the young modoulus
E1, E2, nu = 5e5, 55e3, 0.49

# Create mesh and define function space
N_circle = 16
N_bulk = 64
center = Point(0.2, 0.2)
radius = 0.05
L = 2.2
W = 0.41
radius = 0.1
height = 1.0
resolution = 120  # Number of divisions along the radius

# Create a cylinder mesh using mshr
cylinder = mshr.Cylinder(fe.Point(0, 0, 0), fe.Point(height, 0, 0), radius, radius)
geometry = rod \
             - cylinder
mesh = mshr.generate_mesh(geometry, resolution)

and this is the error i got
"Traceback (most recent call last):
File “/home/mirialex/PycharmProjects/fibers/19-05-24.py”, line 117, in
geometry = rod
TypeError: unsupported operand type(s) for -: ‘dolfin.cpp.generation.BoxMesh’ and ‘mshr.cpp.Cylinder’
"

if there is nothing but to use this dolfinx/python/demo/demo_gmsh.py at e64178c679d4186ed1c5ab3c2e48b4ff28feef6c · FEniCS/dolfinx · GitHub, how can install this libarary, cause the app itself already installed on my pc but not on my ubonto, cause i already tried to use sudo apt-get install gmsh or pip install gmsh and once i run this code dolfinx/python/demo/demo_gmsh.py at e64178c679d4186ed1c5ab3c2e48b4ff28feef6c · FEniCS/dolfinx · GitHub it still tells me "This demo requires gmsh to be installed
"

Please make sure that you run python3 -m pip install gmsh. Then in turn test the following

python3 -c "import gmsh; import dolfinx"

Please specify how you installed (legacy?) Dolfin, what version you are using etc.

it seems to not working as well

this is my info about dolfin and fenics
/usr/bin/python3.10 /home/mirialex/PycharmProjects/fibers/dfg.py
/usr/lib/petsc/lib/python3/dist-packages/dolfin/init .py

im using ubonto
Description: Ubuntu 22.04.2 LTS
Release: 22.04

FEniCS version: 2019.2.0.13.dev0
DOLFIN version: 2019.2.0.dev0

when i tried to run what you suggested i got
Requirement already satisfied: gmsh in ./anaconda3/lib/python3.10/site-packages (4.13.0)

but when i run the code i posted previusly i still get that i need to install gmsh

and when i run what you suggested for the testing i got

managed to install

but once i try to run this

import fenics as fe
from dolfin import *
import matplotlib.pyplot as plt
from ffc.fiatinterface import create_quadrature as cquad
import numpy as np
import mshr
from dolfinx.io import XDMFFile, gmshio
from mpi4py import MPI


try:
    import gmsh  # type: ignore
except ImportError:
    print("This demo requires gmsh to be installed")
    exit(0)

def gmsh_sphere_minus_box(model: gmsh.model, name: str) -> gmsh.model:
    """Create a Gmsh model of a sphere with a box from the sphere removed.

    Args:
        model: Gmsh model to add the mesh to.
        name: Name (identifier) of the mesh to add.

    Returns:
        Gmsh model with a sphere mesh added.
    """
    model.add(name)
    model.setCurrent(name)

    sphere_dim_tags = model.occ.addSphere(0, 0, 0, 1)
    box_dim_tags = model.occ.addBox(0, 0, 0, 1, 1, 1)
    model_dim_tags = model.occ.cut([(3, sphere_dim_tags)], [(3, box_dim_tags)])
    model.occ.synchronize()

    # Add physical tag 1 for exterior surfaces
    boundary = model.getBoundary(model_dim_tags[0], oriented=False)
    boundary_ids = [b[1] for b in boundary]
    model.addPhysicalGroup(2, boundary_ids, tag=1)
    model.setPhysicalName(2, 1, "Sphere surface")

    # Add physical tag 2 for the volume
    volume_entities = [model[1] for model in model.getEntities(3)]
    model.addPhysicalGroup(3, volume_entities, tag=2)
    model.setPhysicalName(3, 2, "Sphere volume")

    model.mesh.generate(dim=3)
    return model

def create_mesh(comm: MPI.Comm, model: gmsh.model, name: str, filename: str, mode: str):
    """Create a DOLFINx from a Gmsh model and output to file.

    Args:
        comm: MPI communicator top create the mesh on.
        model: Gmsh model.
        name: Name (identifier) of the mesh to add.
        filename: XDMF filename.
        mode: XDMF file mode. "w" (write) or "a" (append).
    """
    msh, ct, ft = gmshio.model_to_mesh(model, comm, rank=0)
    msh.name = name
    ct.name = f"{msh.name}_cells"
    ft.name = f"{msh.name}_facets"
    with XDMFFile(msh.comm, filename, mode) as file:
        msh.topology.create_connectivity(2, 3)
        file.write_mesh(msh)
        file.write_meshtags(ct, msh.geometry, geometry_xpath=f"/Xdmf/Domain/Grid[@Name='{msh.name}']/Geometry"
        )
        file.write_meshtags(ft, msh.geometry, geometry_xpath=f"/Xdmf/Domain/Grid[@Name='{msh.name}']/Geometry"
        )


# -

# ## Generate meshes

# Create a Gmsh model and set the verbosity level.


# +
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)

# Create model
model = gmsh.model()
# -

# First, we create a Gmsh model of a sphere using tetrahedral cells
# (linear geometry), then create independent meshes on each MPI rank and
# write each mesh to an XDMF file. The MPI rank is appended to the
# filename since the meshes are not distributed.


# Create model


model = gmsh_sphere_minus_box(model, "Sphere minus box")
model.setCurrent("Sphere minus box")
create_mesh(MPI.COMM_WORLD, model, "ball_d1", "out_gmsh/mesh.xdmf", "w")
# -

# For the mesh of the sphere with a box remove, we can increase the
# degree of the geometry representation to 2 (quadratic geometry
# representation). The higher-order distributed mesh is appended to the
# XDMF file.

# +
model.mesh.generate(3)
gmsh.option.setNumber("General.Terminal", 1)
model.mesh.setOrder(2)
gmsh.option.setNumber("General.Terminal", 0)
create_mesh(MPI.COMM_WORLD, model, "ball_d2", "out_gmsh/mesh.xdmf", "a")
# -

# Finally, we create a distributed mesh using hexahedral cells of
# geometric degree 2, and append the mesh to the XDMF file.

from the doc i posted before, this is what i got
“Traceback (most recent call last):
File “/home/mirialex/PycharmProjects/fibers/19-05-24.py”, line 98, in
create_mesh(MPI.COMM_WORLD, model, “ball_d1”, “out_gmsh/mesh.xdmf”, “w”)
File “/home/mirialex/PycharmProjects/fibers/19-05-24.py”, line 66, in create_mesh
file.write_meshtags(ct, msh.geometry, geometry_xpath=f”/Xdmf/Domain/Grid[@Name=‘{msh.name}’]/Geometry"
TypeError: write_meshtags(): incompatible function arguments. The following argument types are supported:
1. (self: dolfinx.cpp.io.XDMFFile, meshtags: dolfinx.cpp.mesh.MeshTags_int32, geometry_xpath: str = ‘/Xdmf/Domain/Grid/Geometry’, xpath: str = ‘/Xdmf/Domain’) → None

Invoked with: <dolfinx.io.utils.XDMFFile object at 0x7fb9856ea890>, <dolfinx.cpp.mesh.MeshTags_int32 object at 0x7fb984e188b0>, <dolfinx.cpp.mesh.Geometry object at 0x7fb975f326b0>; kwargs: geometry_xpath=“/Xdmf/Domain/Grid[@Name=‘ball_d1’]/Geometry”

any idea why?

You have installed legacy FEniCS, not DOLFINx.
Please install DOLFINx using either conda or apt, as shown in GitHub - FEniCS/dolfinx: Next generation FEniCS problem solving environment

i already worked alot with this version, will it impact my current code?
cant i use what i have now and somehow still use gmsh?

‫בתאריך יום ה׳, 23 במאי 2024 ב-22:27 מאת ‪Jorgen Dokken via FEniCS Project‬‏ <‪notifications@fenicsproject1.discoursemail.com‬‏>:‬

@dokken hi, any chance you reviewed my last comment and can advise?

You can use legacy fenics and Gmsh. However, then I would advice you to use meshio for converting the mesh to xdmf. There are many examples of this across the forum.

is the meshio is only for importing exting files or also to for creating meshes ?
also , do yuo have any doc for how to create a cylinder inside a box with meshio? , similar to this dolfinx/python/demo/demo_gmsh.py at e64178c679d4186ed1c5ab3c2e48b4ff28feef6c · FEniCS/dolfinx · GitHub

also how can i install the meshio
i tried
pip install meshio
sudo apt-get install python-meshio

conda install -c conda-forge meshio

apt install python3-meshio

but nothing helped

What do you mean by

Without the error message you are getting when importing meshio for each of these methods, it is impossible to give you any guidance.
Please note that the way you should install meshio depends on how you installed DOLFINx, so how did you install dolfinx?

Hi, im trying to install meshio since you told me that i can use it with fenics and not fenicx

I dont recall how i installed it but this is the info abount my installetions as i shared above if it helps

Let me know if that helps

Seems like you are using FEniCS from apt, and then you can either install meshio from apt (python3-meshio) or using python3 -m pip install --no-binary=h5py h5py meshio.

Without the error messages you are getting when trying to import meshio, it is impossible to give any further help

this is the error i get after trying what you suggested
Traceback (most recent call last):
File “/home/mirialex/PycharmProjects/fibers/from_community.py”, line 1, in
import meshio
ModuleNotFoundError: No module named ‘meshio’

resolved with sudo apt-get install python3-meshio

but still i would like to get the clarification if in order to use meshio do i need an existing mesh file to created or do i create regulary via the code

There are plenty of tutorials on usage of meshio on the forum, as wells as at: Mesh generation and conversion with GMSH and PYGMSH | Jørgen S. Dokken