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"
52 pthread_mutex_t *amutex;
53 pthread_mutex_t emutex;
57 * the moldyn functions
60 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
62 printf("[moldyn] init\n");
64 /* only needed if compiled without -msse2 (float-store prob!) */
67 memset(moldyn,0,sizeof(t_moldyn));
72 rand_init(&(moldyn->random),NULL,1);
73 moldyn->random.status|=RAND_STAT_VERBOSE;
76 pthread_mutex_init(&emutex,NULL);
82 int moldyn_shutdown(t_moldyn *moldyn) {
88 printf("[moldyn] shutdown\n");
91 for(i=0;i<moldyn->count;i++)
92 pthread_mutex_destroy(&(amutex[i]));
93 pthread_mutex_destroy(&emutex);
96 moldyn_log_shutdown(moldyn);
97 link_cell_shutdown(moldyn);
98 rand_close(&(moldyn->random));
104 int set_int_alg(t_moldyn *moldyn,u8 algo) {
106 printf("[moldyn] integration algorithm: ");
109 case MOLDYN_INTEGRATE_VERLET:
110 moldyn->integrate=velocity_verlet;
111 printf("velocity verlet\n");
114 printf("unknown integration algorithm: %02x\n",algo);
122 int set_cutoff(t_moldyn *moldyn,double cutoff) {
124 moldyn->cutoff=cutoff;
125 moldyn->cutoff_square=cutoff*cutoff;
127 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
132 int set_temperature(t_moldyn *moldyn,double t_ref) {
136 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
141 int set_pressure(t_moldyn *moldyn,double p_ref) {
145 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
150 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
152 moldyn->pt_scale&=(~(P_SCALE_MASK));
153 moldyn->pt_scale|=ptype;
156 printf("[moldyn] p scaling:\n");
158 printf(" p: %s",ptype?"yes":"no ");
160 printf(" | type: %02x | factor: %f",ptype,ptc);
166 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
168 moldyn->pt_scale&=(~(T_SCALE_MASK));
169 moldyn->pt_scale|=ttype;
172 printf("[moldyn] t scaling:\n");
174 printf(" t: %s",ttype?"yes":"no ");
176 printf(" | type: %02x | factor: %f",ttype,ttc);
182 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
184 moldyn->pt_scale=(ptype|ttype);
188 printf("[moldyn] p/t scaling:\n");
190 printf(" p: %s",ptype?"yes":"no ");
192 printf(" | type: %02x | factor: %f",ptype,ptc);
195 printf(" t: %s",ttype?"yes":"no ");
197 printf(" | type: %02x | factor: %f",ttype,ttc);
203 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
209 moldyn->volume=x*y*z;
217 printf("[moldyn] dimensions in A and A^3 respectively:\n");
218 printf(" x: %f\n",moldyn->dim.x);
219 printf(" y: %f\n",moldyn->dim.y);
220 printf(" z: %f\n",moldyn->dim.z);
221 printf(" volume: %f\n",moldyn->volume);
222 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
227 int set_nn_dist(t_moldyn *moldyn,double dist) {
234 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
236 printf("[moldyn] periodic boundary conditions:\n");
239 moldyn->status|=MOLDYN_STAT_PBX;
242 moldyn->status|=MOLDYN_STAT_PBY;
245 moldyn->status|=MOLDYN_STAT_PBZ;
247 printf(" x: %s\n",x?"yes":"no");
248 printf(" y: %s\n",y?"yes":"no");
249 printf(" z: %s\n",z?"yes":"no");
254 int set_potential(t_moldyn *moldyn,u8 type) {
257 case MOLDYN_POTENTIAL_TM:
258 moldyn->func1b=tersoff_mult_1bp;
259 moldyn->func3b_j1=tersoff_mult_3bp_j1;
260 moldyn->func3b_k1=tersoff_mult_3bp_k1;
261 moldyn->func3b_j2=tersoff_mult_3bp_j2;
262 moldyn->func3b_k2=tersoff_mult_3bp_k2;
263 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
265 case MOLDYN_POTENTIAL_AM:
266 moldyn->func3b_j1=albe_mult_3bp_j1;
267 moldyn->func3b_k1=albe_mult_3bp_k1;
268 moldyn->func3b_j2=albe_mult_3bp_j2;
269 moldyn->func3b_k2=albe_mult_3bp_k2;
270 moldyn->check_2b_bond=albe_mult_check_2b_bond;
272 case MOLDYN_POTENTIAL_HO:
273 moldyn->func2b=harmonic_oscillator;
274 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
276 case MOLDYN_POTENTIAL_LJ:
277 moldyn->func2b=lennard_jones;
278 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
281 printf("[moldyn] set potential: unknown type %02x\n",
289 int set_avg_skip(t_moldyn *moldyn,int skip) {
291 printf("[moldyn] skip %d steps before starting average calc\n",skip);
292 moldyn->avg_skip=skip;
297 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
299 strncpy(moldyn->vlsdir,dir,127);
304 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
306 strncpy(moldyn->rauthor,author,63);
307 strncpy(moldyn->rtitle,title,63);
312 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
317 printf("[moldyn] set log: ");
320 case LOG_TOTAL_ENERGY:
321 moldyn->ewrite=timer;
322 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
323 moldyn->efd=open(filename,
324 O_WRONLY|O_CREAT|O_EXCL,
327 perror("[moldyn] energy log fd open");
330 dprintf(moldyn->efd,"# total energy log file\n");
331 printf("total energy (%d)\n",timer);
333 case LOG_TOTAL_MOMENTUM:
334 moldyn->mwrite=timer;
335 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
336 moldyn->mfd=open(filename,
337 O_WRONLY|O_CREAT|O_EXCL,
340 perror("[moldyn] momentum log fd open");
343 dprintf(moldyn->efd,"# total momentum log file\n");
344 printf("total momentum (%d)\n",timer);
347 moldyn->pwrite=timer;
348 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
349 moldyn->pfd=open(filename,
350 O_WRONLY|O_CREAT|O_EXCL,
353 perror("[moldyn] pressure log file\n");
356 dprintf(moldyn->pfd,"# pressure log file\n");
357 printf("pressure (%d)\n",timer);
359 case LOG_TEMPERATURE:
360 moldyn->twrite=timer;
361 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
362 moldyn->tfd=open(filename,
363 O_WRONLY|O_CREAT|O_EXCL,
366 perror("[moldyn] temperature log file\n");
369 dprintf(moldyn->tfd,"# temperature log file\n");
370 printf("temperature (%d)\n",timer);
373 moldyn->vwrite=timer;
374 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
375 moldyn->vfd=open(filename,
376 O_WRONLY|O_CREAT|O_EXCL,
379 perror("[moldyn] volume log file\n");
382 dprintf(moldyn->vfd,"# volume log file\n");
383 printf("volume (%d)\n",timer);
386 moldyn->swrite=timer;
387 printf("save file (%d)\n",timer);
390 moldyn->awrite=timer;
391 ret=visual_init(moldyn,moldyn->vlsdir);
393 printf("[moldyn] visual init failure\n");
396 printf("visual file (%d)\n",timer);
399 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
400 moldyn->rfd=open(filename,
401 O_WRONLY|O_CREAT|O_EXCL,
404 perror("[moldyn] report fd open");
407 printf("report -> ");
409 snprintf(filename,127,"%s/e_plot.scr",
411 moldyn->epfd=open(filename,
412 O_WRONLY|O_CREAT|O_EXCL,
415 perror("[moldyn] energy plot fd open");
418 dprintf(moldyn->epfd,e_plot_script);
423 snprintf(filename,127,"%s/pressure_plot.scr",
425 moldyn->ppfd=open(filename,
426 O_WRONLY|O_CREAT|O_EXCL,
429 perror("[moldyn] p plot fd open");
432 dprintf(moldyn->ppfd,pressure_plot_script);
437 snprintf(filename,127,"%s/temperature_plot.scr",
439 moldyn->tpfd=open(filename,
440 O_WRONLY|O_CREAT|O_EXCL,
443 perror("[moldyn] t plot fd open");
446 dprintf(moldyn->tpfd,temperature_plot_script);
448 printf("temperature ");
450 dprintf(moldyn->rfd,report_start,
451 moldyn->rauthor,moldyn->rtitle);
455 printf("unknown log type: %02x\n",type);
462 int moldyn_log_shutdown(t_moldyn *moldyn) {
466 printf("[moldyn] log shutdown\n");
470 dprintf(moldyn->rfd,report_energy);
471 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
476 if(moldyn->mfd) close(moldyn->mfd);
480 dprintf(moldyn->rfd,report_pressure);
481 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
488 dprintf(moldyn->rfd,report_temperature);
489 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
494 dprintf(moldyn->rfd,report_end);
496 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
499 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
502 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
511 * creating lattice functions
514 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
515 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
524 pthread_mutex_t *mutex;
530 /* how many atoms do we expect */
533 printf("[moldyn] WARNING: create 'none' lattice called");
535 if(type==CUBIC) new*=1;
536 if(type==FCC) new*=4;
537 if(type==DIAMOND) new*=8;
539 /* allocate space for atoms */
540 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
542 perror("[moldyn] realloc (create lattice)");
546 atom=&(moldyn->atom[count]);
549 ptr=realloc(amutex,(count+new)*sizeof(pthread_mutex_t));
551 perror("[moldyn] mutex realloc (add atom)");
555 mutex=&(amutex[count]);
558 /* no atoms on the boundaries (only reason: it looks better!) */
572 set_nn_dist(moldyn,lc);
573 ret=cubic_init(a,b,c,lc,atom,&orig);
574 strcpy(name,"cubic");
578 v3_scale(&orig,&orig,0.5);
579 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
580 ret=fcc_init(a,b,c,lc,atom,&orig);
585 v3_scale(&orig,&orig,0.25);
586 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
587 ret=diamond_init(a,b,c,lc,atom,&orig);
588 strcpy(name,"diamond");
591 printf("unknown lattice type (%02x)\n",type);
597 printf("[moldyn] creating lattice failed\n");
598 printf(" amount of atoms\n");
599 printf(" - expected: %d\n",new);
600 printf(" - created: %d\n",ret);
605 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
607 for(ret=0;ret<new;ret++) {
608 atom[ret].element=element;
611 atom[ret].brand=brand;
612 atom[ret].tag=count+ret;
613 check_per_bound(moldyn,&(atom[ret].r));
614 atom[ret].r_0=atom[ret].r;
616 pthread_mutex_init(&(mutex[ret]),NULL);
620 /* update total system mass */
621 total_mass_calc(moldyn);
626 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
627 t_3dvec *r,t_3dvec *v) {
634 count=(moldyn->count)++; // asshole style!
636 ptr=realloc(atom,(count+1)*sizeof(t_atom));
638 perror("[moldyn] realloc (add atom)");
644 ptr=realloc(moldyn->lc.subcell->list,(count+1)*sizeof(int));
646 perror("[moldyn] list realloc (add atom)");
649 moldyn->lc.subcell->list=ptr;
653 ptr=realloc(amutex,(count+1)*sizeof(pthread_mutex_t));
655 perror("[moldyn] mutex realloc (add atom)");
659 pthread_mutex_init(&(amutex[count]),NULL);
664 /* initialize new atom */
665 memset(&(atom[count]),0,sizeof(t_atom));
668 atom[count].element=element;
669 atom[count].mass=mass;
670 atom[count].brand=brand;
671 atom[count].tag=count;
672 atom[count].attr=attr;
673 check_per_bound(moldyn,&(atom[count].r));
674 atom[count].r_0=atom[count].r;
676 /* update total system mass */
677 total_mass_calc(moldyn);
682 int del_atom(t_moldyn *moldyn,int tag) {
689 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
691 perror("[moldyn]malloc (del atom)");
695 for(cnt=0;cnt<tag;cnt++)
698 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
700 new[cnt-1].tag=cnt-1;
712 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
731 v3_copy(&(atom[count].r),&r);
740 for(i=0;i<count;i++) {
741 atom[i].r.x-=(a*lc)/2.0;
742 atom[i].r.y-=(b*lc)/2.0;
743 atom[i].r.z-=(c*lc)/2.0;
749 /* fcc lattice init */
750 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
763 /* construct the basis */
764 memset(basis,0,3*sizeof(t_3dvec));
772 /* fill up the room */
780 v3_copy(&(atom[count].r),&r);
783 /* the three face centered atoms */
785 v3_add(&n,&r,&basis[l]);
786 v3_copy(&(atom[count].r),&n);
795 /* coordinate transformation */
796 for(i=0;i<count;i++) {
797 atom[i].r.x-=(a*lc)/2.0;
798 atom[i].r.y-=(b*lc)/2.0;
799 atom[i].r.z-=(c*lc)/2.0;
805 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
810 count=fcc_init(a,b,c,lc,atom,origin);
816 if(origin) v3_add(&o,&o,origin);
818 count+=fcc_init(a,b,c,lc,&atom[count],&o);
823 int destroy_atoms(t_moldyn *moldyn) {
825 if(moldyn->atom) free(moldyn->atom);
830 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
833 * - gaussian distribution of velocities
834 * - zero total momentum
835 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
840 t_3dvec p_total,delta;
845 random=&(moldyn->random);
847 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
849 /* gaussian distribution of velocities */
851 for(i=0;i<moldyn->count;i++) {
852 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
854 v=sigma*rand_get_gauss(random);
856 p_total.x+=atom[i].mass*v;
858 v=sigma*rand_get_gauss(random);
860 p_total.y+=atom[i].mass*v;
862 v=sigma*rand_get_gauss(random);
864 p_total.z+=atom[i].mass*v;
867 /* zero total momentum */
868 v3_scale(&p_total,&p_total,1.0/moldyn->count);
869 for(i=0;i<moldyn->count;i++) {
870 v3_scale(&delta,&p_total,1.0/atom[i].mass);
871 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
874 /* velocity scaling */
875 scale_velocity(moldyn,equi_init);
880 double total_mass_calc(t_moldyn *moldyn) {
886 for(i=0;i<moldyn->count;i++)
887 moldyn->mass+=moldyn->atom[i].mass;
892 double temperature_calc(t_moldyn *moldyn) {
894 /* assume up to date kinetic energy, which is 3/2 N k_B T */
896 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
901 double get_temperature(t_moldyn *moldyn) {
906 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
916 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
919 /* get kinetic energy / temperature & count involved atoms */
922 for(i=0;i<moldyn->count;i++) {
923 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
924 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
929 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
930 else return 0; /* no atoms involved in scaling! */
932 /* (temporary) hack for e,t = 0 */
935 if(moldyn->t_ref!=0.0) {
936 thermal_init(moldyn,equi_init);
940 return 0; /* no scaling needed */
944 /* get scaling factor */
945 scale=moldyn->t_ref/moldyn->t;
949 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
950 scale=1.0+(scale-1.0)*moldyn->tau/moldyn->t_tc;
953 /* velocity scaling */
954 for(i=0;i<moldyn->count;i++) {
955 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
956 v3_scale(&(atom[i].v),&(atom[i].v),scale);
962 double ideal_gas_law_pressure(t_moldyn *moldyn) {
966 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
971 double virial_sum(t_moldyn *moldyn) {
976 /* virial (sum over atom virials) */
984 for(i=0;i<moldyn->count;i++) {
985 virial=&(moldyn->atom[i].virial);
986 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
987 moldyn->vir.xx+=virial->xx;
988 moldyn->vir.yy+=virial->yy;
989 moldyn->vir.zz+=virial->zz;
990 moldyn->vir.xy+=virial->xy;
991 moldyn->vir.xz+=virial->xz;
992 moldyn->vir.yz+=virial->yz;
995 /* global virial (absolute coordinates) */
996 virial=&(moldyn->gvir);
997 moldyn->gv=virial->xx+virial->yy+virial->zz;
999 return moldyn->virial;
1002 double pressure_calc(t_moldyn *moldyn) {
1006 * with W = 1/3 sum_i f_i r_i (- skipped!)
1007 * virial = sum_i f_i r_i
1009 * => P = (2 Ekin + virial) / (3V)
1012 /* assume up to date virial & up to date kinetic energy */
1014 /* pressure (atom virials) */
1015 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
1016 moldyn->p/=(3.0*moldyn->volume);
1018 /* pressure (absolute coordinates) */
1019 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
1020 moldyn->gp/=(3.0*moldyn->volume);
1025 int average_reset(t_moldyn *moldyn) {
1027 printf("[moldyn] average reset\n");
1029 /* update skip value */
1030 moldyn->avg_skip=moldyn->total_steps;
1032 /* kinetic energy */
1036 /* potential energy */
1044 moldyn->virial_sum=0.0;
1055 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1059 if(moldyn->total_steps<moldyn->avg_skip)
1062 denom=moldyn->total_steps+1-moldyn->avg_skip;
1064 /* assume up to date energies, temperature, pressure etc */
1066 /* kinetic energy */
1067 moldyn->k_sum+=moldyn->ekin;
1068 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1069 moldyn->k_avg=moldyn->k_sum/denom;
1070 moldyn->k2_avg=moldyn->k2_sum/denom;
1071 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1073 /* potential energy */
1074 moldyn->v_sum+=moldyn->energy;
1075 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1076 moldyn->v_avg=moldyn->v_sum/denom;
1077 moldyn->v2_avg=moldyn->v2_sum/denom;
1078 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1081 moldyn->t_sum+=moldyn->t;
1082 moldyn->t_avg=moldyn->t_sum/denom;
1085 moldyn->virial_sum+=moldyn->virial;
1086 moldyn->virial_avg=moldyn->virial_sum/denom;
1087 moldyn->gv_sum+=moldyn->gv;
1088 moldyn->gv_avg=moldyn->gv_sum/denom;
1091 moldyn->p_sum+=moldyn->p;
1092 moldyn->p_avg=moldyn->p_sum/denom;
1093 moldyn->gp_sum+=moldyn->gp;
1094 moldyn->gp_avg=moldyn->gp_sum/denom;
1095 moldyn->tp_sum+=moldyn->tp;
1096 moldyn->tp_avg=moldyn->tp_sum/denom;
1101 int get_heat_capacity(t_moldyn *moldyn) {
1105 /* averages needed for heat capacity calc */
1106 if(moldyn->total_steps<moldyn->avg_skip)
1109 /* (temperature average)^2 */
1110 temp2=moldyn->t_avg*moldyn->t_avg;
1111 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1114 /* ideal gas contribution */
1115 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1116 printf(" ideal gas contribution: %f\n",
1117 ighc/moldyn->mass*KILOGRAM/JOULE);
1119 /* specific heat for nvt ensemble */
1120 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1121 moldyn->c_v_nvt/=moldyn->mass;
1123 /* specific heat for nve ensemble */
1124 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1125 moldyn->c_v_nve/=moldyn->mass;
1127 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1128 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1129 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)));
1134 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1150 /* store atomic configuration + dimension */
1151 store=malloc(moldyn->count*sizeof(t_atom));
1153 printf("[moldyn] allocating store mem failed\n");
1156 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1161 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1162 su=pow(2.0-h,ONE_THIRD)-1.0;
1163 dv=(1.0-h)*moldyn->volume;
1165 /* scale up dimension and atom positions */
1166 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1167 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1168 link_cell_shutdown(moldyn);
1169 link_cell_init(moldyn,QUIET);
1170 potential_force_calc(moldyn);
1173 /* restore atomic configuration + dim */
1174 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1177 /* scale down dimension and atom positions */
1178 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1179 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1180 link_cell_shutdown(moldyn);
1181 link_cell_init(moldyn,QUIET);
1182 potential_force_calc(moldyn);
1185 /* calculate pressure */
1186 moldyn->tp=-(y1-y0)/(2.0*dv);
1188 /* restore atomic configuration */
1189 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1191 link_cell_shutdown(moldyn);
1192 link_cell_init(moldyn,QUIET);
1193 //potential_force_calc(moldyn);
1195 /* free store buffer */
1202 double get_pressure(t_moldyn *moldyn) {
1208 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1220 if(x) dim->x*=scale;
1221 if(y) dim->y*=scale;
1222 if(z) dim->z*=scale;
1227 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1238 for(i=0;i<moldyn->count;i++) {
1239 r=&(moldyn->atom[i].r);
1248 int scale_volume(t_moldyn *moldyn) {
1254 vdim=&(moldyn->vis.dim);
1258 /* scaling factor */
1259 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1260 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc*moldyn->tau;
1261 scale=pow(scale,ONE_THIRD);
1264 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1267 /* scale the atoms and dimensions */
1268 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1269 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1271 /* visualize dimensions */
1278 /* recalculate scaled volume */
1279 moldyn->volume=dim->x*dim->y*dim->z;
1281 /* adjust/reinit linkcell */
1282 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1283 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1284 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1285 link_cell_shutdown(moldyn);
1286 link_cell_init(moldyn,QUIET);
1297 double e_kin_calc(t_moldyn *moldyn) {
1305 for(i=0;i<moldyn->count;i++) {
1306 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1307 moldyn->ekin+=atom[i].ekin;
1310 return moldyn->ekin;
1313 double get_total_energy(t_moldyn *moldyn) {
1315 return(moldyn->ekin+moldyn->energy);
1318 t_3dvec get_total_p(t_moldyn *moldyn) {
1327 for(i=0;i<moldyn->count;i++) {
1328 v3_scale(&p,&(atom[i].v),atom[i].mass);
1329 v3_add(&p_total,&p_total,&p);
1335 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1339 /* nn_dist is the nearest neighbour distance */
1341 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1350 /* linked list / cell method */
1352 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1355 #ifndef LOWMEM_LISTS
1361 /* partitioning the md cell */
1362 lc->nx=moldyn->dim.x/moldyn->cutoff;
1363 lc->x=moldyn->dim.x/lc->nx;
1364 lc->ny=moldyn->dim.y/moldyn->cutoff;
1365 lc->y=moldyn->dim.y/lc->ny;
1366 lc->nz=moldyn->dim.z/moldyn->cutoff;
1367 lc->z=moldyn->dim.z/lc->nz;
1368 lc->cells=lc->nx*lc->ny*lc->nz;
1371 lc->subcell=malloc(lc->cells*sizeof(int*));
1373 lc->subcell=malloc(sizeof(t_lowmem_list));
1375 lc->subcell=malloc(lc->cells*sizeof(t_list));
1378 if(lc->subcell==NULL) {
1379 perror("[moldyn] cell init (malloc)");
1384 printf("[moldyn] FATAL: less then 27 subcells! (%d)\n",
1389 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1392 printf("[moldyn] initializing 'lowmem' linked cells (%d)\n",
1395 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1398 printf(" x: %d x %f A\n",lc->nx,lc->x);
1399 printf(" y: %d x %f A\n",lc->ny,lc->y);
1400 printf(" z: %d x %f A\n",lc->nz,lc->z);
1405 for(i=0;i<lc->cells;i++) {
1406 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1407 if(lc->subcell[i]==NULL) {
1408 perror("[moldyn] list init (malloc)");
1413 printf(" ---> %d malloc %p (%p)\n",
1414 i,lc->subcell[0],lc->subcell);
1418 lc->subcell->head=malloc(lc->cells*sizeof(int));
1419 if(lc->subcell->head==NULL) {
1420 perror("[moldyn] head init (malloc)");
1423 lc->subcell->list=malloc(moldyn->count*sizeof(int));
1424 if(lc->subcell->list==NULL) {
1425 perror("[moldyn] list init (malloc)");
1429 for(i=0;i<lc->cells;i++)
1430 list_init_f(&(lc->subcell[i]));
1433 /* update the list */
1434 link_cell_update(moldyn);
1439 int link_cell_update(t_moldyn *moldyn) {
1457 for(i=0;i<lc->cells;i++)
1459 memset(lc->subcell[i],-1,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1461 lc->subcell->head[i]=-1;
1463 list_destroy_f(&(lc->subcell[i]));
1466 for(count=0;count<moldyn->count;count++) {
1467 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1468 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1469 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1473 while(lc->subcell[i+j*nx+k*nxy][p]!=-1)
1476 if(p>=MAX_ATOMS_PER_LIST) {
1477 printf("[moldyn] FATAL: amount of atoms too high!\n");
1481 lc->subcell[i+j*nx+k*nxy][p]=count;
1484 lc->subcell->list[count]=lc->subcell->head[p];
1485 lc->subcell->head[p]=count;
1487 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nxy]),
1491 printf(" ---> %d %d malloc %p (%p)\n",
1492 i,count,lc->subcell[i].current,lc->subcell);
1500 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1526 if(i>=nx||j>=ny||k>=nz)
1527 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1530 #ifndef LOWMEM_LISTS
1531 cell[0]=lc->subcell[i+j*nx+k*a];
1533 cell[0]=lc->subcell->head[i+j*nx+k*a];
1535 for(ci=-1;ci<=1;ci++) {
1538 if((x<0)||(x>=nx)) {
1542 for(cj=-1;cj<=1;cj++) {
1545 if((y<0)||(y>=ny)) {
1549 for(ck=-1;ck<=1;ck++) {
1552 if((z<0)||(z>=nz)) {
1556 if(!(ci|cj|ck)) continue;
1558 #ifndef LOWMEM_LISTS
1559 cell[--count2]=lc->subcell[x+y*nx+z*a];
1561 cell[--count2]=lc->subcell->head[x+y*nx+z*a];
1566 #ifndef LOWMEM_LISTS
1567 cell[count1++]=lc->subcell[x+y*nx+z*a];
1569 cell[count1++]=lc->subcell->head[x+y*nx+z*a];
1581 int link_cell_shutdown(t_moldyn *moldyn) {
1583 #ifndef LOWMEM_LISTS
1591 free(lc->subcell->head);
1592 free(lc->subcell->list);
1596 for(i=0;i<lc->cells;i++) {
1598 free(lc->subcell[i]);
1600 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1601 list_destroy_f(&(lc->subcell[i]));
1611 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1615 t_moldyn_schedule *schedule;
1617 schedule=&(moldyn->schedule);
1618 count=++(schedule->total_sched);
1620 ptr=realloc(schedule->runs,count*sizeof(int));
1622 perror("[moldyn] realloc (runs)");
1626 schedule->runs[count-1]=runs;
1628 ptr=realloc(schedule->tau,count*sizeof(double));
1630 perror("[moldyn] realloc (tau)");
1634 schedule->tau[count-1]=tau;
1636 printf("[moldyn] schedule added:\n");
1637 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1643 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1645 moldyn->schedule.hook=hook;
1646 moldyn->schedule.hook_params=hook_params;
1653 * 'integration of newtons equation' - algorithms
1657 /* start the integration */
1659 int moldyn_integrate(t_moldyn *moldyn) {
1662 unsigned int e,m,s,v,p,t,a;
1664 t_moldyn_schedule *sched;
1669 double energy_scale;
1670 struct timeval t1,t2;
1673 #ifdef VISUAL_THREAD
1675 pthread_t io_thread;
1684 sched=&(moldyn->schedule);
1687 /* initialize linked cell method */
1688 link_cell_init(moldyn,VERBOSE);
1690 /* logging & visualization */
1699 /* sqaure of some variables */
1700 moldyn->tau_square=moldyn->tau*moldyn->tau;
1702 /* get current time */
1703 gettimeofday(&t1,NULL);
1705 /* calculate initial forces */
1706 potential_force_calc(moldyn);
1711 /* some stupid checks before we actually start calculating bullshit */
1712 if(moldyn->cutoff>0.5*moldyn->dim.x)
1713 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1714 if(moldyn->cutoff>0.5*moldyn->dim.y)
1715 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1716 if(moldyn->cutoff>0.5*moldyn->dim.z)
1717 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1719 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1720 if(ds>0.05*moldyn->nnd)
1721 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1724 /* zero absolute time */
1725 // should have right values!
1727 //moldyn->total_steps=0;
1729 /* debugging, ignore */
1732 /* zero & init moldyn copy */
1733 #ifdef VISUAL_THREAD
1734 memset(&md_copy,0,sizeof(t_moldyn));
1735 atom_copy=malloc(moldyn->count*sizeof(t_atom));
1736 if(atom_copy==NULL) {
1737 perror("[moldyn] malloc atom copy (init)");
1742 /* tell the world */
1743 printf("[moldyn] integration start, go get a coffee ...\n");
1745 /* executing the schedule */
1747 while(sched->count<sched->total_sched) {
1749 /* setting amount of runs and finite time step size */
1750 moldyn->tau=sched->tau[sched->count];
1751 moldyn->tau_square=moldyn->tau*moldyn->tau;
1752 moldyn->time_steps=sched->runs[sched->count];
1754 /* energy scaling factor (might change!) */
1755 energy_scale=moldyn->count*EV;
1757 /* integration according to schedule */
1759 for(i=0;i<moldyn->time_steps;i++) {
1761 /* integration step */
1762 moldyn->integrate(moldyn);
1764 /* calculate kinetic energy, temperature and pressure */
1766 temperature_calc(moldyn);
1768 pressure_calc(moldyn);
1770 thermodynamic_pressure_calc(moldyn);
1771 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1775 /* calculate fluctuations + averages */
1776 average_and_fluctuation_calc(moldyn);
1779 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1780 scale_velocity(moldyn,FALSE);
1781 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1782 scale_volume(moldyn);
1784 /* check for log & visualization */
1786 if(!(moldyn->total_steps%e))
1787 dprintf(moldyn->efd,
1789 moldyn->time,moldyn->ekin/energy_scale,
1790 moldyn->energy/energy_scale,
1791 get_total_energy(moldyn)/energy_scale);
1794 if(!(moldyn->total_steps%m)) {
1795 momentum=get_total_p(moldyn);
1796 dprintf(moldyn->mfd,
1797 "%f %f %f %f %f\n",moldyn->time,
1798 momentum.x,momentum.y,momentum.z,
1799 v3_norm(&momentum));
1803 if(!(moldyn->total_steps%p)) {
1804 dprintf(moldyn->pfd,
1805 "%f %f %f %f %f %f %f\n",moldyn->time,
1806 moldyn->p/BAR,moldyn->p_avg/BAR,
1807 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1808 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1812 if(!(moldyn->total_steps%t)) {
1813 dprintf(moldyn->tfd,
1815 moldyn->time,moldyn->t,moldyn->t_avg);
1819 if(!(moldyn->total_steps%v)) {
1820 dprintf(moldyn->vfd,
1821 "%f %f\n",moldyn->time,moldyn->volume);
1825 if(!(moldyn->total_steps%s)) {
1826 snprintf(dir,128,"%s/s-%08.f.save",
1827 moldyn->vlsdir,moldyn->time);
1828 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1830 if(fd<0) perror("[moldyn] save fd open");
1832 write(fd,moldyn,sizeof(t_moldyn));
1833 write(fd,moldyn->atom,
1834 moldyn->count*sizeof(t_atom));
1840 if(!(moldyn->total_steps%a)) {
1841 #ifdef VISUAL_THREAD
1842 /* check whether thread has not terminated yet */
1844 ret=pthread_join(io_thread,NULL);
1847 /* prepare and start thread */
1848 if(moldyn->count!=md_copy.count) {
1852 memcpy(&md_copy,moldyn,sizeof(t_moldyn));
1854 atom_copy=malloc(moldyn->count*sizeof(t_atom));
1855 if(atom_copy==NULL) {
1856 perror("[moldyn] malloc atom copy (change)");
1860 md_copy.atom=atom_copy;
1861 memcpy(atom_copy,moldyn->atom,moldyn->count*sizeof(t_atom));
1863 ret=pthread_create(&io_thread,NULL,visual_atoms,&md_copy);
1865 perror("[moldyn] create visual atoms thread\n");
1869 visual_atoms(moldyn);
1874 /* display progress */
1876 /* get current time */
1877 gettimeofday(&t2,NULL);
1879 printf("sched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)\n",
1880 sched->count,i,moldyn->total_steps,
1881 moldyn->t,moldyn->t_avg,
1882 moldyn->p/BAR,moldyn->p_avg/BAR,
1883 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
1885 (int)(t2.tv_sec-t1.tv_sec));
1889 /* copy over time */
1893 /* increase absolute time */
1894 moldyn->time+=moldyn->tau;
1895 moldyn->total_steps+=1;
1899 /* check for hooks */
1901 printf("\n ## schedule hook %d start ##\n",
1903 sched->hook(moldyn,sched->hook_params);
1904 printf(" ## schedule hook end ##\n");
1907 /* increase the schedule counter */
1915 /* velocity verlet */
1917 int velocity_verlet(t_moldyn *moldyn) {
1920 double tau,tau_square,h;
1925 count=moldyn->count;
1927 tau_square=moldyn->tau_square;
1929 for(i=0;i<count;i++) {
1930 /* check whether fixed atom */
1931 if(atom[i].attr&ATOM_ATTR_FP)
1935 v3_scale(&delta,&(atom[i].v),tau);
1936 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1937 v3_scale(&delta,&(atom[i].f),h*tau_square);
1938 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1939 check_per_bound(moldyn,&(atom[i].r));
1941 /* velocities [actually v(t+tau/2)] */
1942 v3_scale(&delta,&(atom[i].f),h*tau);
1943 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1946 /* criticial check */
1947 moldyn_bc_check(moldyn);
1949 /* neighbour list update */
1950 link_cell_update(moldyn);
1952 /* forces depending on chosen potential */
1954 potential_force_calc(moldyn);
1956 albe_potential_force_calc(moldyn);
1959 for(i=0;i<count;i++) {
1960 /* check whether fixed atom */
1961 if(atom[i].attr&ATOM_ATTR_FP)
1963 /* again velocities [actually v(t+tau)] */
1964 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1965 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1974 * potentials & corresponding forces & virial routine
1978 /* generic potential and force calculation */
1980 int potential_force_calc(t_moldyn *moldyn) {
1983 t_atom *itom,*jtom,*ktom;
1987 int *neighbour_i[27];
1991 int neighbour_i[27];
1994 t_list neighbour_i[27];
1995 t_list neighbour_i2[27];
2001 count=moldyn->count;
2011 /* reset global virial */
2012 memset(&(moldyn->gvir),0,sizeof(t_virial));
2014 /* reset force, site energy and virial of every atom */
2016 i=omp_get_thread_num();
2017 #pragma omp parallel for private(virial)
2019 for(i=0;i<count;i++) {
2022 v3_zero(&(itom[i].f));
2025 virial=(&(itom[i].virial));
2033 /* reset site energy */
2038 /* get energy, force and virial of every atom */
2040 /* first (and only) loop over atoms i */
2041 for(i=0;i<count;i++) {
2043 /* single particle potential/force */
2044 if(itom[i].attr&ATOM_ATTR_1BP)
2046 moldyn->func1b(moldyn,&(itom[i]));
2048 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
2051 /* 2 body pair potential/force */
2053 link_cell_neighbour_index(moldyn,
2054 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2055 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
2056 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
2061 /* first loop over atoms j */
2062 if(moldyn->func2b) {
2069 while(neighbour_i[j][p]!=-1) {
2071 jtom=&(atom[neighbour_i[j][p]]);
2079 p=lc->subcell->list[p];
2081 this=&(neighbour_i[j]);
2084 if(this->start==NULL)
2088 jtom=this->current->data;
2091 if(jtom==&(itom[i]))
2094 if((jtom->attr&ATOM_ATTR_2BP)&
2095 (itom[i].attr&ATOM_ATTR_2BP)) {
2096 moldyn->func2b(moldyn,
2106 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2112 /* 3 body potential/force */
2114 if(!(itom[i].attr&ATOM_ATTR_3BP))
2117 /* copy the neighbour lists */
2119 /* no copy needed for static lists */
2121 /* no copy needed for lowmem lists */
2123 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2126 /* second loop over atoms j */
2133 while(neighbour_i[j][p]!=-1) {
2135 jtom=&(atom[neighbour_i[j][p]]);
2143 p=lc->subcell->list[p];
2145 this=&(neighbour_i[j]);
2148 if(this->start==NULL)
2153 jtom=this->current->data;
2156 if(jtom==&(itom[i]))
2159 if(!(jtom->attr&ATOM_ATTR_3BP))
2165 if(moldyn->func3b_j1)
2166 moldyn->func3b_j1(moldyn,
2171 /* in first j loop, 3bp run can be skipped */
2172 if(!(moldyn->run3bp))
2175 /* first loop over atoms k */
2176 if(moldyn->func3b_k1) {
2184 while(neighbour_i[k][q]!=-1) {
2186 ktom=&(atom[neighbour_i[k][q]]);
2194 q=lc->subcell->list[q];
2196 that=&(neighbour_i2[k]);
2199 if(that->start==NULL)
2203 ktom=that->current->data;
2206 if(!(ktom->attr&ATOM_ATTR_3BP))
2212 if(ktom==&(itom[i]))
2215 moldyn->func3b_k1(moldyn,
2226 } while(list_next_f(that)!=\
2234 if(moldyn->func3b_j2)
2235 moldyn->func3b_j2(moldyn,
2240 /* second loop over atoms k */
2241 if(moldyn->func3b_k2) {
2249 while(neighbour_i[k][q]!=-1) {
2251 ktom=&(atom[neighbour_i[k][q]]);
2259 q=lc->subcell->list[q];
2261 that=&(neighbour_i2[k]);
2264 if(that->start==NULL)
2268 ktom=that->current->data;
2271 if(!(ktom->attr&ATOM_ATTR_3BP))
2277 if(ktom==&(itom[i]))
2280 moldyn->func3b_k2(moldyn,
2291 } while(list_next_f(that)!=\
2299 /* 2bp post function */
2300 if(moldyn->func3b_j3) {
2301 moldyn->func3b_j3(moldyn,
2310 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2325 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2326 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2328 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2329 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2330 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2334 /* some postprocessing */
2336 #pragma omp parallel for
2338 for(i=0;i<count;i++) {
2339 /* calculate global virial */
2340 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2341 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2342 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2343 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2344 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2345 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2347 /* check forces regarding the given timestep */
2348 if(v3_norm(&(itom[i].f))>\
2349 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2350 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2358 * virial calculation
2361 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2362 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2364 a->virial.xx+=f->x*d->x;
2365 a->virial.yy+=f->y*d->y;
2366 a->virial.zz+=f->z*d->z;
2367 a->virial.xy+=f->x*d->y;
2368 a->virial.xz+=f->x*d->z;
2369 a->virial.yz+=f->y*d->z;
2375 * periodic boundary checking
2378 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2379 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2390 if(moldyn->status&MOLDYN_STAT_PBX) {
2391 if(a->x>=x) a->x-=dim->x;
2392 else if(-a->x>x) a->x+=dim->x;
2394 if(moldyn->status&MOLDYN_STAT_PBY) {
2395 if(a->y>=y) a->y-=dim->y;
2396 else if(-a->y>y) a->y+=dim->y;
2398 if(moldyn->status&MOLDYN_STAT_PBZ) {
2399 if(a->z>=z) a->z-=dim->z;
2400 else if(-a->z>z) a->z+=dim->z;
2407 * debugging / critical check functions
2410 int moldyn_bc_check(t_moldyn *moldyn) {
2423 for(i=0;i<moldyn->count;i++) {
2424 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2425 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2426 i,atom[i].r.x,dim->x/2);
2427 printf("diagnostic:\n");
2428 printf("-----------\natom.r.x:\n");
2430 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2433 ((byte)&(1<<k))?1:0,
2436 printf("---------------\nx=dim.x/2:\n");
2438 memcpy(&byte,(u8 *)(&x)+j,1);
2441 ((byte)&(1<<k))?1:0,
2444 if(atom[i].r.x==x) printf("the same!\n");
2445 else printf("different!\n");
2447 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2448 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2449 i,atom[i].r.y,dim->y/2);
2450 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2451 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2452 i,atom[i].r.z,dim->z/2);
2462 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2469 fd=open(file,O_RDONLY);
2471 perror("[moldyn] load save file open");
2475 fsize=lseek(fd,0,SEEK_END);
2476 lseek(fd,0,SEEK_SET);
2478 size=sizeof(t_moldyn);
2481 cnt=read(fd,moldyn,size);
2483 perror("[moldyn] load save file read (moldyn)");
2489 size=moldyn->count*sizeof(t_atom);
2491 /* correcting possible atom data offset */
2493 if(fsize!=sizeof(t_moldyn)+size) {
2494 corr=fsize-sizeof(t_moldyn)-size;
2495 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2496 printf(" moifying offset:\n");
2497 printf(" - current pos: %d\n",sizeof(t_moldyn));
2498 printf(" - atom size: %d\n",size);
2499 printf(" - file size: %d\n",fsize);
2500 printf(" => correction: %d\n",corr);
2501 lseek(fd,corr,SEEK_CUR);
2504 moldyn->atom=(t_atom *)malloc(size);
2505 if(moldyn->atom==NULL) {
2506 perror("[moldyn] load save file malloc (atoms)");
2511 cnt=read(fd,moldyn->atom,size);
2513 perror("[moldyn] load save file read (atoms)");
2524 int moldyn_free_save_file(t_moldyn *moldyn) {
2531 int moldyn_load(t_moldyn *moldyn) {
2539 * function to find/callback all combinations of 2 body bonds
2542 int process_2b_bonds(t_moldyn *moldyn,void *data,
2543 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2544 void *data,u8 bc)) {
2554 t_list neighbour[27];
2564 for(i=0;i<moldyn->count;i++) {
2565 /* neighbour indexing */
2566 link_cell_neighbour_index(moldyn,
2567 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2568 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2569 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2574 bc=(j<lc->dnlc)?0:1;
2579 while(neighbour[j][p]!=-1) {
2581 jtom=&(moldyn->atom[neighbour[j][p]]);
2589 p=lc->subcell->list[p];
2591 this=&(neighbour[j]);
2594 if(this->start==NULL)
2599 jtom=this->current->data;
2603 process(moldyn,&(itom[i]),jtom,data,bc);
2610 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2620 * function to find neighboured atoms
2623 int process_neighbours(t_moldyn *moldyn,void *data,t_atom *atom,
2624 int (*process)(t_moldyn *moldyn,t_atom *atom,t_atom *natom,
2625 void *data,u8 bc)) {
2635 t_list neighbour[27];
2644 /* neighbour indexing */
2645 link_cell_neighbour_index(moldyn,
2646 (atom->r.x+moldyn->dim.x/2)/lc->x,
2647 (atom->r.y+moldyn->dim.y/2)/lc->x,
2648 (atom->r.z+moldyn->dim.z/2)/lc->x,
2653 bc=(j<lc->dnlc)?0:1;
2658 while(neighbour[j][p]!=-1) {
2660 natom=&(moldyn->atom[neighbour[j][p]]);
2667 natom=&(moldyn->atom[p]);
2668 p=lc->subcell->list[p];
2670 this=&(neighbour[j]);
2673 if(this->start==NULL)
2678 natom=this->current->data;
2682 process(moldyn,atom,natom,data,bc);
2689 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2698 * post processing functions
2701 int get_line(int fd,char *line,int max) {
2708 if(count==max) return count;
2709 ret=read(fd,line+count,1);
2710 if(ret<=0) return ret;
2711 if(line[count]=='\n') {
2712 memset(line+count,0,max-count-1);
2720 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2726 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2742 for(i=0;i<moldyn->count;i++) {
2744 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2745 check_per_bound(moldyn,&dist);
2746 d2=v3_absolute_square(&dist);
2760 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2761 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2762 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2767 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2772 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2773 t_atom *jtom,void *data,u8 bc) {
2780 /* only count pairs once,
2781 * skip same atoms */
2782 if(itom->tag>=jtom->tag)
2786 * pair correlation calc
2793 v3_sub(&dist,&(jtom->r),&(itom->r));
2794 if(bc) check_per_bound(moldyn,&dist);
2795 d=v3_absolute_square(&dist);
2797 /* ignore if greater cutoff */
2798 if(d>moldyn->cutoff_square)
2801 /* fill the slots */
2805 /* should never happen but it does 8) -
2806 * related to -ffloat-store problem! */
2808 printf("[moldyn] WARNING: pcc (%d/%d)",
2814 if(itom->brand!=jtom->brand) {
2819 /* type a - type a bonds */
2821 pcc->stat[s+pcc->o1]+=1;
2823 /* type b - type b bonds */
2824 pcc->stat[s+pcc->o2]+=1;
2830 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2837 pcc.o1=moldyn->cutoff/dr;
2840 if(pcc.o1*dr<=moldyn->cutoff)
2841 printf("[moldyn] WARNING: pcc (low #slots)\n");
2843 printf("[moldyn] pair correlation calc info:\n");
2844 printf(" time: %f\n",moldyn->time);
2845 printf(" count: %d\n",moldyn->count);
2846 printf(" cutoff: %f\n",moldyn->cutoff);
2847 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2850 pcc.stat=(double *)ptr;
2853 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2854 if(pcc.stat==NULL) {
2855 perror("[moldyn] pair correlation malloc");
2860 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2863 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2866 for(i=1;i<pcc.o1;i++) {
2867 // normalization: 4 pi r^2 dr
2868 // here: not double counting pairs -> 2 pi r r dr
2869 // ... and actually it's a constant times r^2
2872 pcc.stat[pcc.o1+i]/=norm;
2873 pcc.stat[pcc.o2+i]/=norm;
2878 /* todo: store/print pair correlation function */
2885 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2892 if(itom->tag>=jtom->tag)
2896 v3_sub(&dist,&(jtom->r),&(itom->r));
2897 if(bc) check_per_bound(moldyn,&dist);
2898 d=v3_absolute_square(&dist);
2900 /* ignore if greater or equal cutoff */
2901 if(d>moldyn->cutoff_square)
2904 /* check for potential bond */
2905 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2908 /* now count this bonding ... */
2911 /* increase total bond counter
2912 * ... double counting!
2917 ba->acnt[jtom->tag]+=1;
2919 ba->bcnt[jtom->tag]+=1;
2922 ba->acnt[itom->tag]+=1;
2924 ba->bcnt[itom->tag]+=1;
2929 int bond_analyze(t_moldyn *moldyn,double *quality) {
2931 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2939 ba.acnt=malloc(moldyn->count*sizeof(int));
2941 perror("[moldyn] bond analyze malloc (a)");
2944 memset(ba.acnt,0,moldyn->count*sizeof(int));
2946 ba.bcnt=malloc(moldyn->count*sizeof(int));
2948 perror("[moldyn] bond analyze malloc (b)");
2951 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2960 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2962 for(i=0;i<moldyn->count;i++) {
2963 if(atom[i].brand==0) {
2964 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2968 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2976 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2977 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2980 quality[0]=1.0*ccnt/cset;
2981 quality[1]=1.0*qcnt/ba.tcnt;
2984 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2985 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2992 * visualization code
2995 int visual_init(t_moldyn *moldyn,char *filebase) {
2997 strncpy(moldyn->vis.fb,filebase,128);
3002 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
3009 if(itom->tag>=jtom->tag)
3012 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
3015 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
3016 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
3017 itom->r.x,itom->r.y,itom->r.z,
3018 jtom->r.x,jtom->r.y,jtom->r.z);
3023 #ifdef VISUAL_THREAD
3024 void *visual_atoms(void *ptr) {
3026 int visual_atoms(t_moldyn *moldyn) {
3036 #ifdef VISUAL_THREAD
3050 sprintf(file,"%s/atomic_conf_%08.f.xyz",v->fb,moldyn->time);
3051 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
3053 perror("open visual save file fd");
3054 #ifndef VISUAL_THREAD
3059 /* write the actual data file */
3062 dprintf(vb.fd,"# [P] %d %08.f <%f,%f,%f>\n",
3063 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
3065 // atomic configuration
3066 for(i=0;i<moldyn->count;i++)
3067 // atom type, positions, color and kinetic energy
3068 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
3072 pse_col[atom[i].element],
3075 // bonds between atoms
3076 #ifndef VISUAL_THREAD
3077 process_2b_bonds(moldyn,&vb,visual_bonds_process);
3082 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3083 -dim.x/2,-dim.y/2,-dim.z/2,
3084 dim.x/2,-dim.y/2,-dim.z/2);
3085 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3086 -dim.x/2,-dim.y/2,-dim.z/2,
3087 -dim.x/2,dim.y/2,-dim.z/2);
3088 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3089 dim.x/2,dim.y/2,-dim.z/2,
3090 dim.x/2,-dim.y/2,-dim.z/2);
3091 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3092 -dim.x/2,dim.y/2,-dim.z/2,
3093 dim.x/2,dim.y/2,-dim.z/2);
3095 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3096 -dim.x/2,-dim.y/2,dim.z/2,
3097 dim.x/2,-dim.y/2,dim.z/2);
3098 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3099 -dim.x/2,-dim.y/2,dim.z/2,
3100 -dim.x/2,dim.y/2,dim.z/2);
3101 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3102 dim.x/2,dim.y/2,dim.z/2,
3103 dim.x/2,-dim.y/2,dim.z/2);
3104 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3105 -dim.x/2,dim.y/2,dim.z/2,
3106 dim.x/2,dim.y/2,dim.z/2);
3108 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3109 -dim.x/2,-dim.y/2,dim.z/2,
3110 -dim.x/2,-dim.y/2,-dim.z/2);
3111 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3112 -dim.x/2,dim.y/2,dim.z/2,
3113 -dim.x/2,dim.y/2,-dim.z/2);
3114 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3115 dim.x/2,-dim.y/2,dim.z/2,
3116 dim.x/2,-dim.y/2,-dim.z/2);
3117 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3118 dim.x/2,dim.y/2,dim.z/2,
3119 dim.x/2,dim.y/2,-dim.z/2);
3124 #ifdef VISUAL_THREAD
3135 * fpu cntrol functions
3138 // set rounding to double (eliminates -ffloat-store!)
3139 int fpu_set_rtd(void) {
3145 ctrl&=~_FPU_EXTENDED;