GR3Dfunction.R 12.8 KB
Newer Older
1
2
3
#----------------------------------------------
# Temperature effect
#----------------------------------------------
4
5
6
7
8
9
10
11
12
# temperature effect in GR3D
# from Rosso
temperatureEffect = function(tempWater, Tmin, Topt, Tmax){
  response = (tempWater - Tmin) * (tempWater - Tmax) / ((tempWater - Tmin) * (tempWater - Tmax) - (tempWater - Topt)^2)
  
  response[tempWater <= Tmin | tempWater >= Tmax] = 0
  return(response)
}

patrick.lambert's avatar
patrick.lambert committed
13
14
15
16
17
18
19
20
21
thermalRange = function(pct = 0.8, Tmin, Topt, Tmax){
  lower = uniroot(function(x) temperatureEffect(x, Tmin, Topt, Tmax) - pct,
           interval = c(Tmin, Topt))$root
  upper = uniroot(function(x) temperatureEffect(x, Tmin, Topt, Tmax) - pct,
                  interval = c(Topt, Tmax))$root
  return(c(lower = lower,upper = upper))
}

#optimalThermalRange(Tmin = 3, Topt = 17, Tmax =27)
22

23
24
25
#----------------------------------------------
# Growth simulation
#---------------------------------------------
26
27
28
29
30
31
32
33
34
35
36
37
38
# von Bertalaffy : L = f(age)
vonBertalanffyGrowth = function(age, L0, Linf, K){
  t0 = log(1 - L0 / Linf) / K
  return(Linf * (1 - exp(-K * (age - t0))))
}

# von Bertalanffy inverse  : age = f(L)
# with L0 rather the usual t0 parameter
vonBertalanffyInverse = function(L, L0, Linf, K){
  t0 = log(1 - L0/Linf)/K
  return(t0 - log(1 - L/Linf)/K)
}

Poulet Camille's avatar
Poulet Camille committed
39

40
41
# von Bertalanffy increment
# pas cohérent avec la temperature effet sur le coeff de croissance mais ca marche
patrick.lambert's avatar
patrick.lambert committed
42

patrick.lambert's avatar
patrick.lambert committed
43
vonBertalanffyIncrement = function(nStep, L0, Linf, K, timeStepDuration, sigma, withTempEffect=FALSE, TrefAtSea = c(9.876946, 13.489854, 15.891487, 11.554104) ){
44
45
46
47
  tempEffect = temperatureEffect( TrefAtSea , 3, 17, 26)
  L = matrix(nrow = nStep + 1)
  L[1] = L0
  for (i in 1:nStep) {
patrick.lambert's avatar
patrick.lambert committed
48
    mu = log((Linf - L[i]) * (1 - exp(-K * timeStepDuration))) - sigma * sigma / 2
49
50
51
52
53
54
55
56
57
    increment = exp(rnorm(1, mu, sigma))
    if (withTempEffect) {
      increment = increment * tempEffect[((i - 1) %% 4) + 1]
    }
    L[i + 1] = L[i] + increment
  }
  return(L)
}

patrick.lambert's avatar
patrick.lambert committed
58
vonBertalanffyWithNextIncrement = function(L,  Linf, K, timeStepDuration, sigma, tempEffect ){
59
  if (sigma == 0) {
patrick.lambert's avatar
patrick.lambert committed
60
    mu = log((Linf - L) * (1 - exp(-K * timeStepDuration)))
61
62
63
    increment = exp(mu)
  }
  else {
patrick.lambert's avatar
patrick.lambert committed
64
    mu = log((Linf - L) * (1 - exp(-K * timeStepDuration))) - (sigma * sigma) / 2
65
    increment = exp(rnorm(length(mu), mu, sigma))
66
67
68
69
70
  }
  L = L + increment * tempEffect
  return(L)
}

