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[]={
82 static double *pse_mass[]={
104 static double *pse_lc[]={
127 * the moldyn functions
130 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
132 printf("[moldyn] init\n");
134 memset(moldyn,0,sizeof(t_moldyn));
139 rand_init(&(moldyn->random),NULL,1);
140 moldyn->random.status|=RAND_STAT_VERBOSE;
145 int moldyn_shutdown(t_moldyn *moldyn) {
147 printf("[moldyn] shutdown\n");
149 moldyn_log_shutdown(moldyn);
150 link_cell_shutdown(moldyn);
151 rand_close(&(moldyn->random));
157 int set_int_alg(t_moldyn *moldyn,u8 algo) {
159 printf("[moldyn] integration algorithm: ");
162 case MOLDYN_INTEGRATE_VERLET:
163 moldyn->integrate=velocity_verlet;
164 printf("velocity verlet\n");
167 printf("unknown integration algorithm: %02x\n",algo);
175 int set_cutoff(t_moldyn *moldyn,double cutoff) {
177 moldyn->cutoff=cutoff;
178 moldyn->cutoff_square=cutoff*cutoff;
180 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
185 int set_temperature(t_moldyn *moldyn,double t_ref) {
189 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
194 int set_pressure(t_moldyn *moldyn,double p_ref) {
198 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
203 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
205 moldyn->pt_scalei&=(^(P_SCALE_MASK));
206 moldyn->pt_scale|=ptype;
209 printf("[moldyn] p/t scaling:\n");
211 printf(" p: %s",ptype?"yes":"no ");
213 printf(" | type: %02x | factor: %f",ptype,ptc);
216 printf(" t: %s",ttype?"yes":"no ");
218 printf(" | type: %02x | factor: %f",ttype,ttc);
224 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
226 moldyn->pt_scalei&=(^(T_SCALE_MASK));
227 moldyn->pt_scale|=ttype;
230 printf("[moldyn] p/t scaling:\n");
232 printf(" p: %s",ptype?"yes":"no ");
234 printf(" | type: %02x | factor: %f",ptype,ptc);
237 printf(" t: %s",ttype?"yes":"no ");
239 printf(" | type: %02x | factor: %f",ttype,ttc);
245 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
247 moldyn->pt_scale=(ptype|ttype);
251 printf("[moldyn] p/t scaling:\n");
253 printf(" p: %s",ptype?"yes":"no ");
255 printf(" | type: %02x | factor: %f",ptype,ptc);
258 printf(" t: %s",ttype?"yes":"no ");
260 printf(" | type: %02x | factor: %f",ttype,ttc);
266 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
272 moldyn->volume=x*y*z;
280 printf("[moldyn] dimensions in A and A^3 respectively:\n");
281 printf(" x: %f\n",moldyn->dim.x);
282 printf(" y: %f\n",moldyn->dim.y);
283 printf(" z: %f\n",moldyn->dim.z);
284 printf(" volume: %f\n",moldyn->volume);
285 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
290 int set_nn_dist(t_moldyn *moldyn,double dist) {
297 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
299 printf("[moldyn] periodic boundary conditions:\n");
302 moldyn->status|=MOLDYN_STAT_PBX;
305 moldyn->status|=MOLDYN_STAT_PBY;
308 moldyn->status|=MOLDYN_STAT_PBZ;
310 printf(" x: %s\n",x?"yes":"no");
311 printf(" y: %s\n",y?"yes":"no");
312 printf(" z: %s\n",z?"yes":"no");
317 int set_potential(t_moldyn *moldyn,u8 type) {
320 case MOLDYN_POTENTIAL_TM:
321 moldyn->func1b=tersoff_mult_1bp;
322 moldyn->func3b_j1=tersoff_mult_3bp_j1;
323 moldyn->func3b_k1=tersoff_mult_3bp_k1;
324 moldyn->func3b_j2=tersoff_mult_3bp_j2;
325 moldyn->func3b_k2=tersoff_mult_3bp_k2;
326 // missing: check 2b bond func
328 case MOLDYN_POTENTIAL_AM:
329 moldyn->func3b_j1=albe_mult_3bp_j1;
330 moldyn->func3b_k1=albe_mult_3bp_k1;
331 moldyn->func3b_j2=albe_mult_3bp_j2;
332 moldyn->func3b_k2=albe_mult_3bp_k2;
333 moldyn->check_2b_bond=albe_mult_check_2b_bond;
335 case MOLDYN_POTENTIAL_HO:
336 moldyn->func2b=harmonic_oscillator;
337 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
339 case MOLDYN_POTENTIAL_LJ:
340 moldyn->func2b=lennard_jones;
341 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
344 printf("[moldyn] set potential: unknown type %02x\n",
352 int set_avg_skip(t_moldyn *moldyn,int skip) {
354 printf("[moldyn] skip %d steps before starting average calc\n",skip);
355 moldyn->avg_skip=skip;
360 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
362 strncpy(moldyn->vlsdir,dir,127);
367 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
369 strncpy(moldyn->rauthor,author,63);
370 strncpy(moldyn->rtitle,title,63);
375 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
380 printf("[moldyn] set log: ");
383 case LOG_TOTAL_ENERGY:
384 moldyn->ewrite=timer;
385 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
386 moldyn->efd=open(filename,
387 O_WRONLY|O_CREAT|O_EXCL,
390 perror("[moldyn] energy log fd open");
393 dprintf(moldyn->efd,"# total energy log file\n");
394 printf("total energy (%d)\n",timer);
396 case LOG_TOTAL_MOMENTUM:
397 moldyn->mwrite=timer;
398 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
399 moldyn->mfd=open(filename,
400 O_WRONLY|O_CREAT|O_EXCL,
403 perror("[moldyn] momentum log fd open");
406 dprintf(moldyn->efd,"# total momentum log file\n");
407 printf("total momentum (%d)\n",timer);
410 moldyn->pwrite=timer;
411 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
412 moldyn->pfd=open(filename,
413 O_WRONLY|O_CREAT|O_EXCL,
416 perror("[moldyn] pressure log file\n");
419 dprintf(moldyn->pfd,"# pressure log file\n");
420 printf("pressure (%d)\n",timer);
422 case LOG_TEMPERATURE:
423 moldyn->twrite=timer;
424 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
425 moldyn->tfd=open(filename,
426 O_WRONLY|O_CREAT|O_EXCL,
429 perror("[moldyn] temperature log file\n");
432 dprintf(moldyn->tfd,"# temperature log file\n");
433 printf("temperature (%d)\n",timer);
436 moldyn->vwrite=timer;
437 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
438 moldyn->vfd=open(filename,
439 O_WRONLY|O_CREAT|O_EXCL,
442 perror("[moldyn] volume log file\n");
445 dprintf(moldyn->vfd,"# volume log file\n");
446 printf("volume (%d)\n",timer);
449 moldyn->swrite=timer;
450 printf("save file (%d)\n",timer);
453 moldyn->awrite=timer;
454 ret=visual_init(moldyn,moldyn->vlsdir);
456 printf("[moldyn] visual init failure\n");
459 printf("visual file (%d)\n",timer);
462 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
463 moldyn->rfd=open(filename,
464 O_WRONLY|O_CREAT|O_EXCL,
467 perror("[moldyn] report fd open");
470 printf("report -> ");
472 snprintf(filename,127,"%s/e_plot.scr",
474 moldyn->epfd=open(filename,
475 O_WRONLY|O_CREAT|O_EXCL,
478 perror("[moldyn] energy plot fd open");
481 dprintf(moldyn->epfd,e_plot_script);
486 snprintf(filename,127,"%s/pressure_plot.scr",
488 moldyn->ppfd=open(filename,
489 O_WRONLY|O_CREAT|O_EXCL,
492 perror("[moldyn] p plot fd open");
495 dprintf(moldyn->ppfd,pressure_plot_script);
500 snprintf(filename,127,"%s/temperature_plot.scr",
502 moldyn->tpfd=open(filename,
503 O_WRONLY|O_CREAT|O_EXCL,
506 perror("[moldyn] t plot fd open");
509 dprintf(moldyn->tpfd,temperature_plot_script);
511 printf("temperature ");
513 dprintf(moldyn->rfd,report_start,
514 moldyn->rauthor,moldyn->rtitle);
518 printf("unknown log type: %02x\n",type);
525 int moldyn_log_shutdown(t_moldyn *moldyn) {
529 printf("[moldyn] log shutdown\n");
533 dprintf(moldyn->rfd,report_energy);
534 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
539 if(moldyn->mfd) close(moldyn->mfd);
543 dprintf(moldyn->rfd,report_pressure);
544 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
551 dprintf(moldyn->rfd,report_temperature);
552 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
557 dprintf(moldyn->rfd,report_end);
559 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
562 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
565 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
574 * creating lattice functions
577 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
578 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
589 /* how many atoms do we expect */
590 if(type==CUBIC) new*=1;
591 if(type==FCC) new*=4;
592 if(type==DIAMOND) new*=8;
594 /* allocate space for atoms */
595 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
597 perror("[moldyn] realloc (create lattice)");
601 atom=&(moldyn->atom[count]);
603 /* no atoms on the boundaries (only reason: it looks better!) */
617 set_nn_dist(moldyn,lc);
618 ret=cubic_init(a,b,c,lc,atom,&orig);
622 v3_scale(&orig,&orig,0.5);
623 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
624 ret=fcc_init(a,b,c,lc,atom,&orig);
628 v3_scale(&orig,&orig,0.25);
629 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
630 ret=diamond_init(a,b,c,lc,atom,&orig);
633 printf("unknown lattice type (%02x)\n",type);
639 printf("[moldyn] creating lattice failed\n");
640 printf(" amount of atoms\n");
641 printf(" - expected: %d\n",new);
642 printf(" - created: %d\n",ret);
647 printf("[moldyn] created lattice with %d atoms\n",new);
649 for(ret=0;ret<new;ret++) {
650 atom[ret].element=element;
653 atom[ret].brand=brand;
654 atom[ret].tag=count+ret;
655 check_per_bound(moldyn,&(atom[ret].r));
656 atom[ret].r_0=atom[ret].r;
659 /* update total system mass */
660 total_mass_calc(moldyn);
665 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
666 t_3dvec *r,t_3dvec *v) {
673 count=(moldyn->count)++; // asshole style!
675 ptr=realloc(atom,(count+1)*sizeof(t_atom));
677 perror("[moldyn] realloc (add atom)");
684 /* initialize new atom */
685 memset(&(atom[count]),0,sizeof(t_atom));
688 atom[count].element=element;
689 atom[count].mass=mass;
690 atom[count].brand=brand;
691 atom[count].tag=count;
692 atom[count].attr=attr;
693 check_per_bound(moldyn,&(atom[count].r));
694 atom[count].r_0=atom[count].r;
696 /* update total system mass */
697 total_mass_calc(moldyn);
702 int del_atom(t_moldyn *moldyn,int tag) {
709 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
711 perror("[moldyn]malloc (del atom)");
715 for(cnt=0;cnt<tag;cnt++)
718 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
720 new[cnt-1].tag=cnt-1;
732 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
751 v3_copy(&(atom[count].r),&r);
760 for(i=0;i<count;i++) {
761 atom[i].r.x-=(a*lc)/2.0;
762 atom[i].r.y-=(b*lc)/2.0;
763 atom[i].r.z-=(c*lc)/2.0;
769 /* fcc lattice init */
770 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
783 /* construct the basis */
784 memset(basis,0,3*sizeof(t_3dvec));
792 /* fill up the room */
800 v3_copy(&(atom[count].r),&r);
803 /* the three face centered atoms */
805 v3_add(&n,&r,&basis[l]);
806 v3_copy(&(atom[count].r),&n);
815 /* coordinate transformation */
816 for(i=0;i<count;i++) {
817 atom[i].r.x-=(a*lc)/2.0;
818 atom[i].r.y-=(b*lc)/2.0;
819 atom[i].r.z-=(c*lc)/2.0;
825 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
830 count=fcc_init(a,b,c,lc,atom,origin);
836 if(origin) v3_add(&o,&o,origin);
838 count+=fcc_init(a,b,c,lc,&atom[count],&o);
843 int destroy_atoms(t_moldyn *moldyn) {
845 if(moldyn->atom) free(moldyn->atom);
850 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
853 * - gaussian distribution of velocities
854 * - zero total momentum
855 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
860 t_3dvec p_total,delta;
865 random=&(moldyn->random);
867 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
869 /* gaussian distribution of velocities */
871 for(i=0;i<moldyn->count;i++) {
872 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
874 v=sigma*rand_get_gauss(random);
876 p_total.x+=atom[i].mass*v;
878 v=sigma*rand_get_gauss(random);
880 p_total.y+=atom[i].mass*v;
882 v=sigma*rand_get_gauss(random);
884 p_total.z+=atom[i].mass*v;
887 /* zero total momentum */
888 v3_scale(&p_total,&p_total,1.0/moldyn->count);
889 for(i=0;i<moldyn->count;i++) {
890 v3_scale(&delta,&p_total,1.0/atom[i].mass);
891 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
894 /* velocity scaling */
895 scale_velocity(moldyn,equi_init);
900 double total_mass_calc(t_moldyn *moldyn) {
906 for(i=0;i<moldyn->count;i++)
907 moldyn->mass+=moldyn->atom[i].mass;
912 double temperature_calc(t_moldyn *moldyn) {
914 /* assume up to date kinetic energy, which is 3/2 N k_B T */
916 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
921 double get_temperature(t_moldyn *moldyn) {
926 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
936 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
939 /* get kinetic energy / temperature & count involved atoms */
942 for(i=0;i<moldyn->count;i++) {
943 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
944 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
949 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
950 else return 0; /* no atoms involved in scaling! */
952 /* (temporary) hack for e,t = 0 */
955 if(moldyn->t_ref!=0.0) {
956 thermal_init(moldyn,equi_init);
960 return 0; /* no scaling needed */
964 /* get scaling factor */
965 scale=moldyn->t_ref/moldyn->t;
969 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
970 scale=1.0+(scale-1.0)/moldyn->t_tc;
973 /* velocity scaling */
974 for(i=0;i<moldyn->count;i++) {
975 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
976 v3_scale(&(atom[i].v),&(atom[i].v),scale);
982 double ideal_gas_law_pressure(t_moldyn *moldyn) {
986 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
991 double virial_sum(t_moldyn *moldyn) {
996 /* virial (sum over atom virials) */
1004 for(i=0;i<moldyn->count;i++) {
1005 virial=&(moldyn->atom[i].virial);
1006 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
1007 moldyn->vir.xx+=virial->xx;
1008 moldyn->vir.yy+=virial->yy;
1009 moldyn->vir.zz+=virial->zz;
1010 moldyn->vir.xy+=virial->xy;
1011 moldyn->vir.xz+=virial->xz;
1012 moldyn->vir.yz+=virial->yz;
1015 /* global virial (absolute coordinates) */
1016 virial=&(moldyn->gvir);
1017 moldyn->gv=virial->xx+virial->yy+virial->zz;
1019 return moldyn->virial;
1022 double pressure_calc(t_moldyn *moldyn) {
1026 * with W = 1/3 sum_i f_i r_i (- skipped!)
1027 * virial = sum_i f_i r_i
1029 * => P = (2 Ekin + virial) / (3V)
1032 /* assume up to date virial & up to date kinetic energy */
1034 /* pressure (atom virials) */
1035 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
1036 moldyn->p/=(3.0*moldyn->volume);
1038 /* pressure (absolute coordinates) */
1039 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
1040 moldyn->gp/=(3.0*moldyn->volume);
1045 int average_reset(t_moldyn *moldyn) {
1047 printf("[moldyn] average reset\n");
1049 /* update skip value */
1050 moldyn->avg_skip=moldyn->total_steps;
1052 /* kinetic energy */
1056 /* potential energy */
1064 moldyn->virial_sum=0.0;
1075 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1079 if(moldyn->total_steps<moldyn->avg_skip)
1082 denom=moldyn->total_steps+1-moldyn->avg_skip;
1084 /* assume up to date energies, temperature, pressure etc */
1086 /* kinetic energy */
1087 moldyn->k_sum+=moldyn->ekin;
1088 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1089 moldyn->k_avg=moldyn->k_sum/denom;
1090 moldyn->k2_avg=moldyn->k2_sum/denom;
1091 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1093 /* potential energy */
1094 moldyn->v_sum+=moldyn->energy;
1095 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1096 moldyn->v_avg=moldyn->v_sum/denom;
1097 moldyn->v2_avg=moldyn->v2_sum/denom;
1098 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1101 moldyn->t_sum+=moldyn->t;
1102 moldyn->t_avg=moldyn->t_sum/denom;
1105 moldyn->virial_sum+=moldyn->virial;
1106 moldyn->virial_avg=moldyn->virial_sum/denom;
1107 moldyn->gv_sum+=moldyn->gv;
1108 moldyn->gv_avg=moldyn->gv_sum/denom;
1111 moldyn->p_sum+=moldyn->p;
1112 moldyn->p_avg=moldyn->p_sum/denom;
1113 moldyn->gp_sum+=moldyn->gp;
1114 moldyn->gp_avg=moldyn->gp_sum/denom;
1115 moldyn->tp_sum+=moldyn->tp;
1116 moldyn->tp_avg=moldyn->tp_sum/denom;
1121 int get_heat_capacity(t_moldyn *moldyn) {
1125 /* averages needed for heat capacity calc */
1126 if(moldyn->total_steps<moldyn->avg_skip)
1129 /* (temperature average)^2 */
1130 temp2=moldyn->t_avg*moldyn->t_avg;
1131 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1134 /* ideal gas contribution */
1135 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1136 printf(" ideal gas contribution: %f\n",
1137 ighc/moldyn->mass*KILOGRAM/JOULE);
1139 /* specific heat for nvt ensemble */
1140 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1141 moldyn->c_v_nvt/=moldyn->mass;
1143 /* specific heat for nve ensemble */
1144 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1145 moldyn->c_v_nve/=moldyn->mass;
1147 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1148 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1149 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)));
1154 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1170 /* store atomic configuration + dimension */
1171 store=malloc(moldyn->count*sizeof(t_atom));
1173 printf("[moldyn] allocating store mem failed\n");
1176 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1181 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1182 su=pow(2.0-h,ONE_THIRD)-1.0;
1183 dv=(1.0-h)*moldyn->volume;
1185 /* scale up dimension and atom positions */
1186 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1187 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1188 link_cell_shutdown(moldyn);
1189 link_cell_init(moldyn,QUIET);
1190 potential_force_calc(moldyn);
1193 /* restore atomic configuration + dim */
1194 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1197 /* scale down dimension and atom positions */
1198 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1199 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1200 link_cell_shutdown(moldyn);
1201 link_cell_init(moldyn,QUIET);
1202 potential_force_calc(moldyn);
1205 /* calculate pressure */
1206 moldyn->tp=-(y1-y0)/(2.0*dv);
1208 /* restore atomic configuration */
1209 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1211 link_cell_shutdown(moldyn);
1212 link_cell_init(moldyn,QUIET);
1213 //potential_force_calc(moldyn);
1215 /* free store buffer */
1222 double get_pressure(t_moldyn *moldyn) {
1228 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1240 if(x) dim->x*=scale;
1241 if(y) dim->y*=scale;
1242 if(z) dim->z*=scale;
1247 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1258 for(i=0;i<moldyn->count;i++) {
1259 r=&(moldyn->atom[i].r);
1268 int scale_volume(t_moldyn *moldyn) {
1274 vdim=&(moldyn->vis.dim);
1278 /* scaling factor */
1279 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1280 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc;
1281 scale=pow(scale,ONE_THIRD);
1284 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1287 /* scale the atoms and dimensions */
1288 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1289 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1291 /* visualize dimensions */
1298 /* recalculate scaled volume */
1299 moldyn->volume=dim->x*dim->y*dim->z;
1301 /* adjust/reinit linkcell */
1302 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1303 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1304 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1305 link_cell_shutdown(moldyn);
1306 link_cell_init(moldyn,QUIET);
1317 double e_kin_calc(t_moldyn *moldyn) {
1325 for(i=0;i<moldyn->count;i++) {
1326 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1327 moldyn->ekin+=atom[i].ekin;
1330 return moldyn->ekin;
1333 double get_total_energy(t_moldyn *moldyn) {
1335 return(moldyn->ekin+moldyn->energy);
1338 t_3dvec get_total_p(t_moldyn *moldyn) {
1347 for(i=0;i<moldyn->count;i++) {
1348 v3_scale(&p,&(atom[i].v),atom[i].mass);
1349 v3_add(&p_total,&p_total,&p);
1355 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1359 /* nn_dist is the nearest neighbour distance */
1361 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1370 /* linked list / cell method */
1372 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1379 /* partitioning the md cell */
1380 lc->nx=moldyn->dim.x/moldyn->cutoff;
1381 lc->x=moldyn->dim.x/lc->nx;
1382 lc->ny=moldyn->dim.y/moldyn->cutoff;
1383 lc->y=moldyn->dim.y/lc->ny;
1384 lc->nz=moldyn->dim.z/moldyn->cutoff;
1385 lc->z=moldyn->dim.z/lc->nz;
1386 lc->cells=lc->nx*lc->ny*lc->nz;
1389 lc->subcell=malloc(lc->cells*sizeof(int*));
1391 lc->subcell=malloc(lc->cells*sizeof(t_list));
1394 if(lc->subcell==NULL) {
1395 perror("[moldyn] cell init (malloc)");
1400 printf("[moldyn] FATAL: less then 27 subcells!\n");
1404 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1407 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1410 printf(" x: %d x %f A\n",lc->nx,lc->x);
1411 printf(" y: %d x %f A\n",lc->ny,lc->y);
1412 printf(" z: %d x %f A\n",lc->nz,lc->z);
1417 for(i=0;i<lc->cells;i++) {
1418 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1419 if(lc->subcell[i]==NULL) {
1420 perror("[moldyn] list init (malloc)");
1425 printf(" ---> %d malloc %p (%p)\n",
1426 i,lc->subcell[0],lc->subcell);
1430 for(i=0;i<lc->cells;i++)
1431 list_init_f(&(lc->subcell[i]));
1434 /* update the list */
1435 link_cell_update(moldyn);
1440 int link_cell_update(t_moldyn *moldyn) {
1456 for(i=0;i<lc->cells;i++)
1458 memset(lc->subcell[i],0,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1460 list_destroy_f(&(lc->subcell[i]));
1463 for(count=0;count<moldyn->count;count++) {
1464 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1465 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1466 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1470 while(lc->subcell[i+j*nx+k*nx*ny][p]!=0)
1473 if(p>=MAX_ATOMS_PER_LIST) {
1474 printf("[moldyn] FATAL: amount of atoms too high!\n");
1478 lc->subcell[i+j*nx+k*nx*ny][p]=count;
1480 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nx*ny]),
1484 printf(" ---> %d %d malloc %p (%p)\n",
1485 i,count,lc->subcell[i].current,lc->subcell);
1493 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1517 if(i>=nx||j>=ny||k>=nz)
1518 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1521 cell[0]=lc->subcell[i+j*nx+k*a];
1522 for(ci=-1;ci<=1;ci++) {
1525 if((x<0)||(x>=nx)) {
1529 for(cj=-1;cj<=1;cj++) {
1532 if((y<0)||(y>=ny)) {
1536 for(ck=-1;ck<=1;ck++) {
1539 if((z<0)||(z>=nz)) {
1543 if(!(ci|cj|ck)) continue;
1545 cell[--count2]=lc->subcell[x+y*nx+z*a];
1548 cell[count1++]=lc->subcell[x+y*nx+z*a];
1559 int link_cell_shutdown(t_moldyn *moldyn) {
1566 for(i=0;i<lc->cells;i++) {
1568 free(lc->subcell[i]);
1570 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1571 list_destroy_f(&(lc->subcell[i]));
1580 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1584 t_moldyn_schedule *schedule;
1586 schedule=&(moldyn->schedule);
1587 count=++(schedule->total_sched);
1589 ptr=realloc(schedule->runs,count*sizeof(int));
1591 perror("[moldyn] realloc (runs)");
1595 schedule->runs[count-1]=runs;
1597 ptr=realloc(schedule->tau,count*sizeof(double));
1599 perror("[moldyn] realloc (tau)");
1603 schedule->tau[count-1]=tau;
1605 printf("[moldyn] schedule added:\n");
1606 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1612 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1614 moldyn->schedule.hook=hook;
1615 moldyn->schedule.hook_params=hook_params;
1622 * 'integration of newtons equation' - algorithms
1626 /* start the integration */
1628 int moldyn_integrate(t_moldyn *moldyn) {
1631 unsigned int e,m,s,v,p,t,a;
1633 t_moldyn_schedule *sched;
1638 double energy_scale;
1639 struct timeval t1,t2;
1642 sched=&(moldyn->schedule);
1645 /* initialize linked cell method */
1646 link_cell_init(moldyn,VERBOSE);
1648 /* logging & visualization */
1657 /* sqaure of some variables */
1658 moldyn->tau_square=moldyn->tau*moldyn->tau;
1660 /* get current time */
1661 gettimeofday(&t1,NULL);
1663 /* calculate initial forces */
1664 potential_force_calc(moldyn);
1669 /* some stupid checks before we actually start calculating bullshit */
1670 if(moldyn->cutoff>0.5*moldyn->dim.x)
1671 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1672 if(moldyn->cutoff>0.5*moldyn->dim.y)
1673 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1674 if(moldyn->cutoff>0.5*moldyn->dim.z)
1675 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1676 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1677 if(ds>0.05*moldyn->nnd)
1678 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1680 /* zero absolute time */
1682 moldyn->total_steps=0;
1684 /* debugging, ignore */
1687 /* tell the world */
1688 printf("[moldyn] integration start, go get a coffee ...\n");
1690 /* executing the schedule */
1692 while(sched->count<sched->total_sched) {
1694 /* setting amount of runs and finite time step size */
1695 moldyn->tau=sched->tau[sched->count];
1696 moldyn->tau_square=moldyn->tau*moldyn->tau;
1697 moldyn->time_steps=sched->runs[sched->count];
1699 /* energy scaling factor (might change!) */
1700 energy_scale=moldyn->count*EV;
1702 /* integration according to schedule */
1704 for(i=0;i<moldyn->time_steps;i++) {
1706 /* integration step */
1707 moldyn->integrate(moldyn);
1709 /* calculate kinetic energy, temperature and pressure */
1711 temperature_calc(moldyn);
1713 pressure_calc(moldyn);
1714 //thermodynamic_pressure_calc(moldyn);
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];
2426 link_cell_init(moldyn,VERBOSE);
2430 for(i=0;i<moldyn->count;i++) {
2431 /* neighbour indexing */
2432 link_cell_neighbour_index(moldyn,
2433 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2434 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2435 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2440 bc=(j<lc->dnlc)?0:1;
2445 while(neighbour[j][p]!=0) {
2447 jtom=&(moldyn->atom[neighbour[j][p]]);
2450 this=&(neighbour[j]);
2453 if(this->start==NULL)
2458 jtom=this->current->data;
2462 process(moldyn,&(itom[i]),jtom,data,bc);
2467 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2477 * post processing functions
2480 int get_line(int fd,char *line,int max) {
2487 if(count==max) return count;
2488 ret=read(fd,line+count,1);
2489 if(ret<=0) return ret;
2490 if(line[count]=='\n') {
2491 memset(line+count,0,max-count-1);
2499 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2505 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2521 for(i=0;i<moldyn->count;i++) {
2523 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2524 check_per_bound(moldyn,&dist);
2525 d2=v3_absolute_square(&dist);
2539 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2540 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2541 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2546 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2551 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2552 t_atom *jtom,void *data,u8 bc) {
2559 /* only count pairs once,
2560 * skip same atoms */
2561 if(itom->tag>=jtom->tag)
2565 * pair correlation calc
2572 v3_sub(&dist,&(jtom->r),&(itom->r));
2573 if(bc) check_per_bound(moldyn,&dist);
2574 d=v3_absolute_square(&dist);
2576 /* ignore if greater cutoff */
2577 if(d>moldyn->cutoff_square)
2580 /* fill the slots */
2584 /* should never happen but it does 8) -
2585 * related to -ffloat-store problem! */
2587 printf("[moldyn] WARNING: pcc (%d/%d)",
2593 if(itom->brand!=jtom->brand) {
2598 /* type a - type a bonds */
2600 pcc->stat[s+pcc->o1]+=1;
2602 /* type b - type b bonds */
2603 pcc->stat[s+pcc->o2]+=1;
2609 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2616 pcc.o1=moldyn->cutoff/dr;
2619 if(pcc.o1*dr<=moldyn->cutoff)
2620 printf("[moldyn] WARNING: pcc (low #slots)\n");
2622 printf("[moldyn] pair correlation calc info:\n");
2623 printf(" time: %f\n",moldyn->time);
2624 printf(" count: %d\n",moldyn->count);
2625 printf(" cutoff: %f\n",moldyn->cutoff);
2626 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2629 pcc.stat=(double *)ptr;
2632 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2633 if(pcc.stat==NULL) {
2634 perror("[moldyn] pair correlation malloc");
2639 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2642 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2645 for(i=1;i<pcc.o1;i++) {
2646 // normalization: 4 pi r^2 dr
2647 // here: not double counting pairs -> 2 pi r r dr
2648 // ... and actually it's a constant times r^2
2651 pcc.stat[pcc.o1+i]/=norm;
2652 pcc.stat[pcc.o2+i]/=norm;
2657 /* todo: store/print pair correlation function */
2664 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2671 if(itom->tag>=jtom->tag)
2675 v3_sub(&dist,&(jtom->r),&(itom->r));
2676 if(bc) check_per_bound(moldyn,&dist);
2677 d=v3_absolute_square(&dist);
2679 /* ignore if greater or equal cutoff */
2680 if(d>moldyn->cutoff_square)
2683 /* check for potential bond */
2684 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2689 /* now count this bonding ... */
2692 /* increase total bond counter
2693 * ... double counting!
2698 ba->acnt[jtom->tag]+=1;
2700 ba->bcnt[jtom->tag]+=1;
2703 ba->acnt[itom->tag]+=1;
2705 ba->bcnt[itom->tag]+=1;
2710 int bond_analyze(t_moldyn *moldyn,double *quality) {
2712 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2720 ba.acnt=malloc(moldyn->count*sizeof(int));
2722 perror("[moldyn] bond analyze malloc (a)");
2725 memset(ba.acnt,0,moldyn->count*sizeof(int));
2727 ba.bcnt=malloc(moldyn->count*sizeof(int));
2729 perror("[moldyn] bond analyze malloc (b)");
2732 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2741 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2743 for(i=0;i<moldyn->count;i++) {
2744 if(atom[i].brand==0) {
2745 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2749 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2757 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2758 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2761 quality[0]=1.0*ccnt/cset;
2762 quality[1]=1.0*qcnt/ba.tcnt;
2765 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2766 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2773 * visualization code
2776 int visual_init(t_moldyn *moldyn,char *filebase) {
2778 strncpy(moldyn->vis.fb,filebase,128);
2783 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2790 if(itom->tag>=jtom->tag)
2793 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2796 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
2797 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
2798 itom->r.x,itom->r.y,itom->r.z,
2799 jtom->r.x,jtom->r.y,jtom->r.z);
2804 int visual_atoms(t_moldyn *moldyn) {
2822 sprintf(file,"%s/atomic_conf_%07.f.xyz",v->fb,moldyn->time);
2823 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
2825 perror("open visual save file fd");
2829 /* write the actual data file */
2832 dprintf(vb.fd,"# [P] %d %07.f <%f,%f,%f>\n",
2833 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
2835 // atomic configuration
2836 for(i=0;i<moldyn->count;i++)
2837 // atom type, positions, color and kinetic energy
2838 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
2842 pse_col[atom[i].element],
2845 // bonds between atoms
2846 process_2b_bonds(moldyn,&vb,visual_bonds_process);
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);
2859 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2860 -dim.x/2,dim.y/2,-dim.z/2,
2861 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);
2872 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2873 -dim.x/2,dim.y/2,dim.z/2,
2874 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);
2885 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
2886 dim.x/2,dim.y/2,dim.z/2,
2887 dim.x/2,dim.y/2,-dim.z/2);