Skip to content

Commit 1f2da7b

Browse files
committed
separate dumper and logger in 2 files
1 parent 3e5219e commit 1f2da7b

File tree

2 files changed

+60
-42
lines changed

2 files changed

+60
-42
lines changed

docs/source/chapters/chapter5.rst

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@ The two methods named *update_log_minimize()* and *update_dump_file()*, are used
3030
to print the information in the terminal and in a LAMMPS-type data file, respectively.
3131
These two methods will be written in the following.
3232

33-
Update the dump and log
34-
-----------------------
33+
Create logger
34+
-------------
3535

36-
Let us add the following functions named *update_dump_file* and *log_simulation_data*
37-
to the *tools.py* file. Here, some variable are being printed in a file, such as box dimension and atom positions.
38-
All quantities are dimensionalized before getting outputed, and the file follows
39-
a LAMMPS dump format, and can be read by molecular dynamics softwares like VMD.
36+
Let us create functions named *log_simulation_data* to a file named *logger.py*.
37+
With the logger, some output are being printed in a file, as well as in the terminal.
38+
The frequency of printing is set by *thermo_period*, see :ref:`chapter3-label`.
39+
All quantities are re-dimensionalized before getting outputed.
4040

41-
.. label:: start_tools_class
41+
.. label:: start_logger_class
4242

4343
.. code-block:: python
4444
45-
import numpy as np
46-
4745
import logging
4846
logging.basicConfig(
4947
level=logging.INFO,
@@ -64,6 +62,48 @@ a LAMMPS dump format, and can be read by molecular dynamics softwares like VMD.
6462
logger.addHandler(console_handler)
6563
logger.addHandler(file_handler)
6664
65+
def log_simulation_data(code):
66+
if code.thermo_period is not None:
67+
if code.step % code.thermo_period == 0:
68+
if code.step == 0:
69+
Epot = code.compute_potential(output="potential") \
70+
* code.reference_energy # kcal/mol
71+
else:
72+
Epot = code.Epot * code.reference_energy # kcal/mol
73+
if code.step == 0:
74+
if code.thermo_outputs == "Epot":
75+
logger.info(f"step Epot")
76+
elif code.thermo_outputs == "Epot-MaxF":
77+
logger.info(f"step Epot MaxF")
78+
elif code.thermo_outputs == "Epot-press":
79+
logger.info(f"step Epot press")
80+
if code.thermo_outputs == "Epot":
81+
logger.info(f"{code.step} {Epot:.2f}")
82+
elif code.thermo_outputs == "Epot-MaxF":
83+
logger.info(f"{code.step} {Epot:.2f} {code.MaxF:.2f}")
84+
elif code.thermo_outputs == "Epot-press":
85+
code.calculate_pressure()
86+
press = code.pressure \
87+
* code.reference_pressure # Atm
88+
logger.info(f"{code.step} {Epot:.2f} {press:.2f}")
89+
90+
.. label:: end_logger_class
91+
92+
Create dumper
93+
-------------
94+
95+
Let us create a function named *update_dump_file* to a file named
96+
*dumper.py*. The dumper will print a *.lammpstrj file*, which contains the box
97+
dimensions and atom positions at every chosen frame (set by *dumping_period*,
98+
see :ref:`chapter3-label`). All quantities are dimensionalized before getting outputed, and the file follows
99+
a LAMMPS dump format, and can be read by molecular dynamics softwares like VMD.
100+
101+
.. label:: start_dumper_class
102+
103+
.. code-block:: python
104+
105+
import numpy as np
106+
67107
def update_dump_file(code, filename, velocity=False):
68108
if code.dumping_period is not None:
69109
if code.step % code.dumping_period == 0:
@@ -110,55 +150,33 @@ a LAMMPS dump format, and can be read by molecular dynamics softwares like VMD.
110150
f.write(characters % (v[0], v[1], v[2],
111151
v[3], v[4], '\n'))
112152
cpt += 1
113-
f.close()
114-
115-
def log_simulation_data(code):
116-
if code.thermo_period is not None:
117-
if code.step % code.thermo_period == 0:
118-
if code.step == 0:
119-
Epot = code.compute_potential(output="potential") \
120-
* code.reference_energy # kcal/mol
121-
else:
122-
Epot = code.Epot * code.reference_energy # kcal/mol
123-
if code.step == 0:
124-
if code.thermo_outputs == "Epot":
125-
logger.info(f"step Epot")
126-
elif code.thermo_outputs == "Epot-MaxF":
127-
logger.info(f"step Epot MaxF")
128-
elif code.thermo_outputs == "Epot-press":
129-
logger.info(f"step Epot press")
130-
if code.thermo_outputs == "Epot":
131-
logger.info(f"{code.step} {Epot:.2f}")
132-
elif code.thermo_outputs == "Epot-MaxF":
133-
logger.info(f"{code.step} {Epot:.2f} {code.MaxF:.2f}")
134-
elif code.thermo_outputs == "Epot-press":
135-
code.calculate_pressure()
136-
press = code.pressure \
137-
* code.reference_pressure # Atm
138-
logger.info(f"{code.step} {Epot:.2f} {press:.2f}")
153+
f.close()
139154
140-
.. label:: end_tools_class
155+
.. label:: end_dumper_class
141156

142157
Import the functions
143158
--------------------
144159

145-
The Monte Carlo and the Minimize class must import *update_dump_file* and *log_simulation_data*.
160+
The Monte Carlo and the Minimize class must both import *update_dump_file*
161+
and *log_simulation_data*. Add these lines at the top of the *MonteCarlo.py* file:
146162

147163
.. label:: start_MonteCarlo_class
148164

149165
.. code-block:: python
150166
151-
from tools import update_dump_file, log_simulation_data
167+
from dumper import update_dump_file
168+
from logger import log_simulation_data
152169
153170
.. label:: end_MonteCarlo_class
154171

155-
and
172+
Add the same lines at the top of the *MinimizeEnergy.py* file:
156173

157174
.. label:: start_MinimizeEnergy_class
158175

159176
.. code-block:: python
160177
161-
from tools import update_dump_file, log_simulation_data
178+
from dumper import update_dump_file
179+
from logger import log_simulation_data
162180
163181
.. label:: end_MinimizeEnergy_class
164182

docs/source/chapters/chapter7.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ and :math:`T = 55~^\circ\text{C}`. More details are given in the first illustrat
9090
On the side note, a relatively small cut-off as well as a small number of atoms were
9191
chosen to make the calculation faster.
9292

93-
.. label:: start_test_MonteCarloPressure_class
93+
.. label:: start_test_7a_class
9494

9595
.. code-block:: python
9696
@@ -144,7 +144,7 @@ chosen to make the calculation faster.
144144
# pV_over_RT = np.round((pressure * volume / (R * T) * Na).magnitude,2)
145145
# print("p v / R T =", pV_over_RT, " --- (The expected value from Wood1957 is 1.5)")
146146
147-
.. label:: end_test_MonteCarloPressure_class
147+
.. label:: end_test_7a_class
148148

149149
Which should return a value for :math:`p V / R T` that is close to the expected value
150150
of 1.5 by Wood and Parker for :math:`\tau = V/V^* = 2` (see Fig. 4 in Ref. :cite:`woodMonteCarloEquation1957`):

0 commit comments

Comments
 (0)