Seg Fault 11

I have a seg fault on this piece of code and don't know really what from, I am trying to work out how to use/understand valgrind at the moment, but wondered if anyone kind enough/with a little spare time/with a debugger might be able to point the problem out to me... I think there is an issue with the "e" and "Ep" part of the code from line 280, maybe including the if loop on 284?? If this is confirmed as the issue, are there any suggestions of how to solve it?? Thanks for any help in advance, A.

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <sstream>
#include <gmpxx.h>
using namespace std;
#define PI 3.14159265358979323846

int main()
{

  int a,b,c,d,f,i,j,k,m,n,s,Success,Fails;
  double p,theta,phi,Time,Averagetime,Distance,Length,DotProdForce,
  		 Forcemagnitude,ForceMagnitude[200],Force[200][3],Epsilon[3],
         x[200][3],new_x[200][3],y[200][3],A[200],alpha[200][200],degree,bestalpha[500];
  unsigned long int t;
  
  mpf_t Energy,energy,Power,D,En[500],E[1000000],Ep,e;
  
  mpf_set_default_prec(1024);
  
  mpf_init(Power);
  mpf_init(D);
  mpf_init(Ep);
  mpf_init(e);
  mpf_init(Energy);
  mpf_init(energy);
  
  for(i=0;i<1000000;i++){
   mpf_init(E[i]);
  }
  
  for(i=0;i<500;i++){
    mpf_init(En[i]);
  }
  
  mpf_set_d(e,0.00000000000000000000000000000000000000000000000000000000001);

  clock_t t1,t2;
  t1=clock();

t=1000;

while(t<1001){

n=2;

while(n<3){

  b=1;
  Time=0.0;
  
  while(b<2){

    clock_t t3,t4;
    t3=clock();

    if(n>200){
      cout << n << " is too many points for me :-( \n";
      exit(0);
    }
  
    srand((unsigned)time(0));  
  
    for (i=0;i<n;i++){
      x[i][0]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
    
      Length=sqrt(pow(x[i][0],2)+pow(x[i][1],2)+pow(x[i][2],2));
    
      for (k=0;k<3;k++){
        x[i][k]=x[i][k]/Length;
      }
    }

    for(i=0;i<n;i++){
      for(j=0;j<3;j++){
        cout << "x[" << i << "][" << j << "]=" << x[i][j] << "\n";
      }
    }

    mpf_set(Energy,0);
  
    for(i=0;i<n;i++){
      for(j=i+1;j<n;j++){
        Distance=sqrt(pow(x[i][0]-x[j][0],2)+pow(x[i][1]-x[j][1],2)
		             +pow(x[i][2]-x[j][2],2));
		             
		mpf_set_d(D,Distance);
		       
        mpf_pow_ui(Power,D,t);      
        mpf_ui_div(Power,1.0,Power);
        mpf_add(Energy,Energy,Power);
      
      }
    }
  
    for(i=0;i<n;i++){
      y[i][0]=x[i][0];
      y[i][1]=x[i][1];
      y[i][2]=x[i][2];
    }
  
    m=100;
  
    if (m>100){
      cout << "The m="<< m << " loop is inefficient...lessen m \n";
      exit(0);
    }
  
    a=1;
  
    while(a<m){

      for (i=0;i<n;i++){
        x[i][0]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        
        Length=sqrt(pow(x[i][0],2)+pow(x[i][1],2)+pow(x[i][2],2));
    
        for (k=0;k<3;k++){
          x[i][k]=x[i][k]/Length;
        }
      }

      mpf_set(energy,0);
  
      for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
          Distance=sqrt(pow(x[i][0]-x[j][0],2)+pow(x[i][1]-x[j][1],2)
		                +pow(x[i][2]-x[j][2],2));
        
		mpf_set_d(D,Distance);
		       
        mpf_pow_ui(Power,D,t);      
        mpf_ui_div(Power,1.0,Power);
        mpf_add(energy,energy,Power);
        }
      }
    
      if(energy<Energy)
        for(i=0;i<n;i++){
          for(j=0;j<3;j++){
            mpf_set(Energy,energy);
            y[i][j]=x[i][j];
          }
        }
      else
        for(i=0;i<n;i++){
          for(j=0;j<3;j++){
            mpf_set(energy,Energy);
            x[i][j]=y[i][j];
          }
        }
      
      a=a+1;
    }
    
    mpf_set(E[0],Energy);

    a=1;
    s=0;
    f=0;
    Forcemagnitude=1.0;

    if(n>80 && t<11)
      p=0.005;
    else if(n>60 && t<11)
      p=0.01;
    else if(n>40 && t<11)
      p=0.02;
    else if(n>25 && t<11)
      p=0.05;
    else if(n>40 && t>11)
      p=0.002;
    else if(n>30 && t>11)
      p=0.005;
    else if(n>20 && t>11)
      p=0.01;
    else if(n>5 && t>11)
      p=0.025;
    else
      p=0.05;

    while(Forcemagnitude>0.000001 && a<1000001){
  
      for(i=0;i<n;i++){ 
        Force[i][0]=0.0; 
        Force[i][1]=0.0; 
        Force[i][2]=0.0; 
      } 

      for(i=0;i<n;i++){ 
        for(j=0;j<i;j++){
        
          Distance=sqrt(pow(x[i][0]-x[j][0],2)+pow(x[i][1]-x[j][1],2)
                        +pow(x[i][2]-x[j][2],2));
                        
          Force[i][0]=Force[i][0]+((x[i][0]-x[j][0])/(pow((Distance),3))); 
          Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
          Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3))); 
        } 

        for (j=i+1;j<n;j++){ 

          Distance=sqrt(pow(x[i][0]-x[j][0],2)+pow(x[i][1]-x[j][1],2)
                        +pow(x[i][2]-x[j][2],2));
                        
          Force[i][0]=Force[i][0]+((x[i][0]-x[j][0])/(pow((Distance),3))); 
          Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
          Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3)));
        }
      }

      for(i=0;i<n;i++){

        DotProdForce=Force[i][0]*x[i][0]+Force[i][1]*x[i][1]+Force[i][2]*x[i][2];
      
        y[i][0]=x[i][0];
        y[i][1]=x[i][1];
        y[i][2]=x[i][2];
    
        Force[i][0]=Force[i][0]-DotProdForce*y[i][0];
        Force[i][1]=Force[i][1]-DotProdForce*y[i][1];
        Force[i][2]=Force[i][2]-DotProdForce*y[i][2];

        x[i][0] = y[i][0]+p*(Force[i][0]);
        x[i][1] = y[i][1]+p*(Force[i][1]);
        x[i][2] = y[i][2]+p*(Force[i][2]);
      
        Length=sqrt(pow(x[i][0],2)+pow(x[i][1],2)+pow(x[i][2],2));

        for (j=0;j<3;j++){
          x[i][j]=x[i][j]/Length;
        }
      }
    
      mpf_set(energy,0);
  
      for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
      
          Distance=sqrt(pow(x[i][0]-x[j][0],2)+pow(x[i][1]-x[j][1],2)
                        +pow(x[i][2]-x[j][2],2));
                        
          mpf_set_d(D,Distance);
		       
          mpf_pow_ui(Power,D,t);      
          mpf_ui_div(Power,1.0,Power);
          mpf_add(energy,energy,Power);
        }
      }

      mpf_set(E[a],energy);
      
      for(i=0;i<n;i++){
        ForceMagnitude[i]=pow((pow(Force[i][0],2)+pow(Force[i][1],2)
                              +pow(Force[i][2],2)),0.5);
      }

      if (energy<Energy)
        mpf_set(Energy,energy),s=s+1;
      else
        mpf_set(energy,Energy),f=f+1,p=(9.5*p)/10;

      for(i=0;i<n-1;i++){
        if(ForceMagnitude[i]<ForceMagnitude[i+1])
          ForceMagnitude[i]=ForceMagnitude[i+1];
        else
          ForceMagnitude[i+1]=ForceMagnitude[i];
      }

      mpf_sub(Ep,E[a],E[a-1]);
      mpf_pow_ui(Ep,Ep,2);
      mpf_sqrt(Ep,Ep);
      
      if (Ep<e){
        ForceMagnitude[n]=0.000001;
        cout << "BROKEN \n";
      }
      else
        ForceMagnitude[n]=ForceMagnitude[n];

      Forcemagnitude=ForceMagnitude[n];
 
      a=a+1;
    }

    for(i=0;i<n;i++){
      for(j=i+1;j<n;j++){
        Distance=sqrt(pow(x[i][0]-x[j][0],2)+pow(x[i][1]-x[j][1],2)
                        +pow(x[i][2]-x[j][2],2));
                        
        degree=(180/PI);
                        
        alpha[i][j]=degree*acos((2.0-pow(Distance,2))/2.0);
      }
    }

    for(i=0;i<n-1;i++){
      for(j=i+1;j<n-1;j++){
        if(alpha[i][j]>alpha[i][j+1])
          alpha[i][j]=alpha[i][j+1];
        else
          alpha[i][j+1]=alpha[i][j];
      }
    }
    
    for(i=0;i<n-2;i++){
      if(alpha[i][n]>alpha[i+1][n])
        alpha[i][n]=alpha[i+1][n];
      else
        alpha[i+1][n]=alpha[i][n];
    }

    bestalpha[b]=alpha[n-1][n];
    
    mpf_set(En[b],Energy);
    
    b=b+1;
  
    t4=clock();
    float diff ((float)t4-(float)t3);
    float seconds = diff / CLOCKS_PER_SEC;
  
    Time = Time + seconds;
    
  }

  Averagetime = Time/(b-1);
  
  Success=Success+s;
  Fails=Fails+f;

  t2=clock();
    float diff ((float)t2-(float)t1);
    float seconds = diff / CLOCKS_PER_SEC;
  
  n=n+1;
  }
  
t=t+1;
}
  
  return 0;

}
Topic archived. No new replies allowed.