Hi,
When I tried to implement the Subdomain on built-in meshes Tutorial demo on CoLab following Defining subdomains for different materials — FEniCSx tutorial, the following error occurs becasue of the failure of kappa * grad(u). kappa is a vector and grad(u) is a matrix here. Any idea here how to solve this problem? Thanks!
from dolfinx import default_scalar_type
from dolfinx.fem import (Constant, dirichletbc, Function, FunctionSpace, assemble_scalar,
form, locate_dofs_geometrical, locate_dofs_topological)
from dolfinx.fem.petsc import LinearProblem
from dolfinx.io import XDMFFile, gmshio
from dolfinx.mesh import create_unit_square, locate_entities
from dolfinx.plot import vtk_mesh
from ufl import (SpatialCoordinate, TestFunction, TrialFunction,
dx, grad, inner)
from mpi4py import MPI
import meshio
import gmsh
import numpy as np
import pyvista
pyvista.start_xvfb()
mesh = create_unit_square(MPI.COMM_WORLD, 10, 10)
Q = dolfinx.fem.functionspace(mesh, ("DG", 0, (mesh.geometry.dim,)))
def Omega_0(x):
return x[1] <= 0.5
def Omega_1(x):
return x[1] >= 0.5
kappa = Function(Q)
cells_0 = locate_entities(mesh, mesh.topology.dim, Omega_0)
cells_1 = locate_entities(mesh, mesh.topology.dim, Omega_1)
kappa.x.array[cells_0] = np.full_like(cells_0, 1, dtype=default_scalar_type)
kappa.x.array[cells_1] = np.full_like(cells_1, 0.1, dtype=default_scalar_type)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
u, v = TrialFunction(V), TestFunction(V)
a = inner(kappa * grad(u), grad(v)) * dx
# x = SpatialCoordinate(mesh)
# L = Constant(mesh, default_scalar_type(1)) * v * dx
# dofs = locate_dofs_geometrical(V, lambda x: np.isclose(x[0], 0))
# bcs = [dirichletbc(default_scalar_type(1), dofs, V)]
The error information is
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-b35bd3cd4c0d> in <cell line: 40>()
38 V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1, (mesh.geometry.dim,)))
39 u, v = TrialFunction(V), TestFunction(V)
---> 40 a = inner(kappa * grad(u), grad(v)) * dx
41 # x = SpatialCoordinate(mesh)
42 # L = Constant(mesh, default_scalar_type(1)) * v * dx
1 frames
/usr/local/lib/python3.10/dist-packages/ufl/exproperators.py in _mult(a, b)
157
158 else:
--> 159 raise ValueError(f"Invalid ranks {r1} and {r2} in product.")
160
161 # TODO: I think applying as_tensor after index sums results in
ValueError: Invalid ranks 1 and 2 in product.