mammos_entity quickstart#

Some definitions#

  • a mammos_units.Quantity is an object that carries a value and units.

  • a mammos_entity.Entity is a quantity, which in addition links its definition in the EMMO ontology and the MaMMoS additions for magnetic materials

[1]:
import mammos_entity as me

Creating entities#

For entities that are important in MaMMoS, there are convenient attributes to define those. For example for the saturation magnetisation Ms:

[2]:
Ms = me.Ms(800e3)  # defines Ms = 8e5 A/m  (default units are SI, i.e. A/m here)
[3]:
Ms
[3]:
SpontaneousMagnetization(value=800000.0, unit=A / m)

If no units are provided, then the ontology units are used, such as A/m in the example above. These units are SI units without numerical prefactors (such as kilo, milli, etc.)

If units are provided, these are compared with the units expected for that entity. An error is raised if they do not match. This is good practice for extra clarity.

[4]:
M1 = me.Ms(800e3, "A/m")
M1
[4]:
SpontaneousMagnetization(value=800000.0, unit=A / m)

Providing units can be useful, if numerical prefactors are used (such as kilo):

[5]:
M2 = me.Ms(800, "kA/m")
M2
[5]:
SpontaneousMagnetization(value=800.0, unit=kA / m)
[6]:
M1 == M2
[6]:
np.True_

We can create an entity from a Quantity as well:

[7]:
import mammos_units as u

Ms_quantity = 800e3 * u.A / u.m
Ms_quantity
[7]:
$800000 \; \mathrm{\frac{A}{m}}$
[8]:
me.Ms(Ms_quantity)
[8]:
SpontaneousMagnetization(value=800000.0, unit=A / m)

If we like to express spontaneous magnetization in Tesla, we can convert it to the required A/m:

[9]:
me.Ms(1.2, "T")  # Tesla not compatible with A/m
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 me.Ms(1.2, "T")  # Tesla not compatible with A/m

File ~/git/mammos-devtools/packages/mammos-entity/src/mammos_entity/_entities.py:35, in Ms(value, unit)
     20 def Ms(
     21     value: int | float | typing.ArrayLike = 0, unit: None | str = None
     22 ) -> mammos_entity.Entity:
     23     """Create an Entity representing the spontaneous magnetization (Ms).
     24
     25     Args:
   (...)     33
     34     """
---> 35     return Entity("SpontaneousMagnetization", value, unit)

File ~/git/mammos-devtools/packages/mammos-entity/src/mammos_entity/_base.py:124, in Entity.__new__(cls, ontology_label, value, unit, **kwargs)
    122 if (si_unit is not None) and (unit is not None):
    123     if not u.Unit(si_unit).is_equivalent(unit):
--> 124         raise TypeError(
    125             f"The unit {unit} does not match the units of {ontology_label}"
    126         )
    127 elif (si_unit is not None) and (unit is None):
    128     with u.add_enabled_aliases({"Cel": u.K, "mCel": u.K}):

TypeError: The unit T does not match the units of SpontaneousMagnetization
[10]:
# enable implicit conversion from here on:
u.set_enabled_equivalencies(u.magnetic_flux_field())

Js = 1.2 * u.T  # Quantity in Tesla

me.Ms(Js.to("A/m"))  # Convert Quantity to A/m and create entity
[10]:
SpontaneousMagnetization(value=954929.6580315315, unit=A / m)

Access to ontology#

Each entity object knows its label in the ontology:

[11]:
Ms.ontology_label
[11]:
'SpontaneousMagnetization'

The ontology attribute links to an owlready2 object created by the EMMOntoPy package.

When saving data to a file, this ontology_label_with_iri might be useful to save in the metadata as it returns a string containing the ontology label together with the unique identifier of the ontology entry (IRI is the Internationalized Resource Identifier).

