Skip to content

Commit

Permalink
Merge pull request #396 from mstevens-uk/UpdateCellsRefactor
Browse files Browse the repository at this point in the history
Added Cell/Hosts helper functions to Update.cpp
  • Loading branch information
matt-gretton-dann authored Jun 20, 2020
2 parents 20bc2c1 + 072cb14 commit 1cc2e03
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions src/Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ void LatentToInfectious(int cellIndex);
void InfectiousToRecovered(int cellIndex);
void InfectiousToDeath(int cellIndex);

// if the dest cell state == src cell state, use this
void UpdateCell(int* cellPeople, int index, int srcIndex);
void UpdateCell(int* cellPeople, int* srcCellPeople, int index, int srcIndex);

void DoImmune(int ai)
{
// This transfers a person straight from susceptible to immune. Used to start a run with a partially immune population.
Expand All @@ -41,19 +45,19 @@ void DoImmune(int ai)

if (a->listpos < Cells[c].S)
{
Cells[c].susceptible[a->listpos] = Cells[c].susceptible[Cells[c].S];
Hosts[Cells[c].susceptible[a->listpos]].listpos = a->listpos;
UpdateCell(Cells[c].susceptible, a->listpos, Cells[c].S);
}
if (Cells[c].L > 0)
{
Cells[c].susceptible[Cells[c].S] = Cells[c].susceptible[Cells[c].S + Cells[c].L];
Hosts[Cells[c].susceptible[Cells[c].S]].listpos = Cells[c].S;
UpdateCell(Cells[c].susceptible, Cells[c].S, Cells[c].S + Cells[c].L);
}

if (Cells[c].I > 0)
{
Cells[c].susceptible[Cells[c].S + Cells[c].L] = Cells[c].susceptible[Cells[c].S + Cells[c].L + Cells[c].I];
Hosts[Cells[c].susceptible[Cells[c].S + Cells[c].L]].listpos = Cells[c].S + Cells[c].L;
UpdateCell(Cells[c].susceptible, Cells[c].S + Cells[c].L, Cells[c].S + Cells[c].L + Cells[c].I);

}

if (a->listpos < Cells[c].S + Cells[c].L + Cells[c].I)
{
Cells[c].susceptible[Cells[c].S + Cells[c].L + Cells[c].I] = ai;
Expand Down Expand Up @@ -107,8 +111,8 @@ void DoInfect(int ai, double t, int tn, int run) // Change person from susceptib

if (a->listpos < Cells[a->pcell].S)
{
Cells[a->pcell].susceptible[a->listpos] = Cells[a->pcell].susceptible[Cells[a->pcell].S];
Hosts[Cells[a->pcell].susceptible[a->listpos]].listpos = a->listpos;
UpdateCell(Cells[a->pcell].susceptible, a->listpos, Cells[a->pcell].S);

a->listpos = Cells[a->pcell].S; //// person a's position with cell.members now equal to number of susceptibles in cell.
Cells[a->pcell].latent[0] = ai; //// person ai joins front of latent queue.
}
Expand Down Expand Up @@ -540,8 +544,8 @@ void DoIncub(int ai, unsigned short int ts, int tn, int run)

if (Cells[a->pcell].L > 0)
{
Cells[a->pcell].susceptible[a->listpos] = Cells[a->pcell].latent[Cells[a->pcell].L]; //// reset pointers.
Hosts[Cells[a->pcell].susceptible[a->listpos]].listpos = a->listpos;
UpdateCell(Cells[a->pcell].susceptible, Cells[a->pcell].latent, a->listpos, Cells[a->pcell].L);

a->listpos = Cells[a->pcell].S + Cells[a->pcell].L; //// change person a's listpos, which will now refer to their position among infectious people, not latent.
Cells[a->pcell].infected[0] = ai; //// this person is now first infectious person in the array. Pointer was moved back one so now that memory address refers to person ai. Alternative would be to move everyone back one which would take longer.
}
Expand Down Expand Up @@ -935,8 +939,7 @@ void DoRecover(int ai, int tn, int run)
j = Cells[a->pcell].S + Cells[a->pcell].L + Cells[a->pcell].I;
if (i < Cells[a->pcell].S + Cells[a->pcell].L + Cells[a->pcell].I)
{
Cells[a->pcell].susceptible[i] = Cells[a->pcell].susceptible[j];
Hosts[Cells[a->pcell].susceptible[i]].listpos = i;
UpdateCell(Cells[a->pcell].susceptible, i, j);
a->listpos = j;
Cells[a->pcell].susceptible[j] = ai;
}
Expand Down Expand Up @@ -979,8 +982,7 @@ void DoDeath(int ai, int tn, int run)
i = a->listpos;
if (i < Cells[a->pcell].S + Cells[a->pcell].L + Cells[a->pcell].I)
{
Cells[a->pcell].susceptible[a->listpos] = Cells[a->pcell].infected[Cells[a->pcell].I];
Hosts[Cells[a->pcell].susceptible[a->listpos]].listpos = i;
UpdateCell(Cells[a->pcell].susceptible, Cells[a->pcell].infected, a->listpos, Cells[a->pcell].I);
a->listpos = Cells[a->pcell].S + Cells[a->pcell].L + Cells[a->pcell].I;
Cells[a->pcell].susceptible[a->listpos] = ai;
}
Expand Down Expand Up @@ -1431,3 +1433,40 @@ void InfectiousToDeath(int cellIndex)
Cells[cellIndex].D++; //// one more dead person
}

/**
* Function: UpdateCell
*
* Purpose: update Cells and Hosts
* @param cellPeople - pointer to people in cell. e.g. *susceptible identifies where the final susceptible member of cell is.
* @param index - index into cellPeople to update
* @param srcIndex - index into cellPeople to update from
* @return void
*/
void UpdateCell(int* cellPeople,
int index,
int srcIndex)
{
UpdateCell(cellPeople, cellPeople, index, srcIndex);
}

/**
* Function: UpdateCell
*
* Purpose: update Cells and Hosts
* @param cellPeople - pointer to people in cell to update. e.g. *susceptible identifies where the final susceptible member of cell is.
* @param srcCellPeople - pointer to people in cell to update from. e.g. *infected identifies where the final infected member of cell is.
* @param index - index into cellPeople to update
* @param srcIndex - index into srcCellPeople to update from
* @return void
*/
void UpdateCell(int* cellPeople,
int* srcCellPeople,
int index,
int srcIndex)
{
cellPeople[index] = srcCellPeople[srcIndex];

// update the listpos
Hosts[cellPeople[index]].listpos = index;
}

0 comments on commit 1cc2e03

Please sign in to comment.