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