def T_1(self, DEBUG = None): # Water entrainment. IN: Q_a,Q_th,Q_cj,Q_v. OUT: Q_vj,Q_th ''' This function calculates the water entrainment.\ Entrainment is the transport of fluid across an interface\ between two bodies of fluid by a shear induced turbulent flux. I.e. the 'mixing' of two fluids across their interface. \ ''' # ipdb.set_trace() g_prime = ma.calc_g_prime(self.Nj, self.Q_cj, self.rho_j, self.rho_a, g=self.g) if DEBUG is True: g_prime[:,:] = 0.5 Ri = T1.calc_RichardsonNo(g_prime, self.Q_th, self.Q_v) # Ri[np.isnan(Ri)] = 0 # Ri[Ri == 0] = np.inf E_wStar = T1.calc_dimlessIncorporationRate(Ri) # Dimensionless incorporation rate E_w = T1.calc_rateOfSeaWaterIncorp(self.Q_v, E_wStar) # Rate of seawater incorporation nQ_th = self.Q_th + T1.calc_changeIn_q_th(E_w, self.dt) # Update cell current thickness # nQ_th[np.isnan(nQ_th)] = 0 tempQ_cj = T1.calc_new_qcj(self.Q_cj, self.Q_th, nQ_th) # tempQ_cj[np.isnan(tempQ_cj)] = 0 # if (tempQ_cj.sum() - self.Q_cj.sum() > 1e+03): # print("break") self.Q_cj[1:-1, 1:-1] = np.round(tempQ_cj[1:-1, 1:-1],15) self.Q_th[1:-1, 1:-1] = np.round(nQ_th[1:-1, 1:-1],15)
def calc_MaxRelaxationTime(self): # out: matrix g_prime = ma.calc_g_prime(self.Nj, self.Q_cj, self.rho_j, self.rho_a, g=self.g) r_j = self.calc_RunUpHeight(g_prime) r_j[r_j == 0] = np.inf g_prime[g_prime == 0] = np.inf return (self.dx / 2) / np.sqrt(2 * r_j * g_prime)
def I_3(self): # Should be done ''' Update of turbidity flow velocity (speed!). IN: Q_a,Q_th,Q_o,Q_cj. OUT: Q_v. ''' # ipdb.set_trace() # g_prime = np.ndarray(Ny,Nx) g_prime = ma.calc_g_prime(self.Nj, self.Q_cj, self.rho_j, self.rho_a) # print("self.Q_cj=\n",self.Q_cj) # print("g_prime.shape=",g_prime.shape) # print("self.Q_cj.shape=",self.Q_cj.shape) # print("g_prime I_3 = ", g_prime) # print("g_prime =\n", g_prime) sum_q_cj = np.sum(self.Q_cj, axis=2) # TCurrent sediment volume concentration # # print("sum_q_cj = ", sum_q_cj) # # q_o = self.Q_o[1:-1,1:-1,:] # # print("q_o = ", q_o) # # self.calc_Hdiff() U_k = np.zeros((self.Ny - 2, self.Nx - 2, 6)) # # diff = self.diff.copy() # TODO! THIS IS WRONG # # print("diff=\n",diff[:,:,0]) # # diff[np.isinf(diff)] = 0 diff = np.zeros((self.Ny - 2, self.Nx - 2, 6)) for i in range(6): sum1 = self.Q_a + self.Q_th diff[:, :, i] = (sum1)[1:-1, 1:-1] - (sum1)[self.NEIGHBOR[i]] diff[np.isinf(diff)] = 0 # For borders. diff = 0 => U_k = 0. ok. # diff[diff<0] = 0 # To avoid negative values in np.sqrt() t1 = np.zeros((6, self.Ny - 2, self.Nx - 2)) for i in range(6): t1[i, :, :] = diff[:, :, i] for i in range(6): comp1 = (8 * g_prime * sum_q_cj)[1:-1, 1:-1] / (self.f * (1 + self.a)) comp2 = (self.Q_o[1:-1, 1:-1, i] * diff[:, :, i]) temp = np.sqrt(comp1 * comp2) U_k[:, :, i] = temp # # print("U_k[:,:,i]=\n",U_k[:,:,i]) self.Q_v[1:-1, 1:-1] = np.nan_to_num(ma.average_speed_hexagon(U_k))
def I_3(self, DEBUG = None): # Should be done ''' Update of turbidity flow velocity (speed!). IN: Q_a,Q_th,Q_o,Q_cj. OUT: Q_v. ''' # ipdb.set_trace() # g_prime = np.ndarray(Ny,Nx) g_prime = ma.calc_g_prime(self.Nj, self.Q_cj, self.rho_j, self.rho_a) if DEBUG is True: g_prime[:,:] = 1 # print("self.Q_cj=\n",self.Q_cj) # print("g_prime.shape=",g_prime.shape) # print("self.Q_cj.shape=",self.Q_cj.shape) # print("g_prime I_3 = ", g_prime) # print("g_prime =\n", g_prime) sum_q_cj = np.sum(self.Q_cj, axis=2) # TCurrent sediment volume concentration # # print("sum_q_cj = ", sum_q_cj) # # q_o = self.Q_o[1:-1,1:-1,:] # # print("q_o = ", q_o) # # self.calc_Hdiff() U_k = np.zeros((self.Ny - 2, self.Nx - 2, 6)) # # print("diff=\n",diff[:,:,0]) # # diff[np.isinf(diff)] = 0 diff = np.zeros((self.Ny - 2, self.Nx - 2, 6)) sum1 = self.Q_a + self.Q_th for i in range(6): diff[:, :, i] = np.abs((sum1)[1:-1, 1:-1] - (sum1)[self.NEIGHBOR[i]]) diff[np.isinf(diff)] = 0 # For borders. diff = 0 => U_k = 0. ok. # diff[diff<0] = 0 # To avoid negative values in np.sqrt() for i in range(6): comp1 = (8 * g_prime * sum_q_cj)[1:-1, 1:-1] / (self.f * (1 + self.a)) comp2 = (self.Q_o[1:-1, 1:-1, i] * diff[:, :, i]) comp2[comp2<0] = 0 # TODO: Test om denne kan fjernes with np.errstate(invalid='raise'): temp = np.sqrt(comp1 * comp2) U_k[:, :, i] = temp # # print("U_k[:,:,i]=\n",U_k[:,:,i]) self.Q_v[1:-1, 1:-1] = np.round(np.nan_to_num(ma.average_speed_hexagon(U_k)),15)
def I_1(self, DEBUG=None): ''' This function calculates the turbidity current outflows.\ IN: Q_a,Q_th,Q_v,Q_cj. OUT: Q_o self.p_f = np.deg2rad(1) # Height threshold friction angle ''' eligableCells = self.Q_th[1:-1, 1:-1] > 0 # Step (i): angles beta_i g_prime = ma.calc_g_prime(self.Nj, self.Q_cj, self.rho_j, self.rho_a) if DEBUG is True: g_prime[:,:] = 1 # h_k = self.calc_BFroudeNo(g_prime) g: np.ndarray = g_prime.copy() g[g == 0] = np.inf h_k = 0.5 * self.Q_v ** 2 / g r = self.Q_th + h_k # print("run up height = \n", r) central_cell_height = (self.Q_a + r)[1:-1, 1:-1] q_i = (self.Q_a + self.Q_th) delta = np.zeros((self.Ny - 2, self.Nx - 2, 6)) delta[:, :, 0] = central_cell_height - q_i[0:self.Ny - 2, 1:self.Nx - 1] delta[:, :, 1] = central_cell_height - q_i[0:self.Ny - 2, 2:self.Nx] delta[:, :, 2] = central_cell_height - q_i[1:self.Ny - 1, 2:self.Nx] delta[:, :, 3] = central_cell_height - q_i[2:self.Ny, 1:self.Nx - 1] delta[:, :, 4] = central_cell_height - q_i[2:self.Ny, 0:self.Nx - 2] delta[:, :, 5] = central_cell_height - q_i[1:self.Ny - 1, 0:self.Nx - 2] delta[np.isinf(delta)] = 0 # q_i is inf at borders. delta = 0 => angle =0 => no transfer # print("my rank = {0}\nmy delta[:,:,0]=\n{1}".format(self.my_rank,delta[:,:,0])) # debug_delta = np.zeros((6, self.Ny - 2, self.Nx - 2)) # for i in range(6): # debug_delta[i, :, :] = delta[:, :, i] dx = self.dx angle = ne.evaluate('arctan2(delta, dx)') # debug_angle = np.zeros((6, self.Ny - 2, self.Nx - 2)) # for i in range(6): # debug_angle[i, :, :] = angle[:, :, i] # print("angle =\n", np.rad2deg(angle)) indices = angle > self.p_f # indices(Ny,Nx,6). Dette er basically set A. indices *= eligableCells[:, :, np.newaxis] # print("indices\n",indices) # debug_indices = np.zeros((6, self.Ny - 2, self.Nx - 2)) # for i in range(6): # debug_indices[i, :, :] = indices[:, :, i] # Average, indices = I_1_doubleforloop(indices, q_i, r, self.Ny, self.Nx, self.p_adh, self.indexMat) # numba function for ii in range(6): # Step (iii) says to go back to step (ii) if a cell is removed. NumberOfCellsInA = np.sum(indices, axis=2) # Cardinality of set A # print("NumberOfCellsInA =\n",NumberOfCellsInA) # Step (ii) calculate average neighborValues = np.zeros((self.Ny - 2, self.Nx - 2)) # print("neighbors=\n", self.NEIGHBOR[0]) for i in range(6): q_i_nb = q_i[self.NEIGHBOR[i]] with np.errstate(invalid='ignore'): indices_for_ne = indices[:, :, i] neighborValues_thread = q_i_nb * indices_for_ne # neighborValues_thread = vectorMultiply(q_i_nb, indices_for_ne) neighborValues_thread[np.isnan(neighborValues_thread)] = 0 neighborValues += neighborValues_thread # neighborValues += np.nan_to_num(q_i_nb * indices[:, :,i]) # Vi vil bare legge til verdier hvor angle>self.p_f # print("neighborValues=\n", neighborValues) p = (r - self.p_adh)[1:-1, 1:-1] # p[p<0]=0 with np.errstate(divide='ignore', invalid='ignore'): Average = ne.evaluate('(p + neighborValues) / NumberOfCellsInA') Average[np.isinf(Average)] = 0 # for når NumberOfCellsInA =0 Average[np.isnan(Average)] = 0 # print("Average=\n", Average) # print("indices=\n", indices) # Step (iii) Eliminate adjacent cells i with q_i >= Average from A. for i in range(6): # Skal sette posisjoner (j) hvor q_i (til nabocelle) > average (i celle j) til 0 nb = q_i[self.NEIGHBOR[i]] itemp = (nb >= Average) indices[itemp, i] = 0 # Step (iv) Average[np.isinf(Average)] = 0 nonNormalizedOutFlow = np.ones((Average.shape + (6,))) * Average[:, :, np.newaxis] for i in range(6): with np.errstate(invalid='ignore'): nonNormalizedOutFlow[:, :, i] -= np.nan_to_num(q_i[self.NEIGHBOR[i]] * indices[:, :, i]) nonNormalizedOutFlow *= indices # Step (v) with np.errstate(divide='ignore', invalid='ignore'): normalization = self.Q_th / r # nu_nf normalization[np.isnan(normalization)] = 0 # print("normalization=\n", normalization) with np.errstate(invalid='ignore'): relaxation = np.sqrt(2 * r * g_prime) * self.dt / (self.dx/2) # dx is intercellular distance. apothem is used in Salles. relaxation[relaxation >0.8] = 0.8 # Demper outflow litt if np.any(relaxation > 1) or np.any(relaxation < 0): # D'Ambrosio demand raise Exception("Warning! I_1 Relaxation > 1!") factor = (normalization * relaxation)[1:-1, 1:-1] self.Q_o[1:-1, 1:-1,:] = np.round(np.nan_to_num(factor[:,:,None] * nonNormalizedOutFlow), 15) if ((np.sum(self.Q_o, axis=2) > self.Q_th)[1:-1,1:-1].sum() > 0): ii, jj = np.where(np.sum(self.Q_o,axis=2)>self.Q_th) s = ''.join("\nsum(Q_o[%i,%i]) = %.10f > Q_th = %.10f\n" % (ii[x],jj[x],sum(self.Q_o[ii[x],jj[x]]),self.Q_th[ii[x],jj[x]]) for x in range(len(ii))) s = s + ''.join("Relaxation = %03f, Normalization = %03f\n" %(relaxation[ii[x],jj[x]], normalization[ii[x],jj[x]]) for x in range(len(ii))) raise Exception("I_1 warning! More outflow Q_o than thickness Q_th!" + s)
def I_1(self): # TODO Tror det er feil her!! ''' This function calculates the turbidity current outflows.\ IN: Q_a,Q_th,Q_v,Q_cj. OUT: Q_o self.p_f = np.deg2rad(1) # Height threshold friction angle ''' eligableCells = self.Q_th[1:-1, 1:-1] > 0 # Step (i): angles beta_i g_prime = ma.calc_g_prime(self.Nj, self.Q_cj, self.rho_j, self.rho_a) h_k = self.calc_BFroudeNo(g_prime) r = self.Q_th + h_k # print("run up height = \n", r) central_cell_height = (self.Q_a + r)[1:-1, 1:-1] q_i = (self.Q_a + self.Q_th) delta = np.zeros((self.Ny - 2, self.Nx - 2, 6)) for i in range(6): delta[:, :, i] = central_cell_height - q_i[self.NEIGHBOR[i]] delta[np.isinf( delta )] = 0 # q_i is inf at borders. delta = 0 => angle =0 => no transfer debug_delta = np.zeros((6, self.Ny - 2, self.Nx - 2)) for i in range(6): debug_delta[i, :, :] = delta[:, :, i] angle = np.arctan2(delta, self.dx) debug_angle = np.zeros((6, self.Ny - 2, self.Nx - 2)) for i in range(6): debug_angle[i, :, :] = angle[:, :, i] # print("angle =\n", np.rad2deg(angle)) indices = angle > self.p_f # indices(Ny,Nx,6). Dette er basically set A. indices *= eligableCells[:, :, np.newaxis] # print("indices\n",indices) debug_indices = np.zeros((6, self.Ny - 2, self.Nx - 2)) for i in range(6): debug_indices[i, :, :] = indices[:, :, i] for ii in range( 6 ): # Step (iii) says to go back to step (ii) if a cell is removed. NumberOfCellsInA = np.sum(indices, axis=2) # Cardinality of set A # print("NumberOfCellsInA =\n",NumberOfCellsInA) # Step (ii) calculate average neighborValues = np.zeros((self.Ny - 2, self.Nx - 2)) # print("neighbors=\n", self.NEIGHBOR[0]) for i in range(6): q_i_nb = q_i[self.NEIGHBOR[i]] neighborValues += np.nan_to_num( q_i_nb * indices[:, :, i] ) # Vi vil bare legge til verdier hvor angle>self.p_f # print("neighborValues=\n", neighborValues) p = (r - self.p_adh)[1:-1, 1:-1] # p[p<0]=0 with np.errstate(divide='ignore', invalid='ignore'): Average = (p + neighborValues) / NumberOfCellsInA Average[np.isinf(Average)] = 0 # for når NumberOfCellsInA =0 Average[np.isnan(Average)] = 0 # print("Average=\n", Average) # print("indices=\n", indices) # Step (iii) Eliminate adjacent cells i with q_i >= Average from A. for i in range( 6 ): # Skal sette posisjoner (j) hvor q_i (til nabocelle) > average (i celle j) til 0 nb = q_i[self.NEIGHBOR[i]] itemp = (nb >= Average) indices[itemp, i] = 0 for i in range(6): debug_indices[i, :, :] = indices[:, :, i] # Step (iv) Average[np.isinf(Average)] = 0 # TODO Testing! nonNormalizedOutFlow = np.ones( (Average.shape + (6, ))) * Average[:, :, np.newaxis] for i in range(6): nonNormalizedOutFlow[:, :, i] -= np.nan_to_num( q_i[self.NEIGHBOR[i]] * indices[:, :, i]) nonNormalizedOutFlow *= indices # Step (v) with np.errstate(divide='ignore', invalid='ignore'): normalization = self.Q_th / r # nu_nf # print("normalization=\n", normalization) with np.errstate(invalid='ignore'): relaxation = np.sqrt(2 * r * g_prime) * self.dt / (0.5 * self.dx) if (relaxation.any() > 1): print("Warning! Relaxation > 1!") for i in range(6): self.Q_o[1:-1, 1:-1, i] = np.nan_to_num( (normalization * relaxation)[1:-1, 1:-1] * nonNormalizedOutFlow[:, :, i]) if ((np.sum(self.Q_o, axis=2) > self.Q_th).sum() > 0): print("more outflow than thickness!")