def plot_SQ(self): ks = self.ks param = self.param length = self.length alpha = self.alpha m = 5.0 / 3 ie0 = 5.0 dot_s = np.arange(0, 1.0, 0.01) for k in range(0, len(ks)): ie1 = ie0 / ks[k] min_q = ie1 * length max_q = ie0 * length min_s = m / (m + 1) * (ie1 * length**(m + 1) / alpha) * (1 / m) max_s = m / (m + 1) * (ie0 * length**(m + 1) / alpha) * (1 / m) _str = 'k=' + str(ks[k]) p = param[_str] sq = bf.gen(dot_s, p) sq[:, 1] = sq[:, 1] * (max_q - min_q) + min_q sq[:, 0] = sq[:, 0] * (max_s - min_s) + min_s plt.plot(sq[:, 1], sq[:, 0]) plt.plot(self.curve[k][:, 0], self.curve[k][:, 1], '--b') plt.savefig('Falling_Fitting.png') plt.close()
def SQ_curve(self, k): P = self.get_P(k) t = np.arange(0., 1.00001, 1. / 100) curve = bf.gen(t, len(P) - 1, P) return curve
def plot_SQ(self): # max_k = self.max_k param = self.param length = self.length alpha = self.alpha ks = self.ks m = 5.0 / 3 ie0 = 5.0 dot_s = np.arange(0, 1.0, 0.01) for k in range(0, len(ks)): ie1 = ie0 * ks[k] max_q = ie1 * length max_s = m / (m + 1) * (ie1 * length**(m + 1) / alpha) * (1 / m) min_s = m / (m + 1) * (ie0 * length**(m + 1) / alpha) * (1 / m) p = param[k] dot_s = np.arange(0, 1.001, 0.01) dot_q = bf.gen(dot_s, 6, p) dot_s = (dot_s * (max_s - min_s) / max_s + min_s / max_s) * max_s q = (dot_q * (1.0 - 1.0 / k) + 1.0 / k) * max_q plt.plot(q, dot_s) plt.savefig('Rising_Fitting.png') plt.close()
def interpCurve(self, p): Pts, midPoint = self.nearestNeighbor(p) Pts = np.array(Pts) midPoint = np.array(midPoint) """ xrbf = Rbf(Pts[:, 0], Pts[:, 1], Pts[:, 2], Pts[:, 3], midPoint[:, 0]) yrbf = Rbf(Pts[:, 0], Pts[:, 1], Pts[:, 2], Pts[:, 3], midPoint[:, 1]) P1X = xrbf(p[0], p[1], p[2], p[3])*p[0] P1Y = yrbf(p[0], p[1], p[2], p[3])*p[1]""" P1X, P1Y = self.inverseDist(p, Pts, midPoint) P1X = P1X*p[0] P1Y = P1Y*p[1] print(midPoint) print(P1X, P1Y) curvePoints = [[p[0], p[1]], [P1X, P1Y], [p[2], p[3]]] T = np.arange(0., 1.0001, 1./100) curve = bezeir_fit.gen(T, self.degC, np.array(curvePoints)) return curve, T
def plot_norm_SQ(self): param = self.param for j in range(0, len(param)): t = np.arange(0, 1.00001, 1. / 100) p = param[j] curve = bf.gen(t, len(p) - 1, p) _curve = np.array(self.curve[j]) plt.plot(curve[:, 0], curve[:, 1], '-r') plt.plot(_curve[:, 0], _curve[:, 1], '--b') plt.savefig('Falling_Fitting.png', dpi=150) plt.close()
def interpCurve(self, qBD, sBD): PPlist = self.PPlist curvePoints = [[1, 1]] for i in range(0, len(PPlist)): fX = PPlist[i][0] fY = PPlist[i][1] p = [fX(qBD, sBD), fY(qBD, sBD)] curvePoints.append(p) # p[-1] = [qBD, sBD] T = np.arange(0., 1.0001, 1./100) curve = bezeir_fit.gen(T, self.degC, np.array(curvePoints)) return curve, T
def run(): REC = pickle.load(open('Steady_fit/ks1_Rise_CurveRec.flow')) param = pickle.load(open('Steady_fit/fit_1_RiseLimb.pick')) key_str = 'k=10.0' sto = REC['S'][key_str] out = REC['Q'][key_str] sto = sto[0:len(out), :] out[:, 1] = (out[:, 1] - min(out[:, 1])) / (1.0 - min(out[:, 1])) sto[:, 1] = (sto[:, 1] - min(sto[:, 1])) / (1.0 - min(sto[:, 1])) lst = np.arange(1, len(out), len(out) / 10) lst = lst.tolist() lst.pop(-1) plt.figure plt.plot(out[:, 1], sto[0:len(out), 1], color='dodgerblue', lw=3.0, label='Analytic') p = param['param'] p = p['k=10.0'] new_t = np.arange(0, 1.001, 0.01) new_t = new_t.tolist() new_t.append(1.0) new_t = np.array(new_t) y2 = bf.gen(new_t, 6, p) plt.plot(y2[:, 1], y2[:, 0], '--r', label='Bezeir-fit') counter = 0 for point in p: plt.plot(point[0], point[1], marker='s', color='k') plt.text(point[0] + 0.02, point[1] + 0.01, 'P' + str(counter)) counter += 1 plt.legend(loc=4) plt.xlabel('$Q_{{}*{}}$') plt.ylabel('$S_{{}*{}}$') plt.title('k=10 Analytical S-Q fit (n=6)') plt.savefig('s-q fit test.png') plt.close()
def bzCurve(self): # Start from zero self.steady_q.tolist().insert(0, 0.) self.steady_s.tolist().insert(0, 0.) t = np.arange(0., 1.0001, 1. / 10) Q = np.interp(t, self.ie / max(self.ie), self.steady_q) S = np.interp(t, self.ie / max(self.ie), self.steady_s) std_SQ = np.array([zip(Q, S)])[0] p, t = bf.fit(std_SQ, 2, 1.0E-8) print(p) p[0] = [0, 0] p[-1] = std_SQ[-1] t = np.arange(0., 1.0001, 1. / 100) f_std_SQ = bf.gen(t, 2, p) return f_std_SQ, t
def eff_coeifficent(self): self.eff = list() param = self.param for j in range(0, len(param)): t = np.arange(0, 1.00001, 1. / 100) p = param[j] curve = bf.gen(t, len(p) - 1, p) _curve = np.array(self.curve[j]) for j in range(0, len(_curve[:, 1])): if _curve[j, 1] < 0: _curve[j, 1] = 0. fQ = interp1d(curve[:, 1], curve[:, 0]) Q = fQ(_curve[:, 1]) eff = 1 - np.sum((Q - _curve[:, 0])**2) / np.sum( (Q - np.mean(_curve[:, 0]))**2) self.eff.append(eff) plt.plot(1. / np.array(self.ks), self.eff, 'b+')
def plot_norm_SQ(self): # max_k = self.max_k param = self.param ks = self.header curve = self.curve dot_s = np.arange(0, 0.981, 0.01) dot_s = dot_s.tolist() dot_s.append(1.0) dot_s - np.array(dot_s) for k in range(0, len(ks)): p = param[k] out_s = curve[k][:, 1] out_q = curve[k][:, 0] dot_q = bf.gen(dot_s, self.bez_deg, p) plt.plot(dot_q[:, 1], dot_q[:, 0], '-b') plt.plot(out_q[:, 1], out_s[0:len(out_q[:, 1]), 1], '--r') plt.savefig('Falling_Fitting.png', dpi=150) plt.close()
def testFitSDR(): a = pickle.load(open('SDRrec.pick', 'r')) header = a['header'] PT = a['PT'] Plist = list() p2List = list() p3List = list() for j in range(0, len(PT)): Plist.append(PT[j][0]) for k in range(0, len(Plist)): p2List.append(Plist[k][1]) p3List.append(Plist[k][2]) p2List = np.array(p2List) p3List = np.array(p3List) P2, T2 = bezeir_fit.fit(p2List, 4, 1.0E-8) P3, T3 = bezeir_fit.fit(p3List, 4, 1.0E-8) T = np.arange(0., 1.001, 1./50) P2_fit = bezeir_fit.gen(T, 4, P2) P3_fit = bezeir_fit.gen(T, 4, P3) plt.figure plt.plot(P2_fit[:, 0], P2_fit[:, 1], label='P2', lw=3.0) plt.plot(p2List[:, 0], p2List[:, 1], ls='--', lw=3.0) plt.plot(P3_fit[:, 0], P3_fit[:, 1], label='P3') plt.plot(p3List[:, 0], p3List[:, 1], ls='--') plt.legend() plt.savefig('p2p3fit.png') plt.close() t2 = np.interp([1.277], np.fliplr([header])[0], np.fliplr([T2])[0]) t3 = np.interp([1.277], np.fliplr([header])[0], np.fliplr([T3])[0]) _p2 = bezeir_fit.gen(t2, 4, P2)[0] _p3 = bezeir_fit.gen(t3, 4, P3)[0] _dat1 = bezeir_fit.gen(T, 2, [[1, 1], _p2, _p3]) _dat2 = bezeir_fit.gen(T, 2, PT[9][0]) plt.figure plt.plot(_dat1[:, 0], _dat1[:, 1], label='gen') plt.plot(_dat2[:, 0], _dat2[:, 1], label='rec') plt.legend() plt.show()
def run(): _file = "/home/room801/anaoverland/Steady_fit/ks1_Rise_CurveRec.flow" RiseRec = pickle.load(open(_file, 'r')) _file = "/home/room801/anaoverland/Steady_fit/ks1_Fall_CurveRec.flow" FallRec = pickle.load(open(_file, 'r')) RiseParam = pickle.load( open("/home/room801/anaoverland/Steady_fit/fit_1_RiseLimb.pick", 'r')) FallParam = pickle.load( open("/home/room801/anaoverland/Steady_fit/fit_1_FallLimb.pick", 'r')) plt.figure ks = [ 1.01, 1.05, 1.1, 1.2, 1.6, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 15.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0 ] # ks = [2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 15.0, 20.0] k_and_NS = list() for k in ks: keyStr = 'k=' + str(float(k)) out_s = RiseRec['S'][keyStr] out_q = RiseRec['Q'][keyStr] # Normalized curve to [0, 1] out_s[:, 1] = (out_s[:, 1] - min(out_s[:, 1])) / (max(out_s[:, 1]) - min(out_s[:, 1])) out_q[:, 1] = (out_q[:, 1] - min(out_q[:, 1])) / (max(out_q[:, 1]) - min(out_q[:, 1])) # Interpolation on flowrate record standard_s = np.arange(0, 1.001, 0.01) standard_q = np.interp(standard_s, out_s[0:len(out_q), 1], out_q[:, 1]) rec_sq = np.zeros([len(standard_s), 2]) rec_sq[:, 0] = standard_s rec_sq[:, 1] = standard_q t = bf.aff_angle(rec_sq) P = RiseParam.ref_points(k) sq = bf.gen(t, 6, P) R2 = 0.0 for j in range(0, len(sq)): R2 = R2 + ((sq[j, 0] - rec_sq[j, 0])**2 + (sq[j, 1] - rec_sq[j, 1])**2) R2 = sqrt(R2) plt.plot(out_q[:, 1], out_s[0:len(out_q), 1], color='aqua', linewidth='1.2') plt.plot(sq[:, 1], sq[:, 0], '-b') print k, R2 k_and_NS.append([k, R2]) for k in ks: keyStr = 'k=' + str(float(k)) out_s = FallRec['S'][keyStr] out_q = FallRec['Q'][keyStr] # Normalization to [0, 1] out_s[:, 1] = (out_s[:, 1] - min(out_s[:, 1])) / (max(out_s[:, 1]) - min(out_s[:, 1])) out_q[:, 1] = (out_q[:, 1] - min(out_q[:, 1])) / (max(out_q[:, 1]) - min(out_q[:, 1])) standard_s = np.arange(0, 1.001, 0.01) standard_q = np.interp(standard_s, mat_flip(out_s[0:len(out_q), 1]), mat_flip(out_q[:, 1])) rec_sq = np.zeros([len(standard_s), 2]) rec_sq[:, 0] = mat_flip(standard_s) rec_sq[:, 1] = mat_flip(standard_q) t = bf.aff_angle(rec_sq) P = FallParam.ref_points(1. / k) P[0] = [1.0, 1.0] P[-1] = [0.0, 0.0] sq = bf.gen(t, 6, P) R2 = 0.0 for j in range(0, len(sq)): R2 = R2 + ((sq[j, 0] - rec_sq[j, 0])**2 + (sq[j, 1] - rec_sq[j, 1])**2) R2 = sqrt(R2) plt.plot(out_q[:, 1], out_s[0:len(out_q), 1], color='darksalmon', lw=1.2) plt.plot(sq[:, 1], sq[:, 0], '-r') k_and_NS.append([1. / k, R2]) writeTxt('k_and_NS.txt', k_and_NS) plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.0]) plt.xlabel('$q^{{}*{}}$') plt.ylabel('$s^{{}*{}}$') plt.show()
def SWRFit(i0, i1, i2, L, x_space, dt, n, slope, deg): c1 = list() qBDRatio = list() sBDRatio = list() ptList = list() plt.figure plt.figure(figsize=(10, 8)) ax = plt.subplot(111) dat_And_Indicator = list() # Index 0: qB/qD # Index 1: sB/sD # Index 2: C1 # Index 3: [p, t] Secondary Curve Fit for i in range(0, len(i1)): ie1 = i1[i] N = 5./3 # Manning Eq alpha = 1./n*sqrt(slope) # Manning n steady_time = ((ie1/3600./1000.)**(1-N)*L/alpha)**(1/N) ToC = [steady_time*0., steady_time*0.001, steady_time*0.01, steady_time*0.05, steady_time*0.1, steady_time*0.3, steady_time*0.5, steady_time*0.55, steady_time*0.6, steady_time*0.65, steady_time*0.7, steady_time*0.75, steady_time*0.8, steady_time*0.9, steady_time*0.97, steady_time*0.99] for j in range(0, len(i2[i])): ie2 = i2[i][j] q2 = ie2/3600./1000.*L s2 = 1./(N+1)*alpha/(ie2/3600./1000.)*( (ie2/3600./1000.)*L/alpha)**((N+1)/N) for k in range(0, len(ToC)): d = ToC[k] ca = two_step.two_step_overland(i0, ie1, ie2, L, x_space, dt, n, slope) ca.ie1_duration(d) ca.run() SWR = bcdPoints(ca) # Clip Secondary Curve SWR = np.array(SWR) qBD = SWR[-1, 1]/SWR[0, 1] sBD = SWR[-1, 2]/SWR[0, 2] # # # Fit Second Drying Curve From Above # # # SWR[:, 0] = (SWR[:, 0] - SWR[0, 0])/(SWR[-1, 0] - SWR[0, 0]) SWR[:, 1] = SWR[:, 1]/SWR[0, 1] SWR[:, 2] = SWR[:, 2]/SWR[0, 2] t = np.arange(0, 1.01, 1./20) _SWR = np.zeros([21, 2]) _SWR[:, 0] = np.interp(t, SWR[:, 0], SWR[:, 1]) _SWR[:, 1] = np.interp(t, SWR[:, 0], SWR[:, 2]) # Fitting [p, z] = bezeir_fit.fit(_SWR, deg, 1.0E-8) p[0] = [1.0, 1.0] p[-1] = [SWR[-1, 1], SWR[-1, 2]] n_SWR = bezeir_fit.gen(t, deg, p) dat_And_Indicator.append([qBD, sBD, ie1/ie2, [p, z]]) color = randColor() labelString = ('$q_B/q_D$=' + '{:4.3f}'.format(qBD) + ', ' + '$c_1$=' + '{:4.3f}'.format(ie1/ie2)) print(labelString) ax.plot(SWR[:, 1], SWR[:, 2], lw=2.0, ls='-', label=labelString, color=color) ax.plot(n_SWR[:, 0], n_SWR[:, 1], lw=1.0, ls='--', color=color) # dat_And_Indicator = sorted(dat_And_Indicator, key=itemgetter(2)) for i in range(0, len(dat_And_Indicator)): qBDRatio.append(dat_And_Indicator[i][0]) sBDRatio.append(dat_And_Indicator[i][1]) c1.append(dat_And_Indicator[i][2]) ptList.append(dat_And_Indicator[i][3]) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) ax.legend(loc='upper left', bbox_to_anchor=(1.05, 1.0), ncol=2) ax.set_xlabel('q') ax.set_ylabel('s') plt.title('Secondary Wetting Curve - Normalized and Fitting') plt.savefig('SWR_fitting.png') plt.close() f = open('ks1_SWRrec.pick', 'wb') pickle.dump({'qBD': qBDRatio, 'sBD': sBDRatio, 'PT': ptList, 'C1': c1}, f) f.close()