gadget-framework / gadget3

TMB-based gadget implemtation
GNU General Public License v2.0
8 stars 6 forks source link

g3a_naturalmortality error when restricted age #149

Closed vbartolino closed 2 months ago

vbartolino commented 2 months ago

I'm trying to have an addition natural mortality only to the first age (age0) of the immature stock (age0-5) as below

imm_actions <- 
  list(...

    ## natural mortality by age (age0-5)
    gadget3::g3a_naturalmortality(imm_stock, 
                              gadget3::g3a_naturalmortality_exp()),

    ## extra natural mortality only age0
    gadget3::g3a_naturalmortality(imm_stock,
                              gadget3::g3a_naturalmortality_exp(
                                   param_f = g3_parameterized('Mice', by_age = FALSE)),
                                   run_f = ~age == 0),
  ...)

the model returns a likelihood but fails to compile the object function.

> result <- model(tmb_param$value)
> print(result[[1]])
[1] 32.62687

>   obj.fun <- gadget3::g3_tmb_adfun(tmb_model, tmb_param)
Note: Using Makevars in /home/vano0001/.R/Makevars 
g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I"/home/vano0001/R/x86_64-pc-linux-gnu-library/4.2/TMB/include" -I"/home/vano0001/R/x86_64-pc-linux-gnu-library/4.2/RcppEigen/include"  -DTMB_SAFEBOUNDS -DTMB_EIGEN_DISABLE_WARNINGS -DLIB_UNLOAD=R_unload_g3_tmb_93317d4cd0b1e8e8cb381a15addb2c674558c454  -DTMB_LIB_INIT=R_init_g3_tmb_93317d4cd0b1e8e8cb381a15addb2c674558c454  -DCPPAD_FRAMEWORK     -fpic  -std=gnu++11 -Wno-ignored-attributes -DEIGEN_PERMANENTLY_DISABLE_STUPID_WARNINGS -O3 -flto -march=native -c /tmp/RtmpCjrwNy/g3_tmb_93317d4cd0b1e8e8cb381a15addb2c674558c454.cpp -o /tmp/RtmpCjrwNy/g3_tmb_93317d4cd0b1e8e8cb381a15addb2c674558c454.o
/tmp/RtmpCjrwNy/g3_tmb_93317d4cd0b1e8e8cb381a15addb2c674558c454.cpp: In member function ‘Type objective_function<Type>::operator()()’:
/tmp/RtmpCjrwNy/g3_tmb_93317d4cd0b1e8e8cb381a15addb2c674558c454.cpp:1122:14: error: ‘age’ was not declared in this scope
 1122 |         if ( age == 0 ) {
      |              ^~~
make: *** [/usr/lib/R/etc/Makeconf:177: /tmp/RtmpCjrwNy/g3_tmb_93317d4cd0b1e8e8cb381a15addb2c674558c454.o] Error 1
Error in TMB::compile(gsub("\\\\", "/", cpp_path), flags = paste(c("-std=gnu++11",  : 
  Compilation failed

I've tried with a different age or with a different syntax like run_f = g3_formula(~age == 0) but the issue persists. Having g3a_naturalmortality expressed twice for a stock works, I've checked, also playing with ~cur_step ==. The issue seems linked to the ~age ==

lentinj commented 2 months ago

The issue seems linked to the ~age ==

You're right. Seems I was being overenthusiastic with my suggestions, but it's an easy fix. Will push something in a bit and let you know.

vbartolino commented 2 months ago

just tested with the seal model and it works nicely, thanks