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"
15 #include "potentials/tersoff_orig.h"
17 #include "potentials/tersoff.h"
26 int mdrun_usage(void) {
28 printf("%s usage:\n",ME);
33 int mdrun_parse_argv(t_mdrun *mdrun,int argc,char **argv) {
40 printf("%s unknown switch: %s\n",ME,argv[i]);
46 strncpy(mdrun->cfile,argv[++i],128);
49 strncpy(mdrun->sdir,argv[++i],128);
55 printf("%s unknown option: %s\n",ME,argv[i]);
65 int del_stages(t_mdrun *mdrun) {
78 stage=sl->current->data;
81 } while(list_next_f(sl)!=L_NO_NEXT_ELEMENT);
86 int add_stage(t_mdrun *mdrun,u8 type,void *params) {
93 case STAGE_DISPLACE_ATOM:
94 psize=sizeof(t_displace_atom_params);
97 psize=sizeof(t_del_atoms_params);
99 case STAGE_MODIFY_ATOMS:
100 psize=sizeof(t_modify_atoms_params);
102 case STAGE_INSERT_ATOMS:
103 psize=sizeof(t_insert_atoms_params);
105 case STAGE_INSERT_MIXED_ATOMS:
106 psize=sizeof(t_insert_mixed_atoms_params);
109 psize=sizeof(t_continue_params);
112 psize=sizeof(t_anneal_params);
115 psize=sizeof(t_chaattr_params);
118 psize=sizeof(t_chsattr_params);
121 psize=sizeof(t_set_temp_params);
123 case STAGE_SET_TIMESTEP:
124 psize=sizeof(t_set_timestep_params);
127 psize=sizeof(t_fill_params);
129 case STAGE_THERMAL_INIT:
133 psize=sizeof(t_crt_params);
136 printf("%s unknown stage type: %02x\n",ME,type);
140 stage=malloc(sizeof(t_stage));
142 perror("[mdrun] malloc (add stage)");
147 stage->executed=FALSE;
149 stage->params=malloc(psize);
150 if(stage->params==NULL) {
151 perror("[mdrun] malloc (stage params)");
155 memcpy(stage->params,params,psize);
157 list_add_immediate_f(&(mdrun->stage),stage);
162 int mdrun_parse_config(t_mdrun *mdrun) {
172 t_displace_atom_params dap;
173 t_modify_atoms_params map;
174 t_insert_atoms_params iap;
175 t_insert_mixed_atoms_params imp;
176 t_continue_params cp;
178 t_chaattr_params cap;
179 t_chsattr_params csp;
180 t_set_temp_params stp;
181 t_set_timestep_params stsp;
183 t_del_atoms_params delp;
186 /* open config file */
187 fd=open(mdrun->cfile,O_RDONLY);
189 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
194 /* read, parse, set */
199 ret=get_line(fd,line,128);
203 // ignore # lines and \n
204 if((line[0]=='#')|(ret==1))
208 memset(&iap,0,sizeof(t_insert_atoms_params));
209 memset(&map,0,sizeof(t_modify_atoms_params));
210 memset(&imp,0,sizeof(t_insert_mixed_atoms_params));
211 memset(&cp,0,sizeof(t_continue_params));
212 memset(&ap,0,sizeof(t_anneal_params));
213 memset(&cap,0,sizeof(t_chaattr_params));
214 memset(&csp,0,sizeof(t_chsattr_params));
215 memset(&stp,0,sizeof(t_set_temp_params));
216 memset(&stsp,0,sizeof(t_set_timestep_params));
217 memset(&fp,0,sizeof(t_fill_params));
218 memset(&delp,0,sizeof(t_del_atoms_params));
219 memset(&crtp,0,sizeof(t_crt_params));
221 // get command + args
225 wptr=strtok(NULL," \t");
227 wptr=strtok(line," \t");
230 strncpy(word[wcnt],wptr,64);
234 if(!strncmp(word[0],"potential",9)) {
235 if(!strncmp(word[1],"albe",4))
236 mdrun->potential=MOLDYN_POTENTIAL_AM;
237 if(!strncmp(word[1],"tersoff",7))
238 mdrun->potential=MOLDYN_POTENTIAL_TM;
239 if(!strncmp(word[1],"ho",2))
240 mdrun->potential=MOLDYN_POTENTIAL_HO;
241 if(!strncmp(word[1],"lj",2))
242 mdrun->potential=MOLDYN_POTENTIAL_LJ;
244 else if(!strncmp(word[0],"continue",8))
245 strncpy(mdrun->continue_file,word[1],128);
246 else if(!strncmp(word[0],"cutoff",6))
247 mdrun->cutoff=atof(word[1]);
248 else if(!strncmp(word[0],"nnd",3))
249 mdrun->nnd=atof(word[1]);
250 else if(!strncmp(word[0],"intalgo",7)) {
251 if(!strncmp(word[1],"verlet",5))
252 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
254 else if(!strncmp(word[0],"timestep",8))
255 mdrun->timestep=atof(word[1]);
256 else if(!strncmp(word[0],"volume",6)) {
257 mdrun->dim.x=atof(word[1]);
258 mdrun->dim.y=atof(word[2]);
259 mdrun->dim.z=atof(word[3]);
260 if(strncmp(word[4],"0",1))
263 else if(!strncmp(word[0],"pbc",3)) {
264 if(strncmp(word[1],"0",1))
268 if(strncmp(word[2],"0",1))
272 if(strncmp(word[3],"0",1))
277 else if(!strncmp(word[0],"temperature",11))
278 mdrun->temperature=atof(word[1]);
279 else if(!strncmp(word[0],"pressure",8))
280 mdrun->pressure=atof(word[1]);
281 else if(!strncmp(word[0],"lattice",7)) {
282 if(!strncmp(word[1],"zincblende",10))
283 mdrun->lattice=ZINCBLENDE;
284 if(!strncmp(word[1],"fcc",3))
286 if(!strncmp(word[1],"diamond",7))
287 mdrun->lattice=DIAMOND;
288 if(!strncmp(word[1],"none",4))
291 mdrun->lc=atof(word[2]);
293 else if(!strncmp(word[0],"element1",8)) {
294 mdrun->element1=atoi(word[1]);
296 else if(!strncmp(word[0],"element2",8)) {
297 mdrun->element2=atoi(word[1]);
299 else if(!strncmp(word[0],"fill",6)) {
301 fp.fill_element=mdrun->element1;
303 fp.lattice=mdrun->lattice;
307 // parse fill command
310 if(!strncmp(word[i],"lc",2)) {
311 fp.lx=atoi(word[++i]);
312 fp.ly=atoi(word[++i]);
313 fp.lz=atoi(word[++i]);
314 fp.lc=atof(word[++i]);
318 if(!strncmp(word[i],"eb",2)) {
319 fp.fill_element=atoi(word[++i]);
320 fp.fill_brand=atoi(word[++i]);
323 if(word[i][0]=='p') {
328 fp.p_params.type=PART_INSIDE_R;
330 fp.p_params.type=PART_INSIDE_D;
334 fp.p_params.type=PART_OUTSIDE_R;
336 fp.p_params.type=PART_OUTSIDE_D;
341 if((fp.p_params.type==PART_INSIDE_R)||
342 (fp.p_params.type==PART_OUTSIDE_R)) {
343 fp.p_params.r=atof(word[++i]);
344 fp.p_params.p.x=atof(word[++i]);
345 fp.p_params.p.y=atof(word[++i]);
346 fp.p_params.p.z=atof(word[++i]);
348 if((fp.p_params.type==PART_INSIDE_D)||
349 (fp.p_params.type==PART_OUTSIDE_D)) {
350 fp.p_params.p.x=atof(word[++i]);
351 fp.p_params.p.y=atof(word[++i]);
352 fp.p_params.p.z=atof(word[++i]);
353 fp.p_params.d.x=atof(word[++i]);
354 fp.p_params.d.y=atof(word[++i]);
355 fp.p_params.d.z=atof(word[++i]);
359 if(word[i][0]=='d') {
360 switch(word[++i][0]) {
363 fp.d_params.type=DEFECT_TYPE_0D;
364 if(!strncmp(word[i+1],"dbx",3)) {
365 fp.d_params.stype=DEFECT_STYPE_DB_X;
367 if(!strncmp(word[i+1],"dby",3)) {
368 fp.d_params.stype=DEFECT_STYPE_DB_Y;
370 if(!strncmp(word[i+1],"dbz",3)) {
371 fp.d_params.stype=DEFECT_STYPE_DB_Z;
373 if(!strncmp(word[i+1],"dbr",3)) {
374 fp.d_params.stype=DEFECT_STYPE_DB_R;
377 fp.d_params.od=atof(word[++i]);
378 fp.d_params.dd=atof(word[++i]);
379 fp.d_params.element=atoi(word[++i]);
380 fp.d_params.brand=atoi(word[++i]);
382 fp.d_params.attr=ATOM_ATTR_HB|ATOM_ATTR_VA;
383 fp.d_params.attr|=ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
387 fp.d_params.type=DEFECT_TYPE_1D;
390 fp.d_params.type=DEFECT_TYPE_2D;
393 fp.d_params.type=DEFECT_TYPE_3D;
402 if(word[i][0]=='o') {
403 fp.o_params.o.x=atof(word[++i])*fp.lc;
404 fp.o_params.o.y=atof(word[++i])*fp.lc;
405 fp.o_params.o.z=atof(word[++i])*fp.lc;
411 add_stage(mdrun,STAGE_FILL,&fp);
413 else if(!strncmp(word[0],"thermal_init",12)) {
414 add_stage(mdrun,STAGE_THERMAL_INIT,NULL);
416 else if(!strncmp(word[0],"aattr",5)) {
417 // for aatrib line we need a special stage
418 // containing one schedule of 0 loops ...
419 for(i=0;i<strlen(word[1]);i++) {
422 cap.type|=CHAATTR_TOTALV;
425 cap.type|=CHAATTR_REGION;
428 cap.type|=CHAATTR_ELEMENT;
431 cap.type|=CHAATTR_NUMBER;
437 if(cap.type&CHAATTR_REGION) {
438 cap.x0=atof(word[2]);
439 cap.y0=atof(word[3]);
440 cap.z0=atof(word[4]);
441 cap.x1=atof(word[5]);
442 cap.y1=atof(word[6]);
443 cap.z1=atof(word[7]);
446 if(cap.type&CHAATTR_ELEMENT) {
447 cap.element=atoi(word[i]);
450 if(cap.type&CHAATTR_NUMBER) {
451 cap.element=atoi(word[i]);
454 for(o=0;o<strlen(word[i]);o++) {
457 cap.attr|=ATOM_ATTR_VB;
460 cap.attr|=ATOM_ATTR_HB;
463 cap.attr|=ATOM_ATTR_VA;
466 cap.attr|=ATOM_ATTR_FP;
469 cap.attr|=ATOM_ATTR_1BP;
472 cap.attr|=ATOM_ATTR_2BP;
475 cap.attr|=ATOM_ATTR_3BP;
481 add_stage(mdrun,STAGE_CHAATTR,&cap);
483 else if(!strncmp(word[0],"sattr",5)) {
484 // for satrib line we need a special stage
485 // containing one schedule of 0 loops ...
487 for(i=1;i<wcnt;i++) {
488 if(!strncmp(word[i],"pctrl",5)) {
489 csp.ptau=atof(word[++i]);
491 csp.ptau=0.01/(csp.ptau*GPA);
492 csp.type|=CHSATTR_PCTRL;
494 if(!strncmp(word[i],"tctrl",5)) {
495 csp.ttau=atof(word[++i]);
496 csp.type|=CHSATTR_TCTRL;
498 if(!strncmp(word[i],"prelax",6)) {
499 csp.dp=atof(word[++i])*BAR;
500 csp.type|=CHSATTR_PRELAX;
502 if(!strncmp(word[i],"trelax",6)) {
503 csp.dt=atof(word[++i]);
504 csp.type|=CHSATTR_TRELAX;
506 if(!strncmp(word[i],"rsteps",6)) {
507 csp.rsteps=atoi(word[++i]);
508 csp.type|=CHSATTR_RSTEPS;
510 if(!strncmp(word[i],"avgrst",6)) {
511 csp.avgrst=atoi(word[++i]);
512 csp.type|=CHSATTR_AVGRST;
515 add_stage(mdrun,STAGE_CHSATTR,&csp);
517 else if(!strncmp(word[0],"prerun",6))
518 mdrun->prerun=atoi(word[1]);
519 else if(!strncmp(word[0],"avgskip",7))
520 mdrun->avgskip=atoi(word[1]);
521 else if(!strncmp(word[0],"elog",4))
522 mdrun->elog=atoi(word[1]);
523 else if(!strncmp(word[0],"tlog",4))
524 mdrun->tlog=atoi(word[1]);
525 else if(!strncmp(word[0],"plog",4))
526 mdrun->plog=atoi(word[1]);
527 else if(!strncmp(word[0],"vlog",4))
528 mdrun->vlog=atoi(word[1]);
529 else if(!strncmp(word[0],"save",4))
530 mdrun->save=atoi(word[1]);
531 else if(!strncmp(word[0],"visualize",9))
532 mdrun->visualize=atoi(word[1]);
533 else if(!strncmp(word[0],"stage",5)) {
534 // for every stage line, add a stage
535 if(!strncmp(word[1],"displace",8)) {
536 dap.nr=atoi(word[2]);
537 dap.dx=atof(word[3]);
538 dap.dy=atof(word[4]);
539 dap.dz=atof(word[5]);
540 add_stage(mdrun,STAGE_DISPLACE_ATOM,&dap);
542 else if(!strncmp(word[1],"del_atoms",9)) {
543 delp.o.x=atof(word[2]);
544 delp.o.y=atof(word[3]);
545 delp.o.z=atof(word[4]);
546 delp.r=atof(word[5]);
547 add_stage(mdrun,STAGE_DEL_ATOMS,&delp);
549 else if(!strncmp(word[1],"mod_atoms",8)) {
552 if(!strncmp(word[i],"t",1)) {
553 map.tag=atoi(word[++i]);
556 if(!strncmp(word[i],"ekin",5)) {
557 map.ekin.x=atof(word[++i])*EV;
558 map.ekin.y=atof(word[++i])*EV;
559 map.ekin.z=atof(word[++i])*EV;
563 add_stage(mdrun,STAGE_MODIFY_ATOMS,&map);
565 else if(!strncmp(word[1],"ins_atoms",9)) {
566 iap.ins_steps=atoi(word[2]);
567 iap.ins_atoms=atoi(word[3]);
568 iap.element=atoi(word[4]);
569 iap.brand=atoi(word[5]);
570 for(i=0;i<strlen(word[6]);i++) {
573 iap.attr|=ATOM_ATTR_VB;
576 iap.attr|=ATOM_ATTR_HB;
579 iap.attr|=ATOM_ATTR_VA;
582 iap.attr|=ATOM_ATTR_FP;
585 iap.attr|=ATOM_ATTR_1BP;
588 iap.attr|=ATOM_ATTR_2BP;
591 iap.attr|=ATOM_ATTR_3BP;
601 iap.x0=atof(word[8]);
602 iap.y0=atof(word[9]);
603 iap.z0=atof(word[10]);
607 iap.x0=atof(word[8]);
608 iap.y0=atof(word[9]);
609 iap.z0=atof(word[10]);
616 iap.cr=atof(word[9]);
620 iap.x0=atof(word[9]);
621 iap.y0=atof(word[10]);
622 iap.z0=atof(word[11]);
623 iap.x1=atof(word[12]);
624 iap.y1=atof(word[13]);
625 iap.z1=atof(word[14]);
626 iap.cr=atof(word[15]);
630 iap.x0=atof(word[9]);
631 iap.y0=atof(word[10]);
632 iap.z0=atof(word[11]);
633 iap.x1=atof(word[12]);
634 iap.cr=atof(word[13]);
642 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
648 else if(!strncmp(word[1],"ins_m_atoms",11)) {
649 imp.element1=atoi(word[2]);
650 imp.element2=atoi(word[3]);
651 imp.amount1=atoi(word[4]);
652 imp.amount2=atoi(word[5]);
653 imp.brand1=atoi(word[6]);
654 imp.brand2=atoi(word[7]);
655 imp.crmin=atof(word[8]);
656 imp.crmax=atof(word[9]);
658 for(i=0;i<strlen(word[8]);i++) {
661 imp.attr|=ATOM_ATTR_VB;
664 imp.attr|=ATOM_ATTR_HB;
667 imp.attr|=ATOM_ATTR_VA;
670 imp.attr|=ATOM_ATTR_FP;
673 imp.attr|=ATOM_ATTR_1BP;
676 imp.attr|=ATOM_ATTR_2BP;
679 imp.attr|=ATOM_ATTR_3BP;
686 imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
687 imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
688 add_stage(mdrun,STAGE_INSERT_MIXED_ATOMS,&imp);
695 else if(!strncmp(word[1],"continue",8)) {
696 cp.runs=atoi(word[2]);
697 add_stage(mdrun,STAGE_CONTINUE,&cp);
699 else if(!strncmp(word[1],"anneal",6)) {
701 ap.runs=atoi(word[2]);
703 ap.interval=atoi(word[4]);
704 add_stage(mdrun,STAGE_ANNEAL,&ap);
706 else if(!strncmp(word[1],"set_temp",8)) {
707 if(word[2][0]=='c') {
708 stp.type=SET_TEMP_CURRENT;
712 stp.type=SET_TEMP_VALUE;
713 stp.val=atof(word[2]);
715 add_stage(mdrun,STAGE_SET_TEMP,&stp);
717 else if(!strncmp(word[1],"set_timestep",12)) {
718 stsp.tau=atof(word[2]);
719 add_stage(mdrun,STAGE_SET_TIMESTEP,&stsp);
721 else if(!strncmp(word[1],"crt",3)) {
722 crtp.type=atoi(word[2]);
723 crtp.steps=atoi(word[3]);
724 strncpy(crtp.file,word[4],127);
725 add_stage(mdrun,STAGE_CRT,&crtp);
728 printf("%s unknown stage type: %s\n",
734 printf("%s unknown keyword '%s', skipped!\n",
746 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
750 if(!(mdrun->sattr&SATTR_PRELAX)) {
754 delta=moldyn->p_avg-moldyn->p_ref;
765 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
769 if(!(mdrun->sattr&SATTR_TRELAX))
772 delta=moldyn->t_avg-moldyn->t_ref;
783 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
785 t_displace_atom_params *dap;
789 stage=mdrun->stage.current->data;
792 atom=&(moldyn->atom[dap->nr]);
800 int del_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
803 t_del_atoms_params *delp;
807 stage=mdrun->stage.current->data;
810 for(i=0;i<moldyn->count;i++) {
811 v3_sub(&dist,&(delp->o),&(moldyn->atom[i].r));
812 //printf("%d ----> %f %f %f = %f | %f\n",i,dist.x,dist.y,dist.z,v3_absolute_square(&dist),delp->r*delp->r);
813 if(v3_absolute_square(&dist)<=(delp->r*delp->r)) {
814 del_atom(moldyn,moldyn->atom[i].tag);
815 printf("%s atom deleted: %d %d %d\n",ME,
816 moldyn->atom[i].tag,moldyn->atom[i].element,
817 moldyn->atom[i].brand);
824 int modify_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
826 t_modify_atoms_params *map;
833 stage=mdrun->stage.current->data;
835 v.x=0.0; v.y=0.0; v.z=0.0;
837 for(i=0;i<moldyn->count;i++) {
838 if(atom[i].tag==map->tag) {
839 v.x=sqrt(2.0*fabs(map->ekin.x)/atom[i].mass);
842 v.y=sqrt(2.0*fabs(map->ekin.y)/atom[i].mass);
845 v.z=sqrt(2.0*fabs(map->ekin.z)/atom[i].mass);
848 v3_copy(&(atom[i].v),&v);
849 printf("%s atom modified: v = (%f %f %f)\n",
857 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
859 t_insert_atoms_params *iap;
869 stage=mdrun->stage.current->data;
874 v.x=0.0; v.y=0.0; v.z=0.0;
878 switch(mdrun->lattice) {
929 printf("%s unknown insertion mode: %02x\n",
935 while(cnt<iap->ins_atoms) {
938 if((iap->type!=INS_POS)&&(iap->type!=INS_RELPOS)) {
939 r.x=rand_get_double(&(moldyn->random))*x;
940 r.y=rand_get_double(&(moldyn->random))*y;
941 r.z=rand_get_double(&(moldyn->random))*z;
948 if(iap->type==INS_RELPOS) {
959 if(iap->type!=INS_TOTAL) {
966 dmin=1000; // for sure too high!
967 for(i=0;i<moldyn->count;i++) {
968 atom=&(moldyn->atom[i]);
969 v3_sub(&dist,&(atom->r),&r);
970 check_per_bound(moldyn,&dist);
971 d=v3_absolute_square(&dist);
972 if(d<(iap->cr*iap->cr)) {
980 if(iap->type==INS_SPHERE) {
981 if((r.x-iap->x0)*(r.x-iap->x0)+
982 (r.y-iap->y0)*(r.y-iap->y0)+
983 (r.z-iap->z0)*(r.z-iap->z0)>
989 add_atom(moldyn,iap->element,
990 iap->brand,iap->attr,&r,&v);
991 printf("%s atom inserted (%d/%d): %f %f %f\n",
992 ME,(iap->cnt_steps+1)*iap->ins_atoms,
993 iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
994 printf(" attributes: ");
995 if(iap->attr&ATOM_ATTR_VB)
997 if(iap->attr&ATOM_ATTR_HB)
999 if(iap->attr&ATOM_ATTR_VA)
1001 if(iap->attr&ATOM_ATTR_FP)
1003 if(iap->attr&ATOM_ATTR_1BP)
1005 if(iap->attr&ATOM_ATTR_2BP)
1007 if(iap->attr&ATOM_ATTR_3BP)
1010 printf(" d2 = %f/%f\n",dmin,iap->cr*iap->cr);
1017 int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
1020 t_insert_mixed_atoms_params *imp;
1022 double x,x0,y,y0,z,z0;
1023 double dmin,d,cmin,cmax;
1029 stage=mdrun->stage.current->data;
1043 cmin=imp->crmin*imp->crmin;
1044 cmax=imp->crmax*imp->crmax;
1046 while(imp->amount1|imp->amount2) {
1051 r.x=rand_get_double(&(moldyn->random))*x;
1052 r.y=rand_get_double(&(moldyn->random))*y;
1053 r.z=rand_get_double(&(moldyn->random))*z;
1057 dmin=1000.0; // for sure too high!
1058 for(i=0;i<moldyn->count;i++) {
1059 atom=&(moldyn->atom[i]);
1060 v3_sub(&dist,&(atom->r),&r);
1061 check_per_bound(moldyn,&dist);
1062 d=v3_absolute_square(&dist);
1074 add_atom(moldyn,imp->element1,
1075 imp->brand1,imp->attr1,&r,&v);
1076 printf("%s (mixed) atom inserted (%d): %f %f %f\n",
1077 ME,imp->amount1,r.x,r.y,r.z);
1078 printf(" -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
1085 r.x=rand_get_double(&(moldyn->random))*x;
1086 r.y=rand_get_double(&(moldyn->random))*y;
1087 r.z=rand_get_double(&(moldyn->random))*z;
1091 dmin=1000.0; // for sure too high!
1092 for(i=0;i<moldyn->count;i++) {
1093 atom=&(moldyn->atom[i]);
1094 v3_sub(&dist,&(atom->r),&r);
1095 check_per_bound(moldyn,&dist);
1096 d=v3_absolute_square(&dist);
1108 add_atom(moldyn,imp->element2,
1109 imp->brand2,imp->attr2,&r,&v);
1110 printf("%s (mixed) atom inserted (%d): %f %f %f\n",
1111 ME,imp->amount2,r.x,r.y,r.z);
1112 printf(" -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
1120 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
1123 t_chaattr_params *cap;
1127 stage=mdrun->stage.current->data;
1130 for(i=0;i<moldyn->count;i++) {
1131 atom=&(moldyn->atom[i]);
1132 if(cap->type&CHAATTR_ELEMENT) {
1133 if(cap->element!=atom->element)
1136 if(cap->type&CHAATTR_NUMBER) {
1137 if(cap->element!=atom->tag)
1140 if(cap->type&CHAATTR_REGION) {
1141 if(cap->x0>atom->r.x)
1143 if(cap->y0>atom->r.y)
1145 if(cap->z0>atom->r.z)
1147 if(cap->x1<atom->r.x)
1149 if(cap->y1<atom->r.y)
1151 if(cap->z1<atom->r.z)
1154 if(!(cap->type&CHAATTR_TOTALV))
1155 printf(" changing attributes of atom %d (0x%x)\n",
1157 atom->attr=cap->attr;
1165 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
1168 t_chsattr_params *csp;
1170 stage=mdrun->stage.current->data;
1173 if(csp->type&CHSATTR_PCTRL) {
1175 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
1177 set_p_scale(moldyn,P_SCALE_NONE,1.0);
1179 if(csp->type&CHSATTR_TCTRL) {
1181 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
1183 set_t_scale(moldyn,T_SCALE_NONE,1.0);
1185 if(csp->type&CHSATTR_PRELAX) {
1187 mdrun->sattr&=(~(SATTR_PRELAX));
1189 mdrun->sattr|=SATTR_PRELAX;
1192 if(csp->type&CHSATTR_TRELAX) {
1194 mdrun->sattr&=(~(SATTR_TRELAX));
1196 mdrun->sattr|=SATTR_TRELAX;
1199 if(csp->type&CHSATTR_AVGRST) {
1201 mdrun->sattr|=SATTR_AVGRST;
1203 mdrun->sattr&=(~(SATTR_AVGRST));
1205 if(csp->type&CHSATTR_RSTEPS) {
1206 mdrun->relax_steps=csp->rsteps;
1212 int crt(t_moldyn *moldyn,t_mdrun *mdrun) {
1229 stage=mdrun->stage.current->data;
1236 if(crtp->count==0) {
1237 printf(" crt init\n");
1238 // read final positions, constraints and do the alloc
1239 fd=open(crtp->file,O_RDONLY);
1241 perror("[mdrun] FATAL reading constraints file");
1245 ret=get_line(fd,line,128);
1246 // check for end of file
1248 printf(" read %d atom positions\n",acount);
1249 if(acount!=moldyn->count)
1250 printf(" atom count mismatch!!!\n");
1254 // ignore # lines and \n
1255 if((line[0]=='#')|(ret==1))
1257 // allocate new memory
1258 ptr=realloc(crtp->r_fin,(acount+1)*sizeof(t_3dvec));
1260 perror("[mdrun] FATAL realloc crt positions");
1264 ptr=realloc(constraints,(acount+1)*3*sizeof(u8));
1266 perror("[mdrun] FATAL realloc crt constraints");
1271 wptr=strtok(line," \t");
1273 wptr=strtok(NULL," \t");
1274 crtp->r_fin[acount].x=atof(wptr);
1275 wptr=strtok(NULL," \t");
1276 crtp->r_fin[acount].y=atof(wptr);
1277 wptr=strtok(NULL," \t");
1278 crtp->r_fin[acount].z=atof(wptr);
1280 wptr=strtok(NULL," \t");
1281 constraints[3*acount]=atoi(wptr);
1282 wptr=strtok(NULL," \t");
1283 constraints[3*acount+1]=atoi(wptr);
1284 wptr=strtok(NULL," \t");
1285 constraints[3*acount+2]=atoi(wptr);
1289 // allocate trafo angles
1290 trafo_angle=malloc(acount*2*sizeof(double));
1291 if(trafo_angle==NULL) {
1292 perror("[mdrun] FATAL alloc trafo angles");
1299 /* crt routines: calculate displacement + set individual constraints */
1301 printf(" crt step %d of %d in total\n\n",crtp->count+1,crtp->steps);
1303 for(i=0;i<moldyn->count;i++) {
1304 // calc displacements
1306 v3_sub(&disp,&(crtp->r_fin[i]),&(atom[i].r));
1308 trafo_angle[2*i]=atan2(disp.x,disp.y);
1309 trafo_angle[2*i+1]=-atan2(disp.z,
1310 sqrt(disp.x*disp.x+disp.y*disp.y));
1312 frac=1.0/(crtp->steps-crtp->count);
1313 v3_scale(&disp,&disp,frac);
1314 v3_add(&(atom[i].r),&(atom[i].r),&disp);
1320 #define stage_print(m) if(!(stage->executed)) \
1323 int mdrun_hook(void *ptr1,void *ptr2) {
1333 t_insert_atoms_params *iap;
1334 t_insert_mixed_atoms_params *imp;
1335 t_continue_params *cp;
1336 t_anneal_params *ap;
1337 t_set_temp_params *stp;
1338 t_set_timestep_params *stsp;
1349 /* return immediately if there are no more stage */
1350 if(sl->current==NULL)
1353 /* get stage description */
1354 stage=sl->current->data;
1356 /* steps in next schedule */
1357 steps=mdrun->relax_steps;
1359 /* check whether relaxation steps are necessary */
1360 if((check_pressure(moldyn,mdrun)==TRUE)&\
1361 (check_temperature(moldyn,mdrun)==TRUE)) {
1364 stage_print("\n###########################\n");
1365 stage_print("# [mdrun] executing stage #\n");
1366 stage_print("###########################\n\n");
1368 /* stage specific stuff */
1369 switch(stage->type) {
1370 case STAGE_DISPLACE_ATOM:
1371 stage_print(" -> displace atom\n\n");
1372 displace_atom(moldyn,mdrun);
1375 case STAGE_DEL_ATOMS:
1376 stage_print(" -> del atoms\n\n");
1377 del_atoms(moldyn,mdrun);
1380 case STAGE_MODIFY_ATOMS:
1381 stage_print(" -> modify atoms\n\n");
1382 modify_atoms(moldyn,mdrun);
1385 case STAGE_INSERT_ATOMS:
1386 stage_print(" -> insert atoms\n\n");
1388 if(iap->cnt_steps==iap->ins_steps) {
1392 insert_atoms(moldyn,mdrun);
1398 case STAGE_INSERT_MIXED_ATOMS:
1399 stage_print(" -> insert mixed atoms\n\n");
1401 insert_mixed_atoms(moldyn,mdrun);
1407 case STAGE_CONTINUE:
1408 stage_print(" -> continue\n\n");
1409 if(stage->executed==TRUE) {
1417 stage_print(" -> anneal\n\n");
1419 if(ap->count==ap->runs) {
1423 if(moldyn->t_ref+ap->dt>=0.0)
1424 set_temperature(moldyn,
1425 moldyn->t_ref+ap->dt);
1430 stage_print(" -> change atom attributes\n\n");
1431 chaatr(moldyn,mdrun);
1435 stage_print(" -> change sys attributes\n\n");
1436 chsattr(moldyn,mdrun);
1439 case STAGE_SET_TEMP:
1440 stage_print(" -> set temperature\n\n");
1442 if(stp->type&SET_TEMP_CURRENT) {
1443 set_temperature(moldyn,moldyn->t_avg);
1446 set_temperature(moldyn,stp->val);
1450 case STAGE_SET_TIMESTEP:
1451 stage_print(" -> set timestep\n\n");
1453 mdrun->timestep=stsp->tau;
1457 stage_print(" -> fill lattice\n\n");
1459 switch(fp->lattice) {
1462 o.x=0.5*0.25*fp->lc;
1465 create_lattice(moldyn,
1468 DEFAULT_ATOM_ATTR,0,
1469 fp->lx,fp->ly,fp->lz,
1477 create_lattice(moldyn,
1480 DEFAULT_ATOM_ATTR,1,
1481 fp->lx,fp->ly,fp->lz,
1490 create_lattice(moldyn,
1495 fp->lx,fp->ly,fp->lz,
1502 moldyn_bc_check(moldyn);
1505 case STAGE_THERMAL_INIT:
1506 stage_print(" -> thermal init\n\n");
1507 thermal_init(moldyn,TRUE);
1511 stage_print(" -> constraint relaxation");
1512 stage_print(" technique\n\n");
1514 if(crtp->count==crtp->steps) {
1525 printf("%s unknwon stage type\n",ME);
1529 /* mark as executed */
1530 stage->executed=TRUE;
1533 if(change_stage==TRUE) {
1534 printf("%s finished stage\n",ME);
1535 if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
1536 printf("%s no more stages\n",ME);
1546 if(mdrun->sattr&SATTR_AVGRST)
1547 average_reset(moldyn);
1551 /* continue simulation */
1552 moldyn_add_schedule(moldyn,steps,mdrun->timestep);
1557 int main(int argc,char **argv) {
1564 memset(&mdrun,0,sizeof(t_mdrun));
1565 memset(&moldyn,0,sizeof(t_moldyn));
1567 /* init crt variables */
1572 /* parse arguments */
1573 if(mdrun_parse_argv(&mdrun,argc,argv)<0)
1576 /* initialize list system */
1577 list_init_f(&(mdrun.stage));
1579 /* parse config file */
1580 mdrun_parse_config(&mdrun);
1582 /* reset the stage list */
1583 list_reset_f(&(mdrun.stage));
1587 /* prepare simulation */
1589 if(mdrun.continue_file[0]) {
1590 // read the save file
1591 moldyn_read_save_file(&moldyn,mdrun.continue_file);
1592 // manualaadjusting some stuff
1595 rand_init(&(moldyn.random),NULL,1);
1596 moldyn.random.status|=RAND_STAT_VERBOSE;
1599 moldyn_init(&moldyn,argc,argv);
1602 if(set_int_alg(&moldyn,mdrun.intalgo)<0)
1606 set_cutoff(&moldyn,mdrun.cutoff);
1607 if(set_potential(&moldyn,mdrun.potential)<0)
1609 switch(mdrun.potential) {
1610 case MOLDYN_POTENTIAL_AM:
1611 albe_mult_set_params(&moldyn,
1615 case MOLDYN_POTENTIAL_TM:
1616 tersoff_mult_set_params(&moldyn,
1620 case MOLDYN_POTENTIAL_HO:
1621 harmonic_oscillator_set_params(&moldyn,mdrun.element1);
1623 case MOLDYN_POTENTIAL_LJ:
1624 lennard_jones_set_params(&moldyn,mdrun.element1);
1627 printf("%s unknown potential: %02x\n",
1628 ME,mdrun.potential);
1632 /* if it is a continue run, reset schedule and skip lattice init */
1633 if(mdrun.continue_file[0]) {
1634 memset(&(moldyn.schedule),0,sizeof(t_moldyn_schedule));
1638 /* initial lattice and dimensions */
1639 set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
1640 set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
1641 /* replaced by fill stage !!
1642 switch(mdrun.lattice) {
1644 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.fill_element,
1645 mdrun.m1,DEFAULT_ATOM_ATTR,
1646 mdrun.fill_brand,mdrun.lx,
1647 mdrun.ly,mdrun.lz,NULL,0,NULL);
1650 create_lattice(&moldyn,DIAMOND,mdrun.lc,
1652 mdrun.m1,DEFAULT_ATOM_ATTR,
1653 mdrun.fill_brand,mdrun.lx,
1654 mdrun.ly,mdrun.lz,NULL,0,NULL);
1657 o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1658 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1659 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1660 mdrun.ly,mdrun.lz,&o,0,NULL);
1661 o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1662 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
1663 mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
1664 mdrun.ly,mdrun.lz,&o,0,NULL);
1667 set_nn_dist(&moldyn,mdrun.nnd);
1670 printf("%s unknown lattice type: %02x\n",
1674 moldyn_bc_check(&moldyn);
1675 replaced by fill stage !! */
1677 /* temperature and pressure */
1678 set_temperature(&moldyn,mdrun.temperature);
1679 set_pressure(&moldyn,mdrun.pressure);
1680 /* replaced thermal_init stage
1681 thermal_init(&moldyn,TRUE);
1685 /* first schedule */
1686 moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
1689 moldyn_set_log_dir(&moldyn,mdrun.sdir);
1690 moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
1692 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
1694 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
1696 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
1698 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
1700 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
1702 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
1703 moldyn_set_log(&moldyn,CREATE_REPORT,0);
1704 set_avg_skip(&moldyn,mdrun.avgskip);
1706 /* prepare the hook function */
1707 moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
1709 /* run the simulation */
1710 moldyn_integrate(&moldyn);
1713 moldyn_shutdown(&moldyn);
1715 list_destroy_f(&(mdrun.stage));