6bfcf30e0239ce2d0fa964607ff2ef9098ce403d
[physik/posic.git] / potentials / tersoff.c
1 /*
2  * tersoff.c - tersoff 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 "tersoff.h"
21
22 /* create mixed terms from parameters and set them */
23 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
24
25         printf("[moldyn] tersoff parameter completion\n");
26         p->S2[0]=p->S[0]*p->S[0];
27         p->S2[1]=p->S[1]*p->S[1];
28         p->Smixed=sqrt(p->S[0]*p->S[1]);
29         p->S2mixed=p->Smixed*p->Smixed;
30         p->Rmixed=sqrt(p->R[0]*p->R[1]);
31         p->Amixed=sqrt(p->A[0]*p->A[1]);
32         p->Bmixed=sqrt(p->B[0]*p->B[1]);
33         p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
34         p->mu_m=0.5*(p->mu[0]+p->mu[1]);
35
36         printf("[moldyn] tersoff mult parameter info:\n");
37         printf("  S (A)  | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
38         printf("  R (A)  | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
39         printf("  A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
40         printf("  B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
41         printf("  lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
42                                           p->lambda_m);
43         printf("  mu     | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
44         printf("  beta   | %.10f | %.10f\n",p->beta[0],p->beta[1]);
45         printf("  n      | %f | %f\n",p->n[0],p->n[1]);
46         printf("  c      | %f | %f\n",p->c[0],p->c[1]);
47         printf("  d      | %f | %f\n",p->d[0],p->d[1]);
48         printf("  h      | %f | %f\n",p->h[0],p->h[1]);
49         printf("  chi    | %f \n",p->chi);
50
51         return 0;
52 }
53
54 /* tersoff 1 body part */
55 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
56
57         int brand;
58         t_tersoff_mult_params *params;
59         t_tersoff_exchange *exchange;
60         
61         brand=ai->brand;
62         params=moldyn->pot_params;
63         exchange=&(params->exchange);
64
65         /*
66          * simple: point constant parameters only depending on atom i to
67          *         their right values
68          */
69
70         exchange->beta_i=&(params->beta[brand]);
71         exchange->n_i=&(params->n[brand]);
72         exchange->c_i=&(params->c[brand]);
73         exchange->d_i=&(params->d[brand]);
74         exchange->h_i=&(params->h[brand]);
75
76         exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
77         exchange->ci2=params->c[brand]*params->c[brand];
78         exchange->di2=params->d[brand]*params->d[brand];
79         exchange->ci2di2=exchange->ci2/exchange->di2;
80
81         return 0;
82 }
83         
84 /* tersoff 2 body part */
85 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
86
87         t_tersoff_mult_params *params;
88         t_3dvec dist_ij,force;
89         double d_ij,d_ij2;
90         double A,R,S,S2,lambda;
91         double f_r,df_r;
92         double f_c,df_c;
93         int brand;
94         double s_r;
95         double arg;
96
97         printf("WARNING! - tersoff_mult_2bp is obsolete.\n");
98         printf("WARNING! - repulsive part handled in 3bp/j2 routine.\n");
99
100         /* use newtons third law */
101         if(ai<aj) return 0;
102
103         params=moldyn->pot_params;
104         brand=aj->brand;
105
106         /* determine cutoff square */
107         if(brand==ai->brand)
108                 S2=params->S2[brand];
109         else
110                 S2=params->S2mixed;
111
112         /* dist_ij, d_ij2 */
113         v3_sub(&dist_ij,&(aj->r),&(ai->r));
114         if(bc) check_per_bound(moldyn,&dist_ij);
115         d_ij2=v3_absolute_square(&dist_ij);
116
117         /* if d_ij2 > S2 => no force & potential energy contribution */
118         if(d_ij2>S2) {
119                 return 0;
120         }
121
122         /* now we will need the distance */
123         d_ij=sqrt(d_ij2);
124
125         /* more constants */
126         if(brand==ai->brand) {
127                 S=params->S[brand];
128                 R=params->R[brand];
129                 A=params->A[brand];
130                 lambda=params->lambda[brand];
131         }
132         else {
133                 S=params->Smixed;
134                 R=params->Rmixed;
135                 A=params->Amixed;
136                 lambda=params->lambda_m;
137         }
138
139         /* f_r_ij, df_r_ij */
140         f_r=A*exp(-lambda*d_ij);
141         df_r=lambda*f_r/d_ij;
142
143         /* f_c, df_c */
144         if(d_ij<R) {
145                 f_c=1.0;
146                 df_c=0.0;
147                 v3_scale(&force,&dist_ij,-df_r);
148         }
149         else {
150                 s_r=S-R;
151                 arg=M_PI*(d_ij-R)/s_r;
152                 f_c=0.5+0.5*cos(arg);
153                 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
154                 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
155         }
156
157         /* add forces */
158         v3_add(&(ai->f),&(ai->f),&force);
159         v3_sub(&(aj->f),&(aj->f),&force); // reason: dri rij = - drj rij
160
161 #ifdef DEBUG
162         if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
163                 printf("force 2bp: [%d %d]\n",ai->tag,aj->tag);
164                 printf("adding %f %f %f\n",force.x,force.y,force.z);
165                 if(ai==&(moldyn->atom[0]))
166                         printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
167                 if(aj==&(moldyn->atom[0]))
168                         printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
169         }
170 #endif
171
172         /* virial */
173         virial_calc(ai,&force,&dist_ij);
174
175         /* energy 2bp contribution */
176         moldyn->energy+=f_r*f_c;
177
178         return 0;
179 }
180
181 /* tersoff 3 body potential function (first ij loop) */
182 int tersoff_mult_3bp_j1(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
183
184         t_tersoff_mult_params *params;
185         t_tersoff_exchange *exchange;
186         unsigned char brand;
187         double S2;
188         t_3dvec dist_ij;
189         double d_ij2,d_ij;
190
191         params=moldyn->pot_params;
192         exchange=&(params->exchange);
193
194         /* reset zeta sum */
195         exchange->zeta_ij=0.0;
196
197         /*
198          * set ij depending values
199          */
200
201         brand=ai->brand;
202         
203         if(brand==aj->brand)
204                 S2=params->S2[brand];
205         else
206                 S2=params->S2mixed;
207
208         /* dist_ij, d_ij2 */
209         v3_sub(&dist_ij,&(aj->r),&(ai->r));
210         if(bc) check_per_bound(moldyn,&dist_ij);
211         d_ij2=v3_absolute_square(&dist_ij);
212
213         /* if d_ij2 > S2 => no force & potential energy contribution */
214         if(d_ij2>S2) {
215                 moldyn->run3bp=0;
216                 return 0;
217         }
218
219         /* d_ij */
220         d_ij=sqrt(d_ij2);
221
222         /* store values */
223         exchange->dist_ij=dist_ij;
224         exchange->d_ij2=d_ij2;
225         exchange->d_ij=d_ij;
226
227         /* reset k counter for first k loop */
228         exchange->kcount=0;
229                 
230         return 0;
231 }
232
233 /* tersoff 3 body potential function (first k loop) */
234 int tersoff_mult_3bp_k1(t_moldyn *moldyn,
235                         t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
236
237         t_tersoff_mult_params *params;
238         t_tersoff_exchange *exchange;
239         unsigned char brand;
240         double R,S,S2;
241         t_3dvec dist_ij,dist_ik;
242         double d_ik2,d_ik,d_ij;
243         double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
244         double f_c_ik,df_c_ik;
245         int kcount;
246
247         params=moldyn->pot_params;
248         exchange=&(params->exchange);
249         kcount=exchange->kcount;
250
251         if(kcount>TERSOFF_MAXN) {
252                 printf("FATAL: neighbours = %d\n",kcount);
253                 printf("  -> %d %d %d\n",ai->tag,aj->tag,ak->tag);
254         }
255
256         /* ik constants */
257         brand=ai->brand;
258         if(brand==ak->brand) {
259                 R=params->R[brand];
260                 S=params->S[brand];
261                 S2=params->S2[brand];
262         }
263         else {
264                 R=params->Rmixed;
265                 S=params->Smixed;
266                 S2=params->S2mixed;
267         }
268
269         /* dist_ik, d_ik2 */
270         v3_sub(&dist_ik,&(ak->r),&(ai->r));
271         if(bc) check_per_bound(moldyn,&dist_ik);
272         d_ik2=v3_absolute_square(&dist_ik);
273
274         /* store data for second k loop */
275         exchange->dist_ik[kcount]=dist_ik;
276         exchange->d_ik2[kcount]=d_ik2;
277
278         /* return if not within cutoff */
279         if(d_ik2>S2) {
280                 exchange->kcount++;
281                 return 0;
282         }
283
284         /* d_ik */
285         d_ik=sqrt(d_ik2);
286
287         /* dist_ij, d_ij */
288         dist_ij=exchange->dist_ij;
289         d_ij=exchange->d_ij;
290
291         /* cos theta */
292         cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
293
294         /* g_ijk */
295         h_cos=*(exchange->h_i)-cos_theta;
296         d2_h_cos2=exchange->di2+(h_cos*h_cos);
297         frac=exchange->ci2/d2_h_cos2;
298         g=1.0+exchange->ci2di2-frac;
299         dg=-2.0*frac*h_cos/d2_h_cos2;
300
301         /* zeta sum += f_c_ik * g_ijk */
302         if(d_ik<=R) {
303                 exchange->zeta_ij+=g;
304                 f_c_ik=1.0;
305                 df_c_ik=0.0;
306         }
307         else {
308                 s_r=S-R;
309                 arg=M_PI*(d_ik-R)/s_r;
310                 f_c_ik=0.5+0.5*cos(arg);
311                 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
312                 exchange->zeta_ij+=f_c_ik*g;
313         }
314
315 #ifdef DEBUG
316         if((ai==&(moldyn->atom[0]))|
317            (aj==&(moldyn->atom[864]))|
318            (ak==&(moldyn->atom[1003]))) {
319                 printf(" -> %f %f %f\n",exchange->ci2di2,frac,h_cos);
320         }
321 #endif
322
323         /* store even more data for second k loop */
324         exchange->g[kcount]=g;
325         exchange->dg[kcount]=dg;
326         exchange->d_ik[kcount]=d_ik;
327         exchange->cos_theta[kcount]=cos_theta;
328         exchange->f_c_ik[kcount]=f_c_ik;
329         exchange->df_c_ik[kcount]=df_c_ik;
330
331         /* increase k counter */
332         exchange->kcount++;
333
334         return 0;
335 }
336
337 int tersoff_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
338
339         t_tersoff_mult_params *params;
340         t_tersoff_exchange *exchange;
341         t_3dvec force;
342         double f_a,df_a,b,db,f_c,df_c;
343         double f_r,df_r;
344         double scale;
345         double mu,B,chi;
346         double lambda,A;
347         double d_ij;
348         unsigned char brand;
349         double ni,tmp;
350         double S,R,s_r,arg;
351
352         params=moldyn->pot_params;
353         exchange=&(params->exchange);
354
355         brand=aj->brand;
356         if(brand==ai->brand) {
357                 S=params->S[brand];
358                 R=params->R[brand];
359                 B=params->B[brand];
360                 A=params->A[brand];
361                 mu=params->mu[brand];
362                 lambda=params->lambda[brand];
363                 chi=1.0;
364         }
365         else {
366                 S=params->Smixed;
367                 R=params->Rmixed;
368                 B=params->Bmixed;
369                 A=params->Amixed;
370                 mu=params->mu_m;
371                 lambda=params->lambda_m;
372                 chi=params->chi;
373         }
374
375         d_ij=exchange->d_ij;
376
377         /* f_c, df_c */
378         if(d_ij<R) {
379                 f_c=1.0;
380                 df_c=0.0;
381         }
382         else {
383                 s_r=S-R;
384                 arg=M_PI*(d_ij-R)/s_r;
385                 f_c=0.5+0.5*cos(arg);
386                 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
387         }
388
389         /* f_a, df_a */
390         f_a=-B*exp(-mu*d_ij);
391         df_a=mu*f_a/d_ij;
392
393         /* f_r, df_r */
394         f_r=A*exp(-lambda*d_ij);
395         df_r=lambda*f_r/d_ij;
396
397         /* b, db */
398         if(exchange->zeta_ij==0.0) {
399                 b=chi;
400                 db=0.0;
401         }
402         else {
403                 ni=*(exchange->n_i);
404                 tmp=exchange->betaini*pow(exchange->zeta_ij,ni-1.0);
405                 b=(1.0+exchange->zeta_ij*tmp);
406                 db=chi*pow(b,-1.0/(2.0*ni)-1.0);
407                 b=db*b;
408                 db*=-0.5*tmp;
409         }
410
411         /* force contribution */
412         scale=-0.5*(f_c*(df_r+b*df_a)+df_c*(f_r+b*df_a));
413         v3_scale(&force,&(exchange->dist_ij),scale);
414         v3_add(&(ai->f),&(ai->f),&force);
415         v3_sub(&(aj->f),&(aj->f),&force); // dri rij = - drj rij
416
417 #ifdef DEBUG
418         if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
419                 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,aj->tag);
420                 printf("adding %f %f %f\n",force.x,force.y,force.z);
421                 if(ai==&(moldyn->atom[0]))
422                         printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
423                 if(aj==&(moldyn->atom[0]))
424                         printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
425                 printf("energy: %f = %f %f %f %f\n",0.5*f_c*(b*f_a+f_r),
426                                                     f_c,b,f_a,f_r);
427                 printf("        %f %f %f\n",exchange->zeta_ij,.0,.0);
428         }
429 #endif
430
431         /* virial */
432         if(aj<ai)
433                 virial_calc(ai,&force,&(exchange->dist_ij));
434
435         /* dzeta prefactor = - 0.5 f_c f_a db */
436         exchange->pre_dzeta=-0.5*f_a*f_c*db;
437
438         /* energy contribution */
439         moldyn->energy+=0.5*f_c*(b*f_a+f_r);
440
441         /* reset k counter for second k loop */
442         exchange->kcount=0;
443                 
444         return 0;
445 }
446
447 /* tersoff 3 body potential function (second k loop) */
448 int tersoff_mult_3bp_k2(t_moldyn *moldyn,
449                         t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
450
451         t_tersoff_mult_params *params;
452         t_tersoff_exchange *exchange;
453         int kcount;
454         t_3dvec dist_ik,dist_ij;
455         double d_ik2,d_ik,d_ij2,d_ij;
456         unsigned char brand;
457         double S2;
458         double g,dg,cos_theta;
459         double pre_dzeta;
460         double f_c_ik,df_c_ik;
461         double dijdik_inv,fcdg,dfcg;
462         t_3dvec dcosdri,dcosdrj,dcosdrk;
463         t_3dvec force,tmp;
464
465         params=moldyn->pot_params;
466         exchange=&(params->exchange);
467         kcount=exchange->kcount;
468
469         if(kcount>TERSOFF_MAXN)
470                 printf("FATAL: neighbours!\n");
471
472         /* d_ik2 */
473         d_ik2=exchange->d_ik2[kcount];
474
475         brand=ak->brand;
476         if(brand==ai->brand)
477                 S2=params->S2[brand];
478         else
479                 S2=params->S2mixed;
480
481         /* return if d_ik > S */
482         if(d_ik2>S2) {
483                 exchange->kcount++;
484                 return 0;
485         }
486
487         /* prefactor dzeta */
488         pre_dzeta=exchange->pre_dzeta;
489
490         /* dist_ik, d_ik */
491         dist_ik=exchange->dist_ik[kcount];
492         d_ik=exchange->d_ik[kcount];
493
494         /* f_c_ik, df_c_ik */
495         f_c_ik=exchange->f_c_ik[kcount];
496         df_c_ik=exchange->df_c_ik[kcount];
497
498         /* dist_ij, d_ij2, d_ij */
499         dist_ij=exchange->dist_ij;
500         d_ij2=exchange->d_ij2;
501         d_ij=exchange->d_ij;
502
503         /* g, dg, cos_theta */
504         g=exchange->g[kcount];
505         dg=exchange->dg[kcount];
506         cos_theta=exchange->cos_theta[kcount];
507
508         /* cos_theta derivatives wrt i,j,k */
509         dijdik_inv=1.0/(d_ij*d_ik);
510         v3_scale(&dcosdrj,&dist_ik,dijdik_inv);
511         v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
512         v3_add(&dcosdrj,&dcosdrj,&tmp);
513         v3_scale(&dcosdrk,&dist_ij,dijdik_inv);
514         v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
515         v3_add(&dcosdrk,&dcosdrk,&tmp);
516         v3_add(&dcosdri,&dcosdrj,&dcosdrk);
517         v3_scale(&dcosdri,&dcosdri,-1.0);
518
519         /* f_c_ik * dg, df_c_ik * g */
520         fcdg=f_c_ik*dg;
521         dfcg=df_c_ik*g;
522
523         /* derivative wrt i */
524         v3_scale(&force,&dist_ik,dfcg);
525         v3_scale(&tmp,&dcosdri,fcdg);
526         v3_add(&force,&force,&tmp);
527         v3_scale(&force,&force,pre_dzeta);
528
529         /* force contribution */
530         v3_add(&(ai->f),&(ai->f),&force);
531         
532 #ifdef DEBUG
533         if(ai==&(moldyn->atom[0])) {
534                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
535                 printf("adding %f %f %f\n",force.x,force.y,force.z);
536                 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
537         }
538 #endif
539
540         /* virial */
541         //virial_calc(ai,&force,&dist_ij);
542
543         /* derivative wrt j */
544         v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
545
546         /* force contribution */
547         v3_add(&(aj->f),&(aj->f),&force);
548
549 #ifdef DEBUG
550         if(aj==&(moldyn->atom[0])) {
551                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
552                 printf("adding %f %f %f\n",force.x,force.y,force.z);
553                 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
554         }
555 #endif
556
557         /* virial */
558         //v3_scale(&force,&force,-1.0);
559         if(aj<ai)
560                 virial_calc(ai,&force,&dist_ij);
561
562         /* derivative wrt k */
563         v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
564         v3_scale(&tmp,&dcosdrk,fcdg);
565         v3_add(&force,&force,&tmp);
566         v3_scale(&force,&force,pre_dzeta);
567
568         /* force contribution */
569         v3_add(&(ak->f),&(ak->f),&force);
570
571 #ifdef DEBUG
572         if(ak==&(moldyn->atom[0])) {
573                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
574                 printf("adding %f %f %f\n",force.x,force.y,force.z);
575                 printf("total k: %f %f %f\n",ak->f.x,ak->f.y,ak->f.z);
576         }
577 #endif
578
579         /* virial */
580         //v3_scale(&force,&force,-1.0);
581         if(aj<ai)
582                 virial_calc(ai,&force,&dist_ik);
583         
584         /* increase k counter */
585         exchange->kcount++;     
586
587         return 0;
588
589 }