Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runwhile support (limited) #84

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,19 @@ Licensed under the BSD 3-Clause "New" or "Revised" License. For details, please
- [OpenDreamKit](http://opendreamkit.org/) – Horizon 2020 European Research Infrastructure project (676541)

- EPSRC Programme Grant on [Skyrmionics](http://www.skyrmions.ac.uk) (EP/N032128/1)
-

## My changes
- Added TimeTorqueDriver with limited support of RunWhile() mumax3 simulation. Requires passing maxtorque parameter. The example and corresponding mx3 file lines are below.

```
import mumax3c as mc #import the package

max_torque_allowed = 0.005 # the desired value of MaxTorque to pass into RunWhile()
td = mc.TimeTorqueDriver() # set the driver
td.drive(system, maxtorque=max_torque_allowed, verbose=2) #start simulation
```

```
RunWhile(maxtorque > 0.005)
```
2 changes: 1 addition & 1 deletion mumax3c/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# from .compute import compute # compute is not yet supported
from .delete import delete
from .drivers import MinDriver, RelaxDriver, TimeDriver
from .drivers import MinDriver, RelaxDriver, TimeDriver, TimeTorqueDriver

runner = mumax3c.mumax3.Runner()
"""Controls the default runner.
Expand Down
1 change: 1 addition & 0 deletions mumax3c/drivers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from .mindriver import MinDriver
from .relaxdriver import RelaxDriver
from .timedriver import TimeDriver
from .timetorquedriver import TimeTorqueDriver
61 changes: 61 additions & 0 deletions mumax3c/drivers/timetorquedriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from .driver import Driver


class TimeTorqueDriver(Driver):
"""Time driver.

Only attributes in ``_allowed_attributes`` can be defined. For details on
possible values for individual attributes and their default values, please
refer to ``Mumax3`` documentation (https://mumax.github.io).

Examples
--------
1. Defining driver with a keyword argument.

>>> import mumax3c as mc
...
>>> td = mc.TimeTorqueDriver(MaxTorque=1e-3)

2. Passing an argument which is not allowed.

>>> import mumax3c as mc
...
>>> td = mc.TimeTorqueDriver(myarg=1)
Traceback (most recent call last):
...
AttributeError: ...

3. Getting the list of allowed attributes.

>>> import mumax3c as mc
...
>>> td = mc.TimeTorqueDriver()
>>> td._allowed_attributes
[...]

"""

_allowed_attributes = [
"DemagAccuracy",
"dt",
"FixDt",
"Headroom",
"LastErr",
"MaxDt",
"MaxErr",
"MinDt",
"NEval",
"PeakErr",
"step",
"maxtorque",
]

def _checkargs(self, **kwargs):
maxtorque = kwargs["maxtorque"]
if maxtorque <= 0:
msg = f"Cannot drive with {maxtorque=}."
raise ValueError(msg)

@property
def _x(self):
return "maxtorque"
23 changes: 16 additions & 7 deletions mumax3c/scripts/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def driver_script(driver, system, compute=None, ovf_format="bin4", **kwargs):
mx3 = "tableadd(E_total)\n"
mx3 += "tableadd(dt)\n"
mx3 += "tableadd(maxtorque)\n"

if isinstance(driver, mc.MinDriver):
for attr, value in driver:
if attr != "evolver":
Expand All @@ -32,7 +33,7 @@ def driver_script(driver, system, compute=None, ovf_format="bin4", **kwargs):
mx3 += "save(m_full)\n"
mx3 += "tablesave()\n\n"

if isinstance(driver, mc.TimeDriver):
if isinstance(driver, mc.TimeDriver) or isinstance(driver, mc.TimeTorqueDriver):
# Extract dynamics equation parameters.
gamma0 = (
precession[0].gamma0
Expand Down Expand Up @@ -78,12 +79,20 @@ def driver_script(driver, system, compute=None, ovf_format="bin4", **kwargs):
mx3 += "setsolver(5)\n"
mx3 += "fixDt = 0\n\n"

t, n = kwargs["t"], kwargs["n"]
if isinstance(driver, mc.TimeDriver):
t, n = kwargs["t"], kwargs["n"]

mx3 += f"for snap_counter:=0; snap_counter<{n}; snap_counter++{{\n"
mx3 += f" run({t/n})\n"
mx3 += " save(m_full)\n"
mx3 += " tablesave()\n"
mx3 += "}"

if isinstance(driver, mc.TimeTorqueDriver):
torque_val0 = kwargs["maxtorque"]

mx3 += f"for snap_counter:=0; snap_counter<{n}; snap_counter++{{\n"
mx3 += f" run({t/n})\n"
mx3 += " save(m_full)\n"
mx3 += " tablesave()\n"
mx3 += "}"
mx3 += f"RunWhile(maxtorque > {torque_val0})\n"
mx3 += "save(m_full)\n"
mx3 += "tablesave()\n"

return mx3