2 * tersoff.c - tersoff potential
4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
18 #include "../moldyn.h"
19 #include "../math/math.h"
22 /* create mixed terms from parameters and set them */
23 int tersoff_mult_complete_params(t_tersoff_mult_params *p) {
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]);
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],
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);
54 /* tersoff 1 body part */
55 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
58 t_tersoff_mult_params *params;
59 t_tersoff_exchange *exchange;
62 params=moldyn->pot_params;
63 exchange=&(params->exchange);
66 * simple: point constant parameters only depending on atom i to
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]);
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;
84 /* tersoff 2 body part */
85 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
87 t_tersoff_mult_params *params;
88 t_3dvec dist_ij,force;
90 double A,R,S,S2,lambda;
97 /* use newtons third law */
100 params=moldyn->pot_params;
103 /* determine cutoff square */
105 S2=params->S2[brand];
110 v3_sub(&dist_ij,&(aj->r),&(ai->r));
111 if(bc) check_per_bound(moldyn,&dist_ij);
112 d_ij2=v3_absolute_square(&dist_ij);
114 /* if d_ij2 > S2 => no force & potential energy contribution */
119 /* now we will need the distance */
123 if(brand==ai->brand) {
127 lambda=params->lambda[brand];
133 lambda=params->lambda_m;
136 /* f_r_ij, df_r_ij */
137 f_r=A*exp(-lambda*d_ij);
138 df_r=lambda*f_r/d_ij;
144 v3_scale(&force,&dist_ij,-df_r);
148 arg=M_PI*(d_ij-R)/s_r;
149 f_c=0.5+0.5*cos(arg);
150 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
151 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
155 v3_add(&(ai->f),&(ai->f),&force);
156 v3_sub(&(aj->f),&(aj->f),&force); // reason: dri rij = - drj rij
159 if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
160 printf("force 2bp: [%d %d]\n",ai->tag,aj->tag);
161 printf("adding %f %f %f\n",force.x,force.y,force.z);
162 if(ai==&(moldyn->atom[0]))
163 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
164 if(aj==&(moldyn->atom[0]))
165 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
170 //virial_calc(ai,&force,&dist_ij);
171 //virial_calc(aj,&force,&dist_ij);
172 //ai->virial.xx-=force.x*dist_ij.x;
173 //ai->virial.yy-=force.y*dist_ij.y;
174 //ai->virial.zz-=force.z*dist_ij.z;
175 //ai->virial.xy-=force.x*dist_ij.y;
176 //ai->virial.xz-=force.x*dist_ij.z;
177 //ai->virial.yz-=force.y*dist_ij.z;
179 /* energy 2bp contribution */
180 moldyn->energy+=f_r*f_c;
185 /* tersoff 3 body potential function (first ij loop) */
186 int tersoff_mult_3bp_j1(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
188 t_tersoff_mult_params *params;
189 t_tersoff_exchange *exchange;
195 params=moldyn->pot_params;
196 exchange=&(params->exchange);
199 exchange->zeta_ij=0.0;
202 * set ij depending values
208 S2=params->S2[brand];
213 v3_sub(&dist_ij,&(aj->r),&(ai->r));
214 if(bc) check_per_bound(moldyn,&dist_ij);
215 d_ij2=v3_absolute_square(&dist_ij);
217 /* if d_ij2 > S2 => no force & potential energy contribution */
227 exchange->dist_ij=dist_ij;
228 exchange->d_ij2=d_ij2;
231 /* reset k counter for first k loop */
237 /* tersoff 3 body potential function (first k loop) */
238 int tersoff_mult_3bp_k1(t_moldyn *moldyn,
239 t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
241 t_tersoff_mult_params *params;
242 t_tersoff_exchange *exchange;
245 t_3dvec dist_ij,dist_ik;
246 double d_ik2,d_ik,d_ij;
247 double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
248 double f_c_ik,df_c_ik;
251 params=moldyn->pot_params;
252 exchange=&(params->exchange);
253 kcount=exchange->kcount;
255 if(kcount>TERSOFF_MAXN) {
256 printf("FATAL: neighbours = %d\n",kcount);
257 printf(" -> %d %d %d\n",ai->tag,aj->tag,ak->tag);
262 if(brand==ak->brand) {
265 S2=params->S2[brand];
274 v3_sub(&dist_ik,&(ak->r),&(ai->r));
275 if(bc) check_per_bound(moldyn,&dist_ik);
276 d_ik2=v3_absolute_square(&dist_ik);
278 /* store data for second k loop */
279 exchange->dist_ik[kcount]=dist_ik;
280 exchange->d_ik2[kcount]=d_ik2;
282 /* return if not within cutoff */
292 dist_ij=exchange->dist_ij;
296 cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
299 h_cos=*(exchange->h_i)-cos_theta;
300 d2_h_cos2=exchange->di2+(h_cos*h_cos);
301 frac=exchange->ci2/d2_h_cos2;
302 g=1.0+exchange->ci2di2-frac;
303 dg=-2.0*frac*h_cos/d2_h_cos2;
305 /* zeta sum += f_c_ik * g_ijk */
307 exchange->zeta_ij+=g;
313 arg=M_PI*(d_ik-R)/s_r;
314 f_c_ik=0.5+0.5*cos(arg);
315 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
316 exchange->zeta_ij+=f_c_ik*g;
319 /* store even more data for second k loop */
320 exchange->g[kcount]=g;
321 exchange->dg[kcount]=dg;
322 exchange->d_ik[kcount]=d_ik;
323 exchange->cos_theta[kcount]=cos_theta;
324 exchange->f_c_ik[kcount]=f_c_ik;
325 exchange->df_c_ik[kcount]=df_c_ik;
327 /* increase k counter */
333 int tersoff_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
335 t_tersoff_mult_params *params;
336 t_tersoff_exchange *exchange;
338 double f_a,df_a,b,db,f_c,df_c;
345 params=moldyn->pot_params;
346 exchange=&(params->exchange);
349 if(brand==ai->brand) {
353 mu=params->mu[brand];
373 arg=M_PI*(d_ij-R)/s_r;
374 f_c=0.5+0.5*cos(arg);
375 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
379 f_a=-B*exp(-mu*d_ij);
383 if(exchange->zeta_ij==0.0) {
389 tmp=exchange->betaini*pow(exchange->zeta_ij,ni-1.0);
390 b=(1.0+exchange->zeta_ij*tmp);
391 db=chi*pow(b,-1.0/(2.0*ni)-1.0);
396 /* force contribution */
397 v3_scale(&force,&(exchange->dist_ij),df_a*f_c+f_a*df_c);
398 v3_scale(&force,&force,-0.5*b);
399 v3_add(&(ai->f),&(ai->f),&force);
400 v3_sub(&(aj->f),&(aj->f),&force); // dri rij = - drj rij
403 if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
404 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,aj->tag);
405 printf("adding %f %f %f\n",force.x,force.y,force.z);
406 if(ai==&(moldyn->atom[0]))
407 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
408 if(aj==&(moldyn->atom[0]))
409 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
414 //virial_calc(ai,&force,&(exchange->dist_ij));
415 //virial_calc(aj,&force,&(exchange->dist_ij));
417 /* dzeta prefactor = - 0.5 f_c f_a db */
418 exchange->pre_dzeta=-0.5*f_a*f_c*db;
420 /* energy contribution */
421 moldyn->energy+=0.5*f_c*b*f_a;
423 /* reset k counter for second k loop */
429 /* tersoff 3 body potential function (second k loop) */
430 int tersoff_mult_3bp_k2(t_moldyn *moldyn,
431 t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
433 t_tersoff_mult_params *params;
434 t_tersoff_exchange *exchange;
436 t_3dvec dist_ik,dist_ij;
437 double d_ik2,d_ik,d_ij2,d_ij;
440 double g,dg,cos_theta;
442 double f_c_ik,df_c_ik;
443 double dijdik_inv,fcdg,dfcg;
444 t_3dvec dcosdri,dcosdrj,dcosdrk;
447 params=moldyn->pot_params;
448 exchange=&(params->exchange);
449 kcount=exchange->kcount;
451 if(kcount>TERSOFF_MAXN)
452 printf("FATAL: neighbours!\n");
455 d_ik2=exchange->d_ik2[kcount];
459 S2=params->S2[brand];
463 /* return if d_ik > S */
469 /* prefactor dzeta */
470 pre_dzeta=exchange->pre_dzeta;
473 dist_ik=exchange->dist_ik[kcount];
474 d_ik=exchange->d_ik[kcount];
476 /* f_c_ik, df_c_ik */
477 f_c_ik=exchange->f_c_ik[kcount];
478 df_c_ik=exchange->df_c_ik[kcount];
480 /* dist_ij, d_ij2, d_ij */
481 dist_ij=exchange->dist_ij;
482 d_ij2=exchange->d_ij2;
485 /* g, dg, cos_theta */
486 g=exchange->g[kcount];
487 dg=exchange->dg[kcount];
488 cos_theta=exchange->cos_theta[kcount];
490 /* cos_theta derivatives wrt i,j,k */
491 dijdik_inv=1.0/(d_ij*d_ik);
492 v3_scale(&dcosdrj,&dist_ik,dijdik_inv);
493 v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
494 v3_add(&dcosdrj,&dcosdrj,&tmp);
495 v3_scale(&dcosdrk,&dist_ij,dijdik_inv);
496 v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
497 v3_add(&dcosdrk,&dcosdrk,&tmp);
498 v3_add(&dcosdri,&dcosdrj,&dcosdrk);
499 v3_scale(&dcosdri,&dcosdri,-1.0);
501 /* f_c_ik * dg, df_c_ik * g */
505 /* derivative wrt i */
506 v3_scale(&force,&dist_ik,dfcg);
507 v3_scale(&tmp,&dcosdri,fcdg);
508 v3_add(&force,&force,&tmp);
509 v3_scale(&force,&force,pre_dzeta);
511 /* force contribution */
512 v3_add(&(ai->f),&(ai->f),&force);
515 if(ai==&(moldyn->atom[0])) {
516 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
517 printf("adding %f %f %f\n",force.x,force.y,force.z);
518 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
523 //virial_calc(ai,&force,&dist_ij);
525 /* derivative wrt j */
526 v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
528 /* force contribution */
529 v3_add(&(aj->f),&(aj->f),&force);
532 if(aj==&(moldyn->atom[0])) {
533 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
534 printf("adding %f %f %f\n",force.x,force.y,force.z);
535 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
540 //virial_calc(aj,&force,&dist_ij);
542 /* derivative wrt k */
543 v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rij = - drj rij
544 v3_scale(&tmp,&dcosdrk,fcdg);
545 v3_add(&force,&force,&tmp);
546 v3_scale(&force,&force,pre_dzeta);
548 /* force contribution */
549 v3_add(&(ak->f),&(ak->f),&force);
552 if(ak==&(moldyn->atom[0])) {
553 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
554 printf("adding %f %f %f\n",force.x,force.y,force.z);
555 printf("total k: %f %f %f\n",ak->f.x,ak->f.y,ak->f.z);
560 virial_calc(ak,&force,&dist_ik);
562 /* increase k counter */