Skip to content

Quickstart

This example constructs one tetrahedron, assigns a magnetic material, computes the static fields, and writes both averaged and spatial output. It is deliberately small: its purpose is to verify the complete software path, not to represent a well-resolved physical body.

Download quickstart.py

quickstart.py
"""Small, self-contained Nmag simulation used by the quickstart guide."""

from pathlib import Path

import nmag
import nmesh


output_directory = Path("results")
output_directory.mkdir(exist_ok=True)

mesh = nmesh.mesh_from_points_and_simplices(
    points=[
        [0.0, 0.0, 0.0],
        [1.0, 0.0, 0.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, 1.0],
    ],
    simplices_indices=[[0, 1, 2, 3]],
    simplices_regions=[1],
)
mesh_path = output_directory / "quickstart.nmesh.h5"
mesh.save(mesh_path)

permalloy = nmag.MagMaterial(
    name="Py",
    Ms=nmag.SI(1e6, "A/m"),
    exchange_coupling=nmag.SI(13e-12, "J/m"),
)

config = nmag.NmagConfig(
    output_directory=output_directory,
    output_policy="replace",
)
simulation = nmag.Simulation(name="quickstart", config=config)
simulation.load_mesh(
    str(mesh_path),
    [("magnetic", permalloy)],
    unit_length=nmag.SI(1e-9, "m"),
)
simulation.set_m([1.0, 0.0, 0.0])
simulation.set_H_ext([0.0, 0.0, 0.0], nmag.SI("A/m"))
simulation.save_data(fields="all")

print("Average demagnetization field:", simulation.get_subfield_average("H_demag"))
print("Results written to:", output_directory.resolve())

Run it from an environment where nmag-python-3 is installed:

.venv/bin/python quickstart.py

The script creates a results/ directory containing:

  • quickstart.nmesh.h5, the generated mesh;
  • quickstart_dat.ndt, a tab-separated row of averaged quantities; and
  • quickstart_dat.h5, spatial field arrays and mesh metadata.

The mesh coordinates are dimensionless until load_mesh applies unit_length. Here one coordinate unit is one nanometre. Material parameters and fields use explicit SI quantities.

output_policy="replace" makes this learning example safe to rerun. For normal work, the default "error" policy protects existing result files; use checkpoints when a run must continue from earlier state.

Next, use a resolved geometry in the sphere tutorial.