In [1]:
"""
Script to test the finite difference schemes for second order derivative.
"""

import floatpy.derivatives.explicit.second as second_der
import numpy

%matplotlib inline
from matplotlib import pyplot as plt
plt.rc("savefig", dpi = 150)

In [2]:
"""
Test the second order derivatives in the first direction.
"""

x = numpy.empty([1, 50])
x[0, :] = numpy.linspace(0, 2*numpy.pi, 50)
dx = x[0, 1] - x[0, 0]

y = numpy.sin(x)
y_prime_exact = -numpy.sin(x)

y_prime_2 = second_der.differentiateSecondOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')
y_prime_4 = second_der.differentiateFourthOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')
y_prime_6 = second_der.differentiateSixthOrderFiniteDifference(y, dx, 0, 0, True, 1, 'C')

# Compare the the second derivatives.

fig, ax = plt.subplots()
ax.plot(x[0, :], y_prime_2, linestyle = ':', label='second order', linewidth=3)
ax.plot(x[0, :], y_prime_4, linestyle = '--', label='fourth order', linewidth=3)
ax.plot(x[0, :], y_prime_6, linestyle = '-.', label='sixth order', linewidth=3)
ax.plot(x[0, :], y_prime_exact[0, :], label='exact', linewidth=1)

plt.xlim(0.0, 2.0*numpy.pi)
plt.ylim(-1.0, 1.0)
plt.tick_params(axis='both', which='major', labelsize=12)

plt.xlabel('x', fontsize=14)
plt.ylabel('y', fontsize=14)

legend = ax.legend(loc='lower right', shadow = False)



In [3]:
"""
Test the second order derivatives in the second direction.
"""

x = numpy.empty([1, 1, 50])
x[0, 0, :] = numpy.linspace(0, 2*numpy.pi, 50)
dx = x[0, 0, 1] - x[0, 0, 0]

y = numpy.sin(x)
y_prime_exact = -numpy.sin(x)

y_prime_2 = second_der.differentiateSecondOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')
y_prime_4 = second_der.differentiateFourthOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')
y_prime_6 = second_der.differentiateSixthOrderFiniteDifference(y, dx, 1, 0, True, 2, 'C')

# Compare the the second derivatives.

fig, ax = plt.subplots()
ax.plot(x[0, 0, :], y_prime_2[0, :], linestyle = ':', label='second order', linewidth=3)
ax.plot(x[0, 0, :], y_prime_4[0, :], linestyle = '--', label='fourth order', linewidth=3)
ax.plot(x[0, 0, :], y_prime_6[0, :], linestyle = '-.', label='sixth order', linewidth=3)
ax.plot(x[0, 0, :], y_prime_exact[0, 0, :], label='exact', linewidth=1)

plt.xlim(0.0, 2.0*numpy.pi)
plt.ylim(-1.0, 1.0)
plt.tick_params(axis='both', which='major', labelsize=12)

plt.xlabel('x', fontsize=14)
plt.ylabel('y', fontsize=14)

legend = ax.legend(loc='lower right', shadow = False)



In [4]:
"""
Test the second order derivatives in the third direction.
"""

x = numpy.empty([1, 1, 1, 50])
x[0, 0, 0, :] = numpy.linspace(0, 2*numpy.pi, 50)
dx = x[0, 0, 0, 1] - x[0, 0, 0, 0]

y = numpy.sin(x)
y_prime_exact = -numpy.sin(x)

y_prime_2 = second_der.differentiateSecondOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')
y_prime_4 = second_der.differentiateFourthOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')
y_prime_6 = second_der.differentiateSixthOrderFiniteDifference(y, dx, 2, 0, True, 3, 'C')

# Compare the the second derivatives.

fig, ax = plt.subplots()
ax.plot(x[0, 0, 0, :], y_prime_2[0, 0, :], linestyle = ':', label='second order', linewidth=3)
ax.plot(x[0, 0, 0, :], y_prime_4[0, 0, :], linestyle = '--', label='fourth order', linewidth=3)
ax.plot(x[0, 0, 0, :], y_prime_6[0, 0, :], linestyle = '-.', label='sixth order', linewidth=3)
ax.plot(x[0, 0, 0, :], y_prime_exact[0, 0, 0, :], label='exact', linewidth=1)

plt.xlim(0.0, 2.0*numpy.pi)
plt.ylim(-1.0, 1.0)
plt.tick_params(axis='both', which='major', labelsize=12)

plt.xlabel('x', fontsize=14)
plt.ylabel('y', fontsize=14)

legend = ax.legend(loc='lower right', shadow = False)



In [ ]: