05 - Attributes and functions on particle collectionsΒΆ

AMUSE pre-defines a number of calculcated attributes on particle sets, such as the kinetic energy of the particles in the set. These calculated attributes are used often and provide a sufficient set to start out with, but they do not define a complete set. It’s possible to define your own attributes and extend the attributes on a particle set.


from amuse.lab import *

As shown in the previous example, you can create a particle set by specifying the number of particles and setting their attributes. You can also create a particle set by using an inital condition function. For stellar clusters the commonly used plummer and king models are available. For this tutorial we will start with a king model. Global clusters created with a king model need the number of stars in the cluster and a dimensionless depth parameter that determines the depth of the potential well in the center of the cluster.


particles = new_king_model(1000, 3)
print particles
                 key         mass       radius           vx           vy           vz            x            y            z
                   -         mass       length  length / time  length / time  length / time       length       length       length
====================  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========
14941875340832153744    1.000e-03    0.000e+00   -5.142e-02    1.518e-01   -3.030e-01    5.874e-01   -1.260e+00   -3.037e-01
 3047495988204324397    1.000e-03    0.000e+00    9.713e-01   -3.676e-01    3.758e-01   -3.442e-01    4.192e-01   -3.286e-01
 1663902078147430931    1.000e-03    0.000e+00   -5.101e-01    3.633e-01    2.800e-01   -1.065e-01    8.654e-02    8.818e-01
14623721528790517272    1.000e-03    0.000e+00    7.368e-03    1.083e-02    3.661e-02    1.244e-01    3.755e-01   -1.515e-01
12565044392055223970    1.000e-03    0.000e+00   -4.732e-01   -2.554e-01   -3.485e-01   -8.954e-03    3.507e-01   -3.651e-01
 5385784806135041164    1.000e-03    0.000e+00   -4.980e-01   -8.662e-03   -2.807e-02    4.903e-01    3.392e-01    2.768e-01
 1616582969738833703    1.000e-03    0.000e+00    6.510e-02   -5.120e-02    2.340e-01   -4.674e-01    1.291e-01   -5.609e-01
15620339460773635638    1.000e-03    0.000e+00   -6.845e-02   -3.886e-01   -6.583e-01   -1.639e-01   -1.477e-01    1.877e-01
16108179093150181517    1.000e-03    0.000e+00    4.660e-01    1.640e-01    9.824e-03   -6.783e-01    2.762e-01   -1.458e+00
 6598806552639498163    1.000e-03    0.000e+00    2.133e-01   -2.693e-01    2.402e-01   -1.560e+00    8.916e-01   -8.611e-01
12067245923362741690    1.000e-03    0.000e+00    1.170e-02    6.861e-01   -5.301e-02    6.706e-01    2.687e-01    8.707e-02
 1248938253019913516    1.000e-03    0.000e+00    1.254e+00   -8.884e-02   -5.311e-01   -2.176e-01    3.248e-01    1.502e-02
 1592721984417522772    1.000e-03    0.000e+00   -3.245e-01   -8.592e-02    1.871e-01   -3.903e-01    1.076e+00    2.519e-01
11191306389861137428    1.000e-03    0.000e+00   -3.187e-01    1.097e+00    2.865e-01    2.295e-02    1.632e-01   -2.207e-01
 9531577223621737064    1.000e-03    0.000e+00   -5.615e-01   -2.887e-01   -5.723e-01    7.962e-01    3.863e-02   -1.058e+00
 9948240140697596199    1.000e-03    0.000e+00    5.294e-02    2.432e-01   -2.515e-01    5.996e-01    5.793e-01   -3.833e-01
 9570459888895964721    1.000e-03    0.000e+00   -1.782e-01    4.270e-01    3.731e-01    1.027e+00   -6.474e-02    1.162e-01
  801218631441228932    1.000e-03    0.000e+00   -4.313e-01    1.277e-01   -2.302e-01   -8.554e-01   -2.344e-01   -3.136e-01
