2 * moldyn.c - molecular dynamics library main file
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
20 #include <fpu_control.h>
26 #if defined PTHREADS || defined VISUAL_THREAD
31 #include "report/report.h"
33 /* potential includes */
34 #include "potentials/harmonic_oscillator.h"
35 #include "potentials/lennard_jones.h"
36 #include "potentials/albe.h"
38 #include "potentials/tersoff_orig.h"
40 #include "potentials/tersoff.h"
52 pthread_mutex_t *amutex;
53 pthread_mutex_t emutex;
57 * the moldyn functions
60 int moldyn_init(t_moldyn *moldyn,int argc,char **argv) {
62 printf("[moldyn] init\n");
64 /* only needed if compiled without -msse2 (float-store prob!) */
67 memset(moldyn,0,sizeof(t_moldyn));
72 rand_init(&(moldyn->random),NULL,1);
73 moldyn->random.status|=RAND_STAT_VERBOSE;
76 pthread_mutex_init(&emutex,NULL);
82 int moldyn_shutdown(t_moldyn *moldyn) {
88 printf("[moldyn] shutdown\n");
91 for(i=0;i<moldyn->count;i++)
92 pthread_mutex_destroy(&(amutex[i]));
94 pthread_mutex_destroy(&emutex);
97 moldyn_log_shutdown(moldyn);
98 link_cell_shutdown(moldyn);
99 rand_close(&(moldyn->random));
105 int set_int_alg(t_moldyn *moldyn,u8 algo) {
107 printf("[moldyn] integration algorithm: ");
110 case MOLDYN_INTEGRATE_VERLET:
111 moldyn->integrate=velocity_verlet;
112 printf("velocity verlet\n");
115 printf("unknown integration algorithm: %02x\n",algo);
123 int set_cutoff(t_moldyn *moldyn,double cutoff) {
125 moldyn->cutoff=cutoff;
126 moldyn->cutoff_square=cutoff*cutoff;
128 printf("[moldyn] cutoff [A]: %f\n",moldyn->cutoff);
133 int set_temperature(t_moldyn *moldyn,double t_ref) {
137 printf("[moldyn] temperature [K]: %f\n",moldyn->t_ref);
142 int set_pressure(t_moldyn *moldyn,double p_ref) {
146 printf("[moldyn] pressure [bar]: %f\n",moldyn->p_ref/BAR);
151 int set_p_scale(t_moldyn *moldyn,u8 ptype,double ptc) {
153 moldyn->pt_scale&=(~(P_SCALE_MASK));
154 moldyn->pt_scale|=ptype;
157 printf("[moldyn] p scaling:\n");
159 printf(" p: %s",ptype?"yes":"no ");
161 printf(" | type: %02x | factor: %f",ptype,ptc);
167 int set_t_scale(t_moldyn *moldyn,u8 ttype,double ttc) {
169 moldyn->pt_scale&=(~(T_SCALE_MASK));
170 moldyn->pt_scale|=ttype;
173 printf("[moldyn] t scaling:\n");
175 printf(" t: %s",ttype?"yes":"no ");
177 printf(" | type: %02x | factor: %f",ttype,ttc);
183 int set_pt_scale(t_moldyn *moldyn,u8 ptype,double ptc,u8 ttype,double ttc) {
185 moldyn->pt_scale=(ptype|ttype);
189 printf("[moldyn] p/t scaling:\n");
191 printf(" p: %s",ptype?"yes":"no ");
193 printf(" | type: %02x | factor: %f",ptype,ptc);
196 printf(" t: %s",ttype?"yes":"no ");
198 printf(" | type: %02x | factor: %f",ttype,ttc);
204 int set_dim(t_moldyn *moldyn,double x,double y,double z,u8 visualize) {
210 moldyn->volume=x*y*z;
218 printf("[moldyn] dimensions in A and A^3 respectively:\n");
219 printf(" x: %f\n",moldyn->dim.x);
220 printf(" y: %f\n",moldyn->dim.y);
221 printf(" z: %f\n",moldyn->dim.z);
222 printf(" volume: %f\n",moldyn->volume);
223 printf(" visualize simulation box: %s\n",visualize?"yes":"no");
228 int set_nn_dist(t_moldyn *moldyn,double dist) {
235 int set_pbc(t_moldyn *moldyn,u8 x,u8 y,u8 z) {
237 printf("[moldyn] periodic boundary conditions:\n");
240 moldyn->status|=MOLDYN_STAT_PBX;
243 moldyn->status|=MOLDYN_STAT_PBY;
246 moldyn->status|=MOLDYN_STAT_PBZ;
248 printf(" x: %s\n",x?"yes":"no");
249 printf(" y: %s\n",y?"yes":"no");
250 printf(" z: %s\n",z?"yes":"no");
255 int set_potential(t_moldyn *moldyn,u8 type) {
258 case MOLDYN_POTENTIAL_TM:
259 moldyn->func1b=tersoff_mult_1bp;
260 moldyn->func3b_j1=tersoff_mult_3bp_j1;
261 moldyn->func3b_k1=tersoff_mult_3bp_k1;
262 moldyn->func3b_j2=tersoff_mult_3bp_j2;
263 moldyn->func3b_k2=tersoff_mult_3bp_k2;
264 moldyn->check_2b_bond=tersoff_mult_check_2b_bond;
266 case MOLDYN_POTENTIAL_AM:
267 moldyn->func3b_j1=albe_mult_3bp_j1;
268 moldyn->func3b_k1=albe_mult_3bp_k1;
269 moldyn->func3b_j2=albe_mult_3bp_j2;
270 moldyn->func3b_k2=albe_mult_3bp_k2;
271 moldyn->check_2b_bond=albe_mult_check_2b_bond;
273 case MOLDYN_POTENTIAL_HO:
274 moldyn->func2b=harmonic_oscillator;
275 moldyn->check_2b_bond=harmonic_oscillator_check_2b_bond;
277 case MOLDYN_POTENTIAL_LJ:
278 moldyn->func2b=lennard_jones;
279 moldyn->check_2b_bond=lennard_jones_check_2b_bond;
282 printf("[moldyn] set potential: unknown type %02x\n",
290 int set_avg_skip(t_moldyn *moldyn,int skip) {
292 printf("[moldyn] skip %d steps before starting average calc\n",skip);
293 moldyn->avg_skip=skip;
298 int moldyn_set_log_dir(t_moldyn *moldyn,char *dir) {
300 strncpy(moldyn->vlsdir,dir,127);
305 int moldyn_set_report(t_moldyn *moldyn,char *author,char *title) {
307 strncpy(moldyn->rauthor,author,63);
308 strncpy(moldyn->rtitle,title,63);
313 int moldyn_set_log(t_moldyn *moldyn,u8 type,int timer) {
318 printf("[moldyn] set log: ");
321 case LOG_TOTAL_ENERGY:
322 moldyn->ewrite=timer;
323 snprintf(filename,127,"%s/energy",moldyn->vlsdir);
324 moldyn->efd=open(filename,
325 O_WRONLY|O_CREAT|O_EXCL,
328 perror("[moldyn] energy log fd open");
331 dprintf(moldyn->efd,"# total energy log file\n");
332 printf("total energy (%d)\n",timer);
334 case LOG_TOTAL_MOMENTUM:
335 moldyn->mwrite=timer;
336 snprintf(filename,127,"%s/momentum",moldyn->vlsdir);
337 moldyn->mfd=open(filename,
338 O_WRONLY|O_CREAT|O_EXCL,
341 perror("[moldyn] momentum log fd open");
344 dprintf(moldyn->efd,"# total momentum log file\n");
345 printf("total momentum (%d)\n",timer);
348 moldyn->pwrite=timer;
349 snprintf(filename,127,"%s/pressure",moldyn->vlsdir);
350 moldyn->pfd=open(filename,
351 O_WRONLY|O_CREAT|O_EXCL,
354 perror("[moldyn] pressure log file\n");
357 dprintf(moldyn->pfd,"# pressure log file\n");
358 printf("pressure (%d)\n",timer);
360 case LOG_TEMPERATURE:
361 moldyn->twrite=timer;
362 snprintf(filename,127,"%s/temperature",moldyn->vlsdir);
363 moldyn->tfd=open(filename,
364 O_WRONLY|O_CREAT|O_EXCL,
367 perror("[moldyn] temperature log file\n");
370 dprintf(moldyn->tfd,"# temperature log file\n");
371 printf("temperature (%d)\n",timer);
374 moldyn->vwrite=timer;
375 snprintf(filename,127,"%s/volume",moldyn->vlsdir);
376 moldyn->vfd=open(filename,
377 O_WRONLY|O_CREAT|O_EXCL,
380 perror("[moldyn] volume log file\n");
383 dprintf(moldyn->vfd,"# volume log file\n");
384 printf("volume (%d)\n",timer);
387 moldyn->swrite=timer;
388 printf("save file (%d)\n",timer);
391 moldyn->awrite=timer;
392 ret=visual_init(moldyn,moldyn->vlsdir);
394 printf("[moldyn] visual init failure\n");
397 printf("visual file (%d)\n",timer);
400 snprintf(filename,127,"%s/report.tex",moldyn->vlsdir);
401 moldyn->rfd=open(filename,
402 O_WRONLY|O_CREAT|O_EXCL,
405 perror("[moldyn] report fd open");
408 printf("report -> ");
410 snprintf(filename,127,"%s/e_plot.scr",
412 moldyn->epfd=open(filename,
413 O_WRONLY|O_CREAT|O_EXCL,
416 perror("[moldyn] energy plot fd open");
419 dprintf(moldyn->epfd,e_plot_script);
424 snprintf(filename,127,"%s/pressure_plot.scr",
426 moldyn->ppfd=open(filename,
427 O_WRONLY|O_CREAT|O_EXCL,
430 perror("[moldyn] p plot fd open");
433 dprintf(moldyn->ppfd,pressure_plot_script);
438 snprintf(filename,127,"%s/temperature_plot.scr",
440 moldyn->tpfd=open(filename,
441 O_WRONLY|O_CREAT|O_EXCL,
444 perror("[moldyn] t plot fd open");
447 dprintf(moldyn->tpfd,temperature_plot_script);
449 printf("temperature ");
451 dprintf(moldyn->rfd,report_start,
452 moldyn->rauthor,moldyn->rtitle);
456 printf("unknown log type: %02x\n",type);
463 int moldyn_log_shutdown(t_moldyn *moldyn) {
467 printf("[moldyn] log shutdown\n");
471 dprintf(moldyn->rfd,report_energy);
472 snprintf(sc,255,"cd %s && gnuplot e_plot.scr",
477 if(moldyn->mfd) close(moldyn->mfd);
481 dprintf(moldyn->rfd,report_pressure);
482 snprintf(sc,255,"cd %s && gnuplot pressure_plot.scr",
489 dprintf(moldyn->rfd,report_temperature);
490 snprintf(sc,255,"cd %s && gnuplot temperature_plot.scr",
495 dprintf(moldyn->rfd,report_end);
497 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
500 snprintf(sc,255,"cd %s && pdflatex report >/dev/null 2>&1",
503 snprintf(sc,255,"cd %s && dvipdf report >/dev/null 2>&1",
512 * creating lattice functions
515 int create_lattice(t_moldyn *moldyn,u8 type,double lc,int element,double mass,
516 u8 attr,u8 brand,int a,int b,int c,t_3dvec *origin) {
525 pthread_mutex_t *mutex;
531 /* how many atoms do we expect */
534 printf("[moldyn] WARNING: create 'none' lattice called");
536 if(type==CUBIC) new*=1;
537 if(type==FCC) new*=4;
538 if(type==DIAMOND) new*=8;
540 /* allocate space for atoms */
541 ptr=realloc(moldyn->atom,(count+new)*sizeof(t_atom));
543 perror("[moldyn] realloc (create lattice)");
547 atom=&(moldyn->atom[count]);
550 ptr=realloc(amutex,(count+new)*sizeof(pthread_mutex_t));
552 perror("[moldyn] mutex realloc (add atom)");
556 mutex=&(amutex[count]);
559 /* no atoms on the boundaries (only reason: it looks better!) */
573 set_nn_dist(moldyn,lc);
574 ret=cubic_init(a,b,c,lc,atom,&orig);
575 strcpy(name,"cubic");
579 v3_scale(&orig,&orig,0.5);
580 set_nn_dist(moldyn,0.5*sqrt(2.0)*lc);
581 ret=fcc_init(a,b,c,lc,atom,&orig);
586 v3_scale(&orig,&orig,0.25);
587 set_nn_dist(moldyn,0.25*sqrt(3.0)*lc);
588 ret=diamond_init(a,b,c,lc,atom,&orig);
589 strcpy(name,"diamond");
592 printf("unknown lattice type (%02x)\n",type);
598 printf("[moldyn] creating lattice failed\n");
599 printf(" amount of atoms\n");
600 printf(" - expected: %d\n",new);
601 printf(" - created: %d\n",ret);
606 printf("[moldyn] created %s lattice with %d atoms\n",name,new);
608 for(ret=0;ret<new;ret++) {
609 atom[ret].element=element;
612 atom[ret].brand=brand;
613 atom[ret].tag=count+ret;
614 check_per_bound(moldyn,&(atom[ret].r));
615 atom[ret].r_0=atom[ret].r;
617 pthread_mutex_init(&(mutex[ret]),NULL);
621 /* update total system mass */
622 total_mass_calc(moldyn);
627 int add_atom(t_moldyn *moldyn,int element,double mass,u8 brand,u8 attr,
628 t_3dvec *r,t_3dvec *v) {
635 count=(moldyn->count)++; // asshole style!
637 ptr=realloc(atom,(count+1)*sizeof(t_atom));
639 perror("[moldyn] realloc (add atom)");
645 ptr=realloc(moldyn->lc.subcell->list,(count+1)*sizeof(int));
647 perror("[moldyn] list realloc (add atom)");
650 moldyn->lc.subcell->list=ptr;
654 ptr=realloc(amutex,(count+1)*sizeof(pthread_mutex_t));
656 perror("[moldyn] mutex realloc (add atom)");
660 pthread_mutex_init(&(amutex[count]),NULL);
665 /* initialize new atom */
666 memset(&(atom[count]),0,sizeof(t_atom));
669 atom[count].element=element;
670 atom[count].mass=mass;
671 atom[count].brand=brand;
672 atom[count].tag=count;
673 atom[count].attr=attr;
674 check_per_bound(moldyn,&(atom[count].r));
675 atom[count].r_0=atom[count].r;
677 /* update total system mass */
678 total_mass_calc(moldyn);
683 int del_atom(t_moldyn *moldyn,int tag) {
690 new=(t_atom *)malloc((moldyn->count-1)*sizeof(t_atom));
692 perror("[moldyn]malloc (del atom)");
696 for(cnt=0;cnt<tag;cnt++)
699 for(cnt=tag+1;cnt<moldyn->count;cnt++) {
701 new[cnt-1].tag=cnt-1;
713 int cubic_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
732 v3_copy(&(atom[count].r),&r);
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 /* fcc lattice init */
751 int fcc_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
764 /* construct the basis */
765 memset(basis,0,3*sizeof(t_3dvec));
773 /* fill up the room */
781 v3_copy(&(atom[count].r),&r);
784 /* the three face centered atoms */
786 v3_add(&n,&r,&basis[l]);
787 v3_copy(&(atom[count].r),&n);
796 /* coordinate transformation */
797 for(i=0;i<count;i++) {
798 atom[i].r.x-=(a*lc)/2.0;
799 atom[i].r.y-=(b*lc)/2.0;
800 atom[i].r.z-=(c*lc)/2.0;
806 int diamond_init(int a,int b,int c,double lc,t_atom *atom,t_3dvec *origin) {
811 count=fcc_init(a,b,c,lc,atom,origin);
817 if(origin) v3_add(&o,&o,origin);
819 count+=fcc_init(a,b,c,lc,&atom[count],&o);
824 int destroy_atoms(t_moldyn *moldyn) {
826 if(moldyn->atom) free(moldyn->atom);
831 int thermal_init(t_moldyn *moldyn,u8 equi_init) {
834 * - gaussian distribution of velocities
835 * - zero total momentum
836 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
841 t_3dvec p_total,delta;
846 random=&(moldyn->random);
848 printf("[moldyn] thermal init (equi init: %s)\n",equi_init?"yes":"no");
850 /* gaussian distribution of velocities */
852 for(i=0;i<moldyn->count;i++) {
853 sigma=sqrt(2.0*K_BOLTZMANN*moldyn->t_ref/atom[i].mass);
855 v=sigma*rand_get_gauss(random);
857 p_total.x+=atom[i].mass*v;
859 v=sigma*rand_get_gauss(random);
861 p_total.y+=atom[i].mass*v;
863 v=sigma*rand_get_gauss(random);
865 p_total.z+=atom[i].mass*v;
868 /* zero total momentum */
869 v3_scale(&p_total,&p_total,1.0/moldyn->count);
870 for(i=0;i<moldyn->count;i++) {
871 v3_scale(&delta,&p_total,1.0/atom[i].mass);
872 v3_sub(&(atom[i].v),&(atom[i].v),&delta);
875 /* velocity scaling */
876 scale_velocity(moldyn,equi_init);
881 double total_mass_calc(t_moldyn *moldyn) {
887 for(i=0;i<moldyn->count;i++)
888 moldyn->mass+=moldyn->atom[i].mass;
893 double temperature_calc(t_moldyn *moldyn) {
895 /* assume up to date kinetic energy, which is 3/2 N k_B T */
897 moldyn->t=(2.0*moldyn->ekin)/(3.0*K_BOLTZMANN*moldyn->count);
902 double get_temperature(t_moldyn *moldyn) {
907 int scale_velocity(t_moldyn *moldyn,u8 equi_init) {
917 * - velocity scaling (E = 3/2 N k T), E: kinetic energy
920 /* get kinetic energy / temperature & count involved atoms */
923 for(i=0;i<moldyn->count;i++) {
924 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB)) {
925 e+=atom[i].mass*v3_absolute_square(&(atom[i].v));
930 if(count!=0) moldyn->t=e/(1.5*count*K_BOLTZMANN);
931 else return 0; /* no atoms involved in scaling! */
933 /* (temporary) hack for e,t = 0 */
936 if(moldyn->t_ref!=0.0) {
937 thermal_init(moldyn,equi_init);
941 return 0; /* no scaling needed */
945 /* get scaling factor */
946 scale=moldyn->t_ref/moldyn->t;
950 if(moldyn->pt_scale&T_SCALE_BERENDSEN)
951 scale=1.0+(scale-1.0)*moldyn->tau/moldyn->t_tc;
954 /* velocity scaling */
955 for(i=0;i<moldyn->count;i++) {
956 if((equi_init&TRUE)||(atom[i].attr&ATOM_ATTR_HB))
957 v3_scale(&(atom[i].v),&(atom[i].v),scale);
963 double ideal_gas_law_pressure(t_moldyn *moldyn) {
967 p=moldyn->count*moldyn->t*K_BOLTZMANN/moldyn->volume;
972 double virial_sum(t_moldyn *moldyn) {
977 /* virial (sum over atom virials) */
985 for(i=0;i<moldyn->count;i++) {
986 virial=&(moldyn->atom[i].virial);
987 moldyn->virial+=(virial->xx+virial->yy+virial->zz);
988 moldyn->vir.xx+=virial->xx;
989 moldyn->vir.yy+=virial->yy;
990 moldyn->vir.zz+=virial->zz;
991 moldyn->vir.xy+=virial->xy;
992 moldyn->vir.xz+=virial->xz;
993 moldyn->vir.yz+=virial->yz;
996 /* global virial (absolute coordinates) */
997 virial=&(moldyn->gvir);
998 moldyn->gv=virial->xx+virial->yy+virial->zz;
1000 return moldyn->virial;
1003 double pressure_calc(t_moldyn *moldyn) {
1007 * with W = 1/3 sum_i f_i r_i (- skipped!)
1008 * virial = sum_i f_i r_i
1010 * => P = (2 Ekin + virial) / (3V)
1013 /* assume up to date virial & up to date kinetic energy */
1015 /* pressure (atom virials) */
1016 moldyn->p=2.0*moldyn->ekin+moldyn->virial;
1017 moldyn->p/=(3.0*moldyn->volume);
1019 /* pressure (absolute coordinates) */
1020 moldyn->gp=2.0*moldyn->ekin+moldyn->gv;
1021 moldyn->gp/=(3.0*moldyn->volume);
1026 int average_reset(t_moldyn *moldyn) {
1028 printf("[moldyn] average reset\n");
1030 /* update skip value */
1031 moldyn->avg_skip=moldyn->total_steps;
1033 /* kinetic energy */
1037 /* potential energy */
1045 moldyn->virial_sum=0.0;
1056 int average_and_fluctuation_calc(t_moldyn *moldyn) {
1060 if(moldyn->total_steps<moldyn->avg_skip)
1063 denom=moldyn->total_steps+1-moldyn->avg_skip;
1065 /* assume up to date energies, temperature, pressure etc */
1067 /* kinetic energy */
1068 moldyn->k_sum+=moldyn->ekin;
1069 moldyn->k2_sum+=(moldyn->ekin*moldyn->ekin);
1070 moldyn->k_avg=moldyn->k_sum/denom;
1071 moldyn->k2_avg=moldyn->k2_sum/denom;
1072 moldyn->dk2_avg=moldyn->k2_avg-(moldyn->k_avg*moldyn->k_avg);
1074 /* potential energy */
1075 moldyn->v_sum+=moldyn->energy;
1076 moldyn->v2_sum+=(moldyn->energy*moldyn->energy);
1077 moldyn->v_avg=moldyn->v_sum/denom;
1078 moldyn->v2_avg=moldyn->v2_sum/denom;
1079 moldyn->dv2_avg=moldyn->v2_avg-(moldyn->v_avg*moldyn->v_avg);
1082 moldyn->t_sum+=moldyn->t;
1083 moldyn->t_avg=moldyn->t_sum/denom;
1086 moldyn->virial_sum+=moldyn->virial;
1087 moldyn->virial_avg=moldyn->virial_sum/denom;
1088 moldyn->gv_sum+=moldyn->gv;
1089 moldyn->gv_avg=moldyn->gv_sum/denom;
1092 moldyn->p_sum+=moldyn->p;
1093 moldyn->p_avg=moldyn->p_sum/denom;
1094 moldyn->gp_sum+=moldyn->gp;
1095 moldyn->gp_avg=moldyn->gp_sum/denom;
1096 moldyn->tp_sum+=moldyn->tp;
1097 moldyn->tp_avg=moldyn->tp_sum/denom;
1102 int get_heat_capacity(t_moldyn *moldyn) {
1106 /* averages needed for heat capacity calc */
1107 if(moldyn->total_steps<moldyn->avg_skip)
1110 /* (temperature average)^2 */
1111 temp2=moldyn->t_avg*moldyn->t_avg;
1112 printf("[moldyn] specific heat capacity for T=%f K [J/(kg K)]\n",
1115 /* ideal gas contribution */
1116 ighc=3.0*moldyn->count*K_BOLTZMANN/2.0;
1117 printf(" ideal gas contribution: %f\n",
1118 ighc/moldyn->mass*KILOGRAM/JOULE);
1120 /* specific heat for nvt ensemble */
1121 moldyn->c_v_nvt=moldyn->dv2_avg/(K_BOLTZMANN*temp2)+ighc;
1122 moldyn->c_v_nvt/=moldyn->mass;
1124 /* specific heat for nve ensemble */
1125 moldyn->c_v_nve=ighc/(1.0-(moldyn->dv2_avg/(ighc*K_BOLTZMANN*temp2)));
1126 moldyn->c_v_nve/=moldyn->mass;
1128 printf(" NVE: %f\n",moldyn->c_v_nve*KILOGRAM/JOULE);
1129 printf(" NVT: %f\n",moldyn->c_v_nvt*KILOGRAM/JOULE);
1130 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)));
1135 double thermodynamic_pressure_calc(t_moldyn *moldyn) {
1151 /* store atomic configuration + dimension */
1152 store=malloc(moldyn->count*sizeof(t_atom));
1154 printf("[moldyn] allocating store mem failed\n");
1157 memcpy(store,moldyn->atom,moldyn->count*sizeof(t_atom));
1162 h=(1.0-sd)*(1.0-sd)*(1.0-sd);
1163 su=pow(2.0-h,ONE_THIRD)-1.0;
1164 dv=(1.0-h)*moldyn->volume;
1166 /* scale up dimension and atom positions */
1167 scale_dim(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1168 scale_atoms(moldyn,SCALE_UP,su,TRUE,TRUE,TRUE);
1169 link_cell_shutdown(moldyn);
1170 link_cell_init(moldyn,QUIET);
1171 potential_force_calc(moldyn);
1174 /* restore atomic configuration + dim */
1175 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1178 /* scale down dimension and atom positions */
1179 scale_dim(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1180 scale_atoms(moldyn,SCALE_DOWN,sd,TRUE,TRUE,TRUE);
1181 link_cell_shutdown(moldyn);
1182 link_cell_init(moldyn,QUIET);
1183 potential_force_calc(moldyn);
1186 /* calculate pressure */
1187 moldyn->tp=-(y1-y0)/(2.0*dv);
1189 /* restore atomic configuration */
1190 memcpy(moldyn->atom,store,moldyn->count*sizeof(t_atom));
1192 link_cell_shutdown(moldyn);
1193 link_cell_init(moldyn,QUIET);
1194 //potential_force_calc(moldyn);
1196 /* free store buffer */
1203 double get_pressure(t_moldyn *moldyn) {
1209 int scale_dim(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1221 if(x) dim->x*=scale;
1222 if(y) dim->y*=scale;
1223 if(z) dim->z*=scale;
1228 int scale_atoms(t_moldyn *moldyn,u8 dir,double scale,u8 x,u8 y,u8 z) {
1239 for(i=0;i<moldyn->count;i++) {
1240 r=&(moldyn->atom[i].r);
1249 int scale_volume(t_moldyn *moldyn) {
1255 vdim=&(moldyn->vis.dim);
1259 /* scaling factor */
1260 if(moldyn->pt_scale&P_SCALE_BERENDSEN) {
1261 scale=1.0-(moldyn->p_ref-moldyn->p)*moldyn->p_tc*moldyn->tau;
1262 scale=pow(scale,ONE_THIRD);
1265 scale=pow(moldyn->p/moldyn->p_ref,ONE_THIRD);
1268 /* scale the atoms and dimensions */
1269 scale_atoms(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1270 scale_dim(moldyn,SCALE_DIRECT,scale,TRUE,TRUE,TRUE);
1272 /* visualize dimensions */
1279 /* recalculate scaled volume */
1280 moldyn->volume=dim->x*dim->y*dim->z;
1282 /* adjust/reinit linkcell */
1283 if(((int)(dim->x/moldyn->cutoff)!=lc->nx)||
1284 ((int)(dim->y/moldyn->cutoff)!=lc->ny)||
1285 ((int)(dim->z/moldyn->cutoff)!=lc->nx)) {
1286 link_cell_shutdown(moldyn);
1287 link_cell_init(moldyn,QUIET);
1298 double e_kin_calc(t_moldyn *moldyn) {
1306 for(i=0;i<moldyn->count;i++) {
1307 atom[i].ekin=0.5*atom[i].mass*v3_absolute_square(&(atom[i].v));
1308 moldyn->ekin+=atom[i].ekin;
1311 return moldyn->ekin;
1314 double get_total_energy(t_moldyn *moldyn) {
1316 return(moldyn->ekin+moldyn->energy);
1319 t_3dvec get_total_p(t_moldyn *moldyn) {
1328 for(i=0;i<moldyn->count;i++) {
1329 v3_scale(&p,&(atom[i].v),atom[i].mass);
1330 v3_add(&p_total,&p_total,&p);
1336 double estimate_time_step(t_moldyn *moldyn,double nn_dist) {
1340 /* nn_dist is the nearest neighbour distance */
1342 tau=(0.05*nn_dist*moldyn->atom[0].mass)/sqrt(3.0*K_BOLTZMANN*moldyn->t);
1351 /* linked list / cell method */
1353 int link_cell_init(t_moldyn *moldyn,u8 vol) {
1356 #ifndef LOWMEM_LISTS
1362 /* partitioning the md cell */
1363 lc->nx=moldyn->dim.x/moldyn->cutoff;
1364 lc->x=moldyn->dim.x/lc->nx;
1365 lc->ny=moldyn->dim.y/moldyn->cutoff;
1366 lc->y=moldyn->dim.y/lc->ny;
1367 lc->nz=moldyn->dim.z/moldyn->cutoff;
1368 lc->z=moldyn->dim.z/lc->nz;
1369 lc->cells=lc->nx*lc->ny*lc->nz;
1372 lc->subcell=malloc(lc->cells*sizeof(int*));
1374 lc->subcell=malloc(sizeof(t_lowmem_list));
1376 lc->subcell=malloc(lc->cells*sizeof(t_list));
1379 if(lc->subcell==NULL) {
1380 perror("[moldyn] cell init (malloc)");
1385 printf("[moldyn] FATAL: less then 27 subcells! (%d)\n",
1390 printf("[moldyn] initializing 'static' linked cells (%d)\n",
1393 printf("[moldyn] initializing 'lowmem' linked cells (%d)\n",
1396 printf("[moldyn] initializing 'dynamic' linked cells (%d)\n",
1399 printf(" x: %d x %f A\n",lc->nx,lc->x);
1400 printf(" y: %d x %f A\n",lc->ny,lc->y);
1401 printf(" z: %d x %f A\n",lc->nz,lc->z);
1406 for(i=0;i<lc->cells;i++) {
1407 lc->subcell[i]=malloc((MAX_ATOMS_PER_LIST+1)*sizeof(int));
1408 if(lc->subcell[i]==NULL) {
1409 perror("[moldyn] list init (malloc)");
1414 printf(" ---> %d malloc %p (%p)\n",
1415 i,lc->subcell[0],lc->subcell);
1419 lc->subcell->head=malloc(lc->cells*sizeof(int));
1420 if(lc->subcell->head==NULL) {
1421 perror("[moldyn] head init (malloc)");
1424 lc->subcell->list=malloc(moldyn->count*sizeof(int));
1425 if(lc->subcell->list==NULL) {
1426 perror("[moldyn] list init (malloc)");
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) {
1458 for(i=0;i<lc->cells;i++)
1460 memset(lc->subcell[i],-1,(MAX_ATOMS_PER_LIST+1)*sizeof(int));
1462 lc->subcell->head[i]=-1;
1464 list_destroy_f(&(lc->subcell[i]));
1467 for(count=0;count<moldyn->count;count++) {
1468 i=((atom[count].r.x+(moldyn->dim.x/2))/lc->x);
1469 j=((atom[count].r.y+(moldyn->dim.y/2))/lc->y);
1470 k=((atom[count].r.z+(moldyn->dim.z/2))/lc->z);
1474 while(lc->subcell[i+j*nx+k*nxy][p]!=-1)
1477 if(p>=MAX_ATOMS_PER_LIST) {
1478 printf("[moldyn] FATAL: amount of atoms too high!\n");
1482 lc->subcell[i+j*nx+k*nxy][p]=count;
1485 lc->subcell->list[count]=lc->subcell->head[p];
1486 lc->subcell->head[p]=count;
1488 list_add_immediate_f(&(lc->subcell[i+j*nx+k*nxy]),
1492 printf(" ---> %d %d malloc %p (%p)\n",
1493 i,count,lc->subcell[i].current,lc->subcell);
1501 int link_cell_neighbour_index(t_moldyn *moldyn,int i,int j,int k,
1527 if(i>=nx||j>=ny||k>=nz)
1528 printf("[moldyn] WARNING: lcni %d/%d %d/%d %d/%d\n",
1531 #ifndef LOWMEM_LISTS
1532 cell[0]=lc->subcell[i+j*nx+k*a];
1534 cell[0]=lc->subcell->head[i+j*nx+k*a];
1536 for(ci=-1;ci<=1;ci++) {
1539 if((x<0)||(x>=nx)) {
1543 for(cj=-1;cj<=1;cj++) {
1546 if((y<0)||(y>=ny)) {
1550 for(ck=-1;ck<=1;ck++) {
1553 if((z<0)||(z>=nz)) {
1557 if(!(ci|cj|ck)) continue;
1559 #ifndef LOWMEM_LISTS
1560 cell[--count2]=lc->subcell[x+y*nx+z*a];
1562 cell[--count2]=lc->subcell->head[x+y*nx+z*a];
1567 #ifndef LOWMEM_LISTS
1568 cell[count1++]=lc->subcell[x+y*nx+z*a];
1570 cell[count1++]=lc->subcell->head[x+y*nx+z*a];
1582 int link_cell_shutdown(t_moldyn *moldyn) {
1584 #ifndef LOWMEM_LISTS
1592 free(lc->subcell->head);
1593 free(lc->subcell->list);
1597 for(i=0;i<lc->cells;i++) {
1599 free(lc->subcell[i]);
1601 //printf(" ---> %d free %p\n",i,lc->subcell[i].start);
1602 list_destroy_f(&(lc->subcell[i]));
1612 int moldyn_add_schedule(t_moldyn *moldyn,int runs,double tau) {
1616 t_moldyn_schedule *schedule;
1618 schedule=&(moldyn->schedule);
1619 count=++(schedule->total_sched);
1621 ptr=realloc(schedule->runs,count*sizeof(int));
1623 perror("[moldyn] realloc (runs)");
1627 schedule->runs[count-1]=runs;
1629 ptr=realloc(schedule->tau,count*sizeof(double));
1631 perror("[moldyn] realloc (tau)");
1635 schedule->tau[count-1]=tau;
1637 printf("[moldyn] schedule added:\n");
1638 printf(" number: %d | runs: %d | tau: %f\n",count-1,runs,tau);
1644 int moldyn_set_schedule_hook(t_moldyn *moldyn,set_hook hook,void *hook_params) {
1646 moldyn->schedule.hook=hook;
1647 moldyn->schedule.hook_params=hook_params;
1654 * 'integration of newtons equation' - algorithms
1658 /* start the integration */
1660 int moldyn_integrate(t_moldyn *moldyn) {
1663 unsigned int e,m,s,v,p,t,a;
1665 t_moldyn_schedule *sched;
1670 double energy_scale;
1671 struct timeval t1,t2;
1674 #ifdef VISUAL_THREAD
1676 pthread_t io_thread;
1685 sched=&(moldyn->schedule);
1688 /* initialize linked cell method */
1689 link_cell_init(moldyn,VERBOSE);
1691 /* logging & visualization */
1700 /* sqaure of some variables */
1701 moldyn->tau_square=moldyn->tau*moldyn->tau;
1703 /* get current time */
1704 gettimeofday(&t1,NULL);
1706 /* calculate initial forces */
1707 potential_force_calc(moldyn);
1712 /* some stupid checks before we actually start calculating bullshit */
1713 if(moldyn->cutoff>0.5*moldyn->dim.x)
1714 printf("[moldyn] WARNING: cutoff > 0.5 x dim.x\n");
1715 if(moldyn->cutoff>0.5*moldyn->dim.y)
1716 printf("[moldyn] WARNING: cutoff > 0.5 x dim.y\n");
1717 if(moldyn->cutoff>0.5*moldyn->dim.z)
1718 printf("[moldyn] WARNING: cutoff > 0.5 x dim.z\n");
1720 ds=0.5*atom[0].f.x*moldyn->tau_square/atom[0].mass;
1721 if(ds>0.05*moldyn->nnd)
1722 printf("[moldyn] WARNING: forces too high / tau too small!\n");
1725 /* zero absolute time */
1726 // should have right values!
1728 //moldyn->total_steps=0;
1730 /* debugging, ignore */
1733 /* zero & init moldyn copy */
1734 #ifdef VISUAL_THREAD
1735 memset(&md_copy,0,sizeof(t_moldyn));
1736 atom_copy=malloc(moldyn->count*sizeof(t_atom));
1737 if(atom_copy==NULL) {
1738 perror("[moldyn] malloc atom copy (init)");
1743 /* tell the world */
1744 printf("[moldyn] integration start, go get a coffee ...\n");
1746 /* executing the schedule */
1748 while(sched->count<sched->total_sched) {
1750 /* setting amount of runs and finite time step size */
1751 moldyn->tau=sched->tau[sched->count];
1752 moldyn->tau_square=moldyn->tau*moldyn->tau;
1753 moldyn->time_steps=sched->runs[sched->count];
1755 /* energy scaling factor (might change!) */
1756 energy_scale=moldyn->count*EV;
1758 /* integration according to schedule */
1760 for(i=0;i<moldyn->time_steps;i++) {
1762 /* integration step */
1763 moldyn->integrate(moldyn);
1765 /* calculate kinetic energy, temperature and pressure */
1767 temperature_calc(moldyn);
1769 pressure_calc(moldyn);
1771 thermodynamic_pressure_calc(moldyn);
1772 printf("\n\nDEBUG: numeric pressure calc: %f\n\n",
1776 /* calculate fluctuations + averages */
1777 average_and_fluctuation_calc(moldyn);
1780 if(moldyn->pt_scale&(T_SCALE_BERENDSEN|T_SCALE_DIRECT))
1781 scale_velocity(moldyn,FALSE);
1782 if(moldyn->pt_scale&(P_SCALE_BERENDSEN|P_SCALE_DIRECT))
1783 scale_volume(moldyn);
1785 /* check for log & visualization */
1787 if(!(moldyn->total_steps%e))
1788 dprintf(moldyn->efd,
1790 moldyn->time,moldyn->ekin/energy_scale,
1791 moldyn->energy/energy_scale,
1792 get_total_energy(moldyn)/energy_scale);
1795 if(!(moldyn->total_steps%m)) {
1796 momentum=get_total_p(moldyn);
1797 dprintf(moldyn->mfd,
1798 "%f %f %f %f %f\n",moldyn->time,
1799 momentum.x,momentum.y,momentum.z,
1800 v3_norm(&momentum));
1804 if(!(moldyn->total_steps%p)) {
1805 dprintf(moldyn->pfd,
1806 "%f %f %f %f %f %f %f\n",moldyn->time,
1807 moldyn->p/BAR,moldyn->p_avg/BAR,
1808 moldyn->gp/BAR,moldyn->gp_avg/BAR,
1809 moldyn->tp/BAR,moldyn->tp_avg/BAR);
1813 if(!(moldyn->total_steps%t)) {
1814 dprintf(moldyn->tfd,
1816 moldyn->time,moldyn->t,moldyn->t_avg);
1820 if(!(moldyn->total_steps%v)) {
1821 dprintf(moldyn->vfd,
1822 "%f %f\n",moldyn->time,moldyn->volume);
1826 if(!(moldyn->total_steps%s)) {
1827 snprintf(dir,128,"%s/s-%08.f.save",
1828 moldyn->vlsdir,moldyn->time);
1829 fd=open(dir,O_WRONLY|O_TRUNC|O_CREAT,
1831 if(fd<0) perror("[moldyn] save fd open");
1833 write(fd,moldyn,sizeof(t_moldyn));
1834 write(fd,moldyn->atom,
1835 moldyn->count*sizeof(t_atom));
1841 if(!(moldyn->total_steps%a)) {
1842 #ifdef VISUAL_THREAD
1843 /* check whether thread has not terminated yet */
1845 ret=pthread_join(io_thread,NULL);
1848 /* prepare and start thread */
1849 if(moldyn->count!=md_copy.count) {
1853 memcpy(&md_copy,moldyn,sizeof(t_moldyn));
1855 atom_copy=malloc(moldyn->count*sizeof(t_atom));
1856 if(atom_copy==NULL) {
1857 perror("[moldyn] malloc atom copy (change)");
1861 md_copy.atom=atom_copy;
1862 memcpy(atom_copy,moldyn->atom,moldyn->count*sizeof(t_atom));
1864 ret=pthread_create(&io_thread,NULL,visual_atoms,&md_copy);
1866 perror("[moldyn] create visual atoms thread\n");
1870 visual_atoms(moldyn);
1875 /* display progress */
1877 /* get current time */
1878 gettimeofday(&t2,NULL);
1880 printf("sched:%d, steps:%d/%d, T:%4.1f/%4.1f P:%4.1f/%4.1f V:%6.1f (%d)\n",
1881 sched->count,i,moldyn->total_steps,
1882 moldyn->t,moldyn->t_avg,
1883 moldyn->p/BAR,moldyn->p_avg/BAR,
1884 //moldyn->p/BAR,(moldyn->p-2.0*moldyn->ekin/(3.0*moldyn->volume))/BAR,
1886 (int)(t2.tv_sec-t1.tv_sec));
1890 /* copy over time */
1894 /* increase absolute time */
1895 moldyn->time+=moldyn->tau;
1896 moldyn->total_steps+=1;
1900 /* check for hooks */
1902 printf("\n ## schedule hook %d start ##\n",
1904 sched->hook(moldyn,sched->hook_params);
1905 printf(" ## schedule hook end ##\n");
1908 /* increase the schedule counter */
1916 /* velocity verlet */
1918 int velocity_verlet(t_moldyn *moldyn) {
1921 double tau,tau_square,h;
1926 count=moldyn->count;
1928 tau_square=moldyn->tau_square;
1930 for(i=0;i<count;i++) {
1931 /* check whether fixed atom */
1932 if(atom[i].attr&ATOM_ATTR_FP)
1936 v3_scale(&delta,&(atom[i].v),tau);
1937 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1938 v3_scale(&delta,&(atom[i].f),h*tau_square);
1939 v3_add(&(atom[i].r),&(atom[i].r),&delta);
1940 check_per_bound(moldyn,&(atom[i].r));
1942 /* velocities [actually v(t+tau/2)] */
1943 v3_scale(&delta,&(atom[i].f),h*tau);
1944 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1947 /* criticial check */
1948 moldyn_bc_check(moldyn);
1950 /* neighbour list update */
1951 link_cell_update(moldyn);
1953 /* forces depending on chosen potential */
1955 potential_force_calc(moldyn);
1957 albe_potential_force_calc(moldyn);
1960 for(i=0;i<count;i++) {
1961 /* check whether fixed atom */
1962 if(atom[i].attr&ATOM_ATTR_FP)
1964 /* again velocities [actually v(t+tau)] */
1965 v3_scale(&delta,&(atom[i].f),0.5*tau/atom[i].mass);
1966 v3_add(&(atom[i].v),&(atom[i].v),&delta);
1975 * potentials & corresponding forces & virial routine
1979 /* generic potential and force calculation */
1981 int potential_force_calc(t_moldyn *moldyn) {
1984 t_atom *itom,*jtom,*ktom;
1988 int *neighbour_i[27];
1992 int neighbour_i[27];
1995 t_list neighbour_i[27];
1996 t_list neighbour_i2[27];
2002 count=moldyn->count;
2012 /* reset global virial */
2013 memset(&(moldyn->gvir),0,sizeof(t_virial));
2015 /* reset force, site energy and virial of every atom */
2017 i=omp_get_thread_num();
2018 #pragma omp parallel for private(virial)
2020 for(i=0;i<count;i++) {
2023 v3_zero(&(itom[i].f));
2026 virial=(&(itom[i].virial));
2034 /* reset site energy */
2039 /* get energy, force and virial of every atom */
2041 /* first (and only) loop over atoms i */
2042 for(i=0;i<count;i++) {
2044 /* single particle potential/force */
2045 if(itom[i].attr&ATOM_ATTR_1BP)
2047 moldyn->func1b(moldyn,&(itom[i]));
2049 if(!(itom[i].attr&(ATOM_ATTR_2BP|ATOM_ATTR_3BP)))
2052 /* 2 body pair potential/force */
2054 link_cell_neighbour_index(moldyn,
2055 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2056 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
2057 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
2062 /* first loop over atoms j */
2063 if(moldyn->func2b) {
2070 while(neighbour_i[j][p]!=-1) {
2072 jtom=&(atom[neighbour_i[j][p]]);
2080 p=lc->subcell->list[p];
2082 this=&(neighbour_i[j]);
2085 if(this->start==NULL)
2089 jtom=this->current->data;
2092 if(jtom==&(itom[i]))
2095 if((jtom->attr&ATOM_ATTR_2BP)&
2096 (itom[i].attr&ATOM_ATTR_2BP)) {
2097 moldyn->func2b(moldyn,
2107 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2113 /* 3 body potential/force */
2115 if(!(itom[i].attr&ATOM_ATTR_3BP))
2118 /* copy the neighbour lists */
2120 /* no copy needed for static lists */
2122 /* no copy needed for lowmem lists */
2124 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
2127 /* second loop over atoms j */
2134 while(neighbour_i[j][p]!=-1) {
2136 jtom=&(atom[neighbour_i[j][p]]);
2144 p=lc->subcell->list[p];
2146 this=&(neighbour_i[j]);
2149 if(this->start==NULL)
2154 jtom=this->current->data;
2157 if(jtom==&(itom[i]))
2160 if(!(jtom->attr&ATOM_ATTR_3BP))
2166 if(moldyn->func3b_j1)
2167 moldyn->func3b_j1(moldyn,
2172 /* in first j loop, 3bp run can be skipped */
2173 if(!(moldyn->run3bp))
2176 /* first loop over atoms k */
2177 if(moldyn->func3b_k1) {
2185 while(neighbour_i[k][q]!=-1) {
2187 ktom=&(atom[neighbour_i[k][q]]);
2195 q=lc->subcell->list[q];
2197 that=&(neighbour_i2[k]);
2200 if(that->start==NULL)
2204 ktom=that->current->data;
2207 if(!(ktom->attr&ATOM_ATTR_3BP))
2213 if(ktom==&(itom[i]))
2216 moldyn->func3b_k1(moldyn,
2227 } while(list_next_f(that)!=\
2235 if(moldyn->func3b_j2)
2236 moldyn->func3b_j2(moldyn,
2241 /* second loop over atoms k */
2242 if(moldyn->func3b_k2) {
2250 while(neighbour_i[k][q]!=-1) {
2252 ktom=&(atom[neighbour_i[k][q]]);
2260 q=lc->subcell->list[q];
2262 that=&(neighbour_i2[k]);
2265 if(that->start==NULL)
2269 ktom=that->current->data;
2272 if(!(ktom->attr&ATOM_ATTR_3BP))
2278 if(ktom==&(itom[i]))
2281 moldyn->func3b_k2(moldyn,
2292 } while(list_next_f(that)!=\
2300 /* 2bp post function */
2301 if(moldyn->func3b_j3) {
2302 moldyn->func3b_j3(moldyn,
2311 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2326 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
2327 if(moldyn->time>DSTART&&moldyn->time<DEND) {
2329 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
2330 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
2331 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
2335 /* some postprocessing */
2337 #pragma omp parallel for
2339 for(i=0;i<count;i++) {
2340 /* calculate global virial */
2341 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
2342 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
2343 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
2344 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
2345 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
2346 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
2348 /* check forces regarding the given timestep */
2349 if(v3_norm(&(itom[i].f))>\
2350 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
2351 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",
2359 * virial calculation
2362 //inline int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2363 int virial_calc(t_atom *a,t_3dvec *f,t_3dvec *d) {
2365 a->virial.xx+=f->x*d->x;
2366 a->virial.yy+=f->y*d->y;
2367 a->virial.zz+=f->z*d->z;
2368 a->virial.xy+=f->x*d->y;
2369 a->virial.xz+=f->x*d->z;
2370 a->virial.yz+=f->y*d->z;
2376 * periodic boundary checking
2379 //inline int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2380 int check_per_bound(t_moldyn *moldyn,t_3dvec *a) {
2391 if(moldyn->status&MOLDYN_STAT_PBX) {
2392 if(a->x>=x) a->x-=dim->x;
2393 else if(-a->x>x) a->x+=dim->x;
2395 if(moldyn->status&MOLDYN_STAT_PBY) {
2396 if(a->y>=y) a->y-=dim->y;
2397 else if(-a->y>y) a->y+=dim->y;
2399 if(moldyn->status&MOLDYN_STAT_PBZ) {
2400 if(a->z>=z) a->z-=dim->z;
2401 else if(-a->z>z) a->z+=dim->z;
2408 * debugging / critical check functions
2411 int moldyn_bc_check(t_moldyn *moldyn) {
2424 for(i=0;i<moldyn->count;i++) {
2425 if(atom[i].r.x>=dim->x/2||-atom[i].r.x>dim->x/2) {
2426 printf("FATAL: atom %d: x: %.20f (%.20f)\n",
2427 i,atom[i].r.x,dim->x/2);
2428 printf("diagnostic:\n");
2429 printf("-----------\natom.r.x:\n");
2431 memcpy(&byte,(u8 *)(&(atom[i].r.x))+j,1);
2434 ((byte)&(1<<k))?1:0,
2437 printf("---------------\nx=dim.x/2:\n");
2439 memcpy(&byte,(u8 *)(&x)+j,1);
2442 ((byte)&(1<<k))?1:0,
2445 if(atom[i].r.x==x) printf("the same!\n");
2446 else printf("different!\n");
2448 if(atom[i].r.y>=dim->y/2||-atom[i].r.y>dim->y/2)
2449 printf("FATAL: atom %d: y: %.20f (%.20f)\n",
2450 i,atom[i].r.y,dim->y/2);
2451 if(atom[i].r.z>=dim->z/2||-atom[i].r.z>dim->z/2)
2452 printf("FATAL: atom %d: z: %.20f (%.20f)\n",
2453 i,atom[i].r.z,dim->z/2);
2463 int moldyn_read_save_file(t_moldyn *moldyn,char *file) {
2470 fd=open(file,O_RDONLY);
2472 perror("[moldyn] load save file open");
2476 fsize=lseek(fd,0,SEEK_END);
2477 lseek(fd,0,SEEK_SET);
2479 size=sizeof(t_moldyn);
2482 cnt=read(fd,moldyn,size);
2484 perror("[moldyn] load save file read (moldyn)");
2490 size=moldyn->count*sizeof(t_atom);
2492 /* correcting possible atom data offset */
2494 if(fsize!=sizeof(t_moldyn)+size) {
2495 corr=fsize-sizeof(t_moldyn)-size;
2496 printf("[moldyn] WARNING: lsf (illegal file size)\n");
2497 printf(" moifying offset:\n");
2498 printf(" - current pos: %d\n",sizeof(t_moldyn));
2499 printf(" - atom size: %d\n",size);
2500 printf(" - file size: %d\n",fsize);
2501 printf(" => correction: %d\n",corr);
2502 lseek(fd,corr,SEEK_CUR);
2505 moldyn->atom=(t_atom *)malloc(size);
2506 if(moldyn->atom==NULL) {
2507 perror("[moldyn] load save file malloc (atoms)");
2512 cnt=read(fd,moldyn->atom,size);
2514 perror("[moldyn] load save file read (atoms)");
2525 int moldyn_free_save_file(t_moldyn *moldyn) {
2532 int moldyn_load(t_moldyn *moldyn) {
2540 * function to find/callback all combinations of 2 body bonds
2543 int process_2b_bonds(t_moldyn *moldyn,void *data,
2544 int (*process)(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2545 void *data,u8 bc)) {
2555 t_list neighbour[27];
2565 for(i=0;i<moldyn->count;i++) {
2566 /* neighbour indexing */
2567 link_cell_neighbour_index(moldyn,
2568 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
2569 (itom[i].r.y+moldyn->dim.y/2)/lc->x,
2570 (itom[i].r.z+moldyn->dim.z/2)/lc->x,
2575 bc=(j<lc->dnlc)?0:1;
2580 while(neighbour[j][p]!=-1) {
2582 jtom=&(moldyn->atom[neighbour[j][p]]);
2590 p=lc->subcell->list[p];
2592 this=&(neighbour[j]);
2595 if(this->start==NULL)
2600 jtom=this->current->data;
2604 process(moldyn,&(itom[i]),jtom,data,bc);
2611 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2621 * function to find neighboured atoms
2624 int process_neighbours(t_moldyn *moldyn,void *data,t_atom *atom,
2625 int (*process)(t_moldyn *moldyn,t_atom *atom,t_atom *natom,
2626 void *data,u8 bc)) {
2636 t_list neighbour[27];
2645 /* neighbour indexing */
2646 link_cell_neighbour_index(moldyn,
2647 (atom->r.x+moldyn->dim.x/2)/lc->x,
2648 (atom->r.y+moldyn->dim.y/2)/lc->x,
2649 (atom->r.z+moldyn->dim.z/2)/lc->x,
2654 bc=(j<lc->dnlc)?0:1;
2659 while(neighbour[j][p]!=-1) {
2661 natom=&(moldyn->atom[neighbour[j][p]]);
2668 natom=&(moldyn->atom[p]);
2669 p=lc->subcell->list[p];
2671 this=&(neighbour[j]);
2674 if(this->start==NULL)
2679 natom=this->current->data;
2683 process(moldyn,atom,natom,data,bc);
2690 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
2699 * post processing functions
2702 int get_line(int fd,char *line,int max) {
2709 if(count==max) return count;
2710 ret=read(fd,line+count,1);
2711 if(ret<=0) return ret;
2712 if(line[count]=='\n') {
2713 memset(line+count,0,max-count-1);
2721 int pair_correlation_init(t_moldyn *moldyn,double dr) {
2727 int calculate_diffusion_coefficient(t_moldyn *moldyn,double *dc) {
2743 for(i=0;i<moldyn->count;i++) {
2745 v3_sub(&dist,&(atom[i].r),&(atom[i].r_0));
2746 check_per_bound(moldyn,&dist);
2747 d2=v3_absolute_square(&dist);
2761 dc[0]*=(1.0/(6.0*moldyn->time*a_cnt));
2762 dc[1]*=(1.0/(6.0*moldyn->time*b_cnt));
2763 dc[2]*=(1.0/(6.0*moldyn->time*moldyn->count));
2768 int bonding_analyze(t_moldyn *moldyn,double *cnt) {
2773 int calculate_pair_correlation_process(t_moldyn *moldyn,t_atom *itom,
2774 t_atom *jtom,void *data,u8 bc) {
2781 /* only count pairs once,
2782 * skip same atoms */
2783 if(itom->tag>=jtom->tag)
2787 * pair correlation calc
2794 v3_sub(&dist,&(jtom->r),&(itom->r));
2795 if(bc) check_per_bound(moldyn,&dist);
2796 d=v3_absolute_square(&dist);
2798 /* ignore if greater cutoff */
2799 if(d>moldyn->cutoff_square)
2802 /* fill the slots */
2806 /* should never happen but it does 8) -
2807 * related to -ffloat-store problem! */
2809 printf("[moldyn] WARNING: pcc (%d/%d)",
2815 if(itom->brand!=jtom->brand) {
2820 /* type a - type a bonds */
2822 pcc->stat[s+pcc->o1]+=1;
2824 /* type b - type b bonds */
2825 pcc->stat[s+pcc->o2]+=1;
2831 int calculate_pair_correlation(t_moldyn *moldyn,double dr,void *ptr) {
2838 pcc.o1=moldyn->cutoff/dr;
2841 if(pcc.o1*dr<=moldyn->cutoff)
2842 printf("[moldyn] WARNING: pcc (low #slots)\n");
2844 printf("[moldyn] pair correlation calc info:\n");
2845 printf(" time: %f\n",moldyn->time);
2846 printf(" count: %d\n",moldyn->count);
2847 printf(" cutoff: %f\n",moldyn->cutoff);
2848 printf(" temperature: cur=%f avg=%f\n",moldyn->t,moldyn->t_avg);
2851 pcc.stat=(double *)ptr;
2854 pcc.stat=(double *)malloc(3*pcc.o1*sizeof(double));
2855 if(pcc.stat==NULL) {
2856 perror("[moldyn] pair correlation malloc");
2861 memset(pcc.stat,0,3*pcc.o1*sizeof(double));
2864 process_2b_bonds(moldyn,&pcc,calculate_pair_correlation_process);
2867 for(i=1;i<pcc.o1;i++) {
2868 // normalization: 4 pi r^2 dr
2869 // here: not double counting pairs -> 2 pi r r dr
2870 // ... and actually it's a constant times r^2
2873 pcc.stat[pcc.o1+i]/=norm;
2874 pcc.stat[pcc.o2+i]/=norm;
2879 /* todo: store/print pair correlation function */
2886 int bond_analyze_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
2893 if(itom->tag>=jtom->tag)
2897 v3_sub(&dist,&(jtom->r),&(itom->r));
2898 if(bc) check_per_bound(moldyn,&dist);
2899 d=v3_absolute_square(&dist);
2901 /* ignore if greater or equal cutoff */
2902 if(d>moldyn->cutoff_square)
2905 /* check for potential bond */
2906 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
2909 /* now count this bonding ... */
2912 /* increase total bond counter
2913 * ... double counting!
2918 ba->acnt[jtom->tag]+=1;
2920 ba->bcnt[jtom->tag]+=1;
2923 ba->acnt[itom->tag]+=1;
2925 ba->bcnt[itom->tag]+=1;
2930 int bond_analyze(t_moldyn *moldyn,double *quality) {
2932 // by now: # bonds of type 'a-4b' and 'b-4a' / # bonds total
2940 ba.acnt=malloc(moldyn->count*sizeof(int));
2942 perror("[moldyn] bond analyze malloc (a)");
2945 memset(ba.acnt,0,moldyn->count*sizeof(int));
2947 ba.bcnt=malloc(moldyn->count*sizeof(int));
2949 perror("[moldyn] bond analyze malloc (b)");
2952 memset(ba.bcnt,0,moldyn->count*sizeof(int));
2961 process_2b_bonds(moldyn,&ba,bond_analyze_process);
2963 for(i=0;i<moldyn->count;i++) {
2964 if(atom[i].brand==0) {
2965 if((ba.acnt[i]==0)&(ba.bcnt[i]==4))
2969 if((ba.acnt[i]==4)&(ba.bcnt[i]==0)) {
2977 printf("[moldyn] bond analyze: c_cnt=%d | set=%d\n",ccnt,cset);
2978 printf("[moldyn] bond analyze: q_cnt=%d | tot=%d\n",qcnt,ba.tcnt);
2981 quality[0]=1.0*ccnt/cset;
2982 quality[1]=1.0*qcnt/ba.tcnt;
2985 printf("[moldyn] bond analyze: c_bnd_q=%f\n",1.0*qcnt/ba.tcnt);
2986 printf("[moldyn] bond analyze: tot_q=%f\n",1.0*qcnt/ba.tcnt);
2993 * visualization code
2996 int visual_init(t_moldyn *moldyn,char *filebase) {
2998 strncpy(moldyn->vis.fb,filebase,128);
3003 int visual_bonds_process(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,
3010 if(itom->tag>=jtom->tag)
3013 if(moldyn->check_2b_bond(moldyn,itom,jtom,bc)==FALSE)
3016 if((itom->attr&ATOM_ATTR_VB)|(jtom->attr&ATOM_ATTR_VB))
3017 dprintf(vb->fd,"# [B] %f %f %f %f %f %f\n",
3018 itom->r.x,itom->r.y,itom->r.z,
3019 jtom->r.x,jtom->r.y,jtom->r.z);
3024 #ifdef VISUAL_THREAD
3025 void *visual_atoms(void *ptr) {
3027 int visual_atoms(t_moldyn *moldyn) {
3037 #ifdef VISUAL_THREAD
3051 sprintf(file,"%s/atomic_conf_%08.f.xyz",v->fb,moldyn->time);
3052 vb.fd=open(file,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
3054 perror("open visual save file fd");
3055 #ifndef VISUAL_THREAD
3060 /* write the actual data file */
3063 dprintf(vb.fd,"# [P] %d %08.f <%f,%f,%f>\n",
3064 moldyn->count,moldyn->time,help/40.0,help/40.0,-0.8*help);
3066 // atomic configuration
3067 for(i=0;i<moldyn->count;i++)
3068 // atom type, positions, color and kinetic energy
3069 dprintf(vb.fd,"%s %f %f %f %s %f\n",pse_name[atom[i].element],
3073 pse_col[atom[i].element],
3076 // bonds between atoms
3077 #ifndef VISUAL_THREAD
3078 process_2b_bonds(moldyn,&vb,visual_bonds_process);
3083 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3084 -dim.x/2,-dim.y/2,-dim.z/2,
3085 dim.x/2,-dim.y/2,-dim.z/2);
3086 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3087 -dim.x/2,-dim.y/2,-dim.z/2,
3088 -dim.x/2,dim.y/2,-dim.z/2);
3089 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3090 dim.x/2,dim.y/2,-dim.z/2,
3091 dim.x/2,-dim.y/2,-dim.z/2);
3092 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3093 -dim.x/2,dim.y/2,-dim.z/2,
3094 dim.x/2,dim.y/2,-dim.z/2);
3096 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3097 -dim.x/2,-dim.y/2,dim.z/2,
3098 dim.x/2,-dim.y/2,dim.z/2);
3099 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3100 -dim.x/2,-dim.y/2,dim.z/2,
3101 -dim.x/2,dim.y/2,dim.z/2);
3102 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3103 dim.x/2,dim.y/2,dim.z/2,
3104 dim.x/2,-dim.y/2,dim.z/2);
3105 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3106 -dim.x/2,dim.y/2,dim.z/2,
3107 dim.x/2,dim.y/2,dim.z/2);
3109 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3110 -dim.x/2,-dim.y/2,dim.z/2,
3111 -dim.x/2,-dim.y/2,-dim.z/2);
3112 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3113 -dim.x/2,dim.y/2,dim.z/2,
3114 -dim.x/2,dim.y/2,-dim.z/2);
3115 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3116 dim.x/2,-dim.y/2,dim.z/2,
3117 dim.x/2,-dim.y/2,-dim.z/2);
3118 dprintf(vb.fd,"# [D] %f %f %f %f %f %f\n",
3119 dim.x/2,dim.y/2,dim.z/2,
3120 dim.x/2,dim.y/2,-dim.z/2);
3125 #ifdef VISUAL_THREAD
3136 * fpu cntrol functions
3139 // set rounding to double (eliminates -ffloat-store!)
3140 int fpu_set_rtd(void) {
3146 ctrl&=~_FPU_EXTENDED;