{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial 01: Deriving an energy 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 energy terms in `micromagneticmodel` are in `micromagneticmodel/hamiltonian` directory. They are all derived from `micromagneticmodel.hamiltonian.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", "class SpecialEnergy(mm.EnergyTerm):\n", " def __init__(self, U, name='specialenergy'):\n", " self.U = U\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", " se = SpecialEnergy(U=3)\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 SpecialEnergy(mm.EnergyTerm):\n", " _latex = r'$U\\mathbf{m}\\cdot\\mathbf{m}$'\n", "\n", " def __init__(self, U, name='specialenergy'):\n", " self.U = U\n", " self.name = name\n", "\n", " @property\n", " def _repr(self):\n", " return f'SpecialEnergy(U={self.U}, 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": [ "$U\\mathbf{m}\\cdot\\mathbf{m}$" ], "text/plain": [ "SpecialEnergy(U=3, name='specialenergy')" ] }, "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 ubermagutil.typesystem as ts\n", "import discretisedfield as df\n", "\n", "\n", "@ts.typesystem(U=ts.Parameter(descriptor=ts.Scalar(unsigned=True), otherwise=df.Field),\n", " name=ts.Name())\n", "class SpecialEnergy(mm.EnergyTerm):\n", " _latex = r'$U\\mathbf{m}\\cdot\\mathbf{m}$'\n", "\n", " def __init__(self, U, name='specialenergy'):\n", " self.U = U\n", " self.name = name\n", "\n", " @property\n", " def _repr(self):\n", " return f'SpecialEnergy(U={self.U}, 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", " se = SpecialEnergy(U=-3, name='valid_name') # negative U\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", " se = SpecialEnergy(U=3, name='invalid name') # name contains space\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": [ "se = SpecialEnergy(U=5e-5)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5e-05" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "se.U" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'specialenergy'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "se.name" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$U\\mathbf{m}\\cdot\\mathbf{m}$" ], "text/plain": [ "SpecialEnergy(U=5e-05, name='specialenergy')" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "se" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"SpecialEnergy(U=5e-05, name='specialenergy')\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "repr(se)" ] }, { "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": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception raised.\n" ] } ], "source": [ "try:\n", " se._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 }