Please note that you can reproduce the error simply by defining the variational residual (and remove anything related to adios4dolfinx:
from mpi4py import MPI
from petsc4py import PETSc
from dolfinx import default_scalar_type, mesh
from dolfinx.fem import (Constant, dirichletbc, Function, form,
FunctionSpace, locate_dofs_topological)
from ufl import (FacetNormal, VectorElement, Identity, split, MixedElement, TestFunctions,
grad, inner, variable, tr, det, derivative, outer, inv, exp, dx)
from basix.ufl import element
import numpy as np
# Make Mesh
L = 5
W = 2.5
H = 1
domain = mesh.create_box(MPI.COMM_WORLD, [[0.0, 0.0, 0.0], [L, W, H]], [
20, 10, 5], mesh.CellType.tetrahedron)
# Identify Boundary Facets
def side1(x):
return np.isclose(x[0], 0)
side1_facets = mesh.locate_entities_boundary(
domain, domain.topology.dim - 1, side1)
# Marking Facets
marked_facets = np.hstack([side1_facets])
marked_values = np.hstack([np.full_like(side1_facets, 3)])
sorted_facets = np.argsort(marked_facets)
facet_tag = mesh.meshtags(domain, domain.topology.dim - 1,
marked_facets[sorted_facets], marked_values[sorted_facets])
# Unit vectors (needed to set up active stress)
# `Quadrature` in original, `Lagrange`
v_cg = VectorElement("Quadrature", domain.ufl_cell(), 2)
FiberSpace = FunctionSpace(domain, v_cg)
f_0 = Function(FiberSpace)
s_0 = Function(FiberSpace)
n_0 = Function(FiberSpace)
# Loading Quadrature Basis (Important)
# Some variables and Function Spaces
n_mesh = FacetNormal(domain)
V = VectorElement("Lagrange", domain.ufl_cell(), 2)
P = element("Lagrange", domain.basix_cell(), 1)
mixed_el = MixedElement([V, P])
W = FunctionSpace(domain, mixed_el) # mixed function space
w = Function(W)
(u, p) = split(w) # trial functions
(du, dp) = TestFunctions(W)
d = len(u)
I = Identity(d)
F = variable(I + grad(u)) # Deformation gradient
C = F.T*F
B = F*F.T
# Invariants of deformation tensors
I_1 = tr(C)
J = det(F)
# Material parameters
eta = 0.1
rho = Constant(domain, PETSc.ScalarType(1000.0)) # kg
a = 228.0
a_f = 116.85
b = 7.780
b_f = 11.83425 # dimensionless
f = F*f_0
s = F*s_0
n = F*n_0
# Invariants
I_4f = inner(C*f_0, f_0)
I_4s = inner(C*s_0, s_0)
I_4n = inner(C*n_0, n_0)
# Place Boundary Conditions
u_bc = np.array([0, 0, 0], dtype=default_scalar_type)
V0 = W.sub(0)
Q, _ = V0.collapse()
u_bc = Function(Q)
base_dofs = locate_dofs_topological((V0, Q), facet_tag.dim, facet_tag.find(3))
bcs = [dirichletbc(u_bc, base_dofs, V0)]
# Equations
p0 = Constant(domain, PETSc.ScalarType(10))
T_a = Constant(domain, PETSc.ScalarType(2))
# Stress relations
# Passive part
passive_cauchy_stress = a*exp(b*(I_1 - 3))*B - p*I + \
2*a_f*(I_4f-1)*exp(b_f*pow(I_4f - 1, 2))*outer(f, f)
P_p = J*passive_cauchy_stress*inv(F).T
# Active part
active_cauchy_stress = T_a*(outer(f, f)+eta*outer(s, s)+eta*outer(n, n))
P_a = J*active_cauchy_stress*inv(F.T)
P = P_p + P_a
eq = inner(P, grad(du))*dx + inner(J-1, dp)*dx
Jac = derivative(eq, w)
F = form(eq)
and with the current main branch of DOLFINx/UFL/Basix, I cannot reproduce this error:
from mpi4py import MPI
from petsc4py import PETSc
from dolfinx import default_scalar_type, mesh
from dolfinx.fem import (Constant, dirichletbc, Function, form,
functionspace, locate_dofs_topological)
from ufl import (FacetNormal, Identity, split, TestFunctions,
grad, inner, variable, tr, det, derivative, outer, inv, exp, dx)
from basix.ufl import element, mixed_element, quadrature_element
import numpy as np
# Make Mesh
L = 5
W = 2.5
H = 1
domain = mesh.create_box(MPI.COMM_WORLD, [[0.0, 0.0, 0.0], [L, W, H]], [
20, 10, 5], mesh.CellType.tetrahedron)
# Identify Boundary Facets
def side1(x):
return np.isclose(x[0], 0)
side1_facets = mesh.locate_entities_boundary(
domain, domain.topology.dim - 1, side1)
# Marking Facets
marked_facets = np.hstack([side1_facets])
marked_values = np.hstack([np.full_like(side1_facets, 3)])
sorted_facets = np.argsort(marked_facets)
facet_tag = mesh.meshtags(domain, domain.topology.dim - 1,
marked_facets[sorted_facets], marked_values[sorted_facets])
# Unit vectors (needed to set up active stress)
# `Quadrature` in original, `Lagrange`
v_cg = quadrature_element(domain.topology.cell_name(),
degree=2, value_shape=(domain.geometry.dim,))
FiberSpace = functionspace(domain, v_cg)
f_0 = Function(FiberSpace)
s_0 = Function(FiberSpace)
n_0 = Function(FiberSpace)
# Loading Quadrature Basis (Important)
# Some variables and Function Spaces
n_mesh = FacetNormal(domain)
V = element("Lagrange", domain.topology.cell_name(),
2, shape=(domain.geometry.dim,))
P = element("Lagrange", domain.topology.cell_name(), 1)
mixed_el = mixed_element([V, P])
W = functionspace(domain, mixed_el) # mixed function space
w = Function(W)
(u, p) = split(w) # trial functions
(du, dp) = TestFunctions(W)
d = len(u)
I = Identity(d)
F = variable(I + grad(u)) # Deformation gradient
C = F.T*F
B = F*F.T
# Invariants of deformation tensors
I_1 = tr(C)
J = det(F)
# Material parameters
eta = 0.1
rho = Constant(domain, PETSc.ScalarType(1000.0)) # kg
a = 228.0
a_f = 116.85
b = 7.780
b_f = 11.83425 # dimensionless
f = F*f_0
s = F*s_0
n = F*n_0
# Invariants
I_4f = inner(C*f_0, f_0)
I_4s = inner(C*s_0, s_0)
I_4n = inner(C*n_0, n_0)
# Place Boundary Conditions
u_bc = np.array([0, 0, 0], dtype=default_scalar_type)
V0 = W.sub(0)
Q, _ = V0.collapse()
u_bc = Function(Q)
base_dofs = locate_dofs_topological((V0, Q), facet_tag.dim, facet_tag.find(3))
bcs = [dirichletbc(u_bc, base_dofs, V0)]
# Equations
p0 = Constant(domain, PETSc.ScalarType(10))
T_a = Constant(domain, PETSc.ScalarType(2))
# Stress relations
# Passive part
passive_cauchy_stress = a*exp(b*(I_1 - 3))*B - p*I + \
2*a_f*(I_4f-1)*exp(b_f*pow(I_4f - 1, 2))*outer(f, f)
P_p = J*passive_cauchy_stress*inv(F).T
# Active part
active_cauchy_stress = T_a*(outer(f, f)+eta*outer(s, s)+eta*outer(n, n))
P_a = J*active_cauchy_stress*inv(F.T)
P = P_p + P_a
eq = inner(P, grad(du))*dx + inner(J-1, dp)*dx
Jac = derivative(eq, w)
F = form(eq)
Jac = form(eq)
Either I would:
- Change to the main branches of basix, ufl, ffcx and Dolfinx.
- Make the problem smaller, so it is easier to pinpoint where you have an error in your code. I do not have bandwidth to do this, and has to be your job.