forked from Plant-Root-Soil-Interactions-Modelling/CRootBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_length.py
368 lines (316 loc) · 9.58 KB
/
benchmark_length.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#
# Length Benchmark
#
# Compares the analytically calculated lengths with the rootbox approximation
# parameters are set within the code L65- (with no standard deviation)
#
# Benchmark 1: single root, no laterals
# Benchmark 2: single root with laterals
# Benchmark 3: basal roots, no laterals
# Benchmark 4: basal roots with laterals
#
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import py_rootbox as rb
def maxRootLength(la,lb,ln,nob): # maximal length the root will reach
return la+lb+(nob-1)*ln
def rootLength(t,r,k): # root length at a certain age
return k*(1-np.exp(-r*t/k))
def rootAge(l,r,k): # root age at a certain length
return -np.log(1-l/k)*k/r
def rootLateralLength(t,et,r,k): # length of first order laterals (without second order laterals)
i = 0
l = 0
while et[i] <t:
age = t-et[i]
l += rootLength(age,r,k)
i += 1
return l
def v2a(vd): # rb.std_vector_double_ to numpy array
l = np.zeros(len(vd))
for i in range(0,len(vd)):
l[i] = vd[i]
return l
def a2v(a): # numpy array to rb.std_vector_double
l = rb.std_vector_double_()
for d in a:
l.append(d)
return l
def a2i(a): # numpy array to rb.std_vector_int
l = rb.std_vector_int_()
for i in a:
l.append(i)
return l
def vv2a(vd): # rb.std_vector_Vector3_ to numpy array
N = len(vd)
l = np.zeros((N,3))
for i in range(0,N):
l[i,:] = [vd[i].x,vd[i].y,vd[i].z]
return l
#
# Root type parameter
#
p0 = rb.RootTypeParameter()
p1 = rb.RootTypeParameter()
# Taproot
p0.name = "taproot"
p0.type = 1
p0.lb = 1
p0.la = 10
p0.nob = 20
p0.ln = 89./19.
p0.r = 1
p0.dx = 0.5
p0.k = maxRootLength(p0.la,p0.lb,p0.ln,p0.nob)
# print(p0)
# 1st order lateral
p1.name = "lateral"
p1.type = 2
p1.la = 25
p1.ln = 0
p1.r = 2
p1.k = 25
p1.dx = 0.1
# print(p1)
#
# Root system parameter (neglecting shoot borne)
#
maxB = 100
firstB = 10.
delayB = 3.
rsp = rb.RootSystemParameter()
rsp.set(-3., firstB, delayB, maxB, 0, 1.e9, 1.e9, 1.e9, 0., 0.)
times = np.array([7.,15.,30.,60.])
dt = np.zeros(len(times)+1)
dt[1:] = times
dt = np.diff(dt) # how do i put that in 1 line?
#
# Benchmark 1: single root, no laterals
#
print("* ")
print("* Benchmark 1: single root, no laterals")
print("*")
# Analytical
l = rootLength(times,p0.r,p0.k)
# Numerical
rs = rb.RootSystem()
rs.setRootTypeParameter(p0)
rs.initialize()
c = 0
nl = np.zeros(len(times))
non = np.zeros(len(times))
for t in dt:
rs.simulate(t, True)
d = v2a(rs.getScalar(rb.ScalarType.length))
nl[c] = d[0]
non[c] = rs.getNumberOfNodes()
c += 1
# Numerical, same, but with tiny time stepping
rs.initialize() # resets everything
c = 0
nl2 = np.zeros(len(times))
non2 = np.zeros(len(times))
for t in dt:
dt_ = t/1000.
for i in range(0,1000):
rs.simulate(dt_, True)
d = v2a(rs.getScalar(rb.ScalarType.length))
nl2[c] = d[0]
non2[c] = rs.getNumberOfNodes()
c += 1
print("times \t\t\t", times)
print("analytical lenghts \t", l)
print("numerical lenghts \t", nl)
print("numerical lenghts 2 \t", nl2, "\n")
print("mean axial resoltuion = \t", nl/non) # should be less but in the same order as the defined axial resolution
print("mean axial resoltuion 2 = \t", nl2/non2,"\n") # should be less but in the same order as the defined axial resolution
#
# Benchmark 2: single root
#
print("* ")
print("* Benchmark 2: single root")
print("*")
# Analytical
i = 0
et = np.zeros(int(p0.nob))
while i<p0.nob:
et[i] = rootAge(p0.la+p0.lb+p0.ln*i,p0.r,p0.k+1e-12)
i += 1
# print("lateral emergence times", et)
l = rootLength(times,p0.r,p0.k) # zero order lengths
l1 = np.zeros(times.size)
j = 0
for t in times :
l1[j] = rootLateralLength(t,et,p1.r,p1.k)
j=j+1
# Numerical
p0.successor = a2i([2]) # add successors
p0.successorP = a2v([1])
rs = rb.RootSystem()
rs.setRootTypeParameter(p0)
rs.setRootTypeParameter(p1)
rs.initialize()
c = 0
nl = np.zeros(len(times))
nl0 = np.zeros(len(times))
non = np.zeros(len(times))
for t in dt:
rs.simulate(t, True)
d = v2a(rs.getScalar(rb.ScalarType.length))
nl[c] = sum(d)
nl0[c] = d[0] # first entry is the tap root
non[c] = rs.getNumberOfNodes()
c += 1
# Numerical, same, but with tiny time stepping
rs.initialize() # resets everything
c = 0
nl2 = np.zeros(len(times))
nl02 = np.zeros(len(times))
non2 = np.zeros(len(times))
for t in dt:
dt_ = t/1000.
for i in range(0,1000):
rs.simulate(dt_, True)
d = v2a(rs.getScalar(rb.ScalarType.length))
nl2[c] = sum(d)
nl02[c] = d[0] # first entry is the tap root
non2[c] = rs.getNumberOfNodes()
c += 1
print("times \t\t\t\t", times)
print("analytical zero order length \t", l)
print("numerical zero order length \t", nl0)
print("analytical first order length \t", l1)
print("numerical first order length \t", nl-nl0)
print("analytical total length \t", l+l1)
print("numerical total length \t\t", nl)
print("numerical total length 2 \t", nl2, "\n")
print("mean axial resoltuion = \t", nl/non) # should be less but in the same order as the defined axial resolution
print("mean axial resoltuion 2 = \t", nl2/non2,"\n") # should be less but in the same order as the defined axial resolution
#
# Benchmark 3: basal roots, no laterals
#
print("* ")
print("* Benchmark 3: basal roots, no laterals")
print("* ")
# Analytical
etB = np.array(range(maxB))*delayB + np.ones(maxB)*firstB # basal root emergence times
bl = np.zeros(times.size)
j = 0 # time counter
for t in times:
i = 0 # basal root counter
while t-etB[i]>0:
bl[j] += rootLength(t-etB[i],p0.r,p0.k)
i += 1
j += 1
# Numerical
p0.successor = a2i([]) # remove successors
p0.successorP = a2v([])
rs = rb.RootSystem()
rs.setRootSystemParameter(rsp)
rs.setRootTypeParameter(p0)
rs.initialize()
c = 0
nl = np.zeros(len(times))
non = np.zeros(len(times))
nl_tap = np.zeros(len(times))
nl_basal = np.zeros(len(times))
for t in dt:
rs.simulate(t, True)
d = v2a(rs.getScalar(rb.ScalarType.length))
nl[c] = sum(d)
non[c] = rs.getNumberOfNodes()
seg = rs.getSegments()
nodes = rs.getNodes()
ana = rb.SegmentAnalyser(rs)
ana.filter(rb.ScalarType.type,1.) # 1 is the type number of the tap root
nl_tap[c] = ana.getSummed(rb.ScalarType.length)
ana = rb.SegmentAnalyser(rs)
ana.filter(rb.ScalarType.type,4.) # 4 is the default type number of basal roots
nl_basal[c] = ana.getSummed(rb.ScalarType.length)
c += 1
print("times \t\t\t\t", times)
print("analytical tap root lenght \t", l)
print("numerical tap root lenght \t", nl_tap)
print("analytical summed basal length \t", bl)
print("numerical summed basal length \t", nl_basal)
print("analytical total length \t", l+bl)
print("numerical total length \t\t", nl_tap+nl_basal, " (SegmentAnalyser) \n \t\t\t\t", nl, " (RootSystem)\n")
print("mean axial resoltuion = \t", nl/non) # should be less but in the same order as the defined axial resolution
#
# Benchmark 4: basal roots, with laterals
#
print("\n* ")
print("* Benchmark 4")
print("* ")
# Analytical
# etB as berfore
# et a before
bl = np.zeros(times.size)
j = 0 # time counter
for t in times:
i = 0 # basal root counter
while t-etB[i]>0:
bl[j] += ( rootLateralLength(t-etB[i],et,p1.r,p1.k) + rootLength(t-etB[i],p0.r,p0.k) )
i += 1
j += 1
# Numerical
p0.successor = a2i([2]) # add successors
p0.successorP = a2v([1])
rs = rb.RootSystem()
rs.setRootSystemParameter(rsp)
rs.setRootTypeParameter(p0)
rs.setRootTypeParameter(p1)
rs.initialize()
c = 0
nl = np.zeros(len(times))
non = np.zeros(len(times))
nl_tap = np.zeros(len(times))
nl_taplateral = np.zeros(len(times))
nl_basal = np.zeros(len(times))
nl_basallateral = np.zeros(len(times))
for t in dt:
rs.simulate(t,True)
d = v2a(rs.getScalar(rb.ScalarType.length))
nl[c] = sum(d)
non[c] = rs.getNumberOfNodes()
ana = rb.SegmentAnalyser(rs)
ana.filter(rb.ScalarType.type,1.) # 1 is the type number of the tap root
nl_tap[c] = ana.getSummed(rb.ScalarType.length)
ana = rb.SegmentAnalyser(rs)
ana.filter(rb.ScalarType.parenttype,1.) # 1 is the type number of the tap root
nl_taplateral[c] = ana.getSummed(rb.ScalarType.length)
ana = rb.SegmentAnalyser(rs)
ana.filter(rb.ScalarType.type,4.) # 4 is the default type number of basal roots
nl_basal[c] = ana.getSummed(rb.ScalarType.length)
ana = rb.SegmentAnalyser(rs)
ana.filter(rb.ScalarType.parenttype,4.) # 4 is the default type number of basal roots
nl_basallateral[c] = ana.getSummed(rb.ScalarType.length)
c += 1
print("times \t\t\t\t", times)
print("analytical tap root lenght \t", l+l1, " (...all including their laterals) ")
print("numerical tap root lenght \t", nl_tap+nl_taplateral)
print("analytical summed basal length\t", bl)
print("numerical summed basal length \t", nl_basal+nl_basallateral)
print("analytical total length \t", l+l1+bl)
print("numerical total length \t", nl, "\n")
print("mean axial resoltuion = \t", nl/non) # should be less but in the same order as the defined axial resolution
# #
# # Matlab like plotting of analytical sotlution of a single root with laterals
# #
# t_ = np.linspace(0,100,100)
# l_ = rootLength(t_,p0.r,p0.k)
# l1_ = np.zeros(t_.size)
# i = 0
# for t in t_:
# l1_[i] = rootLateralLength(t, et, p1.r, p1.k)
# i += 1
# plt.plot(t_,l_)
# plt.plot(t_,l1_,'g')
# plt.plot(times,l,'ro')
# h0 = mpatches.Patch(color='blue', label='zero order')
# h1 = mpatches.Patch(color='green', label='first order')
# plt.xlabel("Age (days)")
# plt.ylabel("Length (cm)")
# plt.legend([h0, h1],['zero order','first order'])
# plt.show()