patrick.lambert's avatar
patrick.lambert committed
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
# generate a cohort of length trajectories
computeMultipleLengthTrajectories = function(temperaturePattern, Nind = 10, growPar){
  ages = temperaturePattern$age
  
  res <- expand_grid(ind = seq.int(Nind),  age = ages) %>% 
    inner_join(temperaturePattern, by = 'age') %>% 
    arrange(age, ind) %>% 
    mutate(temperatureEffect = temperatureEffect(temperature, 
                                                 growPar$tempMinGrow, 
                                                 growPar$tempOptGrow, 
                                                 growPar$tempMaxGrow), 
           L = if_else(age == 0, growPar$lengthAtHatching, 0))
  
  for (i in 2:length(ages)) {
    previousAge  = ages[i - 1]
    currentAge = ages[i]
    tempEffect = res %>% filter(age == currentAge) %>% select(temperatureEffect) %>% unlist(use.names = FALSE)
    previousL <- res %>% filter(age == previousAge) %>% select(L) %>% unlist(use.names = FALSE)
    
    currentL <- vonBertalanffyWithNextIncrement(L = previousL, 
                                                Linf = growPar$Linf, 
                                                K = growPar$kOpt,
                                                timeStepDuration = currentAge - previousAge, 
                                                sigma = growPar$sigmaDeltaLVonBert, 
                                                tempEffect = tempEffect)
    res = res %>% mutate(L =replace(L, age == ages[i], currentL) ) 
  }
  return(res)
}

computeMultipleLengthTrajectoriesWithRandomSeed = function(temperaturePattern, 
                                                           Nind = 10, 
                                                           growPar, 
                                                           RNGseed =1){
  set.seed(RNGseed)
  ages = temperaturePattern %>% 
    distinct(age) %>% 
    unlist(use.names = FALSE)
  
  res <- expand_grid(ind = seq.int(Nind),  age = ages) %>% 
    mutate(random = rnorm(Nind * length(ages)))  %>% 
    inner_join(temperaturePattern, by = 'age') %>% 
    arrange(age, ind) %>% 
    mutate(temperatureEffect = temperatureEffect(temperature, 
                                                 growPar$tempMinGrow, 
                                                 growPar$tempOptGrow, 
                                                 growPar$tempMaxGrow), 
           L = if_else(age == 0, growPar$lengthAtHatching, 0))
  
  for (i in 2:length(ages)) {
    previousAge  = ages[i - 1]
    currentAge = ages[i]
    tempEffect <-  res %>% filter(age == currentAge) %>% 
      select(temperatureEffect) %>% unlist(use.names = FALSE)
    
    previousL <- res %>% filter(age == previousAge) %>% 
      select(L) %>% unlist(use.names = FALSE)
    rnd <- res %>% filter(age == currentAge) %>% select(random) %>% unlist(use.names = FALSE)
    currentL <- vonBertalanffyWithRandomVector(L = previousL, 
                                               Linf = growPar$Linf, 
                                               K = growPar$kOpt,
                                               timeStepDuration = currentAge - previousAge, 
                                               randomVector = rnd,
                                               sigma = growPar$sigmaDeltaLVonBert, 
                                               tempEffect = tempEffect)
    res = res %>% mutate(L = replace(L, age == ages[i], currentL) ) 
  }
  return(res)
}

#computeMultipleLengthTrajectoriesWithRandomSeed(temperaturePattern, Nind = 10, 
#growPar = growParUnisex,
# RNGseed = 1)

vonBertalanffyWithRandomVector = function(L, Linf, K, timeStepDuration, randomVector, sigma, tempEffect ){
  if (sigma == 0) {
    mu = if_else(L < Linf, log((Linf - L) * (1 - exp(-K * tempEffect * timeStepDuration))),-Inf)
    #mu =  log((Linf - L) * (1 - exp(-K * tempEffect * timeStepDuration)))
    increment = exp(mu)
  }
  else {
    mu = if_else(L < Linf, log((Linf - L) * (1 - exp(-K * tempEffect * timeStepDuration))) - (sigma * sigma) / 2, -Inf)
    #      mu = log((Linf - L) * (1 - exp(-K * tempEffect * timeStepDuration))) - (sigma * sigma) / 2
    increment = exp(randomVector * sigma + mu)
  }
  L = pmin(Linf, L + increment) 
  return(L)
}

patrick.lambert's avatar
patrick.lambert committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
computeOgive = function(lengthTrajectories, lengthAtMaturity){
  ogive <- lengthTrajectories %>%
    group_by(age) %>%
    summarise(nTotal = n()) %>%
    left_join(lengthTrajectories %>%  group_by(age) %>%
                filter(L >= lengthAtMaturity) %>%
                summarise(taller = n(), .groups = 'drop'),
              by = 'age') %>%
    replace_na(list(taller = 0)) %>%
    mutate(mature = c(0,diff(taller)), 
           immature = nTotal - cumsum(mature),
           p = if_else(mature + immature > 0 , mature / (mature + immature), 1)) %>% 
    select(age, p)
  return(ogive)
}
patrick.lambert's avatar
patrick.lambert committed
175

