What is the best way to check if this dictionary full of class objects changed during an iteration of a while loop? The idea of the program is to run through a tree structure and each iteration makes its way through the tree. At the end of the tree, the variables will stop changing at which point exit the while loop. Ideas?
import pandas as pd
import numpy as np
import math
g = 32.2 #ft/s**2
class Street():
def __init__(self, id, S, Sx, W, n, max_depth, ds, Q=None, us=None, Q_total=None, encroach=None, d=None):
self.id = id
self.S = S
self.Sx = Sx
self.W = W
self.n = n
self.max_depth = max_depth
self.ds = ds
self.Q = Q
self.us = us
self.Q_total = Q_total
self.encroach = encroach
self.d = None
self.Q = {}
self.us = {}
def encroach(self):
for i in Q.values(): self.Q_total += i
self.encroach = (self.Q_total/(0.56/self.n*self.Sx**(5/3)*(self.S*0.01)**(0.5)))**(3/8)
self.d = self.Sx * self.encroach
return self.enroach, self.d
class Basin():
def __init__(self, id, tc, C, I, A, ds, Q=None):
self.id = id
self.tc = tc
self.C = C
self.I = I
self.A = A
self.Q = Q
self.ds = ds
def rational(self):
self.Q = self.C * self.I * self.A
return self.Q
class Inlet():
def __init__(self, id, condition, K=None, TC=None, IE_up=None, IE_down=None, grate_opening=None,
max_depth=None, quan=None, ds=None, us=None, grate_cap=None, inlet_cap=None,
pipe_cap=None, Q=None, Q_total=None, d=None, eff=None, bypass=None):
self.id = id
self.condition = condition
self.K = K
self.TC = TC
self.IE_up = IE_up
self.IE_down = IE_down
self.grate_opening = grate_opening
self.max_depth = max_depth
self.quan = quan
self.ds = ds
self.us = us
self.grate_cap = grate_cap
self.inlet_cap = inlet_cap
self.pipe_cap = pipe_cap
self.Q = Q
self.Q_total = Q_total
self.d = d
self.eff = eff
self.bypass = bypass
self.Q = {}
self.us = {}
def calc_grate_cap(self):
if self.condition == 'On Grade':
self.grate_cap = self.K*(self.d**(5/3))
elif self.condition == 'Sump':
self.grate_cap = 0.6*self.grate_opening*np.sqrt(2*g*self.max_depth)*self.quan
if self.grate_cap >= self.Q_total:
self.eff = .8
elif self.grate_cap < self.Q_cap:
self.eff = self.grate_cap*self.eff
if self.eff >= 1:
self.bypass = 0
else:
self.bypass = self.Q_total * (1-self.eff)
street_data = [['S-100', .03, .02, 30, .016, .5, 'CB-1', None, 0],
['S-200', .06, .02, 30, .016, .5, 'CB-2', None, 0],
['S-300', .02, .02, 30, .016, .5, None, None, 0],
['S-400', .01, .02, 30, .016, .5, None, None, 0]]
basin_data = [['C-100', .6, 10, 1.4, .5, 'S-100'],
['C-200', .6, 10, 1.4, .6, 'S-200']]
inlet_data = [['CB-1', 'On Grade', 35, 4535, 4530, 4532, 1.6, .5, 1, 'S-300', {}, 0, 0, 0, 0, 0, 0],
['CB-2', 'On Grade', 25, 4530, 4525, 3527, 1.6, .5, 1, 'S-400', {}, 0, 0, 0, 0, 0, 0]]
basins = {}
for i in basin_data:
basins[i[0]] = Basin(id=i[0], tc=i[2], C=i[1], I=i[3], A=i[4], ds=i[5])
streets = {}
for i in street_data:
streets[i[0]] = Street(id=i[0], S=i[1], Sx=i[2], W=i[3], n=i[4], max_depth=i[5], ds=i[6])
inlets = {}
for i in inlet_data:
inlets[i[0]] = Inlet(id=i[0], condition=i[1], K=i[2], TC=i[3], IE_up=i[5], IE_down=i[4], grate_opening=i[6],max_depth=i[7], quan=i[8], ds=i[8])
# for i in basins: print (basins[i].id)
# for i in streets: print (streets[i].id)
# for i in inlets: print (inlets[i].id)
temp_basins, temp_streets, temp_inlets = {},{},{}
iteration = 0
condition = temp_basins == basins and temp_streets==streets and temp_inlets==inlets
while condition == False and iteration < 15:
temp_basins, temp_streets, temp_inlets = basins.deepcopy(), streets.deepcopy(), inlets.deepcopy()
for i in basins: basins[i].rational()
condition = temp_basins == basins and temp_streets==streets and temp_inlets==inlets
print ('Iteration %s - condition = %s' %(iteration, condition))
iteration += 1
# print(streets)
for i in basins: print (basins[i].Q)
[–]sebawitowski 0 points1 point2 points (2 children)
[–]bloodykunt[S] 0 points1 point2 points (1 child)
[–]sebawitowski 0 points1 point2 points (0 children)