put repulsive part to 3bp/j2 function
[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         /* store even more data for second k loop */
316         exchange->g[kcount]=g;
317         exchange->dg[kcount]=dg;
318         exchange->d_ik[kcount]=d_ik;
319         exchange->cos_theta[kcount]=cos_theta;
320         exchange->f_c_ik[kcount]=f_c_ik;
321         exchange->df_c_ik[kcount]=df_c_ik;
322
323         /* increase k counter */
324         exchange->kcount++;
325
326         return 0;
327 }
328
329 int tersoff_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
330
331         t_tersoff_mult_params *params;
332         t_tersoff_exchange *exchange;
333         t_3dvec force;
334         double f_a,df_a,b,db,f_c,df_c;
335         double f_r,df_r;
336         double scale;
337         double mu,B,chi;
338         double lambda,A;
339         double d_ij;
340         unsigned char brand;
341         double ni,tmp;
342         double S,R,s_r,arg;
343
344         params=moldyn->pot_params;
345         exchange=&(params->exchange);
346
347         brand=aj->brand;
348         if(brand==ai->brand) {
349                 S=params->S[brand];
350                 R=params->R[brand];
351                 B=params->B[brand];
352                 A=params->A[brand];
353                 mu=params->mu[brand];
354                 lambda=params->lambda[brand];
355                 chi=1.0;
356         }
357         else {
358                 S=params->Smixed;
359                 R=params->Rmixed;
360                 B=params->Bmixed;
361                 A=params->Amixed;
362                 mu=params->mu_m;
363                 lambda=params->lambda_m;
364                 chi=params->chi;
365         }
366
367         d_ij=exchange->d_ij;
368
369         /* f_c, df_c */
370         if(d_ij<R) {
371                 f_c=1.0;
372                 df_c=0.0;
373         }
374         else {
375                 s_r=S-R;
376                 arg=M_PI*(d_ij-R)/s_r;
377                 f_c=0.5+0.5*cos(arg);
378                 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
379         }
380
381         /* f_a, df_a */
382         f_a=-B*exp(-mu*d_ij);
383         df_a=mu*f_a/d_ij;
384
385         /* f_r, df_r */
386         f_r=A*exp(-lambda*d_ij);
387         df_r=lambda*f_r/d_ij;
388
389         /* b, db */
390         if(exchange->zeta_ij==0.0) {
391                 b=chi;
392                 db=0.0;
393         }
394         else {
395                 ni=*(exchange->n_i);
396                 tmp=exchange->betaini*pow(exchange->zeta_ij,ni-1.0);
397                 b=(1.0+exchange->zeta_ij*tmp);
398                 db=chi*pow(b,-1.0/(2.0*ni)-1.0);
399                 b=db*b;
400                 db*=-0.5*tmp;
401         }
402
403         /* force contribution */
404         scale=-0.5*(f_c*(df_r+b*df_a)+df_c*(f_r+b*df_a));
405         v3_scale(&force,&(exchange->dist_ij),scale);
406         v3_add(&(ai->f),&(ai->f),&force);
407         v3_sub(&(aj->f),&(aj->f),&force); // dri rij = - drj rij
408
409 #ifdef DEBUG
410         if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
411                 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,aj->tag);
412                 printf("adding %f %f %f\n",force.x,force.y,force.z);
413                 if(ai==&(moldyn->atom[0]))
414                         printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
415                 if(aj==&(moldyn->atom[0]))
416                         printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
417         }
418 #endif
419
420         /* virial */
421         virial_calc(ai,&force,&(exchange->dist_ij));
422
423         /* dzeta prefactor = - 0.5 f_c f_a db */
424         exchange->pre_dzeta=-0.5*f_a*f_c*db;
425
426         /* energy contribution */
427         moldyn->energy+=0.5*f_c*(b*f_a+f_r);
428
429         /* reset k counter for second k loop */
430         exchange->kcount=0;
431                 
432         return 0;
433 }
434
435 /* tersoff 3 body potential function (second k loop) */
436 int tersoff_mult_3bp_k2(t_moldyn *moldyn,
437                         t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
438
439         t_tersoff_mult_params *params;
440         t_tersoff_exchange *exchange;
441         int kcount;
442         t_3dvec dist_ik,dist_ij;
443         double d_ik2,d_ik,d_ij2,d_ij;
444         unsigned char brand;
445         double S2;
446         double g,dg,cos_theta;
447         double pre_dzeta;
448         double f_c_ik,df_c_ik;
449         double dijdik_inv,fcdg,dfcg;
450         t_3dvec dcosdri,dcosdrj,dcosdrk;
451         t_3dvec force,tmp;
452
453         params=moldyn->pot_params;
454         exchange=&(params->exchange);
455         kcount=exchange->kcount;
456
457         if(kcount>TERSOFF_MAXN)
458                 printf("FATAL: neighbours!\n");
459
460         /* d_ik2 */
461         d_ik2=exchange->d_ik2[kcount];
462
463         brand=ak->brand;
464         if(brand==ai->brand)
465                 S2=params->S2[brand];
466         else
467                 S2=params->S2mixed;
468
469         /* return if d_ik > S */
470         if(d_ik2>S2) {
471                 exchange->kcount++;
472                 return 0;
473         }
474
475         /* prefactor dzeta */
476         pre_dzeta=exchange->pre_dzeta;
477
478         /* dist_ik, d_ik */
479         dist_ik=exchange->dist_ik[kcount];
480         d_ik=exchange->d_ik[kcount];
481
482         /* f_c_ik, df_c_ik */
483         f_c_ik=exchange->f_c_ik[kcount];
484         df_c_ik=exchange->df_c_ik[kcount];
485
486         /* dist_ij, d_ij2, d_ij */
487         dist_ij=exchange->dist_ij;
488         d_ij2=exchange->d_ij2;
489         d_ij=exchange->d_ij;
490
491         /* g, dg, cos_theta */
492         g=exchange->g[kcount];
493         dg=exchange->dg[kcount];
494         cos_theta=exchange->cos_theta[kcount];
495
496         /* cos_theta derivatives wrt i,j,k */
497         dijdik_inv=1.0/(d_ij*d_ik);
498         v3_scale(&dcosdrj,&dist_ik,dijdik_inv);
499         v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
500         v3_add(&dcosdrj,&dcosdrj,&tmp);
501         v3_scale(&dcosdrk,&dist_ij,dijdik_inv);
502         v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
503         v3_add(&dcosdrk,&dcosdrk,&tmp);
504         v3_add(&dcosdri,&dcosdrj,&dcosdrk);
505         v3_scale(&dcosdri,&dcosdri,-1.0);
506
507         /* f_c_ik * dg, df_c_ik * g */
508         fcdg=f_c_ik*dg;
509         dfcg=df_c_ik*g;
510
511         /* derivative wrt i */
512         v3_scale(&force,&dist_ik,dfcg);
513         v3_scale(&tmp,&dcosdri,fcdg);
514         v3_add(&force,&force,&tmp);
515         v3_scale(&force,&force,pre_dzeta);
516
517         /* force contribution */
518         v3_add(&(ai->f),&(ai->f),&force);
519         
520 #ifdef DEBUG
521         if(ai==&(moldyn->atom[0])) {
522                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
523                 printf("adding %f %f %f\n",force.x,force.y,force.z);
524                 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
525         }
526 #endif
527
528         /* virial */
529         //virial_calc(ai,&force,&dist_ij);
530
531         /* derivative wrt j */
532         v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
533
534         /* force contribution */
535         v3_add(&(aj->f),&(aj->f),&force);
536
537 #ifdef DEBUG
538         if(aj==&(moldyn->atom[0])) {
539                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
540                 printf("adding %f %f %f\n",force.x,force.y,force.z);
541                 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
542         }
543 #endif
544
545         /* virial */
546         //v3_scale(&force,&force,-1.0);
547         virial_calc(ai,&force,&dist_ij);
548
549         /* derivative wrt k */
550         v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
551         v3_scale(&tmp,&dcosdrk,fcdg);
552         v3_add(&force,&force,&tmp);
553         v3_scale(&force,&force,pre_dzeta);
554
555         /* force contribution */
556         v3_add(&(ak->f),&(ak->f),&force);
557
558 #ifdef DEBUG
559         if(ak==&(moldyn->atom[0])) {
560                 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
561                 printf("adding %f %f %f\n",force.x,force.y,force.z);
562                 printf("total k: %f %f %f\n",ak->f.x,ak->f.y,ak->f.z);
563         }
564 #endif
565
566         /* virial */
567         //v3_scale(&force,&force,-1.0);
568         virial_calc(ai,&force,&dist_ik);
569         
570         /* increase k counter */
571         exchange->kcount++;     
572
573         return 0;
574
575 }