Skip to content

apply ruff rules and fix lots of bugs #1240

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

Merged
merged 7 commits into from
Jun 9, 2023
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
3 changes: 0 additions & 3 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import subprocess
import sys
from datetime import date

# import sys
import recommonmark
from recommonmark.transform import AutoStructify

# -- Project information -----------------------------------------------------

Expand Down
2 changes: 0 additions & 2 deletions dpgen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function, unicode_literals

import logging
import os

Expand Down
1 change: 0 additions & 1 deletion dpgen/arginfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def general_mdata_arginfo(name: str, tasks: Tuple[str]) -> Argument:
Argument
arginfo
"""

doc_api_version = "Please set to 1.0"
doc_deepmd_version = "DeePMD-kit version, e.g. 2.1.3"
doc_run_mdata = "machine.json file"
Expand Down
10 changes: 4 additions & 6 deletions dpgen/auto_test/ABACUS.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

import numpy as np
from dpdata import LabeledSystem
from monty.serialization import dumpfn

Expand All @@ -22,7 +21,7 @@ def __init__(self, inter_parameter, path_to_poscar):
self.deepks = inter_parameter.get("deepks_desc", None)
self.deepks_model = inter_parameter.get("deepks_model", None)
self.path_to_poscar = path_to_poscar
self.if_define_orb_file = False if self.orbfile == None else True
self.if_define_orb_file = False if self.orbfile is None else True

def make_potential_files(self, output_dir):
stru = os.path.abspath(os.path.join(output_dir, "STRU"))
Expand All @@ -41,7 +40,7 @@ def make_potential_files(self, output_dir):
else:
stru_path = output_dir

if pp_files == None:
if pp_files is None:
raise RuntimeError("No pseudopotential information in STRU file")

pp_dir = os.path.abspath(self.potcar_prefix)
Expand Down Expand Up @@ -101,8 +100,7 @@ def make_potential_files(self, output_dir):
filename_in_para = os.path.split(file_param)[1]
if filename_in_stru != filename_in_para:
dlog.warning(
"file name in STRU is not match that defined in parameter setting file: '%s', '%s'."
% (filename_in_stru, filename_in_para)
f"file name in STRU is not match that defined in parameter setting file: '{filename_in_stru}', '{filename_in_para}'."
)

src_file = os.path.join(pp_dir, file_param)
Expand All @@ -119,7 +117,7 @@ def make_potential_files(self, output_dir):

def modify_input(self, incar, x, y):
if x in incar and incar[x] != y:
dlog.info("setting %s to %s" % (x, y))
dlog.info(f"setting {x} to {y}")
incar[x] = y

def make_input_file(self, output_dir, task_type, task_param):
Expand Down
4 changes: 2 additions & 2 deletions dpgen/auto_test/EOS.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, parameter, inter_param=None):
parameter["init_from_suffix"] = parameter.get("init_from_suffix", "00")
self.init_from_suffix = parameter["init_from_suffix"]
self.parameter = parameter
self.inter_param = inter_param if inter_param != None else {"type": "vasp"}
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}

def make_confs(self, path_to_work, path_to_equi, refine=False):
path_to_work = os.path.abspath(path_to_work)
Expand Down Expand Up @@ -259,7 +259,7 @@ def _compute_lower(self, output_file, all_tasks, all_res):
res_data[vol] = task_result["energies"][-1] / sum(
task_result["atom_numbs"]
)
ptr_data += "%7.3f %8.4f \n" % (
ptr_data += "{:7.3f} {:8.4f} \n".format(
vol,
task_result["energies"][-1] / sum(task_result["atom_numbs"]),
)
Expand Down
2 changes: 1 addition & 1 deletion dpgen/auto_test/Elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, parameter, inter_param=None):
# parameter['reproduce'] = False
# self.reprod = parameter['reproduce']
self.parameter = parameter
self.inter_param = inter_param if inter_param != None else {"type": "vasp"}
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}

def make_confs(self, path_to_work, path_to_equi, refine=False):
path_to_work = os.path.abspath(path_to_work)
Expand Down
12 changes: 4 additions & 8 deletions dpgen/auto_test/Gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import numpy as np
from ase.lattice.cubic import BodyCenteredCubic as bcc
from ase.lattice.cubic import FaceCenteredCubic as fcc
from ase.lattice.hexagonal import HexagonalClosedPacked as hcp
from monty.serialization import dumpfn, loadfn
from pymatgen.core.structure import Structure
from pymatgen.core.surface import SlabGenerator
from pymatgen.io.ase import AseAtomsAdaptor

import dpgen.auto_test.lib.abacus as abacus
Expand All @@ -22,9 +20,7 @@


class Gamma(Property):
"""
Calculation of common gamma lines for bcc and fcc
"""
"""Calculation of common gamma lines for bcc and fcc."""

def __init__(self, parameter, inter_param=None):
parameter["reproduce"] = parameter.get("reproduce", False)
Expand Down Expand Up @@ -95,7 +91,7 @@ def __init__(self, parameter, inter_param=None):
parameter["init_from_suffix"] = parameter.get("init_from_suffix", "00")
self.init_from_suffix = parameter["init_from_suffix"]
self.parameter = parameter
self.inter_param = inter_param if inter_param != None else {"type": "vasp"}
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}

def make_confs(self, path_to_work, path_to_equi, refine=False):
path_to_work = os.path.abspath(path_to_work)
Expand Down Expand Up @@ -359,7 +355,7 @@ def __poscar_fix(self, poscar) -> None:
+ fix_dict[self.add_fix[2]]
+ "\n"
)
with open(poscar, "r") as fin1:
with open(poscar) as fin1:
contents = fin1.readlines()
contents.insert(insert_pos - 1, "Selective dynamics\n")
for ii in range(insert_pos, 0, 1):
Expand Down Expand Up @@ -387,7 +383,7 @@ def __inLammpes_fix(self, inLammps) -> None:
+ fix_dict[self.add_fix[2]]
+ "\n"
)
with open(inLammps, "r") as fin1:
with open(inLammps) as fin1:
contents = fin1.readlines()
for ii in range(len(contents)):
upper = re.search("variable N equal count\(all\)", contents[ii])
Expand Down
17 changes: 7 additions & 10 deletions dpgen/auto_test/Interstitial.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import dpgen.auto_test.lib.abacus as abacus
import dpgen.auto_test.lib.lammps as lammps
import dpgen.generator.lib.abacus_scf as abacus_scf
from dpgen.auto_test.Property import Property
from dpgen.auto_test.refine import make_refine
from dpgen.auto_test.reproduce import make_repro, post_repro
Expand Down Expand Up @@ -76,7 +75,7 @@ def __init__(self, parameter, inter_param=None):
parameter["init_from_suffix"] = parameter.get("init_from_suffix", "00")
self.init_from_suffix = parameter["init_from_suffix"]
self.parameter = parameter
self.inter_param = inter_param if inter_param != None else {"type": "vasp"}
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}

def make_confs(self, path_to_work, path_to_equi, refine=False):
path_to_work = os.path.abspath(path_to_work)
Expand Down Expand Up @@ -248,7 +247,7 @@ def make_confs(self, path_to_work, path_to_equi, refine=False):
num_atom = super_size * 2
chl = -num_atom - 2
os.chdir(path_to_work)
with open("POSCAR", "r") as fin:
with open("POSCAR") as fin:
fin.readline()
scale = float(fin.readline().split()[0])
latt_param = float(fin.readline().split()[0])
Expand All @@ -257,7 +256,7 @@ def make_confs(self, path_to_work, path_to_equi, refine=False):
if not os.path.isfile("task.000000/POSCAR"):
raise RuntimeError("need task.000000 structure as reference")

with open("task.000000/POSCAR", "r") as fin:
with open("task.000000/POSCAR") as fin:
pos_line = fin.read().split("\n")

super_latt_param = float(pos_line[2].split()[0])
Expand Down Expand Up @@ -467,13 +466,13 @@ def make_confs(self, path_to_work, path_to_equi, refine=False):

def post_process(self, task_list):
if True:
fin1 = open(os.path.join(task_list[0], "..", "element.out"), "r")
fin1 = open(os.path.join(task_list[0], "..", "element.out"))
for ii in task_list:
conf = os.path.join(ii, "conf.lmp")
inter = os.path.join(ii, "inter.json")
insert_ele = fin1.readline().split()[0]
if os.path.isfile(conf):
with open(conf, "r") as fin2:
with open(conf) as fin2:
conf_line = fin2.read().split("\n")
insert_line = conf_line[-2]
type_map = loadfn(inter)["type_map"]
Expand Down Expand Up @@ -505,9 +504,7 @@ def _compute_lower(self, output_file, all_tasks, all_res):
ptr_data = os.path.dirname(output_file) + "\n"

if not self.reprod:
with open(
os.path.join(os.path.dirname(output_file), "element.out"), "r"
) as fin:
with open(os.path.join(os.path.dirname(output_file), "element.out")) as fin:
fc = fin.read().split("\n")
ptr_data += "Insert_ele-Struct: Inter_E(eV) E(eV) equi_E(eV)\n"
idid = -1
Expand All @@ -528,7 +525,7 @@ def _compute_lower(self, output_file, all_tasks, all_res):
supercell_index = loadfn(os.path.join(ii, "supercell.json"))
# insert_ele = loadfn(os.path.join(ii, 'task.json'))['insert_ele'][0]
insert_ele = fc[idid]
ptr_data += "%s: %7.3f %7.3f %7.3f \n" % (
ptr_data += "{}: {:7.3f} {:7.3f} {:7.3f} \n".format(
insert_ele + "-" + str(supercell_index) + "-" + structure_dir,
evac,
task_result["energies"][-1],
Expand Down
28 changes: 16 additions & 12 deletions dpgen/auto_test/Lammps.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,36 +138,40 @@ def make_input_file(self, output_dir, task_type, task_param):

# deal with user input in.lammps for relaxation
if os.path.isfile(self.in_lammps) and task_type == "relaxation":
with open(self.in_lammps, "r") as fin:
with open(self.in_lammps) as fin:
fc = fin.read()
# user input in.lammps for property calculation
if "input_prop" in cal_setting and os.path.isfile(cal_setting["input_prop"]):
with open(os.path.abspath(cal_setting["input_prop"]), "r") as fin:
with open(os.path.abspath(cal_setting["input_prop"])) as fin:
fc = fin.read()

else:
if "etol" in cal_setting:
dlog.info(
"%s setting etol to %s"
% (self.make_input_file.__name__, cal_setting["etol"])
"{} setting etol to {}".format(
self.make_input_file.__name__, cal_setting["etol"]
)
)
etol = cal_setting["etol"]
if "ftol" in cal_setting:
dlog.info(
"%s setting ftol to %s"
% (self.make_input_file.__name__, cal_setting["ftol"])
"{} setting ftol to {}".format(
self.make_input_file.__name__, cal_setting["ftol"]
)
)
ftol = cal_setting["ftol"]
if "maxiter" in cal_setting:
dlog.info(
"%s setting maxiter to %s"
% (self.make_input_file.__name__, cal_setting["maxiter"])
"{} setting maxiter to {}".format(
self.make_input_file.__name__, cal_setting["maxiter"]
)
)
maxiter = cal_setting["maxiter"]
if "maxeval" in cal_setting:
dlog.info(
"%s setting maxeval to %s"
% (self.make_input_file.__name__, cal_setting["maxeval"])
"{} setting maxeval to {}".format(
self.make_input_file.__name__, cal_setting["maxeval"]
)
)
maxeval = cal_setting["maxeval"]

Expand Down Expand Up @@ -300,7 +304,7 @@ def compute(self, output_dir):
force = []
virial = []
stress = []
with open(dump_lammps, "r") as fin:
with open(dump_lammps) as fin:
dump = fin.read().split("\n")
dumptime = []
for idx, ii in enumerate(dump):
Expand Down Expand Up @@ -354,7 +358,7 @@ def compute(self, output_dir):
fz = float(dump[idx + 9 + jj].split()[7])
force[-1].append([fx, fy, fz])

with open(log_lammps, "r") as fp:
with open(log_lammps) as fp:
if "Total wall time:" not in fp.read():
warnings.warn("lammps not finished " + log_lammps + " skip")
return None
Expand Down
26 changes: 8 additions & 18 deletions dpgen/auto_test/Property.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class Property(ABC):
@abstractmethod
def __init__(self, parameter):
"""
Constructor
"""Constructor.

Parameters
----------
Expand All @@ -23,8 +22,7 @@ def __init__(self, parameter):

@abstractmethod
def make_confs(self, path_to_work, path_to_equi, refine=False):
"""
Make configurations needed to compute the property.
"""Make configurations needed to compute the property.
The tasks directory will be named as path_to_work/task.xxxxxx
IMPORTANT: handel the case when the directory exists.

Expand All @@ -47,31 +45,24 @@ def make_confs(self, path_to_work, path_to_equi, refine=False):

@abstractmethod
def post_process(self, task_list):
"""
post_process the KPOINTS file in elastic.
"""
"""post_process the KPOINTS file in elastic."""
pass

@property
@abstractmethod
def task_type(self):
"""
Return the type of each computational task, for example, 'relaxation', 'static'....
"""
"""Return the type of each computational task, for example, 'relaxation', 'static'...."""
pass

@property
@abstractmethod
def task_param(self):
"""
Return the parameter of each computational task, for example, {'ediffg': 1e-4}
"""
"""Return the parameter of each computational task, for example, {'ediffg': 1e-4}."""
pass

def compute(self, output_file, print_file, path_to_work):
"""
Postprocess the finished tasks to compute the property.
Output the result to a json database
"""Postprocess the finished tasks to compute the property.
Output the result to a json database.

Parameters
----------
Expand Down Expand Up @@ -107,8 +98,7 @@ def compute(self, output_file, print_file, path_to_work):

@abstractmethod
def _compute_lower(self, output_file, all_tasks, all_res):
"""
Compute the property.
"""Compute the property.

Parameters
----------
Expand Down
3 changes: 1 addition & 2 deletions dpgen/auto_test/Surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import dpgen.auto_test.lib.abacus as abacus
import dpgen.auto_test.lib.vasp as vasp
import dpgen.generator.lib.abacus_scf as abacus_scf
from dpgen import dlog
from dpgen.auto_test.Property import Property
from dpgen.auto_test.refine import make_refine
Expand Down Expand Up @@ -83,7 +82,7 @@ def __init__(self, parameter, inter_param=None):
parameter["init_from_suffix"] = parameter.get("init_from_suffix", "00")
self.init_from_suffix = parameter["init_from_suffix"]
self.parameter = parameter
self.inter_param = inter_param if inter_param != None else {"type": "vasp"}
self.inter_param = inter_param if inter_param is not None else {"type": "vasp"}

def make_confs(self, path_to_work, path_to_equi, refine=False):
path_to_work = os.path.abspath(path_to_work)
Expand Down
Loading