2 * mdrun.c - main code to run a md simulation
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
11 #include "potentials/harmonic_oscillator.h"
12 #include "potentials/lennard_jones.h"
13 #include "potentials/albe.h"
15 #include "potentials/tersoff_orig.h"
17 #include "potentials/tersoff.h"
26 int mdrun_usage(void) {
28 printf("%s usage:\n",ME);
33 int mdrun_parse_argv(t_mdrun *mdrun,int argc,char **argv) {
40 printf("%s unknown switch: %s\n",ME,argv[i]);
46 strncpy(mdrun->cfile,argv[++i],128);
49 strncpy(mdrun->sdir,argv[++i],128);
55 printf("%s unknown option: %s\n",ME,argv[i]);
65 int del_stages(t_mdrun *mdrun) {
78 stage=sl->current->data;
81 } while(list_next_f(sl)!=L_NO_NEXT_ELEMENT);
86 int add_stage(t_mdrun *mdrun,u8 type,void *params) {
93 case STAGE_DISPLACE_ATOM:
94 psize=sizeof(t_displace_atom_params);
96 case STAGE_INSERT_ATOMS:
97 psize=sizeof(t_insert_atoms_params);
100 psize=sizeof(t_continue_params);
103 psize=sizeof(t_anneal_params);
106 psize=sizeof(t_chaattr_params);
109 psize=sizeof(t_chsattr_params);
112 printf("%s unknown stage type: %02x\n",ME,type);
116 stage=malloc(sizeof(t_stage));
118 perror("[mdrun] malloc (add stage)");
123 stage->executed=FALSE;
125 stage->params=malloc(psize);
126 if(stage->params==NULL) {
127 perror("[mdrun] malloc (stage params)");
131 memcpy(stage->params,params,psize);
133 list_add_immediate_f(&(mdrun->stage),stage);
138 int mdrun_parse_config(t_mdrun *mdrun) {
148 t_displace_atom_params dap;
149 t_insert_atoms_params iap;
150 t_continue_params cp;
152 t_chaattr_params cap;
153 t_chsattr_params csp;
155 /* open config file */
156 fd=open(mdrun->cfile,O_RDONLY);
158 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
163 /* read, parse, set */
168 ret=get_line(fd,line,128);
172 // ignore # lines and \n
173 if((line[0]=='#')|(ret==1))
177 memset(&iap,0,sizeof(t_insert_atoms_params));
178 memset(&cp,0,sizeof(t_continue_params));
179 memset(&ap,0,sizeof(t_anneal_params));
180 memset(&cap,0,sizeof(t_chaattr_params));
181 memset(&csp,0,sizeof(t_chsattr_params));
183 // get command + args
187 wptr=strtok(NULL," \t");
189 wptr=strtok(line," \t");
192 strncpy(word[wcnt],wptr,32);
196 if(!strncmp(word[0],"potential",9)) {
197 if(!strncmp(word[1],"albe",4))
198 mdrun->potential=MOLDYN_POTENTIAL_AM;
199 if(!strncmp(word[1],"tersoff",7))
200 mdrun->potential=MOLDYN_POTENTIAL_TM;
201 if(!strncmp(word[1],"ho",2))
202 mdrun->potential=MOLDYN_POTENTIAL_HO;
203 if(!strncmp(word[1],"lj",2))
204 mdrun->potential=MOLDYN_POTENTIAL_LJ;
206 else if(!strncmp(word[0],"cutoff",6))
207 mdrun->cutoff=atof(word[1]);
208 else if(!strncmp(word[0],"nnd",3))
209 mdrun->nnd=atof(word[1]);
210 else if(!strncmp(word[0],"intalgo",7)) {
211 if(!strncmp(word[1],"verlet",5))
212 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
214 else if(!strncmp(word[0],"timestep",8))
215 mdrun->timestep=atof(word[1]);
216 else if(!strncmp(word[0],"volume",6)) {
217 mdrun->dim.x=atof(word[1]);
218 mdrun->dim.y=atof(word[2]);
219 mdrun->dim.z=atof(word[3]);
220 if(strncmp(word[4],"0",1))
223 else if(!strncmp(word[0],"pbc",3)) {
224 if(strncmp(word[1],"0",1))
228 if(strncmp(word[2],"0",1))
232 if(strncmp(word[3],"0",1))
237 else if(!strncmp(word[0],"temperature",11))
238 mdrun->temperature=atof(word[1]);
239 else if(!strncmp(word[0],"pressure",8))
240 mdrun->pressure=atof(word[1]);
241 else if(!strncmp(word[0],"lattice",7)) {
242 if(!strncmp(word[1],"zincblende",10))
243 mdrun->lattice=ZINCBLENDE;
244 if(!strncmp(word[1],"fcc",3))
246 if(!strncmp(word[1],"diamond",7))
247 mdrun->lattice=DIAMOND;
249 else if(!strncmp(word[0],"element1",8)) {
250 mdrun->element1=atoi(word[1]);
251 mdrun->m1=pse_mass[mdrun->element1];
253 else if(!strncmp(word[0],"element2",8)) {
254 mdrun->element2=atoi(word[1]);
255 mdrun->m2=pse_mass[mdrun->element2];
257 else if(!strncmp(word[0],"fill",6)) {
258 // only lc mode by now
259 mdrun->lx=atoi(word[2]);
260 mdrun->ly=atoi(word[3]);
261 mdrun->lz=atoi(word[4]);
262 mdrun->lc=atof(word[5]);
264 else if(!strncmp(word[0],"aattr",5)) {
265 // for aatrib line we need a special stage
266 // containing one schedule of 0 loops ...
267 for(i=0;i<strlen(word[1]);i++) {
270 cap.type|=CHAATTR_TOTALV;
273 cap.type|=CHAATTR_REGION;
276 cap.type|=CHAATTR_ELEMENT;
283 if(cap.type&CHAATTR_REGION) {
284 cap.x0=atof(word[1]);
285 cap.y0=atof(word[2]);
286 cap.z0=atof(word[3]);
287 cap.x1=atof(word[4]);
288 cap.y1=atof(word[5]);
289 cap.z1=atof(word[6]);
292 if(cap.type&CHAATTR_ELEMENT) {
293 cap.element=atoi(word[i]);
296 for(o=0;o<strlen(word[i]);o++) {
299 cap.attr|=ATOM_ATTR_VB;
302 cap.attr|=ATOM_ATTR_HB;
305 cap.attr|=ATOM_ATTR_VA;
308 cap.attr|=ATOM_ATTR_FP;
311 cap.attr|=ATOM_ATTR_1BP;
314 cap.attr|=ATOM_ATTR_2BP;
317 cap.attr|=ATOM_ATTR_3BP;
323 add_stage(mdrun,STAGE_CHAATTR,&cap);
325 else if(!strncmp(word[0],"sattr",5)) {
326 // for satrib line we need a special stage
327 // containing one schedule of 0 loops ...
329 for(i=1;i<wcnt;i++) {
330 if(!strncmp(word[i],"pctrl",5)) {
331 csp.ptau=0.01/(atof(word[++i])*GPA);
332 csp.type|=CHSATTR_PCTRL;
334 if(!strncmp(word[i],"tctrl",5)) {
335 csp.ttau=atof(word[++i]);
336 csp.type|=CHSATTR_TCTRL;
338 if(!strncmp(word[i],"prelax",6)) {
339 csp.dp=atof(word[++i])*BAR;
340 csp.type|=CHSATTR_PRELAX;
342 if(!strncmp(word[i],"trelax",6)) {
343 csp.dt=atof(word[++i]);
344 csp.type|=CHSATTR_TRELAX;
346 if(!strncmp(word[i],"rsteps",6)) {
347 csp.rsteps=atoi(word[++i]);
348 csp.type|=CHSATTR_RSTEPS;
350 if(!strncmp(word[i],"avgrst",6)) {
351 csp.avgrst=atoi(word[++i]);
352 csp.type|=CHSATTR_AVGRST;
355 add_stage(mdrun,STAGE_CHSATTR,&csp);
357 else if(!strncmp(word[0],"prerun",6))
358 mdrun->prerun=atoi(word[1]);
359 else if(!strncmp(word[0],"avgskip",7))
360 mdrun->avgskip=atoi(word[1]);
361 else if(!strncmp(word[0],"elog",4))
362 mdrun->elog=atoi(word[1]);
363 else if(!strncmp(word[0],"tlog",4))
364 mdrun->tlog=atoi(word[1]);
365 else if(!strncmp(word[0],"plog",4))
366 mdrun->plog=atoi(word[1]);
367 else if(!strncmp(word[0],"vlog",4))
368 mdrun->vlog=atoi(word[1]);
369 else if(!strncmp(word[0],"save",4))
370 mdrun->save=atoi(word[1]);
371 else if(!strncmp(word[0],"visualize",9))
372 mdrun->visualize=atoi(word[1]);
373 else if(!strncmp(word[0],"stage",5)) {
374 // for every stage line, add a stage
375 if(!strncmp(word[1],"displace",8)) {
376 dap.nr=atoi(word[2]);
377 dap.dx=atof(word[3]);
378 dap.dy=atof(word[4]);
379 dap.dz=atof(word[5]);
380 add_stage(mdrun,STAGE_DISPLACE_ATOM,&dap);
382 else if(!strncmp(word[1],"ins_atoms",9)) {
383 iap.ins_steps=atoi(word[2]);
384 iap.ins_atoms=atoi(word[3]);
385 iap.element=atoi(word[4]);
386 iap.brand=atoi(word[5]);
387 for(i=0;i<strlen(word[6]);i++) {
390 iap.attr|=ATOM_ATTR_VB;
393 iap.attr|=ATOM_ATTR_HB;
396 iap.attr|=ATOM_ATTR_VA;
399 iap.attr|=ATOM_ATTR_FP;
402 iap.attr|=ATOM_ATTR_1BP;
405 iap.attr|=ATOM_ATTR_2BP;
408 iap.attr|=ATOM_ATTR_3BP;
418 iap.x0=atof(word[8]);
419 iap.y0=atof(word[9]);
420 iap.z0=atof(word[10]);
423 if(word[8][0]=='t') {
425 iap.cr=atof(word[9]);
429 iap.x0=atof(word[8]);
430 iap.y0=atof(word[9]);
431 iap.z0=atof(word[10]);
432 iap.x1=atof(word[11]);
433 iap.y1=atof(word[12]);
434 iap.z1=atof(word[13]);
435 iap.cr=atof(word[14]);
440 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
442 else if(!strncmp(word[1],"continue",8)) {
443 cp.runs=atoi(word[2]);
444 add_stage(mdrun,STAGE_CONTINUE,&cp);
446 else if(!strncmp(word[1],"anneal",6)) {
448 ap.runs=atoi(word[2]);
450 add_stage(mdrun,STAGE_ANNEAL,&ap);
453 printf("%s unknown stage type: %s\n",
459 printf("%s unknown keyword '%s', skipped!\n",
471 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
475 if(!(mdrun->sattr&SATTR_PRELAX)) {
479 delta=moldyn->p_avg-moldyn->p_ref;
490 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
494 if(!(mdrun->sattr&SATTR_TRELAX))
497 delta=moldyn->t_avg-moldyn->t_ref;
508 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
510 t_displace_atom_params *dap;
514 stage=mdrun->stage.current->data;
517 atom=&(moldyn->atom[dap->nr]);
525 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
527 t_insert_atoms_params *iap;
537 stage=mdrun->stage.current->data;
542 v.x=0.0; v.y=0.0; v.z=0.0;
546 switch(mdrun->lattice) {
587 printf("%s unknown insertion mode: %02x\n",
593 while(cnt<iap->ins_atoms) {
596 if(iap->type!=INS_POS) {
597 r.x=rand_get_double(&(moldyn->random))*x;
598 r.y=rand_get_double(&(moldyn->random))*y;
599 r.z=rand_get_double(&(moldyn->random))*z;
610 if(iap->type!=INS_TOTAL) {
617 dmin=1000; // for sure too high!
618 for(i=0;i<moldyn->count;i++) {
619 atom=&(moldyn->atom[i]);
620 v3_sub(&dist,&(atom->r),&r);
621 check_per_bound(moldyn,&dist);
622 d=v3_absolute_square(&dist);
623 if(d<(iap->cr*iap->cr)) {
632 add_atom(moldyn,iap->element,pse_mass[iap->element],
633 iap->brand,iap->attr,&r,&v);
634 printf("%s atom inserted (%d/%d): %f %f %f\n",
635 ME,(iap->cnt_steps+1)*iap->ins_atoms,
636 iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
637 printf(" -> d2 = %f/%f\n",dmin,iap->cr*iap->cr);
644 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
647 t_chaattr_params *cap;
651 stage=mdrun->stage.current->data;
654 for(i=0;i<moldyn->count;i++) {
655 atom=&(moldyn->atom[i]);
656 if(cap->type&CHAATTR_ELEMENT) {
657 if(cap->element!=atom->element)
660 if(cap->type&CHAATTR_REGION) {
661 if(cap->x0<atom->r.x)
663 if(cap->y0<atom->r.y)
665 if(cap->z0<atom->r.z)
667 if(cap->x1>atom->r.x)
669 if(cap->y1>atom->r.y)
671 if(cap->z1>atom->r.z)
674 atom->attr=cap->attr;
680 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
683 t_chsattr_params *csp;
685 stage=mdrun->stage.current->data;
688 if(csp->type&CHSATTR_PCTRL) {
690 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
692 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
694 if(csp->type&CHSATTR_TCTRL) {
696 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
698 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
700 if(csp->type&CHSATTR_PRELAX) {
702 mdrun->sattr&=(~(SATTR_PRELAX));
704 mdrun->sattr|=SATTR_PRELAX;
707 if(csp->type&CHSATTR_TRELAX) {
709 mdrun->sattr&=(~(SATTR_TRELAX));
711 mdrun->sattr|=SATTR_TRELAX;
714 if(csp->type&CHSATTR_AVGRST) {
716 mdrun->sattr|=SATTR_AVGRST;
718 mdrun->sattr&=(~(SATTR_AVGRST));
720 if(csp->type&CHSATTR_RSTEPS) {
721 mdrun->relax_steps=csp->rsteps;
727 #define stage_print(m) if(!(stage->executed)) \
730 int mdrun_hook(void *ptr1,void *ptr2) {
740 t_insert_atoms_params *iap;
741 t_continue_params *cp;
751 /* return immediately if there are no more stage */
752 if(sl->current==NULL)
755 /* get stage description */
756 stage=sl->current->data;
758 /* default steps and tau values */
759 steps=mdrun->relax_steps;
762 /* check whether relaxation steps are necessary */
763 if((check_pressure(moldyn,mdrun)==TRUE)&\
764 (check_temperature(moldyn,mdrun)==TRUE)) {
767 stage_print("\n###########################\n");
768 stage_print("# [mdrun] executing stage #\n");
769 stage_print("###########################\n\n");
771 /* stage specific stuff */
772 switch(stage->type) {
773 case STAGE_DISPLACE_ATOM:
774 stage_print(" -> displace atom\n\n");
775 displace_atom(moldyn,mdrun);
778 case STAGE_INSERT_ATOMS:
779 stage_print(" -> insert atoms\n\n");
781 if(iap->cnt_steps==iap->ins_steps) {
785 insert_atoms(moldyn,mdrun);
789 stage_print(" -> continue\n\n");
790 if(stage->executed==TRUE) {
798 stage_print(" -> anneal\n\n");
800 if(ap->count==ap->runs) {
804 if(moldyn->t_ref+ap->dt>=0.0)
805 set_temperature(moldyn,
806 moldyn->t_ref+ap->dt);
810 stage_print(" -> chaattr\n\n");
811 chaatr(moldyn,mdrun);
815 stage_print(" -> chsattr\n\n");
816 chsattr(moldyn,mdrun);
820 printf("%s unknwon stage type\n",ME);
824 /* mark as executed */
825 stage->executed=TRUE;
828 if(change_stage==TRUE) {
829 printf("%s finished stage\n",ME);
830 if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
831 printf("%s no more stages\n",ME);
842 if(mdrun->sattr&SATTR_AVGRST)
843 average_reset(moldyn);
847 /* continue simulation */
848 moldyn_add_schedule(moldyn,steps,tau);
853 int main(int argc,char **argv) {
860 memset(&mdrun,0,sizeof(t_mdrun));
861 memset(&moldyn,0,sizeof(t_moldyn));
863 /* parse arguments */
864 if(mdrun_parse_argv(&mdrun,argc,argv)<0)
867 /* initialize list system */
868 list_init_f(&(mdrun.stage));
870 /* parse config file */
871 mdrun_parse_config(&mdrun);
873 /* reset the stage list */
874 list_reset_f(&(mdrun.stage));
878 /* prepare simulation */
879 moldyn_init(&moldyn,argc,argv);
880 if(set_int_alg(&moldyn,mdrun.intalgo)<0)
882 set_cutoff(&moldyn,mdrun.cutoff);
883 if(set_potential(&moldyn,mdrun.potential)<0)
885 switch(mdrun.potential) {
886 case MOLDYN_POTENTIAL_AM:
887 albe_mult_set_params(&moldyn,
891 case MOLDYN_POTENTIAL_TM:
892 tersoff_mult_set_params(&moldyn,
896 case MOLDYN_POTENTIAL_HO:
897 harmonic_oscillator_set_params(&moldyn,mdrun.element1);
899 case MOLDYN_POTENTIAL_LJ:
900 lennard_jones_set_params(&moldyn,mdrun.element1);
903 printf("%s unknown potential: %02x\n",
907 set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
908 set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
909 switch(mdrun.lattice) {
911 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
912 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
913 mdrun.ly,mdrun.lz,NULL);
916 create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
917 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
918 mdrun.ly,mdrun.lz,NULL);
921 o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
922 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
923 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
924 mdrun.ly,mdrun.lz,&o);
925 o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
926 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
927 mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
928 mdrun.ly,mdrun.lz,&o);
931 printf("%s unknown lattice type: %02x\n",
935 moldyn_bc_check(&moldyn);
936 set_temperature(&moldyn,mdrun.temperature);
937 set_pressure(&moldyn,mdrun.pressure);
938 thermal_init(&moldyn,TRUE);
939 moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
940 moldyn_set_log_dir(&moldyn,mdrun.sdir);
941 moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
943 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
945 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
947 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
949 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
951 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
953 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
954 moldyn_set_log(&moldyn,CREATE_REPORT,0);
955 set_avg_skip(&moldyn,mdrun.avgskip);
957 /* prepare the hook function */
958 moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
960 /* run the simulation */
961 moldyn_integrate(&moldyn);
964 moldyn_shutdown(&moldyn);
966 list_destroy_f(&(mdrun.stage));