Introduction To Computer Simulation of Physical Systems (Lecture 1)
Introduction To Computer Simulation of Physical Systems (Lecture 1)
Systems
(Lecture 1)
PHYS 3061
Importance of computers in physics
• How can I formulate a physical problem on a
computer?
– New ways of thinking about physics
– numerical analysis, symbolic manipulation,
visualization, simulation, and the collection and
analysis of data
• Numerical analysis:
– the solution of well-defined mathematical
problems to produce numerical solutions
– to compute multidimensional integrals,
manipulate large matrices, or solve nonlinear
differential equations…
• Why Nonlinear is so important?
• Matrix operation will be covered in 4061.
• symbolic manipulation.
– Example: the solution of the quadratic equation,
• ax2+ bx + c = 0:
• A symbolic manipulation program can give the solution as
– x = [-b √ (b2-4ac)] / 2a.
– Other mathematical operations
• differentiation, integration, matrix inversion, and power series expansion can
be performed using symbolic manipulation programs.
– Feynman diagrams: multi-dimensional integrals of importance in
quantum electrodynamics
• a major driving force to the development of symbolic expressions.
– Maxima, Maple, and Mathematica: good at symbolic manipulations.
– Matlab and Octave: good at matrix operations.
• visual representation
– Our eyes can determine patterns and trends easily to further understand the physical
mechanisms.
– The use of graphics also can increase our understanding of the nature of analytical
solutions.
– sin x = x-x3/3!+x5/5! +. . ., vs.
• We don’t know.
• Interaction vs. object.
• Why Quantum states are often
discretized?
– Coupling of quantum states or qubits
can give us a sense of 3d space.
• Suppose you live in such a matrix, is
it possible to figure out you live in
such a matrix?
Programming languages
• Strongly encouraged to use C/C++ or JAVA in the
lab. Class lectures will use JAVA code as illustrations.
• C:
– developed by Dennis Ritchie
– at Bell Laboratories around 1972 in parallel with the Unix
operating system.
• C++:
– an extension of C designed by Bjarne Stroustrup
– at Bell laboratories in the mid-eighties.
• C++
– more complex than C,
– object oriented features, and other extensions.
• C/C++ have high performance, difficult to
debug.
– popular choices for developing operating systems
and software applications
– provide direct access to memory and other system
resources.
• Java:
– object-oriented language
– created by James Gosling and others at Sun Microsystems.
– introduced in late 1995,
– rapidly evolved, very popular and is the language of choice in most
introductory computer science courses.
• Java borrows much of its syntax from C++, but has a simpler
structure.
– contains only fifty keywords
– the Java platform adds a rich library
• enables a Java program to connect to the internet,
• render images,
• perform other high-level tasks.
• The idea of object-oriented programming
– functions and data are grouped together in an object,
rather than treated separately.
– A program is a structured collection of objects that
communicate with each other causing the internal state
within a given object to change.
• A fundamental goal of object-oriented design
– to increase the understandability and reusability of
program code by focusing on what an object does and
how it is used, rather than how an object is implemented.
• Java: a platform in itself
– similar to the Macintosh and Windows
– it has an application programming interface (API) that enables cross-
platform graphics and user interfaces.
– Java programs are compiled to a platform neutral byte code so that
they can run on any computer that has a Java Virtual Machine.
• Despite the high level of abstraction and platform
independence, the performance of Java is becoming
comparable with native languages.
• If a project requires more speed, the computationally
demanding parts of the program can be converted to C/C++ or
Fortran.
Very Brief introduction on OOP
• Encapsulation : the way that an object’s essential
information is exposed through a well-documented
interface, but unnecessary details of the code are
hidden.
– For example, we can model a particle as an object.
• Whenever a particle moves, it calculates its acceleration from
the total force on it.
• to use the trajectory of the particle,
• for example to animate the particle’s trajectory,
• refer only to the interface
• no need to know how the trajectory is calculated.
• Inheritance
– allows a programmer to add capabilities to
existing code without having to rewrite it or even
know the details of how the code works.
– For example, programs show the evolution of
planetary systems, quantum mechanical wave
functions, and molecular models..
– They can share a library code called timer
• periodically executes code in your program and then refreshes
the on-screen animation.
• Later, on programming the physics, because it is not necessary
to write the code to produce the timer or to refresh the
screen.
– Similarly, we can design a general purpose graphical user
interface (GUI) by extending code written by Sun
Microsystems known as a JFrame.
• Our GUI has the features of a standard user interface such as a
menu bar, minimize button, and title, even though we did not
write the code to implement these features.
• Polymorphism: helps us to write reusable
code.
– For example, it is easy to imagine many types of
objects that are able to evolve over time.
• We can simulate a system of particles using random
numbers rather than forces to move the particles.
– Any examples?
• By using polymorphism, we can write general purpose
code to do animations with both types of systems.
A few important things you should keep in
mind in this course
• Analysis of results.
– What physical insights can you gain from your
simulations?
– How do we go from numbers to
"understandings" ?
• Programming and debugging techniques. How
do we gain confidence that the computer code
is correct: it is doing what we think it should
be doing?
– Details on debugging techniques will be covered in
lab sessions.
• Learn to explain the logic behind why
simulations are done in a particular way.
• In the whole semester, you will be required to
finish one large class project on molecular
dynamics
– Why MD?
– It will be separated into small pieces
– You will understand all the details in the
simulation!
Course contents
• Random Process: Particle in a box, random
number generator, very brief introduction of
random walk.
• Cooling problem: Euler algorithm for solving
first-order differential equations. Brief
introduction to high order algorithm.
Course contents
• Single particle motion : Newton’s equation of motion.
• Molecular dynamics: Verlet algorithm, hard ball potential, LJ
potential, RDF, how to calculate thermal properties.
• chaotic dynamics: Briefly discuss simple deterministic
nonlinear models which exhibit complex patterns of behaviour.
• How to solve PDE.
– Electrostatic problems: Brief introduction of relaxation method, finite
difference method.
• Monte Carlo simulation
• If we still have time, we may try some advanced topics in
simulations.
Course Contents
• 4 computational laboratories (3 hours each)
for one large molecular dynamic simulation
project, one lab in chaotic motion and one lab
in PDE.
• Presentation of your simulation project results
(5 min) + one freely selected presentation on
literature reviews of simulations (5 min).
Topics need to be preapproved by me.
• Lab 0: Write a few small functions on dot
product and cross product and getting familiar
with the software and systems.
Lab 1
• Random walk, 3d, symmetric probability , random initialization
(Maxwell Boltzmann distribution)
– monitoring position, energy, momentum, angular
momentum
Lab 2
• Ideal gas particle, particle in a box,
• Euler method for integration
Lab 3
• Verlet algorithm
• L J potential
Lab 4
• Radial distribution function
• Heat bath, thermal properties.
• Lab 5, Chaotic motion
• Lab 6, solving PDE.
Assessment Scheme
• Project: 55%
– The deadlines of code, simulation results or lab report are
in two weeks from the lab date.
• Final Project on MD or MC: 25%
• Presentation of literature review: 5 %
• Homework: 10%
• Class activity: 5%
– Answer important questions, point out important errors
in lecture notes, frequent communications in the
classroom. Class attendance will be taken randomly.
Reference books
• Textbook, Harvey Gould, Jan Tobochnik and Wolfgang Christian : An Introduction to Computer
Simulation Methods: Applications to Physical Systems, third edition 2007 Pearson/Addison-
Wesley
• Harvey Gould and Jan Tobochnik : An Introduction to Computer Simulation Methods, second
edition 1996 Addison-Wesley
• (W) Samuel S M Wong : Computational Methods in Physics and Engineering, 1997 World-
Scientific
• William H Press et al : Numerical Recipes in FORTRAN : the Art of Scientific Computing, 1992
Cambridge University Press
• William H Press et al : Numerical Recipes in C : the Art of Scientific Computing, 1988 Cambridge
University Press
• Tao Pang : An Introduction to Computational Physics, 1997 Cambridge University Press for
program listings in Fortran 90 and Mathematica
• Alejandro L Garcia : Numerical Methods for Physics, second edition 2000 Prentice Hall for
program listings in MatLab and C++
• Nicholas J Giordano : Computational Physics, 1997 Prentice Hall for program listings in True
Basic
Tools for doing simulations
• Present some essential syntax of Java for you
to understand.
• We’ll use VMD to plot your simulation results.
The details of VMD will be covered in the lab
sessions.
• Making models:
– If the model is sufficiently detailed, we can determine its
behavior, and then compare the behavior with experiment.
– verification of the model, changes in the model, and further
simulations and experiments.
• begin with a set of initial conditions.
– determine the dynamical behavior of the model numerically
– generate data in the form of tables of numbers, plots, and
animations.
• We begin with a simple example to see how this process
works.
• Suppose a particle near the surface of the
Earth subject to the force of gravity.
• We assume that air friction is negligible and
the gravitational force is given by
– Fg = -mg,
– where m is the mass of the ball and g = 9.8 N/kg is
the gravitational field (force per unit mass) near
the Earth’s surface.
• Suppose the problem with only y direction, we
use Newton’s second law to find the motion of
the ball,
• m d2y/dt2 = F,
• Or d2y/dt2 = g.
• You know the solution, right?
– y(t) = y(0) + v(0)t – ½ gt2
– v(t) = v(0) - gt.
• Suppose we don’t know the solution and are
trying to solve the ODE.
• dy/dt= v
• dv/dt= -g,
• Approximation comes:
• (y(t + Δt)- y(t))/Δt= v(t)
• (v(t + Δt) - v(t))/Δt= -g.
• y(t + Δt) = y(t) + v(t)Δt
• v(t + Δt) = v(t) - gΔt,
• The above finite difference method is called
Euler algorithm.
• We can initialize y0 and v0 and iterate!
• If Δt is small enough, we can achieve very
accurate results!
• One example:
• First order differential equation:
• dy/dx = f(x),
• yn+1 = yn + f(xn)Δx.
• Suppose that f(x) = 2x and y(x = 0) = 0.
– The analytical solution is y(x) = x2.
– Find the finite difference equation using the Euler
algorithm.
– For simplicity, choose Δx = 0.1. It would be a good
idea to first use a calculator or pencil and paper to
determine yn for the first several time steps.
• One common error: