calcgradient.cl 42.8 KB
Newer Older
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
/*

OCLADock, an OpenCL implementation of AutoDock 4.2 running a Lamarckian Genetic Algorithm
Copyright (C) 2017 TU Darmstadt, Embedded Systems and Applications Group, Germany. All rights reserved.

AutoDock is a Trade Mark of the Scripps Research Institute.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

/*
#include "calcenergy_basic.h"
*/
// All related pragmas are in defines.h (accesible by host and device code)

Leonardo Solis's avatar
Leonardo Solis committed
29
30
31
32
33
34
35
36

// The GPU device function calculates the energy's gradient (forces or derivatives) 
// of the entity described by genotype, dockpars and the ligand-data
// arrays in constant memory and returns it in the "gradient_genotype" parameter. 
// The parameter "run_id" has to be equal to the ID of the run 
// whose population includes the current entity (which can be determined with get_group_id(0)), 
// since this determines which reference orientation should be used.

37
38
39
40
41
42

//#define DEBUG_GRAD_TRANSLATION_GENES
//#define DEBUG_GRAD_ROTATION_GENES
#define DEBUG_GRAD_TORSION_GENES
//#define DEBUG_GRAD

43
44
45
46
47
48
49
50
51
52
53
54
55
void gpu_calc_gradient(	    
				int    dockpars_rotbondlist_length,
				char   dockpars_num_of_atoms,
			    	char   dockpars_gridsize_x,
			    	char   dockpars_gridsize_y,
			    	char   dockpars_gridsize_z,
		 __global const float* restrict dockpars_fgrids, // This is too large to be allocated in __constant 
		            	char   dockpars_num_of_atypes,
		            	int    dockpars_num_of_intraE_contributors,
			    	float  dockpars_grid_spacing,
			    	float  dockpars_coeff_elec,
			    	float  dockpars_qasp,
			    	float  dockpars_coeff_desolv,
Leonardo Solis's avatar
Leonardo Solis committed
56
57
58
59
				// Some OpenCL compilers don't allow declaring 
				// local variables within non-kernel functions.
				// These local variables must be declared in a kernel, 
				// and then passed to non-kernel functions.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		    	__local float* genotype,
		    	__local int*   run_id,

		    	__local float* calc_coords_x,
		    	__local float* calc_coords_y,
		    	__local float* calc_coords_z,

	             __constant float* atom_charges_const,
                     __constant char*  atom_types_const,
                     __constant char*  intraE_contributors_const,
                     __constant float* VWpars_AC_const,
                     __constant float* VWpars_BD_const,
                     __constant float* dspars_S_const,
                     __constant float* dspars_V_const,
                     __constant int*   rotlist_const,
                     __constant float* ref_coords_x_const,
                     __constant float* ref_coords_y_const,
                     __constant float* ref_coords_z_const,
                     __constant float* rotbonds_moving_vectors_const,
                     __constant float* rotbonds_unit_vectors_const,
80
81
82
83
                     __constant float* ref_orientation_quats_const,
		     __constant int*   rotbonds_const,
		     __constant int*   rotbonds_atoms_const,
		     __constant int*   num_rotating_atoms_per_rotbond_const
84
85
86
87
88
89
90
91
92
93
94
95

		    // Gradient-related arguments
		    // Calculate gradients (forces) for intermolecular energy
		    // Derived from autodockdev/maps.py
		    // "is_enabled_gradient_calc": enables gradient calculation.
		    // In Genetic-Generation: no need for gradients
		    // In Gradient-Minimizer: must calculate gradients
			,
			    int    dockpars_num_of_genes,
	    	    __local float* gradient_inter_x,
	            __local float* gradient_inter_y,
	            __local float* gradient_inter_z,
96
97
98
		    __local float* gradient_intra_x,
		    __local float* gradient_intra_y,
		    __local float* gradient_intra_z,
99
100
101
		    __local float* gradient_x,
		    __local float* gradient_y,
		    __local float* gradient_z,
102
	            __local float* gradient_per_intracontributor,
103
104
105
		    __local float* gradient_genotype			
)
{
106
	// Initializing gradients (forces) 
107
108
109
110
	// Derived from autodockdev/maps.py
	for (uint atom_id = get_local_id(0);
		  atom_id < dockpars_num_of_atoms;
		  atom_id+= NUM_OF_THREADS_PER_BLOCK) {
111
		// Intermolecular gradients
112
113
114
		gradient_inter_x[atom_id] = 0.0f;
		gradient_inter_y[atom_id] = 0.0f;
		gradient_inter_z[atom_id] = 0.0f;
115
116
117
118
119
120
		// Intramolecular gradients
		gradient_intra_x[atom_id] = 0.0f;
		gradient_intra_y[atom_id] = 0.0f;
		gradient_intra_z[atom_id] = 0.0f;
	}

Leonardo Solis's avatar
Leonardo Solis committed
121
	// Initializing gradients per intramolecular contributor pairs 
122
123
124
125
	for (uint intracontrib_atompair_id = get_local_id(0);
		  intracontrib_atompair_id < dockpars_num_of_intraE_contributors;
		  intracontrib_atompair_id+= NUM_OF_THREADS_PER_BLOCK) {
		gradient_per_intracontributor[intracontrib_atompair_id] = 0.0f;
126
127
	}

Leonardo Solis's avatar
Leonardo Solis committed
128
129
130
131
132
133
134
135
136
	// Initializing gradient genotypes
	for (uint gene_cnt = get_local_id(0);
		  gene_cnt < dockpars_num_of_genes;
		  gene_cnt+= NUM_OF_THREADS_PER_BLOCK) {
		gradient_genotype[gene_cnt] = 0.0f;
	}

	barrier(CLK_LOCAL_MEM_FENCE);

137
138
139
140
141
142
	uchar g1 = dockpars_gridsize_x;
	uint  g2 = dockpars_gridsize_x * dockpars_gridsize_y;
  	uint  g3 = dockpars_gridsize_x * dockpars_gridsize_y * dockpars_gridsize_z;


	// ================================================
Leonardo Solis's avatar
Leonardo Solis committed
143
	// CALCULATING ATOMIC POSITIONS AFTER ROTATIONS
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
	// ================================================
	for (uint rotation_counter = get_local_id(0);
	          rotation_counter < dockpars_rotbondlist_length;
	          rotation_counter+=NUM_OF_THREADS_PER_BLOCK)
	{
		int rotation_list_element = rotlist_const[rotation_counter];

		if ((rotation_list_element & RLIST_DUMMY_MASK) == 0)	// If not dummy rotation
		{
			uint atom_id = rotation_list_element & RLIST_ATOMID_MASK;

			// Capturing atom coordinates
			float atom_to_rotate[3];

			if ((rotation_list_element & RLIST_FIRSTROT_MASK) != 0)	// If first rotation of this atom
			{
				atom_to_rotate[0] = ref_coords_x_const[atom_id];
				atom_to_rotate[1] = ref_coords_y_const[atom_id];
				atom_to_rotate[2] = ref_coords_z_const[atom_id];
			}
			else
			{
				atom_to_rotate[0] = calc_coords_x[atom_id];
				atom_to_rotate[1] = calc_coords_y[atom_id];
				atom_to_rotate[2] = calc_coords_z[atom_id];
			}

			// Capturing rotation vectors and angle
			float rotation_movingvec[3];

			float quatrot_left_x, quatrot_left_y, quatrot_left_z, quatrot_left_q;
			float quatrot_temp_x, quatrot_temp_y, quatrot_temp_z, quatrot_temp_q;

			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	// If general rotation
			{
Leonardo Solis's avatar
Leonardo Solis committed
179
				// Rotational genes in the Shoemake space are expressed in radians
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
				float u1 = genotype[3];
				float u2 = genotype[4];
				float u3 = genotype[5];

				// u1, u2, u3 should be within their valid range of [0,1]
				quatrot_left_q = native_sqrt(1 - u1) * native_sin(PI_TIMES_2*u2); 
				quatrot_left_x = native_sqrt(1 - u1) * native_cos(PI_TIMES_2*u2);
				quatrot_left_y = native_sqrt(u1)     * native_sin(PI_TIMES_2*u3);
				quatrot_left_z = native_sqrt(u1)     * native_cos(PI_TIMES_2*u3);

				rotation_movingvec[0] = genotype[0];
				rotation_movingvec[1] = genotype[1];
				rotation_movingvec[2] = genotype[2];
			}
			else	// If rotating around rotatable bond
			{
				uint rotbond_id = (rotation_list_element & RLIST_RBONDID_MASK) >> RLIST_RBONDID_SHIFT;

				float rotation_unitvec[3];
				rotation_unitvec[0] = rotbonds_unit_vectors_const[3*rotbond_id];
				rotation_unitvec[1] = rotbonds_unit_vectors_const[3*rotbond_id+1];
				rotation_unitvec[2] = rotbonds_unit_vectors_const[3*rotbond_id+2];
				float rotation_angle = genotype[6+rotbond_id]*DEG_TO_RAD;

				rotation_movingvec[0] = rotbonds_moving_vectors_const[3*rotbond_id];
				rotation_movingvec[1] = rotbonds_moving_vectors_const[3*rotbond_id+1];
				rotation_movingvec[2] = rotbonds_moving_vectors_const[3*rotbond_id+2];

				// Performing additionally the first movement which 
				// is needed only if rotating around rotatable bond
				atom_to_rotate[0] -= rotation_movingvec[0];
				atom_to_rotate[1] -= rotation_movingvec[1];
				atom_to_rotate[2] -= rotation_movingvec[2];

				// Transforming torsion angles into quaternions
				// FIXME: add precision choices with preprocessor directives: 
				// NATIVE_PRECISION, HALF_PRECISION, Full precision
				rotation_angle  = native_divide(rotation_angle, 2.0f);
				float sin_angle = native_sin(rotation_angle);
				quatrot_left_q  = native_cos(rotation_angle);
				quatrot_left_x  = sin_angle*rotation_unitvec[0];
				quatrot_left_y  = sin_angle*rotation_unitvec[1];
				quatrot_left_z  = sin_angle*rotation_unitvec[2];
			}

			// Performing rotation
			if ((rotation_list_element & RLIST_GENROT_MASK) != 0)	// If general rotation,
										// two rotations should be performed
										// (multiplying the quaternions)
			{
				// Calculating quatrot_left*ref_orientation_quats_const,
				// which means that reference orientation rotation is the first
				quatrot_temp_q = quatrot_left_q;
				quatrot_temp_x = quatrot_left_x;
				quatrot_temp_y = quatrot_left_y;
				quatrot_temp_z = quatrot_left_z;

				quatrot_left_q = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)]-
						 quatrot_temp_x*ref_orientation_quats_const[4*(*run_id)+1]-
						 quatrot_temp_y*ref_orientation_quats_const[4*(*run_id)+2]-
						 quatrot_temp_z*ref_orientation_quats_const[4*(*run_id)+3];
				quatrot_left_x = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)+1]+
						 ref_orientation_quats_const[4*(*run_id)]*quatrot_temp_x+
						 quatrot_temp_y*ref_orientation_quats_const[4*(*run_id)+3]-
						 ref_orientation_quats_const[4*(*run_id)+2]*quatrot_temp_z;
				quatrot_left_y = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)+2]+
						 ref_orientation_quats_const[4*(*run_id)]*quatrot_temp_y+
						 ref_orientation_quats_const[4*(*run_id)+1]*quatrot_temp_z-
						 quatrot_temp_x*ref_orientation_quats_const[4*(*run_id)+3];
				quatrot_left_z = quatrot_temp_q*ref_orientation_quats_const[4*(*run_id)+3]+
						 ref_orientation_quats_const[4*(*run_id)]*quatrot_temp_z+
						 quatrot_temp_x*ref_orientation_quats_const[4*(*run_id)+2]-
						 ref_orientation_quats_const[4*(*run_id)+1]*quatrot_temp_y;
			}

			quatrot_temp_q = 0 -
					 quatrot_left_x*atom_to_rotate [0] -
					 quatrot_left_y*atom_to_rotate [1] -
					 quatrot_left_z*atom_to_rotate [2];
			quatrot_temp_x = quatrot_left_q*atom_to_rotate [0] +
					 quatrot_left_y*atom_to_rotate [2] -
					 quatrot_left_z*atom_to_rotate [1];
			quatrot_temp_y = quatrot_left_q*atom_to_rotate [1] -
					 quatrot_left_x*atom_to_rotate [2] +
					 quatrot_left_z*atom_to_rotate [0];
			quatrot_temp_z = quatrot_left_q*atom_to_rotate [2] +
					 quatrot_left_x*atom_to_rotate [1] -
					 quatrot_left_y*atom_to_rotate [0];

			atom_to_rotate [0] = 0 -
					  quatrot_temp_q*quatrot_left_x +
					  quatrot_temp_x*quatrot_left_q -
					  quatrot_temp_y*quatrot_left_z +
					  quatrot_temp_z*quatrot_left_y;
			atom_to_rotate [1] = 0 -
					  quatrot_temp_q*quatrot_left_y +
					  quatrot_temp_x*quatrot_left_z +
					  quatrot_temp_y*quatrot_left_q -
					  quatrot_temp_z*quatrot_left_x;
			atom_to_rotate [2] = 0 -
					  quatrot_temp_q*quatrot_left_z -
					  quatrot_temp_x*quatrot_left_y +
					  quatrot_temp_y*quatrot_left_x +
					  quatrot_temp_z*quatrot_left_q;

			// Performing final movement and storing values
			calc_coords_x[atom_id] = atom_to_rotate [0] + rotation_movingvec[0];
			calc_coords_y[atom_id] = atom_to_rotate [1] + rotation_movingvec[1];
			calc_coords_z[atom_id] = atom_to_rotate [2] + rotation_movingvec[2];

		} // End if-statement not dummy rotation

		barrier(CLK_LOCAL_MEM_FENCE);

	} // End rotation_counter for-loop

	// ================================================
