-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·300 lines (281 loc) · 9.01 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
import math
import tkinter as tk
import random
import os
import time
import webcolors as wc
#importlib
#import print_function
class Node:
m_id=""
m_percent=0
m_through=0
m_orig=0
m_checked=0
def __init__(self, idValue):
self.m_id=idValue
def check(self):
self.m_checked=1
nodes=[]
q = []
rows = 3
cols = 3
modelnum = 1
for i in range(1,10):
nodes.append(Node(str(i)))
g = {'1':{'2':-5, '4':-5}, '2':{'1':5, '3':-5, '5':-5}, '3':{'2':5, '6':5}, '4':{'1':5, '5':-5}, '5':{'2':5, '4':5, '6':-8, '8':-2}, '6':{'3':-5, '5':8, '9':-1}, '8':{'5':2}, '9':{'6':1}}
def gen_queue(index):
l = []
for key,value in g[index].items():
if int(key) < 1 or key in q:
continue
elif int(value) < 0:
q.append(key)
l.append(key)
for i in l:
gen_queue(i)
def pick_model():
modelNum = 0
while modelNum < 1 or modelNum > 3:
print("\t1) Percent of water that effects endpoint")
print("\t2) Throughput of water that effects endpoint")
print("\t3) Water throughput originating in a square that effects the endpoint")
num = input("Select a model number to generate (1-3): ")
if num.isdigit():
modelNum = int(num)
if modelNum < 1 or modelNum > 3:
print("Invalid model")
else:
print("Invalid model")
return modelNum
def trace(start,end):
if int(start) < 1:
return 0
for key,value in g[start].items():
if value <= 0:
continue
if key == start and value > 0:
return 1
else:
val = trace(key, end)
if val == 1:
return 1
def sumTo(start, end):
sum=0
if trace(start, end) == 0:
return 0
for key,value in g[start].items():
if int(key) <= 0:
return 0
if value > 0:
if key == end:
return value
else:
if sum_out(key):
sum+=((value*(value/sum_out(key)))+sumTo(key,end))
return sum
def sum_in(id):
sum=0
for key,value in g[id].items():
if value < 0:
sum+=abs(value)
return sum
def sum_out(id):
sum=0
for key,value in g[id].items():
if value > 0:
sum+=abs(value)
return sum
def model_data():
modelnum = pick_model()
endpoint = input("What block do you want to be the end point?\n")
if endpoint in g.keys() and int(endpoint) > 0:
q.append(endpoint)
gen_queue(endpoint)
else:
print("Invalid Block")
exit()
cur=nodes[(int(q.pop(0))-1)]
while 1:
sIn=sum_in(str(cur.m_id))
sOut=sum_out(str(cur.m_id))
sTo=sumTo(str(cur.m_id), endpoint)
if str(cur.m_id) == str(endpoint):
cur.m_percent = 1
cur.m_through=sOut
if sOut>sIn:
cur.m_orig=sOut-sIn
else:
cur.m_orig=0
else:
if sOut:
cur.m_percent=sTo/sOut
if cur.m_percent > 1:
cur.m_percent = 1
else:
cur.m_percent=0
cur.m_through=sOut*cur.m_percent
if sOut>sIn:
cur.m_orig=(sOut-sIn)*cur.m_percent
else:
cur.m_orig=0
cur.check()
if len(q):
cur = nodes[(int(q.pop(0))-1)]
else:
return
def print_models():
print("Model 1:")
for i in nodes:
percent=str(float(i.m_percent)*100)+"% "
if(int(i.m_id)%cols):
print(percent, end='')
else:
print(percent)
print("Model 2:")
for i in nodes:
through=str(float(i.m_through))+" "
if(int(i.m_id)%cols):
print(through, end='')
else:
print(through)
print("Model 3:")
for i in nodes:
origin=str(float(i.m_orig))+" "
if(int(i.m_id)%cols):
print(origin, end='')
else:
print(origin)
def closest_colour(requested_colour):
min_colours = {}
for key, name in wc.css3_hex_to_names.items():
r_c, g_c, b_c = wc.hex_to_rgb(key)
rd = (r_c - requested_colour[0]) ** 2
gd = (g_c - requested_colour[1]) ** 2
bd = (b_c - requested_colour[2]) ** 2
min_colours[(rd + gd + bd)] = name
return min_colours[min(min_colours.keys())]
def get_colour_name(requested_colour):
try:
closest_name = actual_name = wc.rgb_to_name(requested_colour)
except ValueError:
closest_name = closest_colour(requested_colour)
actual_name = None
return closest_name
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.canvas = tk.Canvas(self, width=500, height=500, borderwidth=0, highlightthickness=0)
self.canvas.pack(side="top", fill="both", expand="true")
self.rows = rows
self.columns = cols
self.cellwidth = 500/cols
self.cellheight = 500/rows
self.rect = {}
#modelNum = 1
i=0
for column in range(cols):
for row in range(rows):
x1 = row*self.cellwidth
y1 = column*self.cellheight
x2 = x1+self.cellwidth
y2 = y1+self.cellheight
c = 0
color = "black"
if modelnum == 1:
c = int(nodes[i].m_percent*100)
elif modelnum == 2:
c = int(nodes[i].m_through)
elif modelnum == 3:
c = int(nodes[i].m_orig)
i+=1
color = get_colour_name((0, 0, c*2))
#if c >= 75:
# color = "aqua"
#elif c >= 50:
# color = "blue"
#elif c >= 25:
# color = "navy"
self.rect[row,column] = self.canvas.create_rectangle(x1,y1,x2,y2, fill=color, tags="rect")
userFile = ""
while userFile != "Y" and userFile != "N":
userFile = input("Do you want to use your own Modflow model? (Y/N)")
userFile=userFile.upper()
if userFile != "Y" and userFile != "N":
print("Invalid Input\n")
modFile=""
if userFile == "Y":
modFile=input("Enter the file name of of the model you plan to use:\n")
f=open(modFile, 'r')
exe=open("mod.py", 'w')
exe.write("#!/usr/bin/env python3\n")
exe.write("\n")
for line in f:
if line[:1] == "#" or line[:3] == "fig" or line[:2] == "ax":
continue
if line == "mf = flopy.modflow.Modflow(modelname, exe_name=\'../mf2005\')":
exe.write("mf = flopy.modflow.Modflow(modelname, exe_name='./mf2005')")
continue
exe.write(line)
#if line[:9] == "modelname":
# exe.write("f.open(modelname+\".cbc\", \'w\')")
# exe.write("f.write(\"\n\")")
# exe.write("f.close()")
if line[:3] == "frf":
exe.write("right=open(\"frf.txt\", \'w\')\n")
exe.write("right.write(\', \'.join([str(x) for x in frf]))\n")
if line[:3] == "fff":
exe.write("front=open(\"fff.txt\", \'w\')\n")
exe.write("front.write(\', \'.join([str(x) for x in fff]))\n")
exe.close()
break
if line[:4] == "nrow":
rows = int(line[7:])
if line[:4] == "ncol":
cols = int(line[7:])
os.system('./bash.sh')
if userFile == "Y":
fff = open("fff.txt", 'r')
frf = open("frf.txt", 'r')
nodes = []
g = {}
g.update({"0":{}})
g.update({"-1":{}})
for fff_data in fff:
fff_list = fff_data.split(", ")
for i in range((cols*rows)):
g.update({str(i+1):{}})
nodes.append(Node(str(i+1)))
frf_data = frf.readline()
frf_list = frf_data.split(", ")
for i in range(len(fff_list)):
if i+1 == rows:
g[str(i+1)].update({"0":float(fff_list[i])})
g["0"].update({str(i+1):-1*float(fff_list[i])})
g[str(i+1)].update({"-1":float(frf_list[i])})
g["-1"].update({str(i+1):-1*float(frf_list[i])})
continue
if i+1 <= rows:
g[str(i+1)].update({"0":float(fff_list[i])})
g["0"].update({str(i+1):-1*float(fff_list[i])})
else:
g[str(i+1)].update({str((i+1)-rows):float(fff_list[i])})
g[str((i+1)-rows)].update({str(i+1):-1*float(fff_list[i])})
if i+1 % cols and i+2 <= (rows*cols):
g[str(i+1)].update({str((i+2)):float(frf_list[i])})
g[str((i+2))].update({str(i+1):-1*float(frf_list[i])})
else:
g[str(i+1)].update({"0":float(frf_list[i])})
g["0"].update({str(i+1):-1*float(frf_list[i])})
model_data()
print_models()
myapp = App()
#time.sleep(5)
myapp.mainloop()
else:
model_data()
print_models()
myapp = App()
#time.sleep(5)
myapp.mainloop()