-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAction_Closest.cpp
432 lines (390 loc) · 15.6 KB
/
Action_Closest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include <cmath>
#include <algorithm> // sort
#include <cfloat> // DBL_MAX
#include "Action_Closest.h"
#include "CpptrajStdio.h"
#include <cstdio>
#include <cuda_runtime_api.h>
#include <cuda.h>
// CONSTRUCTOR
Action_Closest::Action_Closest() :
closestWaters_(0),
firstAtom_(false),
useMaskCenter_(false),
NsolventMolecules_(0),
debug_(0)
{}
void Action_Closest::Help() const {
mprintf("\t<# to keep> <mask> [noimage] [first | oxygen] [center]\n"
// "\t[closestout <filename> [name <setname>]] [outprefix <parmprefix>]\n"
// "\t[parmout <file>]\n"
" Keep only the closest <# to keep> solvent molecules to atoms in <mask>.\n"
" Molecules can be marked as solvent with the 'solvent' command.\n"
" If 'center' specified use geometric center of atoms in <mask>.\n");
}
// Action_Closest::Init()
Action_Closest::RetType Action_Closest::Init(ArgList& actionArgs, int debugIn)
{
debug_ = debugIn;
// Get Keywords
closestWaters_ = actionArgs.getNextInteger(-1);
if (closestWaters_ < 0) {
mprinterr("Error: Invalid # solvent molecules to keep (%i).\n",
closestWaters_);
return Action_Closest::ERR;
}
if ( actionArgs.hasKey("oxygen") || actionArgs.hasKey("first") )
firstAtom_=true;
useMaskCenter_ = actionArgs.hasKey("center");
image_.InitImaging( !(actionArgs.hasKey("noimage")) );
// Get Masks
std::string mask1 = actionArgs.GetMaskNext();
if (mask1.empty()) {
mprinterr("Error: No mask specified.\n");
return Action_Closest::ERR;
}
distanceMask_.SetMaskString(mask1);
mprintf(" CLOSEST: Finding closest %i solvent molecules to atoms in mask %s\n",
closestWaters_, distanceMask_.MaskString());
if (useMaskCenter_)
mprintf("\tGeometric center of atoms in mask will be used.\n");
if (!image_.UseImage())
mprintf("\tImaging will be turned off.\n");
if (firstAtom_)
mprintf("\tOnly first atom of solvent molecule used for distance calc.\n");
return Action_Closest::OK;
}
// Action_Closest::Setup()
/** Like the strip action, closest will modify the current parm keeping info
* for atoms in mask plus the closestWaters solvent molecules. Set up the
* vector of MolDist objects, one for every solvent molecule in the original
* parm file. Atom masks for each solvent molecule will be set up.
*/
Action_Closest::RetType Action_Closest::Setup(Topology const& topIn, CoordinateInfo const& cInfoIn)
{
// If there are no solvent molecules this action is not valid.
if (topIn.Nsolvent()==0) {
mprintf("Warning: Parm %s does not contain solvent.\n",topIn.c_str());
return Action_Closest::SKIP;
}
// If # solvent to keep >= solvent in this parm the action is not valid.
if (closestWaters_ >= topIn.Nsolvent()) {
mprintf("Warning: # solvent to keep (%i) >= # solvent molecules in '%s' (%i)\n",
closestWaters_, topIn.c_str(), topIn.Nsolvent());
return Action_Closest::SKIP;
}
image_.SetupImaging( cInfoIn.TrajBox().Type() );
if (image_.ImagingEnabled())
mprintf("\tDistances will be imaged.\n");
else
mprintf("\tImaging off.\n");
// LOOP OVER MOLECULES
// 1: Check that all solvent molecules contain same # atoms. Solvent
// molecules must be identical for the command to work properly;
// the prmtop strip occurs only once so the solvent params become fixed.
// 2: Set up a mask for all solvent molecules.
SolventMols_.clear();
// NOTE: May not be necessary to init 'solvent'
MolDist solvent;
solvent.D = 0.0;
solvent.mol = 0;
SolventMols_.resize(topIn.Nsolvent(), solvent);
std::vector<MolDist>::iterator mdist = SolventMols_.begin();
// 3: Set up the soluteMask for all non-solvent molecules.
int molnum = 1;
int nclosest = 0;
int NsolventAtoms = -1;
for (Topology::mol_iterator Mol = topIn.MolStart();
Mol != topIn.MolEnd(); ++Mol)
{
if ( Mol->IsSolvent() ) {
// Solvent, check for same # of atoms.
if (NsolventAtoms == -1)
NsolventAtoms = Mol->NumAtoms();
else if ( NsolventAtoms != Mol->NumAtoms() ) {
mprinterr("Error: Solvent molecules in '%s' are not of uniform size.\n"
"Error: First solvent mol = %i atoms, solvent mol %i = %i atoms.\n",
topIn.c_str(), NsolventAtoms, molnum, (*Mol).NumAtoms());
return Action_Closest::ERR;
}
// mol here is the output molecule number which is why it starts from 1.
mdist->mol = molnum;
// Solvent molecule mask
mdist->mask.AddAtomRange( Mol->BeginAtom(), Mol->EndAtom() );
// Atoms in the solvent molecule to actually calculate distances to.
if (firstAtom_) {
mdist->solventAtoms.assign(1, Mol->BeginAtom() );
} else {
mdist->solventAtoms.clear();
mdist->solventAtoms.reserve( Mol->NumAtoms() );
for (int svatom = Mol->BeginAtom(); svatom < Mol->EndAtom(); svatom++)
mdist->solventAtoms.push_back( svatom );
}
if (debug_ > 0) {
mprintf("DEBUG:\tSet up mol %i:", mdist->mol); // DEBUG
mdist->mask.PrintMaskAtoms("solvent"); // DEBUG
mprintf("\n"); // DEBUG
}
++mdist;
}
++molnum;
}
// Setup distance atom mask
// NOTE: Should ensure that no solvent atoms are selected!
if ( topIn.SetupIntegerMask(distanceMask_) ) return Action_Closest::ERR;
if (distanceMask_.None()) {
mprintf("Warning: Distance mask '%s' contains no atoms.\n",
distanceMask_.MaskString());
return Action_Closest::SKIP;
}
distanceMask_.MaskInfo();
// Check the total number of solvent atoms to be kept.
NsolventAtoms *= closestWaters_;
mprintf("\tKeeping %i solvent atoms.\n",NsolventAtoms);
if (NsolventAtoms < 1) {
mprintf("Warning: # of solvent atoms to be kept is < 1.\n");
return Action_Closest::SKIP;
}
NsolventMolecules_ = (int)SolventMols_.size();
return Action_Closest::OK;
}
// Action_Closest::DoAction()
/** Find the minimum distance between atoms in distanceMask and each
* solvent Mask.
*/
Action_Closest::RetType Action_Closest::DoAction(int frameNum, Frame& frmIn) {
double maxD;
Matrix_3x3 ucell, recip;
AtomMask::const_iterator solute_atom;
Iarray::const_iterator solvent_atom;
if (image_.ImagingEnabled()) {
frmIn.BoxCrd().ToRecip(ucell, recip);
// Calculate max possible imaged distance
maxD = frmIn.BoxCrd().BoxX() + frmIn.BoxCrd().BoxY() +
frmIn.BoxCrd().BoxZ();
maxD *= maxD;
} else {
// If not imaging, set max distance to an arbitrarily large number
maxD = DBL_MAX;
}
//subroutines to find the distance
// if (image_.ImageType() == NOIMAGE)
// Action_NoImage(frmIn,maxD);
// else if (image_.ImageType() == ORTHO)
// Action_ImageOrtho(frmIn,maxD);
// else
// Action_ImageNonOrtho(frmIn,maxD, ucell,recip);
//remove this ..TODOi
useMaskCenter_ = false;
cudaEvent_t start_event, stop_event;
float elapsed_time_seq;
bool v[2] = { true, false };
int type = 2; //keep it no imaging (as of now)
char* dict[3] = {"NONE", "ORTHO", "NON-ORTHO"};
for (int type = 0; type < 3 ; type++){
printf("-------------------------------------------------------\n");
for(int k =0 ; k < 2 ; k++)
{
printf("Solute Center : %s\n", v[k] ? "YES" : "NO");
printf("Imaging : %s\n", dict[type]);
useMaskCenter_ = v[k];
cudaEventCreate(&start_event);
cudaEventCreate(&stop_event);
cudaEventRecord(start_event, 0);
//serial section of the code
if (type == 0 )
Action_NoImage(frmIn,maxD);
else if (type == 1)
Action_ImageOrtho(frmIn,maxD);
else if (type == 2)
Action_ImageNonOrtho(frmIn, maxD, ucell, recip);
else{
printf("Error, invalid imaging type\n");
exit(1);
}
cudaThreadSynchronize();
cudaEventRecord(stop_event, 0);
cudaEventSynchronize(stop_event);
cudaEventElapsedTime(&elapsed_time_seq,start_event, stop_event );
printf("Done with kernel SEQ Kernel Time: %.2f\n", elapsed_time_seq);
bool result = true;
float elapsed_time_gpu;
if (useMaskCenter_)
result = cuda_action_center(frmIn,maxD,ucell ,recip ,type ,elapsed_time_gpu);
else
result = cuda_action_no_center(frmIn,maxD,ucell ,recip ,type ,elapsed_time_gpu);//handling all the data formatting and copying etc
// we will only care about kernel time
//fixing the overhead will be later
if(result){
printf("CUDA PASS\n");
}
else{
printf("CUDA FAIL!\n");
exit(0);
}
printf("->#Seq Time: = %0.2f\n", elapsed_time_seq);
printf("->#CUDA Time: = %0.2f\n", elapsed_time_gpu);
printf("->#Speedup = %0.2f\n", elapsed_time_seq/elapsed_time_gpu);
}
}
// Sort distances
std::sort( SolventMols_.begin(), SolventMols_.end(), moldist_cmp() );
// Add first closestWaters solvent atoms to stripMask
std::vector<MolDist>::iterator solventend = SolventMols_.begin() + closestWaters_;
for ( std::vector<MolDist>::const_iterator solvent = SolventMols_.begin();
solvent != solventend;
++solvent )
{
solvent_atom = solvent->mask.begin();
mprintf("\tMol= %8i Atom= %8i Dist= %10.4f\n", solvent->mol,
*solvent_atom + 1, sqrt( solvent->D ));
}
return Action_Closest::OK;
}
void Action_Closest::Action_ImageNonOrtho(Frame& frmIn, double maxD, Matrix_3x3 ucell, Matrix_3x3 recip)
{
double Dist;
int solventMol;
AtomMask::const_iterator solute_atom;
Iarray::const_iterator solvent_atom;
// Loop over all solvent molecules in original frame
if (useMaskCenter_) {
Vec3 maskCenter = frmIn.VGeometricCenter( distanceMask_ ); //can be calculated outside
for (solventMol=0; solventMol < NsolventMolecules_; solventMol++) {
SolventMols_[solventMol].D = maxD;
for (solvent_atom = SolventMols_[solventMol].solventAtoms.begin();
solvent_atom != SolventMols_[solventMol].solventAtoms.end(); ++solvent_atom)
{
Dist = DIST2_ImageNonOrtho( maskCenter.Dptr(),
frmIn.XYZ(*solvent_atom),ucell, recip); //frame translation can be done inside gpu
if (Dist < SolventMols_[solventMol].D)
SolventMols_[solventMol].D = Dist;
}
}
} else {
for (solventMol=0; solventMol < NsolventMolecules_; solventMol++) {
if (debug_ > 1)
mprintf("DEBUG: Calculating distance for molecule %i\n", solventMol);
// Set the initial minimum distance for this solvent mol to be the
// max possible distance.
SolventMols_[solventMol].D = maxD;
// Calculate distance between each atom in distanceMask and atoms in solvent Mask
for (solvent_atom = SolventMols_[solventMol].solventAtoms.begin();
solvent_atom != SolventMols_[solventMol].solventAtoms.end(); ++solvent_atom)
{
for (solute_atom = distanceMask_.begin();
solute_atom != distanceMask_.end(); ++solute_atom)
{
Dist = DIST2_ImageNonOrtho(frmIn.XYZ(*solute_atom),
frmIn.XYZ(*solvent_atom), ucell, recip);
if (Dist < SolventMols_[solventMol].D)
SolventMols_[solventMol].D = Dist;
if (debug_ > 2)
mprintf("DEBUG: SolvMol %i, soluteAtom %i, solventAtom %i, D= %f, minD= %f\n",
solventMol, *solute_atom, *solvent_atom, Dist,
sqrt(SolventMols_[solventMol].D));
}
}
if (debug_ > 1) mprintf("DEBUG:\tMol %8i minD= %lf\n",solventMol, SolventMols_[solventMol].D);
} // END for loop over solventMol
}
}
void Action_Closest::Action_ImageOrtho(Frame& frmIn, double maxD)
{
double Dist;
int solventMol;
AtomMask::const_iterator solute_atom;
Iarray::const_iterator solvent_atom;
// Loop over all solvent molecules in original frame
if (useMaskCenter_) {
Vec3 maskCenter = frmIn.VGeometricCenter( distanceMask_ );
for (solventMol=0; solventMol < NsolventMolecules_; solventMol++) {
SolventMols_[solventMol].D = maxD;
for (solvent_atom = SolventMols_[solventMol].solventAtoms.begin();
solvent_atom != SolventMols_[solventMol].solventAtoms.end(); ++solvent_atom)
{
Dist = DIST2_ImageOrtho( maskCenter.Dptr(),
frmIn.XYZ(*solvent_atom),frmIn.BoxCrd());
if (Dist < SolventMols_[solventMol].D)
SolventMols_[solventMol].D = Dist;
}
}
} else {
for (solventMol=0; solventMol < NsolventMolecules_; solventMol++) {
if (debug_ > 1)
mprintf("DEBUG: Calculating distance for molecule %i\n", solventMol);
// Set the initial minimum distance for this solvent mol to be the
// max possible distance.
SolventMols_[solventMol].D = maxD;
// Calculate distance between each atom in distanceMask and atoms in solvent Mask
for (solvent_atom = SolventMols_[solventMol].solventAtoms.begin();
solvent_atom != SolventMols_[solventMol].solventAtoms.end(); ++solvent_atom)
{
for (solute_atom = distanceMask_.begin();
solute_atom != distanceMask_.end(); ++solute_atom)
{
Dist = DIST2_ImageOrtho(frmIn.XYZ(*solute_atom),
frmIn.XYZ(*solvent_atom), frmIn.BoxCrd());
if (Dist < SolventMols_[solventMol].D)
SolventMols_[solventMol].D = Dist;
if (debug_ > 2)
mprintf("DEBUG: SolvMol %i, soluteAtom %i, solventAtom %i, D= %f, minD= %f\n",
solventMol, *solute_atom, *solvent_atom, Dist,
sqrt(SolventMols_[solventMol].D));
}
}
if (debug_ > 1) mprintf("DEBUG:\tMol %8i minD= %lf\n",solventMol, SolventMols_[solventMol].D);
} // END for loop over solventMol
}
}
//pulling out the dist control statement
void Action_Closest::Action_NoImage(Frame& frmIn,double maxD)
{
double Dist;
int solventMol;
AtomMask::const_iterator solute_atom;
Iarray::const_iterator solvent_atom;
// Loop over all solvent molecules in original frame
if (useMaskCenter_) {
Vec3 maskCenter = frmIn.VGeometricCenter( distanceMask_ );
for (solventMol=0; solventMol < NsolventMolecules_; solventMol++) {
SolventMols_[solventMol].D = maxD;
for (solvent_atom = SolventMols_[solventMol].solventAtoms.begin();
solvent_atom != SolventMols_[solventMol].solventAtoms.end(); ++solvent_atom)
{
Dist = DIST2_NoImage( maskCenter.Dptr(),
frmIn.XYZ(*solvent_atom));
//printf("DIST = %f\n", Dist);
if (Dist < SolventMols_[solventMol].D)
SolventMols_[solventMol].D = Dist;
}
}
} else {
for (solventMol=0; solventMol < NsolventMolecules_; solventMol++) {
if (debug_ > 1)
mprintf("DEBUG: Calculating distance for molecule %i\n", solventMol);
// Set the initial minimum distance for this solvent mol to be the
// max possible distance.
SolventMols_[solventMol].D = maxD;
// Calculate distance between each atom in distanceMask and atoms in solvent Mask
for (solvent_atom = SolventMols_[solventMol].solventAtoms.begin();
solvent_atom != SolventMols_[solventMol].solventAtoms.end(); ++solvent_atom)
{
for (solute_atom = distanceMask_.begin();
solute_atom != distanceMask_.end(); ++solute_atom)
{
Dist = DIST2_NoImage(frmIn.XYZ(*solute_atom),
frmIn.XYZ(*solvent_atom));
//printf("no center DIST = %f\n", Dist);
if (Dist < SolventMols_[solventMol].D)
SolventMols_[solventMol].D = Dist;
if (debug_ > 2)
mprintf("DEBUG: SolvMol %i, soluteAtom %i, solventAtom %i, D= %f, minD= %f\n",
solventMol, *solute_atom, *solvent_atom, Dist,
sqrt(SolventMols_[solventMol].D));
}
}
if (debug_ > 1) mprintf("DEBUG:\tMol %8i minD= %lf\n",solventMol, SolventMols_[solventMol].D);
} // END for loop over solventMol
}
}