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