Particle Collections
A set of different particles are represented by subtypes of AbstractParticleCollection
. These can have a cosmological redshift attributed to them. In the context of cosmological simulations, this applies to collections of DM, gas, and star particles.
For an example implementation of a concrete subtype of AbstractParticleCollection
, see the source codes of ParticleCollection
or RedshiftParticleCollection
.
CosmoParticles.AbstractParticleCollection
— Typeabstract type AbstractParticleCollection{AP} end
Abstract supertype for storing data of multiple particle types.
The particles are expected to be stored in a Dict{Symbol,<:AbstractParticles}
.
The inbuilt functionality of AbstractParticleCollection
includes accessing and setting properties via the following syntax:
P<:AbstractParticles
pc::<:AbstractParticleCollection{P}
p::<:P
pc.dm = p
pc[:dm] === p
In addition, the following syntax is equivalent to creating an AllParticles
object: pc.all
. The syntax pc[:all]
is not valid, however.
If the struct has additional fields, these can also be accessed by pc.field
, but not by pc[:field]
. The latter syntax can only be used to access the particles.
Methods
The methods Base.keys
, Base.values
, Base.haskey
, Base.empty
, Base.empty!
, Base.isempty
, and Base.copy!
are forwarded to the particle Dict
.
Concrete types of AbstractParticleCollection
should have the following methods implemented (also see the implementation of ParticleCollection
):
Base.copy
: returns new object containing a copy of theDict
Base.empty
: returns an identical object, but with an emptyDict
Base.:(==)
redshift
: implement this if the redshift of the particle collection is non-zeroBase.propertynames
: implement this if there are additional struct fieldsBase.show(io, mime, pc)
CosmoParticles.ParticleCollection
— Typestruct ParticleCollection{AP<:AbstractParticles} <: AbstractParticleCollection{AP}
particles::Dict{Symbol,AP}
end
Basic particle collection at redshift 0.
CosmoParticles.ParticleCollection
— MethodParticleCollection(::Type{AP}=Particles) where {AP<:AbstractParticles}
Create an empty particle collection of the passed type.
By default, the particle collection is created for the [Particles
] type.
CosmoParticles.ParticleCollection
— MethodParticleCollection(pairs::Pair{Symbol,AP}...) where {AP<:AbstractParticles}
Create a particle collection from pairs of Symbols and particles.
Example
ParticleCollection(:dm => Particles(:dm), :gas => Particles(:gas))
CosmoParticles.ParticleCollection
— MethodParticleCollection(p::Particles...)
Create a particle collection from multiple Particles
.
The Dict
keys are taken from the Particles
type.
Example
ParticleCollection(Particles(:dm), Particles(:gas))
CosmoParticles.RedshiftParticleCollection
— Typestruct RedshiftParticleCollection{AP<:AbstractParticles,T<:Real} <: AbstractParticleCollection{AP}
z::T
particles::Dict{Symbol,AP}
end
Particle collection at a given redshift.
CosmoParticles.RedshiftParticleCollection
— MethodRedshiftParticleCollection([::Type{AP}=Particles,] z::T) where {AP<:AbstractParticles,T<:Real}
Create an empty particle collection of the passed type at redshift z
.
By default, the particle collection is created for the [Particles
] type.
CosmoParticles.RedshiftParticleCollection
— MethodRedshiftParticleCollection(z::T, pairs::Pair{Symbol,AP}...) where {AP<:AbstractParticles,T<:Real}
Create a particle collection at redshift z
from pairs of Symbols and particles.
Example
RedshiftParticleCollection(0.5, :dm => Particles(:dm), :gas => Particles(:gas))
CosmoParticles.RedshiftParticleCollection
— MethodRedshiftParticleCollection(z::Real, p::Particles...)
Create a particle collection at redshift z
from multiple Particles
.
The Dict
keys are taken from the Particles
type.
Example
RedshiftParticleCollection(0.5, Particles(:dm), Particles(:gas))
CosmoParticles.redshift
— Functionredshift(pc::AbstractParticleCollection) = 0
Returns the redshift at which the particles are located.
By default, 0 is returned. This method should be overridden for concrete types of AbstractParticleCollection
for which the redshift differs.
CosmoParticles.get_particles
— Functionget_particles(pc::AbstractParticleCollection)
Return the particles Dict
belonging to the particle collection.
This returns pc.particles
by default if not overridden.
This is not exported.
CosmoParticles.show_properties
— MethodCosmoParticles.show_properties(io::IO, mime, p::AbstractParticleCollection)
Prints the properties of the particles in the collection.
Should be used internally when overriding Base.show
for concrete implementations of AbstractParticleCollection
.
This is not exported.
All Particles
CosmoParticles.AllParticles
— TypeAllParticles{APC} <: AbstractParticles where {APC<:AbstractParticleCollection}
Combines the particles of different types from a particle collection lazily.
The property arrays of the collection's particles are concatenated lazily with LazyArrays.jl. Scalar properties (e.g. mass for equal-mass particles) are repeated according to the particle number of the given particle type with FillArrays.jl. Properties that only exist for one of the particle types are repeated as missing
values in the same way as scalar properties.
Usage
The individual properties of AllParticles
can be converted as regular arrays with Array(ap.pos)
and all or selected particle properties can be converted to a Particles
object with Particles(ap)
, where all lazy arrays are materialized as Array
s. AllParticles
cannot be sorted, filtered, or copied. In-place modifications are possible, such as rotate!
, translate!
, or to_comoving
.
Modifying an AllParticles
object in-place results in the modification of the underlying particle collection! To prevent bugs or unexpected behavior, it is advised to modify the particle collection instead and then obtain the object through pc.all
or AllParticles(pc)
(these are equivalent).
The lazy concatenation occurs only when the property is needed and is only performed once. Note that this means that calling pc.all.prop
multiple times results in the given property being lazily concatenated every time. As long as the property array pointers of the particle collection are not modified (e.g. by sort!
or filter!
), it is possible to reuse an object ap = pc.all
without the need of regenerating the concatenated arrays.
After modifying the property array pointers of a particle collection (e.g. by sort!
or filter!
), any already created AllParticles
objects from that collection may have lazy arrays pointing to the old, non-modified arrays. When this happens, create a new object through pc.all
or AllParticles(pc)
.
Example
julia> pc::ParticleCollection
ParticleCollection
dm: 100 Particles
id mass pos
gas: 50 Particles
id mass pos temp
julia> ap = AllParticles(pc) # equivalent to ap = pc.all
all: 150 Particles
id mass pos temp
julia> p = Particles(ap) # p is detached from pc and therefore modifiable
all: 150 Particles
id mass pos temp
julia> sort!(p, :id)
CosmoParticles.particle_collection
— Functionparticle_collection(p::AllParticles)
Returns the underlying particle collection.
This is not exported.
Base.copy!
— FunctionBase.copy!(dst::AbstractParticles, src::AllParticles[, props])
Copies the materialized particle properties to another particle object.
To only copy certain properties, props
can be passed as a vector of Symbol
s. The dst
cannot be of type AllParticles
.