Skip to content

Commit 41bbccf

Browse files
Adjust migration to prevent densities from going negative at high migration rates.
1 parent 27097b4 commit 41bbccf

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

models/ecolab_model.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,27 @@ void SpatialModel::migrate()
666666

667667
array<int> ssum(species.size(),0);
668668
hostForAll([&,this](EcolabCell& c) {
669+
// adjust delta so that density remains +ve
670+
array<int> adjust=delta[c.idx()]+c.density;
671+
adjust*=-(adjust<0);
672+
if (sum(adjust)>0)
673+
{
674+
// distribute adjust among neighbours
675+
array<int> totalDiff(c.density.size(),0);
676+
for (auto& n: c)
677+
{
678+
auto& nbr=*n->as<EcolabCell>();
679+
totalDiff+=nbr.density-c.density;
680+
}
681+
// adjust adjust to be divisible by totalDiff
682+
adjust-=adjust%totalDiff;
683+
for (auto& n: c)
684+
{
685+
auto& nbr=*n->as<EcolabCell>();
686+
delta[nbr.idx()]+=((nbr.density-c.density)/totalDiff)*adjust;
687+
}
688+
delta[c.idx()]-=adjust;
689+
}
669690
c.density+=delta[c.idx()];
670691
#if !defined(NDEBUG)
671692
#pragma omp critical

models/spatial_ecolab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def randomList(num, min, max):
4747
ecolab.interaction.val(randomList(len(ecolab.interaction.val), ecolab.odiag_min(), ecolab.odiag_max()))
4848

4949
ecolab.mutation(nsp*[ecolab.mut_max()])
50-
ecolab.migration(nsp*[1e-4])
50+
ecolab.migration(nsp*[1e-1])
5151

5252
from plot import plot
5353
from GUI import gui, statusBar, windows

0 commit comments

Comments
 (0)