patrick.lambert's avatar
patrick.lambert committed
176
177
# deprecated
computeOgive3 = function(lengthTrajectories, lengthAtMaturity) {
patrick.lambert's avatar
patrick.lambert committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  ogive <-
    lengthTrajectories %>% 
    group_by(age) %>% 
    summarise(nTotal=n()) %>% 
    left_join(lengthTrajectories %>%  group_by(age) %>% 
                filter(L > lengthAtMaturity) %>% 
                summarise(mature = n(), .groups = 'drop'), 
              by = 'age') %>% 
    replace_na(list(mature = 0)) %>% 
    mutate(p =  mature/nTotal) %>% 
    select(age, p)
  
  return(ogive)
} 

193
194
195
196
197
198
199
200
201
202
203
# -------------------------------------------------------
# Dispersal      
# see see (Chapman et al., 2007; Holloway et al., 2016)
# -------------------------------------------------------
logitKernel =  function(distance, alpha0, alpha1, meanInterDistance, standardDeviationInterDistance,
                        basinSurface = 0, alpha2 = 0, meanBasinSurface = 0, standardDeviationBasinSurface = 1,
                        fishLength = 0, alpha3 = 0, meanLengthAtRepro = 0, standardDeviationLengthAtRepro = 1) {
  logitW =  alpha0 - alpha1 * (distance - meanInterDistance) / standardDeviationInterDistance +
    alpha2 * (basinSurface - meanBasinSurface) / standardDeviationBasinSurface +
    alpha3 * (fishLength - meanLengthAtRepro) / standardDeviationInterDistance;
  
204
205
206
207
208
209
210
211
212
213
214
215
216
  W = 1 / (1+ exp(-logitW))
  return(W)
}
# extended negative exponential kernel
eneKernel =  function(distance, alpha_D, beta_D){
  W = exp(-alpha_D * distance ^ beta_D)
  return(W)
}

strayerSurvival = function(distance, m ){
  # m: extra mortality rate for strayers (L-1)
  return(exp(-m * distance))
}
217
218
219
220
221

#-------------------------------------------------------
# Spwaner survival before reproduction 
# Dome-shape curve with temperature effect
#-------------------------------------------------------
222
spawnerSurvivalPreReproduction <- function(Triver,Tmin, Topt, Tmax, parSurv){
223
224
  
  #River survival
225
  survProbOptRiver =  parSurv$survProbOptRiver
226
  
Poulet Camille's avatar
Poulet Camille committed
227
  spRiver = survProbOptRiver * temperatureEffect(Triver, Tmin, Topt, Tmax)
228
229
230
231
232
233
234
235
236
237
238
239
  
  #StockAfterSurv = S * SpRiver
  
  return(spRiver)
  
}

