hello, i have a problem in plotting solution and mesh, i follow the code as Solving the Poisson equation — FEniCSx tutorial , how do i plot the solution and mesh.
import dolfinx
import ufl
from dolfinx.plot import vtk_mesh
import pyvista
from ufl import Measure, inner, grad, div, TestFunction, TrialFunction, adjoint, derivative
from dolfinx.mesh import CellType, create_rectangle
from dolfinx import fem, nls, io
from mpi4py import MPI
from petsc4py import PETSc
import numpy as np
import time
from matplotlib import*
from ufl import SpatialCoordinate, TestFunction, TrialFunction, dot, ds, dx, grad, inner
import dolfinx.nls.petsc
import dolfinx.fem.petsc
from dolfinx.fem import (Constant, Function, FunctionSpace,
assemble_scalar, dirichletbc, form, locate_dofs_geometrical)
from dolfinx import default_scalar_type
from dolfinx import *
#Parameter
theta0 = 0.5;
theta = theta0;
D0 = 1.0;
k0 = -1.0;
eta = 2.0;
gamma = 2.0;
Rfl = 0.0018;
URF = 0.1;
# mesh and function space
mesh = create_rectangle(MPI.COMM_WORLD, [[0, 0], [1, 1]], [64, 64], CellType.triangle)
#domainU = mesh.create_unit_square(MPI.COMM_WORLD, 100, 100, mesh.CellType.quadrilateral)
V = fem.FunctionSpace(mesh, ("CG", 1))
c = fem.Function(V)
phi = TestFunction(V)
eps = fem.Function(V) # The eps is the design variable
eps.x.array[:] = 0.1
dx = Measure("dx")
x = SpatialCoordinate(mesh)
gu = Constant(mesh, default_scalar_type(0))
# Residual of the variational form of Poisson problem
R = inner(D0*eps**eta*grad(c), grad(phi))*dx - inner(k0*(1-eps)**gamma*c,phi)*dx - ufl.inner(gu,phi) *ufl.ds
##Dirichlet
def on_boundary(x):
return np.isclose(x[0], 0)
dofs_L = fem.locate_dofs_geometrical(V , on_boundary)
bc_L = fem.dirichletbc(default_scalar_type(1), dofs_L, V)
bcs = [bc_L]
#Forming and solving the linear system
problem_u = fem.petsc.NonlinearProblem(R, c, bcs)
solver = nls.petsc.NewtonSolver(MPI.COMM_WORLD,problem_u)
solver.rtol = 1e-16
ch = solver.solve(c)
#plotting
pyvista.start_xvfb()
pyvista_cells, cell_types, geometry = vtk_mesh(V)
grid = pyvista.UnstructuredGrid(pyvista_cells, cell_types, geometry)
grid.point_data["c"] = ch.x.array
grid.set_active_scalars("c")
plotter = pyvista.Plotter()
plotter.add_text("c", position="upper_edge", font_size=14, color="black")
plotter.add_mesh(grid, show_edges=True)
plotter.view_xy()
if not pyvista.OFF_SCREEN:
plotter.show()
else:
figure = plotter.screenshot("c_pic.png")
the error is
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[50], line 4
2 pyvista_cells, cell_types, geometry = vtk_mesh(V)
3 grid = pyvista.UnstructuredGrid(pyvista_cells, cell_types, geometry)
----> 4 grid.point_data["c"] = ch.x.array
5 grid.set_active_scalars("c")
6 plotter = pyvista.Plotter()
AttributeError: 'tuple' object has no attribute 'x'