2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
21 #include "report/report.h"
23 /* potential includes */
24 #include "potentials/harmonic_oscillator.h"
25 #include "potentials/lennard_jones.h"
26 #include "potentials/albe.h"
28 #include "potentials/tersoff_orig.h"
30 #include "potentials/tersoff.h"
35 * global variables, pse and atom colors (only needed here)
38 static char *pse_name[]={
60 static char *pse_col[]={
83 * the moldyn functions
86 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
88 printf("[moldyn] init\n");
90 memset(moldyn,0,sizeof(t_moldyn));
95 rand_init(&(moldyn->random),NULL,1);
96 moldyn->random.status|=RAND_STAT_VERBOSE;
101 int moldyn_shutdown(t_moldyn *moldyn) {
103 printf("[moldyn] shutdown\n");
105 moldyn_log_shutdown(moldyn);
106 link_cell_shutdown(moldyn);
107 rand_close(&(moldyn->random));
113 int set_int_alg(t_moldyn *moldyn,u8 algo) {
115 printf("[moldyn] integration algorithm: ");
118 case MOLDYN_INTEGRATE_VERLET:
119 moldyn->integrate=velocity_verlet;
120 printf("velocity verlet\n");
123 printf("unknown integration algorithm: %02x\n",algo);
131 int set_cutoff(t_moldyn *moldyn,double cutoff) {
133 moldyn->cutoff=cutoff;
135 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
140 int set_bondlen(t_moldyn *moldyn,double b0,double b1,double bm) {
142 moldyn->bondlen[0]=b0*b0;
143 moldyn->bondlen[1]=b1*b1;
145 moldyn->bondlen[2]=b0*b1;
147 moldyn->bondlen[2]=bm*bm;
152 int set_temperature(t_moldyn *moldyn,double t_ref) {
156 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
161 int set_pressure(t_moldyn *moldyn,double p_ref) {
165 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
170 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
172 moldyn->pt_scale=(ptype|ttype);
176 printf("[moldyn] p/t scaling:\n");
178 printf(" p: %s",ptype?"yes":"no ");
180 printf(" | type: %02x | factor: %f",ptype,ptc);
183 printf(" t: %s",ttype?"yes":"no ");
185 printf(" | type: %02x | factor: %f",ttype,ttc);
191 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
197 moldyn->volume=x*y*z;
205 printf("[moldyn] dimensions in A and A^3 respectively:\n");
206 printf(" x: %f\n",moldyn->dim.x);
207 printf(" y: %f\n",moldyn->dim.y);
208 printf(" z: %f\n",moldyn->dim.z);
209 printf(" volume: %f\n",moldyn->volume);
210 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
215 int set_nn_dist(t_moldyn *moldyn,double dist) {
222 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
224 printf("[moldyn] periodic boundary conditions:\n");
227 moldyn->status|=MOLDYN_STAT_PBX;
230 moldyn->status|=MOLDYN_STAT_PBY;
233 moldyn->status|=MOLDYN_STAT_PBZ;
235 printf(" x: %s\n",x?"yes":"no");
236 printf(" y: %s\n",y?"yes":"no");
237 printf(" z: %s\n",z?"yes":"no");
242 int set_potential(t_moldyn *moldyn,u8 type) {
245 case MOLDYN_POTENTIAL_TM:
246 moldyn->func1b=tersoff_mult_1bp;
247 moldyn->func3b_j1=tersoff_mult_3bp_j1;
248 moldyn->func3b_k1=tersoff_mult_3bp_k1;
249 moldyn->func3b_j2=tersoff_mult_3bp_j2;
250 moldyn->func3b_k2=tersoff_mult_3bp_k2;
251 // missing: check 2b bond func
253 case MOLDYN_POTENTIAL_AM:
254 moldyn->func3b_j1=albe_mult_3bp_j1;
255 moldyn->func3b_k1=albe_mult_3bp_k1;
256 moldyn->func3b_j2=albe_mult_3bp_j2;
257 moldyn->func3b_k2=albe_mult_3bp_k2;
258 moldyn->check_2b_bond=albe_mult_check_2b_bond;
260 case MOLDYN_POTENTIAL_HO:
261 moldyn->func2b=harmonic_oscillator;
262 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
264 case MOLDYN_POTENTIAL_LJ:
265 moldyn->func2b=lennard_jones;
266 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
269 printf("[moldyn] set potential: unknown type %02x\n",
277 int set_avg_skip(t_moldyn *moldyn,int skip) {
279 printf("[moldyn] skip %d steps before starting average calc\n",skip);
280 moldyn->avg_skip=skip;
285 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
287 strncpy(moldyn->vlsdir,dir,127);
292 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
294 strncpy(moldyn->rauthor,author,63);
295 strncpy(moldyn->rtitle,title,63);
300 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
305 printf("[moldyn] set log: ");
308 case LOG_TOTAL_ENERGY:
309 moldyn->ewrite=timer;
310 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
311 moldyn->efd=open(filename,
312 O_WRONLY|O_CREAT|O_EXCL,
315 perror("[moldyn] energy log fd open");
318 dprintf(moldyn->efd,"# total energy log file\n");
319 printf("total energy (%d)\n",timer);
321 case LOG_TOTAL_MOMENTUM:
322 moldyn->mwrite=timer;
323 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
324 moldyn->mfd=open(filename,
325 O_WRONLY|O_CREAT|O_EXCL,
328 perror("[moldyn] momentum log fd open");
331 dprintf(moldyn->efd,"# total momentum log file\n");
332 printf("total momentum (%d)\n",timer);
335 moldyn->pwrite=timer;
336 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
337 moldyn->pfd=open(filename,
338 O_WRONLY|O_CREAT|O_EXCL,
341 perror("[moldyn] pressure log file\n");
344 dprintf(moldyn->pfd,"# pressure log file\n");
345 printf("pressure (%d)\n",timer);
347 case LOG_TEMPERATURE:
348 moldyn->twrite=timer;
349 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
350 moldyn->tfd=open(filename,
351 O_WRONLY|O_CREAT|O_EXCL,
354 perror("[moldyn] temperature log file\n");
357 dprintf(moldyn->tfd,"# temperature log file\n");
358 printf("temperature (%d)\n",timer);
361 moldyn->vwrite=timer;
362 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
363 moldyn->vfd=open(filename,
364 O_WRONLY|O_CREAT|O_EXCL,
367 perror("[moldyn] volume log file\n");
370 dprintf(moldyn->vfd,"# volume log file\n");
371 printf("volume (%d)\n",timer);
374 moldyn->swrite=timer;
375 printf("save file (%d)\n",timer);
378 moldyn->awrite=timer;
379 ret=visual_init(moldyn,moldyn->vlsdir);
381 printf("[moldyn] visual init failure\n");
384 printf("visual file (%d)\n",timer);
387 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
388 moldyn->rfd=open(filename,
389 O_WRONLY|O_CREAT|O_EXCL,
392 perror("[moldyn] report fd open");
395 printf("report -> ");
397 snprintf(filename,127,"%s/e_plot.scr",
399 moldyn->epfd=open(filename,
400 O_WRONLY|O_CREAT|O_EXCL,
403 perror("[moldyn] energy plot fd open");
406 dprintf(moldyn->epfd,e_plot_script);
411 snprintf(filename,127,"%s/pressure_plot.scr",
413 moldyn->ppfd=open(filename,
414 O_WRONLY|O_CREAT|O_EXCL,
417 perror("[moldyn] p plot fd open");
420 dprintf(moldyn->ppfd,pressure_plot_script);
425 snprintf(filename,127,"%s/temperature_plot.scr",
427 moldyn->tpfd=open(filename,
428 O_WRONLY|O_CREAT|O_EXCL,
431 perror("[moldyn] t plot fd open");
434 dprintf(moldyn->tpfd,temperature_plot_script);
436 printf("temperature ");
438 dprintf(moldyn->rfd,report_start,
439 moldyn->rauthor,moldyn->rtitle);
443 printf("unknown log type: %02x\n",type);
450 int moldyn_log_shutdown(t_moldyn *moldyn) {
454 printf("[moldyn] log shutdown\n");
458 dprintf(moldyn->rfd,report_energy);
459 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
464 if(moldyn->mfd) close(moldyn->mfd);
468 dprintf(moldyn->rfd,report_pressure);
469 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
476 dprintf(moldyn->rfd,report_temperature);
477 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
482 dprintf(moldyn->rfd,report_end);
484 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
487 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
490 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
499 * creating lattice functions
502 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
503 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
514 /* how many atoms do we expect */
515 if(type==CUBIC) new*=1;
516 if(type==FCC) new*=4;
517 if(type==DIAMOND) new*=8;
519 /* allocate space for atoms */
520 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
522 perror("[moldyn] realloc (create lattice)");
526 atom=&(moldyn->atom[count]);
528 /* no atoms on the boundaries (only reason: it looks better!) */
542 set_nn_dist(moldyn,lc);
543 ret=cubic_init(a,b,c,lc,atom,&orig);
547 v3_scale(&orig,&orig,0.5);
548 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
549 ret=fcc_init(a,b,c,lc,atom,&orig);
553 v3_scale(&orig,&orig,0.25);
554 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
555 ret=diamond_init(a,b,c,lc,atom,&orig);
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 lattice with %d atoms\n",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)");
609 /* initialize new atom */
610 memset(&(atom[count]),0,sizeof(t_atom));
613 atom[count].element=element;
614 atom[count].mass=mass;
615 atom[count].brand=brand;
616 atom[count].tag=count;
617 atom[count].attr=attr;
618 check_per_bound(moldyn,&(atom[count].r));
619 atom[count].r_0=atom[count].r;
621 /* update total system mass */
622 total_mass_calc(moldyn);
627 int del_atom(t_moldyn *moldyn,int tag) {
634 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
636 perror("[moldyn]malloc (del atom)");
640 for(cnt=0;cnt<tag;cnt++)
643 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
645 new[cnt-1].tag=cnt-1;
657 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
676 v3_copy(&(atom[count].r),&r);
685 for(i=0;i<count;i++) {
686 atom[i].r.x-=(a*lc)/2.0;
687 atom[i].r.y-=(b*lc)/2.0;
688 atom[i].r.z-=(c*lc)/2.0;
694 /* fcc lattice init */
695 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
708 /* construct the basis */
709 memset(basis,0,3*sizeof(t_3dvec));
717 /* fill up the room */
725 v3_copy(&(atom[count].r),&r);
728 /* the three face centered atoms */
730 v3_add(&n,&r,&basis[l]);
731 v3_copy(&(atom[count].r),&n);
740 /* coordinate transformation */
741 for(i=0;i<count;i++) {
742 atom[i].r.x-=(a*lc)/2.0;
743 atom[i].r.y-=(b*lc)/2.0;
744 atom[i].r.z-=(c*lc)/2.0;
750 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
755 count=fcc_init(a,b,c,lc,atom,origin);
761 if(origin) v3_add(&o,&o,origin);
763 count+=fcc_init(a,b,c,lc,&atom[count],&o);
768 int destroy_atoms(t_moldyn *moldyn) {
770 if(moldyn->atom) free(moldyn->atom);
775 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
778 * - gaussian distribution of velocities
779 * - zero total momentum
780 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
785 t_3dvec p_total,delta;
790 random=&(moldyn->random);
792 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
794 /* gaussian distribution of velocities */
796 for(i=0;i<moldyn->count;i++) {
797 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
799 v=sigma*rand_get_gauss(random);
801 p_total.x+=atom[i].mass*v;
803 v=sigma*rand_get_gauss(random);
805 p_total.y+=atom[i].mass*v;
807 v=sigma*rand_get_gauss(random);
809 p_total.z+=atom[i].mass*v;
812 /* zero total momentum */
813 v3_scale(&p_total,&p_total,1.0/moldyn->count);
814 for(i=0;i<moldyn->count;i++) {
815 v3_scale(&delta,&p_total,1.0/atom[i].mass);
816 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
819 /* velocity scaling */
820 scale_velocity(moldyn,equi_init);
825 double total_mass_calc(t_moldyn *moldyn) {
831 for(i=0;i<moldyn->count;i++)
832 moldyn->mass+=moldyn->atom[i].mass;
837 double temperature_calc(t_moldyn *moldyn) {
839 /* assume up to date kinetic energy, which is 3/2 N k_B T */
841 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
846 double get_temperature(t_moldyn *moldyn) {
851 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
861 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
864 /* get kinetic energy / temperature & count involved atoms */
867 for(i=0;i<moldyn->count;i++) {
868 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
869 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
874 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
875 else return 0; /* no atoms involved in scaling! */
877 /* (temporary) hack for e,t = 0 */
880 if(moldyn->t_ref!=0.0) {
881 thermal_init(moldyn,equi_init);
885 return 0; /* no scaling needed */
889 /* get scaling factor */
890 scale=moldyn->t_ref/moldyn->t;
894 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
895 scale=1.0+(scale-1.0)/moldyn->t_tc;
898 /* velocity scaling */
899 for(i=0;i<moldyn->count;i++) {
900 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
901 v3_scale(&(atom[i].v),&(atom[i].v),scale);
907 double ideal_gas_law_pressure(t_moldyn *moldyn) {
911 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
916 double virial_sum(t_moldyn *moldyn) {
921 /* virial (sum over atom virials) */
929 for(i=0;i<moldyn->count;i++) {
930 virial=&(moldyn->atom[i].virial);
931 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
932 moldyn->vir.xx+=virial->xx;
933 moldyn->vir.yy+=virial->yy;
934 moldyn->vir.zz+=virial->zz;
935 moldyn->vir.xy+=virial->xy;
936 moldyn->vir.xz+=virial->xz;
937 moldyn->vir.yz+=virial->yz;
940 /* global virial (absolute coordinates) */
941 virial=&(moldyn->gvir);
942 moldyn->gv=virial->xx+virial->yy+virial->zz;
944 return moldyn->virial;
947 double pressure_calc(t_moldyn *moldyn) {
951 * with W = 1/3 sum_i f_i r_i (- skipped!)
952 * virial = sum_i f_i r_i
954 * => P = (2 Ekin + virial) / (3V)
957 /* assume up to date virial & up to date kinetic energy */
959 /* pressure (atom virials) */
960 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
961 moldyn->p/=(3.0*moldyn->volume);
963 /* pressure (absolute coordinates) */
964 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
965 moldyn->gp/=(3.0*moldyn->volume);
970 int average_reset(t_moldyn *moldyn) {
972 printf("[moldyn] average reset\n");
974 /* update skip value */
975 moldyn->avg_skip=moldyn->total_steps;
981 /* potential energy */
989 moldyn->virial_sum=0.0;
1000 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1004 if(moldyn->total_steps<moldyn->avg_skip)
1007 denom=moldyn->total_steps+1-moldyn->avg_skip;
1009 /* assume up to date energies, temperature, pressure etc */
1011 /* kinetic energy */
1012 moldyn->k_sum+=moldyn->ekin;
1013 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1014 moldyn->k_avg=moldyn->k_sum/denom;
1015 moldyn->k2_avg=moldyn->k2_sum/denom;
1016 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1018 /* potential energy */
1019 moldyn->v_sum+=moldyn->energy;
1020 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1021 moldyn->v_avg=moldyn->v_sum/denom;
1022 moldyn->v2_avg=moldyn->v2_sum/denom;
1023 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1026 moldyn->t_sum+=moldyn->t;
1027 moldyn->t_avg=moldyn->t_sum/denom;
1030 moldyn->virial_sum+=moldyn->virial;
1031 moldyn->virial_avg=moldyn->virial_sum/denom;
1032 moldyn->gv_sum+=moldyn->gv;
1033 moldyn->gv_avg=moldyn->gv_sum/denom;
1036 moldyn->p_sum+=moldyn->p;
1037 moldyn->p_avg=moldyn->p_sum/denom;
1038 moldyn->gp_sum+=moldyn->gp;
1039 moldyn->gp_avg=moldyn->gp_sum/denom;
1040 moldyn->tp_sum+=moldyn->tp;
1041 moldyn->tp_avg=moldyn->tp_sum/denom;
1046 int get_heat_capacity(t_moldyn *moldyn) {
1050 /* averages needed for heat capacity calc */
1051 if(moldyn->total_steps<moldyn->avg_skip)
1054 /* (temperature average)^2 */
1055 temp2=moldyn->t_avg*moldyn->t_avg;
1056 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1059 /* ideal gas contribution */
1060 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1061 printf(" ideal gas contribution: %f\n",
1062 ighc/moldyn->mass*KILOGRAM/JOULE);
1064 /* specific heat for nvt ensemble */
1065 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1066 moldyn->c_v_nvt/=moldyn->mass;
1068 /* specific heat for nve ensemble */
1069 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1070 moldyn->c_v_nve/=moldyn->mass;
1072 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1073 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1074 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)));
1079 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1095 /* store atomic configuration + dimension */
1096 store=malloc(moldyn->count*sizeof(t_atom));
1098 printf("[moldyn] allocating store mem failed\n");
1101 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1106 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1107 su=pow(2.0-h,ONE_THIRD)-1.0;
1108 dv=(1.0-h)*moldyn->volume;
1110 /* scale up dimension and atom positions */
1111 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1112 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1113 link_cell_shutdown(moldyn);
1114 link_cell_init(moldyn,QUIET);
1115 potential_force_calc(moldyn);
1118 /* restore atomic configuration + dim */
1119 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1122 /* scale down dimension and atom positions */
1123 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1124 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1125 link_cell_shutdown(moldyn);
1126 link_cell_init(moldyn,QUIET);
1127 potential_force_calc(moldyn);
1130 /* calculate pressure */
1131 moldyn->tp=-(y1-y0)/(2.0*dv);
1133 /* restore atomic configuration */
1134 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1136 link_cell_shutdown(moldyn);
1137 link_cell_init(moldyn,QUIET);
1138 //potential_force_calc(moldyn);
1140 /* free store buffer */
1147 double get_pressure(t_moldyn *moldyn) {
1153 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1165 if(x) dim->x*=scale;
1166 if(y) dim->y*=scale;
1167 if(z) dim->z*=scale;
1172 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1183 for(i=0;i<moldyn->count;i++) {
1184 r=&(moldyn->atom[i].r);
1193 int scale_volume(t_moldyn *moldyn) {
1199 vdim=&(moldyn->vis.dim);
1203 /* scaling factor */
1204 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1205 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1206 scale=pow(scale,ONE_THIRD);
1209 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1212 /* scale the atoms and dimensions */
1213 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1214 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1216 /* visualize dimensions */
1223 /* recalculate scaled volume */
1224 moldyn->volume=dim->x*dim->y*dim->z;
1226 /* adjust/reinit linkcell */
1227 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1228 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1229 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1230 link_cell_shutdown(moldyn);
1231 link_cell_init(moldyn,QUIET);
1242 double e_kin_calc(t_moldyn *moldyn) {
1250 for(i=0;i<moldyn->count;i++) {
1251 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1252 moldyn->ekin+=atom[i].ekin;
1255 return moldyn->ekin;
1258 double get_total_energy(t_moldyn *moldyn) {
1260 return(moldyn->ekin+moldyn->energy);
1263 t_3dvec get_total_p(t_moldyn *moldyn) {
1272 for(i=0;i<moldyn->count;i++) {
1273 v3_scale(&p,&(atom[i].v),atom[i].mass);
1274 v3_add(&p_total,&p_total,&p);
1280 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1284 /* nn_dist is the nearest neighbour distance */
1286 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1295 /* linked list / cell method */
1297 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1304 /* partitioning the md cell */
1305 lc->nx=moldyn->dim.x/moldyn->cutoff;
1306 lc->x=moldyn->dim.x/lc->nx;
1307 lc->ny=moldyn->dim.y/moldyn->cutoff;
1308 lc->y=moldyn->dim.y/lc->ny;
1309 lc->nz=moldyn->dim.z/moldyn->cutoff;
1310 lc->z=moldyn->dim.z/lc->nz;
1311 lc->cells=lc->nx*lc->ny*lc->nz;
1314 lc->subcell=malloc(lc->cells*sizeof(int*));
1316 lc->subcell=malloc(lc->cells*sizeof(t_list));
1319 if(lc->subcell==NULL) {
1320 perror("[moldyn] cell init (malloc)");
1325 printf("[moldyn] FATAL: less then 27 subcells!\n");
1329 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1332 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1335 printf(" x: %d x %f A\n",lc->nx,lc->x);
1336 printf(" y: %d x %f A\n",lc->ny,lc->y);
1337 printf(" z: %d x %f A\n",lc->nz,lc->z);
1342 for(i=0;i<lc->cells;i++) {
1343 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1344 if(lc->subcell[i]==NULL) {
1345 perror("[moldyn] list init (malloc)");
1350 printf(" ---> %d malloc %p (%p)\n",
1351 i,lc->subcell[0],lc->subcell);
1355 for(i=0;i<lc->cells;i++)
1356 list_init_f(&(lc->subcell[i]));
1359 /* update the list */
1360 link_cell_update(moldyn);
1365 int link_cell_update(t_moldyn *moldyn) {
1381 for(i=0;i<lc->cells;i++)
1383 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1385 list_destroy_f(&(lc->subcell[i]));
1388 for(count=0;count<moldyn->count;count++) {
1389 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1390 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1391 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1395 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1398 if(p>=MAX_ATOMS_PER_LIST) {
1399 printf("[moldyn] FATAL: amount of atoms too high!\n");
1403 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1405 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1409 printf(" ---> %d %d malloc %p (%p)\n",
1410 i,count,lc->subcell[i].current,lc->subcell);
1418 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1442 if(i>=nx||j>=ny||k>=nz)
1443 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1446 cell[0]=lc->subcell[i+j*nx+k*a];
1447 for(ci=-1;ci<=1;ci++) {
1450 if((x<0)||(x>=nx)) {
1454 for(cj=-1;cj<=1;cj++) {
1457 if((y<0)||(y>=ny)) {
1461 for(ck=-1;ck<=1;ck++) {
1464 if((z<0)||(z>=nz)) {
1468 if(!(ci|cj|ck)) continue;
1470 cell[--count2]=lc->subcell[x+y*nx+z*a];
1473 cell[count1++]=lc->subcell[x+y*nx+z*a];
1484 int link_cell_shutdown(t_moldyn *moldyn) {
1491 for(i=0;i<lc->cells;i++) {
1493 free(lc->subcell[i]);
1495 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1496 list_destroy_f(&(lc->subcell[i]));
1505 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1509 t_moldyn_schedule *schedule;
1511 schedule=&(moldyn->schedule);
1512 count=++(schedule->total_sched);
1514 ptr=realloc(schedule->runs,count*sizeof(int));
1516 perror("[moldyn] realloc (runs)");
1520 schedule->runs[count-1]=runs;
1522 ptr=realloc(schedule->tau,count*sizeof(double));
1524 perror("[moldyn] realloc (tau)");
1528 schedule->tau[count-1]=tau;
1530 printf("[moldyn] schedule added:\n");
1531 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1537 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1539 moldyn->schedule.hook=hook;
1540 moldyn->schedule.hook_params=hook_params;
1547 * 'integration of newtons equation' - algorithms
1551 /* start the integration */
1553 int moldyn_integrate(t_moldyn *moldyn) {
1556 unsigned int e,m,s,v,p,t,a;
1558 t_moldyn_schedule *sched;
1563 double energy_scale;
1564 struct timeval t1,t2;
1567 sched=&(moldyn->schedule);
1570 /* initialize linked cell method */
1571 link_cell_init(moldyn,VERBOSE);
1573 /* logging & visualization */
1582 /* sqaure of some variables */
1583 moldyn->tau_square=moldyn->tau*moldyn->tau;
1584 moldyn->cutoff_square=moldyn->cutoff*moldyn->cutoff;
1586 /* get current time */
1587 gettimeofday(&t1,NULL);
1589 /* calculate initial forces */
1590 potential_force_calc(moldyn);
1595 /* some stupid checks before we actually start calculating bullshit */
1596 if(moldyn->cutoff>0.5*moldyn->dim.x)
1597 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1598 if(moldyn->cutoff>0.5*moldyn->dim.y)
1599 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1600 if(moldyn->cutoff>0.5*moldyn->dim.z)
1601 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1602 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1603 if(ds>0.05*moldyn->nnd)
1604 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1606 /* zero absolute time */
1608 moldyn->total_steps=0;
1610 /* debugging, ignore */
1613 /* tell the world */
1614 printf("[moldyn] integration start, go get a coffee ...\n");
1616 /* executing the schedule */
1618 while(sched->count<sched->total_sched) {
1620 /* setting amount of runs and finite time step size */
1621 moldyn->tau=sched->tau[sched->count];
1622 moldyn->tau_square=moldyn->tau*moldyn->tau;
1623 moldyn->time_steps=sched->runs[sched->count];
1625 /* energy scaling factor (might change!) */
1626 energy_scale=moldyn->count*EV;
1628 /* integration according to schedule */
1630 for(i=0;i<moldyn->time_steps;i++) {
1632 /* integration step */
1633 moldyn->integrate(moldyn);
1635 /* calculate kinetic energy, temperature and pressure */
1637 temperature_calc(moldyn);
1639 pressure_calc(moldyn);
1640 //thermodynamic_pressure_calc(moldyn);
1642 /* calculate fluctuations + averages */
1643 average_and_fluctuation_calc(moldyn);
1646 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1647 scale_velocity(moldyn,FALSE);
1648 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1649 scale_volume(moldyn);
1651 /* check for log & visualization */
1653 if(!(moldyn->total_steps%e))
1654 dprintf(moldyn->efd,
1656 moldyn->time,moldyn->ekin/energy_scale,
1657 moldyn->energy/energy_scale,
1658 get_total_energy(moldyn)/energy_scale);
1661 if(!(moldyn->total_steps%m)) {
1662 momentum=get_total_p(moldyn);
1663 dprintf(moldyn->mfd,
1664 "%f %f %f %f %f\n",moldyn->time,
1665 momentum.x,momentum.y,momentum.z,
1666 v3_norm(&momentum));
1670 if(!(moldyn->total_steps%p)) {
1671 dprintf(moldyn->pfd,
1672 "%f %f %f %f %f %f %f\n",moldyn->time,
1673 moldyn->p/BAR,moldyn->p_avg/BAR,
1674 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1675 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1679 if(!(moldyn->total_steps%t)) {
1680 dprintf(moldyn->tfd,
1682 moldyn->time,moldyn->t,moldyn->t_avg);
1686 if(!(moldyn->total_steps%v)) {
1687 dprintf(moldyn->vfd,
1688 "%f %f\n",moldyn->time,moldyn->volume);
1692 if(!(moldyn->total_steps%s)) {
1693 snprintf(dir,128,"%s/s-%07.f.save",
1694 moldyn->vlsdir,moldyn->time);
1695 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1697 if(fd<0) perror("[moldyn] save fd open");
1699 write(fd,moldyn,sizeof(t_moldyn));
1700 write(fd,moldyn->atom,
1701 moldyn->count*sizeof(t_atom));
1707 if(!(moldyn->total_steps%a)) {
1708 visual_atoms(moldyn);
1712 /* display progress */
1713 //if(!(moldyn->total_steps%10)) {
1714 /* get current time */
1715 gettimeofday(&t2,NULL);
1717 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1718 sched->count,i,moldyn->total_steps,
1719 moldyn->t,moldyn->t_avg,
1720 moldyn->p/BAR,moldyn->p_avg/BAR,
1722 (int)(t2.tv_sec-t1.tv_sec));
1726 /* copy over time */
1730 /* increase absolute time */
1731 moldyn->time+=moldyn->tau;
1732 moldyn->total_steps+=1;
1736 /* check for hooks */
1738 printf("\n ## schedule hook %d start ##\n",
1740 sched->hook(moldyn,sched->hook_params);
1741 printf(" ## schedule hook end ##\n");
1744 /* increase the schedule counter */
1752 /* velocity verlet */
1754 int velocity_verlet(t_moldyn *moldyn) {
1757 double tau,tau_square,h;
1762 count=moldyn->count;
1764 tau_square=moldyn->tau_square;
1766 for(i=0;i<count;i++) {
1767 /* check whether fixed atom */
1768 if(atom[i].attr&ATOM_ATTR_FP)
1772 v3_scale(&delta,&(atom[i].v),tau);
1773 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1774 v3_scale(&delta,&(atom[i].f),h*tau_square);
1775 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1776 check_per_bound(moldyn,&(atom[i].r));
1778 /* velocities [actually v(t+tau/2)] */
1779 v3_scale(&delta,&(atom[i].f),h*tau);
1780 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1783 /* criticial check */
1784 moldyn_bc_check(moldyn);
1786 /* neighbour list update */
1787 link_cell_update(moldyn);
1789 /* forces depending on chosen potential */
1790 potential_force_calc(moldyn);
1792 for(i=0;i<count;i++) {
1793 /* check whether fixed atom */
1794 if(atom[i].attr&ATOM_ATTR_FP)
1796 /* again velocities [actually v(t+tau)] */
1797 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1798 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1807 * potentials & corresponding forces & virial routine
1811 /* generic potential and force calculation */
1813 int potential_force_calc(t_moldyn *moldyn) {
1816 t_atom *itom,*jtom,*ktom;
1820 int *neighbour_i[27];
1824 t_list neighbour_i[27];
1825 t_list neighbour_i2[27];
1831 count=moldyn->count;
1841 /* reset global virial */
1842 memset(&(moldyn->gvir),0,sizeof(t_virial));
1844 /* reset force, site energy and virial of every atom */
1845 for(i=0;i<count;i++) {
1848 v3_zero(&(itom[i].f));
1851 virial=(&(itom[i].virial));
1859 /* reset site energy */
1864 /* get energy, force and virial of every atom */
1866 /* first (and only) loop over atoms i */
1867 for(i=0;i<count;i++) {
1869 /* single particle potential/force */
1870 if(itom[i].attr&ATOM_ATTR_1BP)
1872 moldyn->func1b(moldyn,&(itom[i]));
1874 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1877 /* 2 body pair potential/force */
1879 link_cell_neighbour_index(moldyn,
1880 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1881 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1882 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1887 /* first loop over atoms j */
1888 if(moldyn->func2b) {
1895 while(neighbour_i[j][p]!=0) {
1897 jtom=&(atom[neighbour_i[j][p]]);
1900 if(jtom==&(itom[i]))
1903 if((jtom->attr&ATOM_ATTR_2BP)&
1904 (itom[i].attr&ATOM_ATTR_2BP)) {
1905 moldyn->func2b(moldyn,
1912 this=&(neighbour_i[j]);
1915 if(this->start==NULL)
1919 jtom=this->current->data;
1921 if(jtom==&(itom[i]))
1924 if((jtom->attr&ATOM_ATTR_2BP)&
1925 (itom[i].attr&ATOM_ATTR_2BP)) {
1926 moldyn->func2b(moldyn,
1931 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
1937 /* 3 body potential/force */
1939 if(!(itom[i].attr&ATOM_ATTR_3BP))
1942 /* copy the neighbour lists */
1944 /* no copy needed for static lists */
1946 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
1949 /* second loop over atoms j */
1956 while(neighbour_i[j][p]!=0) {
1958 jtom=&(atom[neighbour_i[j][p]]);
1961 this=&(neighbour_i[j]);
1964 if(this->start==NULL)
1969 jtom=this->current->data;
1972 if(jtom==&(itom[i]))
1975 if(!(jtom->attr&ATOM_ATTR_3BP))
1981 if(moldyn->func3b_j1)
1982 moldyn->func3b_j1(moldyn,
1987 /* in first j loop, 3bp run can be skipped */
1988 if(!(moldyn->run3bp))
1991 /* first loop over atoms k */
1992 if(moldyn->func3b_k1) {
2000 while(neighbour_i[j][q]!=0) {
2002 ktom=&(atom[neighbour_i[k][q]]);
2005 that=&(neighbour_i2[k]);
2008 if(that->start==NULL)
2012 ktom=that->current->data;
2015 if(!(ktom->attr&ATOM_ATTR_3BP))
2021 if(ktom==&(itom[i]))
2024 moldyn->func3b_k1(moldyn,
2032 } while(list_next_f(that)!=\
2040 if(moldyn->func3b_j2)
2041 moldyn->func3b_j2(moldyn,
2046 /* second loop over atoms k */
2047 if(moldyn->func3b_k2) {
2055 while(neighbour_i[j][q]!=0) {
2057 ktom=&(atom[neighbour_i[k][q]]);
2060 that=&(neighbour_i2[k]);
2063 if(that->start==NULL)
2067 ktom=that->current->data;
2070 if(!(ktom->attr&ATOM_ATTR_3BP))
2076 if(ktom==&(itom[i]))
2079 moldyn->func3b_k2(moldyn,
2088 } while(list_next_f(that)!=\
2096 /* 2bp post function */
2097 if(moldyn->func3b_j3) {
2098 moldyn->func3b_j3(moldyn,
2105 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2120 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2121 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2123 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2124 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2125 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2129 /* some postprocessing */
2130 for(i=0;i<count;i++) {
2131 /* calculate global virial */
2132 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2133 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2134 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2135 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2136 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2137 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2139 /* check forces regarding the given timestep */
2140 if(v3_norm(&(itom[i].f))>\
2141 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2142 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2150 * virial calculation
2153 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2154 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2156 a->virial.xx+=f->x*d->x;
2157 a->virial.yy+=f->y*d->y;
2158 a->virial.zz+=f->z*d->z;
2159 a->virial.xy+=f->x*d->y;
2160 a->virial.xz+=f->x*d->z;
2161 a->virial.yz+=f->y*d->z;
2167 * periodic boundary checking
2170 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2171 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2182 if(moldyn->status&MOLDYN_STAT_PBX) {
2183 if(a->x>=x) a->x-=dim->x;
2184 else if(-a->x>x) a->x+=dim->x;
2186 if(moldyn->status&MOLDYN_STAT_PBY) {
2187 if(a->y>=y) a->y-=dim->y;
2188 else if(-a->y>y) a->y+=dim->y;
2190 if(moldyn->status&MOLDYN_STAT_PBZ) {
2191 if(a->z>=z) a->z-=dim->z;
2192 else if(-a->z>z) a->z+=dim->z;
2199 * debugging / critical check functions
2202 int moldyn_bc_check(t_moldyn *moldyn) {
2215 for(i=0;i<moldyn->count;i++) {
2216 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2217 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2218 i,atom[i].r.x,dim->x/2);
2219 printf("diagnostic:\n");
2220 printf("-----------\natom.r.x:\n");
2222 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2225 ((byte)&(1<<k))?1:0,
2228 printf("---------------\nx=dim.x/2:\n");
2230 memcpy(&byte,(u8 *)(&x)+j,1);
2233 ((byte)&(1<<k))?1:0,
2236 if(atom[i].r.x==x) printf("the same!\n");
2237 else printf("different!\n");
2239 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2240 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2241 i,atom[i].r.y,dim->y/2);
2242 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2243 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2244 i,atom[i].r.z,dim->z/2);
2254 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2261 fd=open(file,O_RDONLY);
2263 perror("[moldyn] load save file open");
2267 fsize=lseek(fd,0,SEEK_END);
2268 lseek(fd,0,SEEK_SET);
2270 size=sizeof(t_moldyn);
2273 cnt=read(fd,moldyn,size);
2275 perror("[moldyn] load save file read (moldyn)");
2281 size=moldyn->count*sizeof(t_atom);
2283 /* correcting possible atom data offset */
2285 if(fsize!=sizeof(t_moldyn)+size) {
2286 corr=fsize-sizeof(t_moldyn)-size;
2287 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2288 printf(" moifying offset:\n");
2289 printf(" - current pos: %d\n",sizeof(t_moldyn));
2290 printf(" - atom size: %d\n",size);
2291 printf(" - file size: %d\n",fsize);
2292 printf(" => correction: %d\n",corr);
2293 lseek(fd,corr,SEEK_CUR);
2296 moldyn->atom=(t_atom *)malloc(size);
2297 if(moldyn->atom==NULL) {
2298 perror("[moldyn] load save file malloc (atoms)");
2303 cnt=read(fd,moldyn->atom,size);
2305 perror("[moldyn] load save file read (atoms)");
2316 int moldyn_free_save_file(t_moldyn *moldyn) {
2323 int moldyn_load(t_moldyn *moldyn) {
2331 * function to find/callback all combinations of 2 body bonds
2334 int process_2b_bonds(t_moldyn *moldyn,void *data,
2335 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2336 void *data,u8 bc)) {
2343 t_list neighbour[27];
2352 link_cell_init(moldyn,VERBOSE);
2356 for(i=0;i<moldyn->count;i++) {
2357 /* neighbour indexing */
2358 link_cell_neighbour_index(moldyn,
2359 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2360 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2361 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2366 bc=(j<lc->dnlc)?0:1;
2371 while(neighbour[j][p]!=0) {
2373 jtom=&(moldyn->atom[neighbour[j][p]]);
2376 this=&(neighbour[j]);
2379 if(this->start==NULL)
2384 jtom=this->current->data;
2388 process(moldyn,&(itom[i]),jtom,data,bc);
2393 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2403 * post processing functions
2406 int get_line(int fd,char *line,int max) {
2413 if(count==max) return count;
2414 ret=read(fd,line+count,1);
2415 if(ret<=0) return ret;
2416 if(line[count]=='\n') {
2424 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2430 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2446 for(i=0;i<moldyn->count;i++) {
2448 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2449 check_per_bound(moldyn,&dist);
2450 d2=v3_absolute_square(&dist);
2464 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2465 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2466 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2471 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2476 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2477 t_atom *jtom,void *data,u8 bc) {
2484 /* only count pairs once,
2485 * skip same atoms */
2486 if(itom->tag>=jtom->tag)
2490 * pair correlation calc
2497 v3_sub(&dist,&(jtom->r),&(itom->r));
2498 if(bc) check_per_bound(moldyn,&dist);
2499 d=v3_absolute_square(&dist);
2501 /* ignore if greater cutoff */
2502 if(d>moldyn->cutoff_square)
2505 /* fill the slots */
2509 /* should never happen but it does 8) -
2510 * related to -ffloat-store problem! */
2512 printf("[moldyn] WARNING: pcc (%d/%d)",
2518 if(itom->brand!=jtom->brand) {
2523 /* type a - type a bonds */
2525 pcc->stat[s+pcc->o1]+=1;
2527 /* type b - type b bonds */
2528 pcc->stat[s+pcc->o2]+=1;
2534 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2541 pcc.o1=moldyn->cutoff/dr;
2544 if(pcc.o1*dr<=moldyn->cutoff)
2545 printf("[moldyn] WARNING: pcc (low #slots)\n");
2547 printf("[moldyn] pair correlation calc info:\n");
2548 printf(" time: %f\n",moldyn->time);
2549 printf(" count: %d\n",moldyn->count);
2550 printf(" cutoff: %f\n",moldyn->cutoff);
2551 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2554 pcc.stat=(double *)ptr;
2557 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2558 if(pcc.stat==NULL) {
2559 perror("[moldyn] pair correlation malloc");
2564 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2567 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2570 for(i=1;i<pcc.o1;i++) {
2571 // normalization: 4 pi r^2 dr
2572 // here: not double counting pairs -> 2 pi r r dr
2573 // ... and actually it's a constant times r^2
2576 pcc.stat[pcc.o1+i]/=norm;
2577 pcc.stat[pcc.o2+i]/=norm;
2582 /* todo: store/print pair correlation function */
2589 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2596 if(itom->tag>=jtom->tag)
2600 v3_sub(&dist,&(jtom->r),&(itom->r));
2601 if(bc) check_per_bound(moldyn,&dist);
2602 d=v3_absolute_square(&dist);
2604 /* ignore if greater or equal cutoff */
2605 if(d>moldyn->cutoff_square)
2608 /* check for potential bond */
2609 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2614 /* now count this bonding ... */
2617 /* increase total bond counter
2618 * ... double counting!
2623 ba->acnt[jtom->tag]+=1;
2625 ba->bcnt[jtom->tag]+=1;
2628 ba->acnt[itom->tag]+=1;
2630 ba->bcnt[itom->tag]+=1;
2635 int bond_analyze(t_moldyn *moldyn,double *quality) {
2637 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2645 ba.acnt=malloc(moldyn->count*sizeof(int));
2647 perror("[moldyn] bond analyze malloc (a)");
2650 memset(ba.acnt,0,moldyn->count*sizeof(int));
2652 ba.bcnt=malloc(moldyn->count*sizeof(int));
2654 perror("[moldyn] bond analyze malloc (b)");
2657 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2666 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2668 for(i=0;i<moldyn->count;i++) {
2669 if(atom[i].brand==0) {
2670 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2674 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2682 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2683 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2686 quality[0]=1.0*ccnt/cset;
2687 quality[1]=1.0*qcnt/ba.tcnt;
2690 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2691 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2698 * visualization code
2701 int visual_init(t_moldyn *moldyn,char *filebase) {
2703 strncpy(moldyn->vis.fb,filebase,128);
2708 int visual_atoms(t_moldyn *moldyn) {
2722 t_list neighbour[27];
2738 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2739 fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2741 perror("open visual save file fd");
2745 /* write the actual data file */
2748 dprintf(fd,"# [P] %d %07.f <%f,%f,%f>\n",
2749 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2751 // atomic configuration
2752 for(i=0;i<moldyn->count;i++) {
2753 // atom type, positions, color and kinetic energy
2754 dprintf(fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2758 pse_col[atom[i].element],
2762 * bond detection should usually be done by potential
2763 * functions. brrrrr! EVIL!
2765 * todo: potentials need to export a 'find_bonds' function!
2768 // bonds between atoms
2769 if(!(atom[i].attr&ATOM_ATTR_VB))
2771 link_cell_neighbour_index(moldyn,
2772 (atom[i].r.x+moldyn->dim.x/2)/lc->x,
2773 (atom[i].r.y+moldyn->dim.y/2)/lc->y,
2774 (atom[i].r.z+moldyn->dim.z/2)/lc->z,
2780 while(neighbour[j][p]!=0) {
2781 btom=&(atom[neighbour[j][p]]);
2784 list_reset_f(&neighbour[j]);
2785 if(neighbour[j].start==NULL)
2788 btom=neighbour[j].current->data;
2790 if(btom==&atom[i]) // skip identical atoms
2792 //if(btom<&atom[i]) // skip half of them
2794 v3_sub(&dist,&(atom[i].r),&(btom->r));
2795 if(bc) check_per_bound(moldyn,&dist);
2796 d2=v3_absolute_square(&dist);
2797 brand=atom[i].brand;
2798 if(brand==btom->brand) {
2799 if(d2>moldyn->bondlen[brand])
2803 if(d2>moldyn->bondlen[2])
2806 dprintf(fd,"# [B] %f %f %f %f %f %f\n",
2807 atom[i].r.x,atom[i].r.y,atom[i].r.z,
2808 btom->r.x,btom->r.y,btom->r.z);
2812 } while(list_next_f(&neighbour[j])!=L_NO_NEXT_ELEMENT);
2819 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2820 -dim.x/2,-dim.y/2,-dim.z/2,
2821 dim.x/2,-dim.y/2,-dim.z/2);
2822 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2823 -dim.x/2,-dim.y/2,-dim.z/2,
2824 -dim.x/2,dim.y/2,-dim.z/2);
2825 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2826 dim.x/2,dim.y/2,-dim.z/2,
2827 dim.x/2,-dim.y/2,-dim.z/2);
2828 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2829 -dim.x/2,dim.y/2,-dim.z/2,
2830 dim.x/2,dim.y/2,-dim.z/2);
2832 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2833 -dim.x/2,-dim.y/2,dim.z/2,
2834 dim.x/2,-dim.y/2,dim.z/2);
2835 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2836 -dim.x/2,-dim.y/2,dim.z/2,
2837 -dim.x/2,dim.y/2,dim.z/2);
2838 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2839 dim.x/2,dim.y/2,dim.z/2,
2840 dim.x/2,-dim.y/2,dim.z/2);
2841 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2842 -dim.x/2,dim.y/2,dim.z/2,
2843 dim.x/2,dim.y/2,dim.z/2);
2845 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2846 -dim.x/2,-dim.y/2,dim.z/2,
2847 -dim.x/2,-dim.y/2,-dim.z/2);
2848 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2849 -dim.x/2,dim.y/2,dim.z/2,
2850 -dim.x/2,dim.y/2,-dim.z/2);
2851 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2852 dim.x/2,-dim.y/2,dim.z/2,
2853 dim.x/2,-dim.y/2,-dim.z/2);
2854 dprintf(fd,"# [D] %f %f %f %f %f %f\n",
2855 dim.x/2,dim.y/2,dim.z/2,
2856 dim.x/2,dim.y/2,-dim.z/2);