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_tersoff_exchange *exchange;
89 t_3dvec dist_ij,force;
91 double A,B,R,S,S2,lambda,mu;
98 /* use newtons third law */
101 params=moldyn->pot_params;
103 exchange=&(params->exchange);
105 /* clear 3bp and 2bp post run */
107 exchange->run2bp_post=0;
109 /* reset S > r > R mark */
110 exchange->d_ij_between_rs=0;
113 * calc of 2bp contribution of V_ij and dV_ij/ji
115 * for Vij and dV_ij we need:
120 * - f_c_ji = f_c_ij, df_c_ji = df_c_ij
121 * - f_r_ji = f_r_ij; df_r_ji = df_r_ij
125 /* determine cutoff square */
127 S2=params->S2[brand];
132 v3_sub(&dist_ij,&(aj->r),&(ai->r));
133 if(bc) check_per_bound(moldyn,&dist_ij);
134 d_ij2=v3_absolute_square(&dist_ij);
136 /* if d_ij2 > S2 => no force & potential energy contribution */
140 /* now we will need the distance */
141 //d_ij=v3_norm(&dist_ij);
144 /* save for use in 3bp */
146 exchange->d_ij2=d_ij2;
147 exchange->dist_ij=dist_ij;
150 exchange->beta_j=&(params->beta[brand]);
151 exchange->n_j=&(params->n[brand]);
152 exchange->c_j=&(params->c[brand]);
153 exchange->d_j=&(params->d[brand]);
154 exchange->h_j=&(params->h[brand]);
155 if(brand==ai->brand) {
160 lambda=params->lambda[brand];
161 mu=params->mu[brand];
163 exchange->betajnj=exchange->betaini;
164 exchange->cj2=exchange->ci2;
165 exchange->dj2=exchange->di2;
166 exchange->cj2dj2=exchange->ci2di2;
173 lambda=params->lambda_m;
175 params->exchange.chi=params->chi;
176 exchange->betajnj=pow(*(exchange->beta_j),*(exchange->n_j));
177 exchange->cj2=params->c[brand]*params->c[brand];
178 exchange->dj2=params->d[brand]*params->d[brand];
179 exchange->cj2dj2=exchange->cj2/exchange->dj2;
182 /* f_r_ij = f_r_ji, df_r_ij = df_r_ji */
183 f_r=A*exp(-lambda*d_ij);
184 df_r=lambda*f_r/d_ij;
186 /* f_a, df_a calc (again, same for ij and ji) | save for later use! */
187 exchange->f_a=-B*exp(-mu*d_ij);
188 exchange->df_a=mu*exchange->f_a/d_ij;
190 /* f_c, df_c calc (again, same for ij and ji) */
192 /* f_c = 1, df_c = 0 */
195 /* two body contribution (ij, ji) */
196 v3_scale(&force,&dist_ij,-df_r);
200 arg=M_PI*(d_ij-R)/s_r;
201 f_c=0.5+0.5*cos(arg);
202 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
203 /* two body contribution (ij, ji) */
204 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
205 /* tell 3bp that S > r > R */
206 exchange->d_ij_between_rs=1;
209 /* add forces of 2bp (ij, ji) contribution
210 * dVij = dVji and we sum up both: no 1/2) */
211 v3_add(&(ai->f),&(ai->f),&force);
214 virial_calc(ai,&force,&dist_ij);
215 //ai->virial.xx-=force.x*dist_ij.x;
216 //ai->virial.yy-=force.y*dist_ij.y;
217 //ai->virial.zz-=force.z*dist_ij.z;
218 //ai->virial.xy-=force.x*dist_ij.y;
219 //ai->virial.xz-=force.x*dist_ij.z;
220 //ai->virial.yz-=force.y*dist_ij.z;
223 if(ai==&(moldyn->atom[0])) {
224 printf("dVij, dVji (2bp) contrib:\n");
225 printf("%f | %f\n",force.x,ai->f.x);
226 printf("%f | %f\n",force.y,ai->f.y);
227 printf("%f | %f\n",force.z,ai->f.z);
231 if(ai==&(moldyn->atom[0])) {
232 printf("dVij, dVji (2bp) contrib:\n");
233 printf("%f | %f\n",force.x*dist_ij.x,ai->virial.xx);
234 printf("%f | %f\n",force.y*dist_ij.y,ai->virial.yy);
235 printf("%f | %f\n",force.z*dist_ij.z,ai->virial.zz);
239 /* energy 2bp contribution (ij, ji) is 0.5 f_r f_c ... */
240 moldyn->energy+=(0.5*f_r*f_c);
242 /* save for use in 3bp */
246 /* enable the run of 3bp function and 2bp post processing */
248 exchange->run2bp_post=1;
251 exchange->zeta_ij=0.0;
252 exchange->zeta_ji=0.0;
253 v3_zero(&(exchange->dzeta_ij));
254 v3_zero(&(exchange->dzeta_ji));
259 /* tersoff 2 body post part */
261 int tersoff_mult_post_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
264 * here we have to allow for the 3bp sums
267 * - zeta_ij, dzeta_ij
268 * - zeta_ji, dzeta_ji
270 * to compute the 3bp contribution to:
276 t_tersoff_mult_params *params;
277 t_tersoff_exchange *exchange;
282 double f_c,df_c,f_a,df_a;
283 double chi,ni,betaini,nj,betajnj;
286 params=moldyn->pot_params;
287 exchange=&(params->exchange);
289 /* we do not run if f_c_ij was detected to be 0! */
290 if(!(exchange->run2bp_post))
297 betaini=exchange->betaini;
298 betajnj=exchange->betajnj;
302 dist_ij=&(exchange->dist_ij);
305 zeta=exchange->zeta_ij;
307 moldyn->debug++; /* just for debugging ... */
309 v3_scale(&force,dist_ij,df_a*b*f_c);
312 tmp=betaini*pow(zeta,ni-1.0); /* beta^n * zeta^n-1 */
313 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
314 db=chi*pow(b,-1.0/(2*ni)-1); /* x(...)^(-1/2n - 1) */
316 db*=-0.5*tmp; /* db_ij */
317 v3_scale(&force,&(exchange->dzeta_ij),f_a*db);
318 v3_scale(&temp,dist_ij,df_a*b);
319 v3_add(&force,&force,&temp);
320 v3_scale(&force,&force,f_c);
322 v3_scale(&temp,dist_ij,df_c*b*f_a);
323 v3_add(&force,&force,&temp);
324 v3_scale(&force,&force,-0.5);
327 v3_add(&(ai->f),&(ai->f),&force);
330 virial_calc(ai,&force,dist_ij);
331 //ai->virial.xx-=force.x*dist_ij->x;
332 //ai->virial.yy-=force.y*dist_ij->y;
333 //ai->virial.zz-=force.z*dist_ij->z;
334 //ai->virial.xy-=force.x*dist_ij->y;
335 //ai->virial.xz-=force.x*dist_ij->z;
336 //ai->virial.yz-=force.y*dist_ij->z;
339 if(ai==&(moldyn->atom[0])) {
340 printf("dVij (3bp) contrib:\n");
341 printf("%f | %f\n",force.x,ai->f.x);
342 printf("%f | %f\n",force.y,ai->f.y);
343 printf("%f | %f\n",force.z,ai->f.z);
347 if(ai==&(moldyn->atom[0])) {
348 printf("dVij (3bp) contrib:\n");
349 printf("%f | %f\n",force.x*dist_ij->x,ai->virial.xx);
350 printf("%f | %f\n",force.y*dist_ij->y,ai->virial.yy);
351 printf("%f | %f\n",force.z*dist_ij->z,ai->virial.zz);
355 /* add energy of 3bp sum */
356 moldyn->energy+=(0.5*f_c*b*f_a);
359 zeta=exchange->zeta_ji;
363 v3_scale(&force,dist_ij,df_a*b*f_c);
366 tmp=betajnj*pow(zeta,nj-1.0); /* beta^n * zeta^n-1 */
367 b=(1+zeta*tmp); /* 1 + beta^n zeta^n */
368 db=chi*pow(b,-1.0/(2*nj)-1); /* x(...)^(-1/2n - 1) */
370 db*=-0.5*tmp; /* db_ij */
371 v3_scale(&force,&(exchange->dzeta_ji),f_a*db);
372 v3_scale(&temp,dist_ij,df_a*b);
373 v3_add(&force,&force,&temp);
374 v3_scale(&force,&force,f_c);
376 v3_scale(&temp,dist_ij,df_c*b*f_a);
377 v3_add(&force,&force,&temp);
378 v3_scale(&force,&force,-0.5);
381 v3_add(&(ai->f),&(ai->f),&force);
383 /* virial - plus sign, as dist_ij = - dist_ji - (really??) */
384 // TEST ... with a minus instead
385 virial_calc(ai,&force,dist_ij);
386 //ai->virial.xx-=force.x*dist_ij->x;
387 //ai->virial.yy-=force.y*dist_ij->y;
388 //ai->virial.zz-=force.z*dist_ij->z;
389 //ai->virial.xy-=force.x*dist_ij->y;
390 //ai->virial.xz-=force.x*dist_ij->z;
391 //ai->virial.yz-=force.y*dist_ij->z;
394 if(ai==&(moldyn->atom[0])) {
395 printf("dVji (3bp) contrib:\n");
396 printf("%f | %f\n",force.x,ai->f.x);
397 printf("%f | %f\n",force.y,ai->f.y);
398 printf("%f | %f\n",force.z,ai->f.z);
402 if(ai==&(moldyn->atom[0])) {
403 printf("dVji (3bp) contrib:\n");
404 printf("%f | %f\n",force.x*dist_ij->x,ai->virial.xx);
405 printf("%f | %f\n",force.y*dist_ij->y,ai->virial.yy);
406 printf("%f | %f\n",force.z*dist_ij->z,ai->virial.zz);
413 /* tersoff 3 body part */
415 int tersoff_mult_3bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
417 t_tersoff_mult_params *params;
418 t_tersoff_exchange *exchange;
419 t_3dvec dist_ij,dist_ik,dist_jk;
424 double d_ij,d_ik,d_jk,d_ij2,d_ik2,d_jk2;
427 double f_c_ik,df_c_ik,arg;
431 double cos_theta,d_costheta1,d_costheta2;
432 double h_cos,d2_h_cos2;
433 double frac,g,zeta,chi;
437 params=moldyn->pot_params;
438 exchange=&(params->exchange);
440 if(!(exchange->run3bp))
444 * calc of 3bp contribution of V_ij and dV_ij/ji/jk &
445 * 2bp contribution of dV_jk
447 * for Vij and dV_ij we still need:
448 * - b_ij, db_ij (zeta_ij)
449 * - f_c_ik, df_c_ik, constants_i, cos_theta_ijk, d_costheta_ijk
451 * for dV_ji we still need:
452 * - b_ji, db_ji (zeta_ji)
453 * - f_c_jk, d_c_jk, constants_j, cos_theta_jik, d_costheta_jik
459 * - f_c_ji, df_c_ji, constants_j, cos_theta_jki, d_costheta_jki
467 /* dist_ij, d_ij - this is < S_ij ! */
468 dist_ij=exchange->dist_ij;
470 d_ij2=exchange->d_ij2;
472 /* f_c_ij, df_c_ij (same for ji) */
477 * calculate unknown values now ...
480 /* V_ij and dV_ij stuff (in b_ij there is f_c_ik) */
483 v3_sub(&dist_ik,&(ak->r),&(ai->r));
484 if(bc) check_per_bound(moldyn,&dist_ik);
485 d_ik2=v3_absolute_square(&dist_ik);
489 if(brand==ak->brand) {
492 S2=params->S2[brand];
500 /* zeta_ij/dzeta_ij contribution only for d_ik < S */
503 /* now we need d_ik */
506 /* get constants_i from exchange data */
513 c2d2=exchange->ci2di2;
515 /* cosine of theta_ijk by scalaproduct */
516 rr=v3_scalar_product(&dist_ij,&dist_ik);
522 d_costheta1=cos_theta/d_ij2-tmp;
523 d_costheta2=cos_theta/d_ik2-tmp;
525 /* some usefull values */
527 d2_h_cos2=d2+(h_cos*h_cos);
533 /* d_costheta_ij and dg(cos_theta) - needed in any case! */
534 v3_scale(&temp1,&dist_ij,d_costheta1);
535 v3_scale(&temp2,&dist_ik,d_costheta2);
536 v3_add(&temp1,&temp1,&temp2);
537 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
539 /* f_c_ik & df_c_ik + {d,}zeta contribution */
540 dzeta=&(exchange->dzeta_ij);
544 // => df_c_ik=0.0; of course we do not set this!
547 exchange->zeta_ij+=g;
550 v3_add(dzeta,dzeta,&temp1);
555 arg=M_PI*(d_ik-R)/s_r;
556 f_c_ik=0.5+0.5*cos(arg);
557 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
560 exchange->zeta_ij+=f_c_ik*g;
563 v3_scale(&temp1,&temp1,f_c_ik);
564 v3_scale(&temp2,&dist_ik,g*df_c_ik);
565 v3_add(&temp1,&temp1,&temp2);
566 v3_add(dzeta,dzeta,&temp1);
570 /* dV_ji stuff (in b_ji there is f_c_jk) + dV_jk stuff! */
573 v3_sub(&dist_jk,&(ak->r),&(aj->r));
574 if(bc) check_per_bound(moldyn,&dist_jk);
575 d_jk2=v3_absolute_square(&dist_jk);
579 if(brand==ak->brand) {
582 S2=params->S2[brand];
584 mu=params->mu[brand];
596 /* zeta_ji/dzeta_ji contribution only for d_jk < S_jk */
599 /* now we need d_ik */
602 /* constants_j from exchange data */
609 c2d2=exchange->cj2dj2;
611 /* cosine of theta_jik by scalaproduct */
612 rr=-v3_scalar_product(&dist_ij,&dist_jk); /* -1, as ij -> ji */
618 d_costheta2=cos_theta/d_ij2;
620 /* some usefull values */
622 d2_h_cos2=d2+(h_cos*h_cos);
628 /* d_costheta_jik and dg(cos_theta) - needed in any case! */
629 v3_scale(&temp1,&dist_jk,d_costheta1);
630 v3_scale(&temp2,&dist_ij,-d_costheta2); /* ji -> ij => -1 */
631 //v3_add(&temp1,&temp1,&temp2);
632 v3_sub(&temp1,&temp1,&temp2); /* there is a minus! */
633 v3_scale(&temp1,&temp1,-2.0*frac*h_cos/d2_h_cos2); /* dg */
635 /* store dg in temp2 and use it for dVjk later */
636 v3_copy(&temp2,&temp1);
638 /* f_c_jk + {d,}zeta contribution (df_c_jk = 0) */
639 dzeta=&(exchange->dzeta_ji);
645 exchange->zeta_ji+=g;
648 v3_add(dzeta,dzeta,&temp1);
653 arg=M_PI*(d_jk-R)/s_r;
654 f_c_jk=0.5+0.5*cos(arg);
657 exchange->zeta_ji+=f_c_jk*g;
660 v3_scale(&temp1,&temp1,f_c_jk);
661 v3_add(dzeta,dzeta,&temp1);
664 /* dV_jk stuff | add force contribution on atom i immediately */
665 if(exchange->d_ij_between_rs) {
667 v3_scale(&temp1,&temp2,f_c);
668 v3_scale(&temp2,&dist_ij,df_c*g);
669 v3_add(&temp2,&temp2,&temp1); /* -> dzeta_jk in temp2 */
673 // dzeta_jk is simply dg, which is stored in temp2
675 /* betajnj * zeta_jk ^ nj-1 */
676 tmp=exchange->betajnj*pow(zeta,(n-1.0));
677 tmp=-chi/2.0*pow((1+tmp*zeta),(-1.0/(2.0*n)-1))*tmp;
678 v3_scale(&temp2,&temp2,tmp*B*exp(-mu*d_jk)*f_c_jk*0.5);
679 v3_add(&(ai->f),&(ai->f),&temp2); /* -1 skipped in f_a calc ^ */
680 /* scaled with 0.5 ^ */
683 ai->virial.xx-=temp2.x*dist_jk.x;
684 ai->virial.yy-=temp2.y*dist_jk.y;
685 ai->virial.zz-=temp2.z*dist_jk.z;
686 ai->virial.xy-=temp2.x*dist_jk.y;
687 ai->virial.xz-=temp2.x*dist_jk.z;
688 ai->virial.yz-=temp2.y*dist_jk.z;
691 if(ai==&(moldyn->atom[0])) {
692 printf("dVjk (3bp) contrib:\n");
693 printf("%f | %f\n",temp2.x,ai->f.x);
694 printf("%f | %f\n",temp2.y,ai->f.y);
695 printf("%f | %f\n",temp2.z,ai->f.z);
699 if(ai==&(moldyn->atom[0])) {
700 printf("dVjk (3bp) contrib:\n");
701 printf("%f | %f\n",temp2.x*dist_jk.x,ai->virial.xx);
702 printf("%f | %f\n",temp2.y*dist_jk.y,ai->virial.yy);
703 printf("%f | %f\n",temp2.z*dist_jk.z,ai->virial.zz);