Skip to content

Commit

Permalink
Merge branch 'develop' into feature/occupationnumberfs
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimbrand committed Jan 12, 2024
2 parents 23d50c2 + c499ae2 commit 21d74a2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/DictVectors/DictVectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import ..Interfaces: deposit!, storage, StochasticStyle, default_style, freeze,
export deposit!, storage, walkernumber, dot_from_right
export DVec, InitiatorDVec, PDVec, PDWorkingMemory

export InitiatorRule, Initiator, SimpleInitiator, NonInitiator, CoherentInitiator

export AbstractProjector, NormProjector, Norm2Projector, UniformProjector, Norm1ProjectorPPop


Expand Down
27 changes: 18 additions & 9 deletions src/DictVectors/initiators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ Concrete implementations:
* [`CoherentInitiator`](@ref)
* [`NonInitiator`](@ref)
`InitiatorRule`s define how to store and retrieve data from associated [`AbstractInitiatorValue`](@ref)s.
When defining a new `InitiatorRule`, also define the following:
# Extended Help
`InitiatorRule`s define how to store and retrieve data from associated
[`AbstractInitiatorValue`](@ref)s. When defining a new `InitiatorRule`, also define the
following:
* [`initiator_valtype`](@ref)
* [`from_initiator_value`](@ref)
Expand Down Expand Up @@ -115,10 +118,11 @@ Convert `v` to an [`AbstractInitiatorValue`](@ref), taking the initiator rule an
to_initiator_value

"""
Initiator(threshold) <: InitiatorRule
Initiator(threshold = 1.0) <: InitiatorRule
Initiator rule to be passed to [`PDVec`](@ref) or [`InitiatorDVec`](@ref). An initiator is a
configuration `add` with a coefficient with magnitude `abs(v[add]) > threshold`. Rules:
configuration `add` with a coefficient with magnitude `abs(v[add]) > threshold`. The
`threshold` can be passed as a keyword argument. Rules:
* Initiators can spawn anywhere.
* Non-initiators can spawn to initiators.
Expand All @@ -128,6 +132,7 @@ See [`InitiatorRule`](@ref).
struct Initiator{T} <: InitiatorRule
threshold::T
end
Initiator(;threshold=1.0) = Initiator(threshold)
initiator_valtype(::Initiator, ::Type{V}) where {V} = InitiatorValue{V}

function from_initiator_value(::Initiator, v::InitiatorValue)
Expand Down Expand Up @@ -156,10 +161,11 @@ function to_initiator_value(rule::Initiator, add, val, parent)
end

"""
SimpleInitiator(threshold) <: InitiatorRule
SimpleInitiator(threshold = 1.0) <: InitiatorRule
Initiator rule to be passed to [`PDVec`](@ref) or [`InitiatorDVec`](@ref). An initiator is
a configuration `add` with a coefficient with magnitude `abs(v[add]) > threshold`. Rules:
a configuration `add` with a coefficient with magnitude `abs(v[add]) > threshold`. The
`threshold` can be passed as a keyword argument. Rules:
* Initiators can spawn anywhere.
* Non-initiators cannot spawn.
Expand All @@ -169,6 +175,7 @@ See [`InitiatorRule`](@ref).
struct SimpleInitiator{T} <: InitiatorRule
threshold::T
end
SimpleInitiator(;threshold=1.0) = SimpleInitiator(threshold)
initiator_valtype(::SimpleInitiator, ::Type{V}) where {V} = InitiatorValue{V}

function from_initiator_value(i::SimpleInitiator, v::InitiatorValue)
Expand All @@ -179,10 +186,11 @@ function to_initiator_value(rule::SimpleInitiator, add, val, parent)
end

