@@ -46,7 +46,7 @@ All quantities are re-dimensionalized before getting outputed.
4646 import logging
4747
4848 # Function to set up the logger
49- def setup_logger (folder_name ):
49+ def setup_logger (folder_name , overwrite = False ):
5050 # Create a custom logger
5151 logger = logging.getLogger(' simulation_logger' )
5252 logger.setLevel(logging.INFO )
@@ -59,7 +59,10 @@ All quantities are re-dimensionalized before getting outputed.
5959 # Create handlers for console and file
6060 console_handler = logging.StreamHandler() # To log to the terminal
6161 log_file_path = os.path.join(folder_name, ' simulation.log' )
62- file_handler = logging.FileHandler(log_file_path) # To log to a file
62+
63+ # Use 'w' mode to overwrite the log file if overwrite is True, else use 'a' mode to append
64+ file_mode = ' w' if overwrite else ' a'
65+ file_handler = logging.FileHandler(log_file_path, mode = file_mode) # To log to a file
6366
6467 # Create formatters and add them to the handlers
6568 formatter = logging.Formatter(' %(message)s ' )
@@ -74,8 +77,8 @@ All quantities are re-dimensionalized before getting outputed.
7477
7578 def log_simulation_data (code ):
7679
77- # Setup the logger with the folder name
78- logger = setup_logger(code.data_folder)
80+ # Setup the logger with the folder name, overwriting the log if code.step is 0
81+ logger = setup_logger(code.data_folder, overwrite = (code.step == 0 ) )
7982
8083 # Logging the simulation data
8184 if code.thermo_period is not None :
@@ -227,7 +230,7 @@ files were indeed created without the *Outputs/* folder:
227230 .. label :: end_test_5a_class
228231
229232I addition to the files getting created, information must be printed in the terminal
230- during the similation :
233+ during the simulation :
231234
232235.. code-block :: bw
233236
@@ -236,4 +239,68 @@ during the similation:
236239 25 -1.08 1.81
237240 50 -1.11 1.42
238241 75 -1.22 3.77
239- 100 -2.10 1.28
242+ 100 -2.10 1.28
243+
244+ The data from the *simulation.log * can be used to generate plots using softwares
245+ line XmGrace, GnuPlot, or Python/Pyplot. For the later, one can use a simple data
246+ reader to import the data from *Outputs/simulation.log * into Python. Copy the
247+ following lines in a file named *reader.py *:
248+
249+ .. label :: start_reader_class
250+
251+ .. code-block :: python
252+
253+ import csv
254+
255+ def import_data (file_path ):
256+ """
257+ Imports a data file with a variable number of columns into a list
258+ of numerical arrays. The first line (header) is read as a string.
259+
260+ Parameters:
261+ - file_path (str): Path to the data file.
262+
263+ Returns:
264+ - header (str): The header line as a string.
265+ - data (list of lists): List where each sublist contains the numeric values of a row.
266+ """
267+ data = []
268+ header = " "
269+ with open (file_path, mode = ' r' ) as file :
270+ # Read the header as a string
271+ header = file .readline().strip()
272+ # Use csv.reader to process the remaining lines
273+ reader = csv.reader(file , delimiter = ' ' )
274+ for row in reader:
275+ # Filter out empty fields resulting from multiple spaces
276+ filtered_row = [float (value) for value in row if value]
277+ data.append(filtered_row)
278+ return header, data
279+
280+ .. label :: end_reader_class
281+
282+ The *import_data * function from *reader.py * can simply be used as follows:
283+
284+ .. label :: start_test_5b_class
285+
286+ from reader import import_data
287+
288+ file_path = "Outputs/simulation.log"
289+ header, data = import_data(file_path)
290+
291+ print(header)
292+ for row in data:
293+ print(row)
294+
295+ .. label :: end_test_5b_class
296+
297+ Which must return:
298+
299+ .. code-block :: bw
300+
301+ step Epot MaxF
302+ [0.0, 9.48, 1049.12]
303+ [25.0, -2.12, 1.22]
304+ [50.0, -2.19, 2.85]
305+ [75.0, -2.64, 0.99]
306+ [100.0, -2.64, 0.51]
0 commit comments