Skip to content

Instantly share code, notes, and snippets.

@jorgensd
Created March 12, 2025 13:09
Show Gist options
  • Save jorgensd/9f6629e112b59ac6108d29f849d13515 to your computer and use it in GitHub Desktop.
Save jorgensd/9f6629e112b59ac6108d29f849d13515 to your computer and use it in GitHub Desktop.
Hermite coordinate element
"""
Hermite as coordinate element in DOLFINx
Author: Jørgen S. Dokken
SPDX License identifier: MIT
"""
from mpi4py import MPI
import dolfinx
import basix.ufl
import numpy as np
import ufl
element = basix.ufl.element(
basix.ElementFamily.serendipity,
basix.CellType.hexahedron,
2,
lagrange_variant=basix.LagrangeVariant.equispaced,
dpc_variant=basix.DPCVariant.simplex_equispaced,
shape=(3,),
)
elements = np.array(
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]],
dtype=np.int64,
)
nodes = np.array(
[
[0, 0, 0],
[1, 0, 0],
[0, 1, 0],
[1, 1, 0],
[0, 0, 1],
[1, 0, 1],
[0, 1, 1],
[1, 1, 1],
[0.5, 0, -0.1],
[0, 0.5, 0],
[0, 0, 0.5],
[1, 0.5, 0],
[1, 0, 0.5],
[0.5, 1, 0],
[0, 1, 0.5],
[1, 1, 0.5],
[0.5, 0, 1],
[0, 0.5, 1],
[1, 0.5, 1],
[0.5, 1, 1],
],
dtype=np.float64,
)
c_el = ufl.Mesh((element))
mesh = dolfinx.mesh.create_mesh(MPI.COMM_WORLD, elements, nodes, c_el)
V = dolfinx.fem.functionspace(mesh, element)
u = dolfinx.fem.Function(V)
u.interpolate(lambda x: (x[0], x[1], x[2]))
V_out = dolfinx.fem.functionspace(mesh, ("Lagrange", 2, (3,)))
u_out = dolfinx.fem.Function(V_out)
u_out.interpolate(u)
with dolfinx.io.VTXWriter(mesh.comm, "output.bp", [u_out]) as writer:
writer.write(0.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment