{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1.5 signac-flow Ideal Gas Example\n", "\n", "## About\n", "\n", "This notebook contains a minimal example for running a signac-flow project from scratch.\n", "The example demonstrates how to compare an ideal gas with a Lennard-Jones fluid by calculating a p-V phase diagram.\n", "\n", "## Author\n", "\n", "Carl Simon Adorf\n", "\n", "## Before you start\n", "\n", "Make sure you installed signac and signac-flow, e.g., with:\n", "\n", "```\n", "conda install -c conda-forge signac signac-flow\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import flow\n", "import numpy as np\n", "import signac\n", "\n", "# Enter the signac project directory\n", "project = signac.init_project(\"projects/tutorial-signac-flow\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to generate a pressure-volume (p-V) phase diagram for an ideal gas.\n", "\n", "We define a function to calculate the result for a given state point:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def V_idg(N, p, kT):\n", " return N * kT / p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For this demonstration we will specialize a `flow.FlowProject` to manage our simple *workflow*.\n", "\n", "The workflow will contain one label and one operation.\n", " - The label function allows us to *label* our jobs in the project *status*. This is especially important for understanding the state of large projects with expensive operations.\n", " - The operation function `compute_volume` is defined with a postcondition and will act on each job. This operation is eligible for execution if its postcondition is unmet (`False`)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class MyProject(flow.FlowProject):\n", " pass\n", "\n", "\n", "@MyProject.label\n", "def estimated(job):\n", " return \"V\" in job.document\n", "\n", "\n", "@MyProject.post(estimated)\n", "@MyProject.operation\n", "def compute_volume(job):\n", " job.document[\"V\"] = V_idg(**job.statepoint())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need to use the `get_project()` *class method* to get a project handle for this special project class." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "project = MyProject.get_project(\"projects/tutorial-signac-flow\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now it's time to actually generate some data! Let's initialize the data space!\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a14ee3104f26974b22bb84f62500c84d\n", "55d4d6ebbc04a1f8a259a2c5de197328\n", "4b299db86ff480f92e520eac91112108\n", "26e2f19b5b211edb80ebe60a5a62bd75\n", "75c10d0fa7a4f4b4742ce5e9c119e8bd\n", "46859989efcfd89653fa65db2375884b\n", "f3f3c351f81acd2140edd239b9183af4\n", "b38e30524c2c36e3ebfe50481eb91992\n", "bb277ac022ce744d38f9e87c1fabe08c\n", "6f9bc79d670e42d4a40041ce7be9cdaf\n" ] } ], "source": [ "for p in np.linspace(0.5, 5.0, 10):\n", " sp = dict(N=1728, kT=1.0, p=float(p))\n", " job = project.open_job(sp)\n", " print(job.id)\n", " job.init()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `print_status()` function allows to get a quick overview of our project's *status*:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c310b2b2815f4d34bf4c1be4268f0a2e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Fetching status: 0%| | 0/10 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%matplotlib inline\n", "\n", "from matplotlib import pyplot as plt\n", "\n", "V = {}\n", "\n", "for job in project:\n", " V[job.statepoint()[\"p\"]] = job.document[\"V\"]\n", "\n", "p = sorted(V.keys())\n", "V = [V[p_] for p_ in p]\n", "print(V)\n", "\n", "plt.plot(p, V, label=\"idG\")\n", "plt.xlabel(r\"pressure [$\\epsilon / \\sigma^3$]\")\n", "plt.ylabel(r\"volume [$\\sigma^3$]\")\n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uncomment and execute the following line to remove all data and start over." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# %rm -r projects/tutorial-signac-flow/workspace" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12.0" } }, "nbformat": 4, "nbformat_minor": 4 }