"""
CoherentInitiator(threshold) <: InitiatorRule
CoherentInitiator(threshold = 1.0) <: InitiatorRule
Initiator rule to be passed to [`PDVec`](@ref) or [`InitiatorDVec`](@ref). An initiator is
a configuration `add` with a coefficient with magnitude `abs(v[add]) > threshold`. Rules:
a configuration `add` with a coefficient with magnitude `abs(v[add]) > threshold`. The
`threshold` can be passed as a keyword argument. Rules:
* Initiators can spawn anywhere.
* Non-initiators can spawn to initiators.
Expand All @@ -194,6 +202,7 @@ See [`InitiatorRule`](@ref).
struct CoherentInitiator{T} <: InitiatorRule
threshold::T
end
CoherentInitiator(;threshold=1.0) = CoherentInitiator(threshold)
initiator_valtype(::CoherentInitiator, ::Type{V}) where {V} = InitiatorValue{V}

function from_initiator_value(i::CoherentInitiator, v::InitiatorValue)
Expand All @@ -208,7 +217,7 @@ function to_initiator_value(rule::CoherentInitiator, add, val, parent)
end

"""
NonInitiator{V} <: InitiatorRule{V}
NonInitiator() <: InitiatorRule
Initiator rule that disables the approximation. This is the default setting for
[`PDVec`](@ref).
Expand Down
12 changes: 10 additions & 2 deletions src/Interfaces/stochasticstyles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ generalised vector `v` that determines how simulations are to proceed.
# Usage
Concrete `StochasticStyle`s can be used for the `style` keyword argument of
[`lomc!`](@ref Main.lomc!) and [`DVec`](@ref Main.DictVectors.DVec).
[`lomc!`](@ref Main.lomc!), [`DVec`](@ref Main.DictVectors.DVec) and
[`PDVec`](@ref Main.DictVectors.PDVec). The following styles are available:
# Interface
* [`IsStochasticInteger`](@ref Main.StochasticStyles.IsStochasticInteger)
* [`IsDeterministic`](@ref Main.StochasticStyles.IsDeterministic)
* [`IsStochasticWithThreshold`](@ref Main.StochasticStyles.IsStochasticWithThreshold)
* [`IsDynamicSemistochastic`](@ref Main.StochasticStyles.IsDynamicSemistochastic)
* [`StyleUnknown`](@ref Main.StochasticStyles.StyleUnknown)
# Extended Help
## Interface
When defining a new `StochasticStyle`, subtype it as `MyStyle<:StochasticStyle{T}` where `T`
is the concrete value type the style is designed to work with.
Expand Down
3 changes: 1 addition & 2 deletions src/StochasticStyles/StochasticStyles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ performing stochastic matrix-vector multiplication.
# Implemented stochastic styles:
* [`StochasticStyle`](@ref)
* [`StochasticStyle`](@ref): abstract type for stochastic styles
* [`IsStochasticInteger`](@ref)
* [`IsDeterministic`](@ref)
* [`IsStochasticWithThreshold`](@ref)
Expand All @@ -17,7 +17,6 @@ The offdiagonal spawning is defined in `spawning.jl` and is controlled by settin
The vector compression strategies are defined in `compression.jl` and are controlled by
setting a [`CompressionStrategy`](@ref).
"""
module StochasticStyles

Expand Down
31 changes: 21 additions & 10 deletions src/lomc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,19 +284,24 @@ end
default_starting_vector(
address=starting_address(hamiltonian);
style=IsStochasticInteger(),
initiator=NonInitiator(),
threading=nothing
)
Return a default starting vector for [`lomc!`](@ref). The default choice for the starting
vector is
```julia
v = PDVec(address => 10; style)
v = PDVec(address => 10; style, initiator)
```
if threading is available or
if threading is available, or otherwise
```julia
v = DVec(address => 10; style)
```
otherwise. See [`PDVec`](@ref), [`DVec`](@ref) and [`StochasticStyle`](@ref).
if `initiator == NonInitiator()`, and
```julia
v = InitiatorDVec(address => 10; style, initiator)
```
if not. See [`PDVec`](@ref), [`DVec`](@ref), [`InitiatorDVec`](@ref),
[`StochasticStyle`](@ref), and [`InitiatorRule`].
"""
function default_starting_vector(
hamiltonian::AbstractHamiltonian;
Expand All @@ -307,14 +312,17 @@ end
function default_starting_vector(address;
style=IsStochasticInteger(),
threading=nothing,
initiator=NonInitiator(),
)
if isnothing(threading)
threading = Threads.nthreads() > 1
end
if threading
v = PDVec(address => 10; style)
else
v = PDVec(address => 10; style, initiator)
elseif initiator isa NonInitiator
v = DVec(address => 10; style)
else
v = InitiatorDVec(address => 10; style, initiator)
end
return v
end
Expand All @@ -336,6 +344,8 @@ Alternatively, a `QMCState` can be passed in to continue a previous simulation.
* `address = starting_address(ham)` - set starting address for default `v` and `shift`.
* `style = IsStochasticInteger()` - set [`StochasticStyle`](@ref) for default `v`; unused
if `v` is specified.
* `initiator = NonInitiator()` - set [`InitiatorRule`](@ref) for default `v`; unused if `v`
is specified.
* `threading` - default is to use multithreading and
[MPI](https://juliaparallel.org/MPI.jl/latest/) if multiple threads are available. Set to
`true` to force [`PDVec`](@ref) for the starting vector, `false` for serial computation;
Expand Down Expand Up @@ -403,9 +413,9 @@ julia> metadata(df2, "hamiltonian") # some metadata is automatically added
be passed for merging with the report `df`.
The default choice for the starting vector is
`v = default_starting_vector(; address, style, threading)`.
See [`default_starting_vector`](@ref), [`PDVec`](@ref), [`DVec`](@ref), and
[`StochasticStyle`](@ref).
`v = default_starting_vector(; address, style, threading, initiator)`.
See [`default_starting_vector`](@ref), [`PDVec`](@ref), [`DVec`](@ref),
[`StochasticStyle`](@ref), and [`InitiatorRule`](@ref).
"""
function lomc!(ham, v; df=DataFrame(), name="lomc!", metadata=nothing, kwargs...)
state = QMCState(ham, v; kwargs...)
Expand All @@ -416,9 +426,10 @@ function lomc!(
style=IsStochasticInteger(),
threading=nothing,
address=starting_address(ham),
initiator=NonInitiator(),
kwargs...
)
v = default_starting_vector(address; style, threading)
v = default_starting_vector(address; style, threading, initiator)
return lomc!(ham, v; address, kwargs...) # pass address for setting the default shift
end
# For continuation, you can pass a QMCState and a DataFrame
Expand Down
3 changes: 3 additions & 0 deletions test/lomc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ using DataFrames

df, state = lomc!(H; laststep=100, style = IsDeterministic())
@test StochasticStyle(state.replicas[1].v) isa IsDeterministic

df, state = lomc!(H; laststep=1, threading=false, initiator=Initiator())
@test state.replicas[1].v isa InitiatorDVec
end

@testset "ShiftStrategy" begin
Expand Down

0 comments on commit 21d74a2

Please sign in to comment.