How Can I Implement a Three-Variable Advection Equation in Python?

  • Thread starter leonmate
  • Start date
  • Tags
    Python
In summary, the conversation is about writing an advection equation using 3 variables and the necessary code to solve it. The conversation also includes information about the variables, methods, and initial conditions. The individual seeking help is unsure about how to proceed with the code and is asking for advice on how to approach the problem. The expert suggests breaking down the steps and creating placeholder functions for complicated steps in order to make the program run and be debugged.
  • #1
leonmate
84
1

Homework Statement



What I'm trying to achieve is an advection equation that will run using 3 variables. I must confess I don't have a great grasp over this part of my course. Its pretty complicated!

Homework Equations



The code I have at the moment is:

Code:
def SolveAdvectionEquation(a, b, t0, t1, Nx, Nt, v, method):
dx = (float) (b-a)/Nx
dt = (t1-t0)/Nt
# We have Nx+1 points for Nx intervals, plus an extra ghost point.
pts = linspace(a-dx,b,Nx+2)
times = linspace(t0,t1,Nt+1)
solndata = zeros((Nt+1,Nx+2))
sigma2 = 2.*0.1**2
solndata[0] = exp(-((pts-0.5)**2)/(sigma2))
solndata[0] = ApplyBoundaryConditions(solndata[0])
for tn in range(0,Nt):
solndata[tn+1] = method(solndata[tn],dx,dt,v)
solndata[tn+1] = ApplyBoundaryConditions(solndata[tn+1])
return times, pts[1:], solndata[:,1:]
Our variables:
a and b are my limits for x. For my example this is -1 and 1
likewise t0 and t1 are my limits for time
Nt and Nx are the number of time and x intervals (there will be Nt+1 times from t=0 to t=T and Nx+1 points in the interval of physical interest, x=a to x=b)
v is our Courant factor (dt/dx)
the method I want to use is Euler, I have a function made for this too:

Code:
def Euler(y0,dx,dt,v):

y1 = zeros(len(y0))

for i in range(1,len(y0)-1):

y1 = y0 - v*dt*(y0[i+1]-y0[i-1])/(2.0*dx)

return y1
This code works for a single varible, I need it to work for three, I believe the structure is similar.

The equations I want to solve are
du/dt = v
dv/dt = dw/dx
dw/dt = dv/dx
i.e., you have three variables (u, v, w) and the evolution equations for each of them depends on the other variables, or the spatial derivatives of the other variables. The assignment provides the initial conditions for each variable, since v = du/dt, and w = du/dx.

The Attempt at a Solution



I don't really know what to show you for this, I've got as far as I've shown above and really don't know where to go from there. I figure I need to alter the for loop at the bottom of the function?

Really need some help here, this is actually a computational physics assignment but felt it was better suited for the computer science section. Let me know if it should really be in one of the physics forums.
 
Last edited:
Technology news on Phys.org
  • #2
Not having really studied your code, I can give you some advice:

Manually write down the steps of how you would solve it. For each step, think about the code you'd write. When you have a complicated step break it up into smaller steps. When you just don't know what to do invent a function to handle it with the input variables needed and the output to be generated. After a while, you get the hang of it.

Note by creating placeholder functions for steps you can't implement yet, you make a program that can be run and debugged up to a certain point.
 

Related to How Can I Implement a Three-Variable Advection Equation in Python?

1. What is the Advection Equation in Python?

The Advection Equation in Python is a mathematical equation used to describe the transport of a quantity by a fluid flow. It is commonly used in meteorology and oceanography to model the movement of air and water, but can also be applied to other fields such as heat transfer and pollution dispersion.

2. How is the Advection Equation solved in Python?

The Advection Equation can be solved using different numerical methods in Python, such as the finite difference method, finite volume method, or finite element method. These methods discretize the equation and solve it iteratively to approximate the solution at different points in space and time.

3. What are the inputs and outputs of the Advection Equation in Python?

The inputs of the Advection Equation in Python are the initial conditions of the quantity being transported, the velocity field of the fluid flow, and the boundary conditions. The output is the solution of the equation, which represents the quantity at different points in space and time.

4. Are there any limitations to using the Advection Equation in Python?

Yes, there are some limitations to using the Advection Equation in Python. It assumes a steady-state flow and a constant velocity field, which may not always be the case in real-world scenarios. Additionally, the accuracy of the solution depends on the chosen numerical method and the resolution of the discretization grid.

5. What are some practical applications of the Advection Equation in Python?

The Advection Equation in Python has many practical applications, including weather forecasting, ocean current modeling, and air pollution dispersion studies. It can also be used in engineering and industrial processes to optimize fluid transport and heat transfer. Additionally, it is a useful tool in understanding and predicting the spread of diseases and contaminants in natural systems.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
Replies
3
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
971
  • Calculus and Beyond Homework Help
Replies
3
Views
991
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
4K
Back
Top