4 * author: Frank Zirkelbach <frank.zirkelbach@physik.uni-augsburg.de>
12 #include <sys/types.h>
24 #include "../moldyn.h"
25 #include "../math/math.h"
32 #define albe_v_calc(a,f,d) a->virial.xx+=f->x*d->x; \
33 a->virial.yy+=f->y*d->y; \
34 a->virial.zz+=f->z*d->z; \
35 a->virial.xy+=f->x*d->y; \
36 a->virial.xz+=f->x*d->z; \
37 a->virial.yz+=f->y*d->z
39 int albe_potential_force_calc(t_moldyn *moldyn) {
42 t_atom *itom,*jtom,*ktom;
52 t_list neighbour_i[27];
53 t_list neighbour_i2[27];
63 t_albe_mult_params *params;
64 t_albe_exchange *exchange;
78 double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
79 double f_c_ik,df_c_ik;
82 double f_a,df_a,b,db,f_c,df_c;
91 double dijdik_inv,fcdg,dfcg;
92 t_3dvec dcosdrj,dcosdrk;
109 params=moldyn->pot_params;
110 exchange=&(params->exchange);
116 /* reset global virial */
117 memset(&(moldyn->gvir),0,sizeof(t_virial));
119 /* reset force, site energy and virial of every atom */
121 #pragma omp parallel for private(virial)
123 for(i=0;i<count;i++) {
126 v3_zero(&(itom[i].f));
129 virial=(&(itom[i].virial));
137 /* reset site energy */
142 /* get energy, force and virial of every atom */
144 /* first (and only) loop over atoms i */
145 for(i=0;i<count;i++) {
147 if(!(itom[i].attr&ATOM_ATTR_3BP))
150 link_cell_neighbour_index(moldyn,
151 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
152 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
153 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
158 /* copy the neighbour lists */
162 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
168 /* loop over atoms j */
175 while(neighbour_i[j][p]!=-1) {
177 jtom=&(itom[neighbour_i[j][p]]);
185 p=lc->subcell->list[p];
187 this=&(neighbour_i[j]);
190 if(this->start==NULL)
195 jtom=this->current->data;
201 if(!(jtom->attr&ATOM_ATTR_3BP))
208 /* j1 func here ... */
209 /* albe 3 body potential function (first ij loop) */
215 * set ij depending values
218 if(brand_i==jtom->brand) {
219 S2=params->S2[brand_i];
226 v3_sub(&dist_ij,&(jtom->r),&(ai->r));
227 if(bc_ij) check_per_bound(moldyn,&dist_ij);
228 d_ij2=v3_absolute_square(&dist_ij);
230 /* if d_ij2 > S2 => no force & potential energy contribution */
237 /* reset k counter for first k loop */
240 /* first loop over atoms k */
247 while(neighbour_i[k][q]!=-1) {
249 ktom=&(itom[neighbour_i[k][q]]);
257 q=lc->subcell->list[q];
259 that=&(neighbour_i2[k]);
262 if(that->start==NULL)
266 ktom=that->current->data;
269 if(!(ktom->attr&ATOM_ATTR_3BP))
279 /* k1 func here ... */
280 /* albe 3 body potential function (first k loop) */
282 if(kcount>ALBE_MAXN) {
283 printf("FATAL: neighbours = %d\n",kcount);
284 printf(" -> %d %d %d\n",ai->tag,jtom->tag,ktom->tag);
288 if(brand_i==ktom->brand) {
289 Rk=params->R[brand_i];
290 Sk=params->S[brand_i];
291 Sk2=params->S2[brand_i];
292 /* albe needs i,k depending c,d,h and gamma values */
293 gamma_i=params->gamma[brand_i];
294 c_i=params->c[brand_i];
295 d_i=params->d[brand_i];
296 h_i=params->h[brand_i];
297 ci2=params->c2[brand_i];
298 di2=params->d2[brand_i];
299 ci2di2=params->c2d2[brand_i];
305 /* albe needs i,k depending c,d,h and gamma values */
306 gamma_i=params->gamma_m;
310 ci2=params->c2_mixed;
311 di2=params->d2_mixed;
312 ci2di2=params->c2d2_m;
316 v3_sub(&dist_ik,&(ktom->r),&(ai->r));
317 if(bc_ik) check_per_bound(moldyn,&dist_ik);
318 d_ik2=v3_absolute_square(&dist_ik);
320 /* store data for second k loop */
321 exchange->dist_ik[kcount]=dist_ik;
322 exchange->d_ik2[kcount]=d_ik2;
324 /* return if not within cutoff */
334 cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
337 h_cos=*(exchange->h_i)+cos_theta; // + in albe formalism
338 d2_h_cos2=exchange->di2+(h_cos*h_cos);
339 frac=exchange->ci2/d2_h_cos2;
340 g=*(exchange->gamma_i)*(1.0+exchange->ci2di2-frac);
341 dg=2.0*frac**(exchange->gamma_i)*h_cos/d2_h_cos2; // + in albe f..
344 h_cos=h_i+cos_theta; // + in albe formalism
345 d2_h_cos2=di2+(h_cos*h_cos);
347 g=gamma_i*(1.0+ci2di2-frac);
348 dg=2.0*frac*gamma_i*h_cos/d2_h_cos2; // + in albe f..
350 /* zeta sum += f_c_ik * g_ijk */
358 arg=M_PI*(d_ik-Rk)/s_r;
359 f_c_ik=0.5+0.5*cos(arg);
360 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
364 /* store even more data for second k loop */
365 exchange->g[kcount]=g;
366 exchange->dg[kcount]=dg;
367 exchange->d_ik[kcount]=d_ik;
368 exchange->cos_theta[kcount]=cos_theta;
369 exchange->f_c_ik[kcount]=f_c_ik;
370 exchange->df_c_ik[kcount]=df_c_ik;
372 /* increase k counter */
380 } while(list_next_f(that)!=\
387 /* j2 func here ... */
390 if(brand_i==jtom->brand) {
391 S=params->S[brand_i];
392 R=params->R[brand_i];
393 B=params->B[brand_i];
394 A=params->A[brand_i];
395 r0=params->r0[brand_i];
396 mu=params->mu[brand_i];
397 lambda=params->lambda[brand_i];
406 lambda=params->lambda_m;
416 arg=M_PI*(d_ij-R)/s_r;
417 f_c=0.5+0.5*cos(arg);
418 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
422 f_a=-B*exp(-mu*(d_ij-r0));
426 f_r=A*exp(-lambda*(d_ij-r0));
427 df_r=lambda*f_r/d_ij;
435 b=1.0/sqrt(1.0+zeta_ij);
436 db=-0.5*b/(1.0+zeta_ij);
439 /* force contribution for atom i */
440 scale=-0.5*(f_c*(df_r-b*df_a)+df_c*(f_r-b*f_a)); // - in albe formalism
441 v3_scale(&force,&(dist_ij),scale);
442 v3_add(&(ai->f),&(ai->f),&force);
444 /* force contribution for atom j */
445 v3_scale(&force,&force,-1.0); // dri rij = - drj rij
446 v3_add(&(jtom->f),&(jtom->f),&force);
449 virial_calc(ai,&force,&(dist_ij));
452 if(moldyn->time>DSTART&&moldyn->time<DEND) {
453 if((ai==&(moldyn->atom[DATOM]))|(jtom==&(moldyn->atom[DATOM]))) {
454 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,jtom->tag);
455 printf(" adding %f %f %f\n",force.x,force.y,force.z);
456 if(ai==&(moldyn->atom[0]))
457 printf(" total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
458 if(jtom==&(moldyn->atom[0]))
459 printf(" total j: %f %f %f\n",jtom->f.x,jtom->f.y,jtom->f.z);
460 printf(" energy: %f = %f %f %f %f\n",0.5*f_c*(b*f_a+f_r),
462 printf(" %f %f %f\n",zeta_ij,.0,.0);
467 /* dzeta prefactor = - f_c f_a db, (* -0.5 due to force calc) */
468 pre_dzeta=0.5*f_a*f_c*db;
470 /* energy contribution */
471 energy=0.5*f_c*(f_r-b*f_a); // - in albe formalism
472 moldyn->energy+=energy;
475 /* reset k counter for second k loop */
479 /* second loop over atoms k */
486 while(neighbour_i[k][q]!=-1) {
488 ktom=&(itom[neighbour_i[k][q]]);
496 q=lc->subcell->list[q];
498 that=&(neighbour_i2[k]);
501 if(that->start==NULL)
505 ktom=that->current->data;
508 if(!(ktom->attr&ATOM_ATTR_3BP))
518 /* k2 func here ... */
519 /* albe 3 body potential function (second k loop) */
522 printf("FATAL: neighbours!\n");
525 d_ik2=exchange->d_ik2[kcount];
527 if(brand_i==ktom->brand)
528 Sk2=params->S2[brand_i];
532 /* return if d_ik > S */
539 dist_ik=exchange->dist_ik[kcount];
540 d_ik=exchange->d_ik[kcount];
542 /* f_c_ik, df_c_ik */
543 f_c_ik=exchange->f_c_ik[kcount];
544 df_c_ik=exchange->df_c_ik[kcount];
546 /* g, dg, cos_theta */
547 g=exchange->g[kcount];
548 dg=exchange->dg[kcount];
549 cos_theta=exchange->cos_theta[kcount];
551 /* cos_theta derivatives wrt j,k */
552 dijdik_inv=1.0/(d_ij*d_ik);
553 v3_scale(&dcosdrj,&dist_ik,dijdik_inv); // j
554 v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
555 v3_add(&dcosdrj,&dcosdrj,&tmp);
556 v3_scale(&dcosdrk,&dist_ij,dijdik_inv); // k
557 v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
558 v3_add(&dcosdrk,&dcosdrk,&tmp);
560 /* f_c_ik * dg, df_c_ik * g */
564 /* derivative wrt j */
565 v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
567 /* force contribution */
568 v3_add(&(jtom->f),&(jtom->f),&force);
571 if(moldyn->time>DSTART&&moldyn->time<DEND) {
572 if(jtom==&(moldyn->atom[DATOM])) {
573 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,jtom->tag,ktom->tag);
574 printf(" adding %f %f %f\n",force.x,force.y,force.z);
575 printf(" total j: %f %f %f\n",jtom->f.x,jtom->f.y,jtom->f.z);
576 printf(" angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
577 printf(" d ij ik = %f %f\n",d_ij,d_ik);
583 virial_calc(ai,&force,&dist_ij);
585 /* force contribution to atom i */
586 v3_scale(&force,&force,-1.0);
587 v3_add(&(ai->f),&(ai->f),&force);
589 /* derivative wrt k */
590 v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
591 v3_scale(&tmp,&dcosdrk,fcdg);
592 v3_add(&force,&force,&tmp);
593 v3_scale(&force,&force,pre_dzeta);
595 /* force contribution */
596 v3_add(&(ktom->f),&(ktom->f),&force);
599 if(moldyn->time>DSTART&&moldyn->time<DEND) {
600 if(ktom==&(moldyn->atom[DATOM])) {
601 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,jtom->tag,ktom->tag);
602 printf(" adding %f %f %f\n",force.x,force.y,force.z);
603 printf(" total k: %f %f %f\n",ktom->f.x,ktom->f.y,ktom->f.z);
604 printf(" angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
605 printf(" d ij ik = %f %f\n",d_ij,d_ik);
611 virial_calc(ai,&force,&dist_ik);
613 /* force contribution to atom i */
614 v3_scale(&force,&force,-1.0);
615 v3_add(&(ai->f),&(ai->f),&force);
617 /* increase k counter */
627 } while(list_next_f(that)!=\
638 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
653 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
654 if(moldyn->time>DSTART&&moldyn->time<DEND) {
656 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
657 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
658 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
662 /* some postprocessing */
664 #pragma omp parallel for
666 for(i=0;i<count;i++) {
667 /* calculate global virial */
668 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
669 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
670 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
671 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
672 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
673 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
675 /* check forces regarding the given timestep */
676 if(v3_norm(&(itom[i].f))>\
677 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
678 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",