• Tidak ada hasil yang ditemukan

Thư viện số Văn Lang: Solving PDEs in Python: The FEniCS Tutorial I

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

Academic year: 2023

Membagikan "Thư viện số Văn Lang: Solving PDEs in Python: The FEniCS Tutorial I"

Copied!
45
0
0

Teks penuh

This is our time-discrete version of the heat equation (3.1), the so-called inverse Euler or implicit Euler discretization. In solving this variational problem, u0 becomes the projection L2 of a given initial value of u0 into finite element space. In the last step of the time loop, we assign the values ​​of the variable u (the new calculated solution) to the variable u_n, which contains the values ​​in the previous time step.

The time step loop above contains no comparison of the numerical and the exact solutions, which we need to include to verify the implementation. Note that we have used a much higher resolution than before to better resolve the features of the solution. To visualize the spread of the Gaussian tray, launch ParaView, select File–Open.., openheat_gaussian/solution.pvd and click Apply in the Properties pane.

Fig. 3.1 A sequence of snapshots of the solution of the Gaussian hill problem created with ParaView.
Fig. 3.1 A sequence of snapshots of the solution of the Gaussian hill problem created with ParaView.

Variational formulation

FEniCS implementation

However, we can use SymPy for symbolic computation and integrate such computations into the FEniCS solver. The main difference from a linear problem is that the unknown functionu in the variational form in the non-linear case must be defined as a Function, not as a TrialFunction. With 2·(8×8) cells, we reach convergence in eight iterations with a tolerance of 10−9, and the error in the numerical solution is about 10−16.

However, experts in the numerical solution of nonlinear PDEs know well that automated procedures can fail for nonlinear problems and that it is often necessary to have much better manual control over the solution process than we have in the present case .

The equations of linear elasticity

PDE problem

In deriving the variational formulation, however, it is convenient to keep the equations split as above.

Variational formulation

Note that the boundary integral on the remaining part∂Ω\∂ΩT vanishes due to the Dirichlet condition. If we express ∇vas a sum of its symmetric and anti-symmetric parts, only the symmetric part will survive in the product σ:∇v since σ is a symmetric tensor.

FEniCS implementation

We first list the code and then comment on the new constructions compared to the previous examples we've seen. With u = Function(V), we get a three-component vector-valued finite element function for this 3D problem. This is not strictly necessary in this problem, but is generally recommended for vector PDEs derived from continuum mechanics if you interpret∇ as a vector in PDE notation; see the box about nabla_grad in section 3.4.2.

It is often advantageous to scale a problem, as it reduces the need to set physical parameters and one obtains dimensionless numbers that reflect the competition between parameters and physical effects. We develop the code for the original model with dimensions and run the scaled problem by adjusting the parameters appropriately. One option for the scaling is to choose U so that γ is of unit size (U=%gL2/µ).

However, in elasticity this leads to displacements of the size of the geometry, which makes plots look very strange. So we want the characteristic displacement to be a small fraction of the characteristic length of the geometry. The dimensionless parameter δ is therefore very important in the problem (as expected, since δ1 is what gives beam theory!).

If E is of the same order as µ, which is the case for many materials, we realize that γ∼δ−2 is an appropriate choice. Experimenting with the code to find a displacement that “looks good” on graphs of the deformed geometry points to γ= 0.4δ−2 as our final choice of γ.

The Navier–Stokes equations

PDE problem

The difference is in the two additional terms %(∂u/∂t+u ∇u) and the other expression for the stress tensor. The two additional terms express the acceleration balanced by the force F=∇ ·σ+f per unit volume in Newton's second law of motion.

Variational formulation

This notation, suitable for problems involving many terms in the variation formulations, requires some explanation. Third, we note that the variation problem (3.32) arises from the integration into parts of the term h−∇ ·σ, vi. The assumption we then make is that the derivative of the velocity in the direction of the channel is zero at the outflow, which corresponds to a flow.

By doing so, the remaining boundary term at the outflow becomes spn−µ∇u·n, which is the term appearing in the variational problem (3.32). We choose the latter here, ∂uj/∂xi, which means that we have to use the FEniCS operator nabla_grad for the implementation. If we use the grad operator and the definition∂ui/∂xj, we must instead keep the termspn−µ(∇u)>·n.

For the Navier-Stokes equations it is important to consider the termu ∇u, which should be interpreted as the vector w with elements wi=P. From the Navier-Stokes equations we can easily see what ∇ means: if the convective term has the form u ∇u, which actually means (u ∇)u, then ∇ is a vector and the implementation becomes dot(u, nabla_grad (u )) in FEniCS, but if we see ∇u uor(gradu) u, the corresponding FEniCS expression is dot(grad(u), u). We now move on to the second step in our splitting scheme for the incompressible Navier-Stokes equations.

