Monte Carlo re-implementation in Python - #4765
Conversation
Provide an alternative implementation of SingleReaction, ReactionAlgorithm and ConstantpHEnsemble in pure Python for rapid prototyping of new Monte Carlo methods. Co-authored-by: Pablo Miguel Blanco Andrés <blancoapa@natur.cuni.cz>
Co-authored-by: Jean-Noël Grad <jgrad@icp.uni-stuttgart.de>
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
Target is ESPResSo 5.1 release, therefore only non-silent API changes are allowed. TODO list:
|
Also rewrite unit tests as Python tests.
|
@kosovan @pm-blanco @jorch28 @RudolfWeeber I would propose to work on the refactoring in two stages. First, convert the C++ code to Python code and merge it into the python branch. This is now 95% complete. There are a few items that would require your input to finalize this PR, and I will discuss them further down below. Second, work on the bigger API changes, such as:
My main motivation for this approach is removing the legacy The following parts of the code would require additional input:
The Python implementation is roughly 3 times slower that the C++ one according to the acid-base reservoir benchmark. The overhead mainly comes from the particle changes. The new code already uses particle property updates via dict, and implementing a bulk particle update (i.e. a list of dicts and a list of ids to trigger a single cell system rebuild) didn't improve runtimes. The particle numbers per type are calculated once at the start of |
|
@jngrad As I have seen, restore_system() depends on delete_created_particles() and restore_system() is employed to reject MC trials moves and restore the system to its original state before proposing the move. This can be seen in methods self.generic_one_way_reaction(), self.displacement_mc_move_for_particles_of_type, self. calculate_particle_insertion_potential_energy(). Therefore, it seems a crucial method to develop new MC algorithms. Thus, if delete_created_particles() becomes private would restore_system() also become a private method? By the way, the rogue method , calculate_log_acceptance_probability, removed in previous commit was actually being employed in method generic_one_way_reaction in line 946. The statement is: ln_bf = self.calculate_log_acceptance_probability( I don't know if you passed the tests after making the commit, but, as ReactionAlgorithm is not a child class, it seems like deleting the rogue method is Bug. |
|
@jorch28 thank you for looking into this. The testsuite passed. The green check mark will give you more details about the testsuites. The method
|
|
@jngrad Ok, now I see that all check have passed refers to the testsuite from pymbe and espresso. Then, there is not much to worry about. Sorry to insist, I must be missing something, but I still think there is something wrong with the method generic_one_way_reaction, but I cannot find what it is: Because, it calls self.log_acceptance_probability in line 946, with an extra argument (which should raise an error), plus it raises NotImplementedError when called from the same class ReactionAlgorithm. Therefore, should give an error instead of returning a float. About delete_created_particles(), users should not touch unless they know what they are doing, because it would break restore_system(). |
Isn't this already fixed by 623d698?
The implementation was removed in 27aee23 because
When I suspect the presence of a bug, I like to craft a minimal working example (MWE) that attempts to trigger it. Typically, a MWE is 5 to 10 lines of Python code. This technique helps me better understand the code behavior and determine which specific conditions need to be met for the bug to occur. I use print statements to guide me through the nested code paths and update my mental model of the functionality. Would you be interested in trying that out?
Would you prefer |
|
My apologies for not replying much earlier.
Yes it was fixed. I did not notice it at a first glance.
Thank you for clarifying, now I understand that
This late afternoon I'm going to dedicate time to craft this MWE to better understand the code.
Honestly, I only have a basic understanding of MonteCarlo methods. If you think that is a strong commitment that we may break at long term, then it is better to put them as private methods . |
|
I think this is a great step towards providing more flexibility to users in terms of MC methods.
invalidate ghostThe AI thinks, and I agree, that this is actually an ommision in the core funciton that delets particles. PerformanceThere are a few candidates, such as using np.choice(..., replace=False) to drw mupltiple pids, or using slices for bulk updates. As mentioned above, I'm also fine with only doing the invalidate_ghost thing in this pr and following up with performance in a different pr. |
|
I agree with @RudolfWeeber that this is a great step towards providing a more flexible MC framework for users and developers and therefore I am looking forward to seeing this PR merged. I think that splitting the refactoring in two stages is sensible, and having an agnostic Regarding if I checked the Monte Carlo implementation and it looks OK to me, apart from the missing documention in various methods. I understand correctly that the plan is to solve the issue with MC methods and constraints in the next PR? I think line 827 in |
Co-authored-by: Pablo Miguel Blanco Andrés <blancoapa@natur.cuni.cz>
|
Here are benchmark results for
Overall, the performance loss of moving all the logic from C++ to Python was well mitigated. When using more than 1 OpenMP thread, we are actually faster. These benchmark results will be slightly less impressive versus 5.0.2, because we plan on backporting some of the MC performance improvements introduced by this PR. @pm-blanco and @jorch28 this PR can be added to the agenda of the next pyMBE meeting. It is now in a state where it can be merged. Further API changes can be introduced in a follow-up PR. To get this to work with pyMBE, you will need to use my es510 branch, where I dropped support for 4.2 and introduced support for 5.1-dev. The pyMBE unit tests pass in CI according to the debian workflow, but some of the |
Fixes #4617, fixes #5178
Description of changes: