Last active
August 29, 2015 14:25
-
-
Save johntfoster/7dfcee71e5f119f65aa2 to your computer and use it in GitHub Desktop.
Codes snippit to extract global nodes from sideset information contained in Exodus database.
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import netCDF4\n", | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Open NetCDF file" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "nc = netCDF4.Dataset('s_curve.g')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Read in connectivity array (minus 1 converts to Numpy stype 0 based indexing)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "connect = np.array(nc.variables['connect1'][:] - 1, dtype=int)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Read in the elements ids that are contained in the sideset (again, convert to Numpy 0 based indexing)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([110, 126, 153, 155, 156, 6, 7, 8, 9, 10, 11, 12, 43,\n", | |
| " 45, 46, 121, 122, 123, 124, 125], dtype=int32)" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "elements_in_sideset = nc.variables['elem_ss1'][:] - 1\n", | |
| "elements_in_sideset" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Indexing into the connectivity array gives us the subset of the connectivity array for only the elements that are in the sideset" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[ 10, 8, 136, 14],\n", | |
| " [ 22, 20, 154, 153],\n", | |
| " [ 20, 18, 205, 154],\n", | |
| " [ 18, 16, 208, 206],\n", | |
| " [ 16, 14, 209, 208],\n", | |
| " [ 10, 14, 15, 12],\n", | |
| " [ 14, 16, 17, 15],\n", | |
| " [ 16, 18, 19, 17],\n", | |
| " [ 18, 20, 21, 19],\n", | |
| " [ 20, 22, 23, 21],\n", | |
| " [ 22, 24, 25, 23],\n", | |
| " [ 26, 27, 28, 29],\n", | |
| " [ 24, 88, 89, 25],\n", | |
| " [ 88, 92, 93, 90],\n", | |
| " [ 92, 29, 94, 93],\n", | |
| " [ 26, 29, 149, 147],\n", | |
| " [ 29, 92, 150, 149],\n", | |
| " [ 92, 88, 151, 150],\n", | |
| " [ 88, 24, 152, 151],\n", | |
| " [ 24, 22, 153, 152]])" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "global_nodes_in_sideset = connect[elements_in_sideset]\n", | |
| "global_nodes_in_sideset" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Here we create a map between the \"side ordering convention\" and the \"local node number convention\" for a Q4 element as defined in the Exodus API. In other the words, the first row (index 0) corresponds to side 1 which has local nodes 1 and 2. The second row (index 1) corresponds to side 2 which has local nodes 2 and 3." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "side_to_local_index = np.array([[0,1],[1,2],[2,3],[1,3]], dtype=np.int)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Read in the sides in the sideset and use those to index into the `side_to_local_index` map to get the local node numbers along the side of the element that is in the sideset" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[1, 3],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [1, 3],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1],\n", | |
| " [0, 1]])" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "local_nodes_in_side_set = side_to_local_index[nc.variables['side_ss1'][:] - 1]\n", | |
| "local_nodes_in_side_set" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Use the local node numbers to index into the subset connectivity array containing only the elements in the sideset. This returns the global node numbers for each side in the side set, grouped by element." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "array([[ 8, 14],\n", | |
| " [22, 20],\n", | |
| " [20, 18],\n", | |
| " [18, 16],\n", | |
| " [16, 14],\n", | |
| " [10, 14],\n", | |
| " [14, 16],\n", | |
| " [16, 18],\n", | |
| " [18, 20],\n", | |
| " [20, 22],\n", | |
| " [22, 24],\n", | |
| " [27, 29],\n", | |
| " [24, 88],\n", | |
| " [88, 92],\n", | |
| " [92, 29],\n", | |
| " [26, 29],\n", | |
| " [29, 92],\n", | |
| " [92, 88],\n", | |
| " [88, 24],\n", | |
| " [24, 22]])" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "global_nodes_in_sideset[np.arange(local_nodes_in_side_set.shape[0])[:,None], local_nodes_in_side_set]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "nc.close()" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.10" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment