Skip to content

Commit 9fc5e87

Browse files
committed
fix detachments
- PR #340 put the correct check in the wrong place to prevent detachment when a cell is attacking another cell. This fixes that by first moving the check into the spring attachments function as an attack produces a spring attachment. Second, it moves the check into the detachment block rather than the attachment block. - also, fix bug iterating over the attached cells. when detaching cells, the for loop would skip checking the last cell in the list if any cells were detached. So, instead iterate backwards through the list of attached cells.
1 parent cbf625e commit 9fc5e87

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

core/PhysiCell_standard_models.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,8 @@ void dynamic_attachments( Cell* pCell , Phenotype& phenotype, double dt )
14231423
{
14241424
// check for detachments
14251425
double detachment_probability = phenotype.mechanics.detachment_rate * dt;
1426-
for( int j=0; j < pCell->state.attached_cells.size(); j++ )
1426+
// detach_cells swaps the detached cell with the last cell in the vector, so we need to iterate backwards
1427+
for( int j=pCell->state.attached_cells.size()-1; j >= 0; j-- )
14271428
{
14281429
Cell* pTest = pCell->state.attached_cells[j];
14291430
if( UniformRandom() <= detachment_probability )
@@ -1441,8 +1442,6 @@ void dynamic_attachments( Cell* pCell , Phenotype& phenotype, double dt )
14411442
while( done == false && j < pCell->state.neighbors.size() )
14421443
{
14431444
Cell* pTest = pCell->state.neighbors[j];
1444-
if (phenotype.cell_interactions.pAttackTarget==pTest || pTest->phenotype.cell_interactions.pAttackTarget==pCell) // do not let attackers detach randomly
1445-
{ continue; }
14461445
if( pTest->state.number_of_attached_cells() < pTest->phenotype.mechanics.maximum_number_of_attachments )
14471446
{
14481447
// std::string search_string = "adhesive affinity to " + pTest->type_name;
@@ -1467,9 +1466,12 @@ void dynamic_spring_attachments( Cell* pCell , Phenotype& phenotype, double dt )
14671466
{
14681467
// check for detachments
14691468
double detachment_probability = phenotype.mechanics.detachment_rate * dt;
1470-
for( int j=0; j < pCell->state.spring_attachments.size(); j++ )
1469+
// detach_cells_as_spring swaps the detached cell with the last cell in the vector, so we need to iterate backwards
1470+
for( int j=pCell->state.spring_attachments.size()-1; j >= 0; j-- )
14711471
{
1472-
Cell* pTest = pCell->state.spring_attachments[j];
1472+
Cell* pTest = pCell->state.spring_attachments[j];
1473+
if (phenotype.cell_interactions.pAttackTarget==pTest || pTest->phenotype.cell_interactions.pAttackTarget==pCell) // do not let attackers detach randomly
1474+
{ continue; }
14731475
if( UniformRandom() <= detachment_probability )
14741476
{ detach_cells_as_spring( pCell , pTest ); }
14751477
}

0 commit comments

Comments
 (0)