DOLFINx version: 0.8.0
Traceback (most recent call last):
File "/home/prusso/dolfinx/scratchpad13.py", line 33, in <module>
element = FiniteElement("Lagrange", ufl.hexahedron, 1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: FiniteElement.__init__() missing 3 required positional arguments: 'reference_value_shape', 'pullback', and 'sobolev_space'
import dolfinx
import dolfinx.io
import meshio
import ufl
from mpi4py import MPI
import numpy as np
import basix
from petsc4py import PETSc
from ufl.finiteelement import FiniteElement
import dolfinx.cpp.mesh
print("DOLFINx version:", dolfinx.__version__)
# Convert the VTK mesh to XDMF format
meshio_mesh = meshio.read("hexahedron_mesh.vtk")
meshio.write("hexahedron_mesh.xdmf", meshio_mesh)
# Read the XDMF file using DOLFINx
with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "hexahedron_mesh.xdmf", "r") as xdmf:
mesh = xdmf.read_mesh(name="Grid")
# Define the material properties
κ = 0.83 # Thermal conductivity
ρ = 7850 # Density of the material (kg/m^3)
c_p = 500 # Specific heat capacity (J/(kg*K))
f = 5000 # Heat source term
# Define the function space
element_family = basix.ElementFamily.P
cell_type = basix.CellType.hexahedron
element_degree = 1
# Define the element
element = FiniteElement("Lagrange", ufl.hexahedron, 1)
V = dolfinx.fem.FunctionSpace(mesh, element)
# Define the boundary condition function
u_D = dolfinx.Function(V)
u_D.interpolate(lambda x: np.zeros((x.shape[1], 3))) # Adjust dimensions based on problem
# Define the degrees of freedom to which the condition applies
# You need to define boundary_facets based on your problem
boundary_facets = ...
dofs = dolfinx.fem.locate_dofs_topological(V, mesh.topology.dim - 1, boundary_facets)
# Define the Dirichlet boundary condition
dirichlet_bc = dolfinx.fem.DirichletBC(u_D, dofs)
# Define the test and trial functions
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
# Define the time step and the initial condition
dt = 0.01 # Time step
u_n = dolfinx.Function(V) # Initial condition (zero by default)
# Define the variational problem
F = (ρ * c_p * u / dt) * v * ufl.dx - (ρ * c_p * u_n / dt) * v * ufl.dx + κ * ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx - f * v * ufl.dx
# Define the bilinear and linear forms
a = ufl.lhs(F)
L = ufl.rhs(F)
# Assemble the system matrix
A = dolfinx.fem.assemble_matrix(a)
A.assemble()
# Print the values of the system matrix
print(A.getValuesCSR())
# Create an empty vector for the solution
u = dolfinx.Function(V)
# Time-stepping
t = 0
T = 1 # Final time
while t < T:
# Assemble the right-hand side
b = dolfinx.fem.assemble_vector(L)
# Apply boundary conditions
dolfinx.fem.apply_lifting(b, [a], [[dirichlet_bc]])
b.ghostUpdate(addv=PETSc.InsertMode.ADD, mode=PETSc.ScatterMode.REVERSE)
dolfinx.fem.set_bc(b, [dirichlet_bc])
# Solve the system
dolfinx.la.solve(A, u.vector, b)
# Update the previous solution
with u.vector.localForm() as loc, u_n.vector.localForm() as loc_n:
loc.copy(loc_n)
# Advance the time step
t += dt
print('done...')
Seems to be some difficulties here:
*element = FiniteElement("Lagrange", ufl.hexahedron, 1)*
Not quite sure yet how to get that resolved it.
Just for informational pruposes the VTK file is constructed so far like this:
import numpy as np
import meshio
# Define the dimensions
x_dim, y_dim, z_dim = 8, 8, 8
# Define the number of segments
segments = 15
# Create a grid of points within the dimensions
x = np.linspace(0, x_dim, segments+1)
y = np.linspace(0, y_dim, segments+1)
z = np.linspace(0, z_dim, segments+1)
# Create a 3D meshgrid
X, Y, Z = np.meshgrid(x, y, z)
# Stack the points into an array
points = np.stack((X.flatten(), Y.flatten(), Z.flatten()), axis=-1)
# Define the connectivity for each hexahedral cell
indices = np.arange((segments+1)**3).reshape((segments+1, segments+1, segments+1))
cells = []
for z in range(segments):
for y in range(segments):
for x in range(segments):
# Indices of the eight corners of the hexahedron
corners = [indices[x, y, z], indices[x+1, y, z], indices[x+1, y+1, z], indices[x, y+1, z],
indices[x, y, z+1], indices[x+1, y, z+1], indices[x+1, y+1, z+1], indices[x, y+1, z+1]]
cells.append(corners)
# Create the mesh
mesh = meshio.Mesh(points, {'hexahedron': np.array(cells)})
# Save the mesh to a file
meshio.write("hexahedron_mesh.vtk", mesh)
boundary_facets = …, is a placeholder right now. I guess that I will need to collect those. My aim is to build in the mass matrix M or just to build M and have it accounted for by the system some way.