pNbody Main Class

class pNbody.main.Nbody(p_name=None, pos=None, vel=None, mass=None, num=None, tpe=None, ftype='default', status='old', byteorder='little', pio='no', local=False, unitsfile=None, skipped_io_blocks=[], ptypes=None, verbose=0, arrays=None, **kws)

Bases: pNbody._transformations.Nbody, pNbody._display_mixin._NbodyDisplayMixin, pNbody._sph_mixin._NbodySphMixin, pNbody._centering_mixin._CenteringNbodyMixin, pNbody._getvelocities_mixin._NbodyGetVelocitiesMixin, pNbody._histo_mixin._NbodyHistoMixin, pNbody._integration_mixin._NbodyIntegrationMixin, pNbody._unitless_quantities_mixin._NbodyUnitlessQuantitiesMixin, pNbody._unitless_quantities_deprecated_mixin._NbodyUnitlessQuantitiesDeprecatedMixin, pNbody._coordinate_transformations_mixin._NbodyCoordinateTransformationsMixin, pNbody._geometric_transformations_mixin._NbodyGeometricTransformationsMixin

Represent an N-body system and manage its numerical data.

This class serves as the primary interface for loading, creating, and manipulating N-body snapshots from various simulation formats.

Parameters
  • p_name (str or list of str, optional) – Name of the file(s) to load. Use a list for multiple files.

  • pos (array_like, optional) – Particle positions as a (3, N) or (N, 3) array.

  • vel (array_like, optional) – Particle velocities as a (3, N) or (N, 3) array.

  • mass (array_like, optional) – Particle masses as a (N,) array.

  • num (array_like, optional) – Unique particle identifiers (IDs) as a (N,) array.

  • tpe (array_like, optional) – Particle type classifications as a (N,) array.

  • ftype ({'swift', 'gh5', 'gadget'}, optional) – The input file format.

  • status ({'old', 'new'}, default 'old') – Object status. ‘old’ opens an existing file; ‘new’ initializes an empty object.

  • byteorder ({'little', 'big'}, optional) – Endianness of the data.

  • pio ({'yes', 'no'}, default 'no') – Enable or disable parallel I/O.

  • local (bool, default True) – Whether the object is local (True) or globally distributed (False). Note: Parallel distribution is not yet implemented.

  • unitsfile (str, optional) – Path to a file defining the physical unit system.

  • verbose ({0, 1, 2}, default 1) – Verbosity level: 0 (None), 1 (General), 2 (Detailed).

  • ptypes (list of int, optional) – Specific particle types to read from the file.

  • arrays (list of str, optional) – Specific data arrays to read from the file.

p_name

The file path(s) associated with the object.

Type

list of str

pos

Coordinates of the particles.

Type

numpy.ndarray

vel

Velocities of the particles.

Type

numpy.ndarray

mass

Masses of the individual particles.

Type

numpy.ndarray

num

Particle IDs.

Type

numpy.ndarray

tpe

Particle types.

Type

numpy.ndarray

ftype

The simulation format type.

Type

str

status

Initialization status (‘old’ or ‘new’).

Type

str

nbody

Number of particles stored in the local memory.

Type

int

nbody_tot

Total number of particles across all MPI ranks.

Type

int

mass_tot

Total mass of the system.

Type

float

npart

Local count of particles per type.

Type

numpy.ndarray

npart_tot

Total count of particles per type across all ranks.

Type

numpy.ndarray

spec_vars

Format-specific scalar variables (metadata).

Type

dict

spec_vect

Format-specific vector arrays.

Type

dict

See also

pNbody._centering_mixin._CenteringNbodyMixin

A mixin handling all grouping all centering operations.

ExchangeParticles()

Exchange particles betwee procs, using peano-hilbert decomposition computed in ptree

SendAllToAll()

Send all particles to all nodes at the end of the day, all nodes have the same nbody object

append(solf, do_not_sort=False, do_init_num=True, do_not_change_num=False)

Add to the current N-body object, particles form the N-body object “new”.

solf : Nbody object

check_arrays()

check if the array contains special values like NaN or Inf

check_ftype()

check the file format

clone_empty(local=False)

