Matrix and Vector Computation

Introduction to the Problem

Regardless of what problem you are trying to solve on a computer, you will encounter vector computation at some point. Vector calculations are integral to how a computer works and how it tries to speed up runtimes of programs down at the silicon level—the only thing the computer knows how to do is operate on numbers, and knowing how to do several of those calculations at once will speed up your program.

  • In order to explore matrix and vector computation in this chapter, we will repeatedly use the example of diffusion in fluids.
  • Diffusion is one of the mechanisms that moves fluids and tries to make them uniformly mixed.One simple example of diffusion is dye in water: if you put several drops of dye into water at room temperature it will slowly move out until it fully mixes with the water.
  • 𝜕/𝜕t u(x,t)=D 𝜕^2/(𝜕𝑥^2 )u(x,t)𝜕/𝜕tux,t=D*𝜕/2𝜕𝑥2∂/∂t u(x,t)=D*∂^2/(∂x^2 )u(x,t)
  • All this being said, the most important thing to know about diffusion for our purposes is its formulation. Stated as a partial differential equation in one dimension (1D), the diffusion equation is written as:
  • In this formulation, u is the vector representing the quantities we are diffusing. D is a physical value that represents properties of the fluid we are simulating. A large value of D represents a fluid that can diffuse very easily. For simplicity, we will set D = 1 for our code, but still include it in the calculations. 
  • Here, the diffusion equation will be taken continuously in space and time, and approximate it using discrete volumes and discrete times. Such as this formulation
  • 𝜕/𝜕t u(x,t)=(u(x,t+dt)-u(x,t))/dt
  • dt is now a fixed number. This fixed number represents the time step, or the resolution in time.
  • In fact, as dt approaches zero, this approximation becomes exact
  • Doing a similar trick for the derivative in x using the finite differences approximation, we arrive at the final equation:
  • u(x,t+dt)=u(x,t)+dt*D*(u(x+dx+t)+u(x-dx,t)-2u(x,t))/(dx^2 )

Pseudocode for 1D diffusion

# Create the initial conditions
u = vector of length N for i in range(N):
u=0ifthereiswater,1ifthereisdye
# Evolve the initial conditions
D=1 t=0
dt = 0.0001 while True:
print "Current time is: %f" % t unew = vector of size N
    # Update step for every cell
for i in range(N):
    unew[i] = u[i] + D * dt * (u[(i+1)%N] + u[(i-1)%N] - 2 * u[i]) # Move the u                
    pdated solution into u
u=unew

visualize(u)

 

This code will take some initial condition of the dye in water and tell us what the system looks like at every 0.0001-second interval in the future. The results of this can be seen in Figure, where we evolve our very concentrated drop of dye (represented by the top-hat function) into the future. We can see how, far into the future, the dye becomes well mixed, to the point where everywhere has a similar concentration of the dye.

Algorithm for calculating 2D diffusion

This numerical diffusion equation in 2D translates to the pseudocode like this, using the same methods we used before. We can now put all of this together and write the full Python 2D diffusion code that we will use as the basis for the rest of this chapter. While the code looks more complicated, the results are similar to that of the 1D diffusion. The result can be seen in the picture.

∂u(x,y,t)=D·(∂2 u(x,y,t)+ ∂2 u(x,y,t))

for i in range(N): for j in range(M):
unew[i][j] = u[i][j] + dt * ( \ (u[(i+1)%N][j] + u[(i-1)%N][j] - 2 * u[i][j]) + \ # d^2 u / dx^2 (u[i][(j+1)%M] + u[j][(j-1)%M] - 2 * u[i][j]) \ # d^2 u / dy^2
)

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章