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