-
Notifications
You must be signed in to change notification settings - Fork 0
/
multicube.c
1669 lines (1349 loc) · 45.5 KB
/
multicube.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<stdio.h>
#include<stdlib.h>
#include<string.h>
// #include<sys/sysctl.h>
#include<sys/stat.h>
#include<signal.h>
#include<math.h>
#ifdef __APPLE__
#include<OpenCL/OpenCL.h>
#else
#include<CL/opencl.h>
#endif
#ifdef _DDOUBLE
typedef double real;
#else
typedef float real;
#endif
volatile sig_atomic_t killed = 0;
// Structure definitions
typedef struct {
real* x; // charge location
real* y;
real* z;
real* charge; // charge
} nuclear_coordinates;
typedef struct {
real *pottable; // Potential grid points from cube table
int sizex; // size of cube
int sizey;
int sizez;
real dx; // delta between gridpoints
real dy;
real dz;
real origin_x; // origin of grid points in space
real origin_y;
real origin_z;
char* mask; // linear gridpoint exclusion mask
real* contraction_buffer;
nuclear_coordinates coordinates; // atom coordinates
int isGood; // verifier
int num_of_atoms; // number of atoms in cube
} cube_def;
typedef struct {
char discription[4]; // 3 character atom type discriptor
int multiplicity; // How many atoms in our cube are of this type
char* in_atoms; // Atoms of this type in all atoms vector
real rVDW;
int* charges_vector_locations; // Locations in all atoms vector
} atomtype;
typedef struct {
atomtype* atomtypes;
int num_of_atomtypes;
int num_of_atoms;
double sum_of_charge;
real *big_charges_vector;
double *small_charges_vector;
real* big_vdw_vector;
double* plane_normal;
double* gradient;
double* displacementvector;
} atom_database;
typedef struct {
cl_command_queue* cmdq; //OpenCL command queue
cl_kernel* kernel; //OpenCL kernels
cl_kernel* contraction;
cl_context* contexts; //OpenCL contexts
cl_mem* gpu_potential; //OpenCL memory potential from charges
cl_mem* gpu_pottable; //potential read from cubes
cl_mem* gpu_mask; // mask for cubes
cl_mem* gpu_charges; //OpenCL memory for charges
cl_mem* gpu_contraction;
cl_mem* gpu_charges_locx; //OpenCL memory for charges locations
cl_mem* gpu_charges_locy;
cl_mem* gpu_charges_locz;
int num_gpus;
} opencl_stuff;
typedef struct {
char dsc[4];
} atom_discriptor;
void signal_handler(int signum) {
killed = 1;
}
void usage() {
printf("The rock'n roll multicube charges fit program!!\n");
printf(" Full of evil crazyness [WOOT!!] \n");
printf("Argument list:\n");
printf("1. string =/path/to/chargesfile \n");
printf("2. string =/path/to/vdw_database \n");
printf("3. string =/path/to/minimized_charges_output \n");
printf("4 int =Number of GPUs you have... >1000 is cool! \n");
printf("5. char =Norm to use 1: L1 2: L2 3: weighted L1 4: weighted L2\n");
printf("6. real =Initial stepsize ~1 should be fine \n");
printf(" otherwise use thumbs devided by Pi\n");
printf(" f(x+h)-f(x-h)\n");
printf("7. real =h used in numerical gradient : -------------\n");
printf(" 2*h \n");
printf(" should be around sqrtf(machine_epsilon)\n");
printf(" machine_epsilon= for 64bit doubles ~1.11e-16\n");
printf(" for 32bit floats ~5.96e-08\n");
printf("8. real =convergence criterium: \n");
printf(" calculation stops if: \n");
printf(" displacement_vector_length < convergence criterium\n");
printf("9. int =maximum number of steps to evaluate\n");
printf("10. multiple stings = /paths/to/cubes/\n");
}
// Function to read in the OpenCL kernel code;
char* load_program_source(const char *filename){
struct stat statbuf;
FILE *fh;
char *source;
fh = fopen(filename, "r");
if (fh == 0)
return 0;
stat(filename, &statbuf);
source = (char *) malloc(statbuf.st_size + 1);
fread(source, statbuf.st_size, 1, fh);
source[statbuf.st_size] = '\0';
return source;
}
opencl_stuff opencl_initialization(atom_database base, cube_def* cubes,
int num_cubes, int num_gpus, int norm_bool) {
opencl_stuff retval;
int i,j,cubes_by_gpus,cubes_rest;
#ifdef _DDOUBLE
char clSourceFile[8]="dpot.cl";
#else
char clSourceFile[8]="spot.cl";
#endif
char *clSource;
cl_platform_id platform;
cl_context_properties contprop[3];
cl_program* programs;
cl_kernel* k_epot;
cl_kernel* k_contraction;
cl_command_queue* cmdq;
cl_context* contexts;
cl_device_id* devices;
cl_char devName[1024];
size_t devNameSize;
char BuildErrorLog[2048];
size_t BuildErrorLength;
cl_mem* gpu_potential;
cl_mem* gpu_pottable;
cl_mem* gpu_mask;
cl_mem* gpu_charges;
cl_mem* gpu_charges_locx;
cl_mem* gpu_charges_locy;
cl_mem* gpu_charges_locz;
cl_mem* gpu_contracted;
cl_int err;
int cube_by_gpus;
int cube_rest;
int whichcube;
int charges_num = base.num_of_atoms;
// Dynamic Host Memory Allocation
#ifdef _DCLOVIS
contexts = (cl_context*)malloc(sizeof(cl_context)*2);
cmdq = (cl_command_queue*)malloc(sizeof(cl_command_queue)*2);
devices = (cl_device_id*)malloc(sizeof(cl_device_id)*2);
#else
contexts = (cl_context*)malloc(sizeof(cl_context)*num_gpus);
cmdq = (cl_command_queue*)malloc(sizeof(cl_command_queue)*num_gpus);
devices = (cl_device_id*)malloc(sizeof(cl_device_id)*num_gpus);
#endif
programs = (cl_program*)malloc(sizeof(cl_program)*num_gpus);
k_epot = (cl_kernel*)malloc(sizeof(cl_kernel)*num_gpus);
k_contraction = (cl_kernel*)malloc(sizeof(cl_kernel)*num_gpus);
gpu_charges = (cl_mem*)malloc(sizeof(cl_kernel)*num_gpus);
gpu_potential = (cl_mem*)malloc(sizeof(cl_mem)*num_cubes);
gpu_pottable = (cl_mem*)malloc(sizeof(cl_mem)*num_cubes);
gpu_mask = (cl_mem*)malloc(sizeof(cl_mem)*num_cubes);
gpu_contracted = (cl_mem*)malloc(sizeof(cl_mem)*num_cubes);
gpu_charges_locx = (cl_mem*)malloc(sizeof(cl_mem)*num_cubes);
gpu_charges_locy = (cl_mem*)malloc(sizeof(cl_mem)*num_cubes);
gpu_charges_locz = (cl_mem*)malloc(sizeof(cl_mem)*num_cubes);
// OpenCL Initialisation
clGetPlatformIDs(1,&platform,NULL);
#ifdef _DCLOVIS
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 2,devices, NULL);
#else
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, num_gpus,devices, NULL);
#endif
contprop[0] = CL_CONTEXT_PLATFORM;
contprop[1] = (cl_context_properties)platform;
contprop[2] = 0;
for (i = 0;i<num_gpus;i++) {
contexts[i] = clCreateContext(contprop, 1, devices+i, NULL, NULL, &err);
if (err != CL_SUCCESS) {
printf("Context Creation failed! OCL-ERROR-CODE: %i\n", err);
}
}
#ifdef _DCLOVIS
if (num_gpus == 1) {
if (err == CL_INVALID_DEVICE) {
printf("First Device Failed, trying 2nd");
contexts[0] = clCreateContext(contprop, 1, devices+1, NULL, NULL, &err);
cmdq[0] = clCreateCommandQueue(contexts[0], devices[1], 0, NULL);
} else {
cmdq[0] = clCreateCommandQueue(contexts[0], devices[0], 0, NULL);
}
}
if (num_gpus == 2) {
for(i=0;i<num_gpus;i++) {
cmdq[i] = clCreateCommandQueue(contexts[i], devices[i], 0, NULL);
}
}
#else
for(i=0;i<num_gpus;i++) {
cmdq[i] = clCreateCommandQueue(contexts[i], devices[i], 0, NULL);
}
#endif
// Building the OpenCL Program
clSource = load_program_source(clSourceFile);
for(i=0;i<num_gpus;i++) {
programs[i] = clCreateProgramWithSource(contexts[i],1,
(const char**)&clSource,NULL,
&err);
if (err != CL_SUCCESS) {
printf("Program creation failed!\n");
}
err = clBuildProgram(programs[i],0,NULL,NULL,NULL,NULL);
if (err != CL_SUCCESS) {
printf("Failed to compile code into an executeable! \n ");
clGetProgramBuildInfo(programs[i],devices[i],CL_PROGRAM_BUILD_LOG,
sizeof(BuildErrorLog),BuildErrorLog,
&BuildErrorLength);
printf("Build Error Log: \n %s \n", BuildErrorLog);
}
}
for (i=0;i<num_gpus;i++) {
// Kernel Creation
k_epot[i] = clCreateKernel(programs[i],"epot",&err);
switch(norm_bool) {
case 1:
k_contraction[i] = clCreateKernel(programs[i],"contractionL1",&err);
break;
case 2:
k_contraction[i] = clCreateKernel(programs[i],"contractionL2",&err);
break;
case 3:
k_contraction[i] = clCreateKernel(programs[i],"contractionL1w",&err);
break;
case 4:
k_contraction[i] = clCreateKernel(programs[i],"contractionL2w",&err);
break;
}
// GPU Memory Allocation
gpu_charges[i] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(real)*charges_num,
NULL, &err);
err = clSetKernelArg(k_epot[i], 1, sizeof(cl_mem), gpu_charges+i);
err = clSetKernelArg(k_epot[i], 5, sizeof(int), &charges_num);
}
// static memory propagation
cubes_by_gpus=num_cubes/num_gpus;
cubes_rest=num_cubes%num_gpus;
for(i=0;i<num_gpus;i++) {
for(j=0;j<cubes_by_gpus;j++){
whichcube = i*cubes_by_gpus+j;
gpu_pottable[whichcube] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(real)*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
NULL, &err);
gpu_contracted[whichcube] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(real)*cubes[whichcube].sizex
*cubes[whichcube].sizey,
NULL, &err);
gpu_mask[whichcube] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(char)*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
NULL, &err);
gpu_potential[whichcube] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(real)*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
NULL, &err);
gpu_charges_locx[whichcube] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(real)*charges_num,
NULL, &err);
gpu_charges_locy[whichcube] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(real)*charges_num,
NULL, &err);
gpu_charges_locz[whichcube] = clCreateBuffer(contexts[i],
CL_MEM_READ_WRITE,
sizeof(real)*charges_num,
NULL, &err);
err = clEnqueueWriteBuffer(cmdq[i], gpu_pottable[whichcube],
CL_TRUE,0,sizeof(real)
*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
(void*)cubes[whichcube].pottable,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[i], gpu_mask[whichcube],
CL_TRUE,0,sizeof(char)
*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
(void*)cubes[whichcube].mask,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[i], gpu_charges_locx[whichcube],
CL_TRUE,0,sizeof(real)*charges_num,
(void*)cubes[whichcube].coordinates.x,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[i], gpu_charges_locy[whichcube],
CL_TRUE,0,sizeof(real)*charges_num,
(void*)cubes[whichcube].coordinates.y,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[i], gpu_charges_locz[whichcube],
CL_TRUE,0,sizeof(real)*charges_num,
(void*)cubes[whichcube].coordinates.z,
0,NULL,NULL);
}
}
for(j=0;j<cubes_rest;j++){
whichcube = num_gpus*cubes_by_gpus+j;
gpu_pottable[whichcube] = clCreateBuffer(contexts[j],
CL_MEM_READ_WRITE,
sizeof(real)
*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
NULL, &err);
gpu_contracted[whichcube] = clCreateBuffer(contexts[j],
CL_MEM_READ_WRITE,
sizeof(real)
*cubes[whichcube].sizex
*cubes[whichcube].sizey,
NULL, &err);
gpu_mask[whichcube] = clCreateBuffer(contexts[j],
CL_MEM_READ_WRITE,
sizeof(char)
*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
NULL, &err);
gpu_potential[whichcube] = clCreateBuffer(contexts[j],
CL_MEM_READ_WRITE,
sizeof(real)
*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
NULL, &err);
gpu_charges_locx[whichcube] = clCreateBuffer(contexts[j],
CL_MEM_READ_WRITE,
sizeof(real)*charges_num,
NULL, &err);
gpu_charges_locy[whichcube] = clCreateBuffer(contexts[j],
CL_MEM_READ_WRITE,
sizeof(real)*charges_num,
NULL, &err);
gpu_charges_locz[whichcube] = clCreateBuffer(contexts[j],
CL_MEM_READ_WRITE,
sizeof(real)*charges_num,
NULL, &err);
err = clEnqueueWriteBuffer(cmdq[j], gpu_pottable[whichcube],
CL_TRUE,0,sizeof(real)
*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
(void*)cubes[whichcube].pottable,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[j], gpu_mask[whichcube],
CL_TRUE,0,sizeof(char)
*cubes[whichcube].sizex
*cubes[whichcube].sizey
*cubes[whichcube].sizez,
(void*)cubes[whichcube].mask,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[j], gpu_charges_locx[whichcube],
CL_TRUE,0,sizeof(real)*charges_num,
(void*)cubes[whichcube].coordinates.x,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[j], gpu_charges_locy[whichcube],
CL_TRUE,0,sizeof(real)*charges_num,
(void*)cubes[whichcube].coordinates.y,
0,NULL,NULL);
err = clEnqueueWriteBuffer(cmdq[j], gpu_charges_locz[whichcube],
CL_TRUE,0,sizeof(real)*charges_num,
(void*)cubes[whichcube].coordinates.z,
0,NULL,NULL);
}
for(i=0;i<num_gpus;i++) {
clFinish(cmdq[i]);
}
retval.cmdq = cmdq;
retval.kernel = k_epot;
retval.contraction = k_contraction;
retval.contexts = contexts;
retval.gpu_pottable = gpu_pottable;
retval.gpu_mask = gpu_mask;
retval.gpu_potential = gpu_potential;
retval.gpu_contraction = gpu_contracted;
retval.gpu_charges_locx = gpu_charges_locx;
retval.gpu_charges_locy = gpu_charges_locy;
retval.gpu_charges_locz = gpu_charges_locz;
retval.gpu_charges = gpu_charges;
retval.num_gpus = num_gpus;
return retval;
}
// function to update per atomtype charges vector from all atom charges vector
void big_to_small_charges_vector(real* big_charges_vector,
double* small_charges_vector,
atomtype* atomtypes,
int num_of_atomtypes) {
int i,j;
real sum_big;
for (i = 0; i<num_of_atomtypes; i++ ) {
sum_big = 0;
for (j = 0; j < atomtypes[i].multiplicity; j++ ) {
sum_big += big_charges_vector[atomtypes[i].charges_vector_locations[j]];
}
small_charges_vector[i] = sum_big/atomtypes[i].multiplicity;
}
}
// function to update the all atom charges vector from the per atomtype
// charges vector
void small_to_big_charges_vector(real* big_charges_vector,
double* small_charges_vector,
atomtype* atomtypes,
int num_of_atomtypes) {
int i,j;
for (i = 0; i<num_of_atomtypes; i++ ) {
for (j = 0; j < atomtypes[i].multiplicity;j++) {
big_charges_vector[atomtypes[i].charges_vector_locations[j]] =
(real)small_charges_vector[i];
}
}
}
double initialdriftcorrection(real* big_charges_vector,
double sum_of_charge,
int num_of_atoms) {
int i;
double sum=0;
double drift;
for (i=0;i<num_of_atoms;i++) {
sum += (double)big_charges_vector[i];
}
drift = sum-sum_of_charge;
for (i=0;i<num_of_atoms;i++) {
big_charges_vector[i]=big_charges_vector[i]*(sum_of_charge/(real)sum);
}
return(drift);
}
double* generate_plane_normal(atomtype* atomtypes,
int num_of_atomtypes) {
int i;
double* plane_normal = (double*)malloc(num_of_atomtypes*sizeof(double));
double normalization_factor = 0.f;
for (i=0;i<num_of_atomtypes;i++) {
plane_normal[i]=(double)atomtypes[i].multiplicity;
normalization_factor += plane_normal[i]*plane_normal[i];
}
normalization_factor=1./sqrt(normalization_factor);
for (i=0;i<num_of_atomtypes;i++) {
plane_normal[i]=normalization_factor*plane_normal[i];
}
return(plane_normal);
}
// function to generate the internal database about atoms and their properties
// from the VDW radii and charges input file
atom_database generate_inital_database(char* vdwfilestring,
char* chargesfilestring,
int num_of_atoms) {
double sum_of_total_charge = 0.;
int num_of_atomtypes = 0;
int type_evaluation_checksum = 0;
int i,j,k;
atom_database retval;
real temporary_rvdw;
atom_discriptor* raw_atom_discriptions;
atomtype* atomtypes;
char* types_evaluated;
real* chargesvector;
double* small_charges_vector;
real* big_vdw_vector;
char buffer[200];
// opening input files
FILE* chargesfile = fopen(chargesfilestring,"r");
FILE* vdwfile = fopen(vdwfilestring,"r");
// verification whether input files could be opened
if ( NULL == chargesfile ) {
printf("Charges file could not be openend!\n");
retval.num_of_atomtypes = -1;
return retval;
}
if ( NULL == vdwfile ) {
printf("Vdw Radii file could not be openend!\n");
retval.num_of_atomtypes = -1;
return retval;
}
// memory allocation for the
// all atoms charges vector
// a buffer that stores the atom descriptions as the are in defined in the
// charges input file
// a buffer that is used during the generation of unique atomtypes
chargesvector = (real*)malloc(sizeof(real)*num_of_atoms);
raw_atom_discriptions = (atom_discriptor*)
malloc(sizeof(atom_discriptor)*num_of_atoms);
types_evaluated = (char*)malloc(sizeof(char)*num_of_atoms);
bzero(types_evaluated,num_of_atoms);
// read in charges from charges input file into all atoms vector
#ifdef _DDOUBLE
for(i=0;i<num_of_atoms;i++) {
if(2 != fscanf(chargesfile,
"%s %lG",raw_atom_discriptions[i].dsc,chargesvector+i) ) {
printf("Charges file seems to be invalid!");
retval.num_of_atomtypes = -1;
return retval;
}
sum_of_total_charge += (double)chargesvector[i];
}
#else
for(i=0;i<num_of_atoms;i++) {
if(2 != fscanf(chargesfile,
"%s %G",raw_atom_discriptions[i].dsc,chargesvector+i) ) {
printf("Charges file seems to be invalid!");
retval.num_of_atomtypes = -1;
return retval;
}
sum_of_total_charge += (double)chargesvector[i];
}
#endif
printf("Charges have been read! Evaluating atom types. \n");
printf("Sum of Charges = %f \n", sum_of_total_charge);
// searching for unique atomtypes
for(i=0;i<num_of_atoms;i++) {
if (types_evaluated[i] == 0) {
num_of_atomtypes++;
memcpy(buffer,raw_atom_discriptions[i].dsc,4);
for(j=0;j<num_of_atoms;j++) {
if (0 == strncmp(buffer,raw_atom_discriptions[j].dsc,3)) {
types_evaluated[j] = 1;
}
}
}
}
printf("Number of different atom types: %i \n", num_of_atomtypes);
// generating unique atomtypes database
atomtypes = (atomtype*)malloc(sizeof(atomtype)*num_of_atomtypes);
for(i=0;i<num_of_atomtypes;i++){
atomtypes[i].in_atoms = (char*)malloc(sizeof(char)*num_of_atoms);
atomtypes[i].multiplicity = 0;
}
num_of_atomtypes=0;
bzero(types_evaluated,num_of_atoms);
for(i=0;i<num_of_atoms;i++) {
if (types_evaluated[i] == 0) {
memcpy(atomtypes[num_of_atomtypes].discription,
raw_atom_discriptions[i].dsc,4);
for(j=0;j<num_of_atoms;j++) {
if (0 == strncmp(atomtypes[num_of_atomtypes].discription,
raw_atom_discriptions[j].dsc,3)) {
atomtypes[num_of_atomtypes].in_atoms[j] = 1;
atomtypes[num_of_atomtypes].multiplicity++;
types_evaluated[j] = 1;
}
}
num_of_atomtypes++;
}
}
printf("Multiplicities for different atom types have been calulculated! \n");
for(i=0;i<num_of_atomtypes;i++) {
atomtypes[i].charges_vector_locations =
(int*)malloc(sizeof(int)*atomtypes[i].multiplicity);
k=0;
for(j=0;j<num_of_atoms;j++) {
if (1 == atomtypes[i].in_atoms[j]) {
atomtypes[i].charges_vector_locations[k] = j;
k++;
}
}
}
// scanning the vdw database file for the unique atomtypes found;
#ifdef _DDOUBLE
for(i=0;i<num_of_atomtypes;i++) {
do {
if ( 2 != fscanf(vdwfile, "%s %lf",buffer,&temporary_rvdw) ) {
usage();
printf("Failure: Malformatted VDW database! \n");
retval.num_of_atomtypes = -1;
return retval;
}
} while (0 != strncmp(atomtypes[i].discription,buffer,(size_t)4));
atomtypes[i].rVDW = temporary_rvdw*1.889725;
printf("N: %i, rVDW: %f \n",i,atomtypes[i].rVDW);
rewind(vdwfile);
}
#else
for(i=0;i<num_of_atomtypes;i++) {
do {
if ( 2 != fscanf(vdwfile, "%s %f",buffer,&temporary_rvdw) ) {
usage();
printf("Failure: Malformatted VDW database! \n");
retval.num_of_atomtypes = -1;
return retval;
}
} while (0 != strncmp(atomtypes[i].discription,buffer,(size_t)4));
atomtypes[i].rVDW = temporary_rvdw*1.889725;
printf("N: %i, rVDW: %f \n",i,atomtypes[i].rVDW);
rewind(vdwfile);
}
#endif
// allocating and initializing the per atomtype charges vector from the
// all atoms charges vector
small_charges_vector = (double*)malloc(sizeof(double)*num_of_atomtypes);
big_to_small_charges_vector(chargesvector,
small_charges_vector,
atomtypes,
num_of_atomtypes);
// correcting charges drift due to different charges on atoms belonging
// to the same atomtype
printf("Drift correction factor after atom type equalisation: %f \n",
initialdriftcorrection(chargesvector,
sum_of_total_charge,
num_of_atoms)
);
printf("In memory atom types database generated\n");
// Free some temporary buffers from the atomtypes database generation
free(raw_atom_discriptions);
free(types_evaluated);
// generating an all atoms vdw radii vector
big_vdw_vector = (real*)malloc(sizeof(real)*num_of_atoms);
for(i=0;i<num_of_atomtypes;i++){
for(j=0;j<atomtypes[i].multiplicity;j++){
big_vdw_vector[atomtypes[i].charges_vector_locations[j]] =
atomtypes[i].rVDW;
}
}
retval.atomtypes = atomtypes;
retval.num_of_atomtypes = num_of_atomtypes;
retval.num_of_atoms = num_of_atoms;
retval.sum_of_charge = sum_of_total_charge;
retval.big_charges_vector = chargesvector;
retval.small_charges_vector = small_charges_vector;
retval.big_vdw_vector = big_vdw_vector;
retval.plane_normal = (double*)generate_plane_normal(atomtypes,num_of_atomtypes);
retval.gradient = (double*)malloc(sizeof(double)*num_of_atomtypes);
retval.displacementvector = (double*)malloc(sizeof(double)*num_of_atomtypes);
fclose(vdwfile);
fclose(chargesfile);
return retval;
}
// function to obtain the number of atoms in single cube file
int get_num_of_atoms_from_a_cubefile(char* cubefilestring) {
int num_of_atoms,i;
char buffer[200];
FILE* cubefile = fopen(cubefilestring,"r");
if (NULL == cubefile) {
printf("Error opening a cubefile at fopen!\n");
return -1;
}
for(i=0;i<2;i++){
fgets(buffer, 200, cubefile);
}
fgets(buffer,200, cubefile);
sscanf(buffer, "%i", &num_of_atoms);
rewind(cubefile);
fclose(cubefile);
return(num_of_atoms);
}
// function to obtain the parameters of a specific cube file
cube_def getCubeProperties(char* cubefilestring,atom_database base) {
int i;
cube_def retval;
nuclear_coordinates coordinates;
int sizex, sizey, sizez, sizexBYsizey;
real xx, xy, xz, yx, yy, yz, zx, zy, zz;
int num_of_gridpoints;
int index_x, index_y, index_z;
real origin_x, origin_y, origin_z;
int currloc;
real distance_x, distance_y, distance_z; // distances between gridpoints
real nuclear_x, nuclear_y, nuclear_z, nuclear_charge;
real r;
char* mask;
double sum_of_nuclear_charge;
int cube_atomtype;
#ifdef _DDOUBLE
char cube_file_scan_string[] = "%lG";
#else
char cube_file_scan_string[] = "%G";
#endif
int num_of_atoms;
int num_of_atoms_test;
char buffer[200];
real* pottable;
real new, min, max;
// opening the cubefile
FILE* cubefile = fopen(cubefilestring,"r");
num_of_atoms = base.num_of_atoms;
// verfication if the cubefile could be opened
if (NULL == cubefile) {
printf("Error opening cubefile: %s at fopen!\n",cubefilestring);
retval.isGood = 0;
return retval;
}
printf("Processing Cubefile: %s \n",cubefilestring);
// Parsing the cube file header
for(i=0;i<2;i++){
fgets(buffer, 200, cubefile);
}
fgets(buffer,200, cubefile);
#ifdef _DDOUBLE
sscanf(buffer, "%i %lf %lf %lf", &num_of_atoms_test,
&origin_x,
&origin_y,
&origin_z);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i %lf %lf %lf", &sizex, &xx, &xy, &xz);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i %lf %lf %lf", &sizey, &yx, &yy, &yz);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i %lf %lf %lf", &sizez, &zx, &zy, &zz);
#else
sscanf(buffer, "%i %f %f %f", &num_of_atoms_test,
&origin_x,
&origin_y,
&origin_z);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i %f %f %f", &sizex, &xx, &xy, &xz);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i %f %f %f", &sizey, &yx, &yy, &yz);
fgets(buffer, 200, cubefile);
sscanf(buffer, "%i %f %f %f", &sizez, &zx, &zy, &zz);
#endif
sizexBYsizey =sizex*sizey;
// verifing if the cubefiles number of atoms corresponds to the global number
// of atoms
if(num_of_atoms_test != num_of_atoms) {
printf("Cubes are not coherent, different numbers of atoms detected! \n");
retval.isGood = 0;
return retval;
}
// Verifing weather the cube files base is orthogonal
if( 0 != (int)(100*xy) || 0 != (int)(100*xy) ||
0 != (int)(100*yx) || 0 != (int)(100*yz) ||
0 != (int)(100*zx) || 0 != (int)(100*zy) ) {
printf("Non orthogonal coordinate systems are not supported!");
retval.isGood = 0;
return retval;
}
printf("Voxel Dimensions: x=%f y=%f z=%f \n",xx,yy,zz);
// assining memory for and reading the coordinates of atoms and their
// charges found in the cubefile
coordinates.x = (real*)malloc(sizeof(real)*num_of_atoms);
coordinates.y = (real*)malloc(sizeof(real)*num_of_atoms);
coordinates.z = (real*)malloc(sizeof(real)*num_of_atoms);
coordinates.charge = (real*)malloc(sizeof(real)*num_of_atoms);
sum_of_nuclear_charge = 0.f;
for(i=0;i<num_of_atoms;i++){
fgets(buffer, 200, cubefile);
#ifdef _DDOUBLE
sscanf(buffer,"%i %lf %lf %lf %lf", &cube_atomtype, &nuclear_charge,
&nuclear_x, &nuclear_y, &nuclear_z);
#else
sscanf(buffer,"%i %f %f %f %f", &cube_atomtype, &nuclear_charge,
&nuclear_x, &nuclear_y, &nuclear_z);
#endif
coordinates.charge[i] = nuclear_charge;
sum_of_nuclear_charge+=(double)nuclear_charge;
coordinates.x[i] = nuclear_x;
coordinates.y[i] = nuclear_y;
coordinates.z[i] = nuclear_z;
}
printf("Number of Atoms: %i \n Total Nuclar Charge: %lf \n",num_of_atoms,
sum_of_nuclear_charge);
// reading the potential values store in the cube file and
// calculating the cubes voxel exclusion mask from using vdw radii
mask = (char*)malloc(sizex*sizey*sizez*sizeof(char));
pottable = (real*)malloc(sizeof(real)*sizex*sizey*sizez);
num_of_gridpoints=sizex*sizey*sizez;
for(index_x=0;index_x<sizex;index_x++) {
for(index_y=0;index_y<sizey;index_y++) {
for(index_z=0;index_z<sizez;index_z++) {
currloc = index_x+index_y*sizex+index_z*sizexBYsizey;
fscanf(cubefile,cube_file_scan_string,&new);
pottable[currloc] = new;
mask[currloc]=0;
for(i=0;i<num_of_atoms;i++) {
distance_x = origin_x+((real)index_x)*xx-coordinates.x[i];
distance_y = origin_y+((real)index_y)*yy-coordinates.y[i];
distance_z = origin_z+((real)index_z)*zz-coordinates.z[i];
r = base.big_vdw_vector[i];
if ( r*r >
distance_x*distance_x
+distance_y*distance_y
+distance_z*distance_z ) {
mask[currloc]=1;
num_of_gridpoints--;
if ( num_of_gridpoints < num_of_atoms ) {
printf("Failure: Number of Gridpoints is lower then the \n");
printf(" Number of Atoms \n");
printf(" Number of Gridpoints %i \n",num_of_gridpoints);
printf(" Number of Atoms %i \n",num_of_atoms);
retval.isGood = 0;
return retval;
}
}
}
if(new > max) {
max = new;
}