Transitioning from mesh.xml to mesh.xdmf, from dolfin-convert to meshio

Thank you for your help.
Here is the code with proper indentation.

def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    out_mesh = meshio.Mesh(points=mesh.points, cells={cell_type: cells}, cell_data={"name_to_read":[cell_data]})
    if prune_z:
        out_mesh.prune_z_0()
    return out_mesh



msh=meshio.read("bokad2.msh")


triangle_mesh = create_mesh(msh, "triangle", False)
tetra_mesh = create_mesh(msh, "tetra", False)
meshio.write("mesh.xdmf", tetra_mesh)
meshio.write("mf.xdmf", triangle_mesh) 
#print(msh.cell_data_dict)
from dolfin import *
parameters["allow_extrapolation"] = True 
parameters["form_compiler"]["optimize"] = True 

mesh=Mesh()
mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim())
with XDMFFile("mesh.xdmf") as infile:
   infile.read(mesh)
   infile.read(mvc, "name_to_read")
cf = cpp.mesh.MeshFunctionSizet(mesh, mvc)


mvc = MeshValueCollection("size_t", mesh, mesh.topology().dim()-1)
with XDMFFile("mf.xdmf") as infile:
    infile.read(mvc, "name_to_read")   
mf = cpp.mesh.MeshFunctionSizet(mesh, mvc) 
  
ds = Measure("ds", domain=mesh, subdomain_data=mf)

dx=Measure("dx", domain=mesh, subdomain_data=cf)
1 Like

Yes, I have verified with paraview by reading xdmf file. when I have seen the file in wireframe or surface it shows correctly. But, if click for the volume it will not show anything

There are two different files to consider here

  1. mesh.xdmf
  2. mf.xdmf

The mesh.xdmf file contains the cells (tetrahedrons) and corresponding markers, while mf.xdmf will show you the facets of the mesh, colored with the corresponding markers.

Yes, I considered mesh.xdmf only.
In the following figures, you can see that, the file is perfectly matching with the gmsh file when I will see it as a wireframe or surface but not in the case of volume.



1 Like

You need to make your code reproducible for anyone to be able to help you. Please supply a minimal script for generating the mesh.

Sorry, Sir,

Here is the .goe file(since the .msh file is too big in size, I am sending .geo file)

// Gmsh project created by Jørgen S. Dokken 2020
SetFactory("OpenCASCADE");
Box(1) = {0, 0, 0, 1, 1, .285};
Box(2) = {0.4, 0.4, 0.285, .2, .2, .03};
Box(3) = {0., 0, 0.315, 1, 1, .01};
Box(4) = {0.0, 0.0, 0.325, 1, 1, 1};
v() = BooleanFragments{ Volume{1}; Delete;}{ Volume{2,3,4}; Delete; };
Physical Volume("air", 1) ={#v()};
Physical Volume("hBN", 2) ={#v()-1};
Physical Volume("cavity", 3) ={#v()-2};
Physical Volume("substrate", 4) ={#v()-3};
Physical Surface("backgate", 5) = {29};
Physical Surface("patterned_gate", 6) = {18, 23};
1 Like

Try editing the opacity values of your ParaView colormap for the volume plot. With the default values, i.e.
default
The volume marked “1” is shown as completely transparent. If you edit your colormap to make all values opaque, i.e.
opaque
you will see that all volumes are present.

1 Like

To add to @conpierce8 excellent comment, I would suggest changing the colormap to Viridis (matplotlib), as it is a much better color map than Cool to Warm, see: Introduction to the viridis color maps

1 Like

Thank you so much. it is working now.

1 Like

Hello. I have tried this code but I get the following error:
ModuleNotFoundError: No module named ‘gmsh’

I have installed gmsh in fenicsproject as well as in my system, but the error still persists.

How Did you install Gmsh?

i have installed gmsh by downloading the dmg file for macos. i already have the gmsh app and can make a figure.
but when i ran it in my terminal under the fenicsproject environment, i got the ModuleNotFoundError. Then, I entered conda install -c conda-forge gmsh, and it was done. but when i ran the code again, i would still get the modulenotfounderror

You Need to make sure that the gmsh.py file that comes with your downloaded installation is in your $PYTHONPATH

Hello Dokken,

When I do this I get the error “UnboundLocalError: local variable ‘cells’ referenced before assignment”. I could not figure it out.
Thank you so much.

You need to provide a minimal code example that reproduces the error.

Hi @dokken ,
I want to create a triangular subdomain inside my geometry and then fill it with another material.
here is my sample geometry. i’m using splines and bezier in my original geometry if that makes any difference

// Gmsh project created on Mon Feb 07 19:14:50 2022
SetFactory("OpenCASCADE");
//+
Point(1) = {2, 2, 0, 1.0};
//+
Point(2) = {0, 2, 0, 1.0};
//+
Point(3) = {0, 0, 0, 1.0};
//+
Point(4) = {2, 0, 0, 1.0};
//+
Point(5) = {0.5, 1.8, 0, 1.0};
//+
Point(6) = {1.5, 1.8, 0, 1.0};
//+
Point(7) = {1, 1, 0, 1.0};
//+
Line(1) = {2, 1};
//+
Line(2) = {1, 4};
//+
Line(3) = {4, 3};
//+
Line(4) = {3, 2};
//+
Line(5) = {5, 6};
//+
Line(6) = {6, 7};
//+
Line(7) = {7, 5};
//+
Curve Loop(1) = {1, 2, 3, 4};
//+
Curve Loop(2) = {5, 6, 7};
//+
Plane Surface(1) = {1, 2};
//+
Plane Surface(2) = {2};
//+
Physical Surface("main_geometry", 1) = {1};
//+
Physical Surface("filled_geometry", 2) = {2};
//+
Physical Curve("internal_boundary", 1) = {5, 6, 7};
//+
Surface Loop(1) = {1, 2};
//+ 
Volume(1) = {1};
//+
Physical Volume(11) = {1};

Then I used the code you provided above,

msh = meshio.read("mesh27.msh")


def create_mesh(mesh, cell_type, prune_z=False):
    cells = mesh.get_cells_type(cell_type)
    cell_data = mesh.get_cell_data("gmsh:physical", cell_type)
    out_mesh = meshio.Mesh(points=mesh.points, cells={
                           cell_type: cells}, cell_data={"name_to_read": [cell_data]})
    if prune_z:
        out_mesh.prune_z_0()
    return out_mesh


tetra_mesh = create_mesh(msh, "tetra")
triangle_mesh = create_mesh(msh, "triangle")
meshio.write("mesh.xdmf", tetra_mesh)

meshio.write("mf.xdmf", triangle_mesh)

But I have

UnboundLocalError: local variable ‘cells’ referenced before assignment

error.

Please note that your mesh is not a 3D mesh, and you should therefore not use

1 Like

Thank you, I still have the same error

So what does your code look like now?

I meant how does your gmsh conversion script look like?