kupdatesd.br 7.6 KB
Newer Older
Mark Friedrichs's avatar
Mark Friedrichs committed
1

2
3
4
5
6
7
8
9
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
10
11
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Mark Friedrichs, Mike Houston                                     *
12
13
 * Contributors:                                                              *
 *                                                                            *
14
15
16
17
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
18
 *                                                                            *
19
20
21
22
 * 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 Lesser General Public License for more details.                        *
23
 *                                                                            *
24
25
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
26
 * -------------------------------------------------------------------------- */
Mark Friedrichs's avatar
Mark Friedrichs committed
27

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Applies a permutation to the given random vector stream
//Totally bandwidth bound and streams are large, should be done infrequently.
//Probably don't want gvin and gvout to be the same stream!

kernel void kpermute_vectors( float gstrwidth, float perm<>, 
		float3 gvin[][], out float3 gvout<> ){
	float2 ind;
	//ind.y = floor( perm / gstrwidth );
   ind.y = round( (perm - fmod( perm, gstrwidth ))/gstrwidth );
	ind.x = perm - ind.y * gstrwidth;

	gvout = gvin[ ind ];
}

Mark Friedrichs's avatar
Mark Friedrichs committed
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
/* Stochastic Integrator
 *
 * Since we don't have random numbers on the GPU, we 
 * precalculate random numbers from a normal distribution
 * and store them in several large textures. Periodically
 * these will have to be refreshed on the CPU.
 *
 * Doing gathers instead of streams for the random vectors, 
 * but the gathers should be fully coherent anyway. 
 * This is just to avoid some
 * painful manipulations with multiple streams and domains.  
 * */


/* First part before first constraint call 
 *
 *
 * Many things can be precalculated here
 * If we become compute bound (wonder how), we can precalc stuff
 *
 * Assuming constant temperature. If you want to change the 
 * temperature, you have to change all the precalculated 
 * constants that use it.
 * */

/*
 * Alternative formulation to handle precision better
 * In updatesd1, we output only deltaV's. The shake
 * kernel uses the deltaV's and the X's to output 
 * a bunch of constrained deltax's
 * */


kernel void kupdate_sd1_fix1(
		float xstrwidth,   //atom stream width
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers

		//sd constants, prefixed with c 
		//to avoid 1 letter names, cxx = sdc[0].xx
		float cem,

		//sd precalculated constants
		float pc1,  //tau_t[0] * ( 1 - sdc[0].em )
		float pc2,  //tau_t[gt] * (sdc[gt].eph - sdc.emh)
		float pc3,  //sdc[0].d/(tau_t * sdc[0].c);
		
		//sdpc = sd precalculated per atom values
		//sdpc.x = 1/sqrt(m)*sig[0].Yv
		//sdpc.y = 1/sqrt(m)*sig[0].V
		float2 sdpc<>,   
		
		//Normally distributed random vectors
		float3 fgauss[][],
	
		float3 sd2X<>,  //This is calculated in sd2 of the previous step	
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
98
		float3 posq<>,
Mark Friedrichs's avatar
Mark Friedrichs committed
99
100
101
102
103
104
		float3 f<>,
		float3 v<>,

		float invmass<>,
		out float3 sd1V<>, //This is V in gromacs, reused in sd2  
		out float3 vnew<>,
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
105
		out float3 posqp<>
Mark Friedrichs's avatar
Mark Friedrichs committed
106
		){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
107
   float  c1;
Mark Friedrichs's avatar
Mark Friedrichs committed
108
109
110
111
112
	float3 Vmh;
	float3 fg1, fg2;
	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
113
	igauss         = indexof( posq ).xy;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
114
	linind_gauss   = igauss.y * xstrwidth + igauss.x;
Mark Friedrichs's avatar
Mark Friedrichs committed
115

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
116
	//2 random vectors used in each fragment
Mark Friedrichs's avatar
Mark Friedrichs committed
117

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
118
	linind_gauss   = 2.0f * linind_gauss + goffset; 	
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
119
120
121
   igauss.y       = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth);
	igauss.x       = linind_gauss - igauss.y * gstrwidth;
	fg1            = fgauss[ igauss ];
Mark Friedrichs's avatar
Mark Friedrichs committed
122

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
123
124
125
126
	linind_gauss  += 1.0f;
   igauss.y       = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth);
	igauss.x       = linind_gauss - igauss.y * gstrwidth;
	fg2            = fgauss[ igauss ];
