hpc-task 0.1.2__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hpc_task
3
- Version: 0.1.2
3
+ Version: 0.2.0
4
4
  Summary: HPC Task python library.
5
5
  Home-page: https://gitee.com/pjren/hpc_task
6
6
  Author: Renpj
@@ -61,13 +61,14 @@ hpc.close() # 此时可以关闭 ssh 通道
61
61
  # 在运行时候再重新构造和连接 hpc
62
62
  hpc = HPCTask(jobid=jobid) # workdir 会根据 jobid 提取得到
63
63
  hpc.connect(target_host)
64
-
64
+ hpc.get_workdir()
65
65
  # 结构、计算参数设置
66
66
  atoms = ase.Atoms('N2', positions=[(0., 0., 0.), (1.4, 0., 0.)],
67
67
  cell=[10, 10, 10], pbc=True)
68
68
  # calc 设置
69
69
  calc = hpc_Vasp(
70
70
  xc='pbe',
71
+ directory=hpc.workdir, # 必须设置, 会新建文件夹,并进入文件夹执行命令
71
72
  command='mpirun -np 8 vasp544_std', # 必须设置,或者提供了 ASE vasp 的相关设置
72
73
  hpctask=hpc, # 这个非常关键,必须提供
73
74
  gamma=True,
@@ -1,63 +1,64 @@
1
- # HPC Task
2
-
3
- Python package for easy HPC task management based on paramiko.
4
-
5
- ## Installation
6
-
7
- pip install -U hpc_task
8
-
9
- **Requirements**
10
- * paramiko
11
- * ase
12
-
13
- ## Usage
14
-
15
- ```
16
- import ase
17
- from hpc_task.hpc import HPCTask
18
- from hpc_task.hpc_calculator.vasp import Vasp as hpc_Vasp # 必须导入hpc_Vasp 而非原生的 ase 的 Vasp
19
-
20
- target_host = {
21
- 'hostname': "TARGET HOST IP",
22
- 'port': 22,
23
- 'username': "YOUR_USERNAME",
24
- 'password': "YOUR_PASSWD",
25
- }
26
-
27
- workdir = 'test_vasp_calc'
28
- hpc = HPCTask(workdir=workdir)
29
- hpc.scriptdir = '/data/bin/remote.job' # 提交作业的脚本
30
- hpc.connect(target_host)
31
- # 启动队列,得到 jobid
32
- hpc.prerun()
33
- jobid = hpc.jobid
34
- hpc.close() # 此时可以关闭 ssh 通道
35
-
36
- # 在运行时候再重新构造和连接 hpc
37
- hpc = HPCTask(jobid=jobid) # workdir 会根据 jobid 提取得到
38
- hpc.connect(target_host)
39
-
40
- # 结构、计算参数设置
41
- atoms = ase.Atoms('N2', positions=[(0., 0., 0.), (1.4, 0., 0.)],
42
- cell=[10, 10, 10], pbc=True)
43
- # calc 设置
44
- calc = hpc_Vasp(
45
- xc='pbe',
46
- command='mpirun -np 8 vasp544_std', # 必须设置,或者提供了 ASE vasp 的相关设置
47
- hpctask=hpc, # 这个非常关键,必须提供
48
- gamma=True,
49
- encut=400,
50
- lwave=False,
51
- lcharg=False,
52
- )
53
- # 计算
54
- atoms.calc = calc
55
- e = atoms.get_potential_energy()
56
- print(f"Energy of N2 is {e} eV.")
57
- # finish job,结束节点任务
58
- hpc.postrun()
59
- # 关闭通道
60
- hpc.close()
61
- ```
62
-
63
- ## TODO
1
+ # HPC Task
2
+
3
+ Python package for easy HPC task management based on paramiko.
4
+
5
+ ## Installation
6
+
7
+ pip install -U hpc_task
8
+
9
+ **Requirements**
10
+ * paramiko
11
+ * ase
12
+
13
+ ## Usage
14
+
15
+ ```
16
+ import ase
17
+ from hpc_task.hpc import HPCTask
18
+ from hpc_task.hpc_calculator.vasp import Vasp as hpc_Vasp # 必须导入hpc_Vasp 而非原生的 ase 的 Vasp
19
+
20
+ target_host = {
21
+ 'hostname': "TARGET HOST IP",
22
+ 'port': 22,
23
+ 'username': "YOUR_USERNAME",
24
+ 'password': "YOUR_PASSWD",
25
+ }
26
+
27
+ workdir = 'test_vasp_calc'
28
+ hpc = HPCTask(workdir=workdir)
29
+ hpc.scriptdir = '/data/bin/remote.job' # 提交作业的脚本
30
+ hpc.connect(target_host)
31
+ # 启动队列,得到 jobid
32
+ hpc.prerun()
33
+ jobid = hpc.jobid
34
+ hpc.close() # 此时可以关闭 ssh 通道
35
+
36
+ # 在运行时候再重新构造和连接 hpc
37
+ hpc = HPCTask(jobid=jobid) # workdir 会根据 jobid 提取得到
38
+ hpc.connect(target_host)
39
+ hpc.get_workdir()
40
+ # 结构、计算参数设置
41
+ atoms = ase.Atoms('N2', positions=[(0., 0., 0.), (1.4, 0., 0.)],
42
+ cell=[10, 10, 10], pbc=True)
43
+ # calc 设置
44
+ calc = hpc_Vasp(
45
+ xc='pbe',
46
+ directory=hpc.workdir, # 必须设置, 会新建文件夹,并进入文件夹执行命令
47
+ command='mpirun -np 8 vasp544_std', # 必须设置,或者提供了 ASE vasp 的相关设置
48
+ hpctask=hpc, # 这个非常关键,必须提供
49
+ gamma=True,
50
+ encut=400,
51
+ lwave=False,
52
+ lcharg=False,
53
+ )
54
+ # 计算
55
+ atoms.calc = calc
56
+ e = atoms.get_potential_energy()
57
+ print(f"Energy of N2 is {e} eV.")
58
+ # finish job,结束节点任务
59
+ hpc.postrun()
60
+ # 关闭通道
61
+ hpc.close()
62
+ ```
63
+
64
+ ## TODO
@@ -79,7 +79,7 @@ class HPCTask:
79
79
  abs_workdir = stdout.read().decode('utf-8').strip().split()[-1]
80
80
  workdir = Path(abs_workdir).relative_to(Path(self._remote_parent_workdir))
81
81
  if self.workdir is None:
82
- self.workdir = str(workdir)
82
+ self.workdir = workdir.as_posix()
83
83
  return self.workdir
84
84
 
85
85
  @property
@@ -292,7 +292,6 @@ class HPCTask:
292
292
  print(f"Waiting for the queue 10 seconds...")
293
293
  sleep(10)
294
294
  status = self.status
295
- need_sync = False
296
295
  elif status in ('SUSPENDED', 'DONE'):
297
296
  output = ''
298
297
  err = f"Job {self.jobid} status: {status}"
@@ -327,12 +326,12 @@ class HPCTask:
327
326
  rel_path = Path(root).relative_to(local_path)
328
327
  remote_dir = remote_path / rel_path
329
328
  if str(rel_path) != ".":
330
- _ensure_remote_dir_exists(sftp_client, str(remote_dir))
329
+ _ensure_remote_dir_exists(sftp_client, remote_dir.as_posix())
331
330
  # 上传文件
332
331
  for file in files:
333
332
  local_file = Path(root) / file
334
333
  remote_file = remote_dir / file
335
- sftp_client.put(str(local_file), str(remote_file), confirm=False)
334
+ sftp_client.put(local_file.as_posix(), remote_file.as_posix(), confirm=False)
336
335
 
337
336
  return None
338
337
 
@@ -365,13 +364,14 @@ def _ensure_remote_dir_exists(sftp, remote_dir):
365
364
  except FileNotFoundError:
366
365
  # 递归创建目录
367
366
  parts = Path(remote_dir).parts
368
- current_path = parts[0]
367
+ current_path = Path(parts[0])
369
368
  for part in parts[1:]: # 跳过根目录
370
- current_path = os.path.join(current_path, part)
369
+ # current_path = os.path.join(current_path, part)
370
+ current_path = current_path / part
371
371
  try:
372
- sftp.stat(current_path)
372
+ sftp.stat(current_path.as_posix())
373
373
  except FileNotFoundError:
374
- sftp.mkdir(current_path)
374
+ sftp.mkdir(current_path.as_posix())
375
375
 
376
376
  def _download_dir(sftp, remote, local):
377
377
  os.makedirs(local, exist_ok=True)
@@ -0,0 +1,88 @@
1
+ import os
2
+ import subprocess
3
+ from pathlib import Path
4
+
5
+ from ase.calculators import calculator
6
+ from ase.calculators.vasp import Vasp as ase_Vasp
7
+ import ase.io
8
+ from hpc_task.hpc import HPCTask
9
+
10
+
11
+ class Vasp(ase_Vasp):
12
+ def __init__(self,
13
+ atoms=None,
14
+ restart=None,
15
+ directory='.',
16
+ label='vasp',
17
+ command=None,
18
+ hpctask:HPCTask=None,
19
+ **kwargs):
20
+ super().__init__(
21
+ atoms=atoms,
22
+ restart=restart,
23
+ directory=directory,
24
+ label=label,
25
+ command=command,
26
+ **kwargs
27
+ )
28
+ self.hpctask = hpctask
29
+
30
+ def _run(self, command=None, out=None, directory=None):
31
+ """Method to explicitly execute VASP"""
32
+ if command is None:
33
+ command = self.command
34
+
35
+ if self.hpctask is not None:
36
+ remote_cmd = f"gosh-remote -vv client run '{command}'"
37
+ returncode, stderr, stdout = self.hpctask.execute(remote_cmd)
38
+
39
+ else:
40
+ result = subprocess.run(command,
41
+ shell=True,
42
+ cwd=directory,
43
+ capture_output=True,
44
+ text=True)
45
+ stdout = result.stdout
46
+ returncode = result.returncode
47
+ stderr = result.stderr
48
+ if out is not None:
49
+ out.write(stdout)
50
+ return returncode, stderr
51
+
52
+ def execute_only(self):
53
+ """
54
+ 仅仅执行 command 命令,从文件夹中读取 INCAR、POTCAR、POSCAR、KPOINTS
55
+ """
56
+ self.read_potcar(filename=self._indir('POTCAR'))
57
+ self.read_incar(filename=self._indir('INCAR'))
58
+ self.read_kpoints(filename=self._indir('KPOINTS'))
59
+ atoms = ase.io.read(filename=self._indir('POSCAR'))
60
+ # 需要读进去ase-sort.dat,否则结果会报错
61
+ sortfile = self._indir('ase-sort.dat')
62
+ if os.path.isfile(sortfile):
63
+ self.sort = []
64
+ self.resort = []
65
+ with open(sortfile) as fd:
66
+ for line in fd:
67
+ sort, resort = line.split()
68
+ self.sort.append(int(sort))
69
+ self.resort.append(int(resort))
70
+ else:
71
+ # Redo the sorting
72
+ self.initialize(atoms)
73
+
74
+ command = self.make_command(self.command)
75
+ with self._txt_outstream() as out:
76
+ errorcode, stderr = self._run(command=command,
77
+ out=out,
78
+ directory=self.directory)
79
+
80
+ if errorcode:
81
+ raise calculator.CalculationFailed(
82
+ '{} in {} returned an error: {:d} stderr {}'.format(
83
+ self.name, Path(self.directory).resolve(), errorcode,
84
+ stderr))
85
+
86
+ # Read results from calculation
87
+ self.update_atoms(atoms)
88
+ self.read_results()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hpc_task
3
- Version: 0.1.2
3
+ Version: 0.2.0
4
4
  Summary: HPC Task python library.
5
5
  Home-page: https://gitee.com/pjren/hpc_task
6
6
  Author: Renpj
@@ -61,13 +61,14 @@ hpc.close() # 此时可以关闭 ssh 通道
61
61
  # 在运行时候再重新构造和连接 hpc
62
62
  hpc = HPCTask(jobid=jobid) # workdir 会根据 jobid 提取得到
63
63
  hpc.connect(target_host)
64
-
64
+ hpc.get_workdir()
65
65
  # 结构、计算参数设置
66
66
  atoms = ase.Atoms('N2', positions=[(0., 0., 0.), (1.4, 0., 0.)],
67
67
  cell=[10, 10, 10], pbc=True)
68
68
  # calc 设置
69
69
  calc = hpc_Vasp(
70
70
  xc='pbe',
71
+ directory=hpc.workdir, # 必须设置, 会新建文件夹,并进入文件夹执行命令
71
72
  command='mpirun -np 8 vasp544_std', # 必须设置,或者提供了 ASE vasp 的相关设置
72
73
  hpctask=hpc, # 这个非常关键,必须提供
73
74
  gamma=True,
@@ -10,7 +10,7 @@ install_requires = [
10
10
 
11
11
  setup(
12
12
  name='hpc_task',
13
- version='0.1.2',
13
+ version='0.2.0',
14
14
  url='https://gitee.com/pjren/hpc_task',
15
15
  license='MIT',
16
16
  author='Renpj',
@@ -153,3 +153,31 @@ class TestHPCTask:
153
153
  # finish job
154
154
  hpc.postrun()
155
155
  hpc.close()
156
+
157
+ def test_hpc_vasp_execute_only(self):
158
+ workdir = 'test_hpc_vasp_execute_only'
159
+ Path(workdir).mkdir(parents=True, exist_ok=True)
160
+ shutil.copyfile('test_files/INCAR', Path(workdir)/'INCAR')
161
+ shutil.copyfile('test_files/POSCAR', Path(workdir)/'POSCAR')
162
+ shutil.copyfile('test_files/POTCAR', Path(workdir)/'POTCAR')
163
+ shutil.copyfile('test_files/KPOINTS', Path(workdir)/'KPOINTS')
164
+
165
+ # 队列占据
166
+ hpc = HPCTask(workdir=workdir)
167
+ hpc.scriptdir = '/data/bin/remote.job'
168
+ hpc.connect(target_host, jump_host)
169
+ hpc.prerun()
170
+ calc = hpc_Vasp(
171
+ directory=workdir,
172
+ command='mpirun -np 2 vasp_gam',
173
+ hpctask=hpc, # 这个非常关键
174
+ )
175
+ # 计算
176
+ calc.execute_only()
177
+ atoms = calc.atoms
178
+ atoms.calc = calc
179
+ e = atoms.get_potential_energy()
180
+ print(f"Energy of N2 is {e} eV.")
181
+ # finish job
182
+ hpc.postrun()
183
+ hpc.close()
@@ -1,46 +0,0 @@
1
- import subprocess
2
-
3
- from ase.calculators.vasp import Vasp as ase_Vasp
4
- from hpc_task.hpc import HPCTask
5
-
6
-
7
- class Vasp(ase_Vasp):
8
- def __init__(self,
9
- atoms=None,
10
- restart=None,
11
- directory='.',
12
- label='vasp',
13
- command=None,
14
- hpctask:HPCTask=None,
15
- **kwargs):
16
- super().__init__(
17
- atoms=atoms,
18
- restart=restart,
19
- directory=directory,
20
- label=label,
21
- command=command,
22
- **kwargs
23
- )
24
- self.hpctask = hpctask
25
-
26
- def _run(self, command=None, out=None, directory=None):
27
- """Method to explicitly execute VASP"""
28
- if command is None:
29
- command = self.command
30
-
31
- if self.hpctask is not None:
32
- remote_cmd = f"gosh-remote -vv client run '{command}'"
33
- returncode, stderr, stdout = self.hpctask.execute(remote_cmd)
34
-
35
- else:
36
- result = subprocess.run(command,
37
- shell=True,
38
- cwd=directory,
39
- capture_output=True,
40
- text=True)
41
- stdout = result.stdout
42
- returncode = result.returncode
43
- stderr = result.stderr
44
- if out is not None:
45
- out.write(stdout)
46
- return returncode, stderr
File without changes
File without changes
File without changes