safety checkin
[physik/posic.git] / moldyn.h
1 /*
2  * moldyn.h - molecular dynamics library header file
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #ifndef MOLDYN_H
9 #define MOLDYN_H
10
11 #include "math/math.h"
12 #include "random/random.h"
13 #include "list/list.h"
14
15 /*
16  * 
17  * datatypes
18  *
19  */
20
21 /* general */
22 typedef unsigned char u8;
23
24 /* virial */
25 typedef struct s_virial {
26         double xx;      /*                      | xx     xy     xz |    */
27         double yy;      /*      V       =       | yx     yy     yz |    */
28         double zz;      /*                      | zx     zy     zz |    */
29         double xy;      /*                                              */
30         double xz;      /*      with:   xy=yx, xz=zx, yz=zy             */
31         double yz;      /*                                              */
32 } t_virial;
33
34 /* the atom of the md simulation */
35 typedef struct s_atom {
36         t_3dvec r_0;            /* initial position */
37         t_3dvec r;              /* position */
38         t_3dvec v;              /* velocity */
39         t_3dvec f;              /* force */
40         t_virial virial;        /* virial */
41         double e;               /* site energy */
42         double ekin;            /* kinetic energy */
43         int element;            /* number of element in pse */
44         double mass;            /* atom mass */
45         u8 brand;               /* brand id */
46         int tag;                /* atom unique id (number of atom) */
47         u8 attr;                /* attributes */
48 } t_atom;
49
50 #define ATOM_ATTR_FP    0x01    /* fixed position (bulk material) */
51 #define ATOM_ATTR_HB    0x02    /* coupled to heat bath (velocity scaling) */
52 #define ATOM_ATTR_VA    0x04    /* visualize this atom */ // TODO
53 #define ATOM_ATTR_VB    0x08    /* visualize the bond of this atom */
54
55 #define ATOM_ATTR_1BP   0x10    /* single paricle potential */
56 #define ATOM_ATTR_2BP   0x20    /* pair potential */
57 #define ATOM_ATTR_3BP   0x40    /* 3 body potential */ 
58
59 /* cell lists */
60 typedef struct s_linkcell {
61         int nx,ny,nz;           /* amount of cells in x, y and z direction */
62         int cells;              /* total amount of cells */
63         double len;             /* prefered cell edge length */
64         double x,y,z;           /* the actual cell lengthes */
65 #ifdef STATIC_LISTS
66         int **subcell;          /* pointer to the cell lists */
67 #else
68         t_list *subcell;        /* pointer to the cell lists */
69 #endif
70         int dnlc;               /* direct neighbour lists counter */
71 } t_linkcell;
72
73 #define MAX_ATOMS_PER_LIST      20
74
75 /* moldyn schedule structure */
76 typedef struct s_moldyn_schedule {
77         int count;
78         int total_sched;
79         int *runs;
80         double *tau;
81         int (*hook)(void *moldyn,void *hook_params);
82         void *hook_params;
83 } t_moldyn_schedule;
84
85 /* visualization structure */
86 typedef struct s_visual {
87         int fd;                 /* rasmol script file descriptor */
88         char fb[128];           /* basename of the save files */
89         t_3dvec dim;            /* dimensions of the simulation cell */
90 } t_visual;
91
92 /* moldyn main structure */
93 typedef struct s_moldyn {
94         int argc;               /* number of arguments */
95         char **args;            /* pointer to arguments */
96
97         int count;              /* total amount of atoms */
98         double mass;            /* total system mass */
99         t_atom *atom;           /* pointer to the atoms */
100
101         t_3dvec dim;            /* dimensions of the simulation volume */
102         double volume;          /* volume of sim cell (dim.x*dim.y*dim.z) */
103
104         /* potential force function and parameter pointers */
105         int (*func1b)(struct s_moldyn *moldyn,t_atom *ai);
106         int (*func2b)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
107         int (*func3b_j1)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
108         int (*func3b_j2)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
109         int (*func3b_j3)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
110         int (*func3b_k1)(struct s_moldyn *moldyn,
111                          t_atom *ai,t_atom *aj,t_atom *ak,u8 bck);
112         int (*func3b_k2)(struct s_moldyn *moldyn,
113                          t_atom *ai,t_atom *aj,t_atom *ak,u8 bck);
114         void *pot_params;
115         unsigned char run3bp;
116
117         double cutoff;          /* cutoff radius */
118         double cutoff_square;   /* square of the cutoff radius */
119         double nnd;             /* nearest neighbour distance (optional) */
120
121         t_linkcell lc;          /* linked cell list interface */
122
123         int avg_skip;           /* amount of steps without average calc */
124
125         double t_ref;           /* reference temperature */
126         double t;               /* actual temperature */
127         double t_sum;           /* sum over all t */
128         double t_avg;           /* average value of t */
129
130         t_virial gvir;          /* global virial (absolute coordinates) */
131         double gv;
132         double gv_sum;
133         double gv_avg;
134
135         double gp;              /* pressure computed from global virial */
136         double gp_sum;          /* sum over all gp */
137         double gp_avg;          /* average value of gp */
138
139         t_virial vir;           /* actual virial */
140         double virial;
141         double virial_sum;      /* sum over all calculated virials */
142         double virial_avg;      /* average of virial */
143
144         double p_ref;           /* reference pressure */
145         double p;               /* actual pressure (computed by virial) */
146         double p_sum;           /* sum over all p */
147         double p_avg;           /* average value of p */
148
149         double tp;              /* thermodynamic pressure dU/dV */
150         double tp_sum;          /* sum over dU/dV pressure */
151         double tp_avg;          /* average value of dU/dV pressure */
152         int tp_cnt;             /* how often to do thermodynamic p calc */
153
154         /* pressure and temperature control (velocity/volume scaling) */
155         /* (t_tc in units of tau, p_tc in units of tau * isoth. compressib.) */
156         unsigned char pt_scale; /* type of p and t scaling */
157         double t_tc;            /* t berendsen control time constant */
158         double p_tc;            /* p berendsen control time constant */
159
160         /* simulation schedule */
161         t_moldyn_schedule schedule;
162         int current;            /* current position in schedule */
163
164         /* integration function pointer */
165         int (*integrate)(struct s_moldyn *moldyn);
166         int time_steps;         /* amount of iterations */
167         double tau;             /* delta t */
168         double time;            /* absolute time */
169         double tau_square;      /* delta t squared */
170         int total_steps;        /* total steps */
171
172         /* energy */
173         double energy;          /* potential energy */
174         double ekin;            /* kinetic energy */
175
176         /* energy averages & fluctuations */
177         double k_sum;           /* sum of kinetic energy */
178         double v_sum;           /* sum of potential energy */
179         double k_avg;           /* average of kinetic energy */
180         double v_avg;           /* average of potential energy */
181         double k2_sum;          /* sum of kinetic energy squared */
182         double v2_sum;          /* sum of potential energy squared */
183         double k2_avg;          /* average of kinetic energy squared */
184         double v2_avg;          /* average of potential energy squared */
185         double dk2_avg;         /* mean square kinetic energy fluctuations */
186         double dv2_avg;         /* mean square potential energy fluctuations */
187         
188         /* response functions */
189         double c_v_nve;         /* constant volume heat capacity (nve) */
190         double c_v_nvt;         /* constant volume heat capacity (nvt) */
191
192         char vlsdir[128];       /* visualization/log/save directory */
193         t_visual vis;           /* visualization interface structure */
194         u8 vlsprop;             /* log/vis/save properties */
195         unsigned int ewrite;    /* how often to log energy */
196         int efd;                /* fd for energy log */
197         unsigned int mwrite;    /* how often to log momentum */
198         int mfd;                /* fd for momentum log */
199         unsigned int pwrite;    /* how often to log pressure */
200         int pfd;                /* fd for pressure log */
201         unsigned int twrite;    /* how often to log temperature */
202         int tfd;                /* fd for temperature log */
203         unsigned int vwrite;    /* how often to log volume */
204         int vfd;                /* fd for volume log */
205         unsigned int awrite;    /* how often to visualize atom information */
206         unsigned int swrite;    /* how often to create a save file */
207         int rfd;                /* report file descriptor */
208         char rtitle[64];        /* report title */
209         char rauthor[64];       /* report author */
210         int epfd;               /* energy gnuplot script file descriptor */
211         int ppfd;               /* pressure gnuplot script file descriptor */
212         int tpfd;               /* temperature gnuplot script file descriptor */
213
214         u8 status;              /* general moldyn properties */
215
216         t_random random;        /* random interface */
217
218         double debug;           /* debugging stuff, ignore */
219
220         /* potential 2 body check function */
221         int (*check_2b_bond)(struct s_moldyn *moldyn,
222                              t_atom *itom,t_atom *jtom,u8 bc);
223 } t_moldyn;
224
225 typedef struct s_pcc {
226         int o1;
227         int o2;
228         double dr;
229         double *stat;
230 } t_pcc;
231
232 typedef struct s_ba {
233         int *acnt;
234         int *bcnt;
235         int tcnt;
236 } t_ba;
237
238 typedef struct s_vb {
239         int fd;
240 } t_vb;
241
242 /*
243  *
244  *  defines
245  *
246  */
247
248 #define MOLDYN_STAT_PBX                 0x01    /* periodic boudaries in x */
249 #define MOLDYN_STAT_PBY                 0x02    /* y */
250 #define MOLDYN_STAT_PBZ                 0x04    /* and z direction */
251
252 #define MOLDYN_PSCALE                   0x08    /* size controlled by piston */
253
254 #define MOLDYN_1BP                      0x10    /* care about single */
255 #define MOLDYN_2BP                      0x20    /* 2 body */
256 #define MOLDYN_3BP                      0x40    /* and 3 body particle pots */
257
258 #define T_SCALE_NONE                    0x00
259 #define T_SCALE_BERENDSEN               0x01    /* berendsen t control */
260 #define T_SCALE_DIRECT                  0x02    /* direct t control */
261 #define T_SCALE_MASK                    0x03
262
263 #define P_SCALE_NONE                    0x00
264 #define P_SCALE_BERENDSEN               0x04    /* berendsen p control */
265 #define P_SCALE_DIRECT                  0x08    /* direct p control */
266 #define P_SCALE_MASK                    0x0c
267
268 /*
269  * default values & units
270  *
271  * - length unit: 1 A (1 A = 1e-10 m)
272  * - time unit: 1 fs (1 fs = 1e-15 s)
273  * - mass unit: 1 amu (1 amu = 1.6605388628e-27 kg )
274  *
275  * fyi: in the following 1 N = (amu*A)/(fs*fs)
276  *
277  */
278
279 #define METER                           1e10                    /* A */
280 #define SECOND                          1e15                    /* fs */
281 #define AMU                             1.6605388628e-27        /* kg */
282 #define KILOGRAM                        (1.0/AMU)               /* amu */
283 #define NEWTON  (METER*KILOGRAM/(SECOND*SECOND))        /* A amu / fs^2 */
284 #define PASCAL  (NEWTON/(METER*METER))                  /* N / A^2 */
285 #define GPA     (1e9*PASCAL)                            /* N / A^2 */
286 #define BAR     ((1.0e5*PASCAL))                        /* N / A^2 */
287 #define K_BOLTZMANN     (1.380650524e-23*METER*NEWTON)  /* NA/K */
288 #define K_B2            (K_BOLTZMANN*K_BOLTZMANN)       /* (NA)^2/K^2 */
289 #define EV              (1.6021765314e-19*METER*NEWTON) /* NA */
290 #define JOULE           (NEWTON*METER)                  /* NA */
291
292 #define MOLDYN_TEMP                     273.0
293 #define MOLDYN_TAU                      1.0
294 #define MOLDYN_CUTOFF                   10.0
295 #define MOLDYN_RUNS                     1000000
296
297 #define MOLDYN_INTEGRATE_VERLET         0x00
298 #define MOLDYN_INTEGRATE_DEFAULT        MOLDYN_INTEGRATE_VERLET
299
300 #define MOLDYN_POTENTIAL_HO             0x00
301 #define MOLDYN_POTENTIAL_LJ             0x01
302 #define MOLDYN_POTENTIAL_TM             0x02
303 #define MOLDYN_POTENTIAL_AM             0x03
304
305 #define LOG_TOTAL_ENERGY                0x01
306 #define LOG_TOTAL_MOMENTUM              0x02
307 #define LOG_PRESSURE                    0x04
308 #define LOG_TEMPERATURE                 0x08
309 #define LOG_VOLUME                      0x10
310 #define SAVE_STEP                       0x20
311 #define VISUAL_STEP                     0x40
312 #define CREATE_REPORT                   0x80
313
314 #define TRUE                            1
315 #define FALSE                           0
316
317 #define VERBOSE                         1
318 #define QUIET                           0
319
320 #define SCALE_UP                        'u'
321 #define SCALE_DOWN                      'd'
322 #define SCALE_DIRECT                    'D'
323
324 /*
325  * usefull constants
326  */
327
328 #define ONE_THIRD               (1.0/3.0)
329
330 /*
331  * element specific defines
332  */
333
334 #define C                       0x06
335 #define LC_C                    3.567                           /* A */
336 #define M_C                     12.011                          /* amu */
337
338 #define SI                      0x0e
339 #define LC_SI                   5.43105                         /* A */
340 #define M_SI                    28.08553                        /* amu */
341
342 #define LC_3C_SIC               4.3596                          /* A */
343
344 /*
345  * lattice types
346  */
347
348 #define CUBIC                   0x01
349 #define FCC                     0x02
350 #define DIAMOND                 0x04
351 #define ZINCBLENDE              0x08
352
353 /*
354  *
355  * function prototypes
356  *
357  */
358
359 int moldyn_init(t_moldyn *moldyn,int argc,char **argv);
360 int moldyn_shutdown(t_moldyn *moldyn);
361
362 int set_int_alg(t_moldyn *moldyn,u8 algo);
363 int set_cutoff(t_moldyn *moldyn,double cutoff);
364 int set_temperature(t_moldyn *moldyn,double t_ref);
365 int set_pressure(t_moldyn *moldyn,double p_ref);
366 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc);
367 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc);
368 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc);
369 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize);
370 int set_nn_dist(t_moldyn *moldyn,double dist);
371 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z);
372 int set_potential(t_moldyn *moldyn,u8 type);
373
374 int set_avg_skip(t_moldyn *moldyn,int skip);
375
376 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir);
377 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title);
378 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer);
379 int moldyn_log_shutdown(t_moldyn *moldyn);
380
381 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
382                    u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin);
383 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
384              t_3dvec *r,t_3dvec *v);
385 int del_atom(t_moldyn *moldyn,int tag);
386 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin);
387 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin);
388 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin);
389 int destroy_atoms(t_moldyn *moldyn);
390
391 int thermal_init(t_moldyn *moldyn,u8 equi_init);
392 double total_mass_calc(t_moldyn *moldyn);
393 double temperature_calc(t_moldyn *moldyn);
394 double get_temperature(t_moldyn *moldyn);
395 int scale_velocity(t_moldyn *moldyn,u8 equi_init);
396 double virial_sum(t_moldyn *moldyn);
397 double pressure_calc(t_moldyn *moldyn);
398 int average_reset(t_moldyn *moldyn);
399 int average_and_fluctuation_calc(t_moldyn *moldyn);
400 int get_heat_capacity(t_moldyn *moldyn);
401 double thermodynamic_pressure_calc(t_moldyn *moldyn);
402 double get_pressure(t_moldyn *moldyn);
403 int scale_volume(t_moldyn *moldyn);
404 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z);
405 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z);
406
407 double e_kin_calc(t_moldyn *moldyn);
408 double get_total_energy(t_moldyn *moldyn);
409 t_3dvec get_total_p(t_moldyn *moldyn);
410
411 double estimate_time_step(t_moldyn *moldyn,double nn_dist);
412
413 int link_cell_init(t_moldyn *moldyn,u8 vol);
414 int link_cell_update(t_moldyn *moldyn);
415 #ifdef STATIC_LISTS
416 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,int **cell);
417 #else
418 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell);
419 #endif
420 int link_cell_shutdown(t_moldyn *moldyn);
421
422 typedef int (*set_hook)(void *,void *);
423
424 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau);
425 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params);
426
427 int moldyn_integrate(t_moldyn *moldyn);
428 int velocity_verlet(t_moldyn *moldyn);
429
430 int potential_force_calc(t_moldyn *moldyn);
431 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d);
432 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d)
433 //      __attribute__((always_inline));
434 int check_per_bound(t_moldyn *moldyn,t_3dvec *a);
435 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a)
436 //      __attribute__((always_inline));
437
438 int moldyn_bc_check(t_moldyn *moldyn);
439
440 int moldyn_read_save_file(t_moldyn *moldyn,char *file);
441 int moldyn_free_save_file(t_moldyn *moldyn);
442 int moldyn_load(t_moldyn *moldyn);
443 int process_2b_bonds(t_moldyn *moldyn,void *data,
444                      int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
445                                     void *data,u8 bc));
446 int get_line(int fd,char *line,int max);
447
448 int pair_correlation_init(t_moldyn *moldyn,double dr);
449 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc);
450 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
451                                        t_atom *jtom,void *data,u8 bc);
452 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr);
453 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
454                          void *data,u8 bc);
455 int bond_analyze(t_moldyn *moldyn,double *quality);
456
457 int visual_init(t_moldyn *moldyn,char *filebase);
458 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
459                          void *data,u8 bc);
460 int visual_atoms(t_moldyn *moldyn);
461
462 #endif
463