# Error in plotting mesh

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 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

##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.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'
``````

The newton-solver does not return the function
https://docs.fenicsproject.org/dolfinx/v0.7.3/python/_modules/dolfinx/nls/petsc.html#NewtonSolver.solve

The function `c` defined above is what you want to plot

Yes, c is the concentration i want to plot, could you give me some example or tutorial. I’m quite confused about it

and another question is, when i print the solution to check if it’s correct

ch = solver.solve(c)
print(ch)

it gives result as

(1, True)

which i don’t know what does that mean

Ch is (number_of_iterations, solver_converged).

For plotting, just replace your usage of `ch` with `c` where you try to use pyvista to plot.