Created
November 2, 2018 07:35
-
-
Save linnil1/53eea41d538e4dd15756d7152b42ad02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
want_size = [1000, 1000] | |
esp = 1e-1 | |
label_num = 40 | |
max_iter = None | |
it = 0 | |
def setBound(): | |
grid[0, :] = 50 | |
grid[:, -1] = 100 | |
grid[-1, :] = 200 | |
grid[:, 0] = 300 | |
def goIter(): | |
global it, grid | |
oldit = it | |
print(nowsize) | |
while True: | |
it += 1 | |
oldgrid = grid.copy() | |
grid = np.mean([ | |
np.roll(grid, 1, axis=0), | |
np.roll(grid, 1, axis=1), | |
np.roll(grid, -1, axis=0), | |
np.roll(grid, -1, axis=1)], axis=0) | |
setBound() | |
# print(it) | |
# print(grid) | |
if not (np.abs(grid - oldgrid) > esp).any(): | |
break | |
if max_iter and it >= max_iter: | |
break | |
print(it - oldit) | |
def extendMatrix(axis): | |
global grid | |
new_len = min(nowsize[axis] * 2, want_size[axis]) | |
ind = (np.arange(new_len) * nowsize[axis] / new_len).astype(np.int) | |
nowsize[axis] = new_len | |
grid = np.take(grid, ind, axis=axis) | |
nowsize = np.array([3, 3]) | |
grid = np.zeros(nowsize) | |
setBound() | |
goIter() | |
while any(nowsize != want_size): | |
extendMatrix(0) | |
extendMatrix(1) | |
setBound() | |
goIter() | |
X, Y = np.meshgrid(np.arange(want_size[0]), np.arange(want_size[1])) | |
# cont = plt.contourf(X, Y, grid, label_num) | |
plt.figure(figsize=(16, 12), dpi=200) | |
cont = plt.contour(X, Y, grid, label_num) | |
plt.clabel(cont) | |
plt.xticks([]) | |
plt.yticks([]) | |
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, | |
hspace=0, wspace=0) | |
plt.savefig('heat_dynamic_grid.png', bbox_inches='tight') | |
plt.show() |
Author
linnil1
commented
Nov 2, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment