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.AbstractParticleCollectionType
abstract 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 the Dict
  • Base.empty: returns an identical object, but with an empty Dict
  • Base.:(==)
  • redshift: implement this if the redshift of the particle collection is non-zero
  • Base.propertynames: implement this if there are additional struct fields
  • Base.show(io, mime, pc)
source
CosmoParticles.ParticleCollectionType
struct ParticleCollection{AP<:AbstractParticles} <: AbstractParticleCollection{AP}
    particles::Dict{Symbol,AP}
end

Basic particle collection at redshift 0.

source
CosmoParticles.ParticleCollectionMethod
ParticleCollection(::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.

source
CosmoParticles.ParticleCollectionMethod
ParticleCollection(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))
source
CosmoParticles.RedshiftParticleCollectionMethod
RedshiftParticleCollection([::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.

source
CosmoParticles.RedshiftParticleCollectionMethod
RedshiftParticleCollection(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))
source
CosmoParticles.redshiftFunction
redshift(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.

source
CosmoParticles.get_particlesFunction
get_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.

source
CosmoParticles.show_propertiesMethod
CosmoParticles.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.

source

All Particles

CosmoParticles.AllParticlesType
AllParticles{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 Arrays. AllParticles cannot be sorted, filtered, or copied. In-place modifications are possible, such as rotate!, translate!, or to_comoving.

Creation

pc::ParticleCollection # e.g. with particle types :dm, :stars, :gas

ap = AllParticles(pc)
ap = pc.all # equivalent to the above

ap = AllParticles(pc, [:dm, :stars]) # just include certain particle types
In-place modifications

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.

Modifying the underlying particle collection

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).

Note that the simplified access with underscore notation for matrices is not available for AllParticles.

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)

julia> p = Particles(ap, [:id, :mass]) # only get certain properties
all: 150 Particles
 id mass
source
Base.copy!Function
Base.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 Symbols. The dst cannot be of type AllParticles.

source