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