Numpy meshgrid to 2D mesh

Hi,

How could I convert a meshgrid generated with numpy to a mesh usable with FEniCSx?

Below is a function for generating an example meshgrid that I would like to use to define the vertices of the mesh.

import numpy as np

def ChebyshevMesh(Nx, Ny, lx, ly):
    # Generate Chebyshev nodes
    x = np.cos (np.pi * np.arange(Nx + 1) / Nx)
    y = np.cos (np.pi * np.arange(Ny + 1) / Ny)
    
    # Scale to the domain
    x = 0.5 * (x + 1) * lx
    y = 0.5 * (y + 1) * ly

    # Generate grid
    xx, yy = np.meshgrid(x, y)

    return (xx, yy)


xx, yy = ChebyshevMesh(30, 15, 8, 3)

Thanks

I would recommend reading: Mesh creation in serial and parallel — FEniCSx Documentation

Thank you, I have got this working now. I’ve left my implementation below if anyone is else is needing to do this.

import numpy as np
import dolfin as fe

def ChebyshevMesh(Lx, Ly, Nx, Ny, refinement = 'xy'):
    # Generate DOLFINx mesh
    mesh = fe.UnitSquareMesh(Nx, Ny)
    coords = mesh.coordinates()

    # Generate Chebyshev nodes
    x = np.cos (np.pi * np.arange(Nx + 1) / Nx)
    y = np.cos (np.pi * np.arange(Ny + 1) / Ny)
    
    # Scale to the domain
    x = 0.5 * (x + 1) * Lx
    y = 0.5 * (y + 1) * Ly

    # Generate meshgrid
    xx, yy = np.meshgrid(x, y)

    # Apply to DOLFINx mesh
    if 'x' in refinement:
        coords[:,0] = xx.flatten()
    else:
        coords[:,0] *= Lx
    if 'y' in refinement:
        coords[:,1] = yy.flatten()
    else:
        coords[:,1] *= Ly

    return mesh