### Leap frog method fortran

GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again. As a engineering student, I has been appealed by the classic textbook - Programming the finite element method for more than five years.

When I was a bachelor, my supervisor Prof. Shang guided me to understand the theory of finite element method using coding. It was really impressive. Recently, I realize that it may be meaningful to reconstruct the code using Microsoft Visual Studio and Intel Visual Fortran Composer XEmaking it convenient for debuging and second development. I once used Microsoft Windowing Application to present the modelling results.

In this repository, I do not pay attention to visualization. What I focus on are the input and output files, algorithms, and the readibility of the code. Conservation of incompressible mass : Conservation of momentum : Stress equations :. Skip to content. MIT License. Dismiss Join GitHub today GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

Sign up. Branch: master. Go back. Launching Xcode If nothing happens, download Xcode and try again. Latest commit. Git stats 22 commits 1 branch 0 tags. Failed to load latest commit information. View code.

Prerequisites Programming the finite element method. Microsoft Visual Studio Ordinary differential equations are everywhere in science and engineering. Some have simple analytical solutions, some others don't and must be solved numerically. There exist several methods to do it. Scientific libraries in Matlab, Python, Fortran, C, However, advanced integration techniques may be picky and it's always good to compare with a simple technique that you know well.

The leapfrog technique is lightweight and very stable. In comparison, the popular Runge-Kutta 4 is more accurate but produces a systematic error, leading to a long-term drift in the solution. As we will see, the energy error in the leapfrog scheme has no such long-term trend. In this paper, I show how to integrate Newton's equations of motion for the driven harmonic oscillator with the leapfrog techniquein Python.

Leapfrog integration is a particular approach to write two coupled first-order ordinary differential equations with finite differences. For example, I can write:. This is shown graphically below source. The equation here is a bit more complex, but perfectly good for a fully explicit numerical integration of equation 1. We go through it now.

For the moment, we work without a force, i. As you see below, integration is fairly simple:. This piece of code actually doesn't solve anything, yet. I created the function "integrate F,x0,v0,gamma ", to make it easy solving the problem with different parameters and compare them. For example:. When plotted, it gives see code for details :. The oscillations in the energy might look weird but think about it a moment. In this interval of time, damping is absent and energy is constant. To add a driving force, I recycle the function packet from the Fast Fourier transform article, that I renamed force to fit in the actual context:. If we add it and drive three different oscillators with the same force:.

When damping is strong, energy is lost almost immediately after the driving force stops. Well it happens that the driven harmonic oscillator with damping is a simple equation that is widely used to model how light interacts with atoms see this reference, for example.

I even got myself interested on improvements to reproduce some quantum mechanical behaviors of atoms in strong light fields for modeling nonlinear optics see here.The algorithm was first used in by Delambre and has been rediscovered many times since then, most recently by Loup Verlet in the s for use in molecular dynamics.

This equation, for various choices of the potential function Vcan be used to describe the evolution of diverse physical systems, from the motion of interacting molecules to the orbit of the planets. After a transformation to bring the mass to the right side and forgetting the structure of multiple particles, the equation may be simplified to.

Where Euler's method uses the forward difference approximation to the first derivative in differential equations of order one, Verlet integration can be seen as using the central difference approximation to the second derivative:. We can see that the first- and third-order terms from the Taylor expansion cancel out, thus making the Verlet integrator an order more accurate than integration by simple Taylor expansion alone.

In computing the global error, that is the distance between exact solution and approximation sequence, those two terms do not cancel exactly, influencing the order of the global error. To gain insight into the relation of local and global errors, it is helpful to examine simple examples where the exact solution, as well as the approximate solution, can be expressed in explicit formulas.

The standard example for this task is the exponential function. These are. To compare them with the exact solutions, Taylor expansions are computed:. That is, although the local discretization error is of order 4, due to the second order of the differential equation the global error is of order 2, with a constant that grows exponentially in time.

Moreover, to obtain this second-order global error, the initial error needs to be of at least third order. This can be corrected using the formula .

This deficiency can either be dealt with using the velocity Verlet algorithm or by estimating the velocity using the position terms and the mean value theorem :. A related, and more commonly used, algorithm is the velocity Verlet algorithm,  similar to the leapfrog methodexcept that the velocity and position are calculated at the same value of the time variable leapfrog does not, as the name suggests.

This uses a similar approach, but explicitly incorporates velocity, solving the problem of the first time step in the basic Verlet algorithm:. It can be shown that the error in the velocity Verlet is of the same order as in the basic Verlet.

Note that the velocity algorithm is not necessarily more memory-consuming, because it's not necessary to keep track of the velocity at every time step during the simulation. The standard implementation scheme of this algorithm is:. One might note that the long-term results of velocity Verlet, and similarly of leapfrog are one order better than the semi-implicit Euler method.

The algorithms are almost identical up to a shift by half a time step in the velocity. This is easily proven by rotating the above loop to start at step 3 and then noticing that the acceleration term in step 1 could be eliminated by combining steps 2 and 4. The only difference is that the midpoint velocity in velocity Verlet is considered the final velocity in semi-implicit Euler method.

The global error of all Euler methods is of order one, whereas the global error of this method is, similar to the midpoint methodof order two. Additionally, if the acceleration indeed results from the forces in a conservative mechanical or Hamiltonian systemthe energy of the approximation essentially oscillates around the constant energy of the exactly solved system, with a global error bound again of order one for semi-explicit Euler and order two for Verlet-leapfrog.

The same goes for all other conserved quantities of the system like linear or angular momentum, that are always preserved or nearly preserved in a symplectic integrator. A simplified drag force is used to demonstrate change in acceleration, however it is only needed if acceleration is not constant.

These can be derived by noting the following:.

In molecular dynamics simulations, the global error is typically far more important than the local error, and the Verlet integrator is therefore known as a second-order integrator. Systems of multiple particles with constraints are simpler to solve with Verlet integration than with Euler methods. Constraints between points may be, for example, potentials constraining them to a specific distance or attractive forces. They may be modeled as springs connecting the particles.One of the principal tools in the theoretical study of biological molecules is the method of molecular dynamics simulations MD.

This computational method calculates the time dependent behavior of a molecular system. MD simulations have provided detailed information on the fluctuations and conformational changes of proteins and nucleic acids. These methods are now routinely used to investigate the structure, dynamics and thermodynamics of biological molecules and their complexes. Biological molecules exhibit a wide range of time scales over which specific processes occur; for example Local Motions 0. The goal of this course is to provide an overview of the theoretical foundations of classical molecular dynamics simulations, to discuss some practical aspects of the method and to provide several specific applications within the framework of the CHARMM program.

### Leapfrog integration

Although the applications will be presented in the framework of the CHARMM program, the concepts are general and applied by a number of different molecular dynamics simulation programs. Section I of this course will focus on the fundamental theory followed by a brief discussion of classical mechanics.

In section II, the potential energy function and some related topics will be presented. Section III will discuss some practical aspects of molecular dynamics simulations and some basic analysis. The remaining sections will present the CHARMM program and provide some tutorials to introduce the user to the program.

This course will concentrate on the classical simulation methods i. Molecular dynamics simulations permit the study of complex, dynamic processes that occur in biological systems.

These include, for example, Protein stability Conformational changes Protein folding Molecular recognition: proteins, DNA, membranes, complexes Ion transport in biological systems and provide the mean to carry out the following studies. The molecular dynamics method was first introduced by Alder and Wainwright in the late 's Alder and Wainwright,to study the interactions of hard spheres.

### Leap frog method fortran

Many important insights concerning the behavior of simple liquids emerged from their studies. The next major advance was inwhen Rahman carried out the first simulation using a realistic potential for liquid argon Rahman, The first molecular dynamics simulation of a realistic system was done by Rahman and Stillinger in their simulation of liquid water in Stillinger and Rahman, The first protein simulations appeared in with the simulation of the bovine pancreatic trypsin inhibitor BPTI McCammon, et al Today in the literature, one routinely finds molecular dynamics simulations of solvated proteins, protein-DNA complexes as well as lipid systems addressing a variety of issues including the thermodynamics of ligand binding and the folding of small proteins.

