-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherosion_gif.py
175 lines (162 loc) · 7.61 KB
/
erosion_gif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import sys
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import erf
from matplotlib.colors import ListedColormap, to_rgba
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from numba import njit
import imageio
import glob
plt.rcParams.update({'font.size': 12})
def readfile(fname):
with open(fname, 'r') as f:
print('Working on '+fname+'\nAnalyzing mesh...')
Nnode = np.nan
Nelem = np.nan
Ncomm = 0
for line in f:
if line.startswith('#') or line.startswith(':'):
Ncomm += 1
if ':NodeCount' in line:
Nnode = np.int64(line.split()[-1])
if ':ElementCount' in line:
Nelem = np.int64(line.split()[-1])
if np.isnan(Nnode) is False and np.isnan(Nelem) is False:
break
node = np.loadtxt(fname,comments=[':','#'],max_rows=Nnode)
elem = np.loadtxt(fname,comments=[':','#'],skiprows=Nnode+Ncomm,dtype=np.int64)
print('NodeCount %d' % len(node))
print('ElementCount %d' % len(elem))
return node, elem
@njit
def erosion(node, elem, rdm, risk, current_iteration_num, coupling_period):
if np.mod(current_iteration_num, coupling_period) == 0:
num_of_element_of_interest = 0
element_of_interest = np.zeros(len(elem),dtype=np.int64)
for i_element in np.random.permutation(np.arange(len(elem))):
i1 = elem[i_element,0] - 1
i2 = elem[i_element,1] - 1
i3 = elem[i_element,2] - 1
if (node[i1,2] == 0 or node[i2,2] == 0 or node[i3,2] == 0) and (node[i1,2] == 1 or node[i2,2] == 1 or node[i3,2] == 1):
element_of_interest[num_of_element_of_interest] = i_element
num_of_element_of_interest += 1
if num_of_element_of_interest != 0:
for i_element in element_of_interest[:num_of_element_of_interest]:
i1 = elem[i_element,0] - 1
i2 = elem[i_element,1] - 1
i3 = elem[i_element,2] - 1
bernoulli = 0
if rdm[i_element] < risk[i_element]:
bernoulli = 1
for i in [i1, i2, i3]:
if node[i,2] == 1 and bernoulli == 1:
node[i,2] = 0
return node[:,2]
def evolution(fname, Niterations, Nrealizations, coupling_period):
if Niterations < 1:
Niterations = 1
if Nrealizations < 1:
Nrealizations = 1
node, elem = readfile(fname)
z = np.copy(node[:, 2])
ave = np.zeros(len(node))
for i, realization in enumerate(range(Nrealizations)):
print('Realization '+str(realization+1))
node[:, 2] = np.copy(z)
np.random.seed(i)
for current_iteration_num in range(Niterations):
rdm = np.random.random(len(elem))
r = np.ones(len(elem))/2
risk = 0.5*(1 + erf(4*(r - 1)/np.sqrt(r**2 + 1)/np.sqrt(2)))
node[:,2] = erosion(node, elem, rdm, risk, current_iteration_num, coupling_period)
sys.stdout.write('\rIteration '+str(current_iteration_num))
sys.stdout.flush()
print()
ave += node[:,2]
ave /= Nrealizations
print()
return node, ave
def main(Niterations, Nrealizations, onlyIC=False, rivercolor='navy', plaincolor=(244/256, 242/256, 205/256)):
mycmap = np.ones((21,4))
mycmap[:,0] = np.linspace(to_rgba(rivercolor)[0], to_rgba(plaincolor)[0], 21)
mycmap[:,1] = np.linspace(to_rgba(rivercolor)[1], to_rgba(plaincolor)[1], 21)
mycmap[:,2] = np.linspace(to_rgba(rivercolor)[2], to_rgba(plaincolor)[2], 21)
mycmap = ListedColormap(mycmap)
fList = ['BOTTOM25.t3s', 'BOTTOM50.t3s', 'BOTTOM100.t3s']
if onlyIC is False:
for couplingList in [[1, 1, 1], [1, 2, 4]]:
prefix = ''.join(str(couplingList)[1:-1].split(', '))
plt.figure(figsize=(8,12))
for i, fname in enumerate(fList):
node, ave = evolution(fname, Niterations, Nrealizations, couplingList[i])
x, y, z = node[:,0], node[:,1], node[:,2]
for j, zplot, ttl in zip([1, 2], [z, ave], ['One Realization', 'Ensemble of '+str(Nrealizations)+' Realizations']):
plt.subplot(int(str(len(fList))+'2'+str(j+i*2)))
plt.text(23.5,19,'River',color='white',rotation=90,fontsize=14)
plt.text(0.5,15,'Floodplain',color='black',rotation=90,fontsize=14)
plt.text(46.5,15,'Floodplain',color='black',rotation=90,fontsize=14)
plt.tripcolor(x,y,zplot,cmap=mycmap)
plt.xlim(np.min(x),np.max(x))
plt.ylim(np.min(y),np.max(y))
plt.xticks(np.arange(np.min(x),np.max(x)+1,10))
plt.yticks(np.arange(np.min(y),np.max(y)+1,10))
plt.title(ttl+'\nMesh Size = '+str(float(fname.split('BOTTOM')[1].split('.t3s')[0])/100)+', C$_P$ = '+str(couplingList[i]))
plt.tight_layout()
plt.savefig(prefix+'_'+str(Niterations)+'.png',dpi=150)
plt.close()
else:
plt.figure(figsize=(9,12))
for i, fname in enumerate(fList):
node, elem = readfile(fname)
print()
x, y, z = node[:,0], node[:,1], node[:,2]
plt.subplot(int(str(len(fList))+'2'+str(2+i*2)))
plt.tripcolor(x,y,z,cmap=mycmap)
plt.colorbar(fraction=0.03,ticks=[0,1])
plt.xlim(np.min(x),np.max(x))
plt.ylim(np.min(y),np.max(y))
plt.xticks(np.arange(np.min(x),np.max(x)+1,10))
plt.yticks(np.arange(np.min(y),np.max(y)+1,10))
plt.title('Initial Condition')
plt.tight_layout()
plt.subplot(int(str(len(fList))+'2'+str(1+i*2)))
patches = []
for i_element in range(len(elem)):
tri = np.zeros((3,2))
for j in range(3):
tri[j] = node[elem[i_element,j]-1][:2]
patches.append(Polygon(tri, closed=True))
p = PatchCollection(patches, edgecolors='k', linewidths=.1, facecolors='none')
plt.gca().add_collection(p)
plt.xlim(np.min(x),np.max(x))
plt.ylim(np.min(y),np.max(y))
plt.xticks(np.arange(np.min(x),np.max(x)+1,10))
plt.yticks(np.arange(np.min(y),np.max(y)+1,10))
plt.title('Mesh size: '+str(float(fname.split('BOTTOM')[1].split('.t3s')[0])/100))
plt.tight_layout()
## inset_axes(plt.gca(), height='35%', width='35%', loc=3)
## patches = []
## for i_element in range(len(elem)):
## tri = np.zeros((3,2))
## for j in range(3):
## tri[j] = node[elem[i_element,j]-1][:2]
## patches.append(Polygon(tri, closed=True))
## p = PatchCollection(patches, edgecolors='k', linewidths=.1, facecolors='none')
## plt.gca().add_collection(p)
## plt.xlim(22,28)
## plt.ylim(22,28)
## plt.xticks([])
## plt.yticks([])
plt.savefig('ic.png',dpi=300)
plt.show()
if __name__ == '__main__':
for i in range(181):
main(Niterations=i, Nrealizations=10, onlyIC=False)
for prefix in ['111', '124']:
image_file_names = sorted(glob.glob(prefix+'_*.png'), key=lambda y: int(y.split('_')[1].split('.')[0]))
images = []
for i in range(len(image_file_names)):
images.append(imageio.imread(image_file_names[i]))
imageio.mimsave(prefix+'.gif', images, 'GIF', loop=1, fps=20)