Skip to content

Conversation

@JonathanMaes
Copy link
Contributor

@JonathanMaes JonathanMaes commented Jan 19, 2026

This PR simplifies the process of switching between single and double floating-point precision.
Previously, the precision had to be chosen before compilation, by editing CMakeLists.txt.
Now, mumax+ compiles two binaries: one for single and one for double precision (unless MUMAXPLUS_FP_PRECISION is set, see below). The appropriate binary is then loaded during the import mumaxplus statement.

Two switches control the floating-point precision, as listed below in descending order of priority. Both can take the values SINGLE/1/32 for single precision and DOUBLE/2/64 for double precision.

  • A command-line flag --mumaxplus-fp-precision passed to the python command.
    • Example: python some_mumax_script.py --mumaxplus-fp-precision DOUBLE.
  • The environment variable MUMAXPLUS_FP_PRECISION.
    • Setting this will also affect the compilation: mumax+ will then only be compiled for one kind of floating point precision. This can be useful to reduce compile time during development, but passing the opposite precision with --mumaxplus-fp-precision will then result in a ModuleNotFoundError.

The used precision can then be accessed as mumaxplus.FP_PRECISION.

Note

Since the choice of precision must be made before the import mumaxplus statement, it is not possible to use single precision in one part of a script and double precision in another: these must be run in separate processes. Reloading mumax+ with a different floating point precision during runtime may lead to a vast wealth of errors.

The unit test is based on examples/errorscaling.py, since the error scaling is highly dependent on the floating point precision.

@JonathanMaes JonathanMaes added the enhancement New feature or request label Jan 19, 2026
Copy link
Collaborator

@ilateur ilateur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone (or some-multiple) should test this on linux

Copy link
Contributor

@DiegoDeGusem DiegoDeGusem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my linux device the code compiles and the test succeeds 🎉
I also agree with @ilateur's comment about a larger description.

@JonathanMaes
Copy link
Contributor Author

A webpage was added in the Tutorials section, explaining how to choose a precision, what errors can occur, and showcasing the effect on the error floor.

Copy link
Contributor

@lamoreel lamoreel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was able to successfully compile with a single (no pun intended) and with both precisions on a Linux machine 🌈 .
I was also able to choose the precision to be used by both the flag and the env var, hence my pending approval.

Most of my comments are about documentation

Comment on lines +222 to +224
"# Restart the kernel so we can load mumax+ again with a different precision.\n",
"# This likely gives a popup saying that the kernel was stopped or will restart.\n",
"get_ipython().kernel.do_shutdown(restart=True)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work for me 😕 . (The kernel dies, but refuses to restart when trying to execute the next cell).

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], [line 8]
        5 import numpy as np
        6 from math import acos, atan, pi, exp, tan, sin, cos, sqrt
  ----> 8 from mumaxplus import *
        9 from mumaxplus.util import *
       12 def magnetic_moment_precession(time, initial_magnetization, hfield_z, damping):

ModuleNotFoundError: No module named 'mumaxplus'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the kernel restarts, but not in the right conda env? The imports before import mumaxplus work just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants