{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Deriving evolver and driver classes\n", "\n", "In `micromagneticmodel` package, base classes `micromagneticmodel.Driver` and `micromagneticmodel.Evolver` are defined. Their purpose is to build individual evolvers and drivers in a particular micromagnetic calculator. In this tutorial, we will demonstrate some of their basic properties, on an example of a `micromagneticmodel.Driver` class. The behaviour of the `micromagneticmodel.Evolver` class is the same.\n", "\n", "Let us derive `MyDriver` class. In order to define it, `_allowed_attributes` list must be defined. It is a list of strings, which lists the kwargs which are allowed to be passed. Additionally, the driver must define a property `_x` for the independent variable (e.g. time)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import micromagneticmodel as mm\n", "\n", "\n", "class MyDriver(mm.Driver):\n", " _allowed_attributes = [\"arg1\", \"arg2\"]\n", "\n", " @property\n", " def _x(self):\n", " return \"t\"\n", "\n", " def drive(system):\n", " return system" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`Driver` class does not require any parameters to be passed at initialisation. If a keyword argument is from `_allowed_kwargs` list, it will be assigned as a class attribute. Otherwise, `AttributeError` will be raised." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "driver = MyDriver(arg1=1, arg2=\"value\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The attributes are" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "driver.arg1" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'value'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "driver.arg2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we try to pass a keyword argument at initialisation, which is not in the `_allowed_kwargs` list, `AttributeError` is rased:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " driver = MyDriver(arg3=1)\n", "except AttributeError:\n", " print(\"Exception raised.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main driver method which must be implemented by a derived class is `drive`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "try:\n", " driver.drive()\n", "except NotImplementedError:\n", " print(\"Exception raised.\")" ] }, { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "To build a driver for an existing external package class `micromagneticmodel.ExternalDriver` should be used. This class provides an additional method `schedule` to submit a calculation job to a scheduling system.\n", "\n", "Furthermore, it has a generic implementation for `drive` and `schedule` that consists of the following steps:\n", "- Create a directory to save the input and output in.\n", "- Write the input files for the existing simulation package.\n", "- Call the existing simulation package to run the simulation. / Schedule a job to run the simulation.\n", "- Update the `system` object with the simulation results (`drive` only).\n", "\n", "Simulation-package specific things such as writing input files and reading results must be implemented in a number of pre-defined abstract methods inside the derived class. Refer to the API reference for more details." ] } ], "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.10.12" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }