|
| 1 | +from dflow.python import ( |
| 2 | + PythonOPTemplate, |
| 3 | + OP, |
| 4 | + OPIO, |
| 5 | + OPIOSign, |
| 6 | + Artifact, |
| 7 | + Slices, |
| 8 | + upload_packages |
| 9 | +) |
| 10 | + |
| 11 | +import subprocess, os, shutil, glob, dpdata, pathlib |
| 12 | +from pathlib import Path |
| 13 | +from typing import List |
| 14 | +from monty.serialization import loadfn |
| 15 | + |
| 16 | +from dflow.python import upload_packages |
| 17 | + |
| 18 | +upload_packages.append(__file__) |
| 19 | + |
| 20 | +from apex.lib.utils import return_prop_list |
| 21 | +try: |
| 22 | + from apex.property.common_equi import (make_equi, post_equi) |
| 23 | + from apex.property.common_prop import (make_property, post_property) |
| 24 | +except: |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class RelaxMakeABACUS(OP): |
| 29 | + """ |
| 30 | + class for making calculation tasks |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self): |
| 34 | + pass |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def get_input_sign(cls): |
| 38 | + return OPIOSign({ |
| 39 | + 'input': Artifact(Path), |
| 40 | + 'param': Artifact(Path) |
| 41 | + }) |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def get_output_sign(cls): |
| 45 | + return OPIOSign({ |
| 46 | + 'output': Artifact(Path), |
| 47 | + 'njobs': int, |
| 48 | + 'task_paths': Artifact(List[Path]) |
| 49 | + }) |
| 50 | + |
| 51 | + @OP.exec_sign_check |
| 52 | + def execute( |
| 53 | + self, |
| 54 | + op_in: OPIO, |
| 55 | + ) -> OPIO: |
| 56 | + from apex.property.common_equi import make_equi |
| 57 | + |
| 58 | + cwd = os.getcwd() |
| 59 | + os.chdir(op_in["input"]) |
| 60 | + work_d = os.getcwd() |
| 61 | + param_argv = op_in["param"] |
| 62 | + structures = loadfn(param_argv)["structures"] |
| 63 | + inter_parameter = loadfn(param_argv)["interaction"] |
| 64 | + parameter = loadfn(param_argv)["relaxation"] |
| 65 | + |
| 66 | + make_equi(structures, inter_parameter, parameter) |
| 67 | + |
| 68 | + conf_dirs = [] |
| 69 | + for conf in structures: |
| 70 | + conf_dirs.extend(glob.glob(conf)) |
| 71 | + conf_dirs.sort() |
| 72 | + |
| 73 | + task_list = [] |
| 74 | + for ii in conf_dirs: |
| 75 | + conf_dir_global = os.path.join(work_d, ii) |
| 76 | + task_list.append(os.path.join(conf_dir_global, 'relaxation/relax_task')) |
| 77 | + |
| 78 | + all_jobs = task_list |
| 79 | + njobs = len(all_jobs) |
| 80 | + jobs = [] |
| 81 | + for job in all_jobs: |
| 82 | + jobs.append(pathlib.Path(job)) |
| 83 | + |
| 84 | + os.chdir(cwd) |
| 85 | + op_out = OPIO({ |
| 86 | + "output": op_in["input"], |
| 87 | + "njobs": njobs, |
| 88 | + "task_paths": jobs |
| 89 | + }) |
| 90 | + return op_out |
| 91 | + |
| 92 | + |
| 93 | +class RunABACUS(OP): |
| 94 | + """ |
| 95 | + class for ABACUS calculation |
| 96 | + """ |
| 97 | + |
| 98 | + def __init__(self, infomode=1): |
| 99 | + self.infomode = infomode |
| 100 | + |
| 101 | + @classmethod |
| 102 | + def get_input_sign(cls): |
| 103 | + return OPIOSign({ |
| 104 | + 'input_abacus': Artifact(Path), |
| 105 | + 'run_command': str |
| 106 | + }) |
| 107 | + |
| 108 | + @classmethod |
| 109 | + def get_output_sign(cls): |
| 110 | + return OPIOSign({ |
| 111 | + 'output_abacus': Artifact(Path, sub_path=False) |
| 112 | + }) |
| 113 | + |
| 114 | + @OP.exec_sign_check |
| 115 | + def execute(self, op_in: OPIO) -> OPIO: |
| 116 | + cwd = os.getcwd() |
| 117 | + os.chdir(op_in["input_abacus"]) |
| 118 | + cmd = op_in["run_command"] |
| 119 | + subprocess.call(cmd, shell=True) |
| 120 | + os.chdir(cwd) |
| 121 | + op_out = OPIO({ |
| 122 | + "output_abacus": op_in["input_abacus"] |
| 123 | + }) |
| 124 | + return op_out |
| 125 | + |
| 126 | + |
| 127 | +class RelaxPostABACUS(OP): |
| 128 | + """ |
| 129 | + class for analyzing calculation results |
| 130 | + """ |
| 131 | + |
| 132 | + def __init__(self): |
| 133 | + pass |
| 134 | + |
| 135 | + @classmethod |
| 136 | + def get_input_sign(cls): |
| 137 | + return OPIOSign({ |
| 138 | + 'input_post': Artifact(Path, sub_path=False), |
| 139 | + 'input_all': Artifact(Path, sub_path=False), |
| 140 | + 'param': Artifact(Path), |
| 141 | + 'path': str |
| 142 | + }) |
| 143 | + |
| 144 | + @classmethod |
| 145 | + def get_output_sign(cls): |
| 146 | + return OPIOSign({ |
| 147 | + 'output_all': Artifact(Path, sub_path=False), |
| 148 | + 'output_post': Artifact(Path, sub_path=False) |
| 149 | + }) |
| 150 | + |
| 151 | + @OP.exec_sign_check |
| 152 | + def execute(self, op_in: OPIO) -> OPIO: |
| 153 | + from apex.property.common_equi import post_equi |
| 154 | + |
| 155 | + cwd = os.getcwd() |
| 156 | + os.chdir(str(op_in['input_all']) + op_in['path']) |
| 157 | + shutil.copytree(str(op_in['input_post']) + op_in['path'], './', dirs_exist_ok=True) |
| 158 | + |
| 159 | + param_argv = op_in['param'] |
| 160 | + post_equi(loadfn(param_argv)["structures"], loadfn(param_argv)["interaction"]) |
| 161 | + |
| 162 | + os.chdir(cwd) |
| 163 | + shutil.copytree(str(op_in['input_all']) + op_in['path'] + '/confs', './confs', dirs_exist_ok=True) |
| 164 | + |
| 165 | + op_out = OPIO({ |
| 166 | + 'output_all': Path(str(op_in["input_all"]) + op_in['path']), |
| 167 | + 'output_post': Path('./confs') |
| 168 | + }) |
| 169 | + return op_out |
| 170 | + |
| 171 | + |
| 172 | +class PropsMakeABACUS(OP): |
| 173 | + """ |
| 174 | + class for making calculation tasks |
| 175 | + """ |
| 176 | + |
| 177 | + def __init__(self): |
| 178 | + pass |
| 179 | + |
| 180 | + @classmethod |
| 181 | + def get_input_sign(cls): |
| 182 | + return OPIOSign({ |
| 183 | + 'input': Artifact(Path), |
| 184 | + 'param': Artifact(Path) |
| 185 | + }) |
| 186 | + |
| 187 | + @classmethod |
| 188 | + def get_output_sign(cls): |
| 189 | + return OPIOSign({ |
| 190 | + 'output': Artifact(Path), |
| 191 | + 'njobs': int, |
| 192 | + 'task_paths': Artifact(List[Path]) |
| 193 | + }) |
| 194 | + |
| 195 | + @OP.exec_sign_check |
| 196 | + def execute( |
| 197 | + self, |
| 198 | + op_in: OPIO, |
| 199 | + ) -> OPIO: |
| 200 | + from apex.property.common_prop import make_property |
| 201 | + |
| 202 | + cwd = os.getcwd() |
| 203 | + os.chdir(op_in["input"]) |
| 204 | + work_d = os.getcwd() |
| 205 | + param_argv = op_in["param"] |
| 206 | + structures = loadfn(param_argv)["structures"] |
| 207 | + inter_parameter = loadfn(param_argv)["interaction"] |
| 208 | + parameter = loadfn(param_argv)["properties"] |
| 209 | + make_property(structures, inter_parameter, parameter) |
| 210 | + |
| 211 | + conf_dirs = [] |
| 212 | + for conf in structures: |
| 213 | + conf_dirs.extend(glob.glob(conf)) |
| 214 | + conf_dirs.sort() |
| 215 | + |
| 216 | + prop_list = return_prop_list(parameter) |
| 217 | + task_list = [] |
| 218 | + for ii in conf_dirs: |
| 219 | + conf_dir_global = os.path.join(work_d, ii) |
| 220 | + for jj in prop_list: |
| 221 | + prop = os.path.join(conf_dir_global, jj) |
| 222 | + os.chdir(prop) |
| 223 | + prop_tasks = glob.glob(os.path.join(prop, 'task.*')) |
| 224 | + prop_tasks.sort() |
| 225 | + for kk in prop_tasks: |
| 226 | + task_list.append(kk) |
| 227 | + |
| 228 | + all_jobs = task_list |
| 229 | + njobs = len(all_jobs) |
| 230 | + jobs = [] |
| 231 | + for job in all_jobs: |
| 232 | + jobs.append(pathlib.Path(job)) |
| 233 | + |
| 234 | + os.chdir(cwd) |
| 235 | + op_out = OPIO({ |
| 236 | + "output": op_in["input"], |
| 237 | + "njobs": njobs, |
| 238 | + "task_paths": jobs |
| 239 | + }) |
| 240 | + return op_out |
| 241 | + |
| 242 | + |
| 243 | +class PropsPostABACUS(OP): |
| 244 | + """ |
| 245 | + class for analyzing calculation results |
| 246 | + """ |
| 247 | + |
| 248 | + def __init__(self): |
| 249 | + pass |
| 250 | + |
| 251 | + @classmethod |
| 252 | + def get_input_sign(cls): |
| 253 | + return OPIOSign({ |
| 254 | + 'input_post': Artifact(Path, sub_path=False), |
| 255 | + 'input_all': Artifact(Path, sub_path=False), |
| 256 | + 'param': Artifact(Path), |
| 257 | + 'path': str |
| 258 | + }) |
| 259 | + |
| 260 | + @classmethod |
| 261 | + def get_output_sign(cls): |
| 262 | + return OPIOSign({ |
| 263 | + 'output_post': Artifact(Path, sub_path=False) |
| 264 | + }) |
| 265 | + |
| 266 | + @OP.exec_sign_check |
| 267 | + def execute(self, op_in: OPIO) -> OPIO: |
| 268 | + from apex.property.common_prop import post_property |
| 269 | + |
| 270 | + cwd = os.getcwd() |
| 271 | + os.chdir(str(op_in['input_all']) + op_in['path']) |
| 272 | + shutil.copytree(str(op_in['input_post']) + op_in['path'], './', dirs_exist_ok=True) |
| 273 | + |
| 274 | + param_argv = op_in["param"] |
| 275 | + post_property(loadfn(param_argv)["structures"], loadfn(param_argv)["interaction"], loadfn(param_argv)["properties"]) |
| 276 | + |
| 277 | + os.chdir(cwd) |
| 278 | + shutil.copytree(str(op_in['input_all']) + op_in['path'] + '/confs', './confs', dirs_exist_ok=True) |
| 279 | + |
| 280 | + op_out = OPIO({ |
| 281 | + 'output_post': Path('./confs') |
| 282 | + }) |
| 283 | + return op_out |
0 commit comments