Leonardo Solis's avatar
Leonardo Solis committed
297
	// CALCULATING INTERMOLECULAR GRADIENTS
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
	// ================================================
	for (uint atom_id = get_local_id(0);
	          atom_id < dockpars_num_of_atoms;
	          atom_id+= NUM_OF_THREADS_PER_BLOCK)
	{
		uint atom_typeid = atom_types_const[atom_id];
		float x = calc_coords_x[atom_id];
		float y = calc_coords_y[atom_id];
		float z = calc_coords_z[atom_id];
		float q = atom_charges_const[atom_id];

		if ((x < 0) || (y < 0) || (z < 0) || (x >= dockpars_gridsize_x-1)
				                  || (y >= dockpars_gridsize_y-1)
						  || (z >= dockpars_gridsize_z-1)){
			
			// Setting gradients (forces) penalties.
			// These are valid as long as they are high
			gradient_inter_x[atom_id] += 16777216.0f;
			gradient_inter_y[atom_id] += 16777216.0f;
			gradient_inter_z[atom_id] += 16777216.0f;
		}
		else
		{
			// Getting coordinates
			int x_low  = (int)floor(x); 
			int y_low  = (int)floor(y); 
			int z_low  = (int)floor(z);
			int x_high = (int)ceil(x); 
			int y_high = (int)ceil(y); 
			int z_high = (int)ceil(z);
			float dx = x - x_low; 
			float dy = y - y_low; 
			float dz = z - z_low;

332
333
			//printf("%-15s %-5u %-10.8f %-10.8f %-10.8f\n", "dx,dy,dz", atom_id, dx, dy, dz);

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
			// Capturing affinity values
			uint ylow_times_g1  = y_low*g1;
			uint yhigh_times_g1 = y_high*g1;
		  	uint zlow_times_g2  = z_low*g2;
			uint zhigh_times_g2 = z_high*g2;

			// Grid offset
			uint offset_cube_000 = x_low  + ylow_times_g1  + zlow_times_g2;
			uint offset_cube_100 = x_high + ylow_times_g1  + zlow_times_g2;
			uint offset_cube_010 = x_low  + yhigh_times_g1 + zlow_times_g2;
			uint offset_cube_110 = x_high + yhigh_times_g1 + zlow_times_g2;
			uint offset_cube_001 = x_low  + ylow_times_g1  + zhigh_times_g2;
			uint offset_cube_101 = x_high + ylow_times_g1  + zhigh_times_g2;
			uint offset_cube_011 = x_low  + yhigh_times_g1 + zhigh_times_g2;
			uint offset_cube_111 = x_high + yhigh_times_g1 + zhigh_times_g2;

			uint mul_tmp = atom_typeid*g3;

			float cube[2][2][2];
			cube [0][0][0] = *(dockpars_fgrids + offset_cube_000 + mul_tmp);
			cube [1][0][0] = *(dockpars_fgrids + offset_cube_100 + mul_tmp);
			cube [0][1][0] = *(dockpars_fgrids + offset_cube_010 + mul_tmp);
		        cube [1][1][0] = *(dockpars_fgrids + offset_cube_110 + mul_tmp);
		        cube [0][0][1] = *(dockpars_fgrids + offset_cube_001 + mul_tmp);
			cube [1][0][1] = *(dockpars_fgrids + offset_cube_101 + mul_tmp);
                        cube [0][1][1] = *(dockpars_fgrids + offset_cube_011 + mul_tmp);
                        cube [1][1][1] = *(dockpars_fgrids + offset_cube_111 + mul_tmp);

			// -------------------------------------------------------------------
			// Deltas dx, dy, dz are already normalized 
			// (by host/src/getparameters.cpp) in OCLaDock.
			// The correspondance between vertices in xyz axes is:
			// 0, 1, 2, 3, 4, 5, 6, 7  and  000, 100, 010, 001, 101, 110, 011, 111
			// -------------------------------------------------------------------
			/*
			    deltas: (x-x0)/(x1-x0), (y-y0...
			    vertices: (000, 100, 010, 001, 101, 110, 011, 111)        

				  Z
				  '
				  3 - - - - 6
				 /.        /|
				4 - - - - 7 |
				| '       | |
				| 0 - - - + 2 -- Y
				'/        |/
				1 - - - - 5
			       /
			      X
			*/

			// Intermediate values for vectors in x-direction
			float x10, x52, x43, x76;
			float vx_z0, vx_z1;

			// Intermediate values for vectors in y-direction
			float y20, y51, y63, y74;
			float vy_z0, vy_z1;

			// Intermediate values for vectors in z-direction
			float z30, z41, z62, z75;
			float vz_y0, vz_y1;

			// -------------------------------------------------------------------
			// Calculating gradients (forces) corresponding to 
			// "atype" intermolecular energy
			// Derived from autodockdev/maps.py
			// -------------------------------------------------------------------

Leonardo Solis's avatar
Leonardo Solis committed
403
			// Vector in x-direction
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
			/*
			x10 = grid[int(vertices[1])] - grid[int(vertices[0])] # z = 0
			x52 = grid[int(vertices[5])] - grid[int(vertices[2])] # z = 0
			x43 = grid[int(vertices[4])] - grid[int(vertices[3])] # z = 1
			x76 = grid[int(vertices[7])] - grid[int(vertices[6])] # z = 1
			vx_z0 = (1-yd) * x10 + yd * x52     #  z = 0
			vx_z1 = (1-yd) * x43 + yd * x76     #  z = 1
			gradient[0] = (1-zd) * vx_z0 + zd * vx_z1 
			*/

			x10 = cube [1][0][0] - cube [0][0][0]; // z = 0
			x52 = cube [1][1][0] - cube [0][1][0]; // z = 0
			x43 = cube [1][0][1] - cube [0][0][1]; // z = 1
			x76 = cube [1][1][1] - cube [0][1][1]; // z = 1
			vx_z0 = (1 - dy) * x10 + dy * x52;     // z = 0
			vx_z1 = (1 - dy) * x43 + dy * x76;     // z = 1
			gradient_inter_x[atom_id] += (1 - dz) * vx_z0 + dz * vx_z1;

Leonardo Solis's avatar
Leonardo Solis committed
422
			// Vector in y-direction
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
			/*
			y20 = grid[int(vertices[2])] - grid[int(vertices[0])] # z = 0
			y51 = grid[int(vertices[5])] - grid[int(vertices[1])] # z = 0
			y63 = grid[int(vertices[6])] - grid[int(vertices[3])] # z = 1
			y74 = grid[int(vertices[7])] - grid[int(vertices[4])] # z = 1
			vy_z0 = (1-xd) * y20 + xd * y51     #  z = 0
			y_z1 = (1-xd) * y63 + xd * y74     #  z = 1
			gradient[1] = (1-zd) * vy_z0 + zd * vy_z1
			*/

			y20 = cube[0][1][0] - cube [0][0][0];	// z = 0
			y51 = cube[1][1][0] - cube [1][0][0];	// z = 0
			y63 = cube[0][1][1] - cube [0][0][1];	// z = 1
			y74 = cube[1][1][1] - cube [1][0][1];	// z = 1
			vy_z0 = (1 - dx) * y20 + dx * y51;	// z = 0
			vy_z1 = (1 - dx) * y63 + dx * y74;	// z = 1
			gradient_inter_y[atom_id] += (1 - dz) * vy_z0 + dz * vy_z1;

Leonardo Solis's avatar
Leonardo Solis committed
441
			// Vectors in z-direction
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
			/*	
			z30 = grid[int(vertices[3])] - grid[int(vertices[0])] # y = 0
			z41 = grid[int(vertices[4])] - grid[int(vertices[1])] # y = 0
			z62 = grid[int(vertices[6])] - grid[int(vertices[2])] # y = 1
			z75 = grid[int(vertices[7])] - grid[int(vertices[5])] # y = 1
			vz_y0 = (1-xd) * z30 + xd * z41     # y = 0
			vz_y1 = (1-xd) * z62 + xd * z75     # y = 1
			gradient[2] = (1-yd) * vz_y0 + yd * vz_y1
			*/

			z30 = cube [0][0][1] - cube [0][0][0];	// y = 0
			z41 = cube [1][0][1] - cube [1][0][0];	// y = 0
			z62 = cube [0][1][1] - cube [0][1][0];	// y = 1 
			z75 = cube [1][1][1] - cube [1][1][0];	// y = 1
			vz_y0 = (1 - dx) * z30 + dx * z41;	// y = 0
			vz_y1 = (1 - dx) * z62 + dx * z75;	// y = 1
			gradient_inter_z[atom_id] += (1 - dy) * vz_y0 + dy * vz_y1;

460
461
			//printf("%-15s %-3u %-10.8f %-10.8f %-10.8f %-10.8f %-10.8f %-10.8f\n", "atom aff", atom_id, vx_z0, vx_z1, vy_z0, vy_z1, vz_y0, vz_y1);

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
			// -------------------------------------------------------------------
			// Calculating gradients (forces) corresponding to 
			// "elec" intermolecular energy
			// Derived from autodockdev/maps.py
			// -------------------------------------------------------------------

			// Capturing electrostatic values
			atom_typeid = dockpars_num_of_atypes;

			mul_tmp = atom_typeid*g3;
			cube [0][0][0] = *(dockpars_fgrids + offset_cube_000 + mul_tmp);
			cube [1][0][0] = *(dockpars_fgrids + offset_cube_100 + mul_tmp);
      			cube [0][1][0] = *(dockpars_fgrids + offset_cube_010 + mul_tmp);
      			cube [1][1][0] = *(dockpars_fgrids + offset_cube_110 + mul_tmp);
		       	cube [0][0][1] = *(dockpars_fgrids + offset_cube_001 + mul_tmp);
		        cube [1][0][1] = *(dockpars_fgrids + offset_cube_101 + mul_tmp);
		        cube [0][1][1] = *(dockpars_fgrids + offset_cube_011 + mul_tmp);
		        cube [1][1][1] = *(dockpars_fgrids + offset_cube_111 + mul_tmp);

Leonardo Solis's avatar
Leonardo Solis committed
481
			// Vector in x-direction
482
483
484
485
486
487
			x10 = cube [1][0][0] - cube [0][0][0]; // z = 0
			x52 = cube [1][1][0] - cube [0][1][0]; // z = 0
			x43 = cube [1][0][1] - cube [0][0][1]; // z = 1
			x76 = cube [1][1][1] - cube [0][1][1]; // z = 1
			vx_z0 = (1 - dy) * x10 + dy * x52;     // z = 0
			vx_z1 = (1 - dy) * x43 + dy * x76;     // z = 1
488
			gradient_inter_x[atom_id] += q * ((1 - dz) * vx_z0 + dz * vx_z1);
489

Leonardo Solis's avatar
Leonardo Solis committed
490
			// Vector in y-direction
491
492
493
494
495
496
			y20 = cube[0][1][0] - cube [0][0][0];	// z = 0
			y51 = cube[1][1][0] - cube [1][0][0];	// z = 0
			y63 = cube[0][1][1] - cube [0][0][1];	// z = 1
			y74 = cube[1][1][1] - cube [1][0][1];	// z = 1
			vy_z0 = (1 - dx) * y20 + dx * y51;	// z = 0
			vy_z1 = (1 - dx) * y63 + dx * y74;	// z = 1
497
			gradient_inter_y[atom_id] += q *((1 - dz) * vy_z0 + dz * vy_z1);
498

Leonardo Solis's avatar
Leonardo Solis committed
499
			// Vectors in z-direction
500
501
502
503
504
505
			z30 = cube [0][0][1] - cube [0][0][0];	// y = 0
			z41 = cube [1][0][1] - cube [1][0][0];	// y = 0
			z62 = cube [0][1][1] - cube [0][1][0];	// y = 1 
			z75 = cube [1][1][1] - cube [1][1][0];	// y = 1
			vz_y0 = (1 - dx) * z30 + dx * z41;	// y = 0
			vz_y1 = (1 - dx) * z62 + dx * z75;	// y = 1
506
507
508
			gradient_inter_z[atom_id] += q *((1 - dy) * vz_y0 + dy * vz_y1);

			//printf("%-15s %-3u %-10.8f %-10.8f %-10.8f %-10.8f %-10.8f %-10.8f\n", "elec", atom_id, vx_z0, vx_z1, vy_z0, vy_z1, vz_y0, vz_y1);
509
510

			// -------------------------------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
511
			// Calculating gradients (forces) corresponding to 
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
			// "dsol" intermolecular energy
			// Derived from autodockdev/maps.py
			// -------------------------------------------------------------------

			// Capturing desolvation values
			atom_typeid = dockpars_num_of_atypes+1;

			mul_tmp = atom_typeid*g3;
			cube [0][0][0] = *(dockpars_fgrids + offset_cube_000 + mul_tmp);
			cube [1][0][0] = *(dockpars_fgrids + offset_cube_100 + mul_tmp);
      			cube [0][1][0] = *(dockpars_fgrids + offset_cube_010 + mul_tmp);
      			cube [1][1][0] = *(dockpars_fgrids + offset_cube_110 + mul_tmp);
      			cube [0][0][1] = *(dockpars_fgrids + offset_cube_001 + mul_tmp);
      			cube [1][0][1] = *(dockpars_fgrids + offset_cube_101 + mul_tmp);
      			cube [0][1][1] = *(dockpars_fgrids + offset_cube_011 + mul_tmp);
      			cube [1][1][1] = *(dockpars_fgrids + offset_cube_111 + mul_tmp);

Leonardo Solis's avatar
Leonardo Solis committed
529
			// Vector in x-direction
530
531
532
533
534
535
			x10 = cube [1][0][0] - cube [0][0][0]; // z = 0
			x52 = cube [1][1][0] - cube [0][1][0]; // z = 0
			x43 = cube [1][0][1] - cube [0][0][1]; // z = 1
			x76 = cube [1][1][1] - cube [0][1][1]; // z = 1
			vx_z0 = (1 - dy) * x10 + dy * x52;     // z = 0
			vx_z1 = (1 - dy) * x43 + dy * x76;     // z = 1
536
			gradient_inter_x[atom_id] += fabs(q) * ((1 - dz) * vx_z0 + dz * vx_z1);
537

Leonardo Solis's avatar
Leonardo Solis committed
538
			// Vector in y-direction
539
540
541
542
543
544
			y20 = cube[0][1][0] - cube [0][0][0];	// z = 0
			y51 = cube[1][1][0] - cube [1][0][0];	// z = 0
			y63 = cube[0][1][1] - cube [0][0][1];	// z = 1
			y74 = cube[1][1][1] - cube [1][0][1];	// z = 1
			vy_z0 = (1 - dx) * y20 + dx * y51;	// z = 0
			vy_z1 = (1 - dx) * y63 + dx * y74;	// z = 1
545
			gradient_inter_y[atom_id] += fabs(q) *((1 - dz) * vy_z0 + dz * vy_z1);
546

Leonardo Solis's avatar
Leonardo Solis committed
547
			// Vectors in z-direction
548
549
550
551
552
553
			z30 = cube [0][0][1] - cube [0][0][0];	// y = 0
			z41 = cube [1][0][1] - cube [1][0][0];	// y = 0
			z62 = cube [0][1][1] - cube [0][1][0];	// y = 1 
			z75 = cube [1][1][1] - cube [1][1][0];	// y = 1
			vz_y0 = (1 - dx) * z30 + dx * z41;	// y = 0
			vz_y1 = (1 - dx) * z62 + dx * z75;	// y = 1
554
555
556
557
			gradient_inter_z[atom_id] += fabs(q) *((1 - dy) * vz_y0 + dy * vz_y1);


			//printf("%-15s %-3u %-10.8f %-10.8f %-10.8f %-10.8f %-10.8f %-10.8f\n", "desol", atom_id, vx_z0, vx_z1, vy_z0, vy_z1, vz_y0, vz_y1);
558
559
560
561
562
563

			// -------------------------------------------------------------------
		}

	} // End atom_id for-loop (INTERMOLECULAR ENERGY)