Mark Friedrichs's avatar
Mark Friedrichs committed
127

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	//Vmh            = (sd2X*pc3) + (sdpc.x*fg1);

	Vmh.x          = pc3*sd2X.x + sdpc.x*fg1.x;
	Vmh.y          = pc3*sd2X.y + sdpc.x*fg1.y;
	Vmh.z          = pc3*sd2X.z + sdpc.x*fg1.z;

	//sd1V           = sdpc.y*fg2;
   sd1V.x         = sdpc.y*fg2.x;
   sd1V.y         = sdpc.y*fg2.y;
   sd1V.z         = sdpc.y*fg2.z;
  
	//vnew           = (v*cem) + (invmass*f*pc1) + sd1V - (Vmh*cem);
   c1             = invmass*pc1;
   vnew.x         = cem*(v.x - Vmh.x) + c1*f.x + sd1V.x;
   vnew.y         = cem*(v.y - Vmh.y) + c1*f.y + sd1V.y;
   vnew.z         = cem*(v.z - Vmh.z) + c1*f.z + sd1V.z;

	posqp.x        = pc2*vnew.x;
	posqp.y        = pc2*vnew.y;
	posqp.z        = pc2*vnew.z;

Mark Friedrichs's avatar
Mark Friedrichs committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
}

kernel void kupdate_sd2_fix1(
		float xstrwidth,   //stream width of atoms
		float gstrwidth,   //stream width of random numbers
		float goffset,     //starting offset for random numbers
		
		float pc1,          //this is 1/pc2 for sd1 
		                    //= 1/(tau_t*(sdc[0].eph-sdc[0].emh))
		float pc2,          //= tau_t * sdc[0].d/(sdc[0].em - 1)
		

		//per atom precalcs
		//sdpc.x = 1/sqrt(m)*sig[0].Yx
		//sdpc.y = 1/sqrt(m)*sig[0].X
		float2 sdpc<>,

		//Normally distributed random vectors
		float3 fgauss[][],
		
		float3 sd1V<>,      //calculated in sd1
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
170
171
		float3 posq<>,      //positions pre-update
		float3 posqp<>,     //deltas post constraint
Mark Friedrichs's avatar
Mark Friedrichs committed
172
173
174
		float3 vnew<>,      //velocities after sd1
		out float3 sd2X<>,  //Used in sd1
		out float3 v<>,     //velocities corrected for constraints
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
175
		out float3 posqp2<> //new deltas, need to be constrained again
Mark Friedrichs's avatar
Mark Friedrichs committed
176
177
178
179
180
181
182
		){

	float linind_gauss; //linear index into the random numbers
	float2 igauss;      //2d index
	float3 fg1, fg2;
	float3 Xmh;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
183
	igauss            = indexof( posq ).xy;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
184
	linind_gauss      = igauss.y * xstrwidth + igauss.x;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
185
	linind_gauss      = 2.0f * linind_gauss + goffset; 	
Mark Friedrichs's avatar
Mark Friedrichs committed
186
	
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
187
188
189
   igauss.y          = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth); 
	igauss.x          = linind_gauss - igauss.y * gstrwidth;
	fg1               = fgauss[ igauss ];
Mark Friedrichs's avatar
Mark Friedrichs committed
190

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
191
192
193
194
	linind_gauss     += 1.0f;
   igauss.y          = round( (linind_gauss - fmod( linind_gauss, gstrwidth))/gstrwidth); 
	igauss.x          = linind_gauss - igauss.y * gstrwidth;
	fg2               = fgauss[ igauss ];
Mark Friedrichs's avatar
Mark Friedrichs committed
195
			
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	v.x               = pc1*posqp.x;
	v.y               = pc1*posqp.y;
	v.z               = pc1*posqp.z;

	//Xmh               = sd1V * pc2 + sdpc.x * fg1;
	Xmh.x             = pc2*sd1V.x + sdpc.x*fg1.x;
	Xmh.y             = pc2*sd1V.y + sdpc.x*fg1.y;
	Xmh.z             = pc2*sd1V.z + sdpc.x*fg1.z;

	//sd2X              = sdpc.y * fg2;
	sd2X.x            = sdpc.y*fg2.x;
	sd2X.y            = sdpc.y*fg2.y;
	sd2X.z            = sdpc.y*fg2.z;

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
210
	posqp2            = posqp + sd2X - Xmh;
Mark Friedrichs's avatar
Mark Friedrichs committed
211
}