Search code examples
numerical-methodsfinite-element-analysisdiscretizationfluid-dynamics

Riding the wave Numerical schemes for hyperbolic PDEs, lorena barba lessons, assistance needed


I am a beginner python user who is trying to get a feel for computer science, I've been learning how to use it by studying concepts/subjects I'm already familiar with, such as Computation Fluid Mechanics & Finite Element Analysis. I got my degree in mechanical engineering, so not much CS background.

I'm studying a series by Lorena Barba on jupyter notebook viewer, Practical Numerical Methods, and i'm looking for some help, hopefully someone familiar with the subjects of CFD & FEA in general.

if you click on the link below and go to the following output line, you'll find what i have below. Really confused on this block of code operated within the function that is defined.

Anyway. If there is anyone out there, with any suggestions on how to tackle learning python, HELP

In[9]

rho_hist = [rho0.copy()]
rho = rho0.copy()            **# im confused by the role of this variable here**
for n in range(nt):

    # Compute the flux.

    F = flux(rho, *args)

    # Advance in time using Lax-Friedrichs scheme.

    rho[1:-1] = (0.5 * (rho[:-2] + rho[2:]) -
                 dt / (2.0 * dx) * (F[2:] - F[:-2]))

    # Set the value at the first location.

    rho[0] = bc_values[0]

    # Set the value at the last location.

    rho[-1] = bc_values[1]

    # Record the time-step solution.

    rho_hist.append(rho.copy())

return rho_hist

http://nbviewer.jupyter.org/github/numerical-mooc/numerical-mooc/blob/master/lessons/03_wave/03_02_convectionSchemes.ipynb


Solution

  • The intent of the first two lines is to preserve rho0 and provide copies of it for the history (copy so that later changes in rho0 do not reflect back here) and as the initial value for the "working" variable rho that is used and modified during the computation.

    The background is that python list and array variables are always references to the object in question. By assigning the variable you produce a copy of the reference, the address of the object, but not the object itself. Both variables refer to the same memory area. Thus not using .copy() will change rho0.

    a = [1,2,3]
    b = a
    b[2] = 5
    print a
    #>>> [1, 2, 5]
    

    Composite objects that themselves contain structured data objects will need a deepcopy to copy the data on all levels.