1 /* -*- c++ -*- ----------------------------------------------------------
2    LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
3    https://www.lammps.org/, Sandia National Laboratories
4    Steve Plimpton, sjplimp@sandia.gov
5 
6    Copyright (2003) Sandia Corporation.  Under the terms of Contract
7    DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
8    certain rights in this software.  This software is distributed under
9    the GNU General Public License.
10 
11    See the README file in the top-level LAMMPS directory.
12 ------------------------------------------------------------------------- */
13 
14 /* ----------------------------------------------------------------------
15    Contributing author: Axel Kohlmeyer (Temple U)
16 ------------------------------------------------------------------------- */
17 
18 #ifndef LMP_MOLFILE_INTERFACE_H
19 #define LMP_MOLFILE_INTERFACE_H
20 
21 namespace LAMMPS_NS {
22 
23 // This class provides an abstract interface
24 // to the VMD plugin library.
25 
26 class MolfileInterface {
27  public:
28   // plugin modes.
29   enum {
30     M_NONE = 0,
31     M_READ = 1 << 0,
32     M_WRITE = 1 << 1,
33     M_RSTRUCT = 1 << 2,
34     M_WSTRUCT = 1 << 3,
35     M_RBONDS = 1 << 4,
36     M_WBONDS = 1 << 5,
37     M_RANGLES = 1 << 6,
38     M_WANGLES = 1 << 7,
39     M_RVOL = 1 << 8,
40     M_WVOL = 1 << 9,
41     M_RVELS = 1 << 10,
42     M_WVELS = 1 << 11,
43     M_LAST = 1 << 12
44   };
45 
46   // plugin finder return values.
47   enum {
48     E_NONE = 0,    //< nothing happened
49     E_DIR,         //< path is not a directory or not readable
50     E_FILE,        //< file not a DSO or not readable
51     E_SYMBOL,      //< DSO is not a VMD plugin
52     E_TYPE,        //< plugin is not of the correct type
53     E_ABI,         //< plugin ABI does not match
54     E_MODE,        //< plugin does not support desired mode
55     E_VERSION,     //< plugin is not newer as the current one
56     E_MATCH,       //< plugin matches
57     E_LAST         //< last entry
58   };
59 
60   // atom structure properties. deliberately not complete.
61   enum {
62     P_NONE = 0,          //< no structure information available
63     P_NAME = 1 << 0,     //< atom name,    char[16]
64     P_TYPE = 1 << 1,     //< atom type,    char[16]
65     P_RESN = 1 << 2,     //< residue name, char[ 8]
66     P_RESI = 1 << 3,     //< residue index, int
67     P_SEGN = 1 << 4,     //< segment name, char[ 8]
68     P_CHAI = 1 << 5,     //< chain id,     char[ 2]
69     P_OCCP = 1 << 6,     //< occupancy,    float
70     P_BFAC = 1 << 7,     //< B factor,     float
71     P_MASS = 1 << 8,     //< atom mass,    float
72     P_CHRG = 1 << 9,     //< atom charge,  float
73     P_RADS = 1 << 10,    //< atom radius,  float
74     P_ATMN = 1 << 11,    //< atomic number, int
75     P_LAST = 1 << 12     //< last entry
76   };
77 
78   MolfileInterface(const char *type, const int mode);
79   ~MolfileInterface();
80 
81   // disallowed default methods
82  private:
MolfileInterface()83   MolfileInterface(){};
MolfileInterface(const MolfileInterface &)84   MolfileInterface(const MolfileInterface &){};
85   MolfileInterface &operator=(const MolfileInterface &) { return *this; };
86 
87  public:
88   // search in the given directory path for a molfile plugin that
89   // is of the right type and supports the desired mode.
90   // if a plugin is already registered and a newer version is
91   // found, this new version will override the old one.
92   int find_plugin(const char *path);
93   // try to register the plugin at given file name
94   int load_plugin(const char *name);
95   // deregister the current plugin/DSO and clean up.
96   void forget_plugin();
97   // return formatted string describing plugin
get_plugin_name()98   char *get_plugin_name() const { return _name; };
99   // return canonical plugin name (= file type)
get_plugin_type()100   char *get_plugin_type() const { return _type; };
101 
102   // file operations
103 
104   // open file through plugin
105   int open(const char *name, int *natoms);
106   // read/write structure info
107   int structure();
108   // read/write timestep
109   int timestep(float *coords, float *vels, float *cell, double *simtime);
110   // close file managed by plugin
111   int close();
112 
113   // inquire on interface status
114 
115   // true if file stream is active.
is_open()116   bool is_open() const { return (_ptr != 0); };
117   // true if file format requires or provides atom properties
has_props()118   bool has_props() const { return (_mode & (M_RSTRUCT | M_WSTRUCT)) != 0; };
119   // true if file format can read or write velocities
has_vels()120   bool has_vels() const { return (_mode & (M_RVELS | M_WVELS)) != 0; };
121 
122   // return number of atoms in current file. -1 if closed/invalid;
get_natoms()123   bool get_natoms() const { return _natoms; };
124   // return property bitmask
get_props()125   bool get_props() const { return _props; };
126 
127   // atom property operations
128 
129   // set/get atom floating point property
130   int property(int propid, int idx, float *prop);
131   // set/get per type floating point property
132   int property(int propid, int *types, float *prop);
133   // set/get per atom floating point property
134   int property(int propid, float *prop);
135   // set/get atom floating point property
136   int property(int propid, int idx, double *prop);
137   // set/get per type floating point property
138   int property(int propid, int *types, double *prop);
139   // set/get per atom floating point property
140   int property(int propid, double *prop);
141   // set/get atom integer property
142   int property(int propid, int idx, int *prop);
143   // set/get per type integer property
144   int property(int propid, int *types, int *prop);
145   // set/get per atom integer property
146   int property(int propid, int *prop);
147   // set/get atom string property
148   int property(int propid, int idx, char *prop);
149   // set/get per type string property
150   int property(int propid, int *types, char **prop);
151   // set/get per atom string property
152   int property(int propid, char **prop);
153 
154   // internal data
155  protected:
156   void *_plugin;    // pointer to plugin struct
157   void *_dso;       // handle to DSO
158   void *_ptr;       // pointer to plugin data handle
159   void *_info;      // pointer to atomic info data
160   char *_type;      // canonical plugin name
161   char *_name;      // plugin formatted name
162   int _natoms;      // number of atoms
163   int _mode;        // plugin mode of operation
164   int _caps;        // plugin capabilities
165   int _props;       // accumulated/available properties
166 };
167 
168 }    // namespace LAMMPS_NS
169 
170 #endif
171