I want to test the SUPG method with a one dimensional problem. And when I interpolate the exact solution on a very fine grid with 500 cells(actually no matter how many cells, I still get the errors), as the codes show,
x=SpatialCoordinate(domain)
domain_e=mesh.create_interval(MPI.COMM_WORLD,500,[0,1])
V_e= fem.FunctionSpace(domain_e,("CG",1))
u_exact=fem.Function(V_e)
u_exact.interpolate(fem.Expression((exp(a[0]*x[0]/kappa)-1)/(exp(a[0]/kappa)-1),V_e.element.interpolation_points()))
I receive the error
[0]PETSC ERROR: ------------------------------------------------------------------------
[0]PETSC ERROR: Caught signal number 11 SEGV: Segmentation Violation, probably memory access out of range
[0]PETSC ERROR: Try option -start_in_debugger or -on_error_attach_debugger
[0]PETSC ERROR: or see https://petsc.org/release/faq/#valgrind
[0]PETSC ERROR: or try http://valgrind.org on GNU/linux and Apple Mac OS X to find memory corruption errors
[0]PETSC ERROR: configure using --with-debugging=yes, recompile, link, and run
[0]PETSC ERROR: to get more information on the crash.
[0]PETSC ERROR: Run with -malloc_debug to check if memory corruption is causing the crash.
malloc(): invalid size (unsorted)
[LAPTOP-8EKSL8AG:05599] *** Process received signal ***
[LAPTOP-8EKSL8AG:05599] Signal: Aborted (6)
[LAPTOP-8EKSL8AG:05599] Signal code: (-6)
So I dont know the reason. please help,thanks in advance!
The complete code is as follows:
from dolfinx import mesh,fem
from ufl import (inner,dot,div,grad,
TrialFunction,TestFunction,dx,
CellDiameter,Min,sqrt,lhs,rhs,SpatialCoordinate,exp
)
from mpi4py import MPI
from petsc4py import PETSc
import numpy as np
use_SUPG=True
N=16
k=1
domain=mesh.create_interval(MPI.COMM_WORLD,N,[0,1])
kappa=fem.Constant(domain,PETSc.ScalarType(5e-4))
a=fem.Constant(domain,np.array((1,),dtype=PETSc.ScalarType))
V=fem.FunctionSpace(domain,("CG",k))
u,v=TrialFunction(V),TestFunction(V)
f=fem.Constant(domain,PETSc.ScalarType(0))
res_Galerkin=(kappa*inner(grad(u),grad(v))+dot(a,grad(u))*v-f*v)*dx
h=CellDiameter(domain)
Cinv=fem.Constant(domain,PETSc.ScalarType(6*k*k))
tau=Min(h*h/(Cinv*kappa),h/(2*sqrt(dot(a,a))))
res_SUPG=tau*(-div(kappa*grad(u))+dot(a,grad(u))-f)*dot(a,grad(v))*dx
res= res_Galerkin
if use_SUPG:
res +=res_SUPG
uh=fem.Function(V)
point0=mesh.locate_entities_boundary(domain,0,lambda x:np.isclose(x[0],0))
point1=mesh.locate_entities_boundary(domain,0,lambda x:np.isclose(x[0],1))
bc=fem.Constant(domain,PETSc.ScalarType(0))
bc0=fem.dirichletbc(bc,fem.locate_dofs_topological(V,0,point0),V)
bc=fem.Constant(domain,PETSc.ScalarType(1))
bc1=fem.dirichletbc(bc,fem.locate_dofs_topological(V,0,point1),V)
bd=[bc0,bc1]
A=lhs(res)
L=rhs(res)
problem=fem.petsc.LinearProblem(A,L,bd)
uh=problem.solve()
x=SpatialCoordinate(domain)
domain_e=mesh.create_interval(MPI.COMM_WORLD,500,[0,1])
V_e= fem.FunctionSpace(domain_e,("CG",1))
u_exact=fem.Function(V_e)
u_exact.interpolate(fem.Expression((exp(a[0]*x[0]/kappa)-1)/(exp(a[0]/kappa)-1),V_e.element.interpolation_points()))
e=uh-u_exact
print("H1 seminorm error = "
+str(sqrt(fem.assemble_scalar(fem.form(dot(grad(e),grad(e))*dx)))))
print("L2 error = "
+str(sqrt(fem.assemble_scalar(fem.form(e*e*dx)))))
x = V.tabulate_dof_coordinates()
x_order = np.argsort(x[:,0])
import matplotlib.pyplot as plt
plt.plot(x[x_order, 0], u_exact.x.array[x_order],label='exact solution')
plt.plot(x[x_order, 0],uh.x.array[x_order],label='numerical solution')
plt.legend()
plt.autoscale()
plt.show()