2 * mdrun.c - main code to run a md simulation
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
11 #include "potentials/harmonic_oscillator.h"
12 #include "potentials/lennard_jones.h"
13 #include "potentials/albe.h"
14 #include "potentials/albe_orig.h"
16 #include "potentials/tersoff_orig.h"
18 #include "potentials/tersoff.h"
27 int mdrun_usage(void) {
29 printf("%s usage:\n",ME);
34 int mdrun_parse_argv(t_mdrun *mdrun,int argc,char **argv) {
41 printf("%s unknown switch: %s\n",ME,argv[i]);
47 strncpy(mdrun->cfile,argv[++i],128);
50 strncpy(mdrun->sdir,argv[++i],128);
56 printf("%s unknown option: %s\n",ME,argv[i]);
66 int del_stages(t_mdrun *mdrun) {
79 stage=sl->current->data;
82 } while(list_next_f(sl)!=L_NO_NEXT_ELEMENT);
87 int add_stage(t_mdrun *mdrun,u8 type,void *params) {
94 case STAGE_DISPLACE_ATOM:
95 psize=sizeof(t_displace_atom_params);
98 psize=sizeof(t_del_atoms_params);
100 case STAGE_MODIFY_ATOMS:
101 psize=sizeof(t_modify_atoms_params);
103 case STAGE_INSERT_ATOMS:
104 psize=sizeof(t_insert_atoms_params);
106 case STAGE_INSERT_MIXED_ATOMS:
107 psize=sizeof(t_insert_mixed_atoms_params);
110 psize=sizeof(t_continue_params);
113 psize=sizeof(t_anneal_params);
116 psize=sizeof(t_chaattr_params);
119 psize=sizeof(t_chsattr_params);
122 psize=sizeof(t_set_temp_params);
124 case STAGE_SET_TIMESTEP:
125 psize=sizeof(t_set_timestep_params);
128 psize=sizeof(t_fill_params);
130 case STAGE_THERMAL_INIT:
134 psize=sizeof(t_crt_params);
137 printf("%s unknown stage type: %02x\n",ME,type);
141 stage=malloc(sizeof(t_stage));
143 perror("[mdrun] malloc (add stage)");
148 stage->executed=FALSE;
150 stage->params=malloc(psize);
151 if(stage->params==NULL) {
152 perror("[mdrun] malloc (stage params)");
156 memcpy(stage->params,params,psize);
158 list_add_immediate_f(&(mdrun->stage),stage);
163 int mdrun_parse_config(t_mdrun *mdrun) {
173 t_displace_atom_params dap;
174 t_modify_atoms_params map;
175 t_insert_atoms_params iap;
176 t_insert_mixed_atoms_params imp;
177 t_continue_params cp;
179 t_chaattr_params cap;
180 t_chsattr_params csp;
181 t_set_temp_params stp;
182 t_set_timestep_params stsp;
184 t_del_atoms_params delp;
187 /* open config file */
188 fd=open(mdrun->cfile,O_RDONLY);
190 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
195 /* read, parse, set */
200 ret=get_line(fd,line,128);
204 // ignore # lines and \n
205 if((line[0]=='#')|(ret==1))
209 memset(&iap,0,sizeof(t_insert_atoms_params));
210 memset(&map,0,sizeof(t_modify_atoms_params));
211 memset(&imp,0,sizeof(t_insert_mixed_atoms_params));
212 memset(&cp,0,sizeof(t_continue_params));
213 memset(&ap,0,sizeof(t_anneal_params));
214 memset(&cap,0,sizeof(t_chaattr_params));
215 memset(&csp,0,sizeof(t_chsattr_params));
216 memset(&stp,0,sizeof(t_set_temp_params));
217 memset(&stsp,0,sizeof(t_set_timestep_params));
218 memset(&fp,0,sizeof(t_fill_params));
219 memset(&delp,0,sizeof(t_del_atoms_params));
220 memset(&crtp,0,sizeof(t_crt_params));
222 // get command + args
226 wptr=strtok(NULL," \t");
228 wptr=strtok(line," \t");
231 strncpy(word[wcnt],wptr,64);
235 if(!strncmp(word[0],"potential",9)) {
236 if(!strncmp(word[1],"albe",4))
237 mdrun->potential=MOLDYN_POTENTIAL_AM;
238 if(!strncmp(word[1],"albe_o",6))
239 mdrun->potential=MOLDYN_POTENTIAL_AO;
240 if(!strncmp(word[1],"tersoff",7))
241 mdrun->potential=MOLDYN_POTENTIAL_TM;
242 if(!strncmp(word[1],"ho",2))
243 mdrun->potential=MOLDYN_POTENTIAL_HO;
244 if(!strncmp(word[1],"lj",2))
245 mdrun->potential=MOLDYN_POTENTIAL_LJ;
247 else if(!strncmp(word[0],"continue",8))
248 strncpy(mdrun->continue_file,word[1],128);
249 else if(!strncmp(word[0],"cutoff",6))
250 mdrun->cutoff=atof(word[1]);
251 else if(!strncmp(word[0],"nnd",3))
252 mdrun->nnd=atof(word[1]);
253 else if(!strncmp(word[0],"intalgo",7)) {
254 if(!strncmp(word[1],"verlet",5))
255 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
257 else if(!strncmp(word[0],"timestep",8))
258 mdrun->timestep=atof(word[1]);
259 else if(!strncmp(word[0],"volume",6)) {
260 mdrun->dim.x=atof(word[1]);
261 mdrun->dim.y=atof(word[2]);
262 mdrun->dim.z=atof(word[3]);
263 if(strncmp(word[4],"0",1))
266 else if(!strncmp(word[0],"pbc",3)) {
267 if(strncmp(word[1],"0",1))
271 if(strncmp(word[2],"0",1))
275 if(strncmp(word[3],"0",1))
280 else if(!strncmp(word[0],"temperature",11))
281 mdrun->temperature=atof(word[1]);
282 else if(!strncmp(word[0],"pressure",8))
283 mdrun->pressure=atof(word[1]);
284 else if(!strncmp(word[0],"lattice",7)) {
285 if(!strncmp(word[1],"zincblende",10))
286 mdrun->lattice=ZINCBLENDE;
287 if(!strncmp(word[1],"fcc",3))
289 if(!strncmp(word[1],"diamond",7))
290 mdrun->lattice=DIAMOND;
291 if(!strncmp(word[1],"none",4))
294 mdrun->lc=atof(word[2]);
296 else if(!strncmp(word[0],"element1",8)) {
297 mdrun->element1=atoi(word[1]);
299 else if(!strncmp(word[0],"element2",8)) {
300 mdrun->element2=atoi(word[1]);
302 else if(!strncmp(word[0],"fill",6)) {
304 fp.fill_element=mdrun->element1;
306 fp.lattice=mdrun->lattice;
310 // parse fill command
313 if(!strncmp(word[i],"lc",2)) {
314 fp.lx=atoi(word[++i]);
315 fp.ly=atoi(word[++i]);
316 fp.lz=atoi(word[++i]);
317 fp.lc=atof(word[++i]);
321 if(!strncmp(word[i],"eb",2)) {
322 fp.fill_element=atoi(word[++i]);
323 fp.fill_brand=atoi(word[++i]);
326 if(word[i][0]=='p') {
331 fp.p_params.type=PART_INSIDE_R;
333 fp.p_params.type=PART_INSIDE_D;
337 fp.p_params.type=PART_OUTSIDE_R;
339 fp.p_params.type=PART_OUTSIDE_D;
344 if((fp.p_params.type==PART_INSIDE_R)||
345 (fp.p_params.type==PART_OUTSIDE_R)) {
346 fp.p_params.r=atof(word[++i]);
347 fp.p_params.p.x=atof(word[++i]);
348 fp.p_params.p.y=atof(word[++i]);
349 fp.p_params.p.z=atof(word[++i]);
351 if((fp.p_params.type==PART_INSIDE_D)||
352 (fp.p_params.type==PART_OUTSIDE_D)) {
353 fp.p_params.p.x=atof(word[++i]);
354 fp.p_params.p.y=atof(word[++i]);
355 fp.p_params.p.z=atof(word[++i]);
356 fp.p_params.d.x=atof(word[++i]);
357 fp.p_params.d.y=atof(word[++i]);
358 fp.p_params.d.z=atof(word[++i]);
362 if(word[i][0]=='d') {
363 switch(word[++i][0]) {
366 fp.d_params.type=DEFECT_TYPE_0D;
367 if(!strncmp(word[i+1],"dbx",3)) {
368 fp.d_params.stype=DEFECT_STYPE_DB_X;
370 if(!strncmp(word[i+1],"dby",3)) {
371 fp.d_params.stype=DEFECT_STYPE_DB_Y;
373 if(!strncmp(word[i+1],"dbz",3)) {
374 fp.d_params.stype=DEFECT_STYPE_DB_Z;
376 if(!strncmp(word[i+1],"dbr",3)) {
377 fp.d_params.stype=DEFECT_STYPE_DB_R;
380 fp.d_params.od=atof(word[++i]);
381 fp.d_params.dd=atof(word[++i]);
382 fp.d_params.element=atoi(word[++i]);
383 fp.d_params.brand=atoi(word[++i]);
385 fp.d_params.attr=ATOM_ATTR_HB|ATOM_ATTR_VA;
386 fp.d_params.attr|=ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
390 fp.d_params.type=DEFECT_TYPE_1D;
393 fp.d_params.type=DEFECT_TYPE_2D;
396 fp.d_params.type=DEFECT_TYPE_3D;
405 if(word[i][0]=='o') {
406 fp.o_params.o.x=atof(word[++i])*fp.lc;
407 fp.o_params.o.y=atof(word[++i])*fp.lc;
408 fp.o_params.o.z=atof(word[++i])*fp.lc;
414 add_stage(mdrun,STAGE_FILL,&fp);
416 else if(!strncmp(word[0],"thermal_init",12)) {
417 add_stage(mdrun,STAGE_THERMAL_INIT,NULL);
419 else if(!strncmp(word[0],"aattr",5)) {
420 // for aatrib line we need a special stage
421 // containing one schedule of 0 loops ...
422 for(i=0;i<strlen(word[1]);i++) {
425 cap.type|=CHAATTR_TOTALV;
428 cap.type|=CHAATTR_REGION;
431 cap.type|=CHAATTR_ELEMENT;
434 cap.type|=CHAATTR_NUMBER;
440 if(cap.type&CHAATTR_REGION) {
441 cap.x0=atof(word[2]);
442 cap.y0=atof(word[3]);
443 cap.z0=atof(word[4]);
444 cap.x1=atof(word[5]);
445 cap.y1=atof(word[6]);
446 cap.z1=atof(word[7]);
449 if(cap.type&CHAATTR_ELEMENT) {
450 cap.element=atoi(word[i]);
453 if(cap.type&CHAATTR_NUMBER) {
454 cap.element=atoi(word[i]);
457 for(o=0;o<strlen(word[i]);o++) {
460 cap.attr|=ATOM_ATTR_VB;
463 cap.attr|=ATOM_ATTR_HB;
466 cap.attr|=ATOM_ATTR_VA;
469 cap.attr|=ATOM_ATTR_FP;
472 cap.attr|=ATOM_ATTR_1BP;
475 cap.attr|=ATOM_ATTR_2BP;
478 cap.attr|=ATOM_ATTR_3BP;
484 add_stage(mdrun,STAGE_CHAATTR,&cap);
486 else if(!strncmp(word[0],"sattr",5)) {
487 // for satrib line we need a special stage
488 // containing one schedule of 0 loops ...
490 for(i=1;i<wcnt;i++) {
491 if(!strncmp(word[i],"pctrl",5)) {
492 csp.ptau=atof(word[++i]);
494 csp.ptau=0.01/(csp.ptau*GPA);
495 csp.type|=CHSATTR_PCTRL;
497 if(!strncmp(word[i],"tctrl",5)) {
498 csp.ttau=atof(word[++i]);
499 csp.type|=CHSATTR_TCTRL;
501 if(!strncmp(word[i],"prelax",6)) {
502 csp.dp=atof(word[++i])*BAR;
503 csp.type|=CHSATTR_PRELAX;
505 if(!strncmp(word[i],"trelax",6)) {
506 csp.dt=atof(word[++i]);
507 csp.type|=CHSATTR_TRELAX;
509 if(!strncmp(word[i],"rsteps",6)) {
510 csp.rsteps=atoi(word[++i]);
511 csp.type|=CHSATTR_RSTEPS;
513 if(!strncmp(word[i],"avgrst",6)) {
514 csp.avgrst=atoi(word[++i]);
515 csp.type|=CHSATTR_AVGRST;
518 add_stage(mdrun,STAGE_CHSATTR,&csp);
520 else if(!strncmp(word[0],"prerun",6))
521 mdrun->prerun=atoi(word[1]);
522 else if(!strncmp(word[0],"avgskip",7))
523 mdrun->avgskip=atoi(word[1]);
524 else if(!strncmp(word[0],"elog",4))
525 mdrun->elog=atoi(word[1]);
526 else if(!strncmp(word[0],"tlog",4))
527 mdrun->tlog=atoi(word[1]);
528 else if(!strncmp(word[0],"plog",4))
529 mdrun->plog=atoi(word[1]);
530 else if(!strncmp(word[0],"vlog",4))
531 mdrun->vlog=atoi(word[1]);
532 else if(!strncmp(word[0],"save",4))
533 mdrun->save=atoi(word[1]);
534 else if(!strncmp(word[0],"visualize",9))
535 mdrun->visualize=atoi(word[1]);
536 else if(!strncmp(word[0],"stage",5)) {
537 // for every stage line, add a stage
538 if(!strncmp(word[1],"displace",8)) {
539 dap.nr=atoi(word[2]);
540 dap.dx=atof(word[3]);
541 dap.dy=atof(word[4]);
542 dap.dz=atof(word[5]);
543 add_stage(mdrun,STAGE_DISPLACE_ATOM,&dap);
545 else if(!strncmp(word[1],"del_atoms",9)) {
546 delp.o.x=atof(word[2]);
547 delp.o.y=atof(word[3]);
548 delp.o.z=atof(word[4]);
549 delp.r=atof(word[5]);
550 add_stage(mdrun,STAGE_DEL_ATOMS,&delp);
552 else if(!strncmp(word[1],"mod_atoms",8)) {
555 if(!strncmp(word[i],"t",1)) {
556 map.tag=atoi(word[++i]);
559 if(!strncmp(word[i],"ekin",5)) {
560 map.ekin.x=atof(word[++i])*EV;
561 map.ekin.y=atof(word[++i])*EV;
562 map.ekin.z=atof(word[++i])*EV;
566 add_stage(mdrun,STAGE_MODIFY_ATOMS,&map);
568 else if(!strncmp(word[1],"ins_atoms",9)) {
569 iap.ins_steps=atoi(word[2]);
570 iap.ins_atoms=atoi(word[3]);
571 iap.element=atoi(word[4]);
572 iap.brand=atoi(word[5]);
573 for(i=0;i<strlen(word[6]);i++) {
576 iap.attr|=ATOM_ATTR_VB;
579 iap.attr|=ATOM_ATTR_HB;
582 iap.attr|=ATOM_ATTR_VA;
585 iap.attr|=ATOM_ATTR_FP;
588 iap.attr|=ATOM_ATTR_1BP;
591 iap.attr|=ATOM_ATTR_2BP;
594 iap.attr|=ATOM_ATTR_3BP;
604 iap.x0=atof(word[8]);
605 iap.y0=atof(word[9]);
606 iap.z0=atof(word[10]);
610 iap.x0=atof(word[8]);
611 iap.y0=atof(word[9]);
612 iap.z0=atof(word[10]);
619 iap.cr=atof(word[9]);
623 iap.x0=atof(word[9]);
624 iap.y0=atof(word[10]);
625 iap.z0=atof(word[11]);
626 iap.x1=atof(word[12]);
627 iap.y1=atof(word[13]);
628 iap.z1=atof(word[14]);
629 iap.cr=atof(word[15]);
633 iap.x0=atof(word[9]);
634 iap.y0=atof(word[10]);
635 iap.z0=atof(word[11]);
636 iap.x1=atof(word[12]);
637 iap.cr=atof(word[13]);
645 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
651 else if(!strncmp(word[1],"ins_m_atoms",11)) {
652 imp.element1=atoi(word[2]);
653 imp.element2=atoi(word[3]);
654 imp.amount1=atoi(word[4]);
655 imp.amount2=atoi(word[5]);
656 imp.brand1=atoi(word[6]);
657 imp.brand2=atoi(word[7]);
658 imp.crmin=atof(word[8]);
659 imp.crmax=atof(word[9]);
661 for(i=0;i<strlen(word[8]);i++) {
664 imp.attr|=ATOM_ATTR_VB;
667 imp.attr|=ATOM_ATTR_HB;
670 imp.attr|=ATOM_ATTR_VA;
673 imp.attr|=ATOM_ATTR_FP;
676 imp.attr|=ATOM_ATTR_1BP;
679 imp.attr|=ATOM_ATTR_2BP;
682 imp.attr|=ATOM_ATTR_3BP;
689 imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
690 imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
691 add_stage(mdrun,STAGE_INSERT_MIXED_ATOMS,&imp);
698 else if(!strncmp(word[1],"continue",8)) {
699 cp.runs=atoi(word[2]);
700 add_stage(mdrun,STAGE_CONTINUE,&cp);
702 else if(!strncmp(word[1],"anneal",6)) {
704 ap.runs=atoi(word[2]);
706 ap.interval=atoi(word[4]);
707 add_stage(mdrun,STAGE_ANNEAL,&ap);
709 else if(!strncmp(word[1],"set_temp",8)) {
710 if(word[2][0]=='c') {
711 stp.type=SET_TEMP_CURRENT;
715 stp.type=SET_TEMP_VALUE;
716 stp.val=atof(word[2]);
718 add_stage(mdrun,STAGE_SET_TEMP,&stp);
720 else if(!strncmp(word[1],"set_timestep",12)) {
721 stsp.tau=atof(word[2]);
722 add_stage(mdrun,STAGE_SET_TIMESTEP,&stsp);
724 else if(!strncmp(word[1],"crt",3)) {
725 crtp.type=atoi(word[2]);
726 crtp.steps=atoi(word[3]);
727 strncpy(crtp.file,word[4],127);
728 add_stage(mdrun,STAGE_CRT,&crtp);
731 printf("%s unknown stage type: %s\n",
737 printf("%s unknown keyword '%s', skipped!\n",
749 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
753 if(!(mdrun->sattr&SATTR_PRELAX)) {
757 delta=moldyn->p_avg-moldyn->p_ref;
768 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
772 if(!(mdrun->sattr&SATTR_TRELAX))
775 delta=moldyn->t_avg-moldyn->t_ref;
786 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
788 t_displace_atom_params *dap;
792 stage=mdrun->stage.current->data;
795 atom=&(moldyn->atom[dap->nr]);
803 int del_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
806 t_del_atoms_params *delp;
812 stage=mdrun->stage.current->data;
818 for(i=0;i<moldyn->count;i++) {
819 v3_sub(&dist,&(delp->o),&(moldyn->atom[i].r));
820 //printf("%d ----> %f %f %f = %f | %f\n",i,dist.x,dist.y,dist.z,v3_absolute_square(&dist),delp->r*delp->r);
821 if(v3_absolute_square(&dist)<=(delp->r*delp->r)) {
823 del_atom(moldyn,moldyn->atom[i].tag);
824 printf("%s atom deleted: %d %d %d\n",ME,
826 moldyn->atom[i].element,
827 moldyn->atom[i].brand);
832 del_atom(moldyn,moldyn->atom[i].tag);
833 printf("%s atom deleted: %d %d %d\n",ME,
835 moldyn->atom[i].element,
836 moldyn->atom[i].brand);
844 int modify_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
846 t_modify_atoms_params *map;
853 stage=mdrun->stage.current->data;
855 v.x=0.0; v.y=0.0; v.z=0.0;
857 for(i=0;i<moldyn->count;i++) {
858 if(atom[i].tag==map->tag) {
859 v.x=sqrt(2.0*fabs(map->ekin.x)/atom[i].mass);
862 v.y=sqrt(2.0*fabs(map->ekin.y)/atom[i].mass);
865 v.z=sqrt(2.0*fabs(map->ekin.z)/atom[i].mass);
868 v3_copy(&(atom[i].v),&v);
869 printf("%s atom modified: v = (%f %f %f)\n",
877 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
879 t_insert_atoms_params *iap;
889 stage=mdrun->stage.current->data;
894 v.x=0.0; v.y=0.0; v.z=0.0;
898 switch(mdrun->lattice) {
949 printf("%s unknown insertion mode: %02x\n",
955 while(cnt<iap->ins_atoms) {
958 if((iap->type!=INS_POS)&&(iap->type!=INS_RELPOS)) {
959 r.x=rand_get_double(&(moldyn->random))*x;
960 r.y=rand_get_double(&(moldyn->random))*y;
961 r.z=rand_get_double(&(moldyn->random))*z;
968 if(iap->type==INS_RELPOS) {
979 if(iap->type!=INS_TOTAL) {
986 dmin=1000; // for sure too high!
987 for(i=0;i<moldyn->count;i++) {
988 atom=&(moldyn->atom[i]);
989 v3_sub(&dist,&(atom->r),&r);
990 check_per_bound(moldyn,&dist);
991 d=v3_absolute_square(&dist);
992 if(d<(iap->cr*iap->cr)) {
1000 if(iap->type==INS_SPHERE) {
1001 if((r.x-iap->x0)*(r.x-iap->x0)+
1002 (r.y-iap->y0)*(r.y-iap->y0)+
1003 (r.z-iap->z0)*(r.z-iap->z0)>
1004 (iap->x1*iap->x1)) {
1009 add_atom(moldyn,iap->element,
1010 iap->brand,iap->attr,&r,&v);
1011 printf("%s atom inserted (%d/%d): %f %f %f\n",
1012 ME,(iap->cnt_steps+1)*iap->ins_atoms,
1013 iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
1014 printf(" attributes: ");
1015 if(iap->attr&ATOM_ATTR_VB)
1017 if(iap->attr&ATOM_ATTR_HB)
1019 if(iap->attr&ATOM_ATTR_VA)
1021 if(iap->attr&ATOM_ATTR_FP)
1023 if(iap->attr&ATOM_ATTR_1BP)
1025 if(iap->attr&ATOM_ATTR_2BP)
1027 if(iap->attr&ATOM_ATTR_3BP)
1030 printf(" d2 = %f/%f\n",dmin,iap->cr*iap->cr);
1037 int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
1040 t_insert_mixed_atoms_params *imp;
1042 double x,x0,y,y0,z,z0;
1043 double dmin,d,cmin,cmax;
1049 stage=mdrun->stage.current->data;
1063 cmin=imp->crmin*imp->crmin;
1064 cmax=imp->crmax*imp->crmax;
1066 while(imp->amount1|imp->amount2) {
1071 r.x=rand_get_double(&(moldyn->random))*x;
1072 r.y=rand_get_double(&(moldyn->random))*y;
1073 r.z=rand_get_double(&(moldyn->random))*z;
1077 dmin=1000.0; // for sure too high!
1078 for(i=0;i<moldyn->count;i++) {
1079 atom=&(moldyn->atom[i]);
1080 v3_sub(&dist,&(atom->r),&r);
1081 check_per_bound(moldyn,&dist);
1082 d=v3_absolute_square(&dist);
1094 add_atom(moldyn,imp->element1,
1095 imp->brand1,imp->attr1,&r,&v);
1096 printf("%s (mixed) atom inserted (%d): %f %f %f\n",
1097 ME,imp->amount1,r.x,r.y,r.z);
1098 printf(" -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
1105 r.x=rand_get_double(&(moldyn->random))*x;
1106 r.y=rand_get_double(&(moldyn->random))*y;
1107 r.z=rand_get_double(&(moldyn->random))*z;
1111 dmin=1000.0; // for sure too high!
1112 for(i=0;i<moldyn->count;i++) {
1113 atom=&(moldyn->atom[i]);
1114 v3_sub(&dist,&(atom->r),&r);
1115 check_per_bound(moldyn,&dist);
1116 d=v3_absolute_square(&dist);
1128 add_atom(moldyn,imp->element2,
1129 imp->brand2,imp->attr2,&r,&v);
1130 printf("%s (mixed) atom inserted (%d): %f %f %f\n",
1131 ME,imp->amount2,r.x,r.y,r.z);
1132 printf(" -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
1140 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
1143 t_chaattr_params *cap;
1147 stage=mdrun->stage.current->data;
1150 for(i=0;i<moldyn->count;i++) {
1151 atom=&(moldyn->atom[i]);
1152 if(cap->type&CHAATTR_ELEMENT) {
1153 if(cap->element!=atom->element)
1156 if(cap->type&CHAATTR_NUMBER) {
1157 if(cap->element!=atom->tag)
1160 if(cap->type&CHAATTR_REGION) {
1161 if(cap->x0>atom->r.x)
1163 if(cap->y0>atom->r.y)
1165 if(cap->z0>atom->r.z)
1167 if(cap->x1<atom->r.x)
1169 if(cap->y1<atom->r.y)
1171 if(cap->z1<atom->r.z)
1174 if(!(cap->type&CHAATTR_TOTALV))
1175 printf(" changing attributes of atom %d (0x%x)\n",
1177 atom->attr=cap->attr;
1185 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
1188 t_chsattr_params *csp;
1190 stage=mdrun->stage.current->data;
1193 if(csp->type&CHSATTR_PCTRL) {
1195 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
1197 set_p_scale(moldyn,P_SCALE_NONE,1.0);
1199 if(csp->type&CHSATTR_TCTRL) {
1201 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
1203 set_t_scale(moldyn,T_SCALE_NONE,1.0);
1205 if(csp->type&CHSATTR_PRELAX) {
1207 mdrun->sattr&=(~(SATTR_PRELAX));
1209 mdrun->sattr|=SATTR_PRELAX;
1212 if(csp->type&CHSATTR_TRELAX) {
1214 mdrun->sattr&=(~(SATTR_TRELAX));
1216 mdrun->sattr|=SATTR_TRELAX;
1219 if(csp->type&CHSATTR_AVGRST) {
1221 mdrun->sattr|=SATTR_AVGRST;
1223 mdrun->sattr&=(~(SATTR_AVGRST));
1225 if(csp->type&CHSATTR_RSTEPS) {
1226 mdrun->relax_steps=csp->rsteps;
1232 int crt(t_moldyn *moldyn,t_mdrun *mdrun) {
1249 stage=mdrun->stage.current->data;
1256 if(crtp->count==0) {
1257 printf(" crt init\n");
1258 // read final positions, constraints and do the alloc
1259 fd=open(crtp->file,O_RDONLY);
1261 perror("[mdrun] FATAL reading constraints file");
1265 ret=get_line(fd,line,128);
1266 // check for end of file
1268 printf(" read %d atom positions\n",acount);
1269 if(acount!=moldyn->count)
1270 printf(" atom count mismatch!!!\n");
1274 // ignore # lines and \n
1275 if((line[0]=='#')|(ret==1))
1277 // allocate new memory
1278 ptr=realloc(crtp->r_fin,(acount+1)*sizeof(t_3dvec));
1280 perror("[mdrun] FATAL realloc crt positions");
1284 ptr=realloc(constraints,(acount+1)*3*sizeof(u8));
1286 perror("[mdrun] FATAL realloc crt constraints");
1291 wptr=strtok(line," \t");
1293 wptr=strtok(NULL," \t");
1294 crtp->r_fin[acount].x=atof(wptr);
1295 wptr=strtok(NULL," \t");
1296 crtp->r_fin[acount].y=atof(wptr);
1297 wptr=strtok(NULL," \t");
1298 crtp->r_fin[acount].z=atof(wptr);
1300 wptr=strtok(NULL," \t");
1301 constraints[3*acount]=atoi(wptr);
1302 wptr=strtok(NULL," \t");
1303 constraints[3*acount+1]=atoi(wptr);
1304 wptr=strtok(NULL," \t");
1305 constraints[3*acount+2]=atoi(wptr);
1310 // allocate trafo angles
1311 trafo_angle=malloc(acount*2*sizeof(double));
1312 if(trafo_angle==NULL) {
1313 perror("[mdrun] FATAL alloc trafo angles");
1320 /* write a save file s-crt_xofy.save */
1321 snprintf(line,128,"%s/s-crt_%03dof%03d.save",
1322 moldyn->vlsdir,crtp->count,crtp->steps);
1323 fd=open(line,O_WRONLY|O_TRUNC|O_CREAT,S_IRUSR|S_IWUSR);
1324 if(fd<0) perror("[mdrun] crt save fd open");
1326 write(fd,moldyn,sizeof(t_moldyn));
1327 write(fd,moldyn->atom,
1328 moldyn->count*sizeof(t_atom));
1331 /* visualize atoms */
1332 visual_atoms(moldyn);
1335 printf(" crt energy: %d - %f\n\n",
1336 crtp->count,(moldyn->ekin+moldyn->energy)/EV);
1338 /* crt routines: calculate displacement + set individual constraints */
1340 printf(" crt step %d of %d in total\n\n",crtp->count+1,crtp->steps);
1342 if((crtp->type==1)|(crtp->count==0))
1343 printf(" crt angle update\n\n");
1345 for(i=0;i<moldyn->count;i++) {
1346 // calc displacements
1348 v3_sub(&disp,&(crtp->r_fin[i]),&(atom[i].r));
1350 if((crtp->type==1)|(crtp->count==0)) {
1351 trafo_angle[2*i]=atan2(disp.x,disp.y);
1352 trafo_angle[2*i+1]=-atan2(disp.z,
1353 sqrt(disp.x*disp.x+disp.y*disp.y));
1356 frac=1.0/(crtp->steps-crtp->count);
1357 v3_scale(&disp,&disp,frac);
1358 v3_add(&(atom[i].r),&(atom[i].r),&disp);
1364 #define stage_print(m) if(!(stage->executed)) \
1367 int mdrun_hook(void *ptr1,void *ptr2) {
1377 t_insert_atoms_params *iap;
1378 t_insert_mixed_atoms_params *imp;
1379 t_continue_params *cp;
1380 t_anneal_params *ap;
1381 t_set_temp_params *stp;
1382 t_set_timestep_params *stsp;
1393 /* return immediately if there are no more stage */
1394 if(sl->current==NULL)
1397 /* get stage description */
1398 stage=sl->current->data;
1400 /* steps in next schedule */
1401 steps=mdrun->relax_steps;
1403 /* check whether relaxation steps are necessary */
1404 if((check_pressure(moldyn,mdrun)==TRUE)&\
1405 (check_temperature(moldyn,mdrun)==TRUE)) {
1408 stage_print("\n###########################\n");
1409 stage_print("# [mdrun] executing stage #\n");
1410 stage_print("###########################\n\n");
1412 /* stage specific stuff */
1413 switch(stage->type) {
1414 case STAGE_DISPLACE_ATOM:
1415 stage_print(" -> displace atom\n\n");
1416 displace_atom(moldyn,mdrun);
1419 case STAGE_DEL_ATOMS:
1420 stage_print(" -> del atoms\n\n");
1421 del_atoms(moldyn,mdrun);
1424 case STAGE_MODIFY_ATOMS:
1425 stage_print(" -> modify atoms\n\n");
1426 modify_atoms(moldyn,mdrun);
1429 case STAGE_INSERT_ATOMS:
1430 stage_print(" -> insert atoms\n\n");
1432 if(iap->cnt_steps==iap->ins_steps) {
1436 insert_atoms(moldyn,mdrun);
1442 case STAGE_INSERT_MIXED_ATOMS:
1443 stage_print(" -> insert mixed atoms\n\n");
1445 insert_mixed_atoms(moldyn,mdrun);
1451 case STAGE_CONTINUE:
1452 stage_print(" -> continue\n\n");
1453 if(stage->executed==TRUE) {
1461 stage_print(" -> anneal\n\n");
1463 if(ap->count==ap->runs) {
1467 if(moldyn->t_ref+ap->dt>=0.0)
1468 set_temperature(moldyn,
1469 moldyn->t_ref+ap->dt);
1474 stage_print(" -> change atom attributes\n\n");
1475 chaatr(moldyn,mdrun);
1479 stage_print(" -> change sys attributes\n\n");
1480 chsattr(moldyn,mdrun);
1483 case STAGE_SET_TEMP:
1484 stage_print(" -> set temperature\n\n");
1486 if(stp->type&SET_TEMP_CURRENT) {
1487 set_temperature(moldyn,moldyn->t_avg);
1490 set_temperature(moldyn,stp->val);
1494 case STAGE_SET_TIMESTEP:
1495 stage_print(" -> set timestep\n\n");
1497 mdrun->timestep=stsp->tau;
1501 stage_print(" -> fill lattice\n\n");
1503 switch(fp->lattice) {
1506 o.x=0.5*0.25*fp->lc;
1509 create_lattice(moldyn,
1512 DEFAULT_ATOM_ATTR,0,
1513 fp->lx,fp->ly,fp->lz,
1521 create_lattice(moldyn,
1524 DEFAULT_ATOM_ATTR,1,
1525 fp->lx,fp->ly,fp->lz,
1534 create_lattice(moldyn,
1539 fp->lx,fp->ly,fp->lz,
1546 moldyn_bc_check(moldyn);
1549 case STAGE_THERMAL_INIT:
1550 stage_print(" -> thermal init\n\n");
1551 thermal_init(moldyn,TRUE);
1555 stage_print(" -> constraint relaxation");
1556 stage_print(" technique\n\n");
1558 if(crtp->count==crtp->steps) {
1569 printf("%s unknwon stage type\n",ME);
1573 /* mark as executed */
1574 stage->executed=TRUE;
1577 if(change_stage==TRUE) {
1578 printf("%s finished stage\n",ME);
1579 if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
1580 printf("%s no more stages\n",ME);
1590 if(mdrun->sattr&SATTR_AVGRST)
1591 average_reset(moldyn);
1595 /* continue simulation */
1596 moldyn_add_schedule(moldyn,steps,mdrun->timestep);
1601 int main(int argc,char **argv) {
1608 memset(&mdrun,0,sizeof(t_mdrun));
1609 memset(&moldyn,0,sizeof(t_moldyn));
1611 /* init crt variables */
1616 /* parse arguments */
1617 if(mdrun_parse_argv(&mdrun,argc,argv)<0)
1620 /* initialize list system */
1621 list_init_f(&(mdrun.stage));
1623 /* parse config file */
1624 mdrun_parse_config(&mdrun);
1626 /* reset the stage list */
1627 list_reset_f(&(mdrun.stage));
1631 /* prepare simulation */
1633 if(mdrun.continue_file[0]) {
1634 // read the save file
1635 moldyn_read_save_file(&moldyn,mdrun.continue_file);
1636 // manualaadjusting some stuff
1639 rand_init(&(moldyn.random),NULL,1);
1640 moldyn.random.status|=RAND_STAT_VERBOSE;
1643 moldyn_init(&moldyn,argc,argv);
1646 if(set_int_alg(&moldyn,mdrun.intalgo)<0)
1650 set_cutoff(&moldyn,mdrun.cutoff);
1651 if(set_potential(&moldyn,mdrun.potential)<0)
1653 switch(mdrun.potential) {
1654 case MOLDYN_POTENTIAL_AM:
1655 albe_mult_set_params(&moldyn,
1659 case MOLDYN_POTENTIAL_AO:
1660 albe_orig_mult_set_params(&moldyn,
1664 case MOLDYN_POTENTIAL_TM:
1665 tersoff_mult_set_params(&moldyn,
1669 case MOLDYN_POTENTIAL_HO:
1670 harmonic_oscillator_set_params(&moldyn,mdrun.element1);
1672 case MOLDYN_POTENTIAL_LJ:
1673 lennard_jones_set_params(&moldyn,mdrun.element1);
1676 printf("%s unknown potential: %02x\n",
1677 ME,mdrun.potential);
1681 /* if it is a continue run, reset schedule and skip lattice init */
1682 if(mdrun.continue_file[0]) {
1683 memset(&(moldyn.schedule),0,sizeof(t_moldyn_schedule));
1687 /* initial lattice and dimensions */
1688 set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
1689 set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
1690 /* replaced by fill stage !!
1691 switch(mdrun.lattice) {
1693 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.fill_element,
1694 mdrun.m1,DEFAULT_ATOM_ATTR,
1695 mdrun.fill_brand,mdrun.lx,
1696 mdrun.ly,mdrun.lz,NULL,0,NULL);
1699 create_lattice(&moldyn,DIAMOND,mdrun.lc,
1701 mdrun.m1,DEFAULT_ATOM_ATTR,
1702 mdrun.fill_brand,mdrun.lx,
1703 mdrun.ly,mdrun.lz,NULL,0,NULL);
1706 o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1707 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1708 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1709 mdrun.ly,mdrun.lz,&o,0,NULL);
1710 o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1711 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
1712 mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
1713 mdrun.ly,mdrun.lz,&o,0,NULL);
1716 set_nn_dist(&moldyn,mdrun.nnd);
1719 printf("%s unknown lattice type: %02x\n",
1723 moldyn_bc_check(&moldyn);
1724 replaced by fill stage !! */
1726 /* temperature and pressure */
1727 set_temperature(&moldyn,mdrun.temperature);
1728 set_pressure(&moldyn,mdrun.pressure);
1729 /* replaced thermal_init stage
1730 thermal_init(&moldyn,TRUE);
1734 /* first schedule */
1735 moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
1738 moldyn_set_log_dir(&moldyn,mdrun.sdir);
1739 moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
1741 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
1743 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
1745 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
1747 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
1749 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
1751 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
1752 moldyn_set_log(&moldyn,CREATE_REPORT,0);
1753 set_avg_skip(&moldyn,mdrun.avgskip);
1755 /* prepare the hook function */
1756 moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
1758 /* run the simulation */
1759 moldyn_integrate(&moldyn);
1762 moldyn_shutdown(&moldyn);
1764 list_destroy_f(&(mdrun.stage));