[codesyntax lang=”python”]

from sys import argv

dd = 0.01
#dd = 0.1
dt = 0.001

print(‘# dd = %9.4f’ % dd)
print(‘# dt = %9.4f’ % dt)

nn = int(2.0/dd) # numero de pontos em x e em y
nt = int(1.0/dt) # numero de pontos em t

print(‘# nn = %9d’ % nn)
print(‘# nt = %9d’ % nt)

from numpy import zeros
u0 = zeros((nn+1,nn+1),float) # um array para conter a solucao

u1 = zeros((nn+1,nn+1),float) # um array para conter a solucao
u2 = zeros((nn+1,nn+1),float) # um array para conter a solucao
u3 = zeros((nn+1,nn+1),float) # um array para conter a solucao

fin = open(‘difusao2d-ana.dat’, ‘rb’) # abre o arquivo com os dados

from numpy import fromfile

aux = fromfile(fin,float,(nn+1)*(nn+1)) # le a condicao inicial
for i in range(nn+1):
u0[i][:] = aux[i*(nn+1):(i+1)*(nn+1)]

m = 1
n = 100

for it in range(m): # para instantes:
for ir in range(n): # le vezes, so guarda a ultima
aux = fromfile(fin,float,(nn+1)*(nn+1))
for i in range(nn+1):
u1[i][:] = aux[i*(nn+1):(i+1)*(nn+1)]

import numpy as np
import matplotlib.pyplot as plt

# Generate some data
#nrows, ncols = nn, nn
nrows, ncols = nn+1, nn+1
xmin, xmax = -nn*dd, nn*dd
ymin, ymax = -nn*dd, nn*dd

dx = (xmax – xmin) / (ncols – 1)
dy = (ymax – ymin) / (ncols – 1)

x = np.linspace(xmin, xmax, ncols)
y = np.linspace(ymin, ymax, nrows)
x, y = np.meshgrid(x, y)

z = u1
x, y, z = [item.flatten() for item in (x,y,z)]

# Scramble the order of the points so that we can’t just simply reshape z
indicies = np.arange(x.size)
x, y, z = [item[indicies] for item in (x, y, z)]

# Up until now we’ve just been generating data…
# Now, x, y, and z probably represent something like you have.

# We need to make a regular grid out of our shuffled x, y, z indicies.
# To do this, we have to know the cellsize (dx & dy) that the grid is on and
# the number of rows and columns in the grid.

# First we convert our x and y positions to indicies…
idx = np.round((x – x.min()) / dx).astype(np.int)
idy = np.round((y – y.min()) / dy).astype(np.int)

# Then we make an empty 2D grid…
grid = np.zeros((nrows, ncols), dtype=np.float)

# Then we fill the grid with our values:
grid[idy, idx] = z

# And now we plot it:
plt.imshow(grid, interpolation=’nearest’, vmin=0, vmax=1,
extent=(x.min(), x.max(), y.max(), y.min()))

Back to Top