Created
October 17, 2013 19:29
-
-
Save wd15/7030819 to your computer and use it in GitHub Desktop.
Upwind face variable for Karim Khayrat
This file contains hidden or 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
{ | |
"metadata": { | |
"name": "upwind" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from fipy.variables.cellToFaceVariable import _CellToFaceVariable\n", | |
"from fipy.tools import numerix\n", | |
"\n", | |
"class UpwindFaceVariable(_CellToFaceVariable):\n", | |
" def __init__(self, var, velocity):\n", | |
" self.velocity = velocity\n", | |
" super(UpwindFaceVariable, self).__init__(var)\n", | |
" \n", | |
" def _calcValue_(self, alpha, id1, id2):\n", | |
" cell1 = numerix.take(self.var, id1, axis=-1)\n", | |
" cell2 = numerix.take(self.var, id2, axis=-1)\n", | |
" normals = self.mesh.faceNormals\n", | |
" flow = numerix.sum(normals * self.velocity)\n", | |
" return numerix.where(flow > 0, cell1, cell2)\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import fipy as fp\n", | |
"\n", | |
"m = fp.Grid1D(nx=4)\n", | |
"v = fp.CellVariable(mesh=m, value=(0, 1, 2, 3))\n", | |
"velocity = fp.FaceVariable(mesh=m, rank=1, value=2 - m.faceCenters)\n", | |
"up = UpwindFaceVariable(v, velocity)\n", | |
"print v\n", | |
"print velocity\n", | |
"print up" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[0 1 2 3]\n", | |
"[[ 2. 1. 0. -1. -2.]]\n", | |
"[0 0 2 3 3]\n" | |
] | |
} | |
], | |
"prompt_number": 29 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import fipy as fp\n", | |
"\n", | |
"m = fp.Grid2D(nx=2, ny=2)\n", | |
"v = fp.CellVariable(mesh=m, value=(0, 1, 2, 3))\n", | |
"X, Y = m.faceCenters\n", | |
"velocity = fp.FaceVariable(mesh=m, rank=1)\n", | |
"velocity[0] = Y - 1\n", | |
"velocity[1] = 1 - X\n", | |
"up = UpwindFaceVariable(v, velocity)\n", | |
"print v\n", | |
"print velocity\n", | |
"print up" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[0 1 2 3]\n", | |
"[[-1. -1. 0. 0. 1. 1. -0.5 -0.5 -0.5 0.5 0.5 0.5]\n", | |
" [ 0.5 -0.5 0.5 -0.5 0.5 -0.5 1. 0. -1. 1. 0. -1. ]]\n", | |
"[0 1 0 3 2 3 0 1 1 2 2 3]\n" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment