-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution to problem in finite difference deriv module, courtesy B…
…randon Clark.
- Loading branch information
1 parent
9078255
commit 0d6f177
Showing
2 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters