The present book is not so concerned with the traditional academic presentation of the subject, but is focused on teaching the practitioner how to obtain reliable calculations involving finite difference methods. One of the book's goals is to present an up-to-date Python ecosystem for implementing finite difference methods.
Vibration ODEs
Finite Difference Discretization
- A Basic Model for Vibrations
- A Centered Finite Difference Scheme
The corresponding period of the oscillations (ie the time between two adjacent peaks in the cosine function) is P D2=!. We also need to replace the derivative in the initial condition with the finite difference.
Implementation
- Making a Solver Function
- Verification
- Scaled Model
By simple induction we can show that the lattice function Dtn2 is also a solution of the discrete equations. Convergence of the sequence r0; r1; : : : ; rm2 requires that the time steps be small enough for the DC.ti/r error model to be valid.
Visualization of Long Time Simulations
- Using a Moving Plot Window
- Making Animations
- Using Bokeh to Compare Graphs
- Using a Line-by-Line Ascii Plotter
- Empirical Analysis of the Solution
Visualize u and the exact solution vs t, using a moving plot window and continuously plotting the curves as they evolve over time. When analyzing very long time series, it is advantageous to calculate and plot instead of automatically getting an impression of the course of the oscillations.
Analysis of the Numerical Scheme
- Deriving a Solution of the Numerical Scheme
- The Error in the Numerical Frequency
- Empirical Convergence Rates and Adjusted !
- Exact Discrete Solution
- Convergence
- The Global Error
- Stability
- About the Accuracy at the Stability Limit
Too large a numerical frequency gives an oscillating curve that oscillates too fast and therefore “lags” behind the precise oscillations, a characteristic you can see in the left graph of Figure 1.2. Note that the approximate formula (1.19) is very inaccurate for this value of t as it predicts. !Q D2:34=pi, which is a 25% reduction.) The corresponding period of the numerical solution is PQ D2=!Q D2t, which means that in the numerical solution there is only one time step between the peak (maximum) and through 12 (minimum ).
Alternative Schemes Based on 1st-Order Equations
- The Forward Euler Scheme
- The Backward Euler Scheme
- The Crank-Nicolson Scheme
- Comparison of Schemes
- Runge-Kutta Methods
- Analysis of the Forward Euler Scheme
We can also compare this scheme with the centered discretization of the second-order ODE. The error in the angular frequency is of the same order as in the scheme (1.7) for the second-order ODE, but the error in the amplitude is serious.
Energy Considerations
- Derivation of the Energy Expression We start out with multiplyingWe start out with multiplying
- An Error Measure Based on Energy
The Runge-Kutta and Crank-Nicolson methods hardly change their energy error with T, while the error in the Forward Euler method grows to enormous levels and a relative error of 1 in the Backward Euler method points to E.t. A striking fact from this table is that the error in the Forward Euler method is reduced by the same factor astis reduced by, while the error in the Crank-Nicolson method has a reduction proportional tot2 (we cannot say anything for Back- department Euler method) .
The Euler-Cromer Method
- Forward-Backward Discretization
- Implementation
- The Störmer-Verlet Algorithm
Verification Since the Euler-Cromer scheme is equivalent to the finite difference method for second-order ODEu00C!2uD0 (see Sect.1.7.2), the execution of the above solver function is the same as for the solver function in Sect.1.2. This deviation in the Euler-Cromer scheme means that the discrete solution given in Sect.1.4.4 is not a solution of the Euler-Cromer scheme.
Staggered Mesh
- The Euler-Cromer Scheme on a Staggered Mesh
- Implementation of the Scheme on a Staggered Mesh
The conclusion is that the offset grid is fully equivalent to this scheme, while the forward-backward version causes a slight deviation in the calculation of u1. Implementation with half-integer indices Some prefer to see a closer connection between code and mathematics for quantities with half-integer indices. Checking this code is straightforward, as we can just compare the calculated u with the u generated by the solver function in vib_undamped.py (which directly solves u00C!2u D 0).
Exercises and Problems
Given some chosen solution u (as a function of t, implemented as a Python function), use the method of manufactured solutions to calculate the source term f, and see if you also solve the discrete equations. The function is very similar to linear.) . e) Will a polynomial of degree three satisfy the discrete equations?
Show linear growth of the phase with time
Improve the accuracy by adjusting the frequency
See if adaptive methods improve the phase error
Derive and investigate the velocity Verlet method
Find the minimal resolution of an oscillatory function
Calculate For special selection of the cosine/sine function formuDexp.i !t /in show it.
Verify convergence rates of the error in energy
Use linear/quadratic functions for verification
Use an exact discrete solution for verification
Use analytical solution for convergence rate tests
Investigate the amplitude errors of many solvers
Minimize memory usage of a simple vibration solver
Create a test function that verifies the implementation by comparing with other code for the same problem.
Minimize memory usage of a general vibration solver
Analysis of the Euler-Cromer scheme
- A Centered Scheme for Linear Damping
- A Centered Scheme for Quadratic Damping
- A Forward-Backward Discretization of the Quadratic Damping TermDamping Term
- Implementation
- Visualization
- User Interface
- The Euler-Cromer Scheme for the Generalized Model
- A Staggered Euler-Cromer Scheme for a Generalized Model
- The PEFRL 4th-Order Accurate Algorithm
- Exercises and Problems
In the derivation of a special equation for the first time, we encounter difficulties: the insertion of (1.76) into (1.82) for n D 0 leads to a complicated non-linear equation foru1. However, this quadratic polynomial in t does not satisfy the discrete equations in the case of quadratic damping, because the geometric mean used in the approximation of this term introduces an error. 1.8.1) and search uat mesh points stn (the numerical value is indicated by un) and vbetween mesh points attnC1=2. the numerical value is indicated by vnC12).
Implement the solver via classes
Use a backward difference for the damping term
- Applications of Vibration Models
- Oscillating Mass Attached to a Spring
- General Mechanical Vibrating System
- A Sliding Mass Attached to a Spring
- A Jumping Washing Machine
- Motion of a Pendulum
- Motion of an Elastic Pendulum
- Two-Body Gravitational Problem
- Electric Circuits
- Exercises
From this exercise, we learn that the first-order differences and the linearization trick play together in the "right way" so that the scheme is as good as if (in Section 1.10.10) we carefully use center differences and the geometric mean on a staggered grid to achieve second-order accuracy. The main difference is that we have amodelforS in terms of movement (as soon as we expressed L byr). The special case of circular motion r D 1 satisfies the equations, since the last equation gives P D const, and the first gives P D 1, i.e. the motion is r .t / D1,.t / Dt with unity angular frequency as expected and period2 as expected.
Simulate resonance
Simulate oscillations of a sliding box
Simulate a bouncing ball
Although the term "mechanical vibration" is used in this book, we should mention that the same type of equations arise when modeling electrical circuits. To avoid this situation, a state variable can be introduced that holds the mode of motion: free fall, impact, or rest.
Simulate a simple pendulum
Simulate an elastic pendulum
If < 10degrees, add a plot comparing the solutions of the scaled, classical, inelastic pendulum and the elastic pendulum (.t. Although the math here uses a bar on scaled quantities, the code should contain simple names xforx,N yfory, andN tfortbarN (rather than numerical errors, the results will be of approximation, etc.). you must believe in correct results and set a (low) tolerance that corresponds to the calculated maximum error.
Simulate an elastic pendulum with air resistance
To use Odespy to solve the equations, arrange the ODEs like PNx;x;PNN y;yNzodatodespy.EulerCromer can be applied. Start by simplifying the ODEs for pure vertical motion and show that y.N t /N satisfies a vibration equation with frequency pˇ=.1ˇ/.
Implement the PEFRL algorithm
Open Access This chapter is licensed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits use, sharing, adaptation, distribution, and reproduction in any medium or form. , provided you give proper credit to the original author(s) and source, provide a link to the Creative Commons license, and indicate whether changes have been made. Images or other third-party material in this chapter are covered under this chapter's Creative Commons license, unless otherwise noted in the credit line for the material. If the material is not covered by a Creative Commons Chapter license and your intended use is not permitted by law or exceeds the permitted use, you will need to obtain permission directly from the copyright holder.
Wave Equations
Simulation of Waves on a String
- Discretizing the Domain
- The Discrete Solution
- Fulfilling the Equation at the Mesh Points
- Replacing Derivatives by Finite Differences
- Sketch of an Implementation
ŒDtDtuDc2DxDxni : (2.12) Interpretation of the equation as a template A characteristic feature of (2.11) is that it includes values only from neighboring points: unC1i ,uni˙1,uni, andun1i. Algebraic version of the initial conditions We also need to replace the derivative in the initial condition (2.3) with a finite difference approximation. We see that the discrete version of the PDE contains only one parameter, C, which is therefore the key parameter, along with Nx, that governs the quality of the numerical solution (see Section 2.10 for details).
Verification
- A Slightly Generalized Model Problem
- Manufactured Solution and Estimation of Convergence Rates Specifying the solution and computing corresponding data One problem with
- Constructing an Exact Solution of the Discrete Equations With a manufactured or known analytical solution, as outlined above, we can esti-With a manufactured or known analytical solution, as outlined above, we can esti-
This property often arises when the exact solution of the PDE is a lower-order polynomial. To realize that the choice is also an exact solution of the discrete equations, we first remind ourselves that it is not. Therefore the exact solution.x; t /Dx.Lx/.1Ct =2/ of the PDE problem is also an exact solution of the discrete problem.
Implementation
- Callback Function for User-Specific Actions
- The Solver Function
- Verification: Exact Quadratic Solution
- Verification: Convergence Rates
- Visualization: Animating the Solution
- Running a Case
- Working with a Scaled PDE Model
The solution to such a problem is to decide on a total number of frames in the animation,number_frames, and plot the solution only for each skip frame frame. Here is a typical length scale, e.g. the length of the domain, and is a typical size of u, e.g. determined from the initial state: aDmaxxjI.x/y. The period in the dimensionless problem is 2, so the end time can be set to the desired number of periods times 2.
Vectorization
- Operations on Slices of Arrays
- Finite Difference Schemes Expressed as Slices
- Verification
- Efficiency Measurements
- Remark on the Updating of Arrays
In the code, we can just set a=c=L=1,x0=0.8, and it is not necessary to calculate with wavelengths and frequencies to estimatec. We wanted to insert the array on the right into the original u2 array for the items corresponding to the internal points in the mesh (1:n-1 or 1:-1). This means that if we return u, as we do in the sample codes here, we are actually returning u_nm1, which is clearly wrong.
Simulate a standing wave
While u_nm1 = u_n is fine, u_n = u is problematic, so the solution to this problem is to ensure that u points to the u_nm1 array. This is mathematically incorrect, but at the next time step new correct values will be entered and so it will be correct. Note that the user_actionfunction reduces the need to return the solution from the solver.).
Add storage of solution in a user action function
The error grid function en can be simulated for 10 periods, while 20–30 periods are needed to show significant differences between the curves for the numerical and exact solution.
Use a class for the user action function
Compare several Courant numbers in one movie
Implementing the solver function as a generator
Calculus with 1D mesh functions
- Generalization: Reflecting Boundaries
- Neumann Boundary Condition
- Discretization of Derivatives at the Boundary
- Implementation of Neumann Conditions
- Index Set Notation
- Verifying the Implementation of Neumann Conditions
- Alternative Implementation via Ghost Cells
- Generalization: Variable Wave Velocity
- The Model PDE with a Variable Coefficient
- Discretizing the Variable Coefficient
- How a Variable Coefficient Affects the Stability
- Neumann Condition and a Variable Coefficient
- A More General PDE Model with Variable Coefficients
- Generalization: Damping
- Building a General 1D Wave Equation Solver
- User Action Function as a Class
- Pulse Propagation in Two Media
- Exercises
At the boundary, we can easily define im1=i+1, while we use im1=i-1 in the inner parts of the mesh. Programwave1D_n0.py contains a complete implementation of the 1D wave equation with boundary conditionsuxD0atxD0andxDL. A useful feature of the wave1D_dn_vc.py program is the specification of the user_action function as a class.
Find the analytical solution to a damped wave equation
Send pulse waves through a layered medium
Explain why numerical noise occurs
Investigate harmonic averaging in a 1D model
Implement open boundary conditions
A much more convenient implementation of the open boundary condition in xDL can be based on an explicit discretization. Because this simplified implementation of the open boundary condition works, there is no need to pursue the more complicated discretization in a). In 2D and 3D it is difficult to calculate the exact wave velocity normal to the boundary, which is necessary in generalizations of open boundary conditions in higher dimensions.
Implement periodic boundary conditions
However, in 2D and 3D, the condition reads Ccxux Ccyuy D 0, kucx andcy are the wave velocities in the xandy directions. Other methods are commonly used in 2D and 3D to let waves move through a computational domain.
Compare discretizations of a Neumann condition
The implementation of the boundary condition atLx=2 is O.x2/exactly, but the interesting question is what influence the motion of the boundary has on the rate of convergence.
Verification by a cubic polynomial in space
- Analysis of the Difference Equations
- Properties of the Solution of the Wave Equation The wave equationThe wave equation
- More Precise Definition of Fourier Representations
- Stability The schemeThe scheme
- Numerical Dispersion Relation
- Extending the Analysis to 2D and 3D
- Finite Difference Methods for 2D and 3D Wave Equations A natural next step is to consider extensions of the methods for various variants ofA natural next step is to consider extensions of the methods for various variants of
- Multi-Dimensional Wave Equations
- Mesh
- Discretization
- Implementation
- Scalar Computations
- Vectorized Computations
- Verification
- Visualization
For an actual functionI.x/, the relevant Python code for calculating and plotting the discrete Fourier transform appears in the example below. That is, determine how well the spatial variation of the wave component is resolved in the mesh. Handling of boundary conditions where known The schemes listed above are valid for the internal points in the mesh.