Hello, I am trying to build a hemi sphere and calculate the area by following the tutorial Integration measures, but the code does not give the correct result. Here is my code.
import gmsh
import dolfinx, ufl
from mpi4py import MPI
import numpy as np
R = 1.0
grid_size = 0.2
order = 2
gmsh.initialize()
gmsh.model.add("hemi_sphere")
sphere = gmsh.model.occ.add_sphere(0.0, 0.0, 0.0, R)
box = gmsh.model.occ.add_box(-R-0.1, -R-0.1, 0, 2*R+0.1, 2*R+0.1, R+0.1)
result = gmsh.model.occ.intersect([(3, sphere)], [(3, box)])
gmsh.model.occ.synchronize()
surfaces = gmsh.model.getEntities(dim=2)
for surface in surfaces:
com = gmsh.model.occ.getCenterOfMass(surface[0], surface[1])
if np.allclose(com, [0, 0, 0]):
plane = surface
gmsh.model.addPhysicalGroup(surface[0], [surface[1]], 10, "circle")
elif np.allclose(com, [0.0, 0.0, R*0.5]):
hemi_sphere = surface
gmsh.model.addPhysicalGroup(surface[0], [surface[1]], 20, "hemi_sphere")
# plane should be used here. There is an extra curve in hemi_sphere
boundary = gmsh.model.getBoundary([(plane[0], plane[1])])
gmsh.model.addPhysicalGroup(1, [b[1] for b in boundary], 1, "boundary")
gmsh.option.setNumber("Mesh.CharacteristicLengthMax", grid_size)
gmsh.model.mesh.generate(2)
gmsh.model.mesh.setOrder(order)
mesh, cell_marker, facet_marker = dolfinx.io.gmshio.model_to_mesh(gmsh.model, MPI.COMM_WORLD, 0, gdim=2)
gmsh.finalize()
dx = ufl.Measure("dx", domain=mesh, subdomain_data=cell_marker)
area = 1 * dx(20)
compiled_area = dolfinx.fem.form(area)
local_area = dolfinx.fem.assemble_scalar(compiled_area)
global_area = mesh.comm.allreduce(local_area, op=MPI.SUM)
print(global_area)
If I integrate 1*dx(20)
, the result should be close to 2\pi, but the result is 3.14, it is uncorrect. If I integrate 1*dx
, it is 6.28, but it should be 3\pi.
Does anyone have some idea ? Thanks.