kupdatesd.br 7.99 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) 2008-2009 Stanford University and the Authors.      *
 * Authors: Peter Eastman, Mark Friedrichs, Chris Bruns, Mike Houston         *
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */
Mark Friedrichs's avatar
Mark Friedrichs committed
32

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//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
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
/* 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
103
		float3 posq<>,
Mark Friedrichs's avatar
Mark Friedrichs committed
104
105
106
107
108
109
		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
110
		out float3 posqp<>
Mark Friedrichs's avatar
Mark Friedrichs committed
111
		){
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
112
   float  c1;
Mark Friedrichs's avatar
Mark Friedrichs committed
113
114
115
116
117
	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
118
	igauss         = indexof( posq ).xy;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
119
	linind_gauss   = igauss.y * xstrwidth + igauss.x;
Mark Friedrichs's avatar
Mark Friedrichs committed
120

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
121
	//2 random vectors used in each fragment
Mark Friedrichs's avatar
Mark Friedrichs committed
122

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
123
	linind_gauss   = 2.0f * linind_gauss + goffset; 	
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
124
125
126
   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
127

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
128
129
130
131
	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
132

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
	//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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
}

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
175
176
		float3 posq<>,      //positions pre-update
		float3 posqp<>,     //deltas post constraint
Mark Friedrichs's avatar
Mark Friedrichs committed
177
178
179
		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
180
		out float3 posqp2<> //new deltas, need to be constrained again
Mark Friedrichs's avatar
Mark Friedrichs committed
181
182
183
184
185
186
187
		){

	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
188
	igauss            = indexof( posq ).xy;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
189
	linind_gauss      = igauss.y * xstrwidth + igauss.x;
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
190
	linind_gauss      = 2.0f * linind_gauss + goffset; 	
Mark Friedrichs's avatar
Mark Friedrichs committed
191
	
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
192
193
194
   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
195

Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
196
197
198
199
	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
200
			
Mark Friedrichs's avatar
Mods  
Mark Friedrichs committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
	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
215
	posqp2            = posqp + sd2X - Xmh;
Mark Friedrichs's avatar
Mark Friedrichs committed
216
}