Skip to content

Commit 39fd1eb

Browse files
author
kerim aydin
committed
Added SSB and recruit monthly output (first cut)
1 parent 55fe84d commit 39fd1eb

File tree

2 files changed

+76
-16
lines changed

2 files changed

+76
-16
lines changed

R/ecosim.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ rsim.run <- function(Rsim.scenario, method = 'RK4', years = 1:100) {
165165
colnames(rout$annual_QB) <- sps
166166
colnames(rout$annual_Qlink)<-1:(length(rout$annual_Qlink[1,]))
167167

168+
colnames(rout$out_SSB) <- scene$stanzas$Oldest
169+
colnames(rout$out_eggs) <- scene$stanzas$Oldest
170+
colnames(rout$out_Ninf) <- scene$stanzas$Oldest
171+
colnames(rout$out_Winf) <- scene$stanzas$Oldest
172+
colnames(rout$out_Nrec) <- scene$stanzas$Groups
173+
colnames(rout$out_Wrec) <- scene$stanzas$Groups
168174
# drop the last row (should be always 0; negative index is entry to drop)
169175
#lastone <- length(rout$annual_Catch[,1])
170176
#rout$annual_Catch <- rout$annual_Catch[-lastone,]
@@ -772,6 +778,7 @@ rsim.stanzas <- function(Rpath.params, state, params){
772778
rstan$Nsplit <- juvfile$NStanzaGroups
773779
#Need leading zeros (+1 col/row) to make indexing in C++ easier
774780
rstan$Nstanzas <- c(0, juvfile$stgroups$nstanzas)
781+
rstan$Totstanzas <- sum(rstan$Nstanzas)
775782
rstan$EcopathCode <- matrix(NA, rstan$Nsplit + 1, max(rstan$Nstanzas) + 1)
776783
rstan$Age1 <- matrix(NA, rstan$Nsplit + 1, max(rstan$Nstanzas) + 1)
777784
rstan$Age2 <- matrix(NA, rstan$Nsplit + 1, max(rstan$Nstanzas) + 1)
@@ -866,6 +873,23 @@ rsim.stanzas <- function(Rpath.params, state, params){
866873
rstan$RscaleSplit <- c(0, rep(1, rstan$Nsplit))
867874
#sPred1<-sPred+1; rstan$stanzaPred <- sPred1-1
868875
sPred1<-sPred+1; rstan$baseStanzaPred <- sPred1-1
876+
877+
# KYA 12/5/25 - added max.age to sim stanza object for indexing ssb during run
878+
rstan$Oldest <- c("Outside", rep(NA, rstan$Nsplit))
879+
rstan$Groups <- c("Outside", rep(NA, rstan$Totstanzas))
880+
rstan$MaxAge <- c(0, rep(0, rstan$Nsplit))
881+
sind <- 1
882+
for(isp in 1:rstan$Nsplit){
883+
for(ist in 1:rstan$Nstanzas[isp + 1]){
884+
sind <- sind + 1
885+
ieco <- 1 + rstan$EcopathCode[isp+1, ist+1]
886+
rstan$MaxAge[isp+1] <- max(rstan$MaxAge[isp+1], rstan$Age2[isp+1,ist+1])
887+
if (rstan$MaxAge[isp+1] == rstan$Age2[isp+1,ist+1]){
888+
rstan$Oldest[isp+1] <- params$spname[ieco]
889+
}
890+
rstan$Groups[sind] <- params$spname[ieco]
891+
}
892+
}
869893

870894
# SplitSetPred(rstan, state)
871895
}
@@ -875,6 +899,7 @@ rsim.stanzas <- function(Rpath.params, state, params){
875899
rstan$Nsplit <- juvfile$NStanzaGroups
876900
#Need leading zeros (+1 col/row) to make indexing in C++ easier
877901
rstan$Nstanzas <- c(0, 0)
902+
rstan$Totstanzas <- 0
878903
rstan$EcopathCode <- matrix(rep(0, 4), 2, 2)
879904
rstan$Age1 <- matrix(rep(0, 4), 2, 2)
880905
rstan$Age2 <- matrix(rep(0, 4), 2, 2)
@@ -902,7 +927,9 @@ rsim.stanzas <- function(Rpath.params, state, params){
902927
rstan$baseSpawnBio <- c(0, 0)
903928
rstan$RscaleSplit <- c(0, 0)
904929
rstan$baseStanzaPred <- c(0, 0)
905-
930+
rstan$Oldest <- c("Outside", "None")
931+
rstan$Groups <- c("Outside", "None")
932+
rstan.MaxAge <- c(0,0)
906933
}
907934

908935

src/ecosim.cpp

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ List Adams_run (List params, List instate, List forcing, List fishing, List stan
224224
int StartYear, int EndYear, List InitDeriv){
225225

226226
int y, m, dd;
227-
227+
//std::cout << " x1 ";
228228
// Get some basic needed numbers from the params List
229229
const int NUM_BIO = as<int>(params["NUM_LIVING"]) + as<int>(params["NUM_DEAD"]);
230230
const int NumPredPreyLinks = as<int>(params["NumPredPreyLinks"]);
@@ -254,15 +254,27 @@ int y, m, dd;
254254

255255
// Parameters from stanzas
256256
const int Nsplit = as<int>(stanzas["Nsplit"]);
257-
257+
const int Totstanzas = as<int>(stanzas["Totstanzas"]);
258+
const NumericVector Nstanzas = as<NumericVector>(stanzas["Nstanzas"]);
259+
const NumericVector MaxAge = as<NumericVector>(stanzas["MaxAge"]);
260+
NumericMatrix Age1 = as<NumericMatrix>(stanzas["Age1"]);
261+
//std::cout << " x1a ";
262+
// stanza outputs
263+
NumericMatrix out_SSB(EndYear * 12, Nsplit + 1);
264+
NumericMatrix out_eggs(EndYear * 12, Nsplit + 1);
265+
NumericMatrix out_Winf(EndYear * 12, Nsplit + 1);
266+
NumericMatrix out_Ninf(EndYear * 12, Nsplit + 1);
267+
NumericMatrix out_Wrec(EndYear * 12, Totstanzas + 1);
268+
NumericMatrix out_Nrec(EndYear * 12, Totstanzas + 1);
269+
//std::cout << " x1b ";
258270
// Parameter need to track catch by Gear
259271
const NumericVector FishFrom = as<NumericVector>(params["FishFrom"]);
260272

261273
// Monthly output matrices
262274
NumericMatrix out_Biomass( EndYear * 12, NUM_BIO + 1);
263275
NumericMatrix out_Catch( EndYear * 12, NUM_BIO + 1);
264-
NumericMatrix out_SSB(EndYear * 12, NUM_BIO + 1);
265-
NumericMatrix out_rec(EndYear * 12, NUM_BIO + 1);
276+
//NumericMatrix out_SSB(EndYear * 12, NUM_BIO + 1);
277+
//NumericMatrix out_rec(EndYear * 12, NUM_BIO + 1);
266278
NumericMatrix out_Gear_Catch(EndYear*12, NumFishingLinks+1);
267279
// Annual output matrices
268280
NumericMatrix annual_Catch(EndYear, NUM_BIO+1);
@@ -274,7 +286,12 @@ int y, m, dd;
274286
// Use Clone to make sure state/stanzas are copies of instate/instanzas, not pointers
275287
List state = clone(instate);
276288
//List stanzas = clone(instanzas);
277-
289+
//std::cout << " x1c ";
290+
NumericMatrix NageS = as<NumericMatrix>(state["NageS"]);
291+
NumericMatrix WageS = as<NumericMatrix>(state["WageS"]);
292+
NumericVector SpawnBio = as<NumericVector>(state["SpawnBio"]);
293+
NumericVector EggsStanza = as<NumericVector>(state["EggsStanza"]);
294+
//std::cout << " x1d ";
278295
// Update sums of split groups to total biomass for derivative calcs
279296
if(Nsplit > 0){
280297
SplitSetPred(stanzas, state);
@@ -284,7 +301,7 @@ int y, m, dd;
284301
List dyt = InitDeriv;
285302

286303
dd = StartYear * STEPS_PER_YEAR;
287-
304+
//std::cout << "x2";
288305
// MAIN LOOP STARTS HERE
289306
// ASSUMES STEPS_PER_MONTH will always be 1.0, took out divisions
290307
for (y = StartYear; y <= EndYear; y++){
@@ -392,8 +409,8 @@ int y, m, dd;
392409

393410
// Write to output matricies
394411
out_Biomass( dd, _) = old_Biomass;
395-
out_SSB(dd, _) = old_Biomass;
396-
out_rec(dd, _) = old_Biomass;
412+
//out_SSB(dd, _) = old_Biomass;
413+
//out_rec(dd, _) = old_Biomass;
397414
out_Catch( dd, _) = new_Catch;
398415
out_Gear_Catch(dd, _) = new_Gear_Catch;
399416
annual_Catch(y-1, _) = annual_Catch(y-1, _) + new_Catch;
@@ -402,12 +419,22 @@ int y, m, dd;
402419
annual_QB(y-1, _) = FoodGain/old_Biomass;
403420
annual_Qlink(y-1, _) = Qlink;
404421
}
405-
406-
//NOJUV for (i = 1; i <= juv_N; i++){
407-
//NOJUV out_SSB(dd, JuvNum[i]) = 0.0;
408-
//NOJUV out_SSB(dd, AduNum[i]) = SpawnBio[i];
409-
//NOJUV out_rec(dd, AduNum[i]) = NageS(firstMoAdu[i], i) * WageS(firstMoAdu[i], i);
410-
422+
//std::cout << "x3";
423+
// Write stanza outputs
424+
int sind, isp, ist;
425+
sind = 0;
426+
for (isp=1; isp<=Nsplit; isp++){
427+
out_Winf(dd,isp) = WageS(MaxAge[isp], isp);
428+
out_Ninf(dd,isp) = NageS(MaxAge[isp], isp);
429+
out_SSB(dd,isp) = SpawnBio[isp];
430+
out_eggs(dd,isp) = EggsStanza[isp];
431+
for (ist=1; ist<=Nstanzas[isp]; ist++){
432+
sind++;
433+
out_Nrec(dd,sind) = NageS(Age1(isp,ist), isp);
434+
out_Wrec(dd,sind) = WageS(Age1(isp,ist), isp);
435+
}
436+
}
437+
//std::cout << "x4";
411438
} // End of main months loop
412439

413440
}// End of years loop
@@ -432,7 +459,13 @@ int y, m, dd;
432459
_["annual_Catch"]=annual_Catch,
433460
_["annual_Biomass"]=annual_Biomass,
434461
_["annual_QB"]=annual_QB,
435-
_["annual_Qlink"]=annual_Qlink,
462+
_["annual_Qlink"]=annual_Qlink,
463+
_["out_SSB"]=out_SSB,
464+
_["out_eggs"]=out_eggs,
465+
_["out_Winf"]=out_Winf,
466+
_["out_Ninf"]=out_Ninf,
467+
_["out_Nrec"]=out_Nrec,
468+
_["out_Wrec"]=out_Wrec,
436469
_["end_state"]=state,
437470
_["crash_year"]=CRASH_YEAR,
438471
_["dyt"]=dyt);

0 commit comments

Comments
 (0)