calcenergy.cpp 24.3 KB
Newer Older
lvs's avatar
lvs committed
1
2
3
#include "calcenergy.h"

/*
4
5
6
7
8
int prepare_const_fields_for_fpga(Liganddata*     myligand_reference,
                                  Dockpars*       mypars,
                                  float*          cpu_ref_ori_angles,
                                  kernelconstant* KerConst)
//The function fills the constant memory field of the FPGA and are
lvs's avatar
lvs committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//based on the parameters which describe the ligand,
//the docking parameters and the reference orientation angles.
//Short description of the field is as follows:

//atom_charges_const: stores the ligand atom charges.
//		      Element i corresponds to atom with atom ID i in myligand_reference.
//atom_types_const: stores the ligand atom type IDs according to myligand_reference.
//		    Element i corresponds to atom with ID i in myligand_reference.
//intraE_contributors_const: each three contiguous items describe an intramolecular contributor.
//		         The first two elements store the atom ID of the contributors according to myligand_reference.
//		         The third element is 0, if no H-bond can occur between the two atoms, and 1, if it can.
//VWpars_AC_const: stores the A or C van der Waals parameters.
//                 The element i*MAX_NUM_OF_ATYPES+j and j*MAX_NUM_OF_ATYPES+i corresponds to A or C in case of
//		   H-bond for atoms with type ID i and j (according to myligand_reference).
//VWpars_BD_const: stores the B or D van der Waals parameters similar to VWpars_AC_const.
//dspars_S_const: stores the S desolvation parameters.
//		  The element i corresponds to the S parameter of atom with type ID i
//		  according to myligand_reference.
//rotlist_const: stores the data describing the rotations for conformation calculation.
//		 Each element describes one rotation, and the elements are in a proper order,
//               considering that NUM_OF_THREADS_PER_BLOCK rotations will be performed in
//		 parallel (that is, each block of contiguous NUM_OF_THREADS_PER_BLOCK pieces of elements describe rotations that can
//		 be performed simultaneously).
32
//               For FPGA, this has been replaced by const int num_of_threads_per_block within gen_rotlist()
lvs's avatar
lvs committed
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
//		 One element is a 32 bit integer, with bit 0 in the LSB position.
//		 Bit 7-0 describe the atom ID of the atom to be rotated (according to myligand_reference).
//		 Bit 15-7 describe the rotatable bond ID of the bond around which the atom is to be rotated (if this is not a general rotation)
//				 (bond ID is according to myligand_reference).
//		 If bit 16 is 1, this is the first rotation of the atom.
//		 If bit 17 is 1, this is a general rotation (so rotbond ID has to be ignored).
//		 If bit 18 is 1, this is a "dummy" rotation, that is, no rotation can be performed in this cycle
//	         (considering the other rotations which are being carried out in this period).
//ref_coords_x_const: stores the x coordinates of the reference ligand atoms.
//		      Element i corresponds to the x coordinate of
//					  atom with atom ID i (according to myligand_reference).
//ref_coords_y_const: stores the y coordinates of the reference ligand atoms similarly to ref_coords_x_const.
//ref_coords_z_const: stores the z coordinates of the reference ligand atoms similarly to ref_coords_x_const.
//rotbonds_moving_vectors_const: stores the coordinates of rotatable bond moving vectors. Element i, i+1 and i+2 (where i%3=0)
//								 correspond to the moving vector coordinates x, y and z of rotbond ID i, respectively
//								 (according to myligand_reference).
//rotbonds_unit_vectors_const: stores the coordinates of rotatable bond unit vectors similarly to rotbonds_moving_vectors_const.
//ref_orientation_quats_const: stores the quaternions describing the reference orientations for each run. Element i, i+1, i+2
//							   and i+3 (where i%4=0) correspond to the quaternion coordinates q, x, y and z of reference
//							   orientation for run i, respectively.
{
	int i, j;
	int type_id1, type_id2;
	float* floatpoi;
	char* charpoi;
	float phi, theta, genrotangle;

	// ------------------------------
	float atom_charges[MAX_NUM_OF_ATOMS];
	char  atom_types[MAX_NUM_OF_ATOMS];
	char  intraE_contributors[3*MAX_INTRAE_CONTRIBUTORS];
	float VWpars_AC[MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES];
	float VWpars_BD[MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES];
	float dspars_S[MAX_NUM_OF_ATYPES];
	float dspars_V[MAX_NUM_OF_ATYPES];
	int   rotlist[MAX_NUM_OF_ROTATIONS];
	float ref_coords_x[MAX_NUM_OF_ATOMS];
	float ref_coords_y[MAX_NUM_OF_ATOMS];
	float ref_coords_z[MAX_NUM_OF_ATOMS];
	float rotbonds_moving_vectors[3*MAX_NUM_OF_ROTBONDS];
	float rotbonds_unit_vectors[3*MAX_NUM_OF_ROTBONDS];
	//float ref_orientation_quats[4*MAX_NUM_OF_RUNS];
	float ref_orientation_quats[4];
	// ------------------------------

	//charges and type id-s
	floatpoi = atom_charges;
	charpoi = atom_types;

	for (i=0; i < myligand_reference->num_of_atoms; i++)
	{
		*floatpoi = (float) myligand_reference->atom_idxyzq[i][4];
		*charpoi = (char) myligand_reference->atom_idxyzq[i][0];
		floatpoi++;
		charpoi++;
	}

	//intramolecular energy contributors
	myligand_reference->num_of_intraE_contributors = 0;
	for (i=0; i<myligand_reference->num_of_atoms-1; i++)
		for (j=i+1; j<myligand_reference->num_of_atoms; j++)
		{
			if (myligand_reference->intraE_contributors[i][j])
				myligand_reference->num_of_intraE_contributors++;
		}

	if (myligand_reference->num_of_intraE_contributors > MAX_INTRAE_CONTRIBUTORS)
	{
		printf("Error: number of intramolecular energy contributor is too high!\n");
		fflush(stdout);
		return 1;
	}

	charpoi = intraE_contributors;
	for (i=0; i<myligand_reference->num_of_atoms-1; i++)
		for (j=i+1; j<myligand_reference->num_of_atoms; j++)
		{
			if (myligand_reference->intraE_contributors[i][j] == 1)
			{
				*charpoi = (char) i;
				charpoi++;
				*charpoi = (char) j;
				charpoi++;

				type_id1 = (int) myligand_reference->atom_idxyzq [i][0];
				type_id2 = (int) myligand_reference->atom_idxyzq [j][0];
				if (is_H_bond(myligand_reference->atom_types[type_id1], myligand_reference->atom_types[type_id2]) != 0)
					*charpoi = (char) 1;
				else
					*charpoi = (char) 0;
				charpoi++;
			}
		}

	//van der Waals parameters
	for (i=0; i<myligand_reference->num_of_atypes; i++)
		for (j=0; j<myligand_reference->num_of_atypes; j++)
		{
			if (is_H_bond(myligand_reference->atom_types[i], myligand_reference->atom_types[j]) != 0)
			{
				floatpoi = VWpars_AC + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_C[i][j];
				floatpoi = VWpars_AC + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_C[j][i];

				floatpoi = VWpars_BD + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_D[i][j];
				floatpoi = VWpars_BD + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_D[j][i];
			}
			else
			{
				floatpoi = VWpars_AC + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_A[i][j];
				floatpoi = VWpars_AC + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_A[j][i];

				floatpoi = VWpars_BD + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_B[i][j];
				floatpoi = VWpars_BD + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_B[j][i];
			}
		}

	//desolvation parameters
	for (i=0; i<myligand_reference->num_of_atypes; i++)
	{
		dspars_S[i] = myligand_reference->solpar[i];
		dspars_V[i] = myligand_reference->volume[i];
	}

	//generate rotation list
	if (gen_rotlist(myligand_reference, rotlist) != 0)
	{
		printf("Error: number of required rotations is too high!\n");
		return 1;
	}

	//coordinates of reference ligand
	for (i=0; i < myligand_reference->num_of_atoms; i++)
	{
		ref_coords_x[i] = myligand_reference->atom_idxyzq[i][1];
		ref_coords_y[i] = myligand_reference->atom_idxyzq[i][2];
		ref_coords_z[i] = myligand_reference->atom_idxyzq[i][3];
	}

	//rotatable bond vectors
	for (i=0; i < myligand_reference->num_of_rotbonds; i++)
		for (j=0; j<3; j++)
		{
			rotbonds_moving_vectors[3*i+j] = myligand_reference->rotbonds_moving_vectors[i][j];
			rotbonds_unit_vectors[3*i+j] = myligand_reference->rotbonds_unit_vectors[i][j];
		}


	//reference orientation quaternions
//	for (i=0; i<mypars->num_of_runs; i++)
//	{
//		//printf("Pregenerated angles for run %d: %f %f %f\n", i, cpu_ref_ori_angles[3*i], cpu_ref_ori_angles[3*i+1], cpu_ref_ori_angles[3*i+2]);
//		phi = cpu_ref_ori_angles[3*i]*DEG_TO_RAD;
//		theta = cpu_ref_ori_angles[3*i+1]*DEG_TO_RAD;
//		genrotangle = cpu_ref_ori_angles[3*i+2]*DEG_TO_RAD;

//		ref_orientation_quats[4*i] = cosf(genrotangle/2.0f);					//q
//		ref_orientation_quats[4*i+1] = sinf(genrotangle/2.0f)*sinf(theta)*cosf(phi);		//x
//		ref_orientation_quats[4*i+2] = sinf(genrotangle/2.0f)*sinf(theta)*sinf(phi);		//y
//		ref_orientation_quats[4*i+3] = sinf(genrotangle/2.0f)*cosf(theta);			//z
//		//printf("Precalculated quaternion for run %d: %f %f %f %f\n", i, ref_orientation_quats[4*i], ref_orientation_quats[4*i+1], ref_orientation_quats[4*i+2], ref_orientation_quats[4*i+3]);
//	}

	phi         = cpu_ref_ori_angles[0]*DEG_TO_RAD;
	theta       = cpu_ref_ori_angles[1]*DEG_TO_RAD;
	genrotangle = cpu_ref_ori_angles[2]*DEG_TO_RAD;

	ref_orientation_quats[0] = cosf(genrotangle/2.0f);				//q
	ref_orientation_quats[1] = sinf(genrotangle/2.0f)*sinf(theta)*cosf(phi);	//x
	ref_orientation_quats[2] = sinf(genrotangle/2.0f)*sinf(theta)*sinf(phi);	//y
	ref_orientation_quats[3] = sinf(genrotangle/2.0f)*cosf(theta);			//z


	int m;
	for (m=0;m<MAX_NUM_OF_ATOMS;m++){ KerConst->atom_charges_const[m] = atom_charges[m]; }
	for (m=0;m<MAX_NUM_OF_ATOMS;m++){ KerConst->atom_types_const[m]   = atom_types[m]; }
	for (m=0;m<3*MAX_INTRAE_CONTRIBUTORS;m++){ KerConst->intraE_contributors_const[m]   = intraE_contributors[m]; }
	for (m=0;m<MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES;m++){ KerConst->VWpars_AC_const[m]   = VWpars_AC[m]; }
	for (m=0;m<MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES;m++){ KerConst->VWpars_BD_const[m]   = VWpars_BD[m]; }
	for (m=0;m<MAX_NUM_OF_ATYPES;m++)		   { KerConst->dspars_S_const[m]    = dspars_S[m]; }
	for (m=0;m<MAX_NUM_OF_ATYPES;m++)		   { KerConst->dspars_V_const[m]    = dspars_V[m]; }
	for (m=0;m<MAX_NUM_OF_ROTATIONS;m++)		   { KerConst->rotlist_const[m]     = rotlist[m]; }
	for (m=0;m<MAX_NUM_OF_ATOMS;m++)		   { KerConst->ref_coords_x_const[m]= ref_coords_x[m]; }
	for (m=0;m<MAX_NUM_OF_ATOMS;m++)		   { KerConst->ref_coords_y_const[m]= ref_coords_y[m]; }
	for (m=0;m<MAX_NUM_OF_ATOMS;m++)		   { KerConst->ref_coords_z_const[m]= ref_coords_z[m]; }
	for (m=0;m<3*MAX_NUM_OF_ROTBONDS;m++){ KerConst->rotbonds_moving_vectors_const[m]= rotbonds_moving_vectors[m]; }
	for (m=0;m<3*MAX_NUM_OF_ROTBONDS;m++){ KerConst->rotbonds_unit_vectors_const[m]  = rotbonds_unit_vectors[m]; }
	//for (m=0;m<4*MAX_NUM_OF_RUNS;m++)    { KerConst->ref_orientation_quats_const[m]  = ref_orientation_quats[m]; }
	for (m=0;m<4;m++)    { KerConst->ref_orientation_quats_const[m]  = ref_orientation_quats[m]; }

	return 0;
}
*/

