lorenzo-rovigatti / oxDNA

A new version of the code to simulate the oxDNA/oxRNA models, now equipped with Python bindings
https://dna.physics.ox.ac.uk/
GNU General Public License v3.0
38 stars 26 forks source link

[BUG] oxpy - FFS Simulation Type Fails to Log Correctly When Running Consecutive Processes #97

Closed JeDeveloper closed 3 months ago

JeDeveloper commented 3 months ago

Describe the bug When oxpy opens a context manager, leaves its scope, and then opens another context manager in the same python script, the second oxpy simulation behaves different. I am specifically working on a forward-flux-sampling simulation.

To Reproduce run the following script (included with the example files i have attached) and compare the log.dat files in sim1 and sim2. sim1 and sim2 are identical. oxpy_bug_eg.zip

import oxpy
import json
import os

os.chdir("sim1")

with open("input.json", "r") as f:
        input_file_dict = json.load(f)

with oxpy.Context():
      ox_input = oxpy.InputFile()
      for k,v in input_file_dict.items():
          ox_input[k] = str(v)
      manager = oxpy.OxpyManager(ox_input)
      manager.run_complete()

os.chdir("../sim3")

with open("input.json", "r") as f:
        input_file_dict = json.load(f)

with oxpy.Context():
      ox_input = oxpy.InputFile()
      for k,v in input_file_dict.items():
          ox_input[k] = str(v)
      manager = oxpy.OxpyManager(ox_input)
      manager.run_complete()

A vim diff of the two log.dat files produces: image ` Expected behavior Running the same simulation should produce the same results.

Desktop (please complete the following information):

lorenzo-rovigatti commented 3 months ago

Thank you for the report Josh. The wrong behaviour was due to 2 concomitant bugs. I committed a change that should solve the issue by fixing one bug, and by making the code print an exception when the user tries to initialise an OxpyManager before the instance of the previous one has been destroyed. In other words, if you update your code the following script should work:

import oxpy
import json
import os

os.chdir("sim1")

with open("input.json", "r") as f:
        input_file_dict = json.load(f)

with oxpy.Context():
      ox_input = oxpy.InputFile()
      for k,v in input_file_dict.items():
          ox_input[k] = str(v)
      manager = oxpy.OxpyManager(ox_input)
      manager.run_complete()
      del manager # <--- note the explicit deletion

os.chdir("../sim3")

with open("input.json", "r") as f:
        input_file_dict = json.load(f)

with oxpy.Context():
      ox_input = oxpy.InputFile()
      for k,v in input_file_dict.items():
          ox_input[k] = str(v)
      manager = oxpy.OxpyManager(ox_input)
      manager.run_complete()