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_sub(&(aj->f),&(aj->f),&force); // reason: dri rij = - drj rij
230 if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
231 printf("force 2bp: [%d %d]\n",ai->tag,aj->tag);
232 printf("adding %f %f %f\n",force.x,force.y,force.z);
233 if(ai==&(moldyn->atom[0]))
234 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
235 if(aj==&(moldyn->atom[0]))
236 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
241 virial_calc(ai,&force,&dist_ij);
243 /* energy 2bp contribution */
245 moldyn->energy+=energy;
252 /* tersoff 3 body potential function (first ij loop) */
253 int tersoff_mult_3bp_j1(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
255 t_tersoff_mult_params *params;
256 t_tersoff_exchange *exchange;
262 params=moldyn->pot_params;
263 exchange=&(params->exchange);
266 exchange->zeta_ij=0.0;
269 * set ij depending values
275 S2=params->S2[brand];
280 v3_sub(&dist_ij,&(aj->r),&(ai->r));
281 if(bc) check_per_bound(moldyn,&dist_ij);
282 d_ij2=v3_absolute_square(&dist_ij);
284 /* if d_ij2 > S2 => no force & potential energy contribution */
294 exchange->dist_ij=dist_ij;
295 exchange->d_ij2=d_ij2;
298 /* reset k counter for first k loop */
304 /* tersoff 3 body potential function (first k loop) */
305 int tersoff_mult_3bp_k1(t_moldyn *moldyn,
306 t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
308 t_tersoff_mult_params *params;
309 t_tersoff_exchange *exchange;
312 t_3dvec dist_ij,dist_ik;
313 double d_ik2,d_ik,d_ij;
314 double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
315 double f_c_ik,df_c_ik;
318 params=moldyn->pot_params;
319 exchange=&(params->exchange);
320 kcount=exchange->kcount;
322 if(kcount>TERSOFF_MAXN) {
323 printf("FATAL: neighbours = %d\n",kcount);
324 printf(" -> %d %d %d\n",ai->tag,aj->tag,ak->tag);
329 if(brand==ak->brand) {
332 S2=params->S2[brand];
341 v3_sub(&dist_ik,&(ak->r),&(ai->r));
342 if(bc) check_per_bound(moldyn,&dist_ik);
343 d_ik2=v3_absolute_square(&dist_ik);
345 /* store data for second k loop */
346 exchange->dist_ik[kcount]=dist_ik;
347 exchange->d_ik2[kcount]=d_ik2;
349 /* return if not within cutoff */
359 dist_ij=exchange->dist_ij;
363 cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
366 h_cos=*(exchange->h_i)-cos_theta;
367 d2_h_cos2=exchange->di2+(h_cos*h_cos);
368 frac=exchange->ci2/d2_h_cos2;
369 g=1.0+exchange->ci2di2-frac;
370 dg=-2.0*frac*h_cos/d2_h_cos2;
372 /* zeta sum += f_c_ik * g_ijk */
374 exchange->zeta_ij+=g;
380 arg=M_PI*(d_ik-R)/s_r;
381 f_c_ik=0.5+0.5*cos(arg);
382 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
383 exchange->zeta_ij+=f_c_ik*g;
387 if((ai==&(moldyn->atom[0]))|
388 (aj==&(moldyn->atom[864]))|
389 (ak==&(moldyn->atom[1003]))) {
390 printf(" -> %f %f %f\n",exchange->ci2di2,frac,h_cos);
394 /* store even more data for second k loop */
395 exchange->g[kcount]=g;
396 exchange->dg[kcount]=dg;
397 exchange->d_ik[kcount]=d_ik;
398 exchange->cos_theta[kcount]=cos_theta;
399 exchange->f_c_ik[kcount]=f_c_ik;
400 exchange->df_c_ik[kcount]=df_c_ik;
402 /* increase k counter */
408 int tersoff_mult_3bp_j2(t_moldyn *moldyn,t_atom *ai,t_atom *aj,u8 bc) {
410 t_tersoff_mult_params *params;
411 t_tersoff_exchange *exchange;
413 double f_a,df_a,b,db,f_c,df_c;
424 params=moldyn->pot_params;
425 exchange=&(params->exchange);
428 if(brand==ai->brand) {
433 mu=params->mu[brand];
434 lambda=params->lambda[brand];
443 lambda=params->lambda_m;
456 arg=M_PI*(d_ij-R)/s_r;
457 f_c=0.5+0.5*cos(arg);
458 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
462 f_a=-B*exp(-mu*d_ij);
466 f_r=A*exp(-lambda*d_ij);
467 df_r=lambda*f_r/d_ij;
470 if(exchange->zeta_ij==0.0) {
476 tmp=exchange->betaini*pow(exchange->zeta_ij,ni-1.0);
477 b=(1.0+exchange->zeta_ij*tmp);
478 db=chi*pow(b,-1.0/(2.0*ni)-1.0);
483 /* force contribution */
484 scale=-0.5*(f_c*(df_r+b*df_a)+df_c*(f_r+b*df_a));
485 v3_scale(&force,&(exchange->dist_ij),scale);
486 v3_add(&(ai->f),&(ai->f),&force);
487 v3_sub(&(aj->f),&(aj->f),&force); // dri rij = - drj rij
490 if((ai==&(moldyn->atom[0]))|(aj==&(moldyn->atom[0]))) {
491 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,aj->tag);
492 printf("adding %f %f %f\n",force.x,force.y,force.z);
493 if(ai==&(moldyn->atom[0]))
494 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
495 if(aj==&(moldyn->atom[0]))
496 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
497 printf("energy: %f = %f %f %f %f\n",0.5*f_c*(b*f_a+f_r),
499 printf(" %f %f %f\n",exchange->zeta_ij,.0,.0);
505 virial_calc(ai,&force,&(exchange->dist_ij));
507 /* dzeta prefactor = - 0.5 f_c f_a db */
508 exchange->pre_dzeta=-0.5*f_a*f_c*db;
510 /* energy contribution */
511 energy=0.5*f_c*(b*f_a+f_r);
512 moldyn->energy+=energy;
515 /* reset k counter for second k loop */
521 /* tersoff 3 body potential function (second k loop) */
522 int tersoff_mult_3bp_k2(t_moldyn *moldyn,
523 t_atom *ai,t_atom *aj,t_atom *ak,u8 bc) {
525 t_tersoff_mult_params *params;
526 t_tersoff_exchange *exchange;
528 t_3dvec dist_ik,dist_ij;
529 double d_ik2,d_ik,d_ij2,d_ij;
532 double g,dg,cos_theta;
534 double f_c_ik,df_c_ik;
535 double dijdik_inv,fcdg,dfcg;
536 t_3dvec dcosdri,dcosdrj,dcosdrk;
539 params=moldyn->pot_params;
540 exchange=&(params->exchange);
541 kcount=exchange->kcount;
543 if(kcount>TERSOFF_MAXN)
544 printf("FATAL: neighbours!\n");
547 d_ik2=exchange->d_ik2[kcount];
551 S2=params->S2[brand];
555 /* return if d_ik > S */
561 /* prefactor dzeta */
562 pre_dzeta=exchange->pre_dzeta;
565 dist_ik=exchange->dist_ik[kcount];
566 d_ik=exchange->d_ik[kcount];
568 /* f_c_ik, df_c_ik */
569 f_c_ik=exchange->f_c_ik[kcount];
570 df_c_ik=exchange->df_c_ik[kcount];
572 /* dist_ij, d_ij2, d_ij */
573 dist_ij=exchange->dist_ij;
574 d_ij2=exchange->d_ij2;
577 /* g, dg, cos_theta */
578 g=exchange->g[kcount];
579 dg=exchange->dg[kcount];
580 cos_theta=exchange->cos_theta[kcount];
582 /* cos_theta derivatives wrt i,j,k */
583 dijdik_inv=1.0/(d_ij*d_ik);
584 v3_scale(&dcosdrj,&dist_ik,dijdik_inv);
585 v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
586 v3_add(&dcosdrj,&dcosdrj,&tmp);
587 v3_scale(&dcosdrk,&dist_ij,dijdik_inv);
588 v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
589 v3_add(&dcosdrk,&dcosdrk,&tmp);
590 v3_add(&dcosdri,&dcosdrj,&dcosdrk);
591 v3_scale(&dcosdri,&dcosdri,-1.0);
593 /* f_c_ik * dg, df_c_ik * g */
597 /* derivative wrt i */
598 v3_scale(&force,&dist_ik,dfcg);
599 v3_scale(&tmp,&dcosdri,fcdg);
600 v3_add(&force,&force,&tmp);
601 v3_scale(&force,&force,pre_dzeta);
603 /* force contribution */
604 v3_add(&(ai->f),&(ai->f),&force);
607 if(ai==&(moldyn->atom[0])) {
608 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
609 printf("adding %f %f %f\n",force.x,force.y,force.z);
610 printf("total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
615 //virial_calc(ai,&force,&dist_ij);
617 /* derivative wrt j */
618 v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
620 /* force contribution */
621 v3_add(&(aj->f),&(aj->f),&force);
624 if(aj==&(moldyn->atom[0])) {
625 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
626 printf("adding %f %f %f\n",force.x,force.y,force.z);
627 printf("total j: %f %f %f\n",aj->f.x,aj->f.y,aj->f.z);
632 //v3_scale(&force,&force,-1.0);
634 virial_calc(ai,&force,&dist_ij);
636 /* derivative wrt k */
637 v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
638 v3_scale(&tmp,&dcosdrk,fcdg);
639 v3_add(&force,&force,&tmp);
640 v3_scale(&force,&force,pre_dzeta);
642 /* force contribution */
643 v3_add(&(ak->f),&(ak->f),&force);
646 if(ak==&(moldyn->atom[0])) {
647 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,aj->tag,ak->tag);
648 printf("adding %f %f %f\n",force.x,force.y,force.z);
649 printf("total k: %f %f %f\n",ak->f.x,ak->f.y,ak->f.z);
654 //v3_scale(&force,&force,-1.0);
656 virial_calc(ai,&force,&dist_ik);
658 /* increase k counter */