I’m using dolfinx, and when I want to define a function, this error occurs in debugging mode, the code is
import dolfinx
from dolfinx.fem import functionspace, dirichletbc, Constant, locate_dofs_geometrical, locate_dofs_topological
from dolfinx.mesh import locate_entities_boundary
from petsc4py import PETSc
import numpy as np
from basix.ufl import element, mixed_element
from dolfinx import fem, io, mesh
from dolfinx.fem.petsc import LinearProblem
from ufl import (Measure, SpatialCoordinate, TestFunctions, TrialFunctions,
div, exp, inner, CellDiameter)
from mesh_and_plot import create_nested_rectangles
# Time stepping parameters
num_steps = 100
dt = 1 / num_steps
max_iter = 10
tol = 1e-6
domain, markers = create_nested_rectangles()
# Physical constants
R = fem.Constant(domain, np.float64(287.05))
c_v = fem.Constant(domain, np.float64(718.0))
c_p = fem.Constant(domain, np.float64(1005.0))
mu = fem.Constant(domain, np.float64(1.81e-5))
kappa = fem.Constant(domain, np.float64(0.025))
# Create mesh
length = 0.4
height = 0.4
nx = 40
ny = 40
# Create mesh and markers using the previously defined function
# Define function spaces
# Create elements
P1_pressure = element("Lagrange", domain.basix_cell(), degree=1, dtype=dolfinx.default_real_type)
P1_velocity = element("Lagrange", domain.basix_cell(), degree=1, shape=(domain.geometry.dim,), dtype=dolfinx.default_real_type)
P1_temperature = element("Lagrange", domain.basix_cell(), degree=1, dtype=dolfinx.default_real_type)
#V_u, V_p, V_T = functionspace(domain, P1_velocity), functionspace(domain, P1_pressure),functionspace(domain, P1_temperature)
V_fluid = mixed_element([P1_pressure, P1_velocity, P1_temperature])
V_fluid_fs=dolfinx.fem.functionspace(domain,V_fluid)
p_init = fem.Function(V_fluid_fs.sub(0).collapse()[0])
and the code for mesh generation is in file mesh_and_plot.py
import dolfinx
from dolfinx import mesh, cpp
from mpi4py import MPI
import pyvista as pv
import numpy as np
def create_nested_rectangles():
# Create outer rectangle domain
domain = mesh.create_rectangle(
comm=MPI.COMM_WORLD,
points=((0.0, 0.0), (0.4, 0.4)),
n=(32, 32)
)
def inner_domain(x):
return np.logical_and(
np.logical_and(x[0] >= 0.1, x[0] <= 0.3),
np.logical_and(x[1] >= 0.15, x[1] <= 0.25)
)
dim = domain.topology.dim
domain.topology.create_entities(dim)
inner_cells = mesh.locate_entities(domain, dim, inner_domain)
# Create default values for all cells
num_cells = domain.topology.index_map(dim).size_local
markers_array = np.zeros(num_cells, dtype=np.int32)
# Set values for inner domain cells
markers_array[inner_cells] = 1
# Create MeshTags object
markers = mesh.meshtags(domain, dim, np.arange(num_cells), markers_array)
return domain, markers
def plot_mesh_with_subdomains(domain, markers):
# Convert to VTK format for plotting
topology, cells, geometry = dolfinx.plot.vtk_mesh(domain)
grid = pv.UnstructuredGrid(topology, cells, geometry)
# Add markers as cell data
grid.cell_data["markers"] = markers.values
# Create plotter
plotter = pv.Plotter(shape=(1, 2))
# Plot full mesh
plotter.subplot(0, 0)
plotter.add_mesh(grid, show_edges=True)
plotter.add_title('Complete Mesh')
# Plot mesh with subdomains
plotter.subplot(0, 1)
plotter.add_mesh(grid, show_edges=True, scalars="markers")
plotter.add_title('Mesh with Subdomains')
plotter.show()
# Create and plot the mesh
domain, markers = create_nested_rectangles()
plot_mesh_with_subdomains(domain, markers)
when I am debugging the code, I find that in debugging mode, in
R = fem.Constant(domain, np.float64(287.05)) c_v = fem.Constant(domain, np.float64(718.0)) c_p = fem.Constant(domain, np.float64(1005.0)) mu = fem.Constant(domain, np.float64(1.81e-5)) kappa = fem.Constant(domain, np.float64(0.025))
we all have this error
whose value is
ValueError('Transposed is only defined for rank 2 tensors.')
and in the following, in
p_init = fem.Function(V_fluid_fs.sub(0).collapse()[0])
we have the same issue, could you please tell me how to fix this? thank you very much