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]);
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
409 iap.x0=atof(word[8]);
410 iap.y0=atof(word[9]);
411 iap.z0=atof(word[10]);
412 iap.x1=atof(word[11]);
413 iap.y1=atof(word[12]);
414 iap.z1=atof(word[13]);
416 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
418 else if(!strncmp(word[1],"continue",8)) {
419 cp.runs=atoi(word[2]);
420 add_stage(mdrun,STAGE_CONTINUE,&cp);
422 else if(!strncmp(word[1],"anneal",6)) {
424 ap.runs=atoi(word[2]);
426 add_stage(mdrun,STAGE_ANNEAL,&ap);
429 printf("%s unknown stage type: %s\n",
435 printf("%s unknown keyword '%s', skipped!\n",
447 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
451 if(!(mdrun->sattr&SATTR_PRELAX))
454 delta=moldyn->p_avg-moldyn->p_ref;
465 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
469 if(!(mdrun->sattr&SATTR_TRELAX))
472 delta=moldyn->t_avg-moldyn->t_ref;
483 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
485 t_insert_atoms_params *iap;
495 stage=mdrun->stage.current->data;
500 v.x=0.0; v.y=0.0; v.z=0.0;
522 printf("%s unknown insertion mode: %02x\n",
528 while(cnt<iap->ins_atoms) {
531 r.x=(rand_get_double(&(moldyn->random))-0.5)*x+x0;
532 r.y=(rand_get_double(&(moldyn->random))-0.5)*y+y0;
533 r.z=(rand_get_double(&(moldyn->random))-0.5)*z+z0;
536 dmin=1000; // for sure too high!
537 for(i=0;i<moldyn->count;i++) {
538 atom=&(moldyn->atom[i]);
539 v3_sub(&dist,&(atom->r),&r);
540 check_per_bound(moldyn,&dist);
541 d=v3_absolute_square(&dist);
542 if(d<(iap->cr*iap->cr)) {
551 add_atom(moldyn,iap->element,pse_mass[iap->element],
552 iap->brand,iap->attr,&r,&v);
553 printf("%s atom inserted: %f %f %f | d squared = %f\n",
554 ME,r.x,r.y,r.z,dmin);
561 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
564 t_chaattr_params *cap;
568 stage=mdrun->stage.current->data;
571 for(i=0;i<moldyn->count;i++) {
572 atom=&(moldyn->atom[i]);
573 if(cap->type&CHAATTR_ELEMENT) {
574 if(cap->element!=atom->element)
577 if(cap->type&CHAATTR_REGION) {
578 if(cap->x0<atom->r.x)
580 if(cap->y0<atom->r.y)
582 if(cap->z0<atom->r.z)
584 if(cap->x1>atom->r.x)
586 if(cap->y1>atom->r.y)
588 if(cap->z1>atom->r.z)
591 atom->attr=cap->attr;
597 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
600 t_chsattr_params *csp;
602 stage=mdrun->stage.current->data;
605 if(csp->type&CHSATTR_PCTRL) {
607 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
609 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
611 if(csp->type&CHSATTR_TCTRL) {
613 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
615 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
617 if(csp->type&CHSATTR_PRELAX) {
619 mdrun->sattr&=(~(SATTR_PRELAX));
621 mdrun->sattr|=SATTR_PRELAX;
624 if(csp->type&CHSATTR_TRELAX) {
626 mdrun->sattr&=(~(SATTR_TRELAX));
628 mdrun->sattr|=SATTR_TRELAX;
631 if(csp->type&CHSATTR_AVGRST) {
633 mdrun->sattr|=SATTR_AVGRST;
635 mdrun->sattr&=(~(SATTR_AVGRST));
637 if(csp->type&CHSATTR_RSTEPS) {
638 mdrun->relax_steps=csp->rsteps;
644 #define stage_print(m) if(!(stage->executed)) \
647 int mdrun_hook(void *ptr1,void *ptr2) {
657 t_insert_atoms_params *iap;
658 t_continue_params *cp;
668 /* return immediately if there are no more stage */
669 if(sl->current==NULL)
672 /* get stage description */
673 stage=sl->current->data;
675 /* default steps and tau values */
676 steps=mdrun->relax_steps;
679 /* check whether relaxation steps are necessary */
680 if(!((check_pressure(moldyn,mdrun)==FALSE)|\
681 (check_temperature(moldyn,mdrun)==FALSE))) {
684 stage_print("\n###########################\n");
685 stage_print("# [mdrun] executing stage #\n");
686 stage_print("###########################\n\n");
688 /* stage specific stuff */
689 switch(stage->type) {
690 case STAGE_INSERT_ATOMS:
691 stage_print(" -> insert atoms\n\n");
693 if(iap->cnt_steps==iap->ins_steps) {
697 insert_atoms(moldyn,mdrun);
701 stage_print(" -> continue\n\n");
702 if(stage->executed==TRUE) {
710 stage_print(" -> anneal\n\n");
712 if(ap->count==ap->runs) {
716 set_temperature(moldyn,moldyn->t_ref+ap->dt);
720 stage_print(" -> chaattr\n\n");
721 chaatr(moldyn,mdrun);
725 stage_print(" -> chsattr\n\n");
726 chsattr(moldyn,mdrun);
730 printf("%s unknwon stage type\n",ME);
734 /* mark as executed */
735 stage->executed=TRUE;
738 if(change_stage==TRUE) {
739 printf("%s finished stage\n",ME);
740 if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
741 printf("%s no more stages\n",ME);
752 if(mdrun->sattr&SATTR_AVGRST)
753 average_reset(moldyn);
757 /* continue simulation */
758 moldyn_add_schedule(moldyn,steps,tau);
763 int main(int argc,char **argv) {
770 memset(&mdrun,0,sizeof(t_mdrun));
771 memset(&moldyn,0,sizeof(t_moldyn));
773 /* parse arguments */
774 if(mdrun_parse_argv(&mdrun,argc,argv)<0)
777 /* initialize list system */
778 list_init_f(&(mdrun.stage));
780 /* parse config file */
781 mdrun_parse_config(&mdrun);
783 /* reset the stage list */
784 list_reset_f(&(mdrun.stage));
788 /* prepare simulation */
789 moldyn_init(&moldyn,argc,argv);
790 if(set_int_alg(&moldyn,mdrun.intalgo)<0)
792 set_cutoff(&moldyn,mdrun.cutoff);
793 if(set_potential(&moldyn,mdrun.potential)<0)
795 switch(mdrun.potential) {
796 case MOLDYN_POTENTIAL_AM:
797 albe_mult_set_params(&moldyn,
801 case MOLDYN_POTENTIAL_TM:
802 tersoff_mult_set_params(&moldyn,
806 case MOLDYN_POTENTIAL_HO:
807 harmonic_oscillator_set_params(&moldyn,mdrun.element1);
809 case MOLDYN_POTENTIAL_LJ:
810 lennard_jones_set_params(&moldyn,mdrun.element1);
813 printf("%s unknown potential: %02x\n",
817 set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
818 set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
819 switch(mdrun.lattice) {
821 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
822 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
823 mdrun.ly,mdrun.lz,NULL);
826 create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
827 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
828 mdrun.ly,mdrun.lz,NULL);
831 o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
832 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
833 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
834 mdrun.ly,mdrun.lz,&o);
835 o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
836 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
837 mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
838 mdrun.ly,mdrun.lz,&o);
841 printf("%s unknown lattice type: %02x\n",
845 moldyn_bc_check(&moldyn);
846 set_temperature(&moldyn,mdrun.temperature);
847 set_pressure(&moldyn,mdrun.pressure);
848 thermal_init(&moldyn,TRUE);
849 moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
850 moldyn_set_log_dir(&moldyn,mdrun.sdir);
851 moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
853 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
855 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
857 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
859 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
861 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
863 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
864 moldyn_set_log(&moldyn,CREATE_REPORT,0);
865 set_avg_skip(&moldyn,mdrun.avgskip);
867 /* prepare the hook function */
868 moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
870 /* run the simulation */
871 moldyn_integrate(&moldyn);
874 moldyn_shutdown(&moldyn);
876 list_destroy_f(&(mdrun.stage));