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

update_info_json method is added to update and add the additional inf… #92

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
30 changes: 28 additions & 2 deletions micromagneticmodel/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def drive(
"""
# This method is implemented in the derived driver class. It raises
# exception if any of the arguments are not valid.
drive_kwargs = kwargs.copy()
lang-m marked this conversation as resolved.
Show resolved Hide resolved
self.drive_kwargs_setup(kwargs)
self._check_system(system)
workingdir = self._setup_working_directory(
Expand All @@ -138,9 +139,12 @@ def drive(
ovf_format=ovf_format,
**kwargs,
)
self._start_time = datetime.datetime.now()
self._write_info_json(system, **drive_kwargs)
lang-m marked this conversation as resolved.
Show resolved Hide resolved
self._call(system=system, runner=runner, verbose=verbose, **kwargs)
self._read_data(system)

self._end_time = datetime.datetime.now()
self._update_info_json()
lang-m marked this conversation as resolved.
Show resolved Hide resolved
system.drive_number += 1

def schedule(
Expand Down Expand Up @@ -297,10 +301,11 @@ def _write_info_json(self, system, **kwargs):
info["drive_number"] = system.drive_number
info["date"] = datetime.datetime.now().strftime("%Y-%m-%d")
info["time"] = datetime.datetime.now().strftime("%H:%M:%S")
info["driver"] = self.__class__.__name__
# "adapter" is the ubermag package (e.g. oommfc) that communicates with the
# calculator (e.g. OOMMF)
info["adapter"] = self.__module__.split(".")[0]
info["driver"] = self.__class__.__name__
info["start_time"] = self._start_time.isoformat(timespec="seconds")
lang-m marked this conversation as resolved.
Show resolved Hide resolved
for k, v in kwargs.items():
info[k] = v
with open("info.json", "w", encoding="utf-8") as jsonfile:
Expand All @@ -325,3 +330,24 @@ def _setup_working_directory(system, dirname, mode, append=True):
workingdir = system_dir / f"{mode}-{next_number}"
workingdir.mkdir(parents=True)
return workingdir

@staticmethod
def _conversion_to_hms(time_dfference):
total_seconds = int(time_dfference.total_seconds())
lang-m marked this conversation as resolved.
Show resolved Hide resolved
hours, remainder = divmod(total_seconds, 3600)
minutes, seconds = divmod(remainder, 60)
if time_dfference.total_seconds() < 1:
microseconds = time_dfference.microseconds
return f"{hours:02}:{minutes:02}:{seconds:02}.{microseconds:06}"
return f"{hours:02}:{minutes:02}:{seconds:02}"
kzqureshi marked this conversation as resolved.
Show resolved Hide resolved

def _update_info_json(self):
with open("info.json", encoding="utf-8") as jsonfile:
info = json.load(jsonfile)
info["end_time"] = self._end_time.isoformat(timespec="seconds")
info["elapsed_time"] = self._conversion_to_hms(
self._end_time - self._start_time
)
lang-m marked this conversation as resolved.
Show resolved Hide resolved
info["success"] = True
lang-m marked this conversation as resolved.
Show resolved Hide resolved
with open("info.json", "w", encoding="utf-8") as jsonfile:
json.dump(info, jsonfile)
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def _check_system(self, system):
def _write_input_files(self, system, **kwargs):
with open(f"{system.name}.input", "w", encoding="utf-8") as f:
f.write(str(-1)) # factor -1 used to invert magnetisation direction in call
self._write_info_json(system, **kwargs)

def _call(self, system, runner, **kwargs):
with open(f"{system.name}.input", encoding="utf-8") as f:
Expand Down
Loading