import pygmsh
import meshio

import argparse

def generate_spherical_mesh(radius=1, meshSize=0.1):
    with pygmsh.geo.Geometry() as geo:
        # Create a spherical (ball) geometry centered at (0,0,0)
        sphere = geo.add_ball([0, 0, 0], radius)
        # Generate a 2D mesh (i.e. surface mesh) of the sphere
        myMesh = geo.generate_mesh(dim=3)
    return myMesh

def write_mfem_mesh(meshData, filename):
    meshio.write(filename, meshData, file_format='gmsh22')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Generate a spherical mesh')
    parser.add_argument('--radius', type=float, default=1.0, help='Radius of the sphere')
    parser.add_argument('--meshSize', type=float, default=0.1, help='Mesh size')
    args = parser.parse_args()

    meshData = generate_spherical_mesh(args.radius, args.meshSize)
    write_mfem_mesh(meshData, 'sphere.msh')