In the first step, we calculated the approximate speed. given the pressure from the previous time step. One way of thinking about this step is to subtract the Navier–Stokes momentum equation (3.29) expressed in terms of the trial velocity u.

FEniCS implementation

This is the driving force of the flow and can be determined as a known parameter in the problem. The first space V is the space of functions with vector values ​​for velocity, and the second space Q is the space of functions with scalar values ​​for pressure. We use partially quadratic elements for velocity, and partially linear elements for pressure.

Stable finite element spaces for the Navier–Stokes equations It is well known that certain finite element spaces are not stable for the Navier–Stokes equations, or even for the simpler Stokes equations. The simple solution is to use continuous piecewise quadratic elements for the velocity and continuous piecewise linear elements for the pressure. At the end, we collect the boundary conditions for the velocity and print in Python lists so that we can easily access them in the next calculation.

There are three variational problems to be defined, one for each step in the IPCS scheme. The next step is to set up the variational form for the first step (3.32) in the solution process. In the last step, FEniCS uses the overloaded solve function to solve the linear system AU = b where U is the vector of degrees of freedom for the function u(x) =P.

New code will also be generated and compiled when a float value for the time step is changed. The complete code for 2D channel flow simulation with FEniCS can be found in fileft07_navier_stokes_channel.py. To solve the cylinder test problem, we only need to make a few small changes to the code we wrote for the duct flow test case.

For the visualization of the velocity we used the Glyph filter to visualize the vector velocity field.

Fig. 3.3 Plot of the velocity profile at the final time for the Navier–Stokes channel flow example.
Fig. 3.3 Plot of the velocity profile at the final time for the Navier–Stokes channel flow example.

A system of advection–diffusion–reaction equations

PDE problem

The reader should be advised that this example program is significantly more demanding than our previous examples in terms of CPU time and memory, but it should be possible to run the program on a fairly modern laptop. We see that the chemical reactions are accounted for on the right-hand side of the PDE system. In addition, we assume that the species A, B and C diffuse throughout the domain with diffusivity (the terms−∇ ·(∇ui)) and are advected with velocityw (the termsew· ∇ui).

To make things visually and physically interesting, we will let the chemical reaction take place in the velocity field calculated from the solution of the incompressible Navier-Stokes equations around a cylinder from the previous section. We assume atu1=u2=u3= 0att= 0and inject the speciesAogBin into the system by specifying non-zero source termsf1 andf2near the vertices at the inflow, and takef3=0. The result will be that A and B are convected by advection and diffusion through the channel, and when they mix, species C will be formed.

Since the system is one-way coupled from the Navier-Stokes subsystem to the advection-diffusion-reaction subsystem, we do not need to recompute the solution to the Navier-Stokes equations, but can only read back the previous one. But we need to learn how to read and write solutions to time-dependent PDE problems.

Variational formulation

FEniCS implementation

The first space we create is the space for the velocity field w from the Navier–Stokes simulation. Importantly, this space is exactly the same as the space we used for the velocity field in the Navier–Stokes solver. For the three concentrations u1, u2 and u3, we want to create a mixed space with functions that represent the entire system (u1, u2, u3) as a single entity.

For the reaction system, we create the mixed element for element = MixedElement([P1, P1, P1]), and one would be tempted to write. Once the space is created, we need to define our test functions and finite element functions. Since the problem is non-linear, we have to work with functions rather than trial functions for the unknowns.

In each time step, we first read the current value for the velocity field from the time series that we previously saved. In our case, we didn't have to worry about setting the initial condition, since we start with u1=u2 =u3 = 0. If we want to set the initial conditions for the system components separately, the simplest solution is to define the initial conditions as a vector-valued expression and then project that ( or interpolate) to a function that represents the entire system.

In our example, we also didn't have to worry about setting boundary conditions since we used a natural Neumann mode. If we want to set Dirichlet conditions for individual components of the system, this can be done as usual by the class DirichletBC, but we need to specify for which subsystem we set the boundary condition.

Fig. 3.7 Plot of the concentrations of the three species A, B, and C (from top to bottom) at final time.
Fig. 3.7 Plot of the concentrations of the three species A, B, and C (from top to bottom) at final time.

Gambar

Fig. 3.1 A sequence of snapshots of the solution of the Gaussian hill problem created with ParaView.
Fig. 3.2 Plot of gravity-induced deflection in a clamped beam for the elasticity prob- prob-lem.
Fig. 3.3 Plot of the velocity profile at the final time for the Navier–Stokes channel flow example.
Fig. 3.4 Geometry for the flow past a cylinder test problem. Notice the slightly per- per-turbed and unsymmetric geometry.
+4

Referensi

Dokumen terkait

Klän1,2 Affiliations: 1Systematic Theology, Lutherische Theologische Hochschule, Germany 2Department of Church History and Church Polity, University of Pretoria, South Africa Note: