00001
00007 #ifndef STRUCTURES_H
00008 #define STRUCTURES_H
00009
00010 #include "common.h"
00011 #include <vector>
00012
00013
00014
00015 #define UnknownAtom 0x00
00016 #define HydrogenAtom 0x01
00017 #define OxygenAtom 0x02
00018 #define HBDonorAtom 0x04
00019 #define HBAcceptorAtom 0x08
00020 #define HBAntecedentAtom 0x10
00021 #define HBHydrogenAtom 0x20
00022 #define LonepairAtom 0x40
00023 #define DrudeAtom 0x80
00024
00025
00026 typedef int Index;
00027
00028
00029 typedef struct atom_name_info
00030 {
00031 char *resname;
00032 char *atomname;
00033 char *atomtype;
00034 } AtomNameInfo;
00035
00036 typedef struct atom_constants
00037 {
00038 Real mass;
00039 Real charge;
00040 Index vdw_type;
00041 int32 status;
00042 int32 partner;
00043 int32 hydrogenList;
00044 } Atom;
00045
00046 typedef struct bond
00047 {
00048 int32 atom1;
00049 int32 atom2;
00050 Index bond_type;
00051 } Bond;
00052
00053 typedef struct angle
00054 {
00055 int32 atom1;
00056 int32 atom2;
00057 int32 atom3;
00058 Index angle_type;
00059 } Angle;
00060
00061 typedef struct dihedral
00062 {
00063 int32 atom1;
00064 int32 atom2;
00065 int32 atom3;
00066 int32 atom4;
00067 Index dihedral_type;
00068 } Dihedral;
00069
00070 typedef struct improper
00071 {
00072 int32 atom1;
00073 int32 atom2;
00074 int32 atom3;
00075 int32 atom4;
00076 Index improper_type;
00077 } Improper;
00078
00079 typedef struct crossterm
00080 {
00081 int32 atom1;
00082 int32 atom2;
00083 int32 atom3;
00084 int32 atom4;
00085 int32 atom5;
00086 int32 atom6;
00087 int32 atom7;
00088 int32 atom8;
00089 Index crossterm_type;
00090 } Crossterm;
00091
00092 typedef struct gromacsPair
00093 {
00094 int32 atom1;
00095 int32 atom2;
00096 Real pairC12;
00097 Real pairC6;
00098 Index gromacsPair_type;
00099 } GromacsPair;
00100
00101
00102
00103 typedef struct drude_constants
00104 {
00105 Real alpha;
00106 Real thole;
00107 } DrudeConst;
00108
00109 typedef struct lphost
00110 {
00111 int32 atom1;
00112 int32 atom2;
00113 int32 atom3;
00114 int32 atom4;
00115 Real distance;
00116 Real angle;
00117 Real dihedral;
00118 } Lphost;
00119
00120 typedef struct aniso
00121 {
00122 int32 atom1;
00123 int32 atom2;
00124 int32 atom3;
00125 int32 atom4;
00126 Real k11;
00127 Real k22;
00128 Real k33;
00129 } Aniso;
00130
00131 typedef struct thole
00132 {
00133 int32 atom1;
00134 int32 atom2;
00135 int32 atom3;
00136 int32 atom4;
00137 Real aa;
00138 Real qq;
00139 } Thole;
00140
00141
00142
00143 class Exclusion
00144 {
00145 public:
00146 Exclusion(void) : modified(0) {;}
00147 Exclusion(int a1, int a2, int mod = 0) :
00148 atom1(a1), atom2(a2), modified(mod) {;}
00149 int32 atom1;
00150 int32 atom2;
00151 Index modified;
00152 int hash(void) const
00153 {
00154 return atom1 + atom2;
00155 }
00156 int operator==(const Exclusion &o) const
00157 {
00158 return atom1 == o.atom1 && atom2 == o.atom2;
00159 }
00160 int operator<(const Exclusion &o) const
00161 {
00162 return
00163 (
00164 ( atom1 < o.atom1 ) ||
00165 ( atom1 == o.atom1 && atom2 < o.atom2 )
00166 );
00167 }
00168 };
00169
00170
00171 class MOStream;
00172 class MIStream;
00173
00174 enum TupleSigType {BOND=0, ANGLE, DIHEDRAL, IMPROPER, DONOR, ACCEPTOR, CROSSTERM, EXCLUSION};
00175
00176 inline unsigned int circShift(unsigned int h, unsigned int by) {
00177 const unsigned int intBits=8*sizeof(unsigned int);
00178 by%=intBits;
00179 return (h<<by)|(h>>(intBits-by));
00180 }
00181
00182 class TupleSignature{
00183 public:
00184
00185 TupleSigType tupleType;
00186 int numOffset;
00187
00188 int *offset;
00189
00190
00191
00192 Index tupleParamType;
00193
00194
00195
00196
00197 char isReal;
00198
00199 public:
00200 TupleSignature(){
00201 offset = NULL;
00202 isReal = 1;
00203 }
00204
00205 TupleSignature(int n, TupleSigType t, Index paramType, char ir=1){
00206 tupleType = t;
00207 numOffset = n;
00208 offset = new int[n];
00209 tupleParamType = paramType;
00210 isReal = ir;
00211 }
00212 TupleSignature(const TupleSignature& oneSig){
00213 tupleType = oneSig.tupleType;
00214 numOffset = oneSig.numOffset;
00215 offset = new int[numOffset];
00216 setOffsets(oneSig.offset);
00217 tupleParamType = oneSig.tupleParamType;
00218 isReal = oneSig.isReal;
00219 }
00220 TupleSignature &operator=(const TupleSignature& oneSig){
00221 tupleType = oneSig.tupleType;
00222 numOffset = oneSig.numOffset;
00223 if(offset) delete [] offset;
00224 offset = new int[numOffset];
00225 setOffsets(oneSig.offset);
00226 tupleParamType = oneSig.tupleParamType;
00227 isReal = oneSig.isReal;
00228 return *this;
00229 }
00230 int operator==(const TupleSignature& sig) const{
00231 if(tupleType!=sig.tupleType)
00232 return 0;
00233
00234 if(tupleParamType != sig.tupleParamType)
00235 return 0;
00236
00237 if(isReal != sig.isReal)
00238 return 0;
00239
00240 if(numOffset != sig.numOffset) return 0;
00241
00242 int equalCnt=0;
00243
00244 for(int i=0; i<numOffset; i++){
00245 equalCnt += (offset[i]==sig.offset[i]);
00246 }
00247 return equalCnt==numOffset;
00248 }
00249 ~TupleSignature(){
00250 if(offset) delete[] offset;
00251 }
00252 void setOffsets(int *offs){
00253 for(int i=0; i<numOffset; i++)
00254 offset[i] = offs[i];
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 }
00281 void setEmpty(){
00282 delete [] offset;
00283 offset = NULL;
00284 }
00285 int isEmpty(){
00286 return offset==NULL;
00287 }
00288 void output(FILE *ofp){
00289 for(int i=0; i<numOffset; i++)
00290 fprintf(ofp, "%d ", offset[i]);
00291 fprintf(ofp, "| %d | %d\n", tupleParamType, isReal);
00292 }
00293
00294 int hash() const {
00295 unsigned int code = tupleType;
00296 unsigned int codesz = 8 * sizeof(int);
00297 unsigned int shift = codesz / numOffset;
00298
00299 if (shift == 0) shift=1;
00300 unsigned int i;
00301 for(i=0; i < numOffset; i++) {
00302 code = circShift(code,shift);
00303 code ^= offset[i];
00304 }
00305 return code;
00306 }
00307
00308 void pack(MOStream *msg);
00309 void unpack(MIStream *msg);
00310 };
00311
00312
00313 class AtomSignature{
00314 public:
00315 int bondCnt;
00316 int angleCnt;
00317 int dihedralCnt;
00318 int improperCnt;
00319 int crosstermCnt;
00320
00321 int gromacsPairCnt;
00322
00323 TupleSignature *bondSigs;
00324 TupleSignature *angleSigs;
00325 TupleSignature *dihedralSigs;
00326 TupleSignature *improperSigs;
00327 TupleSignature *crosstermSigs;
00328
00329 TupleSignature *gromacsPairSigs;
00330
00331 AtomSignature(){
00332 bondCnt=angleCnt=dihedralCnt=improperCnt=crosstermCnt=0;
00333
00334 gromacsPairCnt=0;
00335 bondSigs = NULL;
00336 angleSigs = NULL;
00337 dihedralSigs = NULL;
00338 improperSigs = NULL;
00339 crosstermSigs = NULL;
00340
00341 gromacsPairSigs = NULL;
00342 }
00343 AtomSignature(const AtomSignature &sig){
00344 bondSigs = NULL;
00345 angleSigs = NULL;
00346 dihedralSigs = NULL;
00347 improperSigs = NULL;
00348 crosstermSigs = NULL;
00349
00350 gromacsPairSigs = NULL;
00351
00352 bondCnt = sig.bondCnt;
00353 if(bondCnt>0){
00354 bondSigs = new TupleSignature[bondCnt];
00355 for(int i=0; i<bondCnt; i++)
00356 bondSigs[i] = sig.bondSigs[i];
00357 }
00358
00359 angleCnt = sig.angleCnt;
00360 if(angleCnt>0){
00361 angleSigs = new TupleSignature[angleCnt];
00362 for(int i=0; i<angleCnt; i++)
00363 angleSigs[i] = sig.angleSigs[i];
00364 }
00365
00366 dihedralCnt = sig.dihedralCnt;
00367 if(dihedralCnt>0){
00368 dihedralSigs = new TupleSignature[dihedralCnt];
00369 for(int i=0; i<dihedralCnt; i++)
00370 dihedralSigs[i] = sig.dihedralSigs[i];
00371 }
00372
00373 improperCnt = sig.improperCnt;
00374 if(improperCnt>0){
00375 improperSigs = new TupleSignature[improperCnt];
00376 for(int i=0; i<improperCnt; i++)
00377 improperSigs[i] = sig.improperSigs[i];
00378 }
00379
00380 crosstermCnt = sig.crosstermCnt;
00381 if(crosstermCnt>0){
00382 crosstermSigs = new TupleSignature[crosstermCnt];
00383 for(int i=0; i<crosstermCnt; i++)
00384 crosstermSigs[i] = sig.crosstermSigs[i];
00385 }
00386
00387
00388 gromacsPairCnt = sig.gromacsPairCnt;
00389 if(gromacsPairCnt>0){
00390 gromacsPairSigs = new TupleSignature[gromacsPairCnt];
00391 for(int i=0; i<gromacsPairCnt; i++) {
00392 gromacsPairSigs[i] = sig.gromacsPairSigs[i];
00393 }
00394 }
00395 }
00396 AtomSignature& operator=(const AtomSignature& sig){
00397 bondCnt = sig.bondCnt;
00398 if(bondSigs) delete [] bondSigs;
00399 if(bondCnt>0){
00400 bondSigs = new TupleSignature[bondCnt];
00401 for(int i=0; i<bondCnt; i++)
00402 bondSigs[i] = sig.bondSigs[i];
00403 }else
00404 bondSigs = NULL;
00405
00406 angleCnt = sig.angleCnt;
00407 if(angleSigs) delete [] angleSigs;
00408 if(angleCnt>0){
00409 angleSigs = new TupleSignature[angleCnt];
00410 for(int i=0; i<angleCnt; i++)
00411 angleSigs[i] = sig.angleSigs[i];
00412 }else
00413 angleSigs = NULL;
00414
00415 dihedralCnt = sig.dihedralCnt;
00416 if(dihedralSigs) delete [] dihedralSigs;
00417 if(dihedralCnt>0){
00418 dihedralSigs = new TupleSignature[dihedralCnt];
00419 for(int i=0; i<dihedralCnt; i++)
00420 dihedralSigs[i] = sig.dihedralSigs[i];
00421 }else
00422 dihedralSigs = NULL;
00423
00424 improperCnt = sig.improperCnt;
00425 if(improperSigs) delete [] improperSigs;
00426 if(improperCnt>0){
00427 improperSigs = new TupleSignature[improperCnt];
00428 for(int i=0; i<improperCnt; i++)
00429 improperSigs[i] = sig.improperSigs[i];
00430 }else
00431 improperSigs = NULL;
00432
00433 crosstermCnt = sig.crosstermCnt;
00434 if(crosstermSigs) delete [] crosstermSigs;
00435 if(crosstermCnt>0){
00436 crosstermSigs = new TupleSignature[crosstermCnt];
00437 for(int i=0; i<crosstermCnt; i++)
00438 crosstermSigs[i] = sig.crosstermSigs[i];
00439 }else
00440 crosstermSigs = NULL;
00441
00442
00443 gromacsPairCnt = sig.gromacsPairCnt;
00444 if(gromacsPairSigs) delete [] gromacsPairSigs;
00445 if(gromacsPairCnt>0){
00446 gromacsPairSigs = new TupleSignature[gromacsPairCnt];
00447 for(int i=0; i<gromacsPairCnt; i++)
00448 gromacsPairSigs[i] = sig.gromacsPairSigs[i];
00449 }else
00450 gromacsPairSigs = NULL;
00451
00452 return *this;
00453 }
00454 int operator==(const AtomSignature& sig) const{
00455 if(bondCnt!=sig.bondCnt) return 0;
00456 if(angleCnt!=sig.angleCnt) return 0;
00457 if(dihedralCnt!=sig.dihedralCnt) return 0;
00458 if(improperCnt!=sig.improperCnt) return 0;
00459 if(crosstermCnt!=sig.crosstermCnt) return 0;
00460
00461 if(gromacsPairCnt!=sig.gromacsPairCnt) return 0;
00462
00463 #define CMPSIGS(TUPLE) \
00464 for(int i=0; i<sig.TUPLE##Cnt; i++){ \
00465 if(!(TUPLE##Sigs[i]==sig.TUPLE##Sigs[i])) return 0; \
00466 } \
00467
00468 CMPSIGS(bond)
00469 CMPSIGS(angle)
00470 CMPSIGS(dihedral)
00471 CMPSIGS(improper)
00472 CMPSIGS(crossterm)
00473
00474 CMPSIGS(gromacsPair)
00475
00476 return 1;
00477 }
00478 ~AtomSignature(){
00479 if(bondSigs) delete[] bondSigs;
00480 if(angleSigs) delete[] angleSigs;
00481 if(dihedralSigs) delete[] dihedralSigs;
00482 if(improperSigs) delete[] improperSigs;
00483 if(crosstermSigs) delete[] crosstermSigs;
00484
00485 if(gromacsPairSigs) delete[] gromacsPairSigs;
00486 }
00487
00488 void removeEmptyTupleSigs();
00489 void pack(MOStream *msg);
00490 void unpack(MIStream *msg);
00491 };
00492
00493 struct AtomNameIdx{
00494 Index resnameIdx;
00495 Index atomnameIdx;
00496 Index atomtypeIdx;
00497 };
00498 struct AtomCstInfo{
00499 Index vdw_type;
00500 int32 status;
00501 int32 partner;
00502 int32 hydrogenList;
00503 };
00504
00505 struct ExclusionSignature{
00506 int fullExclCnt;
00507 int *fullOffset;
00508 int modExclCnt;
00509 int *modOffset;
00510 #ifdef NAMD_CUDA
00511 int allExclCnt;
00512 TupleSignature *allTuples;
00513 #endif
00514
00515 ExclusionSignature(){
00516 fullExclCnt = modExclCnt = 0;
00517 fullOffset = modOffset = NULL;
00518 #ifdef NAMD_CUDA
00519 allExclCnt = 0;
00520 allTuples = NULL;
00521 #endif
00522 }
00523 ExclusionSignature(const ExclusionSignature& sig){
00524 fullOffset = modOffset = NULL;
00525 fullExclCnt = sig.fullExclCnt;
00526 if(fullExclCnt>0){
00527 fullOffset = new int[fullExclCnt];
00528 for(int i=0; i<fullExclCnt; i++)
00529 fullOffset[i] = sig.fullOffset[i];
00530 }
00531
00532 modExclCnt = sig.modExclCnt;
00533 if(modExclCnt>0){
00534 modOffset = new int[modExclCnt];
00535 for(int i=0; i<modExclCnt; i++)
00536 modOffset[i] = sig.modOffset[i];
00537 }
00538 #ifdef NAMD_CUDA
00539 allTuples = NULL;
00540 allExclCnt = sig.allExclCnt;
00541 if(allExclCnt>0){
00542 allTuples = new TupleSignature[allExclCnt];
00543 for(int i=0; i<allExclCnt; i++)
00544 allTuples[i] = sig.allTuples[i];
00545 }
00546 #endif
00547 }
00548 ~ExclusionSignature(){
00549 if(fullOffset) delete [] fullOffset;
00550 if(modOffset) delete [] modOffset;
00551 #ifdef NAMD_CUDA
00552 if(allTuples) delete [] allTuples;
00553 #endif
00554 }
00555
00556 ExclusionSignature& operator=(const ExclusionSignature& sig){
00557 fullExclCnt = sig.fullExclCnt;
00558 if(fullOffset) delete [] fullOffset;
00559 if(fullExclCnt>0){
00560 fullOffset = new int[fullExclCnt];
00561 for(int i=0; i<fullExclCnt; i++)
00562 fullOffset[i] = sig.fullOffset[i];
00563 }else
00564 fullOffset = NULL;
00565
00566 modExclCnt = sig.modExclCnt;
00567 if(modOffset) delete [] modOffset;
00568 if(modExclCnt>0){
00569 modOffset = new int[modExclCnt];
00570 for(int i=0; i<modExclCnt; i++)
00571 modOffset[i] = sig.modOffset[i];
00572 }else
00573 modOffset = NULL;
00574 #ifdef NAMD_CUDA
00575 allExclCnt = sig.allExclCnt;
00576 if(allTuples) delete [] allTuples;
00577 if(allExclCnt>0){
00578 allTuples = new TupleSignature[allExclCnt];
00579 for(int i=0; i<allExclCnt; i++)
00580 allTuples[i] = sig.allTuples[i];
00581 }else
00582 allTuples = NULL;
00583 #endif
00584
00585 return *this;
00586 }
00587 int operator==(const ExclusionSignature& sig) const{
00588 if(fullExclCnt!=sig.fullExclCnt) return 0;
00589 if(modExclCnt!=sig.modExclCnt) return 0;
00590
00591 for(int i=0; i<fullExclCnt; i++){
00592 if(fullOffset[i]!=sig.fullOffset[i]) return 0;
00593 }
00594 for(int i=0; i<modExclCnt; i++){
00595 if(modOffset[i]!=sig.modOffset[i]) return 0;
00596 }
00597 return 1;
00598 }
00599
00600 void setOffsets(std::vector<int>& fullVec, std::vector<int>& modVec){
00601 fullExclCnt = fullVec.size();
00602 modExclCnt = modVec.size();
00603 if(fullExclCnt>0) {
00604 fullOffset = new int[fullExclCnt];
00605 for(int i=0; i<fullExclCnt; i++)
00606 fullOffset[i] = fullVec[i];
00607 }
00608
00609 if(modExclCnt>0) {
00610 modOffset = new int[modExclCnt];
00611 for(int i=0; i<modExclCnt; i++)
00612 modOffset[i] = modVec[i];
00613 }
00614 #ifdef NAMD_CUDA
00615 buildTuples();
00616 }
00617 void buildTuples() {
00618 delete [] allTuples;
00619 allTuples = NULL;
00620 allExclCnt = 0;
00621 for(int i=0; i<fullExclCnt; i++)
00622 if ( fullOffset[i] > 0 ) ++allExclCnt;
00623 for(int i=0; i<modExclCnt; i++)
00624 if ( modOffset[i] > 0 ) ++allExclCnt;
00625 if(allExclCnt>0){
00626 allTuples = new TupleSignature[allExclCnt];
00627 int j = 0;
00628 for(int i=0; i<fullExclCnt; i++){
00629 if ( fullOffset[i] <= 0 ) continue;
00630 TupleSignature oneSig(1,EXCLUSION,0);
00631 oneSig.offset[0] = fullOffset[i];
00632 allTuples[j++] = oneSig;
00633 }
00634 for(int i=0; i<modExclCnt; i++){
00635 if ( modOffset[i] <= 0 ) continue;
00636 TupleSignature oneSig(1,EXCLUSION,1);
00637 oneSig.offset[0] = modOffset[i];
00638 allTuples[j++] = oneSig;
00639 }
00640 }
00641 #endif
00642 }
00643
00644 int hash() const {
00645 unsigned int numOffset = fullExclCnt + modExclCnt;
00646 unsigned int code = 0x12345678;
00647 unsigned int codesz = 8 * sizeof(int);
00648 unsigned int shift = codesz / numOffset;
00649
00650 if (shift == 0) shift=1;
00651 unsigned int i;
00652 for(i=0; i < fullExclCnt; i++) {
00653 code = circShift(code,shift);
00654 code ^= fullOffset[i];
00655 }
00656 for(i=0; i < modExclCnt; i++) {
00657 code = circShift(code,shift);
00658 code ^= modOffset[i];
00659 }
00660 return code;
00661 }
00662
00663 void removeEmptyOffset();
00664
00665 int findOffset(int offset, int *fullOrMod);
00666 void pack(MOStream *msg);
00667 void unpack(MIStream *msg);
00668 };
00669
00670 #endif
00671