3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2008, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
32 * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
42 #include "gmx_wallcycle.h"
51 #include "mtop_util.h"
55 #include "gmx_ga2la.h"
58 #include "mpelogging.h"
59 #include "groupcoord.h"
60 #include "pull_rotation.h"
64 static char *RotStr = {"Enforced rotation:"};
67 /* Set the minimum weight for the determination of the slab centers */
68 #define WEIGHT_MIN (10*GMX_FLOAT_MIN)
70 /* Helper structure for sorting positions along rotation vector */
72 real xcproj; /* Projection of xc on the rotation vector */
73 int ind; /* Index of xc */
75 rvec x; /* Position */
76 rvec x_ref; /* Reference position */
80 /* Enforced rotation / flexible: determine the angle of each slab */
81 typedef struct gmx_slabdata
83 int nat; /* Number of atoms belonging to this slab */
84 rvec *x; /* The positions belonging to this slab. In
85 general, this should be all positions of the
86 whole rotation group, but we leave those away
87 that have a small enough weight */
88 rvec *ref; /* Same for reference */
89 real *weight; /* The weight for each atom */
93 /* Enforced rotation data for all groups */
94 typedef struct gmx_enfrot
96 FILE *out_rot; /* Output file for rotation data */
97 FILE *out_torque; /* Output file for torque data */
98 FILE *out_angles; /* Output file for slab angles for flexible type */
99 FILE *out_slabs; /* Output file for slab centers */
100 int bufsize; /* Allocation size of buf */
101 rvec *xbuf; /* Coordinate buffer variable for sorting */
102 real *mbuf; /* Masses buffer variable for sorting */
103 sort_along_vec_t *data; /* Buffer variable needed for position sorting */
104 real *mpi_inbuf; /* MPI buffer */
105 real *mpi_outbuf; /* MPI buffer */
106 int mpi_bufsize; /* Allocation size of in & outbuf */
107 real Vrot; /* (Local) part of the enf. rotation potential */
111 /* Global enforced rotation data for a single rotation group */
112 typedef struct gmx_enfrotgrp
114 real degangle; /* Rotation angle in degree */
115 matrix rotmat; /* Rotation matrix */
116 atom_id *ind_loc; /* Local rotation indices */
117 int nat_loc; /* Number of local group atoms */
118 int nalloc_loc; /* Allocation size for ind_loc and weight_loc */
120 real V; /* Rotation potential for this rotation group */
121 rvec *f_rot_loc; /* Array to store the forces on the local atoms
122 resulting from enforced rotation potential */
124 /* Collective coordinates for the whole rotation group */
125 real *xc_ref_length; /* Length of each x_rotref vector after x_rotref
126 has been put into origin */
127 int *xc_ref_ind; /* Position of each local atom in the collective
129 rvec xc_center; /* Center of the rotation group positions, may
131 rvec xc_ref_center; /* dito, for the reference positions */
132 rvec *xc; /* Current (collective) positions */
133 ivec *xc_shifts; /* Current (collective) shifts */
134 ivec *xc_eshifts; /* Extra shifts since last DD step */
135 rvec *xc_old; /* Old (collective) positions */
136 rvec *xc_norm; /* Normalized form of the current positions */
137 rvec *xc_ref_sorted; /* Reference positions (sorted in the same order
138 as xc when sorted) */
139 int *xc_sortind; /* Where is a position found after sorting? */
140 real *mc; /* Collective masses */
142 real invmass; /* one over the total mass of the rotation group */
143 /* Fixed rotation only */
144 rvec *xr_loc; /* Local reference coords, correctly rotated */
145 rvec *x_loc_pbc; /* Local current coords, correct PBC image */
146 real *m_loc; /* Masses of the current local atoms */
147 real fix_torque_v; /* Torque in the direction of rotation vector */
150 /* Flexible rotation only */
151 int nslabs_alloc; /* For this many slabs memory is allocated */
152 int slab_first; /* Lowermost slab for that the calculation needs
153 to be performed at a given time step */
154 int slab_last; /* Uppermost slab ... */
155 int slab_first_ref; /* First slab for which reference COG is stored */
156 int slab_last_ref; /* Last ... */
157 int slab_buffer; /* Slab buffer region around reference slabs */
158 int *firstatom; /* First relevant atom for a slab */
159 int *lastatom; /* Last relevant atom for a slab */
160 rvec *slab_center; /* Gaussian-weighted slab center (COG) */
161 rvec *slab_center_ref; /* Gaussian-weighted slab COG for the
162 reference positions */
163 real *slab_weights; /* Sum of gaussian weights in a slab */
164 real *slab_torque_v; /* Torque T = r x f for each slab. */
165 /* torque_v = m.v = angular momentum in the
167 real max_beta; /* min_gaussian from inputrec->rotgrp is the
168 minimum value the gaussian must have so that
169 the force is actually evaluated max_beta is
170 just another way to put it */
171 real *gn_atom; /* Precalculated gaussians for a single atom */
172 int *gn_slabind; /* Tells to which slab each precalculated gaussian
174 rvec *slab_innersumvec;/* Inner sum of the flexible2 potential per slab;
175 this is precalculated for optimization reasons */
176 t_gmx_slabdata *slab_data; /* Holds atom positions and gaussian weights
177 of atoms belonging to a slab */
181 static double** allocate_square_matrix(int dim)
195 static void free_square_matrix(double** mat, int dim)
200 for (i=0; i<dim; i++)
206 /* Output rotation energy and torque for each rotation group */
207 static void reduce_output(t_commrec *cr, t_rot *rot, real t)
209 int g,i,islab,nslabs=0;
210 int count; /* MPI element counter */
212 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
213 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
219 /* Fill the MPI buffer with stuff to reduce: */
223 for (g=0; g < rot->ngrp; g++)
227 nslabs = erg->slab_last - erg->slab_first + 1;
228 er->mpi_inbuf[count++] = erg->V;
239 er->mpi_inbuf[count++] = erg->fix_torque_v;
240 er->mpi_inbuf[count++] = erg->fix_angles_v;
241 er->mpi_inbuf[count++] = erg->fix_weight_v;
247 /* (Re-)allocate memory for MPI buffer: */
248 if (er->mpi_bufsize < count+nslabs)
250 er->mpi_bufsize = count+nslabs;
251 srenew(er->mpi_inbuf , er->mpi_bufsize);
252 srenew(er->mpi_outbuf, er->mpi_bufsize);
254 for (i=0; i<nslabs; i++)
255 er->mpi_inbuf[count++] = erg->slab_torque_v[i];
262 MPI_Reduce(er->mpi_inbuf, er->mpi_outbuf, count, GMX_MPI_REAL, MPI_SUM, MASTERRANK(cr), cr->mpi_comm_mygroup);
264 /* Copy back the reduced data from the buffer on the master */
268 for (g=0; g < rot->ngrp; g++)
272 nslabs = erg->slab_last - erg->slab_first + 1;
273 erg->V = er->mpi_outbuf[count++];
284 erg->fix_torque_v = er->mpi_outbuf[count++];
285 erg->fix_angles_v = er->mpi_outbuf[count++];
286 erg->fix_weight_v = er->mpi_outbuf[count++];
292 for (i=0; i<nslabs; i++)
293 erg->slab_torque_v[i] = er->mpi_outbuf[count++];
305 /* Av. angle and total torque for each rotation group */
306 for (g=0; g < rot->ngrp; g++)
309 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
310 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
314 /* Output to main rotation log file: */
317 fprintf(er->out_rot, "%12.4f%12.3e",
318 (erg->fix_angles_v/erg->fix_weight_v)*180.0*M_1_PI,
321 fprintf(er->out_rot, "%12.3e", erg->V);
323 /* Output to torque log file: */
326 fprintf(er->out_torque, "%12.3e%6d", t, g);
327 for (i=erg->slab_first; i<=erg->slab_last; i++)
329 islab = i - erg->slab_first; /* slab index */
330 /* Only output if enough weight is in slab */
331 if (erg->slab_weights[islab] > rotg->min_gaussian)
332 fprintf(er->out_torque, "%6d%12.3e", i, erg->slab_torque_v[islab]);
334 fprintf(er->out_torque , "\n");
337 fprintf(er->out_rot, "\n");
342 /* Add the forces from enforced rotation potential to the local forces.
343 * Should be called after the SR forces have been evaluated */
344 extern real add_rot_forces(t_rot *rot, rvec f[], t_commrec *cr, int step, real t)
348 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
349 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
354 GMX_MPE_LOG(ev_add_rot_forces_start);
356 /* Reduce energy,torque, angles etc. to get the sum values (per rotation group)
357 * on the master and output these values to file. */
358 if (do_per_step(step, rot->nsttout))
359 reduce_output(cr, rot, t);
361 /* Total rotation potential is the sum over all rotation groups */
364 /* Loop over enforced rotation groups (usually 1, though)
365 * Apply the forces from rotation potentials */
366 for (g=0; g<rot->ngrp; g++)
371 for (l=0; l<erg->nat_loc; l++)
373 /* Get the right index of the local force */
374 ii = erg->ind_loc[l];
376 rvec_inc(f[ii],erg->f_rot_loc[l]);
380 GMX_MPE_LOG(ev_add_rot_forces_finish);
382 return (MASTER(cr)? er->Vrot : 0.0);
386 /* Calculate the maximum beta that leads to a gaussian larger min_gaussian,
387 * also does some checks
389 static double calc_beta_max(real min_gaussian, real slab_dist)
391 const double norm = 0.5698457353514458216; /* = 1/1.7548609 */
396 /* Actually the next two checks are already made in grompp */
398 gmx_fatal(FARGS, "Slab distance of flexible rotation groups must be >=0 !");
399 if (min_gaussian <= 0)
400 gmx_fatal(FARGS, "Cutoff value for Gaussian must be > 0. (You requested %f)");
402 /* Define the sigma value */
403 sigma = 0.7*slab_dist;
405 /* Calculate the argument for the logarithm and check that the log() result is negative or 0 */
406 arg = min_gaussian/norm;
408 gmx_fatal(FARGS, "min_gaussian of flexible rotation groups must be <%g", norm);
410 return sqrt(-2.0*sigma*sigma*log(min_gaussian/norm));
414 static inline real calc_beta(rvec curr_x, t_rotgrp *rotg, int n)
416 return iprod(curr_x, rotg->vec) - rotg->slab_dist * n;
420 static inline real gaussian_weight(rvec curr_x, t_rotgrp *rotg, int n)
422 /* norm is chosen such that the sum of the gaussians
423 * over the slabs is approximately 1.0 everywhere */
424 /* a previously used value was norm = 0.5698457353514458216 = 1/1.7548609 */
425 const real norm = 0.569917543430618; /* = 1/1.7546397922417 */
429 /* Define the sigma value */
430 sigma = 0.7*rotg->slab_dist;
431 /* Calculate the Gaussian value of slab n for position curr_x */
432 return norm * exp( -0.5 * sqr( calc_beta(curr_x, rotg, n)/sigma ) );
436 /* Returns the weight in a single slab, also calculates the Gaussian- and mass-
437 * weighted sum of positions for that slab */
438 static real get_slab_weight(int j, t_rotgrp *rotg, rvec xc[], real mc[], rvec *x_weighted_sum)
440 rvec curr_x; /* The position of an atom */
441 rvec curr_x_weighted; /* The gaussian-weighted position */
442 real gaussian; /* A single gaussian weight */
443 real wgauss; /* gaussian times current mass */
444 real slabweight = 0.0; /* The sum of weights in the slab */
446 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
450 clear_rvec(*x_weighted_sum);
453 islab = j - erg->slab_first;
455 /* Loop over all atoms in the rotation group */
456 for (i=0; i<rotg->nat; i++)
458 copy_rvec(xc[i], curr_x);
459 gaussian = gaussian_weight(curr_x, rotg, j);
460 wgauss = gaussian * mc[i];
461 svmul(wgauss, curr_x, curr_x_weighted);
462 rvec_add(*x_weighted_sum, curr_x_weighted, *x_weighted_sum);
463 slabweight += wgauss;
464 } /* END of loop over rotation group atoms */
470 static void get_slab_centers(
471 t_rotgrp *rotg, /* The rotation group information */
472 rvec *xc, /* The rotation group positions; will
473 typically be enfrotgrp->xc, but at first call
474 it is enfrotgrp->xc_ref */
475 real *mc, /* The masses of the rotation group atoms */
476 t_commrec *cr, /* Communication record */
477 int g, /* The number of the rotation group */
478 real time, /* Used for output only */
479 FILE *out_slabs, /* For outputting center per slab information */
480 gmx_bool bOutStep, /* Is this an output step? */
481 gmx_bool bReference) /* If this routine is called from
482 init_rot_group we need to store
483 the reference slab centers */
486 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
491 /* Loop over slabs */
492 for (j = erg->slab_first; j <= erg->slab_last; j++)
494 islab = j - erg->slab_first;
495 erg->slab_weights[islab] = get_slab_weight(j, rotg, xc, mc, &erg->slab_center[islab]);
497 /* We can do the calculations ONLY if there is weight in the slab! */
498 if (erg->slab_weights[islab] > WEIGHT_MIN)
500 svmul(1.0/erg->slab_weights[islab], erg->slab_center[islab], erg->slab_center[islab]);
504 /* We need to check this here, since we divide through slab_weights
505 * in the flexible low-level routines! */
506 gmx_fatal(FARGS, "Not enough weight in slab %d. Slab center cannot be determined!", j);
509 /* At first time step: save the centers of the reference structure */
511 copy_rvec(erg->slab_center[islab], erg->slab_center_ref[islab]);
512 } /* END of loop over slabs */
514 /* Output on the master */
515 if (MASTER(cr) && bOutStep)
517 fprintf(out_slabs, "%12.3e%6d", time, g);
518 for (j = erg->slab_first; j <= erg->slab_last; j++)
520 islab = j - erg->slab_first;
521 fprintf(out_slabs, "%6d%12.3e%12.3e%12.3e",
522 j,erg->slab_center[islab][XX],erg->slab_center[islab][YY],erg->slab_center[islab][ZZ]);
524 fprintf(out_slabs, "\n");
529 static void calc_rotmat(
531 real degangle, /* Angle alpha of rotation at time t in degrees */
532 matrix rotmat) /* Rotation matrix */
534 real radangle; /* Rotation angle in radians */
535 real cosa; /* cosine alpha */
536 real sina; /* sine alpha */
537 real OMcosa; /* 1 - cos(alpha) */
538 real dumxy, dumxz, dumyz; /* save computations */
539 rvec rot_vec; /* Rotate around rot_vec ... */
542 radangle = degangle * M_PI/180.0;
543 copy_rvec(vec , rot_vec );
545 /* Precompute some variables: */
546 cosa = cos(radangle);
547 sina = sin(radangle);
549 dumxy = rot_vec[XX]*rot_vec[YY]*OMcosa;
550 dumxz = rot_vec[XX]*rot_vec[ZZ]*OMcosa;
551 dumyz = rot_vec[YY]*rot_vec[ZZ]*OMcosa;
553 /* Construct the rotation matrix for this rotation group: */
555 rotmat[XX][XX] = cosa + rot_vec[XX]*rot_vec[XX]*OMcosa;
556 rotmat[YY][XX] = dumxy + rot_vec[ZZ]*sina;
557 rotmat[ZZ][XX] = dumxz - rot_vec[YY]*sina;
559 rotmat[XX][YY] = dumxy - rot_vec[ZZ]*sina;
560 rotmat[YY][YY] = cosa + rot_vec[YY]*rot_vec[YY]*OMcosa;
561 rotmat[ZZ][YY] = dumyz + rot_vec[XX]*sina;
563 rotmat[XX][ZZ] = dumxz + rot_vec[YY]*sina;
564 rotmat[YY][ZZ] = dumyz - rot_vec[XX]*sina;
565 rotmat[ZZ][ZZ] = cosa + rot_vec[ZZ]*rot_vec[ZZ]*OMcosa;
570 for (iii=0; iii<3; iii++) {
571 for (jjj=0; jjj<3; jjj++)
572 fprintf(stderr, " %10.8f ", rotmat[iii][jjj]);
573 fprintf(stderr, "\n");
579 /* Calculates torque on the rotation axis tau = position x force */
580 static inline real torque(
581 rvec rotvec, /* rotation vector; MUST be normalized! */
582 rvec force, /* force */
583 rvec x, /* position of atom on which the force acts */
584 rvec pivot) /* pivot point of rotation axis */
589 /* Subtract offset */
590 rvec_sub(x,pivot,vectmp);
592 /* position x force */
593 cprod(vectmp, force, tau);
595 /* Return the part of the torque which is parallel to the rotation vector */
596 return iprod(tau, rotvec);
600 /* Right-aligned output of value with standard width */
601 static void print_aligned(FILE *fp, char *str)
603 fprintf(fp, "%12s", str);
607 /* Right-aligned output of value with standard short width */
608 static void print_aligned_short(FILE *fp, char *str)
610 fprintf(fp, "%6s", str);
614 /* Right-aligned output of value with standard width */
615 static void print_aligned_group(FILE *fp, char *str, int g)
620 sprintf(sbuf, "%s%d", str, g);
621 fprintf(fp, "%12s", sbuf);
625 static FILE *open_output_file(const char *fn, int steps, const char what[])
630 fp = ffopen(fn, "w");
632 fprintf(fp, "# Output of %s is written at intervals of %d time step%s.\n#\n",
633 what,steps, steps>1 ? "s":"");
639 /* Open output file for slab center data. Call on master only */
640 static FILE *open_slab_out(t_rot *rot, const char *fn)
647 for (g=0; g<rot->ngrp; g++)
650 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
651 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
654 fp = open_output_file(fn, rot->nsttout, "gaussian weighted slab centers");
655 fprintf(fp, "# Rotation group %d (%s), slab distance %f nm, %s.\n",
656 g, erotg_names[rotg->eType], rotg->slab_dist, rotg->bMassW? "centers of mass":"geometrical centers");
662 fprintf(fp, "# Reference centers are listed first (t=-1).\n");
663 fprintf(fp, "# The following columns have the syntax:\n");
665 print_aligned_short(fp, "t");
666 print_aligned_short(fp, "grp");
667 /* Print ascii legend for the first two entries only ... */
670 print_aligned_short(fp, "slab");
671 print_aligned(fp, "X center");
672 print_aligned(fp, "Y center");
673 print_aligned(fp, "Z center");
675 fprintf(fp, " ...\n");
683 /* Open output file and print some general information about the rotation groups.
684 * Call on master only */
685 static FILE *open_rot_out(const char *fn, t_rot *rot, const output_env_t oenv,
691 const char **setname;
693 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
697 if (Flags & MD_APPENDFILES)
699 fp = gmx_fio_fopen(fn,"a");
703 fp = xvgropen(fn, "Rotation angles and energy", "Time [ps]", "angles [degree] and energies [kJ/mol]", oenv);
704 fprintf(fp, "# The scalar tau is the torque [kJ/mol] in the direction of the rotation vector v.\n");
705 fprintf(fp, "# To obtain the vectorial torque, multiply tau with the group's rot_vec.\n#\n");
707 for (g=0; g<rot->ngrp; g++)
711 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
712 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
715 fprintf(fp, "# Rotation group %d, potential type '%s':\n" , g, erotg_names[rotg->eType]);
716 fprintf(fp, "# rot_massw%d %s\n" , g, yesno_names[rotg->bMassW]);
717 fprintf(fp, "# rot_vec%d %12.5e %12.5e %12.5e\n" , g, rotg->vec[XX], rotg->vec[YY], rotg->vec[ZZ]);
718 fprintf(fp, "# rot_rate%d %12.5e degree/ps\n" , g, rotg->rate);
719 fprintf(fp, "# rot_k%d %12.5e kJ/(mol*nm^2)\n" , g, rotg->k);
720 if ( rotg->eType==erotgISO || rotg->eType==erotgPM || rotg->eType==erotgRM || rotg->eType==erotgRM2)
721 fprintf(fp, "# rot_pivot%d %12.5e %12.5e %12.5e nm\n", g, rotg->pivot[XX], rotg->pivot[YY], rotg->pivot[ZZ]);
725 fprintf(fp, "# rot_slab_distance%d %f nm\n", g, rotg->slab_dist);
726 fprintf(fp, "# rot_min_gaussian%d %12.5e\n", g, rotg->min_gaussian);
729 /* Output the centers of the rotation groups for the pivot-free potentials */
730 if ((rotg->eType==erotgISOPF) || (rotg->eType==erotgPMPF) || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF
731 || (rotg->eType==erotgFLEXT) || (rotg->eType==erotgFLEX2T)) )
733 fprintf(fp, "# %s of ref. grp. %d %12.5e %12.5e %12.5e\n",
734 rotg->bMassW? "COM":"COG", g,
735 erg->xc_ref_center[XX], erg->xc_ref_center[YY], erg->xc_ref_center[ZZ]);
737 fprintf(fp, "# initial %s grp. %d %12.5e %12.5e %12.5e\n",
738 rotg->bMassW? "COM":"COG", g,
739 erg->xc_center[XX], erg->xc_center[YY], erg->xc_center[ZZ]);
742 if ( (rotg->eType == erotgRM2) || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
744 fprintf(fp, "# rot_eps%d %12.5e nm^2\n", g, rotg->eps);
748 fprintf(fp, "#\n# Legend for the following data columns:\n");
750 print_aligned_short(fp, "t");
752 snew(setname, 4*rot->ngrp);
754 for (g=0; g<rot->ngrp; g++)
757 sprintf(buf, "theta_ref%d [degree]", g);
758 print_aligned_group(fp, "theta_ref", g);
759 setname[nsets] = strdup(buf);
762 for (g=0; g<rot->ngrp; g++)
765 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
766 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
768 /* For flexible axis rotation we use RMSD fitting to determine the
769 * actual angle of the rotation group */
772 sprintf(buf, "theta-av%d [degree]", g);
773 print_aligned_group(fp, "theta_av", g);
774 setname[nsets] = strdup(buf);
776 sprintf(buf, "tau%d [kJ/mol]", g);
777 print_aligned_group(fp, "tau", g);
778 setname[nsets] = strdup(buf);
781 sprintf(buf, "energy%d [kJ/mol]", g);
782 print_aligned_group(fp, "energy", g);
783 setname[nsets] = strdup(buf);
786 fprintf(fp, "\n#\n");
789 xvgr_legend(fp, nsets, setname, oenv);
799 /* Call on master only */
800 static FILE *open_angles_out(t_rot *rot, const char *fn)
807 /* Open output file and write some information about it's structure: */
808 fp = open_output_file(fn, rot->nstrout, "rotation group angles");
809 fprintf(fp, "# All angles given in degrees, time in ps.\n");
810 for (g=0; g<rot->ngrp; g++)
813 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
814 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
816 fprintf(fp, "# Rotation group %d (%s), slab distance %f nm, fit type %s.\n",
817 g, erotg_names[rotg->eType], rotg->slab_dist, erotg_fitnames[rotg->eFittype]);
820 fprintf(fp, "# The following columns will have the syntax:\n");
822 print_aligned_short(fp, "t");
823 print_aligned_short(fp, "grp");
824 print_aligned(fp, "theta_ref");
825 print_aligned(fp, "theta_fit");
826 print_aligned_short(fp, "slab");
827 print_aligned_short(fp, "atoms");
828 print_aligned(fp, "theta_fit");
829 print_aligned_short(fp, "slab");
830 print_aligned_short(fp, "atoms");
831 print_aligned(fp, "theta_fit");
832 fprintf(fp, " ...\n");
838 /* Open torque output file and write some information about it's structure.
839 * Call on master only */
840 static FILE *open_torque_out(t_rot *rot, const char *fn)
847 fp = open_output_file(fn, rot->nsttout,"torques");
849 for (g=0; g<rot->ngrp; g++)
852 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
853 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
855 fprintf(fp, "# Rotation group %d (%s), slab distance %f nm.\n", g, erotg_names[rotg->eType], rotg->slab_dist);
856 fprintf(fp, "# The scalar tau is the torque [kJ/mol] in the direction of the rotation vector.\n");
857 fprintf(fp, "# To obtain the vectorial torque, multiply tau with\n");
858 fprintf(fp, "# rot_vec%d %10.3e %10.3e %10.3e\n", g, rotg->vec[XX], rotg->vec[YY], rotg->vec[ZZ]);
862 fprintf(fp, "# The following columns will have the syntax (tau=torque for that slab):\n");
864 print_aligned_short(fp, "t");
865 print_aligned_short(fp, "grp");
866 print_aligned_short(fp, "slab");
867 print_aligned(fp, "tau");
868 print_aligned_short(fp, "slab");
869 print_aligned(fp, "tau");
870 fprintf(fp, " ...\n");
877 static void swap_val(double* vec, int i, int j)
887 static void swap_col(double **mat, int i, int j)
889 double tmp[3] = {mat[0][j], mat[1][j], mat[2][j]};
902 /* Eigenvectors are stored in columns of eigen_vec */
903 static void diagonalize_symmetric(
911 jacobi(matrix,3,eigenval,eigen_vec,&n_rot);
913 /* sort in ascending order */
914 if (eigenval[0] > eigenval[1])
916 swap_val(eigenval, 0, 1);
917 swap_col(eigen_vec, 0, 1);
919 if (eigenval[1] > eigenval[2])
921 swap_val(eigenval, 1, 2);
922 swap_col(eigen_vec, 1, 2);
924 if (eigenval[0] > eigenval[1])
926 swap_val(eigenval, 0, 1);
927 swap_col(eigen_vec, 0, 1);
932 static void align_with_z(
933 rvec* s, /* Structure to align */
938 rvec zet = {0.0, 0.0, 1.0};
939 rvec rot_axis={0.0, 0.0, 0.0};
940 rvec *rotated_str=NULL;
946 snew(rotated_str, natoms);
948 /* Normalize the axis */
949 ooanorm = 1.0/norm(axis);
950 svmul(ooanorm, axis, axis);
952 /* Calculate the angle for the fitting procedure */
953 cprod(axis, zet, rot_axis);
954 angle = acos(axis[2]);
958 /* Calculate the rotation matrix */
959 calc_rotmat(rot_axis, angle*180.0/M_PI, rotmat);
961 /* Apply the rotation matrix to s */
962 for (i=0; i<natoms; i++)
968 rotated_str[i][j] += rotmat[j][k]*s[i][k];
973 /* Rewrite the rotated structure to s */
974 for(i=0; i<natoms; i++)
978 s[i][j]=rotated_str[i][j];
986 static void calc_correl_matrix(rvec* Xstr, rvec* Ystr, double** Rmat, int natoms)
997 for (k=0; k<natoms; k++)
998 Rmat[i][j] += Ystr[k][i] * Xstr[k][j];
1002 static void weigh_coords(rvec* str, real* weight, int natoms)
1007 for(i=0; i<natoms; i++)
1010 str[i][j] *= sqrt(weight[i]);
1015 static double opt_angle_analytic(
1028 double **Rmat, **RtR, **eigvec;
1030 double V[3][3], WS[3][3];
1031 double rot_matrix[3][3];
1035 /* Do not change the original coordinates */
1036 snew(ref_s_1, natoms);
1037 snew(act_s_1, natoms);
1038 for(i=0; i<natoms; i++)
1040 copy_rvec(ref_s[i], ref_s_1[i]);
1041 copy_rvec(act_s[i], act_s_1[i]);
1044 /* Translate the structures to the origin */
1045 shift[XX] = -ref_com[XX];
1046 shift[YY] = -ref_com[YY];
1047 shift[ZZ] = -ref_com[ZZ];
1048 translate_x(ref_s_1, natoms, shift);
1050 shift[XX] = -act_com[XX];
1051 shift[YY] = -act_com[YY];
1052 shift[ZZ] = -act_com[ZZ];
1053 translate_x(act_s_1, natoms, shift);
1055 /* Align rotation axis with z */
1056 align_with_z(ref_s_1, natoms, axis);
1057 align_with_z(act_s_1, natoms, axis);
1059 /* Correlation matrix */
1060 Rmat = allocate_square_matrix(3);
1062 for (i=0; i<natoms; i++)
1068 /* Weight positions with sqrt(weight) */
1071 weigh_coords(ref_s_1, weight, natoms);
1072 weigh_coords(act_s_1, weight, natoms);
1075 /* Calculate correlation matrices R=YXt (X=ref_s; Y=act_s) */
1076 calc_correl_matrix(ref_s_1, act_s_1, Rmat, natoms);
1079 RtR = allocate_square_matrix(3);
1086 RtR[i][j] += Rmat[k][i] * Rmat[k][j];
1090 /* Diagonalize RtR */
1095 diagonalize_symmetric(RtR, eigvec, eigval);
1096 swap_col(eigvec,0,1);
1097 swap_col(eigvec,1,2);
1098 swap_val(eigval,0,1);
1099 swap_val(eigval,1,2);
1113 WS[i][j] = eigvec[i][j] / sqrt(eigval[j]);
1121 V[i][j] += Rmat[i][k]*WS[k][j];
1125 free_square_matrix(Rmat, 3);
1127 /* Calculate optimal rotation matrix */
1130 rot_matrix[i][j] = 0.0;
1137 rot_matrix[i][j] += eigvec[i][k]*V[j][k];
1141 rot_matrix[2][2] = 1.0;
1143 /* In some cases abs(rot_matrix[0][0]) can be slighly larger
1144 * than unity due to numerical inacurracies. To be able to calculate
1145 * the acos function, we put these values back in range. */
1146 if (rot_matrix[0][0] > 1.0)
1148 rot_matrix[0][0] = 1.0;
1150 else if (rot_matrix[0][0] < -1.0)
1152 rot_matrix[0][0] = -1.0;
1155 /* Determine the optimal rotation angle: */
1156 opt_angle = (-1.0)*acos(rot_matrix[0][0])*180.0/M_PI;
1157 if (rot_matrix[0][1] < 0.0)
1158 opt_angle = (-1.0)*opt_angle;
1160 /* Give back some memory */
1161 free_square_matrix(RtR, 3);
1172 /* Determine actual angle of this slab by RMSD fit to the reference */
1173 /* Not parallelized, call this routine only on the master */
1174 static void flex_fit_angle(
1181 int i,l,n,islab,ind;
1183 rvec *fitcoords=NULL;
1184 rvec act_center; /* Center of actual positions that are passed to the fit routine */
1185 rvec ref_center; /* Same for the reference positions */
1186 double fitangle; /* This will be the actual angle of the rotation group derived
1187 * from an RMSD fit to the reference structure at t=0 */
1191 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1192 real OOm_av; /* 1/average_mass of a rotation group atom */
1193 real m_rel; /* Relative mass of a rotation group atom */
1196 erg=rotg->enfrotgrp;
1198 /* Average mass of a rotation group atom: */
1199 OOm_av = erg->invmass*rotg->nat;
1201 /**********************************/
1202 /* First collect the data we need */
1203 /**********************************/
1205 /* Collect the data for the individual slabs */
1206 for (n = erg->slab_first; n <= erg->slab_last; n++)
1208 islab = n - erg->slab_first; /* slab index */
1209 sd = &(rotg->enfrotgrp->slab_data[islab]);
1210 sd->nat = erg->lastatom[islab]-erg->firstatom[islab]+1;
1213 /* Loop over the relevant atoms in the slab */
1214 for (l=erg->firstatom[islab]; l<=erg->lastatom[islab]; l++)
1216 /* Current position of this atom: x[ii][XX/YY/ZZ] */
1217 copy_rvec(erg->xc[l], curr_x);
1219 /* The (unrotated) reference position of this atom is copied to ref_x.
1220 * Beware, the xc coords have been sorted in do_flexible */
1221 copy_rvec(erg->xc_ref_sorted[l], ref_x);
1223 /* Save data for doing angular RMSD fit later */
1224 /* Save the current atom position */
1225 copy_rvec(curr_x, sd->x[ind]);
1226 /* Save the corresponding reference position */
1227 copy_rvec(ref_x , sd->ref[ind]);
1229 /* Maybe also mass-weighting was requested. If yes, additionally
1230 * multiply the weights with the relative mass of the atom. If not,
1231 * multiply with unity. */
1232 m_rel = erg->mc_sorted[l]*OOm_av;
1234 /* Save the weight for this atom in this slab */
1235 sd->weight[ind] = gaussian_weight(curr_x, rotg, n) * m_rel;
1237 /* Next atom in this slab */
1242 /* Get the center of the whole rotation group. Note, again, the erg->xc have
1243 * been sorted in do_flexible */
1244 get_center(erg->xc, erg->mc_sorted, rotg->nat, act_center);
1246 /******************************/
1247 /* Now do the fit calculation */
1248 /******************************/
1250 /* === Determine the optimal fit angle for the whole rotation group === */
1251 if (rotg->eFittype == erotgFitNORM)
1253 /* Normalize every position to it's reference length
1254 * prior to performing the fit */
1255 for (i=0; i<rotg->nat; i++)
1257 /* First put the center of the positions into the origin */
1258 rvec_sub(erg->xc[i], act_center, coord);
1259 /* Determine the scaling factor for the length: */
1260 scal = erg->xc_ref_length[erg->xc_sortind[i]] / norm(coord);
1261 /* Get position, multiply with the scaling factor and save in buf[i] */
1262 svmul(scal, coord, erg->xc_norm[i]);
1264 fitcoords = erg->xc_norm;
1268 fitcoords = erg->xc;
1270 /* Note that from the point of view of the current positions, the reference has rotated backwards,
1271 * but we want to output the angle relative to the fixed reference, therefore the minus sign. */
1272 fitangle = -opt_angle_analytic(erg->xc_ref_sorted, fitcoords, erg->mc_sorted,
1273 rotg->nat, erg->xc_ref_center, act_center, rotg->vec);
1274 fprintf(fp, "%12.3e%6d%12.3f%12.3lf", t, g, degangle, fitangle);
1277 /* === Now do RMSD fitting for each slab === */
1278 /* We require at least SLAB_MIN_ATOMS in a slab, such that the fit makes sense. */
1279 #define SLAB_MIN_ATOMS 4
1281 for (n = erg->slab_first; n <= erg->slab_last; n++)
1283 islab = n - erg->slab_first; /* slab index */
1284 sd = &(rotg->enfrotgrp->slab_data[islab]);
1285 if (sd->nat >= SLAB_MIN_ATOMS)
1287 /* Get the center of the slabs reference and current positions */
1288 get_center(sd->ref, sd->weight, sd->nat, ref_center);
1289 get_center(sd->x , sd->weight, sd->nat, act_center);
1290 if (rotg->eFittype == erotgFitNORM)
1292 /* Normalize every position to it's reference length
1293 * prior to performing the fit */
1294 for (i=0; i<sd->nat;i++) /* Center */
1296 rvec_dec(sd->ref[i], ref_center);
1297 rvec_dec(sd->x[i] , act_center);
1298 /* Normalize x_i such that it gets the same length as ref_i */
1299 svmul( norm(sd->ref[i])/norm(sd->x[i]), sd->x[i], sd->x[i] );
1301 /* We already subtracted the centers */
1302 clear_rvec(ref_center);
1303 clear_rvec(act_center);
1305 fitangle = -opt_angle_analytic(sd->ref, sd->x, sd->weight, sd->nat, ref_center, act_center, rotg->vec);
1306 fprintf(fp, "%6d%6d%12.3f", n, sd->nat, fitangle);
1311 #undef SLAB_MIN_ATOMS
1315 /* Shift x with is */
1316 static inline void shift_single_coord(matrix box, rvec x, const ivec is)
1327 x[XX] += tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
1328 x[YY] += ty*box[YY][YY]+tz*box[ZZ][YY];
1329 x[ZZ] += tz*box[ZZ][ZZ];
1332 x[XX] += tx*box[XX][XX];
1333 x[YY] += ty*box[YY][YY];
1334 x[ZZ] += tz*box[ZZ][ZZ];
1339 /* Determine the 'home' slab of this atom which is the
1340 * slab with the highest Gaussian weight of all */
1341 #define round(a) (int)(a+0.5)
1342 static inline int get_homeslab(
1343 rvec curr_x, /* The position for which the home slab shall be determined */
1344 rvec rotvec, /* The rotation vector */
1345 real slabdist) /* The slab distance */
1350 /* The distance of the atom to the coordinate center (where the
1351 * slab with index 0) is */
1352 dist = iprod(rotvec, curr_x);
1354 return round(dist / slabdist);
1358 /* For a local atom determine the relevant slabs, i.e. slabs in
1359 * which the gaussian is larger than min_gaussian
1361 static int get_single_atom_gaussians(
1369 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1372 erg=rotg->enfrotgrp;
1374 /* Determine the 'home' slab of this atom: */
1375 homeslab = get_homeslab(curr_x, rotg->vec, rotg->slab_dist);
1377 /* First determine the weight in the atoms home slab: */
1378 g = gaussian_weight(curr_x, rotg, homeslab);
1380 erg->gn_atom[count] = g;
1381 erg->gn_slabind[count] = homeslab;
1385 /* Determine the max slab */
1387 while (g > rotg->min_gaussian)
1390 g = gaussian_weight(curr_x, rotg, slab);
1391 erg->gn_slabind[count]=slab;
1392 erg->gn_atom[count]=g;
1397 /* Determine the max slab */
1402 g = gaussian_weight(curr_x, rotg, slab);
1403 erg->gn_slabind[count]=slab;
1404 erg->gn_atom[count]=g;
1407 while (g > rotg->min_gaussian);
1414 static void flex2_precalc_inner_sum(t_rotgrp *rotg, t_commrec *cr)
1417 rvec xi; /* positions in the i-sum */
1418 rvec xcn, ycn; /* the current and the reference slab centers */
1421 rvec rin; /* Helper variables */
1424 real OOpsii,OOpsiistar;
1425 real sin_rin; /* s_ii.r_ii */
1426 rvec s_in,tmpvec,tmpvec2;
1427 real mi,wi; /* Mass-weighting of the positions */
1429 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1432 erg=rotg->enfrotgrp;
1433 N_M = rotg->nat * erg->invmass;
1435 /* Loop over all slabs that contain something */
1436 for (n=erg->slab_first; n <= erg->slab_last; n++)
1438 islab = n - erg->slab_first; /* slab index */
1440 /* The current center of this slab is saved in xcn: */
1441 copy_rvec(erg->slab_center[islab], xcn);
1442 /* ... and the reference center in ycn: */
1443 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1445 /*** D. Calculate the whole inner sum used for second and third sum */
1446 /* For slab n, we need to loop over all atoms i again. Since we sorted
1447 * the atoms with respect to the rotation vector, we know that it is sufficient
1448 * to calculate from firstatom to lastatom only. All other contributions will
1450 clear_rvec(innersumvec);
1451 for (i = erg->firstatom[islab]; i <= erg->lastatom[islab]; i++)
1453 /* Coordinate xi of this atom */
1454 copy_rvec(erg->xc[i],xi);
1457 gaussian_xi = gaussian_weight(xi,rotg,n);
1458 mi = erg->mc_sorted[i]; /* need the sorted mass here */
1462 copy_rvec(erg->xc_ref_sorted[i],yi0); /* Reference position yi0 */
1463 rvec_sub(yi0, ycn, tmpvec2); /* tmpvec2 = yi0 - ycn */
1464 mvmul(erg->rotmat, tmpvec2, rin); /* rin = Omega.(yi0 - ycn) */
1466 /* Calculate psi_i* and sin */
1467 rvec_sub(xi, xcn, tmpvec2); /* tmpvec2 = xi - xcn */
1468 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x (xi - xcn) */
1469 OOpsiistar = norm2(tmpvec)+rotg->eps; /* OOpsii* = 1/psii* = |v x (xi-xcn)|^2 + eps */
1470 OOpsii = norm(tmpvec); /* OOpsii = 1 / psii = |v x (xi - xcn)| */
1472 /* v x (xi - xcn) */
1473 unitv(tmpvec, s_in); /* sin = ---------------- */
1474 /* |v x (xi - xcn)| */
1476 sin_rin=iprod(s_in,rin); /* sin_rin = sin . rin */
1478 /* Now the whole sum */
1479 fac = OOpsii/OOpsiistar;
1480 svmul(fac, rin, tmpvec);
1481 fac2 = fac*fac*OOpsii;
1482 svmul(fac2*sin_rin, s_in, tmpvec2);
1483 rvec_dec(tmpvec, tmpvec2);
1485 svmul(wi*gaussian_xi*sin_rin, tmpvec, tmpvec2);
1487 rvec_inc(innersumvec,tmpvec2);
1488 } /* now we have the inner sum, used both for sum2 and sum3 */
1490 /* Save it to be used in do_flex2_lowlevel */
1491 copy_rvec(innersumvec, erg->slab_innersumvec[islab]);
1492 } /* END of loop over slabs */
1496 static void flex_precalc_inner_sum(t_rotgrp *rotg, t_commrec *cr)
1499 rvec xi; /* position */
1500 rvec xcn, ycn; /* the current and the reference slab centers */
1501 rvec qin,rin; /* q_i^n and r_i^n */
1504 rvec innersumvec; /* Inner part of sum_n2 */
1505 real gaussian_xi; /* Gaussian weight gn(xi) */
1506 real mi,wi; /* Mass-weighting of the positions */
1509 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1512 erg=rotg->enfrotgrp;
1513 N_M = rotg->nat * erg->invmass;
1515 /* Loop over all slabs that contain something */
1516 for (n=erg->slab_first; n <= erg->slab_last; n++)
1518 islab = n - erg->slab_first; /* slab index */
1520 /* The current center of this slab is saved in xcn: */
1521 copy_rvec(erg->slab_center[islab], xcn);
1522 /* ... and the reference center in ycn: */
1523 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1525 /* For slab n, we need to loop over all atoms i again. Since we sorted
1526 * the atoms with respect to the rotation vector, we know that it is sufficient
1527 * to calculate from firstatom to lastatom only. All other contributions will
1529 clear_rvec(innersumvec);
1530 for (i=erg->firstatom[islab]; i<=erg->lastatom[islab]; i++)
1532 /* Coordinate xi of this atom */
1533 copy_rvec(erg->xc[i],xi);
1536 gaussian_xi = gaussian_weight(xi,rotg,n);
1537 mi = erg->mc_sorted[i]; /* need the sorted mass here */
1540 /* Calculate rin and qin */
1541 rvec_sub(erg->xc_ref_sorted[i], ycn, tmpvec); /* tmpvec = yi0-ycn */
1542 mvmul(erg->rotmat, tmpvec, rin); /* rin = Omega.(yi0 - ycn) */
1543 cprod(rotg->vec, rin, tmpvec); /* tmpvec = v x Omega*(yi0-ycn) */
1545 /* v x Omega*(yi0-ycn) */
1546 unitv(tmpvec, qin); /* qin = --------------------- */
1547 /* |v x Omega*(yi0-ycn)| */
1550 rvec_sub(xi, xcn, tmpvec); /* tmpvec = xi-xcn */
1551 bin = iprod(qin, tmpvec); /* bin = qin*(xi-xcn) */
1553 svmul(wi*gaussian_xi*bin, qin, tmpvec);
1555 /* Add this contribution to the inner sum: */
1556 rvec_add(innersumvec, tmpvec, innersumvec);
1557 } /* now we have the inner sum vector S^n for this slab */
1558 /* Save it to be used in do_flex_lowlevel */
1559 copy_rvec(innersumvec, erg->slab_innersumvec[islab]);
1564 static real do_flex2_lowlevel(
1566 real sigma, /* The Gaussian width sigma */
1568 gmx_bool bCalcTorque,
1572 int count,ic,ii,j,m,n,islab,iigrp;
1573 rvec xj; /* position in the i-sum */
1574 rvec yj0; /* the reference position in the j-sum */
1575 rvec xcn, ycn; /* the current and the reference slab centers */
1576 real V; /* This node's part of the rotation pot. energy */
1577 real gaussian_xj; /* Gaussian weight */
1581 rvec rjn; /* Helper variables */
1584 real OOpsij,OOpsijstar;
1585 real OOsigma2; /* 1/(sigma^2) */
1588 rvec sjn,tmpvec,tmpvec2;
1589 rvec sum1vec_part,sum1vec,sum2vec_part,sum2vec,sum3vec,sum4vec,innersumvec;
1591 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1592 real mj,wj; /* Mass-weighting of the positions */
1594 real Wjn; /* g_n(x_j) m_j / Mjn */
1596 /* To calculate the torque per slab */
1597 rvec slab_force; /* Single force from slab n on one atom */
1598 rvec slab_sum1vec_part;
1599 real slab_sum3part,slab_sum4part;
1600 rvec slab_sum1vec, slab_sum2vec, slab_sum3vec, slab_sum4vec;
1603 erg=rotg->enfrotgrp;
1605 /* Pre-calculate the inner sums, so that we do not have to calculate
1606 * them again for every atom */
1607 flex2_precalc_inner_sum(rotg, cr);
1609 /********************************************************/
1610 /* Main loop over all local atoms of the rotation group */
1611 /********************************************************/
1612 N_M = rotg->nat * erg->invmass;
1614 OOsigma2 = 1.0 / (sigma*sigma);
1615 for (j=0; j<erg->nat_loc; j++)
1617 /* Local index of a rotation group atom */
1618 ii = erg->ind_loc[j];
1619 /* Position of this atom in the collective array */
1620 iigrp = erg->xc_ref_ind[j];
1621 /* Mass-weighting */
1622 mj = erg->mc[iigrp]; /* need the unsorted mass here */
1625 /* Current position of this atom: x[ii][XX/YY/ZZ]
1626 * Note that erg->xc_center contains the center of mass in case the flex2-t
1627 * potential was chosen. For the flex2 potential erg->xc_center must be
1629 rvec_sub(x[ii], erg->xc_center, xj);
1631 /* Shift this atom such that it is near its reference */
1632 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
1634 /* Determine the slabs to loop over, i.e. the ones with contributions
1635 * larger than min_gaussian */
1636 count = get_single_atom_gaussians(xj, cr, rotg);
1638 clear_rvec(sum1vec_part);
1639 clear_rvec(sum2vec_part);
1642 /* Loop over the relevant slabs for this atom */
1643 for (ic=0; ic < count; ic++)
1645 n = erg->gn_slabind[ic];
1647 /* Get the precomputed Gaussian value of curr_slab for curr_x */
1648 gaussian_xj = erg->gn_atom[ic];
1650 islab = n - erg->slab_first; /* slab index */
1652 /* The (unrotated) reference position of this atom is copied to yj0: */
1653 copy_rvec(rotg->x_ref[iigrp], yj0);
1655 beta = calc_beta(xj, rotg,n);
1657 /* The current center of this slab is saved in xcn: */
1658 copy_rvec(erg->slab_center[islab], xcn);
1659 /* ... and the reference center in ycn: */
1660 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1662 rvec_sub(yj0, ycn, tmpvec2); /* tmpvec2 = yj0 - ycn */
1665 mvmul(erg->rotmat, tmpvec2, rjn); /* rjn = Omega.(yj0 - ycn) */
1667 /* Subtract the slab center from xj */
1668 rvec_sub(xj, xcn, tmpvec2); /* tmpvec2 = xj - xcn */
1671 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x (xj - xcn) */
1673 OOpsijstar = norm2(tmpvec)+rotg->eps; /* OOpsij* = 1/psij* = |v x (xj-xcn)|^2 + eps */
1675 numerator = sqr(iprod(tmpvec, rjn));
1677 /*********************************/
1678 /* Add to the rotation potential */
1679 /*********************************/
1680 V += 0.5*rotg->k*wj*gaussian_xj*numerator/OOpsijstar;
1683 /*************************************/
1684 /* Now calculate the force on atom j */
1685 /*************************************/
1687 OOpsij = norm(tmpvec); /* OOpsij = 1 / psij = |v x (xj - xcn)| */
1689 /* v x (xj - xcn) */
1690 unitv(tmpvec, sjn); /* sjn = ---------------- */
1691 /* |v x (xj - xcn)| */
1693 sjn_rjn=iprod(sjn,rjn); /* sjn_rjn = sjn . rjn */
1696 /*** A. Calculate the first of the four sum terms: ****************/
1697 fac = OOpsij/OOpsijstar;
1698 svmul(fac, rjn, tmpvec);
1699 fac2 = fac*fac*OOpsij;
1700 svmul(fac2*sjn_rjn, sjn, tmpvec2);
1701 rvec_dec(tmpvec, tmpvec2);
1702 fac2 = wj*gaussian_xj; /* also needed for sum4 */
1703 svmul(fac2*sjn_rjn, tmpvec, slab_sum1vec_part);
1704 /********************/
1705 /*** Add to sum1: ***/
1706 /********************/
1707 rvec_inc(sum1vec_part, slab_sum1vec_part); /* sum1 still needs to vector multiplied with v */
1709 /*** B. Calculate the forth of the four sum terms: ****************/
1710 betasigpsi = beta*OOsigma2*OOpsij; /* this is also needed for sum3 */
1711 /********************/
1712 /*** Add to sum4: ***/
1713 /********************/
1714 slab_sum4part = fac2*betasigpsi*fac*sjn_rjn*sjn_rjn; /* Note that fac is still valid from above */
1715 sum4 += slab_sum4part;
1717 /*** C. Calculate Wjn for second and third sum */
1718 /* Note that we can safely divide by slab_weights since we check in
1719 * get_slab_centers that it is non-zero. */
1720 Wjn = gaussian_xj*mj/erg->slab_weights[islab];
1722 /* We already have precalculated the inner sum for slab n */
1723 copy_rvec(erg->slab_innersumvec[islab], innersumvec);
1725 /* Weigh the inner sum vector with Wjn */
1726 svmul(Wjn, innersumvec, innersumvec);
1728 /*** E. Calculate the second of the four sum terms: */
1729 /********************/
1730 /*** Add to sum2: ***/
1731 /********************/
1732 rvec_inc(sum2vec_part, innersumvec); /* sum2 still needs to be vector crossproduct'ed with v */
1734 /*** F. Calculate the third of the four sum terms: */
1735 slab_sum3part = betasigpsi * iprod(sjn, innersumvec);
1736 sum3 += slab_sum3part; /* still needs to be multiplied with v */
1738 /*** G. Calculate the torque on the local slab's axis: */
1742 cprod(slab_sum1vec_part, rotg->vec, slab_sum1vec);
1744 cprod(innersumvec, rotg->vec, slab_sum2vec);
1746 svmul(slab_sum3part, rotg->vec, slab_sum3vec);
1748 svmul(slab_sum4part, rotg->vec, slab_sum4vec);
1750 /* The force on atom ii from slab n only: */
1751 for (m=0; m<DIM; m++)
1752 slab_force[m] = rotg->k * (-slab_sum1vec[m] + slab_sum2vec[m] - slab_sum3vec[m] + 0.5*slab_sum4vec[m]);
1754 erg->slab_torque_v[islab] += torque(rotg->vec, slab_force, xj, xcn);
1756 } /* END of loop over slabs */
1758 /* Construct the four individual parts of the vector sum: */
1759 cprod(sum1vec_part, rotg->vec, sum1vec); /* sum1vec = { } x v */
1760 cprod(sum2vec_part, rotg->vec, sum2vec); /* sum2vec = { } x v */
1761 svmul(sum3, rotg->vec, sum3vec); /* sum3vec = { } . v */
1762 svmul(sum4, rotg->vec, sum4vec); /* sum4vec = { } . v */
1764 /* Store the additional force so that it can be added to the force
1765 * array after the normal forces have been evaluated */
1766 for (m=0; m<DIM; m++)
1767 erg->f_rot_loc[j][m] = rotg->k * (-sum1vec[m] + sum2vec[m] - sum3vec[m] + 0.5*sum4vec[m]);
1770 fprintf(stderr," FORCE on ATOM %d/%d = (%15.8f %15.8f %15.8f) \n",
1771 j,ii,erg->f_rot_loc[j][XX], erg->f_rot_loc[j][YY], erg->f_rot_loc[j][ZZ]);
1775 fprintf(stderr, "sum1: %15.8f %15.8f %15.8f\n", -rotg->k*sum1vec[XX], -rotg->k*sum1vec[YY], -rotg->k*sum1vec[ZZ]);
1776 fprintf(stderr, "sum2: %15.8f %15.8f %15.8f\n", rotg->k*sum2vec[XX], rotg->k*sum2vec[YY], rotg->k*sum2vec[ZZ]);
1777 fprintf(stderr, "sum3: %15.8f %15.8f %15.8f\n", -rotg->k*sum3vec[XX], -rotg->k*sum3vec[YY], -rotg->k*sum3vec[ZZ]);
1778 fprintf(stderr, "sum4: %15.8f %15.8f %15.8f\n", 0.5*rotg->k*sum4vec[XX], 0.5*rotg->k*sum4vec[YY], 0.5*rotg->k*sum4vec[ZZ]);
1780 } /* END of loop over local atoms */
1783 fprintf(stderr, "THE POTENTIAL IS V=%f\n", V);
1790 static real do_flex_lowlevel(
1792 real sigma, /* The Gaussian width sigma */
1794 gmx_bool bCalcTorque,
1798 int count,ic,ii,j,m,n,islab,iigrp;
1799 rvec xj,yj0; /* current and reference position */
1800 rvec xcn, ycn; /* the current and the reference slab centers */
1801 rvec xj_xcn; /* xj - xcn */
1802 rvec qjn; /* q_i^n */
1803 rvec sum_n1,sum_n2; /* Two contributions to the rotation force */
1804 rvec innersumvec; /* Inner part of sum_n2 */
1806 rvec force_n; /* Single force from slab n on one atom */
1807 rvec tmpvec,tmpvec2,tmp_f; /* Helper variables */
1808 real V; /* The rotation potential energy */
1809 real OOsigma2; /* 1/(sigma^2) */
1810 real beta; /* beta_n(xj) */
1811 real bjn; /* b_j^n */
1812 real gaussian_xj; /* Gaussian weight gn(xj) */
1813 real betan_xj_sigma2;
1814 real mj,wj; /* Mass-weighting of the positions */
1816 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1819 erg=rotg->enfrotgrp;
1821 /* Pre-calculate the inner sums, so that we do not have to calculate
1822 * them again for every atom */
1823 flex_precalc_inner_sum(rotg, cr);
1825 /********************************************************/
1826 /* Main loop over all local atoms of the rotation group */
1827 /********************************************************/
1828 OOsigma2 = 1.0/(sigma*sigma);
1829 N_M = rotg->nat * erg->invmass;
1831 for (j=0; j<erg->nat_loc; j++)
1833 /* Local index of a rotation group atom */
1834 ii = erg->ind_loc[j];
1835 /* Position of this atom in the collective array */
1836 iigrp = erg->xc_ref_ind[j];
1837 /* Mass-weighting */
1838 mj = erg->mc[iigrp]; /* need the unsorted mass here */
1841 /* Current position of this atom: x[ii][XX/YY/ZZ]
1842 * Note that erg->xc_center contains the center of mass in case the flex-t
1843 * potential was chosen. For the flex potential erg->xc_center must be
1845 rvec_sub(x[ii], erg->xc_center, xj);
1847 /* Shift this atom such that it is near its reference */
1848 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
1850 /* Determine the slabs to loop over, i.e. the ones with contributions
1851 * larger than min_gaussian */
1852 count = get_single_atom_gaussians(xj, cr, rotg);
1857 /* Loop over the relevant slabs for this atom */
1858 for (ic=0; ic < count; ic++)
1860 n = erg->gn_slabind[ic];
1862 /* Get the precomputed Gaussian for xj in slab n */
1863 gaussian_xj = erg->gn_atom[ic];
1865 islab = n - erg->slab_first; /* slab index */
1867 /* The (unrotated) reference position of this atom is saved in yj0: */
1868 copy_rvec(rotg->x_ref[iigrp], yj0);
1870 beta = calc_beta(xj, rotg, n);
1872 /* The current center of this slab is saved in xcn: */
1873 copy_rvec(erg->slab_center[islab], xcn);
1874 /* ... and the reference center in ycn: */
1875 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1877 rvec_sub(yj0, ycn, tmpvec); /* tmpvec = yj0 - ycn */
1880 mvmul(erg->rotmat, tmpvec, tmpvec2); /* tmpvec2 = Omega.(yj0-ycn) */
1882 /* Subtract the slab center from xj */
1883 rvec_sub(xj, xcn, xj_xcn); /* xj_xcn = xj - xcn */
1886 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x Omega.(xj-xcn) */
1888 /* v x Omega.(xj-xcn) */
1889 unitv(tmpvec,qjn); /* qjn = -------------------- */
1890 /* |v x Omega.(xj-xcn)| */
1892 bjn = iprod(qjn, xj_xcn); /* bjn = qjn * (xj - xcn) */
1894 /*********************************/
1895 /* Add to the rotation potential */
1896 /*********************************/
1897 V += 0.5*rotg->k*wj*gaussian_xj*sqr(bjn);
1899 /****************************************************************/
1900 /* sum_n1 will typically be the main contribution to the force: */
1901 /****************************************************************/
1902 betan_xj_sigma2 = beta*OOsigma2; /* beta_n(xj)/sigma^2 */
1904 /* The next lines calculate
1905 * qjn - (bjn*beta(xj)/(2sigma^2))v */
1906 svmul(bjn*0.5*betan_xj_sigma2, rotg->vec, tmpvec2);
1907 rvec_sub(qjn,tmpvec2,tmpvec);
1909 /* Multiply with gn(xj)*bjn: */
1910 svmul(gaussian_xj*bjn,tmpvec,tmpvec2);
1913 rvec_inc(sum_n1,tmpvec2);
1915 /* We already have precalculated the Sn term for slab n */
1916 copy_rvec(erg->slab_innersumvec[islab], s_n);
1918 svmul(betan_xj_sigma2*iprod(s_n, xj_xcn), rotg->vec, tmpvec); /* tmpvec = ---------- s_n (xj-xcn) */
1921 rvec_sub(s_n, tmpvec, innersumvec);
1923 /* We can safely divide by slab_weights since we check in get_slab_centers
1924 * that it is non-zero. */
1925 svmul(gaussian_xj/erg->slab_weights[islab], innersumvec, innersumvec);
1927 rvec_add(sum_n2, innersumvec, sum_n2);
1929 GMX_MPE_LOG(ev_inner_loop_finish);
1931 /* Calculate the torque: */
1934 /* The force on atom ii from slab n only: */
1935 rvec_sub(innersumvec, tmpvec2, force_n);
1936 svmul(rotg->k, force_n, force_n);
1937 erg->slab_torque_v[islab] += torque(rotg->vec, force_n, xj, xcn);
1939 } /* END of loop over slabs */
1941 /* Put both contributions together: */
1942 svmul(wj, sum_n1, sum_n1);
1943 svmul(mj, sum_n2, sum_n2);
1944 rvec_sub(sum_n2,sum_n1,tmp_f); /* F = -grad V */
1946 /* Store the additional force so that it can be added to the force
1947 * array after the normal forces have been evaluated */
1948 for(m=0; m<DIM; m++)
1949 erg->f_rot_loc[j][m] = rotg->k*tmp_f[m];
1951 fprintf(stderr," FORCE on atom %d = %15.8f %15.8f %15.8f 1: %15.8f %15.8f %15.8f 2: %15.8f %15.8f %15.8f\n", iigrp,
1952 rotg->k*tmp_f[XX] , rotg->k*tmp_f[YY] , rotg->k*tmp_f[ZZ] ,
1953 -rotg->k*sum_n1[XX], -rotg->k*sum_n1[YY], -rotg->k*sum_n1[ZZ],
1954 rotg->k*sum_n2[XX], rotg->k*sum_n2[YY], rotg->k*sum_n2[ZZ]);
1956 } /* END of loop over local atoms */
1962 static void print_coordinates(t_commrec *cr, t_rotgrp *rotg, rvec x[], matrix box, int step)
1966 static char buf[STRLEN];
1967 static gmx_bool bFirst=1;
1972 sprintf(buf, "coords%d.txt", cr->nodeid);
1973 fp = fopen(buf, "w");
1977 fprintf(fp, "\nStep %d\n", step);
1978 fprintf(fp, "box: %f %f %f %f %f %f %f %f %f\n",
1979 box[XX][XX], box[XX][YY], box[XX][ZZ],
1980 box[YY][XX], box[YY][YY], box[YY][ZZ],
1981 box[ZZ][XX], box[ZZ][ZZ], box[ZZ][ZZ]);
1982 for (i=0; i<rotg->nat; i++)
1984 fprintf(fp, "%4d %f %f %f\n", i,
1985 erg->xc[i][XX], erg->xc[i][YY], erg->xc[i][ZZ]);
1993 static int projection_compare(const void *a, const void *b)
1995 sort_along_vec_t *xca, *xcb;
1998 xca = (sort_along_vec_t *)a;
1999 xcb = (sort_along_vec_t *)b;
2001 if (xca->xcproj < xcb->xcproj)
2003 else if (xca->xcproj > xcb->xcproj)
2010 static void sort_collective_coordinates(
2011 t_rotgrp *rotg, /* Rotation group */
2012 sort_along_vec_t *data) /* Buffer for sorting the positions */
2015 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2018 erg=rotg->enfrotgrp;
2020 /* The projection of the position vector on the rotation vector is
2021 * the relevant value for sorting. Fill the 'data' structure */
2022 for (i=0; i<rotg->nat; i++)
2024 data[i].xcproj = iprod(erg->xc[i], rotg->vec); /* sort criterium */
2025 data[i].m = erg->mc[i];
2027 copy_rvec(erg->xc[i] , data[i].x );
2028 copy_rvec(rotg->x_ref[i], data[i].x_ref);
2030 /* Sort the 'data' structure */
2031 gmx_qsort(data, rotg->nat, sizeof(sort_along_vec_t), projection_compare);
2033 /* Copy back the sorted values */
2034 for (i=0; i<rotg->nat; i++)
2036 copy_rvec(data[i].x , erg->xc[i] );
2037 copy_rvec(data[i].x_ref, erg->xc_ref_sorted[i]);
2038 erg->mc_sorted[i] = data[i].m;
2039 erg->xc_sortind[i] = data[i].ind;
2044 /* For each slab, get the first and the last index of the sorted atom
2046 static void get_firstlast_atom_per_slab(t_rotgrp *rotg, t_commrec *cr)
2050 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2053 erg=rotg->enfrotgrp;
2055 GMX_MPE_LOG(ev_get_firstlast_start);
2057 /* Find the first atom that needs to enter the calculation for each slab */
2058 n = erg->slab_first; /* slab */
2059 i = 0; /* start with the first atom */
2062 /* Find the first atom that significantly contributes to this slab */
2063 do /* move forward in position until a large enough beta is found */
2065 beta = calc_beta(erg->xc[i], rotg, n);
2067 } while ((beta < -erg->max_beta) && (i < rotg->nat));
2069 islab = n - erg->slab_first; /* slab index */
2070 erg->firstatom[islab] = i;
2071 /* Proceed to the next slab */
2073 } while (n <= erg->slab_last);
2075 /* Find the last atom for each slab */
2076 n = erg->slab_last; /* start with last slab */
2077 i = rotg->nat-1; /* start with the last atom */
2080 do /* move backward in position until a large enough beta is found */
2082 beta = calc_beta(erg->xc[i], rotg, n);
2084 } while ((beta > erg->max_beta) && (i > -1));
2086 islab = n - erg->slab_first; /* slab index */
2087 erg->lastatom[islab] = i;
2088 /* Proceed to the next slab */
2090 } while (n >= erg->slab_first);
2092 GMX_MPE_LOG(ev_get_firstlast_finish);
2096 /* Determine the very first and very last slab that needs to be considered
2097 * For the first slab that needs to be considered, we have to find the smallest
2100 * x_first * v - n*Delta_x <= beta_max
2102 * slab index n, slab distance Delta_x, rotation vector v. For the last slab we
2103 * have to find the largest n that obeys
2105 * x_last * v - n*Delta_x >= -beta_max
2108 static inline int get_first_slab(
2109 t_rotgrp *rotg, /* The rotation group (inputrec data) */
2110 real max_beta, /* The max_beta value, instead of min_gaussian */
2111 rvec firstatom) /* First atom after sorting along the rotation vector v */
2113 /* Find the first slab for the first atom */
2114 return ceil((iprod(firstatom, rotg->vec) - max_beta)/rotg->slab_dist);
2118 static inline int get_last_slab(
2119 t_rotgrp *rotg, /* The rotation group (inputrec data) */
2120 real max_beta, /* The max_beta value, instead of min_gaussian */
2121 rvec lastatom) /* Last atom along v */
2123 /* Find the last slab for the last atom */
2124 return floor((iprod(lastatom, rotg->vec) + max_beta)/rotg->slab_dist);
2128 static void get_firstlast_slab_check(
2129 t_rotgrp *rotg, /* The rotation group (inputrec data) */
2130 t_gmx_enfrotgrp *erg, /* The rotation group (data only accessible in this file) */
2131 rvec firstatom, /* First atom after sorting along the rotation vector v */
2132 rvec lastatom, /* Last atom along v */
2133 int g, /* The rotation group number */
2136 erg->slab_first = get_first_slab(rotg, erg->max_beta, firstatom);
2137 erg->slab_last = get_last_slab(rotg, erg->max_beta, lastatom);
2139 /* Check whether we have reference data to compare against */
2140 if (erg->slab_first < erg->slab_first_ref)
2141 gmx_fatal(FARGS, "%s No reference data for first slab (n=%d), unable to proceed.",
2142 RotStr, erg->slab_first);
2144 /* Check whether we have reference data to compare against */
2145 if (erg->slab_last > erg->slab_last_ref)
2146 gmx_fatal(FARGS, "%s No reference data for last slab (n=%d), unable to proceed.",
2147 RotStr, erg->slab_last);
2151 /* Enforced rotation with a flexible axis */
2152 static void do_flexible(
2154 gmx_enfrot_t enfrot, /* Other rotation data */
2155 t_rotgrp *rotg, /* The rotation group */
2156 int g, /* Group number */
2157 rvec x[], /* The local positions */
2159 double t, /* Time in picoseconds */
2160 int step, /* The time step */
2164 real sigma; /* The Gaussian width sigma */
2165 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2168 erg=rotg->enfrotgrp;
2170 /* Define the sigma value */
2171 sigma = 0.7*rotg->slab_dist;
2173 /* Sort the collective coordinates erg->xc along the rotation vector. This is
2174 * an optimization for the inner loop.
2176 sort_collective_coordinates(rotg, enfrot->data);
2178 /* Determine the first relevant slab for the first atom and the last
2179 * relevant slab for the last atom */
2180 get_firstlast_slab_check(rotg, erg, erg->xc[0], erg->xc[rotg->nat-1], g, cr);
2182 /* Determine for each slab depending on the min_gaussian cutoff criterium,
2183 * a first and a last atom index inbetween stuff needs to be calculated */
2184 get_firstlast_atom_per_slab(rotg, cr);
2186 /* Determine the gaussian-weighted center of positions for all slabs */
2187 get_slab_centers(rotg,erg->xc,erg->mc_sorted,cr,g,t,enfrot->out_slabs,bOutstep,FALSE);
2189 /* Clear the torque per slab from last time step: */
2190 nslabs = erg->slab_last - erg->slab_first + 1;
2191 for (l=0; l<nslabs; l++)
2192 erg->slab_torque_v[l] = 0.0;
2194 /* Call the rotational forces kernel */
2195 GMX_MPE_LOG(ev_flexll_start);
2196 if (rotg->eType == erotgFLEX || rotg->eType == erotgFLEXT)
2197 erg->V = do_flex_lowlevel(rotg, sigma, x, bOutstep, box, cr);
2198 else if (rotg->eType == erotgFLEX2 || rotg->eType == erotgFLEX2T)
2199 erg->V = do_flex2_lowlevel(rotg, sigma, x, bOutstep, box, cr);
2201 gmx_fatal(FARGS, "Unknown flexible rotation type");
2202 GMX_MPE_LOG(ev_flexll_finish);
2204 /* Determine actual angle of this slab by RMSD fit and output to file - Let's hope */
2205 /* this only happens once in a while, since this is not parallelized! */
2206 if (bOutstep && MASTER(cr))
2207 flex_fit_angle(g, rotg, t, erg->degangle, enfrot->out_angles);
2211 /* Calculate the angle between reference and actual rotation group atom,
2212 * both projected into a plane perpendicular to the rotation vector: */
2213 static void angle(t_rotgrp *rotg,
2217 real *weight) /* atoms near the rotation axis should count less than atoms far away */
2219 rvec xp, xrp; /* current and reference positions projected on a plane perpendicular to pg->vec */
2223 /* Project x_ref and x into a plane through the origin perpendicular to rot_vec: */
2224 /* Project x_ref: xrp = x_ref - (vec * x_ref) * vec */
2225 svmul(iprod(rotg->vec, x_ref), rotg->vec, dum);
2226 rvec_sub(x_ref, dum, xrp);
2227 /* Project x_act: */
2228 svmul(iprod(rotg->vec, x_act), rotg->vec, dum);
2229 rvec_sub(x_act, dum, xp);
2231 /* Retrieve information about which vector precedes. gmx_angle always
2232 * returns a positive angle. */
2233 cprod(xp, xrp, dum); /* if reference precedes, this is pointing into the same direction as vec */
2235 if (iprod(rotg->vec, dum) >= 0)
2236 *alpha = -gmx_angle(xrp, xp);
2238 *alpha = +gmx_angle(xrp, xp);
2240 /* Also return the weight */
2245 /* Project first vector onto a plane perpendicular to the second vector
2247 * Note that v must be of unit length.
2249 static inline void project_onto_plane(rvec dr, const rvec v)
2254 svmul(iprod(dr,v),v,tmp); /* tmp = (dr.v)v */
2255 rvec_dec(dr, tmp); /* dr = dr - (dr.v)v */
2259 /* Fixed rotation: The rotation reference group rotates around an axis */
2260 /* The atoms of the actual rotation group are attached with imaginary */
2261 /* springs to the reference atoms. */
2262 static void do_fixed(
2264 t_rotgrp *rotg, /* The rotation group */
2265 rvec x[], /* The positions */
2266 matrix box, /* The simulation box */
2267 double t, /* Time in picoseconds */
2268 int step, /* The time step */
2273 rvec tmp_f; /* Force */
2274 real alpha; /* a single angle between an actual and a reference position */
2275 real weight; /* single weight for a single angle */
2276 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2279 /* for mass weighting: */
2280 real wi; /* Mass-weighting of the positions */
2282 real k_wi; /* k times wi */
2287 erg=rotg->enfrotgrp;
2288 bProject = (rotg->eType==erotgPM) || (rotg->eType==erotgPMPF);
2290 /* Clear values from last time step */
2292 erg->fix_torque_v = 0.0;
2293 erg->fix_angles_v = 0.0;
2294 erg->fix_weight_v = 0.0;
2296 N_M = rotg->nat * erg->invmass;
2298 /* Each process calculates the forces on its local atoms */
2299 for (i=0; i<erg->nat_loc; i++)
2301 /* Calculate (x_i-x_c) resp. (x_i-u) */
2302 rvec_sub(erg->x_loc_pbc[i], erg->xc_center, tmpvec);
2304 /* Calculate Omega*(y_i-y_c)-(x_i-x_c) */
2305 rvec_sub(erg->xr_loc[i], tmpvec, dr);
2308 project_onto_plane(dr, rotg->vec);
2310 /* Mass-weighting */
2311 wi = N_M*erg->m_loc[i];
2313 /* Store the additional force so that it can be added to the force
2314 * array after the normal forces have been evaluated */
2316 for (m=0; m<DIM; m++)
2318 tmp_f[m] = k_wi*dr[m];
2319 erg->f_rot_loc[i][m] = tmp_f[m];
2320 erg->V += 0.5*k_wi*sqr(dr[m]);
2325 /* Add to the torque of this rotation group */
2326 erg->fix_torque_v += torque(rotg->vec, tmp_f, erg->x_loc_pbc[i], erg->xc_center);
2328 /* Calculate the angle between reference and actual rotation group atom. */
2329 angle(rotg, tmpvec, erg->xr_loc[i], &alpha, &weight); /* angle in rad, weighted */
2330 erg->fix_angles_v += alpha * weight;
2331 erg->fix_weight_v += weight;
2333 /* If you want enforced rotation to contribute to the virial,
2334 * activate the following lines:
2337 Add the rotation contribution to the virial
2338 for(j=0; j<DIM; j++)
2340 vir[j][m] += 0.5*f[ii][j]*dr[m];
2344 fprintf(stderr,"step %d node%d FORCE on ATOM %d = (%15.8f %15.8f %15.8f) torque=%15.8f\n", step, cr->nodeid,
2345 erg->xc_ref_ind[i],erg->f_rot_loc[i][XX], erg->f_rot_loc[i][YY], erg->f_rot_loc[i][ZZ],erg->fix_torque_v);
2347 } /* end of loop over local rotation group atoms */
2351 /* Calculate the radial motion potential and forces */
2352 static void do_radial_motion(
2354 t_rotgrp *rotg, /* The rotation group */
2355 rvec x[], /* The positions */
2356 matrix box, /* The simulation box */
2357 double t, /* Time in picoseconds */
2358 int step, /* The time step */
2362 rvec tmp_f; /* Force */
2363 real alpha; /* a single angle between an actual and a reference position */
2364 real weight; /* single weight for a single angle */
2365 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2366 rvec xj_u; /* xj - u */
2371 /* For mass weighting: */
2372 real wj; /* Mass-weighting of the positions */
2376 erg=rotg->enfrotgrp;
2378 /* Clear values from last time step */
2381 erg->fix_torque_v = 0.0;
2382 erg->fix_angles_v = 0.0;
2383 erg->fix_weight_v = 0.0;
2385 N_M = rotg->nat * erg->invmass;
2387 /* Each process calculates the forces on its local atoms */
2388 for (j=0; j<erg->nat_loc; j++)
2390 /* Calculate (xj-u) */
2391 rvec_sub(erg->x_loc_pbc[j], erg->xc_center, xj_u); /* xj_u = xj-u */
2393 /* Calculate Omega.(yj-u) */
2394 cprod(rotg->vec, erg->xr_loc[j], tmpvec); /* tmpvec = v x Omega.(yj-u) */
2396 /* v x Omega.(yj-u) */
2397 unitv(tmpvec, pj); /* pj = -------------------- */
2398 /* | v x Omega.(yj-u) | */
2400 fac = iprod(pj, xj_u); /* fac = pj.(xj-u) */
2403 /* Mass-weighting */
2404 wj = N_M*erg->m_loc[j];
2406 /* Store the additional force so that it can be added to the force
2407 * array after the normal forces have been evaluated */
2408 svmul(-rotg->k*wj*fac, pj, tmp_f);
2409 copy_rvec(tmp_f, erg->f_rot_loc[j]);
2413 /* Add to the torque of this rotation group */
2414 erg->fix_torque_v += torque(rotg->vec, tmp_f, erg->x_loc_pbc[j], erg->xc_center);
2416 /* Calculate the angle between reference and actual rotation group atom. */
2417 angle(rotg, xj_u, erg->xr_loc[j], &alpha, &weight); /* angle in rad, weighted */
2418 erg->fix_angles_v += alpha * weight;
2419 erg->fix_weight_v += weight;
2422 fprintf(stderr,"RM: step %d node%d FORCE on ATOM %d = (%15.8f %15.8f %15.8f) torque=%15.8f\n", step, cr->nodeid,
2423 erg->xc_ref_ind[j],erg->f_rot_loc[j][XX], erg->f_rot_loc[j][YY], erg->f_rot_loc[j][ZZ],erg->fix_torque_v);
2425 } /* end of loop over local rotation group atoms */
2426 erg->V = 0.5*rotg->k*sum;
2430 /* Calculate the radial motion pivot-free potential and forces */
2431 static void do_radial_motion_pf(
2433 t_rotgrp *rotg, /* The rotation group */
2434 rvec x[], /* The positions */
2435 matrix box, /* The simulation box */
2436 double t, /* Time in picoseconds */
2437 int step, /* The time step */
2441 rvec xj; /* Current position */
2442 rvec xj_xc; /* xj - xc */
2443 rvec yj0_yc0; /* yj0 - yc0 */
2444 rvec tmp_f; /* Force */
2445 real alpha; /* a single angle between an actual and a reference position */
2446 real weight; /* single weight for a single angle */
2447 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2448 rvec tmpvec, tmpvec2;
2449 rvec innersumvec; /* Precalculation of the inner sum */
2454 /* For mass weighting: */
2455 real mj,wi,wj; /* Mass-weighting of the positions */
2459 erg=rotg->enfrotgrp;
2461 /* Clear values from last time step */
2464 erg->fix_torque_v = 0.0;
2465 erg->fix_angles_v = 0.0;
2466 erg->fix_weight_v = 0.0;
2468 N_M = rotg->nat * erg->invmass;
2470 /* Get the current center of the rotation group: */
2471 get_center(erg->xc, erg->mc, rotg->nat, erg->xc_center);
2473 /* Precalculate Sum_i [ wi qi.(xi-xc) qi ] which is needed for every single j */
2474 clear_rvec(innersumvec);
2475 for (i=0; i < rotg->nat; i++)
2477 /* Mass-weighting */
2478 wi = N_M*erg->mc[i];
2480 /* Calculate qi. Note that xc_ref_center has already been subtracted from
2481 * x_ref in init_rot_group.*/
2482 mvmul(erg->rotmat, rotg->x_ref[i], tmpvec); /* tmpvec = Omega.(yi0-yc0) */
2484 cprod(rotg->vec, tmpvec, tmpvec2); /* tmpvec2 = v x Omega.(yi0-yc0) */
2486 /* v x Omega.(yi0-yc0) */
2487 unitv(tmpvec2, qi); /* qi = ----------------------- */
2488 /* | v x Omega.(yi0-yc0) | */
2490 rvec_sub(erg->xc[i], erg->xc_center, tmpvec); /* tmpvec = xi-xc */
2492 svmul(wi*iprod(qi, tmpvec), qi, tmpvec2);
2494 rvec_inc(innersumvec, tmpvec2);
2496 svmul(rotg->k*erg->invmass, innersumvec, innersumveckM);
2498 /* Each process calculates the forces on its local atoms */
2499 for (j=0; j<erg->nat_loc; j++)
2501 /* Local index of a rotation group atom */
2502 ii = erg->ind_loc[j];
2503 /* Position of this atom in the collective array */
2504 iigrp = erg->xc_ref_ind[j];
2505 /* Mass-weighting */
2506 mj = erg->mc[iigrp]; /* need the unsorted mass here */
2509 /* Current position of this atom: x[ii][XX/YY/ZZ] */
2510 copy_rvec(x[ii], xj);
2512 /* Shift this atom such that it is near its reference */
2513 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
2515 /* The (unrotated) reference position is yj0. yc0 has already
2516 * been subtracted in init_rot_group */
2517 copy_rvec(rotg->x_ref[iigrp], yj0_yc0); /* yj0_yc0 = yj0 - yc0 */
2519 /* Calculate Omega.(yj0-yc0) */
2520 mvmul(erg->rotmat, yj0_yc0, tmpvec2); /* tmpvec2 = Omega.(yj0 - yc0) */
2522 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x Omega.(yj0-yc0) */
2524 /* v x Omega.(yj0-yc0) */
2525 unitv(tmpvec, qj); /* qj = ----------------------- */
2526 /* | v x Omega.(yj0-yc0) | */
2528 /* Calculate (xj-xc) */
2529 rvec_sub(xj, erg->xc_center, xj_xc); /* xj_xc = xj-xc */
2531 fac = iprod(qj, xj_xc); /* fac = qj.(xj-xc) */
2534 /* Store the additional force so that it can be added to the force
2535 * array after the normal forces have been evaluated */
2536 svmul(-rotg->k*wj*fac, qj, tmp_f); /* part 1 of force */
2537 svmul(mj, innersumveckM, tmpvec); /* part 2 of force */
2538 rvec_inc(tmp_f, tmpvec);
2539 copy_rvec(tmp_f, erg->f_rot_loc[j]);
2543 /* Add to the torque of this rotation group */
2544 erg->fix_torque_v += torque(rotg->vec, tmp_f, xj, erg->xc_center);
2546 /* Calculate the angle between reference and actual rotation group atom. */
2547 angle(rotg, xj_xc, yj0_yc0, &alpha, &weight); /* angle in rad, weighted */
2548 erg->fix_angles_v += alpha * weight;
2549 erg->fix_weight_v += weight;
2552 fprintf(stderr,"RM-PF: step %d node%d FORCE on ATOM %d = (%15.8f %15.8f %15.8f) torque=%15.8f\n", step, cr->nodeid,
2553 erg->xc_ref_ind[j],erg->f_rot_loc[j][XX], erg->f_rot_loc[j][YY], erg->f_rot_loc[j][ZZ],erg->fix_torque_v);
2555 } /* end of loop over local rotation group atoms */
2556 erg->V = 0.5*rotg->k*V;
2560 /* Precalculate the inner sum for the radial motion 2 forces */
2561 static void radial_motion2_precalc_inner_sum(t_rotgrp *rotg, rvec innersumvec)
2564 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2565 rvec xi_xc; /* xj - xc */
2566 rvec tmpvec,tmpvec2;
2570 rvec v_xi_xc; /* v x (xj - u) */
2572 real wi; /* Mass-weighting of the positions */
2576 erg=rotg->enfrotgrp;
2577 N_M = rotg->nat * erg->invmass;
2579 /* Loop over the collective set of positions */
2581 for (i=0; i<rotg->nat; i++)
2583 /* Mass-weighting */
2584 wi = N_M*erg->mc[i];
2586 rvec_sub(erg->xc[i], erg->xc_center, xi_xc); /* xi_xc = xi-xc */
2588 /* Calculate ri. Note that xc_ref_center has already been subtracted from
2589 * x_ref in init_rot_group.*/
2590 mvmul(erg->rotmat, rotg->x_ref[i], ri); /* ri = Omega.(yi0-yc0) */
2592 cprod(rotg->vec, xi_xc, v_xi_xc); /* v_xi_xc = v x (xi-u) */
2594 fac = norm2(v_xi_xc);
2596 psiistar = 1.0/(fac + rotg->eps); /* psiistar = --------------------- */
2597 /* |v x (xi-xc)|^2 + eps */
2599 psii = gmx_invsqrt(fac); /* 1 */
2600 /* psii = ------------- */
2603 svmul(psii, v_xi_xc, si); /* si = psii * (v x (xi-xc) ) */
2605 fac = iprod(v_xi_xc, ri); /* fac = (v x (xi-xc)).ri */
2608 siri = iprod(si, ri); /* siri = si.ri */
2610 svmul(psiistar/psii, ri, tmpvec);
2611 svmul(psiistar*psiistar/(psii*psii*psii) * siri, si, tmpvec2);
2612 rvec_dec(tmpvec, tmpvec2);
2613 cprod(tmpvec, rotg->vec, tmpvec2);
2615 svmul(wi*siri, tmpvec2, tmpvec);
2617 rvec_inc(sumvec, tmpvec);
2619 svmul(rotg->k*erg->invmass, sumvec, innersumvec);
2623 /* Calculate the radial motion 2 potential and forces */
2624 static void do_radial_motion2(
2626 t_rotgrp *rotg, /* The rotation group */
2627 rvec x[], /* The positions */
2628 matrix box, /* The simulation box */
2629 double t, /* Time in picoseconds */
2630 int step, /* The time step */
2634 rvec xj; /* Position */
2635 real alpha; /* a single angle between an actual and a reference position */
2636 real weight; /* single weight for a single angle */
2637 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2638 rvec xj_u; /* xj - u */
2639 rvec tmpvec,tmpvec2;
2640 real fac,fac2,Vpart;
2643 rvec v_xj_u; /* v x (xj - u) */
2645 real mj,wj; /* For mass-weighting of the positions */
2651 erg=rotg->enfrotgrp;
2653 bPF = rotg->eType==erotgRM2PF;
2654 clear_rvec(innersumvec);
2657 /* For the pivot-free variant we have to use the current center of
2658 * mass of the rotation group instead of the pivot u */
2659 get_center(erg->xc, erg->mc, rotg->nat, erg->xc_center);
2661 /* Also, we precalculate the second term of the forces that is identical
2662 * (up to the weight factor mj) for all forces */
2663 radial_motion2_precalc_inner_sum(rotg,innersumvec);
2666 /* Clear values from last time step */
2669 erg->fix_torque_v = 0.0;
2670 erg->fix_angles_v = 0.0;
2671 erg->fix_weight_v = 0.0;
2673 N_M = rotg->nat * erg->invmass;
2675 /* Each process calculates the forces on its local atoms */
2676 for (j=0; j<erg->nat_loc; j++)
2680 /* Local index of a rotation group atom */
2681 ii = erg->ind_loc[j];
2682 /* Position of this atom in the collective array */
2683 iigrp = erg->xc_ref_ind[j];
2684 /* Mass-weighting */
2685 mj = erg->mc[iigrp];
2687 /* Current position of this atom: x[ii] */
2688 copy_rvec(x[ii], xj);
2690 /* Shift this atom such that it is near its reference */
2691 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
2693 /* The (unrotated) reference position is yj0. yc0 has already
2694 * been subtracted in init_rot_group */
2695 copy_rvec(rotg->x_ref[iigrp], tmpvec); /* tmpvec = yj0 - yc0 */
2697 /* Calculate Omega.(yj0-yc0) */
2698 mvmul(erg->rotmat, tmpvec, rj); /* rj = Omega.(yj0-yc0) */
2703 copy_rvec(erg->x_loc_pbc[j], xj);
2704 copy_rvec(erg->xr_loc[j], rj); /* rj = Omega.(yj0-u) */
2706 /* Mass-weighting */
2709 /* Calculate (xj-u) resp. (xj-xc) */
2710 rvec_sub(xj, erg->xc_center, xj_u); /* xj_u = xj-u */
2712 cprod(rotg->vec, xj_u, v_xj_u); /* v_xj_u = v x (xj-u) */
2714 fac = norm2(v_xj_u);
2716 psijstar = 1.0/(fac + rotg->eps); /* psistar = -------------------- */
2717 /* |v x (xj-u)|^2 + eps */
2719 psij = gmx_invsqrt(fac); /* 1 */
2720 /* psij = ------------ */
2723 svmul(psij, v_xj_u, sj); /* sj = psij * (v x (xj-u) ) */
2725 fac = iprod(v_xj_u, rj); /* fac = (v x (xj-u)).rj */
2728 sjrj = iprod(sj, rj); /* sjrj = sj.rj */
2730 svmul(psijstar/psij, rj, tmpvec);
2731 svmul(psijstar*psijstar/(psij*psij*psij) * sjrj, sj, tmpvec2);
2732 rvec_dec(tmpvec, tmpvec2);
2733 cprod(tmpvec, rotg->vec, tmpvec2);
2735 /* Store the additional force so that it can be added to the force
2736 * array after the normal forces have been evaluated */
2737 svmul(-rotg->k*wj*sjrj, tmpvec2, tmpvec);
2738 svmul(mj, innersumvec, tmpvec2); /* This is != 0 only for the pivot-free variant */
2740 rvec_add(tmpvec2, tmpvec, erg->f_rot_loc[j]);
2741 Vpart += wj*psijstar*fac2;
2744 /* Add to the torque of this rotation group */
2745 erg->fix_torque_v += torque(rotg->vec, erg->f_rot_loc[j], xj, erg->xc_center);
2747 /* Calculate the angle between reference and actual rotation group atom. */
2748 angle(rotg, xj_u, rj, &alpha, &weight); /* angle in rad, weighted */
2749 erg->fix_angles_v += alpha * weight;
2750 erg->fix_weight_v += weight;
2753 fprintf(stderr,"RM2: step %d node%d FORCE on ATOM %d = (%15.8f %15.8f %15.8f) torque=%15.8f\n", step, cr->nodeid,
2754 erg->xc_ref_ind[j],erg->f_rot_loc[j][XX], erg->f_rot_loc[j][YY], erg->f_rot_loc[j][ZZ],erg->fix_torque_v);
2756 } /* end of loop over local rotation group atoms */
2757 erg->V = 0.5*rotg->k*Vpart;
2761 /* Determine the smallest and largest position vector (with respect to the
2762 * rotation vector) for the reference group */
2763 static void get_firstlast_atom_ref(
2768 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2770 real xcproj; /* The projection of a reference position on the
2772 real minproj, maxproj; /* Smallest and largest projection on v */
2776 erg=rotg->enfrotgrp;
2778 /* Start with some value */
2779 minproj = iprod(rotg->x_ref[0], rotg->vec);
2782 /* This is just to ensure that it still works if all the atoms of the
2783 * reference structure are situated in a plane perpendicular to the rotation
2786 *lastindex = rotg->nat-1;
2788 /* Loop over all atoms of the reference group,
2789 * project them on the rotation vector to find the extremes */
2790 for (i=0; i<rotg->nat; i++)
2792 xcproj = iprod(rotg->x_ref[i], rotg->vec);
2793 if (xcproj < minproj)
2798 if (xcproj > maxproj)
2807 /* Allocate memory for the slabs */
2808 static void allocate_slabs(
2815 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2819 erg=rotg->enfrotgrp;
2821 /* More slabs than are defined for the reference are never needed */
2822 nslabs = erg->slab_last_ref - erg->slab_first_ref + 1;
2824 /* Remember how many we allocated */
2825 erg->nslabs_alloc = nslabs;
2827 if (MASTER(cr) && bVerbose)
2828 fprintf(fplog, "%s allocating memory to store data for %d slabs (rotation group %d).\n",
2830 snew(erg->slab_center , nslabs);
2831 snew(erg->slab_center_ref , nslabs);
2832 snew(erg->slab_weights , nslabs);
2833 snew(erg->slab_torque_v , nslabs);
2834 snew(erg->slab_data , nslabs);
2835 snew(erg->gn_atom , nslabs);
2836 snew(erg->gn_slabind , nslabs);
2837 snew(erg->slab_innersumvec, nslabs);
2838 for (i=0; i<nslabs; i++)
2840 snew(erg->slab_data[i].x , rotg->nat);
2841 snew(erg->slab_data[i].ref , rotg->nat);
2842 snew(erg->slab_data[i].weight, rotg->nat);
2844 snew(erg->xc_ref_sorted, rotg->nat);
2845 snew(erg->xc_sortind , rotg->nat);
2846 snew(erg->firstatom , nslabs);
2847 snew(erg->lastatom , nslabs);
2851 /* From the extreme coordinates of the reference group, determine the first
2852 * and last slab of the reference. We can never have more slabs in the real
2853 * simulation than calculated here for the reference.
2855 static void get_firstlast_slab_ref(t_rotgrp *rotg, real mc[], int ref_firstindex, int ref_lastindex)
2857 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2858 int first,last,firststart;
2862 erg=rotg->enfrotgrp;
2863 first = get_first_slab(rotg, erg->max_beta, rotg->x_ref[ref_firstindex]);
2864 last = get_last_slab( rotg, erg->max_beta, rotg->x_ref[ref_lastindex ]);
2867 while (get_slab_weight(first, rotg, rotg->x_ref, mc, &dummy) > WEIGHT_MIN)
2871 erg->slab_first_ref = first+1;
2872 while (get_slab_weight(last, rotg, rotg->x_ref, mc, &dummy) > WEIGHT_MIN)
2876 erg->slab_last_ref = last-1;
2878 erg->slab_buffer = firststart - erg->slab_first_ref;
2883 static void init_rot_group(FILE *fplog,t_commrec *cr,int g,t_rotgrp *rotg,
2884 rvec *x,gmx_mtop_t *mtop,gmx_bool bVerbose,FILE *out_slabs)
2888 gmx_bool bFlex,bColl;
2890 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2891 int ref_firstindex, ref_lastindex;
2892 real mass,totalmass;
2895 /* Do we have a flexible axis? */
2896 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
2897 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
2899 /* Do we use a global set of coordinates? */
2900 bColl = bFlex || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF);
2902 erg=rotg->enfrotgrp;
2904 /* Allocate space for collective coordinates if needed */
2907 snew(erg->xc , rotg->nat);
2908 snew(erg->xc_shifts , rotg->nat);
2909 snew(erg->xc_eshifts, rotg->nat);
2911 /* Save the original (whole) set of positions such that later the
2912 * molecule can always be made whole again */
2913 snew(erg->xc_old , rotg->nat);
2916 for (i=0; i<rotg->nat; i++)
2919 copy_rvec(x[ii], erg->xc_old[i]);
2924 gmx_bcast(rotg->nat*sizeof(erg->xc_old[0]),erg->xc_old, cr);
2927 if (rotg->eFittype == erotgFitNORM)
2929 snew(erg->xc_ref_length, rotg->nat); /* in case fit type NORM is chosen */
2930 snew(erg->xc_norm , rotg->nat);
2935 snew(erg->xr_loc , rotg->nat);
2936 snew(erg->x_loc_pbc, rotg->nat);
2939 snew(erg->f_rot_loc , rotg->nat);
2940 snew(erg->xc_ref_ind, rotg->nat);
2942 /* xc_ref_ind needs to be set to identity in the serial case */
2944 for (i=0; i<rotg->nat; i++)
2945 erg->xc_ref_ind[i] = i;
2947 /* Copy the masses so that the COM can be determined. For all types of
2948 * enforced rotation, we store the masses in the erg->mc array. */
2949 snew(erg->mc, rotg->nat);
2951 snew(erg->mc_sorted, rotg->nat);
2953 snew(erg->m_loc, rotg->nat);
2955 for (i=0; i<rotg->nat; i++)
2959 gmx_mtop_atomnr_to_atom(mtop,rotg->ind[i],&atom);
2969 erg->invmass = 1.0/totalmass;
2971 /* Set xc_ref_center for any rotation potential */
2972 if ((rotg->eType==erotgISO) || (rotg->eType==erotgPM) || (rotg->eType==erotgRM) || (rotg->eType==erotgRM2))
2974 /* Set the pivot point for the fixed, stationary-axis potentials. This
2975 * won't change during the simulation */
2976 copy_rvec(rotg->pivot, erg->xc_ref_center);
2977 copy_rvec(rotg->pivot, erg->xc_center );
2981 /* Center of the reference positions */
2982 get_center(rotg->x_ref, erg->mc, rotg->nat, erg->xc_ref_center);
2984 /* Center of the actual positions */
2987 snew(xdum, rotg->nat);
2988 for (i=0; i<rotg->nat; i++)
2991 copy_rvec(x[ii], xdum[i]);
2993 get_center(xdum, erg->mc, rotg->nat, erg->xc_center);
2998 gmx_bcast(sizeof(erg->xc_center), erg->xc_center, cr);
3002 if ( (rotg->eType != erotgFLEX) && (rotg->eType != erotgFLEX2) )
3004 /* Put the reference positions into origin: */
3005 for (i=0; i<rotg->nat; i++)
3006 rvec_dec(rotg->x_ref[i], erg->xc_ref_center);
3009 /* Enforced rotation with flexible axis */
3012 /* Calculate maximum beta value from minimum gaussian (performance opt.) */
3013 erg->max_beta = calc_beta_max(rotg->min_gaussian, rotg->slab_dist);
3015 /* Determine the smallest and largest coordinate with respect to the rotation vector */
3016 get_firstlast_atom_ref(rotg, &ref_firstindex, &ref_lastindex);
3018 /* From the extreme coordinates of the reference group, determine the first
3019 * and last slab of the reference. */
3020 get_firstlast_slab_ref(rotg, erg->mc, ref_firstindex, ref_lastindex);
3022 /* Allocate memory for the slabs */
3023 allocate_slabs(rotg, fplog, g, bVerbose, cr);
3025 /* Flexible rotation: determine the reference centers for the rest of the simulation */
3026 erg->slab_first = erg->slab_first_ref;
3027 erg->slab_last = erg->slab_last_ref;
3028 get_slab_centers(rotg,rotg->x_ref,erg->mc,cr,g,-1,out_slabs,TRUE,TRUE);
3030 /* Length of each x_rotref vector from center (needed if fit routine NORM is chosen): */
3031 if (rotg->eFittype == erotgFitNORM)
3033 for (i=0; i<rotg->nat; i++)
3035 rvec_sub(rotg->x_ref[i], erg->xc_ref_center, coord);
3036 erg->xc_ref_length[i] = norm(coord);
3043 extern void dd_make_local_rotation_groups(gmx_domdec_t *dd,t_rot *rot)
3048 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3052 for(g=0; g<rot->ngrp; g++)
3054 rotg = &rot->grp[g];
3055 erg = rotg->enfrotgrp;
3058 dd_make_local_group_indices(ga2la,rotg->nat,rotg->ind,
3059 &erg->nat_loc,&erg->ind_loc,&erg->nalloc_loc,erg->xc_ref_ind);
3064 extern void init_rot(FILE *fplog,t_inputrec *ir,int nfile,const t_filenm fnm[],
3065 t_commrec *cr, rvec *x, matrix box, gmx_mtop_t *mtop, const output_env_t oenv,
3066 gmx_bool bVerbose, unsigned long Flags)
3071 int nat_max=0; /* Size of biggest rotation group */
3072 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
3073 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3074 rvec *x_pbc; /* Space for the pbc-correct atom positions */
3077 if ( (PAR(cr)) && !DOMAINDECOMP(cr) )
3078 gmx_fatal(FARGS, "Enforced rotation is only implemented for domain decomposition!");
3080 if ( MASTER(cr) && bVerbose)
3081 fprintf(stdout, "%s Initializing ...\n", RotStr);
3085 snew(rot->enfrot, 1);
3088 /* Output every step for reruns */
3089 if (Flags & MD_RERUN)
3092 fprintf(fplog, "%s rerun - will write rotation output every available step.\n", RotStr);
3097 er->out_slabs = NULL;
3099 er->out_slabs = open_slab_out(rot, opt2fn("-rs",nfile,fnm));
3103 /* Remove pbc, make molecule whole.
3104 * When ir->bContinuation=TRUE this has already been done, but ok. */
3105 snew(x_pbc,mtop->natoms);
3106 m_rveccopy(mtop->natoms,x,x_pbc);
3107 do_pbc_first_mtop(NULL,ir->ePBC,box,mtop,x_pbc);
3110 for(g=0; g<rot->ngrp; g++)
3112 rotg = &rot->grp[g];
3115 fprintf(fplog,"%s group %d type '%s'\n", RotStr, g, erotg_names[rotg->eType]);
3119 /* Allocate space for the rotation group's data: */
3120 snew(rotg->enfrotgrp, 1);
3121 erg = rotg->enfrotgrp;
3123 nat_max=max(nat_max, rotg->nat);
3128 erg->nalloc_loc = 0;
3129 erg->ind_loc = NULL;
3133 erg->nat_loc = rotg->nat;
3134 erg->ind_loc = rotg->ind;
3136 init_rot_group(fplog,cr,g,rotg,x_pbc,mtop,bVerbose,er->out_slabs);
3140 /* Allocate space for enforced rotation buffer variables */
3141 er->bufsize = nat_max;
3142 snew(er->data, nat_max);
3143 snew(er->xbuf, nat_max);
3144 snew(er->mbuf, nat_max);
3146 /* Buffers for MPI reducing torques, angles, weights (for each group), and V */
3147 er->mpi_bufsize = 4*rot->ngrp; /* To start with */
3148 snew(er->mpi_inbuf , er->mpi_bufsize);
3149 snew(er->mpi_outbuf, er->mpi_bufsize);
3151 /* Only do I/O on the MASTER */
3152 er->out_angles = NULL;
3154 er->out_torque = NULL;
3157 er->out_rot = open_rot_out(opt2fn("-ro",nfile,fnm), rot, oenv, Flags);
3158 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
3159 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
3161 if (rot->nstrout > 0)
3162 er->out_angles = open_angles_out(rot, opt2fn("-ra",nfile,fnm));
3163 if (rot->nsttout > 0)
3164 er->out_torque = open_torque_out(rot, opt2fn("-rt",nfile,fnm));
3171 extern void finish_rot(FILE *fplog,t_rot *rot)
3173 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
3178 gmx_fio_fclose(er->out_rot);
3180 gmx_fio_fclose(er->out_slabs);
3182 gmx_fio_fclose(er->out_angles);
3184 gmx_fio_fclose(er->out_torque);
3188 /* Rotate the local reference positions and store them in
3189 * erg->xr_loc[0...(nat_loc-1)]
3191 * Note that we already subtracted u or y_c from the reference positions
3192 * in init_rot_group().
3194 static void rotate_local_reference(t_rotgrp *rotg)
3196 gmx_enfrotgrp_t erg;
3200 erg=rotg->enfrotgrp;
3202 for (i=0; i<erg->nat_loc; i++)
3204 /* Index of this rotation group atom with respect to the whole rotation group */
3205 ii = erg->xc_ref_ind[i];
3207 mvmul(erg->rotmat, rotg->x_ref[ii], erg->xr_loc[i]);
3212 /* Select the PBC representation for each local x position and store that
3213 * for later usage. We assume the right PBC image of an x is the one nearest to
3214 * its rotated reference */
3215 static void choose_pbc_image(rvec x[], t_rotgrp *rotg, matrix box, int npbcdim)
3218 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3223 erg=rotg->enfrotgrp;
3225 for (i=0; i<erg->nat_loc; i++)
3229 /* Index of a rotation group atom */
3230 ii = erg->ind_loc[i];
3232 /* Get the reference position. The pivot (or COM or COG) was already
3233 * subtracted in init_rot_group() from the reference positions. Also,
3234 * the reference positions have already been rotated in
3235 * rotate_local_reference() */
3236 copy_rvec(erg->xr_loc[i], xref);
3238 /* Subtract the (old) center from the current positions
3239 * (just to determine the shifts!) */
3240 rvec_sub(x[ii], erg->xc_center, xcurr);
3242 /* Shortest PBC distance between the atom and its reference */
3243 rvec_sub(xcurr, xref, dx);
3245 /* Determine the shift for this atom */
3246 for(m=npbcdim-1; m>=0; m--)
3248 while (dx[m] < -0.5*box[m][m])
3250 for(d=0; d<DIM; d++)
3254 while (dx[m] >= 0.5*box[m][m])
3256 for(d=0; d<DIM; d++)
3262 /* Apply the shift to the current atom */
3263 copy_rvec(x[ii], erg->x_loc_pbc[i]);
3264 shift_single_coord(box, erg->x_loc_pbc[i], shift);
3269 extern void do_rotation(
3276 gmx_wallcycle_t wcycle,
3282 gmx_bool outstep_torque;
3283 gmx_bool bFlex,bColl;
3285 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
3286 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3296 /* At which time steps do we want to output the torque */
3297 outstep_torque = do_per_step(step, rot->nsttout);
3299 /* Output time into rotation output file */
3300 if (outstep_torque && MASTER(cr))
3301 fprintf(er->out_rot, "%12.3e",t);
3303 /**************************************************************************/
3304 /* First do ALL the communication! */
3305 for(g=0; g<rot->ngrp; g++)
3307 rotg = &rot->grp[g];
3308 erg=rotg->enfrotgrp;
3310 /* Do we have a flexible axis? */
3311 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
3312 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
3314 /* Do we use a collective (global) set of coordinates? */
3315 bColl = bFlex || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF);
3317 /* Calculate the rotation matrix for this angle: */
3318 erg->degangle = rotg->rate * t;
3319 calc_rotmat(rotg->vec,erg->degangle,erg->rotmat);
3323 /* Transfer the rotation group's positions such that every node has
3324 * all of them. Every node contributes its local positions x and stores
3325 * it in the collective erg->xc array. */
3326 communicate_group_positions(cr,erg->xc, erg->xc_shifts, erg->xc_eshifts, bNS,
3327 x, rotg->nat, erg->nat_loc, erg->ind_loc, erg->xc_ref_ind, erg->xc_old, box);
3331 /* Fill the local masses array;
3332 * this array changes in DD/neighborsearching steps */
3335 for (i=0; i<erg->nat_loc; i++)
3337 /* Index of local atom w.r.t. the collective rotation group */
3338 ii = erg->xc_ref_ind[i];
3339 erg->m_loc[i] = erg->mc[ii];
3343 /* Calculate Omega*(y_i-y_c) for the local positions */
3344 rotate_local_reference(rotg);
3346 /* Choose the nearest PBC images of the group atoms with respect
3347 * to the rotated reference positions */
3348 choose_pbc_image(x, rotg, box, 3);
3350 /* Get the center of the rotation group */
3351 if ( (rotg->eType==erotgISOPF) || (rotg->eType==erotgPMPF) )
3352 get_center_comm(cr, erg->x_loc_pbc, erg->m_loc, erg->nat_loc, rotg->nat, erg->xc_center);
3355 } /* End of loop over rotation groups */
3357 /**************************************************************************/
3358 /* Done communicating, we can start to count cycles now ... */
3359 wallcycle_start(wcycle, ewcROT);
3360 GMX_MPE_LOG(ev_rotcycles_start);
3366 for(g=0; g<rot->ngrp; g++)
3368 rotg = &rot->grp[g];
3369 erg=rotg->enfrotgrp;
3371 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
3372 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
3374 bColl = bFlex || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF);
3376 if (outstep_torque && MASTER(cr))
3377 fprintf(er->out_rot, "%12.4f", erg->degangle);
3385 do_fixed(cr,rotg,x,box,t,step,outstep_torque);
3388 do_radial_motion(cr,rotg,x,box,t,step,outstep_torque);
3391 do_radial_motion_pf(cr,rotg,x,box,t,step,outstep_torque);
3395 do_radial_motion2(cr,rotg,x,box,t,step,outstep_torque);
3399 /* Subtract the center of the rotation group from the collective positions array
3400 * Also store the center in erg->xc_center since it needs to be subtracted
3401 * in the low level routines from the local coordinates as well */
3402 get_center(erg->xc, erg->mc, rotg->nat, erg->xc_center);
3403 svmul(-1.0, erg->xc_center, transvec);
3404 translate_x(erg->xc, rotg->nat, transvec);
3405 do_flexible(cr,er,rotg,g,x,box,t,step,outstep_torque);
3409 /* Do NOT subtract the center of mass in the low level routines! */
3410 clear_rvec(erg->xc_center);
3411 do_flexible(cr,er,rotg,g,x,box,t,step,outstep_torque);
3414 gmx_fatal(FARGS, "No such rotation potential.");
3421 fprintf(stderr, "%s calculation (step %d) took %g seconds.\n", RotStr, step, MPI_Wtime()-t0);
3424 /* Stop the cycle counter and add to the force cycles for load balancing */
3425 cycles_rot = wallcycle_stop(wcycle,ewcROT);
3426 if (DOMAINDECOMP(cr) && wcycle)
3427 dd_cycles_add(cr->dd,cycles_rot,ddCyclF);
3428 GMX_MPE_LOG(ev_rotcycles_finish);