The number of simulation techniques has greatly expanded; there exist now many specialized techniques for particular problems, including mixed quantum mechanical - classical simulations, that are being employed to study enzymatic reactions in the context of the full protein. Molecular dynamics simulation techniques are widely used in experimental procedures such as X-ray crystallography and NMR structure determination. References Alder, B. Alder, B. Stillinger, F.

McCammon, J. Nature Lond. Molecular dynamics simulations generate information at the microscopic level, including atomic positions and velocities. The conversion of this microscopic information to macroscopic observables such as pressure, energy, heat capacities, etc. Statistical mechanics is fundamental to the study of biological systems by molecular dynamics simulation. In this section, we provide a brief overview of some main topics. For more detailed information, refer to the numerous excellent books available on the subject.

In a molecular dynamics simulation, one often wishes to explore the macroscopic properties of a system through microscopic simulations, for example, to calculate changes in the binding free energy of a particular drug candidate, or to examine the energetics and mechanisms of conformational change.

The connection between microscopic simulations and macroscopic properties is made via statistical mechanics which provides the rigorous mathematical expressions that relate macroscopic properties to the distribution and motion of the atoms and molecules of the N-body system; molecular dynamics simulations provide the means to solve the equation of motion of the particles and evaluate these mathematical formulas. Reference Textbooks on Statistical Mechanics D.

Wilde and S. Statistical mechanics is the branch of physical sciences that studies macroscopic systems from a molecular point of view. The goal is to understand and to predict macroscopic phenomena from the properties of individual molecules making up the system.

The system could range from a collection of solvent molecules to a solvated protein-DNA complex. In order to connect the macroscopic system to the microscopic system, time independent statistical averages are often introduced.The solution to the above problem with 8 steps was pretty good, certainly far better than the Euler method used above with the same step size.

However the solutions for the problem were clearly wrong. In this exercise I want you to investigate this method by looking at the simple 1st order differential equation You can try and write a fortran programme to solve this problem using the leapfrog method. However, if you have insufficient time or motivation then use the link below to get a programme that I have already written.

Read through it to check it does everything it should from the list below. Things you should include: The programme should request two out of 1 step length 2 number of steps and 3 length of interval for finding the solution.

The programme can then calculate the other quantity. The value of a. Print out the value of xy and the error at every time step. Hint: this means that it is not wise to run your programme with too many steps! Don't forget that you will not be able to calculate this exact figure!

Try and complete writing your own programme. If you need help you can have a look at a programme I have written. Now try your programme for a variety of values of ainterval lengths and step lengths. In particular make sure you try a both positive and negative. What do you conclude? Return to X2 Numerical Maths Page.Positions are defined at timesspaced at constant intervalswhile the velocities are defined at times halfway in between, indicated bywhere.

The leapfrog integration scheme then reads: 4. Note that the accelerations are defined only on integer times, just like the positions, while the velocities are defined only on half-integer times. This makes sense, given that : the acceleration on one particle depends only on its position with respect to all other particles, and not on its or their velocities.

Only at the beginning of the integration do we have to set up the velocity at its first half-integer time step. Starting with initial conditions andwe take the first term in the Taylor series expansion to compute the first leap value for : 4. We are then ready to apply Eq. Next we compute the accelerationwhich enables us to compute the second leap value,using Eq.

A second way to write the leapfrog looks quite different at first sight. Defining all quantities only at integer times, we can write: 4. This is still the same leapfrog scheme, although represented in a different way. Notice that the increment in is given by the time step multiplied byeffectively equal to. Similarly, the increment in is given by the time step multiplied byeffectively equal to the intermediate value. In conclusion, although both positions and velocities are defined at integer times, their increments are governed by quantities approximately defined at half-integer values of time.

