{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Deriving a dynamics term\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", "\n", "class SpecialDynamics(mm.DynamicsTerm):\n", " pass" ] }, { "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 `_reprlatex` and `_allowed_attributes` 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", " _reprlatex = r\"$\\xi\\mathbf{m}\\times\\mathbf{v}$\"\n", " _allowed_attributes = [\"xi\", \"v\"]\n", "\n", " def dmdt(self, m):\n", " raise NotImplementedError" ] }, { "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))" ] }, "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 discretisedfield as df\n", "import ubermagutil.typesystem as ts\n", "\n", "\n", "@ts.typesystem(\n", " xi=ts.Parameter(descriptor=ts.Scalar(unsigned=True), otherwise=df.Field),\n", " v=ts.Parameter(descriptor=ts.Vector(size=3), otherwise=df.Field),\n", ")\n", "class SpecialDynamics(mm.DynamicsTerm):\n", " _reprlatex = r\"$\\xi\\mathbf{m}\\times\\mathbf{v}$\"\n", " _allowed_attributes = [\"xi\", \"v\"]\n", "\n", " def dmdt(self, m):\n", " raise NotImplementedError" ] }, { "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))" ] }, { "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))" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sd" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'SpecialDynamics(xi=0.1, v=(0, 1, 1))'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(sd)" ] } ], "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 }