2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
20 #include <fpu_control.h>
26 #if defined PTHREADS || defined VISUAL_THREAD
31 #include "report/report.h"
33 /* potential includes */
34 #include "potentials/harmonic_oscillator.h"
35 #include "potentials/lennard_jones.h"
36 #include "potentials/albe.h"
38 #include "potentials/tersoff_orig.h"
40 #include "potentials/tersoff.h"
54 pthread_mutex_t *amutex;
55 pthread_mutex_t emutex;
59 * the moldyn functions
62 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
64 printf("[moldyn] init\n");
66 /* only needed if compiled without -msse2 (float-store prob!) */
69 memset(moldyn,0,sizeof(t_moldyn));
74 rand_init(&(moldyn->random),NULL,1);
75 moldyn->random.status|=RAND_STAT_VERBOSE;
78 pthread_mutex_init(&emutex,NULL);
84 int moldyn_shutdown(t_moldyn *moldyn) {
90 printf("[moldyn] shutdown\n");
93 for(i=0;i<moldyn->count;i++)
94 pthread_mutex_destroy(&(amutex[i]));
96 pthread_mutex_destroy(&emutex);
99 moldyn_log_shutdown(moldyn);
100 link_cell_shutdown(moldyn);
101 rand_close(&(moldyn->random));
107 int set_int_alg(t_moldyn *moldyn,u8 algo) {
109 printf("[moldyn] integration algorithm: ");
112 case MOLDYN_INTEGRATE_VERLET:
113 moldyn->integrate=velocity_verlet;
114 printf("velocity verlet\n");
117 printf("unknown integration algorithm: %02x\n",algo);
125 int set_cutoff(t_moldyn *moldyn,double cutoff) {
127 moldyn->cutoff=cutoff;
128 moldyn->cutoff_square=cutoff*cutoff;
130 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
135 int set_temperature(t_moldyn *moldyn,double t_ref) {
139 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
144 int set_pressure(t_moldyn *moldyn,double p_ref) {
148 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
153 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
155 moldyn->pt_scale&=(~(P_SCALE_MASK));
156 moldyn->pt_scale|=ptype;
159 printf("[moldyn] p scaling:\n");
161 printf(" p: %s",ptype?"yes":"no ");
163 printf(" | type: %02x | factor: %f",ptype,ptc);
169 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
171 moldyn->pt_scale&=(~(T_SCALE_MASK));
172 moldyn->pt_scale|=ttype;
175 printf("[moldyn] t scaling:\n");
177 printf(" t: %s",ttype?"yes":"no ");
179 printf(" | type: %02x | factor: %f",ttype,ttc);
185 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
187 moldyn->pt_scale=(ptype|ttype);
191 printf("[moldyn] p/t scaling:\n");
193 printf(" p: %s",ptype?"yes":"no ");
195 printf(" | type: %02x | factor: %f",ptype,ptc);
198 printf(" t: %s",ttype?"yes":"no ");
200 printf(" | type: %02x | factor: %f",ttype,ttc);
206 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
212 moldyn->volume=x*y*z;
220 printf("[moldyn] dimensions in A and A^3 respectively:\n");
221 printf(" x: %f\n",moldyn->dim.x);
222 printf(" y: %f\n",moldyn->dim.y);
223 printf(" z: %f\n",moldyn->dim.z);
224 printf(" volume: %f\n",moldyn->volume);
225 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
230 int set_nn_dist(t_moldyn *moldyn,double dist) {
237 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
239 printf("[moldyn] periodic boundary conditions:\n");
242 moldyn->status|=MOLDYN_STAT_PBX;
245 moldyn->status|=MOLDYN_STAT_PBY;
248 moldyn->status|=MOLDYN_STAT_PBZ;
250 printf(" x: %s\n",x?"yes":"no");
251 printf(" y: %s\n",y?"yes":"no");
252 printf(" z: %s\n",z?"yes":"no");
257 int set_potential(t_moldyn *moldyn,u8 type) {
260 case MOLDYN_POTENTIAL_TM:
261 moldyn->func1b=tersoff_mult_1bp;
262 moldyn->func3b_j1=tersoff_mult_3bp_j1;
263 moldyn->func3b_k1=tersoff_mult_3bp_k1;
264 moldyn->func3b_j2=tersoff_mult_3bp_j2;
265 moldyn->func3b_k2=tersoff_mult_3bp_k2;
266 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
268 case MOLDYN_POTENTIAL_AM:
269 moldyn->func3b_j1=albe_mult_3bp_j1;
270 moldyn->func3b_k1=albe_mult_3bp_k1;
271 moldyn->func3b_j2=albe_mult_3bp_j2;
272 moldyn->func3b_k2=albe_mult_3bp_k2;
273 moldyn->check_2b_bond=albe_mult_check_2b_bond;
275 case MOLDYN_POTENTIAL_HO:
276 moldyn->func2b=harmonic_oscillator;
277 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
279 case MOLDYN_POTENTIAL_LJ:
280 moldyn->func2b=lennard_jones;
281 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
284 printf("[moldyn] set potential: unknown type %02x\n",
292 int set_avg_skip(t_moldyn *moldyn,int skip) {
294 printf("[moldyn] skip %d steps before starting average calc\n",skip);
295 moldyn->avg_skip=skip;
300 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
302 strncpy(moldyn->vlsdir,dir,127);
307 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
309 strncpy(moldyn->rauthor,author,63);
310 strncpy(moldyn->rtitle,title,63);
315 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
320 printf("[moldyn] set log: ");
323 case LOG_TOTAL_ENERGY:
324 moldyn->ewrite=timer;
325 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
326 moldyn->efd=open(filename,
327 O_WRONLY|O_CREAT|O_EXCL,
330 perror("[moldyn] energy log fd open");
333 dprintf(moldyn->efd,"# total energy log file\n");
334 printf("total energy (%d)\n",timer);
336 case LOG_TOTAL_MOMENTUM:
337 moldyn->mwrite=timer;
338 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
339 moldyn->mfd=open(filename,
340 O_WRONLY|O_CREAT|O_EXCL,
343 perror("[moldyn] momentum log fd open");
346 dprintf(moldyn->efd,"# total momentum log file\n");
347 printf("total momentum (%d)\n",timer);
350 moldyn->pwrite=timer;
351 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
352 moldyn->pfd=open(filename,
353 O_WRONLY|O_CREAT|O_EXCL,
356 perror("[moldyn] pressure log file\n");
359 dprintf(moldyn->pfd,"# pressure log file\n");
360 printf("pressure (%d)\n",timer);
362 case LOG_TEMPERATURE:
363 moldyn->twrite=timer;
364 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
365 moldyn->tfd=open(filename,
366 O_WRONLY|O_CREAT|O_EXCL,
369 perror("[moldyn] temperature log file\n");
372 dprintf(moldyn->tfd,"# temperature log file\n");
373 printf("temperature (%d)\n",timer);
376 moldyn->vwrite=timer;
377 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
378 moldyn->vfd=open(filename,
379 O_WRONLY|O_CREAT|O_EXCL,
382 perror("[moldyn] volume log file\n");
385 dprintf(moldyn->vfd,"# volume log file\n");
386 printf("volume (%d)\n",timer);
389 moldyn->swrite=timer;
390 printf("save file (%d)\n",timer);
393 moldyn->awrite=timer;
394 ret=visual_init(moldyn,moldyn->vlsdir);
396 printf("[moldyn] visual init failure\n");
399 printf("visual file (%d)\n",timer);
402 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
403 moldyn->rfd=open(filename,
404 O_WRONLY|O_CREAT|O_EXCL,
407 perror("[moldyn] report fd open");
410 printf("report -> ");
412 snprintf(filename,127,"%s/e_plot.scr",
414 moldyn->epfd=open(filename,
415 O_WRONLY|O_CREAT|O_EXCL,
418 perror("[moldyn] energy plot fd open");
421 dprintf(moldyn->epfd,e_plot_script);
426 snprintf(filename,127,"%s/pressure_plot.scr",
428 moldyn->ppfd=open(filename,
429 O_WRONLY|O_CREAT|O_EXCL,
432 perror("[moldyn] p plot fd open");
435 dprintf(moldyn->ppfd,pressure_plot_script);
440 snprintf(filename,127,"%s/temperature_plot.scr",
442 moldyn->tpfd=open(filename,
443 O_WRONLY|O_CREAT|O_EXCL,
446 perror("[moldyn] t plot fd open");
449 dprintf(moldyn->tpfd,temperature_plot_script);
451 printf("temperature ");
453 dprintf(moldyn->rfd,report_start,
454 moldyn->rauthor,moldyn->rtitle);
458 printf("unknown log type: %02x\n",type);
465 int moldyn_log_shutdown(t_moldyn *moldyn) {
469 printf("[moldyn] log shutdown\n");
473 dprintf(moldyn->rfd,report_energy);
474 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
479 if(moldyn->mfd) close(moldyn->mfd);
483 dprintf(moldyn->rfd,report_pressure);
484 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
491 dprintf(moldyn->rfd,report_temperature);
492 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
497 dprintf(moldyn->rfd,report_end);
499 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
502 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
505 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
514 * creating lattice functions
517 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,
518 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin,
519 t_part_params *p_params,t_defect_params *d_params) {
528 pthread_mutex_t *mutex;
534 /* how many atoms do we expect */
537 printf("[moldyn] WARNING: create 'none' lattice called");
539 if(type==CUBIC) new*=1;
540 if(type==FCC) new*=4;
541 if(type==DIAMOND) new*=8;
545 switch(d_params->stype) {
546 case DEFECT_STYPE_DB_X:
547 case DEFECT_STYPE_DB_Y:
548 case DEFECT_STYPE_DB_Z:
549 case DEFECT_STYPE_DB_R:
553 printf("[moldyn] WARNING: cl unknown defect\n");
558 /* allocate space for atoms */
559 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
561 perror("[moldyn] realloc (create lattice)");
565 atom=&(moldyn->atom[count]);
568 ptr=realloc(amutex,(count+new)*sizeof(pthread_mutex_t));
570 perror("[moldyn] mutex realloc (add atom)");
574 mutex=&(amutex[count]);
577 /* no atoms on the boundaries (only reason: it looks better!) */
591 set_nn_dist(moldyn,lc);
592 ret=cubic_init(a,b,c,lc,atom,&orig,p_params,d_params);
593 strcpy(name,"cubic");
597 v3_scale(&orig,&orig,0.5);
598 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
599 ret=fcc_init(a,b,c,lc,atom,&orig,p_params,d_params);
604 v3_scale(&orig,&orig,0.25);
605 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
606 ret=diamond_init(a,b,c,lc,atom,&orig,p_params,d_params);
607 strcpy(name,"diamond");
610 printf("unknown lattice type (%02x)\n",type);
616 printf("[moldyn] creating %s lattice (lc=%f) incomplete\n",
618 printf(" (ignore for partial lattice creation)\n");
619 printf(" amount of atoms\n");
620 printf(" - expected: %d\n",new);
621 printf(" - created: %d\n",ret);
626 printf("[moldyn] created %s lattice with %d atoms\n",name,ret);
628 for(new=0;new<ret;new++) {
629 atom[new].element=element;
630 atom[new].mass=pse_mass[element];
632 atom[new].brand=brand;
633 atom[new].tag=count+new;
634 check_per_bound(moldyn,&(atom[new].r));
635 atom[new].r_0=atom[new].r;
637 pthread_mutex_init(&(mutex[new]),NULL);
641 atom[new].element=d_params->element;
642 atom[new].mass=pse_mass[d_params->element];
643 atom[new].attr=d_params->attr;
644 atom[new].brand=d_params->brand;
645 atom[new].tag=count+new;
646 check_per_bound(moldyn,&(atom[new].r));
647 atom[new].r_0=atom[new].r;
649 pthread_mutex_init(&(mutex[new]),NULL);
655 ptr=realloc(moldyn->atom,moldyn->count*sizeof(t_atom));
657 perror("[moldyn] realloc (create lattice - alloc fix)");
662 // WHAT ABOUT AMUTEX !!!!
665 ptr=realloc(moldyn->lc.subcell->list,moldyn->count*sizeof(int));
667 perror("[moldyn] list realloc (create lattice)");
670 moldyn->lc.subcell->list=ptr;
673 /* update total system mass */
674 total_mass_calc(moldyn);
679 int add_atom(t_moldyn *moldyn,int element,u8 brand,u8 attr,
680 t_3dvec *r,t_3dvec *v) {
687 count=(moldyn->count)++; // asshole style!
689 ptr=realloc(atom,(count+1)*sizeof(t_atom));
691 perror("[moldyn] realloc (add atom)");
697 ptr=realloc(moldyn->lc.subcell->list,(count+1)*sizeof(int));
699 perror("[moldyn] list realloc (add atom)");
702 moldyn->lc.subcell->list=ptr;
706 ptr=realloc(amutex,(count+1)*sizeof(pthread_mutex_t));
708 perror("[moldyn] mutex realloc (add atom)");
712 pthread_mutex_init(&(amutex[count]),NULL);
717 /* initialize new atom */
718 memset(&(atom[count]),0,sizeof(t_atom));
721 atom[count].element=element;
722 atom[count].mass=pse_mass[element];
723 atom[count].brand=brand;
724 atom[count].tag=count;
725 atom[count].attr=attr;
726 check_per_bound(moldyn,&(atom[count].r));
727 atom[count].r_0=atom[count].r;
729 /* update total system mass */
730 total_mass_calc(moldyn);
735 int del_atom(t_moldyn *moldyn,int tag) {
742 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
744 perror("[moldyn]malloc (del atom)");
748 for(cnt=0;cnt<tag;cnt++)
751 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
753 new[cnt-1].tag=cnt-1;
764 #define set_atom_positions(pos) \
765 if(d_params->type) {\
766 d_o.x=0; d_o.y=0; d_o.z=0;\
767 d_d.x=0; d_d.y=0; d_d.z=0;\
768 switch(d_params->stype) {\
769 case DEFECT_STYPE_DB_X:\
773 case DEFECT_STYPE_DB_Y:\
777 case DEFECT_STYPE_DB_Z:\
781 case DEFECT_STYPE_DB_R:\
784 printf("[moldyn] WARNING: unknown defect\n");\
787 v3_add(&dr,&pos,&d_o);\
788 v3_copy(&(atom[count].r),&dr);\
790 v3_add(&dr,&pos,&d_d);\
791 v3_copy(&(atom[count].r),&dr);\
795 v3_copy(&(atom[count].r),&pos);\
800 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin,
801 t_part_params *p_params,t_defect_params *d_params) {
821 /* shift partition values */
823 p.x=p_params->p.x+(a*lc)/2.0;
824 p.y=p_params->p.y+(b*lc)/2.0;
825 p.z=p_params->p.z+(c*lc)/2.0;
834 switch(p_params->type) {
837 if(v3_absolute_square(&dist)<
838 (p_params->r*p_params->r)) {
839 set_atom_positions(r);
844 if(v3_absolute_square(&dist)>=
845 (p_params->r*p_params->r)) {
846 set_atom_positions(r);
851 if((fabs(dist.x)<p_params->d.x)&&
852 (fabs(dist.y)<p_params->d.y)&&
853 (fabs(dist.z)<p_params->d.z)) {
854 set_atom_positions(r);
859 if((fabs(dist.x)>=p_params->d.x)||
860 (fabs(dist.y)>=p_params->d.y)||
861 (fabs(dist.z)>=p_params->d.z)) {
862 set_atom_positions(r);
866 set_atom_positions(r);
876 for(i=0;i<count;i++) {
877 atom[i].r.x-=(a*lc)/2.0;
878 atom[i].r.y-=(b*lc)/2.0;
879 atom[i].r.z-=(c*lc)/2.0;
885 /* fcc lattice init */
886 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin,
887 t_part_params *p_params,t_defect_params *d_params) {
905 /* construct the basis */
906 memset(basis,0,3*sizeof(t_3dvec));
914 /* shift partition values */
916 p.x=p_params->p.x+(a*lc)/2.0;
917 p.y=p_params->p.y+(b*lc)/2.0;
918 p.z=p_params->p.z+(c*lc)/2.0;
921 /* fill up the room */
929 switch(p_params->type) {
932 if(v3_absolute_square(&dist)<
933 (p_params->r*p_params->r)) {
934 set_atom_positions(r);
939 if(v3_absolute_square(&dist)>=
940 (p_params->r*p_params->r)) {
941 set_atom_positions(r);
946 if((fabs(dist.x)<p_params->d.x)&&
947 (fabs(dist.y)<p_params->d.y)&&
948 (fabs(dist.z)<p_params->d.z)) {
949 set_atom_positions(r);
954 if((fabs(dist.x)>=p_params->d.x)||
955 (fabs(dist.y)>=p_params->d.y)||
956 (fabs(dist.z)>=p_params->d.z)) {
957 set_atom_positions(r);
961 set_atom_positions(r);
964 /* the three face centered atoms */
966 v3_add(&n,&r,&basis[l]);
967 switch(p_params->type) {
970 if(v3_absolute_square(&dist)<
971 (p_params->r*p_params->r)) {
972 set_atom_positions(n);
977 if(v3_absolute_square(&dist)>=
978 (p_params->r*p_params->r)) {
979 set_atom_positions(n);
984 if((fabs(dist.x)<p_params->d.x)&&
985 (fabs(dist.y)<p_params->d.y)&&
986 (fabs(dist.z)<p_params->d.z)) {
987 set_atom_positions(n);
992 if((fabs(dist.x)>=p_params->d.x)||
993 (fabs(dist.y)>=p_params->d.y)||
994 (fabs(dist.z)>=p_params->d.z)) {
995 set_atom_positions(n);
999 set_atom_positions(n);
1010 /* coordinate transformation */
1011 for(i=0;i<count;i++) {
1012 atom[i].r.x-=(a*lc)/2.0;
1013 atom[i].r.y-=(b*lc)/2.0;
1014 atom[i].r.z-=(c*lc)/2.0;
1020 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin,
1021 t_part_params *p_params,t_defect_params *d_params) {
1026 count=fcc_init(a,b,c,lc,atom,origin,p_params,d_params);
1032 if(origin) v3_add(&o,&o,origin);
1034 count+=fcc_init(a,b,c,lc,&atom[count],&o,p_params,d_params);
1039 int destroy_atoms(t_moldyn *moldyn) {
1041 if(moldyn->atom) free(moldyn->atom);
1046 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
1049 * - gaussian distribution of velocities
1050 * - zero total momentum
1051 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
1056 t_3dvec p_total,delta;
1061 random=&(moldyn->random);
1063 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
1065 /* gaussian distribution of velocities */
1067 for(i=0;i<moldyn->count;i++) {
1068 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
1070 v=sigma*rand_get_gauss(random);
1072 p_total.x+=atom[i].mass*v;
1074 v=sigma*rand_get_gauss(random);
1076 p_total.y+=atom[i].mass*v;
1078 v=sigma*rand_get_gauss(random);
1080 p_total.z+=atom[i].mass*v;
1083 /* zero total momentum */
1084 v3_scale(&p_total,&p_total,1.0/moldyn->count);
1085 for(i=0;i<moldyn->count;i++) {
1086 v3_scale(&delta,&p_total,1.0/atom[i].mass);
1087 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
1090 /* velocity scaling */
1091 scale_velocity(moldyn,equi_init);
1096 double total_mass_calc(t_moldyn *moldyn) {
1102 for(i=0;i<moldyn->count;i++)
1103 moldyn->mass+=moldyn->atom[i].mass;
1105 return moldyn->mass;
1108 double temperature_calc(t_moldyn *moldyn) {
1110 /* assume up to date kinetic energy, which is 3/2 N k_B T */
1113 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
1119 double get_temperature(t_moldyn *moldyn) {
1124 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
1134 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
1137 /* get kinetic energy / temperature & count involved atoms */
1140 for(i=0;i<moldyn->count;i++) {
1141 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
1142 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
1147 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
1148 else return 0; /* no atoms involved in scaling! */
1150 /* (temporary) hack for e,t = 0 */
1153 if(moldyn->t_ref!=0.0) {
1154 thermal_init(moldyn,equi_init);
1158 return 0; /* no scaling needed */
1162 /* get scaling factor */
1163 scale=moldyn->t_ref/moldyn->t;
1167 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
1168 scale=1.0+(scale-1.0)*moldyn->tau/moldyn->t_tc;
1171 /* velocity scaling */
1172 for(i=0;i<moldyn->count;i++) {
1173 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
1174 v3_scale(&(atom[i].v),&(atom[i].v),scale);
1180 double ideal_gas_law_pressure(t_moldyn *moldyn) {
1184 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
1189 double virial_sum(t_moldyn *moldyn) {
1194 /* virial (sum over atom virials) */
1202 for(i=0;i<moldyn->count;i++) {
1203 virial=&(moldyn->atom[i].virial);
1204 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
1205 moldyn->vir.xx+=virial->xx;
1206 moldyn->vir.yy+=virial->yy;
1207 moldyn->vir.zz+=virial->zz;
1208 moldyn->vir.xy+=virial->xy;
1209 moldyn->vir.xz+=virial->xz;
1210 moldyn->vir.yz+=virial->yz;
1213 /* global virial (absolute coordinates) */
1214 //virial=&(moldyn->gvir);
1215 //moldyn->gv=virial->xx+virial->yy+virial->zz;
1217 return moldyn->virial;
1220 double pressure_calc(t_moldyn *moldyn) {
1224 * with W = 1/3 sum_i f_i r_i (- skipped!)
1225 * virial = sum_i f_i r_i
1227 * => P = (2 Ekin + virial) / (3V)
1230 /* assume up to date virial & up to date kinetic energy */
1232 /* pressure (atom virials) */
1233 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
1234 moldyn->p/=(3.0*moldyn->volume);
1236 //moldyn->px=2.0*moldyn->ekinx+moldyn->vir.xx;
1237 //moldyn->px/=moldyn->volume;
1238 //moldyn->py=2.0*moldyn->ekiny+moldyn->vir.yy;
1239 //moldyn->py/=moldyn->volume;
1240 //moldyn->pz=2.0*moldyn->ekinz+moldyn->vir.zz;
1241 //moldyn->pz/=moldyn->volume;
1243 /* pressure (absolute coordinates) */
1244 //moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
1245 //moldyn->gp/=(3.0*moldyn->volume);
1250 int average_reset(t_moldyn *moldyn) {
1252 printf("[moldyn] average reset\n");
1254 /* update skip value */
1255 moldyn->avg_skip=moldyn->total_steps;
1257 /* kinetic energy */
1261 /* potential energy */
1269 moldyn->virial_sum=0.0;
1270 //moldyn->gv_sum=0.0;
1274 //moldyn->gp_sum=0.0;
1280 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1284 if(moldyn->total_steps<moldyn->avg_skip)
1287 denom=moldyn->total_steps+1-moldyn->avg_skip;
1289 /* assume up to date energies, temperature, pressure etc */
1291 /* kinetic energy */
1292 moldyn->k_sum+=moldyn->ekin;
1293 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1294 moldyn->k_avg=moldyn->k_sum/denom;
1295 moldyn->k2_avg=moldyn->k2_sum/denom;
1296 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1298 /* potential energy */
1299 moldyn->v_sum+=moldyn->energy;
1300 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1301 moldyn->v_avg=moldyn->v_sum/denom;
1302 moldyn->v2_avg=moldyn->v2_sum/denom;
1303 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1306 moldyn->t_sum+=moldyn->t;
1307 moldyn->t_avg=moldyn->t_sum/denom;
1310 moldyn->virial_sum+=moldyn->virial;
1311 moldyn->virial_avg=moldyn->virial_sum/denom;
1312 //moldyn->gv_sum+=moldyn->gv;
1313 //moldyn->gv_avg=moldyn->gv_sum/denom;
1316 moldyn->p_sum+=moldyn->p;
1317 moldyn->p_avg=moldyn->p_sum/denom;
1318 //moldyn->gp_sum+=moldyn->gp;
1319 //moldyn->gp_avg=moldyn->gp_sum/denom;
1320 moldyn->tp_sum+=moldyn->tp;
1321 moldyn->tp_avg=moldyn->tp_sum/denom;
1326 int get_heat_capacity(t_moldyn *moldyn) {
1330 /* averages needed for heat capacity calc */
1331 if(moldyn->total_steps<moldyn->avg_skip)
1334 /* (temperature average)^2 */
1335 temp2=moldyn->t_avg*moldyn->t_avg;
1336 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1339 /* ideal gas contribution */
1340 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1341 printf(" ideal gas contribution: %f\n",
1342 ighc/moldyn->mass*KILOGRAM/JOULE);
1344 /* specific heat for nvt ensemble */
1345 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1346 moldyn->c_v_nvt/=moldyn->mass;
1348 /* specific heat for nve ensemble */
1349 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1350 moldyn->c_v_nve/=moldyn->mass;
1352 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1353 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1354 printf(" --> <dV2> sim: %f experimental: %f\n",moldyn->dv2_avg,1.5*moldyn->count*K_B2*moldyn->t_avg*moldyn->t_avg*(1.0-1.5*moldyn->count*K_BOLTZMANN/(700*moldyn->mass*JOULE/KILOGRAM)));
1359 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1375 /* store atomic configuration + dimension */
1376 store=malloc(moldyn->count*sizeof(t_atom));
1378 printf("[moldyn] allocating store mem failed\n");
1381 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1386 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1387 su=pow(2.0-h,ONE_THIRD)-1.0;
1388 dv=(1.0-h)*moldyn->volume;
1390 /* scale up dimension and atom positions */
1391 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1392 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1393 link_cell_shutdown(moldyn);
1394 link_cell_init(moldyn,QUIET);
1395 potential_force_calc(moldyn);
1398 /* restore atomic configuration + dim */
1399 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1402 /* scale down dimension and atom positions */
1403 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1404 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1405 link_cell_shutdown(moldyn);
1406 link_cell_init(moldyn,QUIET);
1407 potential_force_calc(moldyn);
1410 /* calculate pressure */
1411 moldyn->tp=-(y1-y0)/(2.0*dv);
1413 /* restore atomic configuration */
1414 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1416 link_cell_shutdown(moldyn);
1417 link_cell_init(moldyn,QUIET);
1418 //potential_force_calc(moldyn);
1420 /* free store buffer */
1427 double get_pressure(t_moldyn *moldyn) {
1433 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1445 if(x) dim->x*=scale;
1446 if(y) dim->y*=scale;
1447 if(z) dim->z*=scale;
1452 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1463 for(i=0;i<moldyn->count;i++) {
1464 r=&(moldyn->atom[i].r);
1473 int scale_atoms_ind(t_moldyn *moldyn,double x,double y,double z) {
1478 for(i=0;i<moldyn->count;i++) {
1479 r=&(moldyn->atom[i].r);
1488 int scale_dim_ind(t_moldyn *moldyn,double x,double y,double z) {
1501 int scale_volume(t_moldyn *moldyn) {
1508 vdim=&(moldyn->vis.dim);
1512 /* scaling factor */
1513 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1514 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc*moldyn->tau;
1515 scale=pow(scale,ONE_THIRD);
1518 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1523 sx=1.0-(moldyn->p_ref-moldyn->px)*moldyn->p_tc*moldyn->tau;
1524 sy=1.0-(moldyn->p_ref-moldyn->py)*moldyn->p_tc*moldyn->tau;
1525 sz=1.0-(moldyn->p_ref-moldyn->pz)*moldyn->p_tc*moldyn->tau;
1526 sx=pow(sx,ONE_THIRD);
1527 sy=pow(sy,ONE_THIRD);
1528 sz=pow(sz,ONE_THIRD);
1531 /* scale the atoms and dimensions */
1532 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1533 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1534 //scale_atoms_ind(moldyn,sx,sy,sz);
1535 //scale_dim_ind(moldyn,sx,sy,sz);
1537 /* visualize dimensions */
1544 /* recalculate scaled volume */
1545 moldyn->volume=dim->x*dim->y*dim->z;
1547 /* adjust/reinit linkcell */
1548 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1549 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1550 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1551 link_cell_shutdown(moldyn);
1552 link_cell_init(moldyn,QUIET);
1566 double e_kin_calc(t_moldyn *moldyn) {
1573 //moldyn->ekinx=0.0;
1574 //moldyn->ekiny=0.0;
1575 //moldyn->ekinz=0.0;
1577 for(i=0;i<moldyn->count;i++) {
1578 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1579 moldyn->ekin+=atom[i].ekin;
1580 //moldyn->ekinx+=0.5*atom[i].mass*atom[i].v.x*atom[i].v.x;
1581 //moldyn->ekiny+=0.5*atom[i].mass*atom[i].v.y*atom[i].v.y;
1582 //moldyn->ekinz+=0.5*atom[i].mass*atom[i].v.z*atom[i].v.z;
1585 return moldyn->ekin;
1588 double get_total_energy(t_moldyn *moldyn) {
1590 return(moldyn->ekin+moldyn->energy);
1593 t_3dvec get_total_p(t_moldyn *moldyn) {
1602 for(i=0;i<moldyn->count;i++) {
1603 v3_scale(&p,&(atom[i].v),atom[i].mass);
1604 v3_add(&p_total,&p_total,&p);
1610 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1614 /* nn_dist is the nearest neighbour distance */
1616 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1625 /* linked list / cell method */
1627 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1630 #ifndef LOWMEM_LISTS
1636 /* partitioning the md cell */
1637 lc->nx=moldyn->dim.x/moldyn->cutoff;
1638 lc->x=moldyn->dim.x/lc->nx;
1639 lc->ny=moldyn->dim.y/moldyn->cutoff;
1640 lc->y=moldyn->dim.y/lc->ny;
1641 lc->nz=moldyn->dim.z/moldyn->cutoff;
1642 lc->z=moldyn->dim.z/lc->nz;
1643 lc->cells=lc->nx*lc->ny*lc->nz;
1646 lc->subcell=malloc(lc->cells*sizeof(int*));
1648 lc->subcell=malloc(sizeof(t_lowmem_list));
1650 lc->subcell=malloc(lc->cells*sizeof(t_list));
1653 if(lc->subcell==NULL) {
1654 perror("[moldyn] cell init (malloc)");
1659 printf("[moldyn] FATAL: less then 27 subcells! (%d)\n",
1664 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1667 printf("[moldyn] initializing 'lowmem' linked cells (%d)\n",
1670 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1673 printf(" x: %d x %f A\n",lc->nx,lc->x);
1674 printf(" y: %d x %f A\n",lc->ny,lc->y);
1675 printf(" z: %d x %f A\n",lc->nz,lc->z);
1680 for(i=0;i<lc->cells;i++) {
1681 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1682 if(lc->subcell[i]==NULL) {
1683 perror("[moldyn] list init (malloc)");
1688 printf(" ---> %d malloc %p (%p)\n",
1689 i,lc->subcell[0],lc->subcell);
1693 lc->subcell->head=malloc(lc->cells*sizeof(int));
1694 if(lc->subcell->head==NULL) {
1695 perror("[moldyn] head init (malloc)");
1698 lc->subcell->list=malloc(moldyn->count*sizeof(int));
1699 if(lc->subcell->list==NULL) {
1700 perror("[moldyn] list init (malloc)");
1704 for(i=0;i<lc->cells;i++)
1705 list_init_f(&(lc->subcell[i]));
1708 /* update the list */
1709 link_cell_update(moldyn);
1714 int link_cell_update(t_moldyn *moldyn) {
1732 for(i=0;i<lc->cells;i++)
1734 memset(lc->subcell[i],-1,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1736 lc->subcell->head[i]=-1;
1738 list_destroy_f(&(lc->subcell[i]));
1741 for(count=0;count<moldyn->count;count++) {
1742 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1743 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1744 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1748 while(lc->subcell[i+j*nx+k*nxy][p]!=-1)
1751 if(p>=MAX_ATOMS_PER_LIST) {
1752 printf("[moldyn] FATAL: amount of atoms too high!\n");
1756 lc->subcell[i+j*nx+k*nxy][p]=count;
1759 lc->subcell->list[count]=lc->subcell->head[p];
1760 lc->subcell->head[p]=count;
1762 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nxy]),
1766 printf(" ---> %d %d malloc %p (%p)\n",
1767 i,count,lc->subcell[i].current,lc->subcell);
1775 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1801 if(i>=nx||j>=ny||k>=nz)
1802 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1805 #ifndef LOWMEM_LISTS
1806 cell[0]=lc->subcell[i+j*nx+k*a];
1808 cell[0]=lc->subcell->head[i+j*nx+k*a];
1810 for(ci=-1;ci<=1;ci++) {
1813 if((x<0)||(x>=nx)) {
1817 for(cj=-1;cj<=1;cj++) {
1820 if((y<0)||(y>=ny)) {
1824 for(ck=-1;ck<=1;ck++) {
1827 if((z<0)||(z>=nz)) {
1831 if(!(ci|cj|ck)) continue;
1833 #ifndef LOWMEM_LISTS
1834 cell[--count2]=lc->subcell[x+y*nx+z*a];
1836 cell[--count2]=lc->subcell->head[x+y*nx+z*a];
1841 #ifndef LOWMEM_LISTS
1842 cell[count1++]=lc->subcell[x+y*nx+z*a];
1844 cell[count1++]=lc->subcell->head[x+y*nx+z*a];
1856 int link_cell_shutdown(t_moldyn *moldyn) {
1858 #ifndef LOWMEM_LISTS
1866 free(lc->subcell->head);
1867 free(lc->subcell->list);
1871 for(i=0;i<lc->cells;i++) {
1873 free(lc->subcell[i]);
1875 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1876 list_destroy_f(&(lc->subcell[i]));
1886 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1890 t_moldyn_schedule *schedule;
1892 schedule=&(moldyn->schedule);
1893 count=++(schedule->total_sched);
1895 ptr=realloc(schedule->runs,count*sizeof(int));
1897 perror("[moldyn] realloc (runs)");
1901 schedule->runs[count-1]=runs;
1903 ptr=realloc(schedule->tau,count*sizeof(double));
1905 perror("[moldyn] realloc (tau)");
1909 schedule->tau[count-1]=tau;
1911 printf("[moldyn] schedule added:\n");
1912 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1918 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1920 moldyn->schedule.hook=hook;
1921 moldyn->schedule.hook_params=hook_params;
1928 * 'integration of newtons equation' - algorithms
1932 /* start the integration */
1934 int moldyn_integrate(t_moldyn *moldyn) {
1937 unsigned int e,m,s,v,p,t,a;
1939 t_moldyn_schedule *sched;
1944 double energy_scale;
1945 struct timeval t1,t2;
1948 #ifdef VISUAL_THREAD
1950 pthread_t io_thread;
1959 sched=&(moldyn->schedule);
1962 /* initialize linked cell method */
1963 link_cell_init(moldyn,VERBOSE);
1965 /* logging & visualization */
1974 /* sqaure of some variables */
1975 moldyn->tau_square=moldyn->tau*moldyn->tau;
1977 /* get current time */
1978 gettimeofday(&t1,NULL);
1980 /* calculate initial forces */
1981 potential_force_calc(moldyn);
1986 /* some stupid checks before we actually start calculating bullshit */
1987 if(moldyn->cutoff>0.5*moldyn->dim.x)
1988 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1989 if(moldyn->cutoff>0.5*moldyn->dim.y)
1990 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1991 if(moldyn->cutoff>0.5*moldyn->dim.z)
1992 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1994 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1995 if(ds>0.05*moldyn->nnd)
1996 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1999 /* zero absolute time */
2000 // should have right values!
2002 //moldyn->total_steps=0;
2004 /* debugging, ignore */
2007 /* zero & init moldyn copy */
2008 #ifdef VISUAL_THREAD
2009 memset(&md_copy,0,sizeof(t_moldyn));
2010 atom_copy=malloc(moldyn->count*sizeof(t_atom));
2011 if(atom_copy==NULL) {
2012 perror("[moldyn] malloc atom copy (init)");
2018 printf("##################\n");
2019 printf("# USING PTHREADS #\n");
2020 printf("##################\n");
2022 /* tell the world */
2023 printf("[moldyn] integration start, go get a coffee ...\n");
2025 /* executing the schedule */
2027 while(sched->count<sched->total_sched) {
2029 /* setting amount of runs and finite time step size */
2030 moldyn->tau=sched->tau[sched->count];
2031 moldyn->tau_square=moldyn->tau*moldyn->tau;
2032 moldyn->time_steps=sched->runs[sched->count];
2034 /* energy scaling factor (might change!) */
2035 energy_scale=moldyn->count*EV;
2037 /* integration according to schedule */
2039 for(i=0;i<moldyn->time_steps;i++) {
2041 /* integration step */
2042 moldyn->integrate(moldyn);
2044 /* calculate kinetic energy, temperature and pressure */
2046 temperature_calc(moldyn);
2048 pressure_calc(moldyn);
2050 thermodynamic_pressure_calc(moldyn);
2051 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
2055 /* calculate fluctuations + averages */
2056 average_and_fluctuation_calc(moldyn);
2059 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
2060 scale_velocity(moldyn,FALSE);
2061 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
2062 scale_volume(moldyn);
2064 /* check for log & visualization */
2066 if(!(moldyn->total_steps%e))
2067 dprintf(moldyn->efd,
2069 moldyn->time,moldyn->ekin/energy_scale,
2070 moldyn->energy/energy_scale,
2071 get_total_energy(moldyn)/energy_scale);
2074 if(!(moldyn->total_steps%m)) {
2075 momentum=get_total_p(moldyn);
2076 dprintf(moldyn->mfd,
2077 "%f %f %f %f %f\n",moldyn->time,
2078 momentum.x,momentum.y,momentum.z,
2079 v3_norm(&momentum));
2083 if(!(moldyn->total_steps%p)) {
2084 dprintf(moldyn->pfd,
2085 "%f %f %f %f %f %f %f\n",moldyn->time,
2086 moldyn->p/BAR,moldyn->p_avg/BAR,
2087 moldyn->p/BAR,moldyn->p_avg/BAR,
2088 moldyn->tp/BAR,moldyn->tp_avg/BAR);
2092 if(!(moldyn->total_steps%t)) {
2093 dprintf(moldyn->tfd,
2095 moldyn->time,moldyn->t,moldyn->t_avg);
2099 if(!(moldyn->total_steps%v)) {
2100 dprintf(moldyn->vfd,
2101 "%f %f %f %f %f\n",moldyn->time,
2109 if(!(moldyn->total_steps%s)) {
2110 snprintf(dir,128,"%s/s-%08.f.save",
2111 moldyn->vlsdir,moldyn->time);
2112 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
2114 if(fd<0) perror("[moldyn] save fd open");
2116 write(fd,moldyn,sizeof(t_moldyn));
2117 write(fd,moldyn->atom,
2118 moldyn->count*sizeof(t_atom));
2124 if(!(moldyn->total_steps%a)) {
2125 #ifdef VISUAL_THREAD
2126 /* check whether thread has not terminated yet */
2128 ret=pthread_join(io_thread,NULL);
2131 /* prepare and start thread */
2132 if(moldyn->count!=md_copy.count) {
2136 memcpy(&md_copy,moldyn,sizeof(t_moldyn));
2138 atom_copy=malloc(moldyn->count*sizeof(t_atom));
2139 if(atom_copy==NULL) {
2140 perror("[moldyn] malloc atom copy (change)");
2144 md_copy.atom=atom_copy;
2145 memcpy(atom_copy,moldyn->atom,moldyn->count*sizeof(t_atom));
2147 ret=pthread_create(&io_thread,NULL,visual_atoms,&md_copy);
2149 perror("[moldyn] create visual atoms thread\n");
2153 visual_atoms(moldyn);
2158 /* display progress */
2160 /* get current time */
2161 gettimeofday(&t2,NULL);
2163 printf("sched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)\n",
2164 sched->count,i,moldyn->total_steps,
2165 moldyn->t,moldyn->t_avg,
2166 moldyn->p/BAR,moldyn->p_avg/BAR,
2167 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
2169 (int)(t2.tv_sec-t1.tv_sec));
2173 /* copy over time */
2177 /* increase absolute time */
2178 moldyn->time+=moldyn->tau;
2179 moldyn->total_steps+=1;
2183 /* check for hooks */
2185 printf("\n ## schedule hook %d start ##\n",
2187 sched->hook(moldyn,sched->hook_params);
2188 printf(" ## schedule hook end ##\n");
2191 /* increase the schedule counter */
2199 /* velocity verlet */
2201 int velocity_verlet(t_moldyn *moldyn) {
2204 double tau,tau_square,h;
2209 count=moldyn->count;
2211 tau_square=moldyn->tau_square;
2213 for(i=0;i<count;i++) {
2214 /* check whether fixed atom */
2215 if(atom[i].attr&ATOM_ATTR_FP)
2219 v3_scale(&delta,&(atom[i].v),tau);
2220 v3_add(&(atom[i].r),&(atom[i].r),&delta);
2221 v3_scale(&delta,&(atom[i].f),h*tau_square);
2222 v3_add(&(atom[i].r),&(atom[i].r),&delta);
2223 check_per_bound(moldyn,&(atom[i].r));
2225 /* velocities [actually v(t+tau/2)] */
2226 v3_scale(&delta,&(atom[i].f),h*tau);
2227 v3_add(&(atom[i].v),&(atom[i].v),&delta);
2230 /* criticial check */
2231 moldyn_bc_check(moldyn);
2233 /* neighbour list update */
2234 link_cell_update(moldyn);
2236 /* forces depending on chosen potential */
2238 potential_force_calc(moldyn);
2240 albe_potential_force_calc(moldyn);
2243 for(i=0;i<count;i++) {
2244 /* check whether fixed atom */
2245 if(atom[i].attr&ATOM_ATTR_FP)
2247 /* again velocities [actually v(t+tau)] */
2248 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
2249 v3_add(&(atom[i].v),&(atom[i].v),&delta);
2258 * potentials & corresponding forces & virial routine
2262 /* generic potential and force calculation */
2264 int potential_force_calc(t_moldyn *moldyn) {
2267 t_atom *itom,*jtom,*ktom;
2271 int *neighbour_i[27];
2275 int neighbour_i[27];
2278 t_list neighbour_i[27];
2279 t_list neighbour_i2[27];
2285 count=moldyn->count;
2295 /* reset global virial */
2296 memset(&(moldyn->gvir),0,sizeof(t_virial));
2298 /* reset force, site energy and virial of every atom */
2300 i=omp_get_thread_num();
2301 #pragma omp parallel for private(virial)
2303 for(i=0;i<count;i++) {
2306 v3_zero(&(itom[i].f));
2309 virial=(&(itom[i].virial));
2317 /* reset site energy */
2322 /* get energy, force and virial of every atom */
2324 /* first (and only) loop over atoms i */
2325 for(i=0;i<count;i++) {
2327 /* single particle potential/force */
2328 if(itom[i].attr&ATOM_ATTR_1BP)
2330 moldyn->func1b(moldyn,&(itom[i]));
2332 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
2335 /* 2 body pair potential/force */
2337 link_cell_neighbour_index(moldyn,
2338 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2339 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
2340 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
2345 /* first loop over atoms j */
2346 if(moldyn->func2b) {
2353 while(neighbour_i[j][p]!=-1) {
2355 jtom=&(atom[neighbour_i[j][p]]);
2363 p=lc->subcell->list[p];
2365 this=&(neighbour_i[j]);
2368 if(this->start==NULL)
2372 jtom=this->current->data;
2375 if(jtom==&(itom[i]))
2378 if((jtom->attr&ATOM_ATTR_2BP)&
2379 (itom[i].attr&ATOM_ATTR_2BP)) {
2380 moldyn->func2b(moldyn,
2390 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2396 /* 3 body potential/force */
2398 if(!(itom[i].attr&ATOM_ATTR_3BP))
2401 /* copy the neighbour lists */
2403 /* no copy needed for static lists */
2405 /* no copy needed for lowmem lists */
2407 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2410 /* second loop over atoms j */
2417 while(neighbour_i[j][p]!=-1) {
2419 jtom=&(atom[neighbour_i[j][p]]);
2427 p=lc->subcell->list[p];
2429 this=&(neighbour_i[j]);
2432 if(this->start==NULL)
2437 jtom=this->current->data;
2440 if(jtom==&(itom[i]))
2443 if(!(jtom->attr&ATOM_ATTR_3BP))
2449 if(moldyn->func3b_j1)
2450 moldyn->func3b_j1(moldyn,
2455 /* in first j loop, 3bp run can be skipped */
2456 if(!(moldyn->run3bp))
2459 /* first loop over atoms k */
2460 if(moldyn->func3b_k1) {
2468 while(neighbour_i[k][q]!=-1) {
2470 ktom=&(atom[neighbour_i[k][q]]);
2478 q=lc->subcell->list[q];
2480 that=&(neighbour_i2[k]);
2483 if(that->start==NULL)
2487 ktom=that->current->data;
2490 if(!(ktom->attr&ATOM_ATTR_3BP))
2496 if(ktom==&(itom[i]))
2499 moldyn->func3b_k1(moldyn,
2510 } while(list_next_f(that)!=\
2518 if(moldyn->func3b_j2)
2519 moldyn->func3b_j2(moldyn,
2524 /* second loop over atoms k */
2525 if(moldyn->func3b_k2) {
2533 while(neighbour_i[k][q]!=-1) {
2535 ktom=&(atom[neighbour_i[k][q]]);
2543 q=lc->subcell->list[q];
2545 that=&(neighbour_i2[k]);
2548 if(that->start==NULL)
2552 ktom=that->current->data;
2555 if(!(ktom->attr&ATOM_ATTR_3BP))
2561 if(ktom==&(itom[i]))
2564 moldyn->func3b_k2(moldyn,
2575 } while(list_next_f(that)!=\
2583 /* 2bp post function */
2584 if(moldyn->func3b_j3) {
2585 moldyn->func3b_j3(moldyn,
2594 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2609 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2610 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2612 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2613 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2614 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2618 /* some postprocessing */
2620 #pragma omp parallel for
2622 for(i=0;i<count;i++) {
2623 /* calculate global virial */
2624 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2625 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2626 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2627 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2628 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2629 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2631 /* check forces regarding the given timestep */
2632 if(v3_norm(&(itom[i].f))>\
2633 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2634 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2642 * virial calculation
2645 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2646 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2648 a->virial.xx+=f->x*d->x;
2649 a->virial.yy+=f->y*d->y;
2650 a->virial.zz+=f->z*d->z;
2651 a->virial.xy+=f->x*d->y;
2652 a->virial.xz+=f->x*d->z;
2653 a->virial.yz+=f->y*d->z;
2659 * periodic boundary checking
2662 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2663 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2674 if(moldyn->status&MOLDYN_STAT_PBX) {
2675 if(a->x>=x) a->x-=dim->x;
2676 else if(-a->x>x) a->x+=dim->x;
2678 if(moldyn->status&MOLDYN_STAT_PBY) {
2679 if(a->y>=y) a->y-=dim->y;
2680 else if(-a->y>y) a->y+=dim->y;
2682 if(moldyn->status&MOLDYN_STAT_PBZ) {
2683 if(a->z>=z) a->z-=dim->z;
2684 else if(-a->z>z) a->z+=dim->z;
2691 * debugging / critical check functions
2694 int moldyn_bc_check(t_moldyn *moldyn) {
2707 for(i=0;i<moldyn->count;i++) {
2708 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2709 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2710 i,atom[i].r.x,dim->x/2);
2711 printf("diagnostic:\n");
2712 printf("-----------\natom.r.x:\n");
2714 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2717 ((byte)&(1<<k))?1:0,
2720 printf("---------------\nx=dim.x/2:\n");
2722 memcpy(&byte,(u8 *)(&x)+j,1);
2725 ((byte)&(1<<k))?1:0,
2728 if(atom[i].r.x==x) printf("the same!\n");
2729 else printf("different!\n");
2731 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2732 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2733 i,atom[i].r.y,dim->y/2);
2734 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2735 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2736 i,atom[i].r.z,dim->z/2);
2746 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2753 fd=open(file,O_RDONLY);
2755 perror("[moldyn] load save file open");
2759 fsize=lseek(fd,0,SEEK_END);
2760 lseek(fd,0,SEEK_SET);
2762 size=sizeof(t_moldyn);
2765 cnt=read(fd,moldyn,size);
2767 perror("[moldyn] load save file read (moldyn)");
2773 size=moldyn->count*sizeof(t_atom);
2775 /* correcting possible atom data offset */
2777 if(fsize!=sizeof(t_moldyn)+size) {
2778 corr=fsize-sizeof(t_moldyn)-size;
2779 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2780 printf(" moifying offset:\n");
2781 printf(" - current pos: %d\n",sizeof(t_moldyn));
2782 printf(" - atom size: %d\n",size);
2783 printf(" - file size: %d\n",fsize);
2784 printf(" => correction: %d\n",corr);
2785 lseek(fd,corr,SEEK_CUR);
2788 moldyn->atom=(t_atom *)malloc(size);
2789 if(moldyn->atom==NULL) {
2790 perror("[moldyn] load save file malloc (atoms)");
2795 cnt=read(fd,moldyn->atom,size);
2797 perror("[moldyn] load save file read (atoms)");
2804 amutex=malloc(moldyn->count*sizeof(pthread_mutex_t));
2806 perror("[moldyn] load save file (mutexes)");
2809 for(cnt=0;cnt<moldyn->count;cnt++)
2810 pthread_mutex_init(&(amutex[cnt]),NULL);
2818 int moldyn_free_save_file(t_moldyn *moldyn) {
2825 int moldyn_load(t_moldyn *moldyn) {
2833 * function to find/callback all combinations of 2 body bonds
2836 int process_2b_bonds(t_moldyn *moldyn,void *data,
2837 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2838 void *data,u8 bc)) {
2848 t_list neighbour[27];
2858 for(i=0;i<moldyn->count;i++) {
2859 /* neighbour indexing */
2860 link_cell_neighbour_index(moldyn,
2861 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2862 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2863 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2868 bc=(j<lc->dnlc)?0:1;
2873 while(neighbour[j][p]!=-1) {
2875 jtom=&(moldyn->atom[neighbour[j][p]]);
2883 p=lc->subcell->list[p];
2885 this=&(neighbour[j]);
2888 if(this->start==NULL)
2893 jtom=this->current->data;
2897 process(moldyn,&(itom[i]),jtom,data,bc);
2904 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2914 * function to find neighboured atoms
2917 int process_neighbours(t_moldyn *moldyn,void *data,t_atom *atom,
2918 int (*process)(t_moldyn *moldyn,t_atom *atom,t_atom *natom,
2919 void *data,u8 bc)) {
2929 t_list neighbour[27];
2938 /* neighbour indexing */
2939 link_cell_neighbour_index(moldyn,
2940 (atom->r.x+moldyn->dim.x/2)/lc->x,
2941 (atom->r.y+moldyn->dim.y/2)/lc->x,
2942 (atom->r.z+moldyn->dim.z/2)/lc->x,
2947 bc=(j<lc->dnlc)?0:1;
2952 while(neighbour[j][p]!=-1) {
2954 natom=&(moldyn->atom[neighbour[j][p]]);
2961 natom=&(moldyn->atom[p]);
2962 p=lc->subcell->list[p];
2964 this=&(neighbour[j]);
2967 if(this->start==NULL)
2972 natom=this->current->data;
2976 process(moldyn,atom,natom,data,bc);
2983 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2992 * post processing functions
2995 int get_line(int fd,char *line,int max) {
3002 if(count==max) return count;
3003 ret=read(fd,line+count,1);
3004 if(ret<=0) return ret;
3005 if(line[count]=='\n') {
3006 memset(line+count,0,max-count-1);
3014 int pair_correlation_init(t_moldyn *moldyn,double dr) {
3020 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
3036 for(i=0;i<moldyn->count;i++) {
3038 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
3039 check_per_bound(moldyn,&dist);
3040 d2=v3_absolute_square(&dist);
3054 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
3055 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
3056 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
3061 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
3066 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
3067 t_atom *jtom,void *data,u8 bc) {
3074 /* only count pairs once,
3075 * skip same atoms */
3076 if(itom->tag>=jtom->tag)
3080 * pair correlation calc
3087 v3_sub(&dist,&(jtom->r),&(itom->r));
3088 if(bc) check_per_bound(moldyn,&dist);
3089 d=v3_absolute_square(&dist);
3091 /* ignore if greater cutoff */
3092 if(d>moldyn->cutoff_square)
3095 /* fill the slots */
3099 /* should never happen but it does 8) -
3100 * related to -ffloat-store problem! */
3102 printf("[moldyn] WARNING: pcc (%d/%d)",
3108 if(itom->brand!=jtom->brand) {
3113 /* type a - type a bonds */
3115 pcc->stat[s+pcc->o1]+=1;
3117 /* type b - type b bonds */
3118 pcc->stat[s+pcc->o2]+=1;
3124 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
3131 pcc.o1=moldyn->cutoff/dr;
3134 if(pcc.o1*dr<=moldyn->cutoff)
3135 printf("[moldyn] WARNING: pcc (low #slots)\n");
3137 printf("[moldyn] pair correlation calc info:\n");
3138 printf(" time: %f\n",moldyn->time);
3139 printf(" count: %d\n",moldyn->count);
3140 printf(" cutoff: %f\n",moldyn->cutoff);
3141 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
3144 pcc.stat=(double *)ptr;
3147 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
3148 if(pcc.stat==NULL) {
3149 perror("[moldyn] pair correlation malloc");
3154 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
3157 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
3160 for(i=1;i<pcc.o1;i++) {
3161 // normalization: 4 pi r^2 dr
3162 // here: not double counting pairs -> 2 pi r r dr
3163 // ... and actually it's a constant times r^2
3166 pcc.stat[pcc.o1+i]/=norm;
3167 pcc.stat[pcc.o2+i]/=norm;
3172 /* todo: store/print pair correlation function */
3179 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
3186 if(itom->tag>=jtom->tag)
3190 v3_sub(&dist,&(jtom->r),&(itom->r));
3191 if(bc) check_per_bound(moldyn,&dist);
3192 d=v3_absolute_square(&dist);
3194 /* ignore if greater or equal cutoff */
3195 if(d>moldyn->cutoff_square)
3198 /* check for potential bond */
3199 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
3202 /* now count this bonding ... */
3205 /* increase total bond counter
3206 * ... double counting!
3211 ba->acnt[jtom->tag]+=1;
3213 ba->bcnt[jtom->tag]+=1;
3216 ba->acnt[itom->tag]+=1;
3218 ba->bcnt[itom->tag]+=1;
3223 int bond_analyze(t_moldyn *moldyn,double *quality) {
3225 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
3233 ba.acnt=malloc(moldyn->count*sizeof(int));
3235 perror("[moldyn] bond analyze malloc (a)");
3238 memset(ba.acnt,0,moldyn->count*sizeof(int));
3240 ba.bcnt=malloc(moldyn->count*sizeof(int));
3242 perror("[moldyn] bond analyze malloc (b)");
3245 memset(ba.bcnt,0,moldyn->count*sizeof(int));
3254 process_2b_bonds(moldyn,&ba,bond_analyze_process);
3256 for(i=0;i<moldyn->count;i++) {
3257 if(atom[i].brand==0) {
3258 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
3262 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
3270 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
3271 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
3274 quality[0]=1.0*ccnt/cset;
3275 quality[1]=1.0*qcnt/ba.tcnt;
3278 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
3279 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
3286 * visualization code
3289 int visual_init(t_moldyn *moldyn,char *filebase) {
3291 strncpy(moldyn->vis.fb,filebase,128);
3296 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
3303 if(itom->tag>=jtom->tag)
3306 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
3309 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
3310 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
3311 itom->r.x,itom->r.y,itom->r.z,
3312 jtom->r.x,jtom->r.y,jtom->r.z);
3317 #ifdef VISUAL_THREAD
3318 void *visual_atoms(void *ptr) {
3320 int visual_atoms(t_moldyn *moldyn) {
3331 #ifdef VISUAL_THREAD
3345 sprintf(file,"%s/atomic_conf_%08.f.xyz",v->fb,moldyn->time);
3346 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
3348 perror("open visual save file fd");
3349 #ifndef VISUAL_THREAD
3354 /* write the actual data file */
3357 dprintf(vb.fd,"# [P] %d %08.f <%f,%f,%f>\n",
3358 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
3360 // atomic configuration
3361 for(i=0;i<moldyn->count;i++) {
3362 v3_sub(&strain,&(atom[i].r),&(atom[i].r_0));
3363 check_per_bound(moldyn,&strain);
3364 // atom type, positions, color and kinetic energy
3365 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
3369 pse_col[atom[i].element],
3371 sqrt(v3_absolute_square(&strain)));
3374 // bonds between atoms
3375 #ifndef VISUAL_THREAD
3376 process_2b_bonds(moldyn,&vb,visual_bonds_process);
3381 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3382 -dim.x/2,-dim.y/2,-dim.z/2,
3383 dim.x/2,-dim.y/2,-dim.z/2);
3384 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3385 -dim.x/2,-dim.y/2,-dim.z/2,
3386 -dim.x/2,dim.y/2,-dim.z/2);
3387 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3388 dim.x/2,dim.y/2,-dim.z/2,
3389 dim.x/2,-dim.y/2,-dim.z/2);
3390 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3391 -dim.x/2,dim.y/2,-dim.z/2,
3392 dim.x/2,dim.y/2,-dim.z/2);
3394 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3395 -dim.x/2,-dim.y/2,dim.z/2,
3396 dim.x/2,-dim.y/2,dim.z/2);
3397 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3398 -dim.x/2,-dim.y/2,dim.z/2,
3399 -dim.x/2,dim.y/2,dim.z/2);
3400 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3401 dim.x/2,dim.y/2,dim.z/2,
3402 dim.x/2,-dim.y/2,dim.z/2);
3403 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3404 -dim.x/2,dim.y/2,dim.z/2,
3405 dim.x/2,dim.y/2,dim.z/2);
3407 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3408 -dim.x/2,-dim.y/2,dim.z/2,
3409 -dim.x/2,-dim.y/2,-dim.z/2);
3410 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3411 -dim.x/2,dim.y/2,dim.z/2,
3412 -dim.x/2,dim.y/2,-dim.z/2);
3413 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3414 dim.x/2,-dim.y/2,dim.z/2,
3415 dim.x/2,-dim.y/2,-dim.z/2);
3416 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3417 dim.x/2,dim.y/2,dim.z/2,
3418 dim.x/2,dim.y/2,-dim.z/2);
3423 #ifdef VISUAL_THREAD
3434 * fpu cntrol functions
3437 // set rounding to double (eliminates -ffloat-store!)
3438 int fpu_set_rtd(void) {
3444 ctrl&=~_FPU_EXTENDED;