[12]:
Ms.ontology_label_with_iri
[12]:
'SpontaneousMagnetization https://w3id.org/emmo/domain/magnetic_material#EMMO_032731f8-874d-5efb-9c9d-6dafaa17ef25'

We can use all attributes of the ontology object through Ms.ontology:

[13]:
Ms.ontology.get_annotations()
[13]:
{'prefLabel': [locstr('SpontaneousMagnetization', 'en')],
 'elucidation': [locstr('The spontaneous magnetization, Ms, of a ferromagnet is the result\nof alignment of the magnetic moments of individual atoms. Ms exists\nwithin a domain of a ferromagnet.', 'en')],
 'altLabel': ['Ms'],
 'wikipediaReference': ['https://en.wikipedia.org/wiki/Spontaneous_magnetization'],
 'IECEntry': ['https://www.electropedia.org/iev/iev.nsf/display?openform&ievref=221-02-41']}
[14]:
Ms.ontology.get_class_properties()
[14]:
{emmo.hasMeasurementUnit,
 magnetic_material_mammos.IECEntry,
 core.prefLabel,
 magnetic_material_mammos.wikipediaReference,
 core.altLabel,
 emmo.elucidation}
[15]:
print(Ms.ontology.elucidation[0])
The spontaneous magnetization, Ms, of a ferromagnet is the result
of alignment of the magnetic moments of individual atoms. Ms exists
within a domain of a ferromagnet.

Entities behaves like Quantity#

Each Entity has all the attributes and methods that are available for Quantities. See mammos_units examples for details.

[16]:
Ms.value
[16]:
np.float64(800000.0)
[17]:
Ms.unit
[17]:
$\mathrm{\frac{A}{m}}$
[18]:
Ms.to("kA/m")
[18]:
SpontaneousMagnetization(value=800000.0, unit=A / m)

Numerical operations with entities result in quantities (because EMMOntoPy does not support this):

[19]:
Ms**2
[19]:
$6.4 \times 10^{11} \; \mathrm{\frac{A^{2}}{m^{2}}}$

To convent an entity to a quantity, we can use the quantity attribute:

[20]:
Ms.quantity
[20]:
$800000 \; \mathrm{\frac{A}{m}}$

Defining vector entities (Example Zeeman field)#

[21]:
H = me.H([1e4, 1e4, 1e4], "A/m")
H
[21]:
ExternalMagneticField(value=[10000. 10000. 10000.], unit=A / m)
[22]:
H.ontology
[22]:
magnetic_material_mammos.ExternalMagneticField
[23]:
print(H.ontology.elucidation[0])
The external field H′, acting on a sample that is produced by
electric currents or the stray field of magnets outside the sample
volume, is often called the applied field.

Does mammos_entity not provide your preferred entity?#

The list of convenience attributes is at https://mammos-project.github.io/mammos/_autosummary/mammos_entity.html

If the desired entity is not available, we can search the EMMO ontology (including the magnetic_material_mammos additions), for example for entity labels containing the string “Magnetization”:

[24]:
me.mammos_ontology.get_by_label_all("*Field*")
[24]:
{emmo.ElectricDisplacementFieldUnit,
 emmo.ElectricFieldStrength,
 emmo.ElectricFieldStrengthUnit,
 emmo.MagneticFieldStrength,
 emmo.MagneticFieldStrengthUnit,
 magnetic_material_mammos.AnisotropyField,
 magnetic_material_mammos.DemagnetizingField,
 magnetic_material_mammos.ExternalMagneticField,
 magnetic_material_mammos.InternalMagneticField,
 magnetic_material_mammos.KneeField,
 magnetic_material_mammos.KneeFieldExternal,
 magnetic_material_mammos.SwitchingFieldCoercivity,
 magnetic_material_mammos.SwitchingFieldCoercivityExternal}

Once identified the right label, we create an entity like this:

[25]:
me.Entity("ElectricFieldStrength", value=230)
[25]:
ElectricFieldStrength(value=230.0, unit=kg m / (A s3))