Skip to content

Replace global_vars with frozen SimulationParameters dataclass#17

Merged
bjmorgan merged 6 commits into
masterfrom
modernise/remove-global-state
Mar 4, 2026
Merged

Replace global_vars with frozen SimulationParameters dataclass#17
bjmorgan merged 6 commits into
masterfrom
modernise/remove-global-state

Conversation

@bjmorgan

@bjmorgan bjmorgan commented Mar 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • Eliminate module-level mutable state (temperature, rate_prefactor, kT) from global_vars.py that prevented running simulations at different temperatures in the same process
  • Introduce a frozen SimulationParameters dataclass holding temperature and rate_prefactor (with derived kT property), threaded explicitly through the call chain
  • Simulation.__init__ now requires a SimulationParameters argument (no defaults)
  • Add lattice_mc/constants.py with the k_boltzmann physical constant
  • Delete lattice_mc/global_vars.py
  • Update all tests, examples notebook, and docs

bjmorgan added 2 commits March 4, 2026 10:11
Eliminate module-level mutable state (temperature, rate_prefactor, kT)
that prevented running simulations at different temperatures in the
same process.

Introduce a frozen SimulationParameters dataclass holding temperature
and rate_prefactor, with a derived kT property. This is threaded
explicitly through the call chain: Simulation -> Lattice -> Jump and
Transitions.

- Add lattice_mc/constants.py with k_boltzmann physical constant
- Add SimulationParameters frozen dataclass to simulation.py
- Simulation.__init__ now requires a SimulationParameters argument
- Jump and Transitions accept params as keyword-only argument
- metropolis() takes kT as an explicit parameter
- LookupTable reads kT from lattice.params
- Delete lattice_mc/global_vars.py
- Update all tests, examples, and docs
- Add RuntimeError guard in Lattice.potential_jumps() when params is
  None, with a clear error message
- Fix constants.py module docstring placement (was after import)
- Add trailing newline to notebook
- Add test for the params guard

This comment was marked as resolved.

bjmorgan added 2 commits March 4, 2026 11:19
Validate that temperature and rate_prefactor are positive in
__post_init__. Fix pre-existing TranstitionsTestCase → TransitionsTestCase
typo.

Addresses Copilot review comments on PR #17.
Simulation.setup_lookup_table() advertised 'coordination_number' as a
valid hamiltonian but LookupTable only accepts 'nearest-neighbour',
so passing 'coordination_number' always raised ValueError at the
LookupTable level. The lookup table cannot support coordination-number
energies because the energy change is a many-body calculation that
depends on the full local microstate, not just (l1, l2, c1, c2).

Coordination-number energies remain fully supported via on-the-fly
computation in Jump.coordination_number_delta_E() — no lookup table
is needed since cn_energies are themselves dict lookups.

Add integration test for simulation with cn_energies.

This comment was marked as resolved.

The bare ValueError made debugging harder when an unsupported
hamiltonian was passed. Now includes the invalid value and the
list of accepted values in the error message.

This comment was marked as resolved.

- SimulationTestCase docstring: "Species" -> "Simulation"
- test_jump_raises_BlockedLatticeErrror: extra 'r' removed
- LookupTable docstring: "nearest-neigbour" -> "nearest-neighbour"
@bjmorgan bjmorgan merged commit 0f555ae into master Mar 4, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants