0

I am trying to simulate a random walk of 2000 particles, while the one boundary has the ability to make particles bound on that and merely perform a biased step.

There are of course probabilities for binding unbinding etc... Below I have the whole code. However I get segfault error. I put some print statements in the code to see where the issue lies. But nothing. What I found strange though, is that although seed is fixed, the length of the output statement determined the loop, where code crushed.

I am totally inexperienced in these issues, so if you have any idea on what I could do, would be appreciated.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;


const int pi=6;   
const int epsilon=10;


const int X=3000; 
const int Y=30; //length
const int time_steps=100000;
const int N=2000; // number of molecules

int kinesins[N][3]={0};//[X,Y,bound or not]
int grid[X][Y][2]={0};

void place_kinesins(){
    for (int i=0; i<N;i++){
        int x= rand()%X;
        int y= (rand()%(Y-2))+2;
        if (grid[x][y][0]==0){
            kinesins[i][0]=x;
            kinesins[i][1]=y;
            kinesins[i][2]=0;
            grid[x][y][0]=1;
        }else{i--;}
    }   
}
void create_boundaries(){
    for(int i=0;i<Y;i++){
        grid[0][i][1]=-1;
        grid[X-1][i][1]=-3;
    
    }
    for (int i=0; i<X; i++){
        grid[i][Y-1][1]=-2;
        }
}
void create_filament(){ //in order to create binding affinity.
    for(int i=0; i<X;i++){
        grid[i][1][1]=pi;   
    }
}

void step(int kinesin, int x_step, int y_step){
    int x=kinesins[kinesin][0];
    int y=kinesins[kinesin][1];
    int x_end=x+x_step;
    int y_end=y+y_step;
    if (grid[x_end][y_end][0]==0){      
        grid[x][y][0]=0;
        kinesins[kinesin][0]=x_end;
        kinesins[kinesin][1]=y_end;
        grid[x_end][y_end][0]=1;
    }

}                                   

void bound(int kinesin){
    int roll=rand()%10000 ;
    if (roll<epsilon){
        kinesins[kinesin][2]=0;
        step(kinesin,0,1);
    }else{
        if (roll%63==0){                        //controls the binding rate speed
            step(kinesin, 1,0);
        };
    }
}

void unbound(int kinesin){
    cout<<"1";
    int x= kinesins[kinesin][0];
    int y= kinesins[kinesin][1];
    
    int type= grid[x][y][1];
    
    switch(type){
        case 0:{
        cout<<"2";
            int roll=rand()%4;
            
            switch(roll){
                
                case 0:
                    step(kinesin,-1,0);
                    break;
                case 1:
                    step(kinesin,1,0);
                    break;
                case 2:
                    step(kinesin,0,1);
                    break;
                case 3:             
                    step(kinesin,0,-1);
                    break;
            }
            break;
            }
        case -1:
            step(kinesin,1,0);
            break;
        case -2:
            step(kinesin,0,-1);
            break;
        case -3:
            step(kinesin,-1,0);
            break;
        default:
            int roll=rand()%10000;
            if(roll<grid[x][y][1]){kinesins[kinesin][2]=1;}
            else{ if(roll%2==0){step(kinesin,0,1);}}
    }
}



void kinesin_move(int kinesin){
    cout<<" "<<kinesins[kinesin][0]<<kinesins[kinesin][1];
    if (kinesins[kinesin][2]==0){ 
        unbound(kinesin);
    }else{
    cout<<"3";
        bound(kinesin);
    }

}



        
        
void simulation(){
    
    
    
    for(int j=7000; j<time_steps;j++){
    cout<<endl<< j<<"  "<<endl;
        
        for (int kin=0; kin<N; kin++){
            cout<<kin;
            kinesin_move(kin);
            cout<<"E  " ;
            }   
                
    }
    
}




void programm(){
    srand(1);
    create_boundaries();
    create_filament();
    cout<<"Filament done"<<endl;
    place_kinesins();
    cout<<"Kines placed"<<endl;
    simulation();

}


int main(){
    programm();
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • In which function did the segmentation fault occur? – D-RAJ Feb 03 '21 at 14:31
  • 1
    1) Did you try stepping through your code, **line-by-line**, looking at the values of each variable, and comparing them to your expectations, at **each** execution step? 2) What does "_I put some print statements in the code to see where the issue lies. But nothing._" even mean? Maybe you placed your print statements in wrong places? – Algirdas Preidžius Feb 03 '21 at 14:31
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – 273K Feb 03 '21 at 14:32
  • You might be interested in this: [Why rand is a bad idea](https://stackoverflow.com/a/20136256/12170808). – Gary Strivin' Feb 03 '21 at 14:34
  • 1
    usage of global variables makes it difficult to read the code. Also using `std::array` or `std::vector` would help. Thats what I would fix first and chances are high that afterwards the error is either obvious or gone – 463035818_is_not_an_ai Feb 03 '21 at 14:36
  • 1
    @GaryNLOL "_BTW I could not reproduce the segfault_" Segmentation faults are the sign of undefined behavior, somewhere in the code. Due to the nature of undefined behavior: any minute, and unrelated change (running it on different machine, running it again, etc.) can impact the behavior: and "seeming to work fine" is one of possible manifestations of undefined behavior. – Algirdas Preidžius Feb 03 '21 at 14:40
  • I would start with checking the bounds in `step`. – molbdnilo Feb 03 '21 at 14:43

1 Answers1

0

Problem:

In the function step you're accessing the array grid out of its bonds, which produces Undefined Behaviour and the segmentation fault.

This can be proven adding an assert before if (grid[x_end][y_end][0]==0):

   if(!(x_end < X && x_end >= 0))
        std::cerr << x_end << std::endl;
    assert(x_end < X && x_end >= 0);
    if (grid[x_end][y_end][0]==0){
        grid[x][y][0]=0;
        kinesins[kinesin][0]=x_end;
        kinesins[kinesin][1]=y_end;
        grid[x_end][y_end][0]=1;
    }

Output:

3000
Assertion failed: x_end < X && x_end >= 0 main.cpp line 57

This application has requested the Runtime to terminate it in an unusual way.

Solution:

You will have to check the arguments for step won't make it go out of bonds before each call.

Additional information:

  1. using namespace std; is considered a bad practice (More info here).
  2. rand is not uniformly distributed. You may want use the <random> library instead (More info here).
  3. Global variables are bad (More info here).
  4. You may want to use std::array instead of raw, C-style arrays.
Gary Strivin'
  • 908
  • 7
  • 20