insert mixed atoms feature added (will be renamed an cleand up later)
[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_INSERT_MIXED_ATOMS:
100                         psize=sizeof(t_insert_mixed_atoms_params);
101                         break;
102                 case STAGE_CONTINUE:
103                         psize=sizeof(t_continue_params);
104                         break;
105                 case STAGE_ANNEAL:
106                         psize=sizeof(t_anneal_params);
107                         break;
108                 case STAGE_CHAATTR:
109                         psize=sizeof(t_chaattr_params);
110                         break;
111                 case STAGE_CHSATTR:
112                         psize=sizeof(t_chsattr_params);
113                         break;
114                 default:
115                         printf("%s unknown stage type: %02x\n",ME,type);
116                         return -1;
117         }
118
119         stage=malloc(sizeof(t_stage));
120         if(stage==NULL) {
121                 perror("[mdrun] malloc (add stage)");
122                 return -1;
123         }
124
125         stage->type=type;
126         stage->executed=FALSE;
127
128         stage->params=malloc(psize);
129         if(stage->params==NULL) {
130                 perror("[mdrun] malloc (stage params)");
131                 return -1;
132         }
133
134         memcpy(stage->params,params,psize);
135
136         list_add_immediate_f(&(mdrun->stage),stage);
137
138         return 0;
139 }
140
141 int mdrun_parse_config(t_mdrun *mdrun) {
142
143         int fd,ret;
144         char error[128];
145         char line[128];
146         char *wptr;
147         char word[16][64];
148         int wcnt;
149         int i,o;
150
151         t_displace_atom_params dap;
152         t_insert_atoms_params iap;
153         t_insert_mixed_atoms_params imp;
154         t_continue_params cp;
155         t_anneal_params ap;
156         t_chaattr_params cap;
157         t_chsattr_params csp;
158
159         /* open config file */
160         fd=open(mdrun->cfile,O_RDONLY);
161         if(fd<0) {
162                 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
163                 perror(error);
164                 return fd;
165         }
166
167         /* read, parse, set */
168         ret=1;
169         while(ret>0) {
170
171                 /* read */
172                 ret=get_line(fd,line,128);
173
174                 /* parse */
175
176                 // ignore # lines and \n
177                 if((line[0]=='#')|(ret==1))
178                         continue;
179
180                 // reset
181                 memset(&iap,0,sizeof(t_insert_atoms_params));
182                 memset(&imp,0,sizeof(t_insert_mixed_atoms_params));
183                 memset(&cp,0,sizeof(t_continue_params));
184                 memset(&ap,0,sizeof(t_anneal_params));
185                 memset(&cap,0,sizeof(t_chaattr_params));
186                 memset(&csp,0,sizeof(t_chsattr_params));
187
188                 // get command + args
189                 wcnt=0;
190                 while(1) {
191                         if(wcnt)
192                                 wptr=strtok(NULL," \t");
193                         else
194                                 wptr=strtok(line," \t");
195                         if(wptr==NULL)
196                                 break;
197                         strncpy(word[wcnt],wptr,64);
198                         wcnt+=1;
199                 }
200
201                 if(!strncmp(word[0],"potential",9)) {
202                         if(!strncmp(word[1],"albe",4))
203                                 mdrun->potential=MOLDYN_POTENTIAL_AM;
204                         if(!strncmp(word[1],"tersoff",7))
205                                 mdrun->potential=MOLDYN_POTENTIAL_TM;
206                         if(!strncmp(word[1],"ho",2))
207                                 mdrun->potential=MOLDYN_POTENTIAL_HO;
208                         if(!strncmp(word[1],"lj",2))
209                                 mdrun->potential=MOLDYN_POTENTIAL_LJ;
210                 }
211                 else if(!strncmp(word[0],"continue",8))
212                         strncpy(mdrun->continue_file,word[1],128);
213                 else if(!strncmp(word[0],"cutoff",6))
214                         mdrun->cutoff=atof(word[1]);
215                 else if(!strncmp(word[0],"nnd",3))
216                         mdrun->nnd=atof(word[1]);
217                 else if(!strncmp(word[0],"intalgo",7)) {
218                         if(!strncmp(word[1],"verlet",5))
219                                 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
220                 }
221                 else if(!strncmp(word[0],"timestep",8))
222                         mdrun->timestep=atof(word[1]);
223                 else if(!strncmp(word[0],"volume",6)) {
224                         mdrun->dim.x=atof(word[1]);
225                         mdrun->dim.y=atof(word[2]);
226                         mdrun->dim.z=atof(word[3]);
227                         if(strncmp(word[4],"0",1))
228                                 mdrun->vis=TRUE;
229                 }
230                 else if(!strncmp(word[0],"pbc",3)) {
231                         if(strncmp(word[1],"0",1))
232                                 mdrun->pbcx=TRUE;
233                         else
234                                 mdrun->pbcx=FALSE;
235                         if(strncmp(word[2],"0",1))
236                                 mdrun->pbcy=TRUE;
237                         else
238                                 mdrun->pbcy=FALSE;
239                         if(strncmp(word[3],"0",1))
240                                 mdrun->pbcz=TRUE;
241                         else
242                                 mdrun->pbcz=FALSE;
243                 }
244                 else if(!strncmp(word[0],"temperature",11))
245                         mdrun->temperature=atof(word[1]);
246                 else if(!strncmp(word[0],"pressure",8))
247                         mdrun->pressure=atof(word[1]);
248                 else if(!strncmp(word[0],"lattice",7)) {
249                         if(!strncmp(word[1],"zincblende",10))
250                                 mdrun->lattice=ZINCBLENDE;
251                         if(!strncmp(word[1],"fcc",3))
252                                 mdrun->lattice=FCC;
253                         if(!strncmp(word[1],"diamond",7))
254                                 mdrun->lattice=DIAMOND;
255                         if(!strncmp(word[1],"none",4))
256                                 mdrun->lattice=NONE;
257                 }
258                 else if(!strncmp(word[0],"element1",8)) {
259                         mdrun->element1=atoi(word[1]);
260                         mdrun->m1=pse_mass[mdrun->element1];
261                 }
262                 else if(!strncmp(word[0],"element2",8)) {
263                         mdrun->element2=atoi(word[1]);
264                         mdrun->m2=pse_mass[mdrun->element2];
265                 }
266                 else if(!strncmp(word[0],"fill",6)) {
267                         // only lc mode by now
268                         mdrun->lx=atoi(word[2]);
269                         mdrun->ly=atoi(word[3]);
270                         mdrun->lz=atoi(word[4]);
271                         mdrun->lc=atof(word[5]);
272                 }
273                 else if(!strncmp(word[0],"aattr",5)) {
274                         // for aatrib line we need a special stage
275                         // containing one schedule of 0 loops ...
276                         for(i=0;i<strlen(word[1]);i++) {
277                                 switch(word[1][i]) {
278                                         case 't':
279                                                 cap.type|=CHAATTR_TOTALV;
280                                                 break;
281                                         case 'r':
282                                                 cap.type|=CHAATTR_REGION;
283                                                 break;
284                                         case 'e':
285                                                 cap.type|=CHAATTR_ELEMENT;
286                                                 break;
287                                         default:
288                                                 break;
289                                 }
290                         }
291                         i=2;
292                         if(cap.type&CHAATTR_REGION) {
293                                 cap.x0=atof(word[1]);   
294                                 cap.y0=atof(word[2]);   
295                                 cap.z0=atof(word[3]);   
296                                 cap.x1=atof(word[4]);   
297                                 cap.y1=atof(word[5]);   
298                                 cap.z1=atof(word[6]);
299                                 i+=6;
300                         }
301                         if(cap.type&CHAATTR_ELEMENT) {
302                                 cap.element=atoi(word[i]);
303                                 i+=1;
304                         }
305                         for(o=0;o<strlen(word[i]);o++) {
306                                 switch(word[i][o]) {
307                                         case 'b':
308                                                 cap.attr|=ATOM_ATTR_VB;
309                                                 break;
310                                         case 'h':
311                                                 cap.attr|=ATOM_ATTR_HB;
312                                                 break;
313                                         case 'v':
314                                                 cap.attr|=ATOM_ATTR_VA;
315                                                 break;
316                                         case 'f':
317                                                 cap.attr|=ATOM_ATTR_FP;
318                                                 break;
319                                         case '1':
320                                                 cap.attr|=ATOM_ATTR_1BP;
321                                                 break;
322                                         case '2':
323                                                 cap.attr|=ATOM_ATTR_2BP;
324                                                 break;
325                                         case '3':
326                                                 cap.attr|=ATOM_ATTR_3BP;
327                                                 break;
328                                         default:
329                                                 break;
330                                 }
331                         }
332                         add_stage(mdrun,STAGE_CHAATTR,&cap);
333                 }
334                 else if(!strncmp(word[0],"sattr",5)) {
335                         // for satrib line we need a special stage
336                         // containing one schedule of 0 loops ...
337                         csp.type=0;
338                         for(i=1;i<wcnt;i++) {
339                                 if(!strncmp(word[i],"pctrl",5)) {
340                                         csp.ptau=0.01/(atof(word[++i])*GPA);
341                                         csp.type|=CHSATTR_PCTRL;
342                                 }
343                                 if(!strncmp(word[i],"tctrl",5)) {
344                                         csp.ttau=atof(word[++i]);
345                                         csp.type|=CHSATTR_TCTRL;
346                                 }
347                                 if(!strncmp(word[i],"prelax",6)) {
348                                         csp.dp=atof(word[++i])*BAR;
349                                         csp.type|=CHSATTR_PRELAX;
350                                 }
351                                 if(!strncmp(word[i],"trelax",6)) {
352                                         csp.dt=atof(word[++i]);
353                                         csp.type|=CHSATTR_TRELAX;
354                                 }
355                                 if(!strncmp(word[i],"rsteps",6)) {
356                                         csp.rsteps=atoi(word[++i]);
357                                         csp.type|=CHSATTR_RSTEPS;
358                                 }
359                                 if(!strncmp(word[i],"avgrst",6)) {
360                                         csp.avgrst=atoi(word[++i]);
361                                         csp.type|=CHSATTR_AVGRST;
362                                 }
363                         }
364                         add_stage(mdrun,STAGE_CHSATTR,&csp);
365                 }
366                 else if(!strncmp(word[0],"prerun",6))
367                         mdrun->prerun=atoi(word[1]);
368                 else if(!strncmp(word[0],"avgskip",7))
369                         mdrun->avgskip=atoi(word[1]);
370                 else if(!strncmp(word[0],"elog",4))
371                         mdrun->elog=atoi(word[1]);
372                 else if(!strncmp(word[0],"tlog",4))
373                         mdrun->tlog=atoi(word[1]);
374                 else if(!strncmp(word[0],"plog",4))
375                         mdrun->plog=atoi(word[1]);
376                 else if(!strncmp(word[0],"vlog",4))
377                         mdrun->vlog=atoi(word[1]);
378                 else if(!strncmp(word[0],"save",4))
379                         mdrun->save=atoi(word[1]);
380                 else if(!strncmp(word[0],"visualize",9))
381                         mdrun->visualize=atoi(word[1]);
382                 else if(!strncmp(word[0],"stage",5)) {
383                         // for every stage line, add a stage
384                         if(!strncmp(word[1],"displace",8)) {
385                                 dap.nr=atoi(word[2]);   
386                                 dap.dx=atof(word[3]);
387                                 dap.dy=atof(word[4]);
388                                 dap.dz=atof(word[5]);
389                                 add_stage(mdrun,STAGE_DISPLACE_ATOM,&dap);
390                         }
391                         else if(!strncmp(word[1],"ins_atoms",9)) {
392                                 iap.ins_steps=atoi(word[2]);
393                                 iap.ins_atoms=atoi(word[3]);
394                                 iap.element=atoi(word[4]);
395                                 iap.brand=atoi(word[5]);
396                                 for(i=0;i<strlen(word[6]);i++) {
397                                         switch(word[6][i]) {
398                                                 case 'b':
399                                                         iap.attr|=ATOM_ATTR_VB;
400                                                         break;
401                                                 case 'h':
402                                                         iap.attr|=ATOM_ATTR_HB;
403                                                         break;
404                                                 case 'v':
405                                                         iap.attr|=ATOM_ATTR_VA;
406                                                         break;
407                                                 case 'f':
408                                                         iap.attr|=ATOM_ATTR_FP;
409                                                         break;
410                                                 case '1':
411                                                         iap.attr|=ATOM_ATTR_1BP;
412                                                         break;
413                                                 case '2':
414                                                         iap.attr|=ATOM_ATTR_2BP;
415                                                         break;
416                                                 case '3':
417                                                         iap.attr|=ATOM_ATTR_3BP;
418                                                         break;
419                                                 default:
420                                                         break;
421                                         }
422                                 }
423                                 switch(word[7][0]) {
424                                         // insertion types
425                                         case 'p':
426                                                 iap.type=INS_POS;
427                                                 iap.x0=atof(word[8]);
428                                                 iap.y0=atof(word[9]);
429                                                 iap.z0=atof(word[10]);
430                                                 break;
431                                         case 'r':
432                                                 if(word[8][0]=='t') {
433                                                         iap.type=INS_TOTAL;
434                                                         iap.cr=atof(word[9]);
435                                                 }
436                                                 else {
437                                                         iap.type=INS_REGION;
438                                                         iap.x0=atof(word[8]);
439                                                         iap.y0=atof(word[9]);
440                                                         iap.z0=atof(word[10]);
441                                                         iap.x1=atof(word[11]);
442                                                         iap.y1=atof(word[12]);
443                                                         iap.z1=atof(word[13]);
444                                                         iap.cr=atof(word[14]);
445                                                 }
446                                         default:
447                                                 break;
448                                 }
449                                 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
450                         }
451
452
453                         // HERE WE GO ...
454
455                         else if(!strncmp(word[1],"ins_m_atoms",11)) {
456                                 imp.element1=atoi(word[2]);
457                                 imp.element2=atoi(word[3]);
458                                 imp.amount1=atoi(word[4]);
459                                 imp.amount2=atoi(word[5]);
460                                 imp.brand1=atoi(word[6]);
461                                 imp.brand2=atoi(word[7]);
462                                 imp.cr=atof(word[8]);
463                                 /* do this later ...
464                                 for(i=0;i<strlen(word[8]);i++) {
465                                         switch(word[8][i]) {
466                                                 case 'b':
467                                                         imp.attr|=ATOM_ATTR_VB;
468                                                         break;
469                                                 case 'h':
470                                                         imp.attr|=ATOM_ATTR_HB;
471                                                         break;
472                                                 case 'v':
473                                                         imp.attr|=ATOM_ATTR_VA;
474                                                         break;
475                                                 case 'f':
476                                                         imp.attr|=ATOM_ATTR_FP;
477                                                         break;
478                                                 case '1':
479                                                         imp.attr|=ATOM_ATTR_1BP;
480                                                         break;
481                                                 case '2':
482                                                         imp.attr|=ATOM_ATTR_2BP;
483                                                         break;
484                                                 case '3':
485                                                         imp.attr|=ATOM_ATTR_3BP;
486                                                         break;
487                                                 default:
488                                                         break;
489                                         }
490                                 }
491                                 */
492                                 imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
493                                 imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
494                                 add_stage(mdrun,STAGE_INSERT_MIXED_ATOMS,&imp);
495                         }
496
497
498
499
500
501                         else if(!strncmp(word[1],"continue",8)) {
502                                 cp.runs=atoi(word[2]);
503                                 add_stage(mdrun,STAGE_CONTINUE,&cp);
504                         }
505                         else if(!strncmp(word[1],"anneal",6)) {
506                                 ap.count=0;
507                                 ap.runs=atoi(word[2]);
508                                 ap.dt=atof(word[3]);
509                                 add_stage(mdrun,STAGE_ANNEAL,&ap);
510                         }
511                         else {
512                                 printf("%s unknown stage type: %s\n",
513                                        ME,word[1]);
514                                 return -1;
515                         }
516                 }
517                 else {
518                         printf("%s unknown keyword '%s', skipped!\n",
519                                ME,word[0]);
520                         continue;
521                 }
522         }
523
524         /* close file */
525         close(fd);
526
527         return 0;
528 }
529
530 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
531
532         double delta;
533
534         if(!(mdrun->sattr&SATTR_PRELAX)) {
535                 return TRUE;
536         }
537
538         delta=moldyn->p_avg-moldyn->p_ref;
539
540         if(delta<0)
541                 delta=-delta;
542
543         if(delta>mdrun->dp)
544                 return FALSE;
545
546         return TRUE;
547 }
548
549 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
550
551         double delta;
552
553         if(!(mdrun->sattr&SATTR_TRELAX))
554                 return TRUE;
555
556         delta=moldyn->t_avg-moldyn->t_ref;
557
558         if(delta<0)
559                 delta=-delta;
560
561         if(delta>mdrun->dt)
562                 return FALSE;
563
564         return TRUE;
565 }
566
567 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
568
569         t_displace_atom_params *dap;
570         t_stage *stage;
571         t_atom *atom;
572
573         stage=mdrun->stage.current->data;
574         dap=stage->params;
575
576         atom=&(moldyn->atom[dap->nr]);
577         atom->r.x+=dap->dx;
578         atom->r.y+=dap->dy;
579         atom->r.z+=dap->dz;
580
581         return 0;
582 }
583
584 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
585
586         t_insert_atoms_params *iap;
587         t_stage *stage;
588         t_atom *atom;
589         t_3dvec r,v,dist;
590         double d,dmin,o;
591         int cnt,i;
592         double x,y,z;
593         double x0,y0,z0;
594         u8 cr_check,run;
595         
596         stage=mdrun->stage.current->data;
597         iap=stage->params;
598
599         cr_check=FALSE;
600
601         v.x=0.0; v.y=0.0; v.z=0.0;
602         x=0; y=0; z=0;
603         o=0; dmin=0;
604
605         switch(mdrun->lattice) {
606                 case CUBIC:
607                         o=0.5*mdrun->lc;
608                         break;
609                 case FCC:
610                         o=0.25*mdrun->lc;
611                         break;
612                 case DIAMOND:
613                 case ZINCBLENDE:
614                         o=0.125*mdrun->lc;
615                         break;
616                 default:
617                         break;
618         }
619
620         switch(iap->type) {
621                 case INS_TOTAL:
622                         x=moldyn->dim.x;
623                         x0=-x/2.0;
624                         y=moldyn->dim.y;
625                         y0=-y/2.0;
626                         z=moldyn->dim.z;
627                         z0=-z/2.0;
628                         cr_check=TRUE;
629                         break;
630                 case INS_REGION:
631                         x=iap->x1-iap->x0;
632                         x0=iap->x0;
633                         y=iap->y1-iap->y0;
634                         y0=iap->y0;
635                         z=iap->z1-iap->z0;
636                         z0=iap->z0;
637                         cr_check=TRUE;
638                         break;
639                 case INS_POS:
640                         x0=iap->x0;
641                         y0=iap->y0;
642                         z0=iap->z0;
643                         cr_check=FALSE;
644                         break;
645                 default:
646                         printf("%s unknown insertion mode: %02x\n",
647                                ME,iap->type);
648                         return -1;
649         }
650
651         cnt=0;
652         while(cnt<iap->ins_atoms) {
653                 run=1;
654                 while(run) {
655                         if(iap->type!=INS_POS) {
656                                 r.x=rand_get_double(&(moldyn->random))*x;
657                                 r.y=rand_get_double(&(moldyn->random))*y;
658                                 r.z=rand_get_double(&(moldyn->random))*z;
659                         }
660                         else {
661                                 r.x=0.0;
662                                 r.y=0.0;
663                                 r.z=0.0;
664                         }
665                         r.x+=x0;
666                         r.y+=y0;
667                         r.z+=z0;
668                         // offset
669                         if(iap->type!=INS_TOTAL) {
670                                 r.x+=o;
671                                 r.y+=o;
672                                 r.z+=o;
673                         }
674                         run=0;
675                         if(cr_check==TRUE) {
676                                 dmin=1000;      // for sure too high!
677                                 for(i=0;i<moldyn->count;i++) {
678                                         atom=&(moldyn->atom[i]);
679                                         v3_sub(&dist,&(atom->r),&r);
680                                         check_per_bound(moldyn,&dist);
681                                         d=v3_absolute_square(&dist);
682                                         if(d<(iap->cr*iap->cr)) {
683                                                 run=1;
684                                                 break;
685                                         }
686                                         if(d<dmin)
687                                                 dmin=d;
688                                 }
689                         }
690                 }
691                 add_atom(moldyn,iap->element,pse_mass[iap->element],
692                          iap->brand,iap->attr,&r,&v);
693                 printf("%s atom inserted (%d/%d): %f %f %f\n",
694                        ME,(iap->cnt_steps+1)*iap->ins_atoms,
695                        iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
696                 printf("  -> d2 = %f/%f\n",dmin,iap->cr*iap->cr);
697                 cnt+=1;
698         }
699
700         return 0;
701 }
702
703 int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
704
705         t_stage *stage;
706         t_insert_mixed_atoms_params *imp;
707         t_atom *atom;
708         double x,x0,y,y0,z,z0;
709         double dmin,d,c;
710         u8 retry;
711         t_3dvec r,v,dist;
712         int i;
713         
714
715         stage=mdrun->stage.current->data;
716         imp=stage->params;
717
718         x=moldyn->dim.x;
719         x0=-x/2.0;
720         y=moldyn->dim.y;
721         y0=-y/2.0;
722         z=moldyn->dim.z;
723         z0=-z/2.0;
724
725         v.x=0.0;
726         v.y=0.0;
727         v.z=0.0;
728
729         c=imp->cr*imp->cr;
730
731         while(imp->amount1|imp->amount2) {
732                 if(imp->amount1) {
733                         retry=1;
734                         while(retry) {
735                                 retry=0;
736                                 r.x=rand_get_double(&(moldyn->random))*x;
737                                 r.y=rand_get_double(&(moldyn->random))*y;
738                                 r.z=rand_get_double(&(moldyn->random))*z;
739                                 r.x+=x0;
740                                 r.y+=y0;
741                                 r.z+=z0;
742                                 dmin=1000.0;    // for sure too high!
743                                 for(i=0;i<moldyn->count;i++) {
744                                         atom=&(moldyn->atom[i]);
745                                         v3_sub(&dist,&(atom->r),&r);
746                                         check_per_bound(moldyn,&dist);
747                                         d=v3_absolute_square(&dist);
748                                         if(d<c) {
749                                                 retry=1;
750                                                 break;
751                                         }
752                                         if(d<dmin)
753                                                 dmin=d;
754                                         
755                                 }
756                         }
757                         add_atom(moldyn,imp->element1,pse_mass[imp->element1],
758                                  imp->brand1,imp->attr1,&r,&v);
759                         printf("%s (mixed) atom inserted (%d): %f %f %f\n",
760                                ME,imp->amount1,r.x,r.y,r.z);
761                         printf("  -> d2 = %f/%f\n",dmin,c);
762                         imp->amount1-=1;
763                 }
764                 if(imp->amount2) {
765                         retry=1;
766                         while(retry) {
767                                 retry=0;
768                                 r.x=rand_get_double(&(moldyn->random))*x;
769                                 r.y=rand_get_double(&(moldyn->random))*y;
770                                 r.z=rand_get_double(&(moldyn->random))*z;
771                                 r.x+=x0;
772                                 r.y+=y0;
773                                 r.z+=z0;
774                                 dmin=1000.0;    // for sure too high!
775                                 for(i=0;i<moldyn->count;i++) {
776                                         atom=&(moldyn->atom[i]);
777                                         v3_sub(&dist,&(atom->r),&r);
778                                         check_per_bound(moldyn,&dist);
779                                         d=v3_absolute_square(&dist);
780                                         if(d<c) {
781                                                 retry=1;
782                                                 break;
783                                         }
784                                         if(d<dmin)
785                                                 dmin=d;
786                                         
787                                 }
788                         }
789                         add_atom(moldyn,imp->element2,pse_mass[imp->element2],
790                                  imp->brand2,imp->attr2,&r,&v);
791                         printf("%s (mixed) atom inserted (%d): %f %f %f\n",
792                                ME,imp->amount2,r.x,r.y,r.z);
793                         printf("  -> d2 = %f/%f\n",dmin,c);
794                         imp->amount2-=1;
795                 }
796         }
797
798         return 0;
799 }
800
801 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
802
803         t_stage *stage;
804         t_chaattr_params *cap;
805         t_atom *atom;
806         int i;
807
808         stage=mdrun->stage.current->data;
809         cap=stage->params;
810
811         for(i=0;i<moldyn->count;i++) {
812                 atom=&(moldyn->atom[i]);
813                 if(cap->type&CHAATTR_ELEMENT) {
814                         if(cap->element!=atom->element)
815                                 continue;
816                 }
817                 if(cap->type&CHAATTR_REGION) {
818                         if(cap->x0<atom->r.x)
819                                 continue;
820                         if(cap->y0<atom->r.y)
821                                 continue;
822                         if(cap->z0<atom->r.z)
823                                 continue;
824                         if(cap->x1>atom->r.x)
825                                 continue;
826                         if(cap->y1>atom->r.y)
827                                 continue;
828                         if(cap->z1>atom->r.z)
829                                 continue;
830                 }
831                 atom->attr=cap->attr;
832         }
833
834         return 0;
835 }
836
837 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
838
839         t_stage *stage;
840         t_chsattr_params *csp;
841
842         stage=mdrun->stage.current->data;
843         csp=stage->params;
844
845         if(csp->type&CHSATTR_PCTRL) {
846                 if(csp->ptau>0)
847                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
848                 else
849                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
850         }
851         if(csp->type&CHSATTR_TCTRL) {
852                 if(csp->ttau>0)
853                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
854                 else
855                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
856         }
857         if(csp->type&CHSATTR_PRELAX) {
858                 if(csp->dp<0)
859                         mdrun->sattr&=(~(SATTR_PRELAX));
860                 else
861                         mdrun->sattr|=SATTR_PRELAX;
862                 mdrun->dp=csp->dp;
863         }
864         if(csp->type&CHSATTR_TRELAX) {
865                 if(csp->dt<0)
866                         mdrun->sattr&=(~(SATTR_TRELAX));
867                 else
868                         mdrun->sattr|=SATTR_TRELAX;
869                 mdrun->dt=csp->dt;
870         }
871         if(csp->type&CHSATTR_AVGRST) {
872                 if(csp->avgrst)
873                         mdrun->sattr|=SATTR_AVGRST;
874                 else
875                         mdrun->sattr&=(~(SATTR_AVGRST));
876         }
877         if(csp->type&CHSATTR_RSTEPS) {
878                 mdrun->relax_steps=csp->rsteps;
879         }
880
881         return 0;
882 }
883
884 #define stage_print(m)  if(!(stage->executed)) \
885                                 printf("%s",m)
886
887 int mdrun_hook(void *ptr1,void *ptr2) {
888
889         t_moldyn *moldyn;
890         t_mdrun *mdrun;
891         t_stage *stage;
892         t_list *sl;
893         int steps;
894         double tau;
895         u8 change_stage;
896
897         t_insert_atoms_params *iap;
898         t_insert_mixed_atoms_params *imp;
899         t_continue_params *cp;
900         t_anneal_params *ap;
901
902         moldyn=ptr1;
903         mdrun=ptr2;
904
905         sl=&(mdrun->stage);
906
907         change_stage=FALSE;
908
909         /* return immediately if there are no more stage */
910         if(sl->current==NULL)
911                 return 0;
912
913         /* get stage description */
914         stage=sl->current->data;
915
916         /* default steps and tau values */
917         steps=mdrun->relax_steps;
918         tau=mdrun->timestep;
919
920         /* check whether relaxation steps are necessary */
921         if((check_pressure(moldyn,mdrun)==TRUE)&\
922            (check_temperature(moldyn,mdrun)==TRUE)) {
923         
924                 /* be verbose */
925                 stage_print("\n###########################\n");
926                 stage_print("# [mdrun] executing stage #\n");
927                 stage_print("###########################\n\n");
928                 
929                 /* stage specific stuff */
930                 switch(stage->type) {
931                         case STAGE_DISPLACE_ATOM:
932                                 stage_print("  -> displace atom\n\n");
933                                 displace_atom(moldyn,mdrun);
934                                 change_stage=TRUE;
935                                 break;
936                         case STAGE_INSERT_ATOMS:
937                                 stage_print("  -> insert atoms\n\n");
938                                 iap=stage->params;
939                                 if(iap->cnt_steps==iap->ins_steps) {
940                                         change_stage=TRUE;
941                                         break;
942                                 }
943                                 insert_atoms(moldyn,mdrun);
944                                 iap->cnt_steps+=1;
945                                 break;
946
947
948
949                         case STAGE_INSERT_MIXED_ATOMS:
950                                 stage_print("  -> insert mixed atoms\n\n");
951                                 imp=stage->params;
952                                 insert_mixed_atoms(moldyn,mdrun);
953                                 break;
954
955
956
957                         case STAGE_CONTINUE:
958                                 stage_print("  -> continue\n\n");
959                                 if(stage->executed==TRUE) {
960                                         change_stage=TRUE;
961                                         break;
962                                 }
963                                 cp=stage->params;
964                                 steps=cp->runs;
965                                 break;
966                         case STAGE_ANNEAL:
967                                 stage_print("  -> anneal\n\n");
968                                 ap=stage->params;
969                                 if(ap->count==ap->runs) {
970                                         change_stage=TRUE;
971                                         break;
972                                 }
973                                 if(moldyn->t_ref+ap->dt>=0.0)
974                                         set_temperature(moldyn,
975                                                         moldyn->t_ref+ap->dt);
976                                 ap->count+=1;
977                                 break;
978                         case STAGE_CHAATTR:
979                                 stage_print("  -> chaattr\n\n");
980                                 chaatr(moldyn,mdrun);
981                                 change_stage=TRUE;
982                                 break;
983                         case STAGE_CHSATTR:
984                                 stage_print("  -> chsattr\n\n");
985                                 chsattr(moldyn,mdrun);
986                                 change_stage=TRUE;
987                                 break;
988                         default:
989                                 printf("%s unknwon stage type\n",ME);
990                                 break;
991                 }
992         
993                 /* mark as executed */
994                 stage->executed=TRUE;
995         
996                 /* change state */
997                 if(change_stage==TRUE) {
998                         printf("%s finished stage\n",ME);
999                         if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
1000                                 printf("%s no more stages\n",ME);
1001                                 return 0;
1002                         }
1003                         steps=0;
1004                         tau=mdrun->timestep;
1005                 }
1006
1007         }
1008         else {
1009
1010                 /* averages */
1011                 if(mdrun->sattr&SATTR_AVGRST)
1012                         average_reset(moldyn);
1013
1014         }
1015
1016         /* continue simulation */
1017         moldyn_add_schedule(moldyn,steps,tau);
1018
1019         return 0;
1020 }
1021
1022 int main(int argc,char **argv) {
1023
1024         t_mdrun mdrun;
1025         t_moldyn moldyn;
1026         t_3dvec o;
1027
1028         /* clear structs */
1029         memset(&mdrun,0,sizeof(t_mdrun));
1030         memset(&moldyn,0,sizeof(t_moldyn));
1031
1032         /* parse arguments */
1033         if(mdrun_parse_argv(&mdrun,argc,argv)<0)
1034                 return -1;
1035
1036         /* initialize list system */
1037         list_init_f(&(mdrun.stage));
1038
1039         /* parse config file */
1040         mdrun_parse_config(&mdrun);
1041
1042         /* reset the stage list */
1043         list_reset_f(&(mdrun.stage));
1044
1045         /* sanity check! */
1046
1047         /* prepare simulation */
1048
1049         if(mdrun.continue_file[0]) {
1050                 // read the save file
1051                 moldyn_read_save_file(&moldyn,mdrun.continue_file);
1052                 // manualaadjusting some stuff
1053                 moldyn.argc=argc;
1054                 moldyn.args=argv;
1055                 rand_init(&(moldyn.random),NULL,1);
1056                 moldyn.random.status|=RAND_STAT_VERBOSE;
1057         }
1058         else {
1059                 moldyn_init(&moldyn,argc,argv);
1060         }
1061         
1062         if(set_int_alg(&moldyn,mdrun.intalgo)<0)
1063                 return -1;
1064
1065         /* potential */
1066         set_cutoff(&moldyn,mdrun.cutoff);
1067         if(set_potential(&moldyn,mdrun.potential)<0)
1068                 return -1;
1069         switch(mdrun.potential) {
1070                 case MOLDYN_POTENTIAL_AM:
1071                         albe_mult_set_params(&moldyn,
1072                                              mdrun.element1,
1073                                              mdrun.element2);
1074                         break;
1075                 case MOLDYN_POTENTIAL_TM:
1076                         tersoff_mult_set_params(&moldyn,
1077                                                 mdrun.element1,
1078                                                 mdrun.element2);
1079                         break;
1080                 case MOLDYN_POTENTIAL_HO:
1081                         harmonic_oscillator_set_params(&moldyn,mdrun.element1);
1082                         break;
1083                 case MOLDYN_POTENTIAL_LJ:
1084                         lennard_jones_set_params(&moldyn,mdrun.element1);
1085                         break;
1086                 default:
1087                         printf("%s unknown potential: %02x\n",
1088                                ME,mdrun.potential);
1089                         return -1;
1090         }
1091
1092         /* if it is a continue run, reset schedule and skip lattice init */
1093         if(mdrun.continue_file[0]) {
1094                 memset(&(moldyn.schedule),0,sizeof(t_moldyn_schedule));
1095                 goto addsched;
1096         }
1097
1098         /* initial lattice and dimensions */
1099         set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
1100         set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
1101         switch(mdrun.lattice) {
1102                 case FCC:
1103                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1104                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1105                                        mdrun.ly,mdrun.lz,NULL);
1106                         break;
1107                 case DIAMOND:
1108                         create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
1109                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1110                                        mdrun.ly,mdrun.lz,NULL);
1111                         break;
1112                 case ZINCBLENDE:
1113                         o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1114                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1115                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1116                                        mdrun.ly,mdrun.lz,&o);
1117                         o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1118                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
1119                                        mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
1120                                        mdrun.ly,mdrun.lz,&o);
1121                         break;
1122                 case NONE:
1123                         break;
1124                 default:
1125                         printf("%s unknown lattice type: %02x\n",
1126                                ME,mdrun.lattice);
1127                         return -1;
1128         }
1129         moldyn_bc_check(&moldyn);
1130
1131         /* temperature and pressure */
1132         set_temperature(&moldyn,mdrun.temperature);
1133         set_pressure(&moldyn,mdrun.pressure);
1134         thermal_init(&moldyn,TRUE);
1135
1136 addsched:
1137         /* first schedule */
1138         moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
1139
1140         /* log */
1141         moldyn_set_log_dir(&moldyn,mdrun.sdir);
1142         moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
1143         if(mdrun.elog)
1144                 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
1145         if(mdrun.tlog)
1146                 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
1147         if(mdrun.plog)
1148                 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
1149         if(mdrun.vlog)
1150                 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
1151         if(mdrun.visualize)
1152                 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
1153         if(mdrun.save)
1154                 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
1155         moldyn_set_log(&moldyn,CREATE_REPORT,0);
1156         set_avg_skip(&moldyn,mdrun.avgskip);
1157
1158         /* prepare the hook function */
1159         moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
1160
1161         /* run the simulation */
1162         moldyn_integrate(&moldyn);
1163
1164         /* shutdown */
1165         moldyn_shutdown(&moldyn);
1166         del_stages(&mdrun);
1167         list_destroy_f(&(mdrun.stage));
1168
1169         return 0;
1170 }