{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 02: Deriving a dynamics term\n", "\n", "> Interactive online tutorial:\n", "> [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ubermag/micromagneticmodel/master?filepath=docs%2Fipynb%2Findex.ipynb)\n", "\n", "All dynamics terms in `micromagneticmodel` are in `micromagneticmodel/dynamics` directory. They are all derived from `micromagneticmodel.dynamics.DynamicsTerm` base class.\n", "\n", "For instance, let us say we want to implement a dynamics term with following specifications:\n", "\n", "| property | value |\n", "| --- | --- |\n", "| name | `SpecialDynamics` |\n", "| expression | $\\xi\\mathbf{m}\\times\\mathbf{v}$ |\n", "| parameters | $\\xi$, $\\mathbf{v}$ |\n", "| parameter properties | $\\xi \\ge 0$, can be spatially varying |\n", "| | $\\mathbf{v}$ is three-dimensional, can be spatially varying |\n", "\n", "The dynamics term class would be:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import micromagneticmodel as mm\n", "\n", "class SpecialDynamics(mm.DynamicsTerm):\n", " def __init__(self, xi, v, name='specialdynamics'):\n", " self.xi = xi\n", " self.v = v\n", " self.name = name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can try to instantiate it" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " sd = SpecialDynamics(xi=0.1, v=(0, 0, 1))\n", "except TypeError:\n", " print('Exception raised.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An exception was raised because `_repr` and `_latex` properties must be implemented. Therefore, an extended implementation of the class is:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "class SpecialDynamics(mm.DynamicsTerm):\n", " _latex = r'$\\xi\\mathbf{m}\\times\\mathbf{v}$'\n", "\n", " def __init__(self, xi, v, name='specialdynamics'):\n", " self.xi = xi\n", " self.v = v\n", " self.name = name\n", "\n", " @property\n", " def _repr(self):\n", " return f'SpecialDynamics(xi={self.xi}, v={self.v}, name=\\'{self.name}\\')'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can try to instantiate the class again:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\xi\\mathbf{m}\\times\\mathbf{v}$" ], "text/plain": [ "SpecialDynamics(xi=0.1, v=(0, 0, 1), name='specialdynamics')" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sd = SpecialDynamics(xi=0.1, v=(0, 0, 1))\n", "sd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dynamics object is created. The last thing we have to impose on the energy class is the typesystem. More precisely, we have to make sure no negative $\\xi$ values are allowed, $\\mathbf{v}$ is three-dimensional and that `name` attribute accepts only valid Python variable names. This is done by using `ubermagutil`. Full documentation can be found [here](https://ubermagutil.readthedocs.io/en/latest/)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import ubermagutil.typesystem as ts\n", "import discretisedfield as df\n", "\n", "\n", "@ts.typesystem(xi=ts.Parameter(descriptor=ts.Scalar(unsigned=True), otherwise=df.Field),\n", " v=ts.Parameter(descriptor=ts.Vector(size=3), otherwise=df.Field),\n", " name=ts.Name())\n", "class SpecialDynamics(mm.DynamicsTerm):\n", " _latex = r'$\\xi\\mathbf{m}\\times\\mathbf{v}$'\n", "\n", " def __init__(self, xi, v, name='specialdynamics'):\n", " self.xi = xi\n", " self.v = v\n", " self.name = name\n", "\n", " @property\n", " def _repr(self):\n", " return f'SpecialDynamics(xi={self.xi}, v={self.v}, name=\\'{self.name}\\')'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we now attempt to pass invalid input arguments, exceptions are raised." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " sd = SpecialDynamics(xi=-0.1, v=(0, 0, 1)) # negative xi\n", "except ValueError:\n", " print('Exception raised.')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " sd = SpecialDynamics(xi=0.1, v=(0, 1)) # 2D v\n", "except ValueError:\n", " print('Exception raised.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some of the properties and methods of the implemented energy term are:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "sd = SpecialDynamics(xi=0.1, v=(0, 1, 1), name='specialdynamics')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sd.xi" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0, 1, 1)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sd.v" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'specialdynamics'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sd.name" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\xi\\mathbf{m}\\times\\mathbf{v}$" ], "text/plain": [ "SpecialDynamics(xi=0.1, v=(0, 1, 1), name='specialdynamics')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sd" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"SpecialDynamics(xi=0.1, v=(0, 1, 1), name='specialdynamics')\"" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(sd)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, the class we have just implemented is going to be inherited by a specific micromagnetic calculator, where `_script` method is going to be implemented." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " sd._script\n", "except NotImplementedError:\n", " print('Exception raised.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other\n", "\n", "Full description of all existing descriptors can be found in the [API Reference](https://micromagneticmodel.readthedocs.io/en/latest/?badge=latest)." ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 2 }