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;
50 t_list neighbour_i[27];
51 t_list neighbour_i2[27];
61 t_albe_mult_params *params;
62 t_albe_exchange *exchange;
76 double cos_theta,h_cos,d2_h_cos2,frac,g,dg,s_r,arg;
77 double f_c_ik,df_c_ik;
80 double f_a,df_a,b,db,f_c,df_c;
89 double dijdik_inv,fcdg,dfcg;
90 t_3dvec dcosdrj,dcosdrk;
110 params=moldyn->pot_params;
111 exchange=&(params->exchange);
117 /* reset global virial */
118 memset(&(moldyn->gvir),0,sizeof(t_virial));
120 /* reset force, site energy and virial of every atom */
122 #pragma omp parallel for private(virial)
124 for(i=0;i<count;i++) {
127 v3_zero(&(itom[i].f));
130 virial=(&(itom[i].virial));
138 /* reset site energy */
143 /* get energy, force and virial of every atom */
145 /* first (and only) loop over atoms i */
146 for(i=0;i<count;i++) {
148 if(!(itom[i].attr&ATOM_ATTR_3BP))
151 link_cell_neighbour_index(moldyn,
152 (itom[i].r.x+moldyn->dim.x/2)/lc->x,
153 (itom[i].r.y+moldyn->dim.y/2)/lc->y,
154 (itom[i].r.z+moldyn->dim.z/2)/lc->z,
159 /* copy the neighbour lists */
161 memcpy(neighbour_i2,neighbour_i,27*sizeof(t_list));
167 /* loop over atoms j */
174 while(neighbour_i[j][p]!=0) {
176 jtom=&(atom[neighbour_i[j][p]]);
179 this=&(neighbour_i[j]);
182 if(this->start==NULL)
187 jtom=this->current->data;
193 if(!(jtom->attr&ATOM_ATTR_3BP))
200 /* j1 func here ... */
201 /* albe 3 body potential function (first ij loop) */
207 * set ij depending values
210 if(brand_i==jtom->brand) {
211 S2=params->S2[brand_i];
218 v3_sub(&dist_ij,&(jtom->r),&(ai->r));
219 if(bc_ij) check_per_bound(moldyn,&dist_ij);
220 d_ij2=v3_absolute_square(&dist_ij);
222 /* if d_ij2 > S2 => no force & potential energy contribution */
229 /* reset k counter for first k loop */
232 /* first loop over atoms k */
239 while(neighbour_i[j][q]!=0) {
241 ktom=&(atom[neighbour_i[k][q]]);
244 that=&(neighbour_i2[k]);
247 if(that->start==NULL)
251 ktom=that->current->data;
254 if(!(ktom->attr&ATOM_ATTR_3BP))
264 /* k1 func here ... */
265 /* albe 3 body potential function (first k loop) */
267 if(kcount>ALBE_MAXN) {
268 printf("FATAL: neighbours = %d\n",kcount);
269 printf(" -> %d %d %d\n",ai->tag,jtom->tag,ktom->tag);
273 if(brand_i==ktom->brand) {
274 Rk=params->R[brand_i];
275 Sk=params->S[brand_i];
276 Sk2=params->S2[brand_i];
277 /* albe needs i,k depending c,d,h and gamma values */
278 gamma_i=params->gamma[brand_i];
279 c_i=params->c[brand_i];
280 d_i=params->d[brand_i];
281 h_i=params->h[brand_i];
282 ci2=params->c2[brand_i];
283 di2=params->d2[brand_i];
284 ci2di2=params->c2d2[brand_i];
290 /* albe needs i,k depending c,d,h and gamma values */
291 gamma_i=params->gamma_m;
295 ci2=params->c2_mixed;
296 di2=params->d2_mixed;
297 ci2di2=params->c2d2_m;
301 v3_sub(&dist_ik,&(ktom->r),&(ai->r));
302 if(bc_ik) check_per_bound(moldyn,&dist_ik);
303 d_ik2=v3_absolute_square(&dist_ik);
305 /* store data for second k loop */
306 exchange->dist_ik[kcount]=dist_ik;
307 exchange->d_ik2[kcount]=d_ik2;
309 /* return if not within cutoff */
319 cos_theta=v3_scalar_product(&dist_ij,&dist_ik)/(d_ij*d_ik);
322 h_cos=*(exchange->h_i)+cos_theta; // + in albe formalism
323 d2_h_cos2=exchange->di2+(h_cos*h_cos);
324 frac=exchange->ci2/d2_h_cos2;
325 g=*(exchange->gamma_i)*(1.0+exchange->ci2di2-frac);
326 dg=2.0*frac**(exchange->gamma_i)*h_cos/d2_h_cos2; // + in albe f..
329 h_cos=h_i+cos_theta; // + in albe formalism
330 d2_h_cos2=di2+(h_cos*h_cos);
332 g=gamma_i*(1.0+ci2di2-frac);
333 dg=2.0*frac*gamma_i*h_cos/d2_h_cos2; // + in albe f..
335 /* zeta sum += f_c_ik * g_ijk */
343 arg=M_PI*(d_ik-Rk)/s_r;
344 f_c_ik=0.5+0.5*cos(arg);
345 df_c_ik=0.5*sin(arg)*(M_PI/(s_r*d_ik));
349 /* store even more data for second k loop */
350 exchange->g[kcount]=g;
351 exchange->dg[kcount]=dg;
352 exchange->d_ik[kcount]=d_ik;
353 exchange->cos_theta[kcount]=cos_theta;
354 exchange->f_c_ik[kcount]=f_c_ik;
355 exchange->df_c_ik[kcount]=df_c_ik;
357 /* increase k counter */
363 } while(list_next_f(that)!=\
370 /* j2 func here ... */
373 if(brand_i==jtom->brand) {
374 S=params->S[brand_i];
375 R=params->R[brand_i];
376 B=params->B[brand_i];
377 A=params->A[brand_i];
378 r0=params->r0[brand_i];
379 mu=params->mu[brand_i];
380 lambda=params->lambda[brand_i];
389 lambda=params->lambda_m;
399 arg=M_PI*(d_ij-R)/s_r;
400 f_c=0.5+0.5*cos(arg);
401 df_c=0.5*sin(arg)*(M_PI/(s_r*d_ij));
405 f_a=-B*exp(-mu*(d_ij-r0));
409 f_r=A*exp(-lambda*(d_ij-r0));
410 df_r=lambda*f_r/d_ij;
418 b=1.0/sqrt(1.0+zeta_ij);
419 db=-0.5*b/(1.0+zeta_ij);
422 /* force contribution for atom i */
423 scale=-0.5*(f_c*(df_r-b*df_a)+df_c*(f_r-b*f_a)); // - in albe formalism
424 v3_scale(&force,&(dist_ij),scale);
425 v3_add(&(ai->f),&(ai->f),&force);
427 /* force contribution for atom j */
428 v3_scale(&force,&force,-1.0); // dri rij = - drj rij
429 v3_add(&(jtom->f),&(jtom->f),&force);
432 virial_calc(ai,&force,&(dist_ij));
435 if(moldyn->time>DSTART&&moldyn->time<DEND) {
436 if((ai==&(moldyn->atom[DATOM]))|(jtom==&(moldyn->atom[DATOM]))) {
437 printf("force 3bp (j2): [%d %d sum]\n",ai->tag,jtom->tag);
438 printf(" adding %f %f %f\n",force.x,force.y,force.z);
439 if(ai==&(moldyn->atom[0]))
440 printf(" total i: %f %f %f\n",ai->f.x,ai->f.y,ai->f.z);
441 if(jtom==&(moldyn->atom[0]))
442 printf(" total j: %f %f %f\n",jtom->f.x,jtom->f.y,jtom->f.z);
443 printf(" energy: %f = %f %f %f %f\n",0.5*f_c*(b*f_a+f_r),
445 printf(" %f %f %f\n",zeta_ij,.0,.0);
450 /* dzeta prefactor = - f_c f_a db, (* -0.5 due to force calc) */
451 pre_dzeta=0.5*f_a*f_c*db;
453 /* energy contribution */
454 energy=0.5*f_c*(f_r-b*f_a); // - in albe formalism
455 moldyn->energy+=energy;
458 /* reset k counter for second k loop */
462 /* second loop over atoms k */
469 while(neighbour_i[j][q]!=0) {
471 ktom=&(atom[neighbour_i[k][q]]);
474 that=&(neighbour_i2[k]);
477 if(that->start==NULL)
481 ktom=that->current->data;
484 if(!(ktom->attr&ATOM_ATTR_3BP))
494 /* k2 func here ... */
495 /* albe 3 body potential function (second k loop) */
498 printf("FATAL: neighbours!\n");
501 d_ik2=exchange->d_ik2[kcount];
503 if(brand_i==ktom->brand)
504 Sk2=params->S2[brand_i];
508 /* return if d_ik > S */
515 dist_ik=exchange->dist_ik[kcount];
516 d_ik=exchange->d_ik[kcount];
518 /* f_c_ik, df_c_ik */
519 f_c_ik=exchange->f_c_ik[kcount];
520 df_c_ik=exchange->df_c_ik[kcount];
522 /* g, dg, cos_theta */
523 g=exchange->g[kcount];
524 dg=exchange->dg[kcount];
525 cos_theta=exchange->cos_theta[kcount];
527 /* cos_theta derivatives wrt j,k */
528 dijdik_inv=1.0/(d_ij*d_ik);
529 v3_scale(&dcosdrj,&dist_ik,dijdik_inv); // j
530 v3_scale(&tmp,&dist_ij,-cos_theta/d_ij2);
531 v3_add(&dcosdrj,&dcosdrj,&tmp);
532 v3_scale(&dcosdrk,&dist_ij,dijdik_inv); // k
533 v3_scale(&tmp,&dist_ik,-cos_theta/d_ik2);
534 v3_add(&dcosdrk,&dcosdrk,&tmp);
536 /* f_c_ik * dg, df_c_ik * g */
540 /* derivative wrt j */
541 v3_scale(&force,&dcosdrj,fcdg*pre_dzeta);
543 /* force contribution */
544 v3_add(&(jtom->f),&(jtom->f),&force);
547 if(moldyn->time>DSTART&&moldyn->time<DEND) {
548 if(jtom==&(moldyn->atom[DATOM])) {
549 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,jtom->tag,ktom->tag);
550 printf(" adding %f %f %f\n",force.x,force.y,force.z);
551 printf(" total j: %f %f %f\n",jtom->f.x,jtom->f.y,jtom->f.z);
552 printf(" angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
553 printf(" d ij ik = %f %f\n",d_ij,d_ik);
559 virial_calc(ai,&force,&dist_ij);
561 /* force contribution to atom i */
562 v3_scale(&force,&force,-1.0);
563 v3_add(&(ai->f),&(ai->f),&force);
565 /* derivative wrt k */
566 v3_scale(&force,&dist_ik,-1.0*dfcg); // dri rik = - drk rik
567 v3_scale(&tmp,&dcosdrk,fcdg);
568 v3_add(&force,&force,&tmp);
569 v3_scale(&force,&force,pre_dzeta);
571 /* force contribution */
572 v3_add(&(ktom->f),&(ktom->f),&force);
575 if(moldyn->time>DSTART&&moldyn->time<DEND) {
576 if(ktom==&(moldyn->atom[DATOM])) {
577 printf("force 3bp (k2): [%d %d %d]\n",ai->tag,jtom->tag,ktom->tag);
578 printf(" adding %f %f %f\n",force.x,force.y,force.z);
579 printf(" total k: %f %f %f\n",ktom->f.x,ktom->f.y,ktom->f.z);
580 printf(" angle: %f\n",acos(cos_theta)*360.0/(2*M_PI));
581 printf(" d ij ik = %f %f\n",d_ij,d_ik);
587 virial_calc(ai,&force,&dist_ik);
589 /* force contribution to atom i */
590 v3_scale(&force,&force,-1.0);
591 v3_add(&(ai->f),&(ai->f),&force);
593 /* increase k counter */
601 } while(list_next_f(that)!=\
610 } while(list_next_f(this)!=L_NO_NEXT_ELEMENT);
625 //printf("\nATOM 0: %f %f %f\n\n",itom->f.x,itom->f.y,itom->f.z);
626 if(moldyn->time>DSTART&&moldyn->time<DEND) {
628 printf(" x: %0.40f\n",moldyn->atom[DATOM].f.x);
629 printf(" y: %0.40f\n",moldyn->atom[DATOM].f.y);
630 printf(" z: %0.40f\n",moldyn->atom[DATOM].f.z);
634 /* some postprocessing */
636 #pragma omp parallel for
638 for(i=0;i<count;i++) {
639 /* calculate global virial */
640 moldyn->gvir.xx+=itom[i].r.x*itom[i].f.x;
641 moldyn->gvir.yy+=itom[i].r.y*itom[i].f.y;
642 moldyn->gvir.zz+=itom[i].r.z*itom[i].f.z;
643 moldyn->gvir.xy+=itom[i].r.y*itom[i].f.x;
644 moldyn->gvir.xz+=itom[i].r.z*itom[i].f.x;
645 moldyn->gvir.yz+=itom[i].r.z*itom[i].f.y;
647 /* check forces regarding the given timestep */
648 if(v3_norm(&(itom[i].f))>\
649 0.1*moldyn->nnd*itom[i].mass/moldyn->tau_square)
650 printf("[moldyn] WARNING: pfc (high force: atom %d)\n",