2 * moldyn.h - molecular dynamics library header file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
11 #include "math/math.h"
12 #include "random/random.h"
13 #include "list/list.h"
22 typedef unsigned char u8;
25 typedef struct s_virial {
26 double xx; /* | xx xy xz | */
27 double yy; /* V = | yx yy yz | */
28 double zz; /* | zx zy zz | */
30 double xz; /* with: xy=yx, xz=zx, yz=zy */
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 */
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 */
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 */
60 #define DEFAULT_ATOM_ATTR 0x74 // 1,2,3 body interaction + visualize
62 /* special list structure for low mem approach */
63 typedef struct s_lowmem_list {
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 */
75 int **subcell; /* pointer to the cell lists */
77 t_lowmem_list *subcell; /* low mem approach list */
79 t_list *subcell; /* pointer to the cell lists */
81 int dnlc; /* direct neighbour lists counter */
84 #define MAX_ATOMS_PER_LIST 20
86 /* moldyn schedule structure */
87 typedef struct s_moldyn_schedule {
92 int (*hook)(void *moldyn,void *hook_params);
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 */
103 /* moldyn main structure */
104 typedef struct s_moldyn {
105 int argc; /* number of arguments */
106 char **args; /* pointer to arguments */
108 int count; /* total amount of atoms */
109 double mass; /* total system mass */
110 t_atom *atom; /* pointer to the atoms */
112 t_3dvec dim; /* dimensions of the simulation volume */
113 double volume; /* volume of sim cell (dim.x*dim.y*dim.z) */
115 /* potential force function and parameter pointers */
116 int (*func1b)(struct s_moldyn *moldyn,t_atom *ai);
117 int (*func2b)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
118 int (*func3b_j1)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
119 int (*func3b_j2)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
120 int (*func3b_j3)(struct s_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc);
121 int (*func3b_k1)(struct s_moldyn *moldyn,
122 t_atom *ai,t_atom *aj,t_atom *ak,u8 bck);
123 int (*func3b_k2)(struct s_moldyn *moldyn,
124 t_atom *ai,t_atom *aj,t_atom *ak,u8 bck);
126 unsigned char run3bp;
128 double cutoff; /* cutoff radius */
129 double cutoff_square; /* square of the cutoff radius */
130 double nnd; /* nearest neighbour distance (optional) */
132 t_linkcell lc; /* linked cell list interface */
134 int avg_skip; /* amount of steps without average calc */
136 double t_ref; /* reference temperature */
137 double t; /* actual temperature */
138 double t_sum; /* sum over all t */
139 double t_avg; /* average value of t */
142 t_virial gvir; /* global virial (absolute coordinates) */
150 // gp stuff exchanged by kinetic energies
151 //double gp; /* pressure computed from global virial */
152 //double gp_sum; /* sum over all gp */
153 //double gp_avg; /* average value of gp */
158 t_virial vir; /* actual virial */
160 double virial_sum; /* sum over all calculated virials */
161 double virial_avg; /* average of virial */
163 double p_ref; /* reference pressure */
164 double p; /* actual pressure (computed by virial) */
165 double px,py,pz; /* components of pressure */
166 double p_sum; /* sum over all p */
167 double p_avg; /* average value of p */
169 double tp; /* thermodynamic pressure dU/dV */
170 double tp_sum; /* sum over dU/dV pressure */
171 double tp_avg; /* average value of dU/dV pressure */
172 int tp_cnt; /* how often to do thermodynamic p calc */
174 /* pressure and temperature control (velocity/volume scaling) */
175 /* (t_tc in units of tau, p_tc in units of tau * isoth. compressib.) */
176 unsigned char pt_scale; /* type of p and t scaling */
177 double t_tc; /* t berendsen control time constant */
178 double p_tc; /* p berendsen control time constant */
180 /* simulation schedule */
181 t_moldyn_schedule schedule;
182 int current; /* current position in schedule */
184 /* integration function pointer */
185 int (*integrate)(struct s_moldyn *moldyn);
186 int time_steps; /* amount of iterations */
187 double tau; /* delta t */
188 double time; /* absolute time */
189 double tau_square; /* delta t squared */
190 int total_steps; /* total steps */
193 double energy; /* potential energy */
194 double ekin; /* kinetic energy */
196 /* energy averages & fluctuations */
197 double k_sum; /* sum of kinetic energy */
198 double v_sum; /* sum of potential energy */
199 double k_avg; /* average of kinetic energy */
200 double v_avg; /* average of potential energy */
201 double k2_sum; /* sum of kinetic energy squared */
202 double v2_sum; /* sum of potential energy squared */
203 double k2_avg; /* average of kinetic energy squared */
204 double v2_avg; /* average of potential energy squared */
205 double dk2_avg; /* mean square kinetic energy fluctuations */
206 double dv2_avg; /* mean square potential energy fluctuations */
208 /* response functions */
209 double c_v_nve; /* constant volume heat capacity (nve) */
210 double c_v_nvt; /* constant volume heat capacity (nvt) */
212 char vlsdir[128]; /* visualization/log/save directory */
213 t_visual vis; /* visualization interface structure */
214 u8 vlsprop; /* log/vis/save properties */
215 unsigned int ewrite; /* how often to log energy */
216 int efd; /* fd for energy log */
217 unsigned int mwrite; /* how often to log momentum */
218 int mfd; /* fd for momentum log */
219 unsigned int pwrite; /* how often to log pressure */
220 int pfd; /* fd for pressure log */
221 unsigned int twrite; /* how often to log temperature */
222 int tfd; /* fd for temperature log */
223 unsigned int vwrite; /* how often to log volume */
224 int vfd; /* fd for volume log */
225 unsigned int awrite; /* how often to visualize atom information */
226 unsigned int swrite; /* how often to create a save file */
227 int rfd; /* report file descriptor */
228 char rtitle[64]; /* report title */
229 char rauthor[64]; /* report author */
230 int epfd; /* energy gnuplot script file descriptor */
231 int ppfd; /* pressure gnuplot script file descriptor */
232 int tpfd; /* temperature gnuplot script file descriptor */
234 u8 status; /* general moldyn properties */
236 t_random random; /* random interface */
238 double debug; /* debugging stuff, ignore */
240 /* potential 2 body check function */
241 int (*check_2b_bond)(struct s_moldyn *moldyn,
242 t_atom *itom,t_atom *jtom,u8 bc);
245 typedef struct s_pcc {
252 typedef struct s_ba {
258 typedef struct s_vb {
262 typedef struct s_part_params {
269 #define PART_INSIDE_R 1
270 #define PART_OUTSIDE_R 2
271 #define PART_INSIDE_D 3
272 #define PART_OUTSIDE_D 4
274 typedef struct s_defect_params {
284 #define DEFECT_TYPE_0D 1
285 #define DEFECT_TYPE_1D 2
286 #define DEFECT_TYPE_2D 3
287 #define DEFECT_TYPE_3D 4
289 #define DEFECT_STYPE_DB_X 1
290 #define DEFECT_STYPE_DB_Y 2
291 #define DEFECT_STYPE_DB_Z 3
292 #define DEFECT_STYPE_DB_R 4
294 typedef struct s_offset_params {
305 #define MOLDYN_STAT_PBX 0x01 /* periodic boudaries in x */
306 #define MOLDYN_STAT_PBY 0x02 /* y */
307 #define MOLDYN_STAT_PBZ 0x04 /* and z direction */
309 #define MOLDYN_PSCALE 0x08 /* size controlled by piston */
311 #define MOLDYN_1BP 0x10 /* care about single */
312 #define MOLDYN_2BP 0x20 /* 2 body */
313 #define MOLDYN_3BP 0x40 /* and 3 body particle pots */
315 #define T_SCALE_NONE 0x00
316 #define T_SCALE_BERENDSEN 0x01 /* berendsen t control */
317 #define T_SCALE_DIRECT 0x02 /* direct t control */
318 #define T_SCALE_MASK 0x03
320 #define P_SCALE_NONE 0x00
321 #define P_SCALE_BERENDSEN 0x04 /* berendsen p control */
322 #define P_SCALE_DIRECT 0x08 /* direct p control */
323 #define P_SCALE_MASK 0x0c
326 * default values & units
328 * - length unit: 1 A (1 A = 1e-10 m)
329 * - time unit: 1 fs (1 fs = 1e-15 s)
330 * - mass unit: 1 amu (1 amu = 1.6605388628e-27 kg )
332 * fyi: in the following 1 N = (amu*A)/(fs*fs)
336 #define METER 1e10 /* A */
337 #define SECOND 1e15 /* fs */
338 #define AMU 1.6605388628e-27 /* kg */
339 #define KILOGRAM (1.0/AMU) /* amu */
340 #define NEWTON (METER*KILOGRAM/(SECOND*SECOND)) /* A amu / fs^2 */
341 #define PASCAL (NEWTON/(METER*METER)) /* N / A^2 */
342 #define GPA (1e9*PASCAL) /* N / A^2 */
343 #define BAR ((1.0e5*PASCAL)) /* N / A^2 */
344 #define K_BOLTZMANN (1.380650524e-23*METER*NEWTON) /* NA/K */
345 #define K_B2 (K_BOLTZMANN*K_BOLTZMANN) /* (NA)^2/K^2 */
346 #define EV (1.6021765314e-19*METER*NEWTON) /* NA */
347 #define JOULE (NEWTON*METER) /* NA */
349 #define MOLDYN_TEMP 273.0
350 #define MOLDYN_TAU 1.0
351 #define MOLDYN_CUTOFF 10.0
352 #define MOLDYN_RUNS 1000000
354 #define MOLDYN_INTEGRATE_VERLET 0x00
355 #define MOLDYN_INTEGRATE_DEFAULT MOLDYN_INTEGRATE_VERLET
357 #define MOLDYN_POTENTIAL_HO 0x00
358 #define MOLDYN_POTENTIAL_LJ 0x01
359 #define MOLDYN_POTENTIAL_TM 0x02
360 #define MOLDYN_POTENTIAL_AM 0x03
362 #define LOG_TOTAL_ENERGY 0x01
363 #define LOG_TOTAL_MOMENTUM 0x02
364 #define LOG_PRESSURE 0x04
365 #define LOG_TEMPERATURE 0x08
366 #define LOG_VOLUME 0x10
367 #define SAVE_STEP 0x20
368 #define VISUAL_STEP 0x40
369 #define CREATE_REPORT 0x80
378 #define SCALE_DOWN 'd'
379 #define SCALE_DIRECT 'D'
385 #define ONE_THIRD (1.0/3.0)
388 * element specific defines
392 #define LC_C 3.567 /* A */
393 #define M_C 12.011 /* amu */
396 #define LC_SI 5.43105 /* A */
397 #define M_SI 28.08553 /* amu */
399 #define LC_3C_SIC 4.3596 /* A */
408 #define ZINCBLENDE 0x08
419 * function prototypes
423 int moldyn_init(t_moldyn *moldyn,int argc,char **argv);
424 int moldyn_shutdown(t_moldyn *moldyn);
426 int set_int_alg(t_moldyn *moldyn,u8 algo);
427 int set_cutoff(t_moldyn *moldyn,double cutoff);
428 int set_temperature(t_moldyn *moldyn,double t_ref);
429 int set_pressure(t_moldyn *moldyn,double p_ref);
430 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc);
431 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc);
432 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc);
433 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize);
434 int set_nn_dist(t_moldyn *moldyn,double dist);
435 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z);
436 int set_potential(t_moldyn *moldyn,u8 type);
438 int set_avg_skip(t_moldyn *moldyn,int skip);
440 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir);
441 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title);
442 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer);
443 int moldyn_log_shutdown(t_moldyn *moldyn);
445 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,
446 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin,
447 t_part_params *p_params,t_defect_params *d_params);
448 int add_atom(t_moldyn *moldyn,int element,u8 brand,u8 attr,
449 t_3dvec *r,t_3dvec *v);
450 int del_atom(t_moldyn *moldyn,int tag);
451 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin,
452 t_part_params *p_params,t_defect_params *d_params);
453 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin,
454 t_part_params *p_params,t_defect_params *d_params);
455 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin,
456 t_part_params *p_params,t_defect_params *d_params);
457 int destroy_atoms(t_moldyn *moldyn);
459 int thermal_init(t_moldyn *moldyn,u8 equi_init);
460 double total_mass_calc(t_moldyn *moldyn);
461 double temperature_calc(t_moldyn *moldyn);
462 double get_temperature(t_moldyn *moldyn);
463 int scale_velocity(t_moldyn *moldyn,u8 equi_init);
464 double virial_sum(t_moldyn *moldyn);
465 double pressure_calc(t_moldyn *moldyn);
466 int average_reset(t_moldyn *moldyn);
467 int average_and_fluctuation_calc(t_moldyn *moldyn);
468 int get_heat_capacity(t_moldyn *moldyn);
469 double thermodynamic_pressure_calc(t_moldyn *moldyn);
470 double get_pressure(t_moldyn *moldyn);
471 int scale_volume(t_moldyn *moldyn);
472 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z);
473 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z);
475 double e_kin_calc(t_moldyn *moldyn);
476 double get_total_energy(t_moldyn *moldyn);
477 t_3dvec get_total_p(t_moldyn *moldyn);
479 double estimate_time_step(t_moldyn *moldyn,double nn_dist);
481 int link_cell_init(t_moldyn *moldyn,u8 vol);
482 int link_cell_update(t_moldyn *moldyn);
484 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,int **cell);
486 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,int *cell);
488 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,t_list *cell);
490 int link_cell_shutdown(t_moldyn *moldyn);
492 typedef int (*set_hook)(void *,void *);
494 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau);
495 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params);
497 int moldyn_integrate(t_moldyn *moldyn);
498 int velocity_verlet(t_moldyn *moldyn);
500 int potential_force_calc(t_moldyn *moldyn);
501 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d);
502 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d)
503 // __attribute__((always_inline));
504 int check_per_bound(t_moldyn *moldyn,t_3dvec *a);
505 int check_per_bound_and_care_for_pbc(t_moldyn *moldyn,t_atom *a);
506 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a)
507 // __attribute__((always_inline));
509 int moldyn_bc_check(t_moldyn *moldyn);
511 int moldyn_read_save_file(t_moldyn *moldyn,char *file);
512 int moldyn_free_save_file(t_moldyn *moldyn);
513 int moldyn_load(t_moldyn *moldyn);
514 int process_2b_bonds(t_moldyn *moldyn,void *data,
515 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
517 int process_neighbours(t_moldyn *moldyn,void *data,t_atom *atom,
518 int (*process)(t_moldyn *moldyn,t_atom *atom,t_atom *natom,
521 int get_line(int fd,char *line,int max);
523 int pair_correlation_init(t_moldyn *moldyn,double dr);
524 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc);
525 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
526 t_atom *jtom,void *data,u8 bc);
527 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr);
528 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
530 int bond_analyze(t_moldyn *moldyn,double *quality);
532 int visual_init(t_moldyn *moldyn,char *filebase);
533 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
536 void *visual_atoms(void *ptr);
538 int visual_atoms(t_moldyn *moldyn);
541 int fpu_set_rtd(void);