energy updates
[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         p->c2[0]=p->c[0]*p->c[0];
100         p->c2[1]=p->c[1]*p->c[1];
101         p->c2_mixed=p->c_mixed*p->c_mixed;
102         p->d2[0]=p->d[0]*p->d[0];
103         p->d2[1]=p->d[1]*p->d[1];
104         p->d2_mixed=p->d_mixed*p->d_mixed;
105         p->c2d2[0]=p->c2[0]/p->d2[0];
106         p->c2d2[1]=p->c2[1]/p->d2[1];
107         p->c2d2_m=p->c2_mixed/p->d2_mixed;
108
109         printf("[albe] mult parameter info:\n");
110         printf("  S (A)  | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
111         printf("  R (A)  | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
112         printf("  A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
113         printf("  B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
114         printf("  lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
115                                           p->lambda_m);
116         printf("  mu     | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
117         printf("  gamma  | %f | %f\n",p->gamma[0],p->gamma[1]);
118         printf("  c      | %f | %f\n",p->c[0],p->c[1]);
119         printf("  d      | %f | %f\n",p->d[0],p->d[1]);
120         printf("  h      | %f | %f\n",p->h[0],p->h[1]);
121
122         return 0;
123 }
124
125 /* albe 3 body potential function (first ij loop) */
126 int albe_mult_3bp_j1(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
127
128         t_albe_mult_params *params;
129         t_albe_exchange *exchange;
130         unsigned char brand;
131         double S2;
132         t_3dvec dist_ij;
133         double d_ij2,d_ij;
134
135         params=moldyn->pot_params;
136         exchange=&(params->exchange);
137
138         /* reset zeta sum */
139         exchange->zeta_ij=0.0;
140
141         /*
142          * set ij depending values
143          */
144
145         brand=ai->brand;
146         if(brand==aj->brand) {
147                 S2=params->S2[brand];
148         }
149         else {
150                 S2=params->S2mixed;
151         }
152
153         /* dist_ij, d_ij2 */
154         v3_sub(&dist_ij,&(aj->r),&(ai->r));
155         if(bc) check_per_bound(moldyn,&dist_ij);
156         d_ij2=v3_absolute_square(&dist_ij);
157
158         /* if d_ij2 > S2 => no force & potential energy contribution */
159         if(d_ij2>S2) {
160                 moldyn->run3bp=0;
161                 return 0;
162         }
163
164         /* d_ij */
165         d_ij=sqrt(d_ij2);
166
167         /* store values */
168         exchange->dist_ij=dist_ij;
169         exchange->d_ij2=d_ij2;
170         exchange->d_ij=d_ij;
171
172         /* reset k counter for first k loop */
173         exchange->kcount=0;
174                 
175         return 0;
176 }
177
178 /* albe 3 body potential function (first k loop) */
179 int albe_mult_3bp_k1(t_moldyn *moldyn,
180                      t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
181
182         t_albe_mult_params *params;
183         t_albe_exchange *exchange;
184         unsigned char brand;
185         double R,S,S2;
186         t_3dvec dist_ij,dist_ik;
187         double d_ik2,d_ik,d_ij;
188         double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
189         double f_c_ik,df_c_ik;
190         int kcount;
191
192         params=moldyn->pot_params;
193         exchange=&(params->exchange);
194         kcount=exchange->kcount;
195
196         if(kcount>ALBE_MAXN) {
197                 printf("FATAL: neighbours = %d\n",kcount);
198                 printf("  -> %d %d %d\n",ai->tag,aj->tag,ak->tag);
199         }
200
201         /* ik constants */
202         brand=ai->brand;
203         if(brand==ak->brand) {
204                 R=params->R[brand];
205                 S=params->S[brand];
206                 S2=params->S2[brand];
207                 /* albe needs i,k depending c,d,h and gamma values */
208                 exchange->gamma_i=&(params->gamma[brand]);
209                 exchange->c_i=&(params->c[brand]);
210                 exchange->d_i=&(params->d[brand]);
211                 exchange->h_i=&(params->h[brand]);
212         }
213         else {
214                 R=params->Rmixed;
215                 S=params->Smixed;
216                 S2=params->S2mixed;
217                 /* albe needs i,k depending c,d,h and gamma values */
218                 exchange->gamma_i=&(params->gamma_m);
219                 exchange->c_i=&(params->c_mixed);
220                 exchange->d_i=&(params->d_mixed);
221                 exchange->h_i=&(params->h_mixed);
222         }
223         exchange->ci2=*(exchange->c_i)**(exchange->c_i);
224         exchange->di2=*(exchange->d_i)**(exchange->d_i);
225         exchange->ci2di2=exchange->ci2/exchange->di2;
226
227         /* dist_ik, d_ik2 */
228         v3_sub(&dist_ik,&(ak->r),&(ai->r));
229         if(bc) check_per_bound(moldyn,&dist_ik);
230         d_ik2=v3_absolute_square(&dist_ik);
231
232         /* store data for second k loop */
233         exchange->dist_ik[kcount]=dist_ik;
234         exchange->d_ik2[kcount]=d_ik2;
235
236         /* return if not within cutoff */
237         if(d_ik2>S2) {
238                 exchange->kcount++;
239                 return 0;
240         }
241
242         /* d_ik */
243         d_ik=sqrt(d_ik2);
244
245         /* dist_ij, d_ij */
246         dist_ij=exchange->dist_ij;
247         d_ij=exchange->d_ij;
248
249         /* cos theta */
250         cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
251
252         /* g_ijk */
253         h_cos=*(exchange->h_i)+cos_theta; // + in albe formalism
254         d2_h_cos2=exchange->di2+(h_cos*h_cos);
255         frac=exchange->ci2/d2_h_cos2;
256         g=*(exchange->gamma_i)*(1.0+exchange->ci2di2-frac);
257         dg=2.0*frac**(exchange->gamma_i)*h_cos/d2_h_cos2; // + in albe f..
258
259         /* zeta sum += f_c_ik * g_ijk */
260         if(d_ik<=R) {
261                 exchange->zeta_ij+=g;
262                 f_c_ik=1.0;
263                 df_c_ik=0.0;
264         }
265         else {
266                 s_r=S-R;
267                 arg=M_PI*(d_ik-R)/s_r;
268                 f_c_ik=0.5+0.5*cos(arg);
269                 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
270                 exchange->zeta_ij+=f_c_ik*g;
271         }
272
273 #ifdef DEBUG
274         if(ai==&(moldyn->atom[DATOM])) 
275                 printf("zeta_ij: %f %f %f %f\n",f_c_ik*g,f_c_ik,g,d_ik);
276 #endif
277
278         /* store even more data for second k loop */
279         exchange->g[kcount]=g;
280         exchange->dg[kcount]=dg;
281         exchange->d_ik[kcount]=d_ik;
282         exchange->cos_theta[kcount]=cos_theta;
283         exchange->f_c_ik[kcount]=f_c_ik;
284         exchange->df_c_ik[kcount]=df_c_ik;
285
286         /* increase k counter */
287         exchange->kcount++;
288
289         return 0;
290 }
291
292 int albe_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
293
294         t_albe_mult_params *params;
295         t_albe_exchange *exchange;
296         t_3dvec force;
297         double f_a,df_a,b,db,f_c,df_c;
298         double f_r,df_r;
299         double scale;
300         double mu,B;
301         double lambda,A;
302         double d_ij,r0;
303         unsigned char brand;
304         double S,R,s_r,arg;
305         double energy;
306
307         params=moldyn->pot_params;
308         exchange=&(params->exchange);
309
310         brand=aj->brand;
311         if(brand==ai->brand) {
312                 S=params->S[brand];
313                 R=params->R[brand];
314                 B=params->B[brand];
315                 A=params->A[brand];
316                 r0=params->r0[brand];
317                 mu=params->mu[brand];
318                 lambda=params->lambda[brand];
319         }
320         else {
321                 S=params->Smixed;
322                 R=params->Rmixed;
323                 B=params->Bmixed;
324                 A=params->Amixed;
325                 r0=params->r0_mixed;
326                 mu=params->mu_m;
327                 lambda=params->lambda_m;
328         }
329
330         d_ij=exchange->d_ij;
331
332         /* f_c, df_c */
333         if(d_ij<R) {
334                 f_c=1.0;
335                 df_c=0.0;
336         }
337         else {
338                 s_r=S-R;
339                 arg=M_PI*(d_ij-R)/s_r;
340                 f_c=0.5+0.5*cos(arg);
341                 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
342         }
343
344         /* f_a, df_a */
345         f_a=-B*exp(-mu*(d_ij-r0));
346         df_a=mu*f_a/d_ij;
347
348         /* f_r, df_r */
349         f_r=A*exp(-lambda*(d_ij-r0));
350         df_r=lambda*f_r/d_ij;
351
352         /* b, db */
353         if(exchange->zeta_ij==0.0) {
354                 b=1.0;
355                 db=0.0;
356         }
357         else {
358                 b=1.0/sqrt(1.0+exchange->zeta_ij);
359                 db=-0.5*b/(1.0+exchange->zeta_ij);
360         }
361
362         /* force contribution for atom i */
363         scale=-0.5*(f_c*(df_r-b*df_a)+df_c*(f_r-b*f_a)); // - in albe formalism
364         v3_scale(&force,&(exchange->dist_ij),scale);
365         v3_add(&(ai->f),&(ai->f),&force);
366
367         /* force contribution for atom j */
368         v3_scale(&force,&force,-1.0); // dri rij = - drj rij
369         v3_add(&(aj->f),&(aj->f),&force);
370
371         /* virial */
372         virial_calc(ai,&force,&(exchange->dist_ij));
373
374 #ifdef DEBUG
375         if((ai==&(moldyn->atom[DATOM]))|(aj==&(moldyn->atom[DATOM]))) {
376                 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,aj->tag);
377                 printf("  adding %f %f %f\n",force.x,force.y,force.z);
378                 if(ai==&(moldyn->atom[DATOM]))
379                         printf("  total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
380                 if(aj==&(moldyn->atom[DATOM]))
381                         printf("  total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
382                 printf("  energy: %f = %f %f %f %f\n",0.5*f_c*(b*f_a+f_r),
383                                                     f_c,b,f_a,f_r);
384                 printf("          %f %f %f\n",exchange->zeta_ij,.0,.0);
385         }
386 #endif
387
388         /* dzeta prefactor = - f_c f_a db, (* -0.5 due to force calc) */
389         exchange->pre_dzeta=0.5*f_a*f_c*db;
390
391         /* energy contribution */
392         energy=0.5*f_c*(f_r-b*f_a); // - in albe formalism
393         moldyn->energy+=energy;
394         ai->e+=energy;
395
396         /* reset k counter for second k loop */
397         exchange->kcount=0;
398                 
399         return 0;
400 }
401
402 /* albe 3 body potential function (second k loop) */
403 int albe_mult_3bp_k2(t_moldyn *moldyn,
404                      t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
405
406         t_albe_mult_params *params;
407         t_albe_exchange *exchange;
408         int kcount;
409         t_3dvec dist_ik,dist_ij;
410         double d_ik2,d_ik,d_ij2,d_ij;
411         unsigned char brand;
412         double S2;
413         double g,dg,cos_theta;
414         double pre_dzeta;
415         double f_c_ik,df_c_ik;
416         double dijdik_inv,fcdg,dfcg;
417         t_3dvec dcosdrj,dcosdrk;
418         t_3dvec force,tmp;
419
420         params=moldyn->pot_params;
421         exchange=&(params->exchange);
422         kcount=exchange->kcount;
423
424         if(kcount>ALBE_MAXN)
425                 printf("FATAL: neighbours!\n");
426
427         /* d_ik2 */
428         d_ik2=exchange->d_ik2[kcount];
429
430         brand=ak->brand;
431         if(brand==ai->brand)
432                 S2=params->S2[brand];
433         else
434                 S2=params->S2mixed;
435
436         /* return if d_ik > S */
437         if(d_ik2>S2) {
438                 exchange->kcount++;
439                 return 0;
440         }
441
442         /* prefactor dzeta */
443         pre_dzeta=exchange->pre_dzeta;
444
445         /* dist_ik, d_ik */
446         dist_ik=exchange->dist_ik[kcount];
447         d_ik=exchange->d_ik[kcount];
448
449         /* f_c_ik, df_c_ik */
450         f_c_ik=exchange->f_c_ik[kcount];
451         df_c_ik=exchange->df_c_ik[kcount];
452
453         /* dist_ij, d_ij2, d_ij */
454         dist_ij=exchange->dist_ij;
455         d_ij2=exchange->d_ij2;
456         d_ij=exchange->d_ij;
457
458         /* g, dg, cos_theta */
459         g=exchange->g[kcount];
460         dg=exchange->dg[kcount];
461         cos_theta=exchange->cos_theta[kcount];
462
463         /* cos_theta derivatives wrt j,k */
464         dijdik_inv=1.0/(d_ij*d_ik);
465         v3_scale(&dcosdrj,&dist_ik,dijdik_inv);         // j
466         v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
467         v3_add(&dcosdrj,&dcosdrj,&tmp);
468         v3_scale(&dcosdrk,&dist_ij,dijdik_inv);         // k
469         v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
470         v3_add(&dcosdrk,&dcosdrk,&tmp);
471
472         /* f_c_ik * dg, df_c_ik * g */
473         fcdg=f_c_ik*dg;
474         dfcg=df_c_ik*g;
475
476         /* derivative wrt j */
477         v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
478
479         /* force contribution */
480         v3_add(&(aj->f),&(aj->f),&force);
481
482 #ifdef DEBUG
483         if(aj==&(moldyn->atom[DATOM])) {
484                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
485                 printf("  adding %f %f %f\n",force.x,force.y,force.z);
486                 printf("  total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
487                 printf("  angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
488                 printf("    d ij ik = %f %f\n",d_ij,d_ik);
489         }
490 #endif
491
492         /* virial */
493         virial_calc(ai,&force,&dist_ij);
494
495         /* force contribution to atom i */
496         v3_scale(&force,&force,-1.0);
497         v3_add(&(ai->f),&(ai->f),&force);
498
499         /* derivative wrt k */
500         v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
501         v3_scale(&tmp,&dcosdrk,fcdg);
502         v3_add(&force,&force,&tmp);
503         v3_scale(&force,&force,pre_dzeta);
504
505         /* force contribution */
506         v3_add(&(ak->f),&(ak->f),&force);
507
508 #ifdef DEBUG
509         if(ak==&(moldyn->atom[DATOM])) {
510                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
511                 printf("  adding %f %f %f\n",force.x,force.y,force.z);
512                 printf("  total k: %f %f %f\n",ak->f.x,ak->f.y,ak->f.z);
513                 printf("  angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
514                 printf("    d ij ik = %f %f\n",d_ij,d_ik);
515         }
516 #endif
517
518         /* virial */
519         virial_calc(ai,&force,&dist_ik);
520         
521         /* force contribution to atom i */
522         v3_scale(&force,&force,-1.0);
523         v3_add(&(ai->f),&(ai->f),&force);
524
525         /* increase k counter */
526         exchange->kcount++;     
527
528         return 0;
529
530 }
531
532 int albe_mult_check_2b_bond(t_moldyn *moldyn,t_atom *itom,t_atom *jtom,u8 bc) {
533
534         t_albe_mult_params *params;
535         t_3dvec dist;
536         double d;
537         u8 brand;
538
539         v3_sub(&dist,&(jtom->r),&(itom->r));
540         if(bc) check_per_bound(moldyn,&dist);
541         d=v3_absolute_square(&dist);
542
543         params=moldyn->pot_params;
544         brand=itom->brand;
545
546         if(brand==jtom->brand) {
547                 if(d<=params->S2[brand])
548                         return TRUE;
549         }
550         else {
551                 if(d<=params->S2mixed)
552                         return TRUE;
553         }
554
555         return FALSE;
556 }