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