displace stage type added
[physik/posic.git] / mdrun.c
1 /*
2  * mdrun.c - main code to run a md simulation
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #include "mdrun.h"
9
10 /* potential */
11 #include "potentials/harmonic_oscillator.h"
12 #include "potentials/lennard_jones.h"
13 #include "potentials/albe.h"
14 #ifdef TERSOFF_ORIG
15 #include "potentials/tersoff_orig.h"
16 #else
17 #include "potentials/tersoff.h"
18 #endif
19
20 #define ME      "[mdrun]"
21
22 /*
23  * parse function
24  */
25
26 int mdrun_usage(void) {
27
28         printf("%s usage:\n",ME);
29
30         return 0;
31 }
32
33 int mdrun_parse_argv(t_mdrun *mdrun,int argc,char **argv) {
34
35         int i;
36
37         for(i=1;i<argc;i++) {
38
39                 if(argv[i][0]!='-') {
40                         printf("%s unknown switch: %s\n",ME,argv[i]);
41                         return -1;
42                 }
43
44                 switch(argv[i][1]) {
45                         case 'c':
46                                 strncpy(mdrun->cfile,argv[++i],128);
47                                 break;
48                         case 's':
49                                 strncpy(mdrun->sdir,argv[++i],128);
50                                 break;
51                         case 'h':
52                                 mdrun_usage();
53                                 break;
54                         default:
55                                 printf("%s unknown option: %s\n",ME,argv[i]);
56                                 mdrun_usage();
57                                 return -1;
58                 }
59
60         }
61
62         return 0;
63 }
64
65 int del_stages(t_mdrun *mdrun) {
66
67         t_list *sl;
68         t_stage *stage;
69
70         sl=&(mdrun->stage);
71
72         list_reset_f(sl);
73
74         if(sl->start==NULL)
75                 return 0;
76
77         do {
78                 stage=sl->current->data;
79                 free(stage->params);
80                 free(stage);
81         } while(list_next_f(sl)!=L_NO_NEXT_ELEMENT);
82
83         return 0;
84 }
85
86 int add_stage(t_mdrun *mdrun,u8 type,void *params) {
87
88         int psize;
89
90         t_stage *stage;
91
92         switch(type) {
93                 case STAGE_DISPLACE_ATOM:
94                         psize=sizeof(t_displace_atom_params);
95                         break;
96                 case STAGE_INSERT_ATOMS:
97                         psize=sizeof(t_insert_atoms_params);
98                         break;
99                 case STAGE_CONTINUE:
100                         psize=sizeof(t_continue_params);
101                         break;
102                 case STAGE_ANNEAL:
103                         psize=sizeof(t_anneal_params);
104                         break;
105                 case STAGE_CHAATTR:
106                         psize=sizeof(t_chaattr_params);
107                         break;
108                 case STAGE_CHSATTR:
109                         psize=sizeof(t_chsattr_params);
110                         break;
111                 default:
112                         printf("%s unknown stage type: %02x\n",ME,type);
113                         return -1;
114         }
115
116         stage=malloc(sizeof(t_stage));
117         if(stage==NULL) {
118                 perror("[mdrun] malloc (add stage)");
119                 return -1;
120         }
121
122         stage->type=type;
123         stage->executed=FALSE;
124
125         stage->params=malloc(psize);
126         if(stage->params==NULL) {
127                 perror("[mdrun] malloc (stage params)");
128                 return -1;
129         }
130
131         memcpy(stage->params,params,psize);
132
133         list_add_immediate_f(&(mdrun->stage),stage);
134
135         return 0;
136 }
137
138 int mdrun_parse_config(t_mdrun *mdrun) {
139
140         int fd,ret;
141         char error[128];
142         char line[128];
143         char *wptr;
144         char word[16][32];
145         int wcnt;
146         int i,o;
147
148         t_displace_atom_params dap;
149         t_insert_atoms_params iap;
150         t_continue_params cp;
151         t_anneal_params ap;
152         t_chaattr_params cap;
153         t_chsattr_params csp;
154
155         /* open config file */
156         fd=open(mdrun->cfile,O_RDONLY);
157         if(fd<0) {
158                 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
159                 perror(error);
160                 return fd;
161         }
162
163         /* read, parse, set */
164         ret=1;
165         while(ret>0) {
166
167                 /* read */
168                 ret=get_line(fd,line,128);
169
170                 /* parse */
171
172                 // ignore # lines and \n
173                 if((line[0]=='#')|(ret==1))
174                         continue;
175
176                 // reset
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));
182
183                 // get command + args
184                 wcnt=0;
185                 while(1) {
186                         if(wcnt)
187                                 wptr=strtok(NULL," \t");
188                         else
189                                 wptr=strtok(line," \t");
190                         if(wptr==NULL)
191                                 break;
192                         strncpy(word[wcnt],wptr,32);
193                         wcnt+=1;
194                 }
195
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;
205                 }
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;
213                 }
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))
221                                 mdrun->vis=TRUE;
222                 }
223                 else if(!strncmp(word[0],"pbc",3)) {
224                         if(strncmp(word[1],"0",1))
225                                 mdrun->pbcx=TRUE;
226                         else
227                                 mdrun->pbcx=FALSE;
228                         if(strncmp(word[2],"0",1))
229                                 mdrun->pbcy=TRUE;
230                         else
231                                 mdrun->pbcy=FALSE;
232                         if(strncmp(word[3],"0",1))
233                                 mdrun->pbcz=TRUE;
234                         else
235                                 mdrun->pbcz=FALSE;
236                 }
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))
245                                 mdrun->lattice=FCC;
246                         if(!strncmp(word[1],"diamond",7))
247                                 mdrun->lattice=DIAMOND;
248                 }
249                 else if(!strncmp(word[0],"element1",8)) {
250                         mdrun->element1=atoi(word[1]);
251                         mdrun->m1=pse_mass[mdrun->element1];
252                 }
253                 else if(!strncmp(word[0],"element2",8)) {
254                         mdrun->element2=atoi(word[1]);
255                         mdrun->m2=pse_mass[mdrun->element2];
256                 }
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]);
263                 }
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++) {
268                                 switch(word[1][i]) {
269                                         case 't':
270                                                 cap.type|=CHAATTR_TOTALV;
271                                                 break;
272                                         case 'r':
273                                                 cap.type|=CHAATTR_REGION;
274                                                 break;
275                                         case 'e':
276                                                 cap.type|=CHAATTR_ELEMENT;
277                                                 break;
278                                         default:
279                                                 break;
280                                 }
281                         }
282                         i=2;
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]);
290                                 i+=6;
291                         }
292                         if(cap.type&CHAATTR_ELEMENT) {
293                                 cap.element=atoi(word[i]);
294                                 i+=1;
295                         }
296                         for(o=0;o<strlen(word[i]);o++) {
297                                 switch(word[i][o]) {
298                                         case 'b':
299                                                 cap.attr|=ATOM_ATTR_VB;
300                                                 break;
301                                         case 'h':
302                                                 cap.attr|=ATOM_ATTR_HB;
303                                                 break;
304                                         case 'v':
305                                                 cap.attr|=ATOM_ATTR_VA;
306                                                 break;
307                                         case 'f':
308                                                 cap.attr|=ATOM_ATTR_FP;
309                                                 break;
310                                         case '1':
311                                                 cap.attr|=ATOM_ATTR_1BP;
312                                                 break;
313                                         case '2':
314                                                 cap.attr|=ATOM_ATTR_2BP;
315                                                 break;
316                                         case '3':
317                                                 cap.attr|=ATOM_ATTR_3BP;
318                                                 break;
319                                         default:
320                                                 break;
321                                 }
322                         }
323                         add_stage(mdrun,STAGE_CHAATTR,&cap);
324                 }
325                 else if(!strncmp(word[0],"sattr",5)) {
326                         // for satrib line we need a special stage
327                         // containing one schedule of 0 loops ...
328                         csp.type=0;
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;
333                                 }
334                                 if(!strncmp(word[i],"tctrl",5)) {
335                                         csp.ttau=atof(word[++i]);
336                                         csp.type|=CHSATTR_TCTRL;
337                                 }
338                                 if(!strncmp(word[i],"prelax",6)) {
339                                         csp.dp=atof(word[++i])*BAR;
340                                         csp.type|=CHSATTR_PRELAX;
341                                 }
342                                 if(!strncmp(word[i],"trelax",6)) {
343                                         csp.dt=atof(word[++i]);
344                                         csp.type|=CHSATTR_TRELAX;
345                                 }
346                                 if(!strncmp(word[i],"rsteps",6)) {
347                                         csp.rsteps=atoi(word[++i]);
348                                         csp.type|=CHSATTR_RSTEPS;
349                                 }
350                                 if(!strncmp(word[i],"avgrst",6)) {
351                                         csp.avgrst=atoi(word[++i]);
352                                         csp.type|=CHSATTR_AVGRST;
353                                 }
354                         }
355                         add_stage(mdrun,STAGE_CHSATTR,&csp);
356                 }
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);
381                         }
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++) {
388                                         switch(word[6][i]) {
389                                                 case 'b':
390                                                         iap.attr|=ATOM_ATTR_VB;
391                                                         break;
392                                                 case 'h':
393                                                         iap.attr|=ATOM_ATTR_HB;
394                                                         break;
395                                                 case 'v':
396                                                         iap.attr|=ATOM_ATTR_VA;
397                                                         break;
398                                                 case 'f':
399                                                         iap.attr|=ATOM_ATTR_FP;
400                                                         break;
401                                                 case '1':
402                                                         iap.attr|=ATOM_ATTR_1BP;
403                                                         break;
404                                                 case '2':
405                                                         iap.attr|=ATOM_ATTR_2BP;
406                                                         break;
407                                                 case '3':
408                                                         iap.attr|=ATOM_ATTR_3BP;
409                                                         break;
410                                                 default:
411                                                         break;
412                                         }
413                                 }
414                                 switch(word[7][0]) {
415                                         // insertion types
416                                         case 'p':
417                                                 iap.type=INS_POS;
418                                                 iap.x0=atof(word[8]);
419                                                 iap.y0=atof(word[9]);
420                                                 iap.z0=atof(word[10]);
421                                                 break;
422                                         case 'r':
423                                                 if(word[8][0]=='t') {
424                                                         iap.type=INS_TOTAL;
425                                                         iap.cr=atof(word[9]);
426                                                 }
427                                                 else {
428                                                         iap.type=INS_REGION;
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]);
436                                                 }
437                                         default:
438                                                 break;
439                                 }
440                                 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
441                         }
442                         else if(!strncmp(word[1],"continue",8)) {
443                                 cp.runs=atoi(word[2]);
444                                 add_stage(mdrun,STAGE_CONTINUE,&cp);
445                         }
446                         else if(!strncmp(word[1],"anneal",6)) {
447                                 ap.count=0;
448                                 ap.runs=atoi(word[2]);
449                                 ap.dt=atof(word[3]);
450                                 add_stage(mdrun,STAGE_ANNEAL,&ap);
451                         }
452                         else {
453                                 printf("%s unknown stage type: %s\n",
454                                        ME,word[1]);
455                                 return -1;
456                         }
457                 }
458                 else {
459                         printf("%s unknown keyword '%s', skipped!\n",
460                                ME,word[0]);
461                         continue;
462                 }
463         }
464
465         /* close file */
466         close(fd);
467
468         return 0;
469 }
470
471 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
472
473         double delta;
474
475         if(!(mdrun->sattr&SATTR_PRELAX)) {
476                 return TRUE;
477         }
478
479         delta=moldyn->p_avg-moldyn->p_ref;
480
481         if(delta<0)
482                 delta=-delta;
483
484         if(delta>mdrun->dp)
485                 return FALSE;
486
487         return TRUE;
488 }
489
490 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
491
492         double delta;
493
494         if(!(mdrun->sattr&SATTR_TRELAX))
495                 return TRUE;
496
497         delta=moldyn->t_avg-moldyn->t_ref;
498
499         if(delta<0)
500                 delta=-delta;
501
502         if(delta>mdrun->dt)
503                 return FALSE;
504
505         return TRUE;
506 }
507
508 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
509
510         t_displace_atom_params *dap;
511         t_stage *stage;
512         t_atom *atom;
513
514         stage=mdrun->stage.current->data;
515         dap=stage->params;
516
517         atom=&(moldyn->atom[dap->nr]);
518         atom->r.x+=dap->dx;
519         atom->r.y+=dap->dy;
520         atom->r.z+=dap->dz;
521
522         return 0;
523 }
524
525 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
526
527         t_insert_atoms_params *iap;
528         t_stage *stage;
529         t_atom *atom;
530         t_3dvec r,v,dist;
531         double d,dmin,o;
532         int cnt,i;
533         double x,y,z;
534         double x0,y0,z0;
535         u8 cr_check,run;
536         
537         stage=mdrun->stage.current->data;
538         iap=stage->params;
539
540         cr_check=FALSE;
541
542         v.x=0.0; v.y=0.0; v.z=0.0;
543         x=0; y=0; z=0;
544         o=0; dmin=0;
545
546         switch(mdrun->lattice) {
547                 case CUBIC:
548                         o=0.5*mdrun->lc;
549                         break;
550                 case FCC:
551                         o=0.25*mdrun->lc;
552                         break;
553                 case DIAMOND:
554                 case ZINCBLENDE:
555                         o=0.125*mdrun->lc;
556                         break;
557                 default:
558                         break;
559         }
560
561         switch(iap->type) {
562                 case INS_TOTAL:
563                         x=moldyn->dim.x;
564                         x0=-x/2.0;
565                         y=moldyn->dim.y;
566                         y0=-y/2.0;
567                         z=moldyn->dim.z;
568                         z0=-z/2.0;
569                         cr_check=TRUE;
570                         break;
571                 case INS_REGION:
572                         x=iap->x1-iap->x0;
573                         x0=iap->x0;
574                         y=iap->y1-iap->y0;
575                         y0=iap->y0;
576                         z=iap->z1-iap->z0;
577                         z0=iap->z0;
578                         cr_check=TRUE;
579                         break;
580                 case INS_POS:
581                         x0=iap->x0;
582                         y0=iap->y0;
583                         z0=iap->z0;
584                         cr_check=FALSE;
585                         break;
586                 default:
587                         printf("%s unknown insertion mode: %02x\n",
588                                ME,iap->type);
589                         return -1;
590         }
591
592         cnt=0;
593         while(cnt<iap->ins_atoms) {
594                 run=1;
595                 while(run) {
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;
600                         }
601                         else {
602                                 r.x=0.0;
603                                 r.y=0.0;
604                                 r.z=0.0;
605                         }
606                         r.x+=x0;
607                         r.y+=y0;
608                         r.z+=z0;
609                         // offset
610                         if(iap->type!=INS_TOTAL) {
611                                 r.x+=o;
612                                 r.y+=o;
613                                 r.z+=o;
614                         }
615                         run=0;
616                         if(cr_check==TRUE) {
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)) {
624                                                 run=1;
625                                                 break;
626                                         }
627                                         if(d<dmin)
628                                                 dmin=d;
629                                 }
630                         }
631                 }
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);
638                 cnt+=1;
639         }
640
641         return 0;
642 }
643
644 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
645
646         t_stage *stage;
647         t_chaattr_params *cap;
648         t_atom *atom;
649         int i;
650
651         stage=mdrun->stage.current->data;
652         cap=stage->params;
653
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)
658                                 continue;
659                 }
660                 if(cap->type&CHAATTR_REGION) {
661                         if(cap->x0<atom->r.x)
662                                 continue;
663                         if(cap->y0<atom->r.y)
664                                 continue;
665                         if(cap->z0<atom->r.z)
666                                 continue;
667                         if(cap->x1>atom->r.x)
668                                 continue;
669                         if(cap->y1>atom->r.y)
670                                 continue;
671                         if(cap->z1>atom->r.z)
672                                 continue;
673                 }
674                 atom->attr=cap->attr;
675         }
676
677         return 0;
678 }
679
680 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
681
682         t_stage *stage;
683         t_chsattr_params *csp;
684
685         stage=mdrun->stage.current->data;
686         csp=stage->params;
687
688         if(csp->type&CHSATTR_PCTRL) {
689                 if(csp->ptau>0)
690                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
691                 else
692                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
693         }
694         if(csp->type&CHSATTR_TCTRL) {
695                 if(csp->ttau>0)
696                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
697                 else
698                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
699         }
700         if(csp->type&CHSATTR_PRELAX) {
701                 if(csp->dp<0)
702                         mdrun->sattr&=(~(SATTR_PRELAX));
703                 else
704                         mdrun->sattr|=SATTR_PRELAX;
705                 mdrun->dp=csp->dp;
706         }
707         if(csp->type&CHSATTR_TRELAX) {
708                 if(csp->dt<0)
709                         mdrun->sattr&=(~(SATTR_TRELAX));
710                 else
711                         mdrun->sattr|=SATTR_TRELAX;
712                 mdrun->dt=csp->dt;
713         }
714         if(csp->type&CHSATTR_AVGRST) {
715                 if(csp->avgrst)
716                         mdrun->sattr|=SATTR_AVGRST;
717                 else
718                         mdrun->sattr&=(~(SATTR_AVGRST));
719         }
720         if(csp->type&CHSATTR_RSTEPS) {
721                 mdrun->relax_steps=csp->rsteps;
722         }
723
724         return 0;
725 }
726
727 #define stage_print(m)  if(!(stage->executed)) \
728                                 printf("%s",m)
729
730 int mdrun_hook(void *ptr1,void *ptr2) {
731
732         t_moldyn *moldyn;
733         t_mdrun *mdrun;
734         t_stage *stage;
735         t_list *sl;
736         int steps;
737         double tau;
738         u8 change_stage;
739
740         t_insert_atoms_params *iap;
741         t_continue_params *cp;
742         t_anneal_params *ap;
743
744         moldyn=ptr1;
745         mdrun=ptr2;
746
747         sl=&(mdrun->stage);
748
749         change_stage=FALSE;
750
751         /* return immediately if there are no more stage */
752         if(sl->current==NULL)
753                 return 0;
754
755         /* get stage description */
756         stage=sl->current->data;
757
758         /* default steps and tau values */
759         steps=mdrun->relax_steps;
760         tau=mdrun->timestep;
761
762         /* check whether relaxation steps are necessary */
763         if((check_pressure(moldyn,mdrun)==TRUE)&\
764            (check_temperature(moldyn,mdrun)==TRUE)) {
765         
766                 /* be verbose */
767                 stage_print("\n###########################\n");
768                 stage_print("# [mdrun] executing stage #\n");
769                 stage_print("###########################\n\n");
770                 
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);
776                                 change_stage=TRUE;
777                                 break;
778                         case STAGE_INSERT_ATOMS:
779                                 stage_print("  -> insert atoms\n\n");
780                                 iap=stage->params;
781                                 if(iap->cnt_steps==iap->ins_steps) {
782                                         change_stage=TRUE;
783                                         break;
784                                 }
785                                 insert_atoms(moldyn,mdrun);
786                                 iap->cnt_steps+=1;
787                                 break;
788                         case STAGE_CONTINUE:
789                                 stage_print("  -> continue\n\n");
790                                 if(stage->executed==TRUE) {
791                                         change_stage=TRUE;
792                                         break;
793                                 }
794                                 cp=stage->params;
795                                 steps=cp->runs;
796                                 break;
797                         case STAGE_ANNEAL:
798                                 stage_print("  -> anneal\n\n");
799                                 ap=stage->params;
800                                 if(ap->count==ap->runs) {
801                                         change_stage=TRUE;
802                                         break;
803                                 }
804                                 if(moldyn->t_ref+ap->dt>=0.0)
805                                         set_temperature(moldyn,
806                                                         moldyn->t_ref+ap->dt);
807                                 ap->count+=1;
808                                 break;
809                         case STAGE_CHAATTR:
810                                 stage_print("  -> chaattr\n\n");
811                                 chaatr(moldyn,mdrun);
812                                 change_stage=TRUE;
813                                 break;
814                         case STAGE_CHSATTR:
815                                 stage_print("  -> chsattr\n\n");
816                                 chsattr(moldyn,mdrun);
817                                 change_stage=TRUE;
818                                 break;
819                         default:
820                                 printf("%s unknwon stage type\n",ME);
821                                 break;
822                 }
823         
824                 /* mark as executed */
825                 stage->executed=TRUE;
826         
827                 /* change state */
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);
832                                 return 0;
833                         }
834                         steps=0;
835                         tau=mdrun->timestep;
836                 }
837
838         }
839         else {
840
841                 /* averages */
842                 if(mdrun->sattr&SATTR_AVGRST)
843                         average_reset(moldyn);
844
845         }
846
847         /* continue simulation */
848         moldyn_add_schedule(moldyn,steps,tau);
849
850         return 0;
851 }
852
853 int main(int argc,char **argv) {
854
855         t_mdrun mdrun;
856         t_moldyn moldyn;
857         t_3dvec o;
858
859         /* clear structs */
860         memset(&mdrun,0,sizeof(t_mdrun));
861         memset(&moldyn,0,sizeof(t_moldyn));
862
863         /* parse arguments */
864         if(mdrun_parse_argv(&mdrun,argc,argv)<0)
865                 return -1;
866
867         /* initialize list system */
868         list_init_f(&(mdrun.stage));
869
870         /* parse config file */
871         mdrun_parse_config(&mdrun);
872
873         /* reset the stage list */
874         list_reset_f(&(mdrun.stage));
875
876         /* sanity check! */
877
878         /* prepare simulation */
879         moldyn_init(&moldyn,argc,argv);
880         if(set_int_alg(&moldyn,mdrun.intalgo)<0)
881                 return -1;
882         set_cutoff(&moldyn,mdrun.cutoff);
883         if(set_potential(&moldyn,mdrun.potential)<0)
884                 return -1;
885         switch(mdrun.potential) {
886                 case MOLDYN_POTENTIAL_AM:
887                         albe_mult_set_params(&moldyn,
888                                              mdrun.element1,
889                                              mdrun.element2);
890                         break;
891                 case MOLDYN_POTENTIAL_TM:
892                         tersoff_mult_set_params(&moldyn,
893                                                 mdrun.element1,
894                                                 mdrun.element2);
895                         break;
896                 case MOLDYN_POTENTIAL_HO:
897                         harmonic_oscillator_set_params(&moldyn,mdrun.element1);
898                         break;
899                 case MOLDYN_POTENTIAL_LJ:
900                         lennard_jones_set_params(&moldyn,mdrun.element1);
901                         break;
902                 default:
903                         printf("%s unknown potential: %02x\n",
904                                ME,mdrun.potential);
905                         return -1;
906         }
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) {
910                 case FCC:
911                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
912                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
913                                        mdrun.ly,mdrun.lz,NULL);
914                         break;
915                 case DIAMOND:
916                         create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
917                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
918                                        mdrun.ly,mdrun.lz,NULL);
919                         break;
920                 case ZINCBLENDE:
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);
929                         break;
930                 default:
931                         printf("%s unknown lattice type: %02x\n",
932                                ME,mdrun.lattice);
933                         return -1;
934         }
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");
942         if(mdrun.elog)
943                 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
944         if(mdrun.tlog)
945                 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
946         if(mdrun.plog)
947                 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
948         if(mdrun.vlog)
949                 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
950         if(mdrun.visualize)
951                 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
952         if(mdrun.save)
953                 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
954         moldyn_set_log(&moldyn,CREATE_REPORT,0);
955         set_avg_skip(&moldyn,mdrun.avgskip);
956
957         /* prepare the hook function */
958         moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
959
960         /* run the simulation */
961         moldyn_integrate(&moldyn);
962
963         /* shutdown */
964         moldyn_shutdown(&moldyn);
965         del_stages(&mdrun);
966         list_destroy_f(&(mdrun.stage));
967
968         return 0;
969 }