The section Simple model problems and their solutions lists three functions for
computing the analytical solution of some simple model problems. There
is quite some repetitive code, suggesting that the functions can
benefit from being refactored into a class hierarchy, where the super
class solves \( -(a(x)u'(x))'=f(x) \) and where subclasses define the
equations for the boundary conditions in a model. Make a method for
returning the residual in the differential equation and the boundary
conditions when the solution is inserted in these equations. Create a
test function that verifies that all three residuals vanish for each
of the model problems in the section Simple model problems and their solutions. Also
make a method that returns the solution either as sympy
expression
or as a string in LaTeX format. Add a fourth subclass for the problem
\( -(au')'=f \) with a Robin boundary condition:
$$ u(0)=0,\quad -u'(L) = C(u - D)\tp$$
Demonstrate the use of this subclass for the case \( f=0 \) and \( a=\sqrt{1+x} \).
Filename: uxx_f_sympy_class
.
A hanging cable of length \( L \) with significant tension \( T \) has a deflection \( w(x) \) governed by $$ T w''(x) = \ell(x), $$ where \( \ell(x) \) the vertical load per unit length. The cable is fixed at \( x=0 \) and \( x=L \) so the boundary conditions become \( w(0)=w(L)=0 \). The deflection \( w \) is positive upwards, and \( \ell \) is positive when it acts downwards.
If we assume a constant load \( \ell(x)=\hbox{const} \), the solution is expected to be symmetric around \( x=L/2 \). For a function \( w(x) \) that is symmetric around some point \( x_0 \), it means that \( w(x_0-h) = w(x_0+h) \), and then \( w'(x_0)=\lim_{h\rightarrow 0}(w(x_0+h)- w(x_0-h))/(2h)=0 \). We can therefore utilize symmetry to halve the domain. We then seek \( w(x) \) in \( [0,L/2] \) with boundary conditions \( w(0)=0 \) and \( w'(L/2)=0 \).
The problem can be scaled by introducing dimensionless independent and dependent variables, $$ \bar x = \frac{x}{L/2},\quad \bar u = \frac{w}{w_c},$$ where \( w_c \) is a characteristic size of \( w \). Inserted in the problem for \( w \), $$ \frac{4Tw_c}{L^{2}}\frac{d^2\bar u}{d\bar x^2} = \ell\ (= \hbox{const})\tp$$ A desire is to have \( u \) and its derivatives about unity, so choosing \( w_c \) such that \( |d^2\bar u/d\bar x^2|=1 \) is an idea. Then \( w_c=\frac{1}{4}\ell L^2/T \), and the problem for the scaled vertical deflection \( u \) becomes $$ u'' = 1,\quad x\in (0,1),\quad u(0)=0,\ u'(1)=0\tp $$ Observe that there are no physical parameters in this scaled problem. From now on we have for convenience renamed \( x \) to be the scaled quantity \( \bar x \).
a) Find the exact solution for the deflection \( u \).
b) A possible function space is spanned by \( \baspsi_i=\sin ((2i+1)\pi x/2) \), \( i=0,\ldots,N \). These functions fulfill the necessary condition \( \baspsi_i(0)=0 \), but they also fulfill \( \baspsi_i'(1)=0 \) such that both boundary conditions are fulfilled by the expansion \( u=\sum_jc_j\basphi_j \).
Use a Galerkin and a least squares method to find the coefficients \( c_j \) in \( u(x)=\sum_j c_j\baspsi_j \). Find how fast the coefficients decrease in magnitude by looking at \( c_j/c_{j-1} \). Find the error in the maximum deflection at \( x=1 \) when only one basis function is used (\( N=0 \)).
Hint.
In this case, where the basis functions and their derivatives are
orthogonal, it is easiest to set up the calculations by hand and
use sympy
to help out with the integrals.
c) Visualize the solutions in b) for \( N=0,1,20 \).
d) The functions in b) were selected such that they fulfill the condition \( \baspsi'(1)=0 \). However, in the Galerkin method, where we integrate by parts, the condition \( u'(1)=0 \) is incorporated in the variational form. This leads to the idea of just choosing a simpler basis, namely "all" sine functions \( \baspsi_i = \sin((i+1)\frac{\pi x}{2}) \). Will the method adjust the coefficient such that the additional functions compared with those in b) get vanishing coefficients? Or will the additional basis functions improve the solution? Use Galerkin's method.
e) Now we drop the symmetry condition at \( x=1 \) and extend the domain to \( [0,2] \) such that it covers the entire (scaled) physical cable. The problem now reads $$ u'' = 1,\quad x\in (0,2),\quad u(0)=u(2)=0\tp$$ This time we need basis functions that are zero at \( x=0 \) and \( x=2 \). The set \( \sin((i+1)\frac{\pi x}{2}) \) from d) is a candidate since they vanish \( x=2 \) for any \( i \). Compute the approximation in this case. Why is this approximation without the problem that this set of basis functions introduced in d)?
Filename: cable_sin
.
a) Repeat Exercise 2: Compute the deflection of a cable with sine functions b), but work with the space $$ V = \hbox{span}\{x, x^2, x^3, x^4, \ldots\}\tp $$ Choose the dimension of \( V \) to be 4 and observe that the exact solution is recovered by the Galerkin method.
Hint.
Use the solver
function from varform1D.py
.
b) What happens if we use a least squares method for this problem with the basis in a)?
Filename: cable_xn
.
Consider the Galerkin method for the problem involving \( u \) in Exercise 2: Compute the deflection of a cable with sine functions. Show that the formulas for \( c_j \) are independent of whether we perform integration by parts or not.
Filename: cable_integr_by_parts
.
Solve the problem for \( u \) in Exercise 2: Compute the deflection of a cable with sine functions using two P1 linear elements. Incorporate the condition \( u(0)=0 \) by two methods: 1) excluding the unknown at \( x=0 \), 2) keeping the unknown at \( x=0 \), but modifying the linear system.
Filename: cable_2P1
.
Solve the problem for \( u \) in Exercise 2: Compute the deflection of a cable with sine functions using one P2 element with quadratic basis functions.
Filename: cable_1P2
.
We consider the deflection of a tension cable as described in Exercise 2: Compute the deflection of a cable with sine functions: \( w''=\ell \), \( w(0)=w(L)=0 \). Now the load is discontinuous: $$ \ell (x) =\left\lbrace\begin{array}{ll} \ell_1, & x < L/2,\\ \ell_2, & x \geq L/2 \end{array}\right.\quad x\in [0,L] \tp $$ This load is not symmetric with respect to the midpoint \( x=L/2 \) so the solution loses its symmetry. Scaling the problem by introducing $$ \bar x = \frac{x}{L/2},\quad u = \frac{w}{w_c},\quad\bar\ell = \frac{\ell - \ell_1}{\ell_2 - \ell_1}\tp$$ This leads to a scaled problem on \( [0,2] \) (we rename \( \bar x \) as \( x \) for convenience): $$ u'' = \bar\ell(x) = \left\lbrace\begin{array}{ll} 1, & x < 1,\\ 0, & x \geq 1 \end{array}\right. \quad x\in (0,1),\quad u(0)=0,\ u(2)=0 \tp $$
a) Find the analytical solution of the problem.
Hint. Integrate the equation separately for \( x < 1 \) and \( x>1 \). Use the conditions that \( u \) and \( u' \) must be continuous at \( x=1 \).
b) Use \( \baspsi_i = \sin((i+1)\frac{\pi x}{2}) \), \( i=0,\ldots,N \) and the Galerkin method to find an approximate solution \( u=\sum_j c_j\baspsi_j \). Plot how fast the coefficients \( c_j \) tend to zero (on a log scale).
c) Solve the problem with P1 finite elements. Plot the solution for \( N_e=2,4,8 \) elements.
Filename: cable_discont_load
.
a) Derive the linear system for the problem \( -u''=2 \) on \( [0,1] \), with \( u(0)=0 \) and \( u(1)=1 \), using P1 elements and a non-uniform mesh. The vertices have coordinates \( \xno{0}=0 < \xno{1} < \cdots < \xno{N_n-1}=1 \), and the length of cell number \( e \) is \( h_e = \xno{e+1} -\xno{e} \).
b) It is of interest to compare the discrete equations for the finite element method in a non-uniform mesh with the corresponding discrete equations arising from a finite difference method. Go through the derivation of the finite difference formula \( u''(x_i) \approx [D_x D_x u]_i \) and modify it to find a natural discretization of \( u''(x_i) \) on a non-uniform mesh. Compare the finite element and difference discretizations
Filename: nonuniform_P1
.
The following scaled 1D problem is a very simple, yet relevant, model for convective transport in fluids: $$ \begin{equation} u' = \epsilon u'' ,\quad u(0)=0,\ u(1)=1,\ x\in [0,1] \tp \tag{96} \end{equation} $$
a) Find the analytical solution to this problem. (Introduce \( w=u' \), solve the first-order differential equation for \( w(x) \), and integrate once more.)
b) Derive the variational form of this problem.
c) Introduce a finite element mesh with uniform partitioning. Use P1 elements and compute the element matrix and vector for a general element.
d) Incorporate the boundary conditions and assemble the element contributions.
e) Identify the resulting linear system as a finite difference discretization of the differential equation using $$ [D_{2x}u = \epsilon D_xD_x u]_i \tp $$
f) Compute the numerical solution and plot it together with the exact solution for a mesh with 20 elements and \( \epsilon=10, 1, 0.1, 0.01 \).
Filename: convdiff1D_P1
.
Consider $$ -u''(x)=x^m,\quad x\in (0,L),\quad u'(0)=C,\ u(L)=D,$$ where \( m\geq 0 \) is an integer, and \( L \), \( C \), and \( D \) are given numbers. Utilize a mesh with two (non-uniform) elements: \( \Omega^{(0)}=[0,3] \) and \( \Omega^{(0)}=[3,4] \). Plot the exact solution and the finite element solution for \( d=1,2,3,4 \) and \( m=0, 1, 2, 3, 4 \). Find values of \( d \) and \( m \) that make the finite element solution exact at the nodes in the mesh.
Hint.
Use the mesh_uniform
, finite_element1D
, and u_glob2
functions
from the fe1D.py
module.
Filename: u_xx_xm_P1to4
.
We consider the Poisson problem in a disk with radius \( R \) with Dirichlet conditions at the boundary. Given that the solution is radially symmetric and hence dependent only on the radial coordinate (\( r=\sqrt{x^2+y^2} \)), we can reduce the problem to a 1D Poisson equation $$ \begin{equation} -\frac{1}{r}\frac{d}{dr}\left( r\frac{du}{dr}\right) = f(r),\quad r\in (0,R),\ u'(0)=0,\ u(R)=U_R \tp \tag{97} \end{equation} $$
a) Derive a variational form of (97) by integrating over the whole disk, or posed equivalently: use a weighting function \( 2\pi r v(r) \) and integrate \( r \) from \( 0 \) to \( R \).
b) Use a uniform mesh partition with P1 elements and show what the resulting set of equations becomes. Integrate the matrix entries exact by hand, but use a Trapezoidal rule to integrate the \( f \) term.
c) Explain that an intuitive finite difference method applied to (97) gives $$ \frac{1}{r_i}\frac{1}{h^2}\left( r_{i+\half}(u_{i+1}-u_i) - r_{i-\half}(u_{i}-u_{i-1})\right) = f_i,\quad i=rh \tp $$
For \( i=0 \) the factor \( 1/r_i \) seemingly becomes problematic. One must always have \( u'(0)=0 \), because of the radial symmetry, which implies \( u_{-1}=u_1 \), if we allow introduction of a fictitious value \( u_{-1} \). Using this \( u_{-1} \) in the difference equation for \( i=0 \) gives $$ \begin{align*} &\frac{1}{r_0}\frac{1}{h^2}\left( r_{\half}(u_{1}-u_0) - r_{-\half}(u_{0}-u_{1})\right) = \\ & \qquad \frac{1}{r_0}\frac{1}{2h^2}\left( (r_0 + r_1)(u_{1}-u_0) - (r_{-1} + r_0)(u_{0}-u_{1})\right) \approx 2(u_1-u_0), \end{align*} $$ if we use \( r_{-1}+r_1\approx 2r_0 \).
Set up the complete set of equations for the finite difference method and compare to the finite element method in case a Trapezoidal rule is used to integrate the \( f \) term in the latter method.
Filename: radial_Poisson1D_P1
.
Consider the problem $$ \begin{equation} -\frac{d}{dx}\left( \dfc(x)\frac{du}{dx}\right) + \gamma u = f(x), \quad x\in\Omega=[0,L],\quad u(0)=\dfc,\ u'(L)=\beta\tp \tag{98} \end{equation} $$ We choose \( \dfc(x)=1+x^2 \). Then $$ \begin{equation} u(x) = \dfc + \beta(1+L^2)\tan^{-1}(x), \tag{99} \end{equation} $$ is an exact solution if \( f(x) = \gamma u \).
Derive a variational formulation and compute general expressions for the
element matrix and vector in an arbitrary element, using P1 elements
and a uniform partitioning of \( [0,L] \). The right-hand side
integral is challenging and can be computed by a numerical integration
rule. The Trapezoidal rule \eqref{fem:approx:fe:numint1:trapez}
gives particularly simple expressions.
Filename: atan1D_P1
.
The classical problem of applying a torque to the ends of a rod can be modeled by a Poisson equation defined in the cross section \( \Omega \): $$ -\nabla^2 u = 2,\quad (x,y)\in\Omega,$$ with \( u=0 \) on \( \partial\Omega \). Exactly the same problem arises for the deflection of a membrane with shape \( \Omega \) under a constant load.
For a circular cross section one can readily find an analytical solution. For a rectangular cross section the analytical approach ends up with a sine series. The idea in this exercise is to use a single basis function to obtain an approximate answer.
We assume for simplicity that the cross section is the unit square: \( \Omega = [0,1]\times [0,1] \).
a) We consider the basis \( \baspsi_{p,q}(x,y) = \sin((p+1)\pi x)\sin (q\pi y) \), \( p,q=0,\ldots,n \). These basis functions fulfill the Dirichlet condition. Use a Galerkin method and \( n=0 \).
b) The basis function involving sine functions are orthogonal. Use this property in the Galerkin method to derive the coefficients \( c_{p,q} \) in a formula \( u=\sum_p\sum_q c_{p,q}\baspsi_{p,q}(x,y) \).
c) Another possible basis is \( \baspsi_i(x,y) = (x(1-x)y(1-y))^{i+1} \), \( i=0,\ldots,N \). Use the Galerkin method to compute the solution for \( N=0 \). Which choice of a single basis function is best, \( u\sim x(1-x)y(1-y) \) or \( u\sim \sin(\pi x)\sin(\pi y) \)? In order to answer the question, it is necessary to search the web or the literature for an accurate estimate of the maximum \( u \) value at \( x=y=1/2 \).
Filename: torsion_sin_xy
.