NAMD
structures.h
Go to the documentation of this file.
1 
7 #ifndef STRUCTURES_H
8 #define STRUCTURES_H
9 
10 #include "common.h"
11 #include <vector>
12 
13 
14 // status elements, used for Atom status
15 #define UnknownAtom 0x00
16 #define HydrogenAtom 0x01
17 #define OxygenAtom 0x02
18 #define HBDonorAtom 0x04
19 #define HBAcceptorAtom 0x08
20 #define HBAntecedentAtom 0x10
21 #define HBHydrogenAtom 0x20
22 #define LonepairAtom 0x40
23 #define DrudeAtom 0x80
24 
25 
26 typedef int Index; // Used for index into arrays
27  // or parameters
28 typedef struct atom_name_info
29 {
30  char *resname;
31  char *atomname;
32  char *atomtype;
33 } AtomNameInfo;
34 
35 typedef struct atom_constants
36 {
40  union{
42  int32 status; // flags telling about this atom
43  };
44  int32 partner; // connecting atom, for hydrogens
45  int32 hydrogenList; // index of atom in hydrogenGroup list
46 } Atom;
47 
48 typedef struct bond
49 {
53 } Bond;
54 
55 typedef struct angle
56 {
61 } Angle;
62 
63 typedef struct dihedral
64 {
70 } Dihedral;
71 
72 typedef struct improper
73 {
79 } Improper;
80 
81 typedef struct crossterm
82 {
92 } Crossterm;
93 
94 typedef struct gromacsPair
95 {
101 } GromacsPair;
102 
103 
104 // DRUDE: data read from PSF
105 typedef struct drude_constants // supplement Atom data
106 { // create array length N of these
109 } DrudeConst;
110 
120 typedef struct lphost
121 {
130 } Lphost;
131 
132 typedef struct aniso // anisotropic term
133 {
141 } Aniso;
142 
143 typedef struct thole // Thole terms - constructed implicitly from exclusions
144 {
145  int32 atom1; // first heavy atom
146  int32 atom2; // Drude particle of first heavy atom
147  int32 atom3; // second heavy atom
148  int32 atom4; // Drude particle of second heavy atom
149  Real aa; // constant (alpha_i * alpha_j)^(-6) / (thole_i + thole_j)
150  Real qq; // combined charge of Drudes (C * q_{i+1} * q_{j+1})
151 } Thole;
152 
154  int32 atom1; // first heavy atom
155  int32 atom2; // Drude particle of first heavy atom
156  int32 atom3; // second heavy atom
157  int32 atom4; // Drude particle of second heavy atom
158  Real aa; // constant (alpha_i * alpha_j)^(-6) / (thole_i + thole_j)
159 };
160 // DRUDE
161 
162 
164 {
165 public:
166  Exclusion(void) : modified(0) {;}
167  Exclusion(int a1, int a2, int mod = 0) :
168  atom1(a1), atom2(a2), modified(mod) {;}
172  int hash(void) const
173  {
174  return atom1 + atom2;
175  }
176  int operator==(const Exclusion &o) const
177  {
178  return atom1 == o.atom1 && atom2 == o.atom2;
179  }
180  int operator<(const Exclusion &o) const
181  {
182  return
183  (
184  ( atom1 < o.atom1 ) ||
185  ( atom1 == o.atom1 && atom2 < o.atom2 )
186  );
187  }
188 };
189 
190 
191 class MOStream;
192 class MIStream;
193 
195 
196 inline unsigned int circShift(unsigned int h, unsigned int by) {
197  const unsigned int intBits=8*sizeof(unsigned int);
198  by%=intBits;
199  return (h<<by)|(h>>(intBits-by));
200 }
201 
203 public:
204  //This field indicates which class this tuple belongs to (bond or angle or ...)
207  //the relative offset from the atom index
208  int *offset;
209 
210  //This type is determined by the Parameter object
211  //for Exclusion, this indicates the modified field
213 
214  //indicate this tuple is specified in the psf file, not from other files.
215  //this is added due to the extraBonds feature. All tuples from extraBonds
216  //are not real!
217  char isReal;
218 
219 public:
221  offset = NULL;
222  isReal = 1;
223  }
224 
225  TupleSignature(int n, TupleSigType t, Index paramType, char ir=1){
226  tupleType = t;
227  numOffset = n;
228  offset = new int[n];
229  tupleParamType = paramType;
230  isReal = ir;
231  }
233  tupleType = oneSig.tupleType;
234  numOffset = oneSig.numOffset;
235  offset = new int[numOffset];
236  setOffsets(oneSig.offset);
238  isReal = oneSig.isReal;
239  }
241  tupleType = oneSig.tupleType;
242  numOffset = oneSig.numOffset;
243  if(offset) delete [] offset;
244  offset = new int[numOffset];
245  setOffsets(oneSig.offset);
247  isReal = oneSig.isReal;
248  return *this;
249  }
250  int operator==(const TupleSignature& sig) const{
251  if(tupleType!=sig.tupleType)
252  return 0;
253 
254  if(tupleParamType != sig.tupleParamType)
255  return 0;
256 
257  if(isReal != sig.isReal)
258  return 0;
259 
260  if(numOffset != sig.numOffset) return 0;
261 
262  int equalCnt=0;
263 
264  for(int i=0; i<numOffset; i++){
265  equalCnt += (offset[i]==sig.offset[i]);
266  }
267  return equalCnt==numOffset;
268  }
270  if(offset) delete[] offset;
271  }
272  void setOffsets(int *offs){
273  for(int i=0; i<numOffset; i++)
274  offset[i] = offs[i];
275  //sort the offset in increasing order
276  //based on the input files, this offset is almost sorted increasingly
277  //Therefore using insertion sort
278  /*switch(numOffset){
279  case 1:
280  break;
281  case 2:
282  if(offset[0]>offset[1]){
283  int tmp = offset[0];
284  offset[0] = offset[1];
285  offset[1] = tmp;
286  }
287  break;
288  default: //insertion sort
289  for(int ii=1; ii<numOffset; ii++){
290  int val = offset[ii];
291  for(int jj=ii-1; jj>=0; jj--){
292  if(offset[jj]<val){
293  offset[jj+1] = offset[jj];
294  offset[jj] = val;
295  }else
296  break;
297  }
298  }
299  }*/
300  }
301  void setEmpty(){
302  delete [] offset;
303  offset = NULL;
304  }
305  int isEmpty(){
306  return offset==NULL;
307  }
308  void output(FILE *ofp){
309  for(int i=0; i<numOffset; i++)
310  fprintf(ofp, "%d ", offset[i]);
311  fprintf(ofp, "| %d | %d\n", tupleParamType, isReal);
312  }
313 
314  int hash() const {
315  unsigned int code = tupleType;
316  unsigned int codesz = 8 * sizeof(int);
317  unsigned int shift = codesz / numOffset;
318 
319  if (shift == 0) shift=1;
320  unsigned int i;
321  for(i=0; i < numOffset; i++) {
322  code = circShift(code,shift);
323  code ^= offset[i];
324  }
325  return code;
326  }
327 
328  void pack(MOStream *msg);
329  void unpack(MIStream *msg);
330 };
331 
332 //represents the signatures for atoms
334 public:
335  int bondCnt;
336  int angleCnt;
340  // JLai
342 
348  // JLai
350 
353  // JLai
354  gromacsPairCnt=0;
355  bondSigs = NULL;
356  angleSigs = NULL;
357  dihedralSigs = NULL;
358  improperSigs = NULL;
359  crosstermSigs = NULL;
360  // JLai
361  gromacsPairSigs = NULL;
362  }
364  bondSigs = NULL;
365  angleSigs = NULL;
366  dihedralSigs = NULL;
367  improperSigs = NULL;
368  crosstermSigs = NULL;
369  // JLai
370  gromacsPairSigs = NULL;
371 
372  bondCnt = sig.bondCnt;
373  if(bondCnt>0){
375  for(int i=0; i<bondCnt; i++)
376  bondSigs[i] = sig.bondSigs[i];
377  }
378 
379  angleCnt = sig.angleCnt;
380  if(angleCnt>0){
382  for(int i=0; i<angleCnt; i++)
383  angleSigs[i] = sig.angleSigs[i];
384  }
385 
386  dihedralCnt = sig.dihedralCnt;
387  if(dihedralCnt>0){
389  for(int i=0; i<dihedralCnt; i++)
390  dihedralSigs[i] = sig.dihedralSigs[i];
391  }
392 
393  improperCnt = sig.improperCnt;
394  if(improperCnt>0){
396  for(int i=0; i<improperCnt; i++)
397  improperSigs[i] = sig.improperSigs[i];
398  }
399 
401  if(crosstermCnt>0){
403  for(int i=0; i<crosstermCnt; i++)
404  crosstermSigs[i] = sig.crosstermSigs[i];
405  }
406 
407  // JLai
409  if(gromacsPairCnt>0){
411  for(int i=0; i<gromacsPairCnt; i++) {
412  gromacsPairSigs[i] = sig.gromacsPairSigs[i];
413  }
414  }
415  }
417  bondCnt = sig.bondCnt;
418  if(bondSigs) delete [] bondSigs;
419  if(bondCnt>0){
421  for(int i=0; i<bondCnt; i++)
422  bondSigs[i] = sig.bondSigs[i];
423  }else
424  bondSigs = NULL;
425 
426  angleCnt = sig.angleCnt;
427  if(angleSigs) delete [] angleSigs;
428  if(angleCnt>0){
430  for(int i=0; i<angleCnt; i++)
431  angleSigs[i] = sig.angleSigs[i];
432  }else
433  angleSigs = NULL;
434 
435  dihedralCnt = sig.dihedralCnt;
436  if(dihedralSigs) delete [] dihedralSigs;
437  if(dihedralCnt>0){
439  for(int i=0; i<dihedralCnt; i++)
440  dihedralSigs[i] = sig.dihedralSigs[i];
441  }else
442  dihedralSigs = NULL;
443 
444  improperCnt = sig.improperCnt;
445  if(improperSigs) delete [] improperSigs;
446  if(improperCnt>0){
448  for(int i=0; i<improperCnt; i++)
449  improperSigs[i] = sig.improperSigs[i];
450  }else
451  improperSigs = NULL;
452 
454  if(crosstermSigs) delete [] crosstermSigs;
455  if(crosstermCnt>0){
457  for(int i=0; i<crosstermCnt; i++)
458  crosstermSigs[i] = sig.crosstermSigs[i];
459  }else
460  crosstermSigs = NULL;
461 
462  // JLai
464  if(gromacsPairSigs) delete [] gromacsPairSigs;
465  if(gromacsPairCnt>0){
467  for(int i=0; i<gromacsPairCnt; i++)
468  gromacsPairSigs[i] = sig.gromacsPairSigs[i];
469  }else
470  gromacsPairSigs = NULL;
471 
472  return *this;
473  }
474  int operator==(const AtomSignature& sig) const{
475  if(bondCnt!=sig.bondCnt) return 0;
476  if(angleCnt!=sig.angleCnt) return 0;
477  if(dihedralCnt!=sig.dihedralCnt) return 0;
478  if(improperCnt!=sig.improperCnt) return 0;
479  if(crosstermCnt!=sig.crosstermCnt) return 0;
480  //JLai
481  if(gromacsPairCnt!=sig.gromacsPairCnt) return 0;
482 
483  #define CMPSIGS(TUPLE) \
484  for(int i=0; i<sig.TUPLE##Cnt; i++){ \
485  if(!(TUPLE##Sigs[i]==sig.TUPLE##Sigs[i])) return 0; \
486  } \
487 
488  CMPSIGS(bond)
489  CMPSIGS(angle)
493  // JLai
495 
496  return 1;
497  }
499  if(bondSigs) delete[] bondSigs;
500  if(angleSigs) delete[] angleSigs;
501  if(dihedralSigs) delete[] dihedralSigs;
502  if(improperSigs) delete[] improperSigs;
503  if(crosstermSigs) delete[] crosstermSigs;
504  // JLai
505  if(gromacsPairSigs) delete[] gromacsPairSigs;
506  }
507 
508  void removeEmptyTupleSigs();
509  void pack(MOStream *msg);
510  void unpack(MIStream *msg);
511 };
512 
513 struct AtomNameIdx{
517 };
518 struct AtomCstInfo{
520  union{
522  int32 status; // flags telling about this atom
523  };
526 };
527 
529  int fullExclCnt; //1-2, 1-3 exclusion
530  int *fullOffset; //should be in increasing order
531  int modExclCnt; //1-4 exclusion
532  int *modOffset; //should be in increasing order
533 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
536 #endif
537 
539  fullExclCnt = modExclCnt = 0;
540  fullOffset = modOffset = NULL;
541 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
542  allExclCnt = 0;
543  allTuples = NULL;
544 #endif
545  }
547  fullOffset = modOffset = NULL;
548  fullExclCnt = sig.fullExclCnt;
549  if(fullExclCnt>0){
550  fullOffset = new int[fullExclCnt];
551  for(int i=0; i<fullExclCnt; i++)
552  fullOffset[i] = sig.fullOffset[i];
553  }
554 
555  modExclCnt = sig.modExclCnt;
556  if(modExclCnt>0){
557  modOffset = new int[modExclCnt];
558  for(int i=0; i<modExclCnt; i++)
559  modOffset[i] = sig.modOffset[i];
560  }
561 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
562  allTuples = NULL;
563  allExclCnt = sig.allExclCnt;
564  if(allExclCnt>0){
566  for(int i=0; i<allExclCnt; i++)
567  allTuples[i] = sig.allTuples[i];
568  }
569 #endif
570  }
572  if(fullOffset) delete [] fullOffset;
573  if(modOffset) delete [] modOffset;
574 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
575  if(allTuples) delete [] allTuples;
576 #endif
577  }
578 
580  fullExclCnt = sig.fullExclCnt;
581  if(fullOffset) delete [] fullOffset;
582  if(fullExclCnt>0){
583  fullOffset = new int[fullExclCnt];
584  for(int i=0; i<fullExclCnt; i++)
585  fullOffset[i] = sig.fullOffset[i];
586  }else
587  fullOffset = NULL;
588 
589  modExclCnt = sig.modExclCnt;
590  if(modOffset) delete [] modOffset;
591  if(modExclCnt>0){
592  modOffset = new int[modExclCnt];
593  for(int i=0; i<modExclCnt; i++)
594  modOffset[i] = sig.modOffset[i];
595  }else
596  modOffset = NULL;
597 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
598  allExclCnt = sig.allExclCnt;
599  if(allTuples) delete [] allTuples;
600  if(allExclCnt>0){
602  for(int i=0; i<allExclCnt; i++)
603  allTuples[i] = sig.allTuples[i];
604  }else
605  allTuples = NULL;
606 #endif
607 
608  return *this;
609  }
610  int operator==(const ExclusionSignature& sig) const{
611  if(fullExclCnt!=sig.fullExclCnt) return 0;
612  if(modExclCnt!=sig.modExclCnt) return 0;
613 
614  for(int i=0; i<fullExclCnt; i++){
615  if(fullOffset[i]!=sig.fullOffset[i]) return 0;
616  }
617  for(int i=0; i<modExclCnt; i++){
618  if(modOffset[i]!=sig.modOffset[i]) return 0;
619  }
620  return 1;
621  }
622  //both input should be sorted in increasing order
623  void setOffsets(std::vector<int>& fullVec, std::vector<int>& modVec){
624  fullExclCnt = fullVec.size();
625  modExclCnt = modVec.size();
626  if(fullExclCnt>0) {
627  fullOffset = new int[fullExclCnt];
628  for(int i=0; i<fullExclCnt; i++)
629  fullOffset[i] = fullVec[i];
630  }
631 
632  if(modExclCnt>0) {
633  modOffset = new int[modExclCnt];
634  for(int i=0; i<modExclCnt; i++)
635  modOffset[i] = modVec[i];
636  }
637 #if defined(NAMD_CUDA) || defined(NAMD_HIP)
638  buildTuples();
639  }
640  void buildTuples() {
641  delete [] allTuples;
642  allTuples = NULL;
643  allExclCnt = 0;
644  for(int i=0; i<fullExclCnt; i++)
645  if ( fullOffset[i] > 0 ) ++allExclCnt;
646  for(int i=0; i<modExclCnt; i++)
647  if ( modOffset[i] > 0 ) ++allExclCnt;
648  if(allExclCnt>0){
650  int j = 0;
651  for(int i=0; i<fullExclCnt; i++){
652  if ( fullOffset[i] <= 0 ) continue;
653  TupleSignature oneSig(1,EXCLUSION,0);
654  oneSig.offset[0] = fullOffset[i];
655  allTuples[j++] = oneSig;
656  }
657  for(int i=0; i<modExclCnt; i++){
658  if ( modOffset[i] <= 0 ) continue;
659  TupleSignature oneSig(1,EXCLUSION,1);
660  oneSig.offset[0] = modOffset[i];
661  allTuples[j++] = oneSig;
662  }
663  }
664 #endif
665  }
666 
667  int hash() const {
668  unsigned int numOffset = fullExclCnt + modExclCnt;
669  unsigned int code = 0x12345678;
670  unsigned int codesz = 8 * sizeof(int);
671  unsigned int shift = codesz / numOffset;
672 
673  if (shift == 0) shift=1;
674  unsigned int i;
675  for(i=0; i < fullExclCnt; i++) {
676  code = circShift(code,shift);
677  code ^= fullOffset[i];
678  }
679  for(i=0; i < modExclCnt; i++) {
680  code = circShift(code,shift);
681  code ^= modOffset[i];
682  }
683  return code;
684  }
685 
686  void removeEmptyOffset();
687  //assuming offsets in the signature have been sorted increasingly
688  int findOffset(int offset, int *fullOrMod);
689  void pack(MOStream *msg);
690  void unpack(MIStream *msg);
691 };
692 
693 #endif
694 
struct angle Angle
int32 atom3
Definition: structures.h:147
int operator==(const TupleSignature &sig) const
Definition: structures.h:250
TupleSignature(const TupleSignature &oneSig)
Definition: structures.h:232
PerAtomFlags flags
Definition: structures.h:521
struct lphost Lphost
Index crossterm_type
Definition: structures.h:91
struct atom_name_info AtomNameInfo
int32 atom4
Definition: structures.h:77
Index improper_type
Definition: structures.h:78
Real k11
Definition: structures.h:138
int32 atom2
Definition: structures.h:170
int32 atom2
Definition: structures.h:135
int32 atom2
Definition: structures.h:123
void pack(MOStream *msg)
int32 atom3
Definition: structures.h:136
int32 atom3
Definition: structures.h:67
TupleSignature * improperSigs
Definition: structures.h:346
int32 numhosts
Either 2 or 3 host atoms, depending on LP type.
Definition: structures.h:126
TupleSigType
Definition: structures.h:194
struct dihedral Dihedral
Real pairC6
Definition: structures.h:99
void unpack(MIStream *msg)
struct bond Bond
TupleSignature * dihedralSigs
Definition: structures.h:345
float Real
Definition: common.h:118
int32_t int32
Definition: common.h:38
int32 atom5
Definition: structures.h:87
Real dihedral
Definition: structures.h:129
Real distance
Definition: structures.h:127
int32 atom3
Definition: structures.h:124
struct crossterm Crossterm
int32 atom8
Definition: structures.h:90
ExclusionSignature(const ExclusionSignature &sig)
Definition: structures.h:546
TupleSignature * crosstermSigs
Definition: structures.h:347
int32 atom1
Definition: structures.h:83
int hash() const
Definition: structures.h:667
void unpack(MIStream *msg)
int32 atom4
Definition: structures.h:86
int32 atom4
Definition: structures.h:148
Real pairC12
Definition: structures.h:98
TupleSignature * allTuples
Definition: structures.h:535
ExclusionSignature & operator=(const ExclusionSignature &sig)
Definition: structures.h:579
int32 atom3
Definition: structures.h:85
int32 atom2
Definition: structures.h:97
int hash(void) const
Definition: structures.h:172
struct atom_constants Atom
int32 atom4
Definition: structures.h:125
int32 atom4
Definition: structures.h:68
int32 atom1
Definition: structures.h:50
int Index
Definition: structures.h:26
TupleSignature(int n, TupleSigType t, Index paramType, char ir=1)
Definition: structures.h:225
TupleSignature * gromacsPairSigs
Definition: structures.h:349
int32 atom1
First index is to the LP.
Definition: structures.h:122
void setOffsets(int *offs)
Definition: structures.h:272
void pack(MOStream *msg)
Index modified
Definition: structures.h:171
int32 atom2
Definition: structures.h:146
struct thole Thole
int operator==(const Exclusion &o) const
Definition: structures.h:176
int32 atom2
Definition: structures.h:84
Index resnameIdx
Definition: structures.h:514
Index dihedral_type
Definition: structures.h:69
int32 atom2
Definition: structures.h:58
int32 atom7
Definition: structures.h:89
char * resname
Definition: structures.h:30
int operator==(const ExclusionSignature &sig) const
Definition: structures.h:610
int findOffset(int offset, int *fullOrMod)
char * atomtype
Definition: structures.h:32
AtomSignature & operator=(const AtomSignature &sig)
Definition: structures.h:416
int32 atom1
Definition: structures.h:57
TupleSigType tupleType
Definition: structures.h:205
int32 atom1
Definition: structures.h:145
int32 partner
Definition: structures.h:524
unsigned int circShift(unsigned int h, unsigned int by)
Definition: structures.h:196
int32 atom3
Definition: structures.h:59
int32 hydrogenList
Definition: structures.h:45
void setEmpty()
Definition: structures.h:301
void removeEmptyTupleSigs()
TupleSignature * bondSigs
Definition: structures.h:343
PerAtomFlags flags
Definition: structures.h:41
int32 atom1
Definition: structures.h:169
#define CMPSIGS(TUPLE)
Index angle_type
Definition: structures.h:60
Index vdw_type
Definition: structures.h:519
Real k22
Definition: structures.h:139
Real qq
Definition: structures.h:150
int32 atom2
Definition: structures.h:66
AtomSignature(const AtomSignature &sig)
Definition: structures.h:363
Real aa
Definition: structures.h:149
Real angle
Definition: structures.h:128
int32 atom1
Definition: structures.h:74
struct aniso Aniso
Real k33
Definition: structures.h:140
Index vdw_type
Definition: structures.h:39
int32 atom1
Definition: structures.h:134
struct improper Improper
void setOffsets(std::vector< int > &fullVec, std::vector< int > &modVec)
Definition: structures.h:623
void output(FILE *ofp)
Definition: structures.h:308
Index tupleParamType
Definition: structures.h:212
int32 atom2
Definition: structures.h:75
int operator<(const Exclusion &o) const
Definition: structures.h:180
int32 atom6
Definition: structures.h:88
struct gromacsPair GromacsPair
int operator==(const AtomSignature &sig) const
Definition: structures.h:474
struct drude_constants DrudeConst
Exclusion(void)
Definition: structures.h:166
void unpack(MIStream *msg)
int hash() const
Definition: structures.h:314
Index gromacsPair_type
Definition: structures.h:100
void pack(MOStream *msg)
int32 status
Definition: structures.h:522
Index bond_type
Definition: structures.h:52
int32 atom4
Definition: structures.h:137
Index atomtypeIdx
Definition: structures.h:516
int32 hydrogenList
Definition: structures.h:525
int gromacsPairCnt
Definition: structures.h:341
Exclusion(int a1, int a2, int mod=0)
Definition: structures.h:167
int32 atom3
Definition: structures.h:76
TupleSignature * angleSigs
Definition: structures.h:344
int32 atom1
Definition: structures.h:65
TupleSignature & operator=(const TupleSignature &oneSig)
Definition: structures.h:240
int32 atom2
Definition: structures.h:51
Index atomnameIdx
Definition: structures.h:515
char * atomname
Definition: structures.h:31
int32 atom1
Definition: structures.h:96