CompressPsf.h

Go to the documentation of this file.
00001 #ifndef COMPRESSPSF_H
00002 #define COMPRESSPSF_H
00003 
00004 #include "structures.h"
00005 #include <string>
00006 #include <deque>
00007 #include <ckhashtable.h>
00008 
00009 #define COMPRESSED_PSF_VER 1.72
00010 
00011 //used to detemine big-endian or little-endian for 
00012 //the per-atom binary file
00013 #define COMPRESSED_PSF_MAGICNUM 1234
00014 
00015 class Molecule;
00016 class Parameters;
00017 class SimParameters;
00018 class ConfigList;
00019 
00020 //if the compiler supports anonymous struct, then sSet, iSet
00021 //and fSet could be omitted for simplicity. -Chao Mei
00022 struct OutputAtomRecord{
00023   struct shortVals{
00024     short segNameIdx;
00025     short resNameIdx;
00026     short atomNameIdx;
00027     short atomTypeIdx;
00028     short chargeIdx;
00029     short massIdx;    
00030     short vdw_type;
00031   }sSet;
00032 
00033   struct integerVals{
00034     int atomSigIdx;
00035     int exclSigIdx;
00036     int resID;      
00037     int hydrogenList;
00038     int atomsInGroup;
00039     int GPID;      
00040     int atomsInMigrationGroup;
00041     int MPID;    
00042   }iSet;
00043 
00044   struct floatVals{
00045     Real rigidBondLength;
00046   }fSet;
00047            
00048   void flip();
00049 };
00050 
00051 void compress_molecule_info(Molecule *mol, char *psfFileName, Parameters *param, SimParameters *simParam, ConfigList* cfgList);
00052 
00053 void flipNum(char *elem, int elemSize, int numElems);
00054 
00055 template <typename T>
00056 int lookupCstPool(const std::vector<T>& pool, const T& val)
00057 {
00058     for(int i=0; i<pool.size(); i++)
00059     {
00060         if(pool[i]==val)
00061             return i;
00062     }
00063     return -1;
00064 }
00065 
00066 // Adapt the function prototype for ckhashtable keys to the 
00067 // NAMD XXXSig classes
00068 template <class T> class HashPoolAdaptorT {
00069   T val;
00070 public:
00071   HashPoolAdaptorT<T>(const T &v):val(v) { }
00073   HashPoolAdaptorT<T>(){}
00074   operator T & () { return val; }
00075   operator const T & () const { return val; }
00076   
00077   inline CkHashCode hash(void) const { 
00078     const int hash=val.hash();
00079     return CkHashFunction_int(&hash,sizeof(int));
00080   }
00081 
00082   static CkHashCode staticHash(const void *k,size_t) {
00083     return ((HashPoolAdaptorT<T> *)k)->hash();
00084   }
00085   
00086   inline int compare(const HashPoolAdaptorT<T> &t) const {
00087     return val==t.val;
00088   }
00089   
00090   static int staticCompare(const void *a,const void *b,size_t) {
00091     return ((HashPoolAdaptorT<T> *)a)->compare(*(HashPoolAdaptorT<T> *)b);
00092   }
00093   
00094   inline T& getVal() { return val; }
00095   
00096   // PUPer not tested
00097   void pup(PUP::er &p){
00098     p | *val;
00099   }
00100 };
00101 
00102 template <typename T>
00103 class HashPool {
00104 public:
00105   ~HashPool() {
00106     clear();
00107   }
00108   
00109   void clear() {
00110     // Delete the pool entries
00111     for ( int i=0; i < pool.size(); i++)
00112       delete pool[i];
00113     // Clear the pool and hash table
00114     pool.clear();
00115     index_table.empty();
00116   }
00117   
00118   int lookupCstPool(const T& val)
00119   {
00120     HashPoolAdaptorT<T> hx(val);
00121     // Ugly: Can't store zeros in the table, so add 1 to indices on insert and
00122     // subtract 1 to get real index when retrieving
00123     int loc = index_table.get(hx) - 1;
00124 #if 0
00125     int i;
00126     for(i=0; i < pool.size(); i++)
00127     {
00128       if (pool[i]->getVal() == val)  {
00129         if (i != loc) {
00130           CmiPrintf("Get[%d] returned %d, actual is %d\n",hx.hash(),loc,i);
00131           dump_tables();
00132           loc = i;
00133         }
00134         break;
00135       }
00136     }
00137     if (loc != -1 && i == pool.size()) {
00138       CmiPrintf("Get returned %d, actual not found\n",loc);
00139     }
00140 #endif
00141     return loc;
00142   }
00143   
00144   void push_back(const T& x)
00145   {
00146     // Behave like a STL vector, but also store the indexing info in
00147     // the hashtable index
00148     
00149     // Add to vector
00150     HashPoolAdaptorT<T>* val = new HashPoolAdaptorT<T>(x);
00151     pool.push_back(val);
00152     // Also add to hash table. Make sure pointer doesn't change
00153     int* index = &(index_table.put(*val));
00154     // Ugly: Can't store zeros in the table, so add one to all entries
00155     *index = pool.size(); // pool.size()  - 1 + 1
00156     
00157     //  CmiPrintf("Adding hx=%p hash=%d index[%p]=%d\n",&val,val->hash(),index,*index);
00158     //  dump_tables();  
00159   }
00160   
00161   void dump_tables(); 
00162   
00163   T& operator[](int i) const { return pool[i]->getVal(); }
00164   
00165   int size() { return pool.size(); }
00166 
00167 private:
00168   CkHashtableT<HashPoolAdaptorT<T>,int> index_table;
00169   std::vector<HashPoolAdaptorT<T>*> pool;
00170 };
00171 
00172 #endif

Generated on Sat Sep 23 01:17:11 2017 for NAMD by  doxygen 1.4.7