So I’ve exported the mesh of a 10 mm x 10 mm x 10 mm box (meshed with gmsh within FreeCAD). As FreeCAD asked me to give self defined IDs, I have chosen individual IDs (2,4,8) for “Face”, “Solid” and “Top” respectively:

(FreeCAD users must know to use first order meshes, see Using XDMF Mesh Export File(s) for FEniCS - FreeCAD Forum)
Because of this post:
https://fenicsproject.discourse.group/t/gmsh-mesh-volume-and-surface-is-worth-0/4944
I thought that three meshes would be neccessary to have a successful workaround to handle subdomain data. Previously I used the “main mesh” xml file, a seperate file for “physical_regions” and another seperate file for “facet_regions”.
After exporting in FreeCAD I got one XDMF Mesh file I will show in this post below. A test by using the “assemble” function gives accurate values:
from dolfin import *
mesh = Mesh()
with XDMFFile("box_test.xdmf") as infile:
infile.read(mesh)
physical_reg = MeshValueCollection('size_t', mesh, mesh.topology().dim())
with XDMFFile("box_test.xdmf") as infile:
infile.read(physical_reg, "Solid")
cf = cpp.mesh.MeshFunctionSizet(mesh, physical_reg)
facet_reg = MeshValueCollection('size_t', mesh, mesh.topology().dim()-1)
with XDMFFile("box_test.xdmf") as infile:
infile.read(facet_reg, "Face")
ff = cpp.mesh.MeshFunctionSizet(mesh, facet_reg)
facet_reg2 = MeshValueCollection('size_t', mesh, mesh.topology().dim()-1)
with XDMFFile("box_test.xdmf") as infile:
infile.read(facet_reg2, "Top")
ff2 = cpp.mesh.MeshFunctionSizet(mesh, facet_reg2)
dx = Measure("dx", domain=mesh, subdomain_data=cf, metadata={'quadrature_degree': 2})
ds_front = Measure("ds", domain=mesh, subdomain_data=ff, metadata={'quadrature_degree': 2})
ds_top = Measure("ds", domain=mesh, subdomain_data=ff2, metadata={'quadrature_degree': 2})
vol = assemble(1*dx(4))
surf = assemble(1*ds_front(2))
surf_top = assemble(1*ds_top(subdomain_id=8))
print("total volume: " + str(vol))
print("front surface: " + str(surf))
print("top surface: " + str(surf_top))
which seems to look good so far. The output gives:
total volume: 999.9999999999993
front surface: 100.00000000000003
top surface: 100.00000000000003
In the next few days I may try to extend the code to solve a simple example to solve Poisson equation to give a workaround for interested who want to do the same thing someday.
So if you don’t see anything “strange” in the XDMF file, everything should be fine so far!
I will be in touch if there are any problems after all.
box_test.xdmf:
<?xml version="1.0"?>
<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>
<Xdmf version="3.0"><Domain><Grid Name="base_mesh" GridType="Uniform"><Topology TopologyType="tetrahedron" NumberOfElements="24" NodesPerElement="4"><DataItem NumberType="UInt" Dimensions="24 4" Format="XML">9 11 10 12
8 13 11 10
11 10 13 9
8 10 11 12
1 0 8 10
0 2 8 13
10 0 13 4
3 11 8 2
1 8 3 12
11 13 2 6
4 13 9 6
6 11 9 7
10 4 9 5
11 7 3 12
12 9 7 5
12 1 10 5
13 8 0 10
8 2 11 13
10 8 1 12
13 9 10 4
11 9 13 6
11 3 8 12
7 11 9 12
12 10 9 5</DataItem></Topology><Geometry GeometryType="XYZ"><DataItem Dimensions="14 3" Format="XML">0.000000 0.000000 10.000000
0.000000 0.000000 0.000000
0.000000 10.000000 10.000000
0.000000 10.000000 0.000000
10.000000 0.000000 10.000000
10.000000 0.000000 0.000000
10.000000 10.000000 10.000000
10.000000 10.000000 0.000000
0.000000 5.000000 5.000000
10.000000 5.000000 5.000000
5.000000 0.000000 5.000000
5.000000 10.000000 5.000000
5.000000 5.000000 0.000000
5.000000 5.000000 10.000000</DataItem></Geometry></Grid><Grid Name="Face_mesh" GridType="Uniform"><Topology TopologyType="triangle" NumberOfElements="24" NodesPerElement="3"><DataItem NumberType="UInt" Dimensions="24 3" Format="XML">0 1 10
4 0 10
1 5 10
5 4 10
0 13 2
4 13 0
2 13 6
6 13 4
1 0 8
0 2 8
3 1 8
2 3 8
5 9 4
4 9 6
7 9 5
6 9 7
2 11 3
6 11 2
3 11 7
7 11 6
1 3 12
5 1 12
3 7 12
7 5 12</DataItem></Topology><Geometry Reference="XML">/Xdmf/Domain/Grid/Geometry</Geometry><Attribute AttributeType="Scalar" Center="Cell" Name="Face"><DataItem Dimensions="24 1" Format="XML">2
2
2
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0</DataItem></Attribute></Grid><Grid Name="Solid_mesh" GridType="Uniform"><Topology TopologyType="tetrahedron" NumberOfElements="24" NodesPerElement="4"><DataItem NumberType="UInt" Dimensions="24 4" Format="XML">9 11 10 12
8 13 11 10
11 10 13 9
8 10 11 12
1 0 8 10
0 2 8 13
10 0 13 4
3 11 8 2
1 8 3 12
11 13 2 6
4 13 9 6
6 11 9 7
10 4 9 5
11 7 3 12
12 9 7 5
12 1 10 5
13 8 0 10
8 2 11 13
10 8 1 12
13 9 10 4
11 9 13 6
11 3 8 12
7 11 9 12
12 10 9 5</DataItem></Topology><Geometry Reference="XML">/Xdmf/Domain/Grid/Geometry</Geometry><Attribute AttributeType="Scalar" Center="Cell" Name="Solid"><DataItem Dimensions="24 1" Format="XML">4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4</DataItem></Attribute></Grid><Grid Name="Top_mesh" GridType="Uniform"><Topology TopologyType="triangle" NumberOfElements="24" NodesPerElement="3"><DataItem NumberType="UInt" Dimensions="24 3" Format="XML">0 1 10
4 0 10
1 5 10
5 4 10
0 13 2
4 13 0
2 13 6
6 13 4
1 0 8
0 2 8
3 1 8
2 3 8
5 9 4
4 9 6
7 9 5
6 9 7
2 11 3
6 11 2
3 11 7
7 11 6
1 3 12
5 1 12
3 7 12
7 5 12</DataItem></Topology><Geometry Reference="XML">/Xdmf/Domain/Grid/Geometry</Geometry><Attribute AttributeType="Scalar" Center="Cell" Name="Top"><DataItem Dimensions="24 1" Format="XML">0
0
0
0
8
8
8
8
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0</DataItem></Attribute></Grid></Domain></Xdmf>