17308596196227517542    1.000e-03    0.000e+00   -3.203e-02    4.741e-01   -1.352e-01   -1.734e-02    1.201e-01   -4.890e-01
15587703822984066638    1.000e-03    0.000e+00   -9.070e-02   -3.976e-01   -5.500e-01   -5.743e-02   -3.567e-02    1.700e-01
                 ...          ...          ...          ...          ...          ...          ...          ...          ...
 7171992720560753926    1.000e-03    0.000e+00   -5.822e-02    3.609e-02    8.881e-01    2.383e-01   -7.462e-01    4.752e-02
 6405032883083282668    1.000e-03    0.000e+00    8.783e-02    3.400e-01   -2.778e-01    1.764e-01    2.380e-01    1.349e-01
12535947159898184496    1.000e-03    0.000e+00   -1.418e-01    4.007e-01    1.799e-02   -5.817e-01   -2.729e-02    2.421e-01
 1218929989819455397    1.000e-03    0.000e+00   -3.028e-01   -3.731e-02    7.884e-01    7.716e-02   -6.674e-01   -4.488e-01
 9512772759628793678    1.000e-03    0.000e+00    1.167e-01   -4.454e-01    8.470e-01    1.536e-01   -1.587e-01   -4.298e-01
15875000895156835386    1.000e-03    0.000e+00    3.713e-01    4.399e-01    8.579e-02   -6.670e-02   -9.115e-02   -2.879e-01
12726490809321148962    1.000e-03    0.000e+00    1.519e-01   -3.623e-02   -2.451e-01   -7.669e-01    2.405e-02   -9.309e-01
 2235448861912593272    1.000e-03    0.000e+00   -3.820e-01   -3.062e-01   -1.131e-01    8.205e-01    7.057e-01   -4.381e-01
 7527702350996891299    1.000e-03    0.000e+00    1.688e-01   -1.663e-01    1.395e-02    2.068e+00   -1.143e+00    3.822e-01
15607825729843239555    1.000e-03    0.000e+00    1.350e-01    2.382e-01    1.005e-01   -2.684e-01    5.071e-03   -1.095e+00
 6092252739760677477    1.000e-03    0.000e+00   -3.342e-01   -7.485e-01   -3.993e-01   -1.704e-02    4.675e-01    4.394e-02
 9556526972440563656    1.000e-03    0.000e+00   -1.812e-01   -2.280e-01   -2.275e-01   -4.310e-01   -4.913e-01   -4.266e-02
 7447103547633954671    1.000e-03    0.000e+00    2.207e-01    4.919e-01   -3.188e-01    1.730e-01    8.542e-01   -9.632e-01
17785100334465106793    1.000e-03    0.000e+00    5.482e-01    8.999e-01    5.682e-01   -5.632e-01    3.798e-01   -1.006e-01
16982287236572853457    1.000e-03    0.000e+00   -2.829e-01   -3.069e-01    2.429e-01    5.044e-01   -1.122e+00    1.779e-01
15666422291174284398    1.000e-03    0.000e+00   -4.461e-02   -2.958e-01    1.177e-01   -4.802e-01   -7.685e-01   -5.709e-01
  832502599042199760    1.000e-03    0.000e+00    2.445e-01    8.147e-01    2.712e-01    3.977e-01    1.061e-01    4.246e-01
  550648420349572969    1.000e-03    0.000e+00    5.122e-01   -4.359e-01   -1.515e-01    1.004e+00   -1.128e+00    1.227e-01
16484500969018969108    1.000e-03    0.000e+00    8.539e-03    4.435e-01    2.633e-01    8.164e-02   -9.306e-02    1.617e+00
13916347499853049959    1.000e-03    0.000e+00   -4.961e-01    1.124e-01    2.901e-01   -6.253e-01   -3.860e-01   -4.285e-02
====================  ===========  ===========  ===========  ===========  ===========  ===========  ===========  ===========

Common properties for a stellar cluster are its center of mass position, total kinetic energy and potential energy.


print "center of mass", particles.center_of_mass()
print "kinetic energy", particles.kinetic_energy()
print "potential energy", particles.potential_energy(G = nbody_system.G)
center of mass [1.01915004214e-17, 1.36067372647e-17, -2.04643160057e-18] length
kinetic energy 0.24821889881 mass * length**2 * time**-2
potential energy -0.499615776786 length**2 * mass * time**-2

