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_set_params(t_moldyn *moldyn,int element1,int element2) {
25 t_tersoff_mult_params *p;
27 // set cutoff before parameters (actually only necessary for some pots)
28 if(moldyn->cutoff==0.0) {
29 printf("[tersoff] WARNING: no cutoff!\n");
33 /* alloc mem for potential parameters */
34 moldyn->pot_params=malloc(sizeof(t_tersoff_mult_params));
35 if(moldyn->pot_params==NULL) {
36 perror("[tersoff] pot params alloc");
40 /* these are now tersoff parameters */
43 // only 1 combination by now :p
51 p->lambda[0]=TM_LAMBDA_SI;
53 p->beta[0]=TM_BETA_SI;
63 printf("[tersoff] WARNING: element2\n");
68 printf("[tersoff] WARNING: element1\n");
79 p->lambda[1]=TM_LAMBDA_C;
88 printf("[tersoff] WARNING: element1\n");
92 printf("[tersoff] parameter completion\n");
93 p->S2[0]=p->S[0]*p->S[0];
94 p->S2[1]=p->S[1]*p->S[1];
95 p->Smixed=sqrt(p->S[0]*p->S[1]);
96 p->S2mixed=p->Smixed*p->Smixed;
97 p->Rmixed=sqrt(p->R[0]*p->R[1]);
98 p->Amixed=sqrt(p->A[0]*p->A[1]);
99 p->Bmixed=sqrt(p->B[0]*p->B[1]);
100 p->lambda_m=0.5*(p->lambda[0]+p->lambda[1]);
101 p->mu_m=0.5*(p->mu[0]+p->mu[1]);
103 printf("[tersoff] mult parameter info:\n");
104 printf(" S (A) | %f | %f | %f\n",p->S[0],p->S[1],p->Smixed);
105 printf(" R (A) | %f | %f | %f\n",p->R[0],p->R[1],p->Rmixed);
106 printf(" A (eV) | %f | %f | %f\n",p->A[0]/EV,p->A[1]/EV,p->Amixed/EV);
107 printf(" B (eV) | %f | %f | %f\n",p->B[0]/EV,p->B[1]/EV,p->Bmixed/EV);
108 printf(" lambda | %f | %f | %f\n",p->lambda[0],p->lambda[1],
110 printf(" mu | %f | %f | %f\n",p->mu[0],p->mu[1],p->mu_m);
111 printf(" beta | %.10f | %.10f\n",p->beta[0],p->beta[1]);
112 printf(" n | %f | %f\n",p->n[0],p->n[1]);
113 printf(" c | %f | %f\n",p->c[0],p->c[1]);
114 printf(" d | %f | %f\n",p->d[0],p->d[1]);
115 printf(" h | %f | %f\n",p->h[0],p->h[1]);
116 printf(" chi | %f \n",p->chi);
121 /* tersoff 1 body part */
122 int tersoff_mult_1bp(t_moldyn *moldyn,t_atom *ai) {
125 t_tersoff_mult_params *params;
126 t_tersoff_exchange *exchange;
129 params=moldyn->pot_params;
130 exchange=&(params->exchange);
133 * simple: point constant parameters only depending on atom i to
137 exchange->beta_i=&(params->beta[brand]);
138 exchange->n_i=&(params->n[brand]);
139 exchange->c_i=&(params->c[brand]);
140 exchange->d_i=&(params->d[brand]);
141 exchange->h_i=&(params->h[brand]);
143 exchange->betaini=pow(*(exchange->beta_i),*(exchange->n_i));
144 exchange->ci2=params->c[brand]*params->c[brand];
145 exchange->di2=params->d[brand]*params->d[brand];
146 exchange->ci2di2=exchange->ci2/exchange->di2;
151 /* tersoff 2 body part */
152 int tersoff_mult_2bp(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
154 t_tersoff_mult_params *params;
155 t_3dvec dist_ij,force;
157 double A,R,S,S2,lambda;
165 printf("WARNING! - tersoff_mult_2bp is obsolete.\n");
166 printf("WARNING! - repulsive part handled in 3bp/j2 routine.\n");
168 /* use newtons third law */
171 params=moldyn->pot_params;
174 /* determine cutoff square */
176 S2=params->S2[brand];
181 v3_sub(&dist_ij,&(aj->r),&(ai->r));
182 if(bc) check_per_bound(moldyn,&dist_ij);
183 d_ij2=v3_absolute_square(&dist_ij);
185 /* if d_ij2 > S2 => no force & potential energy contribution */
190 /* now we will need the distance */
194 if(brand==ai->brand) {
198 lambda=params->lambda[brand];
204 lambda=params->lambda_m;
207 /* f_r_ij, df_r_ij */
208 f_r=A*exp(-lambda*d_ij);
209 df_r=lambda*f_r/d_ij;
215 v3_scale(&force,&dist_ij,-df_r);
219 arg=M_PI*(d_ij-R)/s_r;
220 f_c=0.5+0.5*cos(arg);
221 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
222 v3_scale(&force,&dist_ij,-df_c*f_r-df_r*f_c);
226 v3_add(&(ai->f),&(ai->f),&force);
227 v3_scale(&force,&force,-1.0); // reason: dri rij = - drj rij
228 v3_add(&(aj->f),&(aj->f),&force);
231 if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
232 printf("force 2bp: [%d %d]\n",ai->tag,aj->tag);
233 printf("adding %f %f %f\n",force.x,force.y,force.z);
234 if(ai==&(moldyn->atom[0]))
235 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
236 if(aj==&(moldyn->atom[0]))
237 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
242 virial_calc(aj,&force,&dist_ij);
244 /* energy 2bp contribution */
246 moldyn->energy+=energy;
253 /* tersoff 3 body potential function (first ij loop) */
254 int tersoff_mult_3bp_j1(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
256 t_tersoff_mult_params *params;
257 t_tersoff_exchange *exchange;
263 params=moldyn->pot_params;
264 exchange=&(params->exchange);
267 exchange->zeta_ij=0.0;
270 * set ij depending values
276 S2=params->S2[brand];
281 v3_sub(&dist_ij,&(aj->r),&(ai->r));
282 if(bc) check_per_bound(moldyn,&dist_ij);
283 d_ij2=v3_absolute_square(&dist_ij);
285 /* if d_ij2 > S2 => no force & potential energy contribution */
295 exchange->dist_ij=dist_ij;
296 exchange->d_ij2=d_ij2;
299 /* reset k counter for first k loop */
305 /* tersoff 3 body potential function (first k loop) */
306 int tersoff_mult_3bp_k1(t_moldyn *moldyn,
307 t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
309 t_tersoff_mult_params *params;
310 t_tersoff_exchange *exchange;
313 t_3dvec dist_ij,dist_ik;
314 double d_ik2,d_ik,d_ij;
315 double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
316 double f_c_ik,df_c_ik;
319 params=moldyn->pot_params;
320 exchange=&(params->exchange);
321 kcount=exchange->kcount;
323 if(kcount>TERSOFF_MAXN) {
324 printf("FATAL: neighbours = %d\n",kcount);
325 printf(" -> %d %d %d\n",ai->tag,aj->tag,ak->tag);
330 if(brand==ak->brand) {
333 S2=params->S2[brand];
342 v3_sub(&dist_ik,&(ak->r),&(ai->r));
343 if(bc) check_per_bound(moldyn,&dist_ik);
344 d_ik2=v3_absolute_square(&dist_ik);
346 /* store data for second k loop */
347 exchange->dist_ik[kcount]=dist_ik;
348 exchange->d_ik2[kcount]=d_ik2;
350 /* return if not within cutoff */
360 dist_ij=exchange->dist_ij;
364 cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
367 h_cos=*(exchange->h_i)-cos_theta;
368 d2_h_cos2=exchange->di2+(h_cos*h_cos);
369 frac=exchange->ci2/d2_h_cos2;
370 g=1.0+exchange->ci2di2-frac;
371 dg=-2.0*frac*h_cos/d2_h_cos2;
373 /* zeta sum += f_c_ik * g_ijk */
375 exchange->zeta_ij+=g;
381 arg=M_PI*(d_ik-R)/s_r;
382 f_c_ik=0.5+0.5*cos(arg);
383 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
384 exchange->zeta_ij+=f_c_ik*g;
388 if((ai==&(moldyn->atom[0]))|
389 (aj==&(moldyn->atom[864]))|
390 (ak==&(moldyn->atom[1003]))) {
391 printf(" -> %f %f %f\n",exchange->ci2di2,frac,h_cos);
395 /* store even more data for second k loop */
396 exchange->g[kcount]=g;
397 exchange->dg[kcount]=dg;
398 exchange->d_ik[kcount]=d_ik;
399 exchange->cos_theta[kcount]=cos_theta;
400 exchange->f_c_ik[kcount]=f_c_ik;
401 exchange->df_c_ik[kcount]=df_c_ik;
403 /* increase k counter */
409 int tersoff_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
411 t_tersoff_mult_params *params;
412 t_tersoff_exchange *exchange;
414 double f_a,df_a,b,db,f_c,df_c;
425 params=moldyn->pot_params;
426 exchange=&(params->exchange);
429 if(brand==ai->brand) {
434 mu=params->mu[brand];
435 lambda=params->lambda[brand];
444 lambda=params->lambda_m;
457 arg=M_PI*(d_ij-R)/s_r;
458 f_c=0.5+0.5*cos(arg);
459 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
463 f_a=-B*exp(-mu*d_ij);
467 f_r=A*exp(-lambda*d_ij);
468 df_r=lambda*f_r/d_ij;
471 if(exchange->zeta_ij==0.0) {
477 tmp=exchange->betaini*pow(exchange->zeta_ij,ni-1.0);
478 b=(1.0+exchange->zeta_ij*tmp);
479 db=chi*pow(b,-1.0/(2.0*ni)-1.0);
484 /* force contribution */
485 scale=-0.5*(f_c*(df_r+b*df_a)+df_c*(f_r+b*df_a));
486 v3_scale(&force,&(exchange->dist_ij),scale);
487 v3_add(&(ai->f),&(ai->f),&force);
488 v3_scale(&force,&force,-1.0); // dri rij = - drj rij
489 v3_add(&(aj->f),&(aj->f),&force);
492 if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
493 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,aj->tag);
494 printf("adding %f %f %f\n",force.x,force.y,force.z);
495 if(ai==&(moldyn->atom[0]))
496 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
497 if(aj==&(moldyn->atom[0]))
498 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
499 printf("energy: %f = %f %f %f %f\n",0.5*f_c*(b*f_a+f_r),
501 printf(" %f %f %f\n",exchange->zeta_ij,.0,.0);
506 virial_calc(aj,&force,&(exchange->dist_ij));
508 /* dzeta prefactor = - 0.5 f_c f_a db */
509 exchange->pre_dzeta=-0.5*f_a*f_c*db;
511 /* energy contribution */
512 energy=0.5*f_c*(b*f_a+f_r);
513 moldyn->energy+=energy;
516 /* reset k counter for second k loop */
522 /* tersoff 3 body potential function (second k loop) */
523 int tersoff_mult_3bp_k2(t_moldyn *moldyn,
524 t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
526 t_tersoff_mult_params *params;
527 t_tersoff_exchange *exchange;
529 t_3dvec dist_ik,dist_ij;
530 double d_ik2,d_ik,d_ij2,d_ij;
533 double g,dg,cos_theta;
535 double f_c_ik,df_c_ik;
536 double dijdik_inv,fcdg,dfcg;
537 t_3dvec dcosdri,dcosdrj,dcosdrk;
540 params=moldyn->pot_params;
541 exchange=&(params->exchange);
542 kcount=exchange->kcount;
544 if(kcount>TERSOFF_MAXN)
545 printf("FATAL: neighbours!\n");
548 d_ik2=exchange->d_ik2[kcount];
552 S2=params->S2[brand];
556 /* return if d_ik > S */
562 /* prefactor dzeta */
563 pre_dzeta=exchange->pre_dzeta;
566 dist_ik=exchange->dist_ik[kcount];
567 d_ik=exchange->d_ik[kcount];
569 /* f_c_ik, df_c_ik */
570 f_c_ik=exchange->f_c_ik[kcount];
571 df_c_ik=exchange->df_c_ik[kcount];
573 /* dist_ij, d_ij2, d_ij */
574 dist_ij=exchange->dist_ij;
575 d_ij2=exchange->d_ij2;
578 /* g, dg, cos_theta */
579 g=exchange->g[kcount];
580 dg=exchange->dg[kcount];
581 cos_theta=exchange->cos_theta[kcount];
583 /* cos_theta derivatives wrt i,j,k */
584 dijdik_inv=1.0/(d_ij*d_ik);
585 v3_scale(&dcosdrj,&dist_ik,dijdik_inv);
586 v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
587 v3_add(&dcosdrj,&dcosdrj,&tmp);
588 v3_scale(&dcosdrk,&dist_ij,dijdik_inv);
589 v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
590 v3_add(&dcosdrk,&dcosdrk,&tmp);
591 v3_add(&dcosdri,&dcosdrj,&dcosdrk);
592 v3_scale(&dcosdri,&dcosdri,-1.0);
594 /* f_c_ik * dg, df_c_ik * g */
598 /* derivative wrt i */
599 v3_scale(&force,&dist_ik,dfcg);
600 v3_scale(&tmp,&dcosdri,fcdg);
601 v3_add(&force,&force,&tmp);
602 v3_scale(&force,&force,pre_dzeta);
604 /* force contribution */
605 v3_add(&(ai->f),&(ai->f),&force);
608 if(ai==&(moldyn->atom[0])) {
609 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
610 printf("adding %f %f %f\n",force.x,force.y,force.z);
611 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
615 /* derivative wrt j */
616 v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
618 /* force contribution */
619 v3_add(&(aj->f),&(aj->f),&force);
622 if(aj==&(moldyn->atom[0])) {
623 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
624 printf("adding %f %f %f\n",force.x,force.y,force.z);
625 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
630 v3_scale(&force,&force,-1.0);
631 virial_calc(ai,&force,&dist_ij);
633 /* derivative wrt k */
634 v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
635 v3_scale(&tmp,&dcosdrk,fcdg);
636 v3_add(&force,&force,&tmp);
637 v3_scale(&force,&force,pre_dzeta);
639 /* force contribution */
640 v3_add(&(ak->f),&(ak->f),&force);
643 if(ak==&(moldyn->atom[0])) {
644 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
645 printf("adding %f %f %f\n",force.x,force.y,force.z);
646 printf("total k: %f %f %f\n",ak->f.x,ak->f.y,ak->f.z);
651 v3_scale(&force,&force,-1.0);
652 virial_calc(ai,&force,&dist_ik);
654 /* increase k counter */
661 int tersoff_mult_check_2b_bond(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
663 t_tersoff_mult_params *params;
668 v3_sub(&dist,&(aj->r),&(ai->r));
669 if(bc) check_per_bound(moldyn,&dist);
670 d=v3_absolute_square(&dist);
672 params=moldyn->pot_params;
675 if(brand==aj->brand) {
676 if(d<=params->S2[brand])
680 if(d<=params->S2mixed)