564
565
566
567
	// Inter- and intra-molecular energy calculation
	// are independent from each other, so NO barrier is needed here.
  	// As these two require different operations,
	// they can be executed only sequentially on the GPU.
568
569

	// ================================================
Leonardo Solis's avatar
Leonardo Solis committed
570
	// CALCULATING INTRAMOLECULAR GRADIENTS
571
572
573
	// ================================================
	for (uint contributor_counter = get_local_id(0);
	          contributor_counter < dockpars_num_of_intraE_contributors;
Leonardo Solis's avatar
Leonardo Solis committed
574
	          contributor_counter+= NUM_OF_THREADS_PER_BLOCK)
575
	{
576
		// Getting atom IDs
577
578
		uint atom1_id = intraE_contributors_const[3*contributor_counter];
		uint atom2_id = intraE_contributors_const[3*contributor_counter+1];
579
580
581
		/*
		printf ("%-5u %-5u %-5u\n", contributor_counter, atom1_id, atom2_id);
		*/
582

Leonardo Solis's avatar
Leonardo Solis committed
583
584
585
586
587
		// Calculating vector components of vector going
		// from first atom's to second atom's coordinates
		float subx = calc_coords_x[atom1_id] - calc_coords_x[atom2_id];
		float suby = calc_coords_y[atom1_id] - calc_coords_y[atom2_id];
		float subz = calc_coords_z[atom1_id] - calc_coords_z[atom2_id];
588

589
		// Calculating atomic distance
590
591
592
593
594
		float atomic_distance = native_sqrt(subx*subx + suby*suby + subz*subz)*dockpars_grid_spacing;

		if (atomic_distance < 1.0f)
			atomic_distance = 1.0f;

595
		// Calculating gradient contributions
596
597
598
599
600
601
		if ((atomic_distance < 8.0f) && (atomic_distance < 20.48f))
		{
			// Getting type IDs
			uint atom1_typeid = atom_types_const[atom1_id];
			uint atom2_typeid = atom_types_const[atom2_id];

602
603
604
605
			// Calculating van der Waals / hydrogen bond term
			gradient_per_intracontributor[contributor_counter] += native_divide (-12*VWpars_AC_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],
									                     native_powr(atomic_distance, 13)
									       		    );
606

607
608
609
			if (intraE_contributors_const[3*contributor_counter+2] == 1) {	//H-bond
				gradient_per_intracontributor[contributor_counter] += native_divide (10*VWpars_BD_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],
										                     native_powr(atomic_distance, 11)
610
611
												    );

612
613
614
615
616
617
			}
			else {	//van der Waals
				gradient_per_intracontributor[contributor_counter] += native_divide (6*VWpars_BD_const[atom1_typeid * dockpars_num_of_atypes+atom2_typeid],
										                     native_powr(atomic_distance, 7)
										                    );
			}
