Want to import already defined physical groups of Microstructure in Gmsh to FEnics

Hi FEniCS Community, Recently i was trying to import already defined physical groups of Micro structure in Gmsh to FEniCS. I defined 3- grains as Region_1 and grain boundaries as Region_2.
I am confused as i converted to (.xml) file no physical groups is showing and not even group ids of physical group.Please clarify way of importing micro structure to FEniCS.

Any help to this will be highly appreciated.

Thanking you.

Please make a minimal reproducible example, and search for related posts on the forum such as Define Subdomains created from .geo into XDMF file into dolfin legacy - #4 by dokken

Can i get example of any micro structure Mesh file, previously you forwarded link is not understandable in my case. Here my question is that, i previously defined physical groups using Gmsh, why i am not able to see in Fenics as i know it supports another formats as i changed to XML file , only micro structure is showing. Where are my previously defined physical groups? I just want to give different properties to grain and grain boundaries, please give the example that can be understandable.

And, please give example taking micro structure as a base, Here i am not talking about inbuilt geometries available in FEniCS library.

As you haven’t provided an example that reproduces your issue, Im afraid I cannot help you any further.

There are plenty of examples on the forum of how to read in physical entities in Dolfin/Dolfinx.

I added mesh.new as my Gmsh file

import numpy as np
from IPython import embed
import dolfin
import numpy
import meshio


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)
    points = mesh.points[:, :2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
                           "name_to_read": [cell_data]})
    return out_mesh


mesh_from_file = meshio.read("meshnew.msh")

line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("mf.xdmf", line_mesh)
triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
meshio.write("mesh.xdmf", triangle_mesh)

assert np.allclose(line_mesh.points, triangle_mesh.points)
vertices_in_triangles = np.unique(
    triangle_mesh.cells_dict["triangle"].reshape(-1))
print(vertices_in_triangles)

vertices_in_lines = np.unique(line_mesh.cells_dict["line"].reshape(-1))
print(vertices_in_lines)

ERROR:

