Error after long run time

hi,
I have written a program where there are a number of sites around a circle and these sites may contain a particle or not. if site i has a particle and site i+1 doesnt then the particle at site i moves to site i+1. If there is also a particle at site i+1 then the particle at site i cannot move. Anyway the program asks you the number of sites desired and the number of times you want to run the program and how long you want to run the program for. It works fine with small numbers like, 100 sites run the program 10 times and for 100 amount of time but i need to run it for 10000 sites, run the program 100 times and for length 100.

Once I run the program for large numbers something goes wrong with the program and all values in the output are the same.

Here's the program and I have provided outputs of the values for the case that it works (small numbers) and for the case it fails to work (large numbers).

Could the problem be that the address of the variables are storing values that are too large?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* BEGINNING OF DECLARING FUNCTIONS */

void initialise(int L,float rho,int n[]);                           /* Sets up our ASEP system */
int dynamics(int TIME,int L,float p,int n[],float J);                      /* Creates a moving system */
void output1(int L,int n[],float rho,int HIST);                     /* Outputs the initial system to a file */
void output2(int L,int n[],int TIME,float p,float J,int HIST);                       /* Outputs the moving system to a file */
void output3(float rho,float J,int L,int TIME,int HIST);
int rnd1(void);                                                     /* Random number generator between 0 and 1 */
int rnd2(int range);                                                /* Random sites generator */
void seedrnd(void);                                                 /* Seeds random numbers */

/* END OF DECLARING FUNCTIONS */

/* BEGINNING OF MAIN PROGRAM */

int main()
{
    float J;
    int HIST;
    int n;
    int L;                                                          /* Number of sites */
    int TIME;                                                       /* Run time */
    float rho;                                                      /* Density */
    float p;                                                        /* Probability */
    seedrnd();
    printf("How many times would you like to run the program?:");
    scanf("%i",&HIST);
    printf("How long would you like to run your system?:");
    scanf("%i",&TIME);
    printf("Enter a value for L (number of sites):");
    scanf("%i",&L);
    printf("Enter a value for p (probability):");
    scanf("%f",&p);
    for(rho=0;rho<21;rho++)
    {
        J=0;
        for(n=0;n<HIST;n++)
        {
            int n[L];                                                    /* Array of size L */
            {
                initialise(L,rho,n);
                output1(L,n,rho,HIST);
            }
            {
                J=dynamics(TIME,L,p,n,J);
                output2(L,n,TIME,p,J,HIST);
            }
        }
        output3(rho,J,L,TIME,HIST);
    }
    return(0);
}

/* END OF MAIN PROGRAM */

/* BEGINNING OF DEFINING FUNCTIONS */

void initialise(int L,float rho,int n[])
{
    int j;
    double result1;
    for (j=0;j<L;j++)
    {
        result1=(double)rnd1()/RAND_MAX;                            /* Pick a random number between 0 and 1 */
        if(result1<(rho/20))                                             /* If the random number is less than rho */
        {
            n[j]=1;                                                 /* then there is a particle on the site */
        }
        else
        {
            n[j]=0;                                                 /* otherwise there is no particle on the site */
        }
    }
}

int dynamics(int TIME,int L,float p,int n[],float J)
{
    int t;
    int i;
    double result2;
    for(t=0;t<L*TIME;t++)
    {
        i=rnd2(L);                                                  /* Picks a random site */
        result2=(double)rnd1()/RAND_MAX;                            /* Pick a random number between 0 and 1 */
        if(((n[i]==1) && (n[(i+1)%L]==0)) && (result2<p))           /* If the site has a particle on it and the next site is vacant
                                                                       and the random number chosen between 0 and 1 is less than p */
        {
            n[i]=0;                                                 /* then set that site equal to 0 */
            n[(i+1)%L]=1;                                           /* and the next site equal to 1 (so basically the particle has moved) */
            J=J+1;
        }
        else
        {
            continue;                                               /* otherwise do nothing and carry on with the loop */
        }
    }
    return(J);
}

