implemented basic p ctrl stuff + video with 13 fps
[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 */
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         double bondlen[3];      /* bond lengthes (only 2 atomic systems) */
121
122         t_linkcell lc;          /* linked cell list interface */
123
124         int avg_skip;           /* amount of steps without average calc */
125
126         double t_ref;           /* reference temperature */
127         double t;               /* actual temperature */
128         double t_sum;           /* sum over all t */
129         double t_avg;           /* average value of t */
130
131         t_virial gvir;          /* global virial (absolute coordinates) */
132         double gv;
133         double gv_sum;
134         double gv_avg;
135
136         double gp;              /* pressure computed from global virial */
137         double gp_sum;          /* sum over all gp */
138         double gp_avg;          /* average value of gp */
139
140         t_virial vir;           /* actual virial */
141         double virial;
142         double virial_sum;      /* sum over all calculated virials */
143         double virial_avg;      /* average of virial */
144
145         double p_ref;           /* reference pressure */
146         double p;               /* actual pressure (computed by virial) */
147         double p_sum;           /* sum over all p */
148         double p_avg;           /* average value of p */
149
150         double tp;              /* thermodynamic pressure dU/dV */
151         double tp_sum;          /* sum over dU/dV pressure */
152         double tp_avg;          /* average value of dU/dV pressure */
153         int tp_cnt;             /* how often to do thermodynamic p calc */
154
155         /* pressure and temperature control (velocity/volume scaling) */
156         /* (t_tc in units of tau, p_tc in units of tau * isoth. compressib.) */
157         unsigned char pt_scale; /* type of p and t scaling */
158         double t_tc;            /* t berendsen control time constant */
159         double p_tc;            /* p berendsen control time constant */
160
161         /* simulation schedule */
162         t_moldyn_schedule schedule;
163         int current;            /* current position in schedule */
164
165         /* integration function pointer */
166         int (*integrate)(struct s_moldyn *moldyn);
167         int time_steps;         /* amount of iterations */
168         double tau;             /* delta t */
169         double time;            /* absolute time */
170         double tau_square;      /* delta t squared */
171         int total_steps;        /* total steps */
172
173         /* energy */
174         double energy;          /* potential energy */
175         double ekin;            /* kinetic energy */
176
177         /* energy averages & fluctuations */
178         double k_sum;           /* sum of kinetic energy */
179         double v_sum;           /* sum of potential energy */
180         double k_avg;           /* average of kinetic energy */
181         double v_avg;           /* average of potential energy */
182         double k2_sum;          /* sum of kinetic energy squared */
183         double v2_sum;          /* sum of potential energy squared */
184         double k2_avg;          /* average of kinetic energy squared */
185         double v2_avg;          /* average of potential energy squared */
186         double dk2_avg;         /* mean square kinetic energy fluctuations */
187         double dv2_avg;         /* mean square potential energy fluctuations */
188         
189         /* response functions */
190         double c_v_nve;         /* constant volume heat capacity (nve) */
191         double c_v_nvt;         /* constant volume heat capacity (nvt) */
192
193         char vlsdir[128];       /* visualization/log/save directory */
194         t_visual vis;           /* visualization interface structure */
195         u8 vlsprop;             /* log/vis/save properties */
196         unsigned int ewrite;    /* how often to log energy */
197         int efd;                /* fd for energy log */
198         unsigned int mwrite;    /* how often to log momentum */
199         int mfd;                /* fd for momentum log */
200         unsigned int pwrite;    /* how often to log pressure */
201         int pfd;                /* fd for pressure log */
202         unsigned int twrite;    /* how often to log temperature */
203         int tfd;                /* fd for temperature log */
204         unsigned int vwrite;    /* how often to log volume */
205         int vfd;                /* fd for volume log */
206         unsigned int awrite;    /* how often to visualize atom information */
207         unsigned int swrite;    /* how often to create a save file */
208         int rfd;                /* report file descriptor */
209         char rtitle[64];        /* report title */
210         char rauthor[64];       /* report author */
211         int epfd;               /* energy gnuplot script file descriptor */
212         int ppfd;               /* pressure gnuplot script file descriptor */
213         int tpfd;               /* temperature gnuplot script file descriptor */
214
215         u8 status;              /* general moldyn properties */
216
217         t_random random;        /* random interface */
218
219         double debug;           /* debugging stuff, ignore */
220 } t_moldyn;
221
222 /*
223  *
224  *  defines
225  *
226  */
227
228 #define MOLDYN_STAT_PBX                 0x01    /* periodic boudaries in x */
229 #define MOLDYN_STAT_PBY                 0x02    /* y */
230 #define MOLDYN_STAT_PBZ                 0x04    /* and z direction */
231
232 #define MOLDYN_PSCALE                   0x08    /* size controlled by piston */
233
234 #define MOLDYN_1BP                      0x10    /* care about single */
235 #define MOLDYN_2BP                      0x20    /* 2 body */
236 #define MOLDYN_3BP                      0x40    /* and 3 body particle pots */
237
238 #define T_SCALE_BERENDSEN               0x01    /* berendsen t control */
239 #define T_SCALE_DIRECT                  0x02    /* direct t control */
240 #define P_SCALE_BERENDSEN               0x04    /* berendsen p control */
241 #define P_SCALE_DIRECT                  0x08    /* direct p control */
242
243 /*
244  * default values & units
245  *
246  * - length unit: 1 A (1 A = 1e-10 m)
247  * - time unit: 1 fs (1 fs = 1e-15 s)
248  * - mass unit: 1 amu (1 amu = 1.6605388628e-27 kg )
249  *
250  * fyi: in the following 1 N = (amu*A)/(fs*fs)
251  *
252  */
253
254 #define METER                           1e10                    /* A */
255 #define SECOND                          1e15                    /* fs */
256 #define AMU                             1.6605388628e-27        /* kg */
257 #define KILOGRAM                        (1.0/AMU)               /* amu */
258 #define NEWTON  (METER*KILOGRAM/(SECOND*SECOND))        /* A amu / fs^2 */
259 #define PASCAL  (NEWTON/(METER*METER))                  /* N / A^2 */
260 #define GPA     (1e9*PASCAL)                            /* N / A^2 */
261 #define BAR     ((1.0e5*PASCAL))                        /* N / A^2 */
262 #define K_BOLTZMANN     (1.380650524e-23*METER*NEWTON)  /* NA/K */
263 #define K_B2            (K_BOLTZMANN*K_BOLTZMANN)       /* (NA)^2/K^2 */
264 #define EV              (1.6021765314e-19*METER*NEWTON) /* NA */
265 #define JOULE           (NEWTON*METER)                  /* NA */
266
267 #define MOLDYN_TEMP                     273.0
268 #define MOLDYN_TAU                      1.0
269 #define MOLDYN_CUTOFF                   10.0
270 #define MOLDYN_RUNS                     1000000
271
272 #define MOLDYN_INTEGRATE_VERLET         0x00
273 #define MOLDYN_INTEGRATE_DEFAULT        MOLDYN_INTEGRATE_VERLET
274
275 #define MOLDYN_POTENTIAL_HO             0x00
276 #define MOLDYN_POTENTIAL_LJ             0x01
277 #define MOLDYN_POTENTIAL_TM             0x02
278
279 #define LOG_TOTAL_ENERGY                0x01
280 #define LOG_TOTAL_MOMENTUM              0x02
281 #define LOG_PRESSURE                    0x04
282 #define LOG_TEMPERATURE                 0x08
283 #define LOG_VOLUME                      0x10
284 #define SAVE_STEP                       0x20
285 #define VISUAL_STEP                     0x40
286 #define CREATE_REPORT                   0x80
287
288 #define TRUE                            1
289 #define FALSE                           0
290
291 #define VERBOSE                         1
292 #define QUIET                           0
293
294 #define SCALE_UP                        'u'
295 #define SCALE_DOWN                      'd'
296 #define SCALE_DIRECT                    'D'
297
298 /*
299  * potential related phsical values / constants
300  *
301  */
302
303 #define ONE_THIRD               (1.0/3.0)
304
305 #define C                       0x06
306 #define LC_C                    3.567                           /* A */
307 #define M_C                     12.011                          /* amu */
308
309 #define SI                      0x0e
310 #define LC_SI                   5.43105                         /* A */
311 #define M_SI                    28.08553                        /* amu */
312
313 #define LC_3C_SIC               4.3596                          /* A */
314
315 #define LJ_SIGMA_SI             ((0.25*sqrt(3.0)*LC_SI)/1.122462)       /* A */
316 //#define LJ_SIGMA_SI           (LC_SI/1.122462)                        /* A */
317 //#define LJ_SIGMA_SI           (0.5*sqrt(2.0)*LC_SI/1.122462)          /* A */
318 #define LJ_EPSILON_SI           (2.1678*EV)                             /* NA */
319
320 #define TM_R_SI                 2.7                             /* A */
321 #define TM_S_SI                 3.0                             /* A */
322 #define TM_A_SI                 (1830.8*EV)                     /* NA */
323 #define TM_B_SI                 (471.18*EV)                     /* NA */
324 #define TM_LAMBDA_SI            2.4799                          /* 1/A */
325 #define TM_MU_SI                1.7322                          /* 1/A */
326 #define TM_BETA_SI              1.1000e-6
327 #define TM_N_SI                 0.78734
328 #define TM_C_SI                 1.0039e5
329 #define TM_D_SI                 16.217
330 #define TM_H_SI                 -0.59825
331
332 #define TM_R_C                  1.8                             /* A */
333 #define TM_S_C                  2.1                             /* A */
334 #define TM_A_C                  (1393.6*EV)                     /* NA */
335 #define TM_B_C                  (346.7*EV)                      /* NA */
336 #define TM_LAMBDA_C             3.4879                          /* 1/A */
337 #define TM_MU_C                 2.2119                          /* 1/A */
338 #define TM_BETA_C               1.5724e-7
339 #define TM_N_C                  0.72751
340 #define TM_C_C                  3.8049e4
341 #define TM_D_C                  4.384
342 #define TM_H_C                  -0.57058
343
344 #define TM_CHI_SIC              0.9776
345
346 #define TM_LC_SIC               4.32                            /* A */
347
348 #define ALBE_R_SI               (2.82-0.14)
349 #define ALBE_S_SI               (2.82+0.14)
350 #define ALBE_A_SI               (3.24*EV/0.842)
351 #define ALBE_B_SI               (-1.842*3.24*EV/0.842)
352 #define ALBE_R0_SI              2.232
353 #define ALBE_LAMBDA_SI          (1.4761*sqrt(2.0*1.842))
354 #define ALBE_MU_SI              (1.4761*sqrt(2.0/1.842))
355 #define ALBE_GAMMA_SI           0.114354
356 #define ALBE_C_SI               2.00494
357 #define ALBE_D_SI               0.81472
358 #define ALBE_H_SI               0.259
359
360 #define ALBE_LC_SI              5.429
361
362 #define ALBE_R_C                (2.00-0.15)
363 #define ALBE_S_C                (2.00+0.15)
364 #define ALBE_A_C                (6.00*EV/1.167)
365 #define ALBE_B_C                (-2.167*6.00*EV/1.167)
366 #define ALBE_R0_C               1.4276
367 #define ALBE_LAMBDA_C           (2.0099*sqrt(2.0*2.167))
368 #define ALBE_MU_C               (2.0099*sqrt(2.0/2.167))
369 #define ALBE_GAMMA_C            0.11233
370 #define ALBE_C_C                181.910
371 #define ALBE_D_C                6.28433
372 #define ALBE_H_C                0.5556
373
374 #define ALBE_LC_C               3.566
375
376 #define ALBE_R_SIC              (2.40-0.20)
377 #define ALBE_S_SIC              (2.40+0.20)
378 #define ALBE_A_SIC              (4.36*EV/0.847)
379 #define ALBE_B_SIC              (-1.847*4.36*EV/0.847)
380 #define ALBE_R0_SIC             1.79
381 #define ALBE_LAMBDA_SIC         (1.6991*sqrt(2.0*1.847))
382 #define ALBE_MU_SIC             (1.6991*sqrt(2.0/1.847))
383 #define ALBE_GAMMA_SIC          0.011877
384 #define ALBE_C_SIC              273987
385 #define ALBE_D_SIC              180.314
386 #define ALBE_H_SIC              0.68
387
388 #define ALBE_LC_SIC             4.359
389
390
391 /*
392  * lattice types
393  */
394
395 #define CUBIC                   0x01
396 #define FCC                     0x02
397 #define DIAMOND                 0x04
398
399 /*
400  *
401  * function prototypes
402  *
403  */
404
405 typedef int (*pf_func1b)(t_moldyn *,t_atom *);
406 typedef int (*pf_func2b)(t_moldyn *,t_atom *,t_atom *,u8);
407 typedef int (*pf_func3b)(t_moldyn *,t_atom *,t_atom *,t_atom *,u8);
408
409 int moldyn_init(t_moldyn *moldyn,int argc,char **argv);
410 int moldyn_shutdown(t_moldyn *moldyn);
411
412 int set_int_alg(t_moldyn *moldyn,u8 algo);
413 int set_cutoff(t_moldyn *moldyn,double cutoff);
414 int set_bondlen(t_moldyn *moldyn,double b0,double b1,double bm);
415 int set_temperature(t_moldyn *moldyn,double t_ref);
416 int set_pressure(t_moldyn *moldyn,double p_ref);
417 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc);
418 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize);
419 int set_nn_dist(t_moldyn *moldyn,double dist);
420 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z);
421 int set_potential1b(t_moldyn *moldyn,pf_func1b func);
422 int set_potential2b(t_moldyn *moldyn,pf_func2b func);
423 int set_potential3b_j1(t_moldyn *moldyn,pf_func2b func);
424 int set_potential3b_j2(t_moldyn *moldyn,pf_func2b func);
425 int set_potential3b_j3(t_moldyn *moldyn,pf_func2b func);
426 int set_potential3b_k1(t_moldyn *moldyn,pf_func3b func);
427 int set_potential3b_k2(t_moldyn *moldyn,pf_func3b func);
428 int set_potential_params(t_moldyn *moldyn,void *params);
429
430 int set_avg_skip(t_moldyn *moldyn,int skip);
431
432 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir);
433 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title);
434 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer);
435 int moldyn_log_shutdown(t_moldyn *moldyn);
436
437 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
438                    u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin);
439 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
440              t_3dvec *r,t_3dvec *v);
441 int del_atom(t_moldyn *moldyn,int tag);
442 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin);
443 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin);
444 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin);
445 int destroy_atoms(t_moldyn *moldyn);
446
447 int thermal_init(t_moldyn *moldyn,u8 equi_init);
448 double total_mass_calc(t_moldyn *moldyn);
449 double temperature_calc(t_moldyn *moldyn);
450 double get_temperature(t_moldyn *moldyn);
451 int scale_velocity(t_moldyn *moldyn,u8 equi_init);
452 double virial_sum(t_moldyn *moldyn);
453 double pressure_calc(t_moldyn *moldyn);
454 int average_reset(t_moldyn *moldyn);
455 int average_and_fluctuation_calc(t_moldyn *moldyn);
456 int get_heat_capacity(t_moldyn *moldyn);
457 double thermodynamic_pressure_calc(t_moldyn *moldyn);
458 double get_pressure(t_moldyn *moldyn);
459 int scale_volume(t_moldyn *moldyn);
460 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z);
461 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z);
462
463 double e_kin_calc(t_moldyn *moldyn);
464 double get_total_energy(t_moldyn *moldyn);
465 t_3dvec get_total_p(t_moldyn *moldyn);
466
467 double estimate_time_step(t_moldyn *moldyn,double nn_dist);
468
469 int link_cell_init(t_moldyn *moldyn,u8 vol);
470 int link_cell_update(t_moldyn *moldyn);
471 #ifdef STATIC_LISTS
472 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,int **cell);
473 #else
474 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell);
475 #endif
476 int link_cell_shutdown(t_moldyn *moldyn);
477
478 typedef int (*set_hook)(void *,void *);
479
480 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau);
481 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params);
482
483 int moldyn_integrate(t_moldyn *moldyn);
484 int velocity_verlet(t_moldyn *moldyn);
485
486 int potential_force_calc(t_moldyn *moldyn);
487 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d);
488 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d)
489 //      __attribute__((always_inline));
490 int check_per_bound(t_moldyn *moldyn,t_3dvec *a);
491 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a)
492 //      __attribute__((always_inline));
493
494 int moldyn_bc_check(t_moldyn *moldyn);
495
496 int moldyn_read_save_file(t_moldyn *moldyn,char *file);
497 int moldyn_load(t_moldyn *moldyn);
498 int get_line(int fd,char *line,int max);
499
500 int pair_correlation_init(t_moldyn *moldyn,double dr);
501 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc);
502 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr);
503
504 int visual_init(t_moldyn *moldyn,char *filebase);
505 int visual_atoms(t_moldyn *moldyn);
506
507 #endif
508