introduced critical max (didn't help though!)
[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.crmin=atof(word[8]);
463                                 imp.crmax=atof(word[9]);
464                                 /* do this later ...
465                                 for(i=0;i<strlen(word[8]);i++) {
466                                         switch(word[8][i]) {
467                                                 case 'b':
468                                                         imp.attr|=ATOM_ATTR_VB;
469                                                         break;
470                                                 case 'h':
471                                                         imp.attr|=ATOM_ATTR_HB;
472                                                         break;
473                                                 case 'v':
474                                                         imp.attr|=ATOM_ATTR_VA;
475                                                         break;
476                                                 case 'f':
477                                                         imp.attr|=ATOM_ATTR_FP;
478                                                         break;
479                                                 case '1':
480                                                         imp.attr|=ATOM_ATTR_1BP;
481                                                         break;
482                                                 case '2':
483                                                         imp.attr|=ATOM_ATTR_2BP;
484                                                         break;
485                                                 case '3':
486                                                         imp.attr|=ATOM_ATTR_3BP;
487                                                         break;
488                                                 default:
489                                                         break;
490                                         }
491                                 }
492                                 */
493                                 imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
494                                 imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
495                                 add_stage(mdrun,STAGE_INSERT_MIXED_ATOMS,&imp);
496                         }
497
498
499
500
501
502                         else if(!strncmp(word[1],"continue",8)) {
503                                 cp.runs=atoi(word[2]);
504                                 add_stage(mdrun,STAGE_CONTINUE,&cp);
505                         }
506                         else if(!strncmp(word[1],"anneal",6)) {
507                                 ap.count=0;
508                                 ap.runs=atoi(word[2]);
509                                 ap.dt=atof(word[3]);
510                                 add_stage(mdrun,STAGE_ANNEAL,&ap);
511                         }
512                         else {
513                                 printf("%s unknown stage type: %s\n",
514                                        ME,word[1]);
515                                 return -1;
516                         }
517                 }
518                 else {
519                         printf("%s unknown keyword '%s', skipped!\n",
520                                ME,word[0]);
521                         continue;
522                 }
523         }
524
525         /* close file */
526         close(fd);
527
528         return 0;
529 }
530
531 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
532
533         double delta;
534
535         if(!(mdrun->sattr&SATTR_PRELAX)) {
536                 return TRUE;
537         }
538
539         delta=moldyn->p_avg-moldyn->p_ref;
540
541         if(delta<0)
542                 delta=-delta;
543
544         if(delta>mdrun->dp)
545                 return FALSE;
546
547         return TRUE;
548 }
549
550 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
551
552         double delta;
553
554         if(!(mdrun->sattr&SATTR_TRELAX))
555                 return TRUE;
556
557         delta=moldyn->t_avg-moldyn->t_ref;
558
559         if(delta<0)
560                 delta=-delta;
561
562         if(delta>mdrun->dt)
563                 return FALSE;
564
565         return TRUE;
566 }
567
568 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
569
570         t_displace_atom_params *dap;
571         t_stage *stage;
572         t_atom *atom;
573
574         stage=mdrun->stage.current->data;
575         dap=stage->params;
576
577         atom=&(moldyn->atom[dap->nr]);
578         atom->r.x+=dap->dx;
579         atom->r.y+=dap->dy;
580         atom->r.z+=dap->dz;
581
582         return 0;
583 }
584
585 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
586
587         t_insert_atoms_params *iap;
588         t_stage *stage;
589         t_atom *atom;
590         t_3dvec r,v,dist;
591         double d,dmin,o;
592         int cnt,i;
593         double x,y,z;
594         double x0,y0,z0;
595         u8 cr_check,run;
596         
597         stage=mdrun->stage.current->data;
598         iap=stage->params;
599
600         cr_check=FALSE;
601
602         v.x=0.0; v.y=0.0; v.z=0.0;
603         x=0; y=0; z=0;
604         o=0; dmin=0;
605
606         switch(mdrun->lattice) {
607                 case CUBIC:
608                         o=0.5*mdrun->lc;
609                         break;
610                 case FCC:
611                         o=0.25*mdrun->lc;
612                         break;
613                 case DIAMOND:
614                 case ZINCBLENDE:
615                         o=0.125*mdrun->lc;
616                         break;
617                 default:
618                         break;
619         }
620
621         switch(iap->type) {
622                 case INS_TOTAL:
623                         x=moldyn->dim.x;
624                         x0=-x/2.0;
625                         y=moldyn->dim.y;
626                         y0=-y/2.0;
627                         z=moldyn->dim.z;
628                         z0=-z/2.0;
629                         cr_check=TRUE;
630                         break;
631                 case INS_REGION:
632                         x=iap->x1-iap->x0;
633                         x0=iap->x0;
634                         y=iap->y1-iap->y0;
635                         y0=iap->y0;
636                         z=iap->z1-iap->z0;
637                         z0=iap->z0;
638                         cr_check=TRUE;
639                         break;
640                 case INS_POS:
641                         x0=iap->x0;
642                         y0=iap->y0;
643                         z0=iap->z0;
644                         cr_check=FALSE;
645                         break;
646                 default:
647                         printf("%s unknown insertion mode: %02x\n",
648                                ME,iap->type);
649                         return -1;
650         }
651
652         cnt=0;
653         while(cnt<iap->ins_atoms) {
654                 run=1;
655                 while(run) {
656                         if(iap->type!=INS_POS) {
657                                 r.x=rand_get_double(&(moldyn->random))*x;
658                                 r.y=rand_get_double(&(moldyn->random))*y;
659                                 r.z=rand_get_double(&(moldyn->random))*z;
660                         }
661                         else {
662                                 r.x=0.0;
663                                 r.y=0.0;
664                                 r.z=0.0;
665                         }
666                         r.x+=x0;
667                         r.y+=y0;
668                         r.z+=z0;
669                         // offset
670                         if(iap->type!=INS_TOTAL) {
671                                 r.x+=o;
672                                 r.y+=o;
673                                 r.z+=o;
674                         }
675                         run=0;
676                         if(cr_check==TRUE) {
677                                 dmin=1000;      // for sure too high!
678                                 for(i=0;i<moldyn->count;i++) {
679                                         atom=&(moldyn->atom[i]);
680                                         v3_sub(&dist,&(atom->r),&r);
681                                         check_per_bound(moldyn,&dist);
682                                         d=v3_absolute_square(&dist);
683                                         if(d<(iap->cr*iap->cr)) {
684                                                 run=1;
685                                                 break;
686                                         }
687                                         if(d<dmin)
688                                                 dmin=d;
689                                 }
690                         }
691                 }
692                 add_atom(moldyn,iap->element,pse_mass[iap->element],
693                          iap->brand,iap->attr,&r,&v);
694                 printf("%s atom inserted (%d/%d): %f %f %f\n",
695                        ME,(iap->cnt_steps+1)*iap->ins_atoms,
696                        iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
697                 printf("  -> d2 = %f/%f\n",dmin,iap->cr*iap->cr);
698                 cnt+=1;
699         }
700
701         return 0;
702 }
703
704 int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
705
706         t_stage *stage;
707         t_insert_mixed_atoms_params *imp;
708         t_atom *atom;
709         double x,x0,y,y0,z,z0;
710         double dmin,d,cmin,cmax;
711         u8 retry;
712         t_3dvec r,v,dist;
713         int i;
714         
715
716         stage=mdrun->stage.current->data;
717         imp=stage->params;
718
719         x=moldyn->dim.x;
720         x0=-x/2.0;
721         y=moldyn->dim.y;
722         y0=-y/2.0;
723         z=moldyn->dim.z;
724         z0=-z/2.0;
725
726         v.x=0.0;
727         v.y=0.0;
728         v.z=0.0;
729
730         cmin=imp->crmin*imp->crmin;
731         cmax=imp->crmax*imp->crmax;
732
733         while(imp->amount1|imp->amount2) {
734                 if(imp->amount1) {
735                         retry=1;
736                         while(retry) {
737                                 retry=0;
738                                 r.x=rand_get_double(&(moldyn->random))*x;
739                                 r.y=rand_get_double(&(moldyn->random))*y;
740                                 r.z=rand_get_double(&(moldyn->random))*z;
741                                 r.x+=x0;
742                                 r.y+=y0;
743                                 r.z+=z0;
744                                 dmin=1000.0;    // for sure too high!
745                                 for(i=0;i<moldyn->count;i++) {
746                                         atom=&(moldyn->atom[i]);
747                                         v3_sub(&dist,&(atom->r),&r);
748                                         check_per_bound(moldyn,&dist);
749                                         d=v3_absolute_square(&dist);
750                                         if(d<cmin) {
751                                                 retry=1;
752                                                 break;
753                                         }
754                                         if(d<dmin)
755                                                 dmin=d;
756                                 }
757                                 if(dmin!=1000.0)
758                                         if(dmin>cmax)
759                                                 retry=1;
760                         }
761                         add_atom(moldyn,imp->element1,pse_mass[imp->element1],
762                                  imp->brand1,imp->attr1,&r,&v);
763                         printf("%s (mixed) atom inserted (%d): %f %f %f\n",
764                                ME,imp->amount1,r.x,r.y,r.z);
765                         printf("  -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
766                         imp->amount1-=1;
767                 }
768                 if(imp->amount2) {
769                         retry=1;
770                         while(retry) {
771                                 retry=0;
772                                 r.x=rand_get_double(&(moldyn->random))*x;
773                                 r.y=rand_get_double(&(moldyn->random))*y;
774                                 r.z=rand_get_double(&(moldyn->random))*z;
775                                 r.x+=x0;
776                                 r.y+=y0;
777                                 r.z+=z0;
778                                 dmin=1000.0;    // for sure too high!
779                                 for(i=0;i<moldyn->count;i++) {
780                                         atom=&(moldyn->atom[i]);
781                                         v3_sub(&dist,&(atom->r),&r);
782                                         check_per_bound(moldyn,&dist);
783                                         d=v3_absolute_square(&dist);
784                                         if(d<cmin) {
785                                                 retry=1;
786                                                 break;
787                                         }
788                                         if(d<dmin)
789                                                 dmin=d;
790                                 }
791                                 if(dmin!=1000.0)
792                                         if(dmin>cmax)
793                                                 retry=1;
794                         }
795                         add_atom(moldyn,imp->element2,pse_mass[imp->element2],
796                                  imp->brand2,imp->attr2,&r,&v);
797                         printf("%s (mixed) atom inserted (%d): %f %f %f\n",
798                                ME,imp->amount2,r.x,r.y,r.z);
799                         printf("  -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
800                         imp->amount2-=1;
801                 }
802         }
803
804         return 0;
805 }
806
807 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
808
809         t_stage *stage;
810         t_chaattr_params *cap;
811         t_atom *atom;
812         int i;
813
814         stage=mdrun->stage.current->data;
815         cap=stage->params;
816
817         for(i=0;i<moldyn->count;i++) {
818                 atom=&(moldyn->atom[i]);
819                 if(cap->type&CHAATTR_ELEMENT) {
820                         if(cap->element!=atom->element)
821                                 continue;
822                 }
823                 if(cap->type&CHAATTR_REGION) {
824                         if(cap->x0<atom->r.x)
825                                 continue;
826                         if(cap->y0<atom->r.y)
827                                 continue;
828                         if(cap->z0<atom->r.z)
829                                 continue;
830                         if(cap->x1>atom->r.x)
831                                 continue;
832                         if(cap->y1>atom->r.y)
833                                 continue;
834                         if(cap->z1>atom->r.z)
835                                 continue;
836                 }
837                 atom->attr=cap->attr;
838         }
839
840         return 0;
841 }
842
843 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
844
845         t_stage *stage;
846         t_chsattr_params *csp;
847
848         stage=mdrun->stage.current->data;
849         csp=stage->params;
850
851         if(csp->type&CHSATTR_PCTRL) {
852                 if(csp->ptau>0)
853                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
854                 else
855                         set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
856         }
857         if(csp->type&CHSATTR_TCTRL) {
858                 if(csp->ttau>0)
859                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
860                 else
861                         set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
862         }
863         if(csp->type&CHSATTR_PRELAX) {
864                 if(csp->dp<0)
865                         mdrun->sattr&=(~(SATTR_PRELAX));
866                 else
867                         mdrun->sattr|=SATTR_PRELAX;
868                 mdrun->dp=csp->dp;
869         }
870         if(csp->type&CHSATTR_TRELAX) {
871                 if(csp->dt<0)
872                         mdrun->sattr&=(~(SATTR_TRELAX));
873                 else
874                         mdrun->sattr|=SATTR_TRELAX;
875                 mdrun->dt=csp->dt;
876         }
877         if(csp->type&CHSATTR_AVGRST) {
878                 if(csp->avgrst)
879                         mdrun->sattr|=SATTR_AVGRST;
880                 else
881                         mdrun->sattr&=(~(SATTR_AVGRST));
882         }
883         if(csp->type&CHSATTR_RSTEPS) {
884                 mdrun->relax_steps=csp->rsteps;
885         }
886
887         return 0;
888 }
889
890 #define stage_print(m)  if(!(stage->executed)) \
891                                 printf("%s",m)
892
893 int mdrun_hook(void *ptr1,void *ptr2) {
894
895         t_moldyn *moldyn;
896         t_mdrun *mdrun;
897         t_stage *stage;
898         t_list *sl;
899         int steps;
900         double tau;
901         u8 change_stage;
902
903         t_insert_atoms_params *iap;
904         t_insert_mixed_atoms_params *imp;
905         t_continue_params *cp;
906         t_anneal_params *ap;
907
908         moldyn=ptr1;
909         mdrun=ptr2;
910
911         sl=&(mdrun->stage);
912
913         change_stage=FALSE;
914
915         /* return immediately if there are no more stage */
916         if(sl->current==NULL)
917                 return 0;
918
919         /* get stage description */
920         stage=sl->current->data;
921
922         /* default steps and tau values */
923         steps=mdrun->relax_steps;
924         tau=mdrun->timestep;
925
926         /* check whether relaxation steps are necessary */
927         if((check_pressure(moldyn,mdrun)==TRUE)&\
928            (check_temperature(moldyn,mdrun)==TRUE)) {
929         
930                 /* be verbose */
931                 stage_print("\n###########################\n");
932                 stage_print("# [mdrun] executing stage #\n");
933                 stage_print("###########################\n\n");
934                 
935                 /* stage specific stuff */
936                 switch(stage->type) {
937                         case STAGE_DISPLACE_ATOM:
938                                 stage_print("  -> displace atom\n\n");
939                                 displace_atom(moldyn,mdrun);
940                                 change_stage=TRUE;
941                                 break;
942                         case STAGE_INSERT_ATOMS:
943                                 stage_print("  -> insert atoms\n\n");
944                                 iap=stage->params;
945                                 if(iap->cnt_steps==iap->ins_steps) {
946                                         change_stage=TRUE;
947                                         break;
948                                 }
949                                 insert_atoms(moldyn,mdrun);
950                                 iap->cnt_steps+=1;
951                                 break;
952
953
954
955                         case STAGE_INSERT_MIXED_ATOMS:
956                                 stage_print("  -> insert mixed atoms\n\n");
957                                 imp=stage->params;
958                                 insert_mixed_atoms(moldyn,mdrun);
959                                 change_stage=TRUE;
960                                 break;
961
962
963
964                         case STAGE_CONTINUE:
965                                 stage_print("  -> continue\n\n");
966                                 if(stage->executed==TRUE) {
967                                         change_stage=TRUE;
968                                         break;
969                                 }
970                                 cp=stage->params;
971                                 steps=cp->runs;
972                                 break;
973                         case STAGE_ANNEAL:
974                                 stage_print("  -> anneal\n\n");
975                                 ap=stage->params;
976                                 if(ap->count==ap->runs) {
977                                         change_stage=TRUE;
978                                         break;
979                                 }
980                                 if(moldyn->t_ref+ap->dt>=0.0)
981                                         set_temperature(moldyn,
982                                                         moldyn->t_ref+ap->dt);
983                                 ap->count+=1;
984                                 break;
985                         case STAGE_CHAATTR:
986                                 stage_print("  -> chaattr\n\n");
987                                 chaatr(moldyn,mdrun);
988                                 change_stage=TRUE;
989                                 break;
990                         case STAGE_CHSATTR:
991                                 stage_print("  -> chsattr\n\n");
992                                 chsattr(moldyn,mdrun);
993                                 change_stage=TRUE;
994                                 break;
995                         default:
996                                 printf("%s unknwon stage type\n",ME);
997                                 break;
998                 }
999         
1000                 /* mark as executed */
1001                 stage->executed=TRUE;
1002         
1003                 /* change state */
1004                 if(change_stage==TRUE) {
1005                         printf("%s finished stage\n",ME);
1006                         if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
1007                                 printf("%s no more stages\n",ME);
1008                                 return 0;
1009                         }
1010                         steps=0;
1011                         tau=mdrun->timestep;
1012                 }
1013
1014         }
1015         else {
1016
1017                 /* averages */
1018                 if(mdrun->sattr&SATTR_AVGRST)
1019                         average_reset(moldyn);
1020
1021         }
1022
1023         /* continue simulation */
1024         moldyn_add_schedule(moldyn,steps,tau);
1025
1026         return 0;
1027 }
1028
1029 int main(int argc,char **argv) {
1030
1031         t_mdrun mdrun;
1032         t_moldyn moldyn;
1033         t_3dvec o;
1034
1035         /* clear structs */
1036         memset(&mdrun,0,sizeof(t_mdrun));
1037         memset(&moldyn,0,sizeof(t_moldyn));
1038
1039         /* parse arguments */
1040         if(mdrun_parse_argv(&mdrun,argc,argv)<0)
1041                 return -1;
1042
1043         /* initialize list system */
1044         list_init_f(&(mdrun.stage));
1045
1046         /* parse config file */
1047         mdrun_parse_config(&mdrun);
1048
1049         /* reset the stage list */
1050         list_reset_f(&(mdrun.stage));
1051
1052         /* sanity check! */
1053
1054         /* prepare simulation */
1055
1056         if(mdrun.continue_file[0]) {
1057                 // read the save file
1058                 moldyn_read_save_file(&moldyn,mdrun.continue_file);
1059                 // manualaadjusting some stuff
1060                 moldyn.argc=argc;
1061                 moldyn.args=argv;
1062                 rand_init(&(moldyn.random),NULL,1);
1063                 moldyn.random.status|=RAND_STAT_VERBOSE;
1064         }
1065         else {
1066                 moldyn_init(&moldyn,argc,argv);
1067         }
1068         
1069         if(set_int_alg(&moldyn,mdrun.intalgo)<0)
1070                 return -1;
1071
1072         /* potential */
1073         set_cutoff(&moldyn,mdrun.cutoff);
1074         if(set_potential(&moldyn,mdrun.potential)<0)
1075                 return -1;
1076         switch(mdrun.potential) {
1077                 case MOLDYN_POTENTIAL_AM:
1078                         albe_mult_set_params(&moldyn,
1079                                              mdrun.element1,
1080                                              mdrun.element2);
1081                         break;
1082                 case MOLDYN_POTENTIAL_TM:
1083                         tersoff_mult_set_params(&moldyn,
1084                                                 mdrun.element1,
1085                                                 mdrun.element2);
1086                         break;
1087                 case MOLDYN_POTENTIAL_HO:
1088                         harmonic_oscillator_set_params(&moldyn,mdrun.element1);
1089                         break;
1090                 case MOLDYN_POTENTIAL_LJ:
1091                         lennard_jones_set_params(&moldyn,mdrun.element1);
1092                         break;
1093                 default:
1094                         printf("%s unknown potential: %02x\n",
1095                                ME,mdrun.potential);
1096                         return -1;
1097         }
1098
1099         /* if it is a continue run, reset schedule and skip lattice init */
1100         if(mdrun.continue_file[0]) {
1101                 memset(&(moldyn.schedule),0,sizeof(t_moldyn_schedule));
1102                 goto addsched;
1103         }
1104
1105         /* initial lattice and dimensions */
1106         set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
1107         set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
1108         switch(mdrun.lattice) {
1109                 case FCC:
1110                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1111                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1112                                        mdrun.ly,mdrun.lz,NULL);
1113                         break;
1114                 case DIAMOND:
1115                         create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
1116                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1117                                        mdrun.ly,mdrun.lz,NULL);
1118                         break;
1119                 case ZINCBLENDE:
1120                         o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1121                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1122                                        mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1123                                        mdrun.ly,mdrun.lz,&o);
1124                         o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1125                         create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
1126                                        mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
1127                                        mdrun.ly,mdrun.lz,&o);
1128                         break;
1129                 case NONE:
1130                         break;
1131                 default:
1132                         printf("%s unknown lattice type: %02x\n",
1133                                ME,mdrun.lattice);
1134                         return -1;
1135         }
1136         moldyn_bc_check(&moldyn);
1137
1138         /* temperature and pressure */
1139         set_temperature(&moldyn,mdrun.temperature);
1140         set_pressure(&moldyn,mdrun.pressure);
1141         thermal_init(&moldyn,TRUE);
1142
1143 addsched:
1144         /* first schedule */
1145         moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
1146
1147         /* log */
1148         moldyn_set_log_dir(&moldyn,mdrun.sdir);
1149         moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
1150         if(mdrun.elog)
1151                 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
1152         if(mdrun.tlog)
1153                 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
1154         if(mdrun.plog)
1155                 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
1156         if(mdrun.vlog)
1157                 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
1158         if(mdrun.visualize)
1159                 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
1160         if(mdrun.save)
1161                 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
1162         moldyn_set_log(&moldyn,CREATE_REPORT,0);
1163         set_avg_skip(&moldyn,mdrun.avgskip);
1164
1165         /* prepare the hook function */
1166         moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
1167
1168         /* run the simulation */
1169         moldyn_integrate(&moldyn);
1170
1171         /* shutdown */
1172         moldyn_shutdown(&moldyn);
1173         del_stages(&mdrun);
1174         list_destroy_f(&(mdrun.stage));
1175
1176         return 0;
1177 }