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,64);
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],"continue",8))
207 strncpy(mdrun->continue_file,word[1],128);
208 else if(!strncmp(word[0],"cutoff",6))
209 mdrun->cutoff=atof(word[1]);
210 else if(!strncmp(word[0],"nnd",3))
211 mdrun->nnd=atof(word[1]);
212 else if(!strncmp(word[0],"intalgo",7)) {
213 if(!strncmp(word[1],"verlet",5))
214 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
216 else if(!strncmp(word[0],"timestep",8))
217 mdrun->timestep=atof(word[1]);
218 else if(!strncmp(word[0],"volume",6)) {
219 mdrun->dim.x=atof(word[1]);
220 mdrun->dim.y=atof(word[2]);
221 mdrun->dim.z=atof(word[3]);
222 if(strncmp(word[4],"0",1))
225 else if(!strncmp(word[0],"pbc",3)) {
226 if(strncmp(word[1],"0",1))
230 if(strncmp(word[2],"0",1))
234 if(strncmp(word[3],"0",1))
239 else if(!strncmp(word[0],"temperature",11))
240 mdrun->temperature=atof(word[1]);
241 else if(!strncmp(word[0],"pressure",8))
242 mdrun->pressure=atof(word[1]);
243 else if(!strncmp(word[0],"lattice",7)) {
244 if(!strncmp(word[1],"zincblende",10))
245 mdrun->lattice=ZINCBLENDE;
246 if(!strncmp(word[1],"fcc",3))
248 if(!strncmp(word[1],"diamond",7))
249 mdrun->lattice=DIAMOND;
251 else if(!strncmp(word[0],"element1",8)) {
252 mdrun->element1=atoi(word[1]);
253 mdrun->m1=pse_mass[mdrun->element1];
255 else if(!strncmp(word[0],"element2",8)) {
256 mdrun->element2=atoi(word[1]);
257 mdrun->m2=pse_mass[mdrun->element2];
259 else if(!strncmp(word[0],"fill",6)) {
260 // only lc mode by now
261 mdrun->lx=atoi(word[2]);
262 mdrun->ly=atoi(word[3]);
263 mdrun->lz=atoi(word[4]);
264 mdrun->lc=atof(word[5]);
266 else if(!strncmp(word[0],"aattr",5)) {
267 // for aatrib line we need a special stage
268 // containing one schedule of 0 loops ...
269 for(i=0;i<strlen(word[1]);i++) {
272 cap.type|=CHAATTR_TOTALV;
275 cap.type|=CHAATTR_REGION;
278 cap.type|=CHAATTR_ELEMENT;
285 if(cap.type&CHAATTR_REGION) {
286 cap.x0=atof(word[1]);
287 cap.y0=atof(word[2]);
288 cap.z0=atof(word[3]);
289 cap.x1=atof(word[4]);
290 cap.y1=atof(word[5]);
291 cap.z1=atof(word[6]);
294 if(cap.type&CHAATTR_ELEMENT) {
295 cap.element=atoi(word[i]);
298 for(o=0;o<strlen(word[i]);o++) {
301 cap.attr|=ATOM_ATTR_VB;
304 cap.attr|=ATOM_ATTR_HB;
307 cap.attr|=ATOM_ATTR_VA;
310 cap.attr|=ATOM_ATTR_FP;
313 cap.attr|=ATOM_ATTR_1BP;
316 cap.attr|=ATOM_ATTR_2BP;
319 cap.attr|=ATOM_ATTR_3BP;
325 add_stage(mdrun,STAGE_CHAATTR,&cap);
327 else if(!strncmp(word[0],"sattr",5)) {
328 // for satrib line we need a special stage
329 // containing one schedule of 0 loops ...
331 for(i=1;i<wcnt;i++) {
332 if(!strncmp(word[i],"pctrl",5)) {
333 csp.ptau=0.01/(atof(word[++i])*GPA);
334 csp.type|=CHSATTR_PCTRL;
336 if(!strncmp(word[i],"tctrl",5)) {
337 csp.ttau=atof(word[++i]);
338 csp.type|=CHSATTR_TCTRL;
340 if(!strncmp(word[i],"prelax",6)) {
341 csp.dp=atof(word[++i])*BAR;
342 csp.type|=CHSATTR_PRELAX;
344 if(!strncmp(word[i],"trelax",6)) {
345 csp.dt=atof(word[++i]);
346 csp.type|=CHSATTR_TRELAX;
348 if(!strncmp(word[i],"rsteps",6)) {
349 csp.rsteps=atoi(word[++i]);
350 csp.type|=CHSATTR_RSTEPS;
352 if(!strncmp(word[i],"avgrst",6)) {
353 csp.avgrst=atoi(word[++i]);
354 csp.type|=CHSATTR_AVGRST;
357 add_stage(mdrun,STAGE_CHSATTR,&csp);
359 else if(!strncmp(word[0],"prerun",6))
360 mdrun->prerun=atoi(word[1]);
361 else if(!strncmp(word[0],"avgskip",7))
362 mdrun->avgskip=atoi(word[1]);
363 else if(!strncmp(word[0],"elog",4))
364 mdrun->elog=atoi(word[1]);
365 else if(!strncmp(word[0],"tlog",4))
366 mdrun->tlog=atoi(word[1]);
367 else if(!strncmp(word[0],"plog",4))
368 mdrun->plog=atoi(word[1]);
369 else if(!strncmp(word[0],"vlog",4))
370 mdrun->vlog=atoi(word[1]);
371 else if(!strncmp(word[0],"save",4))
372 mdrun->save=atoi(word[1]);
373 else if(!strncmp(word[0],"visualize",9))
374 mdrun->visualize=atoi(word[1]);
375 else if(!strncmp(word[0],"stage",5)) {
376 // for every stage line, add a stage
377 if(!strncmp(word[1],"displace",8)) {
378 dap.nr=atoi(word[2]);
379 dap.dx=atof(word[3]);
380 dap.dy=atof(word[4]);
381 dap.dz=atof(word[5]);
382 add_stage(mdrun,STAGE_DISPLACE_ATOM,&dap);
384 else if(!strncmp(word[1],"ins_atoms",9)) {
385 iap.ins_steps=atoi(word[2]);
386 iap.ins_atoms=atoi(word[3]);
387 iap.element=atoi(word[4]);
388 iap.brand=atoi(word[5]);
389 for(i=0;i<strlen(word[6]);i++) {
392 iap.attr|=ATOM_ATTR_VB;
395 iap.attr|=ATOM_ATTR_HB;
398 iap.attr|=ATOM_ATTR_VA;
401 iap.attr|=ATOM_ATTR_FP;
404 iap.attr|=ATOM_ATTR_1BP;
407 iap.attr|=ATOM_ATTR_2BP;
410 iap.attr|=ATOM_ATTR_3BP;
420 iap.x0=atof(word[8]);
421 iap.y0=atof(word[9]);
422 iap.z0=atof(word[10]);
425 if(word[8][0]=='t') {
427 iap.cr=atof(word[9]);
431 iap.x0=atof(word[8]);
432 iap.y0=atof(word[9]);
433 iap.z0=atof(word[10]);
434 iap.x1=atof(word[11]);
435 iap.y1=atof(word[12]);
436 iap.z1=atof(word[13]);
437 iap.cr=atof(word[14]);
442 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
444 else if(!strncmp(word[1],"continue",8)) {
445 cp.runs=atoi(word[2]);
446 add_stage(mdrun,STAGE_CONTINUE,&cp);
448 else if(!strncmp(word[1],"anneal",6)) {
450 ap.runs=atoi(word[2]);
452 add_stage(mdrun,STAGE_ANNEAL,&ap);
455 printf("%s unknown stage type: %s\n",
461 printf("%s unknown keyword '%s', skipped!\n",
473 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
477 if(!(mdrun->sattr&SATTR_PRELAX)) {
481 delta=moldyn->p_avg-moldyn->p_ref;
492 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
496 if(!(mdrun->sattr&SATTR_TRELAX))
499 delta=moldyn->t_avg-moldyn->t_ref;
510 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
512 t_displace_atom_params *dap;
516 stage=mdrun->stage.current->data;
519 atom=&(moldyn->atom[dap->nr]);
527 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
529 t_insert_atoms_params *iap;
539 stage=mdrun->stage.current->data;
544 v.x=0.0; v.y=0.0; v.z=0.0;
548 switch(mdrun->lattice) {
589 printf("%s unknown insertion mode: %02x\n",
595 while(cnt<iap->ins_atoms) {
598 if(iap->type!=INS_POS) {
599 r.x=rand_get_double(&(moldyn->random))*x;
600 r.y=rand_get_double(&(moldyn->random))*y;
601 r.z=rand_get_double(&(moldyn->random))*z;
612 if(iap->type!=INS_TOTAL) {
619 dmin=1000; // for sure too high!
620 for(i=0;i<moldyn->count;i++) {
621 atom=&(moldyn->atom[i]);
622 v3_sub(&dist,&(atom->r),&r);
623 check_per_bound(moldyn,&dist);
624 d=v3_absolute_square(&dist);
625 if(d<(iap->cr*iap->cr)) {
634 add_atom(moldyn,iap->element,pse_mass[iap->element],
635 iap->brand,iap->attr,&r,&v);
636 printf("%s atom inserted (%d/%d): %f %f %f\n",
637 ME,(iap->cnt_steps+1)*iap->ins_atoms,
638 iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
639 printf(" -> d2 = %f/%f\n",dmin,iap->cr*iap->cr);
646 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
649 t_chaattr_params *cap;
653 stage=mdrun->stage.current->data;
656 for(i=0;i<moldyn->count;i++) {
657 atom=&(moldyn->atom[i]);
658 if(cap->type&CHAATTR_ELEMENT) {
659 if(cap->element!=atom->element)
662 if(cap->type&CHAATTR_REGION) {
663 if(cap->x0<atom->r.x)
665 if(cap->y0<atom->r.y)
667 if(cap->z0<atom->r.z)
669 if(cap->x1>atom->r.x)
671 if(cap->y1>atom->r.y)
673 if(cap->z1>atom->r.z)
676 atom->attr=cap->attr;
682 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
685 t_chsattr_params *csp;
687 stage=mdrun->stage.current->data;
690 if(csp->type&CHSATTR_PCTRL) {
692 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
694 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
696 if(csp->type&CHSATTR_TCTRL) {
698 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
700 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
702 if(csp->type&CHSATTR_PRELAX) {
704 mdrun->sattr&=(~(SATTR_PRELAX));
706 mdrun->sattr|=SATTR_PRELAX;
709 if(csp->type&CHSATTR_TRELAX) {
711 mdrun->sattr&=(~(SATTR_TRELAX));
713 mdrun->sattr|=SATTR_TRELAX;
716 if(csp->type&CHSATTR_AVGRST) {
718 mdrun->sattr|=SATTR_AVGRST;
720 mdrun->sattr&=(~(SATTR_AVGRST));
722 if(csp->type&CHSATTR_RSTEPS) {
723 mdrun->relax_steps=csp->rsteps;
729 #define stage_print(m) if(!(stage->executed)) \
732 int mdrun_hook(void *ptr1,void *ptr2) {
742 t_insert_atoms_params *iap;
743 t_continue_params *cp;
753 /* return immediately if there are no more stage */
754 if(sl->current==NULL)
757 /* get stage description */
758 stage=sl->current->data;
760 /* default steps and tau values */
761 steps=mdrun->relax_steps;
764 /* check whether relaxation steps are necessary */
765 if((check_pressure(moldyn,mdrun)==TRUE)&\
766 (check_temperature(moldyn,mdrun)==TRUE)) {
769 stage_print("\n###########################\n");
770 stage_print("# [mdrun] executing stage #\n");
771 stage_print("###########################\n\n");
773 /* stage specific stuff */
774 switch(stage->type) {
775 case STAGE_DISPLACE_ATOM:
776 stage_print(" -> displace atom\n\n");
777 displace_atom(moldyn,mdrun);
780 case STAGE_INSERT_ATOMS:
781 stage_print(" -> insert atoms\n\n");
783 if(iap->cnt_steps==iap->ins_steps) {
787 insert_atoms(moldyn,mdrun);
791 stage_print(" -> continue\n\n");
792 if(stage->executed==TRUE) {
800 stage_print(" -> anneal\n\n");
802 if(ap->count==ap->runs) {
806 if(moldyn->t_ref+ap->dt>=0.0)
807 set_temperature(moldyn,
808 moldyn->t_ref+ap->dt);
812 stage_print(" -> chaattr\n\n");
813 chaatr(moldyn,mdrun);
817 stage_print(" -> chsattr\n\n");
818 chsattr(moldyn,mdrun);
822 printf("%s unknwon stage type\n",ME);
826 /* mark as executed */
827 stage->executed=TRUE;
830 if(change_stage==TRUE) {
831 printf("%s finished stage\n",ME);
832 if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
833 printf("%s no more stages\n",ME);
844 if(mdrun->sattr&SATTR_AVGRST)
845 average_reset(moldyn);
849 /* continue simulation */
850 moldyn_add_schedule(moldyn,steps,tau);
855 int main(int argc,char **argv) {
862 memset(&mdrun,0,sizeof(t_mdrun));
863 memset(&moldyn,0,sizeof(t_moldyn));
865 /* parse arguments */
866 if(mdrun_parse_argv(&mdrun,argc,argv)<0)
869 /* initialize list system */
870 list_init_f(&(mdrun.stage));
872 /* parse config file */
873 mdrun_parse_config(&mdrun);
875 /* reset the stage list */
876 list_reset_f(&(mdrun.stage));
880 /* prepare simulation */
882 if(mdrun.continue_file[0]) {
883 // read the save file
884 moldyn_read_save_file(&moldyn,mdrun.continue_file);
885 // manualaadjusting some stuff
888 rand_init(&(moldyn.random),NULL,1);
889 moldyn.random.status|=RAND_STAT_VERBOSE;
892 moldyn_init(&moldyn,argc,argv);
895 if(set_int_alg(&moldyn,mdrun.intalgo)<0)
899 set_cutoff(&moldyn,mdrun.cutoff);
900 if(set_potential(&moldyn,mdrun.potential)<0)
902 switch(mdrun.potential) {
903 case MOLDYN_POTENTIAL_AM:
904 albe_mult_set_params(&moldyn,
908 case MOLDYN_POTENTIAL_TM:
909 tersoff_mult_set_params(&moldyn,
913 case MOLDYN_POTENTIAL_HO:
914 harmonic_oscillator_set_params(&moldyn,mdrun.element1);
916 case MOLDYN_POTENTIAL_LJ:
917 lennard_jones_set_params(&moldyn,mdrun.element1);
920 printf("%s unknown potential: %02x\n",
925 /* if it is a continue run, reset schedule and skip lattice init */
926 if(mdrun.continue_file[0]) {
927 memset(&(moldyn.schedule),0,sizeof(t_moldyn_schedule));
931 /* initial lattice and dimensions */
932 set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
933 set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
934 switch(mdrun.lattice) {
936 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
937 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
938 mdrun.ly,mdrun.lz,NULL);
941 create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
942 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
943 mdrun.ly,mdrun.lz,NULL);
946 o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
947 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
948 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
949 mdrun.ly,mdrun.lz,&o);
950 o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
951 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
952 mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
953 mdrun.ly,mdrun.lz,&o);
956 printf("%s unknown lattice type: %02x\n",
960 moldyn_bc_check(&moldyn);
962 /* temperature and pressure */
963 set_temperature(&moldyn,mdrun.temperature);
964 set_pressure(&moldyn,mdrun.pressure);
965 thermal_init(&moldyn,TRUE);
969 moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
972 moldyn_set_log_dir(&moldyn,mdrun.sdir);
973 moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
975 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
977 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
979 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
981 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
983 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
985 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
986 moldyn_set_log(&moldyn,CREATE_REPORT,0);
987 set_avg_skip(&moldyn,mdrun.avgskip);
989 /* prepare the hook function */
990 moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
992 /* run the simulation */
993 moldyn_integrate(&moldyn);
996 moldyn_shutdown(&moldyn);
998 list_destroy_f(&(mdrun.stage));