void output1(int L,int n[],float rho,int HIST)
{
    int j;
    {
        FILE *fp1;
        fp1=fopen("file1.txt","w");
        if(fp1)
        {
            fprintf(fp1,"rho = ");
            fprintf(fp1,"%f\t",rho);
            fprintf(fp1,"HIST = ");
            fprintf(fp1,"%i\n",HIST);
            fprintf(fp1,"site\t\tnumber of particles\n");
            for (j=0;j<L;j++)
            {
                fprintf(fp1,"%i",j);
                fprintf(fp1,"\t\t");
                fprintf(fp1,"%i\n",n[j]);
            }
        }
        fclose(fp1);
    }
}

void output2(int L,int n[],int TIME,float p,float J,int HIST)
{
    int i;
    {
        FILE *fp2;
        fp2=fopen("file2.txt","w");
        if(fp2)
        {
            fprintf(fp2,"Run time: ");
            fprintf(fp2,"%i\t",TIME);
            fprintf(fp2,"p = ");
            fprintf(fp2,"%f\t",p);
            fprintf(fp2,"site\t\tnumber of particles\n");
            for(i=0;i<L;i++)
            {
                fprintf(fp2,"%i",i);
                fprintf(fp2,"\t\t");
                fprintf(fp2,"%i\n",n[i]);
            }
        }
        fclose(fp2);
    }
}

void output3(float rho,float J,int L,int TIME,int HIST)
{
    FILE *fp3;
    fp3=fopen("file3.txt","a");
    if(fp3)
    {
        fprintf(fp3,"rho\t\t");
        fprintf(fp3,"Current\n");
        fprintf(fp3,"%f\t\t",rho/20);
        fprintf(fp3,"%f\n",J/(L*TIME*HIST));
    }
}

int rnd1(void)
{
    int r;
    r=rand();
    return(r);
}

int rnd2(int range)
{
    int s;
    s=rand()%range;
    return(s);
}

void seedrnd(void)
{
    srand((unsigned)time(NULL));
}

/* END OF DEFINING FUNCTIONS */


working output 3

rho Current
0.000000 0.000000
rho Current
0.050000 0.051630
rho Current
0.100000 0.093710
rho Current
0.150000 0.139940
rho Current
0.200000 0.172080
rho Current
0.250000 0.188470
rho Current
0.300000 0.212430
rho Current
0.350000 0.230410
rho Current
0.400000 0.249250
rho Current
0.450000 0.249250
rho Current
0.500000 0.250120
rho Current
0.550000 0.252260
rho Current
0.600000 0.243300
rho Current
0.650000 0.228850
rho Current
0.700000 0.208210
rho Current
0.750000 0.182670
rho Current
0.800000 0.162320
rho Current
0.850000 0.135470
rho Current
0.900000 0.102670
rho Current
0.950000 0.054890
rho Current
1.000000 0.000000


error output 3

rho Current
0.000000 0.000000
rho Current
0.050000 0.011898
rho Current
0.100000 0.011898
rho Current
0.150000 0.011898
rho Current
0.200000 0.011898
rho Current
0.250000 0.011898
rho Current
0.300000 0.011898
rho Current
0.350000 0.011898
rho Current
0.400000 0.011898
rho Current
0.450000 0.011898
rho Current
0.500000 0.011898
rho Current
0.550000 0.011898
rho Current
0.600000 0.011898
rho Current
0.650000 0.011898
rho Current
0.700000 0.011898
rho Current
0.750000 0.011898
rho Current
0.800000 0.011898
rho Current
0.850000 0.011898
rho Current
0.900000 0.011898
rho Current
0.950000 0.011898
rho Current
1.000000 0.000244


as you can see, the error output gives out values which are the same where it is supposed to look more like the correct version (0.25 at 0.5). Plus it takes ages for me to run the program so i'm hoping someone can spot whats going wrong on the run with large numbers.

Any advice would be helpful
Than you


EDIT: I think I was right, my number was a float and when I ran the debugger I watched this number and it was always maxed out at 167million or something. so I changed it to a double and I started getting higher values which varied this time. I will put the program to run again tonight to see if it's fixed. Also how do I increase the max float number if anyone knows?
Last edited on
Topic archived. No new replies allowed.