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