A most interesting way to see the equivalence of Eqs. For the two systems to be equivalent, they'd better share this property. Let us inspect. Starting with Eqs. We will take one step forward, taking a time stepto evolve toand then we will take one step backwards, using the same scheme, taking a time step.

Clearly, the time will return to the same value sincebut we have to inspect where the final positions and velocities are indeed equal to their initial values. Here is the calculation, resulting from applying Eqs. In an almost trivial way, we can see clearly that time reversal causes both positions and velocities to return to their old values, not only in an approximate way, but exactly.Finite-difference time-domain or Yee's method named after the Chinese American applied mathematician Kane S.

Yeeborn is a numerical analysis technique used for modeling computational electrodynamics finding approximate solutions to the associated system of differential equations. Since it is a time-domain method, FDTD solutions can cover a wide frequency range with a single simulation run, and treat nonlinear material properties in a natural way. The FDTD method belongs in the general class of grid -based differential numerical modeling methods finite difference methods.

The time-dependent Maxwell's equations in partial differential form are discretized using central-difference approximations to the space and time partial derivatives. The resulting finite-difference equations are solved in either software or hardware in a leapfrog manner: the electric field vector components in a volume of space are solved at a given instant in time; then the magnetic field vector components in the same spatial volume are solved at the next instant in time; and the process is repeated over and over again until the desired transient or steady-state electromagnetic field behavior is fully evolved.

Finite difference schemes for time-dependent partial differential equations PDEs have been employed for many years in computational fluid dynamics problems,  including the idea of using centered finite difference operators on staggered grids in space and time to achieve second-order accuracy.

An appreciation of the basis, technical development, and possible future of FDTD numerical techniques for Maxwell's equations can be developed by first considering their history.

The following lists some of the key publications in this area. When Maxwell's differential equations are examined, it can be seen that the change in the E-field in time the time derivative is dependent on the change in the H-field across space the curl. This results in the basic FDTD time-stepping relation that, at any point in space, the updated value of the E-field in time is dependent on the stored value of the E-field and the numerical curl of the local distribution of the H-field in space.

The H-field is time-stepped in a similar manner. At any point in space, the updated value of the H-field in time is dependent on the stored value of the H-field and the numerical curl of the local distribution of the E-field in space.

Iterating the E-field and H-field updates results in a marching-in-time process wherein sampled-data analogs of the continuous electromagnetic waves under consideration propagate in a numerical grid stored in the computer memory.

When multiple dimensions are considered, calculating the numerical curl can become complicated. Kane Yee's seminal paper proposed spatially staggering the vector components of the E-field and H-field about rectangular unit cells of a Cartesian computational grid so that each E-field vector component is located midway between a pair of H-field vector components, and conversely.

## Code for verlet frog leap method.

Furthermore, Yee proposed a leapfrog scheme for marching in time wherein the E-field and H-field updates are staggered so that E-field updates are conducted midway during each time-step between successive H-field updates, and conversely.

On the minus side, this scheme mandates an upper bound on the time-step to ensure numerical stability. To implement an FDTD solution of Maxwell's equations, a computational domain must first be established. The computational domain is simply the physical region over which the simulation will be performed.

The E and H fields are determined at every point in space within that computational domain. The material of each cell within the computational domain must be specified.

Typically, the material is either free-space airmetalor dielectric. Any material can be used as long as the permeabilitypermittivityand conductivity are specified. The permittivity of dispersive materials in tabular form cannot be directly substituted into the FDTD scheme.

Instead, it can be approximated using multiple Debye, Drude, Lorentz or critical point terms. This approximation can be obtained using open fitting programs  and does not necessarily have physical meaning. Once the computational domain and the grid materials are established, a source is specified.

The source can be current on a wire, applied electric field or impinging plane wave. In the last case FDTD can be used to simulate light scattering from arbitrary shaped objects, planar periodic structures at various incident angles,   and photonic band structure of infinite periodic structures. Since the E and H fields are determined directly, the output of the simulation is usually the E or H field at a point or a series of points within the computational domain.

The simulation evolves the E and H fields forward in time.