Skip to content

Commit

Permalink
ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lang-m committed May 20, 2024
1 parent 51e52ef commit 6332e0d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 41 deletions.
35 changes: 17 additions & 18 deletions oommfc/oommf/oommf.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def errors(self):

except FileNotFoundError:
msg = "boxsi.errors cannot be retrieved."
raise OSError(msg)
raise OSError(msg) from None

def __repr__(self):
return f"ExeOOMMFRunner({self.oommf_exe})"
Expand Down Expand Up @@ -600,22 +600,21 @@ def overhead():
True
"""
with tempfile.TemporaryDirectory() as workingdir:
with uu.changedir(workingdir):
# Running OOMMF through oommfc.
system = mm.examples.macrospin()
td = oc.TimeDriver()
oommfc_start = time.time()
td.drive(system, t=1e-12, n=1)
oommfc_stop = time.time()
oommfc_time = oommfc_stop - oommfc_start

# Running OOMMF directly.
oommf_runner = oc.runner.runner
mifpath = pathlib.Path(f"{system.name}/drive-0/macrospin.mif").resolve()
oommf_start = time.time()
oommf_runner.call(str(mifpath))
oommf_stop = time.time()
oommf_time = oommf_stop - oommf_start
with tempfile.TemporaryDirectory() as workingdir, uu.changedir(workingdir):
# Running OOMMF through oommfc.
system = mm.examples.macrospin()
td = oc.TimeDriver()
oommfc_start = time.time()
td.drive(system, t=1e-12, n=1)
oommfc_stop = time.time()
oommfc_time = oommfc_stop - oommfc_start

# Running OOMMF directly.
oommf_runner = oc.runner.runner
mifpath = pathlib.Path(f"{system.name}/drive-0/macrospin.mif").resolve()
oommf_start = time.time()
oommf_runner.call(str(mifpath))
oommf_stop = time.time()
oommf_time = oommf_stop - oommf_start

return oommfc_time - oommf_time
5 changes: 1 addition & 4 deletions oommfc/scripts/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ def driver_script(
driver.evolver.fixed_spins = resstr

# What is saved in output?
if output_step:
output_str = "Step"
else:
output_str = "Stage"
output_str = "Step" if output_step else "Stage"

mif += oc.scripts.evolver_script(driver.evolver)

Expand Down
25 changes: 11 additions & 14 deletions oommfc/scripts/energy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import numbers
import warnings

Expand All @@ -22,10 +23,7 @@ def exchange_script(term, system):
mif += "}\n\n"

elif isinstance(term.A, dict):
if "default" in term.A.keys():
default_value = term.A["default"]
else:
default_value = 0
default_value = term.A.get("default", 0)
mif = "# Exchange6Ngbr\n"
mif += f"Specify Oxs_Exchange6Ngbr:{term.name} {{\n"
mif += f" default_A {default_value}\n"
Expand Down Expand Up @@ -61,7 +59,9 @@ def zeeman_script(term, system):
if isinstance(term.wave, str) or isinstance(term.func, str):
if isinstance(term.wave, str):
warnings.warn(
"Parameter `wave` is deprecated; use `func` instead.", FutureWarning
"Parameter `wave` is deprecated; use `func` instead.",
FutureWarning,
stacklevel=2,
)
if isinstance(term.H, (df.Field, dict)):
if term.wave == "sin" or term.func == "sin":
Expand Down Expand Up @@ -220,10 +220,8 @@ def zeeman_script(term, system):
mif += f'Specify {term.tcl_strings["energy"]}:{term.name} {{\n'
mif += f' script {term.tcl_strings["script_name"]}\n'
for key in ["type", "script_args"]:
try:
with contextlib.suppress(KeyError):
mif += f" {key} {term.tcl_strings[key]}\n"
except KeyError:
pass
if term.tcl_strings["energy"] == "Oxs_TransformZeeman":
mif += f" field {Hname}\n"
mif += "}\n\n"
Expand Down Expand Up @@ -264,14 +262,16 @@ def dmi_script(term, system):
elif (tcc := term.crystalclass) in ["D2d_x", "D2d_y", "D2d_z", "D2d"]:
if tcc == "D2d":
warnings.warn(
"Use of `D2d` is deprecated; use `D2d_z` instead.", FutureWarning
"Use of `D2d` is deprecated; use `D2d_z` instead.",
FutureWarning,
stacklevel=2,
)
tcc = "D2d_z"
oxs = f"Oxs_DMI_{tcc}"
elif (tcc := term.crystalclass) in ["Cnv_x", "Cnv_y", "Cnv_z", "Cnv"]:
if tcc == "Cnv":
msg = "Use of `Cnv` is deprecated; use `Cnv_z` instead."
warnings.warn(msg, FutureWarning)
warnings.warn(msg, FutureWarning, stacklevel=2)
tcc = "Cnv_z"
oxs = f"Oxs_DMI_{tcc}"

Expand All @@ -290,10 +290,7 @@ def dmi_script(term, system):
mif += "}\n\n"

elif isinstance(term.D, dict):
if "default" in term.D.keys():
default_value = term.D["default"]
else:
default_value = 0
default_value = term.D.get("default", 0)
mif += f" default_D {default_value}\n"
mif += " atlas :main_atlas\n"
mif += " D {\n"
Expand Down
2 changes: 1 addition & 1 deletion oommfc/scripts/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def mesh_script(mesh):
mif += oc.scripts.box_atlas(mesh.region.pmin, mesh.region.pmax, name="entire")
mif += "# MultiAtlas\n"
mif += "Specify Oxs_MultiAtlas:main_atlas {\n"
for name in mesh.subregions.keys():
for name in mesh.subregions:
mif += f" atlas :{name}_atlas\n"
mif += " atlas :entire_atlas\n"
mif += f" xrange {{ {mesh.region.pmin[0]} {mesh.region.pmax[0]} }}\n"
Expand Down
4 changes: 2 additions & 2 deletions oommfc/scripts/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def setup_scalar_parameter(parameter, name):
# to avoid changing the dictionary used in the respective term
# in the system (from micromagneticmodel)
param = parameter.copy()
if "default" not in param.keys():
if "default" not in param:
param["default"] = 0
mif = atlas_scalar_field(param, f"{name}")
return mif, f"{name}"
Expand All @@ -102,7 +102,7 @@ def setup_vector_parameter(parameter, name):
return mif, f"{name}"

elif isinstance(parameter, dict):
if "default" not in parameter.keys():
if "default" not in parameter:
parameter["default"] = (0, 0, 0)
mif = atlas_vector_field(parameter, f"{name}")
return mif, f"{name}"
Expand Down
4 changes: 2 additions & 2 deletions oommfc/tests/test_oommf.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_missing_oommf():
oc.runner.oommf_exe = "wrong_name"
oc.runner.docker_exe = "wrong_name"
with pytest.raises(EnvironmentError):
oc.runner.runner
oc.runner.runner # noqa: B018


def test_get_cached_runner(reset_runner, monkeypatch):
Expand All @@ -136,7 +136,7 @@ def test_get_cached_runner(reset_runner, monkeypatch):
oc.runner.cache_runner = False
oc.runner.docker_exe = "wrong_name" # ensure that we do not find docker
with pytest.raises(EnvironmentError):
oc.runner.runner
oc.runner.runner # noqa: B018

oc.runner.envvar = "OOMMFTCL"
if oommf_tcl_path():
Expand Down

0 comments on commit 6332e0d

Please sign in to comment.