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>
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"
51 * the moldyn functions
54 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
56 printf("[moldyn] init\n");
58 /* only needed if compiled without -msse2 (float-store prob!) */
61 memset(moldyn,0,sizeof(t_moldyn));
66 rand_init(&(moldyn->random),NULL,1);
67 moldyn->random.status|=RAND_STAT_VERBOSE;
72 int moldyn_shutdown(t_moldyn *moldyn) {
74 printf("[moldyn] shutdown\n");
76 moldyn_log_shutdown(moldyn);
77 link_cell_shutdown(moldyn);
78 rand_close(&(moldyn->random));
84 int set_int_alg(t_moldyn *moldyn,u8 algo) {
86 printf("[moldyn] integration algorithm: ");
89 case MOLDYN_INTEGRATE_VERLET:
90 moldyn->integrate=velocity_verlet;
91 printf("velocity verlet\n");
94 printf("unknown integration algorithm: %02x\n",algo);
102 int set_cutoff(t_moldyn *moldyn,double cutoff) {
104 moldyn->cutoff=cutoff;
105 moldyn->cutoff_square=cutoff*cutoff;
107 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
112 int set_temperature(t_moldyn *moldyn,double t_ref) {
116 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
121 int set_pressure(t_moldyn *moldyn,double p_ref) {
125 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
130 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
132 moldyn->pt_scale&=(~(P_SCALE_MASK));
133 moldyn->pt_scale|=ptype;
136 printf("[moldyn] p scaling:\n");
138 printf(" p: %s",ptype?"yes":"no ");
140 printf(" | type: %02x | factor: %f",ptype,ptc);
146 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
148 moldyn->pt_scale&=(~(T_SCALE_MASK));
149 moldyn->pt_scale|=ttype;
152 printf("[moldyn] t scaling:\n");
154 printf(" t: %s",ttype?"yes":"no ");
156 printf(" | type: %02x | factor: %f",ttype,ttc);
162 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
164 moldyn->pt_scale=(ptype|ttype);
168 printf("[moldyn] p/t scaling:\n");
170 printf(" p: %s",ptype?"yes":"no ");
172 printf(" | type: %02x | factor: %f",ptype,ptc);
175 printf(" t: %s",ttype?"yes":"no ");
177 printf(" | type: %02x | factor: %f",ttype,ttc);
183 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
189 moldyn->volume=x*y*z;
197 printf("[moldyn] dimensions in A and A^3 respectively:\n");
198 printf(" x: %f\n",moldyn->dim.x);
199 printf(" y: %f\n",moldyn->dim.y);
200 printf(" z: %f\n",moldyn->dim.z);
201 printf(" volume: %f\n",moldyn->volume);
202 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
207 int set_nn_dist(t_moldyn *moldyn,double dist) {
214 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
216 printf("[moldyn] periodic boundary conditions:\n");
219 moldyn->status|=MOLDYN_STAT_PBX;
222 moldyn->status|=MOLDYN_STAT_PBY;
225 moldyn->status|=MOLDYN_STAT_PBZ;
227 printf(" x: %s\n",x?"yes":"no");
228 printf(" y: %s\n",y?"yes":"no");
229 printf(" z: %s\n",z?"yes":"no");
234 int set_potential(t_moldyn *moldyn,u8 type) {
237 case MOLDYN_POTENTIAL_TM:
238 moldyn->func1b=tersoff_mult_1bp;
239 moldyn->func3b_j1=tersoff_mult_3bp_j1;
240 moldyn->func3b_k1=tersoff_mult_3bp_k1;
241 moldyn->func3b_j2=tersoff_mult_3bp_j2;
242 moldyn->func3b_k2=tersoff_mult_3bp_k2;
243 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
245 case MOLDYN_POTENTIAL_AM:
246 moldyn->func3b_j1=albe_mult_3bp_j1;
247 moldyn->func3b_k1=albe_mult_3bp_k1;
248 moldyn->func3b_j2=albe_mult_3bp_j2;
249 moldyn->func3b_k2=albe_mult_3bp_k2;
250 moldyn->check_2b_bond=albe_mult_check_2b_bond;
252 case MOLDYN_POTENTIAL_HO:
253 moldyn->func2b=harmonic_oscillator;
254 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
256 case MOLDYN_POTENTIAL_LJ:
257 moldyn->func2b=lennard_jones;
258 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
261 printf("[moldyn] set potential: unknown type %02x\n",
269 int set_avg_skip(t_moldyn *moldyn,int skip) {
271 printf("[moldyn] skip %d steps before starting average calc\n",skip);
272 moldyn->avg_skip=skip;
277 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
279 strncpy(moldyn->vlsdir,dir,127);
284 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
286 strncpy(moldyn->rauthor,author,63);
287 strncpy(moldyn->rtitle,title,63);
292 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
297 printf("[moldyn] set log: ");
300 case LOG_TOTAL_ENERGY:
301 moldyn->ewrite=timer;
302 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
303 moldyn->efd=open(filename,
304 O_WRONLY|O_CREAT|O_EXCL,
307 perror("[moldyn] energy log fd open");
310 dprintf(moldyn->efd,"# total energy log file\n");
311 printf("total energy (%d)\n",timer);
313 case LOG_TOTAL_MOMENTUM:
314 moldyn->mwrite=timer;
315 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
316 moldyn->mfd=open(filename,
317 O_WRONLY|O_CREAT|O_EXCL,
320 perror("[moldyn] momentum log fd open");
323 dprintf(moldyn->efd,"# total momentum log file\n");
324 printf("total momentum (%d)\n",timer);
327 moldyn->pwrite=timer;
328 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
329 moldyn->pfd=open(filename,
330 O_WRONLY|O_CREAT|O_EXCL,
333 perror("[moldyn] pressure log file\n");
336 dprintf(moldyn->pfd,"# pressure log file\n");
337 printf("pressure (%d)\n",timer);
339 case LOG_TEMPERATURE:
340 moldyn->twrite=timer;
341 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
342 moldyn->tfd=open(filename,
343 O_WRONLY|O_CREAT|O_EXCL,
346 perror("[moldyn] temperature log file\n");
349 dprintf(moldyn->tfd,"# temperature log file\n");
350 printf("temperature (%d)\n",timer);
353 moldyn->vwrite=timer;
354 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
355 moldyn->vfd=open(filename,
356 O_WRONLY|O_CREAT|O_EXCL,
359 perror("[moldyn] volume log file\n");
362 dprintf(moldyn->vfd,"# volume log file\n");
363 printf("volume (%d)\n",timer);
366 moldyn->swrite=timer;
367 printf("save file (%d)\n",timer);
370 moldyn->awrite=timer;
371 ret=visual_init(moldyn,moldyn->vlsdir);
373 printf("[moldyn] visual init failure\n");
376 printf("visual file (%d)\n",timer);
379 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
380 moldyn->rfd=open(filename,
381 O_WRONLY|O_CREAT|O_EXCL,
384 perror("[moldyn] report fd open");
387 printf("report -> ");
389 snprintf(filename,127,"%s/e_plot.scr",
391 moldyn->epfd=open(filename,
392 O_WRONLY|O_CREAT|O_EXCL,
395 perror("[moldyn] energy plot fd open");
398 dprintf(moldyn->epfd,e_plot_script);
403 snprintf(filename,127,"%s/pressure_plot.scr",
405 moldyn->ppfd=open(filename,
406 O_WRONLY|O_CREAT|O_EXCL,
409 perror("[moldyn] p plot fd open");
412 dprintf(moldyn->ppfd,pressure_plot_script);
417 snprintf(filename,127,"%s/temperature_plot.scr",
419 moldyn->tpfd=open(filename,
420 O_WRONLY|O_CREAT|O_EXCL,
423 perror("[moldyn] t plot fd open");
426 dprintf(moldyn->tpfd,temperature_plot_script);
428 printf("temperature ");
430 dprintf(moldyn->rfd,report_start,
431 moldyn->rauthor,moldyn->rtitle);
435 printf("unknown log type: %02x\n",type);
442 int moldyn_log_shutdown(t_moldyn *moldyn) {
446 printf("[moldyn] log shutdown\n");
450 dprintf(moldyn->rfd,report_energy);
451 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
456 if(moldyn->mfd) close(moldyn->mfd);
460 dprintf(moldyn->rfd,report_pressure);
461 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
468 dprintf(moldyn->rfd,report_temperature);
469 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
474 dprintf(moldyn->rfd,report_end);
476 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
479 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
482 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
491 * creating lattice functions
494 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
495 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
507 /* how many atoms do we expect */
510 printf("[moldyn] WARNING: create 'none' lattice called");
512 if(type==CUBIC) new*=1;
513 if(type==FCC) new*=4;
514 if(type==DIAMOND) new*=8;
516 /* allocate space for atoms */
517 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
519 perror("[moldyn] realloc (create lattice)");
523 atom=&(moldyn->atom[count]);
525 /* no atoms on the boundaries (only reason: it looks better!) */
539 set_nn_dist(moldyn,lc);
540 ret=cubic_init(a,b,c,lc,atom,&orig);
541 strcpy(name,"cubic");
545 v3_scale(&orig,&orig,0.5);
546 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
547 ret=fcc_init(a,b,c,lc,atom,&orig);
552 v3_scale(&orig,&orig,0.25);
553 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
554 ret=diamond_init(a,b,c,lc,atom,&orig);
555 strcpy(name,"diamond");
558 printf("unknown lattice type (%02x)\n",type);
564 printf("[moldyn] creating lattice failed\n");
565 printf(" amount of atoms\n");
566 printf(" - expected: %d\n",new);
567 printf(" - created: %d\n",ret);
572 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
574 for(ret=0;ret<new;ret++) {
575 atom[ret].element=element;
578 atom[ret].brand=brand;
579 atom[ret].tag=count+ret;
580 check_per_bound(moldyn,&(atom[ret].r));
581 atom[ret].r_0=atom[ret].r;
584 /* update total system mass */
585 total_mass_calc(moldyn);
590 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
591 t_3dvec *r,t_3dvec *v) {
598 count=(moldyn->count)++; // asshole style!
600 ptr=realloc(atom,(count+1)*sizeof(t_atom));
602 perror("[moldyn] realloc (add atom)");
608 ptr=realloc(moldyn->lc.subcell->list,(count+1)*sizeof(int));
610 perror("[moldyn] list realloc (add atom)");
613 moldyn->lc.subcell->list=ptr;
618 /* initialize new atom */
619 memset(&(atom[count]),0,sizeof(t_atom));
622 atom[count].element=element;
623 atom[count].mass=mass;
624 atom[count].brand=brand;
625 atom[count].tag=count;
626 atom[count].attr=attr;
627 check_per_bound(moldyn,&(atom[count].r));
628 atom[count].r_0=atom[count].r;
630 /* update total system mass */
631 total_mass_calc(moldyn);
636 int del_atom(t_moldyn *moldyn,int tag) {
643 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
645 perror("[moldyn]malloc (del atom)");
649 for(cnt=0;cnt<tag;cnt++)
652 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
654 new[cnt-1].tag=cnt-1;
666 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
685 v3_copy(&(atom[count].r),&r);
694 for(i=0;i<count;i++) {
695 atom[i].r.x-=(a*lc)/2.0;
696 atom[i].r.y-=(b*lc)/2.0;
697 atom[i].r.z-=(c*lc)/2.0;
703 /* fcc lattice init */
704 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
717 /* construct the basis */
718 memset(basis,0,3*sizeof(t_3dvec));
726 /* fill up the room */
734 v3_copy(&(atom[count].r),&r);
737 /* the three face centered atoms */
739 v3_add(&n,&r,&basis[l]);
740 v3_copy(&(atom[count].r),&n);
749 /* coordinate transformation */
750 for(i=0;i<count;i++) {
751 atom[i].r.x-=(a*lc)/2.0;
752 atom[i].r.y-=(b*lc)/2.0;
753 atom[i].r.z-=(c*lc)/2.0;
759 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
764 count=fcc_init(a,b,c,lc,atom,origin);
770 if(origin) v3_add(&o,&o,origin);
772 count+=fcc_init(a,b,c,lc,&atom[count],&o);
777 int destroy_atoms(t_moldyn *moldyn) {
779 if(moldyn->atom) free(moldyn->atom);
784 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
787 * - gaussian distribution of velocities
788 * - zero total momentum
789 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
794 t_3dvec p_total,delta;
799 random=&(moldyn->random);
801 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
803 /* gaussian distribution of velocities */
805 for(i=0;i<moldyn->count;i++) {
806 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
808 v=sigma*rand_get_gauss(random);
810 p_total.x+=atom[i].mass*v;
812 v=sigma*rand_get_gauss(random);
814 p_total.y+=atom[i].mass*v;
816 v=sigma*rand_get_gauss(random);
818 p_total.z+=atom[i].mass*v;
821 /* zero total momentum */
822 v3_scale(&p_total,&p_total,1.0/moldyn->count);
823 for(i=0;i<moldyn->count;i++) {
824 v3_scale(&delta,&p_total,1.0/atom[i].mass);
825 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
828 /* velocity scaling */
829 scale_velocity(moldyn,equi_init);
834 double total_mass_calc(t_moldyn *moldyn) {
840 for(i=0;i<moldyn->count;i++)
841 moldyn->mass+=moldyn->atom[i].mass;
846 double temperature_calc(t_moldyn *moldyn) {
848 /* assume up to date kinetic energy, which is 3/2 N k_B T */
850 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
855 double get_temperature(t_moldyn *moldyn) {
860 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
870 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
873 /* get kinetic energy / temperature & count involved atoms */
876 for(i=0;i<moldyn->count;i++) {
877 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
878 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
883 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
884 else return 0; /* no atoms involved in scaling! */
886 /* (temporary) hack for e,t = 0 */
889 if(moldyn->t_ref!=0.0) {
890 thermal_init(moldyn,equi_init);
894 return 0; /* no scaling needed */
898 /* get scaling factor */
899 scale=moldyn->t_ref/moldyn->t;
903 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
904 scale=1.0+(scale-1.0)*moldyn->tau/moldyn->t_tc;
907 /* velocity scaling */
908 for(i=0;i<moldyn->count;i++) {
909 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
910 v3_scale(&(atom[i].v),&(atom[i].v),scale);
916 double ideal_gas_law_pressure(t_moldyn *moldyn) {
920 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
925 double virial_sum(t_moldyn *moldyn) {
930 /* virial (sum over atom virials) */
938 for(i=0;i<moldyn->count;i++) {
939 virial=&(moldyn->atom[i].virial);
940 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
941 moldyn->vir.xx+=virial->xx;
942 moldyn->vir.yy+=virial->yy;
943 moldyn->vir.zz+=virial->zz;
944 moldyn->vir.xy+=virial->xy;
945 moldyn->vir.xz+=virial->xz;
946 moldyn->vir.yz+=virial->yz;
949 /* global virial (absolute coordinates) */
950 virial=&(moldyn->gvir);
951 moldyn->gv=virial->xx+virial->yy+virial->zz;
953 return moldyn->virial;
956 double pressure_calc(t_moldyn *moldyn) {
960 * with W = 1/3 sum_i f_i r_i (- skipped!)
961 * virial = sum_i f_i r_i
963 * => P = (2 Ekin + virial) / (3V)
966 /* assume up to date virial & up to date kinetic energy */
968 /* pressure (atom virials) */
969 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
970 moldyn->p/=(3.0*moldyn->volume);
972 /* pressure (absolute coordinates) */
973 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
974 moldyn->gp/=(3.0*moldyn->volume);
979 int average_reset(t_moldyn *moldyn) {
981 printf("[moldyn] average reset\n");
983 /* update skip value */
984 moldyn->avg_skip=moldyn->total_steps;
990 /* potential energy */
998 moldyn->virial_sum=0.0;
1009 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1013 if(moldyn->total_steps<moldyn->avg_skip)
1016 denom=moldyn->total_steps+1-moldyn->avg_skip;
1018 /* assume up to date energies, temperature, pressure etc */
1020 /* kinetic energy */
1021 moldyn->k_sum+=moldyn->ekin;
1022 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1023 moldyn->k_avg=moldyn->k_sum/denom;
1024 moldyn->k2_avg=moldyn->k2_sum/denom;
1025 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1027 /* potential energy */
1028 moldyn->v_sum+=moldyn->energy;
1029 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1030 moldyn->v_avg=moldyn->v_sum/denom;
1031 moldyn->v2_avg=moldyn->v2_sum/denom;
1032 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1035 moldyn->t_sum+=moldyn->t;
1036 moldyn->t_avg=moldyn->t_sum/denom;
1039 moldyn->virial_sum+=moldyn->virial;
1040 moldyn->virial_avg=moldyn->virial_sum/denom;
1041 moldyn->gv_sum+=moldyn->gv;
1042 moldyn->gv_avg=moldyn->gv_sum/denom;
1045 moldyn->p_sum+=moldyn->p;
1046 moldyn->p_avg=moldyn->p_sum/denom;
1047 moldyn->gp_sum+=moldyn->gp;
1048 moldyn->gp_avg=moldyn->gp_sum/denom;
1049 moldyn->tp_sum+=moldyn->tp;
1050 moldyn->tp_avg=moldyn->tp_sum/denom;
1055 int get_heat_capacity(t_moldyn *moldyn) {
1059 /* averages needed for heat capacity calc */
1060 if(moldyn->total_steps<moldyn->avg_skip)
1063 /* (temperature average)^2 */
1064 temp2=moldyn->t_avg*moldyn->t_avg;
1065 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1068 /* ideal gas contribution */
1069 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1070 printf(" ideal gas contribution: %f\n",
1071 ighc/moldyn->mass*KILOGRAM/JOULE);
1073 /* specific heat for nvt ensemble */
1074 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1075 moldyn->c_v_nvt/=moldyn->mass;
1077 /* specific heat for nve ensemble */
1078 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1079 moldyn->c_v_nve/=moldyn->mass;
1081 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1082 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1083 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)));
1088 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1104 /* store atomic configuration + dimension */
1105 store=malloc(moldyn->count*sizeof(t_atom));
1107 printf("[moldyn] allocating store mem failed\n");
1110 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1115 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1116 su=pow(2.0-h,ONE_THIRD)-1.0;
1117 dv=(1.0-h)*moldyn->volume;
1119 /* scale up dimension and atom positions */
1120 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1121 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1122 link_cell_shutdown(moldyn);
1123 link_cell_init(moldyn,QUIET);
1124 potential_force_calc(moldyn);
1127 /* restore atomic configuration + dim */
1128 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1131 /* scale down dimension and atom positions */
1132 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1133 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1134 link_cell_shutdown(moldyn);
1135 link_cell_init(moldyn,QUIET);
1136 potential_force_calc(moldyn);
1139 /* calculate pressure */
1140 moldyn->tp=-(y1-y0)/(2.0*dv);
1142 /* restore atomic configuration */
1143 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1145 link_cell_shutdown(moldyn);
1146 link_cell_init(moldyn,QUIET);
1147 //potential_force_calc(moldyn);
1149 /* free store buffer */
1156 double get_pressure(t_moldyn *moldyn) {
1162 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1174 if(x) dim->x*=scale;
1175 if(y) dim->y*=scale;
1176 if(z) dim->z*=scale;
1181 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1192 for(i=0;i<moldyn->count;i++) {
1193 r=&(moldyn->atom[i].r);
1202 int scale_volume(t_moldyn *moldyn) {
1208 vdim=&(moldyn->vis.dim);
1212 /* scaling factor */
1213 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1214 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc*moldyn->tau;
1215 scale=pow(scale,ONE_THIRD);
1218 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1221 /* scale the atoms and dimensions */
1222 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1223 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1225 /* visualize dimensions */
1232 /* recalculate scaled volume */
1233 moldyn->volume=dim->x*dim->y*dim->z;
1235 /* adjust/reinit linkcell */
1236 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1237 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1238 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1239 link_cell_shutdown(moldyn);
1240 link_cell_init(moldyn,QUIET);
1251 double e_kin_calc(t_moldyn *moldyn) {
1259 for(i=0;i<moldyn->count;i++) {
1260 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1261 moldyn->ekin+=atom[i].ekin;
1264 return moldyn->ekin;
1267 double get_total_energy(t_moldyn *moldyn) {
1269 return(moldyn->ekin+moldyn->energy);
1272 t_3dvec get_total_p(t_moldyn *moldyn) {
1281 for(i=0;i<moldyn->count;i++) {
1282 v3_scale(&p,&(atom[i].v),atom[i].mass);
1283 v3_add(&p_total,&p_total,&p);
1289 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1293 /* nn_dist is the nearest neighbour distance */
1295 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1304 /* linked list / cell method */
1306 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1309 #ifndef LOWMEM_LISTS
1315 /* partitioning the md cell */
1316 lc->nx=moldyn->dim.x/moldyn->cutoff;
1317 lc->x=moldyn->dim.x/lc->nx;
1318 lc->ny=moldyn->dim.y/moldyn->cutoff;
1319 lc->y=moldyn->dim.y/lc->ny;
1320 lc->nz=moldyn->dim.z/moldyn->cutoff;
1321 lc->z=moldyn->dim.z/lc->nz;
1322 lc->cells=lc->nx*lc->ny*lc->nz;
1325 lc->subcell=malloc(lc->cells*sizeof(int*));
1327 lc->subcell=malloc(sizeof(t_lowmem_list));
1329 lc->subcell=malloc(lc->cells*sizeof(t_list));
1332 if(lc->subcell==NULL) {
1333 perror("[moldyn] cell init (malloc)");
1338 printf("[moldyn] FATAL: less then 27 subcells! (%d)\n",
1343 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1346 printf("[moldyn] initializing 'lowmem' linked cells (%d)\n",
1349 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1352 printf(" x: %d x %f A\n",lc->nx,lc->x);
1353 printf(" y: %d x %f A\n",lc->ny,lc->y);
1354 printf(" z: %d x %f A\n",lc->nz,lc->z);
1359 for(i=0;i<lc->cells;i++) {
1360 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1361 if(lc->subcell[i]==NULL) {
1362 perror("[moldyn] list init (malloc)");
1367 printf(" ---> %d malloc %p (%p)\n",
1368 i,lc->subcell[0],lc->subcell);
1372 lc->subcell->head=malloc(lc->cells*sizeof(int));
1373 if(lc->subcell->head==NULL) {
1374 perror("[moldyn] head init (malloc)");
1377 lc->subcell->list=malloc(moldyn->count*sizeof(int));
1378 if(lc->subcell->list==NULL) {
1379 perror("[moldyn] list init (malloc)");
1383 for(i=0;i<lc->cells;i++)
1384 list_init_f(&(lc->subcell[i]));
1387 /* update the list */
1388 link_cell_update(moldyn);
1393 int link_cell_update(t_moldyn *moldyn) {
1411 for(i=0;i<lc->cells;i++)
1413 memset(lc->subcell[i],-1,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1415 lc->subcell->head[i]=-1;
1417 list_destroy_f(&(lc->subcell[i]));
1420 for(count=0;count<moldyn->count;count++) {
1421 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1422 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1423 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1427 while(lc->subcell[i+j*nx+k*nxy][p]!=-1)
1430 if(p>=MAX_ATOMS_PER_LIST) {
1431 printf("[moldyn] FATAL: amount of atoms too high!\n");
1435 lc->subcell[i+j*nx+k*nxy][p]=count;
1438 lc->subcell->list[count]=lc->subcell->head[p];
1439 lc->subcell->head[p]=count;
1441 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nxy]),
1445 printf(" ---> %d %d malloc %p (%p)\n",
1446 i,count,lc->subcell[i].current,lc->subcell);
1454 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1480 if(i>=nx||j>=ny||k>=nz)
1481 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1484 #ifndef LOWMEM_LISTS
1485 cell[0]=lc->subcell[i+j*nx+k*a];
1487 cell[0]=lc->subcell->head[i+j*nx+k*a];
1489 for(ci=-1;ci<=1;ci++) {
1492 if((x<0)||(x>=nx)) {
1496 for(cj=-1;cj<=1;cj++) {
1499 if((y<0)||(y>=ny)) {
1503 for(ck=-1;ck<=1;ck++) {
1506 if((z<0)||(z>=nz)) {
1510 if(!(ci|cj|ck)) continue;
1512 #ifndef LOWMEM_LISTS
1513 cell[--count2]=lc->subcell[x+y*nx+z*a];
1515 cell[--count2]=lc->subcell->head[x+y*nx+z*a];
1520 #ifndef LOWMEM_LISTS
1521 cell[count1++]=lc->subcell[x+y*nx+z*a];
1523 cell[count1++]=lc->subcell->head[x+y*nx+z*a];
1535 int link_cell_shutdown(t_moldyn *moldyn) {
1537 #ifndef LOWMEM_LISTS
1545 free(lc->subcell->head);
1546 free(lc->subcell->list);
1550 for(i=0;i<lc->cells;i++) {
1552 free(lc->subcell[i]);
1554 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1555 list_destroy_f(&(lc->subcell[i]));
1565 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1569 t_moldyn_schedule *schedule;
1571 schedule=&(moldyn->schedule);
1572 count=++(schedule->total_sched);
1574 ptr=realloc(schedule->runs,count*sizeof(int));
1576 perror("[moldyn] realloc (runs)");
1580 schedule->runs[count-1]=runs;
1582 ptr=realloc(schedule->tau,count*sizeof(double));
1584 perror("[moldyn] realloc (tau)");
1588 schedule->tau[count-1]=tau;
1590 printf("[moldyn] schedule added:\n");
1591 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1597 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1599 moldyn->schedule.hook=hook;
1600 moldyn->schedule.hook_params=hook_params;
1607 * 'integration of newtons equation' - algorithms
1611 /* start the integration */
1613 int moldyn_integrate(t_moldyn *moldyn) {
1616 unsigned int e,m,s,v,p,t,a;
1618 t_moldyn_schedule *sched;
1623 double energy_scale;
1624 struct timeval t1,t2;
1629 pthread_t io_thread;
1638 sched=&(moldyn->schedule);
1641 /* initialize linked cell method */
1642 link_cell_init(moldyn,VERBOSE);
1644 /* logging & visualization */
1653 /* sqaure of some variables */
1654 moldyn->tau_square=moldyn->tau*moldyn->tau;
1656 /* get current time */
1657 gettimeofday(&t1,NULL);
1659 /* calculate initial forces */
1660 potential_force_calc(moldyn);
1665 /* some stupid checks before we actually start calculating bullshit */
1666 if(moldyn->cutoff>0.5*moldyn->dim.x)
1667 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1668 if(moldyn->cutoff>0.5*moldyn->dim.y)
1669 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1670 if(moldyn->cutoff>0.5*moldyn->dim.z)
1671 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1673 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1674 if(ds>0.05*moldyn->nnd)
1675 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1678 /* zero absolute time */
1679 // should have right values!
1681 //moldyn->total_steps=0;
1683 /* debugging, ignore */
1686 /* zero & init moldyn copy */
1688 memset(&md_copy,0,sizeof(t_moldyn));
1689 atom_copy=malloc(moldyn->count*sizeof(t_atom));
1690 if(atom_copy==NULL) {
1691 perror("[moldyn] malloc atom copy (init)");
1696 /* tell the world */
1697 printf("[moldyn] integration start, go get a coffee ...\n");
1699 /* executing the schedule */
1701 while(sched->count<sched->total_sched) {
1703 /* setting amount of runs and finite time step size */
1704 moldyn->tau=sched->tau[sched->count];
1705 moldyn->tau_square=moldyn->tau*moldyn->tau;
1706 moldyn->time_steps=sched->runs[sched->count];
1708 /* energy scaling factor (might change!) */
1709 energy_scale=moldyn->count*EV;
1711 /* integration according to schedule */
1713 for(i=0;i<moldyn->time_steps;i++) {
1715 /* integration step */
1716 moldyn->integrate(moldyn);
1718 /* calculate kinetic energy, temperature and pressure */
1720 temperature_calc(moldyn);
1722 pressure_calc(moldyn);
1724 thermodynamic_pressure_calc(moldyn);
1725 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1729 /* calculate fluctuations + averages */
1730 average_and_fluctuation_calc(moldyn);
1733 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1734 scale_velocity(moldyn,FALSE);
1735 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1736 scale_volume(moldyn);
1738 /* check for log & visualization */
1740 if(!(moldyn->total_steps%e))
1741 dprintf(moldyn->efd,
1743 moldyn->time,moldyn->ekin/energy_scale,
1744 moldyn->energy/energy_scale,
1745 get_total_energy(moldyn)/energy_scale);
1748 if(!(moldyn->total_steps%m)) {
1749 momentum=get_total_p(moldyn);
1750 dprintf(moldyn->mfd,
1751 "%f %f %f %f %f\n",moldyn->time,
1752 momentum.x,momentum.y,momentum.z,
1753 v3_norm(&momentum));
1757 if(!(moldyn->total_steps%p)) {
1758 dprintf(moldyn->pfd,
1759 "%f %f %f %f %f %f %f\n",moldyn->time,
1760 moldyn->p/BAR,moldyn->p_avg/BAR,
1761 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1762 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1766 if(!(moldyn->total_steps%t)) {
1767 dprintf(moldyn->tfd,
1769 moldyn->time,moldyn->t,moldyn->t_avg);
1773 if(!(moldyn->total_steps%v)) {
1774 dprintf(moldyn->vfd,
1775 "%f %f\n",moldyn->time,moldyn->volume);
1779 if(!(moldyn->total_steps%s)) {
1780 snprintf(dir,128,"%s/s-%08.f.save",
1781 moldyn->vlsdir,moldyn->time);
1782 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1784 if(fd<0) perror("[moldyn] save fd open");
1786 write(fd,moldyn,sizeof(t_moldyn));
1787 write(fd,moldyn->atom,
1788 moldyn->count*sizeof(t_atom));
1794 if(!(moldyn->total_steps%a)) {
1796 /* check whether thread has not terminated yet */
1798 ret=pthread_join(io_thread,NULL);
1801 /* prepare and start thread */
1802 if(moldyn->count!=md_copy.count) {
1806 memcpy(&md_copy,moldyn,sizeof(t_moldyn));
1808 atom_copy=malloc(moldyn->count*sizeof(t_atom));
1809 if(atom_copy==NULL) {
1810 perror("[moldyn] malloc atom copy (change)");
1814 md_copy.atom=atom_copy;
1815 memcpy(atom_copy,moldyn->atom,moldyn->count*sizeof(t_atom));
1817 ret=pthread_create(&io_thread,NULL,visual_atoms,&md_copy);
1819 perror("[moldyn] create visual atoms thread\n");
1823 visual_atoms(moldyn);
1828 /* display progress */
1830 /* get current time */
1831 gettimeofday(&t2,NULL);
1833 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1834 sched->count,i,moldyn->total_steps,
1835 moldyn->t,moldyn->t_avg,
1836 moldyn->p/BAR,moldyn->p_avg/BAR,
1837 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
1839 (int)(t2.tv_sec-t1.tv_sec));
1843 /* copy over time */
1847 /* increase absolute time */
1848 moldyn->time+=moldyn->tau;
1849 moldyn->total_steps+=1;
1853 /* check for hooks */
1855 printf("\n ## schedule hook %d start ##\n",
1857 sched->hook(moldyn,sched->hook_params);
1858 printf(" ## schedule hook end ##\n");
1861 /* increase the schedule counter */
1869 /* velocity verlet */
1871 int velocity_verlet(t_moldyn *moldyn) {
1874 double tau,tau_square,h;
1879 count=moldyn->count;
1881 tau_square=moldyn->tau_square;
1883 for(i=0;i<count;i++) {
1884 /* check whether fixed atom */
1885 if(atom[i].attr&ATOM_ATTR_FP)
1889 v3_scale(&delta,&(atom[i].v),tau);
1890 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1891 v3_scale(&delta,&(atom[i].f),h*tau_square);
1892 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1893 check_per_bound(moldyn,&(atom[i].r));
1895 /* velocities [actually v(t+tau/2)] */
1896 v3_scale(&delta,&(atom[i].f),h*tau);
1897 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1900 /* criticial check */
1901 moldyn_bc_check(moldyn);
1903 /* neighbour list update */
1904 link_cell_update(moldyn);
1906 /* forces depending on chosen potential */
1908 potential_force_calc(moldyn);
1910 albe_potential_force_calc(moldyn);
1913 for(i=0;i<count;i++) {
1914 /* check whether fixed atom */
1915 if(atom[i].attr&ATOM_ATTR_FP)
1917 /* again velocities [actually v(t+tau)] */
1918 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1919 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1928 * potentials & corresponding forces & virial routine
1932 /* generic potential and force calculation */
1934 int potential_force_calc(t_moldyn *moldyn) {
1937 t_atom *itom,*jtom,*ktom;
1941 int *neighbour_i[27];
1945 int neighbour_i[27];
1948 t_list neighbour_i[27];
1949 t_list neighbour_i2[27];
1956 pthread_t kthread[27];
1960 count=moldyn->count;
1968 memset(kdata,0,27*sizeof(t_kdata));
1974 /* reset global virial */
1975 memset(&(moldyn->gvir),0,sizeof(t_virial));
1977 /* reset force, site energy and virial of every atom */
1979 i=omp_get_thread_num();
1980 #pragma omp parallel for private(virial)
1982 for(i=0;i<count;i++) {
1985 v3_zero(&(itom[i].f));
1988 virial=(&(itom[i].virial));
1996 /* reset site energy */
2001 /* get energy, force and virial of every atom */
2003 /* first (and only) loop over atoms i */
2004 for(i=0;i<count;i++) {
2006 /* single particle potential/force */
2007 if(itom[i].attr&ATOM_ATTR_1BP)
2009 moldyn->func1b(moldyn,&(itom[i]));
2011 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
2014 /* 2 body pair potential/force */
2016 link_cell_neighbour_index(moldyn,
2017 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2018 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
2019 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
2024 /* first loop over atoms j */
2025 if(moldyn->func2b) {
2032 while(neighbour_i[j][p]!=-1) {
2034 jtom=&(atom[neighbour_i[j][p]]);
2042 p=lc->subcell->list[p];
2044 this=&(neighbour_i[j]);
2047 if(this->start==NULL)
2051 jtom=this->current->data;
2054 if(jtom==&(itom[i]))
2057 if((jtom->attr&ATOM_ATTR_2BP)&
2058 (itom[i].attr&ATOM_ATTR_2BP)) {
2059 moldyn->func2b(moldyn,
2069 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2075 /* 3 body potential/force */
2077 if(!(itom[i].attr&ATOM_ATTR_3BP))
2080 /* copy the neighbour lists */
2082 /* no copy needed for static lists */
2084 /* no copy needed for lowmem lists */
2086 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2089 /* second loop over atoms j */
2096 while(neighbour_i[j][p]!=-1) {
2098 jtom=&(atom[neighbour_i[j][p]]);
2106 p=lc->subcell->list[p];
2108 this=&(neighbour_i[j]);
2111 if(this->start==NULL)
2116 jtom=this->current->data;
2119 if(jtom==&(itom[i]))
2122 if(!(jtom->attr&ATOM_ATTR_3BP))
2128 if(moldyn->func3b_j1)
2129 moldyn->func3b_j1(moldyn,
2134 /* in first j loop, 3bp run can be skipped */
2135 if(!(moldyn->run3bp))
2138 /* first loop over atoms k */
2140 if(moldyn->func3b_k1) {
2149 while(neighbour_i[k][q]!=-1) {
2151 ktom=&(atom[neighbour_i[k][q]]);
2159 q=lc->subcell->list[q];
2161 that=&(neighbour_i2[k]);
2164 if(that->start==NULL)
2168 ktom=that->current->data;
2171 if(!(ktom->attr&ATOM_ATTR_3BP))
2177 if(ktom==&(itom[i]))
2181 kdata[k].moldyn=moldyn;
2182 kdata[k].ai=&(itom[i]);
2186 ret=pthread_create(&(kthread[k]),NULL,moldyn->func3b_k1,&(kdata[k]));
2188 perror("[moldyn] create k1 thread");
2192 moldyn->func3b_k1(moldyn,
2204 } while(list_next_f(that)!=\
2214 if(moldyn->func3b_j2)
2215 moldyn->func3b_j2(moldyn,
2220 /* second loop over atoms k */
2221 if(moldyn->func3b_k2) {
2229 while(neighbour_i[k][q]!=-1) {
2231 ktom=&(atom[neighbour_i[k][q]]);
2239 q=lc->subcell->list[q];
2241 that=&(neighbour_i2[k]);
2244 if(that->start==NULL)
2248 ktom=that->current->data;
2251 if(!(ktom->attr&ATOM_ATTR_3BP))
2257 if(ktom==&(itom[i]))
2260 moldyn->func3b_k2(moldyn,
2271 } while(list_next_f(that)!=\
2279 /* 2bp post function */
2280 if(moldyn->func3b_j3) {
2281 moldyn->func3b_j3(moldyn,
2290 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2305 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2306 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2308 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2309 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2310 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2314 /* some postprocessing */
2316 #pragma omp parallel for
2318 for(i=0;i<count;i++) {
2319 /* calculate global virial */
2320 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2321 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2322 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2323 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2324 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2325 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2327 /* check forces regarding the given timestep */
2328 if(v3_norm(&(itom[i].f))>\
2329 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2330 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2338 * virial calculation
2341 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2342 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2344 a->virial.xx+=f->x*d->x;
2345 a->virial.yy+=f->y*d->y;
2346 a->virial.zz+=f->z*d->z;
2347 a->virial.xy+=f->x*d->y;
2348 a->virial.xz+=f->x*d->z;
2349 a->virial.yz+=f->y*d->z;
2355 * periodic boundary checking
2358 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2359 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2370 if(moldyn->status&MOLDYN_STAT_PBX) {
2371 if(a->x>=x) a->x-=dim->x;
2372 else if(-a->x>x) a->x+=dim->x;
2374 if(moldyn->status&MOLDYN_STAT_PBY) {
2375 if(a->y>=y) a->y-=dim->y;
2376 else if(-a->y>y) a->y+=dim->y;
2378 if(moldyn->status&MOLDYN_STAT_PBZ) {
2379 if(a->z>=z) a->z-=dim->z;
2380 else if(-a->z>z) a->z+=dim->z;
2387 * debugging / critical check functions
2390 int moldyn_bc_check(t_moldyn *moldyn) {
2403 for(i=0;i<moldyn->count;i++) {
2404 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2405 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2406 i,atom[i].r.x,dim->x/2);
2407 printf("diagnostic:\n");
2408 printf("-----------\natom.r.x:\n");
2410 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2413 ((byte)&(1<<k))?1:0,
2416 printf("---------------\nx=dim.x/2:\n");
2418 memcpy(&byte,(u8 *)(&x)+j,1);
2421 ((byte)&(1<<k))?1:0,
2424 if(atom[i].r.x==x) printf("the same!\n");
2425 else printf("different!\n");
2427 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2428 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2429 i,atom[i].r.y,dim->y/2);
2430 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2431 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2432 i,atom[i].r.z,dim->z/2);
2442 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2449 fd=open(file,O_RDONLY);
2451 perror("[moldyn] load save file open");
2455 fsize=lseek(fd,0,SEEK_END);
2456 lseek(fd,0,SEEK_SET);
2458 size=sizeof(t_moldyn);
2461 cnt=read(fd,moldyn,size);
2463 perror("[moldyn] load save file read (moldyn)");
2469 size=moldyn->count*sizeof(t_atom);
2471 /* correcting possible atom data offset */
2473 if(fsize!=sizeof(t_moldyn)+size) {
2474 corr=fsize-sizeof(t_moldyn)-size;
2475 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2476 printf(" moifying offset:\n");
2477 printf(" - current pos: %d\n",sizeof(t_moldyn));
2478 printf(" - atom size: %d\n",size);
2479 printf(" - file size: %d\n",fsize);
2480 printf(" => correction: %d\n",corr);
2481 lseek(fd,corr,SEEK_CUR);
2484 moldyn->atom=(t_atom *)malloc(size);
2485 if(moldyn->atom==NULL) {
2486 perror("[moldyn] load save file malloc (atoms)");
2491 cnt=read(fd,moldyn->atom,size);
2493 perror("[moldyn] load save file read (atoms)");
2504 int moldyn_free_save_file(t_moldyn *moldyn) {
2511 int moldyn_load(t_moldyn *moldyn) {
2519 * function to find/callback all combinations of 2 body bonds
2522 int process_2b_bonds(t_moldyn *moldyn,void *data,
2523 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2524 void *data,u8 bc)) {
2534 t_list neighbour[27];
2544 for(i=0;i<moldyn->count;i++) {
2545 /* neighbour indexing */
2546 link_cell_neighbour_index(moldyn,
2547 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2548 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2549 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2554 bc=(j<lc->dnlc)?0:1;
2559 while(neighbour[j][p]!=-1) {
2561 jtom=&(moldyn->atom[neighbour[j][p]]);
2569 p=lc->subcell->list[p];
2571 this=&(neighbour[j]);
2574 if(this->start==NULL)
2579 jtom=this->current->data;
2583 process(moldyn,&(itom[i]),jtom,data,bc);
2590 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2600 * function to find neighboured atoms
2603 int process_neighbours(t_moldyn *moldyn,void *data,t_atom *atom,
2604 int (*process)(t_moldyn *moldyn,t_atom *atom,t_atom *natom,
2605 void *data,u8 bc)) {
2615 t_list neighbour[27];
2624 /* neighbour indexing */
2625 link_cell_neighbour_index(moldyn,
2626 (atom->r.x+moldyn->dim.x/2)/lc->x,
2627 (atom->r.y+moldyn->dim.y/2)/lc->x,
2628 (atom->r.z+moldyn->dim.z/2)/lc->x,
2633 bc=(j<lc->dnlc)?0:1;
2638 while(neighbour[j][p]!=-1) {
2640 natom=&(moldyn->atom[neighbour[j][p]]);
2647 natom=&(moldyn->atom[p]);
2648 p=lc->subcell->list[p];
2650 this=&(neighbour[j]);
2653 if(this->start==NULL)
2658 natom=this->current->data;
2662 process(moldyn,atom,natom,data,bc);
2669 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2678 * post processing functions
2681 int get_line(int fd,char *line,int max) {
2688 if(count==max) return count;
2689 ret=read(fd,line+count,1);
2690 if(ret<=0) return ret;
2691 if(line[count]=='\n') {
2692 memset(line+count,0,max-count-1);
2700 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2706 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2722 for(i=0;i<moldyn->count;i++) {
2724 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2725 check_per_bound(moldyn,&dist);
2726 d2=v3_absolute_square(&dist);
2740 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2741 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2742 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2747 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2752 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2753 t_atom *jtom,void *data,u8 bc) {
2760 /* only count pairs once,
2761 * skip same atoms */
2762 if(itom->tag>=jtom->tag)
2766 * pair correlation calc
2773 v3_sub(&dist,&(jtom->r),&(itom->r));
2774 if(bc) check_per_bound(moldyn,&dist);
2775 d=v3_absolute_square(&dist);
2777 /* ignore if greater cutoff */
2778 if(d>moldyn->cutoff_square)
2781 /* fill the slots */
2785 /* should never happen but it does 8) -
2786 * related to -ffloat-store problem! */
2788 printf("[moldyn] WARNING: pcc (%d/%d)",
2794 if(itom->brand!=jtom->brand) {
2799 /* type a - type a bonds */
2801 pcc->stat[s+pcc->o1]+=1;
2803 /* type b - type b bonds */
2804 pcc->stat[s+pcc->o2]+=1;
2810 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2817 pcc.o1=moldyn->cutoff/dr;
2820 if(pcc.o1*dr<=moldyn->cutoff)
2821 printf("[moldyn] WARNING: pcc (low #slots)\n");
2823 printf("[moldyn] pair correlation calc info:\n");
2824 printf(" time: %f\n",moldyn->time);
2825 printf(" count: %d\n",moldyn->count);
2826 printf(" cutoff: %f\n",moldyn->cutoff);
2827 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2830 pcc.stat=(double *)ptr;
2833 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2834 if(pcc.stat==NULL) {
2835 perror("[moldyn] pair correlation malloc");
2840 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2843 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2846 for(i=1;i<pcc.o1;i++) {
2847 // normalization: 4 pi r^2 dr
2848 // here: not double counting pairs -> 2 pi r r dr
2849 // ... and actually it's a constant times r^2
2852 pcc.stat[pcc.o1+i]/=norm;
2853 pcc.stat[pcc.o2+i]/=norm;
2858 /* todo: store/print pair correlation function */
2865 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2872 if(itom->tag>=jtom->tag)
2876 v3_sub(&dist,&(jtom->r),&(itom->r));
2877 if(bc) check_per_bound(moldyn,&dist);
2878 d=v3_absolute_square(&dist);
2880 /* ignore if greater or equal cutoff */
2881 if(d>moldyn->cutoff_square)
2884 /* check for potential bond */
2885 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2888 /* now count this bonding ... */
2891 /* increase total bond counter
2892 * ... double counting!
2897 ba->acnt[jtom->tag]+=1;
2899 ba->bcnt[jtom->tag]+=1;
2902 ba->acnt[itom->tag]+=1;
2904 ba->bcnt[itom->tag]+=1;
2909 int bond_analyze(t_moldyn *moldyn,double *quality) {
2911 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2919 ba.acnt=malloc(moldyn->count*sizeof(int));
2921 perror("[moldyn] bond analyze malloc (a)");
2924 memset(ba.acnt,0,moldyn->count*sizeof(int));
2926 ba.bcnt=malloc(moldyn->count*sizeof(int));
2928 perror("[moldyn] bond analyze malloc (b)");
2931 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2940 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2942 for(i=0;i<moldyn->count;i++) {
2943 if(atom[i].brand==0) {
2944 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2948 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2956 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2957 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2960 quality[0]=1.0*ccnt/cset;
2961 quality[1]=1.0*qcnt/ba.tcnt;
2964 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2965 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2972 * visualization code
2975 int visual_init(t_moldyn *moldyn,char *filebase) {
2977 strncpy(moldyn->vis.fb,filebase,128);
2982 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2989 if(itom->tag>=jtom->tag)
2992 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2995 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2996 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2997 itom->r.x,itom->r.y,itom->r.z,
2998 jtom->r.x,jtom->r.y,jtom->r.z);
3004 void *visual_atoms(void *ptr) {
3006 int visual_atoms(t_moldyn *moldyn) {
3030 sprintf(file,"%s/atomic_conf_%08.f.xyz",v->fb,moldyn->time);
3031 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
3033 perror("open visual save file fd");
3039 /* write the actual data file */
3042 dprintf(vb.fd,"# [P] %d %08.f <%f,%f,%f>\n",
3043 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
3045 // atomic configuration
3046 for(i=0;i<moldyn->count;i++)
3047 // atom type, positions, color and kinetic energy
3048 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
3052 pse_col[atom[i].element],
3055 // bonds between atoms
3056 process_2b_bonds(moldyn,&vb,visual_bonds_process);
3060 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3061 -dim.x/2,-dim.y/2,-dim.z/2,
3062 dim.x/2,-dim.y/2,-dim.z/2);
3063 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3064 -dim.x/2,-dim.y/2,-dim.z/2,
3065 -dim.x/2,dim.y/2,-dim.z/2);
3066 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3067 dim.x/2,dim.y/2,-dim.z/2,
3068 dim.x/2,-dim.y/2,-dim.z/2);
3069 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3070 -dim.x/2,dim.y/2,-dim.z/2,
3071 dim.x/2,dim.y/2,-dim.z/2);
3073 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3074 -dim.x/2,-dim.y/2,dim.z/2,
3075 dim.x/2,-dim.y/2,dim.z/2);
3076 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3077 -dim.x/2,-dim.y/2,dim.z/2,
3078 -dim.x/2,dim.y/2,dim.z/2);
3079 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3080 dim.x/2,dim.y/2,dim.z/2,
3081 dim.x/2,-dim.y/2,dim.z/2);
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);
3086 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3087 -dim.x/2,-dim.y/2,dim.z/2,
3088 -dim.x/2,-dim.y/2,-dim.z/2);
3089 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3090 -dim.x/2,dim.y/2,dim.z/2,
3091 -dim.x/2,dim.y/2,-dim.z/2);
3092 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3093 dim.x/2,-dim.y/2,dim.z/2,
3094 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);
3113 * fpu cntrol functions
3116 // set rounding to double (eliminates -ffloat-store!)
3117 int fpu_set_rtd(void) {
3123 ctrl&=~_FPU_EXTENDED;