Container#

class micromagneticmodel.abstract.Container(terms=None)#

Container abstract class.

Container can be initialised with a list of either energy or dynamics terms.

Parameters:

terms (list, optional) – A list of either energy or dynamics terms. Defaults to None. If terms is not passed, an empty container is initialised.

Examples

  1. Defining energy terms container.

>>> import micromagneticmodel as mm
...
>>> terms = [mm.Exchange(A=1e-12), mm.Demag()]
>>> energy = mm.Energy(terms=terms)
>>> len(energy)  # the number of terms
2
  1. Defining dynamics terms container, by adding terms individually.

>>> dynamics = mm.Dynamics()
>>> len(dynamics)
0
>>> dynamics += mm.Precession(gamma0=mm.consts.gamma0)
>>> len(dynamics)
1
>>> dynamics += mm.Damping()
>>> len(dynamics)
2

Methods

__add__

Binary + operator.

__contains__

Determine whether a term of the same type as item is in the container.

__dir__

Extension of the dir(self) list.

__eq__

Relational operator ==.

__getattr__

Accessing an individual term from the container.

__getitem__

Access individual terms from the container by index.

__iter__

Generator yielding all terms in the container.

__len__

The number of terms in the container.

__repr__

Representation string.

__sub__

Binary - operator.

get

Return a list of all terms of type type in the container.


__add__(other)#

Binary + operator.

It can be applied only between micromagneticmodel.abstract.Term or micromagneticmodel.abstract.TermsContainer objects. If the term with the same name is already present in the container ValueError is raised.

Parameters:

other (micromagneticmodel.abstract.Term, TermsContainer) – Second operand.

Returns:

Resulting container.

Return type:

micromagneticmodel.abstract.TermContainer

Raises:
  • TypeError – If the operator cannot be applied.

  • ValueError – If the term with the same name is already present in the container.

Examples

  1. Binary operator +.

>>> import micromagneticmodel as mm
...
>>> dynamics = mm.Dynamics()
>>> dynamics += mm.Precession(gamma0=mm.consts.gamma0)
>>> dynamics += mm.Damping(alpha=0.2)
>>> len(dynamics)
2
__contains__(item)#

Determine whether a term of the same type as item is in the container.

Parameters:

item (micromagneticmodel.abstract.Term) – Energy or dynamics term.

Returns:

True if term of the same type as item is in the container and False otherwise.

Return type:

bool

Example

  1. Checking if the container contains a term.

>>> import micromagneticmodel as mm
...
>>> exchange = mm.Exchange(A=1e-12)
>>> demag = mm.Demag()
>>> energy = mm.Energy(terms=[exchange, demag])
>>> exchange in energy
True
>>> demag in energy
True
>>> mm.Zeeman(H=(0, 0, 1)) in energy
False

A check with a different term is only True if all attributes (e.g. exchange constant A and name) match one of the terms in the energy equation

>>> mm.Exchange(A=5e-11) in energy
False
>>> mm.Exchange(A=1e-12, name="my_exchange") in energy
False
__dir__()#

Extension of the dir(self) list.

Adds the names of terms in the container to the list of attributes.

Returns:

Avalilable attributes.

Return type:

list

Examples

  1. Checking the list of attributes using dir().

>>> import micromagneticmodel as mm
...
>>> dynamics = mm.Dynamics()
>>> 'precession' in dir(dynamics)
False
>>> 'damping' in dir(dynamics)
False
>>> dynamics += mm.Precession(gamma0=mm.consts.gamma0)
>>> 'precession' in dir(dynamics)
True
>>> dynamics += mm.Damping(alpha=0.2)
>>> 'damping' in dir(dynamics)
True
__eq__(other)#

Relational operator ==.

Two containers are considered to be equal if they have the same number of terms and the same types of terms in them.

Parameters:

other (micromagneticmodel.TermsContainer) – Second operand.

Returns:

True if two container have the same number of terms and the same types of terms in them and False otherwise.

Return type:

bool

Examples

  1. Comparing term containers.