618

619
620
			// Calculating electrostatic term
			// http://www.wolframalpha.com/input/?i=1%2F(x*(A%2B(B%2F(1%2BK*exp(-h*B*x)))))
Leonardo Solis's avatar
Leonardo Solis committed
621
			float upper = DIEL_A*native_powr(native_exp(DIEL_B_TIMES_H*atomic_distance) + DIEL_K, 2) + (DIEL_B)*native_exp(DIEL_B_TIMES_H*atomic_distance)*(DIEL_B_TIMES_H_TIMES_K*atomic_distance + native_exp(DIEL_B_TIMES_H*atomic_distance) + DIEL_K);
622
		
Leonardo Solis's avatar
Leonardo Solis committed
623
			float lower = native_powr(atomic_distance, 2) * native_powr(DIEL_A * (native_exp(DIEL_B_TIMES_H*atomic_distance) + DIEL_K) + DIEL_B * native_exp(DIEL_B_TIMES_H*atomic_distance), 2);
624

Leonardo Solis's avatar
Leonardo Solis committed
625
        		gradient_per_intracontributor[contributor_counter] +=  -dockpars_coeff_elec * atom_charges_const[atom1_id] * atom_charges_const[atom2_id] * native_divide (upper, lower);
626

627
628
629
630
631
632
			// Calculating desolvation term
			gradient_per_intracontributor[contributor_counter] += (
									       (dspars_S_const[atom1_typeid] + dockpars_qasp*fabs(atom_charges_const[atom1_id])) * dspars_V_const[atom2_typeid] +
							                       (dspars_S_const[atom2_typeid] + dockpars_qasp*fabs(atom_charges_const[atom2_id])) * dspars_V_const[atom1_typeid]
				        				      ) *
					                       			dockpars_coeff_desolv * -0.07716049382716049 * atomic_distance * native_exp(-0.038580246913580245*native_powr(atomic_distance, 2));
