Skip to content

Commit

Permalink
mutate and condense now working in spatial model
Browse files Browse the repository at this point in the history
Display of number of species by cell.
  • Loading branch information
highperformancecoder committed Sep 11, 2024
1 parent c32d643 commit 1fc3745
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
20 changes: 20 additions & 0 deletions models/ecolab_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ void EcolabPoint::generate(unsigned niter, const ModelData& model)
density = ROUND(n);
}

unsigned EcolabPoint::nsp() const
{return sum(density!=0);}

array<unsigned> SpatialModel::nsp() const
{
array<unsigned> nsp;
for (auto& i: objects) nsp<<=i->nsp();
return nsp;
}

void EcolabPoint::condense(const array<bool>& mask, size_t mask_true)
{
Expand Down Expand Up @@ -129,6 +138,17 @@ void PanmicticModel::condense()
EcolabPoint::condense(mask, mask_true);
}

void SpatialModel::condense()
{
array<int> total_density(species.size());
for (auto& i: *this) total_density+=i->as<EcoLabCell>()->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<EcoLabCell>()->condense(mask, mask_true);
}



/*
Expand Down
6 changes: 4 additions & 2 deletions models/ecolab_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ struct EcolabPoint
array<int> density;
void generate(unsigned niter, const ModelData&);
void condense(const array<bool>& mask, size_t mask_true);
array<int> mutate(const array<double>&);
array<int> mutate(const array<double>&);
unsigned nsp() const; ///< number of living species in this cell
};

struct PanmicticModel: public ModelData, public EcolabPoint
Expand All @@ -89,10 +90,11 @@ class SpatialModel: public ModelData, public graphcode::Graph<EcoLabCell>
EcoLabCell& cell(size_t x, size_t y) {
return *objects[makeId(x,y)];
}
array<unsigned> nsp() const;
void makeConsistent();
void generate(unsigned niter);
void generate() {generate(1);}
// void condense();
void condense();
void mutate();
// void migrate();
};
Expand Down
3 changes: 2 additions & 1 deletion models/spatial_ecolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 1fc3745

Please sign in to comment.