Advanced Programming in Engineering Lecture Notes
Total Page:16
File Type:pdf, Size:1020Kb
Advanced Programming in Engineering lecture notes P.J. Visser, S. Luding, S. Srivastava March 13, 2011 Changes 13/3/2011 Added Section 7.4 to 7.7. Added Section 4.4 7/3/2011 Added chapter Chapter 7 23/2/2011 Corrected the equation for the average temperature in terms of micro- scopic quantities, Eq. 3.14. Corrected the equation for the speed distribution in 2D, Eq. 3.19. Added links to sources of `true' random numbers available through the web as footnotes in Section 5.1. Added examples of choices for parameters of the LCG and LFG in Section 5.1.1 and 5.1.2, respectively. Added the chi-square test and correlation functions as ways for testing random numbers in Section 5.1.4. Improved the derivation of the average squared distance of particles in a random walk being linear with time, in Section 5.2.1. Added a discussion of the meaning of the terms in the master equation, in Section 5.2.5. Small changes. 19/2/2011 Corrected the Maxwell-Boltzmann distribution for the speed, Eq. 3.19. 14/2/2011 1 2 Added Chapter 6. 6/2/2011 Verified Eq. 3.15 and added footnote discussing its meaning in case of periodic boundary conditions. Added formula for the radial distribution function in Section 3.4.2, Eq. 3.21. 31/1/2011 Added Chapter 5. 9/1/2010 Corrected labels on the axes in Fig. 3.10, changed U · " and r · σ to U=" and r/σ, respectively. Added truncated version of the Lennard-Jones potential in Section 3.3.1. Corrected speed distribution, Eq. 3.19 to be valid in 2D. Small changes. 3/12/2010 Added section on simulating fluids, Section 3.3. Added section on statistical mechanics, Section 3.4. 13/12/2010 (2) Corrected that spring equilibrium lengths for Np particles in 2D are fully determined by a particle pair rather than a particle. I.e. the equiblibrium lengths of Section 3.2.2 are req;ij; i = 1::Np; j = 1::Np, rather than req;i; i = 1::Np. Corrected that the outer loop of the force calculation, Algorithm 4, can run from 1 to Np − 1 rather than from 1 to Np. Added section on the potential and kinetic energy in 2D, Section 3.2.3. Added exercise asking to add the computation of the potential and kinetic energy to the algorithm. Small changes. 13/12/2010 3 Changed algorithms. Loop variables are now updated at the start of an integration step rather than the end. Positions at the current time step is stored rather than the position at the next time step. Loop runs from n = 0 to n = Nt rather than n = 1 to n = Nt. It makes more sense this way. T Corrected ~ri = xi yi ; i = 1::Np to ~ri = xi yi ; i = 1::Np in Sec- tion 3.2.3. Corrected force calculation for particles connected with springs in 2D, Section 3.2; introduced spring compression δ and corrected that ~nij is the vector to particle i from particle j, not from particle i to particle j. Expanded section on solids, Section 3.2.4 a bit. Other small changes. Preface The present script attempts to describe into sufficient detail the material treated during the lectures. It is based mainly on handwritten notes made by the author during the 09/10 lectures. It therefore attempts to largely reflect the material as lectured by professor Luding (the topics of differential equations, numerical integration of ordinary differential equations, molecular dynamics, random numbers and fractals), Katia Bertoldi (the topic of finite element method) and Anthony Thornton (the topics of finite volume method and SPH method). It is a work in progress, and as of yet only material covering the first few lectures has been written. The material that has been written will become available in parallel to the lectures covering it. The latest version will be available through Blackboard. For certain topics, hyperlinks that point to further reading and other helpful places have been added to this document. Of course these will only be available when reading the digital version in a PDF reader supporting hyperlinks, such as Adobe Reader 1. Throughout this document exercises are described. They are intended for self study purposes and are not mandatory. Many thanks to all who have helped out in making this document, in particular to professor Luding, Saurabh Srivastava, Thomas Weinhart and Martin Robinson for their feedback on drafts of the document. I hope you find this document a helpful addition to the lectures. I am sure that you will encounter many mistakes, be it in spelling or content. Any corrections and other comments would be greatly appreciated. Please send them to [email protected]. 1available on http://get.adobe.com/reader/ 4 Contents 1 Numeral systems 8 1.1 Representation of positive integers and real numbers . .8 1.2 Representation of negative numbers . .9 2 Ordinary differential equations 11 2.1 The harmonic oscillator . 11 2.1.1 Analytic solution . 12 2.2 Nondimensionalization . 13 2.3 Numerical integration of ODE's . 14 2.3.1 Euler integration . 15 2.3.2 Euler-Cromer integration . 16 2.3.3 Verlet integration . 16 2.3.4 Runge-Kutta integration . 17 2.3.5 MATLAB ode45 . 19 2.3.6 Other integration schemes . 21 3 Molecular Dynamics 23 3.1 Particles in one dimension connected with springs . 23 3.1.1 Two particles connected with a spring . 23 3.1.2 Np particles connected with a spring . 24 3.1.3 Debugging using laws of physics . 26 3.2 Particles in two dimensions connected with springs . 28 3.2.1 Two particles in two dimensions connected with a spring 29 3.2.2 Np particles in two dimensions connected with springs 30 3.2.3 An algorithm for 2D molecular dynamics . 31 3.2.4 Modeling a solid . 36 3.3 Simulating fluids using molecular dynamics . 38 3.3.1 Potentials . 39 3.3.2 Efficiently simulating fluids . 41 3.4 Statistical mechanics, connecting microscopic and macroscopic properties . 44 3.4.1 Obtaining familiar macroscopic quantities from simu- lations . 45 5 CONTENTS 6 3.4.2 Other important concepts in statistical mechanics . 47 3.4.3 Local instantaneous quantities . 49 3.4.4 Finding new microstates . 49 4 The Finite Element Method 50 4.1 The finite element method in the quasistatic limit . 51 4.1.1 The quasistatic limit . 51 4.1.2 The bar element . 52 4.1.3 An example of solving a problem with FEM . 53 4.1.4 Elements in arbitrary orientation . 56 4.2 Formal procedure of deriving the stiffness matrix in FEM . 57 4.2.1 Derivation of the strong formulation . 58 4.2.2 Derivation of the weak formulation . 59 4.2.3 Discretization . 61 4.3 Dynamic formulation for FEM . 63 4.3.1 Discretization of the dynamic formulation; the mass matrix . 64 4.3.2 Modal analysis . 68 4.3.3 Transient analysis; the Newmark-β method . 69 4.4 Finite Element Method for Nonlinear Systems . 70 4.4.1 A primer on nonlinearities . 70 4.4.2 Conjugate Gradient Method . 71 4.4.3 Newton-Raphson Method . 71 4.4.4 Nonlinear bar . 72 4.4.5 Newton's method for nonlinear finite elements . 73 5 Random numbers 75 5.1 Random number generators . 75 5.1.1 The linear congruential generator . 76 5.1.2 The lagged Fibonacci generator . 76 5.1.3 Other random number generators . 77 5.1.4 Testing randomness . 77 5.2 Random walks . 78 5.2.1 The one dimensional random walk . 79 5.2.2 Random walks in 3D . 81 5.2.3 Continuous time random walks . 81 5.2.4 The probability distribution for the position of a par- ticle in a random walk . 81 5.2.5 The random walk and diffusion . 82 6 Smoothed Particle Hydrodynamics 84 6.1 Introduction . 84 6.2 Interpolation . 85 6.2.1 Integral and summation interpolants . 85 CONTENTS 7 6.2.2 First derivatives . 86 6.3 Simple Equations of Motion . 86 6.3.1 SPH equations of motion satisfying the conservation laws ............................ 86 6.3.2 Integration of the equations of motion . 87 6.4 Viscosity . 87 7 Finite Volume Method 88 7.1 The FVM integral equation . 88 7.2 Total variation diminishing schemes . 91 7.3 The CFL condition . 92 7.4 The Lax-Friedrichs method . 92 7.5 Flux limiters . 93 7.6 TVD Lax-Friedrichs . 94 7.7 Initial and boundary conditions . 95 7.7.1 Initial conditions . 95 7.7.2 Boundary conditions . 95 Chapter 1 Numeral systems Numeral systems are writing systems for expressing numbers. The most commonly used numeral system is the positional system. Here a numeral is written as a series of digits ak in a base B, where ak = 0; 1; :::; B − 1. The number N represented by the numeral is given by X k N = akB (1.1) k The digits used to denote numbers are those of the arabic numerals for bases smaller or equal to ten. Thus the symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. For larger bases letters are used in addition to arabic numerals. For example, the digits used in hexedecimal notation (B = 16) are the arabic digits together with the symbols A, B, C, D, E and F . In case when it is unclear from context in what base a numeral is written a subscript with the base number is usually added.