ndiffusion.assign_materials

ndiffusion.assign_materials(mesh, spec, copy=False)[source]

Paint material indices onto an existing mesh.

Parameters:
  • mesh (UnstructuredMesh2D) – Geometry to assign materials to.

  • spec (callable, dict, or sequence) –

    callable(x, y) -> int

    Evaluated at each cell centroid, so the assignment agrees with the centroids the FVM solver itself uses.

    dict

    Remaps the material ids already on the mesh. Keys are either region ids (int) or region names (str, for a mesh from load_gmsh()). Every region present in the mesh must appear as a key - a partial mapping is rejected rather than silently leaving some cells on their old index.

    sequence of int

    Used directly; must have one entry per cell. This is the fast path on a large mesh - the callable form has to make one Python call per cell, so vectorizing it with cell_centroids() is roughly twice as quick:

    cx, cy = nd.cell_centroids(mesh)
    nd.assign_materials(mesh, np.where(np.asarray(cx) > x0, 1, 0))
    

  • copy (bool, optional) – False (default) rewrites mesh.material_id in place and returns mesh, which is safe to do between solver constructions: each solver takes its own copy of the mesh at construction, so repainting afterward cannot disturb one already built. True leaves the input untouched and returns a new mesh.

Returns:

The painted mesh (mesh itself unless copy is set).

Return type:

UnstructuredMesh2D

Raises:
  • ValueError – If the spec does not cover every region, a name is unknown, a sequence has the wrong length, or any resulting index is negative.

  • TypeError – If spec is not a callable, dict, or sequence.

Examples

One geometry, three layouts:

mesh = nd.load_gmsh("core.msh")
nd.assign_materials(mesh, {"fuel": 0, "reflector": 1})
unrodded = nd.KEigenSolverUnstructured2D(mats, mesh, bc).solve()

nd.assign_materials(mesh, {"fuel": 0, "reflector": 1, "rod": 2})
rodded = nd.KEigenSolverUnstructured2D(mats, mesh, bc).solve()

nd.assign_materials(mesh, lambda x, y: 0 if x * x + y * y < R * R else 1)
annular = nd.KEigenSolverUnstructured2D(mats, mesh, bc).solve()