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 printf("%s unknown stage type: %02x\n",ME,type);
137 stage=malloc(sizeof(t_stage));
139 perror("[mdrun] malloc (add stage)");
144 stage->executed=FALSE;
146 stage->params=malloc(psize);
147 if(stage->params==NULL) {
148 perror("[mdrun] malloc (stage params)");
152 memcpy(stage->params,params,psize);
154 list_add_immediate_f(&(mdrun->stage),stage);
159 int mdrun_parse_config(t_mdrun *mdrun) {
169 t_displace_atom_params dap;
170 t_modify_atoms_params map;
171 t_insert_atoms_params iap;
172 t_insert_mixed_atoms_params imp;
173 t_continue_params cp;
175 t_chaattr_params cap;
176 t_chsattr_params csp;
177 t_set_temp_params stp;
178 t_set_timestep_params stsp;
180 t_del_atoms_params delp;
182 /* open config file */
183 fd=open(mdrun->cfile,O_RDONLY);
185 snprintf(error,128,"%s open cfile %s",ME,mdrun->cfile);
190 /* read, parse, set */
195 ret=get_line(fd,line,128);
199 // ignore # lines and \n
200 if((line[0]=='#')|(ret==1))
204 memset(&iap,0,sizeof(t_insert_atoms_params));
205 memset(&map,0,sizeof(t_modify_atoms_params));
206 memset(&imp,0,sizeof(t_insert_mixed_atoms_params));
207 memset(&cp,0,sizeof(t_continue_params));
208 memset(&ap,0,sizeof(t_anneal_params));
209 memset(&cap,0,sizeof(t_chaattr_params));
210 memset(&csp,0,sizeof(t_chsattr_params));
211 memset(&stp,0,sizeof(t_set_temp_params));
212 memset(&stsp,0,sizeof(t_set_timestep_params));
213 memset(&fp,0,sizeof(t_fill_params));
214 memset(&delp,0,sizeof(t_del_atoms_params));
216 // get command + args
220 wptr=strtok(NULL," \t");
222 wptr=strtok(line," \t");
225 strncpy(word[wcnt],wptr,64);
229 if(!strncmp(word[0],"potential",9)) {
230 if(!strncmp(word[1],"albe",4))
231 mdrun->potential=MOLDYN_POTENTIAL_AM;
232 if(!strncmp(word[1],"tersoff",7))
233 mdrun->potential=MOLDYN_POTENTIAL_TM;
234 if(!strncmp(word[1],"ho",2))
235 mdrun->potential=MOLDYN_POTENTIAL_HO;
236 if(!strncmp(word[1],"lj",2))
237 mdrun->potential=MOLDYN_POTENTIAL_LJ;
239 else if(!strncmp(word[0],"continue",8))
240 strncpy(mdrun->continue_file,word[1],128);
241 else if(!strncmp(word[0],"cutoff",6))
242 mdrun->cutoff=atof(word[1]);
243 else if(!strncmp(word[0],"nnd",3))
244 mdrun->nnd=atof(word[1]);
245 else if(!strncmp(word[0],"intalgo",7)) {
246 if(!strncmp(word[1],"verlet",5))
247 mdrun->intalgo=MOLDYN_INTEGRATE_VERLET;
249 else if(!strncmp(word[0],"timestep",8))
250 mdrun->timestep=atof(word[1]);
251 else if(!strncmp(word[0],"volume",6)) {
252 mdrun->dim.x=atof(word[1]);
253 mdrun->dim.y=atof(word[2]);
254 mdrun->dim.z=atof(word[3]);
255 if(strncmp(word[4],"0",1))
258 else if(!strncmp(word[0],"pbc",3)) {
259 if(strncmp(word[1],"0",1))
263 if(strncmp(word[2],"0",1))
267 if(strncmp(word[3],"0",1))
272 else if(!strncmp(word[0],"temperature",11))
273 mdrun->temperature=atof(word[1]);
274 else if(!strncmp(word[0],"pressure",8))
275 mdrun->pressure=atof(word[1]);
276 else if(!strncmp(word[0],"lattice",7)) {
277 if(!strncmp(word[1],"zincblende",10))
278 mdrun->lattice=ZINCBLENDE;
279 if(!strncmp(word[1],"fcc",3))
281 if(!strncmp(word[1],"diamond",7))
282 mdrun->lattice=DIAMOND;
283 if(!strncmp(word[1],"none",4))
286 mdrun->lc=atof(word[2]);
288 else if(!strncmp(word[0],"element1",8)) {
289 mdrun->element1=atoi(word[1]);
291 else if(!strncmp(word[0],"element2",8)) {
292 mdrun->element2=atoi(word[1]);
294 else if(!strncmp(word[0],"fill",6)) {
296 fp.fill_element=mdrun->element1;
298 fp.lattice=mdrun->lattice;
302 // parse fill command
305 if(!strncmp(word[i],"lc",2)) {
306 fp.lx=atoi(word[++i]);
307 fp.ly=atoi(word[++i]);
308 fp.lz=atoi(word[++i]);
309 fp.lc=atof(word[++i]);
313 if(!strncmp(word[i],"eb",2)) {
314 fp.fill_element=atoi(word[++i]);
315 fp.fill_brand=atoi(word[++i]);
318 if(word[i][0]=='p') {
323 fp.p_params.type=PART_INSIDE_R;
325 fp.p_params.type=PART_INSIDE_D;
329 fp.p_params.type=PART_OUTSIDE_R;
331 fp.p_params.type=PART_OUTSIDE_D;
336 if((fp.p_params.type==PART_INSIDE_R)||
337 (fp.p_params.type==PART_OUTSIDE_R)) {
338 fp.p_params.r=atof(word[++i]);
339 fp.p_params.p.x=atof(word[++i]);
340 fp.p_params.p.y=atof(word[++i]);
341 fp.p_params.p.z=atof(word[++i]);
343 if((fp.p_params.type==PART_INSIDE_D)||
344 (fp.p_params.type==PART_OUTSIDE_D)) {
345 fp.p_params.p.x=atof(word[++i]);
346 fp.p_params.p.y=atof(word[++i]);
347 fp.p_params.p.z=atof(word[++i]);
348 fp.p_params.d.x=atof(word[++i]);
349 fp.p_params.d.y=atof(word[++i]);
350 fp.p_params.d.z=atof(word[++i]);
354 if(word[i][0]=='d') {
355 switch(word[++i][0]) {
358 fp.d_params.type=DEFECT_TYPE_0D;
359 if(!strncmp(word[i+1],"dbx",3)) {
360 fp.d_params.stype=DEFECT_STYPE_DB_X;
362 if(!strncmp(word[i+1],"dby",3)) {
363 fp.d_params.stype=DEFECT_STYPE_DB_Y;
365 if(!strncmp(word[i+1],"dbz",3)) {
366 fp.d_params.stype=DEFECT_STYPE_DB_Z;
368 if(!strncmp(word[i+1],"dbr",3)) {
369 fp.d_params.stype=DEFECT_STYPE_DB_R;
372 fp.d_params.od=atof(word[++i]);
373 fp.d_params.dd=atof(word[++i]);
374 fp.d_params.element=atoi(word[++i]);
375 fp.d_params.brand=atoi(word[++i]);
377 fp.d_params.attr=ATOM_ATTR_HB|ATOM_ATTR_VA;
378 fp.d_params.attr|=ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
382 fp.d_params.type=DEFECT_TYPE_1D;
385 fp.d_params.type=DEFECT_TYPE_2D;
388 fp.d_params.type=DEFECT_TYPE_3D;
397 if(word[i][0]=='o') {
398 fp.o_params.o.x=atof(word[++i])*fp.lc;
399 fp.o_params.o.y=atof(word[++i])*fp.lc;
400 fp.o_params.o.z=atof(word[++i])*fp.lc;
406 add_stage(mdrun,STAGE_FILL,&fp);
408 else if(!strncmp(word[0],"thermal_init",12)) {
409 add_stage(mdrun,STAGE_THERMAL_INIT,NULL);
411 else if(!strncmp(word[0],"aattr",5)) {
412 // for aatrib line we need a special stage
413 // containing one schedule of 0 loops ...
414 for(i=0;i<strlen(word[1]);i++) {
417 cap.type|=CHAATTR_TOTALV;
420 cap.type|=CHAATTR_REGION;
423 cap.type|=CHAATTR_ELEMENT;
430 if(cap.type&CHAATTR_REGION) {
431 cap.x0=atof(word[2]);
432 cap.y0=atof(word[3]);
433 cap.z0=atof(word[4]);
434 cap.x1=atof(word[5]);
435 cap.y1=atof(word[6]);
436 cap.z1=atof(word[7]);
439 if(cap.type&CHAATTR_ELEMENT) {
440 cap.element=atoi(word[i]);
443 for(o=0;o<strlen(word[i]);o++) {
446 cap.attr|=ATOM_ATTR_VB;
449 cap.attr|=ATOM_ATTR_HB;
452 cap.attr|=ATOM_ATTR_VA;
455 cap.attr|=ATOM_ATTR_FP;
458 cap.attr|=ATOM_ATTR_1BP;
461 cap.attr|=ATOM_ATTR_2BP;
464 cap.attr|=ATOM_ATTR_3BP;
470 add_stage(mdrun,STAGE_CHAATTR,&cap);
472 else if(!strncmp(word[0],"sattr",5)) {
473 // for satrib line we need a special stage
474 // containing one schedule of 0 loops ...
476 for(i=1;i<wcnt;i++) {
477 if(!strncmp(word[i],"pctrl",5)) {
478 csp.ptau=atof(word[++i]);
480 csp.ptau=0.01/(csp.ptau*GPA);
481 csp.type|=CHSATTR_PCTRL;
483 if(!strncmp(word[i],"tctrl",5)) {
484 csp.ttau=atof(word[++i]);
485 csp.type|=CHSATTR_TCTRL;
487 if(!strncmp(word[i],"prelax",6)) {
488 csp.dp=atof(word[++i])*BAR;
489 csp.type|=CHSATTR_PRELAX;
491 if(!strncmp(word[i],"trelax",6)) {
492 csp.dt=atof(word[++i]);
493 csp.type|=CHSATTR_TRELAX;
495 if(!strncmp(word[i],"rsteps",6)) {
496 csp.rsteps=atoi(word[++i]);
497 csp.type|=CHSATTR_RSTEPS;
499 if(!strncmp(word[i],"avgrst",6)) {
500 csp.avgrst=atoi(word[++i]);
501 csp.type|=CHSATTR_AVGRST;
504 add_stage(mdrun,STAGE_CHSATTR,&csp);
506 else if(!strncmp(word[0],"prerun",6))
507 mdrun->prerun=atoi(word[1]);
508 else if(!strncmp(word[0],"avgskip",7))
509 mdrun->avgskip=atoi(word[1]);
510 else if(!strncmp(word[0],"elog",4))
511 mdrun->elog=atoi(word[1]);
512 else if(!strncmp(word[0],"tlog",4))
513 mdrun->tlog=atoi(word[1]);
514 else if(!strncmp(word[0],"plog",4))
515 mdrun->plog=atoi(word[1]);
516 else if(!strncmp(word[0],"vlog",4))
517 mdrun->vlog=atoi(word[1]);
518 else if(!strncmp(word[0],"save",4))
519 mdrun->save=atoi(word[1]);
520 else if(!strncmp(word[0],"visualize",9))
521 mdrun->visualize=atoi(word[1]);
522 else if(!strncmp(word[0],"stage",5)) {
523 // for every stage line, add a stage
524 if(!strncmp(word[1],"displace",8)) {
525 dap.nr=atoi(word[2]);
526 dap.dx=atof(word[3]);
527 dap.dy=atof(word[4]);
528 dap.dz=atof(word[5]);
529 add_stage(mdrun,STAGE_DISPLACE_ATOM,&dap);
531 else if(!strncmp(word[1],"del_atoms",9)) {
532 delp.o.x=atof(word[2]);
533 delp.o.y=atof(word[3]);
534 delp.o.z=atof(word[4]);
535 delp.r=atof(word[5]);
536 add_stage(mdrun,STAGE_DEL_ATOMS,&delp);
538 else if(!strncmp(word[1],"mod_atoms",8)) {
541 if(!strncmp(word[i],"t",1)) {
542 map.tag=atoi(word[++i]);
545 if(!strncmp(word[i],"ekin",5)) {
546 map.ekin.x=atof(word[++i])*EV;
547 map.ekin.y=atof(word[++i])*EV;
548 map.ekin.z=atof(word[++i])*EV;
552 add_stage(mdrun,STAGE_MODIFY_ATOMS,&map);
554 else if(!strncmp(word[1],"ins_atoms",9)) {
555 iap.ins_steps=atoi(word[2]);
556 iap.ins_atoms=atoi(word[3]);
557 iap.element=atoi(word[4]);
558 iap.brand=atoi(word[5]);
559 for(i=0;i<strlen(word[6]);i++) {
562 iap.attr|=ATOM_ATTR_VB;
565 iap.attr|=ATOM_ATTR_HB;
568 iap.attr|=ATOM_ATTR_VA;
571 iap.attr|=ATOM_ATTR_FP;
574 iap.attr|=ATOM_ATTR_1BP;
577 iap.attr|=ATOM_ATTR_2BP;
580 iap.attr|=ATOM_ATTR_3BP;
590 iap.x0=atof(word[8]);
591 iap.y0=atof(word[9]);
592 iap.z0=atof(word[10]);
599 iap.cr=atof(word[9]);
603 iap.x0=atof(word[9]);
604 iap.y0=atof(word[10]);
605 iap.z0=atof(word[11]);
606 iap.x1=atof(word[12]);
607 iap.y1=atof(word[13]);
608 iap.z1=atof(word[14]);
609 iap.cr=atof(word[15]);
613 iap.x0=atof(word[9]);
614 iap.y0=atof(word[10]);
615 iap.z0=atof(word[11]);
616 iap.x1=atof(word[12]);
617 iap.cr=atof(word[13]);
625 add_stage(mdrun,STAGE_INSERT_ATOMS,&iap);
631 else if(!strncmp(word[1],"ins_m_atoms",11)) {
632 imp.element1=atoi(word[2]);
633 imp.element2=atoi(word[3]);
634 imp.amount1=atoi(word[4]);
635 imp.amount2=atoi(word[5]);
636 imp.brand1=atoi(word[6]);
637 imp.brand2=atoi(word[7]);
638 imp.crmin=atof(word[8]);
639 imp.crmax=atof(word[9]);
641 for(i=0;i<strlen(word[8]);i++) {
644 imp.attr|=ATOM_ATTR_VB;
647 imp.attr|=ATOM_ATTR_HB;
650 imp.attr|=ATOM_ATTR_VA;
653 imp.attr|=ATOM_ATTR_FP;
656 imp.attr|=ATOM_ATTR_1BP;
659 imp.attr|=ATOM_ATTR_2BP;
662 imp.attr|=ATOM_ATTR_3BP;
669 imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
670 imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
671 add_stage(mdrun,STAGE_INSERT_MIXED_ATOMS,&imp);
678 else if(!strncmp(word[1],"continue",8)) {
679 cp.runs=atoi(word[2]);
680 add_stage(mdrun,STAGE_CONTINUE,&cp);
682 else if(!strncmp(word[1],"anneal",6)) {
684 ap.runs=atoi(word[2]);
686 ap.interval=atoi(word[4]);
687 add_stage(mdrun,STAGE_ANNEAL,&ap);
689 else if(!strncmp(word[1],"set_temp",8)) {
690 if(word[2][0]=='c') {
691 stp.type=SET_TEMP_CURRENT;
695 stp.type=SET_TEMP_VALUE;
696 stp.val=atof(word[2]);
698 add_stage(mdrun,STAGE_SET_TEMP,&stp);
700 else if(!strncmp(word[1],"set_timestep",12)) {
701 stsp.tau=atof(word[2]);
702 add_stage(mdrun,STAGE_SET_TIMESTEP,&stsp);
705 printf("%s unknown stage type: %s\n",
711 printf("%s unknown keyword '%s', skipped!\n",
723 int check_pressure(t_moldyn *moldyn,t_mdrun *mdrun) {
727 if(!(mdrun->sattr&SATTR_PRELAX)) {
731 delta=moldyn->p_avg-moldyn->p_ref;
742 int check_temperature(t_moldyn *moldyn,t_mdrun *mdrun) {
746 if(!(mdrun->sattr&SATTR_TRELAX))
749 delta=moldyn->t_avg-moldyn->t_ref;
760 int displace_atom(t_moldyn *moldyn,t_mdrun *mdrun) {
762 t_displace_atom_params *dap;
766 stage=mdrun->stage.current->data;
769 atom=&(moldyn->atom[dap->nr]);
777 int del_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
780 t_del_atoms_params *delp;
784 stage=mdrun->stage.current->data;
787 for(i=0;i<moldyn->count;i++) {
788 v3_sub(&dist,&(delp->o),&(moldyn->atom[i].r));
789 //printf("%d ----> %f %f %f = %f | %f\n",i,dist.x,dist.y,dist.z,v3_absolute_square(&dist),delp->r*delp->r);
790 if(v3_absolute_square(&dist)<=(delp->r*delp->r)) {
791 del_atom(moldyn,moldyn->atom[i].tag);
792 printf("%s atom deleted: %d %d %d\n",ME,
793 moldyn->atom[i].tag,moldyn->atom[i].element,
794 moldyn->atom[i].brand);
801 int modify_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
803 t_modify_atoms_params *map;
810 stage=mdrun->stage.current->data;
812 v.x=0.0; v.y=0.0; v.z=0.0;
814 for(i=0;i<moldyn->count;i++) {
815 if(atom[i].tag==map->tag) {
816 v.x=sqrt(2.0*fabs(map->ekin.x)/atom[i].mass);
819 v.y=sqrt(2.0*fabs(map->ekin.y)/atom[i].mass);
822 v.z=sqrt(2.0*fabs(map->ekin.z)/atom[i].mass);
825 v3_copy(&(atom[i].v),&v);
826 printf("%s atom modified: v = (%f %f %f)\n",
834 int insert_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
836 t_insert_atoms_params *iap;
846 stage=mdrun->stage.current->data;
851 v.x=0.0; v.y=0.0; v.z=0.0;
855 switch(mdrun->lattice) {
905 printf("%s unknown insertion mode: %02x\n",
911 while(cnt<iap->ins_atoms) {
914 if(iap->type!=INS_POS) {
915 r.x=rand_get_double(&(moldyn->random))*x;
916 r.y=rand_get_double(&(moldyn->random))*y;
917 r.z=rand_get_double(&(moldyn->random))*z;
928 if(iap->type!=INS_TOTAL) {
935 dmin=1000; // for sure too high!
936 for(i=0;i<moldyn->count;i++) {
937 atom=&(moldyn->atom[i]);
938 v3_sub(&dist,&(atom->r),&r);
939 check_per_bound(moldyn,&dist);
940 d=v3_absolute_square(&dist);
941 if(d<(iap->cr*iap->cr)) {
949 if(iap->type==INS_SPHERE) {
950 if((r.x-iap->x0)*(r.x-iap->x0)+
951 (r.y-iap->y0)*(r.y-iap->y0)+
952 (r.z-iap->z0)*(r.z-iap->z0)>
958 add_atom(moldyn,iap->element,
959 iap->brand,iap->attr,&r,&v);
960 printf("%s atom inserted (%d/%d): %f %f %f\n",
961 ME,(iap->cnt_steps+1)*iap->ins_atoms,
962 iap->ins_steps*iap->ins_atoms,r.x,r.y,r.z);
963 printf(" attributes: ");
964 if(iap->attr&ATOM_ATTR_VB)
966 if(iap->attr&ATOM_ATTR_HB)
968 if(iap->attr&ATOM_ATTR_VA)
970 if(iap->attr&ATOM_ATTR_FP)
972 if(iap->attr&ATOM_ATTR_1BP)
974 if(iap->attr&ATOM_ATTR_2BP)
976 if(iap->attr&ATOM_ATTR_3BP)
979 printf(" d2 = %f/%f\n",dmin,iap->cr*iap->cr);
986 int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
989 t_insert_mixed_atoms_params *imp;
991 double x,x0,y,y0,z,z0;
992 double dmin,d,cmin,cmax;
998 stage=mdrun->stage.current->data;
1012 cmin=imp->crmin*imp->crmin;
1013 cmax=imp->crmax*imp->crmax;
1015 while(imp->amount1|imp->amount2) {
1020 r.x=rand_get_double(&(moldyn->random))*x;
1021 r.y=rand_get_double(&(moldyn->random))*y;
1022 r.z=rand_get_double(&(moldyn->random))*z;
1026 dmin=1000.0; // for sure too high!
1027 for(i=0;i<moldyn->count;i++) {
1028 atom=&(moldyn->atom[i]);
1029 v3_sub(&dist,&(atom->r),&r);
1030 check_per_bound(moldyn,&dist);
1031 d=v3_absolute_square(&dist);
1043 add_atom(moldyn,imp->element1,
1044 imp->brand1,imp->attr1,&r,&v);
1045 printf("%s (mixed) atom inserted (%d): %f %f %f\n",
1046 ME,imp->amount1,r.x,r.y,r.z);
1047 printf(" -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
1054 r.x=rand_get_double(&(moldyn->random))*x;
1055 r.y=rand_get_double(&(moldyn->random))*y;
1056 r.z=rand_get_double(&(moldyn->random))*z;
1060 dmin=1000.0; // for sure too high!
1061 for(i=0;i<moldyn->count;i++) {
1062 atom=&(moldyn->atom[i]);
1063 v3_sub(&dist,&(atom->r),&r);
1064 check_per_bound(moldyn,&dist);
1065 d=v3_absolute_square(&dist);
1077 add_atom(moldyn,imp->element2,
1078 imp->brand2,imp->attr2,&r,&v);
1079 printf("%s (mixed) atom inserted (%d): %f %f %f\n",
1080 ME,imp->amount2,r.x,r.y,r.z);
1081 printf(" -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
1089 int chaatr(t_moldyn *moldyn,t_mdrun *mdrun) {
1092 t_chaattr_params *cap;
1096 stage=mdrun->stage.current->data;
1099 for(i=0;i<moldyn->count;i++) {
1100 atom=&(moldyn->atom[i]);
1101 if(cap->type&CHAATTR_ELEMENT) {
1102 if(cap->element!=atom->element)
1105 if(cap->type&CHAATTR_REGION) {
1106 if(cap->x0>atom->r.x)
1108 if(cap->y0>atom->r.y)
1110 if(cap->z0>atom->r.z)
1112 if(cap->x1<atom->r.x)
1114 if(cap->y1<atom->r.y)
1116 if(cap->z1<atom->r.z)
1119 if(!(cap->type&CHAATTR_TOTALV))
1120 printf(" changing attributes of atom %d (0x%x)\n",
1122 atom->attr=cap->attr;
1130 int chsattr(t_moldyn *moldyn,t_mdrun *mdrun) {
1133 t_chsattr_params *csp;
1135 stage=mdrun->stage.current->data;
1138 if(csp->type&CHSATTR_PCTRL) {
1140 set_p_scale(moldyn,P_SCALE_BERENDSEN,csp->ptau);
1142 set_p_scale(moldyn,P_SCALE_NONE,1.0);
1144 if(csp->type&CHSATTR_TCTRL) {
1146 set_t_scale(moldyn,T_SCALE_BERENDSEN,csp->ttau);
1148 set_t_scale(moldyn,T_SCALE_NONE,1.0);
1150 if(csp->type&CHSATTR_PRELAX) {
1152 mdrun->sattr&=(~(SATTR_PRELAX));
1154 mdrun->sattr|=SATTR_PRELAX;
1157 if(csp->type&CHSATTR_TRELAX) {
1159 mdrun->sattr&=(~(SATTR_TRELAX));
1161 mdrun->sattr|=SATTR_TRELAX;
1164 if(csp->type&CHSATTR_AVGRST) {
1166 mdrun->sattr|=SATTR_AVGRST;
1168 mdrun->sattr&=(~(SATTR_AVGRST));
1170 if(csp->type&CHSATTR_RSTEPS) {
1171 mdrun->relax_steps=csp->rsteps;
1177 #define stage_print(m) if(!(stage->executed)) \
1180 int mdrun_hook(void *ptr1,void *ptr2) {
1190 t_insert_atoms_params *iap;
1191 t_insert_mixed_atoms_params *imp;
1192 t_continue_params *cp;
1193 t_anneal_params *ap;
1194 t_set_temp_params *stp;
1195 t_set_timestep_params *stsp;
1205 /* return immediately if there are no more stage */
1206 if(sl->current==NULL)
1209 /* get stage description */
1210 stage=sl->current->data;
1212 /* steps in next schedule */
1213 steps=mdrun->relax_steps;
1215 /* check whether relaxation steps are necessary */
1216 if((check_pressure(moldyn,mdrun)==TRUE)&\
1217 (check_temperature(moldyn,mdrun)==TRUE)) {
1220 stage_print("\n###########################\n");
1221 stage_print("# [mdrun] executing stage #\n");
1222 stage_print("###########################\n\n");
1224 /* stage specific stuff */
1225 switch(stage->type) {
1226 case STAGE_DISPLACE_ATOM:
1227 stage_print(" -> displace atom\n\n");
1228 displace_atom(moldyn,mdrun);
1231 case STAGE_DEL_ATOMS:
1232 stage_print(" -> del atoms\n\n");
1233 del_atoms(moldyn,mdrun);
1236 case STAGE_MODIFY_ATOMS:
1237 stage_print(" -> modify atoms\n\n");
1238 modify_atoms(moldyn,mdrun);
1241 case STAGE_INSERT_ATOMS:
1242 stage_print(" -> insert atoms\n\n");
1244 if(iap->cnt_steps==iap->ins_steps) {
1248 insert_atoms(moldyn,mdrun);
1254 case STAGE_INSERT_MIXED_ATOMS:
1255 stage_print(" -> insert mixed atoms\n\n");
1257 insert_mixed_atoms(moldyn,mdrun);
1263 case STAGE_CONTINUE:
1264 stage_print(" -> continue\n\n");
1265 if(stage->executed==TRUE) {
1273 stage_print(" -> anneal\n\n");
1275 if(ap->count==ap->runs) {
1279 if(moldyn->t_ref+ap->dt>=0.0)
1280 set_temperature(moldyn,
1281 moldyn->t_ref+ap->dt);
1286 stage_print(" -> change atom attributes\n\n");
1287 chaatr(moldyn,mdrun);
1291 stage_print(" -> change sys attributes\n\n");
1292 chsattr(moldyn,mdrun);
1295 case STAGE_SET_TEMP:
1296 stage_print(" -> set temperature\n\n");
1298 if(stp->type&SET_TEMP_CURRENT) {
1299 set_temperature(moldyn,moldyn->t_avg);
1302 set_temperature(moldyn,stp->val);
1306 case STAGE_SET_TIMESTEP:
1307 stage_print(" -> set timestep\n\n");
1309 mdrun->timestep=stsp->tau;
1313 stage_print(" -> fill lattice\n\n");
1315 switch(fp->lattice) {
1318 o.x=0.5*0.25*fp->lc;
1321 create_lattice(moldyn,
1324 DEFAULT_ATOM_ATTR,0,
1325 fp->lx,fp->ly,fp->lz,
1333 create_lattice(moldyn,
1336 DEFAULT_ATOM_ATTR,1,
1337 fp->lx,fp->ly,fp->lz,
1346 create_lattice(moldyn,
1351 fp->lx,fp->ly,fp->lz,
1358 moldyn_bc_check(moldyn);
1361 case STAGE_THERMAL_INIT:
1362 stage_print(" -> thermal init\n\n");
1363 thermal_init(moldyn,TRUE);
1367 printf("%s unknwon stage type\n",ME);
1371 /* mark as executed */
1372 stage->executed=TRUE;
1375 if(change_stage==TRUE) {
1376 printf("%s finished stage\n",ME);
1377 if(list_next_f(sl)==L_NO_NEXT_ELEMENT) {
1378 printf("%s no more stages\n",ME);
1388 if(mdrun->sattr&SATTR_AVGRST)
1389 average_reset(moldyn);
1393 /* continue simulation */
1394 moldyn_add_schedule(moldyn,steps,mdrun->timestep);
1399 int main(int argc,char **argv) {
1406 memset(&mdrun,0,sizeof(t_mdrun));
1407 memset(&moldyn,0,sizeof(t_moldyn));
1409 /* parse arguments */
1410 if(mdrun_parse_argv(&mdrun,argc,argv)<0)
1413 /* initialize list system */
1414 list_init_f(&(mdrun.stage));
1416 /* parse config file */
1417 mdrun_parse_config(&mdrun);
1419 /* reset the stage list */
1420 list_reset_f(&(mdrun.stage));
1424 /* prepare simulation */
1426 if(mdrun.continue_file[0]) {
1427 // read the save file
1428 moldyn_read_save_file(&moldyn,mdrun.continue_file);
1429 // manualaadjusting some stuff
1432 rand_init(&(moldyn.random),NULL,1);
1433 moldyn.random.status|=RAND_STAT_VERBOSE;
1436 moldyn_init(&moldyn,argc,argv);
1439 if(set_int_alg(&moldyn,mdrun.intalgo)<0)
1443 set_cutoff(&moldyn,mdrun.cutoff);
1444 if(set_potential(&moldyn,mdrun.potential)<0)
1446 switch(mdrun.potential) {
1447 case MOLDYN_POTENTIAL_AM:
1448 albe_mult_set_params(&moldyn,
1452 case MOLDYN_POTENTIAL_TM:
1453 tersoff_mult_set_params(&moldyn,
1457 case MOLDYN_POTENTIAL_HO:
1458 harmonic_oscillator_set_params(&moldyn,mdrun.element1);
1460 case MOLDYN_POTENTIAL_LJ:
1461 lennard_jones_set_params(&moldyn,mdrun.element1);
1464 printf("%s unknown potential: %02x\n",
1465 ME,mdrun.potential);
1469 /* if it is a continue run, reset schedule and skip lattice init */
1470 if(mdrun.continue_file[0]) {
1471 memset(&(moldyn.schedule),0,sizeof(t_moldyn_schedule));
1475 /* initial lattice and dimensions */
1476 set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
1477 set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
1478 /* replaced by fill stage !!
1479 switch(mdrun.lattice) {
1481 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.fill_element,
1482 mdrun.m1,DEFAULT_ATOM_ATTR,
1483 mdrun.fill_brand,mdrun.lx,
1484 mdrun.ly,mdrun.lz,NULL,0,NULL);
1487 create_lattice(&moldyn,DIAMOND,mdrun.lc,
1489 mdrun.m1,DEFAULT_ATOM_ATTR,
1490 mdrun.fill_brand,mdrun.lx,
1491 mdrun.ly,mdrun.lz,NULL,0,NULL);
1494 o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1495 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
1496 mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
1497 mdrun.ly,mdrun.lz,&o,0,NULL);
1498 o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
1499 create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
1500 mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
1501 mdrun.ly,mdrun.lz,&o,0,NULL);
1504 set_nn_dist(&moldyn,mdrun.nnd);
1507 printf("%s unknown lattice type: %02x\n",
1511 moldyn_bc_check(&moldyn);
1512 replaced by fill stage !! */
1514 /* temperature and pressure */
1515 set_temperature(&moldyn,mdrun.temperature);
1516 set_pressure(&moldyn,mdrun.pressure);
1517 /* replaced thermal_init stage
1518 thermal_init(&moldyn,TRUE);
1522 /* first schedule */
1523 moldyn_add_schedule(&moldyn,mdrun.prerun,mdrun.timestep);
1526 moldyn_set_log_dir(&moldyn,mdrun.sdir);
1527 moldyn_set_report(&moldyn,"CHANGE ME","CHANGE ME TOO");
1529 moldyn_set_log(&moldyn,LOG_TOTAL_ENERGY,mdrun.elog);
1531 moldyn_set_log(&moldyn,LOG_TEMPERATURE,mdrun.tlog);
1533 moldyn_set_log(&moldyn,LOG_PRESSURE,mdrun.plog);
1535 moldyn_set_log(&moldyn,LOG_VOLUME,mdrun.vlog);
1537 moldyn_set_log(&moldyn,VISUAL_STEP,mdrun.visualize);
1539 moldyn_set_log(&moldyn,SAVE_STEP,mdrun.save);
1540 moldyn_set_log(&moldyn,CREATE_REPORT,0);
1541 set_avg_skip(&moldyn,mdrun.avgskip);
1543 /* prepare the hook function */
1544 moldyn_set_schedule_hook(&moldyn,&mdrun_hook,&mdrun);
1546 /* run the simulation */
1547 moldyn_integrate(&moldyn);
1550 moldyn_shutdown(&moldyn);
1552 list_destroy_f(&(mdrun.stage));