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 static double pse_mass[]={
105 static double pse_lc[]={
129 * the moldyn functions
132 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
134 printf("[moldyn] init\n");
136 memset(moldyn,0,sizeof(t_moldyn));
141 rand_init(&(moldyn->random),NULL,1);
142 moldyn->random.status|=RAND_STAT_VERBOSE;
147 int moldyn_shutdown(t_moldyn *moldyn) {
149 printf("[moldyn] shutdown\n");
151 moldyn_log_shutdown(moldyn);
152 link_cell_shutdown(moldyn);
153 rand_close(&(moldyn->random));
159 int set_int_alg(t_moldyn *moldyn,u8 algo) {
161 printf("[moldyn] integration algorithm: ");
164 case MOLDYN_INTEGRATE_VERLET:
165 moldyn->integrate=velocity_verlet;
166 printf("velocity verlet\n");
169 printf("unknown integration algorithm: %02x\n",algo);
177 int set_cutoff(t_moldyn *moldyn,double cutoff) {
179 moldyn->cutoff=cutoff;
180 moldyn->cutoff_square=cutoff*cutoff;
182 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
187 int set_temperature(t_moldyn *moldyn,double t_ref) {
191 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
196 int set_pressure(t_moldyn *moldyn,double p_ref) {
200 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
205 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
207 moldyn->pt_scale&=(~(P_SCALE_MASK));
208 moldyn->pt_scale|=ptype;
211 printf("[moldyn] p/t scaling:\n");
213 printf(" p: %s",ptype?"yes":"no ");
215 printf(" | type: %02x | factor: %f",ptype,ptc);
221 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
223 moldyn->pt_scale&=(~(T_SCALE_MASK));
224 moldyn->pt_scale|=ttype;
227 printf("[moldyn] p/t scaling:\n");
229 printf(" t: %s",ttype?"yes":"no ");
231 printf(" | type: %02x | factor: %f",ttype,ttc);
237 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
239 moldyn->pt_scale=(ptype|ttype);
243 printf("[moldyn] p/t scaling:\n");
245 printf(" p: %s",ptype?"yes":"no ");
247 printf(" | type: %02x | factor: %f",ptype,ptc);
250 printf(" t: %s",ttype?"yes":"no ");
252 printf(" | type: %02x | factor: %f",ttype,ttc);
258 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
264 moldyn->volume=x*y*z;
272 printf("[moldyn] dimensions in A and A^3 respectively:\n");
273 printf(" x: %f\n",moldyn->dim.x);
274 printf(" y: %f\n",moldyn->dim.y);
275 printf(" z: %f\n",moldyn->dim.z);
276 printf(" volume: %f\n",moldyn->volume);
277 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
282 int set_nn_dist(t_moldyn *moldyn,double dist) {
289 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
291 printf("[moldyn] periodic boundary conditions:\n");
294 moldyn->status|=MOLDYN_STAT_PBX;
297 moldyn->status|=MOLDYN_STAT_PBY;
300 moldyn->status|=MOLDYN_STAT_PBZ;
302 printf(" x: %s\n",x?"yes":"no");
303 printf(" y: %s\n",y?"yes":"no");
304 printf(" z: %s\n",z?"yes":"no");
309 int set_potential(t_moldyn *moldyn,u8 type) {
312 case MOLDYN_POTENTIAL_TM:
313 moldyn->func1b=tersoff_mult_1bp;
314 moldyn->func3b_j1=tersoff_mult_3bp_j1;
315 moldyn->func3b_k1=tersoff_mult_3bp_k1;
316 moldyn->func3b_j2=tersoff_mult_3bp_j2;
317 moldyn->func3b_k2=tersoff_mult_3bp_k2;
318 // missing: check 2b bond func
320 case MOLDYN_POTENTIAL_AM:
321 moldyn->func3b_j1=albe_mult_3bp_j1;
322 moldyn->func3b_k1=albe_mult_3bp_k1;
323 moldyn->func3b_j2=albe_mult_3bp_j2;
324 moldyn->func3b_k2=albe_mult_3bp_k2;
325 moldyn->check_2b_bond=albe_mult_check_2b_bond;
327 case MOLDYN_POTENTIAL_HO:
328 moldyn->func2b=harmonic_oscillator;
329 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
331 case MOLDYN_POTENTIAL_LJ:
332 moldyn->func2b=lennard_jones;
333 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
336 printf("[moldyn] set potential: unknown type %02x\n",
344 int set_avg_skip(t_moldyn *moldyn,int skip) {
346 printf("[moldyn] skip %d steps before starting average calc\n",skip);
347 moldyn->avg_skip=skip;
352 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
354 strncpy(moldyn->vlsdir,dir,127);
359 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
361 strncpy(moldyn->rauthor,author,63);
362 strncpy(moldyn->rtitle,title,63);
367 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
372 printf("[moldyn] set log: ");
375 case LOG_TOTAL_ENERGY:
376 moldyn->ewrite=timer;
377 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
378 moldyn->efd=open(filename,
379 O_WRONLY|O_CREAT|O_EXCL,
382 perror("[moldyn] energy log fd open");
385 dprintf(moldyn->efd,"# total energy log file\n");
386 printf("total energy (%d)\n",timer);
388 case LOG_TOTAL_MOMENTUM:
389 moldyn->mwrite=timer;
390 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
391 moldyn->mfd=open(filename,
392 O_WRONLY|O_CREAT|O_EXCL,
395 perror("[moldyn] momentum log fd open");
398 dprintf(moldyn->efd,"# total momentum log file\n");
399 printf("total momentum (%d)\n",timer);
402 moldyn->pwrite=timer;
403 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
404 moldyn->pfd=open(filename,
405 O_WRONLY|O_CREAT|O_EXCL,
408 perror("[moldyn] pressure log file\n");
411 dprintf(moldyn->pfd,"# pressure log file\n");
412 printf("pressure (%d)\n",timer);
414 case LOG_TEMPERATURE:
415 moldyn->twrite=timer;
416 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
417 moldyn->tfd=open(filename,
418 O_WRONLY|O_CREAT|O_EXCL,
421 perror("[moldyn] temperature log file\n");
424 dprintf(moldyn->tfd,"# temperature log file\n");
425 printf("temperature (%d)\n",timer);
428 moldyn->vwrite=timer;
429 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
430 moldyn->vfd=open(filename,
431 O_WRONLY|O_CREAT|O_EXCL,
434 perror("[moldyn] volume log file\n");
437 dprintf(moldyn->vfd,"# volume log file\n");
438 printf("volume (%d)\n",timer);
441 moldyn->swrite=timer;
442 printf("save file (%d)\n",timer);
445 moldyn->awrite=timer;
446 ret=visual_init(moldyn,moldyn->vlsdir);
448 printf("[moldyn] visual init failure\n");
451 printf("visual file (%d)\n",timer);
454 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
455 moldyn->rfd=open(filename,
456 O_WRONLY|O_CREAT|O_EXCL,
459 perror("[moldyn] report fd open");
462 printf("report -> ");
464 snprintf(filename,127,"%s/e_plot.scr",
466 moldyn->epfd=open(filename,
467 O_WRONLY|O_CREAT|O_EXCL,
470 perror("[moldyn] energy plot fd open");
473 dprintf(moldyn->epfd,e_plot_script);
478 snprintf(filename,127,"%s/pressure_plot.scr",
480 moldyn->ppfd=open(filename,
481 O_WRONLY|O_CREAT|O_EXCL,
484 perror("[moldyn] p plot fd open");
487 dprintf(moldyn->ppfd,pressure_plot_script);
492 snprintf(filename,127,"%s/temperature_plot.scr",
494 moldyn->tpfd=open(filename,
495 O_WRONLY|O_CREAT|O_EXCL,
498 perror("[moldyn] t plot fd open");
501 dprintf(moldyn->tpfd,temperature_plot_script);
503 printf("temperature ");
505 dprintf(moldyn->rfd,report_start,
506 moldyn->rauthor,moldyn->rtitle);
510 printf("unknown log type: %02x\n",type);
517 int moldyn_log_shutdown(t_moldyn *moldyn) {
521 printf("[moldyn] log shutdown\n");
525 dprintf(moldyn->rfd,report_energy);
526 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
531 if(moldyn->mfd) close(moldyn->mfd);
535 dprintf(moldyn->rfd,report_pressure);
536 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
543 dprintf(moldyn->rfd,report_temperature);
544 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
549 dprintf(moldyn->rfd,report_end);
551 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
554 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
557 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
566 * creating lattice functions
569 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
570 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
582 /* how many atoms do we expect */
583 if(type==CUBIC) new*=1;
584 if(type==FCC) new*=4;
585 if(type==DIAMOND) new*=8;
587 /* allocate space for atoms */
588 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
590 perror("[moldyn] realloc (create lattice)");
594 atom=&(moldyn->atom[count]);
596 /* no atoms on the boundaries (only reason: it looks better!) */
610 set_nn_dist(moldyn,lc);
611 ret=cubic_init(a,b,c,lc,atom,&orig);
612 strcpy(name,"cubic");
616 v3_scale(&orig,&orig,0.5);
617 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
618 ret=fcc_init(a,b,c,lc,atom,&orig);
623 v3_scale(&orig,&orig,0.25);
624 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
625 ret=diamond_init(a,b,c,lc,atom,&orig);
626 strcpy(name,"diamond");
629 printf("unknown lattice type (%02x)\n",type);
635 printf("[moldyn] creating lattice failed\n");
636 printf(" amount of atoms\n");
637 printf(" - expected: %d\n",new);
638 printf(" - created: %d\n",ret);
643 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
645 for(ret=0;ret<new;ret++) {
646 atom[ret].element=element;
649 atom[ret].brand=brand;
650 atom[ret].tag=count+ret;
651 check_per_bound(moldyn,&(atom[ret].r));
652 atom[ret].r_0=atom[ret].r;
655 /* update total system mass */
656 total_mass_calc(moldyn);
661 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
662 t_3dvec *r,t_3dvec *v) {
669 count=(moldyn->count)++; // asshole style!
671 ptr=realloc(atom,(count+1)*sizeof(t_atom));
673 perror("[moldyn] realloc (add atom)");
680 /* initialize new atom */
681 memset(&(atom[count]),0,sizeof(t_atom));
684 atom[count].element=element;
685 atom[count].mass=mass;
686 atom[count].brand=brand;
687 atom[count].tag=count;
688 atom[count].attr=attr;
689 check_per_bound(moldyn,&(atom[count].r));
690 atom[count].r_0=atom[count].r;
692 /* update total system mass */
693 total_mass_calc(moldyn);
698 int del_atom(t_moldyn *moldyn,int tag) {
705 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
707 perror("[moldyn]malloc (del atom)");
711 for(cnt=0;cnt<tag;cnt++)
714 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
716 new[cnt-1].tag=cnt-1;
728 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
747 v3_copy(&(atom[count].r),&r);
756 for(i=0;i<count;i++) {
757 atom[i].r.x-=(a*lc)/2.0;
758 atom[i].r.y-=(b*lc)/2.0;
759 atom[i].r.z-=(c*lc)/2.0;
765 /* fcc lattice init */
766 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
779 /* construct the basis */
780 memset(basis,0,3*sizeof(t_3dvec));
788 /* fill up the room */
796 v3_copy(&(atom[count].r),&r);
799 /* the three face centered atoms */
801 v3_add(&n,&r,&basis[l]);
802 v3_copy(&(atom[count].r),&n);
811 /* coordinate transformation */
812 for(i=0;i<count;i++) {
813 atom[i].r.x-=(a*lc)/2.0;
814 atom[i].r.y-=(b*lc)/2.0;
815 atom[i].r.z-=(c*lc)/2.0;
821 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
826 count=fcc_init(a,b,c,lc,atom,origin);
832 if(origin) v3_add(&o,&o,origin);
834 count+=fcc_init(a,b,c,lc,&atom[count],&o);
839 int destroy_atoms(t_moldyn *moldyn) {
841 if(moldyn->atom) free(moldyn->atom);
846 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
849 * - gaussian distribution of velocities
850 * - zero total momentum
851 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
856 t_3dvec p_total,delta;
861 random=&(moldyn->random);
863 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
865 /* gaussian distribution of velocities */
867 for(i=0;i<moldyn->count;i++) {
868 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
870 v=sigma*rand_get_gauss(random);
872 p_total.x+=atom[i].mass*v;
874 v=sigma*rand_get_gauss(random);
876 p_total.y+=atom[i].mass*v;
878 v=sigma*rand_get_gauss(random);
880 p_total.z+=atom[i].mass*v;
883 /* zero total momentum */
884 v3_scale(&p_total,&p_total,1.0/moldyn->count);
885 for(i=0;i<moldyn->count;i++) {
886 v3_scale(&delta,&p_total,1.0/atom[i].mass);
887 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
890 /* velocity scaling */
891 scale_velocity(moldyn,equi_init);
896 double total_mass_calc(t_moldyn *moldyn) {
902 for(i=0;i<moldyn->count;i++)
903 moldyn->mass+=moldyn->atom[i].mass;
908 double temperature_calc(t_moldyn *moldyn) {
910 /* assume up to date kinetic energy, which is 3/2 N k_B T */
912 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
917 double get_temperature(t_moldyn *moldyn) {
922 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
932 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
935 /* get kinetic energy / temperature & count involved atoms */
938 for(i=0;i<moldyn->count;i++) {
939 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
940 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
945 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
946 else return 0; /* no atoms involved in scaling! */
948 /* (temporary) hack for e,t = 0 */
951 if(moldyn->t_ref!=0.0) {
952 thermal_init(moldyn,equi_init);
956 return 0; /* no scaling needed */
960 /* get scaling factor */
961 scale=moldyn->t_ref/moldyn->t;
965 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
966 scale=1.0+(scale-1.0)/moldyn->t_tc;
969 /* velocity scaling */
970 for(i=0;i<moldyn->count;i++) {
971 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
972 v3_scale(&(atom[i].v),&(atom[i].v),scale);
978 double ideal_gas_law_pressure(t_moldyn *moldyn) {
982 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
987 double virial_sum(t_moldyn *moldyn) {
992 /* virial (sum over atom virials) */
1000 for(i=0;i<moldyn->count;i++) {
1001 virial=&(moldyn->atom[i].virial);
1002 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
1003 moldyn->vir.xx+=virial->xx;
1004 moldyn->vir.yy+=virial->yy;
1005 moldyn->vir.zz+=virial->zz;
1006 moldyn->vir.xy+=virial->xy;
1007 moldyn->vir.xz+=virial->xz;
1008 moldyn->vir.yz+=virial->yz;
1011 /* global virial (absolute coordinates) */
1012 virial=&(moldyn->gvir);
1013 moldyn->gv=virial->xx+virial->yy+virial->zz;
1015 return moldyn->virial;
1018 double pressure_calc(t_moldyn *moldyn) {
1022 * with W = 1/3 sum_i f_i r_i (- skipped!)
1023 * virial = sum_i f_i r_i
1025 * => P = (2 Ekin + virial) / (3V)
1028 /* assume up to date virial & up to date kinetic energy */
1030 /* pressure (atom virials) */
1031 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
1032 moldyn->p/=(3.0*moldyn->volume);
1034 /* pressure (absolute coordinates) */
1035 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
1036 moldyn->gp/=(3.0*moldyn->volume);
1041 int average_reset(t_moldyn *moldyn) {
1043 printf("[moldyn] average reset\n");
1045 /* update skip value */
1046 moldyn->avg_skip=moldyn->total_steps;
1048 /* kinetic energy */
1052 /* potential energy */
1060 moldyn->virial_sum=0.0;
1071 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1075 if(moldyn->total_steps<moldyn->avg_skip)
1078 denom=moldyn->total_steps+1-moldyn->avg_skip;
1080 /* assume up to date energies, temperature, pressure etc */
1082 /* kinetic energy */
1083 moldyn->k_sum+=moldyn->ekin;
1084 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1085 moldyn->k_avg=moldyn->k_sum/denom;
1086 moldyn->k2_avg=moldyn->k2_sum/denom;
1087 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1089 /* potential energy */
1090 moldyn->v_sum+=moldyn->energy;
1091 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1092 moldyn->v_avg=moldyn->v_sum/denom;
1093 moldyn->v2_avg=moldyn->v2_sum/denom;
1094 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1097 moldyn->t_sum+=moldyn->t;
1098 moldyn->t_avg=moldyn->t_sum/denom;
1101 moldyn->virial_sum+=moldyn->virial;
1102 moldyn->virial_avg=moldyn->virial_sum/denom;
1103 moldyn->gv_sum+=moldyn->gv;
1104 moldyn->gv_avg=moldyn->gv_sum/denom;
1107 moldyn->p_sum+=moldyn->p;
1108 moldyn->p_avg=moldyn->p_sum/denom;
1109 moldyn->gp_sum+=moldyn->gp;
1110 moldyn->gp_avg=moldyn->gp_sum/denom;
1111 moldyn->tp_sum+=moldyn->tp;
1112 moldyn->tp_avg=moldyn->tp_sum/denom;
1117 int get_heat_capacity(t_moldyn *moldyn) {
1121 /* averages needed for heat capacity calc */
1122 if(moldyn->total_steps<moldyn->avg_skip)
1125 /* (temperature average)^2 */
1126 temp2=moldyn->t_avg*moldyn->t_avg;
1127 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1130 /* ideal gas contribution */
1131 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1132 printf(" ideal gas contribution: %f\n",
1133 ighc/moldyn->mass*KILOGRAM/JOULE);
1135 /* specific heat for nvt ensemble */
1136 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1137 moldyn->c_v_nvt/=moldyn->mass;
1139 /* specific heat for nve ensemble */
1140 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1141 moldyn->c_v_nve/=moldyn->mass;
1143 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1144 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1145 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)));
1150 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1166 /* store atomic configuration + dimension */
1167 store=malloc(moldyn->count*sizeof(t_atom));
1169 printf("[moldyn] allocating store mem failed\n");
1172 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1177 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1178 su=pow(2.0-h,ONE_THIRD)-1.0;
1179 dv=(1.0-h)*moldyn->volume;
1181 /* scale up dimension and atom positions */
1182 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1183 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1184 link_cell_shutdown(moldyn);
1185 link_cell_init(moldyn,QUIET);
1186 potential_force_calc(moldyn);
1189 /* restore atomic configuration + dim */
1190 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1193 /* scale down dimension and atom positions */
1194 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1195 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1196 link_cell_shutdown(moldyn);
1197 link_cell_init(moldyn,QUIET);
1198 potential_force_calc(moldyn);
1201 /* calculate pressure */
1202 moldyn->tp=-(y1-y0)/(2.0*dv);
1204 /* restore atomic configuration */
1205 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1207 link_cell_shutdown(moldyn);
1208 link_cell_init(moldyn,QUIET);
1209 //potential_force_calc(moldyn);
1211 /* free store buffer */
1218 double get_pressure(t_moldyn *moldyn) {
1224 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1236 if(x) dim->x*=scale;
1237 if(y) dim->y*=scale;
1238 if(z) dim->z*=scale;
1243 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1254 for(i=0;i<moldyn->count;i++) {
1255 r=&(moldyn->atom[i].r);
1264 int scale_volume(t_moldyn *moldyn) {
1270 vdim=&(moldyn->vis.dim);
1274 /* scaling factor */
1275 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1276 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1277 scale=pow(scale,ONE_THIRD);
1280 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1283 /* scale the atoms and dimensions */
1284 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1285 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1287 /* visualize dimensions */
1294 /* recalculate scaled volume */
1295 moldyn->volume=dim->x*dim->y*dim->z;
1297 /* adjust/reinit linkcell */
1298 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1299 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1300 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1301 link_cell_shutdown(moldyn);
1302 link_cell_init(moldyn,QUIET);
1313 double e_kin_calc(t_moldyn *moldyn) {
1321 for(i=0;i<moldyn->count;i++) {
1322 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1323 moldyn->ekin+=atom[i].ekin;
1326 return moldyn->ekin;
1329 double get_total_energy(t_moldyn *moldyn) {
1331 return(moldyn->ekin+moldyn->energy);
1334 t_3dvec get_total_p(t_moldyn *moldyn) {
1343 for(i=0;i<moldyn->count;i++) {
1344 v3_scale(&p,&(atom[i].v),atom[i].mass);
1345 v3_add(&p_total,&p_total,&p);
1351 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1355 /* nn_dist is the nearest neighbour distance */
1357 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1366 /* linked list / cell method */
1368 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1375 /* partitioning the md cell */
1376 lc->nx=moldyn->dim.x/moldyn->cutoff;
1377 lc->x=moldyn->dim.x/lc->nx;
1378 lc->ny=moldyn->dim.y/moldyn->cutoff;
1379 lc->y=moldyn->dim.y/lc->ny;
1380 lc->nz=moldyn->dim.z/moldyn->cutoff;
1381 lc->z=moldyn->dim.z/lc->nz;
1382 lc->cells=lc->nx*lc->ny*lc->nz;
1385 lc->subcell=malloc(lc->cells*sizeof(int*));
1387 lc->subcell=malloc(lc->cells*sizeof(t_list));
1390 if(lc->subcell==NULL) {
1391 perror("[moldyn] cell init (malloc)");
1396 printf("[moldyn] FATAL: less then 27 subcells!\n");
1400 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1403 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1406 printf(" x: %d x %f A\n",lc->nx,lc->x);
1407 printf(" y: %d x %f A\n",lc->ny,lc->y);
1408 printf(" z: %d x %f A\n",lc->nz,lc->z);
1413 for(i=0;i<lc->cells;i++) {
1414 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1415 if(lc->subcell[i]==NULL) {
1416 perror("[moldyn] list init (malloc)");
1421 printf(" ---> %d malloc %p (%p)\n",
1422 i,lc->subcell[0],lc->subcell);
1426 for(i=0;i<lc->cells;i++)
1427 list_init_f(&(lc->subcell[i]));
1430 /* update the list */
1431 link_cell_update(moldyn);
1436 int link_cell_update(t_moldyn *moldyn) {
1452 for(i=0;i<lc->cells;i++)
1454 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1456 list_destroy_f(&(lc->subcell[i]));
1459 for(count=0;count<moldyn->count;count++) {
1460 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1461 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1462 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1466 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1469 if(p>=MAX_ATOMS_PER_LIST) {
1470 printf("[moldyn] FATAL: amount of atoms too high!\n");
1474 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1476 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1480 printf(" ---> %d %d malloc %p (%p)\n",
1481 i,count,lc->subcell[i].current,lc->subcell);
1489 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1513 if(i>=nx||j>=ny||k>=nz)
1514 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1517 cell[0]=lc->subcell[i+j*nx+k*a];
1518 for(ci=-1;ci<=1;ci++) {
1521 if((x<0)||(x>=nx)) {
1525 for(cj=-1;cj<=1;cj++) {
1528 if((y<0)||(y>=ny)) {
1532 for(ck=-1;ck<=1;ck++) {
1535 if((z<0)||(z>=nz)) {
1539 if(!(ci|cj|ck)) continue;
1541 cell[--count2]=lc->subcell[x+y*nx+z*a];
1544 cell[count1++]=lc->subcell[x+y*nx+z*a];
1555 int link_cell_shutdown(t_moldyn *moldyn) {
1562 for(i=0;i<lc->cells;i++) {
1564 free(lc->subcell[i]);
1566 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1567 list_destroy_f(&(lc->subcell[i]));
1576 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1580 t_moldyn_schedule *schedule;
1582 schedule=&(moldyn->schedule);
1583 count=++(schedule->total_sched);
1585 ptr=realloc(schedule->runs,count*sizeof(int));
1587 perror("[moldyn] realloc (runs)");
1591 schedule->runs[count-1]=runs;
1593 ptr=realloc(schedule->tau,count*sizeof(double));
1595 perror("[moldyn] realloc (tau)");
1599 schedule->tau[count-1]=tau;
1601 printf("[moldyn] schedule added:\n");
1602 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1608 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1610 moldyn->schedule.hook=hook;
1611 moldyn->schedule.hook_params=hook_params;
1618 * 'integration of newtons equation' - algorithms
1622 /* start the integration */
1624 int moldyn_integrate(t_moldyn *moldyn) {
1627 unsigned int e,m,s,v,p,t,a;
1629 t_moldyn_schedule *sched;
1634 double energy_scale;
1635 struct timeval t1,t2;
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");
1672 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1673 if(ds>0.05*moldyn->nnd)
1674 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1676 /* zero absolute time */
1678 moldyn->total_steps=0;
1680 /* debugging, ignore */
1683 /* tell the world */
1684 printf("[moldyn] integration start, go get a coffee ...\n");
1686 /* executing the schedule */
1688 while(sched->count<sched->total_sched) {
1690 /* setting amount of runs and finite time step size */
1691 moldyn->tau=sched->tau[sched->count];
1692 moldyn->tau_square=moldyn->tau*moldyn->tau;
1693 moldyn->time_steps=sched->runs[sched->count];
1695 /* energy scaling factor (might change!) */
1696 energy_scale=moldyn->count*EV;
1698 /* integration according to schedule */
1700 for(i=0;i<moldyn->time_steps;i++) {
1702 /* integration step */
1703 moldyn->integrate(moldyn);
1705 /* calculate kinetic energy, temperature and pressure */
1707 temperature_calc(moldyn);
1709 pressure_calc(moldyn);
1711 thermodynamic_pressure_calc(moldyn);
1712 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1716 /* calculate fluctuations + averages */
1717 average_and_fluctuation_calc(moldyn);
1720 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1721 scale_velocity(moldyn,FALSE);
1722 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1723 scale_volume(moldyn);
1725 /* check for log & visualization */
1727 if(!(moldyn->total_steps%e))
1728 dprintf(moldyn->efd,
1730 moldyn->time,moldyn->ekin/energy_scale,
1731 moldyn->energy/energy_scale,
1732 get_total_energy(moldyn)/energy_scale);
1735 if(!(moldyn->total_steps%m)) {
1736 momentum=get_total_p(moldyn);
1737 dprintf(moldyn->mfd,
1738 "%f %f %f %f %f\n",moldyn->time,
1739 momentum.x,momentum.y,momentum.z,
1740 v3_norm(&momentum));
1744 if(!(moldyn->total_steps%p)) {
1745 dprintf(moldyn->pfd,
1746 "%f %f %f %f %f %f %f\n",moldyn->time,
1747 moldyn->p/BAR,moldyn->p_avg/BAR,
1748 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1749 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1753 if(!(moldyn->total_steps%t)) {
1754 dprintf(moldyn->tfd,
1756 moldyn->time,moldyn->t,moldyn->t_avg);
1760 if(!(moldyn->total_steps%v)) {
1761 dprintf(moldyn->vfd,
1762 "%f %f\n",moldyn->time,moldyn->volume);
1766 if(!(moldyn->total_steps%s)) {
1767 snprintf(dir,128,"%s/s-%07.f.save",
1768 moldyn->vlsdir,moldyn->time);
1769 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1771 if(fd<0) perror("[moldyn] save fd open");
1773 write(fd,moldyn,sizeof(t_moldyn));
1774 write(fd,moldyn->atom,
1775 moldyn->count*sizeof(t_atom));
1781 if(!(moldyn->total_steps%a)) {
1782 visual_atoms(moldyn);
1786 /* display progress */
1787 //if(!(moldyn->total_steps%10)) {
1788 /* get current time */
1789 gettimeofday(&t2,NULL);
1791 printf("\rsched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)",
1792 sched->count,i,moldyn->total_steps,
1793 moldyn->t,moldyn->t_avg,
1794 moldyn->p/BAR,moldyn->p_avg/BAR,
1796 (int)(t2.tv_sec-t1.tv_sec));
1800 /* copy over time */
1804 /* increase absolute time */
1805 moldyn->time+=moldyn->tau;
1806 moldyn->total_steps+=1;
1810 /* check for hooks */
1812 printf("\n ## schedule hook %d start ##\n",
1814 sched->hook(moldyn,sched->hook_params);
1815 printf(" ## schedule hook end ##\n");
1818 /* increase the schedule counter */
1826 /* velocity verlet */
1828 int velocity_verlet(t_moldyn *moldyn) {
1831 double tau,tau_square,h;
1836 count=moldyn->count;
1838 tau_square=moldyn->tau_square;
1840 for(i=0;i<count;i++) {
1841 /* check whether fixed atom */
1842 if(atom[i].attr&ATOM_ATTR_FP)
1846 v3_scale(&delta,&(atom[i].v),tau);
1847 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1848 v3_scale(&delta,&(atom[i].f),h*tau_square);
1849 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1850 check_per_bound(moldyn,&(atom[i].r));
1852 /* velocities [actually v(t+tau/2)] */
1853 v3_scale(&delta,&(atom[i].f),h*tau);
1854 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1857 /* criticial check */
1858 moldyn_bc_check(moldyn);
1860 /* neighbour list update */
1861 link_cell_update(moldyn);
1863 /* forces depending on chosen potential */
1864 potential_force_calc(moldyn);
1866 for(i=0;i<count;i++) {
1867 /* check whether fixed atom */
1868 if(atom[i].attr&ATOM_ATTR_FP)
1870 /* again velocities [actually v(t+tau)] */
1871 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1872 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1881 * potentials & corresponding forces & virial routine
1885 /* generic potential and force calculation */
1887 int potential_force_calc(t_moldyn *moldyn) {
1890 t_atom *itom,*jtom,*ktom;
1894 int *neighbour_i[27];
1898 t_list neighbour_i[27];
1899 t_list neighbour_i2[27];
1905 count=moldyn->count;
1915 /* reset global virial */
1916 memset(&(moldyn->gvir),0,sizeof(t_virial));
1918 /* reset force, site energy and virial of every atom */
1919 for(i=0;i<count;i++) {
1922 v3_zero(&(itom[i].f));
1925 virial=(&(itom[i].virial));
1933 /* reset site energy */
1938 /* get energy, force and virial of every atom */
1940 /* first (and only) loop over atoms i */
1941 for(i=0;i<count;i++) {
1943 /* single particle potential/force */
1944 if(itom[i].attr&ATOM_ATTR_1BP)
1946 moldyn->func1b(moldyn,&(itom[i]));
1948 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
1951 /* 2 body pair potential/force */
1953 link_cell_neighbour_index(moldyn,
1954 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
1955 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
1956 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
1961 /* first loop over atoms j */
1962 if(moldyn->func2b) {
1969 while(neighbour_i[j][p]!=0) {
1971 jtom=&(atom[neighbour_i[j][p]]);
1974 if(jtom==&(itom[i]))
1977 if((jtom->attr&ATOM_ATTR_2BP)&
1978 (itom[i].attr&ATOM_ATTR_2BP)) {
1979 moldyn->func2b(moldyn,
1986 this=&(neighbour_i[j]);
1989 if(this->start==NULL)
1993 jtom=this->current->data;
1995 if(jtom==&(itom[i]))
1998 if((jtom->attr&ATOM_ATTR_2BP)&
1999 (itom[i].attr&ATOM_ATTR_2BP)) {
2000 moldyn->func2b(moldyn,
2005 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2011 /* 3 body potential/force */
2013 if(!(itom[i].attr&ATOM_ATTR_3BP))
2016 /* copy the neighbour lists */
2018 /* no copy needed for static lists */
2020 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2023 /* second loop over atoms j */
2030 while(neighbour_i[j][p]!=0) {
2032 jtom=&(atom[neighbour_i[j][p]]);
2035 this=&(neighbour_i[j]);
2038 if(this->start==NULL)
2043 jtom=this->current->data;
2046 if(jtom==&(itom[i]))
2049 if(!(jtom->attr&ATOM_ATTR_3BP))
2055 if(moldyn->func3b_j1)
2056 moldyn->func3b_j1(moldyn,
2061 /* in first j loop, 3bp run can be skipped */
2062 if(!(moldyn->run3bp))
2065 /* first loop over atoms k */
2066 if(moldyn->func3b_k1) {
2074 while(neighbour_i[j][q]!=0) {
2076 ktom=&(atom[neighbour_i[k][q]]);
2079 that=&(neighbour_i2[k]);
2082 if(that->start==NULL)
2086 ktom=that->current->data;
2089 if(!(ktom->attr&ATOM_ATTR_3BP))
2095 if(ktom==&(itom[i]))
2098 moldyn->func3b_k1(moldyn,
2106 } while(list_next_f(that)!=\
2114 if(moldyn->func3b_j2)
2115 moldyn->func3b_j2(moldyn,
2120 /* second loop over atoms k */
2121 if(moldyn->func3b_k2) {
2129 while(neighbour_i[j][q]!=0) {
2131 ktom=&(atom[neighbour_i[k][q]]);
2134 that=&(neighbour_i2[k]);
2137 if(that->start==NULL)
2141 ktom=that->current->data;
2144 if(!(ktom->attr&ATOM_ATTR_3BP))
2150 if(ktom==&(itom[i]))
2153 moldyn->func3b_k2(moldyn,
2162 } while(list_next_f(that)!=\
2170 /* 2bp post function */
2171 if(moldyn->func3b_j3) {
2172 moldyn->func3b_j3(moldyn,
2179 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2194 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2195 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2197 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2198 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2199 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2203 /* some postprocessing */
2204 for(i=0;i<count;i++) {
2205 /* calculate global virial */
2206 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2207 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2208 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2209 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2210 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2211 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2213 /* check forces regarding the given timestep */
2214 if(v3_norm(&(itom[i].f))>\
2215 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2216 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2224 * virial calculation
2227 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2228 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2230 a->virial.xx+=f->x*d->x;
2231 a->virial.yy+=f->y*d->y;
2232 a->virial.zz+=f->z*d->z;
2233 a->virial.xy+=f->x*d->y;
2234 a->virial.xz+=f->x*d->z;
2235 a->virial.yz+=f->y*d->z;
2241 * periodic boundary checking
2244 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2245 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2256 if(moldyn->status&MOLDYN_STAT_PBX) {
2257 if(a->x>=x) a->x-=dim->x;
2258 else if(-a->x>x) a->x+=dim->x;
2260 if(moldyn->status&MOLDYN_STAT_PBY) {
2261 if(a->y>=y) a->y-=dim->y;
2262 else if(-a->y>y) a->y+=dim->y;
2264 if(moldyn->status&MOLDYN_STAT_PBZ) {
2265 if(a->z>=z) a->z-=dim->z;
2266 else if(-a->z>z) a->z+=dim->z;
2273 * debugging / critical check functions
2276 int moldyn_bc_check(t_moldyn *moldyn) {
2289 for(i=0;i<moldyn->count;i++) {
2290 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2291 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2292 i,atom[i].r.x,dim->x/2);
2293 printf("diagnostic:\n");
2294 printf("-----------\natom.r.x:\n");
2296 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2299 ((byte)&(1<<k))?1:0,
2302 printf("---------------\nx=dim.x/2:\n");
2304 memcpy(&byte,(u8 *)(&x)+j,1);
2307 ((byte)&(1<<k))?1:0,
2310 if(atom[i].r.x==x) printf("the same!\n");
2311 else printf("different!\n");
2313 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2314 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2315 i,atom[i].r.y,dim->y/2);
2316 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2317 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2318 i,atom[i].r.z,dim->z/2);
2328 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2335 fd=open(file,O_RDONLY);
2337 perror("[moldyn] load save file open");
2341 fsize=lseek(fd,0,SEEK_END);
2342 lseek(fd,0,SEEK_SET);
2344 size=sizeof(t_moldyn);
2347 cnt=read(fd,moldyn,size);
2349 perror("[moldyn] load save file read (moldyn)");
2355 size=moldyn->count*sizeof(t_atom);
2357 /* correcting possible atom data offset */
2359 if(fsize!=sizeof(t_moldyn)+size) {
2360 corr=fsize-sizeof(t_moldyn)-size;
2361 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2362 printf(" moifying offset:\n");
2363 printf(" - current pos: %d\n",sizeof(t_moldyn));
2364 printf(" - atom size: %d\n",size);
2365 printf(" - file size: %d\n",fsize);
2366 printf(" => correction: %d\n",corr);
2367 lseek(fd,corr,SEEK_CUR);
2370 moldyn->atom=(t_atom *)malloc(size);
2371 if(moldyn->atom==NULL) {
2372 perror("[moldyn] load save file malloc (atoms)");
2377 cnt=read(fd,moldyn->atom,size);
2379 perror("[moldyn] load save file read (atoms)");
2390 int moldyn_free_save_file(t_moldyn *moldyn) {
2397 int moldyn_load(t_moldyn *moldyn) {
2405 * function to find/callback all combinations of 2 body bonds
2408 int process_2b_bonds(t_moldyn *moldyn,void *data,
2409 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2410 void *data,u8 bc)) {
2417 t_list neighbour[27];
2427 for(i=0;i<moldyn->count;i++) {
2428 /* neighbour indexing */
2429 link_cell_neighbour_index(moldyn,
2430 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2431 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2432 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2437 bc=(j<lc->dnlc)?0:1;
2442 while(neighbour[j][p]!=0) {
2444 jtom=&(moldyn->atom[neighbour[j][p]]);
2447 this=&(neighbour[j]);
2450 if(this->start==NULL)
2455 jtom=this->current->data;
2459 process(moldyn,&(itom[i]),jtom,data,bc);
2464 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2474 * post processing functions
2477 int get_line(int fd,char *line,int max) {
2484 if(count==max) return count;
2485 ret=read(fd,line+count,1);
2486 if(ret<=0) return ret;
2487 if(line[count]=='\n') {
2488 memset(line+count,0,max-count-1);
2496 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2502 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2518 for(i=0;i<moldyn->count;i++) {
2520 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2521 check_per_bound(moldyn,&dist);
2522 d2=v3_absolute_square(&dist);
2536 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2537 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2538 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2543 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2548 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2549 t_atom *jtom,void *data,u8 bc) {
2556 /* only count pairs once,
2557 * skip same atoms */
2558 if(itom->tag>=jtom->tag)
2562 * pair correlation calc
2569 v3_sub(&dist,&(jtom->r),&(itom->r));
2570 if(bc) check_per_bound(moldyn,&dist);
2571 d=v3_absolute_square(&dist);
2573 /* ignore if greater cutoff */
2574 if(d>moldyn->cutoff_square)
2577 /* fill the slots */
2581 /* should never happen but it does 8) -
2582 * related to -ffloat-store problem! */
2584 printf("[moldyn] WARNING: pcc (%d/%d)",
2590 if(itom->brand!=jtom->brand) {
2595 /* type a - type a bonds */
2597 pcc->stat[s+pcc->o1]+=1;
2599 /* type b - type b bonds */
2600 pcc->stat[s+pcc->o2]+=1;
2606 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2613 pcc.o1=moldyn->cutoff/dr;
2616 if(pcc.o1*dr<=moldyn->cutoff)
2617 printf("[moldyn] WARNING: pcc (low #slots)\n");
2619 printf("[moldyn] pair correlation calc info:\n");
2620 printf(" time: %f\n",moldyn->time);
2621 printf(" count: %d\n",moldyn->count);
2622 printf(" cutoff: %f\n",moldyn->cutoff);
2623 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2626 pcc.stat=(double *)ptr;
2629 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2630 if(pcc.stat==NULL) {
2631 perror("[moldyn] pair correlation malloc");
2636 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2639 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2642 for(i=1;i<pcc.o1;i++) {
2643 // normalization: 4 pi r^2 dr
2644 // here: not double counting pairs -> 2 pi r r dr
2645 // ... and actually it's a constant times r^2
2648 pcc.stat[pcc.o1+i]/=norm;
2649 pcc.stat[pcc.o2+i]/=norm;
2654 /* todo: store/print pair correlation function */
2661 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2668 if(itom->tag>=jtom->tag)
2672 v3_sub(&dist,&(jtom->r),&(itom->r));
2673 if(bc) check_per_bound(moldyn,&dist);
2674 d=v3_absolute_square(&dist);
2676 /* ignore if greater or equal cutoff */
2677 if(d>moldyn->cutoff_square)
2680 /* check for potential bond */
2681 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2686 /* now count this bonding ... */
2689 /* increase total bond counter
2690 * ... double counting!
2695 ba->acnt[jtom->tag]+=1;
2697 ba->bcnt[jtom->tag]+=1;
2700 ba->acnt[itom->tag]+=1;
2702 ba->bcnt[itom->tag]+=1;
2707 int bond_analyze(t_moldyn *moldyn,double *quality) {
2709 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2717 ba.acnt=malloc(moldyn->count*sizeof(int));
2719 perror("[moldyn] bond analyze malloc (a)");
2722 memset(ba.acnt,0,moldyn->count*sizeof(int));
2724 ba.bcnt=malloc(moldyn->count*sizeof(int));
2726 perror("[moldyn] bond analyze malloc (b)");
2729 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2738 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2740 for(i=0;i<moldyn->count;i++) {
2741 if(atom[i].brand==0) {
2742 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2746 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2754 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2755 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2758 quality[0]=1.0*ccnt/cset;
2759 quality[1]=1.0*qcnt/ba.tcnt;
2762 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2763 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2770 * visualization code
2773 int visual_init(t_moldyn *moldyn,char *filebase) {
2775 strncpy(moldyn->vis.fb,filebase,128);
2780 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2787 if(itom->tag>=jtom->tag)
2790 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2793 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2794 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2795 itom->r.x,itom->r.y,itom->r.z,
2796 jtom->r.x,jtom->r.y,jtom->r.z);
2801 int visual_atoms(t_moldyn *moldyn) {
2819 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2820 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2822 perror("open visual save file fd");
2826 /* write the actual data file */
2829 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2830 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2832 // atomic configuration
2833 for(i=0;i<moldyn->count;i++)
2834 // atom type, positions, color and kinetic energy
2835 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2839 pse_col[atom[i].element],
2842 // bonds between atoms
2843 process_2b_bonds(moldyn,&vb,visual_bonds_process);
2847 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2848 -dim.x/2,-dim.y/2,-dim.z/2,
2849 dim.x/2,-dim.y/2,-dim.z/2);
2850 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2851 -dim.x/2,-dim.y/2,-dim.z/2,
2852 -dim.x/2,dim.y/2,-dim.z/2);
2853 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2854 dim.x/2,dim.y/2,-dim.z/2,
2855 dim.x/2,-dim.y/2,-dim.z/2);
2856 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2857 -dim.x/2,dim.y/2,-dim.z/2,
2858 dim.x/2,dim.y/2,-dim.z/2);
2860 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2861 -dim.x/2,-dim.y/2,dim.z/2,
2862 dim.x/2,-dim.y/2,dim.z/2);
2863 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2864 -dim.x/2,-dim.y/2,dim.z/2,
2865 -dim.x/2,dim.y/2,dim.z/2);
2866 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2867 dim.x/2,dim.y/2,dim.z/2,
2868 dim.x/2,-dim.y/2,dim.z/2);
2869 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2870 -dim.x/2,dim.y/2,dim.z/2,
2871 dim.x/2,dim.y/2,dim.z/2);
2873 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2874 -dim.x/2,-dim.y/2,dim.z/2,
2875 -dim.x/2,-dim.y/2,-dim.z/2);
2876 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2877 -dim.x/2,dim.y/2,dim.z/2,
2878 -dim.x/2,dim.y/2,-dim.z/2);
2879 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2880 dim.x/2,-dim.y/2,dim.z/2,
2881 dim.x/2,-dim.y/2,-dim.z/2);
2882 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2883 dim.x/2,dim.y/2,dim.z/2,
2884 dim.x/2,dim.y/2,-dim.z/2);