A sample of toy FDM code
Solving equation
Time integration is full implicit, and during each time step Newton’s method is employed.
PROGRAM MAIN
!--------------------------------------------------
!Implicit time evolution of equation
!u_t-u''=f(x,u)
!f(x,u)=-2+r*(sin(u)-sin(x^2)),r=2
!--------------------------------------------------
! t=time points
! dt=time step
! h=neighboring nodes distance
! u=unknown
! x=node location
! n=No. of unknowns
!USE SUNPERF
IMPLICIT NONE
INTEGER(4) :: n
REAL(4) :: elapse,time(2),etime
REAL(8) :: t=0,dt=0.1
REAL(8) :: h
REAL(8),ALLOCATABLE :: u(:),x(:)
INTEGER(4) :: count,i
n=50
h=1/(DBLE(n)+1.)
ALLOCATE (x(n),u(n))
x=(/(i*h,i=1,n)/)
u=(/((i*h)**0.5,i=1,n)/)
OPEN(UNIT=100,FILE='data',STATUS='UNKNOWN',ACTION='&
&WRITE')
DO i=1,n
WRITE(100,*) t,x(i),u(i)
END DO
DO WHILE (t<1.0)
CALL newton(n,h,dt,u,x)
t=t+dt
DO i=1,n
WRITE(100,*) t,x(i),u(i)
END DO END DO CLOSE(UNIT=100)
elapse=etime(time)
WRITE(*,*) 'elapsed:',elapse
END PROGRAM MAIN
!--------------------------------------------------
SUBROUTINE newton(n,h,dt,u,x)
IMPLICIT NONE
INTEGER(4),INTENT(IN) :: n
REAL(8),INTENT(IN) :: x(n),h,dt
REAL(8),INTENT(INOUT) :: u(n)
REAL(8) :: r=2,c(n),error,b(n,1),b0(n,1),a(2,n)
REAL(8) :: alpha=1.0,beta=-1.0
INTEGER(4) :: info,i,iter
c=u*h*h/dt
iter=0
error=1
DO WHILE (error>1.e-13)
iter=iter+1
a(1,:)=-1
a(2,:)=2+h*h/dt
b(:,1)=(-2+r*(SIN(u)-SIN(x*x)))*h*h+c
b(n,1)=b(n,1)+1
CALL DSBMV('u',n,1,alpha,a,2,u,1,beta,b,1)
b=-b
a(2,:)=a(2,:)-r*COS(u)*h*h
CALL DGTSV(n,1,a(1,2:),a(2,:),a(1,2:),b,n,info)
error=MAXVAL(ABS(b))
u=u+b(:,1)
END DO
WRITE(*,*) 'INFO',info,'Number of iterations:',iter
END SUBROUTINE newton
!--------------------------------------------------
Output would be like:
% f95 -dalign implicit_newton.f95 -xlic_lib=sunperf % a.out INFO 0 Number of iterations: 4 INFO 0 Number of iterations: 4 INFO 0 Number of iterations: 4 INFO 0 Number of iterations: 4 INFO 0 Number of iterations: 4 INFO 0 Number of iterations: 3 INFO 0 Number of iterations: 3 INFO 0 Number of iterations: 3 INFO 0 Number of iterations: 3 INFO 0 Number of iterations: 3 elapsed: 0.061407
Of course the exact solution is . Here is time evolution plot of the numerical solution.
Mixed-Eularian-Lagrangian method
In last post I talked wrote about a time integration for NS equation. As a special case, time update for potential flow is actually easier, due to fact that
1. the governing equation, i.e. the Laplace equation is linear.
2. the solution dependes only on the boundary condition, in which only the free surface boundary condition is the tricky part.
3. Even though the field equation is in Eularian representation, the free surface can be described using Lagrangian representation.
For free surface potential flow, the time updating of the problem is twofold: update the (free surface) geometry and update the boundary conditioin at the free surface. Without mentioning the boundary condition at other part of the domain boundary, we are solving the problem characterized by
Field equation:
Free surface kinetic boundary condition(FSKBC):
Free surface dynamic boundary condition(FSDBC):
The first boundary condition says that the particle velocity for material particle on the free surface, i.e. the velocity of free surface, equals to the flow velocity defined by velocity potential. The second boundary condition is the Bernoulli equation, with constant on RHS assigned to 0, which could be looked as consumed by . Remembering that
FSDBC can be converted to
The plan of mixed-Eularian-Lagrangian(MEL) method, is to solve at time
, then use the BC equations to update the boundary condition. One should remember, the LHS of BC equations use material particle as variable, i.e. in Lagrangian representation. Denote particle on the free surface by
, MEL is characterized by two steps:
1. Solve field equation with BCs with variable at , i.e.
;
2. Update the BCs at the free surface using
As mentioned at the end of last post, usually the computing cost is characterized by the first step, while the second step concerns the stability and convergence.
Time integration again
In the post time integration: incompressible flow I explained a scheme example using PFEM. I don’t think it’s really “meshless”, even though by definition of same group it is. Personally, I more tend to see it a FEM with time-updated elements and shape functions, at lease mathematically. Anyway, the nice way of time spitting is what interests me here. Within an iteration, the variables are found following:
For each step above, equation for the unknowns to be solved are: Elliptic equation for , elliptic equation for
and algebra/finite difference for
. This means for this particular time updating plan, a single linear elliptic equation solver can be used. Remember, this virtue is due to two things, the first is the MEL to eliminate the annoying nonlinear convection term, second, the time splitting using
as the intermediate variable. In summary, this scheme, as many other ones, gains the sequential solution by paying the price with solving one more variable (implicitly). It can be seen that within one iteration the solving of field equation costs most of the computing, while the choice of time integration scheme is the main source of stability issues.
Time integration: incompressible flow
I try to consolidate last post using a scheme for incompressible flow under Lagrangian description. Those two condition make somehow make the plan much easier against general case. Since for the moment I am reading this paper, I will take some of its ideas, even though this whole modern time splitting scheme by projection methods owns its origin to Chorin and Temam in 1960′s.
Consider the governing equations:
(MEL-in mixed-Eulerian-Lagrangian form) or
(Eulerian form)
For a specific material particle , both the displacement and velocity are functions of time:
Now, let’s suppose the variables at time are known as
. For velocity we have the general time integration:
In equation above, partial derivative by means take the derivative within the spatial configuration of time
. This spatial domain is obtained using velocity updating:
Parameter varies between 0 and 1. When
, RHS is based on time
, i.e. implicit scheme, while when
we have explicit scheme. Generally, implicit schemes are prefered for its stability merit, which I adopt in this post. So we have
There are two choices to implement the discrete governing equations. One is two resolve all together, by coupling equation above with mass conservation equation of Eulerian form
. The other one is try to update velocity separately, for this purpose we have to transform the mass equation, because it is this equation that couples velocities in different directions. On the other hand, we also want to decouple the pressure and velocity. Before introduce the scheme, let’s first take a look at the physical interpretation of the coordnate system.
When solving the dynamical problem numerically, we hope at one time step, we take care of time derivatives and space derivatives separately. Suppose I am examining a fixed time step , the material particle indexed by
can actually be indexed by the spatial position (in Eulerian coordinates)
. In other words, when we talk about some specific material particle, it can be labeled with location
at time
. So
simply means the
location of some particle which at
occupies
. By this notation, the momentum equation, assuming constant density and body force, is
and the mass equation is
In order to decouple velocity and pressure in momentum equation, we split it:
The two terms on both side form pairs accordingly. The second terms gives an implicit scheme for intermediat variable . Similar split in mass equation gives
which gives , then the first terms in split momentum equation is used to obtain
:
The three equations above, are to be used to find sequentially. This means to solve two spatial domain PDEs for the first two equations, and take one derivative for the last.
Time integration in a dynamical FEM prob.
For any dynamical problems involving spatial evolution, each step is to be solved by proposed spatial discretization scheme. But unlike steady/static problem, which requires no particular time updating, dynamical problem requires discretization in one more dimension, the time.
This leads to one of the most important and, to me, most messy part of numerical simulation. Say, at any time , spatial problem is (to skip the boundary condition)
with time evolusion equation
in which both and
are global operators in the spatial domain. At each time like
, the first equation is to be solved, in discretized version, one or another. And the second equation is used to formulate the spatial domain in next step, after the local velocity
is resolved and used to update the spatial configuration using
Here comes the tricky part. In equation above, which time to use for velocity? In particular, should one use or
? And to the next step, when one tries to solve (this is so called “time integration”, because it’s discrete version of taking integral) differential equation of
, what time should be introduced on RHS?
There are various time updating plans in use, which can be catogorized into implicit and explicit families, depending on whether the unresolved variable at next time step show up on RHS. And the story continues in balancing the advantage/disadvatange of those catogories.

1 comment