bodkan / slendr

Population genetic simulations in R 🌍
https://bodkan.net/slendr
Other
54 stars 5 forks source link

Avoid repeated resetting of spatial bounds (closes #147) #148

Closed bodkan closed 9 months ago

bodkan commented 9 months ago

As discussed in #147, this is a fix for a backwards incompatibility change / bugfix which caused issues with slendr's built-in SLiM script because of repeated (and unnecessary) call to setSpatialBounds() upon each map update. Because the dimensions of raster maps of slendr models are always the same for all populations, this call can happen upon first creating a population and not after.

Minimum reproducible example:

devtools::load_all()
init_env()

# create an example map
map <- world(xrange = c(-25, 55), yrange = c(-32, 35), crs = 4326)

# create a single population spread all over the map
pop <- population("pop", time = 1, N = 2000, map = map)

# compile a single-population model
model <- compile_model(
  populations = pop, generation_time = 30, simulation_length = 1000,
  resolution = 1, competition = 1, dispersal = 1, mating = 1
)

# run the model through SLiM, return a tree sequence
ts <- slim(model, sequence_length = 1000, recombination_rate = 0) # <=== crashes in SLiM 4.1, but works fine with 4.0.1

Crash log:

// Initial random seed:                                                                          
7837909936800388019

// RunInitializeCallbacks():
SEED: 7837909936800388019
initializeSLiMOptions(keepPedigrees = T, dimensionality = 'xy');
initializeInteractionType(0, "xy", reciprocal=T, maxDistance=1);
initializeInteractionType(1, "xy", reciprocal=T, maxDistance=1);
initializeTreeSeq();
initializeMutationType(0, 0.5, "f", 0);
initializeGenomicElementType(1, m0, 1);
initializeGenomicElement(g1, 0, 999);
initializeMutationRate(0);
initializeRecombinationRate(0);

// Starting run at tick <start>:
1 

Generation 1: starting the simulation
Generation 1: creating pop(p0)
Generation 1: updating map for pop(p0)
ERROR (Subpopulation::ExecuteMethod_setSpatialBounds): setSpatialBounds() new spatial bounds are not compatible with an attached map named 'world'; use removeSpatialMap() to remove incompatible spatial maps before changing the spatial bounds.  (This enforces internal consistency and avoids accidentally stretching a map to new spatial bounds.)

Error on script line 222, character 12:

        pop.setSpatialBounds(c(0.0, 0.0, asFloat(WIDTH) - 1, asFloat(HEIGHT) - 1));
            ^^^^^^^^^^^^^^^^
Error: Unfortunately SLiM terminated before a tree sequence was saved.
See the above for an indication of where things could have gone wrong.
codecov[bot] commented 9 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Comparison is base (04c0e3c) 89.95% compared to head (80f13c2) 89.95%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #148 +/- ## ======================================= Coverage 89.95% 89.95% ======================================= Files 8 8 Lines 3037 3037 ======================================= Hits 2732 2732 Misses 305 305 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.