/home/scholar/.local/lib/python3.8/site-packages/h5py/__init__.py:36: UserWarning: h5py is running against HDF5 1.10.4 when it was built against 1.14.2, this may cause problems
  _warn(("h5py is running against HDF5 {0} when it was built against {1}, "

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[22], line 20
     17 mesh_from_file = meshio.read("meshnew.msh")
     19 line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
---> 20 meshio.write("mf.xdmf", line_mesh)
     21 triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
     22 meshio.write("mesh.xdmf", triangle_mesh)

File ~/.local/lib/python3.8/site-packages/meshio/_helpers.py:188, in write(filename, mesh, file_format, **kwargs)
    185         pass
    187 # Write
--> 188 return writer(filename, mesh, **kwargs)

File ~/.local/lib/python3.8/site-packages/meshio/xdmf/main.py:546, in write(*args, **kwargs)
    545 def write(*args, **kwargs):
--> 546     XdmfWriter(*args, **kwargs)

File ~/.local/lib/python3.8/site-packages/meshio/xdmf/main.py:338, in XdmfWriter.__init__(self, filename, mesh, data_format, compression, compression_opts)
    335 def __init__(
    336     self, filename, mesh, data_format="HDF", compression="gzip", compression_opts=4
    337 ):
--> 338     import h5py
    340     if data_format not in ["XML", "Binary", "HDF"]:
    341         raise WriteError(
    342             "Unknown XDMF data format "
    343             f"'{data_format}' (use 'XML', 'Binary', or 'HDF'.)"
    344         )

File ~/.local/lib/python3.8/site-packages/h5py/__init__.py:56
     51 _register_lzf()
     54 # --- Public API --------------------------------------------------------------
---> 56 from . import h5a, h5d, h5ds, h5f, h5fd, h5g, h5r, h5s, h5t, h5p, h5z, h5pl
     58 from ._hl import filters
     59 from ._hl.base import is_hdf5, HLObject, Empty

File h5py/h5fd.pyx:222, in init h5py.h5fd()

ValueError: `open' and/or `close' methods are not defined (`open' and/or `close' methods are not defined)

Use “```” when posting the error message, otherwise it is difficult to read it.

You error is clearly

ERROR:

/home/scholar/.local/lib/python3.8/site-packages/h5py/init.py:36: UserWarning: h5py is running against HDF5 1.10.4 when it was built against 1.14.2, this may cause problems
_warn(("h5py is running against HDF5 {0} when it was built against {1}, "

Double check if you have a system installation of h5py and hdf5: if that is the case, just remove the installation from $HOME/.local

The previous error was resolved however
Xdmf file is not showing any regions and h5py is not opening. What will be the reason?

As you haven’t supplied the msh file, it is really hard to give you any guidance.

Its a little big file so i didn’t supplied here, any other way to share other than this platform as i tried however, this particular message box is limited to some range.

Good evening,
Hope you are doing well.

Hi, My name is Himalaya Singh. I contacted yesterday through the Fenics Community Website.

This is my .msh file you were asking for.

Himalaya singh.

(Attachment meshnew.msh is missing)

Cant you share your Gmsh geo file or Gmsh Python script?

cl1=10.00000;

Point(1)={0.000000000000,0.000000000000,0.000000000000,10.000000000000};
Point(2)={0.000000000000,142.396746892315,0.000000000000,10.000000000000};
Point(3)={0.000000000000,145.466715498889,0.000000000000,10.000000000000};
Point(4)={0.000000000000,200.000000000000,0.000000000000,10.000000000000};
Point(5)={77.743845982826,111.656425285816,0.000000000000,10.000000000000};
Point(6)={78.755203779039,114.726393892391,0.000000000000,10.000000000000};
Point(7)={80.725851100509,112.331578854362,0.000000000000,10.000000000000};
Point(8)={115.633749225333,0.000000000000,0.000000000000,10.000000000000};
Point(9)={118.615754343016,0.000000000000,0.000000000000,10.000000000000};
Point(10)={200.000000000000,0.000000000000,0.000000000000,10.000000000000};
Point(11)={200.000000000000,189.447515960198,0.000000000000,10.000000000000};
Point(12)={200.000000000000,191.842330998227,0.000000000000,10.000000000000};
Point(13)={200.000000000000,200.000000000000,0.000000000000,10.000000000000};


Line(1)={9,10};
Line(2)={10,11};
Line(3)={11,7};
Line(4)={7,9};
Line(5)={2,1};
Line(6)={1,8};
Line(7)={8,5};
Line(8)={5,2};
Line(9)={13,4};
Line(10)={4,3};
Line(11)={3,6};
Line(12)={6,12};
Line(13)={12,13};
Line(14)={8,9};
Line(16)={7,5};
Line(19)={6,7};
Line(21)={11,12};
Line(22)={3,2};
Line(24)={5,6};
Line(28)={5,7};


Curve Loop(1) = {1 , 2 , 3 , 4};
Curve Loop(2) = {5 , 6 , 7 , 8};
Curve Loop(3) = {9 , 10 , 11 , 12 , 13};
Curve Loop(4) = {14 , -4 , 16 , -7};
Curve Loop(5) = {-12 , 19 , -3 , 21};
Curve Loop(6) = {22 , -8 , 24 , -11};
Curve Loop(7) = {-19 , -24 , 28};


Physical Line("Right",1) = {2,13,21};
Physical Line("Left",2) = {5,10,22};
Physical Line("Top",3) = {9};
Physical Line("Bottom",4) = {1,6,14};


Plane Surface(1) = {1};
Plane Surface(2) = {2};
Plane Surface(3) = {3};
Plane Surface(4) = {4};
Plane Surface(5) = {5};
Plane Surface(6) = {6};
Plane Surface(7) = {7};


Physical Surface ("Gr1",5) = {1};
Physical Surface ("Gr2",6) = {2};
Physical Surface ("Gr3",7) = {3};
Physical Surface ("Gb",8) = {4,5,6,7};

I didn’t received any update.

Is there any update to my problem, i asked earlier ?

@Himalaya_singh9510 as an administrator of this forum, before you send any further reminders I would urge you to keep in mind that:

  1. everyone who is answering is doing so in their own free time, on top of everything else that is already going on in their professional life.
  2. in Europe and US this time (Christmas) is often a period of vacation, so users (including the two people who previously replied to you) may not be connecting as often as during the rest of the year.
1 Like

I cannot reproduce your issue, as the following files produce:

cl1=10.00000;

Point(1)={0.000000000000,0.000000000000,0.000000000000,10.000000000000};
Point(2)={0.000000000000,142.396746892315,0.000000000000,10.000000000000};
Point(3)={0.000000000000,145.466715498889,0.000000000000,10.000000000000};
Point(4)={0.000000000000,200.000000000000,0.000000000000,10.000000000000};
Point(5)={77.743845982826,111.656425285816,0.000000000000,10.000000000000};
Point(6)={78.755203779039,114.726393892391,0.000000000000,10.000000000000};
Point(7)={80.725851100509,112.331578854362,0.000000000000,10.000000000000};
Point(8)={115.633749225333,0.000000000000,0.000000000000,10.000000000000};
Point(9)={118.615754343016,0.000000000000,0.000000000000,10.000000000000};
Point(10)={200.000000000000,0.000000000000,0.000000000000,10.000000000000};
Point(11)={200.000000000000,189.447515960198,0.000000000000,10.000000000000};
Point(12)={200.000000000000,191.842330998227,0.000000000000,10.000000000000};
Point(13)={200.000000000000,200.000000000000,0.000000000000,10.000000000000};


Line(1)={9,10};
Line(2)={10,11};
Line(3)={11,7};
Line(4)={7,9};
Line(5)={2,1};
Line(6)={1,8};
Line(7)={8,5};
Line(8)={5,2};
Line(9)={13,4};
Line(10)={4,3};
Line(11)={3,6};
Line(12)={6,12};
Line(13)={12,13};
Line(14)={8,9};
Line(16)={7,5};
Line(19)={6,7};
Line(21)={11,12};
Line(22)={3,2};
Line(24)={5,6};
Line(28)={5,7};


Curve Loop(1) = {1 , 2 , 3 , 4};
Curve Loop(2) = {5 , 6 , 7 , 8};
Curve Loop(3) = {9 , 10 , 11 , 12 , 13};
Curve Loop(4) = {14 , -4 , 16 , -7};
Curve Loop(5) = {-12 , 19 , -3 , 21};
Curve Loop(6) = {22 , -8 , 24 , -11};
Curve Loop(7) = {-19 , -24 , 28};


Physical Line("Right",1) = {2,13,21};
Physical Line("Left",2) = {5,10,22};
Physical Line("Top",3) = {9};
Physical Line("Bottom",4) = {1,6,14};


Plane Surface(1) = {1};
Plane Surface(2) = {2};
Plane Surface(3) = {3};
Plane Surface(4) = {4};
Plane Surface(5) = {5};
Plane Surface(6) = {6};
Plane Surface(7) = {7};


Physical Surface ("Gr1",5) = {1};
Physical Surface ("Gr2",6) = {2};
Physical Surface ("Gr3",7) = {3};
Physical Surface ("Gb",8) = {4,5,6,7};

and

import numpy as np
import meshio


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)
    points = mesh.points[:, :2] if prune_z else mesh.points
    out_mesh = meshio.Mesh(points=points, cells={cell_type: cells}, cell_data={
                           "name_to_read": [cell_data]})
    return out_mesh


mesh_from_file = meshio.read("meshnew.msh")

line_mesh = create_mesh(mesh_from_file, "line", prune_z=True)
meshio.write("mf.xdmf", line_mesh)
triangle_mesh = create_mesh(mesh_from_file, "triangle", prune_z=True)
meshio.write("mesh.xdmf", triangle_mesh)

assert np.allclose(line_mesh.points, triangle_mesh.points)
vertices_in_triangles = np.unique(
    triangle_mesh.cells_dict["triangle"].reshape(-1))
print(vertices_in_triangles)

vertices_in_lines = np.unique(line_mesh.cells_dict["line"].reshape(-1))
print(vertices_in_lines)

executed in

docker run -ti --network=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v $(pwd):/root/shared -w /root/shared --rm  ghcr.io/scientificcomputing/fenics-gmsh:2023-11-15

produces:

1 Like

Thanks for your help. Now i can able to visualize the regions. As i am new to FEniCS, i am finding out how to give material properties to grain and grain boundaries, by using the same regions as i defined in (.geo file). Can you help me out with this particular problem?

I would strongly suggest using FEniCSx if you are new to FEniCS, as it is the recommended tool: The new DOLFINx solver is now recommended over DOLFIN

There we have demos illustrating this:
https://jsdokken.com/dolfinx-tutorial/chapter3/subdomains.html#subdomains-defined-from-external-mesh-data

Thanks. This particular document i have already been visited and read before. I did with inbuilt mesh and i know how to give material properties for simple domains like rectangle and square, which is Created by Fenics library itself and i know for multiple material also. My question is that, i want to ask how to implement properties to grains and grain boundaries as its domain is very different from inbuilt meshes that can be directly called.
can you give me a little understandable solution to this?