{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Implementing a new simulation code\n", "Appendix C in the pyiron paper\n", "https://www.sciencedirect.com/science/article/pii/S0927025618304786" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from os.path import join" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from pyiron_base import GenericJob, GenericParameters" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class ToyJob(GenericJob):\n", " def __init__(self, project, job_name):\n", " super().__init__(project, job_name) \n", " self.input = GenericParameters(table_name=\"input\")\n", " self.input['input_energy'] = 100\n", " self.executable = \"cat input > output\"\n", "\n", " def write_input(self): \n", " self.input.write_file( \n", " file_name=\"input\",\n", " cwd=self.working_directory\n", " )\n", "\n", " def collect_output(self):\n", " file = join(self.working_directory, \"output\") \n", " with open(file) as f:\n", " line = f.readlines()[0]\n", " energy = float(line.split()[1]) \n", " with self.project_hdf5.open(\"output/generic\") as h5out: \n", " h5out[\"energy_tot\"] = energy\n", "\n", " def to_hdf(self, hdf=None, group_name=None): \n", " super().to_hdf(\n", " hdf=hdf,\n", " group_name=group_name\n", " )\n", " with self.project_hdf5.open(\"input\") as h5in:\n", " self.input.to_hdf(h5in)\n", "\n", " def from_hdf(self, hdf=None, group_name=None): \n", " super().from_hdf(\n", " hdf=hdf,\n", " group_name=group_name\n", " )\n", " with self.project_hdf5.open(\"input\") as h5in:\n", " self.input.from_hdf(h5in)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " Parameter Value Comment\n", "0 input_energy 100 \n", "The job toy was saved and received the ID: 1\n" ] }, { "data": { "text/plain": [ "100.0" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from pyiron_base import Project\n", "pr = Project('test')\n", "pr.remove_jobs_silently(recursive=True)\n", "job = pr.create_job(job_type=ToyJob, job_name=\"toy\")\n", "print(job.input)\n", "job.run()\n", "job['output/generic/energy_tot']" ] } ], "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }