Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

LammpsSimulator.py 2.4 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  1. from __future__ import annotations
  2. import dataclasses
  3. import pathlib
  4. import subprocess
  5. import jinja2
  6. from zntrack import Node, dvc, zn
  7. @dataclasses.dataclass
  8. class Params:
  9. name: str
  10. timestep: float = 0.001
  11. dump_interval: int = 1000
  12. temperature: float = 1100
  13. thermo_intervall: int = 1000
  14. steps: int = 200000
  15. barostat_factor: int = 500
  16. pressure: float = 1.0
  17. input_nstep: int = 0
  18. @property
  19. def input_file(self):
  20. return pathlib.Path(f"{self.name}.in")
  21. @property
  22. def dump_file(self):
  23. return f"{self.name}.lammpstraj"
  24. @property
  25. def log_file(self):
  26. return f"{self.name}.log"
  27. class LammpsSimulator(Node):
  28. input_trajectory = dvc.deps()
  29. input_simulation: LammpsSimulator = zn.deps()
  30. template_file = dvc.deps()
  31. parameters = zn.Method()
  32. input_script: pathlib.Path = dvc.outs_no_cache()
  33. dump_file = dvc.outs()
  34. log_file = dvc.outs()
  35. def __init__(
  36. self,
  37. template_file=None,
  38. temperature=None,
  39. input_trajectory=None,
  40. input_simulation: LammpsSimulator = None,
  41. **kwargs,
  42. ):
  43. super().__init__(**kwargs)
  44. if not self.is_loaded:
  45. self.parameters = Params(self.node_name)
  46. self.template_file = pathlib.Path(template_file)
  47. self.input_simulation = input_simulation
  48. self.input_trajectory = input_trajectory
  49. # update temperature
  50. self.parameters.temperature = temperature
  51. # Let DVC know which files to track
  52. self.input_script = self.parameters.input_file
  53. self.dump_file = self.parameters.dump_file
  54. self.log_file = self.parameters.log_file
  55. def render_file(self):
  56. template = jinja2.Template(self.template_file.read_text())
  57. if self.input_trajectory is not None:
  58. input_trajectory = self.input_trajectory
  59. else:
  60. input_trajectory = self.input_simulation.dump_file
  61. self.parameters.input_nstep = self.input_simulation.parameters.steps
  62. self.input_script.write_text(
  63. template.render(
  64. **dataclasses.asdict(self.parameters),
  65. dump_file=self.parameters.dump_file,
  66. log_file=self.parameters.log_file,
  67. input_trajectory=input_trajectory,
  68. )
  69. )
  70. def run(self):
  71. self.render_file()
  72. subprocess.check_call([f"lmp_stable -in {self.input_script}"], shell=True)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...