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

Add new option "Hsteps" to HysteresisDriver #138

Merged
merged 16 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions oommfc/drivers/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def drive_kwargs_setup(self, drive_kwargs):
drive_kwargs.setdefault("output_step", False)
drive_kwargs.setdefault("n_threads", None)
drive_kwargs.setdefault("compute", None)
return drive_kwargs
lang-m marked this conversation as resolved.
Show resolved Hide resolved

def schedule_kwargs_setup(self, schedule_kwargs):
"""Additional keyword arguments allowed for schedule.
Expand Down
54 changes: 49 additions & 5 deletions oommfc/drivers/hysteresisdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class HysteresisDriver(Driver):
>>> hd._allowed_attributes
[...]

4. How to define multiple steps with this driver.
>>> import oommfc as oc
...
>>> system = oc.System(name="my_system")
...
>>> sd = oc.HysteresisDriver()
>>> sd.drive(system, Hsteps=[
>>> [(0, 0, 0), (0, 0, 1), 10],
>>> [(0, 0, 1), (0, 0, -1), 10],
>>> [(0, 0, -1), (0, 0, 0), 10],
>>> ])

"""

_allowed_attributes = [
Expand All @@ -59,12 +71,45 @@ class HysteresisDriver(Driver):
]

def _checkargs(self, kwargs):
Hmin, Hmax, n = kwargs["Hmin"], kwargs["Hmax"], kwargs["n"]
for i in [Hmin, Hmax]:
if not isinstance(i, (list, tuple, np.ndarray)):
if any(item in kwargs for item in ["Hmin", "Hmax", "n"]) and "Hsteps" in kwargs:
# both Hsteps and (Hmin, Hmax, n) are defined, which is not allowed
msg = "Cannot define both (Hmin, Hmax, n) and Hsteps."
raise ValueError(msg)

if all(item in kwargs for item in ["Hmin", "Hmax", "n"]):
newton-per-sqm marked this conversation as resolved.
Show resolved Hide resolved
# case of a symmetric hysteresis simulation
# construct symmetric Hsteps from (Hmin, Hmax, n)
kwargs = {
"Hsteps": [
[kwargs["Hmin"], kwargs["Hmax"], kwargs["n"]],
[kwargs["Hmax"], kwargs["Hmin"], kwargs["n"]],
]
}

if "Hsteps" in kwargs:
# case of a stepped hysteresis simulation
for step in kwargs["Hsteps"]:
if len(step) != 3:
msg = (
"Each list item in Hsteps must have 3 entries: Hstart, Hend, n."
)
raise ValueError(msg)
self._checkvalues(step[0], step[1], step[2])
else:
msg = (
"Cannot drive without a full definition of (Hmin, Hmax, n) xor Hsteps."
)
return ValueError(msg)

return kwargs

@staticmethod
def _checkvalues(Hmin, Hmax, n):
for item in [Hmin, Hmax]:
if not isinstance(item, (list, tuple, np.ndarray)):
msg = "Hmin and Hmax must have array_like values."
raise ValueError(msg)
if len(i) != 3:
if len(item) != 3:
msg = "Hmin and Hmax must have length 3."
raise ValueError(msg)
if not isinstance(n, int):
Expand All @@ -73,7 +118,6 @@ def _checkargs(self, kwargs):
if n - 1 <= 0: # OOMMF counts steps, not points (n -> n-1)
msg = f"Cannot drive with {n=}."
raise ValueError(msg)
return kwargs

def _check_system(self, system):
"""Checks the system has energy in it"""
Expand Down
5 changes: 2 additions & 3 deletions oommfc/scripts/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ def driver_script(
mif += oc.scripts.evolver_script(driver.evolver)

# Oxs_UZeeman
Hmin, Hmax, n = kwargs["Hmin"], kwargs["Hmax"], kwargs["n"]
mif += "# OxS_UZeeman\n"
mif += "Specify Oxs_UZeeman:hysteresis {\n"
mif += " Hrange {\n"
mif += " {{ {} {} {} {} {} {} {} }}\n".format(*Hmin, *Hmax, n - 1)
mif += " {{ {} {} {} {} {} {} {} }}\n".format(*Hmax, *Hmin, n - 1)
for Hstart, Hend, n in kwargs["Hsteps"]:
lang-m marked this conversation as resolved.
Show resolved Hide resolved
mif += " {{ {} {} {} {} {} {} {} }}\n".format(*Hstart, *Hend, n - 1)
mif += " }\n"
mif += "}\n\n"

Expand Down
Loading