Skip to content

Commit

Permalink
document some internals (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen authored Nov 14, 2020
1 parent e9dd9c4 commit fb61791
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ makedocs(
"Examples" => "examples.md",
"Linear vs. Monte-Carlo uncertainty propagation" => "comparison.md",
"Performance tips" => "performance.md",
"Advanced usage" => "advanced_usage.md",
"API" => "api.md",
],
format = Documenter.HTML(prettyurls = haskey(ENV, "CI")),
Expand Down
10 changes: 10 additions & 0 deletions docs/src/advanced_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Advanced usage
Several non-exported functions that may facilitate working with structures that contain unertain parameters (struct-of-arrays, SoA) exist. These are not to be considered part of the API and are subject to breakage at any time, but may nevertheless be of use in special situations.

```@docs
MonteCarloMeasurements.mean_object
MonteCarloMeasurements.replace_particles
MonteCarloMeasurements.has_particles
MonteCarloMeasurements.build_mutable_container
MonteCarloMeasurements.build_container
```
24 changes: 11 additions & 13 deletions src/deconstruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Recursively visits all fields of `P` and replaces all instances of `StaticPartic
"""
function build_mutable_container(P)
has_mutable_particles(P) && (return P)
replace_particles(P, P->P isa AbstractParticles, P->Particles(Vector(P.particles)))
replace_particles(P, replacer=P->Particles(Vector(P.particles)))
end

"""
Expand All @@ -59,7 +59,7 @@ end
Replaces all fields of `P` that are particles with `Particles(1)`
"""
function make_scalar(P)
replace_particles(P, P->P isa AbstractParticles, P->Particles([mean(P)]))
replace_particles(P, replacer=P->Particles([mean(P)]))
end

"""
Expand All @@ -68,7 +68,7 @@ end
Replaces all fields of `P` that are `Particles(1)` with `Particles(N)`
"""
function restore_scalar(P, N)
replace_particles(P, P->P isa AbstractParticles, P->Particles(N))
replace_particles(P, replacer = P->Particles(N))
end

"""
Expand All @@ -77,42 +77,42 @@ Replaces all mutable particles inside `P` with `StaticParticles`.
"""
function make_static(P)
!has_mutable_particles(P) && (return P)
replace_particles(P, P->P isa AbstractParticles, P->StaticParticles(P.particles))
replace_particles(P, replacer = P->StaticParticles(P.particles))
end

"""
build_container(P)
Recursively visits all fields of `P` and replaces all instances of `AbstractParticles{T,N}` with `::T`
"""
build_container(P) = replace_particles(P,P->P isa AbstractParticles,P->P[1])
build_container(P) = replace_particles(P)

"""
mean_object(x)
Returns an object similar to `x`, but where all internal instances of `Particles` are replaced with their mean. The generalization of this function is `replace_particles`.
"""
mean_object(p::AbstractParticles) = mean(p)
mean_object(p::AbstractArray{<:AbstractParticles}) = mean.(p)
mean_object(P) = replace_particles(P,P->P isa AbstractParticles,P->mean(P))
mean_object(P) = replace_particles(P; replacer = P->mean(P))

"""
replace_particles(x,condition=P->P isa AbstractParticles,replacer = P->P[1])
replace_particles(x; condition=P->P isa AbstractParticles,replacer = P->P[1])
This function recursively scans through the structure `x`, every time a field that matches `condition` is found, `replacer` is called on that field and the result is used instead of `P`. See function `mean_object`, which uses this function to replace all instances of `Particles` with their mean.
"""
function replace_particles(P,condition::F1=P->P isa AbstractParticles,replacer::F2 = P->P[1]) where {F1,F2}
function replace_particles(P; condition::F1=P->P isa AbstractParticles,replacer::F2 = P->P[1]) where {F1,F2}
# @show typeof(P)
condition(P) && (return replacer(P))
has_particles(P) || (return P) # No need to carry on
if P isa AbstractArray # Special handling for arrays
return map(P->replace_particles(P,condition,replacer), P)
return map(P->replace_particles(P;condition,replacer), P)
end
P isa Complex && condition(real(P)) && (return complex(replacer(real(P)), replacer(imag(P))))
P isa Number && (return P)
fields = map(fieldnames(typeof(P))) do n
f = getfield(P,n)
has_particles(f) || (return f)
# @show typeof(f), n
replace_particles(f,condition,replacer)
replace_particles(f; condition,replacer)
end
T = nakedtypeof(P)

Expand Down Expand Up @@ -295,9 +295,7 @@ end
"""
struct Workspace{T1, T2, T3, T4, T5, T6}
DOCSTRING
#Arguments:
# Arguments:
- `simple_input`: Input object `f` will be called with, does not contain any particles
- `simple_result`: Simple output from `f` without particles
- `result`: Complete output of `f` including particles
Expand Down

0 comments on commit fb61791

Please sign in to comment.