{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Let's Explore PyNE Meshes!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from yt.config import ytcfg; ytcfg[\"yt\",\"suppressStreamLogging\"] = \"True\"\n", "from yt.mods import *\n", "from pyne.xs.channels import sigma_t\n", "from pyne.material import Material, from_atom_frac\n", "from pyne.mesh import Mesh, IMeshTag, MetadataTag, ComputedTag" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "No module named halo_catalog.api", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mImportError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0myt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfig\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mytcfg\u001b[0m\u001b[1;33m;\u001b[0m \u001b[0mytcfg\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"yt\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"suppressStreamLogging\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"True\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0myt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmods\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[1;33m*\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpyne\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mxs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mchannels\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msigma_t\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpyne\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmaterial\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mMaterial\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mfrom_atom_frac\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mpyne\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmesh\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mMesh\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mIMeshTag\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mMetadataTag\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mComputedTag\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m/home/scopatz/.local/lib/python2.7/site-packages/yt-3.0dev-py2.7-linux-x86_64.egg/yt/mods.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 99\u001b[0m \u001b[0mRAMSESStaticOutput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mRAMSESFieldInfo\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 100\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 101\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0myt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mfrontends\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhalo_catalogs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapi\u001b[0m \u001b[1;32mimport\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 102\u001b[0m \u001b[0mHaloCatalogStaticOutput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mHaloCatalogFieldInfo\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 103\u001b[0m \u001b[0mRockstarStaticOutput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mRockstarFieldInfo\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32m/home/scopatz/.local/lib/python2.7/site-packages/yt-3.0dev-py2.7-linux-x86_64.egg/yt/frontends/halo_catalogs/api.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[1;31m#-----------------------------------------------------------------------------\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mhalo_catalog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mapi\u001b[0m \u001b[1;32mimport\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[0mHaloCatalogStaticOutput\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[0mIOHandlerHaloCatalogHDF5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mImportError\u001b[0m: No module named halo_catalog.api" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "import yt" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "yt.__file__" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "'/home/scopatz/.local/lib/python2.7/site-packages/yt-3.0dev-py2.7-linux-x86_64.egg/yt/__init__.pyc'" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's create a 2D structured mesh, whose grid points in x & y are [Cantor dust](http://en.wikipedia.org/wiki/Cantor_set#Cantor_dust). Most of the mesh will have water as a coolant. Some volume elements will have fuel, though." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def cantor(n):\n", " return [0.] + cant(0., 1., n) + [1.]\n", "\n", "def cant(x, y, n):\n", " if n == 0:\n", " return []\n", " new_pts = [2.*x/3. + y/3., x/3. + 2.*y/3.]\n", " return cant(x, new_pts[0], n-1) + new_pts + cant(new_pts[1], y, n-1)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "c5 = cantor(5)\n", "coords = [c5, c5, [0.0, 1.0]]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We access volume elements (`ve`) on the mesh through a volume element index (`ve_idx`). This index is a unique integer `i` that gives a sort ordering to an otherwise unorder mesh. The volume element index is defined on the range from 0 (inclusive) to the number of volumes in the mesh (exclusive). You may access a volume element's material through the `mats` attribute and indexing with `i`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "m = Mesh(structured_coords=coords, structured=True)\n", "fuel = from_atom_frac({'U235': 0.045, 'U238': 0.955, 'O16': 2.0}, density=10.7)\n", "cool = from_atom_frac({'H1': 2.0, 'O16': 1.0}, density=1.0)\n", "for i in range(len(m)):\n", " m.mats[i] = cool\n", "m.mats[len(m)/2] = fuel\n", "m.mats[len(m)/4] = fuel\n", "for i, c in enumerate(c5[:-1]):\n", " m.mats[i*len(c5)] = fuel" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Tags" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Tag`s - sometimes known as fields - are generic way of storing data on a mesh. Tags can be accessed as attributes on the mesh class itself. For example, the density tag may be grabbed via:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "m.density " ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "MaterialPropertyTag(name='density', doc='the density [g/cc]')" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get the value of the density, you have to provide the location by its index." ] }, { "cell_type": "code", "collapsed": false, "input": [ "m.density[42]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "1.0" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also slice, mask, or fancy index tags:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print m.density[::100] # slice" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 10.7 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n", " 1. 1. 1. 1. 10.7 1. 1. 1. 1. 1. 1. 1.\n", " 1. 1. 1. 1. 1. 1. 1. 1. 10.7 1. 1. 1.\n", " 1. 1. 1. 1. ]\n" ] } ], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "print m.density[m.density[:] >= 10] # mask" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7\n", " 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7\n", " 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7\n", " 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7\n", " 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7 10.7\n", " 10.7 10.7 10.7 10.7]\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "print m.density[[10, 0, 11, 100]] # fancy index is fancy" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[ 1. 10.7 1. 1. ]\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a list of all current tag names, uses the `tags` dictionary on the mesh:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "m.tags.keys()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "['sub_act',\n", " 'sub_lan',\n", " 'number_density',\n", " 'density',\n", " 'comp',\n", " 'expand_elements',\n", " 'to_atom_frac',\n", " 'molecular_weight',\n", " 'mass_density',\n", " 'atoms_per_mol',\n", " 'sub_tru',\n", " 'mass',\n", " 'attrs',\n", " 'mult_by_mass',\n", " 'sub_ma',\n", " 've_idx',\n", " 'sub_fp']" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Analysis & Visulaization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PyNE meshes are supported by the [yt project](http://yt-project.org/). Use yt's plotting infrastructure to display tags on your mesh." ] }, { "cell_type": "code", "collapsed": false, "input": [ "pf = PyneMoabHex8StaticOutput(m)\n", "s = SlicePlot(pf, 'z', 'density', origin='native')\n", "s.display()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Computed Tags" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Computed tags - also known as 'derived fields' - are a way of having a lazily evaluated 'virtual tag.' A computed tag is defined with a function, lambda, or any other callable object. The function must have the following interface:\n", "\n", "```python\n", "def f(mesh, i):\n", " \"\"\"mesh is a pyne.mesh.Mesh() object and i is the volume element index\n", " to compute.\n", " \"\"\"\n", " # ... do some work ...\n", " return anything_you_want\n", "```\n", "\n", "Here is a somewhat silly example which squares the density. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "m.density2 = ComputedTag(lambda mesh, i: mesh.density[i]**2)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the bounds on the color bar have changed." ] }, { "cell_type": "code", "collapsed": false, "input": [ "pf = PyneMoabHex8StaticOutput(m)\n", "s = SlicePlot(pf, 'z', 'density2', origin='native')\n", "s.display()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a more serious example which uses PyNE's cross section tools to compute the one-group total cross section $\\sigma_t$ everywhere on the mesh." ] }, { "cell_type": "code", "collapsed": false, "input": [ "m.sigma_t = ComputedTag(lambda mesh, i: sigma_t(mesh.mats[i], group_struct=[10.0, 1e-6], phi_g=[1.0])[0])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "pf = PyneMoabHex8StaticOutput(m)\n", "s = SlicePlot(pf, 'z', 'sigma_t', origin='native')\n", "s.display()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Yes, that was only one line of code." ] } ], "metadata": {} } ] }