create a new empty object using the same class

debug(msg)

Print debug message

by default, the verbosity is 10, the level 2, the color blue.

dump(aname=None)

Dump the array aname to a file

dumpToHDF5(aname=None)

Dump the array aname to a file

error(msg, verbosity=0)

Print error message and trigger and Error exception.

by default, the verbosity is 0, the level 2, the color red.

extend_format()

Extend format with format file (e.g. gh5) and extensions (config/extension)

find_format(default)

Test the default format and if not good, test a few. :returns: format name

find_vars()

This function return a list of variables defined in the current object

gather_mass()

Gather in a unique array all mass of all nodes.

gather_num()

Gather in a unique array all num of all nodes.

gather_pos()

Gather in a unique array all positions of all nodes.

gather_vec(vec)

Gather in a unique array all vectors vec of all nodes.

gather_vel()

Gather in a unique array all velocites of all nodes.

get_arrays_memory_footprint()

Return the memory footprint of each array

get_default_arrays_props()

return default arrays properties for the class

get_default_spec_array()

return specific array default values for the class

get_default_spec_vars()

return specific variables default values for the class

get_excluded_extension()

Return a list of file to avoid when extending the default class.

get_ext_methods()

get the list of extensions methods together with their origin

get_format_file()

return the format file

get_ftype(ftype='swift')

get the current used format

get_list_of_arrays()

Return the list of numpy vectors of size nbody.

get_list_of_external_methods()

Print the list of extensions methods including their origin

This is an alias of get_ext_methods

get_list_of_methods(display=False)

Return the list of instance methods (functions).

get_list_of_properties()

Return the list of class properties.

Identify attributes defined with the @property decorator by inspecting the class hierarchy. This avoids executing the property logic, preventing crashes if internal data (like self.pos) is not yet loaded.

Returns

A list of strings containing the names of all properties.

Return type

list

get_list_of_vars()

Get the list of vars that are linked to the model

get_mxntpe()

Return the max number of type for this format

get_nbody()

Return the local number of particles.

get_nbody_tot()

Return the total number of particles.

get_npart()

Return the local number of particles of each types, based on the variable tpe

get_npart_all(npart_tot, NTask)

From npart_tot, the total number of particles per type, return npart_per_proc, an array where each element corresponds to the value of npart of each process.

get_npart_and_npart_all(npart)

From npart (usually read for the header of a file), compute :

npart : number of particles in each type npart_tot : total number of particles in each type npart_all : npart for each process.

get_npart_tot()

Return the total number of particles of each types.

get_ns()

Return in an array the number of particles of each node.

get_ntype()

return the number of paticles types

get_num()

Compute the num variable in order to be consistent with particles types

get_read_fcts()

returns the functions needed to read a snapshot file.

get_verbosity()

return the current verbosity level

get_write_fcts()

returns the functions needed to write a snapshot file.

getindex(num)

Return an array of index of a particle from its specific number id. The array is empty if no particle corresponds to the specific number id.

num : Id of the particle

has_array(name)

Return true if the object pNbody has an array called self.name

has_var(name)

Return true if the object pNbody has a variable called self.name

import_check_ftype(filename)

Import check_spec_ftype from the format file.

info()

Write info

init()

Initialize normal and specific class variables

init_units()

This function is responsible for the units initialization.

It will create :

self.unitsparameters

that contains parameters like
  • the hydrogen mass fraction,

  • the metalicity ionisation flag

  • the adiabatic index

and

self.localsystem_of_units

a UnitSystem object that really defines the system of units in the Nbody object. It uses the values :

UnitLength_in_cm UnitMass_in_g UnitVelocity_in_cm_per_s

All physical values computed in pNbody should use self.localsystem_of_units to be converted in other units. self.unitsparameters is usefull if other parameters needs to be known, like the adiabatic index, etc.

load(name=None, ptypes=None, force=False)

Load array from the file. This function relays on the info stored in self._AM.arrays_props Here, we assume that npart is known and correct

name : the array name ptypes : the list of particles types to read force : for reloading the array

loadFromHDF5(name=None, ptypes=None, force=False)

