 # How to improve the accuracy of projection

Hi all, I am trying to project a variable in the DG-0 function space to another DG-0 function space to test the accuracy of projection. However, sometimes the projection can give inaccurate results. The minimal working example is as follows:

``````import numpy as np
import dolfinx
from dolfinx.cpp.mesh import CellType
from mpi4py import MPI
import ufl

mesh = dolfinx.RectangleMesh(MPI.COMM_WORLD, [np.array([0, 0, 0]), np.array([6, 6, 0])], \

S0 = dolfinx.FunctionSpace(mesh, ('DG', 0))
z_field = dolfinx.Function(S0)
z_field.vector.array = np.array([0.5,0.5,0.5,0.5])

def ExpressionProjection(mesh, expr):
V_proj = dolfinx.FunctionSpace(mesh, ('DG', 0))
p = ufl.TrialFunction(V_proj)
q = ufl.TestFunction(V_proj)
a_v = ufl.inner(p, q) * ufl.dx
L_v = ufl.inner(expr, q) * ufl.dx
sol = dolfinx.fem.LinearProblem(a_v, L_v).solve().vector.array
return sol

sol = ExpressionProjection(mesh, z_field)
print(sol) # projected results
print(z_field.vector.array) # original values
``````

The output results are:

``````[1.42080553e-316 1.01306145e-316 5.00000000e-001 5.00000000e-001]
[0.5 0.5 0.5 0.5]
``````

Apparently, the projected results and original values are different in this case. Also, there might be underflow values in the projected results. Do we have any methods to fix this? Thanks in advance!

It seems the following modification works, although I am not sure why:

``````def ExpressionProjection(mesh, expr):
V_proj = dolfinx.FunctionSpace(mesh, ('DG', 0))
p = ufl.TrialFunction(V_proj)
q = ufl.TestFunction(V_proj)
a_v = ufl.inner(p, q) * ufl.dx
L_v = ufl.inner(expr, q) * ufl.dx
problem = dolfinx.fem.LinearProblem(a_v, L_v)
sol = problem.solve()
return sol

sol = ExpressionProjection(mesh, z_field)
data = sol.vector.array
print(data) # projected results
print(z_field.vector.array) # original values
``````

The results are:

``````[0.5 0.5 0.5 0.5]
[0.5 0.5 0.5 0.5]
``````