633

634
635
		}
	} // End contributor_counter for-loop (INTRAMOLECULAR ENERGY)
636

637
	barrier(CLK_LOCAL_MEM_FENCE);
638

639
	// Accumulating gradients from "gradient_per_intracontributor" for each each
640
641
642
643
644
645
646
647
648
	if (get_local_id(0) == 0) {
		for (uint contributor_counter = 0;
			  contributor_counter < dockpars_num_of_intraE_contributors;
			  contributor_counter ++) {

			// Getting atom IDs
			uint atom1_id = intraE_contributors_const[3*contributor_counter];
			uint atom2_id = intraE_contributors_const[3*contributor_counter+1];

649
650
651
652
653
654
			// Calculating xyz distances in Angstroms of vector
			// that goes from "atom1_id"-to-"atom2_id"
			float subx = (calc_coords_x[atom2_id] - calc_coords_x[atom1_id]);
			float suby = (calc_coords_y[atom2_id] - calc_coords_y[atom1_id]);
			float subz = (calc_coords_z[atom2_id] - calc_coords_z[atom1_id]);
			float dist = native_sqrt(subx*subx + suby*suby + subz*subz);
655
656
657
658

			// Calculating gradients in xyz components.
			// Gradients for both atoms in a single contributor pair
			// have the same magnitude, but opposite directions
659
660
661
			gradient_intra_x[atom1_id] -= gradient_per_intracontributor[contributor_counter] * subx / dist;
			gradient_intra_y[atom1_id] -= gradient_per_intracontributor[contributor_counter] * suby / dist;
			gradient_intra_z[atom1_id] -= gradient_per_intracontributor[contributor_counter] * subz / dist;
662

663
664
665
			gradient_intra_x[atom2_id] += gradient_per_intracontributor[contributor_counter] * subx / dist;
			gradient_intra_y[atom2_id] += gradient_per_intracontributor[contributor_counter] * suby / dist;
			gradient_intra_z[atom2_id] += gradient_per_intracontributor[contributor_counter] * subz / dist;
666
667
668
		}
	}
	
