I was solving a weak form problem from 1-dimensional ODE problem,
The code are as follows:
from fenics import *
import math
import sys
sys.path.append('../..') # change directory
from mshr import *
# Create mesh
mesh = IntervalMesh(10, 0, math.pi*2)
# Define function spaces
V = FunctionSpace(mesh, 'Lagrange', 1)
# Define boundaries
left = 'near(x[0], 0)' # left boundary x = 0
# Define boundary conditions
bcu_fixed = DirichletBC(V, Constant(0), left)
bcs = [bcu_fixed]
# Define test functions
u = TrialFunction(V)
v = TestFunction(V)
# Define the weak form
a = u.dx(0)*v.dx(0)*dx
# inner(grad(u), grad(v))*dx
# Define the distributed load
f = Expression("sin(x[0])", degree = 4)
g = Expression("1", degree = 3)
# L = f*v*dx + g*v*dx # second term is a natural B.C.
L = f*v*dx
# Compute solution
u = Function(V)
solve(a == L, u, bcs)
# Ouput results into a file in VTK format
vtkfile = File('result1.pvd')
vtkfile << u
file_write = open('test.txt', 'w')
target = u.vector().get_local()
result = []
for element in target:
result = result + [float(element)]
print(result)
# print(mesh.coordinates())
However, the result fits like \pi sin(x-\pi)-\pi, instead of sin(x)
but if I change the mesh from
IntervalMesh(10, 0, math.pi*2)
to
IntervalMesh(10, 0, math.pi/2)
the solution will fit sin(x) perfectly
I don’t know why it is like that