"This notebook demonstrates some basic post-processing tasks that can be performed with the Python API, such as plotting a 2D mesh tally and plotting neutron source sites from an eigenvalue calculation. The problem we will use is a simple reflected pin-cell."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from IPython.display import Image\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import openmc\n",
"from openmc.statepoint import StatePoint\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Generate Input Files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we need to define materials that will be used in the problem. Before defining a material, we must create nuclides that are used in the material."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Instantiate some Nuclides\n",
"h1 = openmc.Nuclide('H-1')\n",
"b10 = openmc.Nuclide('B-10')\n",
"o16 = openmc.Nuclide('O-16')\n",
"u235 = openmc.Nuclide('U-235')\n",
"u238 = openmc.Nuclide('U-238')\n",
"zr90 = openmc.Nuclide('Zr-90')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With the nuclides we defined, we will now create three materials for the fuel, water, and cladding of the fuel pin."
"With our three materials, we can now create a materials file object that can be exported to an actual XML file."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Instantiate a MaterialsFile, add Materials\n",
"materials_file = openmc.MaterialsFile()\n",
"materials_file.add_material(fuel)\n",
"materials_file.add_material(water)\n",
"materials_file.add_material(zircaloy)\n",
"materials_file.default_xs = '71c'\n",
"\n",
"# Export to \"materials.xml\"\n",
"materials_file.export_to_xml()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's move on to the geometry. Our problem will have three regions for the fuel, the clad, and the surrounding coolant. The first step is to create the bounding surfaces -- in this case two cylinders and six reflective planes."
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n"
"OpenMC requires that there is a \"root\" universe. Let us create a root cell that is filled by the pin cell universe and then assign it to the root universe."
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n",
"/usr/local/lib/python2.7/dist-packages/openmc-0.7.0-py2.7.egg/openmc/universe.py:199: DeprecationWarning: Cell.add_surface(...) has been deprecated and may be removed in a future version. The region for a Cell should be defined using the region property directly.\n"
"We now must create a geometry that is assigned a root universe, put the geometry into a geometry file, and export it to XML."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Create Geometry and set root Universe\n",
"geometry = openmc.Geometry()\n",
"geometry.root_universe = root_universe"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Instantiate a GeometryFile\n",
"geometry_file = openmc.GeometryFile()\n",
"geometry_file.geometry = geometry\n",
"\n",
"# Export to \"geometry.xml\"\n",
"geometry_file.export_to_xml()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With the geometry and materials finished, we now just need to define simulation parameters. In this case, we will use 10 inactive batches and 90 active batches each with 5000 particles."
"Let us also create a plot file that we can use to verify that our pin cell geometry was created successfully."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Instantiate a Plot\n",
"plot = openmc.Plot(plot_id=1)\n",
"plot.filename = 'materials-xy'\n",
"plot.origin = [0, 0, 0]\n",
"plot.width = [1.26, 1.26]\n",
"plot.pixels = [250, 250]\n",
"plot.color = 'mat'\n",
"\n",
"# Instantiate a PlotsFile, add Plot, and export to \"plots.xml\"\n",
"plot_file = openmc.PlotsFile()\n",
"plot_file.add_plot(plot)\n",
"plot_file.export_to_xml()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With the plots.xml file, we can now generate and view the plot. OpenMC outputs plots in .ppm format, which can be converted into a compressed format like .png with the convert utility."
"As we can see from the plot, we have a nice pin cell with fuel, cladding, and water! Before we run our simulation, we need to tell the code what we want to tally. The following code shows how to create a 2D mesh tally."
"Our simulation ran successfully and created a statepoint file with all the tally data in it. We begin our analysis here loading the statepoint file and 'reading' the results. By default, data from the statepoint file is only read into memory when it is requested. This helps keep the memory use to a minimum even when a statepoint file may be huge."
"The statepoint file actually stores the sum and sum-of-squares for each tally bin from which the mean and variance can be calculated as described [here](http://mit-crpg.github.io/openmc/methods/tallies.html#variance). The sum and sum-of-squares can be accessed using the ``sum`` and ``sum_sq`` properties:"
"However, the mean and standard deviation of the mean are usually what you are more interested in. The Tally class also has properties ``mean`` and ``std_dev`` which automatically calculate these statistics on-the-fly."
"The tally data has three dimensions: one for filter combinations, one for nuclides, and one for scores. We see that there are 10000 filter combinations (corresponding to the 100 x 100 mesh bins), a single nuclide (since none was specified), and two scores. If we only want to look at a single score, we can use the ``get_slice(...)`` method as follows."
"Now let's say we want to look at the distribution of relative errors of our tally bins for flux. First we create a new variable called ``relative_error`` and set it to the ratio of the standard deviation and the mean, being careful not to divide by zero in case some bins were never scored to."
"Source sites can be accessed from the ``source`` property. As shown below, the source sites are represented as a numpy array with a structured datatype."
"Now, we can look at things like the energy distribution of source sites. Note that we don't directly use the ``matplotlib.pyplot.hist`` method since our binning is logarithmic."
"Let's also look at the spatial distribution of the sites. To make the plot a little more interesting, we can also include the direction of the particle emitted from the source and color each source by the logarithm of its energy."