Skip to content

Commit

Permalink
Add solution to problem in finite difference deriv module, courtesy B…
Browse files Browse the repository at this point in the history
…randon Clark.
  • Loading branch information
zachetienne committed Sep 10, 2018
1 parent 9078255 commit 0d6f177
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
99 changes: 99 additions & 0 deletions Tutorial-Finite_Difference_Derivatives-FDtable_soln.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating the Finite-Difference Table for Centered First and Second Derivatives, from Second through Eighth-Order Accuracy\n",
"\n",
"## Courtesy Brandon Clark"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Derivative Accuracy -4 -3 -2 -1 0 1 2 3 4 \n",
"---------- -------- ------ ------ ----- ---- ------- --- ----- ----- ------\n",
" First 2 0 0 0 -1/2 0 1/2 0 0 0\n",
" First 4 0 0 1/12 -2/3 0 2/3 -1/12 0 0\n",
" First 6 0 -1/60 3/20 -3/4 0 3/4 -3/20 1/60 0\n",
" First 8 1/280 -4/105 1/5 -4/5 0 4/5 -1/5 4/105 -1/280\n",
" Second 2 0 0 0 1 -2 1 0 0 0\n",
" Second 4 0 0 -1/12 4/3 -5/2 4/3 -1/12 0 0\n",
" Second 6 0 1/90 -3/20 3/2 -49/18 3/2 -3/20 1/90 0\n",
" Second 8 -1/560 8/315 -1/5 8/5 -205/72 8/5 -1/5 8/315 -1/560\n"
]
}
],
"source": [
"# Step 0: Import needed modules\n",
"import numpy as np\n",
"import finite_difference as fin\n",
"from astropy.table import Table\n",
"\n",
"# Step 1: Set the maximum finite-difference accuracy order\n",
"max_fdorder = 8\n",
"\n",
"# Step 2: Set up table parameters\n",
"# One column for deriv order, one for deriv accuracy, and max_fdorder+1\n",
"numcols = 2 + max_fdorder + 1\n",
"# 8 rows: 4 accuracy orders per derivative order.\n",
"numrows = 8\n",
"# Center column index of table will be at 2 + max_fdorder/2 (zero-offset indexing)\n",
"column_corresponding_to_zero_fd_point = 2 + int(max_fdorder/2)\n",
"# The table is initialized as a matrix of zeroes in numpy:\n",
"numpy_matrix = np.zeros((numrows, numcols), dtype=object)\n",
"\n",
"# Step 3: Construct the first-order derivative finite difference coefficients\n",
"rowcount = 0\n",
"for fdorder in range(2, max_fdorder+1, 2): # loop runs from 2 to max_fdorder inclusive, skipping odd orders.\n",
" numpy_matrix[rowcount, 0] = \"First\"\n",
" numpy_matrix[rowcount, 1] = fdorder \n",
" fdcoeffs, fdstencl = fin.compute_fdcoeffs_fdstencl(\"D0\", fdorder)\n",
" for i in range(fdorder):\n",
" numpy_matrix[rowcount, column_corresponding_to_zero_fd_point + fdstencl[i][0]] = fdcoeffs[i]\n",
" rowcount += 1\n",
"\n",
"# Step 4: Construct the first-order derivative finite difference coefficients\n",
"for fdorder in range(2, max_fdorder+1, 2): # loop runs from 2 to max_fdorder inclusive, skipping odd orders.\n",
" numpy_matrix[rowcount, 0] = \"Second\"\n",
" numpy_matrix[rowcount, 1] = fdorder\n",
" fdcoeffs, fdstencl = fin.compute_fdcoeffs_fdstencl(\"DD00\", fdorder)\n",
" for i in range(fdorder+1):\n",
" numpy_matrix[rowcount, column_corresponding_to_zero_fd_point + fdstencl[i][0]] = fdcoeffs[i]\n",
" rowcount += 1 \n",
"\n",
"# Step 5: Construct an astropy table from the numpy matrix with the following header info, and then print it:\n",
"table = Table(numpy_matrix, names=('Derivative', 'Accuracy', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4'))\n",
"print(table)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion Tutorial-Finite_Difference_Derivatives.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"\n",
"In addition, if the gridfunction $u$ exists on a grid that is less than four (spatial) dimensions, it is up to the user to truncate the additional index information.\n",
"\n",
"#### Exercise: Using compute_fdcoeffs_fdstencl(), write the necessary loops to output the finite difference coefficient tables in the Wikipedia article on [finite difference coefficients](https://en.wikipedia.org/wiki/Finite_difference_coefficients), for up to second-order derivatives (i.e., up to $\\partial_i^2$), and for upwinded derivatives up to second-order accuracy."
"#### Exercise: Using compute_fdcoeffs_fdstencl(), write the necessary loops to output the finite difference coefficient tables in the Wikipedia article on [finite difference coefficients](https://en.wikipedia.org/wiki/Finite_difference_coefficients), for first and second centered derivatives (i.e., up to $\\partial_i^2$) up to eighth-order accuracy. [Solution, courtesy Brandon Clark](Tutorial-Finite_Difference_Derivatives-FDtable_soln.ipynb)."
]
},
{
Expand Down

0 comments on commit 0d6f177

Please sign in to comment.