diff --git a/models/ecolab_model.cc b/models/ecolab_model.cc index 1f9144c..6179f58 100644 --- a/models/ecolab_model.cc +++ b/models/ecolab_model.cc @@ -93,6 +93,15 @@ void EcolabPoint::generate(unsigned niter, const ModelData& model) density = ROUND(n); } +unsigned EcolabPoint::nsp() const +{return sum(density!=0);} + +array SpatialModel::nsp() const +{ + array nsp; + for (auto& i: objects) nsp<<=i->nsp(); + return nsp; +} void EcolabPoint::condense(const array& mask, size_t mask_true) { @@ -129,6 +138,17 @@ void PanmicticModel::condense() EcolabPoint::condense(mask, mask_true); } +void SpatialModel::condense() +{ + array total_density(species.size()); + for (auto& i: *this) total_density+=i->as()->density; + auto mask=total_density != 0; + size_t mask_true=sum(mask); + if (mask.size()==mask_true) return; /* no change ! */ + ModelData::condense(mask,mask_true); + for (auto& i: *this) i->as()->condense(mask, mask_true); +} + /* diff --git a/models/ecolab_model.h b/models/ecolab_model.h index 0d23fd0..b39bd01 100644 --- a/models/ecolab_model.h +++ b/models/ecolab_model.h @@ -63,7 +63,8 @@ struct EcolabPoint array density; void generate(unsigned niter, const ModelData&); void condense(const array& mask, size_t mask_true); - array mutate(const array&); + array mutate(const array&); + unsigned nsp() const; ///< number of living species in this cell }; struct PanmicticModel: public ModelData, public EcolabPoint @@ -89,10 +90,11 @@ class SpatialModel: public ModelData, public graphcode::Graph EcoLabCell& cell(size_t x, size_t y) { return *objects[makeId(x,y)]; } + array nsp() const; void makeConsistent(); void generate(unsigned niter); void generate() {generate(1);} -// void condense(); + void condense(); void mutate(); // void migrate(); }; diff --git a/models/spatial_ecolab.py b/models/spatial_ecolab.py index 4f4c3ac..6d5ccc7 100644 --- a/models/spatial_ecolab.py +++ b/models/spatial_ecolab.py @@ -39,10 +39,11 @@ def randomList(num, min, max): def step(): ecolab.generate() ecolab.mutate() - #ecolab.condense() + ecolab.condense() nsp=len(ecolab.species) statusBar.configure(text=f't={ecolab.tstep()} nsp:{nsp}') plot('No. species',ecolab.tstep(),nsp) + plot('No. species by cell',ecolab.tstep(),ecolab.nsp()()) for i in range(numX): for j in range(numY): plot(f'Density({i},{j})',ecolab.tstep(),ecolab.cell(i,j).density(), pens=ecolab.species())