669
670
671

	barrier(CLK_LOCAL_MEM_FENCE);

672
673
674
675
	// Accumulating inter- and intramolecular gradients
	for (uint atom_cnt = get_local_id(0);
		  atom_cnt < dockpars_num_of_atoms;
		  atom_cnt+= NUM_OF_THREADS_PER_BLOCK) {
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695

		// Grid gradients were calculated in the grid space,
		// so they have to be put back in Angstrom.

		// Intramolecular gradients were already in Angstrom,
		// so no scaling for them is required.
		gradient_inter_x[atom_cnt] = gradient_inter_x[atom_cnt] / dockpars_grid_spacing;
		gradient_inter_y[atom_cnt] = gradient_inter_y[atom_cnt] / dockpars_grid_spacing;
		gradient_inter_z[atom_cnt] = gradient_inter_z[atom_cnt] / dockpars_grid_spacing;

		gradient_x[atom_cnt] = gradient_inter_x[atom_cnt] ;//+ gradient_intra_x[atom_cnt];
		gradient_y[atom_cnt] = gradient_inter_y[atom_cnt] ;//+ gradient_intra_y[atom_cnt];
		gradient_z[atom_cnt] = gradient_inter_z[atom_cnt] ;//+ gradient_intra_z[atom_cnt];
	
		printf("%-15s %-5u %-10.8f %-10.8f %-10.8f\n", "grad_grid", atom_cnt, gradient_inter_x[atom_cnt], gradient_inter_y[atom_cnt], gradient_inter_z[atom_cnt]);

		//printf("%-15s %-5u %-10.8f %-10.8f %-10.8f\n", "grad_intra", atom_cnt, gradient_intra_x[atom_cnt], gradient_intra_y[atom_cnt], gradient_intra_z[atom_cnt]);

		//printf("%-15s %-5u %-10.8f %-10.8f %-10.8f\n", "calc_coords", atom_cnt, calc_coords_x[atom_cnt], calc_coords_y[atom_cnt], calc_coords_z[atom_cnt]);

696
697
	}

698
699
	barrier(CLK_LOCAL_MEM_FENCE);

700
	// ------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
701
	// Obtaining translation-related gradients
702
703
704
705
706
	// ------------------------------------------
	if (get_local_id(0) == 0) {
		for (uint lig_atom_id = 0;
			  lig_atom_id<dockpars_num_of_atoms;
			  lig_atom_id++) {
707
708
709
			gradient_genotype[0] += gradient_x[lig_atom_id]; // gradient for gene 0: gene x
			gradient_genotype[1] += gradient_y[lig_atom_id]; // gradient for gene 1: gene y
			gradient_genotype[2] += gradient_z[lig_atom_id]; // gradient for gene 2: gene z
710
		}
711

712
713
714
715
716
717
718
719
720
		// Scaling gradient for translational genes as 
		// their corresponding gradients were calculated in the space 
		// where these genes are in Angstrom,
		// but OCLaDock translational genes are within in grids
		gradient_genotype[0] *= dockpars_grid_spacing;
		gradient_genotype[1] *= dockpars_grid_spacing;
		gradient_genotype[2] *= dockpars_grid_spacing;

		#if defined (DEBUG_GRAD_TRANSLATION_GENES)
721
722
723
		printf("gradient_x:%f\n", gradient_genotype [0]);
		printf("gradient_y:%f\n", gradient_genotype [1]);
		printf("gradient_z:%f\n", gradient_genotype [2]);
724
725
		#endif

726
727
728
	}

	// ------------------------------------------
Leonardo Solis's avatar
Leonardo Solis committed
729
730
	// Obtaining rotation-related gradients
	// ------------------------------------------ 