234
int prepare_conststatic_fields_for_fpga(Liganddata* 	       myligand_reference,
235
236
				 	Dockpars*   	       mypars,
				 	float*      	       cpu_ref_ori_angles,
lvs's avatar
lvs committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
				 	kernelconstant_static* KerConstStatic)
{
	int i, j;
	int type_id1, type_id2;
	float* floatpoi;
	char* charpoi;
	cl_char3* charpoi3;
	//float phi, theta, genrotangle;

	// arrays to store intermmediate data
	float atom_charges[MAX_NUM_OF_ATOMS];
	char  atom_types[MAX_NUM_OF_ATOMS];
/*
	char  intraE_contributors[3*MAX_INTRAE_CONTRIBUTORS];
*/

	cl_char3  intraE_contributors[MAX_INTRAE_CONTRIBUTORS];

Leonardo Solis's avatar
Leonardo Solis committed
255
256
257
258
259
	float reqm [ATYPE_NUM];
        float reqm_hbond [ATYPE_NUM];
	unsigned int atom1_types_reqm [ATYPE_NUM];
        unsigned int atom2_types_reqm [ATYPE_NUM];

lvs's avatar
lvs committed
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
	float VWpars_AC[MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES];
	float VWpars_BD[MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES];
	float dspars_S[MAX_NUM_OF_ATYPES];
	float dspars_V[MAX_NUM_OF_ATYPES];
	int   rotlist[MAX_NUM_OF_ROTATIONS];

	//charges and type id-s
	floatpoi = atom_charges;
	charpoi = atom_types;

	for (i=0; i < myligand_reference->num_of_atoms; i++)
	{
		*floatpoi = (float) myligand_reference->atom_idxyzq[i][4];
		*charpoi = (char) myligand_reference->atom_idxyzq[i][0];
		floatpoi++;
		charpoi++;
	}

	//intramolecular energy contributors
	myligand_reference->num_of_intraE_contributors = 0;
	for (i=0; i<myligand_reference->num_of_atoms-1; i++)
		for (j=i+1; j<myligand_reference->num_of_atoms; j++)
		{
			if (myligand_reference->intraE_contributors[i][j])
				myligand_reference->num_of_intraE_contributors++;
		}

	if (myligand_reference->num_of_intraE_contributors > MAX_INTRAE_CONTRIBUTORS)
	{
		printf("Error: number of intramolecular energy contributor is too high!\n");
		fflush(stdout);
		return 1;
	}

	charpoi3 = intraE_contributors;
	for (i=0; i<myligand_reference->num_of_atoms-1; i++)
		for (j=i+1; j<myligand_reference->num_of_atoms; j++)
		{
			if (myligand_reference->intraE_contributors[i][j] == 1)
			{
/*
				*charpoi = (char) i;
				charpoi++;
				*charpoi = (char) j;
				charpoi++;

				type_id1 = (int) myligand_reference->atom_idxyzq [i][0];
				type_id2 = (int) myligand_reference->atom_idxyzq [j][0];
				if (is_H_bond(myligand_reference->atom_types[type_id1], myligand_reference->atom_types[type_id2]) != 0)
					*charpoi = (char) 1;
				else
					*charpoi = (char) 0;
				charpoi++;
*/
				(*charpoi3).x = (char) i;
				//charpoi++;
				(*charpoi3).y = (char) j;
				//charpoi++;

				type_id1 = (int) myligand_reference->atom_idxyzq [i][0];
				type_id2 = (int) myligand_reference->atom_idxyzq [j][0];
				if (is_H_bond(myligand_reference->atom_types[type_id1], myligand_reference->atom_types[type_id2]) != 0)
					(*charpoi3).z = (char) 1;
				else
					(*charpoi3).z = (char) 0;
				charpoi3++;
			}
		}

Leonardo Solis's avatar
Leonardo Solis committed
329
330
331
332
333
334
335
336
337
338
339
340
341
        // -------------------------------------------
        // Smoothed pairwise potentials
        // -------------------------------------------
	// reqm, reqm_hbond: equilibrium internuclear separation for vdW and hbond
	for (i= 0; i<ATYPE_NUM/*myligand_reference->num_of_atypes*/; i++) {
		reqm[i]       = myligand_reference->reqm[i];
		reqm_hbond[i] = myligand_reference->reqm_hbond[i];

		atom1_types_reqm [i] = myligand_reference->atom1_types_reqm[i];
        	atom2_types_reqm [i] = myligand_reference->atom2_types_reqm[i];
	}
	// -------------------------------------------

lvs's avatar
lvs committed
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
	//van der Waals parameters
	for (i=0; i<myligand_reference->num_of_atypes; i++)
		for (j=0; j<myligand_reference->num_of_atypes; j++)
		{
			if (is_H_bond(myligand_reference->atom_types[i], myligand_reference->atom_types[j]) != 0)
			{
				floatpoi = VWpars_AC + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_C[i][j];
				floatpoi = VWpars_AC + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_C[j][i];

				floatpoi = VWpars_BD + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_D[i][j];
				floatpoi = VWpars_BD + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_D[j][i];
			}
			else
			{
				floatpoi = VWpars_AC + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_A[i][j];
				floatpoi = VWpars_AC + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_A[j][i];

				floatpoi = VWpars_BD + i*myligand_reference->num_of_atypes + j;
				*floatpoi = (float) myligand_reference->VWpars_B[i][j];
				floatpoi = VWpars_BD + j*myligand_reference->num_of_atypes + i;
				*floatpoi = (float) myligand_reference->VWpars_B[j][i];
			}
		}

	//desolvation parameters
	for (i=0; i<myligand_reference->num_of_atypes; i++)
	{
		dspars_S[i] = myligand_reference->solpar[i];
		dspars_V[i] = myligand_reference->volume[i];
	}

	//generate rotation list
	if (gen_rotlist(myligand_reference, rotlist) != 0)
	{
		printf("Error: number of required rotations is too high!\n");
		return 1;
	}

	int m;
	for (m=0;m<MAX_NUM_OF_ATOMS;m++) {
		KerConstStatic->atom_charges_const[m] = atom_charges[m]; 
	}



	for (m=0;m<MAX_NUM_OF_ATOMS;m++)                   { KerConstStatic->atom_types_const[m]   = atom_types[m];   }
/*
	for (m=0;m<3*MAX_INTRAE_CONTRIBUTORS;m++)          { KerConstStatic->intraE_contributors_const[m]   = intraE_contributors[m]; }
*/
	for (m=0;m<MAX_INTRAE_CONTRIBUTORS;m++)          { KerConstStatic->intraE_contributors_const[m]   = intraE_contributors[m]; }

Leonardo Solis's avatar
Leonardo Solis committed
399
400
401
402
403
404
405
406
	// -------------------------------------------
        // Smoothed pairwise potentials
        // -------------------------------------------
	for (m=0;m<ATYPE_NUM;m++)			   { KerConstStatic->reqm_const[m]             = reqm[m]; }
	for (m=0;m<ATYPE_NUM;m++)			   { KerConstStatic->reqm_hbond_const[m]       = reqm_hbond[m]; }
	for (m=0;m<ATYPE_NUM;m++)			   { KerConstStatic->atom1_types_reqm_const[m] = atom1_types_reqm[m]; }
	for (m=0;m<ATYPE_NUM;m++)                          { KerConstStatic->atom2_types_reqm_const[m] = atom2_types_reqm[m]; }
	// -------------------------------------------
lvs's avatar
lvs committed
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

	for (m=0;m<MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES;m++){ KerConstStatic->VWpars_AC_const[m]    = VWpars_AC[m];    }
	for (m=0;m<MAX_NUM_OF_ATYPES*MAX_NUM_OF_ATYPES;m++){ KerConstStatic->VWpars_BD_const[m]    = VWpars_BD[m];    }
	for (m=0;m<MAX_NUM_OF_ATYPES;m++)		   { KerConstStatic->dspars_S_const[m]     = dspars_S[m];     }
	for (m=0;m<MAX_NUM_OF_ATYPES;m++)		   { KerConstStatic->dspars_V_const[m]     = dspars_V[m];     }
	for (m=0;m<MAX_NUM_OF_ROTATIONS;m++)		   { KerConstStatic->rotlist_const[m]      = rotlist[m];      }

	//coordinates of reference ligand
	for (i=0; i < myligand_reference->num_of_atoms; i++) {
		KerConstStatic->ref_coords_const[i].x = myligand_reference->atom_idxyzq[i][1];
		KerConstStatic->ref_coords_const[i].y = myligand_reference->atom_idxyzq[i][2];
		KerConstStatic->ref_coords_const[i].z = myligand_reference->atom_idxyzq[i][3];
	}


	//rotatable bond vectors
	for (i=0; i < myligand_reference->num_of_rotbonds; i++) {
		KerConstStatic->rotbonds_moving_vectors_const[i].x = myligand_reference->rotbonds_moving_vectors[i][0];
		KerConstStatic->rotbonds_moving_vectors_const[i].y = myligand_reference->rotbonds_moving_vectors[i][1];
		KerConstStatic->rotbonds_moving_vectors_const[i].z = myligand_reference->rotbonds_moving_vectors[i][2];
		KerConstStatic->rotbonds_unit_vectors_const[i].x = myligand_reference->rotbonds_unit_vectors[i][0];
		KerConstStatic->rotbonds_unit_vectors_const[i].y = myligand_reference->rotbonds_unit_vectors[i][1];
		KerConstStatic->rotbonds_unit_vectors_const[i].z = myligand_reference->rotbonds_unit_vectors[i][2];
	}

	float phi, theta, genrotangle;

	//reference orientation quaternions
435
	for (unsigned int i=0; i<mypars->num_of_runs; i++)
lvs's avatar
lvs committed
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
	{
		//printf("Pregenerated angles for run %d: %f %f %f\n", i, cpu_ref_ori_angles[3*i], cpu_ref_ori_angles[3*i+1], cpu_ref_ori_angles[3*i+2]);
		phi = cpu_ref_ori_angles[3*i]*DEG_TO_RAD;
		theta = cpu_ref_ori_angles[3*i+1]*DEG_TO_RAD;
		genrotangle = cpu_ref_ori_angles[3*i+2]*DEG_TO_RAD;

		/*
		KerConstStatic->ref_orientation_quats_const[4*i]   = cosf(genrotangle/2.0f);				//q
		KerConstStatic->ref_orientation_quats_const[4*i+1] = sinf(genrotangle/2.0f)*sinf(theta)*cosf(phi);	//x
		KerConstStatic->ref_orientation_quats_const[4*i+2] = sinf(genrotangle/2.0f)*sinf(theta)*sinf(phi);	//y
		KerConstStatic->ref_orientation_quats_const[4*i+3] = sinf(genrotangle/2.0f)*cosf(theta);		//z
		*/
		KerConstStatic->ref_orientation_quats_const[i].x = cosf(genrotangle/2.0f);				//q
		KerConstStatic->ref_orientation_quats_const[i].y = sinf(genrotangle/2.0f)*sinf(theta)*cosf(phi);	//x
		KerConstStatic->ref_orientation_quats_const[i].z = sinf(genrotangle/2.0f)*sinf(theta)*sinf(phi);	//y
		KerConstStatic->ref_orientation_quats_const[i].w = sinf(genrotangle/2.0f)*cosf(theta);		//z
	}

	return 0;
}

