• Tidak ada hasil yang ditemukan

Thư viện số Văn Lang: Finite Difference Computing with PDEs: A Modern Software Approach

N/A
N/A
Nguyễn Gia Hào

Academic year: 2023

Membagikan "Thư viện số Văn Lang: Finite Difference Computing with PDEs: A Modern Software Approach"

Copied!
92
0
0

Teks penuh

This property often arises when the exact solution of the PDE is a lower order polynomial. Therefore, the exact solution is.x; t /Dx.Lx/.1Ct =2/of the PDE problem is also an exact solution of the discrete problem.

Fig. 2.1 Mesh in space and time. The circles show points connected in a finite difference equation
Fig. 2.1 Mesh in space and time. The circles show points connected in a finite difference equation

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

Here is a typical length scale, eg, the length of the domain, and is a typical size ofu, eg, defined by the initial condition:aDmaxxjI.x/j. The period in the dimensionless problem is 2, so the completion 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=c=L=1,x0=0.8 and there is no need to calculate the wavelengths and frequencies for estimation. We intended to insert the right-hand side matrix into the original matrix u2 for the entries corresponding to the interior points in the lattice (1:n-1or1:-1).

Fig. 2.3 Illustration of sub- sub-tracting two slices of two arrays
Fig. 2.3 Illustration of sub- sub-tracting two slices of two arrays

Simulate a standing wave

This is mathematically incorrect, but new correct values ​​will be filled in at the next time step and make it correct. Note that the user_action function reduces the need to return the solution from the solver.).

Add storage of solution in a user action function

The error mesh function and 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

Since we have used central differences in all other derivative approximations in the scheme, it is tempting to apply (2.35) atxD0andt Dtnnga the difference. We have seen in the previous section that special formulas for the boundary points arise from replacing uni1 with uniC1 when computing unC1i from the template formula fori D 0. This observation can easily be used in the coding: we just work with the general template formula. but write the code so that it is easy to replace u[i-1]byu[i+1] and vice versa.

At the border we can easily define im1=i+1 while utilization1=i-1 in the inner parts of the network. Similarly, in code you can defineIx=range(Nx+1) orIx=range(1,Q) and expressions like Ix[0]andIx[1:-1] remain correct. The solver function in the wave1D_dn.py program described in the box above accepts Dirichlet or Neumann conditions on x D 0andx D L .

However, the phantom cell technique should be seen as a purely mathematical technique, which is valid in the limit x!0 and helps us implement derivatives.

Fig. 2.4 Modified stencil at a boundary with a Neumann condition
Fig. 2.4 Modified stencil at a boundary with a Neumann condition

Generalization: Variable Wave Velocity

  • The Model PDE with a Variable Coefficient
  • Discretizing the Variable Coefficient
  • Computing the Coefficient Between Mesh Points If q is a known function of x, we can easily evaluate q iC 1
  • How a Variable Coefficient Affects the Stability
  • Neumann Condition and a Variable Coefficient
  • A More General PDE Model with Variable Coefficients
  • Generalization: Damping

Our discretization directly reflects this principle: qux at the right end of the cell minusqux at the left end because this follows from formula (2.43) or ŒDx.qDxu/ni. The notation qx also specifies that the coefficient of the variable is approximated by the arithmetic mean, and the definition is ŒqxiC1. An implementation of the variable wavelet speed schemeq.x/Dc2.x/may assume that q is available as an arrayq[i]at spatial grid points.

With Neumannux D 0 conditions on the boundary, we must combine this scheme with the discrete version of the boundary condition, as shown in Section 2.7.5. Assuming dq=dxD0 at the boundaries, we can implement the scheme at the boundary with the following code. With phantom cells we can simply reuse the formula for interior points on the boundary as well, provided the two-way phantom values ​​are updated correctly to ensure uxD0andqxD0.

In 2D and 3D, the energy of the wave spreads out in space, and energy conservation then requires the amplitude to decrease.

Building a General 1D Wave Equation Solver

  • User Action Function as a Class
  • Pulse Propagation in Two Media

The utility function in the wave1D_dn_vc.py program is the specification of the user_actionfunction as a class. You can place these vertex-shaped initial conditions at the center (loc='center') or at the left end (loc='left') of the domain. With the pulse in the middle, it splits into two parts, each with half the initial amplitude, traveling in opposite directions.

There is also a leftward pulse, but it travels from x D0 in the negativex direction and is not visible in the Œ0 domain; L. The pulse function is a flexible tool for playing with different waveforms and jumps in wave speed (ie, discontinuous media). The PlotMediumAndSolution class used here is a subclass of PlotAndStore Solution where the c-valued medium, as defined by the subject interval, is visualized in the plots.

As can be seen in the pulse function, the specified t is chosen according to the limit C D1, so if C < 1, tred remains the same, but the solver function works with a larger x and smaller Nx than specified in the call to pulse.

Exercises

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. ŒDCt uCcDxuD0ni; iDNx: (2.72) From this equation, unC1Nx can be solved and the formula can be used as the Dirichlet condition at the boundary point. Implement this scheme for the wave equation t D c2uxx in the domain Œ0; L, where you haveuxD0atx D0, condition (2.69) atxDL and an initial perturbation in the middle of the domain, e.g. plug profile as is.

Note that the initial wave is split into two parts, the left wave is reflected at x D 0 and both waves travel from x D L, so the solution is u D 0 andŒ0; L. Use the unit of Courant number so that the numerical solution is exact. Since this simplified implementation of the open boundary condition works, there is no need for the more complex discretization in a). In 2D and 3D, it is difficult to calculate the correct wave velocity normal to the boundary, which is necessary in generalizations of open boundary conditions in higher dimensions.

