Skip to content

Commit

Permalink
add parameter thermal_ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
peter0627ustc committed Jul 2, 2024
1 parent 67fcd88 commit e4b91b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
block=16,
measure::Union{Nothing,Function}=nothing,
measurefreq::Int=1,
thermal_ratio::Int=100,
inplace::Bool=false,
adapt=true,
gamma=1.0,
Expand Down Expand Up @@ -35,6 +36,7 @@ Calculate the integrals, collect statistics, and return a `Result` struct contai
- For `solver = :vegas` or `:vegasmc`, the function signature should be `measure(var, obs, relative_weights, config)`. Here, `obs` is a vector of observable values for each component of the integrand and `relative_weights` are the weights calculated from the integrand multiplied by the probability of the corresponding variables.
- For `solver = :mcmc`, the signature should be `measure(idx, var, obs, relative_weight, config)`, where `obs` is the observable vector and `relative_weight` is the weight calculated from the `idx`-th integrand multiplied by the probability of the variables.
- `measurefreq`: How often the measurement function is called (default: `1`).
- `thermal_ratio` : Tha thermalization steps to total steps ratio for MCMC method
- `inplace`: Whether to use the inplace version of the integrand. Default is `false`, which is more convenient for integrand with a few return values but may cause type instability. Only useful for the :vegas and :vegasmc solver.
- `adapt`: Whether to adapt the grid and the reweight factor (default: `true`).
- `gamma`: Learning rate of the reweight factor after each iteration (default: `1.0`).
Expand Down Expand Up @@ -80,6 +82,7 @@ function integrate(integrand::Function;
ignore::Int=adapt ? 1 : 0, #ignore the first `ignore` iterations in average
measure::Union{Nothing,Function}=nothing,
measurefreq::Int=1,
thermal_ratio::Int = 100,
inplace::Bool=false, # whether to use the inplace version of the integrand
parallel::Symbol=:nothread, # :thread or :nothread
print=-1, printio=stdout, timer=[],
Expand Down Expand Up @@ -151,13 +154,13 @@ function integrate(integrand::Function;
Threads.@threads for _ in 1:block/MCUtility.mpi_nprocs()
_block!(configs, obsSum, obsSquaredSum, summedConfig, solver, progress,
integrand, nevalperblock, print, timer, debug,
measure, measurefreq, inplace, parallel)
measure, measurefreq, thermal_ratio, inplace, parallel)
end
else
for _ in 1:block/MCUtility.mpi_nprocs()
_block!(configs, obsSum, obsSquaredSum, summedConfig, solver, progress,
integrand, nevalperblock, print, timer, debug,
measure, measurefreq, inplace, parallel)
measure, measurefreq, thermal_ratio, inplace, parallel)
end
end
end
Expand Down Expand Up @@ -233,7 +236,7 @@ end
function _block!(configs, obsSum, obsSquaredSum, summedConfig,
solver, progress,
integrand::Function, nevalperblock, print, timer, debug::Bool,
measure::Union{Nothing,Function}, measurefreq, inplace, parallel)
measure::Union{Nothing,Function}, measurefreq, thermal_ratio, inplace, parallel)

rank = MCUtility.threadid(parallel)
# println(rank)
Expand All @@ -249,7 +252,7 @@ function _block!(configs, obsSum, obsSquaredSum, summedConfig,
measure=measure, measurefreq=measurefreq, inplace=inplace)
elseif solver == :mcmc
MCMC.montecarlo(config_n, integrand, nevalperblock, print, timer, debug;
measure=measure, measurefreq=measurefreq)
measure=measure, measurefreq=measurefreq, thermal_ratio = thermal_ratio)
else
error("Solver $solver is not supported!")
end
Expand Down
5 changes: 3 additions & 2 deletions src/mcmc/montecarlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ function montecarlo(config::Configuration{N,V,P,O,T}, integrand::Function, neval
verbose=0, timer=[], debug=false;
measurefreq::Int=1,
measure::Union{Nothing,Function}=nothing,
idx::Int=1 # the integral to start with
idx::Int=1, # the integral to start with
thermal_ratio::Int=100
) where {N,V,P,O,T}

@assert measurefreq > 0
Expand Down Expand Up @@ -140,7 +141,7 @@ function montecarlo(config::Configuration{N,V,P,O,T}, integrand::Function, neval
if debug && (isfinite(state.probability) == false)
@warn("integrand probability = $(state.probability) is not finite at step $(config.neval)")
end
if i % measurefreq == 0 && i >= neval / 100
if i % measurefreq == 0 && i >= neval / thermal_ratio

######## accumulate variable #################
if state.curr != config.norm
Expand Down

0 comments on commit e4b91b7

Please sign in to comment.