#----------------------------------------------
# Stock Recruitement relationship
#See Rougier et al, 2014; 2015 and Jatteau et al., 2017
#----------------------------------------------
#survival of larvae 14dph from Jatteau et al, 2017
240
241
242
243
244
245
246
247
248
249
stockRecruitementRelationship <- function(Triver,Tmin, Topt, Tmax, survivalStock, parRep, parSurv, surfaceWatershed = 84810) {
  
  #surfaceWatershed = 84810 Surface de la Garonne 
  lambda = parRep$lambda
  deltaT = parRep$deltaT
  survOptRep =  parRep$survOptRep
  eta = parRep$eta #simule l'effet allee
  ratioS95_S50 = parRep$ratioS95_S50
  alpha =  parRep$a #species fecundity 
  survProbOptRiver =  parSurv$survProbOptRiver
250
251
252
253
  periodAtSea = 5 - deltaT
  
  ###--------------------- SR relationship -----------------
  
Poulet Camille's avatar
Poulet Camille committed
254
  #parametre c de la RS de BH int?grant un effet du BV consid?r? 
255
256
  cj = lambda/surfaceWatershed
  
Poulet Camille's avatar
Poulet Camille committed
257
  #parametre b repr?sentant la mortalit? densit? d?pendante de la RS de BH int?grant un effet de la temperature
258
259
260
261
262
  # bj = (-(1/deltaTrecruitement))*
  #   log(survOptRep * temperatureEffect(temp, 9.8, 20.0, 26.0))
  
  bj = -log(survOptRep * temperatureEffect(Triver, Tmin , Topt , Tmax)) / deltaT
  
Poulet Camille's avatar
Poulet Camille committed
263
  #parametre a (f?condit? de l'esp?ce) de la RS de BH int?grant un effet de la temperature
264
265
266
  alphaj = (bj * exp(-bj * deltaT)) / (cj * (1 - exp(-bj * deltaT)))
  alphaj[is.na(alphaj)] <- 0
  
Poulet Camille's avatar
Poulet Camille committed
267
  #Bj = param?tre de la relation SR int?grant l'effet de la temp?rature 
268
269
  betaj = bj/(alpha*cj*(1 - exp(-bj*deltaT)))
  
Poulet Camille's avatar
Poulet Camille committed
270
  #p = proportion de g?niteurs participant ? la reproduction en focntion de la quantit? de g?niteur total
271
272
  #p = 1/(1+exp(-log(19)*(S-n)/(Ratio*surfaceWatershed)))
  
273
  S95 = eta * surfaceWatershed
274
275
276
277
  S50 = S95/ratioS95_S50
  
  p = 1/(1 + exp(-log(19)*(survivalStock - S50)/(S95 - S50)))
  
Poulet Camille's avatar
Poulet Camille committed
278
  #relation Stock Recrutement ie calcul le nombre de recrues en fonction du nombre de g?niteurs et de la T en int?grant l'effet Allee 
279
280
281
  
  #R0 = aj * S * p 
  
282
  alleeEffect = 1/(1 + exp(-log(19)*(survivalStock - eta/ratioS95_S50*surfaceWatershed)/(eta*surfaceWatershed - eta/ratioS95_S50*surfaceWatershed)))
283
284
  
  
Poulet Camille's avatar
Poulet Camille committed
285
  Rj = (alphaj * survivalStock * alleeEffect)/(betaj + survivalStock * alleeEffect)
286
287
288
289
290
291
292
293
294
295
296
297
  
  #Rj = ((aj * S) * p)/(Bj +S * p)
  
  stockRecruitement = as.vector(Rj)
  
  return(stockRecruitement)
}


# ----------------------------------------------
# Spawner survival after reproduction 
#----------------------------------------------
298
299
300
301
302
303
304
305
306
307
308
#Logit with temperature effect depending on a Triver 
spawnerSurvivalPostReproductionTempRiver <- function(Triver, coeffa, coeffb){
  
  spRiverPostSpawn = (coeffb/ (1 + exp(coeffa*(Triver))))
  
  return(spRiverPostSpawn)
  
}

#Logit with temperature effect depending on the difference between the river temperature and Tref
spawnerSurvivalPostReproductionTempRef <- function(Triver,Tref, coeffa, coeffb){
309
310
311
312
313
314
315
  
  spRiverPostSpawn = (coeffb/ (1 + exp(coeffa*(Triver-Tref))))
  
  return(spRiverPostSpawn)
  
}

316
317
318
319
320
321
322
#Logit for survival after reproduction with log19
#similar to spawnerSurvivalPostReproductionWithTempRef which include a

logit2 = function(Triver, Tref,minTempForIteroparity){
  return( 1/ (1+exp((log(19)/(minTempForIteroparity-Tref))*(Triver-Tref))))
}

323
#Dome-shape curve with temperature effect 
324
spawnerSurvivalPostReproductionWithBellCurve <- function(Triver, Tmin, Topt, Tmax, coeffb){
325
326
327
328
329
330
331
332
333
334
335
  
  #P1: 
  #SpRiverPostSpawn = probSurvAfterRepro/(probSurvAfterRepro + (1 - probSurvAfterRepro)*exp(-coeffLogit*(TemperatureEffect(Triver, Tmin, Topt, Tmax))))
  
  #P2: 
  #SpRiverPostSpawn = probSurvAfterRepro/(probSurvAfterRepro + (1 - probSurvAfterRepro)*exp(-coeffLogit))
  #SpRiverPostSpawnTempEffect = (probSurvAfterRepro/(probSurvAfterRepro + (1 - probSurvAfterRepro)*exp(-coeffLogit))) * TemperatureEffect(Triver, Tmin, Topt, Tmax)
  
  #P3: 
  spRiverPostSpawn = coeffb * temperatureEffect(Triver, Tmin, Topt, Tmax)
  
336
  #stockAfterSpawn = S * spRiverPostSpawn 
337
338
339
340
341
342
  
  return(spRiverPostSpawn)
}



343