void make_reqrot_ordering(char number_of_req_rotations[MAX_NUM_OF_ATOMS],
			  char atom_id_of_numrots[MAX_NUM_OF_ATOMS],
			  int  num_of_atoms)
//The function puts the first array into a descending order and
//performs the same operations on the second array (since element i of
//number_or_req_rotations and element i of atom_id_of_numrots correspond to each other).
//Element i of the former array stores how many rotations have to be perfomed on the atom
//whose atom ID is stored by element i of the latter one. The third parameter has to be equal
//to the number of ligand atoms
{
	int i, j;
	char temp;

	for (j=0; j<num_of_atoms-1; j++)
		for (i=num_of_atoms-2; i>=j; i--)
			if (number_of_req_rotations[i+1] > number_of_req_rotations[i])
			{
				temp = number_of_req_rotations[i];
				number_of_req_rotations[i] = number_of_req_rotations[i+1];
				number_of_req_rotations[i+1] = temp;

				temp = atom_id_of_numrots[i];
				atom_id_of_numrots[i] = atom_id_of_numrots[i+1];
				atom_id_of_numrots[i+1] = temp;
			}

/*	printf("\n\nRotation priority list after re-ordering:\n");
	for (i=0; i<num_of_atoms; i++)
		printf("Rotation of %d (required rots remaining: %d)\n", atom_id_of_numrots[i], number_of_req_rotations[i]);
	printf("\n\n");*/


}