For the potential energy calculation we need to specify the gravitational constant, as the default value will use the gavitational constant in S.I. units and we are working in nbody units for this tutorial.

In N-body calculations and reporting, the kinetic and potential energy of a set of stars is often scaled to exactly 0.25 and -0.5 respectively. AMUSE also has a function for this.


print "kinetic energy", particles.kinetic_energy()
print "potential energy", particles.potential_energy(G = nbody_system.G)
kinetic energy 0.25 mass * length**2 * time**-2
potential energy -0.5 length**2 * mass * time**-2

Note that the potential energy and scaling calculations are implemented as order N-squared operations

Attributes of particle sets are always 1 dimensional by default, an array with a single value per particle attribute. But for some attributes it is easier to work with a 2d set, an array with multiple values (or an array of values) per particle attribute. For example, the positions of all particles. These attributes are called vector-attributes and are defined as a combination of 2 or more simple attributes.

The position attribute combines the values of the x, y and z attributes.


print particles[0].x
print particles[0].y
print particles[0].z
print particles[0].position
0.586963925986 length
-1.25883702112 length
-0.303424583804 length
[0.586963925986, -1.25883702112, -0.303424583804] length

Other common vector attributes are velocity (combination of vx,vy,vz) and acceleration (combination of ax,ay,az).

You can set the value of a position attribute and the underlying x, y or z attributes will be changed.


particles[0].position = [0, 0.1, 0.2] | nbody_system.length
print particles[0].x
print particles[0].y
print particles[0].z
0.0 length
0.1 length
0.2 length

You can set the value of the x, y or z attribute and the position will change (as the position is just a combination of these attributes).


particles[0].x = 0.3 | nbody_system.length
print particles[0].position
[0.3, 0.1, 0.2] length

You cannot change an item in the position array and thereby change the x, y, or z positions


particles[0].position[0] = 0.5 | nbody_system.length # this will not change anything in the particles set as the position is a copy
print particles[0].x
print particles[0].position
0.3 length
[0.3, 0.1, 0.2] length

You can use the position attribute on the entire set. Let’s print the positions of the first 10 particles.


print particles.position[0:10]
[[ 0.3  0.1  0.2], [-0.34390633  0.41883074 -0.32834773], [-0.10638456  0.08647715  0.8811212 ], [ 0.12426249  0.37524894 -0.15137324], [-0.00894735  0.35044208 -0.36484632], [ 0.48994552  0.33888992  0.27661958], [-0.46705333  0.12900976 -0.56044784], [-0.16375834 -0.14763582  0.18759088], [-0.67774479  0.27599753 -1.45662795], [-1.55897325  0.89094977 -0.86048394]] length

You can also use the position attribute to set values for the entire set


particles.position = [0.1, 0.2, 0.3] | nbody_system.length # set the position of all particles in the set to the same value
print particles.position[0:10]
print particles.x[0:10]
[[ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3], [ 0.1  0.2  0.3]] length
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1] length

Defining a new vector attribute is done by calling the add_vector_attribute or add_global_vector_attribute. The first call will define the attribute on the particle set and not on any other set. The second call will define the attribute on the particle set and any future sets created in the script. (The second call is used in the amuse framework itself to define the position, velocity and acceleration attributes)


particles.add_vector_attribute('position2d', ('x', 'y'))
print particles[0].position2d
[0.1, 0.2] length

If you enter particles.add_ and press tab you’ll notice two other function besides the add_vector_attribute function; add_calculated_attribute will create an attribute where the values are calculated based on other attributes, add_function_attribute will create a function on the set that gets the set and optional function parameters. These function also have global versions (add_global_...). The add_global_function_attribute call is used in the AMUSE framework to implement the kinetic_energy and potential_energy functions.


particles.add_function_attribute('calculate_total_mass', lambda particles : particles.mass.sum())
print particles.calculate_total_mass()
1.0 mass

Previous topic

04 - Collections of Particles

Next topic

06 - Using a community code

This Page