Load array from the hdf5 file. This function relays on the info stored self.arrays_props Here, we assume that npart is known and correct

name : the array name ptypes : the list of particles types to read force : for reloading the array

make_default_vars_global()

Make specific variables global

memory_info()

Write info on memory size of the current object (only counting arrays size)

message(msg, verbosity=1, color=None)

Print message

by default, the verbosity is 1, the level 2, the color black.

nodes_info()

Write info on nodes

object_info()

Write class(object) info

open_and_read(name, readfct)

open and read file name

name : name of the input readfct : function used to read the file

open_and_write(name, writefct)

Open and write file

name : name of the output writefct : function used to write the file

print_filenames()

Print files names

read()

Read the particle file(s)

read_arrays(ptypes=None)

Read arrays that must be loaded.

read_num(name)

Read a num file

name : name of the input

redistribute()

This function redistribute particles amoung all nodes in order to have a similar number of particles per nodes

reduc(n, mass=False)

Return an N-body object that contain a fraction 1/n of particles.

n : inverse of the fraction of particule to be returned

rename(p_name=None)

Rename the files

p_name : new name(s)

selectc(c, local=False)

Return an N-body object that contain only particles where the corresponding value in c is not zero. c is a nx1 Nbody array.

c : the condition vector local : local selection (True) or global selection (False)

selecti(i, local=False)

Return an N-body object that contain only particles having their index (not id) in i.

i : vector containing indexes local : local selection (True) or global selection (False)

selectp(lst=None, file=None, reject=False, local=False, from_num=True)

Return an N-body object that contain only particles with specific number id.

The list of id’s is given either by lst (nx1 int array) or by the name (“file”) of a file containing the list of id’s.

lst : vector list (integer)

reject : True/False : if True, reject particles in lst (default = False) local : local selection (True) or global selection (False)

frum_numif True, use self.num to select particules

if False, use arange(self.nbody)

set_command_line()

Set the command line

set_date()

Set a date

set_filenames(p_name, pio=None)

Set the local and global names

p_name : new name(s) pio : ‘yes’ or ‘no’

set_ftype(ftype='swift')

Change the type of the file

ftype : type of the file

set_gittag()

Set the git tag

set_local_system_of_units(params='default', UnitLength_in_cm=None, UnitVelocity_in_cm_per_s=None, UnitMass_in_g=None, unitparameterfile=None, gadgetparameterfile=None)

Set local system of units using UnitLength_in_cm,UnitVelocity_in_cm_per_s,UnitMass_in_g

  1. if nothing is given, we use self.unitsparameters to obtain these values

2a) if UnitLength_in_cm, UnitVelocity_in_cm_per_s, UnitMass_in_g are given, we use them

2b) if UnitLength_in_cm, UnitVelocity_in_cm_per_s, UnitMass_in_g are given in a dictionary

  1. if unitparameterfile is given we read the parameters from the file (units parameter format)

  2. if gadgetparameterfile is given we read the parameters from the file (gadget param format)

set_npart(npart)

Set the local number of particles of each types. This function modifies the variable self.tpe

set_parameters(params)

Set parameters for the class

set_pio(pio)

Set parallel input/output or not io

pio : ‘yes’ or ‘no’

set_tpe(tpe)

Set all particles to the type tpe

set_unitsparameters(unitsparams)

Set units parameters for the class.

set_username()

Set a username

set_verbosity(verbose)

set the verbosity level

verbose : 0 to 10

sort(vec=None, local=False)

sort particles according to the vector vec

vec : vector on which to sort (default=self.num)

sort_type(local=False)

Contrary to sort, this fonction sort particles respecting their type.

spec_info()

Write specific info

sub(n1=0, n2=None)

Return an N-body object that have particles whith indicies in the range [n1:n2].

n1 : number of the first particule n2 : number of the last particule

Note : the first particle is 0

take(vec=None, local=False)

extract particles according to the vector vec

vec : vector (default=self.num)

update_creation_info()

Update creation info

warning(msg, verbosity=1)

Print warning message

by default, the verbosity is 1, the level 2, the color red.

write(name=None)

Write the particle file(s)

write_num(name)

Write a num file

name : name of the output