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 printf("WARNING! - tersoff_mult_2bp is obsolete.\n");
98 printf("WARNING! - repulsive part handled in 3bp/j2 routine.\n");
100 /* use newtons third law */
103 params=moldyn->pot_params;
106 /* determine cutoff square */
108 S2=params->S2[brand];
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);
117 /* if d_ij2 > S2 => no force & potential energy contribution */
122 /* now we will need the distance */
126 if(brand==ai->brand) {
130 lambda=params->lambda[brand];
136 lambda=params->lambda_m;
139 /* f_r_ij, df_r_ij */
140 f_r=A*exp(-lambda*d_ij);
141 df_r=lambda*f_r/d_ij;
147 v3_scale(&force,&dist_ij,-df_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);
158 v3_add(&(ai->f),&(ai->f),&force);
159 v3_sub(&(aj->f),&(aj->f),&force); // reason: dri rij = - drj rij
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);
173 virial_calc(ai,&force,&dist_ij);
175 /* energy 2bp contribution */
176 moldyn->energy+=f_r*f_c;
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) {
184 t_tersoff_mult_params *params;
185 t_tersoff_exchange *exchange;
191 params=moldyn->pot_params;
192 exchange=&(params->exchange);
195 exchange->zeta_ij=0.0;
198 * set ij depending values
204 S2=params->S2[brand];
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);
213 /* if d_ij2 > S2 => no force & potential energy contribution */
223 exchange->dist_ij=dist_ij;
224 exchange->d_ij2=d_ij2;
227 /* reset k counter for first k loop */
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) {
237 t_tersoff_mult_params *params;
238 t_tersoff_exchange *exchange;
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;
247 params=moldyn->pot_params;
248 exchange=&(params->exchange);
249 kcount=exchange->kcount;
251 if(kcount>TERSOFF_MAXN) {
252 printf("FATAL: neighbours = %d\n",kcount);
253 printf(" -> %d %d %d\n",ai->tag,aj->tag,ak->tag);
258 if(brand==ak->brand) {
261 S2=params->S2[brand];
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);
274 /* store data for second k loop */
275 exchange->dist_ik[kcount]=dist_ik;
276 exchange->d_ik2[kcount]=d_ik2;
278 /* return if not within cutoff */
288 dist_ij=exchange->dist_ij;
292 cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
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;
301 /* zeta sum += f_c_ik * g_ijk */
303 exchange->zeta_ij+=g;
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;
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);
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;
331 /* increase k counter */
337 int tersoff_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
339 t_tersoff_mult_params *params;
340 t_tersoff_exchange *exchange;
342 double f_a,df_a,b,db,f_c,df_c;
352 params=moldyn->pot_params;
353 exchange=&(params->exchange);
356 if(brand==ai->brand) {
361 mu=params->mu[brand];
362 lambda=params->lambda[brand];
371 lambda=params->lambda_m;
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));
390 f_a=-B*exp(-mu*d_ij);
394 f_r=A*exp(-lambda*d_ij);
395 df_r=lambda*f_r/d_ij;
398 if(exchange->zeta_ij==0.0) {
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);
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
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),
427 printf(" %f %f %f\n",exchange->zeta_ij,.0,.0);
433 virial_calc(ai,&force,&(exchange->dist_ij));
435 /* dzeta prefactor = - 0.5 f_c f_a db */
436 exchange->pre_dzeta=-0.5*f_a*f_c*db;
438 /* energy contribution */
439 moldyn->energy+=0.5*f_c*(b*f_a+f_r);
441 /* reset k counter for second k loop */
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) {
451 t_tersoff_mult_params *params;
452 t_tersoff_exchange *exchange;
454 t_3dvec dist_ik,dist_ij;
455 double d_ik2,d_ik,d_ij2,d_ij;
458 double g,dg,cos_theta;
460 double f_c_ik,df_c_ik;
461 double dijdik_inv,fcdg,dfcg;
462 t_3dvec dcosdri,dcosdrj,dcosdrk;
465 params=moldyn->pot_params;
466 exchange=&(params->exchange);
467 kcount=exchange->kcount;
469 if(kcount>TERSOFF_MAXN)
470 printf("FATAL: neighbours!\n");
473 d_ik2=exchange->d_ik2[kcount];
477 S2=params->S2[brand];
481 /* return if d_ik > S */
487 /* prefactor dzeta */
488 pre_dzeta=exchange->pre_dzeta;
491 dist_ik=exchange->dist_ik[kcount];
492 d_ik=exchange->d_ik[kcount];
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];
498 /* dist_ij, d_ij2, d_ij */
499 dist_ij=exchange->dist_ij;
500 d_ij2=exchange->d_ij2;
503 /* g, dg, cos_theta */
504 g=exchange->g[kcount];
505 dg=exchange->dg[kcount];
506 cos_theta=exchange->cos_theta[kcount];
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);
519 /* f_c_ik * dg, df_c_ik * g */
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);
529 /* force contribution */
530 v3_add(&(ai->f),&(ai->f),&force);
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);
541 //virial_calc(ai,&force,&dist_ij);
543 /* derivative wrt j */
544 v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
546 /* force contribution */
547 v3_add(&(aj->f),&(aj->f),&force);
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);
558 //v3_scale(&force,&force,-1.0);
560 virial_calc(ai,&force,&dist_ij);
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);
568 /* force contribution */
569 v3_add(&(ak->f),&(ak->f),&force);
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);
580 //v3_scale(&force,&force,-1.0);
582 virial_calc(ai,&force,&dist_ik);
584 /* increase k counter */