How to import tetgen data into dolfinx 0.7.2?

import numpy as np
import sys
import os 
sys.path.append(os.getcwd())
from dolfinx.fem import FunctionSpace, assemble_matrix
from dolfinx import mesh
from ufl import dx, grad, inner, TestFunction, TrialFunction
from mpi4py import MPI
import dolfinx
import numpy as np 
import ufl
from ufl import Cell
import dolfinx.cpp

def read_node_file(filename):
    with open(filename, 'r') as f:
        num_nodes = int(f.readline().split()[0])
        nodes = np.empty((num_nodes, 3))
        for i in range(num_nodes):
            line = f.readline().split()
            nodes[i] = [float(x) for x in line[1:4]]
    return nodes

def read_ele_file(filename):
    with open(filename, 'r') as f:
        num_elements = int(f.readline().split()[0])
        elements = np.empty((num_elements, 4), dtype=int)
        for i in range(num_elements):
            line = f.readline().split()
            elements[i] = [int(x) for x in line[1:5]]
    return elements

def read_face_file(filename):
    with open(filename, 'r') as f:
        num_faces = int(f.readline().split()[0])
        faces = np.empty((num_faces, 3), dtype=int)
        for i in range(num_faces):
            line = f.readline().split()
            faces[i] = [int(x) for x in line[1:4]]
    return faces

def read_edge_file(filename):
    with open(filename, 'r') as f:
        num_edges = int(f.readline().split()[0])
        edges = np.empty((num_edges, 2), dtype=int)
        for i in range(num_edges):
            line = f.readline().split()
            edges[i] = [int(x) for x in line[1:3]]
    return edges

# Usage:
nodes = read_node_file('cylinder.1.node')
elements = read_ele_file('cylinder.1.ele')
faces = read_face_file('cylinder.1.face')
edges = read_edge_file('cylinder.1.edge')

print('nodes~~~')
print(nodes)
print('elements~~~')
print(elements)
print('faces~~~')
print(faces)
print('edges~~~')
print(edges)

A snapshot of the data looks like:

nodes~~~
[[ 4.99378471e+01  2.49229431e+00  1.00000000e+03]
 [ 4.97515373e+01  4.97839212e+00  1.00000000e+03]
 [ 4.94415398e+01  7.45211315e+00  1.00000000e+03]
 [ 4.90086250e+01  9.90730762e+00  0.00000000e+00]
 [ 4.84538651e+01  1.23378696e+01  1.00000000e+03]
 [ 4.84538651e+01  1.23378696e+01  0.00000000e+00]
 ...
 [ 2.91871834e+01  4.05969009e+01  1.00000000e+03]]
elements~~~
[[223  45  49  47]
 [  6 241   3   4]
 [128 173 124 166]
 ...
 [189 196 200   3]
 [ 28  35  27 243]
 [243  31  28  35]]
faces~~~
[[142 240   1]
 [  2 187 188]
 [188 142   1]
 ...
 [ 16 230  25]
 [230  40  25]
 [ 17  19  16]]
edges~~~
[[240 142]
 ...
 [141 142]]

I tried to create a dolfinx mesh from the data but I was not able to do it yet. Is there a way to compose a dolfinx mesh from the data set that was generated on cylinder.stl from tetgen?

Meshio supports converting the data.

However, if you want to read the data directly into dolfinx, i would suggest reading
http://jsdokken.com/FEniCS23-tutorial/src/mesh_generation.html

1 Like
# Convert elements to zero-based indexing
elements = elements - 1

# Now create the mesh
c_el = ufl.Mesh(ufl.VectorElement("Lagrange", ufl.tetrahedron, 1))
domain = dolfinx.mesh.create_mesh(MPI.COMM_WORLD, elements, nodes, c_el)

So I was able to get things through the system without an error like this. Not sure yet if that is 100% percent what is needed but it does seem to work so far.

It is possible to install version dolfinx 0.7.3? I installed dolfinx version 0.8 and the tutorial (tutorial does not work

I change the line
FiniteElement by finiteelement

but I have not found what is the change of
VectorElement

for this reason I would like to install dolfinx 0.7 version

See: What happened to VectorElement? - #2 by dokken
for updated syntax.

The tutorial is being updated, see: