Skip to content

Commit

Permalink
Merge branch 'update.interface' into update
Browse files Browse the repository at this point in the history
  • Loading branch information
aoymt committed Jul 16, 2024
2 parents 84d5bca + 39ce132 commit 60b09da
Show file tree
Hide file tree
Showing 20 changed files with 785 additions and 481 deletions.
22 changes: 16 additions & 6 deletions extra/leed/src/leed/leed.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,20 @@ def check_keywords(key, segment, registered_list):
def name(self) -> str:
return self._name

def prepare(self, message: py2dmat.Message) -> None:
def evaluate(self, x: np.ndarray, args = (), nprocs: int = 1, nthreads: int = 1) -> float:
self.prepare(x, args)
cwd = os.getcwd()
os.chdir(self.work_dir)
self.run(nprocs, nthreads)
os.chdir(cwd)
result = self.get_results()
return result

def prepare(self, x: np.ndarray, args) -> None:
self.work_dir = self.proc_dir
for dir in [self.path_to_base_dir]:
copy_tree(os.path.join(self.root_dir, dir), os.path.join(self.work_dir))
self.input.prepare(message)
self.input.prepare(x, args)

def run(self, nprocs: int = 1, nthreads: int = 1) -> None:
self._run_by_subprocess([str(self.path_to_solver)])
Expand Down Expand Up @@ -149,10 +158,11 @@ def __init__(self, info):
self.root_dir = info.base["root_dir"]
self.output_dir = info.base["output_dir"]

def prepare(self, message: py2dmat.Message):
x_list = message.x
step = message.step
extra = message.set > 0
def prepare(self, x: np.ndarray, args):
x_list = x
#step, iset = args
#extra = iset > 0

# Delete output files
delete_files = ["search.s", "gleed.o"]
for file in delete_files:
Expand Down
24 changes: 18 additions & 6 deletions extra/sim-trhepd-rheed/src/sim_trhepd_rheed/sim_trhepd_rheed.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,18 @@ def command(self) -> List[str]:
"""Command to invoke solver"""
return [str(self.path_to_solver)]

def prepare(self, message: py2dmat.Message) -> None:
fitted_x_list, subdir = self.input.prepare(message)
def evaluate(self, x: np.ndarray, args = (), nprocs: int = 1, nthreads: int = 1) -> float:
self.prepare(x, args)
cwd = os.getcwd()
os.chdir(self.work_dir)
self.run(nprocs, nthreads)
os.chdir(cwd)
result = self.get_results()
return result

def prepare(self, x: np.ndarray, args) -> None:
# fitted_x_list, subdir = self.input.prepare(message)
fitted_x_list, subdir = self.input.prepare(x, args)
self.work_dir = self.proc_dir / Path(subdir)

self.output.prepare(fitted_x_list)
Expand Down Expand Up @@ -306,13 +316,15 @@ def load_bulk_output_file(self, filename):
bulk_f = np.array(bulk_file)
return bulk_f

def prepare(self, message: py2dmat.Message):
def prepare(self, x: np.ndarray, args):
if self.isLogmode:
time_sta = time.perf_counter()

x_list = message.x
step = message.step
iset = message.set
# x_list = message.x
# step = message.step
# iset = message.set
x_list = x
step, iset = args

dimension = self.dimension
string_list = self.string_list
Expand Down
21 changes: 15 additions & 6 deletions extra/sxrd/src/sxrd/sxrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,18 @@ def check_keywords(key, segment, registered_list):
def name(self) -> str:
return self._name

def prepare(self, message: py2dmat.Message) -> None:
def evaluate(self, x: np.ndarray, args = (), nprocs: int = 1, nthreads: int = 1) -> float:
self.prepare(x, args)
cwd = os.getcwd()
os.chdir(self.work_dir)
self.run(nprocs, nthreads)
os.chdir(cwd)
result = self.get_results()
return result

def prepare(self, x: np.ndarray, args) -> None:
self.work_dir = self.proc_dir
self.input.prepare(message)
self.input.prepare(x, args)
import shutil

for file in ["lsfit.in", self.path_to_f_in, self.path_to_bulk]:
Expand Down Expand Up @@ -180,10 +189,10 @@ def __init__(self, info):
info_s["config"], info_s["reference"], info_s["param"]["domain"]
)

def prepare(self, message: py2dmat.Message):
x_list = message.x
step = message.step
extra = message.set > 0
def prepare(self, x: np.ndarray, args):
x_list = x
#step, iset = args
#extra = iset > 0

# Generate fit file
# Add variables by numpy array.(Variables are updated in optimization process).
Expand Down
1 change: 0 additions & 1 deletion src/py2dmat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# Pay attention to the dependencies and the order of imports!
# For example, Runner depends on solver.

from ._message import Message
from ._info import Info
from . import solver
from ._runner import Runner
Expand Down
15 changes: 15 additions & 0 deletions src/py2dmat/_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

from typing import MutableMapping, Optional
from pathlib import Path
from fnmatch import fnmatch

from .util import toml
from . import mpi
from . import exception


Expand Down Expand Up @@ -61,3 +64,15 @@ def _cleanup(self) -> None:
self.algorithm = {}
self.solver = {}
self.runner = {}

@classmethod
def from_file(cls, file_name, fmt="", **kwargs):
if fmt == "toml" or fnmatch(file_name.lower(), "*.toml"):
inp = {}
if mpi.rank() == 0:
inp = toml.load(file_name)
if mpi.size() > 1:
inp = mpi.comm().bcast(inp, root=0)
return cls(inp)
else:
raise ValueError("unsupported file format: {}".format(file_name))
13 changes: 7 additions & 6 deletions src/py2dmat/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def main():
args = parser.parse_args()

file_name = args.inputfile
inp = {}
if py2dmat.mpi.rank() == 0:
inp = py2dmat.util.toml.load(file_name)
if py2dmat.mpi.size() > 1:
inp = py2dmat.mpi.comm().bcast(inp, root=0)
info = py2dmat.Info(inp)
# inp = {}
# if py2dmat.mpi.rank() == 0:
# inp = py2dmat.util.toml.load(file_name)
# if py2dmat.mpi.size() > 1:
# inp = py2dmat.mpi.comm().bcast(inp, root=0)
# info = py2dmat.Info(inp)
info = py2dmat.Info.from_file(file_name)

algname = info.algorithm["name"]
if algname == "mapper":
Expand Down
Loading

0 comments on commit 60b09da

Please sign in to comment.