Skip to content

Commit

Permalink
Merge pull request #95 from kirk0830/spillapi-refactor-2
Browse files Browse the repository at this point in the history
Refactor: moving all import line to the top of files
  • Loading branch information
kirk0830 authored Sep 28, 2024
2 parents d7e186c + 37f4bdf commit 173b64c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 96 deletions.
13 changes: 6 additions & 7 deletions SIAB/driver/front.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# interface to initialize
import os
from SIAB.io.read_input import read, parse
from SIAB.interface.abacus import run_all
import SIAB.spillage.pytorch_swat.api as ssps_api # old version of backend
import SIAB.spillage.api as ss_api # new version of backend

def initialize(version: str = "0.1.0",
fname: str = "./SIAB_INPUT"):
Expand All @@ -22,8 +26,6 @@ def initialize(version: str = "0.1.0",
fname: str
input filename, default is "./SIAB_INPUT"
"""
import os
from SIAB.io.read_input import read, parse

fname = fname.strip().replace("\\", "/")
user_settings = read(fname=fname, version=version)
Expand Down Expand Up @@ -104,7 +106,7 @@ def abacus(general: dict,
test: bool
whether to run in test mode, default is True
"""
from SIAB.interface.abacus import run_all

folders = run_all(general=general,
structures=structures,
calculation_settings=calculation_settings,
Expand Down Expand Up @@ -156,9 +158,6 @@ def spillage(folders: list,
}
```
"""
# interface to Spillage optimization
import SIAB.spillage.pytorch_swat.api as ssps_api # old version of backend
import SIAB.spillage.api as ss_api # new version of backend
# iteratively generate numerical atomic orbitals here
optimizer = siab_settings.get("optimizer", "none").lower()
caller_map = {
Expand Down
16 changes: 8 additions & 8 deletions SIAB/interface/abacus.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import SIAB.interface.env as sienv
import os
import numpy as np
import SIAB.io.read_output as read_output
import SIAB.data.interface as db
from scipy.optimize import curve_fit
import re

BLSCAN_WARNMSG = """
WARNING: since SIAB version 2.1(2024.6.3), the original functionality invoked by value \"auto\" is replaced by
\"scan\", and for dimer the \"auto\" now will directly use in-built dimer database if available, otherwise will
Expand Down Expand Up @@ -355,8 +363,6 @@ def write(suffix, inp, stru):
else:
yield write(folder, input_setting, stru_setting)


import SIAB.interface.env as sienv
def archive(footer: str = "", env: str = "local"):

"""mkdir and move correspnding input files to folder"""
Expand Down Expand Up @@ -432,7 +438,6 @@ def run_all(general: dict,
folders.append(folders_istructure)
return folders

import os
def is_duplicate(folder: str, abacus_setting: dict):
"""check if the abacus calculation can be safely (really?)
skipped"""
Expand Down Expand Up @@ -504,10 +509,6 @@ def is_duplicate(folder: str, abacus_setting: dict):
# TASK KIND1 - blscan #
# DESCRIPTION: search bond lengths #
# -------------------------------------------#
import numpy as np
import SIAB.io.read_output as read_output
import SIAB.data.interface as db
from scipy.optimize import curve_fit
def blscan(general: dict, # general settings
calculation_setting: dict, # calculation setting, for setting up INPUT file
env_settings: dict, # calculation environment settings
Expand Down Expand Up @@ -773,7 +774,6 @@ def normal(general: dict,
"""wait for all jobs to finish"""
return folders

import re
def read_INPUT(folder: str = "") -> dict:
"""parse ABACUS INPUT file, return a dict"""
if folder.startswith("INPUT_PARAMETERS"):
Expand Down
18 changes: 9 additions & 9 deletions SIAB/interface/abacustest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
instead of contents of the file
"""
import os
import json
import time
import re

#ABACUS_IMAGE = "registry.dp.tech/deepmodeling/abacus-intel:latest"
ABACUS_IMAGE = "registry.dp.tech/dptech/abacus:3.6.4" # this is for submit large batch of jobs, but need to always update
Expand All @@ -28,16 +32,14 @@
PYTHON_IMAGE = "python:3.8"
ABACUS_COMMAND = "OMP_NUM_THREADS=1 mpirun -n 16 abacus | tee out.log"

import os
import json
def read_apns_inp(fname: str) -> dict:
assert os.path.exists(fname), f"File not found: {fname}"
with open(fname, "r") as f:
inp = json.load(f)
return inp.get("abacustest", {})

def manual_submit(username, password, project_id, ncores, memory, folders):
import time, os

assert all([os.path.exists(f) for f in folders]), "Some folders do not exist."
jobgroup = f"apns_{time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())}"
run_dft = [{"ifrun": True, "job_folders": folders, "command": ABACUS_COMMAND, "ncores": ncores, "memory": memory}]
Expand All @@ -47,7 +49,6 @@ def manual_submit(username, password, project_id, ncores, memory, folders):
result_folder = submit(param)
return result_folder


def bohrium_machine(ncores: int, memory: float, device: str, supplier: str):
"""Configure Bohrium machine information
Expand Down Expand Up @@ -181,7 +182,7 @@ def write_abacustest_param(jobgroup_name: str, bohrium_login: dict, save_dir: st
Returns:
dict: abacustest param.json contents
"""
import time

prepare = prepare or {}
predft = predft or {}
rundft = rundft or []
Expand All @@ -205,7 +206,7 @@ def write_abacustest_param(jobgroup_name: str, bohrium_login: dict, save_dir: st
return result

def submit(abacustest_param: dict) -> str:
import time

fparam = f"param-{time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())}.json"
with open(fparam, "w") as f:
json.dump(abacustest_param, f, indent=4)
Expand All @@ -217,7 +218,7 @@ def submit(abacustest_param: dict) -> str:
return folder

def read_keyvals_frominput(fin, keyword: str = None):
import re

kv = r"^([\w_-]+)(\s+)([^#]*)(.*)$"

result = {}
Expand All @@ -232,7 +233,7 @@ def read_keyvals_frominput(fin, keyword: str = None):
return result if keyword is None else result[keyword]

def abacus_default():
import os

fthis = os.path.abspath(__file__)
fabacus = fthis.replace("/abacustest.py", "/abacus_input_example")
result = read_keyvals_frominput(fabacus).items()
Expand All @@ -241,7 +242,6 @@ def abacus_default():

if __name__ == "__main__":

import os, time
src = "/root/documents/simulation/orbgen/apns-orbgen-project/nelec_delta_test/lcao-v2.0"
jobgroup = "u-nspin2_lcao-v2.0"
fgroup = os.path.join(src, jobgroup)
Expand Down
7 changes: 4 additions & 3 deletions SIAB/interface/env.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""this module is for converting common Liunx command to the version
compatible with High Performance Computing (HPC) system or say for
Supercomputer."""
import os
import subprocess
import sys

##############################################
# #
##############################################
import os
def submit(folder: str = "",
module_load_command: str = "",
mpi_command: str = "",
Expand Down Expand Up @@ -34,8 +36,7 @@ def submit(folder: str = "",
##############################################
# basic wrapped linux commands #
##############################################
import subprocess
import sys

def run(command: str,
env: str = "local",
additional_args: list = None,
Expand Down
1 change: 0 additions & 1 deletion SIAB/interface/old_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,6 @@ def ov_c_init(orbitals: list, folder: str = None):
return result

import uuid
import time
def convert(calculation_setting: dict,
siab_settings: dict):
"""
Expand Down
27 changes: 14 additions & 13 deletions SIAB/io/read_input.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import re
import os
import json
import SIAB.io.pseudopotential.tools.basic as siptb
from SIAB.data.interface import PERIODIC_TABLE_TOINDEX
from SIAB.io.pseudopotential.api import ppinfo
from numpy import ceil
import unittest
import uuid

def read_siab_plaintext(fname: str = ""):
keyvalue_pattern = r"^(\w+)(\s+)([^#]*)(#.*)?"
Expand All @@ -21,14 +29,12 @@ def read_siab_plaintext(fname: str = ""):

return result

import json
def read_siab_json(fname: str = ""):
"""parse SIAB_INPUT file with version 0.2.0 in json format"""
with open(fname, "r") as f:
result = json.load(f)
return result

import os
def read(fname: str = "", version: str = "0.1.0"):
"""default value setting is absent"""
print(f"""
Expand Down Expand Up @@ -186,7 +192,7 @@ def natom_from_shape(shape: str):
return natom.get(shape, 0)

def nbands_from_str(option: str|float|int, shape: str, z_val: float):
import re

if isinstance(option, str):
assert re.match(r"(auto|occ((\+|-)\d+)?|all)", option), f"option should be auto, occ, occ+/-n or all: {option}"
if option == "auto":
Expand Down Expand Up @@ -274,7 +280,6 @@ def abacus_settings(user_settings: dict, minimal_basis: list = None, z_val: floa
{"lmaxmax": lmax_monomer, "nbands": nbands_monomer})
return result

import SIAB.io.pseudopotential.tools.basic as siptb
def siab_settings(user_settings: dict, minimal_basis: list, z_val: float = 0):
"""convert user_settings to SIAB settings the information needed by spillage optimization
information is organized as follows:
Expand Down Expand Up @@ -375,7 +380,7 @@ def nzetagen(zeta_notation, minimal_basis: list):
list of int
the nzeta, like [2, 2, 1]
"""
import re

assert isinstance(minimal_basis, list), "minimal_basis should be a list"

#
Expand Down Expand Up @@ -464,7 +469,7 @@ def skip_ppread(user_settings: dict):
bool
True if the pseudopotential read-in can be skipped, False otherwise
"""
import re

skip = True
# case 1
# if nbands is specified as auto, occ, all, it must requires the number of valence
Expand Down Expand Up @@ -537,9 +542,7 @@ def parse(user_settings: dict):
"""
# move the information fetch from pseudopotential from front.py here...
# get value from the dict returned by function from_pseudopotential
from SIAB.data.interface import PERIODIC_TABLE_TOINDEX
from SIAB.io.pseudopotential.api import ppinfo
import os

if skip_ppread(user_settings):
"""the logic here is, because there are pseudopotential can be parsed automatically,
but the range of supported are limited. For those pseudopotential that is not with
Expand Down Expand Up @@ -568,7 +571,6 @@ def parse(user_settings: dict):
general = description(symbol, user_settings)
return structures, abacus, siab, env, general


def abacus_params():
pattern = r"^([\w]+)(\s+)([^#]+)(\s*)(#.*)?"
keys = []
Expand Down Expand Up @@ -596,7 +598,7 @@ def cal_nbands_fill_lmax(zval: int, zcore: int, lmax: int, fill_lmax: bool = Tru
Returns:
int: the number of bands to include in the calculation
"""
from numpy import ceil

# the first and the last element that fills shell of lmax.
# S: 1, Hydrogen; P: 5, Boron; D: 21, Scandium; F: 58, Cerium;
# S: 2, Helium; P: 10, Neon; D: 30, Zinc; F: 70, Ytterbium
Expand Down Expand Up @@ -995,15 +997,14 @@ def cal_nbands_fill_lmax(zval: int, zcore: int, lmax: int, fill_lmax: bool = Tru
qo_basis hydrogen #type of QO basis function: hydrogen: hydrogen-like basis, pswfc: read basis from pseudopotential
qo_thr 1e-06 #accuracy for evaluating cutoff radius of QO basis function"""

import unittest
class TestReadInput(unittest.TestCase):

"""use example input as test material
./SIAB/example_Si/SIAB_INPUT
"""

def test_parse(self):
import uuid, os

self.maxDiff = None
example = """
#--------------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion SIAB/io/read_output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import unittest

def read_energy(folder: str,
suffix: str,
calculation: str = "scf"):
Expand Down Expand Up @@ -35,7 +37,7 @@ def read_natom(folder: str,
return natom
return -1

import unittest

class TestReadOutput(unittest.TestCase):

def test_read_energy(self):
Expand Down
Loading

0 comments on commit 173b64c

Please sign in to comment.