I am afraid, the first for-loop picks only a subset of triangle/tetra, if there are multiple cell blocks.
Instead of for cell in msh.cells
, there should be for key in mesh.cells_dict
and so on, as it is done for the cell_data. Also a mesh.prune()
might be useful to get rid of orphaned points, since in the current example code, all points are saved for both submeshes.
Hi everyone, hi dokken,
I have a geo file, then a 2D msh file created with gmsh, I converted it in xdmf with
meshio-convert whistle.msh whistle.xdmf
I had the same initial problem
“*** -------------------------------------------------------------------------
*** Error: Unable to recognise cell type.
*** Reason: Unknown value “mixed”.
*** Where: This error was encountered inside XDMFFile.cpp.
*** Process: 0”
So i read this post and i tried different routines to convert in xdmf properly, but each one failed.
When I tried
meshio.write(“whistle.xdmf”, meshio.Mesh(points=msh.points, cells={“line”: msh.cells[“line”]}))
meshio.write(“mf.xdmf”, meshio.Mesh(points=msh.points, cells={“triangle”: msh.cells[“triangle”]},
cell_data={“triangle”: {“name_to_read”: msh.cell_data[“triangle”][“gmsh:physical”]}}))
I get the error
File “<tmp 1>”, line 9, in
meshio.write(“whistle.xdmf”, meshio.Mesh(points=msh.points, cells={“line”: msh.cells[“line”]}))
TypeError: list indices must be integers or slices, not str
I tried with “tetra” "triangle and “line”, each time same result.
I also tried with
for cell in msh.cells:
if cell.type == “triangle”:
triangle_cells = cell.data
elif cell.type == “tetra”:
tetra_cells = cell.data
for key in msh.cell_data_dict[“gmsh:physical”].keys():
if key == “triangle”:
triangle_data = msh.cell_data_dict[“gmsh:physical”][key]
elif key == “tetra”:
tetra_data = msh.cell_data_dict[“gmsh:physical”][key]
tetra_mesh = meshio.Mesh(points=msh.points, cells={“tetra”: tetra_cells},
cell_data={“name_to_read”:[tetra_data]})
triangle_mesh = meshio.Mesh(points=msh.points,
cells=[(“triangle”, triangle_cells)],
cell_data={“name_to_read”: [triangle_data]})
meshio.write(“mesh.xdmf”, tetra_mesh)
I get this mistake :
Traceback (most recent call last):
File “<tmp 1>”, line 13, in
for key in msh.cell_data_dict[“gmsh:physical”].keys():
KeyError: ‘gmsh:physical’
I am not clearly understanding what i am doing, i’m very new to gmsh, meshio and dolfin. Thanks for your answer.
Here is my geo file :
//Dimensions (mm)
h = 7.69;
D=25.0;
delta = 3.0;
e = 0.5;
L1 = 50;
H1 = 50;
L2 = 50;
H2 = 50;
epsilon = 0.5;
// Points
Point(1) = {-L1, H1/2, 0};
Point(2) = {0, H1/2, 0};
Point(3) = {0, delta/2, 0};
Point(4) = {e, delta/2+epsilon, 0};
Point(5) = {e, D/2, 0};
Point(6) = {h+e, D/2, 0};
Point(7) = {h+e, delta/2, 0};
Point(8) = {h+2e, delta/2+epsilon, 0};
Point(9) = {h+2e, H2/2, 0};
Point(10) = {h+2*e+L2, H2/2, 0};
Point(11) = {-L1, -H1/2, 0};
Point(12) = {0, -H1/2, 0};
Point(13) = {0, -delta/2, 0};
Point(14) = {e, -delta/2-epsilon, 0};
Point(15) = {e, -D/2, 0};
Point(16) = {h+e, -D/2, 0};
Point(17) = {h+e, -delta/2, 0};
Point(18) = {h+2e, -delta/2-epsilon, 0};
Point(19) = {h+2e, -H2/2, 0};
Point(20) = {h+2*e+L2, -H2/2, 0};
// Lines
Line(1) = {1,2};
Line(2) = {2,3};
Line(3) = {3,4};
Line(4) = {4,5};
Line(5) = {5,6};
Line(6) = {6,7};
Line(7) = {7,8};
Line(8) = {8,9};
Line(9) = {9,10};
Line(10) = {10,20};
Line(11) = {20,19};
Line(12) = {19,18};
Line(13) = {18,17};
Line(14) = {17,16};
Line(15) = {16,15};
Line(16) = {15,14};
Line(17) = {14,13};
Line(18) = {13,12};
Line(19) = {12,11};
Line(20) = {11,1};
// Loops
Line Loop(1) = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
// Surface
Plane Surface(1) = {1};
Recombine Surface{1};
Transfinite Line{2:8} = 15;
Transfinite Line{12:18} = 15;
First, you need to use physical markers on your lines and surfaces
See for instance this tutorial: https://jsdokken.com/converted_files/tutorial_pygmsh.html
In particular have a look at part 3.
What you should also do is inspect the objects that you are trying to access.
I.e. print msh.cells[“line”] etc.
Thanks a lot for your explanation and furthermore for the tutorial. It worked really well and was very clear
Hello everyone, I have gone through the majority of the above posts and have just ran into issues left and right I was trying to use the code provided by raboynics to convert from a mesh file to a xdmf. but I have gotten the following error code.
Mesh.RandomFactor=1.0e-6;
lc=0.008;
Mesh.CharacteristicLengthFromPoints = 0;
Mesh.CharacteristicLengthExtendFromBoundary = 1;
Mesh.CharacteristicLengthFromCurvature = 0;
Mesh.CharacteristicLengthMin = lc/20;
Mesh.CharacteristicLengthMax = lc;
// bottom motif
a = 2;
b = 1;
c = 0.03;
Point(2) = {-a/2, -b/2, -c/2};
Point(3) = {-a/2, b/2, -c/2};
Point(4) = {a/2, b/2, -c/2};
Point(5) = {a/2, -b/2, -c/2};
Line(100) = {2,3};
Line(101) = {3,4};
Line(102) = {4,5};
Line(103) = {5,2};
Line Loop(200) = {100,101,102,103};
Plane Surface(300) = {200};
// top motif
Point(6) = {-a/2, -b/2, c/2};
Point(7) = {-a/2, b/2, c/2};
Point(8) = {a/2, b/2, c/2};
Point(9) = {a/2, -b/2, c/2};
Line(104) = {6,7};
Line(105) = {7,8};
Line(106) = {8,9};
Line(107) = {9,6};
Line Loop(201) = {104,105,106,107};
Plane Surface(301) = {201};
// vertical lines between the 2 surfaces
Line(108) = {2, 6};
Line(109) = {7, 3};
Line(110) = {4, 8};
Line(111) = {5, 9};
// left
Line Loop(202) = {108,104,109,-100};
Plane Surface(302) = {202};
// front
Line Loop(203) = {103,108,-107,-111};
Plane Surface(303) = {203};
// right
Line Loop(204) = {106,-111,-102,110};
Plane Surface(304) = {204};
// back
Line Loop(205) = {-105,109,101,110};
Plane Surface(305) = {205};
// auxiliary points over top and bottom surface
//rectangle 1
Point(112) = {-a/8,-b/8, c/2};
Point(113) = {-a/8,b/8,c/2};
Point(114) = {a/8, b/8,c/2};
Point(115) = {a/8,-b/8,c/2};
Line(112) = {112,113};
Line(113) = {113,114};
Line(114) = {114,115};
Line(115) = {115,112};
Line Loop(206) = {112,113,114,115};
Plane Surface(306) = {206};
// rectangle 2
Point(116) = {-a/16+a/4,-b/16, -c/2};
Point(117) = {-a/16+a/4,b/16,-c/2};
Point(118) = {a/16+a/4, b/16,-c/2};
Point(119) = {a/16+a/4,-b/16,-c/2};
Line(116) = {116,117};
Line(117) = {117,118};
Line(118) = {118,119};
Line(119) = {119,116};
Line Loop(207) = {116,117,118,119};
Plane Surface(307) = {207};
// rectangle 3
Point(120) = {-a/16+a/4,-b/16, c/2};
Point(121) = {-a/16+a/4,b/16,c/2};
Point(122) = {a/16+a/4, b/16,c/2};
Point(123) = {a/16+a/4,-b/16,c/2};
Line(120) = {120,121};
Line(121) = {121,122};
Line(122) = {122,123};
Line(123) = {123,120};
Line Loop(208) = {120,121,122,123};
Plane Surface(308) = {208};
//rectangle 4
Point(124) = {-a/22,-b/22, -c/2};
Point(125) = {-a/22,b/22,-c/2};
Point(126) = {a/22, b/22,-c/2};
Point(127) = {a/22,-b/22,-c/2};
Line(124) = {124,125};
Line(125) = {125,126};
Line(126) = {126,127};
Line(127) = {127,124};
Line Loop(209) = {124,125,126,127};
Plane Surface(309) = {209};
// Physical Surfaces to build the mesh.
Physical Surface(“bot_box”) = {300};
Physical Surface(“top_box”) = {301};
Physical Surface(“left_box”) = {302};
Physical Surface(“front_box”) = {303};
Physical Surface(“right_box”) = {304};
Physical Surface(“back_box”) = {305};
// rectangular surfaces
Physical Surface(“rect_1”) = {306};
Physical Surface(“rect_2”) = {307};
Physical Surface(“rect_3”) = {308};
Physical Surface(“rect_4”) = {309};
Surface Loop(400) ={300,301,302,304,303,305};
Physical Volume(1) = {400};
File “test1.py”, line 10, in
msh = meshio.read(“test9.msh”)
File “/usr/local/lib/python3.6/dist-packages/meshio/_helpers.py”, line 69, in read
return reader_mapfile_format
File “/usr/local/lib/python3.6/dist-packages/meshio/gmsh/main.py”, line 19, in read
mesh = read_buffer(f)
File “/usr/local/lib/python3.6/dist-packages/meshio/gmsh/main.py”, line 48, in read_buffer
return reader.read_buffer(f, is_ascii, data_size)
File “/usr/local/lib/python3.6/dist-packages/meshio/gmsh/_gmsh41.py”, line 102, in read_buffer
raise ReadError("$Element section not found.")
meshio._exceptions.ReadError: $Element section not found.
Any Ideas?
Please encapsulate the code with ``` to make it render nicely. Similarly with the error message.
As this error seems to only involve meshio, i would suggest reporting an issue at: Sign in to GitHub · GitHub and follow the guidelines there.
I appreciate the help so fast I am new to this and have been sent on a wild goose chase it feels like to make complex geometries in Solidworks (.stl) file and then convert them to (.msh) files to calculate for boundary condition to ultimately get it into fenics
do you have any recommendation for a step by step guild for something like this?
Thank you in advance.
A step by step guide for mesh generation? Gmsh has a large variety of tutorials tutorial · master · gmsh / gmsh · GitLab and there exists Python apis such as pygmsh. See for instance: Mesh generation and conversion with GMSH and PYGMSH | Jørgen S. Dokken
Hello,
I am trying to import an XDMF file into my FEniCS code as the mesh. Can you please give me a piece of advice on the code that needs to be added?
Thank you so much!
See for instance: How to define a mesh in colab as the one imported? - #2 by dokken
Thank you so much!
I used the following codes:
mesh = Mesh()
with XDMFFile(“perfect_contact.xdmf”) as infile:
infile.read(mesh)
But I encountered the following error:
Traceback (most recent call last):
File “periodic_poisson_3d.py”, line 8, in
infile.read(mesh)
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
*** fenics-support@googlegroups.com
*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.
*** -------------------------------------------------------------------------
*** Error: Unable to open XDMF file.
*** Reason: XDMF file “cube.xdmf” does not exist.
*** Where: This error was encountered inside XDMFFile.cpp.
*** Process: 0
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset: unknown
*** -------------------------------------------------------------------------
Any idea on this error?
First of all, you are here stating you are reading in the file “perfect_contact”, but your error message state that you are trying to open “cube.xdmf”
So I believe the code you are showing above is not the code you are running. Have you tried to do ls *.xdmf
in the folder that you are running your script from? What does it show?
Dear Dokken,
Thank you so much for your advice. I had made a mistake about the name of the XDMF file. I sorted it out. But now it gives me a new error message as:
Traceback (most recent call last):
File “periodic_poisson_3d.py”, line 8, in
infile.read(mesh)
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at
*** fenics-support@googlegroups.com
*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.
*** -------------------------------------------------------------------------
*** Error: Unable to recognise cell type.
*** Reason: Unknown value “mixed”.
*** Where: This error was encountered inside XDMFFile.cpp.
*** Process: 0
*** DOLFIN version: 2019.2.0.dev0
*** Git changeset: unknown
*** -------------------------------------------------------------------------
I generate XDMF file using the following command:
meshio-convert in.msh out.xdmf
You need to create distinct xdmf-files for each entity type, as having a Mixed XDMFFile is not parallel friendly. See for instance: Extracting mesh regions for gmsh mesh - #16 by dokken
Where you can replace the msh-file that is read in with the xdmf file you originally had.
Dear Dokken,
I tried to execute the code mentioned in the link to create distinct DXMF files, but I encountered the following error:
Traceback (most recent call last):
File “code.py”, line 15, in
line_mesh = create_mesh(mesh_from_file, “line”, prune_z=True)
File “code.py”, line 8, in create_mesh
for key in mesh.cell_data_dict[“gmsh:physical”].keys() if key==cell_type])
KeyError: ‘gmsh:physical’
Thank you so much.
Please check what the contents of mesh.cell_data_dict is. You only need to add cell data to your mesh if you would like to have markers for each cell (and similarly for facets)
Dear Dokken,
I used this code to import the xdmf file but I encountered this error:
Traceback (most recent call last):
File “code.py”, line 1, in
from fenics import *
File “/usr/lib/python3/dist-packages/fenics/init.py”, line 7, in
from dolfin import *
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/init.py”, line 138, in
from . import parameter
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/parameter/init.py”, line 11, in
from ffc import default_jit_parameters
File “/usr/lib/python3/dist-packages/ffc/init.py”, line 24, in
from ffc.compiler import compile_form, compile_element
File “/usr/lib/python3/dist-packages/ffc/compiler.py”, line 127, in
from ffc.representation import compute_ir
File “/usr/lib/python3/dist-packages/ffc/representation.py”, line 44, in
from ffc.fiatinterface import create_element, reference_cell
File “/usr/lib/python3/dist-packages/ffc/fiatinterface.py”, line 31, in
import FIAT
File “/usr/lib/python3/dist-packages/FIAT/init.py”, line 8, in
from FIAT.finite_element import FiniteElement, CiarletElement # noqa: F401
File “/usr/lib/python3/dist-packages/FIAT/finite_element.py”, line 13, in
from FIAT.polynomial_set import PolynomialSet
File “/usr/lib/python3/dist-packages/FIAT/polynomial_set.py”, line 19, in
from FIAT import expansions
File “/usr/lib/python3/dist-packages/FIAT/expansions.py”, line 12, in
import sympy
File “/usr/lib/python3/dist-packages/sympy/init.py”, line 57, in
from .core import *
File “/usr/lib/python3/dist-packages/sympy/core/init.py”, line 8, in
from .expr import Expr, AtomicExpr, UnevaluatedExpr
File “/usr/lib/python3/dist-packages/sympy/core/expr.py”, line 6, in
from .evalf import EvalfMixin, pure_complex
File “/usr/lib/python3/dist-packages/sympy/core/evalf.py”, line 28, in
from sympy.utilities.iterables import is_sequence
File “/usr/lib/python3/dist-packages/sympy/utilities/init.py”, line 13, in
from .lambdify import lambdify
File “/usr/lib/python3/dist-packages/sympy/utilities/lambdify.py”, line 19, in
from sympy.utilities.decorator import doctest_depends_on
File “/usr/lib/python3/dist-packages/sympy/utilities/decorator.py”, line 11, in
from sympy.utilities.runtests import DependencyError, SymPyDocTests, PyTestReporter
File “/usr/lib/python3/dist-packages/sympy/utilities/runtests.py”, line 22, in
import pdb
File “/usr/lib/python3.8/pdb.py”, line 77, in
import code
File “/mnt/d/research/fenics_codes/meshing/code.py”, line 11, in
meshio.write(“mesh.xdmf”, meshio.Mesh(points=msh.points, cells={“tetra”: msh.cells[“tetra”]}))
TypeError: list indices must be integers or slices, not str
any idea on how to get rid of this error?
Thanks
Please have a careful look at the colab example I posted above.
There I am not accessing cells with a string key, but collecting all cells of tetrahedral elements with:
cells = np.vstack([cell.data for cell in mesh.cells if cell.type==cell_type])
which in turn is sent into the meshio.Mesh
.
Dear Dokken,
Why I am getting the “KeyError: ‘gmsh:physical’” error after running the code?
Please look at one thing at the time.
The first step is simply to extract your mesh data (the set of entities, either lines, triangles or tetrahedrons)
def create_mesh(mesh, cell_type, prune_z=False):
cells = numpy.vstack([cell.data for cell in mesh.cells if cell.type==cell_type])
# Remove z-coordinates from mesh if we have a 2D cell and all points have the same third coordinate
points= mesh.points
if prune_z:
points = points[:,:2]
mesh_new = meshio.Mesh(points=points, cells={cell_type: cells})
return mesh_new
line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("facet_mesh.xdmf", line_mesh)
triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
meshio.write("mesh.xdmf", triangle_mesh)
If this works, it means that you have converted your mesh successfully.
If you have physical markers for your mesh, you should be able to inspect it by printing
print(mesh_from_file.cell_data_dict)
You then need to add the corresponding cell data to the mesh, as shown in the previous post.