Commit 471b4c07 by Jigyasa Watwani

sine solution instead of straight line solution

parent bcd69818
Showing with 58 additions and 0 deletions
import dolfin as df
import matplotlib.pyplot as plt
import numpy as np
df.set_log_level(df.LogLevel.ERROR)
df.parameters['form_compiler']['optimize'] = True
def laplace(Nx, L, cL, cR):
# mesh, function space, function, test function
mesh = df.IntervalMesh(Nx, 0, L)
x = mesh.coordinates()[:,0]
function_space = df.FunctionSpace(mesh, 'P', 1)
c, tc = df.Function(function_space), df.TestFunction(function_space)
# Dirichlet boundary
dbc = df.DirichletBC(function_space, df.Constant(0), 'on_boundary')
def left(x, on_boundary): # inhomogeneous dirichlet boundary
return df.near(x[0], 0.0) and on_boundary
def right(x, on_boundary):
return df.near(x[0], L) and on_boundary
dbc_left = df.DirichletBC(function_space, df.Constant(cL), left)
dbc_right = df.DirichletBC(function_space, df.Constant(cR), right)
dbc = [dbc_left, dbc_right]
# form
form = (-df.inner(c.dx(0), tc.dx(0)) + df.inner(tc, df.Expression('sin(x[0])', degree=1))) * df.dx(mesh)
# solve
df.solve(form == 0, c, dbc)
c_numerical = c.compute_vertex_values(mesh)
# exact solution
c_exact = np.sin(x)
# plot
# plt.plot(x, c_numerical, 'bo', label='Numerical solution')
# plt.plot(x, c_exact, label='Exact solution')
# plt.xlabel('x')
# plt.ylabel('$c(x)$')
# plt.legend()
# plt.show()
error = np.max(np.abs(c_numerical - c_exact))
return(error)
# parameters
L, cL, cR = np.pi, 0, 0
Nx_array = np.array([5, 10, 20, 40, 80, 100, 200, 400, 800, 1600])
dx_array = L/Nx_array
error_array = np.zeros(len(dx_array))
for i in range(0, len(dx_array)):
error_array[i] = laplace(Nx_array[i], L, cL, cR)
plt.loglog(dx_array, error_array, 'bo')
plt.show()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment