// The following constants need to be defined:
// - outfile: path to save the tree sequence of the simulation.
// - popsize: population size.
// - num_gens: run simulation for num_gens ticks.
// - infile: a string with path to tree sequence to start from; optional.
initialize()
{
    initializeSLiMModelType("WF");
    initializeTreeSeq();
    initializeMutationRate(1e-8);
    initializeMutationType("m1", 0.5, "f", -0.01);
    initializeGenomicElementType("g1", m1, 0.1);
    initializeGenomicElement(g1, 0, 1e6-1);
    initializeRecombinationRate(1e-9);
}

1 late() {
    // if no input tree sequence is provided, then start a subpopulation
    if (infile == "") {
        p = sim.addSubpop("p1", popsize);
    } else {
        // reloading must happen in late()
        sim.readFromPopulationFile(infile);
        parent = sim.subpopulations[0];
        p = sim.addSubpopSplit(max(sim.subpopulations.id) + 1, popsize, parent);
        parent.setSubpopulationSize(0);
    }
    p.name = popname;
}

// schedule the end of the simulation
1 late() {
    finaltick = num_gens + community.tick;
    community.rescheduleScriptBlock(s0, ticks=finaltick);
}

// event that saves the tree sequence
s0 1000 late() {
    sim.treeSeqRememberIndividuals(sim.subpopulations.individuals);
    sim.treeSeqOutput(outfile);
}
