{ "cells": [ { "cell_type": "markdown", "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "source": [ "# Deriving an energy term\n", "\n", "All energy terms in `micromagneticmodel` are in `micromagneticmodel/energy` directory. They are all derived from `micromagneticmodel.energy.EnergyTerm` base class.\n", "\n", "For instance, let us say we want to implement an energy term with following specifications:\n", "\n", "| property | value |\n", "| --- | --- |\n", "| name | `SpecialEnergy` |\n", "| expression | $U\\mathbf{m}\\cdot\\mathbf{m}$ |\n", "| parameter | $U$ |\n", "| parameter properties | $U \\ge 0$, can be spatially varying |\n", "\n", "The energy term class would be:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import micromagneticmodel as mm\n", "\n", "\n", "class SpecialEnergy(mm.EnergyTerm):\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", " se = SpecialEnergy(U=3)\n", "except TypeError:\n", " print(\"Exception raised.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An exception was raised because `effective_field`, `_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 SpecialEnergy(mm.EnergyTerm):\n", " _reprlatex = r\"$U\\mathbf{m}\\cdot\\mathbf{m}$\"\n", " _allowed_attributes = [\"U\"]\n", "\n", " def effective_field(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": [ "$$U\\mathbf{m}\\cdot\\mathbf{m}$$" ], "text/plain": [ "SpecialEnergy(U=3)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "se = SpecialEnergy(U=3)\n", "se" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The energy 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 $U$ values are allowed 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(U=ts.Parameter(descriptor=ts.Scalar(unsigned=True), otherwise=df.Field))\n", "class SpecialEnergy(mm.EnergyTerm):\n", " _reprlatex = r\"$U\\mathbf{m}\\cdot\\mathbf{m}$\"\n", " _allowed_attributes = [\"U\"]\n", "\n", " def effective_field(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", " se = SpecialEnergy(U=-3, name=\"valid_name\") # negative U\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": 7, "metadata": {}, "outputs": [], "source": [ "se = SpecialEnergy(U=5e-5)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5e-05" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "se.U" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$$U\\mathbf{m}\\cdot\\mathbf{m}$$" ], "text/plain": [ "SpecialEnergy(U=5e-05)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "se" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "editable": true, "slideshow": { "slide_type": "" }, "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'SpecialEnergy(U=5e-05)'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(se)" ] } ], "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 }