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"
63 static char *RotStr = {"Enforced rotation:"};
66 /* Set the minimum weight for the determination of the slab centers */
67 #define WEIGHT_MIN (10*GMX_FLOAT_MIN)
69 /* Helper structure for sorting positions along rotation vector */
71 real xcproj; /* Projection of xc on the rotation vector */
72 int ind; /* Index of xc */
74 rvec x; /* Position */
75 rvec x_ref; /* Reference position */
79 /* Enforced rotation / flexible: determine the angle of each slab */
80 typedef struct gmx_slabdata
82 int nat; /* Number of atoms belonging to this slab */
83 rvec *x; /* The positions belonging to this slab. In
84 general, this should be all positions of the
85 whole rotation group, but we leave those away
86 that have a small enough weight */
87 rvec *ref; /* Same for reference */
88 real *weight; /* The weight for each atom */
92 /* Enforced rotation data for all groups */
93 typedef struct gmx_enfrot
95 FILE *out_rot; /* Output file for rotation data */
96 FILE *out_torque; /* Output file for torque data */
97 FILE *out_angles; /* Output file for slab angles for flexible type */
98 FILE *out_slabs; /* Output file for slab centers */
99 int bufsize; /* Allocation size of buf */
100 rvec *xbuf; /* Coordinate buffer variable for sorting */
101 real *mbuf; /* Masses buffer variable for sorting */
102 sort_along_vec_t *data; /* Buffer variable needed for position sorting */
103 real *mpi_inbuf; /* MPI buffer */
104 real *mpi_outbuf; /* MPI buffer */
105 int mpi_bufsize; /* Allocation size of in & outbuf */
106 real Vrot; /* (Local) part of the enf. rotation potential */
110 /* Global enforced rotation data for a single rotation group */
111 typedef struct gmx_enfrotgrp
113 real degangle; /* Rotation angle in degree */
114 matrix rotmat; /* Rotation matrix */
115 atom_id *ind_loc; /* Local rotation indices */
116 int nat_loc; /* Number of local group atoms */
117 int nalloc_loc; /* Allocation size for ind_loc and weight_loc */
119 real V; /* Rotation potential for this rotation group */
120 rvec *f_rot_loc; /* Array to store the forces on the local atoms
121 resulting from enforced rotation potential */
123 /* Collective coordinates for the whole rotation group */
124 real *xc_ref_length; /* Length of each x_rotref vector after x_rotref
125 has been put into origin */
126 int *xc_ref_ind; /* Position of each local atom in the collective
128 rvec xc_center; /* Center of the rotation group positions, may
130 rvec xc_ref_center; /* dito, for the reference positions */
131 rvec *xc; /* Current (collective) positions */
132 ivec *xc_shifts; /* Current (collective) shifts */
133 ivec *xc_eshifts; /* Extra shifts since last DD step */
134 rvec *xc_old; /* Old (collective) positions */
135 rvec *xc_norm; /* Normalized form of the current positions */
136 rvec *xc_ref_sorted; /* Reference positions (sorted in the same order
137 as xc when sorted) */
138 int *xc_sortind; /* Where is a position found after sorting? */
139 real *mc; /* Collective masses */
141 real invmass; /* one over the total mass of the rotation group */
142 /* Fixed rotation only */
143 rvec *xr_loc; /* Local reference coords, correctly rotated */
144 rvec *x_loc_pbc; /* Local current coords, correct PBC image */
145 real *m_loc; /* Masses of the current local atoms */
146 real fix_torque_v; /* Torque in the direction of rotation vector */
149 /* Flexible rotation only */
150 int nslabs_alloc; /* For this many slabs memory is allocated */
151 int slab_first; /* Lowermost slab for that the calculation needs
152 to be performed at a given time step */
153 int slab_last; /* Uppermost slab ... */
154 int slab_first_ref; /* First slab for which reference COG is stored */
155 int slab_last_ref; /* Last ... */
156 int slab_buffer; /* Slab buffer region around reference slabs */
157 int *firstatom; /* First relevant atom for a slab */
158 int *lastatom; /* Last relevant atom for a slab */
159 rvec *slab_center; /* Gaussian-weighted slab center (COG) */
160 rvec *slab_center_ref; /* Gaussian-weighted slab COG for the
161 reference positions */
162 real *slab_weights; /* Sum of gaussian weights in a slab */
163 real *slab_torque_v; /* Torque T = r x f for each slab. */
164 /* torque_v = m.v = angular momentum in the
166 real max_beta; /* min_gaussian from inputrec->rotgrp is the
167 minimum value the gaussian must have so that
168 the force is actually evaluated max_beta is
169 just another way to put it */
170 real *gn_atom; /* Precalculated gaussians for a single atom */
171 int *gn_slabind; /* Tells to which slab each precalculated gaussian
173 rvec *slab_innersumvec;/* Inner sum of the flexible2 potential per slab;
174 this is precalculated for optimization reasons */
175 t_gmx_slabdata *slab_data; /* Holds atom positions and gaussian weights
176 of atoms belonging to a slab */
180 static double** allocate_square_matrix(int dim)
194 static void free_square_matrix(double** mat, int dim)
199 for (i=0; i<dim; i++)
205 /* Output rotation energy and torque for each rotation group */
206 static void reduce_output(t_commrec *cr, t_rot *rot, real t)
208 int g,i,islab,nslabs=0;
209 int count; /* MPI element counter */
211 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
212 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
218 /* Fill the MPI buffer with stuff to reduce: */
222 for (g=0; g < rot->ngrp; g++)
226 nslabs = erg->slab_last - erg->slab_first + 1;
227 er->mpi_inbuf[count++] = erg->V;
238 er->mpi_inbuf[count++] = erg->fix_torque_v;
239 er->mpi_inbuf[count++] = erg->fix_angles_v;
240 er->mpi_inbuf[count++] = erg->fix_weight_v;
246 /* (Re-)allocate memory for MPI buffer: */
247 if (er->mpi_bufsize < count+nslabs)
249 er->mpi_bufsize = count+nslabs;
250 srenew(er->mpi_inbuf , er->mpi_bufsize);
251 srenew(er->mpi_outbuf, er->mpi_bufsize);
253 for (i=0; i<nslabs; i++)
254 er->mpi_inbuf[count++] = erg->slab_torque_v[i];
261 MPI_Reduce(er->mpi_inbuf, er->mpi_outbuf, count, GMX_MPI_REAL, MPI_SUM, MASTERRANK(cr), cr->mpi_comm_mygroup);
263 /* Copy back the reduced data from the buffer on the master */
267 for (g=0; g < rot->ngrp; g++)
271 nslabs = erg->slab_last - erg->slab_first + 1;
272 erg->V = er->mpi_outbuf[count++];
283 erg->fix_torque_v = er->mpi_outbuf[count++];
284 erg->fix_angles_v = er->mpi_outbuf[count++];
285 erg->fix_weight_v = er->mpi_outbuf[count++];
291 for (i=0; i<nslabs; i++)
292 erg->slab_torque_v[i] = er->mpi_outbuf[count++];
304 /* Av. angle and total torque for each rotation group */
305 for (g=0; g < rot->ngrp; g++)
308 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
309 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
313 /* Output to main rotation log file: */
316 fprintf(er->out_rot, "%12.4f%12.3e",
317 (erg->fix_angles_v/erg->fix_weight_v)*180.0*M_1_PI,
320 fprintf(er->out_rot, "%12.3e", erg->V);
322 /* Output to torque log file: */
325 fprintf(er->out_torque, "%12.3e%6d", t, g);
326 for (i=erg->slab_first; i<=erg->slab_last; i++)
328 islab = i - erg->slab_first; /* slab index */
329 /* Only output if enough weight is in slab */
330 if (erg->slab_weights[islab] > rotg->min_gaussian)
331 fprintf(er->out_torque, "%6d%12.3e", i, erg->slab_torque_v[islab]);
333 fprintf(er->out_torque , "\n");
336 fprintf(er->out_rot, "\n");
341 /* Add the forces from enforced rotation potential to the local forces.
342 * Should be called after the SR forces have been evaluated */
343 extern real add_rot_forces(t_rot *rot, rvec f[], t_commrec *cr, int step, real t)
347 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
348 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
353 GMX_MPE_LOG(ev_add_rot_forces_start);
355 /* Reduce energy,torque, angles etc. to get the sum values (per rotation group)
356 * on the master and output these values to file. */
357 if (do_per_step(step, rot->nsttout))
358 reduce_output(cr, rot, t);
360 /* Total rotation potential is the sum over all rotation groups */
363 /* Loop over enforced rotation groups (usually 1, though)
364 * Apply the forces from rotation potentials */
365 for (g=0; g<rot->ngrp; g++)
370 for (l=0; l<erg->nat_loc; l++)
372 /* Get the right index of the local force */
373 ii = erg->ind_loc[l];
375 rvec_inc(f[ii],erg->f_rot_loc[l]);
379 GMX_MPE_LOG(ev_add_rot_forces_finish);
381 return (MASTER(cr)? er->Vrot : 0.0);
385 /* Calculate the maximum beta that leads to a gaussian larger min_gaussian,
386 * also does some checks
388 static double calc_beta_max(real min_gaussian, real slab_dist)
390 const double norm = 0.5698457353514458216; /* = 1/1.7548609 */
395 /* Actually the next two checks are already made in grompp */
397 gmx_fatal(FARGS, "Slab distance of flexible rotation groups must be >=0 !");
398 if (min_gaussian <= 0)
399 gmx_fatal(FARGS, "Cutoff value for Gaussian must be > 0. (You requested %f)");
401 /* Define the sigma value */
402 sigma = 0.7*slab_dist;
404 /* Calculate the argument for the logarithm and check that the log() result is negative or 0 */
405 arg = min_gaussian/norm;
407 gmx_fatal(FARGS, "min_gaussian of flexible rotation groups must be <%g", norm);
409 return sqrt(-2.0*sigma*sigma*log(min_gaussian/norm));
413 static inline real calc_beta(rvec curr_x, t_rotgrp *rotg, int n)
415 return iprod(curr_x, rotg->vec) - rotg->slab_dist * n;
419 static inline real gaussian_weight(rvec curr_x, t_rotgrp *rotg, int n)
421 /* norm is chosen such that the sum of the gaussians
422 * over the slabs is approximately 1.0 everywhere */
423 /* a previously used value was norm = 0.5698457353514458216 = 1/1.7548609 */
424 const real norm = 0.569917543430618; /* = 1/1.7546397922417 */
428 /* Define the sigma value */
429 sigma = 0.7*rotg->slab_dist;
430 /* Calculate the Gaussian value of slab n for position curr_x */
431 return norm * exp( -0.5 * sqr( calc_beta(curr_x, rotg, n)/sigma ) );
435 /* Returns the weight in a single slab, also calculates the Gaussian- and mass-
436 * weighted sum of positions for that slab */
437 static real get_slab_weight(int j, t_rotgrp *rotg, rvec xc[], real mc[], rvec *x_weighted_sum)
439 rvec curr_x; /* The position of an atom */
440 rvec curr_x_weighted; /* The gaussian-weighted position */
441 real gaussian; /* A single gaussian weight */
442 real wgauss; /* gaussian times current mass */
443 real slabweight = 0.0; /* The sum of weights in the slab */
445 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
449 clear_rvec(*x_weighted_sum);
452 islab = j - erg->slab_first;
454 /* Loop over all atoms in the rotation group */
455 for (i=0; i<rotg->nat; i++)
457 copy_rvec(xc[i], curr_x);
458 gaussian = gaussian_weight(curr_x, rotg, j);
459 wgauss = gaussian * mc[i];
460 svmul(wgauss, curr_x, curr_x_weighted);
461 rvec_add(*x_weighted_sum, curr_x_weighted, *x_weighted_sum);
462 slabweight += wgauss;
463 } /* END of loop over rotation group atoms */
469 static void get_slab_centers(
470 t_rotgrp *rotg, /* The rotation group information */
471 rvec *xc, /* The rotation group positions; will
472 typically be enfrotgrp->xc, but at first call
473 it is enfrotgrp->xc_ref */
474 real *mc, /* The masses of the rotation group atoms */
475 t_commrec *cr, /* Communication record */
476 int g, /* The number of the rotation group */
477 real time, /* Used for output only */
478 FILE *out_slabs, /* For outputting center per slab information */
479 gmx_bool bOutStep, /* Is this an output step? */
480 gmx_bool bReference) /* If this routine is called from
481 init_rot_group we need to store
482 the reference slab centers */
485 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
490 /* Loop over slabs */
491 for (j = erg->slab_first; j <= erg->slab_last; j++)
493 islab = j - erg->slab_first;
494 erg->slab_weights[islab] = get_slab_weight(j, rotg, xc, mc, &erg->slab_center[islab]);
496 /* We can do the calculations ONLY if there is weight in the slab! */
497 if (erg->slab_weights[islab] > WEIGHT_MIN)
499 svmul(1.0/erg->slab_weights[islab], erg->slab_center[islab], erg->slab_center[islab]);
503 /* We need to check this here, since we divide through slab_weights
504 * in the flexible low-level routines! */
505 gmx_fatal(FARGS, "Not enough weight in slab %d. Slab center cannot be determined!", j);
508 /* At first time step: save the centers of the reference structure */
510 copy_rvec(erg->slab_center[islab], erg->slab_center_ref[islab]);
511 } /* END of loop over slabs */
513 /* Output on the master */
514 if (MASTER(cr) && bOutStep)
516 fprintf(out_slabs, "%12.3e%6d", time, g);
517 for (j = erg->slab_first; j <= erg->slab_last; j++)
519 islab = j - erg->slab_first;
520 fprintf(out_slabs, "%6d%12.3e%12.3e%12.3e",
521 j,erg->slab_center[islab][XX],erg->slab_center[islab][YY],erg->slab_center[islab][ZZ]);
523 fprintf(out_slabs, "\n");
528 static void calc_rotmat(
530 real degangle, /* Angle alpha of rotation at time t in degrees */
531 matrix rotmat) /* Rotation matrix */
533 real radangle; /* Rotation angle in radians */
534 real cosa; /* cosine alpha */
535 real sina; /* sine alpha */
536 real OMcosa; /* 1 - cos(alpha) */
537 real dumxy, dumxz, dumyz; /* save computations */
538 rvec rot_vec; /* Rotate around rot_vec ... */
541 radangle = degangle * M_PI/180.0;
542 copy_rvec(vec , rot_vec );
544 /* Precompute some variables: */
545 cosa = cos(radangle);
546 sina = sin(radangle);
548 dumxy = rot_vec[XX]*rot_vec[YY]*OMcosa;
549 dumxz = rot_vec[XX]*rot_vec[ZZ]*OMcosa;
550 dumyz = rot_vec[YY]*rot_vec[ZZ]*OMcosa;
552 /* Construct the rotation matrix for this rotation group: */
554 rotmat[XX][XX] = cosa + rot_vec[XX]*rot_vec[XX]*OMcosa;
555 rotmat[YY][XX] = dumxy + rot_vec[ZZ]*sina;
556 rotmat[ZZ][XX] = dumxz - rot_vec[YY]*sina;
558 rotmat[XX][YY] = dumxy - rot_vec[ZZ]*sina;
559 rotmat[YY][YY] = cosa + rot_vec[YY]*rot_vec[YY]*OMcosa;
560 rotmat[ZZ][YY] = dumyz + rot_vec[XX]*sina;
562 rotmat[XX][ZZ] = dumxz + rot_vec[YY]*sina;
563 rotmat[YY][ZZ] = dumyz - rot_vec[XX]*sina;
564 rotmat[ZZ][ZZ] = cosa + rot_vec[ZZ]*rot_vec[ZZ]*OMcosa;
569 for (iii=0; iii<3; iii++) {
570 for (jjj=0; jjj<3; jjj++)
571 fprintf(stderr, " %10.8f ", rotmat[iii][jjj]);
572 fprintf(stderr, "\n");
578 /* Calculates torque on the rotation axis tau = position x force */
579 static inline real torque(
580 rvec rotvec, /* rotation vector; MUST be normalized! */
581 rvec force, /* force */
582 rvec x, /* position of atom on which the force acts */
583 rvec pivot) /* pivot point of rotation axis */
588 /* Subtract offset */
589 rvec_sub(x,pivot,vectmp);
591 /* position x force */
592 cprod(vectmp, force, tau);
594 /* Return the part of the torque which is parallel to the rotation vector */
595 return iprod(tau, rotvec);
599 /* Right-aligned output of value with standard width */
600 static void print_aligned(FILE *fp, char *str)
602 fprintf(fp, "%12s", str);
606 /* Right-aligned output of value with standard short width */
607 static void print_aligned_short(FILE *fp, char *str)
609 fprintf(fp, "%6s", str);
613 /* Right-aligned output of value with standard width */
614 static void print_aligned_group(FILE *fp, char *str, int g)
619 sprintf(sbuf, "%s%d", str, g);
620 fprintf(fp, "%12s", sbuf);
624 static FILE *open_output_file(const char *fn, int steps)
629 fp = ffopen(fn, "w");
631 fprintf(fp, "# Output is written every %d time steps.\n\n", steps);
637 /* Open output file for slab COG data. Call on master only */
638 static FILE *open_slab_out(t_rot *rot, const char *fn)
645 for (g=0; g<rot->ngrp; g++)
648 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
649 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
652 fp = open_output_file(fn, rot->nsttout);
653 fprintf(fp, "# Rotation group %d (%s), slab distance %f nm\n", g, erotg_names[rotg->eType], rotg->slab_dist);
659 fprintf(fp, "# The following columns will have the syntax: (COG = center of geometry, gaussian weighted)\n");
661 print_aligned_short(fp, "t");
662 print_aligned_short(fp, "grp");
663 print_aligned_short(fp, "slab");
664 print_aligned(fp, "COG-X");
665 print_aligned(fp, "COG-Y");
666 print_aligned(fp, "COG-Z");
667 print_aligned_short(fp, "slab");
668 print_aligned(fp, "COG-X");
669 print_aligned(fp, "COG-Y");
670 print_aligned(fp, "COG-Z");
671 print_aligned_short(fp, "slab");
672 fprintf(fp, " ...\n");
680 /* Open output file and print some general information about the rotation groups.
681 * Call on master only */
682 static FILE *open_rot_out(const char *fn, t_rot *rot, const output_env_t oenv,
688 const char **setname;
690 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
694 if (Flags & MD_APPENDFILES)
696 fp = gmx_fio_fopen(fn,"a");
700 fp = xvgropen(fn, "Rotation angles and energy", "Time [ps]", "angles [degree] and energies [kJ/mol]", oenv);
701 fprintf(fp, "# The scalar tau is the torque [kJ/mol] in the direction of the rotation vector v.\n");
702 fprintf(fp, "# To obtain the vectorial torque, multiply tau with the group's rot_vec.\n#\n");
704 for (g=0; g<rot->ngrp; g++)
708 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
709 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
712 fprintf(fp, "# Rotation group %d, potential type '%s':\n" , g, erotg_names[rotg->eType]);
713 fprintf(fp, "# rot_massw%d %s\n" , g, yesno_names[rotg->bMassW]);
714 fprintf(fp, "# rot_vec%d %12.5e %12.5e %12.5e\n" , g, rotg->vec[XX], rotg->vec[YY], rotg->vec[ZZ]);
715 fprintf(fp, "# rot_rate%d %12.5e degree/ps\n" , g, rotg->rate);
716 fprintf(fp, "# rot_k%d %12.5e kJ/(mol*nm^2)\n" , g, rotg->k);
717 if ( rotg->eType==erotgISO || rotg->eType==erotgPM || rotg->eType==erotgRM || rotg->eType==erotgRM2)
718 fprintf(fp, "# rot_pivot%d %12.5e %12.5e %12.5e nm\n", g, rotg->pivot[XX], rotg->pivot[YY], rotg->pivot[ZZ]);
722 fprintf(fp, "# rot_slab_distance%d %f nm\n", g, rotg->slab_dist);
723 fprintf(fp, "# rot_min_gaussian%d %12.5e\n", g, rotg->min_gaussian);
726 /* Output the centers of the rotation groups for the pivot-free potentials */
727 if ((rotg->eType==erotgISOPF) || (rotg->eType==erotgPMPF) || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF
728 || (rotg->eType==erotgFLEXT) || (rotg->eType==erotgFLEX2T)) )
730 fprintf(fp, "# %s of ref. grp. %d %12.5e %12.5e %12.5e\n",
731 rotg->bMassW? "COM":"COG", g,
732 erg->xc_ref_center[XX], erg->xc_ref_center[YY], erg->xc_ref_center[ZZ]);
734 fprintf(fp, "# initial %s grp. %d %12.5e %12.5e %12.5e\n",
735 rotg->bMassW? "COM":"COG", g,
736 erg->xc_center[XX], erg->xc_center[YY], erg->xc_center[ZZ]);
739 if ( (rotg->eType == erotgRM2) || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
741 fprintf(fp, "# rot_eps%d %12.5e nm^2\n", g, rotg->eps);
745 fprintf(fp, "#\n# Legend for the following data columns:\n");
747 print_aligned_short(fp, "t");
749 snew(setname, 4*rot->ngrp);
751 for (g=0; g<rot->ngrp; g++)
754 sprintf(buf, "theta_ref%d [degree]", g);
755 print_aligned_group(fp, "theta_ref", g);
756 setname[nsets] = strdup(buf);
759 for (g=0; g<rot->ngrp; g++)
762 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
763 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
765 /* For flexible axis rotation we use RMSD fitting to determine the
766 * actual angle of the rotation group */
769 sprintf(buf, "theta-av%d [degree]", g);
770 print_aligned_group(fp, "theta_av", g);
771 setname[nsets] = strdup(buf);
773 sprintf(buf, "tau%d [kJ/mol]", g);
774 print_aligned_group(fp, "tau", g);
775 setname[nsets] = strdup(buf);
778 sprintf(buf, "energy%d [kJ/mol]", g);
779 print_aligned_group(fp, "energy", g);
780 setname[nsets] = strdup(buf);
783 fprintf(fp, "\n#\n");
786 xvgr_legend(fp, nsets, setname, oenv);
796 /* Call on master only */
797 static FILE *open_angles_out(t_rot *rot, const char *fn)
804 /* Open output file and write some information about it's structure: */
805 fp = open_output_file(fn, rot->nstrout);
806 fprintf(fp, "# All angles given in degrees, time in ps\n");
807 for (g=0; g<rot->ngrp; g++)
810 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
811 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
813 fprintf(fp, "# Rotation group %d (%s), slab distance %f nm, fit type %s\n",
814 g, erotg_names[rotg->eType], rotg->slab_dist, erotg_fitnames[rotg->eFittype]);
817 fprintf(fp, "# The following columns will have the syntax:\n");
819 print_aligned_short(fp, "t");
820 print_aligned_short(fp, "grp");
821 print_aligned(fp, "theta_ref");
822 print_aligned(fp, "theta_fit");
823 print_aligned_short(fp, "slab");
824 print_aligned_short(fp, "atoms");
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 fprintf(fp, " ...\n");
835 /* Open torque output file and write some information about it's structure.
836 * Call on master only */
837 static FILE *open_torque_out(t_rot *rot, const char *fn)
844 fp = open_output_file(fn, rot->nsttout);
846 for (g=0; g<rot->ngrp; g++)
849 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
850 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
852 fprintf(fp, "# Rotation group %d (%s), slab distance %f nm\n", g, erotg_names[rotg->eType], rotg->slab_dist);
853 fprintf(fp, "# The scalar tau is the torque [kJ/mol] in the direction of the rotation vector.\n");
854 fprintf(fp, "# To obtain the vectorial torque, multiply tau with\n");
855 fprintf(fp, "# rot_vec%d %10.3e %10.3e %10.3e\n", g, rotg->vec[XX], rotg->vec[YY], rotg->vec[ZZ]);
859 fprintf(fp, "# The following columns will have the syntax (tau=torque for that slab):\n");
861 print_aligned_short(fp, "t");
862 print_aligned_short(fp, "grp");
863 print_aligned_short(fp, "slab");
864 print_aligned(fp, "tau");
865 print_aligned_short(fp, "slab");
866 print_aligned(fp, "tau");
867 fprintf(fp, " ...\n");
874 static void swap_val(double* vec, int i, int j)
884 static void swap_col(double **mat, int i, int j)
886 double tmp[3] = {mat[0][j], mat[1][j], mat[2][j]};
899 /* Eigenvectors are stored in columns of eigen_vec */
900 static void diagonalize_symmetric(
908 jacobi(matrix,3,eigenval,eigen_vec,&n_rot);
910 /* sort in ascending order */
911 if (eigenval[0] > eigenval[1])
913 swap_val(eigenval, 0, 1);
914 swap_col(eigen_vec, 0, 1);
916 if (eigenval[1] > eigenval[2])
918 swap_val(eigenval, 1, 2);
919 swap_col(eigen_vec, 1, 2);
921 if (eigenval[0] > eigenval[1])
923 swap_val(eigenval, 0, 1);
924 swap_col(eigen_vec, 0, 1);
929 static void align_with_z(
930 rvec* s, /* Structure to align */
935 rvec zet = {0.0, 0.0, 1.0};
936 rvec rot_axis={0.0, 0.0, 0.0};
937 rvec *rotated_str=NULL;
943 snew(rotated_str, natoms);
945 /* Normalize the axis */
946 ooanorm = 1.0/norm(axis);
947 svmul(ooanorm, axis, axis);
949 /* Calculate the angle for the fitting procedure */
950 cprod(axis, zet, rot_axis);
951 angle = acos(axis[2]);
955 /* Calculate the rotation matrix */
956 calc_rotmat(rot_axis, angle*180.0/M_PI, rotmat);
958 /* Apply the rotation matrix to s */
959 for (i=0; i<natoms; i++)
965 rotated_str[i][j] += rotmat[j][k]*s[i][k];
970 /* Rewrite the rotated structure to s */
971 for(i=0; i<natoms; i++)
975 s[i][j]=rotated_str[i][j];
983 static void calc_correl_matrix(rvec* Xstr, rvec* Ystr, double** Rmat, int natoms)
994 for (k=0; k<natoms; k++)
995 Rmat[i][j] += Ystr[k][i] * Xstr[k][j];
999 static void weigh_coords(rvec* str, real* weight, int natoms)
1004 for(i=0; i<natoms; i++)
1007 str[i][j] *= sqrt(weight[i]);
1012 static double opt_angle_analytic(
1025 double **Rmat, **RtR, **eigvec;
1027 double V[3][3], WS[3][3];
1028 double rot_matrix[3][3];
1032 /* Do not change the original coordinates */
1033 snew(ref_s_1, natoms);
1034 snew(act_s_1, natoms);
1035 for(i=0; i<natoms; i++)
1037 copy_rvec(ref_s[i], ref_s_1[i]);
1038 copy_rvec(act_s[i], act_s_1[i]);
1041 /* Translate the structures to the origin */
1042 shift[XX] = -ref_com[XX];
1043 shift[YY] = -ref_com[YY];
1044 shift[ZZ] = -ref_com[ZZ];
1045 translate_x(ref_s_1, natoms, shift);
1047 shift[XX] = -act_com[XX];
1048 shift[YY] = -act_com[YY];
1049 shift[ZZ] = -act_com[ZZ];
1050 translate_x(act_s_1, natoms, shift);
1052 /* Align rotation axis with z */
1053 align_with_z(ref_s_1, natoms, axis);
1054 align_with_z(act_s_1, natoms, axis);
1056 /* Correlation matrix */
1057 Rmat = allocate_square_matrix(3);
1059 for (i=0; i<natoms; i++)
1065 /* Weight positions with sqrt(weight) */
1068 weigh_coords(ref_s_1, weight, natoms);
1069 weigh_coords(act_s_1, weight, natoms);
1072 /* Calculate correlation matrices R=YXt (X=ref_s; Y=act_s) */
1073 calc_correl_matrix(ref_s_1, act_s_1, Rmat, natoms);
1076 RtR = allocate_square_matrix(3);
1083 RtR[i][j] += Rmat[k][i] * Rmat[k][j];
1087 /* Diagonalize RtR */
1092 diagonalize_symmetric(RtR, eigvec, eigval);
1093 swap_col(eigvec,0,1);
1094 swap_col(eigvec,1,2);
1095 swap_val(eigval,0,1);
1096 swap_val(eigval,1,2);
1110 WS[i][j] = eigvec[i][j] / sqrt(eigval[j]);
1118 V[i][j] += Rmat[i][k]*WS[k][j];
1122 free_square_matrix(Rmat, 3);
1124 /* Calculate optimal rotation matrix */
1127 rot_matrix[i][j] = 0.0;
1134 rot_matrix[i][j] += eigvec[i][k]*V[j][k];
1138 rot_matrix[2][2] = 1.0;
1140 /* Determine the optimal rotation angle: */
1141 opt_angle = (-1.0)*acos(rot_matrix[0][0])*180.0/M_PI;
1142 if (rot_matrix[0][1] < 0.0)
1143 opt_angle = (-1.0)*opt_angle;
1145 /* Give back some memory */
1146 free_square_matrix(RtR, 3);
1157 /* Determine actual angle of this slab by RMSD fit */
1158 /* Not parallelized, call this routine only on the master */
1159 /* TODO: make this routine mass-weighted */
1160 static void flex_fit_angle(
1167 int i,l,n,islab,ind;
1169 rvec *fitcoords=NULL;
1170 rvec act_center; /* Center of actual positions that are passed to the fit routine */
1171 rvec ref_center; /* Same for the reference positions */
1172 double fitangle; /* This will be the actual angle of the rotation group derived
1173 * from an RMSD fit to the reference structure at t=0 */
1177 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1180 erg=rotg->enfrotgrp;
1182 /**********************************/
1183 /* First collect the data we need */
1184 /**********************************/
1186 /* Loop over slabs */
1187 for (n = erg->slab_first; n <= erg->slab_last; n++)
1189 islab = n - erg->slab_first; /* slab index */
1190 sd = &(rotg->enfrotgrp->slab_data[islab]);
1191 sd->nat = erg->lastatom[islab]-erg->firstatom[islab]+1;
1194 /* Loop over the relevant atoms in the slab */
1195 for (l=erg->firstatom[islab]; l<=erg->lastatom[islab]; l++)
1197 /* Current position of this atom: x[ii][XX/YY/ZZ] */
1198 copy_rvec(erg->xc[l], curr_x);
1200 /* The (unrotated) reference position of this atom is copied to ref_x.
1201 * Beware, the xc coords have been sorted in do_flex! */
1202 copy_rvec(erg->xc_ref_sorted[l], ref_x);
1204 /* Save data for doing angular RMSD fit later */
1205 /* Save the current atom position */
1206 copy_rvec(curr_x, sd->x[ind]);
1207 /* Save the corresponding reference position */
1208 copy_rvec(ref_x , sd->ref[ind]);
1209 /* Save the weight for this atom in this slab */
1210 sd->weight[ind] = gaussian_weight(curr_x, rotg, n);
1212 /* Next atom in this slab */
1217 /* Get the geometrical center of the whole rotation group: */
1218 get_center(erg->xc, NULL, rotg->nat, act_center);
1221 /******************************/
1222 /* Now do the fit calculation */
1223 /******************************/
1225 /* === Determine the optimal fit angle for the whole rotation group === */
1226 if (rotg->eFittype == erotgFitNORM)
1228 /* Normalize every position to it's reference length
1229 * prior to performing the fit */
1230 for (i=0; i<rotg->nat; i++)
1232 /* First put geometrical center of positions into origin */
1233 rvec_sub(erg->xc[i], act_center, coord);
1234 /* Determine the scaling factor for the length: */
1235 scal = erg->xc_ref_length[erg->xc_sortind[i]] / norm(coord);
1236 /* Get position, multiply with the scaling factor and save in buf[i] */
1237 svmul(scal, coord, erg->xc_norm[i]);
1239 fitcoords = erg->xc_norm;
1243 fitcoords = erg->xc;
1245 /* Note that from the point of view of the current positions, the reference has rotated backwards,
1246 * but we want to output the angle relative to the fixed reference, therefore the minus sign. */
1247 fitangle = -opt_angle_analytic(erg->xc_ref_sorted, fitcoords, NULL, rotg->nat, erg->xc_ref_center, act_center, rotg->vec);
1248 fprintf(fp, "%12.3e%6d%12.3f%12.3lf", t, g, degangle, fitangle);
1251 /* === Now do RMSD fitting for each slab === */
1252 /* We require at least SLAB_MIN_ATOMS in a slab, such that the fit makes sense. */
1253 #define SLAB_MIN_ATOMS 4
1255 for (n = erg->slab_first; n <= erg->slab_last; n++)
1257 islab = n - erg->slab_first; /* slab index */
1258 sd = &(rotg->enfrotgrp->slab_data[islab]);
1259 if (sd->nat >= SLAB_MIN_ATOMS)
1261 /* Get the center of the slabs reference and current positions */
1262 get_center(sd->ref, sd->weight, sd->nat, ref_center);
1263 get_center(sd->x , sd->weight, sd->nat, act_center);
1264 if (rotg->eFittype == erotgFitNORM)
1266 /* Normalize every position to it's reference length
1267 * prior to performing the fit */
1268 for (i=0; i<sd->nat;i++) /* Center */
1270 rvec_dec(sd->ref[i], ref_center);
1271 rvec_dec(sd->x[i] , act_center);
1272 /* Normalize x_i such that it gets the same length as ref_i */
1273 svmul( norm(sd->ref[i])/norm(sd->x[i]), sd->x[i], sd->x[i] );
1275 /* We already subtracted the centers */
1276 clear_rvec(ref_center);
1277 clear_rvec(act_center);
1279 fitangle = -opt_angle_analytic(sd->ref, sd->x, sd->weight, sd->nat, ref_center, act_center, rotg->vec);
1280 fprintf(fp, "%6d%6d%12.3f", n, sd->nat, fitangle);
1285 #undef SLAB_MIN_ATOMS
1289 /* Shift x with is */
1290 static inline void shift_single_coord(matrix box, rvec x, const ivec is)
1301 x[XX] += tx*box[XX][XX]+ty*box[YY][XX]+tz*box[ZZ][XX];
1302 x[YY] += ty*box[YY][YY]+tz*box[ZZ][YY];
1303 x[ZZ] += tz*box[ZZ][ZZ];
1306 x[XX] += tx*box[XX][XX];
1307 x[YY] += ty*box[YY][YY];
1308 x[ZZ] += tz*box[ZZ][ZZ];
1313 /* Determine the 'home' slab of this atom which is the
1314 * slab with the highest Gaussian weight of all */
1315 #define round(a) (int)(a+0.5)
1316 static inline int get_homeslab(
1317 rvec curr_x, /* The position for which the home slab shall be determined */
1318 rvec rotvec, /* The rotation vector */
1319 real slabdist) /* The slab distance */
1324 /* The distance of the atom to the coordinate center (where the
1325 * slab with index 0) is */
1326 dist = iprod(rotvec, curr_x);
1328 return round(dist / slabdist);
1332 /* For a local atom determine the relevant slabs, i.e. slabs in
1333 * which the gaussian is larger than min_gaussian
1335 static int get_single_atom_gaussians(
1343 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1346 erg=rotg->enfrotgrp;
1348 /* Determine the 'home' slab of this atom: */
1349 homeslab = get_homeslab(curr_x, rotg->vec, rotg->slab_dist);
1351 /* First determine the weight in the atoms home slab: */
1352 g = gaussian_weight(curr_x, rotg, homeslab);
1354 erg->gn_atom[count] = g;
1355 erg->gn_slabind[count] = homeslab;
1359 /* Determine the max slab */
1361 while (g > rotg->min_gaussian)
1364 g = gaussian_weight(curr_x, rotg, slab);
1365 erg->gn_slabind[count]=slab;
1366 erg->gn_atom[count]=g;
1371 /* Determine the max slab */
1376 g = gaussian_weight(curr_x, rotg, slab);
1377 erg->gn_slabind[count]=slab;
1378 erg->gn_atom[count]=g;
1381 while (g > rotg->min_gaussian);
1388 static void flex2_precalc_inner_sum(t_rotgrp *rotg, t_commrec *cr)
1391 rvec xi; /* positions in the i-sum */
1392 rvec xcn, ycn; /* the current and the reference slab centers */
1395 rvec rin; /* Helper variables */
1398 real OOpsii,OOpsiistar;
1399 real sin_rin; /* s_ii.r_ii */
1400 rvec s_in,tmpvec,tmpvec2;
1401 real mi,wi; /* Mass-weighting of the positions */
1403 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1406 erg=rotg->enfrotgrp;
1407 N_M = rotg->nat * erg->invmass;
1409 /* Loop over all slabs that contain something */
1410 for (n=erg->slab_first; n <= erg->slab_last; n++)
1412 islab = n - erg->slab_first; /* slab index */
1414 /* The current center of this slab is saved in xcn: */
1415 copy_rvec(erg->slab_center[islab], xcn);
1416 /* ... and the reference center in ycn: */
1417 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1419 /*** D. Calculate the whole inner sum used for second and third sum */
1420 /* For slab n, we need to loop over all atoms i again. Since we sorted
1421 * the atoms with respect to the rotation vector, we know that it is sufficient
1422 * to calculate from firstatom to lastatom only. All other contributions will
1424 clear_rvec(innersumvec);
1425 for (i = erg->firstatom[islab]; i <= erg->lastatom[islab]; i++)
1427 /* Coordinate xi of this atom */
1428 copy_rvec(erg->xc[i],xi);
1431 gaussian_xi = gaussian_weight(xi,rotg,n);
1432 mi = erg->mc_sorted[i]; /* need the sorted mass here */
1436 copy_rvec(erg->xc_ref_sorted[i],yi0); /* Reference position yi0 */
1437 rvec_sub(yi0, ycn, tmpvec2); /* tmpvec2 = yi0 - ycn */
1438 mvmul(erg->rotmat, tmpvec2, rin); /* rin = Omega.(yi0 - ycn) */
1440 /* Calculate psi_i* and sin */
1441 rvec_sub(xi, xcn, tmpvec2); /* tmpvec2 = xi - xcn */
1442 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x (xi - xcn) */
1443 OOpsiistar = norm2(tmpvec)+rotg->eps; /* OOpsii* = 1/psii* = |v x (xi-xcn)|^2 + eps */
1444 OOpsii = norm(tmpvec); /* OOpsii = 1 / psii = |v x (xi - xcn)| */
1446 /* v x (xi - xcn) */
1447 unitv(tmpvec, s_in); /* sin = ---------------- */
1448 /* |v x (xi - xcn)| */
1450 sin_rin=iprod(s_in,rin); /* sin_rin = sin . rin */
1452 /* Now the whole sum */
1453 fac = OOpsii/OOpsiistar;
1454 svmul(fac, rin, tmpvec);
1455 fac2 = fac*fac*OOpsii;
1456 svmul(fac2*sin_rin, s_in, tmpvec2);
1457 rvec_dec(tmpvec, tmpvec2);
1459 svmul(wi*gaussian_xi*sin_rin, tmpvec, tmpvec2);
1461 rvec_inc(innersumvec,tmpvec2);
1462 } /* now we have the inner sum, used both for sum2 and sum3 */
1464 /* Save it to be used in do_flex2_lowlevel */
1465 copy_rvec(innersumvec, erg->slab_innersumvec[islab]);
1466 } /* END of loop over slabs */
1470 static void flex_precalc_inner_sum(t_rotgrp *rotg, t_commrec *cr)
1473 rvec xi; /* position */
1474 rvec xcn, ycn; /* the current and the reference slab centers */
1475 rvec qin,rin; /* q_i^n and r_i^n */
1478 rvec innersumvec; /* Inner part of sum_n2 */
1479 real gaussian_xi; /* Gaussian weight gn(xi) */
1480 real mi,wi; /* Mass-weighting of the positions */
1483 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1486 erg=rotg->enfrotgrp;
1487 N_M = rotg->nat * erg->invmass;
1489 /* Loop over all slabs that contain something */
1490 for (n=erg->slab_first; n <= erg->slab_last; n++)
1492 islab = n - erg->slab_first; /* slab index */
1494 /* The current center of this slab is saved in xcn: */
1495 copy_rvec(erg->slab_center[islab], xcn);
1496 /* ... and the reference center in ycn: */
1497 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1499 /* For slab n, we need to loop over all atoms i again. Since we sorted
1500 * the atoms with respect to the rotation vector, we know that it is sufficient
1501 * to calculate from firstatom to lastatom only. All other contributions will
1503 clear_rvec(innersumvec);
1504 for (i=erg->firstatom[islab]; i<=erg->lastatom[islab]; i++)
1506 /* Coordinate xi of this atom */
1507 copy_rvec(erg->xc[i],xi);
1510 gaussian_xi = gaussian_weight(xi,rotg,n);
1511 mi = erg->mc_sorted[i]; /* need the sorted mass here */
1514 /* Calculate rin and qin */
1515 rvec_sub(erg->xc_ref_sorted[i], ycn, tmpvec); /* tmpvec = yi0-ycn */
1516 mvmul(erg->rotmat, tmpvec, rin); /* rin = Omega.(yi0 - ycn) */
1517 cprod(rotg->vec, rin, tmpvec); /* tmpvec = v x Omega*(yi0-ycn) */
1519 /* v x Omega*(yi0-ycn) */
1520 unitv(tmpvec, qin); /* qin = --------------------- */
1521 /* |v x Omega*(yi0-ycn)| */
1524 rvec_sub(xi, xcn, tmpvec); /* tmpvec = xi-xcn */
1525 bin = iprod(qin, tmpvec); /* bin = qin*(xi-xcn) */
1527 svmul(wi*gaussian_xi*bin, qin, tmpvec);
1529 /* Add this contribution to the inner sum: */
1530 rvec_add(innersumvec, tmpvec, innersumvec);
1531 } /* now we have the inner sum vector S^n for this slab */
1532 /* Save it to be used in do_flex_lowlevel */
1533 copy_rvec(innersumvec, erg->slab_innersumvec[islab]);
1538 static real do_flex2_lowlevel(
1540 real sigma, /* The Gaussian width sigma */
1542 gmx_bool bCalcTorque,
1546 int count,ic,ii,j,m,n,islab,iigrp;
1547 rvec xj; /* position in the i-sum */
1548 rvec yj0; /* the reference position in the j-sum */
1549 rvec xcn, ycn; /* the current and the reference slab centers */
1550 real V; /* This node's part of the rotation pot. energy */
1551 real gaussian_xj; /* Gaussian weight */
1555 rvec rjn; /* Helper variables */
1558 real OOpsij,OOpsijstar;
1559 real OOsigma2; /* 1/(sigma^2) */
1562 rvec sjn,tmpvec,tmpvec2;
1563 rvec sum1vec_part,sum1vec,sum2vec_part,sum2vec,sum3vec,sum4vec,innersumvec;
1565 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1566 real mj,wj; /* Mass-weighting of the positions */
1568 real Wjn; /* g_n(x_j) m_j / Mjn */
1570 /* To calculate the torque per slab */
1571 rvec slab_force; /* Single force from slab n on one atom */
1572 rvec slab_sum1vec_part;
1573 real slab_sum3part,slab_sum4part;
1574 rvec slab_sum1vec, slab_sum2vec, slab_sum3vec, slab_sum4vec;
1577 erg=rotg->enfrotgrp;
1579 /* Pre-calculate the inner sums, so that we do not have to calculate
1580 * them again for every atom */
1581 flex2_precalc_inner_sum(rotg, cr);
1583 /********************************************************/
1584 /* Main loop over all local atoms of the rotation group */
1585 /********************************************************/
1586 N_M = rotg->nat * erg->invmass;
1588 OOsigma2 = 1.0 / (sigma*sigma);
1589 for (j=0; j<erg->nat_loc; j++)
1591 /* Local index of a rotation group atom */
1592 ii = erg->ind_loc[j];
1593 /* Position of this atom in the collective array */
1594 iigrp = erg->xc_ref_ind[j];
1595 /* Mass-weighting */
1596 mj = erg->mc[iigrp]; /* need the unsorted mass here */
1599 /* Current position of this atom: x[ii][XX/YY/ZZ]
1600 * Note that erg->xc_center contains the center of mass in case the flex2-t
1601 * potential was chosen. For the flex2 potential erg->xc_center must be
1603 rvec_sub(x[ii], erg->xc_center, xj);
1605 /* Shift this atom such that it is near its reference */
1606 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
1608 /* Determine the slabs to loop over, i.e. the ones with contributions
1609 * larger than min_gaussian */
1610 count = get_single_atom_gaussians(xj, cr, rotg);
1612 clear_rvec(sum1vec_part);
1613 clear_rvec(sum2vec_part);
1616 /* Loop over the relevant slabs for this atom */
1617 for (ic=0; ic < count; ic++)
1619 n = erg->gn_slabind[ic];
1621 /* Get the precomputed Gaussian value of curr_slab for curr_x */
1622 gaussian_xj = erg->gn_atom[ic];
1624 islab = n - erg->slab_first; /* slab index */
1626 /* The (unrotated) reference position of this atom is copied to yj0: */
1627 copy_rvec(rotg->x_ref[iigrp], yj0);
1629 beta = calc_beta(xj, rotg,n);
1631 /* The current center of this slab is saved in xcn: */
1632 copy_rvec(erg->slab_center[islab], xcn);
1633 /* ... and the reference center in ycn: */
1634 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1636 rvec_sub(yj0, ycn, tmpvec2); /* tmpvec2 = yj0 - ycn */
1639 mvmul(erg->rotmat, tmpvec2, rjn); /* rjn = Omega.(yj0 - ycn) */
1641 /* Subtract the slab center from xj */
1642 rvec_sub(xj, xcn, tmpvec2); /* tmpvec2 = xj - xcn */
1645 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x (xj - xcn) */
1647 OOpsijstar = norm2(tmpvec)+rotg->eps; /* OOpsij* = 1/psij* = |v x (xj-xcn)|^2 + eps */
1649 numerator = sqr(iprod(tmpvec, rjn));
1651 /*********************************/
1652 /* Add to the rotation potential */
1653 /*********************************/
1654 V += 0.5*rotg->k*wj*gaussian_xj*numerator/OOpsijstar;
1657 /*************************************/
1658 /* Now calculate the force on atom j */
1659 /*************************************/
1661 OOpsij = norm(tmpvec); /* OOpsij = 1 / psij = |v x (xj - xcn)| */
1663 /* v x (xj - xcn) */
1664 unitv(tmpvec, sjn); /* sjn = ---------------- */
1665 /* |v x (xj - xcn)| */
1667 sjn_rjn=iprod(sjn,rjn); /* sjn_rjn = sjn . rjn */
1670 /*** A. Calculate the first of the four sum terms: ****************/
1671 fac = OOpsij/OOpsijstar;
1672 svmul(fac, rjn, tmpvec);
1673 fac2 = fac*fac*OOpsij;
1674 svmul(fac2*sjn_rjn, sjn, tmpvec2);
1675 rvec_dec(tmpvec, tmpvec2);
1676 fac2 = wj*gaussian_xj; /* also needed for sum4 */
1677 svmul(fac2*sjn_rjn, tmpvec, slab_sum1vec_part);
1678 /********************/
1679 /*** Add to sum1: ***/
1680 /********************/
1681 rvec_inc(sum1vec_part, slab_sum1vec_part); /* sum1 still needs to vector multiplied with v */
1683 /*** B. Calculate the forth of the four sum terms: ****************/
1684 betasigpsi = beta*OOsigma2*OOpsij; /* this is also needed for sum3 */
1685 /********************/
1686 /*** Add to sum4: ***/
1687 /********************/
1688 slab_sum4part = fac2*betasigpsi*fac*sjn_rjn*sjn_rjn; /* Note that fac is still valid from above */
1689 sum4 += slab_sum4part;
1691 /*** C. Calculate Wjn for second and third sum */
1692 /* Note that we can safely divide by slab_weights since we check in
1693 * get_slab_centers that it is non-zero. */
1694 Wjn = gaussian_xj*mj/erg->slab_weights[islab];
1696 /* We already have precalculated the inner sum for slab n */
1697 copy_rvec(erg->slab_innersumvec[islab], innersumvec);
1699 /* Weigh the inner sum vector with Wjn */
1700 svmul(Wjn, innersumvec, innersumvec);
1702 /*** E. Calculate the second of the four sum terms: */
1703 /********************/
1704 /*** Add to sum2: ***/
1705 /********************/
1706 rvec_inc(sum2vec_part, innersumvec); /* sum2 still needs to be vector crossproduct'ed with v */
1708 /*** F. Calculate the third of the four sum terms: */
1709 slab_sum3part = betasigpsi * iprod(sjn, innersumvec);
1710 sum3 += slab_sum3part; /* still needs to be multiplied with v */
1712 /*** G. Calculate the torque on the local slab's axis: */
1716 cprod(slab_sum1vec_part, rotg->vec, slab_sum1vec);
1718 cprod(innersumvec, rotg->vec, slab_sum2vec);
1720 svmul(slab_sum3part, rotg->vec, slab_sum3vec);
1722 svmul(slab_sum4part, rotg->vec, slab_sum4vec);
1724 /* The force on atom ii from slab n only: */
1725 for (m=0; m<DIM; m++)
1726 slab_force[m] = rotg->k * (-slab_sum1vec[m] + slab_sum2vec[m] - slab_sum3vec[m] + 0.5*slab_sum4vec[m]);
1728 erg->slab_torque_v[islab] += torque(rotg->vec, slab_force, xj, xcn);
1730 } /* END of loop over slabs */
1732 /* Construct the four individual parts of the vector sum: */
1733 cprod(sum1vec_part, rotg->vec, sum1vec); /* sum1vec = { } x v */
1734 cprod(sum2vec_part, rotg->vec, sum2vec); /* sum2vec = { } x v */
1735 svmul(sum3, rotg->vec, sum3vec); /* sum3vec = { } . v */
1736 svmul(sum4, rotg->vec, sum4vec); /* sum4vec = { } . v */
1738 /* Store the additional force so that it can be added to the force
1739 * array after the normal forces have been evaluated */
1740 for (m=0; m<DIM; m++)
1741 erg->f_rot_loc[j][m] = rotg->k * (-sum1vec[m] + sum2vec[m] - sum3vec[m] + 0.5*sum4vec[m]);
1744 fprintf(stderr," FORCE on ATOM %d/%d = (%15.8f %15.8f %15.8f) \n",
1745 j,ii,erg->f_rot_loc[j][XX], erg->f_rot_loc[j][YY], erg->f_rot_loc[j][ZZ]);
1749 fprintf(stderr, "sum1: %15.8f %15.8f %15.8f\n", -rotg->k*sum1vec[XX], -rotg->k*sum1vec[YY], -rotg->k*sum1vec[ZZ]);
1750 fprintf(stderr, "sum2: %15.8f %15.8f %15.8f\n", rotg->k*sum2vec[XX], rotg->k*sum2vec[YY], rotg->k*sum2vec[ZZ]);
1751 fprintf(stderr, "sum3: %15.8f %15.8f %15.8f\n", -rotg->k*sum3vec[XX], -rotg->k*sum3vec[YY], -rotg->k*sum3vec[ZZ]);
1752 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]);
1754 } /* END of loop over local atoms */
1757 fprintf(stderr, "THE POTENTIAL IS V=%f\n", V);
1764 static real do_flex_lowlevel(
1766 real sigma, /* The Gaussian width sigma */
1768 gmx_bool bCalcTorque,
1772 int count,ic,ii,j,m,n,islab,iigrp;
1773 rvec xj,yj0; /* current and reference position */
1774 rvec xcn, ycn; /* the current and the reference slab centers */
1775 rvec xj_xcn; /* xj - xcn */
1776 rvec qjn; /* q_i^n */
1777 rvec sum_n1,sum_n2; /* Two contributions to the rotation force */
1778 rvec innersumvec; /* Inner part of sum_n2 */
1780 rvec force_n; /* Single force from slab n on one atom */
1781 rvec tmpvec,tmpvec2,tmp_f; /* Helper variables */
1782 real V; /* The rotation potential energy */
1783 real OOsigma2; /* 1/(sigma^2) */
1784 real beta; /* beta_n(xj) */
1785 real bjn; /* b_j^n */
1786 real gaussian_xj; /* Gaussian weight gn(xj) */
1787 real betan_xj_sigma2;
1788 real mj,wj; /* Mass-weighting of the positions */
1790 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1793 erg=rotg->enfrotgrp;
1795 /* Pre-calculate the inner sums, so that we do not have to calculate
1796 * them again for every atom */
1797 flex_precalc_inner_sum(rotg, cr);
1799 /********************************************************/
1800 /* Main loop over all local atoms of the rotation group */
1801 /********************************************************/
1802 OOsigma2 = 1.0/(sigma*sigma);
1803 N_M = rotg->nat * erg->invmass;
1805 for (j=0; j<erg->nat_loc; j++)
1807 /* Local index of a rotation group atom */
1808 ii = erg->ind_loc[j];
1809 /* Position of this atom in the collective array */
1810 iigrp = erg->xc_ref_ind[j];
1811 /* Mass-weighting */
1812 mj = erg->mc[iigrp]; /* need the unsorted mass here */
1815 /* Current position of this atom: x[ii][XX/YY/ZZ]
1816 * Note that erg->xc_center contains the center of mass in case the flex-t
1817 * potential was chosen. For the flex potential erg->xc_center must be
1819 rvec_sub(x[ii], erg->xc_center, xj);
1821 /* Shift this atom such that it is near its reference */
1822 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
1824 /* Determine the slabs to loop over, i.e. the ones with contributions
1825 * larger than min_gaussian */
1826 count = get_single_atom_gaussians(xj, cr, rotg);
1831 /* Loop over the relevant slabs for this atom */
1832 for (ic=0; ic < count; ic++)
1834 n = erg->gn_slabind[ic];
1836 /* Get the precomputed Gaussian for xj in slab n */
1837 gaussian_xj = erg->gn_atom[ic];
1839 islab = n - erg->slab_first; /* slab index */
1841 /* The (unrotated) reference position of this atom is saved in yj0: */
1842 copy_rvec(rotg->x_ref[iigrp], yj0);
1844 beta = calc_beta(xj, rotg, n);
1846 /* The current center of this slab is saved in xcn: */
1847 copy_rvec(erg->slab_center[islab], xcn);
1848 /* ... and the reference center in ycn: */
1849 copy_rvec(erg->slab_center_ref[islab+erg->slab_buffer], ycn);
1851 rvec_sub(yj0, ycn, tmpvec); /* tmpvec = yj0 - ycn */
1854 mvmul(erg->rotmat, tmpvec, tmpvec2); /* tmpvec2 = Omega.(yj0-ycn) */
1856 /* Subtract the slab center from xj */
1857 rvec_sub(xj, xcn, xj_xcn); /* xj_xcn = xj - xcn */
1860 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x Omega.(xj-xcn) */
1862 /* v x Omega.(xj-xcn) */
1863 unitv(tmpvec,qjn); /* qjn = -------------------- */
1864 /* |v x Omega.(xj-xcn)| */
1866 bjn = iprod(qjn, xj_xcn); /* bjn = qjn * (xj - xcn) */
1868 /*********************************/
1869 /* Add to the rotation potential */
1870 /*********************************/
1871 V += 0.5*rotg->k*wj*gaussian_xj*sqr(bjn);
1873 /****************************************************************/
1874 /* sum_n1 will typically be the main contribution to the force: */
1875 /****************************************************************/
1876 betan_xj_sigma2 = beta*OOsigma2; /* beta_n(xj)/sigma^2 */
1878 /* The next lines calculate
1879 * qjn - (bjn*beta(xj)/(2sigma^2))v */
1880 svmul(bjn*0.5*betan_xj_sigma2, rotg->vec, tmpvec2);
1881 rvec_sub(qjn,tmpvec2,tmpvec);
1883 /* Multiply with gn(xj)*bjn: */
1884 svmul(gaussian_xj*bjn,tmpvec,tmpvec2);
1887 rvec_inc(sum_n1,tmpvec2);
1889 /* We already have precalculated the Sn term for slab n */
1890 copy_rvec(erg->slab_innersumvec[islab], s_n);
1892 svmul(betan_xj_sigma2*iprod(s_n, xj_xcn), rotg->vec, tmpvec); /* tmpvec = ---------- s_n (xj-xcn) */
1895 rvec_sub(s_n, tmpvec, innersumvec);
1897 /* We can safely divide by slab_weights since we check in get_slab_centers
1898 * that it is non-zero. */
1899 svmul(gaussian_xj/erg->slab_weights[islab], innersumvec, innersumvec);
1901 rvec_add(sum_n2, innersumvec, sum_n2);
1903 GMX_MPE_LOG(ev_inner_loop_finish);
1905 /* Calculate the torque: */
1908 /* The force on atom ii from slab n only: */
1909 rvec_sub(innersumvec, tmpvec2, force_n);
1910 svmul(rotg->k, force_n, force_n);
1911 erg->slab_torque_v[islab] += torque(rotg->vec, force_n, xj, xcn);
1913 } /* END of loop over slabs */
1915 /* Put both contributions together: */
1916 svmul(wj, sum_n1, sum_n1);
1917 svmul(mj, sum_n2, sum_n2);
1918 rvec_sub(sum_n2,sum_n1,tmp_f); /* F = -grad V */
1920 /* Store the additional force so that it can be added to the force
1921 * array after the normal forces have been evaluated */
1922 for(m=0; m<DIM; m++)
1923 erg->f_rot_loc[j][m] = rotg->k*tmp_f[m];
1925 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,
1926 rotg->k*tmp_f[XX] , rotg->k*tmp_f[YY] , rotg->k*tmp_f[ZZ] ,
1927 -rotg->k*sum_n1[XX], -rotg->k*sum_n1[YY], -rotg->k*sum_n1[ZZ],
1928 rotg->k*sum_n2[XX], rotg->k*sum_n2[YY], rotg->k*sum_n2[ZZ]);
1930 } /* END of loop over local atoms */
1936 static void print_coordinates(t_commrec *cr, t_rotgrp *rotg, rvec x[], matrix box, int step)
1940 static char buf[STRLEN];
1941 static gmx_bool bFirst=1;
1946 sprintf(buf, "coords%d.txt", cr->nodeid);
1947 fp = fopen(buf, "w");
1951 fprintf(fp, "\nStep %d\n", step);
1952 fprintf(fp, "box: %f %f %f %f %f %f %f %f %f\n",
1953 box[XX][XX], box[XX][YY], box[XX][ZZ],
1954 box[YY][XX], box[YY][YY], box[YY][ZZ],
1955 box[ZZ][XX], box[ZZ][ZZ], box[ZZ][ZZ]);
1956 for (i=0; i<rotg->nat; i++)
1958 fprintf(fp, "%4d %f %f %f\n", i,
1959 erg->xc[i][XX], erg->xc[i][YY], erg->xc[i][ZZ]);
1967 static int projection_compare(const void *a, const void *b)
1969 sort_along_vec_t *xca, *xcb;
1972 xca = (sort_along_vec_t *)a;
1973 xcb = (sort_along_vec_t *)b;
1975 if (xca->xcproj < xcb->xcproj)
1977 else if (xca->xcproj > xcb->xcproj)
1984 static void sort_collective_coordinates(
1985 t_rotgrp *rotg, /* Rotation group */
1986 sort_along_vec_t *data) /* Buffer for sorting the positions */
1989 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
1992 erg=rotg->enfrotgrp;
1994 /* The projection of the position vector on the rotation vector is
1995 * the relevant value for sorting. Fill the 'data' structure */
1996 for (i=0; i<rotg->nat; i++)
1998 data[i].xcproj = iprod(erg->xc[i], rotg->vec); /* sort criterium */
1999 data[i].m = erg->mc[i];
2001 copy_rvec(erg->xc[i] , data[i].x );
2002 copy_rvec(rotg->x_ref[i], data[i].x_ref);
2004 /* Sort the 'data' structure */
2005 gmx_qsort(data, rotg->nat, sizeof(sort_along_vec_t), projection_compare);
2007 /* Copy back the sorted values */
2008 for (i=0; i<rotg->nat; i++)
2010 copy_rvec(data[i].x , erg->xc[i] );
2011 copy_rvec(data[i].x_ref, erg->xc_ref_sorted[i]);
2012 erg->mc_sorted[i] = data[i].m;
2013 erg->xc_sortind[i] = data[i].ind;
2018 /* For each slab, get the first and the last index of the sorted atom
2020 static void get_firstlast_atom_per_slab(t_rotgrp *rotg, t_commrec *cr)
2024 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2027 erg=rotg->enfrotgrp;
2029 GMX_MPE_LOG(ev_get_firstlast_start);
2031 /* Find the first atom that needs to enter the calculation for each slab */
2032 n = erg->slab_first; /* slab */
2033 i = 0; /* start with the first atom */
2036 /* Find the first atom that significantly contributes to this slab */
2037 do /* move forward in position until a large enough beta is found */
2039 beta = calc_beta(erg->xc[i], rotg, n);
2041 } while ((beta < -erg->max_beta) && (i < rotg->nat));
2043 islab = n - erg->slab_first; /* slab index */
2044 erg->firstatom[islab] = i;
2045 /* Proceed to the next slab */
2047 } while (n <= erg->slab_last);
2049 /* Find the last atom for each slab */
2050 n = erg->slab_last; /* start with last slab */
2051 i = rotg->nat-1; /* start with the last atom */
2054 do /* move backward in position until a large enough beta is found */
2056 beta = calc_beta(erg->xc[i], rotg, n);
2058 } while ((beta > erg->max_beta) && (i > -1));
2060 islab = n - erg->slab_first; /* slab index */
2061 erg->lastatom[islab] = i;
2062 /* Proceed to the next slab */
2064 } while (n >= erg->slab_first);
2066 GMX_MPE_LOG(ev_get_firstlast_finish);
2070 /* Determine the very first and very last slab that needs to be considered
2071 * For the first slab that needs to be considered, we have to find the smallest
2074 * x_first * v - n*Delta_x <= beta_max
2076 * slab index n, slab distance Delta_x, rotation vector v. For the last slab we
2077 * have to find the largest n that obeys
2079 * x_last * v - n*Delta_x >= -beta_max
2082 static inline int get_first_slab(
2083 t_rotgrp *rotg, /* The rotation group (inputrec data) */
2084 real max_beta, /* The max_beta value, instead of min_gaussian */
2085 rvec firstatom) /* First atom after sorting along the rotation vector v */
2087 /* Find the first slab for the first atom */
2088 return ceil((iprod(firstatom, rotg->vec) - max_beta)/rotg->slab_dist);
2092 static inline int get_last_slab(
2093 t_rotgrp *rotg, /* The rotation group (inputrec data) */
2094 real max_beta, /* The max_beta value, instead of min_gaussian */
2095 rvec lastatom) /* Last atom along v */
2097 /* Find the last slab for the last atom */
2098 return floor((iprod(lastatom, rotg->vec) + max_beta)/rotg->slab_dist);
2102 static void get_firstlast_slab_check(
2103 t_rotgrp *rotg, /* The rotation group (inputrec data) */
2104 t_gmx_enfrotgrp *erg, /* The rotation group (data only accessible in this file) */
2105 rvec firstatom, /* First atom after sorting along the rotation vector v */
2106 rvec lastatom, /* Last atom along v */
2107 int g, /* The rotation group number */
2110 erg->slab_first = get_first_slab(rotg, erg->max_beta, firstatom);
2111 erg->slab_last = get_last_slab(rotg, erg->max_beta, lastatom);
2113 /* Check whether we have reference data to compare against */
2114 if (erg->slab_first < erg->slab_first_ref)
2115 gmx_fatal(FARGS, "%s No reference data for first slab (n=%d), unable to proceed.",
2116 RotStr, erg->slab_first);
2118 /* Check whether we have reference data to compare against */
2119 if (erg->slab_last > erg->slab_last_ref)
2120 gmx_fatal(FARGS, "%s No reference data for last slab (n=%d), unable to proceed.",
2121 RotStr, erg->slab_last);
2125 /* Enforced rotation with a flexible axis */
2126 static void do_flexible(
2128 gmx_enfrot_t enfrot, /* Other rotation data */
2129 t_rotgrp *rotg, /* The rotation group */
2130 int g, /* Group number */
2131 rvec x[], /* The local positions */
2133 double t, /* Time in picoseconds */
2134 int step, /* The time step */
2138 real sigma; /* The Gaussian width sigma */
2139 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2142 erg=rotg->enfrotgrp;
2144 /* Define the sigma value */
2145 sigma = 0.7*rotg->slab_dist;
2147 /* Sort the collective coordinates erg->xc along the rotation vector. This is
2148 * an optimization for the inner loop.
2150 sort_collective_coordinates(rotg, enfrot->data);
2152 /* Determine the first relevant slab for the first atom and the last
2153 * relevant slab for the last atom */
2154 get_firstlast_slab_check(rotg, erg, erg->xc[0], erg->xc[rotg->nat-1], g, cr);
2156 /* Determine for each slab depending on the min_gaussian cutoff criterium,
2157 * a first and a last atom index inbetween stuff needs to be calculated */
2158 get_firstlast_atom_per_slab(rotg, cr);
2160 /* Determine the gaussian-weighted center of positions for all slabs */
2161 get_slab_centers(rotg,erg->xc,erg->mc_sorted,cr,g,t,enfrot->out_slabs,bOutstep,FALSE);
2163 /* Clear the torque per slab from last time step: */
2164 nslabs = erg->slab_last - erg->slab_first + 1;
2165 for (l=0; l<nslabs; l++)
2166 erg->slab_torque_v[l] = 0.0;
2168 /* Call the rotational forces kernel */
2169 GMX_MPE_LOG(ev_flexll_start);
2170 if (rotg->eType == erotgFLEX || rotg->eType == erotgFLEXT)
2171 erg->V = do_flex_lowlevel(rotg, sigma, x, bOutstep, box, cr);
2172 else if (rotg->eType == erotgFLEX2 || rotg->eType == erotgFLEX2T)
2173 erg->V = do_flex2_lowlevel(rotg, sigma, x, bOutstep, box, cr);
2175 gmx_fatal(FARGS, "Unknown flexible rotation type");
2176 GMX_MPE_LOG(ev_flexll_finish);
2178 /* Determine actual angle of this slab by RMSD fit and output to file - Let's hope */
2179 /* this only happens once in a while, since this is not parallelized! */
2180 if (bOutstep && MASTER(cr))
2181 flex_fit_angle(g, rotg, t, erg->degangle, enfrot->out_angles);
2185 /* Calculate the angle between reference and actual rotation group atom,
2186 * both projected into a plane perpendicular to the rotation vector: */
2187 static void angle(t_rotgrp *rotg,
2191 real *weight) /* atoms near the rotation axis should count less than atoms far away */
2193 rvec xp, xrp; /* current and reference positions projected on a plane perpendicular to pg->vec */
2197 /* Project x_ref and x into a plane through the origin perpendicular to rot_vec: */
2198 /* Project x_ref: xrp = x_ref - (vec * x_ref) * vec */
2199 svmul(iprod(rotg->vec, x_ref), rotg->vec, dum);
2200 rvec_sub(x_ref, dum, xrp);
2201 /* Project x_act: */
2202 svmul(iprod(rotg->vec, x_act), rotg->vec, dum);
2203 rvec_sub(x_act, dum, xp);
2205 /* Retrieve information about which vector precedes. gmx_angle always
2206 * returns a positive angle. */
2207 cprod(xp, xrp, dum); /* if reference precedes, this is pointing into the same direction as vec */
2209 if (iprod(rotg->vec, dum) >= 0)
2210 *alpha = -gmx_angle(xrp, xp);
2212 *alpha = +gmx_angle(xrp, xp);
2214 /* Also return the weight */
2219 /* Project first vector onto a plane perpendicular to the second vector
2221 * Note that v must be of unit length.
2223 static inline void project_onto_plane(rvec dr, const rvec v)
2228 svmul(iprod(dr,v),v,tmp); /* tmp = (dr.v)v */
2229 rvec_dec(dr, tmp); /* dr = dr - (dr.v)v */
2233 /* Fixed rotation: The rotation reference group rotates around an axis */
2234 /* The atoms of the actual rotation group are attached with imaginary */
2235 /* springs to the reference atoms. */
2236 static void do_fixed(
2238 t_rotgrp *rotg, /* The rotation group */
2239 rvec x[], /* The positions */
2240 matrix box, /* The simulation box */
2241 double t, /* Time in picoseconds */
2242 int step, /* The time step */
2247 rvec tmp_f; /* Force */
2248 real alpha; /* a single angle between an actual and a reference position */
2249 real weight; /* single weight for a single angle */
2250 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2253 /* for mass weighting: */
2254 real wi; /* Mass-weighting of the positions */
2256 real k_wi; /* k times wi */
2261 erg=rotg->enfrotgrp;
2262 bProject = (rotg->eType==erotgPM) || (rotg->eType==erotgPMPF);
2264 /* Clear values from last time step */
2266 erg->fix_torque_v = 0.0;
2267 erg->fix_angles_v = 0.0;
2268 erg->fix_weight_v = 0.0;
2270 N_M = rotg->nat * erg->invmass;
2272 /* Each process calculates the forces on its local atoms */
2273 for (i=0; i<erg->nat_loc; i++)
2275 /* Calculate (x_i-x_c) resp. (x_i-u) */
2276 rvec_sub(erg->x_loc_pbc[i], erg->xc_center, tmpvec);
2278 /* Calculate Omega*(y_i-y_c)-(x_i-x_c) */
2279 rvec_sub(erg->xr_loc[i], tmpvec, dr);
2282 project_onto_plane(dr, rotg->vec);
2284 /* Mass-weighting */
2285 wi = N_M*erg->m_loc[i];
2287 /* Store the additional force so that it can be added to the force
2288 * array after the normal forces have been evaluated */
2290 for (m=0; m<DIM; m++)
2292 tmp_f[m] = k_wi*dr[m];
2293 erg->f_rot_loc[i][m] = tmp_f[m];
2294 erg->V += 0.5*k_wi*sqr(dr[m]);
2299 /* Add to the torque of this rotation group */
2300 erg->fix_torque_v += torque(rotg->vec, tmp_f, erg->x_loc_pbc[i], erg->xc_center);
2302 /* Calculate the angle between reference and actual rotation group atom. */
2303 angle(rotg, tmpvec, erg->xr_loc[i], &alpha, &weight); /* angle in rad, weighted */
2304 erg->fix_angles_v += alpha * weight;
2305 erg->fix_weight_v += weight;
2307 /* If you want enforced rotation to contribute to the virial,
2308 * activate the following lines:
2311 Add the rotation contribution to the virial
2312 for(j=0; j<DIM; j++)
2314 vir[j][m] += 0.5*f[ii][j]*dr[m];
2318 fprintf(stderr,"step %d node%d FORCE on ATOM %d = (%15.8f %15.8f %15.8f) torque=%15.8f\n", step, cr->nodeid,
2319 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);
2321 } /* end of loop over local rotation group atoms */
2325 /* Calculate the radial motion potential and forces */
2326 static void do_radial_motion(
2328 t_rotgrp *rotg, /* The rotation group */
2329 rvec x[], /* The positions */
2330 matrix box, /* The simulation box */
2331 double t, /* Time in picoseconds */
2332 int step, /* The time step */
2336 rvec tmp_f; /* Force */
2337 real alpha; /* a single angle between an actual and a reference position */
2338 real weight; /* single weight for a single angle */
2339 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2340 rvec xj_u; /* xj - u */
2345 /* For mass weighting: */
2346 real wj; /* Mass-weighting of the positions */
2350 erg=rotg->enfrotgrp;
2352 /* Clear values from last time step */
2355 erg->fix_torque_v = 0.0;
2356 erg->fix_angles_v = 0.0;
2357 erg->fix_weight_v = 0.0;
2359 N_M = rotg->nat * erg->invmass;
2361 /* Each process calculates the forces on its local atoms */
2362 for (j=0; j<erg->nat_loc; j++)
2364 /* Calculate (xj-u) */
2365 rvec_sub(erg->x_loc_pbc[j], erg->xc_center, xj_u); /* xj_u = xj-u */
2367 /* Calculate Omega.(yj-u) */
2368 cprod(rotg->vec, erg->xr_loc[j], tmpvec); /* tmpvec = v x Omega.(yj-u) */
2370 /* v x Omega.(yj-u) */
2371 unitv(tmpvec, pj); /* pj = -------------------- */
2372 /* | v x Omega.(yj-u) | */
2374 fac = iprod(pj, xj_u); /* fac = pj.(xj-u) */
2377 /* Mass-weighting */
2378 wj = N_M*erg->m_loc[j];
2380 /* Store the additional force so that it can be added to the force
2381 * array after the normal forces have been evaluated */
2382 svmul(-rotg->k*wj*fac, pj, tmp_f);
2383 copy_rvec(tmp_f, erg->f_rot_loc[j]);
2387 /* Add to the torque of this rotation group */
2388 erg->fix_torque_v += torque(rotg->vec, tmp_f, erg->x_loc_pbc[j], erg->xc_center);
2390 /* Calculate the angle between reference and actual rotation group atom. */
2391 angle(rotg, xj_u, erg->xr_loc[j], &alpha, &weight); /* angle in rad, weighted */
2392 erg->fix_angles_v += alpha * weight;
2393 erg->fix_weight_v += weight;
2396 fprintf(stderr,"RM: step %d node%d FORCE on ATOM %d = (%15.8f %15.8f %15.8f) torque=%15.8f\n", step, cr->nodeid,
2397 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);
2399 } /* end of loop over local rotation group atoms */
2400 erg->V = 0.5*rotg->k*sum;
2404 /* Calculate the radial motion pivot-free potential and forces */
2405 static void do_radial_motion_pf(
2407 t_rotgrp *rotg, /* The rotation group */
2408 rvec x[], /* The positions */
2409 matrix box, /* The simulation box */
2410 double t, /* Time in picoseconds */
2411 int step, /* The time step */
2415 rvec xj; /* Current position */
2416 rvec xj_xc; /* xj - xc */
2417 rvec yj0_yc0; /* yj0 - yc0 */
2418 rvec tmp_f; /* Force */
2419 real alpha; /* a single angle between an actual and a reference position */
2420 real weight; /* single weight for a single angle */
2421 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2422 rvec tmpvec, tmpvec2;
2423 rvec innersumvec; /* Precalculation of the inner sum */
2428 /* For mass weighting: */
2429 real mj,wi,wj; /* Mass-weighting of the positions */
2433 erg=rotg->enfrotgrp;
2435 /* Clear values from last time step */
2438 erg->fix_torque_v = 0.0;
2439 erg->fix_angles_v = 0.0;
2440 erg->fix_weight_v = 0.0;
2442 N_M = rotg->nat * erg->invmass;
2444 /* Get the current center of the rotation group: */
2445 get_center(erg->xc, erg->mc, rotg->nat, erg->xc_center);
2447 /* Precalculate Sum_i [ wi qi.(xi-xc) qi ] which is needed for every single j */
2448 clear_rvec(innersumvec);
2449 for (i=0; i < rotg->nat; i++)
2451 /* Mass-weighting */
2452 wi = N_M*erg->mc[i];
2454 /* Calculate qi. Note that xc_ref_center has already been subtracted from
2455 * x_ref in init_rot_group.*/
2456 mvmul(erg->rotmat, rotg->x_ref[i], tmpvec); /* tmpvec = Omega.(yi0-yc0) */
2458 cprod(rotg->vec, tmpvec, tmpvec2); /* tmpvec2 = v x Omega.(yi0-yc0) */
2460 /* v x Omega.(yi0-yc0) */
2461 unitv(tmpvec2, qi); /* qi = ----------------------- */
2462 /* | v x Omega.(yi0-yc0) | */
2464 rvec_sub(erg->xc[i], erg->xc_center, tmpvec); /* tmpvec = xi-xc */
2466 svmul(wi*iprod(qi, tmpvec), qi, tmpvec2);
2468 rvec_inc(innersumvec, tmpvec2);
2470 svmul(rotg->k*erg->invmass, innersumvec, innersumveckM);
2472 /* Each process calculates the forces on its local atoms */
2473 for (j=0; j<erg->nat_loc; j++)
2475 /* Local index of a rotation group atom */
2476 ii = erg->ind_loc[j];
2477 /* Position of this atom in the collective array */
2478 iigrp = erg->xc_ref_ind[j];
2479 /* Mass-weighting */
2480 mj = erg->mc[iigrp]; /* need the unsorted mass here */
2483 /* Current position of this atom: x[ii][XX/YY/ZZ] */
2484 copy_rvec(x[ii], xj);
2486 /* Shift this atom such that it is near its reference */
2487 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
2489 /* The (unrotated) reference position is yj0. yc0 has already
2490 * been subtracted in init_rot_group */
2491 copy_rvec(rotg->x_ref[iigrp], yj0_yc0); /* yj0_yc0 = yj0 - yc0 */
2493 /* Calculate Omega.(yj0-yc0) */
2494 mvmul(erg->rotmat, yj0_yc0, tmpvec2); /* tmpvec2 = Omega.(yj0 - yc0) */
2496 cprod(rotg->vec, tmpvec2, tmpvec); /* tmpvec = v x Omega.(yj0-yc0) */
2498 /* v x Omega.(yj0-yc0) */
2499 unitv(tmpvec, qj); /* qj = ----------------------- */
2500 /* | v x Omega.(yj0-yc0) | */
2502 /* Calculate (xj-xc) */
2503 rvec_sub(xj, erg->xc_center, xj_xc); /* xj_xc = xj-xc */
2505 fac = iprod(qj, xj_xc); /* fac = qj.(xj-xc) */
2508 /* Store the additional force so that it can be added to the force
2509 * array after the normal forces have been evaluated */
2510 svmul(-rotg->k*wj*fac, qj, tmp_f); /* part 1 of force */
2511 svmul(mj, innersumveckM, tmpvec); /* part 2 of force */
2512 rvec_inc(tmp_f, tmpvec);
2513 copy_rvec(tmp_f, erg->f_rot_loc[j]);
2517 /* Add to the torque of this rotation group */
2518 erg->fix_torque_v += torque(rotg->vec, tmp_f, xj, erg->xc_center);
2520 /* Calculate the angle between reference and actual rotation group atom. */
2521 angle(rotg, xj_xc, yj0_yc0, &alpha, &weight); /* angle in rad, weighted */
2522 erg->fix_angles_v += alpha * weight;
2523 erg->fix_weight_v += weight;
2526 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,
2527 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);
2529 } /* end of loop over local rotation group atoms */
2530 erg->V = 0.5*rotg->k*V;
2534 /* Precalculate the inner sum for the radial motion 2 forces */
2535 static void radial_motion2_precalc_inner_sum(t_rotgrp *rotg, rvec innersumvec)
2538 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2539 rvec xi_xc; /* xj - xc */
2540 rvec tmpvec,tmpvec2;
2544 rvec v_xi_xc; /* v x (xj - u) */
2546 real wi; /* Mass-weighting of the positions */
2550 erg=rotg->enfrotgrp;
2551 N_M = rotg->nat * erg->invmass;
2553 /* Loop over the collective set of positions */
2555 for (i=0; i<rotg->nat; i++)
2557 /* Mass-weighting */
2558 wi = N_M*erg->mc[i];
2560 rvec_sub(erg->xc[i], erg->xc_center, xi_xc); /* xi_xc = xi-xc */
2562 /* Calculate ri. Note that xc_ref_center has already been subtracted from
2563 * x_ref in init_rot_group.*/
2564 mvmul(erg->rotmat, rotg->x_ref[i], ri); /* ri = Omega.(yi0-yc0) */
2566 cprod(rotg->vec, xi_xc, v_xi_xc); /* v_xi_xc = v x (xi-u) */
2568 fac = norm2(v_xi_xc);
2570 psiistar = 1.0/(fac + rotg->eps); /* psiistar = --------------------- */
2571 /* |v x (xi-xc)|^2 + eps */
2573 psii = gmx_invsqrt(fac); /* 1 */
2574 /* psii = ------------- */
2577 svmul(psii, v_xi_xc, si); /* si = psii * (v x (xi-xc) ) */
2579 fac = iprod(v_xi_xc, ri); /* fac = (v x (xi-xc)).ri */
2582 siri = iprod(si, ri); /* siri = si.ri */
2584 svmul(psiistar/psii, ri, tmpvec);
2585 svmul(psiistar*psiistar/(psii*psii*psii) * siri, si, tmpvec2);
2586 rvec_dec(tmpvec, tmpvec2);
2587 cprod(tmpvec, rotg->vec, tmpvec2);
2589 svmul(wi*siri, tmpvec2, tmpvec);
2591 rvec_inc(sumvec, tmpvec);
2593 svmul(rotg->k*erg->invmass, sumvec, innersumvec);
2597 /* Calculate the radial motion 2 potential and forces */
2598 static void do_radial_motion2(
2600 t_rotgrp *rotg, /* The rotation group */
2601 rvec x[], /* The positions */
2602 matrix box, /* The simulation box */
2603 double t, /* Time in picoseconds */
2604 int step, /* The time step */
2608 rvec xj; /* Position */
2609 real alpha; /* a single angle between an actual and a reference position */
2610 real weight; /* single weight for a single angle */
2611 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2612 rvec xj_u; /* xj - u */
2613 rvec tmpvec,tmpvec2;
2614 real fac,fac2,Vpart;
2617 rvec v_xj_u; /* v x (xj - u) */
2619 real mj,wj; /* For mass-weighting of the positions */
2625 erg=rotg->enfrotgrp;
2627 bPF = rotg->eType==erotgRM2PF;
2628 clear_rvec(innersumvec);
2631 /* For the pivot-free variant we have to use the current center of
2632 * mass of the rotation group instead of the pivot u */
2633 get_center(erg->xc, erg->mc, rotg->nat, erg->xc_center);
2635 /* Also, we precalculate the second term of the forces that is identical
2636 * (up to the weight factor mj) for all forces */
2637 radial_motion2_precalc_inner_sum(rotg,innersumvec);
2640 /* Clear values from last time step */
2643 erg->fix_torque_v = 0.0;
2644 erg->fix_angles_v = 0.0;
2645 erg->fix_weight_v = 0.0;
2647 N_M = rotg->nat * erg->invmass;
2649 /* Each process calculates the forces on its local atoms */
2650 for (j=0; j<erg->nat_loc; j++)
2654 /* Local index of a rotation group atom */
2655 ii = erg->ind_loc[j];
2656 /* Position of this atom in the collective array */
2657 iigrp = erg->xc_ref_ind[j];
2658 /* Mass-weighting */
2659 mj = erg->mc[iigrp];
2661 /* Current position of this atom: x[ii] */
2662 copy_rvec(x[ii], xj);
2664 /* Shift this atom such that it is near its reference */
2665 shift_single_coord(box, xj, erg->xc_shifts[iigrp]);
2667 /* The (unrotated) reference position is yj0. yc0 has already
2668 * been subtracted in init_rot_group */
2669 copy_rvec(rotg->x_ref[iigrp], tmpvec); /* tmpvec = yj0 - yc0 */
2671 /* Calculate Omega.(yj0-yc0) */
2672 mvmul(erg->rotmat, tmpvec, rj); /* rj = Omega.(yj0-yc0) */
2677 copy_rvec(erg->x_loc_pbc[j], xj);
2678 copy_rvec(erg->xr_loc[j], rj); /* rj = Omega.(yj0-u) */
2680 /* Mass-weighting */
2683 /* Calculate (xj-u) resp. (xj-xc) */
2684 rvec_sub(xj, erg->xc_center, xj_u); /* xj_u = xj-u */
2686 cprod(rotg->vec, xj_u, v_xj_u); /* v_xj_u = v x (xj-u) */
2688 fac = norm2(v_xj_u);
2690 psijstar = 1.0/(fac + rotg->eps); /* psistar = -------------------- */
2691 /* |v x (xj-u)|^2 + eps */
2693 psij = gmx_invsqrt(fac); /* 1 */
2694 /* psij = ------------ */
2697 svmul(psij, v_xj_u, sj); /* sj = psij * (v x (xj-u) ) */
2699 fac = iprod(v_xj_u, rj); /* fac = (v x (xj-u)).rj */
2702 sjrj = iprod(sj, rj); /* sjrj = sj.rj */
2704 svmul(psijstar/psij, rj, tmpvec);
2705 svmul(psijstar*psijstar/(psij*psij*psij) * sjrj, sj, tmpvec2);
2706 rvec_dec(tmpvec, tmpvec2);
2707 cprod(tmpvec, rotg->vec, tmpvec2);
2709 /* Store the additional force so that it can be added to the force
2710 * array after the normal forces have been evaluated */
2711 svmul(-rotg->k*wj*sjrj, tmpvec2, tmpvec);
2712 svmul(mj, innersumvec, tmpvec2); /* This is != 0 only for the pivot-free variant */
2714 rvec_add(tmpvec2, tmpvec, erg->f_rot_loc[j]);
2715 Vpart += wj*psijstar*fac2;
2718 /* Add to the torque of this rotation group */
2719 erg->fix_torque_v += torque(rotg->vec, erg->f_rot_loc[j], xj, erg->xc_center);
2721 /* Calculate the angle between reference and actual rotation group atom. */
2722 angle(rotg, xj_u, rj, &alpha, &weight); /* angle in rad, weighted */
2723 erg->fix_angles_v += alpha * weight;
2724 erg->fix_weight_v += weight;
2727 fprintf(stderr,"RM2: step %d node%d FORCE on ATOM %d = (%15.8f %15.8f %15.8f) torque=%15.8f\n", step, cr->nodeid,
2728 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);
2730 } /* end of loop over local rotation group atoms */
2731 erg->V = 0.5*rotg->k*Vpart;
2735 /* Determine the smallest and largest position vector (with respect to the
2736 * rotation vector) for the reference group */
2737 static void get_firstlast_atom_ref(
2742 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2744 real xcproj; /* The projection of a reference position on the
2746 real minproj, maxproj; /* Smallest and largest projection on v */
2750 erg=rotg->enfrotgrp;
2752 /* Start with some value */
2753 minproj = iprod(rotg->x_ref[0], rotg->vec);
2756 /* This is just to ensure that it still works if all the atoms of the
2757 * reference structure are situated in a plane perpendicular to the rotation
2760 *lastindex = rotg->nat-1;
2762 /* Loop over all atoms of the reference group,
2763 * project them on the rotation vector to find the extremes */
2764 for (i=0; i<rotg->nat; i++)
2766 xcproj = iprod(rotg->x_ref[i], rotg->vec);
2767 if (xcproj < minproj)
2772 if (xcproj > maxproj)
2781 /* Allocate memory for the slabs */
2782 static void allocate_slabs(
2789 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2793 erg=rotg->enfrotgrp;
2795 /* More slabs than are defined for the reference are never needed */
2796 nslabs = erg->slab_last_ref - erg->slab_first_ref + 1;
2798 /* Remember how many we allocated */
2799 erg->nslabs_alloc = nslabs;
2801 if (MASTER(cr) && bVerbose)
2802 fprintf(fplog, "%s allocating memory to store data for %d slabs (rotation group %d).\n",
2804 snew(erg->slab_center , nslabs);
2805 snew(erg->slab_center_ref , nslabs);
2806 snew(erg->slab_weights , nslabs);
2807 snew(erg->slab_torque_v , nslabs);
2808 snew(erg->slab_data , nslabs);
2809 snew(erg->gn_atom , nslabs);
2810 snew(erg->gn_slabind , nslabs);
2811 snew(erg->slab_innersumvec, nslabs);
2812 for (i=0; i<nslabs; i++)
2814 snew(erg->slab_data[i].x , rotg->nat);
2815 snew(erg->slab_data[i].ref , rotg->nat);
2816 snew(erg->slab_data[i].weight, rotg->nat);
2818 snew(erg->xc_ref_sorted, rotg->nat);
2819 snew(erg->xc_sortind , rotg->nat);
2820 snew(erg->firstatom , nslabs);
2821 snew(erg->lastatom , nslabs);
2825 /* From the extreme coordinates of the reference group, determine the first
2826 * and last slab of the reference. We can never have more slabs in the real
2827 * simulation than calculated here for the reference.
2829 static void get_firstlast_slab_ref(t_rotgrp *rotg, real mc[], int ref_firstindex, int ref_lastindex)
2831 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2832 int first,last,firststart;
2836 erg=rotg->enfrotgrp;
2837 first = get_first_slab(rotg, erg->max_beta, rotg->x_ref[ref_firstindex]);
2838 last = get_last_slab( rotg, erg->max_beta, rotg->x_ref[ref_lastindex ]);
2841 while (get_slab_weight(first, rotg, rotg->x_ref, mc, &dummy) > WEIGHT_MIN)
2845 erg->slab_first_ref = first+1;
2846 while (get_slab_weight(last, rotg, rotg->x_ref, mc, &dummy) > WEIGHT_MIN)
2850 erg->slab_last_ref = last-1;
2852 erg->slab_buffer = firststart - erg->slab_first_ref;
2857 static void init_rot_group(FILE *fplog,t_commrec *cr,int g,t_rotgrp *rotg,
2858 rvec *x,gmx_mtop_t *mtop,gmx_bool bVerbose,FILE *out_slabs)
2862 gmx_bool bFlex,bColl;
2864 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
2865 int ref_firstindex, ref_lastindex;
2866 real mass,totalmass;
2869 /* Do we have a flexible axis? */
2870 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
2871 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
2873 /* Do we use a global set of coordinates? */
2874 bColl = bFlex || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF);
2876 erg=rotg->enfrotgrp;
2878 /* Allocate space for collective coordinates if needed */
2881 snew(erg->xc , rotg->nat);
2882 snew(erg->xc_shifts , rotg->nat);
2883 snew(erg->xc_eshifts, rotg->nat);
2885 /* Save the original (whole) set of positions such that later the
2886 * molecule can always be made whole again */
2887 snew(erg->xc_old , rotg->nat);
2890 for (i=0; i<rotg->nat; i++)
2893 copy_rvec(x[ii], erg->xc_old[i]);
2898 gmx_bcast(rotg->nat*sizeof(erg->xc_old[0]),erg->xc_old, cr);
2901 if (rotg->eFittype == erotgFitNORM)
2903 snew(erg->xc_ref_length, rotg->nat); /* in case fit type NORM is chosen */
2904 snew(erg->xc_norm , rotg->nat);
2909 snew(erg->xr_loc , rotg->nat);
2910 snew(erg->x_loc_pbc, rotg->nat);
2913 snew(erg->f_rot_loc , rotg->nat);
2914 snew(erg->xc_ref_ind, rotg->nat);
2916 /* xc_ref_ind needs to be set to identity in the serial case */
2918 for (i=0; i<rotg->nat; i++)
2919 erg->xc_ref_ind[i] = i;
2921 /* Copy the masses so that the COM can be determined. For all types of
2922 * enforced rotation, we store the masses in the erg->mc array. */
2923 snew(erg->mc, rotg->nat);
2925 snew(erg->mc_sorted, rotg->nat);
2927 snew(erg->m_loc, rotg->nat);
2929 for (i=0; i<rotg->nat; i++)
2933 gmx_mtop_atomnr_to_atom(mtop,rotg->ind[i],&atom);
2943 erg->invmass = 1.0/totalmass;
2945 /* Set xc_ref_center for any rotation potential */
2946 if ((rotg->eType==erotgISO) || (rotg->eType==erotgPM) || (rotg->eType==erotgRM) || (rotg->eType==erotgRM2))
2948 /* Set the pivot point for the fixed, stationary-axis potentials. This
2949 * won't change during the simulation */
2950 copy_rvec(rotg->pivot, erg->xc_ref_center);
2951 copy_rvec(rotg->pivot, erg->xc_center );
2955 /* Center of the reference positions */
2956 get_center(rotg->x_ref, erg->mc, rotg->nat, erg->xc_ref_center);
2958 /* Center of the actual positions */
2961 snew(xdum, rotg->nat);
2962 for (i=0; i<rotg->nat; i++)
2965 copy_rvec(x[ii], xdum[i]);
2967 get_center(xdum, erg->mc, rotg->nat, erg->xc_center);
2972 gmx_bcast(sizeof(erg->xc_center), erg->xc_center, cr);
2976 if ( (rotg->eType != erotgFLEX) && (rotg->eType != erotgFLEX2) )
2978 /* Put the reference positions into origin: */
2979 for (i=0; i<rotg->nat; i++)
2980 rvec_dec(rotg->x_ref[i], erg->xc_ref_center);
2983 /* Enforced rotation with flexible axis */
2986 /* Calculate maximum beta value from minimum gaussian (performance opt.) */
2987 erg->max_beta = calc_beta_max(rotg->min_gaussian, rotg->slab_dist);
2989 /* Determine the smallest and largest coordinate with respect to the rotation vector */
2990 get_firstlast_atom_ref(rotg, &ref_firstindex, &ref_lastindex);
2992 /* From the extreme coordinates of the reference group, determine the first
2993 * and last slab of the reference. */
2994 get_firstlast_slab_ref(rotg, erg->mc, ref_firstindex, ref_lastindex);
2996 /* Allocate memory for the slabs */
2997 allocate_slabs(rotg, fplog, g, bVerbose, cr);
2999 /* Flexible rotation: determine the reference centers for the rest of the simulation */
3000 erg->slab_first = erg->slab_first_ref;
3001 erg->slab_last = erg->slab_last_ref;
3002 get_slab_centers(rotg,rotg->x_ref,erg->mc,cr,g,-1,out_slabs,TRUE,TRUE);
3004 /* Length of each x_rotref vector from center (needed if fit routine NORM is chosen): */
3005 if (rotg->eFittype == erotgFitNORM)
3007 for (i=0; i<rotg->nat; i++)
3009 rvec_sub(rotg->x_ref[i], erg->xc_ref_center, coord);
3010 erg->xc_ref_length[i] = norm(coord);
3017 extern void dd_make_local_rotation_groups(gmx_domdec_t *dd,t_rot *rot,t_mdatoms *md)
3022 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3026 for(g=0; g<rot->ngrp; g++)
3028 rotg = &rot->grp[g];
3029 erg = rotg->enfrotgrp;
3032 dd_make_local_group_indices(ga2la,rotg->nat,rotg->ind,
3033 &erg->nat_loc,&erg->ind_loc,&erg->nalloc_loc,erg->xc_ref_ind);
3038 extern void init_rot(FILE *fplog,t_inputrec *ir,int nfile,const t_filenm fnm[],
3039 t_commrec *cr, rvec *x, matrix box, gmx_mtop_t *mtop, const output_env_t oenv,
3040 gmx_bool bVerbose, unsigned long Flags)
3045 int nat_max=0; /* Size of biggest rotation group */
3046 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
3047 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3048 rvec *x_pbc; /* Space for the pbc-correct atom positions */
3051 if ( (PAR(cr)) && !DOMAINDECOMP(cr) )
3052 gmx_fatal(FARGS, "Enforced rotation is only implemented for domain decomposition!");
3054 if ( MASTER(cr) && bVerbose)
3055 fprintf(stdout, "%s Initializing ...\n", RotStr);
3059 snew(rot->enfrot, 1);
3062 /* Output every step for reruns */
3063 if (Flags & MD_RERUN)
3066 fprintf(fplog, "%s rerun - will write rotation output every available step.\n", RotStr);
3071 er->out_slabs = NULL;
3073 er->out_slabs = open_slab_out(rot, opt2fn("-rs",nfile,fnm));
3077 /* Remove pbc, make molecule whole.
3078 * When ir->bContinuation=TRUE this has already been done, but ok. */
3079 snew(x_pbc,mtop->natoms);
3080 m_rveccopy(mtop->natoms,x,x_pbc);
3081 do_pbc_first_mtop(NULL,ir->ePBC,box,mtop,x_pbc);
3084 for(g=0; g<rot->ngrp; g++)
3086 rotg = &rot->grp[g];
3089 fprintf(fplog,"%s group %d type '%s'\n", RotStr, g, erotg_names[rotg->eType]);
3093 /* Allocate space for the rotation group's data: */
3094 snew(rotg->enfrotgrp, 1);
3095 erg = rotg->enfrotgrp;
3097 nat_max=max(nat_max, rotg->nat);
3102 erg->nalloc_loc = 0;
3103 erg->ind_loc = NULL;
3107 erg->nat_loc = rotg->nat;
3108 erg->ind_loc = rotg->ind;
3110 init_rot_group(fplog,cr,g,rotg,x_pbc,mtop,bVerbose,er->out_slabs);
3114 /* Allocate space for enforced rotation buffer variables */
3115 er->bufsize = nat_max;
3116 snew(er->data, nat_max);
3117 snew(er->xbuf, nat_max);
3118 snew(er->mbuf, nat_max);
3120 /* Buffers for MPI reducing torques, angles, weights (for each group), and V */
3121 er->mpi_bufsize = 4*rot->ngrp; /* To start with */
3122 snew(er->mpi_inbuf , er->mpi_bufsize);
3123 snew(er->mpi_outbuf, er->mpi_bufsize);
3125 /* Only do I/O on the MASTER */
3126 er->out_angles = NULL;
3128 er->out_torque = NULL;
3131 er->out_rot = open_rot_out(opt2fn("-ro",nfile,fnm), rot, oenv, Flags);
3132 if ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
3133 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) )
3135 if (rot->nstrout > 0)
3136 er->out_angles = open_angles_out(rot, opt2fn("-ra",nfile,fnm));
3137 if (rot->nsttout > 0)
3138 er->out_torque = open_torque_out(rot, opt2fn("-rt",nfile,fnm));
3145 extern void finish_rot(FILE *fplog,t_rot *rot)
3147 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
3152 gmx_fio_fclose(er->out_rot);
3154 gmx_fio_fclose(er->out_slabs);
3156 gmx_fio_fclose(er->out_angles);
3158 gmx_fio_fclose(er->out_torque);
3162 /* Rotate the local reference positions and store them in
3163 * erg->xr_loc[0...(nat_loc-1)]
3165 * Note that we already subtracted u or y_c from the reference positions
3166 * in init_rot_group().
3168 static void rotate_local_reference(t_rotgrp *rotg)
3170 gmx_enfrotgrp_t erg;
3174 erg=rotg->enfrotgrp;
3176 for (i=0; i<erg->nat_loc; i++)
3178 /* Index of this rotation group atom with respect to the whole rotation group */
3179 ii = erg->xc_ref_ind[i];
3181 mvmul(erg->rotmat, rotg->x_ref[ii], erg->xr_loc[i]);
3186 /* Select the PBC representation for each local x position and store that
3187 * for later usage. We assume the right PBC image of an x is the one nearest to
3188 * its rotated reference */
3189 static void choose_pbc_image(rvec x[], t_rotgrp *rotg, matrix box, int npbcdim)
3192 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3197 erg=rotg->enfrotgrp;
3199 for (i=0; i<erg->nat_loc; i++)
3203 /* Index of a rotation group atom */
3204 ii = erg->ind_loc[i];
3206 /* Get the reference position. The pivot (or COM or COG) was already
3207 * subtracted in init_rot_group() from the reference positions. Also,
3208 * the reference positions have already been rotated in
3209 * rotate_local_reference() */
3210 copy_rvec(erg->xr_loc[i], xref);
3212 /* Subtract the (old) center from the current positions
3213 * (just to determine the shifts!) */
3214 rvec_sub(x[ii], erg->xc_center, xcurr);
3216 /* Shortest PBC distance between the atom and its reference */
3217 rvec_sub(xcurr, xref, dx);
3219 /* Determine the shift for this atom */
3220 for(m=npbcdim-1; m>=0; m--)
3222 while (dx[m] < -0.5*box[m][m])
3224 for(d=0; d<DIM; d++)
3228 while (dx[m] >= 0.5*box[m][m])
3230 for(d=0; d<DIM; d++)
3236 /* Apply the shift to the current atom */
3237 copy_rvec(x[ii], erg->x_loc_pbc[i]);
3238 shift_single_coord(box, erg->x_loc_pbc[i], shift);
3243 extern void do_rotation(
3250 gmx_wallcycle_t wcycle,
3256 gmx_bool outstep_torque;
3257 gmx_bool bFlex,bColl;
3259 gmx_enfrot_t er; /* Pointer to the enforced rotation buffer variables */
3260 gmx_enfrotgrp_t erg; /* Pointer to enforced rotation group data */
3270 /* At which time steps do we want to output the torque */
3271 outstep_torque = do_per_step(step, rot->nsttout);
3273 /* Output time into rotation output file */
3274 if (outstep_torque && MASTER(cr))
3275 fprintf(er->out_rot, "%12.3e",t);
3277 /**************************************************************************/
3278 /* First do ALL the communication! */
3279 for(g=0; g<rot->ngrp; g++)
3281 rotg = &rot->grp[g];
3282 erg=rotg->enfrotgrp;
3284 /* Do we have a flexible axis? */
3285 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
3286 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
3288 /* Do we use a collective (global) set of coordinates? */
3289 bColl = bFlex || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF);
3291 /* Calculate the rotation matrix for this angle: */
3292 erg->degangle = rotg->rate * t;
3293 calc_rotmat(rotg->vec,erg->degangle,erg->rotmat);
3297 /* Transfer the rotation group's positions such that every node has
3298 * all of them. Every node contributes its local positions x and stores
3299 * it in the collective erg->xc array. */
3300 communicate_group_positions(cr,erg->xc, erg->xc_shifts, erg->xc_eshifts, bNS,
3301 x, rotg->nat, erg->nat_loc, erg->ind_loc, erg->xc_ref_ind, erg->xc_old, box);
3305 /* Fill the local masses array;
3306 * this array changes in DD/neighborsearching steps */
3309 for (i=0; i<erg->nat_loc; i++)
3311 /* Index of local atom w.r.t. the collective rotation group */
3312 ii = erg->xc_ref_ind[i];
3313 erg->m_loc[i] = erg->mc[ii];
3317 /* Calculate Omega*(y_i-y_c) for the local positions */
3318 rotate_local_reference(rotg);
3320 /* Choose the nearest PBC images of the group atoms with respect
3321 * to the rotated reference positions */
3322 choose_pbc_image(x, rotg, box, 3);
3324 /* Get the center of the rotation group */
3325 if ( (rotg->eType==erotgISOPF) || (rotg->eType==erotgPMPF) )
3326 get_center_comm(cr, erg->x_loc_pbc, erg->m_loc, erg->nat_loc, rotg->nat, erg->xc_center);
3329 } /* End of loop over rotation groups */
3331 /**************************************************************************/
3332 /* Done communicating, we can start to count cycles now ... */
3333 wallcycle_start(wcycle, ewcROT);
3334 GMX_MPE_LOG(ev_rotcycles_start);
3340 for(g=0; g<rot->ngrp; g++)
3342 rotg = &rot->grp[g];
3343 erg=rotg->enfrotgrp;
3345 bFlex = ( (rotg->eType==erotgFLEX ) || (rotg->eType==erotgFLEXT )
3346 || (rotg->eType==erotgFLEX2) || (rotg->eType==erotgFLEX2T) );
3348 bColl = bFlex || (rotg->eType==erotgRMPF) || (rotg->eType==erotgRM2PF);
3350 if (outstep_torque && MASTER(cr))
3351 fprintf(er->out_rot, "%12.4f", erg->degangle);
3359 do_fixed(cr,rotg,x,box,t,step,outstep_torque);
3362 do_radial_motion(cr,rotg,x,box,t,step,outstep_torque);
3365 do_radial_motion_pf(cr,rotg,x,box,t,step,outstep_torque);
3369 do_radial_motion2(cr,rotg,x,box,t,step,outstep_torque);
3373 /* Subtract the center of the rotation group from the collective positions array
3374 * Also store the center in erg->xc_center since it needs to be subtracted
3375 * in the low level routines from the local coordinates as well */
3376 get_center(erg->xc, erg->mc, rotg->nat, erg->xc_center);
3377 svmul(-1.0, erg->xc_center, transvec);
3378 translate_x(erg->xc, rotg->nat, transvec);
3379 do_flexible(cr,er,rotg,g,x,box,t,step,outstep_torque);
3383 /* Do NOT subtract the center of mass in the low level routines! */
3384 clear_rvec(erg->xc_center);
3385 do_flexible(cr,er,rotg,g,x,box,t,step,outstep_torque);
3388 gmx_fatal(FARGS, "No such rotation potential.");
3395 fprintf(stderr, "%s calculation (step %d) took %g seconds.\n", RotStr, step, MPI_Wtime()-t0);
3398 /* Stop the cycle counter and add to the force cycles for load balancing */
3399 cycles_rot = wallcycle_stop(wcycle,ewcROT);
3400 if (DOMAINDECOMP(cr) && wcycle)
3401 dd_cycles_add(cr->dd,cycles_rot,ddCyclF);
3402 GMX_MPE_LOG(ev_rotcycles_finish);