731
732
733
734
735
736
737
738
739
740
741
				
	// Transform gradients_inter_{x|y|z} 
	// into local_gradients[i] (with four quaternion genes)
	// Derived from autodockdev/motions.py/forces_to_delta_genes()

	// Transform local_gradients[i] (with four quaternion genes)
	// into local_gradients[i] (with three Shoemake genes)
	// Derived from autodockdev/motions.py/_get_cube3_gradient()
	// ------------------------------------------
	if (get_local_id(0) == 1) {

742
743
744
745
		float3 torque_rot;
		torque_rot.x = 0.0f;
		torque_rot.y = 0.0f;
		torque_rot.z = 0.0f;
746

747
		#if defined (DEBUG_GRAD_ROTATION_GENES)
748
		printf("%-20s %-10.5f %-10.5f %-10.5f\n", "initial torque: ", torque_rot.x, torque_rot.y, torque_rot.z);
749
		#endif
750

Leonardo Solis's avatar
Leonardo Solis committed
751
		// Center of rotation 
752
753
		// In getparameters.cpp, it indicates 
		// translation genes are in grid spacing (instead of Angstroms)
Leonardo Solis's avatar
Leonardo Solis committed
754
		float3 about;
755
756
757
		about.x = /*30*/genotype[0];
		about.y = /*30*/genotype[1];
		about.z = /*30*/genotype[2];
758
	
759
760
761
		// Temporal variable to calculate translation differences.
		// They are converted back to Angstroms here
		float3 r;
762
			
763
764
765
		for (uint lig_atom_id = 0;
			  lig_atom_id<dockpars_num_of_atoms;
			  lig_atom_id++) {
Leonardo Solis's avatar
Leonardo Solis committed
766
767
768
			r.x = (calc_coords_x[lig_atom_id] - about.x) * dockpars_grid_spacing; 
			r.y = (calc_coords_y[lig_atom_id] - about.y) * dockpars_grid_spacing;  
			r.z = (calc_coords_z[lig_atom_id] - about.z) * dockpars_grid_spacing; 
769

770
771
772
773
774
			float3 force;
			force.x	= gradient_x[lig_atom_id];
			force.y	= gradient_y[lig_atom_id]; 
			force.z	= gradient_z[lig_atom_id];

775
			torque_rot += cross(r, force);
776
777

			#if defined (DEBUG_GRAD_ROTATION_GENES)
778
779
780
781
782
			printf("%-20s %-10u\n", "contrib. of atom-id: ", lig_atom_id);
			printf("%-20s %-10.5f %-10.5f %-10.5f\n", "r             : ", r.x, r.y, r.z);
			printf("%-20s %-10.5f %-10.5f %-10.5f\n", "force         : ", force.x, force.y, force.z);
			printf("%-20s %-10.5f %-10.5f %-10.5f\n", "partial torque: ", torque_rot.x, torque_rot.y, torque_rot.z);
			printf("\n");
783
			#endif
784
		}
785

786
		#if defined (DEBUG_GRAD_ROTATION_GENES)
787
		printf("%-20s %-10.5f %-10.5f %-10.5f\n", "final torque: ", torque_rot.x, torque_rot.y, torque_rot.z);
788
		#endif
789
790
791

		// Derived from rotation.py/axisangle_to_q()
		// genes[3:7] = rotation.axisangle_to_q(torque, rad)
792
		float torque_length = fast_length(torque_rot);
793
794
		
		#if defined (DEBUG_GRAD_ROTATION_GENES)
795
		printf("%-20s %-10.5f\n", "torque length: ", torque_length);
796
		#endif
797

798
		// Infinitesimal rotation in radians
799
		const float infinitesimal_radian = 1E-5;
800
801
802
803
804
805
806
807

		// Finding the quaternion that performs
		// the infinitesimal rotation around torque axis
		float4 quat_torque;
		quat_torque.w = native_cos(infinitesimal_radian*0.5f);
		quat_torque.x = fast_normalize(torque_rot).x * native_sin(infinitesimal_radian*0.5f);
		quat_torque.y = fast_normalize(torque_rot).y * native_sin(infinitesimal_radian*0.5f);
		quat_torque.z = fast_normalize(torque_rot).z * native_sin(infinitesimal_radian*0.5f);
808
809

		#if defined (DEBUG_GRAD_ROTATION_GENES)
810
		printf("%-20s %-10.5f %-10.5f %-10.5f %-10.5f\n", "quat_torque (w,x,y,z): ", quat_torque.w, quat_torque.x, quat_torque.y, quat_torque.z);
811
		#endif
812

Leonardo Solis's avatar
Leonardo Solis committed
813
		// Converting quaternion gradients into Shoemake gradients 
814
815
		// Derived from autodockdev/motion.py/_get_cube3_gradient

816
		// This is where we are in Shoemake space
817
818
819
820
		float current_u1, current_u2, current_u3;
		current_u1 = genotype[3]; // check very initial input Shoemake genes
		current_u2 = genotype[4];
		current_u3 = genotype[5];
821
822
		
		#if defined (DEBUG_GRAD_ROTATION_GENES)
823
		printf("%-30s %-10.5f %-10.5f %-10.5f\n", "current_u (1,2,3): ", genotype[3], genotype[4], genotype[5]);
824
		#endif		
825

Leonardo Solis's avatar
Leonardo Solis committed
826
		// This is where we are in quaternion space
827
		// current_q = cube3_to_quaternion(current_u)
828
829
830
831
832
		float4 current_q;
		current_q.w = native_sqrt(1-current_u1) * native_sin(PI_TIMES_2*current_u2);
		current_q.x = native_sqrt(1-current_u1) * native_cos(PI_TIMES_2*current_u2);
		current_q.y = native_sqrt(current_u1)   * native_sin(PI_TIMES_2*current_u3);
		current_q.z = native_sqrt(current_u1)   * native_cos(PI_TIMES_2*current_u3);
833
834

		#if defined (DEBUG_GRAD_ROTATION_GENES)
835
		printf("%-30s %-10.8f %-10.8f %-10.8f %-10.8f\n", "current_q (w,x,y,z): ", current_q.w, current_q.x, current_q.y, current_q.z);
836
		#endif
837

Leonardo Solis's avatar
Leonardo Solis committed
838
		// This is where we want to be in quaternion space
839
		float4 target_q;
840
841
842
843

		// target_q = rotation.q_mult(q, current_q)
		// Derived from autodockdev/rotation.py/q_mult()
		// In our terms means q_mult(quat_{w|x|y|z}, current_q{w|x|y|z})
844
845
846
847
		target_q.w = quat_torque.w*current_q.w - quat_torque.x*current_q.x - quat_torque.y*current_q.y - quat_torque.z*current_q.z;// w
		target_q.x = quat_torque.w*current_q.x + quat_torque.x*current_q.w + quat_torque.y*current_q.z - quat_torque.z*current_q.y;// x
		target_q.y = quat_torque.w*current_q.y + quat_torque.y*current_q.w + quat_torque.z*current_q.x - quat_torque.x*current_q.z;// y
		target_q.z = quat_torque.w*current_q.z + quat_torque.z*current_q.w + quat_torque.x*current_q.y - quat_torque.y*current_q.x;// z
848
		#if defined (DEBUG_GRAD_ROTATION_GENES)
849
		printf("%-30s %-10.8f %-10.8f %-10.8f %-10.8f\n", "target_q (w,x,y,z): ", target_q.w, target_q.x, target_q.y, target_q.z);
850
		#endif
851

852
		// This is where we want to be in Shoemake space
853
854
855
856
857
		float target_u1, target_u2, target_u3;

		// target_u = quaternion_to_cube3(target_q)
		// Derived from autodockdev/motions.py/quaternion_to_cube3()
		// In our terms means quaternion_to_cube3(target_q{w|x|y|z})
858
859
860
		target_u1 = target_q.y*target_q.y + target_q.z*target_q.z;
		target_u2 = atan2(target_q.w, target_q.x);
		target_u3 = atan2(target_q.y, target_q.z);
861
		
862
863
864
865
866
		if (target_u2 < 0.0f)       { target_u2 += PI_TIMES_2; }
		if (target_u2 > PI_TIMES_2) { target_u2 -= PI_TIMES_2; }
		if (target_u3 < 0.0f) 	    { target_u3 += PI_TIMES_2; }
		if (target_u3 > PI_TIMES_2) { target_u3 -= PI_TIMES_2; }
		/*
867
		printf("%-30s %-10.8f %-10.8f %-10.8f\n", "target_u (1,2,3) - before mapping: ", target_u1, target_u2, target_u3);
868
869
		target_u2 = target_u2 / PI_TIMES_2;
		target_u3 = target_u3 / PI_TIMES_2;
870
871
872
		*/

		#if defined (DEBUG_GRAD_ROTATION_GENES)
873
		printf("%-30s %-10.8f %-10.8f %-10.8f\n", "target_u (1,2,3) - after mapping: ", target_u1, target_u2, target_u3);
874
		#endif
875
876
877
878
879
880
881
882
		
   		// The infinitesimal rotation will produce an infinitesimal displacement
    		// in shoemake space. This is to guarantee that the direction of
    		// the displacement in shoemake space is not distorted.
    		// The correct amount of displacement in shoemake space is obtained
		// by multiplying the infinitesimal displacement by shoemake_scaling:
		float shoemake_scaling = torque_length / infinitesimal_radian;

Leonardo Solis's avatar
Leonardo Solis committed
883
		// Derivates in cube3
884
885
		// "current_u2" and "current_u3" are mapped into 
		// the same range [0, 2PI] of "target_u2" and "target_u3"
886
		float grad_u1, grad_u2, grad_u3;
887
		grad_u1 = shoemake_scaling * (target_u1 - current_u1);
888
889
		grad_u2 = shoemake_scaling * (target_u2 - current_u2 * PI_TIMES_2);
		grad_u3 = shoemake_scaling * (target_u3 - current_u3 * PI_TIMES_2);
890
891

		#if defined (DEBUG_GRAD_ROTATION_GENES)
892
		printf("%-30s %-10.8f %-10.8f %-10.8f\n", "grad_u (1,2,3) - before emp. scaling: ", grad_u1, grad_u2, grad_u3);
893
		#endif
894
			
Leonardo Solis's avatar
Leonardo Solis committed
895
		// Empirical scaling
896
		float temp_u1 = genotype[3];
897
			
898
		if ((0.0f < temp_u1) && (temp_u1 < 1.0f)){
899
			grad_u1 *= ((1.0f/temp_u1) + (1.0f/(1.0f-temp_u1)));
900
		}
901
902
		grad_u2 *= 4.0f * (1.0f-temp_u1);
		grad_u3 *= 4.0f * temp_u1;
903
904

		#if defined (DEBUG_GRAD_ROTATION_GENES)
905
		printf("%-30s %-10.8f %-10.8f %-10.8f\n", "grad_u (1,2,3) - after emp. scaling: ", grad_u1, grad_u2, grad_u3);
906
		#endif
907
		
908
909
910
911
		// Setting gradient rotation-related genotypes in cube3.
		// Scaling gradient for u2 and u3 genes as 
		// their corresponding gradients were calculated in the space where u2/3 are within [0, 2PI]
		// but OCLaDock u2/3 genes are within [0, 1]
912
		gradient_genotype[3] = grad_u1;
913
914
915
		gradient_genotype[4] = grad_u2 * PI_TIMES_2; 
		gradient_genotype[5] = grad_u3 * PI_TIMES_2;
		
916
917
918
919
920
		/*
		printf("gradient_shoemake_u1:%f\n", gradient_genotype [3]);
		printf("gradient_shoemake_u2:%f\n", gradient_genotype [4]);
		printf("gradient_shoemake_u3:%f\n", gradient_genotype [5]);
		*/
921
922
	}

