-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonte_carlo.c
1537 lines (1486 loc) · 64.4 KB
/
monte_carlo.c
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <omp.h>
extern "C"
{
#include <gsl/gsl_math.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
}
#define PI 3.14159265358979323846264338327950288419716939937510
//int numSpheres = 512; // the number of spheres in Santos
int numSpheres = 1000; // the number of spheres
int binNum = 500; // the number of bins
long double hardCore = 0.0556; // hard core diameter
long double hardCoreSquared = 0.0556*0.0556; // the square of the hardcore diameter
long double potentialRange = 0.091; // the range of the potential
long double potentialRangeSquared = 0.091*0.091; // the square of the potential
long double squareWellDepthBoltzmann[35]; // test for hard core sphere interactions
long double squarePatchDepthBoltzmann[35]; // test for hard core sphere interactions
long double potentialRangeMult[35];
long double alpha = 0.05; //
long double jumpsize = 0.05; // max size of the jumps away from original position in a monte carlo step
long numMoved = 0; // number of successful monte carlo steps
int samples = 0;
int testSamples = 0;
int a = 0;
long totalneighbors = 0;
long totalwellneighbors = 0;
long totalpatchneighbors = 0;
long totalneighborsprev = 0;
long totalwellneighborsprev = 0;
long totalpatchneighborsprev = 0;
long double etabar = 0.0;
long double etabarwell = 0.0;
long double etabarpatch = 0.0;
int movenum;
FILE *neighborout;
int testing = 0;
/**
* all the variables necessary to define a bin
* count is the number of spheres inside the bin
* volume is the volume of the bin
* sradius is the inner radius of the spherical shell
* eradius is the outer radius of the spherical shell
* sradius2 is the inner radius of the shell squared
* eradius2 is the outer radius of the spherical shell squared
*/
typedef struct {
long count;
long double volume, sradius, eradius;
long double sradius2, eradius2;
long double sum, sumofsquares;
long double avg, merr;
} Bin;
/**
* all the variables needed to define the position and orrientation of a sphere
* x, y, z are the positions of the sphere and are from 0 to 1 in each coordinate
* a, b, c are the euler angles of the sphere in radians
*/
typedef struct {
long double x,y,z; //position
long double a,b,c; //euler angle of sphere
long mcount;
long wellneighbors, currentwellneighbors;
long patchneighbors, currentpatchneighbors;
double wellneighborssq;
double patchneighborssq;
long ncount;
} Sphere;
/**
* function prototypes
*/
long double productBoltzmann(Sphere *s, Sphere *test, long old, long double delta);
void countNeighbors(Sphere *s, Sphere *test, long old, long double delta);
void Init(Sphere *s, long num);
void InitBins(Bin *b, int num);
void oneMonteCarloStep(Sphere *s);
void radialDistribution(Sphere *s, Bin *bin, long num, long binNum, FILE *radialf);
void printavg(double percent, Bin *b);
void printPositionVTK(Sphere *s, int radnum, double percent);
int patchWork(Sphere *test, Sphere *si);
//void oneRadialDistribution(Sphere *s, long old, long binNum, FILE *rad);
/**
* the main program which initializes the spheres positons and then runs the
* monte carlo symulation
* At the moment it does not take any command line arguments
*/
int main(int argn, char* argv[]){
int i;
clock_t endclock, startclock;
FILE *finish, *radialf, *etaout, *santos, *paramtxt;
//start a clock to figure out how much time the program uses
startclock = clock();
//initialize the files for the final positions and radial distribution
char tmp[100];
memset(tmp, 0, 100);
Sphere *spheres, *testSpheres;
Bin *bins, *testBins;
double percent, dR = 0.0, dPercent = 0.01;
//Santos Values
// potentialRangeMult[0] = 1.05;
// potentialRangeMult[1] = 1.05;
// potentialRangeMult[2] = 1.05;
// potentialRangeMult[3] = 1.10;
// potentialRangeMult[4] = 1.10;
// potentialRangeMult[5] = 1.10;
// potentialRangeMult[6] = 1.20;
// potentialRangeMult[7] = 1.20;
// potentialRangeMult[8] = 1.20;
// potentialRangeMult[9] = 1.30;
// potentialRangeMult[10] = 1.30;
// potentialRangeMult[11] = 1.30;
// potentialRangeMult[12] = 1.40;
// potentialRangeMult[13] = 1.40;
// potentialRangeMult[14] = 1.40;
// potentialRangeMult[15] = 1.50;
// potentialRangeMult[16] = 1.50;
// potentialRangeMult[17] = 1.50;
// potentialRangeMult[18] = 1.60;
// potentialRangeMult[19] = 1.60;
// potentialRangeMult[20] = 1.60;
// potentialRangeMult[21] = 1.70;
// potentialRangeMult[22] = 1.70;
// potentialRangeMult[23] = 1.70;
// potentialRangeMult[24] = 1.80;
// potentialRangeMult[25] = 1.80;
// potentialRangeMult[26] = 1.80;
// potentialRangeMult[27] = 1.90;
// potentialRangeMult[28] = 1.90;
// potentialRangeMult[29] = 2.00;
// potentialRangeMult[30] = 2.00;
//Santos Values
// squareWellDepthBoltzmann[0] = 1.0/0.50;
// squareWellDepthBoltzmann[1] = 1.0/0.70;
// squareWellDepthBoltzmann[2] = 1.0/1.00;
// squareWellDepthBoltzmann[3] = 1.0/0.50;
// squareWellDepthBoltzmann[4] = 1.0/0.70;
// squareWellDepthBoltzmann[5] = 1.0/1.00;
// squareWellDepthBoltzmann[6] = 1.0/0.70;
// squareWellDepthBoltzmann[7] = 1.0/1.00;
// squareWellDepthBoltzmann[8] = 1.0/1.50;
// squareWellDepthBoltzmann[9] = 1.0/1.00;
// squareWellDepthBoltzmann[10] = 1.0/1.50;
// squareWellDepthBoltzmann[11] = 1.0/2.00;
// squareWellDepthBoltzmann[12] = 1.0/1.00;
// squareWellDepthBoltzmann[13] = 1.0/1.50;
// squareWellDepthBoltzmann[14] = 1.0/2.00;
// squareWellDepthBoltzmann[15] = 1.0/1.50;
// squareWellDepthBoltzmann[16] = 1.0/2.00;
// squareWellDepthBoltzmann[17] = 1.0/3.00;
// squareWellDepthBoltzmann[18] = 1.0/1.50;
// squareWellDepthBoltzmann[19] = 1.0/2.00;
// squareWellDepthBoltzmann[20] = 1.0/3.00;
// squareWellDepthBoltzmann[21] = 1.0/2.00;
// squareWellDepthBoltzmann[22] = 1.0/3.00;
// squareWellDepthBoltzmann[23] = 1.0/5.00;
// squareWellDepthBoltzmann[24] = 1.0/2.00;
// squareWellDepthBoltzmann[25] = 1.0/3.00;
// squareWellDepthBoltzmann[26] = 1.0/5.00;
// squareWellDepthBoltzmann[27] = 1.0/3.00;
// squareWellDepthBoltzmann[28] = 1.0/5.00;
// squareWellDepthBoltzmann[29] = 1.0/3.00;
// squareWellDepthBoltzmann[30] = 1.0/5.00;
// squareWellDepthBoltzmann[1] = 2.400;
// squareWellDepthBoltzmann[2] = 2.500;
// squareWellDepthBoltzmann[3] = 2.650;
// squareWellDepthBoltzmann[4] = 2.700;
// squareWellDepthBoltzmann[5] = 2.800;
// squareWellDepthBoltzmann[6] = 3.000;
// squareWellDepthBoltzmann[7] = 3.100;
// squareWellDepthBoltzmann[0] = 3.33097681297;
// squareWellDepthBoltzmann[1] = 2.650;
// squareWellDepthBoltzmann[2] = 1.60555434234;
// squareWellDepthBoltzmann[3] = 1.15542608076;
//Standard values:
potentialRangeMult[0] = 1.05;
potentialRangeMult[1] = 1.15;
potentialRangeMult[2] = 1.25;
squareWellDepthBoltzmann[0] = 2.000;
squareWellDepthBoltzmann[1] = 2.000;
squareWellDepthBoltzmann[2] = 2.000;
squarePatchDepthBoltzmann[0] = 7.950;
double v_patch = 0.0, v_well = 0.0;
printf("------------------------------------------\n");
//for(a = 0; a < 1; a++) {
for(a = 0; a < 3; a++) {
printf("Beginning lambda = %1.2Lf epsilon = %1.3Lf\n", potentialRangeMult[a], squareWellDepthBoltzmann[a]);
printf("------------------------------------------\n");
fflush(stdout);
//open file for neighbor density
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf", potentialRangeMult[a], squareWellDepthBoltzmann[a]);
mkdir(tmp,S_IRWXU);
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/eta", potentialRangeMult[a], squareWellDepthBoltzmann[a]);
mkdir(tmp,S_IRWXU);
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/eta/etal%1.2Lfe%1.3Lf.txt", potentialRangeMult[a], squareWellDepthBoltzmann[a], potentialRangeMult[a], squareWellDepthBoltzmann[a]);
etaout = fopen(tmp,"w");
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/santos.txt", potentialRangeMult[a], squareWellDepthBoltzmann[a]);
santos = fopen(tmp,"w");
if(etaout == NULL){
fprintf(stderr, "could not open file eta.txt\n");
exit(EXIT_FAILURE);
}
if(santos == NULL){
fprintf(stderr, "could not open file santos.txt\n");
exit(EXIT_FAILURE);
}
fprintf(etaout,"%f %f %f %f %f\n", 0.0, 0.0, 0.0, 0.0, 0.0);
fflush(etaout);
//dPercent = 0.01;
//for(percent = 0.15; percent < 0.26; percent+= dPercent){
dPercent = 0.01;
for(percent = 0.01; percent < 0.21; percent+= dPercent){
if(percent >= 0.03) dPercent = 0.02;
if(percent >= 0.05) dPercent = 0.05;
if(percent >= 0.10) dPercent = 0.10;
printf("Percent: %1.2f", percent);
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/vol%1.2fp", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent);
mkdir(tmp,S_IRWXU);
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/vol%1.2fp/final_pos.txt", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent);
finish = fopen(tmp, "w");
if(finish == NULL){
fprintf(stderr, "could not open file final_pos.txt\n");
exit(EXIT_FAILURE);
}
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/vol%1.2fp/params.txt", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent);
paramtxt = fopen(tmp,"w");
if(paramtxt == NULL){
fprintf(stderr, "could not open file params.txt\n");
exit(EXIT_FAILURE);
}
// Santos paper calc...
//hardCore = cbrt(percent/((double)numSpheres));
// Standard volume fraction calc...
hardCore = cbrt(percent/((double)numSpheres*4.0/3.0*PI))*2.0;
hardCoreSquared = pow(hardCore,2);
potentialRange = potentialRangeMult[a]*hardCore;
potentialRangeSquared = pow(potentialRange,2);
dR = fabs((potentialRange-hardCore)/((double)50.0)); // size of spacing of bins
binNum=ceil((0.5-hardCore)/dR);
//Calculate necessary energy for patch/well balance
//v_patch = 4.0/3.0*(30.0*PI/180.0)*(pow(potentialRangeMult[0]*hardCore/2.0,3.0)-pow(hardCore/2.0,3.0));
//v_well = 4.0/3.0*((180.0-30.0)*PI/180.0)*(pow(potentialRangeMult[0]*hardCore/2.0,3.0)-pow(hardCore/2.0,3.0));
//squarePatchDepthBoltzmann[a] = log(exp(squareWellDepthBoltzmann[a])*v_well/v_patch);
//printf("\n%Lf * %f = %f * %f\n",squarePatchDepthBoltzmann[a],v_patch,exp(squareWellDepthBoltzmann[a]),v_well);
fprintf(santos,"---------------------------------------------------------\n");
fprintf(santos,"p g(1+) SEM g(l-) SEM g(l+) SEM \n", hardCore+dR, potentialRange-dR, potentialRange+dR);
fprintf(santos,"p g(%Lf) SEM g(%Lf) SEM g(%Lf) SEM \n", hardCore+dR, potentialRange-dR, potentialRange+dR);
//fprintf(santos,"---------------------------------------------------------\n");
//alpha = cbrt(1.0/((double)numSpheres)/percent);
//alpha = 1.715*hardCore;
//alpha = 1.0;
//alpha = (1.893904-5.391428*percent)*hardCore;
jumpsize = alpha;
printf("\tRadius of spheres: %3.10Lf\n", hardCore/2.0);
memset(tmp, 0, 100);
//verify in bounds...
double vps = 4.0/3.0*PI*pow(hardCore/2.0,3);
printf("Volume per sphere %f by %d spheres = %f\n", vps, numSpheres, vps*numSpheres);
//define and array of spheres to use in the simulation
spheres = (Sphere *)malloc(numSpheres*sizeof(Sphere));
//define an array of bins to use for calculation
bins = (Bin *)malloc(binNum*sizeof(Bin));
//create a seed for the random number generator based on the time
time_t seed = (unsigned)time(NULL);
srand(seed);
//initialize the sphere positions making sure none overlap
Init(spheres, numSpheres);
InitBins(bins, binNum);
//initialize the total neighbors
//totalneighbors = 0;
//totalwellneighbors = 0;
//totalpatchneighbors = 0;
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/vol%1.2fp/neighbors.txt", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent);
neighborout = fopen(tmp,"w");
//run the amount of steps needed for the simulation
int radnum = 0;
int avgmoves = 0;
int mps = 0;
int j;
double testAlpha=0.0;
double prevaccepted = 10.0, prevacceptedact = 0.0;
int testMoves=100;
testing = 1;
for(testAlpha=0.01*hardCore;testAlpha<=1.0;testAlpha+=0.01*hardCore){
//printf("Testing %3.10f jumpsize, %3.10Lf diameter\n", testAlpha, hardCore);
//define an array of spheres to use in the simulation
testSpheres = (Sphere *)malloc(numSpheres*sizeof(Sphere));
//define an array of bins to use for calculation
testBins = (Bin *)malloc(binNum*sizeof(Bin));
//printf("Making test environment\n");
memcpy(testSpheres, spheres, numSpheres*sizeof(Sphere));
memcpy(testBins, bins, binNum*sizeof(Bin));
//if(testSpheres[1].x == spheres[1].x) printf("spheres look good\n");
//else printf("nope: test = %3.5f, actual = %3.5f\n", testSpheres[1].x, spheres[1].x);
//if(testBins[1].volume == bins[1].volume) printf("bins look good\n");
//else printf("nope: test = %3.5f, actual = %3.5f\n", testBins[1].volume, bins[1].volume);
jumpsize = testAlpha;
for(i=0;i<=testMoves;i++){
oneMonteCarloStep(testSpheres);
/*for(j = 0; j < numSpheres; j++) {
mps += testSpheres[j].mcount;
}
mps = mps/numSpheres;
if( mps > avgmoves ) {
//if(testSamples % 10 == 0 && testSamples != 0){
// printf("%d\n", i);
// fflush(stdout);
//} else {
// printf("%d, ", i);
// fflush(stdout);
//}
radialDistribution(testSpheres, testBins, numSpheres, binNum, radialf);
testSamples++;
avgmoves += 3;
}
mps = 0;*/
}
if(fabs(0.5-((double)numMoved)/((double)testMoves)) < prevaccepted){
prevaccepted = fabs(0.5-((double)numMoved)/((double)testMoves));
prevacceptedact = ((double)numMoved)/((double)testMoves);
//printf("found: %3.5f=%ld/%d ",prevacceptedact,numMoved,testMoves);
alpha = testAlpha;
}
free(testSpheres);
free(testBins);
numMoved = 0;
}
testing = 0;
jumpsize = alpha;
printf("best jump size found: %3.5Lf, associated acceptance rate: %1.1f\n", jumpsize, prevacceptedact);
radnum = 0;
mps = 0;
avgmoves = 0;
int numMoves=100000000;
fprintf(paramtxt,"lambda = %Lf \n", potentialRangeMult[a]);
fprintf(paramtxt,"epsilon = %Lf \n", squareWellDepthBoltzmann[a]);
fprintf(paramtxt,"number of moves = %d \n", numMoves);
fprintf(paramtxt,"num spheres = %d \n", numSpheres);
fprintf(paramtxt,"radius of spheres = %3.10Lf \n", hardCore/2.0);
fprintf(paramtxt,"range of potential = %3.10Lf \n", potentialRange);
fprintf(paramtxt,"volume per sphere = %f \n", vps);
fprintf(paramtxt,"volume fraction = %f \n", percent);
fprintf(paramtxt,"vps * num spheres = %f \n", vps*numSpheres);
fprintf(paramtxt,"dR of shells = %f \n", dR);
fprintf(paramtxt,"number of bins = %d \n", binNum);
fprintf(paramtxt,"jumpsize = %Lf \n", jumpsize);
fprintf(paramtxt,"seed for rand = %d", (int)seed);
fflush(paramtxt);
fclose(paramtxt);
int lastsamplemovenum = 0;
double dmoves = 0.0;
//int numMoves=100000;
for(movenum=0;movenum<=numMoves;movenum++){
oneMonteCarloStep(spheres);
for(j = 0; j < numSpheres; j++) {
mps += spheres[j].mcount;
}
mps = mps/numSpheres;
if( mps > avgmoves ) {
if(samples % 10 == 0 && samples != 0){
printf("%d \t\tdmoves = %10.1f\n", movenum, dmoves/10.0);
fflush(stdout);
dmoves = 0.0;
} else {
printf("%d, ", movenum);
fflush(stdout);
fflush(neighborout);
dmoves += movenum - lastsamplemovenum;
lastsamplemovenum = movenum;
}
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/vol%1.2fp", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent);
mkdir(tmp,S_IRWXU);
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/vol%1.2fp/radial", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent);
mkdir(tmp,S_IRWXU);
memset(tmp, 0, 100);
sprintf(tmp, "l%1.2Lfe%1.3Lf/vol%1.2fp/radial/%d.txt", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent, radnum);
printPositionVTK(spheres, radnum, percent);
//radialf = fopen(tmp,"w");
//if(radialf == NULL){
// fprintf(stderr, "could not open file %d.txt\n", radnum);
// exit(EXIT_FAILURE);
//}
radnum++;
radialDistribution(spheres, bins, numSpheres, binNum, radialf);
samples++;
//fflush(radialf);
//fclose(radialf);
avgmoves += 3;
memset(tmp, 0, 100);
}
mps = 0;
}
//record the final positions
printf("\nNumber moved %ld", numMoved);
printf("\tPercent moved %9.2f", ((double)numMoved)/((double)numMoves));
numMoved = 0;
double etaavg = 0.0;
double etaavgwell = 0.0;
double etaavgpatch = 0.0;
double meanerror = 0.0;
double meanerrorwell = 0.0;
double meanerrorpatch = 0.0;
double asymptote = 0.0;
long sumncount = 0;
for(i=0; i<numSpheres; i++){
fprintf(finish,"%Lf %Lf %Lf %Lf %Lf %Lf\n",spheres[i].x,spheres[i].y,spheres[i].z,spheres[i].a,spheres[i].b,spheres[i].c);
etaavg+=((double)spheres[i].wellneighbors + (double)spheres[i].patchneighbors)/((double)spheres[i].ncount);
etaavgwell+=((double)spheres[i].wellneighbors)/((double)spheres[i].ncount);
etaavgpatch+=((double)spheres[i].wellneighbors)/((double)spheres[i].ncount);
sumncount+=spheres[i].ncount;
}
etaavg=etaavg/((double)numSpheres);
etaavgwell=etaavgwell/((double)numSpheres);
etaavgpatch=etaavgpatch/((double)numSpheres);
for(i=0; i<numSpheres; i++){
meanerror+=pow(((double)spheres[i].wellneighbors + (double)spheres[i].patchneighbors)/((double)spheres[i].ncount)-etaavg,2.0);
meanerrorwell+=pow(((double)spheres[i].wellneighbors)/((double)spheres[i].ncount)-etaavgwell,2.0);
meanerrorpatch+=pow(((double)spheres[i].patchneighbors)/((double)spheres[i].ncount)-etaavgpatch,2.0);
}
meanerror=sqrt(meanerror/((double)numSpheres-1.0))/sqrt((double)numSpheres);
meanerrorwell=sqrt(meanerrorwell/((double)numSpheres-1.0))/sqrt((double)numSpheres);
meanerrorpatch=sqrt(meanerrorpatch/((double)numSpheres-1.0))/sqrt((double)numSpheres);
// meanerror = sqrt((spheres[i].wellneighborssq-pow(etaavg,2.0)*((double)sumncount))/((double)sumncount-1.0))/sqrt((double)sumncount);
asymptote = 4.0*PI*((double)numSpheres)*exp(squareWellDepthBoltzmann[a])/3.0*pow(potentialRangeMult[a]*hardCore,3.0)-
4.0*PI*((double)numSpheres)*exp(squareWellDepthBoltzmann[a])/3.0*pow(hardCore,3.0);
fprintf(etaout,"%f %f %f %f %f %f %f %f %f %f %f\n",percent, etaavg, etaavg-meanerror, etaavg+meanerror,
etaavgwell, etaavgwell-meanerrorwell, etaavgwell+meanerrorwell,
etaavgpatch, etaavgpatch-meanerrorpatch, etaavgpatch+meanerrorpatch, asymptote);
//fprintf(etaout,"%f %f %f\n",percent, etaavg, asymptote);
fflush(etaout);
//find the radial distribution of the spheres
//radialDistribution(spheres, bins, numSpheres, 100, radialf);
printf("\t\tNumber of samples %d\n", samples);
printavg(percent, bins);
fprintf(santos,"%f %Lf %Lf %Lf %Lf %Lf %Lf\n\n",percent, bins[0].avg, bins[0].merr, bins[49].avg, bins[49].merr, bins[50].avg, bins[50].merr);
fflush(santos);
endclock = clock ();
//record the time taken to run the simulation
printf("time taken %9.2f seconds\n",
(float)(endclock-startclock)/(float)CLOCKS_PER_SEC );
free(spheres);
free(bins);
fclose(finish);
printf("------------------------------------------\n");
}
fflush(etaout);
fclose(etaout);
fflush(santos);
fclose(santos);
fflush(neighborout);
fclose(neighborout);
}
return 0;
}
void printavg(double percent, Bin *bins) {
FILE *average;
char tmp2[100];
long double mean=0.0, meanp=0.0, meanerror=0.0;
memset(tmp2, 0, 100);
sprintf(tmp2, "l%1.2Lfe%1.3Lf/vol%1.2fp/radial/avg.txt", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent);
average = fopen(tmp2, "w");
if(average == NULL){
fprintf(stderr, "could not open file avg.txt\n");
exit(EXIT_FAILURE);
}
int j;
long double area = 0.0;
for(j = 0; j < binNum; j++ ) {
mean = bins[j].sum/((double)samples);
bins[j].avg = mean;
if(j!=0) area += 4.0*PI*numSpheres*(bins[j].eradius-bins[j].sradius)*(pow(bins[j].eradius,2.0)*mean+pow(bins[j].sradius,2.0)*meanp)/2.0;
//variance = bins[j].sumofsquares/((double)samples-1.0)-((double)samples)/((double)samples-1.0)*pow(mean,2.0);
meanerror = sqrt((bins[j].sumofsquares-pow(bins[j].sum,2.0)/((double)samples))/((double)samples-1.0))/sqrt((double)samples);
bins[j].merr = meanerror;
fprintf(average,"%Lf %Lf %Lf %Lf %Lf\n ", bins[j].sradius/hardCore, mean, mean-meanerror, mean+meanerror, area);
meanp = mean;
}
fflush(average);
fclose(average);
samples = 0;
testSamples = 0;
etabar = ((double)totalneighbors)/((double)numSpheres);
etabarwell = ((double)totalwellneighbors)/((double)numSpheres);
etabarpatch = ((double)totalpatchneighbors)/((double)numSpheres);
printf("after simulation neighbor count: %ld\n",totalneighbors);
printf("after simulation well neighbor count: %ld\n",totalwellneighbors);
printf("after simulation patch neighbor count: %ld\n",totalpatchneighbors);
if(!testing){
fprintf(neighborout,"%d %ld %ld %ld\n ", movenum, totalneighbors, totalwellneighbors, totalpatchneighbors);
}
fflush(neighborout);
}
void printPositionVTK(Sphere *s, int radnum, double percent) {
char tmp2[100];
FILE *vtkpos;
memset(tmp2, 0, 100);
sprintf(tmp2, "l%1.2Lfe%1.3Lf/vol%1.2fp/radial/%d.vtk", potentialRangeMult[a], squareWellDepthBoltzmann[a], percent, radnum);
vtkpos = fopen(tmp2, "w");
fprintf(vtkpos, "# vtk DataFile Version 2.0\n");
fprintf(vtkpos, "3D Proteins\n");
fprintf(vtkpos, "ASCII\n");
fprintf(vtkpos, "DATASET UNSTRUCTURED_GRID \n");
fprintf(vtkpos, "POINTS %d float\n", numSpheres);
int i = 0;
for(i = 0; i < numSpheres; i++){
fprintf(vtkpos, "%Lf %Lf %Lf\n", s[i].x, s[i].y, s[i].z);
}
fprintf(vtkpos, "CELLS %d %d\n", numSpheres, numSpheres*2);
for(i = numSpheres-1; i > -1; i--){
fprintf(vtkpos, "1 %d\n",i);
}
fprintf(vtkpos, "CELL_TYPES %d\n", numSpheres);
for(i = 0; i < numSpheres; i++){
if(i % 10 == 0) {
fprintf(vtkpos, "1\n");
} else {
fprintf(vtkpos, "1 ");
}
}
fprintf(vtkpos, "POINT_DATA %d\n", numSpheres);
fprintf(vtkpos, "SCALARS scalars float 1\n");
fprintf(vtkpos, "LOOKUP_TABLE default\n");
for(i = 1; i <= numSpheres; i++){
if(i % 10 == 0) {
fprintf(vtkpos, "%d\n", i);
} else {
fprintf(vtkpos, "%d ", i);
}
}
fflush(vtkpos);
fclose(vtkpos);
}
void InitBins(Bin *bins, int binNum) {
long double deltaX = 0.0, deltaY = 0.0, deltaZ = 0.0;
long double radius2 = 0.0, dR = 0.0, radius = 0.0;
long double idealDens = 0.0;
long double ratio = 0.0;
long i = 0, n = 0, j = 0, index = 0;
// dR = 0.5/((double)binNum);
// dR = (0.5-hardCore)/((double)binNum); // size of spacing of bins
dR = fabs((potentialRange-hardCore)/((double)50.0)); // size of spacing of bins
binNum=ceil((0.5-hardCore)/dR);
//initialize the bins
//find the volume and starting and stopping radius
for(j=0;j<binNum;j++){
bins[j].count = 0; //make sure the starting count is 0
// bins[j].sradius = j * dR;
// bins[j].eradius = (j+1)*dR;
bins[j].sradius = j * dR + hardCore;
bins[j].eradius = (j + 1) * dR + hardCore;
bins[j].sradius2 = bins[j].sradius
* bins[j].sradius;
bins[j].eradius2 = bins[j].eradius
* bins[j].eradius;
bins[j].volume = 4.0/3.0 * PI
* (pow(bins[j].eradius,3.0)-pow(bins[j].sradius,3.0));
bins[j].sum = 0.0;
bins[j].sumofsquares = 0.0;
bins[j].avg = 0.0;
bins[j].merr = 0.0;
}
}
/**
* Given an array of spheres
* create a random positions for the spheres and make sure that
* the spheres do not overlap
*/
void Init(Sphere *s, long num){
// The x, y, and z distances between this sphere and any other
long double deltaX, deltaY, deltaZ;
// the radial separation between spheres, squared
long double radius2;
// continue placing spheres while this is 1, if it is 0 then
// 2 spheres are inside one another.
int goOn = 1;
long i,n;
// Stamp the beginning of the positioning process, when
// vol. frac. is high, this can take a long time.
printf("Positioning:");
fflush(stdout);
for(i=0;i<num;i++){
// Stamp the 10, 20, 30,.... percent completion times so we know
// how much of the process has been completed.
if(i % (num/10) == 0){
printf("%ld-->",i);
fflush(stdout);
}
//keep on picking positions until no overlap
do{
// generate random positions in the unit cube
s[i].x=(long double)rand()/((long double)RAND_MAX);
s[i].y=(long double)rand()/((long double)RAND_MAX);
s[i].z=(long double)rand()/((long double)RAND_MAX);
// check to make sure no overlap
goOn=1;
// for each of the other spheres,
for(n=0;n<i;n++){
// check the distances
deltaX=fabs(s[i].x-s[n].x);
if(deltaX>.5) deltaX = 1.0-deltaX;
deltaY=fabs(s[i].y-s[n].y);
if(deltaY>.5) deltaY = 1.0-deltaY;
deltaZ=fabs(s[i].z-s[n].z);
if(deltaZ>.5) deltaZ = 1.0-deltaZ;
// combining the squared x, y, z separations
radius2 = deltaX*deltaX
+deltaY*deltaY+deltaZ*deltaZ;
// and testing to see if the distances are
// less than the diameter squared
if(radius2<hardCoreSquared) goOn = 0;
}
}while(goOn==0);
//pick random angles, see the ZXZ' Euler angles
//rotation scheme for details
// a is azimuthal rotation
s[i].a=2.0*PI*(long double)rand()/((long double)RAND_MAX);
// b is polar rotation
s[i].b=acos(1.0-2.0*(((long double)rand())/((long double)RAND_MAX)));
// c is the 2nd azimuthal rotation
s[i].c=2.0*PI*(long double)rand()/((long double)RAND_MAX);
// initializee the various statistic keeping parameters:
// the number of moves.
s[i].mcount = 0;
// the current number of neighbors in the well
s[i].currentwellneighbors = 0;
// the current number of patch neighbors
s[i].currentpatchneighbors = 0;
// the following parameters are used to reduce computation time,
// instead of recounting the number of neighbors at each step,
// they are updated whenever a move is accepted,
// and contribute to the total number of neighbors,
// which is correspondingly updated move-wise,
// instead of being retotalled after every move.
// the per-sphere total number of neighbors that have ever been in the well
s[i].wellneighbors = 0;
// the per-sphere square of the total number of well neighbors
s[i].wellneighborssq = 0.0;
// the per-sphere total number of patch neighbors that have ever been in the patch
s[i].patchneighbors = 0;
// the per-sphere square of the total number of patch neighbors
s[i].patchneighborssq = 0.0;
// the number of times these total sum neighbor numbers have been counted
s[i].ncount = 0;
}
// After all spheres have been placed, initialize the total number of neighbors to 0
totalneighbors = 0;
// same for the total well neighbors
totalwellneighbors = 0;
// same for the total patch neighbors
totalpatchneighbors = 0;
// Stamp completion
printf("Done!\n");
fflush(stdout);
// Count all of the neighbors of the system, Order(1/2n^3) computation
for(i=0;i<num;i++){
// count the neighbors
countNeighbors(s, &s[i], i, potentialRange);
// sum the counts of current and patch neighbors after this sphere
// has had its current neighbor quantity counted
totalneighbors += s[i].currentwellneighbors + s[i].currentpatchneighbors;
// similarly for the total well neighbors
totalwellneighbors += s[i].currentwellneighbors;
// and the total patch neighbors
totalpatchneighbors += s[i].currentpatchneighbors;
// If the total neighbors is ever less than 0, something is wrong...
if(totalneighbors < 0){
// jalapeno is Nick's debugging point...
// I set a breakpoint at jalapeno=0, a useless statement
// to observe the properties that led to such a count...
int jalapeno = 0;
}
}
// calculate the average number of neighbors
etabar = ((double)totalneighbors)/((double)num);
// and the average in the well
etabarwell = ((double)totalwellneighbors)/((double)num);
// and the average in the patch
etabarpatch = ((double)totalpatchneighbors)/((double)num);
// Let the user know the initial count for this parameterization
printf("finished counting neighbors: %ld\n", totalneighbors);
printf("finished counting well neighbors: %ld\n", totalwellneighbors);
printf("finished counting patch neighbors: %ld\n", totalpatchneighbors);
fflush(stdout);
}
/**
* calculate the radial distribution of a group of spheres and
* write the distribution to a file
*
* s is an array of spheres
* num is the number of spheres in the array
* binNum is the number of bins to calculate
* radialf is a file to write the distribution to
*
*/
void radialDistribution(Sphere *s, Bin *bins, long num, long binNum, FILE *radialf){
long double deltaX = 0.0, deltaY = 0.0, deltaZ = 0.0;
long double radius2 = 0.0, dR = 0.0, radius = 0.0;
long double idealDens = 0.0;
long double ratio = 0.0, numExpected = 0.0;
long countLarge = 0; // number of distances between spheres greater than .5
long countSmall = 0;
long i = 0, n = 0, j = 0, index = 0;
idealDens = (long double) num; // the number density
// dR = 0.5/((double)binNum);
// dR = (0.5-hardCore)/((double)binNum); // size of spacing of bins
dR = fabs((potentialRange-hardCore)/((double)50.0)); // size of spacing of bins
//calculate the shortest distance between all spheres
//choose a sphere
for(i = 0; i < num; i++){
//choose another sphere making sure not to choose pairs that the distance
//has already been calculated for
for(n = i + 1; n < num; n++){
radius2 = 0.0;
radius = 0.0;
if(i!=n){
//calculate shortest radius by accounting for periodic boundary
deltaX=fabs(s[i].x-s[n].x);
if(deltaX>0.5) deltaX = 1.0-deltaX;
deltaY=fabs(s[i].y-s[n].y);
if(deltaY>0.5) deltaY = 1.0-deltaY;
deltaZ=fabs(s[i].z-s[n].z);
if(deltaZ>0.5) deltaZ = 1.0-deltaZ;
radius2 = deltaX*deltaX
+deltaY*deltaY+deltaZ*deltaZ;
// Determine correct binning
if(radius2>0.25){
//larger than half of the container size
countLarge++;
}else{
//find the index of the correct bin and add to bin
radius = sqrt(radius2);
// index = (long)floor(radius/dR);
index = (long)floor((radius-hardCore)/dR);
bins[index].count++;
countSmall++;
}
}
}
}
//fprintf(radialf,"#greater than .5 %ld\n", countLarge);
//fprintf(radialf,"#less than .5 %ld\n", countSmall);
long totalCounted = 0;
for(j=0;j<binNum;j++){
// calculate the number of expected centers in bin
numExpected = idealDens*bins[j].volume;
// normalize and calculate ratio of centers in bin to expected centers
ratio = (2.0/((long double)num))*(((long double)bins[j].count)/numExpected);
totalCounted += bins[j].count;
bins[j].count = 0;
//record to file
bins[j].sum += ratio;
bins[j].sumofsquares += pow(ratio,2.0);
//if(radialf != 0)
//fprintf(radialf,"%Lf %Lf\n ", bins[j].sradius/hardCore, ratio);
}
//fprintf(radialf,"#counted in all bins this round %ld", totalCounted);
}
/**
* one Monte Carlo step
* takes an array of sphere positons
*/
void oneMonteCarloStep(Sphere *s){
long pos;
// tempnew is a candidate for a move
Sphere tempnew;
long double oldBoltzmann = 0.0, newBoltzmann = 0.0, random;
int oldneighbors = 0;
int oldwellneighbors = 0;
int oldpatchneighbors = 0;
//pick a random sphere
pos = rand()%numSpheres;
oldBoltzmann = productBoltzmann(s, &s[pos], pos, potentialRange);
oldneighbors = (s[pos].currentwellneighbors + s[pos].currentpatchneighbors);
oldwellneighbors = s[pos].currentwellneighbors;
oldpatchneighbors = s[pos].currentpatchneighbors;
/*
oldneighbors = s[pos].currentwellneighbors + s[pos].currentpatchneighbors;
oldwellneighbors = s[pos].currentwellneighbors;
oldpatchneighbors = s[pos].currentpatchneighbors;
*/
etabar = ((double)totalneighbors)/((double)numSpheres);
etabarwell = ((double)totalwellneighbors)/((double)numSpheres);
etabarpatch = ((double)totalpatchneighbors)/((double)numSpheres);
//if(!testing && abs(totalneighborsprev - totalneighbors) > 20){
/*if(!testing && (movenum % 1000 == 0)){
fprintf(neighborout,"%d %ld %ld %ld\n ", movenum, totalneighbors, totalwellneighbors, totalpatchneighbors);
totalneighborsprev = totalneighbors;
totalwellneighborsprev = totalwellneighbors;
totalpatchneighborsprev = totalpatchneighbors;
}*/
//pick a new position for the sphere
tempnew.x = jumpsize*((long double)rand()/((long double)RAND_MAX)-0.5)+s[pos].x;
tempnew.x = tempnew.x - floor(tempnew.x);
tempnew.y = jumpsize*((long double)rand()/((long double)RAND_MAX)-0.5)+s[pos].y;
tempnew.y = tempnew.y - floor(tempnew.y);
tempnew.z = jumpsize*((long double)rand()/((long double)RAND_MAX)-0.5)+s[pos].z;
tempnew.z = tempnew.z - floor(tempnew.z);
tempnew.a = 2.0*PI*(long double)rand()/((long double)RAND_MAX);
tempnew.b = acos(1.0-2.0*(((long double)rand())/((long double)RAND_MAX)));
tempnew.c = 2.0*PI*(long double)rand()/((long double)RAND_MAX);
tempnew.wellneighbors = 0;
tempnew.currentwellneighbors = 0;
tempnew.wellneighborssq = 0;
//tempnew.wellneighbors = s[pos].wellneighbors;
//tempnew.currentwellneighbors = s[pos].currentwellneighbors;
//tempnew.wellneighborssq = s[pos].wellneighborssq;
tempnew.patchneighbors = 0;
tempnew.currentpatchneighbors = 0;
tempnew.patchneighborssq = 0;
//tempnew.patchneighbors = s[pos].patchneighbors;
//tempnew.currentpatchneighbors = s[pos].currentpatchneighbors;
//tempnew.patchneighborssq = s[pos].patchneighborssq;
tempnew.mcount = s[pos].mcount;
tempnew.ncount = s[pos].ncount;
//calculate the new Boltzmann factor based on the new position
newBoltzmann = productBoltzmann(s, &tempnew, pos, potentialRange);
//calculate a random number between 0 and 1
random = (long double)rand()/((long double)RAND_MAX);
// If the oldBoltzmann factor is 0.0...
if(oldBoltzmann == 0.0){
s[pos].x = tempnew.x;
s[pos].y = tempnew.y;
s[pos].z = tempnew.z;
s[pos].a = tempnew.a;
s[pos].b = tempnew.b;
s[pos].c = tempnew.c;
s[pos].mcount++;
numMoved++;
s[pos].currentwellneighbors = tempnew.currentwellneighbors;
s[pos].wellneighbors += tempnew.wellneighbors;
s[pos].wellneighborssq += tempnew.wellneighborssq;
s[pos].currentpatchneighbors = tempnew.currentpatchneighbors;
s[pos].patchneighbors += tempnew.patchneighbors;
s[pos].patchneighborssq += tempnew.patchneighborssq;
s[pos].ncount = tempnew.ncount;
if(!testing){
totalneighbors += 2*(s[pos].currentwellneighbors + s[pos].currentpatchneighbors - oldneighbors);
totalwellneighbors += 2*(s[pos].currentwellneighbors - oldwellneighbors);
totalpatchneighbors += 2*(s[pos].currentpatchneighbors - oldpatchneighbors);
if(totalneighbors < 0){
exit(EXIT_FAILURE);
}
}
etabar = ((double)totalneighbors)/((double)numSpheres);
etabarwell = ((double)totalwellneighbors)/((double)numSpheres);
etabarpatch = ((double)totalpatchneighbors)/((double)numSpheres);
// if(s[pos].currentwellneighbors - oldneighbors < 0){
// printf("hi there :)");
// }
//if(!testing && abs(totalneighborsprev - totalneighbors) > 20){
if(!testing && (movenum % 1000 == 0)){
fprintf(neighborout,"%d %ld %ld %ld\n ", movenum, totalneighbors, totalwellneighbors, totalpatchneighbors);
totalneighborsprev = totalneighbors;
totalwellneighborsprev = totalwellneighbors;
totalpatchneighborsprev = totalpatchneighbors;
}
return;
}
//see if should accept the move
if(random < (newBoltzmann/oldBoltzmann)){
s[pos].x = tempnew.x;
s[pos].y = tempnew.y;
s[pos].z = tempnew.z;
s[pos].a = tempnew.a;
s[pos].b = tempnew.b;
s[pos].c = tempnew.c;
s[pos].mcount++;
numMoved++;
s[pos].currentwellneighbors = tempnew.currentwellneighbors;
s[pos].wellneighbors += tempnew.wellneighbors;
s[pos].wellneighborssq += tempnew.wellneighborssq;
s[pos].currentpatchneighbors = tempnew.currentpatchneighbors;
s[pos].patchneighbors += tempnew.patchneighbors;
s[pos].patchneighborssq += tempnew.patchneighborssq;
s[pos].ncount = tempnew.ncount;
if(!testing){
totalneighbors += 2*(s[pos].currentwellneighbors + s[pos].currentpatchneighbors - oldneighbors);
totalwellneighbors += 2*(s[pos].currentwellneighbors - oldwellneighbors);
totalpatchneighbors += 2*(s[pos].currentpatchneighbors - oldpatchneighbors);
if(totalneighbors < 0){
exit(EXIT_FAILURE);
}
}
etabar = ((double)totalneighbors)/((double)numSpheres);
etabarwell = ((double)totalwellneighbors)/((double)numSpheres);
etabarpatch = ((double)totalpatchneighbors)/((double)numSpheres);
// if(s[pos].currentwellneighbors - oldneighbors < 0){
// printf("hi there :)");
// }
//if(!testing && abs(totalneighborsprev - totalneighbors) > 20){
if(!testing && (movenum % 1000 == 0)){
fprintf(neighborout,"%d %ld %ld %ld\n ", movenum, totalneighbors, totalwellneighbors, totalpatchneighbors);
totalneighborsprev = totalneighbors;
totalwellneighborsprev = totalwellneighbors;
totalpatchneighborsprev = totalpatchneighbors;
}
}
return;
}
/**
* calculate the boltzmann factor
* s is an array of spheres
* test is the position around which to calculate the boltzmann factor
* old is the index of the sphere to ignore
* delta is the size of potential range
*/
long double productBoltzmann(Sphere *s, Sphere *test, long old, long double delta){
long wellcount=0, patchcount=0;
int edgex=0, edgey=0, edgez=0;
int patchfound=0;
long double lengthSquared;
long double sx_min, sy_min, sz_min;
long double sx_max, sy_max, sz_max;
long double delta_x, delta_y, delta_z;
int i, gonewrong=0;
//make sure that the delta is not bigger than half of the container
if((2*delta)>1.0) exit(EXIT_FAILURE);
//find out what boundaries a box around the test sphere should have
sx_min = test->x - delta;
sy_min = test->y - delta;
sz_min = test->z - delta;
sx_max = test->x + delta;
sy_max = test->y + delta;
sz_max = test->z + delta;
if(sx_min<0){ sx_min+=1.0; edgex =1;}
if(sy_min<0){ sy_min+=1.0; edgey =1;}
if(sz_min<0){ sz_min+=1.0; edgez =1;}
if(sx_max>1){ sx_max-=1.0; edgex =1;}
if(sy_max>1){ sy_max-=1.0; edgey =1;}
if(sz_max>1){ sz_max-=1.0; edgez =1;}
//printf("x %Lf %Lf, y %Lf %Lf, z %Lf %Lf", sx_min, sx_max, sy_min, sy_max, sz_min, sz_max);
//find the spheres inside the box
//#pragma omp parallel for default(shared) \
// shared(s,test,old,delta,edgex,edgey,edgez,sx_min,sy_min,sz_min,sx_max,sy_max,sz_max,gonewrong) private(i,lengthSquared,delta_x,delta_y,delta_z) reduction(+:wellcount)
for(i=0; i<numSpheres; i++){
if((edgex==0 && s[i].x<= sx_max && s[i].x>= sx_min)||(edgex==1
&& (s[i].x<= sx_max || s[i].x>= sx_min))){
//printf("The index of sphere inside x is %d\n", i);
if((edgey==0 && s[i].y<= sy_max && s[i].y>= sy_min)||(edgey==1
&& (s[i].y<= sy_max || s[i].y>= sy_min))){
//printf("The index of sphere inside y is %d\n", i);
if((edgez==0 && s[i].z<= sz_max && s[i].z>= sz_min)||(edgez==1
&& (s[i].z<= sz_max || s[i].z>= sz_min))){
if(i!=old){
//printf("The index of sphere inside this is %d\n", i);
if(s[i].x>=sx_min){
delta_x = s[i].x-(sx_min+delta);
}else{
delta_x = s[i].x-(sx_max-delta);
}
if(s[i].y>=sy_min){
delta_y = s[i].y-(sy_min+delta);