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

support the DOS (electronic density of states) fitting #2449

Merged
merged 46 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
e3917fc
add compute_fparam
mingzhong15 May 4, 2022
cf177ab
Revert "add compute_fparam"
mingzhong15 May 4, 2022
8d8a56c
definition for make_fparam
mingzhong15 May 4, 2022
2af920a
support fparam-update from compute
mingzhong15 May 4, 2022
2cd776d
support fparam-update from compute
mingzhong15 May 4, 2022
5517bbd
improve the print information
mingzhong15 Jan 15, 2023
e75df18
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Jan 15, 2023
9c19b96
1. variable `FixTTMMod` in make_ttm_fparam is changed to `FixTTMDP` 2…
mingzhong15 Jan 17, 2023
939a32c
user interface is changed from to
mingzhong15 Jan 28, 2023
933a3a7
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Jan 28, 2023
e555ee6
update the document about \`fparam_from_compute\`
mingzhong15 Jan 28, 2023
1cdc311
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Jan 29, 2023
a2c5189
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Feb 19, 2023
26feb1b
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Feb 19, 2023
58b95ff
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Feb 19, 2023
d7d449b
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Feb 28, 2023
9bab57a
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Mar 27, 2023
7a06270
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Mar 29, 2023
9108c2e
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Apr 3, 2023
8001f29
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Apr 6, 2023
d4a7706
support DeepDOS model
mingzhong15 Apr 6, 2023
97ec763
fix a little bug
mingzhong15 Apr 7, 2023
97e6566
update fitting_type_args
mingzhong15 Apr 7, 2023
0dc22d6
Update argcheck.py
mingzhong15 Apr 7, 2023
c9bbb2c
fix bugs for training and testing
mingzhong15 Apr 8, 2023
975a79e
add example and doc
mingzhong15 Apr 9, 2023
a23c532
update the reference
mingzhong15 Apr 9, 2023
499e529
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 9, 2023
8a0d69d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 9, 2023
c91518e
reduce the size of dataset in example
mingzhong15 Apr 10, 2023
367d2ad
remove useless argument
mingzhong15 Apr 11, 2023
fc0bafa
add infer-test
mingzhong15 Apr 11, 2023
ef14891
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2023
7523d37
fix a little bug
mingzhong15 Apr 11, 2023
1bf9e72
set `default_places` to 4
mingzhong15 Apr 11, 2023
cf83375
fix the name bug
mingzhong15 Apr 11, 2023
bba6b20
change to infer atomic dos
mingzhong15 Apr 11, 2023
cfd918b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2023
b0f93fa
fix the output of total dos & add UT for `dos`
mingzhong15 Apr 11, 2023
9a13d46
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 11, 2023
597feb3
Merge branch 'deepmodeling:devel' into devel
mingzhong15 Apr 12, 2023
28a9dd8
add DOSFitting test
mingzhong15 Apr 13, 2023
7ba1b7d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 13, 2023
89fb880
add unit test for `DOSmodel`
mingzhong15 Apr 13, 2023
4771f1d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 13, 2023
f284e1c
rename the suffix in `model.build`
mingzhong15 Apr 13, 2023
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
7 changes: 7 additions & 0 deletions deepmd/entrypoints/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ def _make_node_names(
"fitting_attr/dfparam",
"fitting_attr/daparam",
]
elif model_type == "dos":
nodes += [
"o_dos",
"fitting_attr/numb_dos",
"fitting_attr/dfparam",
"fitting_attr/daparam",
]
elif model_type == "wfc":
nodes += [
"o_wfc",
Expand Down
173 changes: 173 additions & 0 deletions deepmd/entrypoints/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
if TYPE_CHECKING:
from deepmd.infer import (
DeepDipole,
DeepDOS,
DeepPolar,
DeepPot,
DeepWFC,
Expand Down Expand Up @@ -123,6 +124,16 @@ def test(
atomic,
append_detail=(cc != 0),
)
elif dp.model_type == "dos":
err = test_dos(
dp,
data,
system,
numb_test,
detail_file,
atomic,
append_detail=(cc != 0),
)
elif dp.model_type == "dipole":
err = test_dipole(dp, data, numb_test, detail_file, atomic)
elif dp.model_type == "polar":
Expand All @@ -147,6 +158,8 @@ def test(
log.info(f"# number of systems : {len(all_sys)}")
if dp.model_type == "ener":
print_ener_sys_avg(avg_err)
elif dp.model_type == "dos":
print_dos_sys_avg(avg_err)
elif dp.model_type == "dipole":
print_dipole_sys_avg(avg_err)
elif dp.model_type == "polar":
Expand Down Expand Up @@ -435,6 +448,166 @@ def print_ener_sys_avg(avg: Dict[str, float]):
log.info(f"Virial RMSE/Natoms : {avg['rmse_va']:e} eV")


def test_dos(
dp: "DeepDOS",
data: DeepmdData,
system: str,
numb_test: int,
detail_file: Optional[str],
has_atom_dos: bool,
append_detail: bool = False,
) -> Tuple[List[np.ndarray], List[int]]:
"""Test DOS type model.

Parameters
----------
dp : DeepDOS
instance of deep potential
data : DeepmdData
data container object
system : str
system directory
numb_test : int
munber of tests to do
detail_file : Optional[str]
file where test details will be output
has_atom_dos : bool
whether per atom quantities should be computed
append_detail : bool, optional
if true append output detail file, by default False

Returns
-------
Tuple[List[np.ndarray], List[int]]
arrays with results and their shapes
"""
data.add("dos", dp.numb_dos, atomic=False, must=True, high_prec=True)
if has_atom_dos:
data.add("atom_dos", dp.numb_dos, atomic=True, must=False, high_prec=True)

if dp.get_dim_fparam() > 0:
data.add(
"fparam", dp.get_dim_fparam(), atomic=False, must=True, high_prec=False
)
if dp.get_dim_aparam() > 0:
data.add("aparam", dp.get_dim_aparam(), atomic=True, must=True, high_prec=False)

test_data = data.get_test()
mixed_type = data.mixed_type
natoms = len(test_data["type"][0])
nframes = test_data["box"].shape[0]
numb_test = min(nframes, numb_test)

coord = test_data["coord"][:numb_test].reshape([numb_test, -1])
box = test_data["box"][:numb_test]

if not data.pbc:
box = None
if mixed_type:
atype = test_data["type"][:numb_test].reshape([numb_test, -1])
else:
atype = test_data["type"][0]
if dp.get_dim_fparam() > 0:
fparam = test_data["fparam"][:numb_test]
else:
fparam = None
if dp.get_dim_aparam() > 0:
aparam = test_data["aparam"][:numb_test]
else:
aparam = None

ret = dp.eval(
coord,
box,
atype,
fparam=fparam,
aparam=aparam,
atomic=has_atom_dos,
mixed_type=mixed_type,
)
dos = ret[0]

dos = dos.reshape([numb_test, dp.numb_dos])

if has_atom_dos:
ados = ret[1]
ados = ados.reshape([numb_test, natoms * dp.numb_dos])

diff_dos = dos - test_data["dos"][:numb_test]
mae_dos = mae(diff_dos)
rmse_dos = rmse(diff_dos)

mae_dosa = mae_dos / natoms
rmse_dosa = rmse_dos / natoms

if has_atom_dos:
diff_ados = ados - test_data["atom_dos"][:numb_test]
mae_ados = mae(diff_ados)
rmse_ados = rmse(diff_ados)

log.info(f"# number of test data : {numb_test:d} ")

log.info(f"DOS MAE : {mae_dos:e} Occupation/eV")
log.info(f"DOS RMSE : {rmse_dos:e} Occupation/eV")
log.info(f"DOS MAE/Natoms : {mae_dosa:e} Occupation/eV")
log.info(f"DOS RMSE/Natoms : {rmse_dosa:e} Occupation/eV")

if has_atom_dos:
log.info(f"Atomic DOS MAE : {mae_ados:e} Occupation/eV")
log.info(f"Atomic DOS RMSE : {rmse_ados:e} Occupation/eV")

if detail_file is not None:
detail_path = Path(detail_file)

for ii in range(numb_test):
test_out = test_data["dos"][ii].reshape(-1, 1)
pred_out = dos[ii].reshape(-1, 1)

frame_output = np.hstack((test_out, pred_out))

save_txt_file(
detail_path.with_suffix(".dos.out.%.d" % ii),
frame_output,
header="%s - %.d: data_dos pred_dos" % (system, ii),
append=append_detail,
)

if has_atom_dos:
for ii in range(numb_test):
test_out = test_data["atom_dos"][ii].reshape(-1, 1)
pred_out = ados[ii].reshape(-1, 1)

frame_output = np.hstack((test_out, pred_out))

save_txt_file(
detail_path.with_suffix(".ados.out.%.d" % ii),
frame_output,
header="%s - %.d: data_ados pred_ados" % (system, ii),
append=append_detail,
)

return {
"mae_dos": (mae_dos, dos.size),
"mae_dosa": (mae_dosa, dos.size),
"rmse_dos": (rmse_dos, dos.size),
"rmse_dosa": (rmse_dosa, dos.size),
}


def print_dos_sys_avg(avg: Dict[str, float]):
"""Print errors summary for DOS type potential.

Parameters
----------
avg : np.ndarray
array with summaries
"""
log.info(f"DOS MAE : {avg['mae_dos']:e} Occupation/eV")
log.info(f"DOS RMSE : {avg['rmse_dos']:e} Occupation/eV")
log.info(f"DOS MAE/Natoms : {avg['mae_dosa']:e} Occupation/eV")
log.info(f"DOS RMSE/Natoms : {avg['rmse_dosa']:e} Occupation/eV")


def run_test(dp: "DeepTensor", test_data: dict, numb_test: int):
"""Run tests.

Expand Down
4 changes: 4 additions & 0 deletions deepmd/fit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from .dipole import (
DipoleFittingSeA,
)
from .dos import (
DOSFitting,
)
from .ener import (
EnerFitting,
)
Expand All @@ -12,6 +15,7 @@
__all__ = [
"DipoleFittingSeA",
"EnerFitting",
"DOSFitting",
"GlobalPolarFittingSeA",
"PolarFittingSeA",
]
Loading