next up previous
Next: The Lennard-Jones potential Up: From bouncing balls to Previous: From bouncing balls to

Subsections

Choosing an appropriate data structure

As with all problems in computer science, there is more than one solution to which data structure should be used. Some are clearly better than others, but sometimes the distinction may not be so clear. Before choosing a data structure, it is important to work out what the priorities should be.

Two possible data structures are considered here with their advantages and disadvantages.

List of atom objects

In this data structure, an atom class would be created. It would store everything there is to know about the atom, its position, velocity, the force on it, even graphical information. The atom objects would be stored in a list.

The advantage of this data structure is that it is highly extensible and modifiable. New attributes can easily be added, without changing much code. The implementation of the class can be completely changed without the code that uses it being changed at all.

The biggest disadvantage of this data structure is that it has a high overhead. High level objects typically use more resources and take longer to process than objects who's implementation is in built into the language. Another disadvantage is that sometimes only a small amount of the data in the object is needed for a particular function, acting on an object that contains much data, especially creating and deleting such objects, may slow things down.

Lists of vectors

This data structure has a list for each attribute of an atom. In the case where the list is referring to a vector, the vector is either stored as a tuple or a list.

The major advantage of this data structure is its efficiency. The python interpreter does not have to follow through as many references in order to find the data. It also means only the data needed at the time need be created or deleted at the time, for example, if a particular function is only concerned about position, it only has to create and delete atoms in the position array, without worrying about the velocity and force attributes.

The major disadvantage is it is very messy. A function needs to be passed each list that it's acting on, rather than the simple list of atoms. It can also be easy to lose track of what data has been updated and what the names of the references are.

When storing the vectors, there are two main ways of doing it. The first is using tuples. Tuples are a immutable sequence type, this means the data in the tuple cannot be modified. The other way is in lists, which is mutable, and so the data can be modified. Depending on the implementation of the interpreter, either may be more efficient. Mutable sequence types generally have a higher overhead, immutable sequence types are usually faster, but modifying them is not allowed, so the old sequence needs to be destroyed and the new one created, in order to modify.

The chosen data structure

As efficiency is the key priority in this software, we choose a list of vectors. Furthermore, because Visual Python can easily create its own vectors from tuples, we choose tuples as the implementation of the vectors.

Building the data structure

Now we need to build the data structure. To start, we will use a simple cube structure. It will be placed in a function so that if, in future, more complex structures are desired, switching will be simple.

<Create Simple Cube>=
def createSimpleCube(rside, nside):
    # Return a cube with length rside and nside atoms on each side
    cube = []
    spacing = [0,0,0]
    for n in range(3):
        if nside[n] == 1:
            spacing[n] = 0
        else:
            spacing[n] = float(rside[n]) / (nside[n] - 1)
    for x in range(nside[0]):
        for y in range(nside[1]):
            for z in range(nside[2]):
                cube.append((spacing[0] * x, spacing[1] * y, spacing[2] * z))
    return cube
Used below (1), below (2).

In this code, rside is a tuple of the width of each side, and nside is a tuple of the number of atoms on each side.


next up previous
Next: The Lennard-Jones potential Up: From bouncing balls to Previous: From bouncing balls to
James Roper 2004-02-12