mesh

The mesh module provides mesh generators: methods that return a topology and an accompanying geometry function. Meshes can either be generated on the fly, e.g. rectilinear(), or read from external an externally prepared file, gmsh(), and converted to nutils format. Note that no mesh writers are provided at this point.

nutils.mesh.rectilinear(richshape, periodic=(), name='rect')

rectilinear mesh

nutils.mesh.multipatch(patches, nelems, patchverts=None, name='multipatch')

multipatch rectilinear mesh generator

Generator for a MultipatchTopology and geometry. The MultipatchTopology consists of a set patches, where each patch is a StructuredTopology and all patches have the same number of dimensions.

The patches argument, a numpy.ndarray-like with shape (npatches, 2*ndims) or (npatches,)+(2,)*ndims, defines the connectivity by labelling the patch vertices. For example, three one-dimensional patches can be connected at one edge by:

# connectivity:     3
#                   │
#                1──0──2

patches=[[0,1], [0,2], [0,3]]

Or two two-dimensional patches along an edge by:

# connectivity:  3──4──5
#                │  │  │
#                0──1──2

patches=[[[0,3],[1,4]], [[1,4],[2,5]]]

The geometry is specified by the patchverts argument: a numpy.ndarray-like with shape (nverts,ngeomdims) specifying for each vertex a coordinate. Note that the dimension of the geometry may be higher than the dimension of the patches. The created geometry is a patch-wise linear interpolation of the vertex coordinates. If the patchverts argument is omitted the geometry describes a unit hypercube per patch.

The nelems argument is either an int defining the number of elements per patch per dimension, or a dict with edges (a pair of vertex numbers) as keys and the number of elements (int) as values, with key None specifying the default number of elements. Example:

# connectivity:  3─────4─────5
#                │ 4x3 │ 8x3 │
#                0─────1─────2

patches=[[[0,3],[1,4]], [[1,4],[2,5]]]
nelems={None: 4, (1,2): 8, (4,5): 8, (0,3): 3, (1,4): 3, (2,5): 3}

Since the patches are structured topologies, the number of elements per patch per dimension should be unambiguous. In above example specifying nelems={None: 4, (1,2): 8} will raise an exception because the patch on the right has 8 elements along edge (1,2) and 4 along (4,5).

Example

An L-shaped domain can be generated by:

# connectivity:  2──5
#                │  |
#                1──4─────7     y
#                │  │     │     │
#                0──3─────6     └──x

domain, geom = mesh.multipatch(
  patches=[[0,1,3,4], [1,2,4,5], [3,4,6,7]],
  patchverts=[[0,0], [0,1], [0,2], [1,0], [1,1], [1,2], [3,0], [3,1]],
  nelems={None: 4, (3,6): 8, (4,7): 8})

The number of elements is chosen such that all elements in the domain have the same size.

A topology and geometry describing the surface of a sphere can be generated by creating a multipatch cube surface and inflating the cube to a sphere:

 # connectivity:    3────7
 #                 ╱│   ╱│
 #                2────6 │     y
 #                │ │  │ │     │
 #                │ 1──│─5     │ z
 #                │╱   │╱      │╱
 #                0────4       *────x

 topo, cube = multipatch(
   patches=[
     # The order of the vertices is chosen such that normals point outward.
     [2,3,0,1],
     [4,5,6,7],
     [4,6,0,2],
     [1,3,5,7],
     [1,5,0,4],
     [2,6,3,7],
   ],
   patchverts=tuple(itertools.product(*([[-1,1]]*3))),
   nelems=10,
)
 sphere = cube / function.sqrt((cube**2).sum(0))
Parameters:
  • patches – A numpy.ndarray with shape sequence of patches with each patch being a list of vertex indices.
  • patchverts – A sequence of coordinates of the vertices.
  • nelems – Either an int specifying the number of elements per patch per dimension, or a dict with edges (a pair of vertex numbers) as keys and the number of elements (int) as values, with key None specifying the default number of elements.
Returns:

nutils.mesh.gmsh(fname, name='gmsh')

Gmsh parser

Parser for Gmsh files in .msh format. Only files with physical groups are supported. See the Gmsh manual for details.

Parameters:
  • fname (str) – Path to mesh file.
  • name (str or None) – Name of parsed topology, defaults to ‘gmsh’.
Returns:

nutils.mesh.fromfunc(func, nelems, ndims, degree=1)

piecewise

nutils.mesh.unitsquare(nelems, etype)

Unit square mesh.

Parameters:
  • nelems (int) – Number of elements along boundary
  • etype (str) –

    Type of element used for meshing. Supported are:

    • "square": structured mesh of squares.
    • "triangle": unstructured mesh of triangles.
    • "mixed": unstructured mesh of triangles and squares.
Returns: