+ All Categories
Home > Documents > [email protected] 1facultymembers.sbu.ac.ir/abdussalam/plan2.pdf · Introduction,Ref[0] Ref[0]:...

[email protected] 1facultymembers.sbu.ac.ir/abdussalam/plan2.pdf · Introduction,Ref[0] Ref[0]:...

Date post: 27-Jan-2021
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
125
Transcript
  • [email protected] 1

  • Computational Physics

    S. AbdusSalam

    Department of Physics,Shahid Beheshti University

    1st semester 1395/1396 (2016/2017)

    [email protected] 2

  • Aims, overview, & outlook

    I Introduce computational concepts and techniques that shouldmake students confident and able to use computers for solvingproblems

    I Address: Linux OS; C/C++ and python; numerialdifferentiation and applications; numerical integration; scietificlibraries, Monte Carlo methods, Bayesian inference tools, andHPC

    I Saturdays, Mondays 10:30 to 12:00 from 1395/06/20 to1395/10/06. Exams: 1395/08/17 and 1395/10/13

    I attendance(1), quiz(1), projects(5) & exams(13)

    [email protected] 3

  • [email protected] 4

  • [email protected] 5

  • Time table

    Date Subject Date Subject

    1395/06/20 Linux, Shell, Emacs 1395/07/19 Radioactive Decay (RD)

    1395/06/22 Linux, Shell, Emacs 1395/07/24 RD, Random Numbers

    1395/06/27 C Programming 1395/07/26 Projectiles

    1395/06/29 C Programming 1395/08/01 Simple Harmonic Motion

    1395/07/03 Make & Makefile 1395/08/01 Surgery/Hands-on, Gnuplot

    1395/07/05 Debugging 1395/08/03 Sun-Earth-Jupiter System

    1395/07/10 C++ Basics 1395/08/08 EulerCromerMethod.h Dev.

    1395/07/12 C++ Basics 1395/08/10 Chaotic Pendulum

    1395/07/17 GNU Scientific Library 1395/08/15 Surgery/Hands-on, Makefile

    Date Subjects Date Subjects

    1395/08/16 Chaotic/Non-linear/Complex Sys. 1395/09/13 Molecular Dynamics

    1395/08/17 Mid-term exams 1395/09/15 QM, Variational Methods

    1395/09/20 QM, Variational Methods

    1395/08/22 Monte Carlo Methods 1395/09/22 Interpolations & Fittings

    1395/08/24 Monte Carlo Methods 1395/09/27 Interpolations & Fittings

    1395/08/29 Num. Integrations & Apps. 1395/09/29 Interpolations & Fittings

    1395/09/01 Ising Model 1395/10/04 Error/Data/Stat. Analyses

    1395/09/06 Molecular Dynamics 1395/10/06 Error/Data/Stat. Analyses

    1395/09/08 Surgery/Hands-on Session 1395/10/13 Final exams

    [email protected] 6

  • Feed backFor making a better Computational Physics courseSay what you don’t like & how to improve, etc

    [email protected] 7

  • Day 01

    Introduction, Linux, Emacs & Shell

    [email protected] 8

  • Introduction, Ref[0]

    [email protected] 9

  • Introduction, Ref[0]

    Ref[0]: R.H. Landau, M.J. PAEZ, & C.C. Bordeianu,“Computational Physics – Problem Solving with Computers”

    [email protected] 10

  • Linux, Emacs & Shell

    The topics to be address:I Linux system basicsI Files, directories, file system; viewing content, creating,

    moving, deleting; permissionsI Text editors, emacs or vi and, optionally, IDEs (netbeans,

    eclipse, etc.)I Bash shell

    References:1) Machtelt Garrels, “Introduction to Linux – A Hands on Guide”2) https://www.gnu.org/software/emacs/tour/index.html3) Machtelt Garrels, “Bash Guide for Beginners”4) http://www.shellscript.sh/index.htmlRef [1]: chapters 2, 3, 6, and section 7.2.5

    Ref [3]: chapters 1, 2, 3, 4, and 7; 6 is optional

    [email protected] 11

  • Linux, Emacs & Shell

    Let’s go through:Ref[1]: chapters 2, 3, 6, and section 7.2.5Ref[2] and Ref[4]Ref[3]: chapters 1, 2, 3, 4, and 7; 6 is optionalCan download this course materials:

    http://www.roma1.infn.it/~abdussas/20160910_CP01.pdf

    http://www.roma1.infn.it/~abdussas/20160910_CP01_Ref1.pdfhttp://www.roma1.infn.it/~abdussas/20160910_CP01_Ref3.pdfOr

    http://users.ictp.it/~shehu/20160910_CP01.pdf

    http://users.ictp.it/~shehu/20160910_CP01_Ref1.pdfhttp://users.ictp.it/~shehu/20160910_CP01_Ref3.pdf

    [email protected] 12

  • Exercise 01

    I (a) Install Linux on a PC or laptop. Read installation manualfor your chosen distribution and do it.http://www.tldp.org/HOWTO/Installation-HOWTO/index.html.

    I (b) Do exercise in the following subsections of Ref[1]:2.5: all; 3.6.2: all; 3.6.4: 1,2, List the files in / in reversealphabetical order, 11, 12; 3.6.5: all

    I (c) Subsection 6.5 of Ref[1]: Emacs or vi; netbeans(this isoptional and only if you already know one of emacs or vi orboth). You may follow Ref[2] for emacs.

    I (d) Follow through chapters 1, 2, 3, 4, and 7 of Ref[3]

    [email protected] 13

  • Summary

    By following lectures and exercises you should have covered:

    Familiarity with a text editor (and an IDE)Ability to write/edit a simple bash script

    [email protected] 14

  • Day 02

    Introduction, Linux, Emacs & Shell

    Day 02 was a holliday. ... should have been about more bash andtext editor (emacs/vi). (Exercises were given out.)

    [email protected] 15

  • Day 03, 04 & 05

    The C/C++ Basics&

    Intro. GNU Scientific Library

    [email protected] 16

  • Aims, overview, & outlook

    I Recap of Days 01 to 02; another Shell scriptI C/C++ Basics; C in C++, Class, ObjectsI Hands-on C++ BasicsI Introduction to GNU Scientific LibraryI Summary and Exercises 02

    [email protected] 17

  • Recap of previous lectures

    I Linux commands, introductionhttps://learnpythonthehardway.org/book/appendixa.html

    I Text editors, Emacs or vihttps://www.gnu.org/software/emacs/tour/index.html

    I Basic Shell programminghttp://www.shellscript.sh/index.html

    Day_01_example.sh:#!/bin/bash

    # Example on:# how to use Emacs for making a script# chmod, PATH, ls

    ls

    [email protected] 18

  • $ ls -ltr Day_01_example.sh$ chmod a+x Day_01_example.sh$ ls -ltr Day_01_example.sh$ ./Day_01_example.sh

    [email protected] 19

  • Recap, another Shell script

    Day_02_example.sh:1 #!/bin/bash2

    3 # Example on:4 # bash shell programming and using emacs5 # bash if-statement in script6

    7 # see Tables 7-1 and 7-2 of Ref[3] for details8 if [ -f ~/.bashrc ]; then . ~/.bashrc; fi9

    10 rm *.old *~11

    12 # make copies of all files in a directory13 LIST=‘ls‘

    [email protected] 20

  • Recap, another Shell script

    Day_02_example.sh:14 for i in $LIST; do15 ORIG=$i16 DEST=$i.old17 cp $ORIG $DEST18 echo "copied $i"19 done20

    21 exit 0Execute it with the following commands

    $ chmod a+x Day_02_example.sh$ ./Day_02_example.sh

    [email protected] 21

  • C/C++ Basics

    C/C++* Functions, Modularity* Controling executions, for, if-else, while, statements etc* Data types, structure, pointerNotes:

    - Only data structures in C, no member functions- Variables declaration: struct StructureName variable1, var2;

    [email protected] 22

  • C programming example

    Day_03_example_1a.cc: modularity#include #include using namespace std;

    // function declarationschar cfunc(int i);void accelerate(void);void brake(void);void stop(string );

    int main() {cout > val;

    [email protected] 23

  • cout

  • char cfunc(int i) {if(i == 0)

    return ’a’;if(i == 1)

    return ’g’;if(i == 5)

    return ’z’;return ’c’;

    }

    [email protected] 25

  • void accelerate(void){cout

  • C++ Basics

    C++ only* Function overloading* References* Class, Object oriented, ....Class=new type similar to int, double etc but with more

    Class=structure with functions as membersObject=instance of a class, a variableAccess specifiers: private, public or protected

    The scope operator, ::Class defination, .h files

    Class implementation, .cpp files

    [email protected] 27

  • C++ programming example

    Day_03_example_1.cc: function call by reference// Thinking in C++, 2nd Ed. vol 1#include using namespace std;

    void f(int& r) {cout

  • cout

  • C++ Class example

    Day_03_example_2.cc: uses object of type RectangleDefined in Rectangle.h & Rectangle.cc#include #include "Rectangle.h"

    using namespace std;

    int main () {int width = 3, hight = 4;Rectangle rect;rect.set_values (width, hight);cout

  • Rectangle.h:

    using namespace std;

    class Rectangle {public:void set_values (int,int);int area() {return width*height;}

    private:int width, height;

    };

    [email protected] 31

  • Rectangle.cc:

    #include "Rectangle.h"using namespace std;

    void Rectangle::set_values (int x, int y) {width = x;height = y;

    }

    [email protected] 32

  • C++ Class template basics

    Day_05_ClassTemplate.h: class template defination

    template class DataPair {

    T values [2];public:DataPair (T first, T second){

    values[0]=first; values[1]=second;}

    };

    The template class DataPair stores data pairs of any valid type.

    [email protected] 33

  • C++ Class template basics

    Day_05_ClassTemplate2.cc:#include using namespace std;

    template class DataPair {

    T a, b;public:

    DataPair (T first, T second){a=first; b=second;}T getmax ();

    };

    [email protected] 34

  • C++ Class template basics

    template T DataPair::getmax (){

    T retval;retval = a>b? a : b;return retval;

    }

    int main () {DataPair myobject (100, 75);cout

  • Intro. GNU Scientific Library

    (1) GSL is a numerical library for C and C++ programmershttps://www.gnu.org/software/gsl(2) GSL Reference Manual

    https://www.gnu.org/software/gsl/manual/html_node/index.html(3) Download and installation

    $ wget http://mirror.rackdc.com/gnu/gsl/gsl-2.2.tar.gz$ tar xzf gsl-2.2.tar.gz$ cd gsl-2.2$ echo "Make sure to read the README and INSTALL files"$ configure; make; make install

    (4) Example use: code, gslpp Library

    [email protected] 36

  • GNU simple example

    /* Computes the Bessel function J_0(x) for x=5 */#include #include

    int main(void){double x = 5.0;double y = gsl_sf_bessel_J0 (x);printf ("J0(%g) = %.18e\n", x, y);return 0;

    }// J0(5) = -1.775967713143382642e-01

    $ echo "compilation: next command should produce 20161010_gsl_nbee.o"$ gcc -Wall -I/usr/local/include -c 20161010_gsl_nbee.c$ echo "linking: next command should produce the executable a.out"$ gcc -L/usr/local/lib 20161010_gsl_nbee.o -lgsl -lgslcblas -lm

    The gslpp in Ex2c.tar.gz shows a C++ GSL [email protected] 37

  • Exercise 02

    Use the Terminal and/or Emacs for doing the exercises(a) Over the next few weeks, go through

    https://learnpythonthehardway.org/book/ex0.html(b) Ex2b.cpp: what to do is explained at the end of the .cpp file.

    (c) Ex2c.tar.gz, see the end of file MixedCcpp.c for instruction.

    [email protected] 38

  • Day 06 to 08

    Radioactive Decay,Random Numbers,Euler Method, and

    Projectiles

    [email protected] 39

  • Radioactive Decay

    http://schools.wikia.com/wiki/[email protected] 40

  • Radioactive Decay

    I N(t) = no. of 14C in a sample at time, t

    I Decay is governed by dN(t)dt = −N(t)τ , for large sample

    I For small sample, the decay is stochastic process.I Analytically, N(t) = N(0) e−t/τ ; decay “time-constant”= τ .

    I Task: solve [email protected] 41

  • Radioactive DecayRadioactiveDecay.c:

    1 // C programming example for use of2 // functions and pointers34 #include 5 #include 67 #define MAX 10089 #include "mheader.h"

    1011 int main(){12 double nsample[MAX]; /* no. of atoms in sample*/13 double t[MAX]; /* time values */14 double tau; /* half life of atom */15 double dt; /* time steps */1617 initialise(nsample, t, & tau, & dt);18 calculate_nt(nsample, t, & tau, & dt);19 store_result(nsample, t);2021 return 0;22 }2324 /* function for initialising variables */25 void initialise(double *nsamp, double *time, double *tau, double *delta_t){26 printf("Enter initial number of atoms: ");27 scanf("%lf", nsamp);28 printf("Enter time constant for the atom: ");29 scanf("%lf", tau);30 printf("Enter time step: ");31 scanf("%lf", delta_t);32 time[0] = 0.0;33 }

    [email protected] 42

  • Radioactive Decay

    RadioactiveDecay.c: continuation3435 /* Calculate the results and store in the arrays nsamp and t*/36 void calculate_nt(double *n_i, double *time, double *tau, double *delta_t){37 int i;38 for(i=0; i < MAX-1; i++){39 n_i[i+1] = n_i[i] - (n_i[i]/(*tau)) * *delta_t;40 time[i+1] = time[i] + *delta_t;41 }42 }4344 /* save the result to a file*/45 void store_result(double *n_i, double *time){4647 FILE *file_out;48 int i;4950 file_out = fopen("RadioActiveDecay.txt", "w");51 for(i=0; i < MAX; i++){52 fprintf(file_out, "%g %g\n", time[i], n_i[i]);53 }54 fclose(file_out);55 }56 // gnuplot57 // plot "RadioActiveDecay.txt" using 1:258 //

    [email protected] 43

  • Radioactive Decay

    rand.c:1 #include 2 #include 3 int main(){4 int i, n = 1;5 /* Intializes random number generator */6 srand(time(0));7 /* Print 5 random numbers from 0 to 1 */8 for( i = 0 ; i < n ; i++ )9 printf("%f\n", (float) rand()/RAND_MAX);

    1011 return 0;12 }

    [email protected] 44

  • Radioactive Decay

    rand.cpp:1 #include 2 #include 34 int main(){56 // use time(0) as seed for the pseudo-random no. gen.tor7 std::default_random_engine generator(time(0));89 std::uniform_int_distribution distribution(1,5);

    10 // std::uniform_real_distribution1112 std::cout

  • Radioactive Decay, Intro C++ Class

    EulerMethod.h:1 // Class for Euler Method2 class EulerMethod {3 protected:4 double zaman_akher, dlt, zaman;5 double xip1;6 double (*dfdt)(double );7 public:8 EulerMethod();9 ~EulerMethod();

    10 void initialise(double *ic, double (*dfdt_in)(double )); // can be done using the constructorfunction but accept as is for this example↪→

    11 // void initialise(double *, double (*)(double *));12 void solve();13 };

    [email protected] 46

  • Radioactive Decay, Intro C++ ClassEulerMethod.cpp:

    1 #include "EulerMethod.h"2 #include 34 using namespace std;56 EulerMethod::EulerMethod(){}7 EulerMethod::~EulerMethod(){}89 void EulerMethod::initialise(double *ic, double (*dfdt_in)(double ))

    10 {11 zaman_akher = *ic;12 dlt = *(ic + 1);13 zaman = 0.0;14 xip1 = *(ic + 2);15 dfdt = dfdt_in;16 }1718 void EulerMethod::solve()19 {20 fstream file("EulerMethod.dat", ios::out);2122 file

  • Radioactive Decay, Intro C++ ClassRadioActiveDecay.cpp

    1 /*2 g++ -c -I EulerMethod.cpp;3 g++ -c -I. 20161029_RadioActiveDecay.cpp;4 g++ EulerMethod.o 20161029_RadioActiveDecay.o;5 ./a.out;6 gnuplot file.gnuplot;7 display RD.png8 rm a.out *.o *~ EulerMethod.dat9 */

    1011 #include "EulerMethod.h"1213 // constants and initial conditions:14 double N0 = 1.0e4, halflife = 0.0005, dlt = halflife/100.0, Tend = 500*dlt;15 double dndt(double);1617 int main(){18 //make an array from the constants and initial conditions19 double initc[]={Tend, dlt, N0};20 EulerMethod rdecay;2122 rdecay.initialise(initc, dndt);23 rdecay.solve();2425 return 0;26 }2728 double dndt(double ni){29 return -1.0 * ni / halflife ;30 }

    [email protected] 48

  • Radioactive Decay, Intro C++ Class

    Figure : Numerical solution using Euler method, number of nuclei N(t) against time t.N(t = 0) = 10000, τ = 0.0005.

    [email protected] 49

  • Day 09 to 10 + 2 days make-up

    Simple Harmonic MotionComputing Surgery, Programming

    Errors-Solving SessionGNU plot

    [email protected] 50

  • Pendulum

    1 #include 2 int main(){34 double thip1=0.2, wip1=0.0, dlt=0.04, zaman=0, tmp;5 int i;6 FILE *file = fopen("pendulum", "w");78 fprintf(file, "%e %e\n", zaman, thip1);9 for(i=0; i

  • Pendulum

    Figure : The displacement and speed with time of a simple pendulum.

    [email protected] 52

  • Days 13 to 19 + 2 days extra

    Physics-to-programming approach2x computing surgery sessions

    Euler-Cromer Method1- & 2-body Solar System Examples

    EulerMethod.h designpointer to functions, Inheritance

    Review, Q&A sessionsMid-term exams

    [email protected] 53

  • Sun-Earth-Jupiter

    1 // g++ 20161024_SunEarthJupiter.cpp; ./a.out; gnuplot file.gnuplot; display SunEarthJupiter.png2 #include 3 #include 4 #include 56 using namespace std;7 double sq(double x){ return x*x; }89 int main(){

    10 // Try with the following: factor = 1.0, 1000.0 and zaman_akher=12.0, 3.0 repectively11 double factor = 1.0;12 double ppii = 4.0*atan(1.0); //constants13 double mjM = factor * 1.9e27/2.0e30; // mass_jupiter/mass_sun14 double meM = 6.e24/2.0e30; // mass_earth/mass_sun1516 double dlt=0.002, zaman=0.0, zaman_akher=12.0; // time is measured in years17 double xip1=1.0, yip1=0.0, vxip1=0.0, vyip1=2.0*ppii; //earth initial cordinates18 double jxip1=5.2, jyip1=0.0, jvxip1=0.0, jvyip1=2.0*ppii*5.2/12.0; //jupiter initial

    cordinates↪→1920 fstream file("SunEarthJupiter.dat", ios::out);21 file

  • Sun-Earth-Jupiter

    28 double rjqube = rj2*sqrt(rj2);2930 double rej2 = sq(xip1 - jxip1) + sq(yip1 - jyip1);31 double rejqube = rej2*sqrt(rej2);3233 // velocity for earth34 vxip1 = vxip1 - 4.0*sq(ppii)*xip1*dlt/reqube - 4.0*sq(ppii)*mjM*dlt*(xip1 - jxip1)/rejqube;35 vyip1 = vyip1 - 4.0*sq(ppii)*yip1*dlt/reqube - 4.0*sq(ppii)*mjM*dlt*(yip1 - jyip1)/rejqube;3637 // velocity for jupiter38 jvxip1 = jvxip1 - 4.0*sq(ppii)*jxip1*dlt/rjqube - 4.0*sq(ppii)*meM*dlt*(jxip1 -

    xip1)/rejqube;↪→39 jvyip1 = jvyip1 - 4.0*sq(ppii)*jyip1*dlt/rjqube - 4.0*sq(ppii)*meM*dlt*(jyip1 -

    yip1)/rejqube;↪→4041 xip1 = xip1 + vxip1*dlt; // position coordinates for earth42 yip1 = yip1 + vyip1*dlt;4344 jxip1 = jxip1 + jvxip1*dlt; // position coordinates for jupiter45 jyip1 = jyip1 + jvyip1*dlt;4647 zaman = zaman + dlt;48 file

  • Sun-Earth-Jupiter

    Figure : The Sun-Earth-Jupiter system.

    [email protected] 56

  • Intro C++ Inheritance

    EulerCromerMethod.h:1 // Class for Euler-Cromer Method2 // shows a simple example of inheritance3 #include "EulerMethod.h"45 class EulerCromerMethod : public EulerMethod {6 double vip1;7 public:8 EulerCromerMethod(double *, double (*dxdt_in)(double *));9 void solve(const char *);

    10 };

    [email protected] 57

  • Intro C++ InheritanceEulerCromerMethod.cpp:

    1 #include "EulerCromerMethod.h"2 #include 34 using namespace std;56 EulerCromerMethod::EulerCromerMethod(double *ic, double (*dxdt_in)(double *))7 : EulerMethod(ic, dxdt_in) //, vip1( *(ic + 3) )8 {9 vip1 = *(ic + 3);

    10 }1112 void EulerCromerMethod::solve(const char * outf)13 {14 // extra: check if initialised, else initialise it here15 fstream file(outf, ios::out);1617 file

  • Intro C++ InheritanceProjectile.cpp:

    1 /*2 g++ -c -I. EulerMethod.cpp; \3 g++ -c -I. EulerCromerMethod.cpp; \4 g++ -c -I. 20161030_Projectile.cpp; \5 g++ EulerMethod.o EulerCromerMethod.o 20161030_Projectile.o;\6 ./a.out; gnuplot file.gnuplot; display projectile.png7 rm a.out *.o *~ *.dat8 */9

    10 #include "EulerCromerMethod.h"11 #include 1213 double d2xdt2_x(double *xi){ return 0.0; }14 double d2xdt2_y(double *xi){ double gravConst = 9.8; return - gravConst;}1516 int main(){17 // constants and initial conditions:18 double x0=0.0, y0=0.0, vx0=3.0/sqrt(2.0), vy0=vx0, dlt = 0.01, Tend = 60.0*dlt;19 //make an array from the constants and initial conditions20 double initc_x[]={Tend, dlt, x0, vx0};21 double initc_y[]={Tend, dlt, y0, vy0};2223 EulerCromerMethod projx(initc_x, d2xdt2_x);24 EulerCromerMethod projy(initc_y, d2xdt2_y);2526 projx.solve("projectileX.dat");27 projy.solve("projectiley.dat");28 return 0;29 }

    [email protected] 59

  • Intro C++ Inheritance, Projectile

    Figure : Trajectory of a projectile.

    [email protected] 60

  • Intro C++ Inheritance

    DamptPendulum.cpp:1 /*2 g++ -c -I. EulerMethod.cpp; \3 g++ -c -I. EulerCromerMethod.cpp; \4 g++ -c -I. 20161030_DamptPendulum.cpp; \5 g++ EulerMethod.o EulerCromerMethod.o 20161030_DamptPendulum.o;\6 ./a.out; gnuplot file.gnuplot; display pendulum.png7 rm a.out *.o *~ ECM.dat8 */9 #include "EulerCromerMethod.h"

    1011 double thip1=0.2, wip1=0.001, dlt=0.04, Tend = 500*dlt; // constants and initial conditions:12 double dxdt(double *);1314 int main(){15 //make an array from the constants and initial conditions16 double initc[]={Tend, dlt, thip1, wip1};1718 EulerCromerMethod x(initc, dxdt);19 x.solve("damped_pendulum.dat");2021 return 0;22 }2324 double dxdt(double *xi){25 double gravConst = 9.8, qConst = 1.0, mass = 1.0;26 return -gravConst * *xi / 1.0 - qConst * *(xi + 1)/mass;27 }

    [email protected] 61

  • Intro C++ Inheritance, Dampted Pendulum

    Figure : Damped pendulum.

    [email protected] 62

  • Session 20

    Mid-term exams

    INGExams I: 1395/08/17 10:30Answer all questions

    (1) Use Euler-Cromer method to numerically solve for theSun-Earth-Jupiter system. Asumming the Sun is stationary at theorigin of a cartesian cordinate system. You should use Newton’slaws in x- and y-directions for deriving the equations of motion.Then write a C/C++ program for solving the equations of motions.For simplicity, assume all constants to be equal to one.(2) Show why Euler method is not good for solving a simpleharmonic pendulum system.

    [email protected] 63

  • Session 21

    Introduction:Chaotic, non-linearity or complex systems;a Rialistic, Damped & Driven Pendulum

    Example

    Reference:Chapter 3, “Computational Physics” by Nicholas J. Giordano andHisao Nakanishi.

    [email protected] 64

  • Rialistic, Damped & Driven Pendulum

    The equation of motion is:

    d2θdt2

    = −g`sin θ − q dθ

    dt+ FD sinΩDt (1)

    Using Euler-Cromer method, solve for θ(t) and ω(t) using thefollowing constants and initial conditions:

    {∆t = 0.04,TEND = 60, g = ` = 9.8, q = 0.5,ΩD = 2/3} (2)

    Make the plots for FD = 0.0, 0.5 and 1.2. To keep θ ∈ [−π, π],add or subtract 2π respectively when θ < −π or θ > π.

    [email protected] 65

  • Rialistic, Damped & Driven Pendulum

    Figure : Visualisation of the realistic pendulum behaviour transiting to a chaotic phase as one of thesystem parameters is varied. First raw: θ(t) for FD = 0.0, 0.5 and 1.2. Second raw: correspondingphase-space (θ(t), ω(t)) diagrams.

    [email protected] 66

  • Rialistic, Damped & Driven Pendulum

    θ(t) reach steady state with time. Except for FD = 1.2 with whichthe pattern never repeats. The behaviour/pattern is notpredictable. It however reveals a deterministic structure in phasespace:

    Figure : The phase-space (θ(t), ω(t)) diagram for FD = 1.2 at points in phase with the drivingforce, ΩD t = 2nπ. Here n is an integer and the phase requirement is taken within ∆t.

    For a wide range of initial conditions, this phase-space trajectoryremains the same. This is known as an strange attractor and havea fractal [email protected] 67

  • Rialistic, Damped & Driven Pendulum

    Chaos: deterministic, unpredictable systems. No complete theory(yet?) Chaotic behaviour are not random, can be described by“strange attractors” – fractal objects.

    Visual apprehension for the transitionto chaos can be achieved viabifurcation diagrams. ForFD ∈ [1.30, 1.47], compute θ(t) afterwaiting 300 driving periods to allowfor transient solutions to decay. Thenplot (FD , θ) at times in phase withthe driving force, up to the 300driving periods. Figure : (θ(t), FD ) bifurcation diagram.

    Other example systems that can be chaotic include: Logistic mapssystem and the Lorenz (weather) model.

    [email protected] 68

  • Hands-on group practicals & announcements

    Computing practicals:Write the programs for making plots in the above Figures.

    Announcements:(1) Projects submission(2) ICTP-Univ.Sharif ScientificComputing Workshop,http://indico.ictp.it/event/7952

    Figure : ICTP-Univ.Sharif ScientificComputing Workshop, call for applications. Seehttp:indico.ictp.itevent7952

    [email protected] 69

  • Sessions 22-25

    - Chaos & Fourier Transforms (FT),- Discrete FT in GSL & FFTW packages,- Runge-Kutta Implementations in GSL,- Make & Makefile,

    [email protected] 70

  • Chaos & Fourier Transforms

    - θ(t) for chaotic pendulum considered a time-dependent signal- Decompose complex θ(t) to component sinusoidal wave forms- Tools for decomposition rely on Fourier Transforms- Components power spectrum reveals “noise in chaos”

    Express θ(t) as superposition of sinusoidal components:

    θ(t) =∫ +∞−∞

    dω Y (ω)e iωt√2π

    (3)

    where Y (ω) is the Fourier transform of θ(t),

    Y (ω) =∫ +∞−∞

    dte−iωt√2π

    θ(t) (4)

    measures “amount” of frequencies in θ(t). |Y (ω)|2 versus ω powerspectrum reveals strengths of θ(t) component frequencies.

    [email protected] 71

  • Discrete FT in GSL & FFTW packages

    For numerical implementation, the Fourier transform integral haveto be discretrised. In practice θ(t) is measured at finite N points intime:

    θj(tj) from t0 = 0 to tN = N ∆t, tj = j∆t. (5)

    For non-repeating θ(t), the period T is chosen to be T = tN withθ(t + T ) = θ(t). Therefore Discrete FT is an approximation.

    Lowest frequency component in Fourier representation of θ(t) isω1 =

    2πT . The other frequencies are

    ωj =2πjT, j = 2, 3, . . . ,N (6)

    DFT computations can be reduced using fast Fourier transform(FFT) algorithms (implemented in GSL and FFTW packages, forinstance).

    [email protected] 72

  • GNU Scientific Library Installation

    Get location address for most recent GSL version fromftp://ftp.gnu.org/gnu/gsl. Download and install it as follows:$ wget ftp://ftp.gnu.org/gnu/gsl/gsl-2.2.tar.gz

    $ tar xzf gsl-2.2.tar.gz

    $ cd gsl-2.2

    $ ./configure --help

    $ ./configure

    $ make

    $ sudo make install

    Check for where GSL is installed:$ which gsl-config

    $ gsl-config

    $ gsl-config --cflags

    $ gsl-config --libs

    [email protected] 73

  • GSL Usage Example (1), Discrete Fourier Transform

    To use GSL’s Fast Fourier Transform functions,read gsl-ref info or GSL Reference Manual, chapter 16:$ info gsl-ref

    https://www.gnu.org/software/gsl/manual/gsl-ref.pdf

    GSL header file where the functions needed are declared must beincluded

    #include

    To perform the FFT, call the following 3 functionswork = gsl_fft_real_workspace_alloc (n);// the algorithm require additional working space to hold intermediate steps of thetransform.↪→

    real = gsl_fft_real_wavetable_alloc (n);// factorises the length n into n = f_1*f_2*...*f_i*.... such that no. of operations madeis of order n \sum_i f_i instead of order n^2.↪→

    gsl_fft_real_transform (data, 1, n, real, work);// executes the GSL FFT algorithm. Here stride=1. "stride" parameter allows to performtransforms on ’z[stride*i]’ instead of ’z[i]’. the output, data is in a "packed arrays"format: the real and imaginary are placed in alternate neighboring elements.

    ↪→↪→

    Let’s apply these to the chaotic pendulum θ(t)

    [email protected] 74

  • GSL Usage Example (1), Discrete Fourier Transform1 #include 2 #include 3 using namespace std;4 int main(){5 int i, n = 1500; // n = Tend/delta_t6 double tend = 60.0; // Tend used for computing theta(t)7 double data[n];8 ofstream fileout("powerspectrum");9 ifstream inputfile("theta_of_time");

    10 gsl_fft_real_wavetable * real;11 gsl_fft_real_workspace * work;1213 work = gsl_fft_real_workspace_alloc (n);14 real = gsl_fft_real_wavetable_alloc (n);1516 for(i = 0; i < n; i++){ // read the data, theta(t)17 inputfile >> data[i];18 }19 inputfile.close();2021 gsl_fft_real_transform (data, 1, n, real, work);2223 for (i = 0; i < n/2; i++){24 fileout

  • GSL Usage Example (1), Discrete Fourier Transform

    Compilation and linking the example program above, do:$ gsl-config --cflags

    $ gsl-config --libs

    $ g++ -I/usr/local/include gslffteg.cpp -L/usr/local/lib -lgsl -lgslcblas -lm

    $ ./a.out

    $ gnuplot file.gnuplot

    $ display powerspectrum.png

    file.gnuplot has commands for plotting the θ(t) Power Spectrum1 # www.gnuplot.info/2 # http://gnuplot.sourceforge.net/docs_4.0/gpcard.pdf3 set term png4 set output "powerspectrum.png"5 set xrange [0:20]6 plot "powerspectrum" using 1:2 title "chaotic pendulum PS" w l7 #set output "SunEarthJupiter.png"8 #plot "SunEarthJupiter.dat" u 1:2, "SunEarthJupiter.dat" u 3:4 t "Solar"9 set term x11

    The Power Spectrum for θ(t) is shown in the next page. It revealsa “noisy” aspect in the chaotic pendulum [email protected] 76

  • GSL Usage Example (1), Discrete Fourier Transform

    Figure : example power spectrum for chaotic pendulum with FD = 1.2 computed using the FFTalgorithm in GSL. Similar plot can be obtained using the FFTW package.

    Similar result can be obtained by using FFTW package. Seehttp://www.fftw.org/

    [email protected] 77

  • GSL Usage Example (2), Solving ODE

    Read gsl-ref info or GSL Reference Manual, chapter 27. ExpressODEs of any order as n simultaneous 1st-order ODEs:

    dyidt

    = fi (t, y1(t), y2(t), . . . , yn(t)), i = 1, 2, . . . , n (7)

    For fi , no dependence on dyidt is allowed. fi and, in some cases, theJacobian Jij = dfidyj are input to the GSL solvers defined ingsl_odeiv2.h.

    Set the ODEs using the data type gsl_odeiv2_systemtypedef struct{

    int (*function) (double t, const double y[], double dydt[], void *params);int (*jacobian) (double t, const double y[], double *dfdy, double dfdt[], void *params);size_t dimension;void *params;

    }gsl_odeiv2_system;

    [email protected] 78

  • GSL Usage Example (2), Solving ODE

    The pointer-to-function function stores fi (t, y1(t), y2(t), . . . , yn(t))in the array dydt[] for arguments t, y[] and parameters params.

    The jacobian stores dfidt in the array dfdt[] and the Jacobian matrixJij in the array dfdy in the form J(i,j) = [i*dimension+j].

    Stepping algorithms advance solution from t to t + ∆t. Forexample, we consider the Runge-Kutta method. GSL allows foradaptive step-size control instead of fixed time-steps.

    GSL driver object gsl_odeiv2_driver implements an algorithm forsolving ODE by combining the evolution, control and stepperobjects.

    [email protected] 79

  • GSL Usage Example (2), Solving ODEtypedef struct gsl_odeiv2_driver_struct gsl_odeiv2_driver;

    struct gsl_odeiv2_driver_struct{

    const gsl_odeiv2_system *sys; /* ODE system */gsl_odeiv2_step *s; /* stepper object */gsl_odeiv2_control *c; /* control object */gsl_odeiv2_evolve *e; /* evolve object */double h; /* step size */double hmin; /* minimum step size allowed */double hmax; /* maximum step size allowed */unsigned long int n; /* number of steps taken */unsigned long int nmax; /* Maximum number of steps allowed */

    };

    Consider the DE of an oscillator with non-linear damping

    d2θdt2

    = −θ + q(1− θ2)dθdt

    (8)

    This 2nd-order ODE can be reduced to 2-dimensional 1st-orderdθdt

    = ω y1 = θ,f1(t, y1, y2) = y2 (9)

    dωdt

    = −θ + q(1− θ2)ω, y2 = ω,f2(t, y1, y2) = −y1 + q(1− y21 )y2(10)

    [email protected] 80

  • GSL Usage Example (2), Solving ODEhttps://www.gnu.org/software/gsl/manual/html_node/ODE-Example-programs.html

    1 #include "gslode.h"23 int main(){4 double mu = 10;5 gsl_odeiv2_system sys = {func, jac, 2, &mu};67 gsl_odeiv2_driver * d =8 gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk8pd,1e-6, 1e-6, 0.0);9 // allocates and initialises the evolve, control and stepper objects

    10 // for sys, using stepper type .._rk8pd, with initial step size 1e-611 // last two argurements for stepsize error control: chapter 27.3 gsl-ref1213 int i;14 double t = 0.0, t1 = 100.0;15 double y[2] = { 1.0, 0.0 };1617 printf("%.5e %.5e %.5e\n", t, y[0], y[1]);18 for(i = 1; i

  • GSL Usage Example (2), Solving ODE

    File gslode.h:1 #include 2 #include 3 #include 4 #include 56 int func(double t, const double y[], double f[], void *params){7 (void)(t); /* avoid unused parameter warning */8 double mu = *(double *)params;9 f[0] = y[1];

    10 f[1] = -y[0] + mu*y[1]*(1.0 - y[0]*y[0]);11 return GSL_SUCCESS;12 }1314 int jac(double t, const double y[], double *dfdy, double dfdt[], void *params){15 (void)(t); /* avoid unused parameter warning */16 double mu = *(double *)params;17 gsl_matrix_view dfdy_mat = gsl_matrix_view_array(dfdy, 2, 2);18 // returns a matrix view of the array dfdy.19 gsl_matrix * m = &dfdy_mat.matrix;20 gsl_matrix_set(m, 0, 0, 0.0);21 gsl_matrix_set(m, 0, 1, 1.0);22 gsl_matrix_set(m, 1, 0, -2.0*mu*y[0]*y[1] - 1.0);23 gsl_matrix_set(m, 1, 1, -mu*(y[0]*y[0] - 1.0));24 dfdt[0] = 0.0;25 dfdt[1] = 0.0;26 return GSL_SUCCESS;27 }

    [email protected] 82

  • GSL Usage Example (2), Solving ODE

    Figure : Phase space diagram for an oscillation with an θ-dependent damping term as described bythe system eq.(8) with q = 0.5, 1.0, . . . , 4.0 solved using GSL’s Runge-Kutta-4 stepper function.

    Read about the various GSL stepper type,GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rk2;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rk4;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rkf45;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rkck;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rk8pd;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rk2imp;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rk4imp;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_bsimp;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_rk1imp;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_msadams;GSL_VAR const gsl_odeiv2_step_type *gsl_odeiv2_step_msbdf;

    [email protected] 83

  • GSL Usage Example (3), Vectors & Matricesusing namespace gslpp;

    complex z3; // gslpp_complex.hvector vd1(3, 3.); //gslpp_vector_double.hvector vc1(3, 3. + complex::i()); // gslpp_vector_complex.h

    // gslpp_matrix_double.h, gslpp_matrix_complex.hstd::cout

  • GSL Usage Example (3), Vectors & Matrices1 #include 2 #include 3 #include 4 extern "C" int setSCKM(double AUr[3][3], double AUc[3][3]);56 int setSCKM(double AUr[3][3], double AUc[3][3]){7 // here, intentionally, we are using non portable GNU extension8 // see https://gcc.gnu.org/onlinedocs/gcc/Complex.html9 __complex__ double thevckm[3][3]

    10 = {{0.97384+0.0i, 0.22719+0.0i, 0.00205-0.00315i},11 {-0.22708-0.00014i, 0.97302+0.00002i, 0.04077+0.0i },12 {0.00727-0.00307i, -0.04017-0.00072i, 0.99916+0.0i }};1314 gslpp::matrix theckm(3,3,0.);15 gslpp::matrix TUhat(3,3,0.);1617 for (int indi = 0; indi < 3; indi++)18 for (int indj = 0; indj < 3; indj++ ){19 theckm.assignre(indi,indj, __real__ thevckm[indi][indj]);20 theckm.assignim(indi,indj, __imag__ thevckm[indi][indj]);21 TUhat.assignre(indi,indj, AUr[indi][indj] );22 TUhat.assignim(indi,indj, AUc[indi][indj] );23 }24 TUhat = sqrt(2.) * TUhat * theckm.hconjugate();25 for (int indi = 0; indi < 3; indi++)26 for (int indj = 0; indj < 3; indj++ ){27 AUr[indi][indj] = TUhat(indi, indj).real();28 AUc[indi][indj] = TUhat(indi, indj).imag();29 }30 return 0;31 }

    [email protected] 85

  • GSL Usage Example (3), Vectors & Matrices

    1 extern "C" int setSCKM(double AUr[3][3], double AUc[3][3]);23 int main(int argc, char *argv[]){4 double AUr[3][3], AUc[3][3];5 int indi, indj;6 // here, intentionally, we are using non portable GNU extension7 // see https://gcc.gnu.org/onlinedocs/gcc/Complex.html8 __complex__ double TUIN[3][3]9 = {{0.+0.0i, 0.+0.0i, 0.0-0.0i},

    10 {0.+0.0i, 0.+0.0i, 0.0-0.0i},11 {-1.58727498e+01 + 4.35197986e+01i, 1.63703148e+02 - 1.69765236e+02i, -5.18859604e+03 +

    6.45109210e+03i}};↪→1213 for (indi = 0; indi < 3; indi++)14 for (indj = 0; indj < 3; indj++ ){15 AUr[indi][indj] = __real__ TUIN[indi][indj];16 AUc[indi][indj] = __imag__ TUIN[indi][indj];17 }18 setSCKM(AUr, AUc); // 3 by 3 matrix AU is transformed: AU --> sqrt(2.0) * AU * VCKM^\dagger19 return 0;20 }

    [email protected] 86

  • GSL Usage Example (3), Vectors & Matrices

    #Download the golden C++ wrapper for GSL matrix and vector classes gslpp.tgz#See what it can do, look at the test programs in gslpp/tests##To compile and run example, do:#

    g++ -I./gslpp/include -c gslpp/src/*.cpp

    g++ -I./gslpp/include -I/usr/local/include -c SetSCKM.cppg++ -c MixedCcpp.c

    g++ -o a.out SetSCKM.o MixedCcpp.o gslpp*.o -L/usr/local/lib -lgsl -lgslcblas

    ./a.out

    [email protected] 87

  • Make &Makefile

    make manages, primarily, the compilation(s) and linking(s) within aproject as instructed in a text file called makefile.

    Their could be chain of targets to be made within a project. Thesecan range from being executable files to the production ofpublication-level plots for an analysis.

    make can compress the series of compilations, linkings, and anyother commands into a single command for making the target fromsource codes.

    Re-executing make, after a target is made, only changed files andthose affected by the changes are recompiled. make automaticallykeeps track of files/targets dependencies.

    [email protected] 88

  • Make &Makefile

    make looks for a makefile in PWD. Otherwise use the “-f” flag:make -f nameOffile

    Inside a makefile, “#” at the begining of line indicates comment# a comment linesalam.x: salam.cppg++ salam.cpp -o salam.x

    target=hello.x depends on hello.cpp=source. If hello.cpp is newerin time than hello.x, then make executes the rule=g++ salam.cpp

    # a comment lineCPP = g++salam.x: salam.cpp$(CPP) salam.cpp -o salam.x

    Here “=” makes CPP a macro. The “$” and parenthesis in“$(CPP)” expands the macro. That is, replaces CPP with g++.

    [email protected] 89

  • Make &MakefileHere is a simple example for the chaotic pendulum codes:

    1 CC = g++2 CF = -c -I.3 objects = EulerMethod.o EulerCromerMethod.o 20161031_ChaoticPendulum.o45 all: a.out plot.png67 EulerMethod.o: EulerMethod.cpp EulerMethod.h8 $(CC) $(CF) EulerMethod.cpp9

    10 EulerCromerMethod.o: EulerCromerMethod.cpp EulerCromerMethod.h11 $(CC) $(CF) EulerCromerMethod.cpp1213 20161031_ChaoticPendulum.o: 20161031_ChaoticPendulum.cpp EulerCromerMethod.h EulerMethod.h14 $(CC) $(CF) 20161031_ChaoticPendulum.cpp1516 a.out: $(objects)17 $(CC) $(objects) -o a.out1819 plot.png: a.out chaotic_pendulumFD12.dat20 ./a.out; gnuplot file.gnuplot21 display chaotic_pendulumFD120.png &22 cp chaotic_pendulumFD120.png plot.png2324 clean:25 rm a.out *.o *~

    www.gnu.org/software/make/manual/make.pdf

    www.gnu.org/software/autoconf/autoconf.html

    autotools.io/index.html, www.cmake.org

    [email protected] 90

  • Make &MakefileA makefile for gslpp:

    1 CC = g++2 CF = -c -I./include3 SRC = src4 objects = gslpp_complex.o gslpp_matrix_double.o\5 gslpp_vector_double.o gslpp_matrix_complex.o\6 gslpp_vector_complex.o7 AR = ar r8 GSL_INC = -I/usr/local/include9 GSL_LINK = -L/usr/local/lib -lgsl -lgslcblas

    1011 all: libgslpp.a test.x1213 %.o: $(SRC)/%.cpp14 $(CC) $(CF) $< -o $@1516 libgslpp.a: $(objects)17 $(AR) $@ $^18 # $@ target name19 # $< 1st prerequisite or dependent source20 # $^ all prerequisites21 # see https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html2223 test.x: tests/newsimpletest.cpp libgslpp.a24 $(CC) $(GSL_INC) $(CF) $< -o test.o25 $(CC) test.o -L./ -lgslpp $(GSL_LINK) -o $@26 chmod +x $@2728 clean:29 rm *.o test.x

    [email protected] 91

  • Exercises partly started during lectures(0) [a] The HEPfit collaboration’s C++ wrapper for GNU Scientific Library (GSL) called gslpp isgiven for Project 02. Use this multiply a non-trivial 3-by-3 complex matrix with it’s inverse and print theresult. [b] Compile and run the numerical differentiation atwww.gnu.org/software/gsl/manual/html_node/Numerical-Differentiation.html

    (1) Use GSL package for numerically solving a simple projectile motion. Plot the result to show theprojectile’s path.

    (2) Using GSL package, and the example provided during lectures, compute the power spectrumfor the chaotic pendulum system with FD = 1.2, 1.45 and 1.45.

    (3) Download and install FFTW. Use it for computing the power spectrum as in (1). You may use:int n, nout;double *in;complex *out;fftw_plan p;//....nout = ceil(n/2.0);// Create a FFTW plan for a 1D DFT with real input and complex outputin = (double*) fftw_malloc(sizeof(double) * n);out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * nout);p = fftw_plan_dft_r2c_1d(n, in, out, FFTW_ESTIMATE);//....// after creating the plan, then read the theta(t) values// from a file and into the array in[n].// ....// then perform the discrete Frourier transformfftw_execute(p);// print the power spectrumsqrt( sq(creal(out[idx])/n) + sq(cimag(out[idx])/n) );

    See http://www.fftw.org/fftw3_doc/One_002dDimensional-DFTs-of-Real-Data.html

    [email protected] 92

  • Sessions 26-28

    - Numerical Differentiation- Numerical Integration,- Monte Carlo Simulations:- Random Walks & Ising Model

    [email protected] 93

  • Numerical Differentiation

    Differentiation as a limit:

    dy(t)dt

    = lim∆t→0

    y(t + ∆t)− y(t)∆t

    (11)

    is not good for computing differentiation numerically or from atable of numbers. The numerator fluctuates between 0 and thecomputer’s precision �.

    Given a table of projectile positions y(t) taken at various time tduring it’s flight, the velocity can be estimated by the forwarddifference approximation via Taylor series:

    y(t + ∆t) = y(t) + ∆tdy(t)

    dt+

    (∆t)2

    2!d2y(t)

    dt2+ . . . (12)

    dydt

    ∣∣∣fd≈ y(t + ∆t)− y(t)

    ∆t=

    dy(t)dt

    +(∆t)2

    d2y(t)dt2

    + . . . (13)

    [email protected] 94

  • Numerical Differentiation

    Order (∆t)2 error improvement is obtained via a central differencemethod which makes half-step forward and half-step backward

    dydt

    ∣∣∣cd≈ y(t + ∆t/2)− y(t −∆t/2)

    ∆t(14)

    In similar way, further improvement can be obtained withquarter-steps.

    GSL functions for numerical differentiation are defined ingsl_deriv.h. See GSL reference manual, Chapter 29.

    int gsl_deriv_central(const gsl_function * f, double x, double h, double * result, double * abserr)

    int gsl_deriv_forward(const gsl_function * f, double x, double h, double * result, double * abserr)

    Differentiation and searching, & application to eigenvalue problemsPartial differential equations

    [email protected] 95

  • Numerical Integration

    The integral of f (x) from a to b is the area under the curve f (x) tothe x-axis. It can be approximated as sum of the area of nrectangular panels with width ∆x = (b − a)/n:∫ b

    af (x) dx ≈

    n−1∑i=0

    f (xi ) ∆x . (15)

    Using trapeziodal panels instead, get a better estimate:∫ ba

    f (x) dx ≈n−1∑i=1

    f (xi ) ∆x + [f (a) + f (b)] ∆x/2. (16)

    Instead of using equal-width panels, even better approximations canbe achieved by better approximation of the panels’ upper edges.

    [email protected] 96

  • Numerical Integration

    Figure : Integration algorithms using Trapezoid (dashed lines) or Simpson’s (dotted line) rule.

    Consider n + 1 evenly-spaced points xi , i = 0, 1, 2, . . . , n over theintegration region [a, b]. These make n panels of width ∆x = b−ansuch that xi = a + i ∆x .

    Trapezoid:∫ xi+1

    xif (x) dx ≈ ∆x

    2fi +

    ∆x2

    fi+1 (17)

    The integral is thus approximated as weighted sum over values ofthe function at two [email protected] 97

  • Numerical Integration

    Simpson’r rule employs a parabolic approximate to f (x):f (x) ≈ αx2 + βx + γ such that∫ xi+1

    xif (x) dx ≈

    (αx3

    3+βx2

    2+ γx

    ) ∣∣∣xi+1xi

    (18)

    For relating α, β, γ to the integrand, f (x), consider the interval −1to +1 for which it can be shown that∫ +1

    −1αx2 + βx + γ dx =

    f (−1)3

    +4f (0)3

    +f (1)3. (19)

    That is, the integration is approximated as weighted sum overvalues of the function at three points. This generalises to:

    [email protected] 98

  • Numerical Integration

    ∫ xi+1xi−1

    f (x) dx =∫ xi

    xi−1f (x) dx +

    ∫ xi+1xi

    f (x) dx (20)

    ≈ ∆x3

    fi−1 +4∆x3

    fi +∆x3

    fi+1. (21)

    The integration is over pairs of panels as such total number ofpanels (n − 1) should be an even number.

    There are other higher orders algorithms such as Gaussianquadrature. Here we move next to the integration via Monte Carlomethod.

    [email protected] 99

  • Monte Carlo Integration

    - Compute π by estimating area ofinscribed circle- Area under a curve from ratio ofrandom points that fall below it.- Simplest Monte Carlo integrationalgorith = finding average of f (x)over region of interest〈f 〉 = 1n

    ∑ni=1 fi Figure : Computing area under f (x) within

    interval x ∈ [−1, 1] by using random numbers.

    By generating n points randomly in [a, b] then∫ ba

    f (x) dx ≈ b − an

    n∑i=1

    f (xi )±σf√n, σf =

    〈f 2〉− 〈f 〉2 (22)

    [email protected] 100

  • Monte Carlo Integration1 #include 2 #include 3 #include 4 using namespace std;56 double a = -1.0, b = 1.0;78 double f(double x){ return 1.0 - x*x; }9

    10 double trapezoid(double f(double), double xi, double xip1){11 return (xip1 - xi) * (f(xi) + f(xip1))/2.0;12 }1314 double simpson(double f(double), double xi, double xip2){15 double xip1 = xi + (xip2 - xi)/2.0;16 return (xip1 - xi) * (f(xi) + 4.0 * f(xip1) + f(xip2)) / 3.0;17 }1819 double montecarlo(double f(double), double a, double b, int n){20 double integ = 0.0;21 double random_0_to_1;22 srand(time(0));23 for (int i = 0; i < n; i++) {24 random_0_to_1 = (double) rand()/RAND_MAX;25 double xi = a + (b - a) * random_0_to_1;26 integ = integ + f(xi);27 }28 integ = integ * (b - a) / double(n);29 return integ;30 }3132

    [email protected] 101

  • Monte Carlo Integration33 int main(){34 int n=100001, i;35 double integ = 0.0;36 double xi, xip1, xip2;37 double deltax = (b - a) / double(n);3839 cout

  • Monte Carlo Simulations

    Aim in Monte Carlo Methods1: to generate samples {x (r)}Rr=1 froma probability distribution P(x). The target density, P(x), could bea distribution from statistical physics or some conditionaldistribution from data modelling, e.g. the posterior probability of amodel’s parameters given a set of observed data.

    Given a generated sample, an estimate for the expectation value ofan observable Φ(x) = 〈φ(x)〉 ≡

    ∫dnx P(x)φ(x), can be computed

    as:Φ̂ =

    1R

    ∑r

    φ(x (r)). (23)

    Variance Φ̂, σ2

    R , decreases with number of samples R , independentof dimensionality. Here σ2 =

    ∫dnx P(x) (φ(x)− Φ)2

    1DJC MacKay, “Introduction to monte carlo methods”, Learning in graphical models, 1998 -Springer.

    [email protected] 103

  • Monte Carlo Methods Vs. Simulations

    Monte Carlo Methods: Drawing samples, especially fromhigh-dimensions is a difficult task. Some ways for doing that:uniform sampling, importance sampling, rejection sampling andMetropolis algorithm.

    Monte Carlo Simulations: We have already played with randonnumbers, for simulating physics behaviours/properties, inradioactive decay and numerical integration studies

    Here: give two more examples(1) One-dimensional random walk(2) Ising model MC simulation

    [email protected] 104

  • Monte Carlo, Random WalkConsider an equally spaced lattice points on a line. At each point awalker can move right or left s = ±1 at equal probabilities.

    Walker’s position, xn, after n steps is xn =∑n

    i=1 si which averagesto zero over a large number of steps. But

    〈x2n〉

    = n.

    An algorithm: generate a random number r between 0 and 1 andthen compare the generated values to 0.5. If r < 0.5 the walkermoves left (s = −1). If r > 0.5, it moves right (s = +1).

    To write a code:- seed a random number generator- declare a file where to save xn and < x2n >- loop over number of walkers; another loop over number of steps- inner loop: perform the Monte Carlo algorith above- after both loops, compute the averages and print to file

    [email protected] 105

  • Monte Carlo, Random Walk

    Figure : The positions of 5 random walkers with time.

    [email protected] 106

  • Monte Carlo, Random Walk

    Figure : < x2n > versus the step numbers, averaged over 500 one dimensional random walkers.

    [email protected] 107

  • Ising Model

    Consider a 2D Ising lattice with points that can take spin-up orspin-down s` = ±1 where ` = 1, 2, . . . , L2 and L is the length of thelattice square.

    At a given microstate or configuration i , where i = 1, 2, . . . , n themagnetisation and energy is mi =

    ∑L2` s` and ei = −J

    ∑L2 s` sk

    respectively. For ei , the summation is over nearest neighbour spins< `k > and J > 0 (assume J = +1).

    Statistical mechanics: the probability pi of finding the system inmicrostate i is propotional to the Boltzman factor, pi ∼ e−ei/kBT .

    Macroscopic observables (e.g. energy, E & magnetisation, M) arethen measured as averages over the microstates that system visits.For instance M =

    ∑ni mi pi & E =

    ∑ni ei pi .

    [email protected] 108

  • Monte Carlo Simulation, Ising Model

    Consider a set of n = 64 Ising lattice points on a square grid(L = 8) with temperature T = 2.3.

    Initialise all spins to ±1, in a random manner.Loop-1: over the number of Monte Carlo sweeps to be takenLoop-2: over the L lattice rowsLoop-3: over the spins in a row for updating.

    I Compute the energy difference ∆E should a spin be flipped.I Generate a random r ∈ [0, 1]. If e−∆E/kBT > r flip the spin.

    After sweeps, save configuration energy and magnetisation in a file.

    [email protected] 109

  • Monte Carlo Simulation, Ising Model1 #include 2 #include 3 #include 4 #include 5 #define Lrows 146 #define Lcolumns 147 using namespace std;89 int spins[Lrows][Lcolumns], Nspins = Lrows * Lcolumns;

    10 double T = 2.1;1112 void InitialConfiguration(void);13 void ConfigurationObservs(double * results);14 void SpinFlip(void);1516 int main(int argc, char *argv[]){17 int Nsweeps = 100000;18 ofstream file("IsingModel.dat");19 double results[2];2021 srand(time(0));2223 InitialConfiguration();2425 // thermalise first26 for(int i = 0; i < (int)(0.3*Nsweeps); i++)27 for(int j = 0; j < Lrows*Lcolumns; j++) SpinFlip();2829 double e = 0.0, m = 0.0;30 double e2 = 0.0, m2 = 0.0;31 for(int i = 0; i < Nsweeps; i++){32 for(int j = 0; j < Lrows*Lcolumns; j++) SpinFlip();

    [email protected] 110

  • Monte Carlo Simulation, Ising Model33 ConfigurationObservs(results);34 file

  • Monte Carlo Simulation, Ising Model

    64 (spins[north][jcolumn] + spins[south][jcolumn] +65 spins[irow][left] + spins[irow][right] );6667 Magn += spins[irow][jcolumn];68 }69 results[0] = EnergySum/Nspins;70 results[1] = Magn/Nspins;7172 }7374 void SpinFlip(void){75 int irow = rand()%Lrows;76 int jcolumn = rand()%Lcolumns;77 // nearest neighbors with periodic boundary conditions78 int north = irow == 0 ? Lrows-1 : irow-1;79 int south = irow == Lrows-1 ? 0 : irow+1;80 int left = jcolumn == 0 ? Lcolumns-1 : jcolumn-1;81 int right = jcolumn == Lcolumns-1 ? 0 : jcolumn+1;8283 int neighbours =84 spins[north][jcolumn] + spins[south][jcolumn] +85 spins[irow][left] + spins[irow][right];8687 int energy_diff = 2.0*spins[irow][jcolumn] * neighbours;88 double rnd = (double) rand()/RAND_MAX;89 bool compare = exp( - (double)energy_diff/T ) > rnd ? 1 : 0;9091 if(compare) spins[irow][jcolumn] = -spins[irow][jcolumn];92 }

    [email protected] 112

  • Monte Carlo Simulation, Ising Model

    Figure : Magnetisation per spin versus time for a 14× 14 Ising model spins grid at T = 2.1.

    [email protected] 113

  • Sessions 29-31

    - Molecular Dynamics- Data Fitting- Error/Statistical Analysis

    [email protected] 114

  • Molecular Dynamics (MD)

    Monte Carlo simulations: for system’s equillibrium properties.

    Molecular dynamics simulation is complementary, uses equations ofmotion. Macroscopic properties of the system are measured asaverages over time. Note: here “molecule” is a generic name forobjects with mass, position and velocity whose motion is in accordwith Newton’s equations of motion.

    Molecular dynamics are applied to simulate solids, liquids, gases,flame, particles, biological elements, and cosmological/large-scalestructures.

    [email protected] 115

  • Molecular Dynamics (MD)

    For a simple MD simulation, the force between two particlesF = −dV (r)/dr depends on their seperation r according to theLennard-Jones potential

    V (r) = 4�[(σ

    r

    )12−(σ

    r

    )6](24)

    where σ and � are fix molecular energy and distance scale.

    For each particle i with position (xi , yi ), its acceleration isdetermined by the forces on it due to other particles. The equationsof motion will be:

    dvi ,xdt

    = ai ,xdxidt

    = vi ,x (25)

    dvi ,ydt

    = ai ,ydyidt

    = vi ,y (26)

    [email protected] 116

  • Molecular Dynamics (MD)

    The equations of motion can be solved by Verlet method (Euler orEuler-Cromer methods’ accuracies are not good enough)

    xi (t + ∆t) ≈ xi (t) + vx ,i (t)∆t +12ax ,i (t)(∆t)2 (27)

    vx ,i (t + ∆t) ≈ vx ,i (t) +12

    [ax ,i (t + ∆t) + ax ,i (t)] ∆t (28)

    A periodic boundary condition will be assumed. With this, theshortest of the separations between two given particles will be usedfor computing F .

    For initialising the molecular dynamics system, distance betweentwo objects should not be less than σ.

    Consider N particles in a 2-dimensional box with lenght lbox .Initialise and then update the system over time steps as follows.

    [email protected] 117

  • Molecular Dynamics (MD)

    Initialisation:- Choose the number of particles (N), dimension (dim) and size ofthe box (lbox) to put the particles in.

    - Set computational parameters such as the time step,Lennard-Jones’ potential parameters, and variables for allocatingthe particles’ co-ordinates, velocities and accelerations.

    - Make a function for Initialising the system: i.e. initial positions ofthe particles as points on a dim-dimensional grid.

    - Set initial velocities randomly within a chosen maximum values.

    [email protected] 118

  • Molecular Dynamics (MD)

    1 #include 2 #include 3 #include 4 #include 56 using namespace std;78 const int dim = 2; // 2d simulation9 const int N = 64; // number of particles

    10 int lx = 8, ly = 8; // particles are initially arranged11 // on grid points of lx rows and ly columns1213 double deltat = 0.01; // time step length14 double lbox = 14.0; // size of square box containing particles1516 double maxvel = 0.1; // maximum initial particle velocity component17 double sigma = 1.0, epsilon = 1.0; // Lennard-Jonnes Potential parameters1819 double xij[N][dim]; // particles’ coordinates20 double vel[N][dim]; // particles’ velocities21 double acc[N][dim]; // particles’ accelerations22

    [email protected] 119

  • Molecular Dynamics (MD)

    23 void Initialise(void){24 srand(time(0)); // seed for randon numbers generator25 double space = lbox/(double)lx; // space between particles’ initial positions26 int point = 0; // counts the individual particles27 for (int i = 0; i < lx; i++) // these for-loops loop over a 2d lx * ly28 for (int j = 0; j < ly; j++){ // lattice, placing particles on grid points29 if(point < N){30 xij[point][0] = (i + 0.5)*space; // initial x-coordinate31 xij[point][1] = (j + 0.5)*space; // initial y-coordinate32 // // initial x- & y- coordinate velocities33 vel[point][0] = maxvel * ( 2.0*rand() / (double)RAND_MAX - 1.0 );34 vel[point][1] = maxvel * ( 2.0*rand() / (double)RAND_MAX - 1.0 );35 }36 point++;37 }38 }

    [email protected] 120

  • Molecular Dynamics (MD)Compute accelerations; update positions & velocities:- Iterate over all particles to compute new positions and velocities

    I use Newton’s 3rd law, for particles k and `, |Fk`| = |F`k |.I for particle i , calculate separation rij from other paricles jI calculate force on particle i due to particle j , Fij . Sum all the

    forces on particle i due to other particles,ax [i ] = 1m

    ∑j 6=i Fij ∆xij , ay [i ] =

    1m∑

    j 6=i Fij ∆yij ,

    Fij = 24�[2(σr2)7 − ( σr2 )4] & r2 = (∆xij)2 + (∆yij)2

    I impose nearest range interaction because of boundaryconditions

    I use velocity Verlet method to update position of particle i .xt+∆t [i ] = xt [i ] + vx ,t [i ]∆t + 12ax ,t [i ](∆t)

    2

    vx ,t+∆t [i ] = vx ,t [i ] + 12 [ax ,t+∆t [i ] + ax ,t [i ]]and similarly for y-components.

    I Impose periodic boundary conditions of the particle [email protected] 121

  • Molecular Dynamics (MD)1 void Accelerate(void){2 // initialise accelerations to zero3 for (int point = 0; point < N; point++)4 for (int k = 0; k < dim; k++) acc[point][k] = 0.0;56 // compute new accelerations7 for(int i = 0; i < N-1; i++)8 for(int j = i+1; j < N; j++){9 double rij[dim];

    10 double rij2 = 0;11 for(int k = 0; k < dim; k++){12 rij[k] = xij[i][k] - xij[j][k];1314 // nearest range interaction because of periodic B.C.15 if(abs(rij[k]) > 0.5*lbox)16 rij[k] = rij[k] > 0 ? rij[k] - lbox : rij[k]+lbox;1718 rij2 += rij[k] * rij[k];19 }2021 // carefull. rsquared should not be zero.22 if( sqrt(rij2) == 0.0) cout

  • Molecular Dynamics (MD)1 int main(){2 int Nsteps = 1400;3 ofstream file("MD.dat"); // open file for printing temperature4 Initialise();56 for (int i = 0; i < Nsteps; i++){7 Accelerate();8 for (int point = 0; point < N; point++)9 for (int k = 0; k < dim; k++){

    10 // using velocity verlet method11 xij[point][k] += vel[point][k]*deltat + 0.5*acc[point][k]*deltat*deltat;12 // impose periodic boundary conditions13 xij[point][k] = xij[point][k] >= lbox ? xij[point][k] - lbox : xij[point][k];14 xij[point][k] = xij[point][k] < 0.0 ? lbox + xij[point][k] : xij[point][k];15 // first part of velocity update. second part needs a_t_plus_deltaT16 vel[point][k] += 0.5*acc[point][k]*deltat;17 }18 Accelerate(); // need this for computing a_t_plus_deltaT19 double velSquaredSum = 0.0; // for temperature20 for (int point = 0; point < N; point++)21 for (int k = 0; k < dim; k++){22 // second part of velocity update. first part is done above23 vel[point][k] += 0.5*acc[point][k]*deltat;24 // summing velocities for computing temperature25 velSquaredSum += 0.5 * vel[point][k] * vel[point][k];26 }27 file

  • Molecular Dynamics (MD)

    Figure : Temperature of the MD system versus simulation time steps.

    [email protected] 124

  • Project 4.5

    (1) Modify the Ising model code, or write your own code fromscratch, such that the temperature of the system can be varied (sayfrom 1.0 to 5.0 units). Plot the average magnetisation of thesystem against the temperature.(2) Modify the molecular dynamics code discussed in class. Addfunctions or changes to the code for doing the following:

    I Print the total energy per particle. Total energy is sum of thepotential energy between all distinct particle pairs and thekinetic energies of particles.

    I Plot the velocity distribution of the particles.(3) Fit the velocity distribution from question (2) above to theMaxwell’s speed distribution.

    [email protected] 125


Recommended