>>> import micromagneticmodel as mm
...
>>> exchange = mm.Exchange(A=1e-12)
>>> demag = mm.Demag()
>>> energy1 = mm.Energy(terms=[exchange, demag])
>>> energy2 = mm.Energy(terms=[demag])
>>> energy1 == energy1
True
>>> energy1 == energy2
False
>>> energy1 != energy2
True
__getattr__(attr)#

Accessing an individual term from the container.

Each term in the container can be accessed using its name. The name of the term is the same as the name of its class in lowercase. For example, for micromagneticmodel.DMI, the name is dmi.

Parameters:

attr (str) – The name of the term.

Returns:

Term from the container.

Return type:

micromagneticmodel.Term

Raises:

AttributeError – If attr is not in the container.

Examples

  1. Accessing individual terms from the container.

>>> import micromagneticmodel as mm
...
>>> dynamics = mm.Dynamics()
>>> dynamics += mm.Precession(gamma0=500)
>>> dynamics += mm.Damping(alpha=0.2)
>>> dynamics.precession
Precession(gamma0=500)
>>> dynamics.damping
Damping(alpha=0.2)
>>> dynamics.stt
Traceback (most recent call last):
...
AttributeError: ...
__getitem__(index)#

Access individual terms from the container by index.

Parameters:

item (int) – Index of the term.

Returns:

Term from the container.

Return type:

micromagneticmodel.Term

Examples

  1. Accessing individual terms from the container.

>>> import micromagneticmodel as mm
...
>>> dynamics = mm.Dynamics()
>>> dynamics += mm.Precession(gamma0=500)
>>> dynamics += mm.Damping(alpha=0.2)
>>> dynamics[0]
Precession(gamma0=500)
>>> dynamics[1]
Damping(alpha=0.2)
>>> dynamics[-1]
Damping(alpha=0.2)
>>> dynamics[3]
Traceback (most recent call last):
...
IndexError: ...
__iter__()#

Generator yielding all terms in the container.

Yields:

micromagneticmodel.abstract.Term – Term in the container.

Examples

  1. Iterating energy terms container.

>>> import micromagneticmodel as mm
...
>>> terms = [mm.Exchange(A=1e-12), mm.Demag()]
>>> energy = mm.Energy(terms=terms)
>>> for term in energy:
...     print(term)
Exchange(A=1e-12)
Demag()
__len__()#

The number of terms in the container.

Returns:

The number of terms.

Return type:

int

Examples

  1. Getting the number of terms in the container.

>>> import micromagneticmodel as mm
...
>>> dynamics = mm.Dynamics()
>>> len(dynamics)
0
>>> dynamics += mm.Precession(gamma0=mm.consts.gamma0)
>>> len(dynamics)
1
>>> dynamics += mm.Damping(alpha=0.2)
>>> len(dynamics)
2
__repr__()#

Representation string.

Returns:

Representation string.

Return type:

str

Examples

  1. Getting representation string.

>>> import micromagneticmodel as mm
...
>>> exchange = mm.Exchange(A=1e-12)
>>> demag = mm.Demag()
>>> energy = exchange + demag
>>> repr(energy)
'Exchange(A=1e-12) + Demag()'
__sub__(other)#

Binary - operator.

It can be applied only between micromagneticmodel.abstract.TermsContainer and micromagneticmodel.abstract.Term. It removes the term with the same name from the container.

Parameters:

other (micromagneticmodel.abstract.Term) – Second operand.

Returns:

Resulting container.

Return type:

micromagneticmodel.abstract.TermContainer

Raises:
  • TypeError – If the operator cannot be applied.

  • ValueError – If the term with the same name is not present in the container.

Examples

  1. Binary operator -.

>>> import micromagneticmodel as mm
...
>>> dynamics = mm.Dynamics()
>>> dynamics += mm.Precession(gamma0=mm.consts.gamma0)
>>> len(dynamics)
1
>>> damping = mm.Damping(alpha=0.2)
>>> dynamics += damping
>>> len(dynamics)
2
>>> dynamics -= damping
>>> len(dynamics)
1
>>> damping in dynamics
False
get(*, type)#

Return a list of all terms of type type in the container.

__hash__ = None#