5.2 The read function

Now, its time to write the read_particles functions.

  def read_particles(self,f):
 
    tpl = (float32,int32,int32)
    tnow, ngas, nstar = io.ReadBlock(f,tpl,byteorder=self.byteorder,pio=self.pio)

The variable tpl describe the type of the data contained in the header, then, use the function io.ReadBlock() to read the header and send it to other nodes, if necessary (if self.pio='no').

Now, before reading the vectors blocks, we have to define how particles, depending on their type, will be distributed among nodes. First, create the npart_read particle, that corresponds to npart read from the header. Then simply use get_npart_and_npart_all() to create npart and npart_all. npart gives you the number of particles of each type present in the local node. npart_all is a list of npart corresponding to all nodes. Thanks to this latter variable, the io.ReadArray() function will be able to read data blocks and send the right particles to the right nodes, taking into account the type of the particles.

        
    npart_read = array([ngas,nstar])
    npart,npart_all       = self.get_npart_and_npart_all(npart_read)
Now, as the metallicity block contains informations only for gas particles, we have to compute an equivalent of npart_all but only for the first type.
 
    npartgas,npartgas_all = self.get_npart_and_npart_all(npart_read[0])

Now, it is usefull to compute the specific local variables ngas,nstar and nbody.

     
    # now, get the corect values
    ngas  = npart[0]
    nstar = npart[1]
    nbody = sum(npart)

It is time now to read the position, velocities, mass and metallicity blocks, by giving to the function io.ReadArray() the right particle distribution (npart_all or npartgas_all). The self. in front of the variables is important to make these vectors global. In the last line, we complete the vector self.meta by adding zeros, in order to have a value for each particles (even for stars).

            		
    self.pos = io.ReadArray(f,float32,shape=(nbody,3),byteorder=self.byteorder,pio=self.pio,nlocal=npart_all)
    self.vel = io.ReadArray(f,float32,shape=(nbody,3),byteorder=self.byteorder,pio=self.pio,nlocal=npart_all)
    self.mass= io.ReadArray(f,float32,                byteorder=self.byteorder,pio=self.pio,nlocal=npart_all)
    self.meta= io.ReadArray(f,float32,                byteorder=self.byteorder,pio=self.pio,nlocal=npartgas_all)
    self.meta  = concatenate((self.meta,zeros(nbody-npart[0]).astype(float32)))

Finally, invoce the two following commands to ensure that all variables will be accessible in the main class.

    self.make_default_variables_global(vars())
    self.make_specific_variables_global(vars())

See About this document... for information on suggesting changes.