Copy and paste (erase and redraw)

Hello friends,

My model uses discrete time steps, and events in the simulation always follow the same, predictable order:

(beginning of the year)

1. Floaters and newborns compete for open nests

2. Floaters who don’t get nests potentially disperse to different

3. Breeders give birth

4. Breeders mature or die; those that die leave open nests

(count the birds)

(end of the year, beginning of next year)

This makes the most sense; if we think of each “year” as beginning at the beginning of a breeding season, then the year begins with a scrum for nesting sites. Only after all of those sites are filled will the leftover birds look elsewhere for sites. Meanwhile, birds that are settled in sites will lay eggs and those eggs will hatch and the babies will fledge. At the end of the breeding season, not all of those birds will be there; some will die, some will move away. In any event, it means that the year ends with open nests, which the birds of the next time step occupy.

Until last week, my code featured only two habitat patches. It was a poor, rather inflexible block of text. Adding an extra population would have meant manually copying and pasting ugly lines all over the place, and manually searching each bit and maybe changing a number here or a letter there. I didn’t really do any of that, because it was was a lot of work. But for a project like this, any realistic or worthwhile simulation will have many, many habitat patches. I needed a way to automatically generate landscapes with as many patches as needed.

That is what I’ve spent the last three days doing. Basically, I rewrote the whole simulation. The equations are the same and the timing of events is almost the same (I tweaked it a little for the better). But now, instead of having to manually write habitat patches into the code, I simply input an array of carrying capacities (with the contaminated patches first and uncontaminated patches second) and the number of contaminated and uncontaminated patches. All population and allele frequencies are stored in a 3-dimensional (whoah man) array. Each event in a time step is executed by a function, and all of the functions are called sequentially in a main function. This took quite a bit of work (and reading) in R, but because R is great at handling arrays/vectors and matrices, the code comes out a lot cleaner than I think it would have in Python or Matlab.

The main function is so simple.

sim1 = function(){

b <<- pop[,1,] * min(1,phi/apply(pop[,1,],2,sum))

birth(1)

mature(1)

for (time in 2:(len-1)){

# generate birthing population

resettle(time)

# dispersal of floaters

# disperse(time)

# creation of subadults

birth(time)

# creation of adults

mature(time)

}

}

That’s the whole thing. The ~name(time) lines you see are calls to externally defined functions that do some computations that simulate the birth of new offspring, or dispersal of birds, etc. This is so much easier to deal with than the code I had before. I still need to fix how dispersal works (I have in mind several different dispersal schemes that I will try to implement), which means also allowing the input of some sort of distance matrix for patches. That’s my task for the rest of this week. I’m sure you’ll hear about it when it’s done.