Leonardo Solis's avatar
Leonardo Solis committed
923
924
925
	// ------------------------------------------
	// Obtaining torsion-related gradients
	// ------------------------------------------
926
927
928
929
930
931
	if (get_local_id(0) == 2) {

		for (uint rotbond_id = 0;
			  rotbond_id < dockpars_num_of_genes-6;
			  rotbond_id ++) {

932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
			// Querying ids of atoms belonging to the rotatable bond in question
			int atom1_id = rotbonds_const[2*rotbond_id];
			int atom2_id = rotbonds_const[2*rotbond_id+1];

			float3 atomRef_coords;
			atomRef_coords.x = calc_coords_x[atom1_id];
			atomRef_coords.y = calc_coords_y[atom1_id];
			atomRef_coords.z = calc_coords_z[atom1_id];

			#if defined (DEBUG_GRAD_TORSION_GENES)
			printf("%-15s %-10u\n", "rotbond_id: ", rotbond_id);
			printf("%-15s %-10i\n", "atom1_id: ", atom1_id);
			printf("%-15s %-10.8f %-10.8f %-10.8f\n", "atom1_coords: ", calc_coords_x[atom1_id], calc_coords_y[atom1_id], calc_coords_z[atom1_id]);

			printf("%-15s %-10i\n", "atom2_id: ", atom2_id);
			printf("%-15s %-10.8f %-10.8f %-10.8f\n", "atom2_coords: ", calc_coords_x[atom2_id], calc_coords_y[atom2_id], calc_coords_z[atom2_id]);
			printf("\n");
			#endif		

951
			float3 rotation_unitvec;
952
			/*
953
954
955
			rotation_unitvec.x = rotbonds_unit_vectors_const[3*rotbond_id];
			rotation_unitvec.y = rotbonds_unit_vectors_const[3*rotbond_id+1];
			rotation_unitvec.z = rotbonds_unit_vectors_const[3*rotbond_id+2];
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
			*/
			rotation_unitvec.x = calc_coords_x[atom2_id] - calc_coords_x[atom1_id];
			rotation_unitvec.y = calc_coords_y[atom2_id] - calc_coords_y[atom1_id];
			rotation_unitvec.z = calc_coords_z[atom2_id] - calc_coords_z[atom1_id];
			rotation_unitvec = fast_normalize(rotation_unitvec);


			/*
			float3 rotation_movingvec;
			rotation_movingvec.x = rotbonds_moving_vectors_const[3*rotbond_id];
			rotation_movingvec.y = rotbonds_moving_vectors_const[3*rotbond_id+1];
			rotation_movingvec.z = rotbonds_moving_vectors_const[3*rotbond_id+2];			
			*/

			// Atom belonging to "rotbond_id"
			//uint atom_id = rotation_list_element & RLIST_ATOMID_MASK;

973
974

			// Torque of torsions
975
976
977
978
979
980
981
982
983
984
			float3 torque_tor;
			torque_tor.x = 0.0f;
			torque_tor.y = 0.0f;
			torque_tor.z = 0.0f;

			// Iterating over each ligand atom that rotates 
			// if the bond in question rotates
			for (uint rotable_atom_cnt = 0;
				  rotable_atom_cnt<num_rotating_atoms_per_rotbond_const[rotbond_id];
				  rotable_atom_cnt++) {
985

986
				uint lig_atom_id = rotbonds_atoms_const[MAX_NUM_OF_ATOMS*rotbond_id + rotable_atom_cnt];
987

Leonardo Solis's avatar
Leonardo Solis committed
988
				// Calculating torque on point "A" 
989
				// (could be any other point "B" along the rotation axis)
990
991
992
993
994
995
996
997
998
999
1000
				float3 atom_coords;
				atom_coords.x = calc_coords_x[lig_atom_id];
				atom_coords.y = calc_coords_y[lig_atom_id];
				atom_coords.z = calc_coords_z[lig_atom_id];

				// Temporal variable to calculate translation differences.
				// They are converted back to Angstroms here
				float3 r;
				r.x = (atom_coords.x - atomRef_coords.x) * dockpars_grid_spacing;
				r.y = (atom_coords.y - atomRef_coords.y) * dockpars_grid_spacing;
				r.z = (atom_coords.z - atomRef_coords.z) * dockpars_grid_spacing;