However, in 2D and 3D, the mode reads Ccxux Ccyuy D 0 , where cx andcy are the wave speeds in xandy directions.

Implement periodic boundary conditions

Other methods are commonly used in 2D and 3D to let waves move through a computational domain.

Compare discretizations of a Neumann condition

Applying the boundary condition atLx=2 is O.x2/correct, but the interesting question is what effect does moving the boundary have 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 equation
  • More Precise Definition of Fourier Representations
  • Stability The scheme
  • Numerical Dispersion Relation
  • Extending the Analysis to 2D and 3D

However, when I.x/ is nonzero only in a small part in the middle of the space fieldŒ0; L, which means that the boundaries are set too far from the initial perturbation u, the solution (2.76) is very clearly observed in a simulation. For a real function I.x/, the corresponding Python code for computing and plotting the discrete Fourier transform is shown in the example below. As explained, C > 1 for this very small noise will lead to exponential growth of the shortest possible wavelength component in the lattice.

For a closer examination of the error in the numerical dispersion relation when C < 1, we can study!Q !,!=!, or similar mat errors in the wave speed:Q cQcandc=c, whereQ cD!= kandcQD Q != k. That is, it predicts how well the spatial variation of the wave component is resolved in the mesh. From thers_error_leading_orderexpression above, we see that the leading order term in the error of this series expansion is.

Since we already know that the numerical solution is exact for C D 1, the remaining terms in the Taylor series expansion will also contain factors of C 1 and subtract for C D1.

Fig. 2.6 The fractional error in the wave velocity for different Courant numbers
Fig. 2.6 The fractional error in the wave velocity for different Courant numbers

Finite Difference Methods for 2D and 3D Wave Equations A natural next step is to consider extensions of the methods for various variants of

  • Multi-Dimensional Wave Equations
  • Mesh
  • Discretization

Handling Boundary Conditions Where Known The above schemes apply to internal points in the mesh. When we update them, we need to visit all grid points on the boundaries and set the prescribed value. Discretization of the Neumann condition The condition @u=@n D 0 was implemented in 1D by a center-difference discretization D2xu, followed by elimination of a fictitious point u outside the grid using a general boundary point scheme.

Alternatively, one can introduce ghost cells and update a ghost value for use in the Neumann mode. We can then just insert;1foruni;1 into this equation and solve for the limitunC1i;0 , just as it was done in 1D. The mesh is extended by an additional line (2D) or plane (3D) of ghost cells at a Neumann boundary.

In the current example it means that we need a line with ghost cells under their axis.

Implementation

  • Scalar Computations
  • Vectorized Computations
  • Verification
  • Visualization

In multidimensional problems, it makes less sense to specify a Courant number, since the wave speed is a vector and the mesh spacings can be different in the different spatial directions. The solution function in wave2D_u0.pycode updates the arrays to the next time step by switching references as described in Sect.2.4.5. Any use of u on the user side is assumed to occur in the user action function.

In the vectorized case, we need to be able to evaluate user-given functions such as I.x; y/andf .x; j; t /for the entire mesh in one operation (without loops). In the code segment above, the situation is a little more complicated, because each slice moved in one direction is accompanied by a 1:-1 slice in the other. Also in the pre_vectorized function we introduced a Boolean step1 to reuse the formula for the first step in the same way as we did with pre_scalar.

Thetest_quadraticfunction in wave2D_u0.pyprogram implements this verification as a proper test function for the pytest and nose frameworks.

Fig. 2.8 Snapshots of the surface plotted by Gnuplot
Fig. 2.8 Snapshots of the surface plotted by Gnuplot

Exercises

Check that a solution fulfills the discrete model

This is a point to start - visualization is as always a very time-consuming and experimental discipline. The particular variation between grid points can be taken as bilinear, but this is not important since we will use a Trapezoidal product rule to approximate the integral over a grid cell and then we only need to evaluate .x; y/at grid points. Then, FiC1;jC1 can be calculated by adding the integral over the last corner cell FiC1;j CFi;jC1Fi;j.

Work out the details of these calculations and implement a function that can return Fi;j for all grid indices inj. Use the fact that the trapezoidal rule is exact for linear functions and write the test function.

Implement Neumann conditions in 2D

Test the efficiency of compiled loops in 3D

Applications of Wave Equations

  • Waves on a String

Although many wave motion problems in physics can be modeled by a standard linear wave equation or a similar formulation with a system of first-order equations, there are some exceptions. Quantum mechanical waves are another example where the waves are governed by the Schrödinger equation, i.e. not the standard wave equation. Many wave phenomena must also take into account nonlinear effects when the amplitude of the wave is important.

Those with a solid background in continuum mechanics will probably have enough knowledge to fill in the details, while other readers will hopefully get a sense of the physics and approximations involved in establishing wave equation models. The cord is modeled as a set of discrete point masses (at mesh points) with elastic cords in between. The position of the mass at time isxii Cui.t /j, where andj are unit vectors in the xandy direction, respectively.

Gambar

Fig. 2.1 Mesh in space and time. The circles show points connected in a finite difference equation
Fig. 2.2 Modified stencil for the first time step
Fig. 2.3 Illustration of sub- sub-tracting two slices of two arrays
Fig. 2.4 Modified stencil at a boundary with a Neumann condition
+7

Referensi

Dokumen terkait

Mathew, Domain Decomposition Methods for the Numerical Solution of Partial Differential Equations.. Graziani ed., Computational Methods in Transport: Verification and