Hey,
I try to interpolate a given function (as expression and function)
def exact_solution(x,t):
return exp(t-x)*sin(t+x)
u_expression = Expression("exp(t-x[0])*sin(t+x[0])",t=0)
from $t \in (0,1)$ and $\Omega = [0,1]$. In each time step, I use
u_int = interpolate(u_expression,V)
u_int_array = u_int.vector().array()
and save afterwards the nodal values of the interpolated and exact function in arrays
for i in range(0,nx):
u_int_nod_vals.append(u_int_array[i])
u_ex_nod_vals.append(exact_solution(x_array[i],t))
sol_exact.append(u_ex_nod_vals)
sol_int.append(u_int_nod_vals)
However, all the values in "sol_exact" are horizontally flipped. At first I thought my output function is the problem, but then the exact function should be flipped as well. Did anybody else encounter this problem?
Here is my entire source code:
from __future__ import division
from dolfin import *
import numpy as np
import matplotlib as mpl
mpl.use( "agg" )
import matplotlib.pyplot as plt
from matplotlib import animation
from math import exp
from math import sin
# ------------------- functions
def save_solution(sol, xmax, title, filename):
ymax = max(max(l) for l in sol)
fig = plt.figure()
axe = plt.axes(xlim=(0, xmax*1.1), ylim=(0, ymax*1.1))
line, = axe.plot([], [], lw=2, color = 'g')
plt.xlabel('Position')
plt.ylabel('Value')
plt.title(title)
def init():
line.set_data([], [])
return line,
def animate(i):
y = np.array(sol[i])
line.set_data(x_array, y)
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(sol), interval=20, blit=True)
anim.save(filename+'.mp4', fps=15)
# ------------------- functions
nx = 80
xmax = 1
mesh1D = IntervalMesh(nx, 0, xmax)
# definition of solving method
V = FunctionSpace(mesh1D, 'Lagrange', 1)
dt = 1/nx
T=1
t = dt
def exact_solution(x,t):
return exp(t-x)*sin(t+x)
u_expression = Expression("exp(t-x[0])*sin(t+x[0])",t=0)
#constants for ploting
x_points = []
for i in range(0,nx):
x_points.append((xmax*i)/(1.0*nx))
x_array = np.array(x_points)
sol_exact = []
sol_int = []
while t <= T:
u_ex_nod_vals = []
u_int = []
u_int_nod_vals = []
u_int = interpolate(u_expression,V)
u_int_array = u_int.vector().array()
for i in range(0,nx):
u_int_nod_vals.append(u_int_array[i])
u_ex_nod_vals.append(exact_solution(x_array[i],t))
sol_exact.append(u_ex_nod_vals)
sol_int.append(u_int_nod_vals)
t += dt
u_expression.t=t
save_solution(sol_int,xmax, 'interpolated solution', 'sol_interpolate')
save_solution(sol_exact,xmax, 'Exact solution', 'sol_exact')