{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ignition Example\n", "\n", "This example uses `ctwrap` for ideal gas constant pressure reactor simulations (i.e. ignition) defined by the simulation module `ignition` (`ctwrap.modules.ignition`)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function/Class Definitions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import matplotlib as mpl\n", "import h5py\n", "import json\n", "col = mpl.rcParams['axes.prop_cycle'].by_key()['color']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import ctwrap as cw\n", "cw.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Simulations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create Simulation object\n", "\n", "Uses a module in the `modules` folder. Modules do not have to depend on `ctwrap`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sim = cw.Simulation.from_module(cw.modules.ignition)\n", "sim" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create Simulation Handler\n", "\n", "Parameters are specified via YAML file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create variation object\n", "fname = 'ignition.yaml'\n", "sh = cw.SimulationHandler.from_yaml(fname, verbosity=1)\n", "sh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run parallel simulation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%time\n", "sh.run_parallel(sim)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create graphical output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load data from file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = h5py.File(sh.output_name, 'r')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data.attrs.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var = json.loads(data.attrs['strategy'])\n", "var" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "default = json.loads(data.attrs['defaults'])\n", "default" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "phi = var['sequence'].get('initial.phi')\n", "phi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot\n", "\n", "Display time histories for gas temperature." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# create figure and adjust size\n", "fig, ax = plt.subplots(1) \n", "fig.set_size_inches(8.,8.)\n", "\n", "# plot results\n", "for f, key in enumerate(data):\n", " df = data[key]\n", " ax.plot(1000*df['t'][:], df['T'][:], color=col[f], label=phi[f]) \n", "\n", "# add title/axis labels\n", "ax.set_title(r'IdealGasConstPressure Reactor Simulation (ignition)')\n", "ax.set_xlabel('Time (ms)')\n", "ax.set_ylabel('Temperature (K)')\n", "\n", "# add legend\n", "leg = ax.legend(loc='upper left', title='Equivalence ratio')\n", "leg.draw_frame(False) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.8" } }, "nbformat": 4, "nbformat_minor": 2 }