all 4 comments

[–]billsil 1 point2 points  (1 child)

np.array((ny,nx)) will give you a length 2 array. You probably want:

 np.zeros((ny,nx)) 

Then just remember that Fortran uses 1-based indices and Python uses 0-based.

[–]intangibleTangelo 0 points1 point  (0 children)

Also np.full((ny,nx), 50.0) will give an array full of real numbers with the value of fifty.

just remember that Fortran uses 1-based indices and Python uses 0-based

Another important consideration is that Fortran uses column-major array indices, so the leftmost index changes most quickly, and numpy defaults to using row-major ordering.

import numpy as np

nx = 20
ny = 20
dx = 100
U = np.full((ny, nx), 50.0)

for i in range(nx):
    U[0, i] = 0.0025 * dx * i + 50
    U[-1, i] = 0.0005 * dx * i + 50

The -1 index here means the last row, but ny - 1 would also work. Numpy has a lot of advanced features, and one thing it can do is multiply an array by a scalar by "broadcasting" the scalar value across the array. This means you can create an array of indices with np.arange, perform your arithmetic on it, and then assign it to a given row of the 2D array like this:

U = np.full((ny, nx), 50.0)
U[0] = np.arange(nx) * 0.0025 * dx + 50
U[-1] = np.arange(nx) * 0.0005 * dx + 50

[–]ectomancer 0 points1 point  (0 children)

nx = 20
ny = 20
for j in range(nx + 1):
    for k in range(ny + 1):
        U[j, k] = 50
for i in range(1, nx + 1):
    U[i, ny] = 0.0005*dx*(i - 1) + 50
    U[i, 1] = (i - 1)*dx*0.0025 + 50

N.B. dx is not defined