1 | # Code to check out the effect of different bedslope approximations. |
---|
2 | |
---|
3 | |
---|
4 | # Compute side length |
---|
5 | len<-function(p1, p2){ |
---|
6 | sum( (p1[1]-p2[1])**2 + (p1[2]-p2[2])**2)^0.5 |
---|
7 | } |
---|
8 | |
---|
9 | |
---|
10 | # Compute normals |
---|
11 | nml<-function(p1, p2){ |
---|
12 | # This will produce a normal to p2-p1 (positive in the direction rotated |
---|
13 | # left from p2-p1) |
---|
14 | n_parallel = p2[1:2]-p1[1:2] |
---|
15 | n_perp = c(n_parallel[2], -n_parallel[1]) |
---|
16 | n_perp_len = sum(n_perp**2)^0.5 |
---|
17 | n_perp/n_perp_len |
---|
18 | } |
---|
19 | |
---|
20 | # Compute Area |
---|
21 | Area<-function(p1,p2,p3){ |
---|
22 | n1=nml(p2,p1) |
---|
23 | # 0.5*base*vertical height |
---|
24 | 0.5*len(p1,p2)*sum((p3[1:2]-p2[1:2])*n1)^0.5 |
---|
25 | } |
---|
26 | |
---|
27 | # z edge values |
---|
28 | z_edge<-function(p1,p2){ |
---|
29 | 0.5*(p2[3]+p1[3]) |
---|
30 | } |
---|
31 | |
---|
32 | # z centroid values |
---|
33 | z_c<-function(p1,p2,p3){ |
---|
34 | (p1[3]+p2[3]+p3[3])/3.0 |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | bedslope_difference<-function(water, p1,p2,p3, rescale=1){ |
---|
39 | |
---|
40 | # Use rescale to scale the triangle side lengths |
---|
41 | p2 = p1+(p2-p1)*rescale |
---|
42 | p3 = p1 + (p3-p1)*rescale |
---|
43 | |
---|
44 | # Side lengths |
---|
45 | l1 = len(p1, p2) |
---|
46 | l2 = len(p2, p3) |
---|
47 | l3 = len(p3, p1) |
---|
48 | |
---|
49 | # Normals |
---|
50 | n1=nml(p1,p2) |
---|
51 | n2=nml(p2,p3) |
---|
52 | n3=nml(p3,p1) |
---|
53 | |
---|
54 | # Ordinary bedslope |
---|
55 | dzdx=(l1*n1*z_edge(p1,p2) + l2*n2*z_edge(p2,p3) + l3*n3*z_edge(p3,p1))/Area(p1,p2,p3) |
---|
56 | # Centroid depth |
---|
57 | dc = water-z_c(p1,p2,p3) |
---|
58 | |
---|
59 | # Bedslope 1 |
---|
60 | b1=dzdx*g*dc |
---|
61 | |
---|
62 | # Bedslope 2 |
---|
63 | b2=-g/2*(l1*n1*(water-z_edge(p1,p2))**2 + l2*n2*(water-z_edge(p2,p3))**2 + l3*n3*(water-z_edge(p3,p1))**2)/Area(p1,p2,p3) |
---|
64 | |
---|
65 | # Alternative_depth |
---|
66 | d2= b2/(dzdx*g) |
---|
67 | |
---|
68 | out=list() |
---|
69 | |
---|
70 | out$standard_bedslope=b1 |
---|
71 | out$alternative_bedslope=b2 |
---|
72 | out$ratio=b2/b1 |
---|
73 | out$centroid_depth=dc |
---|
74 | out$alternative_depth_vector=d2 |
---|
75 | #print(c('Standard bedslope', b1)) |
---|
76 | #print(c('Alternative bedslope', b2)) |
---|
77 | #print(c('Ratio b2/b1', b2/b1)) |
---|
78 | #print(c('Centroid depth: ', dc, ' Alternative depth vector:', d2)) |
---|
79 | out |
---|
80 | |
---|
81 | } |
---|
82 | |
---|
83 | #############################3 |
---|
84 | # |
---|
85 | # MAIN COMPUTATION |
---|
86 | # |
---|
87 | ############################# |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | # Water elevation |
---|
92 | water=1.2 |
---|
93 | |
---|
94 | # Triangle points |
---|
95 | p1 = c(0,1, 0) |
---|
96 | p2 = c(0, 0, 1) |
---|
97 | p3= c(1,0,0) |
---|
98 | |
---|
99 | # Gravity |
---|
100 | g=9.8 |
---|
101 | |
---|
102 | out=bedslope_difference(water, p1,p2,p3) |
---|
103 | |
---|
104 | n=100 |
---|
105 | waters=seq(1.1,10,len=n) |
---|
106 | store1=matrix(NA,nrow=n,ncol=2) |
---|
107 | storep5=matrix(NA,nrow=n,ncol=2) |
---|
108 | storep25=matrix(NA,nrow=n,ncol=2) |
---|
109 | for(i in 1:length(waters)){ |
---|
110 | out=bedslope_difference(waters[i],p1,p2,p3) |
---|
111 | store1[i,]=out$ratio |
---|
112 | out=bedslope_difference(waters[i],p1,p2,p3, rescale=0.5) |
---|
113 | storep5[i,]=out$ratio |
---|
114 | out=bedslope_difference(waters[i],p1,p2,p3, rescale=0.25) |
---|
115 | storep25[i,]=out$ratio |
---|
116 | } |
---|