Hello everyone,
I have created a 2D curve mesh (1D topological dimension) like this:
I need to use and visualize the normal unit vector, I looked at some post but in these they use a 3D mesh, is there a way to compute the normal unit vector to use in a variational formulation,
Also, here is my code for the mesh generation
import os
from mpi4py import MPI
from petsc4py import PETSc
import numpy as np
import math
import ufl
from basix.ufl import element, mixed_element
from dolfinx import default_real_type, log, plot, mesh
from dolfinx.fem import Function, functionspace, Expression
from dolfinx.fem.petsc import NonlinearProblem
from dolfinx.io import XDMFFile, gmshio
from dolfinx.mesh import CellType, create_unit_square, GhostMode
from dolfinx.nls.petsc import NewtonSolver
from ufl import dx, grad, inner
# Save all logging to file
log.set_output_file("log.txt")
# -
# Next, various model parameters are defined:
dt = 5.0e-05 # time step
# A unit square mesh with 96 cells edges in each direction is created,
# and on this mesh a
# {py:class}`FunctionSpaceBase <dolfinx.fem.FunctionSpaceBase>` `ME` is built
# using a pair of linear Lagrange elements.
try:
import gmsh # type: ignore
except ImportError:
import sys
print("This demo requires gmsh to be installed")
sys.exit(0)
def gmsh_circle(model: gmsh.model, name: str) -> gmsh.model:
"""Create a Gmsh model of a circle.
Args:
model: Gmsh model to add the mesh to.
name: Name (identifier) of the mesh to add.
Returns:
Gmsh model with a circle mesh added.
"""
model.add(name)
model.setCurrent(name)
gmsh.option.setNumber("Mesh.CharacteristicLengthFactor", 0.2)
circle = model.occ.addCircle(0, 0, 0, 1, tag=1)
# Synchronize OpenCascade representation with gmsh model
model.occ.synchronize()
# Add physical marker for cells. It is important to call this
# function after OpenCascade synchronization
model.add_physical_group(dim=1, tags=[circle])
# Generate the mesh
model.mesh.generate(dim=1)
return model
def create_mesh(comm: MPI.Comm, model: gmsh.model, name: str, filename: str, mode: str):
"""Create a DOLFINx from a Gmsh model and output to file.
Args:
comm: MPI communicator top create the mesh on.
model: Gmsh model.
name: Name (identifier) of the mesh to add.
filename: XDMF filename.
mode: XDMF file mode. "w" (write) or "a" (append).
"""
msh, ct, ft = gmshio.model_to_mesh(model, comm, rank=0,gdim=2)
msh.name = name
ct.name = f"{msh.name}_cells"
ft.name = f"{msh.name}_facets"
with XDMFFile(msh.comm, filename, mode) as file:
msh.topology.create_connectivity(0, 1)
file.write_mesh(msh)
file.write_meshtags(ct, msh.geometry, geometry_xpath=f"/Xdmf/Domain/Grid[@Name='{msh.name}']/Geometry")
file.write_meshtags(ft, msh.geometry, geometry_xpath=f"/Xdmf/Domain/Grid[@Name='{msh.name}']/Geometry")
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)
# Create model
model = gmsh.model()
model = gmsh_circle(model, "circle")
model.setCurrent("circle")
create_mesh(MPI.COMM_SELF, model, "circle", f"out_gmsh/mesh_rankCircle_{MPI.COMM_WORLD.rank}.xdmf", "w")
with XDMFFile(MPI.COMM_WORLD, "out_gmsh/mesh_rankCircle_0.xdmf", "r") as xdmf:
mesh = xdmf.read_mesh(name="circle")
mesh.topology.create_connectivity(mesh.topology.dim-1, mesh.topology.dim)
gmsh.finalize
thank you