[HN Gopher] A simplified Python simulation of diffusion
       ___________________________________________________________________
        
       A simplified Python simulation of diffusion
        
       Author : rbanffy
       Score  : 118 points
       Date   : 2024-06-30 18:24 UTC (1 days ago)
        
 (HTM) web link (www.thepythoncodingstack.com)
 (TXT) w3m dump (www.thepythoncodingstack.com)
        
       | forgotpwd16 wrote:
       | An excellently presented article. Impressed by the usage of
       | `turtle` module. Didn't knew it was that capable.
        
       | SushiHippie wrote:
       | This somehow reminds me of the Coding Challenges from The Coding
       | Train:
       | 
       | https://youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhf...
       | 
       | Though he uses https://p5js.org/ for most if not all of his
       | challenges (at least the last time I watched his videos).
        
       | semi-extrinsic wrote:
       | I'm sorry, but this is not good code for teaching anyone about
       | anything to do with physics. It manages to be both verbose, non-
       | idiomatic, slow and wrong. Feynman would not touch this with a
       | ten foot pole.
       | 
       | First ditch all the object orientation and encapsulation and
       | stuff. Your data is a 2xN Numpy array. Your visualization is a
       | scatter plot in Matplotlib. Voila, 80% of the code is gone.
       | 
       | For the position updates, you either use a repulsive potential to
       | approximate the hard spheres and do molecular dynamics, showing
       | how to integrate Newton's second law and the Verlet scheme and
       | ergodicity and the whole shebang. Or you do Monte Carlo for the
       | positional updates and keep the exact hard spheres. You discuss
       | statistical mechanics concepts like ensembles and thermostats and
       | stuff.
       | 
       | Then you produce results like the pair correlation function and
       | compare it with the Carnahan-Starling equation, dig into the
       | really cool stuff. Compute velocity autocorrelation functions,
       | test what happens when you change density and temperature, talk
       | about phase diagrams, etc.
       | 
       | This is actually an amazingly deep subject, yet very accessible
       | and intuitive, that sits on the border between physics and
       | chemistry. Sad to see it treated like this. Would suggest that
       | people have a look at the book by Allen and Tildesley which is
       | much much better. They have both Python and Fortran example code
       | on Github.
        
         | evrimoztamur wrote:
         | I agree with your comments, but it's still good to recognise
         | that although his modelling might not be correct or imprecise,
         | the results _do_ show diffusion occurring.
         | 
         | It might not be the realistic simulation of what's going on but
         | gets the intuition across very well nonetheless.
        
         | kevmo314 wrote:
         | There's an excellent statistical mechanics course on Coursera
         | that goes through these steps:
         | https://www.coursera.org/learn/statistical-mechanics
         | 
         | It uses python and had a mindblowing moment when I realized how
         | the simulation I wrote connected to the world, making it one of
         | the few I actually managed to finish. As you alluded to, the
         | lecturers definitely hint that simpler code is easier to work
         | with as the later stuff becomes impossibly complex with
         | anything more.
        
           | maurits wrote:
           | Definitely a personal favorite for me to. Excelled
           | introduction to MCMC methods, lots and lots of python
           | programs and nicely filmed against a green screen.
        
         | wrycoder wrote:
         | TFA is a decent first cut. It may be inefficient (the author
         | already mentioned and discarded a N^2 approach), but it's
         | easily understandable. The author also mentioned that he was
         | planning a NumPy version, so maybe some of the topics you
         | addressed will be covered.
        
       | programjames wrote:
       | I think it'd be really cool to add a histogram of particle speeds
       | with an entropy counter. To control temperature, make particles
       | gain a little momentum in a random direction when they hit the
       | sides of the box.
        
         | ijustlovemath wrote:
         | Wouldn't they lose momentum to heating the environment?
        
           | powersnail wrote:
           | At the level of particles, isn't "heating" the same as
           | speeding up?
        
             | ijustlovemath wrote:
             | Yes! I was pointing out that particles typically lose
             | energy to collision with the environment, not gain it (as
             | the OP implied)
        
               | topherclay wrote:
               | OP was suggesting a way to artificially control the
               | temperature, so they would artificially add energy to
               | raise temperature.
        
               | programjames wrote:
               | Nah, the goal isn't to add or subtract energy, just
               | change the entropy. If you have a process that _on
               | average_ doesn 't change energy, but makes the
               | distribution of speeds more or less narrow, then that is
               | temperature control. I thought adding a fixed amount of
               | momentum in a random direction (where higher temperature
               | = more momentum) would be a way to control temperature.
               | However, that actually increases energy on average, so
               | you would need some way to lose energy to the environment
               | as @ijustlovemath pointed out.
        
       | spacecadet wrote:
       | Reminds me of the 2D universe simulator I wrote over Christmas
       | last year. Mine consists of square entities (going for a pixel
       | art aesthetic) of random initial size that have a weak gravity,
       | slowly bumping into each other, condensing down into small
       | objects with very strong gravity... you can imagine where thats
       | going. The code is god awful but it was fun to hack through and
       | it looks cute. I used Pygame, but it also spits out some matplot
       | visualizations for a historical pov.
        
       | youssefabdelm wrote:
       | which feynman lecture is he referencing (on diffusion)?
       | 
       | edit: there's one called "#43 Diffusion (5/1/62)" here:
       | https://www.feynmanlectures.caltech.edu/flptapes.html#restor...
       | so I assume that
        
       | cocodill wrote:
       | I really underestimated the turtle package.
        
       | block_dagger wrote:
       | If Feynman _were_ teaching today.
        
       ___________________________________________________________________
       (page generated 2024-07-01 23:02 UTC)