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_INSERT_ATOMS:
94 psize=sizeof(t_insert_atoms_params);
97 psize=sizeof(t_continue_params);
100 psize=sizeof(t_anneal_params);
103 psize=sizeof(t_chaattr_params);
106 psize=sizeof(t_chsattr_params);
109 printf("%s unknown stage type: %02x\n",ME,type);
113 stage=malloc(sizeof(t_stage));
115 perror("[mdrun] malloc (add stage)");
120 stage->executed=FALSE;
122 stage->params=malloc(psize);
123 if(stage->params==NULL) {
124 perror("[mdrun] malloc (stage params)");
128 memcpy(stage->params,params,psize);
130 list_add_immediate_f(&(mdrun->stage),stage);
135 int mdrun_parse_config(t_mdrun *mdrun) {
145 t_insert_atoms_params iap;
146 t_continue_params cp;
148 t_chaattr_params cap;
149 t_chsattr_params csp;
151 /* open config file */
152 fd=open(mdrun->cfile,O_RDONLY);
154 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
159 /* read, parse, set */
164 ret=get_line(fd,line,128);
168 // ignore # lines and \n
169 if((line[0]=='#')|(ret==1))
173 memset(&iap,0,sizeof(t_insert_atoms_params));
174 memset(&cp,0,sizeof(t_continue_params));
175 memset(&ap,0,sizeof(t_anneal_params));
176 memset(&cap,0,sizeof(t_chaattr_params));
177 memset(&csp,0,sizeof(t_chsattr_params));
179 // get command + args
183 wptr=strtok(NULL," \t");
185 wptr=strtok(line," \t");
188 strncpy(word[wcnt],wptr,32);
192 if(!strncmp(word[0],"potential",9)) {
193 if(!strncmp(word[1],"albe",4))
194 mdrun->potential=MOLDYN_POTENTIAL_AM;
195 if(!strncmp(word[1],"tersoff",7))
196 mdrun->potential=MOLDYN_POTENTIAL_TM;
197 if(!strncmp(word[1],"ho",2))
198 mdrun->potential=MOLDYN_POTENTIAL_HO;
199 if(!strncmp(word[1],"lj",2))
200 mdrun->potential=MOLDYN_POTENTIAL_LJ;
202 else if(!strncmp(word[0],"cutoff",6))
203 mdrun->cutoff=atof(word[1]);
204 else if(!strncmp(word[0],"nnd",3))
205 mdrun->nnd=atof(word[1]);
206 else if(!strncmp(word[0],"intalgo",7)) {
207 if(!strncmp(word[1],"verlet",5))
208 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
210 else if(!strncmp(word[0],"timestep",8))
211 mdrun->timestep=atof(word[1]);
212 else if(!strncmp(word[0],"volume",6)) {
213 mdrun->dim.x=atof(word[1]);
214 mdrun->dim.y=atof(word[2]);
215 mdrun->dim.z=atof(word[3]);
216 if(strncmp(word[4],"0",1))
219 else if(!strncmp(word[0],"pbc",3)) {
220 if(strncmp(word[1],"0",1))
224 if(strncmp(word[2],"0",1))
228 if(strncmp(word[3],"0",1))
233 else if(!strncmp(word[0],"temperature",11))
234 mdrun->temperature=atof(word[1]);
235 else if(!strncmp(word[0],"pressure",8))
236 mdrun->pressure=atof(word[1]);
237 else if(!strncmp(word[0],"lattice",7)) {
238 if(!strncmp(word[1],"zincblende",10))
239 mdrun->lattice=ZINCBLENDE;
240 if(!strncmp(word[1],"fcc",3))
242 if(!strncmp(word[1],"diamond",7))
243 mdrun->lattice=DIAMOND;
245 else if(!strncmp(word[0],"element1",8)) {
246 mdrun->element1=atoi(word[1]);
247 mdrun->m1=pse_mass[mdrun->element1];
249 else if(!strncmp(word[0],"element2",8)) {
250 mdrun->element2=atoi(word[1]);
251 mdrun->m2=pse_mass[mdrun->element2];
253 else if(!strncmp(word[0],"fill",6)) {
254 // only lc mode by now
255 mdrun->lx=atoi(word[2]);
256 mdrun->ly=atoi(word[3]);
257 mdrun->lz=atoi(word[4]);
258 mdrun->lc=atof(word[5]);
260 else if(!strncmp(word[0],"aattr",5)) {
261 // for aatrib line we need a special stage
262 // containing one schedule of 0 loops ...
263 for(i=0;i<strlen(word[1]);i++) {
266 cap.type|=CHAATTR_TOTALV;
269 cap.type|=CHAATTR_REGION;
272 cap.type|=CHAATTR_ELEMENT;
279 if(cap.type&CHAATTR_REGION) {
280 cap.x0=atof(word[1]);
281 cap.y0=atof(word[2]);
282 cap.z0=atof(word[3]);
283 cap.x1=atof(word[4]);
284 cap.y1=atof(word[5]);
285 cap.z1=atof(word[6]);
288 if(cap.type&CHAATTR_ELEMENT) {
289 cap.element=atoi(word[i]);
292 for(o=0;o<strlen(word[i]);o++) {
295 cap.attr|=ATOM_ATTR_VB;
298 cap.attr|=ATOM_ATTR_HB;
301 cap.attr|=ATOM_ATTR_VA;
304 cap.attr|=ATOM_ATTR_FP;
307 cap.attr|=ATOM_ATTR_1BP;
310 cap.attr|=ATOM_ATTR_2BP;
313 cap.attr|=ATOM_ATTR_3BP;
319 add_stage(mdrun,STAGE_CHAATTR,&cap);
321 else if(!strncmp(word[0],"sattr",5)) {
322 // for satrib line we need a special stage
323 // containing one schedule of 0 loops ...
325 for(i=1;i<wcnt;i++) {
326 if(!strncmp(word[i],"pctrl",5)) {
327 csp.ptau=0.01/(atof(word[++i])*GPA);
328 csp.type|=CHSATTR_PCTRL;
330 if(!strncmp(word[i],"tctrl",5)) {
331 csp.ttau=atof(word[++i]);
332 csp.type|=CHSATTR_TCTRL;
334 if(!strncmp(word[i],"prelax",6)) {
335 csp.dp=atof(word[++i])*BAR;
336 csp.type|=CHSATTR_PRELAX;
338 if(!strncmp(word[i],"trelax",6)) {
339 csp.dt=atof(word[++i]);
340 csp.type|=CHSATTR_TRELAX;
342 if(!strncmp(word[i],"rsteps",6)) {
343 csp.rsteps=atoi(word[++i]);
344 csp.type|=CHSATTR_RSTEPS;
346 if(!strncmp(word[i],"avgrst",6)) {
347 csp.avgrst=atoi(word[++i]);
348 csp.type|=CHSATTR_AVGRST;
351 add_stage(mdrun,STAGE_CHSATTR,&csp);
353 else if(!strncmp(word[0],"prerun",6))
354 mdrun->prerun=atoi(word[1]);
355 else if(!strncmp(word[0],"avgskip",7))
356 mdrun->avgskip=atoi(word[1]);
357 else if(!strncmp(word[0],"elog",4))
358 mdrun->elog=atoi(word[1]);
359 else if(!strncmp(word[0],"tlog",4))
360 mdrun->tlog=atoi(word[1]);
361 else if(!strncmp(word[0],"plog",4))
362 mdrun->plog=atoi(word[1]);
363 else if(!strncmp(word[0],"vlog",4))
364 mdrun->vlog=atoi(word[1]);
365 else if(!strncmp(word[0],"save",4))
366 mdrun->save=atoi(word[1]);
367 else if(!strncmp(word[0],"visualize",9))
368 mdrun->visualize=atoi(word[1]);
369 else if(!strncmp(word[0],"stage",5)) {
370 // for every stage line, add a stage
371 if(!strncmp(word[1],"ins_atoms",9)) {
372 iap.ins_steps=atoi(word[2]);
373 iap.ins_atoms=atoi(word[3]);
374 iap.element=atoi(word[4]);
375 iap.element=atoi(word[4]);
376 iap.brand=atoi(word[5]);
377 for(i=0;i<strlen(word[6]);i++) {
380 iap.attr|=ATOM_ATTR_VB;
383 iap.attr|=ATOM_ATTR_HB;
386 iap.attr|=ATOM_ATTR_VA;
389 iap.attr|=ATOM_ATTR_FP;
392 iap.attr|=ATOM_ATTR_1BP;
395 iap.attr|=ATOM_ATTR_2BP;
398 iap.attr|=ATOM_ATTR_3BP;
404 // only rand mode by now
405 if(word[8][0]=='t') {
407 iap.cr=atof(word[9]);
411 iap.x0=atof(word[8]);
412 iap.y0=atof(word[9]);
413 iap.z0=atof(word[10]);
414 iap.x1=atof(word[11]);
415 iap.y1=atof(word[12]);
416 iap.z1=atof(word[13]);
417 iap.cr=atof(word[14]);
419 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
421 else if(!strncmp(word[1],"continue",8)) {
422 cp.runs=atoi(word[2]);
423 add_stage(mdrun,STAGE_CONTINUE,&cp);
425 else if(!strncmp(word[1],"anneal",6)) {
427 ap.runs=atoi(word[2]);
429 add_stage(mdrun,STAGE_ANNEAL,&ap);
432 printf("%s unknown stage type: %s\n",
438 printf("%s unknown keyword '%s', skipped!\n",
450 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
454 if(!(mdrun->sattr&SATTR_PRELAX)) {
458 delta=moldyn->p_avg-moldyn->p_ref;
469 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
473 if(!(mdrun->sattr&SATTR_TRELAX))
476 delta=moldyn->t_avg-moldyn->t_ref;
487 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
489 t_insert_atoms_params *iap;
499 stage=mdrun->stage.current->data;
504 v.x=0.0; v.y=0.0; v.z=0.0;
526 printf("%s unknown insertion mode: %02x\n",
532 while(cnt<iap->ins_atoms) {
535 r.x=(rand_get_double(&(moldyn->random))-0.5)*x+x0;
536 r.y=(rand_get_double(&(moldyn->random))-0.5)*y+y0;
537 r.z=(rand_get_double(&(moldyn->random))-0.5)*z+z0;
540 dmin=1000; // for sure too high!
541 for(i=0;i<moldyn->count;i++) {
542 atom=&(moldyn->atom[i]);
543 v3_sub(&dist,&(atom->r),&r);
544 check_per_bound(moldyn,&dist);
545 d=v3_absolute_square(&dist);
546 if(d<(iap->cr*iap->cr)) {
555 add_atom(moldyn,iap->element,pse_mass[iap->element],
556 iap->brand,iap->attr,&r,&v);
557 printf("%s atom inserted (%d/%d): %f %f %f\n",
558 ME,(iap->cnt_steps+1)*iap->ins_atoms,
559 iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
560 printf(" -> d2 = %f/%f\n",dmin,iap->cr*iap->cr);
567 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
570 t_chaattr_params *cap;
574 stage=mdrun->stage.current->data;
577 for(i=0;i<moldyn->count;i++) {
578 atom=&(moldyn->atom[i]);
579 if(cap->type&CHAATTR_ELEMENT) {
580 if(cap->element!=atom->element)
583 if(cap->type&CHAATTR_REGION) {
584 if(cap->x0<atom->r.x)
586 if(cap->y0<atom->r.y)
588 if(cap->z0<atom->r.z)
590 if(cap->x1>atom->r.x)
592 if(cap->y1>atom->r.y)
594 if(cap->z1>atom->r.z)
597 atom->attr=cap->attr;
603 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
606 t_chsattr_params *csp;
608 stage=mdrun->stage.current->data;
611 if(csp->type&CHSATTR_PCTRL) {
613 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
615 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
617 if(csp->type&CHSATTR_TCTRL) {
619 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
621 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
623 if(csp->type&CHSATTR_PRELAX) {
625 mdrun->sattr&=(~(SATTR_PRELAX));
627 mdrun->sattr|=SATTR_PRELAX;
630 if(csp->type&CHSATTR_TRELAX) {
632 mdrun->sattr&=(~(SATTR_TRELAX));
634 mdrun->sattr|=SATTR_TRELAX;
637 if(csp->type&CHSATTR_AVGRST) {
639 mdrun->sattr|=SATTR_AVGRST;
641 mdrun->sattr&=(~(SATTR_AVGRST));
643 if(csp->type&CHSATTR_RSTEPS) {
644 mdrun->relax_steps=csp->rsteps;
650 #define stage_print(m) if(!(stage->executed)) \
653 int mdrun_hook(void *ptr1,void *ptr2) {
663 t_insert_atoms_params *iap;
664 t_continue_params *cp;
674 /* return immediately if there are no more stage */
675 if(sl->current==NULL)
678 /* get stage description */
679 stage=sl->current->data;
681 /* default steps and tau values */
682 steps=mdrun->relax_steps;
685 /* check whether relaxation steps are necessary */
686 if((check_pressure(moldyn,mdrun)==TRUE)&\
687 (check_temperature(moldyn,mdrun)==TRUE)) {
690 stage_print("\n###########################\n");
691 stage_print("# [mdrun] executing stage #\n");
692 stage_print("###########################\n\n");
694 /* stage specific stuff */
695 switch(stage->type) {
696 case STAGE_INSERT_ATOMS:
697 stage_print(" -> insert atoms\n\n");
699 if(iap->cnt_steps==iap->ins_steps) {
703 insert_atoms(moldyn,mdrun);
707 stage_print(" -> continue\n\n");
708 if(stage->executed==TRUE) {
716 stage_print(" -> anneal\n\n");
718 if(ap->count==ap->runs) {
722 set_temperature(moldyn,moldyn->t_ref+ap->dt);
726 stage_print(" -> chaattr\n\n");
727 chaatr(moldyn,mdrun);
731 stage_print(" -> chsattr\n\n");
732 chsattr(moldyn,mdrun);
736 printf("%s unknwon stage type\n",ME);
740 /* mark as executed */
741 stage->executed=TRUE;
744 if(change_stage==TRUE) {
745 printf("%s finished stage\n",ME);
746 if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
747 printf("%s no more stages\n",ME);
758 if(mdrun->sattr&SATTR_AVGRST)
759 average_reset(moldyn);
763 /* continue simulation */
764 moldyn_add_schedule(moldyn,steps,tau);
769 int main(int argc,char **argv) {
776 memset(&mdrun,0,sizeof(t_mdrun));
777 memset(&moldyn,0,sizeof(t_moldyn));
779 /* parse arguments */
780 if(mdrun_parse_argv(&mdrun,argc,argv)<0)
783 /* initialize list system */
784 list_init_f(&(mdrun.stage));
786 /* parse config file */
787 mdrun_parse_config(&mdrun);
789 /* reset the stage list */
790 list_reset_f(&(mdrun.stage));
794 /* prepare simulation */
795 moldyn_init(&moldyn,argc,argv);
796 if(set_int_alg(&moldyn,mdrun.intalgo)<0)
798 set_cutoff(&moldyn,mdrun.cutoff);
799 if(set_potential(&moldyn,mdrun.potential)<0)
801 switch(mdrun.potential) {
802 case MOLDYN_POTENTIAL_AM:
803 albe_mult_set_params(&moldyn,
807 case MOLDYN_POTENTIAL_TM:
808 tersoff_mult_set_params(&moldyn,
812 case MOLDYN_POTENTIAL_HO:
813 harmonic_oscillator_set_params(&moldyn,mdrun.element1);
815 case MOLDYN_POTENTIAL_LJ:
816 lennard_jones_set_params(&moldyn,mdrun.element1);
819 printf("%s unknown potential: %02x\n",
823 set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
824 set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
825 switch(mdrun.lattice) {
827 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
828 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
829 mdrun.ly,mdrun.lz,NULL);
832 create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
833 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
834 mdrun.ly,mdrun.lz,NULL);
837 o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
838 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
839 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
840 mdrun.ly,mdrun.lz,&o);
841 o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
842 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
843 mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
844 mdrun.ly,mdrun.lz,&o);
847 printf("%s unknown lattice type: %02x\n",
851 moldyn_bc_check(&moldyn);
852 set_temperature(&moldyn,mdrun.temperature);
853 set_pressure(&moldyn,mdrun.pressure);
854 thermal_init(&moldyn,TRUE);
855 moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
856 moldyn_set_log_dir(&moldyn,mdrun.sdir);
857 moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
859 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
861 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
863 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
865 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
867 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
869 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
870 moldyn_set_log(&moldyn,CREATE_REPORT,0);
871 set_avg_skip(&moldyn,mdrun.avgskip);
873 /* prepare the hook function */
874 moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
876 /* run the simulation */
877 moldyn_integrate(&moldyn);
880 moldyn_shutdown(&moldyn);
882 list_destroy_f(&(mdrun.stage));