Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ repos:
rev: 23.3.0
hooks:
- id: black-jupyter
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: v0.0.243
hooks:
- id: isort
files: \.py$
- id: ruff
args: ["--fix"]
# numpydoc
- repo: https://github.com/Carreau/velin
rev: 0.0.12
Expand Down
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
Expand Down
15 changes: 7 additions & 8 deletions docs/make_format.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import csv
from collections import defaultdict
from typing import Any

# ensure all plugins are loaded!
import dpdata.plugins
from dpdata.driver import Driver, Minimizer
from dpdata.format import Format
from dpdata.system import get_cls_name


def get_formats() -> dict:
Expand All @@ -31,7 +28,7 @@ def get_minimizer() -> dict:


def detect_overridden(cls: Format, method: str) -> bool:
"""Check whether a method is override
"""Check whether a method is override.

Parameters
----------
Expand Down Expand Up @@ -61,7 +58,9 @@ def get_cls_link(cls: object) -> str:
str
the link of a class
"""
return ":class:`%s <%s>`" % (cls.__name__, ".".join([cls.__module__, cls.__name__]))
return ":class:`{} <{}>`".format(
cls.__name__, ".".join([cls.__module__, cls.__name__])
)


def check_supported(fmt: Format):
Expand Down Expand Up @@ -112,7 +111,7 @@ def check_supported(fmt: Format):
writer.writerow(
{
"Class": get_cls_link(kk),
"Alias": "\n".join(("``%s``" % vvv for vvv in vv)),
"Alias": "\n".join("``%s``" % vvv for vvv in vv),
"Supported Functions": "\n".join(
method_links[mtd] for mtd in check_supported(kk)
),
Expand All @@ -132,7 +131,7 @@ def check_supported(fmt: Format):
writer.writerow(
{
"Class": get_cls_link(kk),
"Alias": "\n".join(("``%s``" % vvv for vvv in vv)),
"Alias": "\n".join("``%s``" % vvv for vvv in vv),
}
)

Expand All @@ -149,6 +148,6 @@ def check_supported(fmt: Format):
writer.writerow(
{
"Class": get_cls_link(kk),
"Alias": "\n".join(("``%s``" % vvv for vvv in vv)),
"Alias": "\n".join("``%s``" % vvv for vvv in vv),
}
)
13 changes: 12 additions & 1 deletion dpdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
# BondOrder System has dependency on rdkit
try:
# prevent conflict with dpdata.rdkit
import rdkit as _
import rdkit as _ # noqa: F401

USE_RDKIT = True
except ModuleNotFoundError:
USE_RDKIT = False

if USE_RDKIT:
from .bond_order_system import BondOrderSystem

__all__ = [
"__version__",
"lammps",
"md",
"vasp",
"System",
"LabeledSystem",
"MultiSystems",
"BondOrderSystem",
]
14 changes: 5 additions & 9 deletions dpdata/abacus/md.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import os
import re
import sys
import warnings
from ast import dump

import numpy as np

from .scf import (
bohr2ang,
get_block,
get_cell,
get_coords,
get_geometry_in,
kbar2evperang3,
ry2ev,
)

# Read in geometries from an ABACUS MD trajectory.
Expand Down Expand Up @@ -166,12 +162,12 @@ def get_frame(fname):
path_in = os.path.join(fname, "INPUT")
else:
raise RuntimeError("invalid input")
with open(path_in, "r") as fp:
with open(path_in) as fp:
inlines = fp.read().split("\n")
geometry_path_in = get_geometry_in(fname, inlines) # base dir of STRU
path_out = get_path_out(fname, inlines)

with open(geometry_path_in, "r") as fp:
with open(geometry_path_in) as fp:
geometry_inlines = fp.read().split("\n")
celldm, cell = get_cell(geometry_inlines)
atom_names, natoms, types, coords = get_coords(
Expand All @@ -182,11 +178,11 @@ def get_frame(fname):
# ndump = int(os.popen("ls -l %s | grep 'md_pos_' | wc -l" %path_out).readlines()[0])
# number of dumped geometry files
# coords = get_coords_from_cif(ndump, dump_freq, atom_names, natoms, types, path_out, cell)
with open(os.path.join(path_out, "MD_dump"), "r") as fp:
with open(os.path.join(path_out, "MD_dump")) as fp:
dumplines = fp.read().split("\n")
coords, cells, force, stress = get_coords_from_dump(dumplines, natoms)
ndump = np.shape(coords)[0]
with open(os.path.join(path_out, "running_md.log"), "r") as fp:
with open(os.path.join(path_out, "running_md.log")) as fp:
outlines = fp.read().split("\n")
energy = get_energy(outlines, ndump, dump_freq)

Expand All @@ -201,7 +197,7 @@ def get_frame(fname):
unconv_stru += "%d " % i
ndump = len(energy)
if unconv_stru != "":
warnings.warn(f"Structure %s are unconverged and not collected!" % unconv_stru)
warnings.warn("Structure %s are unconverged and not collected!" % unconv_stru)

for iframe in range(ndump):
stress[iframe] *= np.linalg.det(cells[iframe, :, :].reshape([3, 3]))
Expand Down
12 changes: 5 additions & 7 deletions dpdata/abacus/relax.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys

import numpy as np

Expand All @@ -16,19 +15,18 @@ def get_log_file(fname, inlines):
suffix = line.split()[1]
elif "calculation" in line and "calculation" == line.split()[0]:
calculation = line.split()[1]
logf = os.path.join(fname, "OUT.%s/running_%s.log" % (suffix, calculation))
logf = os.path.join(fname, f"OUT.{suffix}/running_{calculation}.log")
return logf


def get_coords_from_log(loglines, natoms):
"""
NOTICE: unit of coords and cells is Angstrom
"""NOTICE: unit of coords and cells is Angstrom
order:
coordinate
cell (no output if cell is not changed)
energy (no output, if SCF is not converged)
force (no output, if cal_force is not setted or abnormal ending)
stress (no output, if set cal_stress is not setted or abnormal ending)
stress (no output, if set cal_stress is not setted or abnormal ending).
"""
natoms_log = 0
for line in loglines:
Expand Down Expand Up @@ -175,10 +173,10 @@ def get_frame(fname):
path_in = os.path.join(fname, "INPUT")
else:
raise RuntimeError("invalid input")
with open(path_in, "r") as fp:
with open(path_in) as fp:
inlines = fp.read().split("\n")
geometry_path_in = get_geometry_in(fname, inlines) # base dir of STRU
with open(geometry_path_in, "r") as fp:
with open(geometry_path_in) as fp:
geometry_inlines = fp.read().split("\n")
celldm, cell = get_cell(geometry_inlines)
atom_names, natoms, types, coord_tmp = get_coords(
Expand Down
9 changes: 4 additions & 5 deletions dpdata/abacus/scf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import re
import sys

import numpy as np

Expand Down Expand Up @@ -172,17 +171,17 @@ def get_frame(fname):
if not CheckFile(path_in):
return data

with open(path_in, "r") as fp:
with open(path_in) as fp:
inlines = fp.read().split("\n")

geometry_path_in = get_geometry_in(fname, inlines)
path_out = get_path_out(fname, inlines)
if not (CheckFile(geometry_path_in) and CheckFile(path_out)):
return data

with open(geometry_path_in, "r") as fp:
with open(geometry_path_in) as fp:
geometry_inlines = fp.read().split("\n")
with open(path_out, "r") as fp:
with open(path_out) as fp:
outlines = fp.read().split("\n")

celldm, cell = get_cell(geometry_inlines)
Expand Down Expand Up @@ -257,7 +256,7 @@ def get_nele_from_stru(geometry_inlines):

def get_frame_from_stru(fname):
assert type(fname) == str
with open(fname, "r") as fp:
with open(fname) as fp:
geometry_inlines = fp.read().split("\n")
nele = get_nele_from_stru(geometry_inlines)
inlines = ["ntype %d" % nele]
Expand Down
4 changes: 2 additions & 2 deletions dpdata/amber/mask.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Amber mask"""
"""Amber mask."""
try:
import parmed
except ImportError:
pass


def pick_by_amber_mask(param, maskstr, coords=None):
"""Pick atoms by amber masks
"""Pick atoms by amber masks.

Parameters
----------
Expand Down
5 changes: 3 additions & 2 deletions dpdata/amber/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def read_amber_traj(
* mdfrc, NetCDF format, stores forces
* mden (optional), text format, stores energies
* mdout (optional), text format, may store energies if there is no mden_file
* parm7, text format, stores types
* parm7, text format, stores types.

Parameters
----------
Expand All @@ -41,8 +41,9 @@ def read_amber_traj(
instead of amber types. For example, a ligand will use C, H, O, N, and so on
instead of h1, hc, o, os, and so on.
IF use_element_symbols is str, it will be considered as Amber mask.
labeled : bool
Whether to return labeled data
"""

flag_atom_type = False
flag_atom_numb = False
amber_types = []
Expand Down
4 changes: 1 addition & 3 deletions dpdata/amber/sqm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@


def parse_sqm_out(fname):
"""
Read atom symbols, charges and coordinates from ambertools sqm.out file
"""
"""Read atom symbols, charges and coordinates from ambertools sqm.out file."""
atom_symbols = []
coords = []
charges = []
Expand Down
41 changes: 13 additions & 28 deletions dpdata/bond_order_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
from rdkit.Chem import Conformer

import dpdata.rdkit.utils
from dpdata.rdkit.sanitize import SanitizeError, Sanitizer
from dpdata.system import Axis, DataType, LabeledSystem, System, load_format
from dpdata.rdkit.sanitize import Sanitizer
from dpdata.system import Axis, DataType, System

# import dpdata.rdkit.mol2


class BondOrderSystem(System):
"""
The system with chemical bond and formal charges information
"""The system with chemical bond and formal charges information.

For example, a labeled methane system named `d_example` has one molecule (5 atoms, 4 bonds) and `n_frames` frames. The bond order and formal charge information can be accessed by
- `d_example['bonds']` : a numpy array of size 4 x 3, and
Expand Down Expand Up @@ -44,8 +43,7 @@ def __init__(
verbose=False,
**kwargs,
):
"""
Constructor
"""Constructor.

Parameters
----------
Expand Down Expand Up @@ -73,8 +71,9 @@ def __init__(
whether to raise an Exception if sanitization procedure fails.
verbose : bool
whether to print information in the sanitization procedure.
**kwargs : dict
Additional arguments for the format.
"""

System.__init__(self)
self.sanitizer = Sanitizer(sanitize_level, raise_errors, verbose)

Expand Down Expand Up @@ -112,9 +111,7 @@ def to_fmt_obj(self, fmtobj, *args, **kwargs):
return fmtobj.to_bond_order_system(self.data, self.rdkit_mol, *args, **kwargs)

def __str__(self):
"""
A brief summary of the system
"""
"""A brief summary of the system."""
ret = "Data Summary"
ret += "\nBondOrder System"
ret += "\n-------------------"
Expand All @@ -128,33 +125,23 @@ def __str__(self):
return ret

def get_nbonds(self):
"""
Return the number of bonds
"""
"""Return the number of bonds."""
return len(self.data["bonds"])

def get_charge(self):
"""
Return the total formal charge of the moleclue
"""
"""Return the total formal charge of the moleclue."""
return sum(self.data["formal_charges"])

def get_mol(self):
"""
Return the rdkit.Mol object
"""
"""Return the rdkit.Mol object."""
return self.rdkit_mol

def get_bond_order(self, begin_atom_idx, end_atom_idx):
"""
Return the bond order between given atoms
"""
"""Return the bond order between given atoms."""
return self.data["bond_dict"][f"{int(begin_atom_idx)}-{int(end_atom_idx)}"]

def get_formal_charges(self):
"""
Return the formal charges on each atom
"""
"""Return the formal charges on each atom."""
return self.data["formal_charges"]

def copy(self):
Expand All @@ -178,9 +165,7 @@ def __add__(self, other):
# raise RuntimeError(f"Unsupported data structure: {type(other)}")

def from_rdkit_mol(self, rdkit_mol):
"""
Initialize from a rdkit.Chem.rdchem.Mol object
"""
"""Initialize from a rdkit.Chem.rdchem.Mol object."""
rdkit_mol = self.sanitizer.sanitize(rdkit_mol)
self.data = dpdata.rdkit.utils.mol_to_system_data(rdkit_mol)
self.data["bond_dict"] = dict(
Expand Down
Loading