From 25c57ee987d097e223b5894d36459e58aab1543d Mon Sep 17 00:00:00 2001 From: Joachim Brand Date: Wed, 8 May 2024 22:07:26 +1200 Subject: [PATCH] rename spectral_states to single_states, replica_states to spectral_states --- src/QMCSimulation.jl | 64 ++++++++++---------- src/lomc.jl | 6 +- src/qmc_states.jl | 30 ++++----- src/strategies_and_params/replicastrategy.jl | 8 +-- test/ProjectorMonteCarloProblem.jl | 20 +++--- test/allocation_tests.jl | 2 +- test/lomc.jl | 22 +++---- 7 files changed, 75 insertions(+), 77 deletions(-) diff --git a/src/QMCSimulation.jl b/src/QMCSimulation.jl index 5425b8abb..0983c1961 100644 --- a/src/QMCSimulation.jl +++ b/src/QMCSimulation.jl @@ -10,9 +10,9 @@ See also [`state_vectors`](@ref), [`single_states`](@ref), [`ProjectorMonteCarloProblem`](@ref), [`init`](@ref), [`solve!`](@ref). """ mutable struct QMCSimulation - qmc_problem::ProjectorMonteCarloProblem + problem::ProjectorMonteCarloProblem algorithm # currently only FCIQMC() is implemented - qmc_state::ReplicaState + state::ReplicaState report::Report modified::Bool aborted::Bool @@ -76,9 +76,9 @@ function QMCSimulation(problem::ProjectorMonteCarloProblem; copy_vectors=true) end wm = working_memory(first(vectors)) - # set up the replica_states + # set up the spectral_states if n_replicas == 1 - replica_states = (SpectralState( + spectral_states = (SpectralState( (SingleState( hamiltonian, only(vectors), @@ -92,7 +92,7 @@ function QMCSimulation(problem::ProjectorMonteCarloProblem; copy_vectors=true) spectral_strategy ),) else - replica_states = ntuple(n_replicas) do i + spectral_states = ntuple(n_replicas) do i v, sp = vectors[i], shift_parameters[i] rwm = (typeof(v) == typeof(first(vectors))) ? wm : working_memory(v) SpectralState( @@ -110,12 +110,12 @@ function QMCSimulation(problem::ProjectorMonteCarloProblem; copy_vectors=true) ) end end - @assert replica_states isa NTuple{n_replicas, <:SpectralState} + @assert spectral_states isa NTuple{n_replicas, <:SpectralState} # set up the initial state - qmc_state = ReplicaState( + state = ReplicaState( hamiltonian, # hamiltonian - replica_states, # replica_states + spectral_states, # spectral_states Ref(maxlength), # maxlength Ref(simulation_plan.starting_step), # step simulation_plan, # simulation_plan @@ -125,19 +125,19 @@ function QMCSimulation(problem::ProjectorMonteCarloProblem; copy_vectors=true) ) report = Report() report_metadata!(report, "algorithm", algorithm) - report_default_metadata!(report, qmc_state) + report_default_metadata!(report, state) report_metadata!(report, metadata) # add user metadata # Sanity checks. - check_transform(qmc_state.replica_strategy, qmc_state.hamiltonian) + check_transform(state.replica_strategy, state.hamiltonian) return QMCSimulation( - problem, algorithm, qmc_state, report, false, false, false, "", 0.0 + problem, algorithm, state, report, false, false, false, "", 0.0 ) end function Base.show(io::IO, sm::QMCSimulation) print(io, "QMCSimulation") - st = sm.qmc_state + st = sm.state print(io, " with ", num_replicas(st), " replica(s) and ") print(io, num_spectral_states(st), " spectral state(s).") print(io, "\n Algorithm: ", sm.algorithm) @@ -147,8 +147,8 @@ function Base.show(io::IO, sm::QMCSimulation) sm.message == "" || print(io, "\n message: ", sm.message) end -num_spectral_states(sm::QMCSimulation) = num_spectral_states(sm.qmc_state) -num_replicas(sm::QMCSimulation) = num_replicas(sm.qmc_state) +num_spectral_states(sm::QMCSimulation) = num_spectral_states(sm.state) +num_replicas(sm::QMCSimulation) = num_replicas(sm.state) function report_simulation_status_metadata!(report::Report, sm::QMCSimulation) @unpack modified, aborted, success, message, elapsed_time = sm @@ -167,7 +167,7 @@ function Base.iterate(sm::QMCSimulation, state=1) if state == 1 return DataFrame(sm), 2 elseif state == 2 - return sm.qmc_state, 3 + return sm.state, 3 else return nothing end @@ -177,8 +177,6 @@ end function Base.getproperty(sm::QMCSimulation, key::Symbol) if key == :df return DataFrame(sm) - elseif key == :state - return sm.qmc_state else return getfield(sm, key) end @@ -192,8 +190,8 @@ Tables.columnaccess(::Type{<:QMCSimulation}) = true Tables.columns(sm::QMCSimulation) = Tables.columns(sm.report.data) Tables.schema(sm::QMCSimulation) = Tables.schema(sm.report.data) -state_vectors(sim::QMCSimulation) = state_vectors(sim.qmc_state) -single_states(sim::QMCSimulation) = single_states(sim.qmc_state) +state_vectors(sim::QMCSimulation) = state_vectors(sim.state) +single_states(sim::QMCSimulation) = single_states(sim.state) # TODO: interface for reading results @@ -224,9 +222,9 @@ See also [`ProjectorMonteCarloProblem`](@ref), [`init`](@ref), [`solve!`](@ref), [`Rimu.QMCSimulation`](@ref). """ function CommonSolve.step!(sm::QMCSimulation) - @unpack qmc_state, report, algorithm = sm - @unpack replica_states, simulation_plan, step, reporting_strategy, - replica_strategy = qmc_state + @unpack state, report, algorithm = sm + @unpack spectral_states, simulation_plan, step, reporting_strategy, + replica_strategy = state if sm.aborted || sm.success @warn "Simulation is already aborted or finished." @@ -245,17 +243,17 @@ function CommonSolve.step!(sm::QMCSimulation) end proceed = true - # advance all replica_states - for replica in replica_states - proceed &= advance!(algorithm, report, qmc_state, replica) + # advance all spectral_states + for replica in spectral_states + proceed &= advance!(algorithm, report, state, replica) end sm.modified = true # report replica stats if step[] % reporting_interval(reporting_strategy) == 0 - replica_names, replica_values = replica_stats(replica_strategy, replica_states) + replica_names, replica_values = replica_stats(replica_strategy, spectral_states) report!(reporting_strategy, step[], report, replica_names, replica_values) - report_after_step!(reporting_strategy, step[], report, qmc_state) + report_after_step!(reporting_strategy, step[], report, state) ensure_correct_lengths(report) end @@ -298,21 +296,21 @@ function CommonSolve.solve!(sm::QMCSimulation; walltime = nothing, reset_time = false, ) - @unpack qmc_state = sm + @unpack state = sm reset_flags = reset_time # reset flags if resetting time if !isnothing(last_step) - sm.qmc_state = @set qmc_state.simulation_plan.last_step = last_step + sm.state = @set state.simulation_plan.last_step = last_step report_metadata!(sm.report, "laststep", last_step) reset_flags = true end if !isnothing(walltime) - sm.qmc_state = @set qmc_state.simulation_plan.walltime = walltime + sm.state = @set state.simulation_plan.walltime = walltime reset_flags = true end @unpack report = sm - @unpack simulation_plan, step, reporting_strategy = sm.qmc_state + @unpack simulation_plan, step, reporting_strategy = sm.state last_step = simulation_plan.last_step initial_step = step[] @@ -365,9 +363,9 @@ function lomc!(state::ReplicaState, df=DataFrame(); laststep=0, name="lomc!", me if !iszero(laststep) state = @set state.simulation_plan.last_step = laststep end - @unpack hamiltonian, replica_states, maxlength, step, simulation_plan, + @unpack hamiltonian, spectral_states, maxlength, step, simulation_plan, reporting_strategy, post_step_strategy, replica_strategy = state - first_replica = only(first(replica_states).spectral_states) # SingleState + first_replica = only(first(spectral_states).single_states) # SingleState @assert step[] ≥ simulation_plan.starting_step problem = ProjectorMonteCarloProblem(hamiltonian; start_at = first_replica.v, diff --git a/src/lomc.jl b/src/lomc.jl index c6e4d582f..cc31f4ece 100644 --- a/src/lomc.jl +++ b/src/lomc.jl @@ -230,7 +230,7 @@ function advance!(::FCIQMC, report, state::ReplicaState, replica::SingleState) end if len == 0 - if length(state.replica_states) > 1 + if length(state.spectral_states) > 1 @error "population in replica $(replica.id) is dead. Aborting." else @error "population is dead. Aborting." @@ -238,7 +238,7 @@ function advance!(::FCIQMC, report, state::ReplicaState, replica::SingleState) return false end if len > state.maxlength[] - if length(state.replica_states) > 1 + if length(state.spectral_states) > 1 @error "`maxlength` reached in replica $(replica.id). Aborting." else @error "`maxlength` reached. Aborting." @@ -249,6 +249,6 @@ function advance!(::FCIQMC, report, state::ReplicaState, replica::SingleState) end function advance!(algorithm, report, state::ReplicaState, replica::SpectralState{1}) - return advance!(algorithm, report, state, only(replica.spectral_states)) + return advance!(algorithm, report, state, only(replica.single_states)) end # TODO: add advance! for SpectralState{N} where N > 1 diff --git a/src/qmc_states.jl b/src/qmc_states.jl index 2254d573b..7fb1ce4d4 100644 --- a/src/qmc_states.jl +++ b/src/qmc_states.jl @@ -76,7 +76,7 @@ struct SpectralState{ NS<:NTuple{N,SingleState}, SS<:SpectralStrategy{N} } - spectral_states::NS # Tuple of SingleState + single_states::NS # Tuple of SingleState spectral_strategy::SS # SpectralStrategy id::String # id is appended to column names end @@ -89,17 +89,17 @@ function Base.show(io::IO, s::SpectralState) print(io, "SpectralState") print(io, " with ", num_spectral_states(s), " spectral states") print(io, "\n spectral_strategy: ", s.spectral_strategy) - for (i, r) in enumerate(s.spectral_states) + for (i, r) in enumerate(s.single_states) print(io, "\n $i: ", r) end end function state_vectors(state::SpectralState) - return mapreduce(state_vectors, vcat, state.spectral_states) + return mapreduce(state_vectors, vcat, state.single_states) end function single_states(state::SpectralState) - return mapreduce(single_states, vcat, state.spectral_states) + return mapreduce(single_states, vcat, state.single_states) end """ @@ -133,7 +133,7 @@ struct ReplicaState{ PS<:NTuple{<:Any,PostStepStrategy}, } hamiltonian::H - replica_states::R + spectral_states::R maxlength::Ref{Int} step::Ref{Int} simulation_plan::SimulationPlan @@ -223,14 +223,14 @@ function ReplicaState( post_step_strategy = (post_step_strategy,) end - # set up spectral_states + # set up single_states n_spectral_states = num_spectral_states(spectral_strategy) n_spectral_states == 1 || throw(ArgumentError("Only one spectral state is supported.")) - # Set up replica_states + # Set up spectral_states nreplicas = num_replicas(replica_strategy) if nreplicas > 1 - replica_states = ntuple(nreplicas) do i + spectral_states = ntuple(nreplicas) do i SpectralState( (SingleState( hamiltonian, @@ -246,14 +246,14 @@ function ReplicaState( ) end else - replica_states = (SpectralState( + spectral_states = (SpectralState( (SingleState(hamiltonian, v, wm, s_strat, τ_strat, shift, dτ),), spectral_strategy ),) end return ReplicaState( - hamiltonian, replica_states, Ref(Int(maxlength)), + hamiltonian, spectral_states, Ref(Int(maxlength)), Ref(simulation_plan.starting_step), # step simulation_plan, # Ref(Int(laststep)), @@ -271,7 +271,7 @@ function Base.show(io::IO, st::ReplicaState) print(io, "\n H: ", st.hamiltonian) print(io, "\n step: ", st.step[], " / ", st.simulation_plan.last_step) print(io, "\n replicas: ") - for (i, r) in enumerate(st.replica_states) + for (i, r) in enumerate(st.spectral_states) print(io, "\n $i: ", r) end end @@ -288,9 +288,9 @@ See also [`single_states`](@ref), [`SingleState`](@ref), [`ReplicaState`](@ref), @inline function state_vectors(state::ReplicaState{N,S}) where {N,S} # Annoyingly this function is allocating if N > 1 return SMatrix{S,N}( - state.replica_states[fld1(i,S)].spectral_states[mod1(i,S)].v for i in 1:N*S + state.spectral_states[fld1(i,S)].single_states[mod1(i,S)].v for i in 1:N*S ) - # return SMatrix{S,N}(mapreduce(state_vectors, hcat, state.replica_states)) + # return SMatrix{S,N}(mapreduce(state_vectors, hcat, state.spectral_states)) end """ @@ -303,13 +303,13 @@ See also [`state_vectors`](@ref), [`SingleState`](@ref), [`ReplicaState`](@ref), [`SpectralState`](@ref), [`QMCSimulation`](@ref). """ function single_states(state::ReplicaState{N,S}) where {N,S} - return SMatrix{S,N}(mapreduce(single_states, hcat, state.replica_states)) + return SMatrix{S,N}(mapreduce(single_states, hcat, state.spectral_states)) end function report_default_metadata!(report::Report, state::ReplicaState) report_metadata!(report, "Rimu.PACKAGE_VERSION", Rimu.PACKAGE_VERSION) # add metadata from state - replica = state.replica_states[1].spectral_states[1] + replica = state.spectral_states[1].single_states[1] shift_parameters = replica.shift_parameters report_metadata!(report, "laststep", state.simulation_plan.last_step) report_metadata!(report, "num_replicas", num_replicas(state)) diff --git a/src/strategies_and_params/replicastrategy.jl b/src/strategies_and_params/replicastrategy.jl index 25414d712..a652a4dd9 100644 --- a/src/strategies_and_params/replicastrategy.jl +++ b/src/strategies_and_params/replicastrategy.jl @@ -29,7 +29,7 @@ Return the number of replicas used in the simulation. num_replicas(::ReplicaStrategy{N}) where {N} = N """ - replica_stats(RS::ReplicaStrategy{N}, replica_states::NTuple{N,SingleState}) -> (names, values) + replica_stats(RS::ReplicaStrategy{N}, spectral_states::NTuple{N,SingleState}) -> (names, values) Return the names and values of statistics related to `N` replica states consistent with the [`ReplicaStrategy`](@ref) `RS`. `names` @@ -129,11 +129,11 @@ function AllOverlaps(num_replicas=2; operator=nothing, transform=nothing, vecnor end @deprecate AllOverlaps(num_replicas, operator) AllOverlaps(num_replicas; operator) -function replica_stats(rs::AllOverlaps{N,<:Any,<:Any,B}, replica_states::NTuple{N}) where {N,B} +function replica_stats(rs::AllOverlaps{N,<:Any,<:Any,B}, spectral_states::NTuple{N}) where {N,B} # Not using broadcasting because it wasn't inferred properly. # For now implement this assuming only a single spectral state; generalise later - vecs = ntuple(i -> only(replica_states[i].spectral_states).v, Val(N)) - wms = ntuple(i -> only(replica_states[i].spectral_states).wm, Val(N)) + vecs = ntuple(i -> only(spectral_states[i].single_states).v, Val(N)) + wms = ntuple(i -> only(spectral_states[i].single_states).wm, Val(N)) return all_overlaps(rs.operators, vecs, wms, Val(B)) end diff --git a/test/ProjectorMonteCarloProblem.jl b/test/ProjectorMonteCarloProblem.jl index 5f458d2a6..aecab4d6b 100644 --- a/test/ProjectorMonteCarloProblem.jl +++ b/test/ProjectorMonteCarloProblem.jl @@ -18,7 +18,7 @@ using OrderedCollections: freeze @test Rimu.num_replicas(p) == 1 simulation = init(p) - @test simulation.qmc_state.hamiltonian == h + @test simulation.state.hamiltonian == h @test only(state_vectors(simulation)) isa PDVec ps = ProjectorMonteCarloProblem(h; initial_shift_parameters=sp, threading=false) @@ -61,7 +61,7 @@ using OrderedCollections: freeze sm = init(p; copy_vectors=false) @test state_vectors(sm)[1] === dv1 @test state_vectors(sm)[2] === dv2 - @test_throws BoundsError sm.qmc_state.replica_states[3] + @test_throws BoundsError sm.state.spectral_states[3] end @testset "QMCSimulation" begin @@ -121,30 +121,30 @@ using Rimu: num_replicas, num_spectral_states @test step!(sm) isa Rimu.QMCSimulation @test sm.modified == true @test is_finalized(sm.report) == false - @test size(DataFrame(sm))[1] == sm.qmc_state.step[] + @test size(DataFrame(sm))[1] == sm.state.step[] @test solve!(sm) isa Rimu.QMCSimulation @test sm.modified == true @test sm.success == true @test is_finalized(sm.report) == true - @test size(DataFrame(sm))[1] == sm.qmc_state.step[] - @test num_replicas(sm) == num_replicas(p) == num_replicas(sm.qmc_state) - @test num_spectral_states(sm) == num_spectral_states(p) == num_spectral_states(sm.qmc_state) + @test size(DataFrame(sm))[1] == sm.state.step[] + @test num_replicas(sm) == num_replicas(p) == num_replicas(sm.state) + @test num_spectral_states(sm) == num_spectral_states(p) == num_spectral_states(sm.state) @test size(state_vectors(sm)) == (num_replicas(sm), num_spectral_states(sm)) @test size(single_states(sm)) == (num_replicas(sm), num_spectral_states(sm)) df, state = sm # deconstruction for backward compatibility @test df == DataFrame(sm) == sm.df - @test state == sm.qmc_state == sm.state + @test state == sm.state # Tables.jl interface @test Tables.istable(sm) @test map(NamedTuple, Tables.rows(sm)) == map(NamedTuple, Tables.rows(df)) # continue simulation - @test sm.qmc_state.step[] == 100 + @test sm.state.step[] == 100 solve!(sm; last_step=200) - @test sm.qmc_state.step[] == 200 + @test sm.state.step[] == 200 @test sm.success == true == parse(Bool, (Rimu.get_metadata(sm.report, "success"))) # time out @@ -158,5 +158,5 @@ using Rimu: num_replicas, num_spectral_states sm2 = solve!(sm; walltime=1.0) @test sm2 === sm @test sm.success == true - @test sm.qmc_state.step[] == 500 + @test sm.state.step[] == 500 end diff --git a/test/allocation_tests.jl b/test/allocation_tests.jl index 4a50454d9..902f793a3 100644 --- a/test/allocation_tests.jl +++ b/test/allocation_tests.jl @@ -106,7 +106,7 @@ end laststep=1, ) - r = only(only(st.replica_states).spectral_states) + r = only(only(st.spectral_states).single_states) # Warmup for step! apply_operator_wrap!(r) diff --git a/test/lomc.jl b/test/lomc.jl index facfee65e..ddca33c92 100644 --- a/test/lomc.jl +++ b/test/lomc.jl @@ -29,15 +29,15 @@ Random.seed!(1234) v = copy(dv) wm = copy(dv) df, state = lomc!(H, v; wm, laststep=9) - @test state.replica_states[1].spectral_states[1].wm === wm # after number of steps divisible by 3 + @test state.spectral_states[1].single_states[1].wm === wm # after number of steps divisible by 3 @test state_vectors(state)[1] === v - @test state.replica_states[1].spectral_states[1].pv !== v - @test state.replica_states[1].spectral_states[1].pv !== wm + @test state.spectral_states[1].single_states[1].pv !== v + @test state.spectral_states[1].single_states[1].pv !== wm # @set state.simulation_plan.last_step = 10 df = lomc!(state, df, laststep=10).df - @test state.replica_states[1].spectral_states[1].v === wm - @test state.replica_states[1].spectral_states[1].pv === v + @test state.spectral_states[1].single_states[1].v === wm + @test state.spectral_states[1].single_states[1].pv === v @test size(df, 1) == 10 @test state.step[] == 10 @@ -58,8 +58,8 @@ Random.seed!(1234) H = HubbardReal1D(address; u=0.1) dv = DVec(address => 1; style=IsStochasticInteger()) df, state = @test_logs (:warn, Regex("(Simulation)")) lomc!(H, dv; laststep=0, shift=23.1, dτ=0.002) - @test state.replica_states[1].spectral_states[1].shift_parameters.time_step == 0.002 - @test state.replica_states[1].spectral_states[1].shift_parameters.shift == 23.1 + @test state.spectral_states[1].single_states[1].shift_parameters.time_step == 0.002 + @test state.spectral_states[1].single_states[1].shift_parameters.shift == 23.1 @test state.replica_strategy == NoStats{1}() # uses getfield method end @testset "default_starting_vector" begin @@ -96,7 +96,7 @@ Random.seed!(1234) @test median(walkers) ≈ 1000 rtol=0.1 _, state = @test_logs (:warn, Regex("(Simulation)")) lomc!(H, copy(dv); targetwalkers=500, laststep=0) - @test state.replica_states[1].spectral_states[1].s_strat.targetwalkers == 500 + @test state.spectral_states[1].single_states[1].s_strat.targetwalkers == 500 end @testset "Replicas" begin @@ -107,13 +107,13 @@ Random.seed!(1234) dv = DVec(address => 1, style=IsDynamicSemistochastic()) df, state = lomc!(H, dv; replica_strategy=NoStats(1)) @test state.replica_strategy == NoStats(1) - @test length(state.replica_states) == 1 + @test length(state.spectral_states) == 1 @test "shift" ∈ names(df) @test "shift_1" ∉ names(df) df, state = lomc!(H, dv; replica_strategy=NoStats(3)) @test state.replica_strategy == NoStats(3) - @test length(state.replica_states) == 3 + @test length(state.spectral_states) == 3 @test df.shift_1 ≠ df.shift_2 && df.shift_2 ≠ df.shift_3 @test "shift_4" ∉ names(df) @@ -507,7 +507,7 @@ Random.seed!(1234) @test all(≈(3), sum.(df.single_particle_density[2:2:end])) @test df.single_particle_density[end] == single_particle_density( - st.replica_states[1].spectral_states[1].v + st.spectral_states[1].single_states[1].v ) for address in (