]> hackdaworld.org Git - physik/posic.git/blob - potentials/albe.c
added 2b bond check for tersoff
[physik/posic.git] / potentials / albe.c
1 /*
2  * albe.c - albe potential
3  *
4  * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
5  *
6  */
7
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <math.h>
17
18 #include "../moldyn.h"
19 #include "../math/math.h"
20 #include "albe.h"
21
22 /* create mixed terms from parameters and set them */
23 int albe_mult_set_params(t_moldyn *moldyn,int element1,int element2) {
24
25         t_albe_mult_params *p;
26
27         // set cutoff before parameters (actually only necessary for some pots)
28         if(moldyn->cutoff==0.0) {
29                 printf("[albe] WARNING: no cutoff!\n");
30                 return -1;
31         }
32
33         /* alloc mem for potential parameters */
34         moldyn->pot_params=malloc(sizeof(t_albe_mult_params));
35         if(moldyn->pot_params==NULL) {
36                 perror("[albe] pot params alloc");
37                 return -1;
38         }
39
40         /* these are now albe parameters */
41         p=moldyn->pot_params;
42
43         // only 1 combination by now :p
44         switch(element1) {
45                 case SI:
46                         /* type: silicon */
47                         p->S[0]=ALBE_S_SI;
48                         p->R[0]=ALBE_R_SI;
49                         p->A[0]=ALBE_A_SI;
50                         p->B[0]=ALBE_B_SI;
51                         p->r0[0]=ALBE_R0_SI;
52                         p->lambda[0]=ALBE_LAMBDA_SI;
53                         p->mu[0]=ALBE_MU_SI;
54                         p->gamma[0]=ALBE_GAMMA_SI;
55                         p->c[0]=ALBE_C_SI;
56                         p->d[0]=ALBE_D_SI;
57                         p->h[0]=ALBE_H_SI;
58                         switch(element2) {
59                                 case C:
60                                         /* type: carbon */
61                                         p->S[1]=ALBE_S_C;
62                                         p->R[1]=ALBE_R_C;
63                                         p->A[1]=ALBE_A_C;
64                                         p->B[1]=ALBE_B_C;
65                                         p->r0[1]=ALBE_R0_C;
66                                         p->lambda[1]=ALBE_LAMBDA_C;
67                                         p->mu[1]=ALBE_MU_C;
68                                         p->gamma[1]=ALBE_GAMMA_C;
69                                         p->c[1]=ALBE_C_C;
70                                         p->d[1]=ALBE_D_C;
71                                         p->h[1]=ALBE_H_C;
72                                         /* mixed type: silicon carbide */
73                                         p->Smixed=ALBE_S_SIC;
74                                         p->Rmixed=ALBE_R_SIC;
75                                         p->Amixed=ALBE_A_SIC;
76                                         p->Bmixed=ALBE_B_SIC;
77                                         p->r0_mixed=ALBE_R0_SIC;
78                                         p->lambda_m=ALBE_LAMBDA_SIC;
79                                         p->mu_m=ALBE_MU_SIC;
80                                         p->gamma_m=ALBE_GAMMA_SIC;
81                                         p->c_mixed=ALBE_C_SIC;
82                                         p->d_mixed=ALBE_D_SIC;
83                                         p->h_mixed=ALBE_H_SIC;
84                                         break;
85                                 default:
86                                         printf("[albe] WARNING: element2\n");
87                                         return -1;
88                         }
89                         break;
90                 default:
91                         printf("[albe] WARNING: element1\n");
92                         return -1;
93         }
94
95         printf("[albe] parameter completion\n");
96         p->S2[0]=p->S[0]*p->S[0];
97         p->S2[1]=p->S[1]*p->S[1];
98         p->S2mixed=p->Smixed*p->Smixed;
99
100         printf("[albe] mult parameter info:\n");
101         printf("  S (A)  | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
102         printf("  R (A)  | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
103         printf("  A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
104         printf("  B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
105         printf("  lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
106                                           p->lambda_m);
107         printf("  mu     | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
108         printf("  gamma  | %f | %f\n",p->gamma[0],p->gamma[1]);
109         printf("  c      | %f | %f\n",p->c[0],p->c[1]);
110         printf("  d      | %f | %f\n",p->d[0],p->d[1]);
111         printf("  h      | %f | %f\n",p->h[0],p->h[1]);
112
113         return 0;
114 }
115
116 /* albe 3 body potential function (first ij loop) */
117 int albe_mult_3bp_j1(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
118
119         t_albe_mult_params *params;
120         t_albe_exchange *exchange;
121         unsigned char brand;
122         double S2;
123         t_3dvec dist_ij;
124         double d_ij2,d_ij;
125
126         params=moldyn->pot_params;
127         exchange=&(params->exchange);
128
129         /* reset zeta sum */
130         exchange->zeta_ij=0.0;
131
132         /*
133          * set ij depending values
134          */
135
136         brand=ai->brand;
137         if(brand==aj->brand) {
138                 S2=params->S2[brand];
139         }
140         else {
141                 S2=params->S2mixed;
142         }
143
144         /* dist_ij, d_ij2 */
145         v3_sub(&dist_ij,&(aj->r),&(ai->r));
146         if(bc) check_per_bound(moldyn,&dist_ij);
147         d_ij2=v3_absolute_square(&dist_ij);
148
149         /* if d_ij2 > S2 => no force & potential energy contribution */
150         if(d_ij2>S2) {
151                 moldyn->run3bp=0;
152                 return 0;
153         }
154
155         /* d_ij */
156         d_ij=sqrt(d_ij2);
157
158         /* store values */
159         exchange->dist_ij=dist_ij;
160         exchange->d_ij2=d_ij2;
161         exchange->d_ij=d_ij;
162
163         /* reset k counter for first k loop */
164         exchange->kcount=0;
165                 
166         return 0;
167 }
168
169 /* albe 3 body potential function (first k loop) */
170 int albe_mult_3bp_k1(t_moldyn *moldyn,
171                      t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
172
173         t_albe_mult_params *params;
174         t_albe_exchange *exchange;
175         unsigned char brand;
176         double R,S,S2;
177         t_3dvec dist_ij,dist_ik;
178         double d_ik2,d_ik,d_ij;
179         double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
180         double f_c_ik,df_c_ik;
181         int kcount;
182
183         params=moldyn->pot_params;
184         exchange=&(params->exchange);
185         kcount=exchange->kcount;
186
187         if(kcount>ALBE_MAXN) {
188                 printf("FATAL: neighbours = %d\n",kcount);
189                 printf("  -> %d %d %d\n",ai->tag,aj->tag,ak->tag);
190         }
191
192         /* ik constants */
193         brand=ai->brand;
194         if(brand==ak->brand) {
195                 R=params->R[brand];
196                 S=params->S[brand];
197                 S2=params->S2[brand];
198                 /* albe needs i,k depending c,d,h and gamma values */
199                 exchange->gamma_i=&(params->gamma[brand]);
200                 exchange->c_i=&(params->c[brand]);
201                 exchange->d_i=&(params->d[brand]);
202                 exchange->h_i=&(params->h[brand]);
203         }
204         else {
205                 R=params->Rmixed;
206                 S=params->Smixed;
207                 S2=params->S2mixed;
208                 /* albe needs i,k depending c,d,h and gamma values */
209                 exchange->gamma_i=&(params->gamma_m);
210                 exchange->c_i=&(params->c_mixed);
211                 exchange->d_i=&(params->d_mixed);
212                 exchange->h_i=&(params->h_mixed);
213         }
214         exchange->ci2=*(exchange->c_i)**(exchange->c_i);
215         exchange->di2=*(exchange->d_i)**(exchange->d_i);
216         exchange->ci2di2=exchange->ci2/exchange->di2;
217
218         /* dist_ik, d_ik2 */
219         v3_sub(&dist_ik,&(ak->r),&(ai->r));
220         if(bc) check_per_bound(moldyn,&dist_ik);
221         d_ik2=v3_absolute_square(&dist_ik);
222
223         /* store data for second k loop */
224         exchange->dist_ik[kcount]=dist_ik;
225         exchange->d_ik2[kcount]=d_ik2;
226
227         /* return if not within cutoff */
228         if(d_ik2>S2) {
229                 exchange->kcount++;
230                 return 0;
231         }
232
233         /* d_ik */
234         d_ik=sqrt(d_ik2);
235
236         /* dist_ij, d_ij */
237         dist_ij=exchange->dist_ij;
238         d_ij=exchange->d_ij;
239
240         /* cos theta */
241         cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
242
243         /* g_ijk */
244         h_cos=*(exchange->h_i)+cos_theta; // + in albe formalism
245         d2_h_cos2=exchange->di2+(h_cos*h_cos);
246         frac=exchange->ci2/d2_h_cos2;
247         g=*(exchange->gamma_i)*(1.0+exchange->ci2di2-frac);
248         dg=2.0*frac**(exchange->gamma_i)*h_cos/d2_h_cos2; // + in albe f..
249
250         /* zeta sum += f_c_ik * g_ijk */
251         if(d_ik<=R) {
252                 exchange->zeta_ij+=g;
253                 f_c_ik=1.0;
254                 df_c_ik=0.0;
255         }
256         else {
257                 s_r=S-R;
258                 arg=M_PI*(d_ik-R)/s_r;
259                 f_c_ik=0.5+0.5*cos(arg);
260                 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
261                 exchange->zeta_ij+=f_c_ik*g;
262         }
263
264         /* store even more data for second k loop */
265         exchange->g[kcount]=g;
266         exchange->dg[kcount]=dg;
267         exchange->d_ik[kcount]=d_ik;
268         exchange->cos_theta[kcount]=cos_theta;
269         exchange->f_c_ik[kcount]=f_c_ik;
270         exchange->df_c_ik[kcount]=df_c_ik;
271
272         /* increase k counter */
273         exchange->kcount++;
274
275         return 0;
276 }
277
278 int albe_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
279
280         t_albe_mult_params *params;
281         t_albe_exchange *exchange;
282         t_3dvec force;
283         double f_a,df_a,b,db,f_c,df_c;
284         double f_r,df_r;
285         double scale;
286         double mu,B;
287         double lambda,A;
288         double d_ij,r0;
289         unsigned char brand;
290         double S,R,s_r,arg;
291         double energy;
292
293         params=moldyn->pot_params;
294         exchange=&(params->exchange);
295
296         brand=aj->brand;
297         if(brand==ai->brand) {
298                 S=params->S[brand];
299                 R=params->R[brand];
300                 B=params->B[brand];
301                 A=params->A[brand];
302                 r0=params->r0[brand];
303                 mu=params->mu[brand];
304                 lambda=params->lambda[brand];
305         }
306         else {
307                 S=params->Smixed;
308                 R=params->Rmixed;
309                 B=params->Bmixed;
310                 A=params->Amixed;
311                 r0=params->r0_mixed;
312                 mu=params->mu_m;
313                 lambda=params->lambda_m;
314         }
315
316         d_ij=exchange->d_ij;
317
318         /* f_c, df_c */
319         if(d_ij<R) {
320                 f_c=1.0;
321                 df_c=0.0;
322         }
323         else {
324                 s_r=S-R;
325                 arg=M_PI*(d_ij-R)/s_r;
326                 f_c=0.5+0.5*cos(arg);
327                 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
328         }
329
330         /* f_a, df_a */
331         f_a=-B*exp(-mu*(d_ij-r0));
332         df_a=mu*f_a/d_ij;
333
334         /* f_r, df_r */
335         f_r=A*exp(-lambda*(d_ij-r0));
336         df_r=lambda*f_r/d_ij;
337
338         /* b, db */
339         if(exchange->zeta_ij==0.0) {
340                 b=1.0;
341                 db=0.0;
342         }
343         else {
344                 b=1.0/sqrt(1.0+exchange->zeta_ij);
345                 db=-0.5*b/(1.0+exchange->zeta_ij);
346         }
347
348         /* force contribution for atom i */
349         scale=-0.5*(f_c*(df_r-b*df_a)+df_c*(f_r-b*f_a)); // - in albe formalism
350         v3_scale(&force,&(exchange->dist_ij),scale);
351         v3_add(&(ai->f),&(ai->f),&force);
352
353         /* force contribution for atom j */
354         v3_scale(&force,&force,-1.0); // dri rij = - drj rij
355         v3_add(&(aj->f),&(aj->f),&force);
356
357         /* virial */
358         virial_calc(aj,&force,&(exchange->dist_ij));
359
360 #ifdef DEBUG
361 if(moldyn->time>DSTART&&moldyn->time<DEND) {
362         if((ai==&(moldyn->atom[DATOM]))|(aj==&(moldyn->atom[DATOM]))) {
363                 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,aj->tag);
364                 printf("  adding %f %f %f\n",force.x,force.y,force.z);
365                 if(ai==&(moldyn->atom[0]))
366                         printf("  total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
367                 if(aj==&(moldyn->atom[0]))
368                         printf("  total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
369                 printf("  energy: %f = %f %f %f %f\n",0.5*f_c*(b*f_a+f_r),
370                                                     f_c,b,f_a,f_r);
371                 printf("          %f %f %f\n",exchange->zeta_ij,.0,.0);
372         }
373 }
374 #endif
375
376         /* dzeta prefactor = - f_c f_a db, (* -0.5 due to force calc) */
377         exchange->pre_dzeta=0.5*f_a*f_c*db;
378
379         /* energy contribution */
380         energy=0.5*f_c*(f_r-b*f_a); // - in albe formalism
381         moldyn->energy+=energy;
382         ai->e+=energy;
383
384         /* reset k counter for second k loop */
385         exchange->kcount=0;
386                 
387         return 0;
388 }
389
390 /* albe 3 body potential function (second k loop) */
391 int albe_mult_3bp_k2(t_moldyn *moldyn,
392                      t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
393
394         t_albe_mult_params *params;
395         t_albe_exchange *exchange;
396         int kcount;
397         t_3dvec dist_ik,dist_ij;
398         double d_ik2,d_ik,d_ij2,d_ij;
399         unsigned char brand;
400         double S2;
401         double g,dg,cos_theta;
402         double pre_dzeta;
403         double f_c_ik,df_c_ik;
404         double dijdik_inv,fcdg,dfcg;
405         t_3dvec dcosdrj,dcosdrk;
406         t_3dvec force,tmp;
407
408         params=moldyn->pot_params;
409         exchange=&(params->exchange);
410         kcount=exchange->kcount;
411
412         if(kcount>ALBE_MAXN)
413                 printf("FATAL: neighbours!\n");
414
415         /* d_ik2 */
416         d_ik2=exchange->d_ik2[kcount];
417
418         brand=ak->brand;
419         if(brand==ai->brand)
420                 S2=params->S2[brand];
421         else
422                 S2=params->S2mixed;
423
424         /* return if d_ik > S */
425         if(d_ik2>S2) {
426                 exchange->kcount++;
427                 return 0;
428         }
429
430         /* prefactor dzeta */
431         pre_dzeta=exchange->pre_dzeta;
432
433         /* dist_ik, d_ik */
434         dist_ik=exchange->dist_ik[kcount];
435         d_ik=exchange->d_ik[kcount];
436
437         /* f_c_ik, df_c_ik */
438         f_c_ik=exchange->f_c_ik[kcount];
439         df_c_ik=exchange->df_c_ik[kcount];
440
441         /* dist_ij, d_ij2, d_ij */
442         dist_ij=exchange->dist_ij;
443         d_ij2=exchange->d_ij2;
444         d_ij=exchange->d_ij;
445
446         /* g, dg, cos_theta */
447         g=exchange->g[kcount];
448         dg=exchange->dg[kcount];
449         cos_theta=exchange->cos_theta[kcount];
450
451         /* cos_theta derivatives wrt j,k */
452         dijdik_inv=1.0/(d_ij*d_ik);
453         v3_scale(&dcosdrj,&dist_ik,dijdik_inv);         // j
454         v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
455         v3_add(&dcosdrj,&dcosdrj,&tmp);
456         v3_scale(&dcosdrk,&dist_ij,dijdik_inv);         // k
457         v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
458         v3_add(&dcosdrk,&dcosdrk,&tmp);
459
460         /* f_c_ik * dg, df_c_ik * g */
461         fcdg=f_c_ik*dg;
462         dfcg=df_c_ik*g;
463
464         /* derivative wrt j */
465         v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
466
467         /* force contribution */
468         v3_add(&(aj->f),&(aj->f),&force);
469
470 #ifdef DEBUG
471 if(moldyn->time>DSTART&&moldyn->time<DEND) {
472         if(aj==&(moldyn->atom[DATOM])) {
473                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
474                 printf("  adding %f %f %f\n",force.x,force.y,force.z);
475                 printf("  total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
476                 printf("  angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
477                 printf("    d ij ik = %f %f\n",d_ij,d_ik);
478         }
479 }
480 #endif
481
482         /* force contribution to atom i */
483         v3_scale(&force,&force,-1.0);
484         v3_add(&(ai->f),&(ai->f),&force);
485
486         /* virial */
487         virial_calc(ai,&force,&dist_ij);
488
489         /* derivative wrt k */
490         v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
491         v3_scale(&tmp,&dcosdrk,fcdg);
492         v3_add(&force,&force,&tmp);
493         v3_scale(&force,&force,pre_dzeta);
494
495         /* force contribution */
496         v3_add(&(ak->f),&(ak->f),&force);
497
498 #ifdef DEBUG
499 if(moldyn->time>DSTART&&moldyn->time<DEND) {
500         if(ak==&(moldyn->atom[DATOM])) {
501                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
502                 printf("  adding %f %f %f\n",force.x,force.y,force.z);
503                 printf("  total k: %f %f %f\n",ak->f.x,ak->f.y,ak->f.z);
504                 printf("  angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
505                 printf("    d ij ik = %f %f\n",d_ij,d_ik);
506         }
507 }
508 #endif
509
510         /* force contribution to atom i */
511         v3_scale(&force,&force,-1.0);
512         v3_add(&(ai->f),&(ai->f),&force);
513
514         /* virial */
515         virial_calc(ai,&force,&dist_ik);
516         
517         /* increase k counter */
518         exchange->kcount++;     
519
520         return 0;
521
522 }
523
524 int albe_mult_check_2b_bond(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,u8 bc) {
525
526         t_albe_mult_params *params;
527         t_3dvec dist;
528         double d;
529         u8 brand;
530
531         v3_sub(&dist,&(jtom->r),&(itom->r));
532         if(bc) check_per_bound(moldyn,&dist);
533         d=v3_absolute_square(&dist);
534
535         params=moldyn->pot_params;
536         brand=itom->brand;
537
538         if(brand==jtom->brand) {
539                 if(d<=params->S2[brand])
540                         return TRUE;
541         }
542         else {
543                 if(d<=params->S2mixed)
544                         return TRUE;
545         }
546
547         return FALSE;
548 }