float-store bugfix (unstatisfying!) + slightly new design of sic.c
[physik/posic.git] / sic.c
1 /*
2  * sic.c - investigation of the sic precipitation process of silicon carbide
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #include <math.h>
9  
10 #include "moldyn.h"
11
12 /* potential */
13 #include "potentials/harmonic_oscillator.h"
14 #include "potentials/lennard_jones.h"
15 #include "potentials/albe.h"
16 #ifdef TERSOFF_ORIG
17 #include "potentials/tersoff_orig.h"
18 #else
19 #include "potentials/tersoff.h"
20 #endif
21
22 typedef struct s_hp {
23         int prerun_count;       /* prerun count */
24         int insert_count;       /* insert count */
25         int postrun_count;      /* post run count */
26         unsigned char state;    /* current state */
27         int argc;               /* arg count */
28         char **argv;            /* args */
29 } t_hp;
30
31 #define STATE_PRERUN    0x00
32 #define STATE_INSERT    0x01
33 #define STATE_POSTRUN   0x02
34
35 /* include the config file */
36 #include "config.h"
37
38 int insert_atoms(t_moldyn *moldyn) {
39
40         int i,j;
41         u8 run;
42         t_3dvec r,v,dist;
43         double d;
44
45         t_atom *atom;
46
47         atom=moldyn->atom;
48
49         v.x=0; v.y=0; v.z=0;
50
51         for(j=0;j<INS_ATOMS;j++) {
52                 run=1;
53                 while(run) {
54                         // tetrahedral
55                         /*
56                         r.x=0.0;
57                         r.y=0.0;
58                         r.z=0.0;
59                         */
60                         // hexagonal
61                         /*
62                         r.x=-1.0/8.0*ALBE_LC_SI;
63                         r.y=-1.0/8.0*ALBE_LC_SI;
64                         r.z=1.0/8.0*ALBE_LC_SI;
65                         */
66                         // 110 dumbbell
67                         /*
68                         r.x=(-0.5+0.25+0.125)*ALBE_LC_SI;
69                         r.y=(-0.5+0.25+0.125)*ALBE_LC_SI;
70                         r.z=(-0.5+0.25)*ALBE_LC_SI;
71                         md->atom[4372].r.x=(-0.5+0.125+0.125)*ALBE_LC_SI;
72                         md->atom[4372].r.y=(-0.5+0.125+0.125)*ALBE_LC_SI;
73                         */
74                         // random
75                         //
76                         r.x=(rand_get_double(&(moldyn->random))-0.5)*INS_LENX;
77                         r.y=(rand_get_double(&(moldyn->random))-0.5)*INS_LENY;
78                         r.z=(rand_get_double(&(moldyn->random))-0.5)*INS_LENZ;
79                         //
80                         // offset
81                         r.x+=INS_OFFSET;
82                         r.y+=INS_OFFSET;
83                         r.z+=INS_OFFSET;
84                         /* assume valid coordinates */
85                         run=0;
86                         for(i=0;i<moldyn->count;i++) {
87                                 atom=&(moldyn->atom[i]);
88                                 v3_sub(&dist,&(atom->r),&r);
89                                 check_per_bound(moldyn,&dist);
90                                 d=v3_absolute_square(&dist);
91                                 /* reject coordinates */
92                                 if(d<INS_R_C) {
93                                         //printf("atom %d - %f\n",i,d);
94                                         run=1;
95                                         break;
96                                 }
97                         }
98                 }
99                 add_atom(moldyn,INS_TYPE,INS_MASS,INS_BRAND,
100                          ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|\
101                          //ATOM_ATTR_HB|ATOM_ATTR_VB,
102                          ATOM_ATTR_HB,
103                          &r,&v);
104         }
105
106         return 0;
107 }
108
109 int sic_hook(void *moldyn,void *hook_params) {
110
111         t_hp *hp;
112         t_moldyn *md;
113         int steps;
114         double tau;
115
116         hp=hook_params;
117         md=moldyn;
118
119         /* switch on t scaling */
120         if(md->schedule.count==0)
121                 set_pt_scale(md,0,0,T_SCALE_BERENDSEN,100.0);
122
123         /* my lousy state machine ! */
124
125         /* switch to insert state immediately */
126         if(hp->state==STATE_PRERUN)
127                 hp->state=STATE_INSERT;
128
129         /* act according to state */
130         switch(hp->state) {
131                 case STATE_INSERT:
132                         /* check temperature */
133                         if(md->t_avg-md->t_ref>INS_DELTA_TC) {
134                                 steps=INS_RELAX;
135                                 tau=INS_TAU;
136                                 break;
137                         }
138                         /* insert atoms */
139                         hp->insert_count+=1;
140                         printf("   ### insert atoms (%d/%d) ###\n",
141                                hp->insert_count*INS_ATOMS,INS_RUNS*INS_ATOMS);
142                         insert_atoms(md);
143                         /* change state after last insertion */
144                         if(hp->insert_count==INS_RUNS)
145                                 hp->state=STATE_POSTRUN;
146                         break;
147                 case STATE_POSTRUN:
148                         /* settings */
149                         if(md->t-md->t_ref>POST_DELTA_TC) {
150                                 steps=POST_RELAX;
151                                 tau=POST_TAU;
152                         }
153                         /* decrease temperature */
154                         hp->postrun_count+=1;
155                         printf(" ### postrun (%d/%d) ###\n",
156                                hp->postrun_count,POST_RUNS);
157                         set_temperature(md,md->t_ref-POST_DT);
158                         if(hp->postrun_count==POST_RUNS)
159                                 return 0;
160                         break;
161                 default:
162                         printf("[hook] FATAL (default case!?!)\n");
163                         break;
164         }
165
166         /* add schedule */
167         moldyn_add_schedule(md,steps,tau);
168
169         return 0;
170 }
171
172 int main(int argc,char **argv) {
173
174         /* main moldyn structure */
175         t_moldyn md;
176
177         /* hook parameter structure */
178         t_hp hookparam;
179
180         /* potential parameters */
181         t_tersoff_mult_params tp;
182         t_albe_mult_params ap;
183
184         /* testing location & velocity vector */
185         t_3dvec r,v;
186         memset(&r,0,sizeof(t_3dvec));
187         memset(&v,0,sizeof(t_3dvec));
188
189         /* initialize moldyn */
190         moldyn_init(&md,argc,argv);
191
192         /* choose integration algorithm */
193         set_int_alg(&md,MOLDYN_INTEGRATE_VERLET);
194
195         /* choose potential */
196 #ifdef ALBE
197         set_potential3b_j1(&md,albe_mult_3bp_j1);
198         set_potential3b_k1(&md,albe_mult_3bp_k1);
199         set_potential3b_j2(&md,albe_mult_3bp_j2);
200         set_potential3b_k2(&md,albe_mult_3bp_k2);
201 #else
202         set_potential1b(&md,tersoff_mult_1bp);
203         set_potential3b_j1(&md,tersoff_mult_3bp_j1);
204         set_potential3b_k1(&md,tersoff_mult_3bp_k1);
205         set_potential3b_j2(&md,tersoff_mult_3bp_j2);
206         set_potential3b_k2(&md,tersoff_mult_3bp_k2);
207 #endif
208
209 #ifdef ALBE
210         set_potential_params(&md,&ap);
211 #else
212         set_potential_params(&md,&tp);
213 #endif
214
215         /* cutoff radius & bondlen */
216 #ifdef ALBE
217         set_cutoff(&md,ALBE_S_SI);
218         set_bondlen(&md,ALBE_S_SI,ALBE_S_C,ALBE_S_SIC);
219         //set_cutoff(&md,ALBE_S_C);
220 #else
221         set_cutoff(&md,TM_S_SI);
222         set_bondlen(&md,TM_S_SI,TM_S_C,-1.0);
223         //set_cutoff(&md,TM_S_C);
224 #endif
225
226         /*
227          * potential parameters
228          */
229
230         /*
231          * tersoff mult potential parameters for SiC
232          */
233         tp.S[0]=TM_S_SI;
234         tp.R[0]=TM_R_SI;
235         tp.A[0]=TM_A_SI;
236         tp.B[0]=TM_B_SI;
237         tp.lambda[0]=TM_LAMBDA_SI;
238         tp.mu[0]=TM_MU_SI;
239         tp.beta[0]=TM_BETA_SI;
240         tp.n[0]=TM_N_SI;
241         tp.c[0]=TM_C_SI;
242         tp.d[0]=TM_D_SI;
243         tp.h[0]=TM_H_SI;
244
245         tp.S[1]=TM_S_C;
246         tp.R[1]=TM_R_C;
247         tp.A[1]=TM_A_C;
248         tp.B[1]=TM_B_C;
249         tp.lambda[1]=TM_LAMBDA_C;
250         tp.mu[1]=TM_MU_C;
251         tp.beta[1]=TM_BETA_C;
252         tp.n[1]=TM_N_C;
253         tp.c[1]=TM_C_C;
254         tp.d[1]=TM_D_C;
255         tp.h[1]=TM_H_C;
256
257         tp.chi=TM_CHI_SIC;
258
259         tersoff_mult_complete_params(&tp);
260
261         /*
262          * albe mult potential parameters for SiC
263          */
264         ap.S[0]=ALBE_S_SI;
265         ap.R[0]=ALBE_R_SI;
266         ap.A[0]=ALBE_A_SI;
267         ap.B[0]=ALBE_B_SI;
268         ap.r0[0]=ALBE_R0_SI;
269         ap.lambda[0]=ALBE_LAMBDA_SI;
270         ap.mu[0]=ALBE_MU_SI;
271         ap.gamma[0]=ALBE_GAMMA_SI;
272         ap.c[0]=ALBE_C_SI;
273         ap.d[0]=ALBE_D_SI;
274         ap.h[0]=ALBE_H_SI;
275
276         ap.S[1]=ALBE_S_C;
277         ap.R[1]=ALBE_R_C;
278         ap.A[1]=ALBE_A_C;
279         ap.B[1]=ALBE_B_C;
280         ap.r0[1]=ALBE_R0_C;
281         ap.lambda[1]=ALBE_LAMBDA_C;
282         ap.mu[1]=ALBE_MU_C;
283         ap.gamma[1]=ALBE_GAMMA_C;
284         ap.c[1]=ALBE_C_C;
285         ap.d[1]=ALBE_D_C;
286         ap.h[1]=ALBE_H_C;
287
288         ap.Smixed=ALBE_S_SIC;
289         ap.Rmixed=ALBE_R_SIC;
290         ap.Amixed=ALBE_A_SIC;
291         ap.Bmixed=ALBE_B_SIC;
292         ap.r0_mixed=ALBE_R0_SIC;
293         ap.lambda_m=ALBE_LAMBDA_SIC;
294         ap.mu_m=ALBE_MU_SIC;
295         ap.gamma_m=ALBE_GAMMA_SIC;
296         ap.c_mixed=ALBE_C_SIC;
297         ap.d_mixed=ALBE_D_SIC;
298         ap.h_mixed=ALBE_H_SIC;
299
300         albe_mult_complete_params(&ap);
301
302         /* set (initial) dimensions of simulation volume */
303 #ifdef ALBE
304         set_dim(&md,LCNTX*ALBE_LC_SI,LCNTY*ALBE_LC_SI,LCNTZ*ALBE_LC_SI,TRUE);
305         //set_dim(&md,LCNTX*ALBE_LC_C,LCNTY*ALBE_LC_C,LCNTZ*ALBE_LC_C,TRUE);
306         //set_dim(&md,LCNTX*ALBE_LC_SIC,LCNTY*ALBE_LC_SIC,LCNTZ*ALBE_LC_SIC,TRUE);
307 #else
308         set_dim(&md,LCNTX*LC_SI,LCNTY*LC_SI,LCNTZ*LC_SI,TRUE);
309         //set_dim(&md,LCNTX*LC_C,LCNTY*LC_C,LCNTZ*LC_C,TRUE);
310         //set_dim(&md,LCNTX*TM_LC_SIC,LCNTY*TM_LC_SIC,LCNTZ*TM_LC_SIC,TRUE);
311 #endif
312
313         /* set periodic boundary conditions in all directions */
314         set_pbc(&md,TRUE,TRUE,TRUE);
315
316         /* create the lattice / place atoms */
317         //
318 #ifdef ALBE
319         create_lattice(&md,DIAMOND,ALBE_LC_SI,SI,M_SI,
320         //create_lattice(&md,DIAMOND,ALBE_LC_C,C,M_C,
321 #else
322         create_lattice(&md,DIAMOND,LC_SI,SI,M_SI,
323 #endif
324                        ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
325         //               ATOM_ATTR_2BP|ATOM_ATTR_HB,
326                        0,LCNTX,LCNTY,LCNTZ,NULL);
327         //               1,LCNTX,LCNTY,LCNTZ,NULL);
328
329         /* create zinkblende structure */
330         /*
331 #ifdef ALBE
332         r.x=0.5*0.25*ALBE_LC_SIC; r.y=r.x; r.z=r.x;
333         create_lattice(&md,FCC,ALBE_LC_SIC,SI,M_SI,
334                        ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
335                        0,LCNTX,LCNTY,LCNTZ,&r);
336         r.x+=0.25*ALBE_LC_SIC; r.y=r.x; r.z=r.x;
337         create_lattice(&md,FCC,ALBE_LC_SIC,C,M_C,
338                        ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
339                        1,LCNTX,LCNTY,LCNTZ,&r);
340 #else
341         r.x=0.5*0.25*TM_LC_SIC; r.y=r.x; r.z=r.x;
342         create_lattice(&md,FCC,TM_LC_SIC,SI,M_SI,
343                        ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
344                        0,LCNTX,LCNTY,LCNTZ,&r);
345         r.x+=0.25*TM_LC_SIC; r.y=r.x; r.z=r.x;
346         create_lattice(&md,FCC,TM_LC_SIC,C,M_C,
347                        ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
348                        1,LCNTX,LCNTY,LCNTZ,&r);
349 #endif
350         */
351
352         /* check for right atom placing */
353         moldyn_bc_check(&md);
354
355         /* testing configuration */
356         //r.x=0.27*sqrt(3.0)*LC_SI/2.0; v.x=0;
357         //r.x=(TM_S_SI+TM_R_SI)/4.0;    v.x=0;
358         //r.y=0;                v.y=0;
359         //r.z=0;                v.z=0;
360         //add_atom(&md,SI,M_SI,0,
361         //           ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
362         //           ATOM_ATTR_2BP|ATOM_ATTR_HB,
363         //           &r,&v);
364         //r.x=-r.x;     v.x=-v.x;
365         //r.y=0;                v.y=0;
366         //r.z=0;                v.z=0;
367         //add_atom(&md,SI,M_SI,0,
368         //           ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
369         //           ATOM_ATTR_2BP|ATOM_ATTR_HB,
370         //           &r,&v);
371         //r.z=0.27*sqrt(3.0)*LC_SI/2.0; v.z=0;
372         //r.x=(TM_S_SI+TM_R_SI)/4.0;    v.x=0;
373         //r.y=0;                v.y=0;
374         //r.x=0;                v.x=0;
375         //add_atom(&md,SI,M_SI,0,
376         //           ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
377         //           ATOM_ATTR_2BP|ATOM_ATTR_HB,
378         //           &r,&v);
379         //r.z=-r.z;     v.z=-v.z;
380         //r.y=0;                v.y=0;
381         //r.x=0;                v.x=0;
382         //add_atom(&md,SI,M_SI,0,
383         //           ATOM_ATTR_1BP|ATOM_ATTR_2BP|ATOM_ATTR_3BP|ATOM_ATTR_HB,
384         //           ATOM_ATTR_2BP|ATOM_ATTR_HB,
385         //           &r,&v);
386
387         /* set temperature & pressure */
388         set_temperature(&md,atof(argv[2])+273.0);
389         set_pressure(&md,BAR);
390
391         /* set amount of steps to skip before average calc */
392         set_avg_skip(&md,AVG_SKIP);
393
394         /* set p/t scaling */
395         //set_pt_scale(&md,0,0,T_SCALE_BERENDSEN,100.0);
396         //set_pt_scale(&md,P_SCALE_BERENDSEN,0.001,
397         //                 T_SCALE_BERENDSEN,100.0);
398         //set_pt_scale(&md,0,0,T_SCALE_DIRECT,1.0);
399         //set_pt_scale(&md,P_SCALE_BERENDSEN,0.001,0,0);
400         
401         /* initial thermal fluctuations of particles (in equilibrium) */
402         thermal_init(&md,TRUE);
403
404         /* create the simulation schedule */
405         moldyn_add_schedule(&md,PRERUN,PRE_TAU);
406
407         /* schedule hook function */
408         memset(&hookparam,0,sizeof(t_hp));
409         hookparam.argc=argc;
410         hookparam.argv=argv;
411         moldyn_set_schedule_hook(&md,&sic_hook,&hookparam);
412         //moldyn_set_schedule_hook(&md,&hook_del_atom,&hookparam);
413         //moldyn_add_schedule(&md,POSTRUN,1.0);
414
415         /* activate logging */
416         moldyn_set_log_dir(&md,argv[1]);
417         moldyn_set_report(&md,"Frank Zirkelbach",R_TITLE);
418         moldyn_set_log(&md,LOG_TOTAL_ENERGY,LOG_E);
419         moldyn_set_log(&md,LOG_TEMPERATURE,LOG_T);
420         moldyn_set_log(&md,LOG_PRESSURE,LOG_P);
421         moldyn_set_log(&md,VISUAL_STEP,LOG_V);
422         moldyn_set_log(&md,SAVE_STEP,LOG_S);
423         moldyn_set_log(&md,CREATE_REPORT,0);
424
425         /*
426          * let's do the actual md algorithm now
427          *
428          * integration of newtons equations
429          */
430         moldyn_integrate(&md);
431 #ifdef dEBUG
432 return 0;
433 #endif
434
435         /*
436          * post processing the data
437          */
438
439         /* close */
440         moldyn_shutdown(&md);
441         
442         return 0;
443 }
444