forked from dptech-corp/dpgen2
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- new module `conf` is provided. - create abstract base class `conf.ConfGenerator` - all the configuration definitions are derived from the `conf.ConfGenerator` - examples of `conf.AlloyConfGenerator` and `conf.FileConfGenerator` are provided. The former generate alloy configurations, and the latter generates confs from files. Co-authored-by: Han Wang <wang_han@iapcm.ac.cn> Co-authored-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
- Loading branch information
1 parent
db4ea65
commit ea66892
Showing
23 changed files
with
604 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .conf_generator import ( | ||
ConfGenerator, | ||
) | ||
from .alloy_conf import ( | ||
AlloyConfGenerator, | ||
) | ||
from .file_conf import ( | ||
FileConfGenerator, | ||
) | ||
|
||
conf_styles = { | ||
"alloy" : AlloyConfGenerator, | ||
"file" : FileConfGenerator, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import dpdata | ||
import dargs | ||
import tempfile | ||
from pathlib import Path | ||
from typing import ( | ||
List, Dict, | ||
) | ||
from abc import ( | ||
ABC, abstractmethod, | ||
) | ||
|
||
class ConfGenerator(ABC): | ||
@abstractmethod | ||
def generate( | ||
self, | ||
type_map, | ||
) -> dpdata.MultiSystems: | ||
r"""Method of generating configurations. | ||
Parameters | ||
---------- | ||
type_map: List[str] | ||
The type map. | ||
Returns | ||
------- | ||
confs: dpdata.MultiSystems | ||
The returned configurations in `dpdata.MultiSystems` format | ||
""" | ||
pass | ||
|
||
|
||
def get_file_content( | ||
self, | ||
type_map, | ||
fmt='lammps/lmp', | ||
) -> List[str]: | ||
r"""Get the file content of configurations | ||
Parameters | ||
---------- | ||
type_map: List[str] | ||
The type map. | ||
Returns | ||
------- | ||
conf_list: List[str] | ||
A list of file content of configurations. | ||
""" | ||
ret = [] | ||
ms = self.generate(type_map) | ||
for ii in range(len(ms)): | ||
ss = ms[ii] | ||
for jj in range(ss.get_nframes()): | ||
with tempfile.NamedTemporaryFile() as ft: | ||
tf = Path(ft.name) | ||
ss[jj].to(fmt, tf) | ||
ret.append(tf.read_text()) | ||
return ret | ||
|
||
|
||
@staticmethod | ||
@abstractmethod | ||
def args() -> List[dargs.Argument]: | ||
pass | ||
|
||
|
||
@classmethod | ||
def normalize_config( | ||
cls, | ||
data: Dict={}, | ||
strict: bool=True, | ||
) -> Dict: | ||
r"""Normalized the argument. | ||
Parameters | ||
---------- | ||
data: Dict | ||
The input dict of arguments. | ||
strict: bool | ||
Strictly check the arguments. | ||
Returns | ||
------- | ||
data: Dict | ||
The normalized arguments. | ||
""" | ||
ta = cls.args() | ||
base = dargs.Argument("base", dict, ta) | ||
data = base.normalize_value(data, trim_pattern="_*") | ||
base.check_value(data, strict=strict) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import os | ||
import dpdata | ||
import glob | ||
from pathlib import Path | ||
from .conf_generator import ConfGenerator | ||
from typing import ( | ||
Optional, Union, List, Tuple | ||
) | ||
from dargs import ( | ||
Argument, | ||
Variant, | ||
) | ||
|
||
|
||
class FileConfGenerator(ConfGenerator): | ||
def __init__( | ||
self, | ||
files : Union[str,List[str]], | ||
fmt : str = 'auto', | ||
prefix : Optional[str] = None, | ||
): | ||
if not isinstance(files, list): | ||
assert(isinstance(files, str)) | ||
files = [files] | ||
if prefix is not None: | ||
pfiles = [Path(prefix) / Path(ii) for ii in files] | ||
else: | ||
pfiles = [Path(ii) for ii in files] | ||
self.files = [] | ||
for ii in pfiles: | ||
ff = glob.glob(str(ii.absolute())) | ||
ff.sort() | ||
self.files += ff | ||
self.fmt = fmt | ||
|
||
|
||
def generate( | ||
self, | ||
type_map, | ||
) -> dpdata.MultiSystems: | ||
ms = dpdata.MultiSystems(type_map=type_map) | ||
for ff in self.files: | ||
ms.append(dpdata.System(ff, fmt=self.fmt)) | ||
return ms | ||
|
||
|
||
@staticmethod | ||
def args() -> List[Argument]: | ||
doc_files = "The paths to the configuration files. widecards are supported." | ||
doc_prefix = "The prefix of file paths." | ||
doc_fmt = "The format (dpdata accepted formats) of the files." | ||
|
||
return [ | ||
Argument("files", [str, list], optional=False, doc=doc_files), | ||
Argument("prefix", str, optional=True, default=None, doc=doc_prefix), | ||
Argument("fmt", str, optional=True, default='auto', doc=doc_fmt), | ||
] | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.