Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix mc random moves that still use outdated occupations #69

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/MonteCarloMoves/random_walks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ function MC_random_walk!(n_steps::Int,
for i_mc_step in 1:n_steps
current_lattice = lattice.configuration
# select a random site to hop from
hop_from = rand(eachindex(current_lattice.occupations))
hop_from = rand(eachindex(current_lattice.components[1]))
# select a random site to hop to (can be the same as hop_from)
hop_to = rand(eachindex(current_lattice.occupations))
hop_to = rand(eachindex(current_lattice.components[1]))
# propose a swap in occupation state (only if it maintains constant N)
proposed_lattice = deepcopy(current_lattice)

if proposed_lattice.occupations[hop_from] != proposed_lattice.occupations[hop_to]
proposed_lattice.occupations[hop_from], proposed_lattice.occupations[hop_to] =
proposed_lattice.occupations[hop_to], proposed_lattice.occupations[hop_from]
if proposed_lattice.components[1][hop_from] != proposed_lattice.components[1][hop_to]
proposed_lattice.components[1][hop_from], proposed_lattice.components[1][hop_to] =
proposed_lattice.components[1][hop_to], proposed_lattice.components[1][hop_from]
end

perturbation_energy = energy_perturb * (rand() - 0.5) * unit(lattice.energy)
Expand Down
32 changes: 9 additions & 23 deletions src/SamplingSchemes/nested_sampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,14 @@ routine for generating new samples. It performs a single step of the nested samp
- `liveset::LatticeGasWalkers`: The liveset of lattice gas walkers.
- `ns_params::LatticeNestedSamplingParameters`: The parameters for nested sampling.
- `mc_routine::MCRoutine`: The Monte Carlo routine for generating new samples.
- `accept_same_config::Bool=true`: A flag indicating whether to accept a new sample with the same configuration as an existing one. Default is `true`. Note that
if it is set to `false`, the sampling process may take significantly longer to find a new configuration.

## Returns
- `iter`: The iteration number of the liveset after the step.
- `emax`: The maximum energy of the liveset after the step.
"""
function nested_sampling_step!(liveset::LatticeGasWalkers,
ns_params::LatticeNestedSamplingParameters,
mc_routine::MCRoutine;
accept_same_config::Bool=true)
mc_routine::MCRoutine)
sort_by_energy!(liveset)
ats = liveset.walkers
h = liveset.hamiltonian
Expand All @@ -175,11 +172,7 @@ function nested_sampling_step!(liveset::LatticeGasWalkers,
error("Unsupported MCRoutine type: $mc_routine")
end
accept, rate, at = MC_random_walk!(ns_params.mc_steps, to_walk, h, emax; energy_perturb=ns_params.energy_perturbation)
configs = [wk.configuration.occupations for wk in ats]
if (at.configuration.occupations in configs) && !accept_same_config
@warn "Configuration already exists in the liveset"
accept = false
end

@info "iter: $(liveset.walkers[1].iter), acceptance rate: $rate, emax: $emax, is_accepted: $accept"
if accept
push!(ats, at)
Expand Down Expand Up @@ -217,8 +210,7 @@ This function takes a `liveset` of lattice gas walkers, `ns_params` containing t
"""
function nested_sampling_step!(liveset::LatticeGasWalkers,
ns_params::LatticeNestedSamplingParameters,
mc_routine::MCNewSample;
accept_same_config::Bool=true)
mc_routine::MCNewSample)
sort_by_energy!(liveset)
ats = liveset.walkers
h = liveset.hamiltonian
Expand All @@ -228,11 +220,7 @@ function nested_sampling_step!(liveset::LatticeGasWalkers,
to_walk = deepcopy(ats[1])

accept, at = MC_new_sample!(to_walk, h, emax; energy_perturb=ns_params.energy_perturbation)
# configs = [wk.configuration.occupations for wk in ats]
# if (at.configuration.occupations in configs) && !accept_same_config
# @warn "Configuration already exists in the liveset"
# accept = false
# end

@info "iter: $(liveset.walkers[1].iter), emax: $emax, is_accepted: $accept"
if accept
push!(ats, at)
Expand Down Expand Up @@ -316,7 +304,7 @@ function nested_sampling_loop!(liveset::AtomWalkers,
end

"""
nested_sampling_loop!(liveset::LatticeGasWalkers, ns_params::LatticeNestedSamplingParameters, n_steps::Int64, mc_routine::MCRoutine; args...)
nested_sampling_loop!(liveset::LatticeGasWalkers, ns_params::LatticeNestedSamplingParameters, n_steps::Int64, mc_routine::MCRoutine, save_strategy::DataSavingStrategy)

Perform a nested sampling loop on a lattice gas system for a given number of steps.

Expand All @@ -325,9 +313,7 @@ Perform a nested sampling loop on a lattice gas system for a given number of ste
- `ns_params::LatticeNestedSamplingParameters`: The parameters for nested sampling.
- `n_steps::Int64`: The number of steps to perform.
- `mc_routine::MCRoutine`: The Monte Carlo routine to use.

# Keyword Arguments
- `args...`: Additional arguments.
- `save_strategy::DataSavingStrategy`: The strategy for saving data.

# Returns
- `df`: A DataFrame containing the iteration number and maximum energy for each step.
Expand All @@ -338,15 +324,15 @@ function nested_sampling_loop!(liveset::LatticeGasWalkers,
ns_params::LatticeNestedSamplingParameters,
n_steps::Int64,
mc_routine::MCRoutine,
save_strategy::DataSavingStrategy;
accept_same_config::Bool=false)
save_strategy::DataSavingStrategy)

df = DataFrame(iter=Int[], emax=Float64[], config=Any[])
for i in 1:n_steps
# write_walker_every_n(liveset.walkers[1], i, save_strategy)

config = liveset.walkers[1].configuration.components

iter, emax, liveset, ns_params = nested_sampling_step!(liveset, ns_params, mc_routine; accept_same_config=accept_same_config)
iter, emax, liveset, ns_params = nested_sampling_step!(liveset, ns_params, mc_routine)
@debug "n_step $i, iter: $iter, emax: $emax"
if ns_params.fail_count >= ns_params.allowed_fail_count
@warn "Failed to accept MC move $(ns_params.allowed_fail_count) times in a row. Break!"
Expand Down
Loading