int gen_rotlist(Liganddata* myligand, int rotlist[MAX_NUM_OF_ROTATIONS])
//The function generates the rotation list which will be stored in the constant memory field rotlist_const by
495
//prepare_const_fields_for_fpga(). The structure of this array is described at that function.
lvs's avatar
lvs committed
496
497
498
499
500
501
502
503
504
505
506
507
508
509
{
	int atom_id, rotb_id, parallel_rot_id, rotlist_id;
	char number_of_req_rotations[MAX_NUM_OF_ATOMS];
	char atom_id_of_numrots[MAX_NUM_OF_ATOMS];
	char atom_wasnt_rotated_yet[MAX_NUM_OF_ATOMS];
	int new_rotlist_element;
	char rotbond_found;
	char rotbond_candidate;
	char remaining_rots_around_rotbonds;


	myligand->num_of_rotcyc = 0;
	myligand->num_of_rotations_required = 0;

510
511
	// On the FPGA: kernels are single threaded
	const int num_of_threads_per_block = 1;
lvs's avatar
lvs committed
512

513
	for (atom_id=0; atom_id<num_of_threads_per_block; atom_id++)	//handling special case when num_of_atoms<num_of_threads_per_block
lvs's avatar
lvs committed
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
		number_of_req_rotations[atom_id] = 0;

	for (atom_id=0; atom_id<myligand->num_of_atoms; atom_id++)
	{
		atom_id_of_numrots[atom_id] = atom_id;
		atom_wasnt_rotated_yet[atom_id] = 1;

		number_of_req_rotations[atom_id] = 1;

		for (rotb_id=0; rotb_id<myligand->num_of_rotbonds; rotb_id++)
			if (myligand->atom_rotbonds[atom_id][rotb_id] != 0)
				(number_of_req_rotations[atom_id])++;

		myligand->num_of_rotations_required += number_of_req_rotations[atom_id];
	}


	rotlist_id=0;
	make_reqrot_ordering(number_of_req_rotations, atom_id_of_numrots, myligand->num_of_atoms);
	while (number_of_req_rotations[0] != 0)	//if the atom with the most remaining rotations has to be rotated 0 times, done
	{
		if (rotlist_id == MAX_NUM_OF_ROTATIONS)
			return 1;

538
539
		//putting the num_of_threads_per_block pieces of most important rotations to the list
		for (parallel_rot_id=0; parallel_rot_id<num_of_threads_per_block; parallel_rot_id++)
lvs's avatar
lvs committed
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
		{
			if (number_of_req_rotations[parallel_rot_id] == 0)	//if the atom has not to be rotated anymore, dummy rotation
				new_rotlist_element = RLIST_DUMMY_MASK;
			else
			{
				atom_id = atom_id_of_numrots[parallel_rot_id];
				new_rotlist_element = ((int) atom_id) & RLIST_ATOMID_MASK;

				if (number_of_req_rotations[parallel_rot_id] == 1)
					new_rotlist_element |= RLIST_GENROT_MASK;
				else
				{
					rotbond_found = 0;
					rotbond_candidate = myligand->num_of_rotbonds - 1;
					remaining_rots_around_rotbonds = number_of_req_rotations[parallel_rot_id] - 1;	//-1 because of genrot

					while (rotbond_found == 0)
					{
						if (myligand->atom_rotbonds[atom_id][rotbond_candidate] != 0)	//if the atom has to be rotated around current candidate
						{
							if (remaining_rots_around_rotbonds == 1)	//if current value of remaining rots is 1, the proper rotbond is found
								rotbond_found = 1;
							else
								remaining_rots_around_rotbonds--;	//if not, decresing remaining rots (that is, skipping rotations which have to be performed later
						}

						if (rotbond_found == 0)
							rotbond_candidate--;

						if (rotbond_candidate < 0)
							return 1;
					}

					new_rotlist_element |= (((int) rotbond_candidate) << RLIST_RBONDID_SHIFT) & RLIST_RBONDID_MASK;
				}

				if (atom_wasnt_rotated_yet[atom_id] != 0)
					new_rotlist_element |= RLIST_FIRSTROT_MASK;

				//put atom_id's next rotation to rotlist
				atom_wasnt_rotated_yet[atom_id] = 0;
				(number_of_req_rotations[parallel_rot_id])--;
			}


			rotlist[rotlist_id] = new_rotlist_element;

			rotlist_id++;
		}

		make_reqrot_ordering(number_of_req_rotations, atom_id_of_numrots, myligand->num_of_atoms);
		(myligand->num_of_rotcyc)++;
	}

	return 0;
}