]> hackdaworld.org Git - physik/posic.git/blob - mdrun.c
added Posselt formation energy calc
[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 /* pse */
21 #define PSE_MASS
22 #include "pse.h"
23 #undef PSE_MASS
24
25 #define ME      "[mdrun]"
26
27 /*
28  * parse function
29  */
30
31 int mdrun_usage(void) {
32
33         printf("%s usage:\n",ME);
34
35         return 0;
36 }
37
38 int mdrun_parse_argv(t_mdrun *mdrun,int argc,char **argv) {
39
40         int i;
41
42         for(i=1;i<argc;i++) {
43
44                 if(argv[i][0]!='-') {
45                         printf("%s unknown switch: %s\n",ME,argv[i]);
46                         return -1;
47                 }
48
49                 switch(argv[i][1]) {
50                         case 'c':
51                                 strncpy(mdrun->cfile,argv[++i],128);
52                                 break;
53                         case 's':
54                                 strncpy(mdrun->sdir,argv[++i],128);
55                                 break;
56                         case 'h':
57                                 mdrun_usage();
58                                 break;
59                         default:
60                                 printf("%s unknown option: %s\n",ME,argv[i]);
61                                 mdrun_usage();
62                                 return -1;
63                 }
64
65         }
66
67         return 0;
68 }
69
70 int del_stages(t_mdrun *mdrun) {
71
72         t_list *sl;
73         t_stage *stage;
74
75         sl=&(mdrun->stage);
76
77         list_reset_f(sl);
78
79         if(sl->start==NULL)
80                 return 0;
81
82         do {
83                 stage=sl->current->data;
84                 free(stage->params);
85                 free(stage);
86         } while(list_next_f(sl)!=L_NO_NEXT_ELEMENT);
87
88         return 0;
89 }
90
91 int add_stage(t_mdrun *mdrun,u8 type,void *params) {
92
93         int psize;
94
95         t_stage *stage;
96
97         switch(type) {
98                 case STAGE_DISPLACE_ATOM:
99                         psize=sizeof(t_displace_atom_params);
100                         break;
101                 case STAGE_DEL_ATOMS:
102                         psize=sizeof(t_del_atoms_params);
103                         break;
104                 case STAGE_INSERT_ATOMS:
105                         psize=sizeof(t_insert_atoms_params);
106                         break;
107                 case STAGE_INSERT_MIXED_ATOMS:
108                         psize=sizeof(t_insert_mixed_atoms_params);
109                         break;
110                 case STAGE_CONTINUE:
111                         psize=sizeof(t_continue_params);
112                         break;
113                 case STAGE_ANNEAL:
114                         psize=sizeof(t_anneal_params);
115                         break;
116                 case STAGE_CHAATTR:
117                         psize=sizeof(t_chaattr_params);
118                         break;
119                 case STAGE_CHSATTR:
120                         psize=sizeof(t_chsattr_params);
121                         break;
122                 case STAGE_SET_TEMP:
123                         psize=sizeof(t_set_temp_params);
124                         break;
125                 case STAGE_SET_TIMESTEP:
126                         psize=sizeof(t_set_timestep_params);
127                         break;
128                 case STAGE_FILL:
129                         psize=sizeof(t_fill_params);
130                         break;
131                 case STAGE_THERMAL_INIT:
132                         psize=0;
133                         break;
134                 default:
135                         printf("%s unknown stage type: %02x\n",ME,type);
136                         return -1;
137         }
138
139         stage=malloc(sizeof(t_stage));
140         if(stage==NULL) {
141                 perror("[mdrun] malloc (add stage)");
142                 return -1;
143         }
144
145         stage->type=type;
146         stage->executed=FALSE;
147
148         stage->params=malloc(psize);
149         if(stage->params==NULL) {
150                 perror("[mdrun] malloc (stage params)");
151                 return -1;
152         }
153
154         memcpy(stage->params,params,psize);
155
156         list_add_immediate_f(&(mdrun->stage),stage);
157
158         return 0;
159 }
160
161 int mdrun_parse_config(t_mdrun *mdrun) {
162
163         int fd,ret;
164         char error[128];
165         char line[128];
166         char *wptr;
167         char word[32][64];
168         int wcnt;
169         int i,o;
170
171         t_displace_atom_params dap;
172         t_insert_atoms_params iap;
173         t_insert_mixed_atoms_params imp;
174         t_continue_params cp;
175         t_anneal_params ap;
176         t_chaattr_params cap;
177         t_chsattr_params csp;
178         t_set_temp_params stp;
179         t_set_timestep_params stsp;
180         t_fill_params fp;
181         t_del_atoms_params delp;
182
183         /* open config file */
184         fd=open(mdrun->cfile,O_RDONLY);
185         if(fd<0) {
186                 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
187                 perror(error);
188                 return fd;
189         }
190
191         /* read, parse, set */
192         ret=1;
193         while(ret>0) {
194
195                 /* read */
196                 ret=get_line(fd,line,128);
197
198                 /* parse */
199
200                 // ignore # lines and \n
201                 if((line[0]=='#')|(ret==1))
202                         continue;
203
204                 // reset
205                 memset(&iap,0,sizeof(t_insert_atoms_params));
206                 memset(&imp,0,sizeof(t_insert_mixed_atoms_params));
207                 memset(&cp,0,sizeof(t_continue_params));
208                 memset(&ap,0,sizeof(t_anneal_params));
209                 memset(&cap,0,sizeof(t_chaattr_params));
210                 memset(&csp,0,sizeof(t_chsattr_params));
211                 memset(&stp,0,sizeof(t_set_temp_params));
212                 memset(&stsp,0,sizeof(t_set_timestep_params));
213                 memset(&fp,0,sizeof(t_fill_params));
214                 memset(&delp,0,sizeof(t_del_atoms_params));
215
216                 // get command + args
217                 wcnt=0;
218                 while(1) {
219                         if(wcnt)
220                                 wptr=strtok(NULL," \t");
221                         else
222                                 wptr=strtok(line," \t");
223                         if(wptr==NULL)
224                                 break;
225                         strncpy(word[wcnt],wptr,64);
226                         wcnt+=1;
227                 }
228
229                 if(!strncmp(word[0],"potential",9)) {
230                         if(!strncmp(word[1],"albe",4))
231                                 mdrun->potential=MOLDYN_POTENTIAL_AM;
232                         if(!strncmp(word[1],"tersoff",7))
233                                 mdrun->potential=MOLDYN_POTENTIAL_TM;
234                         if(!strncmp(word[1],"ho",2))
235                                 mdrun->potential=MOLDYN_POTENTIAL_HO;
236                         if(!strncmp(word[1],"lj",2))
237                                 mdrun->potential=MOLDYN_POTENTIAL_LJ;
238                 }
239                 else if(!strncmp(word[0],"continue",8))
240                         strncpy(mdrun->continue_file,word[1],128);
241                 else if(!strncmp(word[0],"cutoff",6))
242                         mdrun->cutoff=atof(word[1]);
243                 else if(!strncmp(word[0],"nnd",3))
244                         mdrun->nnd=atof(word[1]);
245                 else if(!strncmp(word[0],"intalgo",7)) {
246                         if(!strncmp(word[1],"verlet",5))
247                                 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
248                 }
249                 else if(!strncmp(word[0],"timestep",8))
250                         mdrun->timestep=atof(word[1]);
251                 else if(!strncmp(word[0],"volume",6)) {
252                         mdrun->dim.x=atof(word[1]);
253                         mdrun->dim.y=atof(word[2]);
254                         mdrun->dim.z=atof(word[3]);
255                         if(strncmp(word[4],"0",1))
256                                 mdrun->vis=TRUE;
257                 }
258                 else if(!strncmp(word[0],"pbc",3)) {
259                         if(strncmp(word[1],"0",1))
260                                 mdrun->pbcx=TRUE;
261                         else
262                                 mdrun->pbcx=FALSE;
263                         if(strncmp(word[2],"0",1))
264                                 mdrun->pbcy=TRUE;
265                         else
266                                 mdrun->pbcy=FALSE;
267                         if(strncmp(word[3],"0",1))
268                                 mdrun->pbcz=TRUE;
269                         else
270                                 mdrun->pbcz=FALSE;
271                 }
272                 else if(!strncmp(word[0],"temperature",11))
273                         mdrun->temperature=atof(word[1]);
274                 else if(!strncmp(word[0],"pressure",8))
275                         mdrun->pressure=atof(word[1]);
276                 else if(!strncmp(word[0],"lattice",7)) {
277                         if(!strncmp(word[1],"zincblende",10))
278                                 mdrun->lattice=ZINCBLENDE;
279                         if(!strncmp(word[1],"fcc",3))
280                                 mdrun->lattice=FCC;
281                         if(!strncmp(word[1],"diamond",7))
282                                 mdrun->lattice=DIAMOND;
283                         if(!strncmp(word[1],"none",4))
284                                 mdrun->lattice=NONE;
285                         if(wcnt==3)
286                                 mdrun->lc=atof(word[2]);
287                 }
288                 else if(!strncmp(word[0],"element1",8)) {
289                         mdrun->element1=atoi(word[1]);
290                 }
291                 else if(!strncmp(word[0],"element2",8)) {
292                         mdrun->element2=atoi(word[1]);
293                 }
294                 else if(!strncmp(word[0],"fill",6)) {
295                         // default values
296                         fp.fill_element=mdrun->element1;
297                         fp.fill_brand=0;
298                         fp.lattice=mdrun->lattice;
299                         fp.p_params.type=0;
300                         fp.d_params.type=0;
301                         fp.d_params.stype=0;
302                         // parse fill command
303                         i=1;
304                         while(i<wcnt) {
305                                 if(!strncmp(word[i],"lc",2)) {
306                                         fp.lx=atoi(word[++i]);
307                                         fp.ly=atoi(word[++i]);
308                                         fp.lz=atoi(word[++i]);
309                                         fp.lc=atof(word[++i]);
310                                         mdrun->lc=fp.lc;
311                                 }
312                                 if(!strncmp(word[i],"eb",2)) {
313                                         fp.fill_element=atoi(word[++i]);
314                                         fp.fill_brand=atoi(word[++i]);
315                                 }
316                                 if(word[i][0]=='p') {
317                                         i+=1;
318                                         switch(word[i][0]) {
319                                         case 'i':
320                                                 if(word[i][1]=='r')
321                                                 fp.p_params.type=PART_INSIDE_R;
322                                                 else
323                                                 fp.p_params.type=PART_INSIDE_D;
324                                                 break;
325                                         case 'o':
326                                                 if(word[i][1]=='r')
327                                                 fp.p_params.type=PART_OUTSIDE_R;
328                                                 else
329                                                 fp.p_params.type=PART_OUTSIDE_D;
330                                                 break;
331                                         default:
332                                                 break;
333                                         }
334                                         if((fp.p_params.type==PART_INSIDE_R)||
335                                           (fp.p_params.type==PART_OUTSIDE_R)) {
336                                                 fp.p_params.r=atof(word[++i]);  
337                                                 fp.p_params.p.x=atof(word[++i]);
338                                                 fp.p_params.p.y=atof(word[++i]);
339                                                 fp.p_params.p.z=atof(word[++i]);
340                                         }
341                                         if((fp.p_params.type==PART_INSIDE_D)||
342                                            (fp.p_params.type==PART_OUTSIDE_D)) {
343                                                 fp.p_params.p.x=atof(word[++i]);
344                                                 fp.p_params.p.y=atof(word[++i]);
345                                                 fp.p_params.p.z=atof(word[++i]);
346                                                 fp.p_params.d.x=atof(word[++i]);
347                                                 fp.p_params.d.y=atof(word[++i]);
348                                                 fp.p_params.d.z=atof(word[++i]);
349                                         }
350                                 }
351                                 if(word[i][0]=='d') {
352                                         switch(word[++i][0]) {
353                                                 case '0':
354
355                                 fp.d_params.type=DEFECT_TYPE_0D;
356                                 if(!strncmp(word[i+1],"dbx",3)) {
357                                         fp.d_params.stype=DEFECT_STYPE_DB_X;
358                                 }
359                                 if(!strncmp(word[i+1],"dby",3)) {
360                                         fp.d_params.stype=DEFECT_STYPE_DB_Y;
361                                 }
362                                 if(!strncmp(word[i+1],"dbz",3)) {
363                                         fp.d_params.stype=DEFECT_STYPE_DB_Z;
364                                 }
365                                 if(!strncmp(word[i+1],"dbr",3)) {
366                                         fp.d_params.stype=DEFECT_STYPE_DB_R;
367                                 }
368                                 i+=1;
369                                 fp.d_params.od=atof(word[++i]);
370                                 fp.d_params.dd=atof(word[++i]);
371                                 fp.d_params.element=atoi(word[++i]);
372                                 fp.d_params.brand=atoi(word[++i]);
373                                 // parsed in future
374                 fp.d_params.attr=ATOM_ATTR_HB|ATOM_ATTR_VA;
375                 fp.d_params.attr|=ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
376                                 break;
377
378                                                 case '1':
379                                 fp.d_params.type=DEFECT_TYPE_1D;
380                                 break;
381                                                 case '2':
382                                 fp.d_params.type=DEFECT_TYPE_2D;
383                                 break;
384                                                 case '3':
385                                 fp.d_params.type=DEFECT_TYPE_3D;
386                                 break;
387                                                 default:
388                                                         break;
389                                         }
390
391                                 }
392                                 i+=1;
393                         }
394                         add_stage(mdrun,STAGE_FILL,&fp);
395                 }
396                 else if(!strncmp(word[0],"thermal_init",12)) {
397                         add_stage(mdrun,STAGE_THERMAL_INIT,NULL);
398                 }
399                 else if(!strncmp(word[0],"aattr",5)) {
400                         // for aatrib line we need a special stage
401                         // containing one schedule of 0 loops ...
402                         for(i=0;i<strlen(word[1]);i++) {
403                                 switch(word[1][i]) {
404                                         case 't':
405                                                 cap.type|=CHAATTR_TOTALV;
406                                                 break;
407                                         case 'r':
408                                                 cap.type|=CHAATTR_REGION;
409                                                 break;
410                                         case 'e':
411                                                 cap.type|=CHAATTR_ELEMENT;
412                                                 break;
413                                         default:
414                                                 break;
415                                 }
416                         }
417                         i=2;
418                         if(cap.type&CHAATTR_REGION) {
419                                 cap.x0=atof(word[1]);   
420                                 cap.y0=atof(word[2]);   
421                                 cap.z0=atof(word[3]);   
422                                 cap.x1=atof(word[4]);   
423                                 cap.y1=atof(word[5]);   
424                                 cap.z1=atof(word[6]);
425                                 i+=6;
426                         }
427                         if(cap.type&CHAATTR_ELEMENT) {
428                                 cap.element=atoi(word[i]);
429                                 i+=1;
430                         }
431                         for(o=0;o<strlen(word[i]);o++) {
432                                 switch(word[i][o]) {
433                                         case 'b':
434                                                 cap.attr|=ATOM_ATTR_VB;
435                                                 break;
436                                         case 'h':
437                                                 cap.attr|=ATOM_ATTR_HB;
438                                                 break;
439                                         case 'v':
440                                                 cap.attr|=ATOM_ATTR_VA;
441                                                 break;
442                                         case 'f':
443                                                 cap.attr|=ATOM_ATTR_FP;
444                                                 break;
445                                         case '1':
446                                                 cap.attr|=ATOM_ATTR_1BP;
447                                                 break;
448                                         case '2':
449                                                 cap.attr|=ATOM_ATTR_2BP;
450                                                 break;
451                                         case '3':
452                                                 cap.attr|=ATOM_ATTR_3BP;
453                                                 break;
454                                         default:
455                                                 break;
456                                 }
457                         }
458                         add_stage(mdrun,STAGE_CHAATTR,&cap);
459                 }
460                 else if(!strncmp(word[0],"sattr",5)) {
461                         // for satrib line we need a special stage
462                         // containing one schedule of 0 loops ...
463                         csp.type=0;
464                         for(i=1;i<wcnt;i++) {
465                                 if(!strncmp(word[i],"pctrl",5)) {
466                                         csp.ptau=0.01/(atof(word[++i])*GPA);
467                                         csp.type|=CHSATTR_PCTRL;
468                                 }
469                                 if(!strncmp(word[i],"tctrl",5)) {
470                                         csp.ttau=atof(word[++i]);
471                                         csp.type|=CHSATTR_TCTRL;
472                                 }
473                                 if(!strncmp(word[i],"prelax",6)) {
474                                         csp.dp=atof(word[++i])*BAR;
475                                         csp.type|=CHSATTR_PRELAX;
476                                 }
477                                 if(!strncmp(word[i],"trelax",6)) {
478                                         csp.dt=atof(word[++i]);
479                                         csp.type|=CHSATTR_TRELAX;
480                                 }
481                                 if(!strncmp(word[i],"rsteps",6)) {
482                                         csp.rsteps=atoi(word[++i]);
483                                         csp.type|=CHSATTR_RSTEPS;
484                                 }
485                                 if(!strncmp(word[i],"avgrst",6)) {
486                                         csp.avgrst=atoi(word[++i]);
487                                         csp.type|=CHSATTR_AVGRST;
488                                 }
489                         }
490                         add_stage(mdrun,STAGE_CHSATTR,&csp);
491                 }
492                 else if(!strncmp(word[0],"prerun",6))
493                         mdrun->prerun=atoi(word[1]);
494                 else if(!strncmp(word[0],"avgskip",7))
495                         mdrun->avgskip=atoi(word[1]);
496                 else if(!strncmp(word[0],"elog",4))
497                         mdrun->elog=atoi(word[1]);
498                 else if(!strncmp(word[0],"tlog",4))
499                         mdrun->tlog=atoi(word[1]);
500                 else if(!strncmp(word[0],"plog",4))
501                         mdrun->plog=atoi(word[1]);
502                 else if(!strncmp(word[0],"vlog",4))
503                         mdrun->vlog=atoi(word[1]);
504                 else if(!strncmp(word[0],"save",4))
505                         mdrun->save=atoi(word[1]);
506                 else if(!strncmp(word[0],"visualize",9))
507                         mdrun->visualize=atoi(word[1]);
508                 else if(!strncmp(word[0],"stage",5)) {
509                         // for every stage line, add a stage
510                         if(!strncmp(word[1],"displace",8)) {
511                                 dap.nr=atoi(word[2]);   
512                                 dap.dx=atof(word[3]);
513                                 dap.dy=atof(word[4]);
514                                 dap.dz=atof(word[5]);
515                                 add_stage(mdrun,STAGE_DISPLACE_ATOM,&dap);
516                         }
517                         else if(!strncmp(word[1],"del_atoms",9)) {
518                                 delp.o.x=atof(word[2]);
519                                 delp.o.y=atof(word[3]);
520                                 delp.o.z=atof(word[4]);
521                                 delp.r=atof(word[5]);
522                                 add_stage(mdrun,STAGE_DEL_ATOMS,&delp);
523                         }
524                         else if(!strncmp(word[1],"ins_atoms",9)) {
525                                 iap.ins_steps=atoi(word[2]);
526                                 iap.ins_atoms=atoi(word[3]);
527                                 iap.element=atoi(word[4]);
528                                 iap.brand=atoi(word[5]);
529                                 for(i=0;i<strlen(word[6]);i++) {
530                                         switch(word[6][i]) {
531                                                 case 'b':
532                                                         iap.attr|=ATOM_ATTR_VB;
533                                                         break;
534                                                 case 'h':
535                                                         iap.attr|=ATOM_ATTR_HB;
536                                                         break;
537                                                 case 'v':
538                                                         iap.attr|=ATOM_ATTR_VA;
539                                                         break;
540                                                 case 'f':
541                                                         iap.attr|=ATOM_ATTR_FP;
542                                                         break;
543                                                 case '1':
544                                                         iap.attr|=ATOM_ATTR_1BP;
545                                                         break;
546                                                 case '2':
547                                                         iap.attr|=ATOM_ATTR_2BP;
548                                                         break;
549                                                 case '3':
550                                                         iap.attr|=ATOM_ATTR_3BP;
551                                                         break;
552                                                 default:
553                                                         break;
554                                         }
555                                 }
556                                 switch(word[7][0]) {
557                                         // insertion types
558                                         case 'p':
559                                                 iap.type=INS_POS;
560                                                 iap.x0=atof(word[8]);
561                                                 iap.y0=atof(word[9]);
562                                                 iap.z0=atof(word[10]);
563                                                 break;
564                                         case 'r':
565                                                 switch(word[8][0]) {
566
567                                                 case 't':
568                                                         iap.type=INS_TOTAL;
569                                                         iap.cr=atof(word[9]);
570                                                         break;
571                                                 case 'r':
572                                                         iap.type=INS_RECT;
573                                                         iap.x0=atof(word[9]);
574                                                         iap.y0=atof(word[10]);
575                                                         iap.z0=atof(word[11]);
576                                                         iap.x1=atof(word[12]);
577                                                         iap.y1=atof(word[13]);
578                                                         iap.z1=atof(word[14]);
579                                                         iap.cr=atof(word[15]);
580                                                         break;
581                                                 case 's':
582                                                         iap.type=INS_SPHERE;
583                                                         iap.x0=atof(word[9]);
584                                                         iap.y0=atof(word[10]);
585                                                         iap.z0=atof(word[11]);
586                                                         iap.x1=atof(word[12]);
587                                                         iap.cr=atof(word[13]);
588                                                         break;
589                                                 default:
590                                                         break;
591                                                 }
592                                         default:
593                                                 break;
594                                 }
595                                 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
596                         }
597
598
599                         // HERE WE GO ...
600
601                         else if(!strncmp(word[1],"ins_m_atoms",11)) {
602                                 imp.element1=atoi(word[2]);
603                                 imp.element2=atoi(word[3]);
604                                 imp.amount1=atoi(word[4]);
605                                 imp.amount2=atoi(word[5]);
606                                 imp.brand1=atoi(word[6]);
607                                 imp.brand2=atoi(word[7]);
608                                 imp.crmin=atof(word[8]);
609                                 imp.crmax=atof(word[9]);
610                                 /* do this later ...
611                                 for(i=0;i<strlen(word[8]);i++) {
612                                         switch(word[8][i]) {
613                                                 case 'b':
614                                                         imp.attr|=ATOM_ATTR_VB;
615                                                         break;
616                                                 case 'h':
617                                                         imp.attr|=ATOM_ATTR_HB;
618                                                         break;
619                                                 case 'v':
620                                                         imp.attr|=ATOM_ATTR_VA;
621                                                         break;
622                                                 case 'f':
623                                                         imp.attr|=ATOM_ATTR_FP;
624                                                         break;
625                                                 case '1':
626                                                         imp.attr|=ATOM_ATTR_1BP;
627                                                         break;
628                                                 case '2':
629                                                         imp.attr|=ATOM_ATTR_2BP;
630                                                         break;
631                                                 case '3':
632                                                         imp.attr|=ATOM_ATTR_3BP;
633                                                         break;
634                                                 default:
635                                                         break;
636                                         }
637                                 }
638                                 */
639                                 imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
640                                 imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
641                                 add_stage(mdrun,STAGE_INSERT_MIXED_ATOMS,&imp);
642                         }
643
644
645
646
647
648                         else if(!strncmp(word[1],"continue",8)) {
649                                 cp.runs=atoi(word[2]);
650                                 add_stage(mdrun,STAGE_CONTINUE,&cp);
651                         }
652                         else if(!strncmp(word[1],"anneal",6)) {
653                                 ap.count=0;
654                                 ap.runs=atoi(word[2]);
655                                 ap.dt=atof(word[3]);
656                                 ap.interval=atoi(word[4]);
657                                 add_stage(mdrun,STAGE_ANNEAL,&ap);
658                         }
659                         else if(!strncmp(word[1],"set_temp",8)) {
660                                 if(word[2][0]=='c') {
661                                         stp.type=SET_TEMP_CURRENT;
662                                         stp.val=0.0;
663                                 }
664                                 else {
665                                         stp.type=SET_TEMP_VALUE;
666                                         stp.val=atof(word[2]);
667                                 }
668                                 add_stage(mdrun,STAGE_SET_TEMP,&stp);
669                         }
670                         else if(!strncmp(word[1],"set_timestep",12)) {
671                                 stsp.tau=atof(word[2]);
672                                 add_stage(mdrun,STAGE_SET_TIMESTEP,&stsp);
673                         }
674                         else {
675                                 printf("%s unknown stage type: %s\n",
676                                        ME,word[1]);
677                                 return -1;
678                         }
679                 }
680                 else {
681                         printf("%s unknown keyword '%s', skipped!\n",
682                                ME,word[0]);
683                         continue;
684                 }
685         }
686
687         /* close file */
688         close(fd);
689
690         return 0;
691 }
692
693 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
694
695         double delta;
696
697         if(!(mdrun->sattr&SATTR_PRELAX)) {
698                 return TRUE;
699         }
700
701         delta=moldyn->p_avg-moldyn->p_ref;
702
703         if(delta<0)
704                 delta=-delta;
705
706         if(delta>mdrun->dp)
707                 return FALSE;
708
709         return TRUE;
710 }
711
712 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
713
714         double delta;
715
716         if(!(mdrun->sattr&SATTR_TRELAX))
717                 return TRUE;
718
719         delta=moldyn->t_avg-moldyn->t_ref;
720
721         if(delta<0)
722                 delta=-delta;
723
724         if(delta>mdrun->dt)
725                 return FALSE;
726
727         return TRUE;
728 }
729
730 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
731
732         t_displace_atom_params *dap;
733         t_stage *stage;
734         t_atom *atom;
735
736         stage=mdrun->stage.current->data;
737         dap=stage->params;
738
739         atom=&(moldyn->atom[dap->nr]);
740         atom->r.x+=dap->dx;
741         atom->r.y+=dap->dy;
742         atom->r.z+=dap->dz;
743
744         return 0;
745 }
746
747 int del_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
748
749         t_stage *stage;
750         t_del_atoms_params *delp;
751         int i;
752         t_3dvec dist;
753         
754         stage=mdrun->stage.current->data;
755         delp=stage->params;
756
757         for(i=0;i<moldyn->count;i++) {
758                 v3_sub(&dist,&(delp->o),&(moldyn->atom[i].r));
759 //printf("%d ----> %f %f %f = %f | %f\n",i,dist.x,dist.y,dist.z,v3_absolute_square(&dist),delp->r*delp->r);
760                 if(v3_absolute_square(&dist)<=(delp->r*delp->r)) {
761                         del_atom(moldyn,moldyn->atom[i].tag);
762                         printf("%s atom deleted: %d %d %d\n",ME,
763                                moldyn->atom[i].tag,moldyn->atom[i].element,
764                                moldyn->atom[i].brand);
765                 }
766         }
767
768         return 0;
769
770 }
771
772 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
773
774         t_insert_atoms_params *iap;
775         t_stage *stage;
776         t_atom *atom;
777         t_3dvec r,v,dist;
778         double d,dmin,o;
779         int cnt,i;
780         double x,y,z;
781         double x0,y0,z0;
782         u8 cr_check,run;
783         
784         stage=mdrun->stage.current->data;
785         iap=stage->params;
786
787         cr_check=FALSE;
788
789         v.x=0.0; v.y=0.0; v.z=0.0;
790         x=0; y=0; z=0;
791         o=0; dmin=0;
792
793         switch(mdrun->lattice) {
794                 case CUBIC:
795                         o=0.5*mdrun->lc;
796                         break;
797                 case FCC:
798                         o=0.25*mdrun->lc;
799                         break;
800                 case DIAMOND:
801                 case ZINCBLENDE:
802                         o=0.125*mdrun->lc;
803                         break;
804                 default:
805                         break;
806         }
807
808         switch(iap->type) {
809                 case INS_TOTAL:
810                         x=moldyn->dim.x;
811                         x0=-x/2.0;
812                         y=moldyn->dim.y;
813                         y0=-y/2.0;
814                         z=moldyn->dim.z;
815                         z0=-z/2.0;
816                         cr_check=TRUE;
817                         break;
818                 case INS_RECT:
819                         x=iap->x1-iap->x0;
820                         x0=iap->x0;
821                         y=iap->y1-iap->y0;
822                         y0=iap->y0;
823                         z=iap->z1-iap->z0;
824                         z0=iap->z0;
825                         cr_check=TRUE;
826                         break;
827                 case INS_SPHERE:
828                         x=2.0*iap->x1;
829                         x0=iap->x0-iap->x1;
830                         y=x;
831                         y0=iap->y0-iap->x1;
832                         z=x;
833                         z0=iap->z0-iap->x1;
834                         cr_check=TRUE;
835                         break;
836                 case INS_POS:
837                         x0=iap->x0;
838                         y0=iap->y0;
839                         z0=iap->z0;
840                         cr_check=FALSE;
841                         break;
842                 default:
843                         printf("%s unknown insertion mode: %02x\n",
844                                ME,iap->type);
845                         return -1;
846         }
847
848         cnt=0;
849         while(cnt<iap->ins_atoms) {
850                 run=1;
851                 while(run) {
852                         if(iap->type!=INS_POS) {
853                                 r.x=rand_get_double(&(moldyn->random))*x;
854                                 r.y=rand_get_double(&(moldyn->random))*y;
855                                 r.z=rand_get_double(&(moldyn->random))*z;
856                         }
857                         else {
858                                 r.x=0.0;
859                                 r.y=0.0;
860                                 r.z=0.0;
861                         }
862                         r.x+=x0;
863                         r.y+=y0;
864                         r.z+=z0;
865                         // offset
866                         if(iap->type!=INS_TOTAL) {
867                                 r.x+=o;
868                                 r.y+=o;
869                                 r.z+=o;
870                         }
871                         run=0;
872                         if(cr_check==TRUE) {
873                                 dmin=1000;      // for sure too high!
874                                 for(i=0;i<moldyn->count;i++) {
875                                         atom=&(moldyn->atom[i]);
876                                         v3_sub(&dist,&(atom->r),&r);
877                                         check_per_bound(moldyn,&dist);
878                                         d=v3_absolute_square(&dist);
879                                         if(d<(iap->cr*iap->cr)) {
880                                                 run=1;
881                                                 break;
882                                         }
883                                         if(d<dmin)
884                                                 dmin=d;
885                                 }
886                         }
887                         if(iap->type==INS_SPHERE) {
888                                 if((r.x-iap->x0)*(r.x-iap->x0)+
889                                    (r.y-iap->y0)*(r.y-iap->y0)+
890                                    (r.z-iap->z0)*(r.z-iap->z0)>
891                                    (iap->x1*iap->x1)) {
892                                         run=1;
893                                 }
894                         }
895                 }
896                 add_atom(moldyn,iap->element,
897                          iap->brand,iap->attr,&r,&v);
898                 printf("%s atom inserted (%d/%d): %f %f %f\n",
899                        ME,(iap->cnt_steps+1)*iap->ins_atoms,
900                        iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
901                 printf("  -> d2 = %f/%f\n",dmin,iap->cr*iap->cr);
902                 cnt+=1;
903         }
904
905         return 0;
906 }
907
908 int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
909
910         t_stage *stage;
911         t_insert_mixed_atoms_params *imp;
912         t_atom *atom;
913         double x,x0,y,y0,z,z0;
914         double dmin,d,cmin,cmax;
915         u8 retry;
916         t_3dvec r,v,dist;
917         int i;
918         
919
920         stage=mdrun->stage.current->data;
921         imp=stage->params;
922
923         x=moldyn->dim.x;
924         x0=-x/2.0;
925         y=moldyn->dim.y;
926         y0=-y/2.0;
927         z=moldyn->dim.z;
928         z0=-z/2.0;
929
930         v.x=0.0;
931         v.y=0.0;
932         v.z=0.0;
933
934         cmin=imp->crmin*imp->crmin;
935         cmax=imp->crmax*imp->crmax;
936
937         while(imp->amount1|imp->amount2) {
938                 if(imp->amount1) {
939                         retry=1;
940                         while(retry) {
941                                 retry=0;
942                                 r.x=rand_get_double(&(moldyn->random))*x;
943                                 r.y=rand_get_double(&(moldyn->random))*y;
944                                 r.z=rand_get_double(&(moldyn->random))*z;
945                                 r.x+=x0;
946                                 r.y+=y0;
947                                 r.z+=z0;
948                                 dmin=1000.0;    // for sure too high!
949                                 for(i=0;i<moldyn->count;i++) {
950                                         atom=&(moldyn->atom[i]);
951                                         v3_sub(&dist,&(atom->r),&r);
952                                         check_per_bound(moldyn,&dist);
953                                         d=v3_absolute_square(&dist);
954                                         if(d<cmin) {
955                                                 retry=1;
956                                                 break;
957                                         }
958                                         if(d<dmin)
959                                                 dmin=d;
960                                 }
961                                 if(dmin!=1000.0)
962                                         if(dmin>cmax)
963                                                 retry=1;
964                         }
965                         add_atom(moldyn,imp->element1,
966                                  imp->brand1,imp->attr1,&r,&v);
967                         printf("%s (mixed) atom inserted (%d): %f %f %f\n",
968                                ME,imp->amount1,r.x,r.y,r.z);
969                         printf("  -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
970                         imp->amount1-=1;
971                 }
972                 if(imp->amount2) {
973                         retry=1;
974                         while(retry) {
975                                 retry=0;
976                                 r.x=rand_get_double(&(moldyn->random))*x;
977                                 r.y=rand_get_double(&(moldyn->random))*y;
978                                 r.z=rand_get_double(&(moldyn->random))*z;
979                                 r.x+=x0;
980                                 r.y+=y0;
981                                 r.z+=z0;
982                                 dmin=1000.0;    // for sure too high!
983                                 for(i=0;i<moldyn->count;i++) {
984                                         atom=&(moldyn->atom[i]);
985                                         v3_sub(&dist,&(atom->r),&r);
986                                         check_per_bound(moldyn,&dist);
987                                         d=v3_absolute_square(&dist);
988                                         if(d<cmin) {
989                                                 retry=1;
990                                                 break;
991                                         }
992                                         if(d<dmin)
993                                                 dmin=d;
994                                 }
995                                 if(dmin!=1000.0)
996                                         if(dmin>cmax)
997                                                 retry=1;
998                         }
999                         add_atom(moldyn,imp->element2,
1000                                  imp->brand2,imp->attr2,&r,&v);
1001                         printf("%s (mixed) atom inserted (%d): %f %f %f\n",
1002                                ME,imp->amount2,r.x,r.y,r.z);
1003                         printf("  -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
1004                         imp->amount2-=1;
1005                 }
1006         }
1007
1008         return 0;
1009 }
1010
1011 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
1012
1013         t_stage *stage;
1014         t_chaattr_params *cap;
1015         t_atom *atom;
1016         int i;
1017
1018         stage=mdrun->stage.current->data;
1019         cap=stage->params;
1020
1021         for(i=0;i<moldyn->count;i++) {
1022                 atom=&(moldyn->atom[i]);
1023                 if(cap->type&CHAATTR_ELEMENT) {
1024                         if(cap->element!=atom->element)
1025                                 continue;
1026                 }
1027                 if(cap->type&CHAATTR_REGION) {
1028                         if(cap->x0<atom->r.x)
1029                                 continue;
1030                         if(cap->y0<atom->r.y)
1031                                 continue;
1032                         if(cap->z0<atom->r.z)
1033                                 continue;
1034                         if(cap->x1>atom->r.x)
1035                                 continue;
1036                         if(cap->y1>atom->r.y)
1037                                 continue;
1038                         if(cap->z1>atom->r.z)
1039                                 continue;
1040                 }
1041                 atom->attr=cap->attr;
1042         }
1043
1044         return 0;
1045 }
1046
1047 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
1048
1049         t_stage *stage;
1050         t_chsattr_params *csp;
1051
1052         stage=mdrun->stage.current->data;
1053         csp=stage->params;
1054
1055         if(csp->type&CHSATTR_PCTRL) {
1056                 if(csp->ptau>0)
1057                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
1058                 else
1059                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
1060         }
1061         if(csp->type&CHSATTR_TCTRL) {
1062                 if(csp->ttau>0)
1063                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
1064                 else
1065                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
1066         }
1067         if(csp->type&CHSATTR_PRELAX) {
1068                 if(csp->dp<0)
1069                         mdrun->sattr&=(~(SATTR_PRELAX));
1070                 else
1071                         mdrun->sattr|=SATTR_PRELAX;
1072                 mdrun->dp=csp->dp;
1073         }
1074         if(csp->type&CHSATTR_TRELAX) {
1075                 if(csp->dt<0)
1076                         mdrun->sattr&=(~(SATTR_TRELAX));
1077                 else
1078                         mdrun->sattr|=SATTR_TRELAX;
1079                 mdrun->dt=csp->dt;
1080         }
1081         if(csp->type&CHSATTR_AVGRST) {
1082                 if(csp->avgrst)
1083                         mdrun->sattr|=SATTR_AVGRST;
1084                 else
1085                         mdrun->sattr&=(~(SATTR_AVGRST));
1086         }
1087         if(csp->type&CHSATTR_RSTEPS) {
1088                 mdrun->relax_steps=csp->rsteps;
1089         }
1090
1091         return 0;
1092 }
1093
1094 #define stage_print(m)  if(!(stage->executed)) \
1095                                 printf("%s",m)
1096
1097 int mdrun_hook(void *ptr1,void *ptr2) {
1098
1099         t_moldyn *moldyn;
1100         t_mdrun *mdrun;
1101         t_stage *stage;
1102         t_list *sl;
1103         int steps;
1104         u8 change_stage;
1105         t_3dvec o;
1106
1107         t_insert_atoms_params *iap;
1108         t_insert_mixed_atoms_params *imp;
1109         t_continue_params *cp;
1110         t_anneal_params *ap;
1111         t_set_temp_params *stp;
1112         t_set_timestep_params *stsp;
1113         t_fill_params *fp;
1114
1115         moldyn=ptr1;
1116         mdrun=ptr2;
1117
1118         sl=&(mdrun->stage);
1119
1120         change_stage=FALSE;
1121
1122         /* return immediately if there are no more stage */
1123         if(sl->current==NULL)
1124                 return 0;
1125
1126         /* get stage description */
1127         stage=sl->current->data;
1128
1129         /* steps in next schedule */
1130         steps=mdrun->relax_steps;
1131
1132         /* check whether relaxation steps are necessary */
1133         if((check_pressure(moldyn,mdrun)==TRUE)&\
1134            (check_temperature(moldyn,mdrun)==TRUE)) {
1135         
1136                 /* be verbose */
1137                 stage_print("\n###########################\n");
1138                 stage_print("# [mdrun] executing stage #\n");
1139                 stage_print("###########################\n\n");
1140                 
1141                 /* stage specific stuff */
1142                 switch(stage->type) {
1143                         case STAGE_DISPLACE_ATOM:
1144                                 stage_print("  -> displace atom\n\n");
1145                                 displace_atom(moldyn,mdrun);
1146                                 change_stage=TRUE;
1147                                 break;
1148                         case STAGE_DEL_ATOMS:
1149                                 stage_print(" -> del atoms\n\n");
1150                                 del_atoms(moldyn,mdrun);
1151                                 change_stage=TRUE;
1152                                 break;
1153                         case STAGE_INSERT_ATOMS:
1154                                 stage_print("  -> insert atoms\n\n");
1155                                 iap=stage->params;
1156                                 if(iap->cnt_steps==iap->ins_steps) {
1157                                         change_stage=TRUE;
1158                                         break;
1159                                 }
1160                                 insert_atoms(moldyn,mdrun);
1161                                 iap->cnt_steps+=1;
1162                                 break;
1163
1164
1165
1166                         case STAGE_INSERT_MIXED_ATOMS:
1167                                 stage_print("  -> insert mixed atoms\n\n");
1168                                 imp=stage->params;
1169                                 insert_mixed_atoms(moldyn,mdrun);
1170                                 change_stage=TRUE;
1171                                 break;
1172
1173
1174
1175                         case STAGE_CONTINUE:
1176                                 stage_print("  -> continue\n\n");
1177                                 if(stage->executed==TRUE) {
1178                                         change_stage=TRUE;
1179                                         break;
1180                                 }
1181                                 cp=stage->params;
1182                                 steps=cp->runs;
1183                                 break;
1184                         case STAGE_ANNEAL:
1185                                 stage_print("  -> anneal\n\n");
1186                                 ap=stage->params;
1187                                 if(ap->count==ap->runs) {
1188                                         change_stage=TRUE;
1189                                         break;
1190                                 }
1191                                 if(moldyn->t_ref+ap->dt>=0.0)
1192                                         set_temperature(moldyn,
1193                                                         moldyn->t_ref+ap->dt);
1194                                 ap->count+=1;
1195                                 steps=ap->interval;
1196                                 break;
1197                         case STAGE_CHAATTR:
1198                                 stage_print("  -> change atom attributes\n\n");
1199                                 chaatr(moldyn,mdrun);
1200                                 change_stage=TRUE;
1201                                 break;
1202                         case STAGE_CHSATTR:
1203                                 stage_print("  -> change sys attributes\n\n");
1204                                 chsattr(moldyn,mdrun);
1205                                 change_stage=TRUE;
1206                                 break;
1207                         case STAGE_SET_TEMP:
1208                                 stage_print("  -> set temperature\n\n");
1209                                 stp=stage->params;
1210                                 if(stp->type&SET_TEMP_CURRENT) {
1211                                         set_temperature(moldyn,moldyn->t_avg);
1212                                 }
1213                                 else {
1214                                         set_temperature(moldyn,stp->val);
1215                                 }
1216                                 change_stage=TRUE;
1217                                 break;
1218                         case STAGE_SET_TIMESTEP:
1219                                 stage_print("  -> set timestep\n\n");
1220                                 stsp=stage->params;
1221                                 mdrun->timestep=stsp->tau;
1222                                 change_stage=TRUE;
1223                                 break;
1224                         case STAGE_FILL:
1225                                 stage_print("  -> fill lattice\n\n");
1226                                 fp=stage->params;
1227                                 switch(fp->lattice) {
1228                                         case ZINCBLENDE:
1229
1230                                         o.x=0.5*0.25*fp->lc;
1231                                         o.y=o.x;
1232                                         o.z=o.x;
1233                                         create_lattice(moldyn,
1234                                                        FCC,fp->lc,
1235                                                        mdrun->element1,
1236                                                        DEFAULT_ATOM_ATTR,0,
1237                                                        fp->lx,fp->ly,fp->lz,
1238                                                        &o,
1239                                                        &(fp->p_params),
1240                                                        &(fp->d_params));
1241                                         o.x+=0.25*fp->lc;
1242                                         o.y=o.x;
1243                                         o.z=o.x;
1244                                         create_lattice(moldyn,
1245                                                        FCC,fp->lc,
1246                                                        mdrun->element2,
1247                                                        DEFAULT_ATOM_ATTR,1,
1248                                                        fp->lx,fp->ly,fp->lz,
1249                                                        &o,
1250                                                        &(fp->p_params),
1251                                                        &(fp->d_params));
1252                                         break;
1253
1254                                         default:
1255
1256                                         create_lattice(moldyn,
1257                                                        fp->lattice,fp->lc,
1258                                                        mdrun->element1,
1259                                                        DEFAULT_ATOM_ATTR,0,
1260                                                        fp->lx,fp->ly,fp->lz,
1261                                                        NULL,
1262                                                        &(fp->p_params),
1263                                                        &(fp->d_params));
1264                                         break;
1265                                 }
1266                                 moldyn_bc_check(moldyn);
1267                                 change_stage=TRUE;
1268                                 break;
1269                         case STAGE_THERMAL_INIT:
1270                                 stage_print("  -> thermal init\n\n");
1271                                 thermal_init(moldyn,TRUE);
1272                                 change_stage=TRUE;
1273                                 break;
1274                         default:
1275                                 printf("%s unknwon stage type\n",ME);
1276                                 break;
1277                 }
1278         
1279                 /* mark as executed */
1280                 stage->executed=TRUE;
1281         
1282                 /* change state */
1283                 if(change_stage==TRUE) {
1284                         printf("%s finished stage\n",ME);
1285                         if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
1286                                 printf("%s no more stages\n",ME);
1287                                 return 0;
1288                         }
1289                         steps=0;
1290                 }
1291
1292         }
1293         else {
1294
1295                 /* averages */
1296                 if(mdrun->sattr&SATTR_AVGRST)
1297                         average_reset(moldyn);
1298
1299         }
1300
1301         /* continue simulation */
1302         moldyn_add_schedule(moldyn,steps,mdrun->timestep);
1303
1304         return 0;
1305 }
1306
1307 int main(int argc,char **argv) {
1308
1309         t_mdrun mdrun;
1310         t_moldyn moldyn;
1311         //t_3dvec o;
1312
1313         /* clear structs */
1314         memset(&mdrun,0,sizeof(t_mdrun));
1315         memset(&moldyn,0,sizeof(t_moldyn));
1316
1317         /* parse arguments */
1318         if(mdrun_parse_argv(&mdrun,argc,argv)<0)
1319                 return -1;
1320
1321         /* initialize list system */
1322         list_init_f(&(mdrun.stage));
1323
1324         /* parse config file */
1325         mdrun_parse_config(&mdrun);
1326
1327         /* reset the stage list */
1328         list_reset_f(&(mdrun.stage));
1329
1330         /* sanity check! */
1331
1332         /* prepare simulation */
1333
1334         if(mdrun.continue_file[0]) {
1335                 // read the save file
1336                 moldyn_read_save_file(&moldyn,mdrun.continue_file);
1337                 // manualaadjusting some stuff
1338                 moldyn.argc=argc;
1339                 moldyn.args=argv;
1340                 rand_init(&(moldyn.random),NULL,1);
1341                 moldyn.random.status|=RAND_STAT_VERBOSE;
1342         }
1343         else {
1344                 moldyn_init(&moldyn,argc,argv);
1345         }
1346         
1347         if(set_int_alg(&moldyn,mdrun.intalgo)<0)
1348                 return -1;
1349
1350         /* potential */
1351         set_cutoff(&moldyn,mdrun.cutoff);
1352         if(set_potential(&moldyn,mdrun.potential)<0)
1353                 return -1;
1354         switch(mdrun.potential) {
1355                 case MOLDYN_POTENTIAL_AM:
1356                         albe_mult_set_params(&moldyn,
1357                                              mdrun.element1,
1358                                              mdrun.element2);
1359                         break;
1360                 case MOLDYN_POTENTIAL_TM:
1361                         tersoff_mult_set_params(&moldyn,
1362                                                 mdrun.element1,
1363                                                 mdrun.element2);
1364                         break;
1365                 case MOLDYN_POTENTIAL_HO:
1366                         harmonic_oscillator_set_params(&moldyn,mdrun.element1);
1367                         break;
1368                 case MOLDYN_POTENTIAL_LJ:
1369                         lennard_jones_set_params(&moldyn,mdrun.element1);
1370                         break;
1371                 default:
1372                         printf("%s unknown potential: %02x\n",
1373                                ME,mdrun.potential);
1374                         return -1;
1375         }
1376
1377         /* if it is a continue run, reset schedule and skip lattice init */
1378         if(mdrun.continue_file[0]) {
1379                 memset(&(moldyn.schedule),0,sizeof(t_moldyn_schedule));
1380                 goto addsched;
1381         }
1382
1383         /* initial lattice and dimensions */
1384         set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
1385         set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
1386         /* replaced by fill stage !! 
1387         switch(mdrun.lattice) {
1388                 case FCC:
1389                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.fill_element,
1390                                        mdrun.m1,DEFAULT_ATOM_ATTR,
1391                                        mdrun.fill_brand,mdrun.lx,
1392                                        mdrun.ly,mdrun.lz,NULL,0,NULL);
1393                         break;
1394                 case DIAMOND:
1395                         create_lattice(&moldyn,DIAMOND,mdrun.lc,
1396                                        mdrun.fill_element,
1397                                        mdrun.m1,DEFAULT_ATOM_ATTR,
1398                                        mdrun.fill_brand,mdrun.lx,
1399                                        mdrun.ly,mdrun.lz,NULL,0,NULL);
1400                         break;
1401                 case ZINCBLENDE:
1402                         o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1403                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1404                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1405                                        mdrun.ly,mdrun.lz,&o,0,NULL);
1406                         o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1407                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
1408                                        mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
1409                                        mdrun.ly,mdrun.lz,&o,0,NULL);
1410                         break;
1411                 case NONE:
1412                         set_nn_dist(&moldyn,mdrun.nnd);
1413                         break;
1414                 default:
1415                         printf("%s unknown lattice type: %02x\n",
1416                                ME,mdrun.lattice);
1417                         return -1;
1418         }
1419         moldyn_bc_check(&moldyn);
1420         replaced by fill stage !! */
1421
1422         /* temperature and pressure */
1423         set_temperature(&moldyn,mdrun.temperature);
1424         set_pressure(&moldyn,mdrun.pressure);
1425         /* replaced thermal_init stage
1426         thermal_init(&moldyn,TRUE);
1427         */
1428
1429 addsched:
1430         /* first schedule */
1431         moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
1432
1433         /* log */
1434         moldyn_set_log_dir(&moldyn,mdrun.sdir);
1435         moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
1436         if(mdrun.elog)
1437                 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
1438         if(mdrun.tlog)
1439                 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
1440         if(mdrun.plog)
1441                 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
1442         if(mdrun.vlog)
1443                 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
1444         if(mdrun.visualize)
1445                 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
1446         if(mdrun.save)
1447                 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
1448         moldyn_set_log(&moldyn,CREATE_REPORT,0);
1449         set_avg_skip(&moldyn,mdrun.avgskip);
1450
1451         /* prepare the hook function */
1452         moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
1453
1454         /* run the simulation */
1455         moldyn_integrate(&moldyn);
1456
1457         /* shutdown */
1458         moldyn_shutdown(&moldyn);
1459         del_stages(&mdrun);
1460         list_destroy_f(&(mdrun.stage));
1461
1462         return 0;
1463 }