security checkin: fixes to lattice stage, px py pz calc (now default!
[physik/posic.git] / mdrun.c
diff --git a/mdrun.c b/mdrun.c
index 31943ef..6effe1a 100644 (file)
--- a/mdrun.c
+++ b/mdrun.c
 #include "potentials/tersoff.h"
 #endif
 
+/* pse */
+#define PSE_MASS
+#include "pse.h"
+#undef PSE_MASS
+
 #define ME     "[mdrun]"
 
 /*
@@ -111,6 +116,18 @@ int add_stage(t_mdrun *mdrun,u8 type,void *params) {
                case STAGE_CHSATTR:
                        psize=sizeof(t_chsattr_params);
                        break;
+               case STAGE_SET_TEMP:
+                       psize=sizeof(t_set_temp_params);
+                       break;
+               case STAGE_SET_TIMESTEP:
+                       psize=sizeof(t_set_timestep_params);
+                       break;
+               case STAGE_FILL:
+                       psize=sizeof(t_fill_params);
+                       break;
+               case STAGE_THERMAL_INIT:
+                       psize=0;
+                       break;
                default:
                        printf("%s unknown stage type: %02x\n",ME,type);
                        return -1;
@@ -155,6 +172,9 @@ int mdrun_parse_config(t_mdrun *mdrun) {
        t_anneal_params ap;
        t_chaattr_params cap;
        t_chsattr_params csp;
+       t_set_temp_params stp;
+       t_set_timestep_params stsp;
+       t_fill_params fp;
 
        /* open config file */
        fd=open(mdrun->cfile,O_RDONLY);
@@ -184,6 +204,9 @@ int mdrun_parse_config(t_mdrun *mdrun) {
                memset(&ap,0,sizeof(t_anneal_params));
                memset(&cap,0,sizeof(t_chaattr_params));
                memset(&csp,0,sizeof(t_chsattr_params));
+               memset(&stp,0,sizeof(t_set_temp_params));
+               memset(&stsp,0,sizeof(t_set_timestep_params));
+               memset(&fp,0,sizeof(t_fill_params));
 
                // get command + args
                wcnt=0;
@@ -264,11 +287,60 @@ int mdrun_parse_config(t_mdrun *mdrun) {
                        mdrun->m2=pse_mass[mdrun->element2];
                }
                else if(!strncmp(word[0],"fill",6)) {
-                       // only lc mode by now
-                       mdrun->lx=atoi(word[2]);
-                       mdrun->ly=atoi(word[3]);
-                       mdrun->lz=atoi(word[4]);
-                       mdrun->lc=atof(word[5]);
+                       fp.lx=atoi(word[2]);
+                       fp.ly=atoi(word[3]);
+                       fp.lz=atoi(word[4]);
+                       fp.lc=atof(word[5]);
+                       mdrun->lc=fp.lc;
+                       if(!strncmp(word[1],"lc",2)) {
+                               if(wcnt==8) {
+                                       fp.fill_element=atoi(word[6]);
+                                       fp.fill_brand=atoi(word[7]);
+                               }
+                               else {
+                                       fp.fill_element=mdrun->element1;
+                                       fp.fill_brand=0;
+                               }
+                       }
+                       else {
+                               switch(word[6][0]) {
+                                       case 'i':
+                                               if(word[6][1]=='r')
+                                                       fp.p_type=PART_INSIDE_R;
+                                               else
+                                                       fp.p_type=PART_INSIDE_D;
+                                               break;
+                                       case 'o':
+                                               if(word[6][1]=='r')
+                                               fp.p_type=PART_OUTSIDE_R;
+                                               else
+                                               fp.p_type=PART_OUTSIDE_D;
+                                               break;
+                                       default:
+                                               break;
+                               }
+                       }
+                       if((fp.p_type==PART_INSIDE_R)||
+                           (fp.p_type==PART_OUTSIDE_R)) {
+                               fp.p_vals.r=atof(word[7]);      
+                               fp.p_vals.p.x=atof(word[8]);    
+                               fp.p_vals.p.y=atof(word[9]);    
+                               fp.p_vals.p.z=atof(word[10]);
+                       }
+                       if((fp.p_type==PART_INSIDE_D)||
+                           (fp.p_type==PART_OUTSIDE_D)) {
+                               fp.p_vals.p.x=atof(word[7]);    
+                               fp.p_vals.p.y=atof(word[8]);    
+                               fp.p_vals.p.z=atof(word[9]);
+                               fp.p_vals.d.x=atof(word[10]);   
+                               fp.p_vals.d.y=atof(word[11]);   
+                               fp.p_vals.d.z=atof(word[12]);
+                       }
+                       fp.lattice=mdrun->lattice;
+                       add_stage(mdrun,STAGE_FILL,&fp);
+               }
+               else if(!strncmp(word[0],"thermal_init",12)) {
+                       add_stage(mdrun,STAGE_THERMAL_INIT,NULL);
                }
                else if(!strncmp(word[0],"aattr",5)) {
                        // for aatrib line we need a special stage
@@ -459,7 +531,8 @@ int mdrun_parse_config(t_mdrun *mdrun) {
                                imp.amount2=atoi(word[5]);
                                imp.brand1=atoi(word[6]);
                                imp.brand2=atoi(word[7]);
-                               imp.cr=atof(word[8]);
+                               imp.crmin=atof(word[8]);
+                               imp.crmax=atof(word[9]);
                                /* do this later ...
                                for(i=0;i<strlen(word[8]);i++) {
                                        switch(word[8][i]) {
@@ -489,8 +562,8 @@ int mdrun_parse_config(t_mdrun *mdrun) {
                                        }
                                }
                                */
-                               imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
-                               imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP;
+                               imp.attr1=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
+                               imp.attr2=ATOM_ATTR_HB|ATOM_ATTR_VA|ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_FP;
                                add_stage(mdrun,STAGE_INSERT_MIXED_ATOMS,&imp);
                        }
 
@@ -506,8 +579,24 @@ int mdrun_parse_config(t_mdrun *mdrun) {
                                ap.count=0;
                                ap.runs=atoi(word[2]);
                                ap.dt=atof(word[3]);
+                               ap.interval=atoi(word[4]);
                                add_stage(mdrun,STAGE_ANNEAL,&ap);
                        }
+                       else if(!strncmp(word[1],"set_temp",8)) {
+                               if(word[2][0]=='c') {
+                                       stp.type=SET_TEMP_CURRENT;
+                                       stp.val=0.0;
+                               }
+                               else {
+                                       stp.type=SET_TEMP_VALUE;
+                                       stp.val=atof(word[2]);
+                               }
+                               add_stage(mdrun,STAGE_SET_TEMP,&stp);
+                       }
+                       else if(!strncmp(word[1],"set_timestep",12)) {
+                               stsp.tau=atof(word[2]);
+                               add_stage(mdrun,STAGE_SET_TIMESTEP,&stsp);
+                       }
                        else {
                                printf("%s unknown stage type: %s\n",
                                       ME,word[1]);
@@ -706,7 +795,7 @@ int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
        t_insert_mixed_atoms_params *imp;
        t_atom *atom;
        double x,x0,y,y0,z,z0;
-       double dmin,d,c;
+       double dmin,d,cmin,cmax;
        u8 retry;
        t_3dvec r,v,dist;
        int i;
@@ -726,7 +815,8 @@ int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
        v.y=0.0;
        v.z=0.0;
 
-       c=imp->cr*imp->cr;
+       cmin=imp->crmin*imp->crmin;
+       cmax=imp->crmax*imp->crmax;
 
        while(imp->amount1|imp->amount2) {
                if(imp->amount1) {
@@ -745,20 +835,22 @@ int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
                                        v3_sub(&dist,&(atom->r),&r);
                                        check_per_bound(moldyn,&dist);
                                        d=v3_absolute_square(&dist);
-                                       if(d<c) {
+                                       if(d<cmin) {
                                                retry=1;
                                                break;
                                        }
                                        if(d<dmin)
                                                dmin=d;
-                                       
                                }
+                               if(dmin!=1000.0)
+                                       if(dmin>cmax)
+                                               retry=1;
                        }
                        add_atom(moldyn,imp->element1,pse_mass[imp->element1],
                                  imp->brand1,imp->attr1,&r,&v);
                        printf("%s (mixed) atom inserted (%d): %f %f %f\n",
                                ME,imp->amount1,r.x,r.y,r.z);
-                       printf("  -> d2 = %f/%f\n",dmin,c);
+                       printf("  -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
                        imp->amount1-=1;
                }
                if(imp->amount2) {
@@ -777,20 +869,22 @@ int insert_mixed_atoms(t_moldyn *moldyn,t_mdrun *mdrun) {
                                        v3_sub(&dist,&(atom->r),&r);
                                        check_per_bound(moldyn,&dist);
                                        d=v3_absolute_square(&dist);
-                                       if(d<c) {
+                                       if(d<cmin) {
                                                retry=1;
                                                break;
                                        }
                                        if(d<dmin)
                                                dmin=d;
-                                       
                                }
+                               if(dmin!=1000.0)
+                                       if(dmin>cmax)
+                                               retry=1;
                        }
                        add_atom(moldyn,imp->element2,pse_mass[imp->element2],
                                  imp->brand2,imp->attr2,&r,&v);
                        printf("%s (mixed) atom inserted (%d): %f %f %f\n",
                                ME,imp->amount2,r.x,r.y,r.z);
-                       printf("  -> d2 = %f/%f\n",dmin,c);
+                       printf("  -> d2 = %f/%f/%f\n",dmin,cmin,cmax);
                        imp->amount2-=1;
                }
        }
@@ -891,13 +985,16 @@ int mdrun_hook(void *ptr1,void *ptr2) {
        t_stage *stage;
        t_list *sl;
        int steps;
-       double tau;
        u8 change_stage;
+       t_3dvec o;
 
        t_insert_atoms_params *iap;
        t_insert_mixed_atoms_params *imp;
        t_continue_params *cp;
        t_anneal_params *ap;
+       t_set_temp_params *stp;
+       t_set_timestep_params *stsp;
+       t_fill_params *fp;
 
        moldyn=ptr1;
        mdrun=ptr2;
@@ -913,9 +1010,8 @@ int mdrun_hook(void *ptr1,void *ptr2) {
        /* get stage description */
        stage=sl->current->data;
 
-       /* default steps and tau values */
+       /* steps in next schedule */
        steps=mdrun->relax_steps;
-       tau=mdrun->timestep;
 
        /* check whether relaxation steps are necessary */
        if((check_pressure(moldyn,mdrun)==TRUE)&\
@@ -950,6 +1046,7 @@ int mdrun_hook(void *ptr1,void *ptr2) {
                                stage_print("  -> insert mixed atoms\n\n");
                                imp=stage->params;
                                insert_mixed_atoms(moldyn,mdrun);
+                               change_stage=TRUE;
                                break;
 
 
@@ -974,17 +1071,80 @@ int mdrun_hook(void *ptr1,void *ptr2) {
                                        set_temperature(moldyn,
                                                        moldyn->t_ref+ap->dt);
                                ap->count+=1;
+                               steps=ap->interval;
                                break;
                        case STAGE_CHAATTR:
-                               stage_print("  -> chaattr\n\n");
+                               stage_print("  -> change atom attributes\n\n");
                                chaatr(moldyn,mdrun);
                                change_stage=TRUE;
                                break;
                        case STAGE_CHSATTR:
-                               stage_print("  -> chsattr\n\n");
+                               stage_print("  -> change sys attributes\n\n");
                                chsattr(moldyn,mdrun);
                                change_stage=TRUE;
                                break;
+                       case STAGE_SET_TEMP:
+                               stage_print("  -> set temperature\n\n");
+                               stp=stage->params;
+                               if(stp->type&SET_TEMP_CURRENT) {
+                                       set_temperature(moldyn,moldyn->t_avg);
+                               }
+                               else {
+                                       set_temperature(moldyn,stp->val);
+                               }
+                               change_stage=TRUE;
+                               break;
+                       case STAGE_SET_TIMESTEP:
+                               stage_print("  -> set timestep\n\n");
+                               stsp=stage->params;
+                               mdrun->timestep=stsp->tau;
+                               change_stage=TRUE;
+                               break;
+                       case STAGE_FILL:
+                               stage_print("  -> fill lattice\n\n");
+                               fp=stage->params;
+                               if(fp->lattice!=ZINCBLENDE) {
+                                       create_lattice(moldyn,
+                                                      fp->lattice,fp->lc,
+                                                      mdrun->element1,
+                                                      mdrun->m1,
+                                                      DEFAULT_ATOM_ATTR,0,
+                                                      fp->lx,fp->ly,fp->lz,
+                                                      NULL,fp->p_type,
+                                                      &(fp->p_vals));
+                               }
+                               else {
+                                       o.x=0.5*0.25*fp->lc;
+                                       o.y=o.x;
+                                       o.z=o.x;
+                                       create_lattice(moldyn,
+                                                      FCC,fp->lc,
+                                                      mdrun->element1,
+                                                      mdrun->m1,
+                                                      DEFAULT_ATOM_ATTR,0,
+                                                      fp->lx,fp->ly,fp->lz,
+                                                      &o,fp->p_type,
+                                                      &(fp->p_vals));
+                                       o.x+=0.25*fp->lc;
+                                       o.y=o.x;
+                                       o.z=o.x;
+                                       create_lattice(moldyn,
+                                                      FCC,fp->lc,
+                                                      mdrun->element2,
+                                                      mdrun->m2,
+                                                      DEFAULT_ATOM_ATTR,1,
+                                                      fp->lx,fp->ly,fp->lz,
+                                                      &o,fp->p_type,
+                                                      &(fp->p_vals));
+                               }
+                               moldyn_bc_check(moldyn);
+                               change_stage=TRUE;
+                               break;
+                       case STAGE_THERMAL_INIT:
+                               stage_print("  -> thermal init\n\n");
+                               thermal_init(moldyn,TRUE);
+                               change_stage=TRUE;
+                               break;
                        default:
                                printf("%s unknwon stage type\n",ME);
                                break;
@@ -1001,7 +1161,6 @@ int mdrun_hook(void *ptr1,void *ptr2) {
                                return 0;
                        }
                        steps=0;
-                       tau=mdrun->timestep;
                }
 
        }
@@ -1014,7 +1173,7 @@ int mdrun_hook(void *ptr1,void *ptr2) {
        }
 
        /* continue simulation */
-       moldyn_add_schedule(moldyn,steps,tau);
+       moldyn_add_schedule(moldyn,steps,mdrun->timestep);
 
        return 0;
 }
@@ -1023,7 +1182,7 @@ int main(int argc,char **argv) {
 
        t_mdrun mdrun;
        t_moldyn moldyn;
-       t_3dvec o;
+       //t_3dvec o;
 
        /* clear structs */
        memset(&mdrun,0,sizeof(t_mdrun));
@@ -1098,28 +1257,33 @@ int main(int argc,char **argv) {
        /* initial lattice and dimensions */
        set_dim(&moldyn,mdrun.dim.x,mdrun.dim.y,mdrun.dim.z,mdrun.vis);
        set_pbc(&moldyn,mdrun.pbcx,mdrun.pbcy,mdrun.pbcz);
+       /* replaced by fill stage !! 
        switch(mdrun.lattice) {
                case FCC:
-                       create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
-                                      mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
-                                      mdrun.ly,mdrun.lz,NULL);
+                       create_lattice(&moldyn,FCC,mdrun.lc,mdrun.fill_element,
+                                      mdrun.m1,DEFAULT_ATOM_ATTR,
+                                       mdrun.fill_brand,mdrun.lx,
+                                      mdrun.ly,mdrun.lz,NULL,0,NULL);
                        break;
                case DIAMOND:
-                       create_lattice(&moldyn,DIAMOND,mdrun.lc,mdrun.element1,
-                                      mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
-                                      mdrun.ly,mdrun.lz,NULL);
+                       create_lattice(&moldyn,DIAMOND,mdrun.lc,
+                                       mdrun.fill_element,
+                                      mdrun.m1,DEFAULT_ATOM_ATTR,
+                                       mdrun.fill_brand,mdrun.lx,
+                                      mdrun.ly,mdrun.lz,NULL,0,NULL);
                        break;
                case ZINCBLENDE:
                        o.x=0.5*0.25*mdrun.lc; o.y=o.x; o.z=o.x;
                        create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element1,
                                       mdrun.m1,DEFAULT_ATOM_ATTR,0,mdrun.lx,
-                                      mdrun.ly,mdrun.lz,&o);
+                                      mdrun.ly,mdrun.lz,&o,0,NULL);
                        o.x+=0.25*mdrun.lc; o.y=o.x; o.z=o.x;
                        create_lattice(&moldyn,FCC,mdrun.lc,mdrun.element2,
                                       mdrun.m2,DEFAULT_ATOM_ATTR,1,mdrun.lx,
-                                      mdrun.ly,mdrun.lz,&o);
+                                      mdrun.ly,mdrun.lz,&o,0,NULL);
                        break;
                case NONE:
+                       set_nn_dist(&moldyn,mdrun.nnd);
                        break;
                default:
                        printf("%s unknown lattice type: %02x\n",
@@ -1127,11 +1291,14 @@ int main(int argc,char **argv) {
                        return -1;
        }
        moldyn_bc_check(&moldyn);
+       replaced by fill stage !! */
 
        /* temperature and pressure */
        set_temperature(&moldyn,mdrun.temperature);
        set_pressure(&moldyn,mdrun.pressure);
+       /* replaced thermal_init stage
        thermal_init(&moldyn,TRUE);
+       */
 
 addsched:
        /* first schedule */