projects / python / water model

3D Water Model

There was a really interesting question on SO ( that grabbed my attention. Unfortunately, it has been closed, but I thought it was a great question, and had started a mash-up answer. Seeing as it is now closed, I will work through my model here.

The scenario:

We have an input file that contains the measured (x, y, z) coordinates of all three atoms in a water molecule. Our job is to extend the (x, y, z) data to represent any number of water molecules. Coordinates should be generated psuedo-randomly and no molecule should be close that 0.2 nm from any other.

This project is open to anyone who wants to help out! Feel free to post comments, suggestions or different implementations.

Below is my initial work on this (it is a work in progress):


These functions do the following:

  • import_data(infile):   reads the measured data from the file and formats it
  • data_fields(data):    if necessary, this function will group all ‘like’ fields (i.e. create a list of all x coords, or all atom_IDs)(and below…)
  • distance(a, b): calculates the distance between two 3-dimensional points
  • gen_coords(): this one is interesting, it takes as input the coordinates of a neighbouring atom, and then adds a psuedo-random shift to create the new coords. The size of the shift is determined by (min_range, max_range) which is approx. the distance of a covalent or polar bond as necessary
  • test_coords() test whether the psuedo-random shift above is within an appropriate range (i.e. covalent bonds should be approx. 1 Armstrong in length, and polar bonds are approximately 2 Armstrongs long). Will recall gen_coords() until condition is satisfied

Still to do:

  • generate coords for HW2. The above works well for O and HW1, but is not accurate for HW2. To be more accurate, HW2 should be a 120 degree rotation of HW1. Ideally, this would be accomplished by a rotation matrix.
  • Add molecule coords and meta-data back into the datafile.
  • Lastly, plot it, see what the model looks like.

Writing out to file and plotting are straightforward. The rotation matrix is a little trickier, and will require me to re-learn some maths I haven’t used for years.



This project is open to anyone who would like to help out – feel free to post comments, suggestions or implementations on this 🙂


2 thoughts on “3D Water Model

  1. Hi!

    I never noticed your replied. If you’re still interested, I was able to solve the problem few days after I had posted at SO (I have added some more stuff – mainly through options; the molecules are rotated, too):

    Python code:
    Input file (water.gro):

    You can run it with: python -n 1000 -rcfp.

    n – number of molecules generated

    r – will display the system using VMD (you need to comment/uncomment for Windows/UNIX as the commands are very different for pipes)

    c – counts in real time the progress

    p – plots the number of attempts, collisions and accepted molecules

    Have a nice day,



  2. BTW, if you’re interested, I have coded several other programs:

    – reads the PDB protein sequence and creates new PDB files with Ala-mutations for every reqidue in the chain – one at a time,

    – calculates and plots concentrations for aA -> bB -> cC -> …,

    – full GUI, Lotka-Volterra, Brusselator and Oregonator models of oscillating reactions,

    – Monte Carlo gas simulation using Metropolis algorithm (uses lists, not arrays, though, but haven’t noticed inferior performance compared to tutor’s code; not that interesting, though).




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s