2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
45 #include "gromacs/utility/cstringutil.h"
47 #include "gromacs/utility/smalloc.h"
53 #include "gmx_fatal.h"
64 #include "md_support.h"
69 #include "mtop_util.h"
72 #include "gmx_omp_nthreads.h"
73 #include "md_logging.h"
75 #include "gromacs/fileio/confio.h"
76 #include "gromacs/fileio/trajectory_writing.h"
77 #include "gromacs/linearalgebra/mtxio.h"
78 #include "gromacs/linearalgebra/sparsematrix.h"
79 #include "gromacs/timing/wallcycle.h"
80 #include "gromacs/timing/walltime_accounting.h"
81 #include "gromacs/imd/imd.h"
92 static em_state_t *init_em_state()
98 /* does this need to be here? Should the array be declared differently (staticaly)in the state definition? */
99 snew(ems->s.lambda, efptNR);
104 static void print_em_start(FILE *fplog,
106 gmx_walltime_accounting_t walltime_accounting,
107 gmx_wallcycle_t wcycle,
110 walltime_accounting_start(walltime_accounting);
111 wallcycle_start(wcycle, ewcRUN);
112 print_start(fplog, cr, walltime_accounting, name);
114 static void em_time_end(gmx_walltime_accounting_t walltime_accounting,
115 gmx_wallcycle_t wcycle)
117 wallcycle_stop(wcycle, ewcRUN);
119 walltime_accounting_end(walltime_accounting);
122 static void sp_header(FILE *out, const char *minimizer, real ftol, int nsteps)
125 fprintf(out, "%s:\n", minimizer);
126 fprintf(out, " Tolerance (Fmax) = %12.5e\n", ftol);
127 fprintf(out, " Number of steps = %12d\n", nsteps);
130 static void warn_step(FILE *fp, real ftol, gmx_bool bLastStep, gmx_bool bConstrain)
136 "\nEnergy minimization reached the maximum number "
137 "of steps before the forces reached the requested "
138 "precision Fmax < %g.\n", ftol);
143 "\nEnergy minimization has stopped, but the forces have "
144 "not converged to the requested precision Fmax < %g (which "
145 "may not be possible for your system). It stopped "
146 "because the algorithm tried to make a new step whose size "
147 "was too small, or there was no change in the energy since "
148 "last step. Either way, we regard the minimization as "
149 "converged to within the available machine precision, "
150 "given your starting configuration and EM parameters.\n%s%s",
152 sizeof(real) < sizeof(double) ?
153 "\nDouble precision normally gives you higher accuracy, but "
154 "this is often not needed for preparing to run molecular "
158 "You might need to increase your constraint accuracy, or turn\n"
159 "off constraints altogether (set constraints = none in mdp file)\n" :
162 fputs(wrap_lines(buffer, 78, 0, FALSE), fp);
167 static void print_converged(FILE *fp, const char *alg, real ftol,
168 gmx_int64_t count, gmx_bool bDone, gmx_int64_t nsteps,
169 real epot, real fmax, int nfmax, real fnorm)
171 char buf[STEPSTRSIZE];
175 fprintf(fp, "\n%s converged to Fmax < %g in %s steps\n",
176 alg, ftol, gmx_step_str(count, buf));
178 else if (count < nsteps)
180 fprintf(fp, "\n%s converged to machine precision in %s steps,\n"
181 "but did not reach the requested Fmax < %g.\n",
182 alg, gmx_step_str(count, buf), ftol);
186 fprintf(fp, "\n%s did not converge to Fmax < %g in %s steps.\n",
187 alg, ftol, gmx_step_str(count, buf));
191 fprintf(fp, "Potential Energy = %21.14e\n", epot);
192 fprintf(fp, "Maximum force = %21.14e on atom %d\n", fmax, nfmax+1);
193 fprintf(fp, "Norm of force = %21.14e\n", fnorm);
195 fprintf(fp, "Potential Energy = %14.7e\n", epot);
196 fprintf(fp, "Maximum force = %14.7e on atom %d\n", fmax, nfmax+1);
197 fprintf(fp, "Norm of force = %14.7e\n", fnorm);
201 static void get_f_norm_max(t_commrec *cr,
202 t_grpopts *opts, t_mdatoms *mdatoms, rvec *f,
203 real *fnorm, real *fmax, int *a_fmax)
206 real fmax2, fmax2_0, fam;
207 int la_max, a_max, start, end, i, m, gf;
209 /* This routine finds the largest force and returns it.
210 * On parallel machines the global max is taken.
217 end = mdatoms->homenr;
218 if (mdatoms->cFREEZE)
220 for (i = start; i < end; i++)
222 gf = mdatoms->cFREEZE[i];
224 for (m = 0; m < DIM; m++)
226 if (!opts->nFreeze[gf][m])
241 for (i = start; i < end; i++)
253 if (la_max >= 0 && DOMAINDECOMP(cr))
255 a_max = cr->dd->gatindex[la_max];
263 snew(sum, 2*cr->nnodes+1);
264 sum[2*cr->nodeid] = fmax2;
265 sum[2*cr->nodeid+1] = a_max;
266 sum[2*cr->nnodes] = fnorm2;
267 gmx_sumd(2*cr->nnodes+1, sum, cr);
268 fnorm2 = sum[2*cr->nnodes];
269 /* Determine the global maximum */
270 for (i = 0; i < cr->nnodes; i++)
272 if (sum[2*i] > fmax2)
275 a_max = (int)(sum[2*i+1] + 0.5);
283 *fnorm = sqrt(fnorm2);
295 static void get_state_f_norm_max(t_commrec *cr,
296 t_grpopts *opts, t_mdatoms *mdatoms,
299 get_f_norm_max(cr, opts, mdatoms, ems->f, &ems->fnorm, &ems->fmax, &ems->a_fmax);
302 void init_em(FILE *fplog, const char *title,
303 t_commrec *cr, t_inputrec *ir,
304 t_state *state_global, gmx_mtop_t *top_global,
305 em_state_t *ems, gmx_localtop_t **top,
306 rvec **f, rvec **f_global,
307 t_nrnb *nrnb, rvec mu_tot,
308 t_forcerec *fr, gmx_enerdata_t **enerd,
309 t_graph **graph, t_mdatoms *mdatoms, gmx_global_stat_t *gstat,
310 gmx_vsite_t *vsite, gmx_constr_t constr,
311 int nfile, const t_filenm fnm[],
312 gmx_mdoutf_t *outf, t_mdebin **mdebin,
313 int imdport, unsigned long gmx_unused Flags)
320 fprintf(fplog, "Initiating %s\n", title);
323 state_global->ngtc = 0;
325 /* Initialize lambda variables */
326 initialize_lambdas(fplog, ir, &(state_global->fep_state), state_global->lambda, NULL);
330 /* Interactive molecular dynamics */
331 init_IMD(ir, cr, top_global, fplog, 1, state_global->x,
332 nfile, fnm, NULL, imdport, Flags);
334 if (DOMAINDECOMP(cr))
336 *top = dd_init_local_top(top_global);
338 dd_init_local_state(cr->dd, state_global, &ems->s);
342 /* Distribute the charge groups over the nodes from the master node */
343 dd_partition_system(fplog, ir->init_step, cr, TRUE, 1,
344 state_global, top_global, ir,
345 &ems->s, &ems->f, mdatoms, *top,
346 fr, vsite, NULL, constr,
348 dd_store_state(cr->dd, &ems->s);
352 snew(*f_global, top_global->natoms);
362 snew(*f, top_global->natoms);
364 /* Just copy the state */
365 ems->s = *state_global;
366 snew(ems->s.x, ems->s.nalloc);
367 snew(ems->f, ems->s.nalloc);
368 for (i = 0; i < state_global->natoms; i++)
370 copy_rvec(state_global->x[i], ems->s.x[i]);
372 copy_mat(state_global->box, ems->s.box);
374 *top = gmx_mtop_generate_local_top(top_global, ir);
377 forcerec_set_excl_load(fr, *top);
379 setup_bonded_threading(fr, &(*top)->idef);
381 if (ir->ePBC != epbcNONE && !fr->bMolPBC)
383 *graph = mk_graph(fplog, &((*top)->idef), 0, top_global->natoms, FALSE, FALSE);
390 atoms2md(top_global, ir, 0, NULL, top_global->natoms, mdatoms);
391 update_mdatoms(mdatoms, state_global->lambda[efptFEP]);
395 set_vsite_top(vsite, *top, mdatoms, cr);
401 if (ir->eConstrAlg == econtSHAKE &&
402 gmx_mtop_ftype_count(top_global, F_CONSTR) > 0)
404 gmx_fatal(FARGS, "Can not do energy minimization with %s, use %s\n",
405 econstr_names[econtSHAKE], econstr_names[econtLINCS]);
408 if (!DOMAINDECOMP(cr))
410 set_constraints(constr, *top, ir, mdatoms, cr);
413 if (!ir->bContinuation)
415 /* Constrain the starting coordinates */
417 constrain(PAR(cr) ? NULL : fplog, TRUE, TRUE, constr, &(*top)->idef,
418 ir, NULL, cr, -1, 0, mdatoms,
419 ems->s.x, ems->s.x, NULL, fr->bMolPBC, ems->s.box,
420 ems->s.lambda[efptFEP], &dvdl_constr,
421 NULL, NULL, nrnb, econqCoord, FALSE, 0, 0);
427 *gstat = global_stat_init(ir);
430 *outf = init_mdoutf(fplog, nfile, fnm, 0, cr, ir, top_global, NULL);
433 init_enerdata(top_global->groups.grps[egcENER].nr, ir->fepvals->n_lambda,
438 /* Init bin for energy stuff */
439 *mdebin = init_mdebin(mdoutf_get_fp_ene(*outf), top_global, ir, NULL);
443 calc_shifts(ems->s.box, fr->shift_vec);
446 static void finish_em(t_commrec *cr, gmx_mdoutf_t outf,
447 gmx_walltime_accounting_t walltime_accounting,
448 gmx_wallcycle_t wcycle)
450 if (!(cr->duty & DUTY_PME))
452 /* Tell the PME only node to finish */
453 gmx_pme_send_finish(cr);
458 em_time_end(walltime_accounting, wcycle);
461 static void swap_em_state(em_state_t *ems1, em_state_t *ems2)
470 static void copy_em_coords(em_state_t *ems, t_state *state)
474 for (i = 0; (i < state->natoms); i++)
476 copy_rvec(ems->s.x[i], state->x[i]);
480 static void write_em_traj(FILE *fplog, t_commrec *cr,
482 gmx_bool bX, gmx_bool bF, const char *confout,
483 gmx_mtop_t *top_global,
484 t_inputrec *ir, gmx_int64_t step,
486 t_state *state_global, rvec *f_global)
489 gmx_bool bIMDout = FALSE;
492 /* Shall we do IMD output? */
495 bIMDout = do_per_step(step, IMD_get_step(ir->imd->setup));
498 if ((bX || bF || bIMDout || confout != NULL) && !DOMAINDECOMP(cr))
500 copy_em_coords(state, state_global);
507 mdof_flags |= MDOF_X;
511 mdof_flags |= MDOF_F;
514 /* If we want IMD output, set appropriate MDOF flag */
517 mdof_flags |= MDOF_IMD;
520 mdoutf_write_to_trajectory_files(fplog, cr, outf, mdof_flags,
521 top_global, step, (double)step,
522 &state->s, state_global, state->f, f_global);
524 if (confout != NULL && MASTER(cr))
526 if (ir->ePBC != epbcNONE && !ir->bPeriodicMols && DOMAINDECOMP(cr))
528 /* Make molecules whole only for confout writing */
529 do_pbc_mtop(fplog, ir->ePBC, state_global->box, top_global,
533 write_sto_conf_mtop(confout,
534 *top_global->name, top_global,
535 state_global->x, NULL, ir->ePBC, state_global->box);
539 static void do_em_step(t_commrec *cr, t_inputrec *ir, t_mdatoms *md,
541 em_state_t *ems1, real a, rvec *f, em_state_t *ems2,
542 gmx_constr_t constr, gmx_localtop_t *top,
543 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
556 if (DOMAINDECOMP(cr) && s1->ddp_count != cr->dd->ddp_count)
558 gmx_incons("state mismatch in do_em_step");
561 s2->flags = s1->flags;
563 if (s2->nalloc != s1->nalloc)
565 s2->nalloc = s1->nalloc;
566 srenew(s2->x, s1->nalloc);
567 srenew(ems2->f, s1->nalloc);
568 if (s2->flags & (1<<estCGP))
570 srenew(s2->cg_p, s1->nalloc);
574 s2->natoms = s1->natoms;
575 copy_mat(s1->box, s2->box);
576 /* Copy free energy state */
577 for (i = 0; i < efptNR; i++)
579 s2->lambda[i] = s1->lambda[i];
581 copy_mat(s1->box, s2->box);
589 #pragma omp parallel num_threads(gmx_omp_nthreads_get(emntUpdate))
594 #pragma omp for schedule(static) nowait
595 for (i = start; i < end; i++)
601 for (m = 0; m < DIM; m++)
603 if (ir->opts.nFreeze[gf][m])
609 x2[i][m] = x1[i][m] + a*f[i][m];
614 if (s2->flags & (1<<estCGP))
616 /* Copy the CG p vector */
619 #pragma omp for schedule(static) nowait
620 for (i = start; i < end; i++)
622 copy_rvec(x1[i], x2[i]);
626 if (DOMAINDECOMP(cr))
628 s2->ddp_count = s1->ddp_count;
629 if (s2->cg_gl_nalloc < s1->cg_gl_nalloc)
632 s2->cg_gl_nalloc = s1->cg_gl_nalloc;
633 srenew(s2->cg_gl, s2->cg_gl_nalloc);
636 s2->ncg_gl = s1->ncg_gl;
637 #pragma omp for schedule(static) nowait
638 for (i = 0; i < s2->ncg_gl; i++)
640 s2->cg_gl[i] = s1->cg_gl[i];
642 s2->ddp_count_cg_gl = s1->ddp_count_cg_gl;
648 wallcycle_start(wcycle, ewcCONSTR);
650 constrain(NULL, TRUE, TRUE, constr, &top->idef,
651 ir, NULL, cr, count, 0, md,
652 s1->x, s2->x, NULL, bMolPBC, s2->box,
653 s2->lambda[efptBONDED], &dvdl_constr,
654 NULL, NULL, nrnb, econqCoord, FALSE, 0, 0);
655 wallcycle_stop(wcycle, ewcCONSTR);
659 static void em_dd_partition_system(FILE *fplog, int step, t_commrec *cr,
660 gmx_mtop_t *top_global, t_inputrec *ir,
661 em_state_t *ems, gmx_localtop_t *top,
662 t_mdatoms *mdatoms, t_forcerec *fr,
663 gmx_vsite_t *vsite, gmx_constr_t constr,
664 t_nrnb *nrnb, gmx_wallcycle_t wcycle)
666 /* Repartition the domain decomposition */
667 wallcycle_start(wcycle, ewcDOMDEC);
668 dd_partition_system(fplog, step, cr, FALSE, 1,
669 NULL, top_global, ir,
671 mdatoms, top, fr, vsite, NULL, constr,
672 nrnb, wcycle, FALSE);
673 dd_store_state(cr->dd, &ems->s);
674 wallcycle_stop(wcycle, ewcDOMDEC);
677 static void evaluate_energy(FILE *fplog, t_commrec *cr,
678 gmx_mtop_t *top_global,
679 em_state_t *ems, gmx_localtop_t *top,
680 t_inputrec *inputrec,
681 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
682 gmx_global_stat_t gstat,
683 gmx_vsite_t *vsite, gmx_constr_t constr,
685 t_graph *graph, t_mdatoms *mdatoms,
686 t_forcerec *fr, rvec mu_tot,
687 gmx_enerdata_t *enerd, tensor vir, tensor pres,
688 gmx_int64_t count, gmx_bool bFirst)
693 tensor force_vir, shake_vir, ekin;
694 real dvdl_constr, prescorr, enercorr, dvdlcorr;
697 /* Set the time to the initial time, the time does not change during EM */
698 t = inputrec->init_t;
701 (DOMAINDECOMP(cr) && ems->s.ddp_count < cr->dd->ddp_count))
703 /* This is the first state or an old state used before the last ns */
709 if (inputrec->nstlist > 0)
713 else if (inputrec->nstlist == -1)
715 nabnsb = natoms_beyond_ns_buffer(inputrec, fr, &top->cgs, NULL, ems->s.x);
718 gmx_sumi(1, &nabnsb, cr);
726 construct_vsites(vsite, ems->s.x, 1, NULL,
727 top->idef.iparams, top->idef.il,
728 fr->ePBC, fr->bMolPBC, cr, ems->s.box);
731 if (DOMAINDECOMP(cr) && bNS)
733 /* Repartition the domain decomposition */
734 em_dd_partition_system(fplog, count, cr, top_global, inputrec,
735 ems, top, mdatoms, fr, vsite, constr,
739 /* Calc force & energy on new trial position */
740 /* do_force always puts the charge groups in the box and shifts again
741 * We do not unshift, so molecules are always whole in congrad.c
743 do_force(fplog, cr, inputrec,
744 count, nrnb, wcycle, top, &top_global->groups,
745 ems->s.box, ems->s.x, &ems->s.hist,
746 ems->f, force_vir, mdatoms, enerd, fcd,
747 ems->s.lambda, graph, fr, vsite, mu_tot, t, NULL, NULL, TRUE,
748 GMX_FORCE_STATECHANGED | GMX_FORCE_ALLFORCES |
749 GMX_FORCE_VIRIAL | GMX_FORCE_ENERGY |
750 (bNS ? GMX_FORCE_NS | GMX_FORCE_DO_LR : 0));
752 /* Clear the unused shake virial and pressure */
753 clear_mat(shake_vir);
756 /* Communicate stuff when parallel */
757 if (PAR(cr) && inputrec->eI != eiNM)
759 wallcycle_start(wcycle, ewcMoveE);
761 global_stat(fplog, gstat, cr, enerd, force_vir, shake_vir, mu_tot,
762 inputrec, NULL, NULL, NULL, 1, &terminate,
763 top_global, &ems->s, FALSE,
769 wallcycle_stop(wcycle, ewcMoveE);
772 /* Calculate long range corrections to pressure and energy */
773 calc_dispcorr(fplog, inputrec, fr, count, top_global->natoms, ems->s.box, ems->s.lambda[efptVDW],
774 pres, force_vir, &prescorr, &enercorr, &dvdlcorr);
775 enerd->term[F_DISPCORR] = enercorr;
776 enerd->term[F_EPOT] += enercorr;
777 enerd->term[F_PRES] += prescorr;
778 enerd->term[F_DVDL] += dvdlcorr;
780 ems->epot = enerd->term[F_EPOT];
784 /* Project out the constraint components of the force */
785 wallcycle_start(wcycle, ewcCONSTR);
787 constrain(NULL, FALSE, FALSE, constr, &top->idef,
788 inputrec, NULL, cr, count, 0, mdatoms,
789 ems->s.x, ems->f, ems->f, fr->bMolPBC, ems->s.box,
790 ems->s.lambda[efptBONDED], &dvdl_constr,
791 NULL, &shake_vir, nrnb, econqForceDispl, FALSE, 0, 0);
792 if (fr->bSepDVDL && fplog)
794 gmx_print_sepdvdl(fplog, "Constraints", t, dvdl_constr);
796 enerd->term[F_DVDL_CONSTR] += dvdl_constr;
797 m_add(force_vir, shake_vir, vir);
798 wallcycle_stop(wcycle, ewcCONSTR);
802 copy_mat(force_vir, vir);
806 enerd->term[F_PRES] =
807 calc_pres(fr->ePBC, inputrec->nwall, ems->s.box, ekin, vir, pres);
809 sum_dhdl(enerd, ems->s.lambda, inputrec->fepvals);
811 if (EI_ENERGY_MINIMIZATION(inputrec->eI))
813 get_state_f_norm_max(cr, &(inputrec->opts), mdatoms, ems);
817 static double reorder_partsum(t_commrec *cr, t_grpopts *opts, t_mdatoms *mdatoms,
819 em_state_t *s_min, em_state_t *s_b)
823 int ncg, *cg_gl, *index, c, cg, i, a0, a1, a, gf, m;
825 unsigned char *grpnrFREEZE;
829 fprintf(debug, "Doing reorder_partsum\n");
835 cgs_gl = dd_charge_groups_global(cr->dd);
836 index = cgs_gl->index;
838 /* Collect fm in a global vector fmg.
839 * This conflicts with the spirit of domain decomposition,
840 * but to fully optimize this a much more complicated algorithm is required.
842 snew(fmg, mtop->natoms);
844 ncg = s_min->s.ncg_gl;
845 cg_gl = s_min->s.cg_gl;
847 for (c = 0; c < ncg; c++)
852 for (a = a0; a < a1; a++)
854 copy_rvec(fm[i], fmg[a]);
858 gmx_sum(mtop->natoms*3, fmg[0], cr);
860 /* Now we will determine the part of the sum for the cgs in state s_b */
862 cg_gl = s_b->s.cg_gl;
866 grpnrFREEZE = mtop->groups.grpnr[egcFREEZE];
867 for (c = 0; c < ncg; c++)
872 for (a = a0; a < a1; a++)
874 if (mdatoms->cFREEZE && grpnrFREEZE)
878 for (m = 0; m < DIM; m++)
880 if (!opts->nFreeze[gf][m])
882 partsum += (fb[i][m] - fmg[a][m])*fb[i][m];
894 static real pr_beta(t_commrec *cr, t_grpopts *opts, t_mdatoms *mdatoms,
896 em_state_t *s_min, em_state_t *s_b)
902 /* This is just the classical Polak-Ribiere calculation of beta;
903 * it looks a bit complicated since we take freeze groups into account,
904 * and might have to sum it in parallel runs.
907 if (!DOMAINDECOMP(cr) ||
908 (s_min->s.ddp_count == cr->dd->ddp_count &&
909 s_b->s.ddp_count == cr->dd->ddp_count))
915 /* This part of code can be incorrect with DD,
916 * since the atom ordering in s_b and s_min might differ.
918 for (i = 0; i < mdatoms->homenr; i++)
920 if (mdatoms->cFREEZE)
922 gf = mdatoms->cFREEZE[i];
924 for (m = 0; m < DIM; m++)
926 if (!opts->nFreeze[gf][m])
928 sum += (fb[i][m] - fm[i][m])*fb[i][m];
935 /* We need to reorder cgs while summing */
936 sum = reorder_partsum(cr, opts, mdatoms, mtop, s_min, s_b);
940 gmx_sumd(1, &sum, cr);
943 return sum/sqr(s_min->fnorm);
946 double do_cg(FILE *fplog, t_commrec *cr,
947 int nfile, const t_filenm fnm[],
948 const output_env_t gmx_unused oenv, gmx_bool bVerbose, gmx_bool gmx_unused bCompact,
949 int gmx_unused nstglobalcomm,
950 gmx_vsite_t *vsite, gmx_constr_t constr,
951 int gmx_unused stepout,
952 t_inputrec *inputrec,
953 gmx_mtop_t *top_global, t_fcdata *fcd,
954 t_state *state_global,
956 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
957 gmx_edsam_t gmx_unused ed,
959 int gmx_unused repl_ex_nst, int gmx_unused repl_ex_nex, int gmx_unused repl_ex_seed,
960 gmx_membed_t gmx_unused membed,
961 real gmx_unused cpt_period, real gmx_unused max_hours,
962 const char gmx_unused *deviceOptions,
964 unsigned long gmx_unused Flags,
965 gmx_walltime_accounting_t walltime_accounting)
967 const char *CG = "Polak-Ribiere Conjugate Gradients";
969 em_state_t *s_min, *s_a, *s_b, *s_c;
971 gmx_enerdata_t *enerd;
973 gmx_global_stat_t gstat;
975 rvec *f_global, *p, *sf, *sfm;
976 double gpa, gpb, gpc, tmp, sum[2], minstep;
979 real a, b, c, beta = 0.0;
983 gmx_bool converged, foundlower;
985 gmx_bool do_log = FALSE, do_ene = FALSE, do_x, do_f;
987 int number_steps, neval = 0, nstcg = inputrec->nstcgsteep;
989 int i, m, gf, step, nminstep;
994 s_min = init_em_state();
995 s_a = init_em_state();
996 s_b = init_em_state();
997 s_c = init_em_state();
999 /* Init em and store the local state in s_min */
1000 init_em(fplog, CG, cr, inputrec,
1001 state_global, top_global, s_min, &top, &f, &f_global,
1002 nrnb, mu_tot, fr, &enerd, &graph, mdatoms, &gstat, vsite, constr,
1003 nfile, fnm, &outf, &mdebin, imdport, Flags);
1005 /* Print to log file */
1006 print_em_start(fplog, cr, walltime_accounting, wcycle, CG);
1008 /* Max number of steps */
1009 number_steps = inputrec->nsteps;
1013 sp_header(stderr, CG, inputrec->em_tol, number_steps);
1017 sp_header(fplog, CG, inputrec->em_tol, number_steps);
1020 /* Call the force routine and some auxiliary (neighboursearching etc.) */
1021 /* do_force always puts the charge groups in the box and shifts again
1022 * We do not unshift, so molecules are always whole in congrad.c
1024 evaluate_energy(fplog, cr,
1025 top_global, s_min, top,
1026 inputrec, nrnb, wcycle, gstat,
1027 vsite, constr, fcd, graph, mdatoms, fr,
1028 mu_tot, enerd, vir, pres, -1, TRUE);
1033 /* Copy stuff to the energy bin for easy printing etc. */
1034 upd_mdebin(mdebin, FALSE, FALSE, (double)step,
1035 mdatoms->tmass, enerd, &s_min->s, inputrec->fepvals, inputrec->expandedvals, s_min->s.box,
1036 NULL, NULL, vir, pres, NULL, mu_tot, constr);
1038 print_ebin_header(fplog, step, step, s_min->s.lambda[efptFEP]);
1039 print_ebin(mdoutf_get_fp_ene(outf), TRUE, FALSE, FALSE, fplog, step, step, eprNORMAL,
1040 TRUE, mdebin, fcd, &(top_global->groups), &(inputrec->opts));
1044 /* Estimate/guess the initial stepsize */
1045 stepsize = inputrec->em_stepsize/s_min->fnorm;
1049 fprintf(stderr, " F-max = %12.5e on atom %d\n",
1050 s_min->fmax, s_min->a_fmax+1);
1051 fprintf(stderr, " F-Norm = %12.5e\n",
1052 s_min->fnorm/sqrt(state_global->natoms));
1053 fprintf(stderr, "\n");
1054 /* and copy to the log file too... */
1055 fprintf(fplog, " F-max = %12.5e on atom %d\n",
1056 s_min->fmax, s_min->a_fmax+1);
1057 fprintf(fplog, " F-Norm = %12.5e\n",
1058 s_min->fnorm/sqrt(state_global->natoms));
1059 fprintf(fplog, "\n");
1061 /* Start the loop over CG steps.
1062 * Each successful step is counted, and we continue until
1063 * we either converge or reach the max number of steps.
1066 for (step = 0; (number_steps < 0 || (number_steps >= 0 && step <= number_steps)) && !converged; step++)
1069 /* start taking steps in a new direction
1070 * First time we enter the routine, beta=0, and the direction is
1071 * simply the negative gradient.
1074 /* Calculate the new direction in p, and the gradient in this direction, gpa */
1079 for (i = 0; i < mdatoms->homenr; i++)
1081 if (mdatoms->cFREEZE)
1083 gf = mdatoms->cFREEZE[i];
1085 for (m = 0; m < DIM; m++)
1087 if (!inputrec->opts.nFreeze[gf][m])
1089 p[i][m] = sf[i][m] + beta*p[i][m];
1090 gpa -= p[i][m]*sf[i][m];
1091 /* f is negative gradient, thus the sign */
1100 /* Sum the gradient along the line across CPUs */
1103 gmx_sumd(1, &gpa, cr);
1106 /* Calculate the norm of the search vector */
1107 get_f_norm_max(cr, &(inputrec->opts), mdatoms, p, &pnorm, NULL, NULL);
1109 /* Just in case stepsize reaches zero due to numerical precision... */
1112 stepsize = inputrec->em_stepsize/pnorm;
1116 * Double check the value of the derivative in the search direction.
1117 * If it is positive it must be due to the old information in the
1118 * CG formula, so just remove that and start over with beta=0.
1119 * This corresponds to a steepest descent step.
1124 step--; /* Don't count this step since we are restarting */
1125 continue; /* Go back to the beginning of the big for-loop */
1128 /* Calculate minimum allowed stepsize, before the average (norm)
1129 * relative change in coordinate is smaller than precision
1132 for (i = 0; i < mdatoms->homenr; i++)
1134 for (m = 0; m < DIM; m++)
1136 tmp = fabs(s_min->s.x[i][m]);
1145 /* Add up from all CPUs */
1148 gmx_sumd(1, &minstep, cr);
1151 minstep = GMX_REAL_EPS/sqrt(minstep/(3*state_global->natoms));
1153 if (stepsize < minstep)
1159 /* Write coordinates if necessary */
1160 do_x = do_per_step(step, inputrec->nstxout);
1161 do_f = do_per_step(step, inputrec->nstfout);
1163 write_em_traj(fplog, cr, outf, do_x, do_f, NULL,
1164 top_global, inputrec, step,
1165 s_min, state_global, f_global);
1167 /* Take a step downhill.
1168 * In theory, we should minimize the function along this direction.
1169 * That is quite possible, but it turns out to take 5-10 function evaluations
1170 * for each line. However, we dont really need to find the exact minimum -
1171 * it is much better to start a new CG step in a modified direction as soon
1172 * as we are close to it. This will save a lot of energy evaluations.
1174 * In practice, we just try to take a single step.
1175 * If it worked (i.e. lowered the energy), we increase the stepsize but
1176 * the continue straight to the next CG step without trying to find any minimum.
1177 * If it didn't work (higher energy), there must be a minimum somewhere between
1178 * the old position and the new one.
1180 * Due to the finite numerical accuracy, it turns out that it is a good idea
1181 * to even accept a SMALL increase in energy, if the derivative is still downhill.
1182 * This leads to lower final energies in the tests I've done. / Erik
1184 s_a->epot = s_min->epot;
1186 c = a + stepsize; /* reference position along line is zero */
1188 if (DOMAINDECOMP(cr) && s_min->s.ddp_count < cr->dd->ddp_count)
1190 em_dd_partition_system(fplog, step, cr, top_global, inputrec,
1191 s_min, top, mdatoms, fr, vsite, constr,
1195 /* Take a trial step (new coords in s_c) */
1196 do_em_step(cr, inputrec, mdatoms, fr->bMolPBC, s_min, c, s_min->s.cg_p, s_c,
1197 constr, top, nrnb, wcycle, -1);
1200 /* Calculate energy for the trial step */
1201 evaluate_energy(fplog, cr,
1202 top_global, s_c, top,
1203 inputrec, nrnb, wcycle, gstat,
1204 vsite, constr, fcd, graph, mdatoms, fr,
1205 mu_tot, enerd, vir, pres, -1, FALSE);
1207 /* Calc derivative along line */
1211 for (i = 0; i < mdatoms->homenr; i++)
1213 for (m = 0; m < DIM; m++)
1215 gpc -= p[i][m]*sf[i][m]; /* f is negative gradient, thus the sign */
1218 /* Sum the gradient along the line across CPUs */
1221 gmx_sumd(1, &gpc, cr);
1224 /* This is the max amount of increase in energy we tolerate */
1225 tmp = sqrt(GMX_REAL_EPS)*fabs(s_a->epot);
1227 /* Accept the step if the energy is lower, or if it is not significantly higher
1228 * and the line derivative is still negative.
1230 if (s_c->epot < s_a->epot || (gpc < 0 && s_c->epot < (s_a->epot + tmp)))
1233 /* Great, we found a better energy. Increase step for next iteration
1234 * if we are still going down, decrease it otherwise
1238 stepsize *= 1.618034; /* The golden section */
1242 stepsize *= 0.618034; /* 1/golden section */
1247 /* New energy is the same or higher. We will have to do some work
1248 * to find a smaller value in the interval. Take smaller step next time!
1251 stepsize *= 0.618034;
1257 /* OK, if we didn't find a lower value we will have to locate one now - there must
1258 * be one in the interval [a=0,c].
1259 * The same thing is valid here, though: Don't spend dozens of iterations to find
1260 * the line minimum. We try to interpolate based on the derivative at the endpoints,
1261 * and only continue until we find a lower value. In most cases this means 1-2 iterations.
1263 * I also have a safeguard for potentially really patological functions so we never
1264 * take more than 20 steps before we give up ...
1266 * If we already found a lower value we just skip this step and continue to the update.
1274 /* Select a new trial point.
1275 * If the derivatives at points a & c have different sign we interpolate to zero,
1276 * otherwise just do a bisection.
1278 if (gpa < 0 && gpc > 0)
1280 b = a + gpa*(a-c)/(gpc-gpa);
1287 /* safeguard if interpolation close to machine accuracy causes errors:
1288 * never go outside the interval
1290 if (b <= a || b >= c)
1295 if (DOMAINDECOMP(cr) && s_min->s.ddp_count != cr->dd->ddp_count)
1297 /* Reload the old state */
1298 em_dd_partition_system(fplog, -1, cr, top_global, inputrec,
1299 s_min, top, mdatoms, fr, vsite, constr,
1303 /* Take a trial step to this new point - new coords in s_b */
1304 do_em_step(cr, inputrec, mdatoms, fr->bMolPBC, s_min, b, s_min->s.cg_p, s_b,
1305 constr, top, nrnb, wcycle, -1);
1308 /* Calculate energy for the trial step */
1309 evaluate_energy(fplog, cr,
1310 top_global, s_b, top,
1311 inputrec, nrnb, wcycle, gstat,
1312 vsite, constr, fcd, graph, mdatoms, fr,
1313 mu_tot, enerd, vir, pres, -1, FALSE);
1315 /* p does not change within a step, but since the domain decomposition
1316 * might change, we have to use cg_p of s_b here.
1321 for (i = 0; i < mdatoms->homenr; i++)
1323 for (m = 0; m < DIM; m++)
1325 gpb -= p[i][m]*sf[i][m]; /* f is negative gradient, thus the sign */
1328 /* Sum the gradient along the line across CPUs */
1331 gmx_sumd(1, &gpb, cr);
1336 fprintf(debug, "CGE: EpotA %f EpotB %f EpotC %f gpb %f\n",
1337 s_a->epot, s_b->epot, s_c->epot, gpb);
1340 epot_repl = s_b->epot;
1342 /* Keep one of the intervals based on the value of the derivative at the new point */
1345 /* Replace c endpoint with b */
1346 swap_em_state(s_b, s_c);
1352 /* Replace a endpoint with b */
1353 swap_em_state(s_b, s_a);
1359 * Stop search as soon as we find a value smaller than the endpoints.
1360 * Never run more than 20 steps, no matter what.
1364 while ((epot_repl > s_a->epot || epot_repl > s_c->epot) &&
1367 if (fabs(epot_repl - s_min->epot) < fabs(s_min->epot)*GMX_REAL_EPS ||
1370 /* OK. We couldn't find a significantly lower energy.
1371 * If beta==0 this was steepest descent, and then we give up.
1372 * If not, set beta=0 and restart with steepest descent before quitting.
1382 /* Reset memory before giving up */
1388 /* Select min energy state of A & C, put the best in B.
1390 if (s_c->epot < s_a->epot)
1394 fprintf(debug, "CGE: C (%f) is lower than A (%f), moving C to B\n",
1395 s_c->epot, s_a->epot);
1397 swap_em_state(s_b, s_c);
1405 fprintf(debug, "CGE: A (%f) is lower than C (%f), moving A to B\n",
1406 s_a->epot, s_c->epot);
1408 swap_em_state(s_b, s_a);
1418 fprintf(debug, "CGE: Found a lower energy %f, moving C to B\n",
1421 swap_em_state(s_b, s_c);
1426 /* new search direction */
1427 /* beta = 0 means forget all memory and restart with steepest descents. */
1428 if (nstcg && ((step % nstcg) == 0))
1434 /* s_min->fnorm cannot be zero, because then we would have converged
1438 /* Polak-Ribiere update.
1439 * Change to fnorm2/fnorm2_old for Fletcher-Reeves
1441 beta = pr_beta(cr, &inputrec->opts, mdatoms, top_global, s_min, s_b);
1443 /* Limit beta to prevent oscillations */
1444 if (fabs(beta) > 5.0)
1450 /* update positions */
1451 swap_em_state(s_min, s_b);
1454 /* Print it if necessary */
1459 fprintf(stderr, "\rStep %d, Epot=%12.6e, Fnorm=%9.3e, Fmax=%9.3e (atom %d)\n",
1460 step, s_min->epot, s_min->fnorm/sqrt(state_global->natoms),
1461 s_min->fmax, s_min->a_fmax+1);
1463 /* Store the new (lower) energies */
1464 upd_mdebin(mdebin, FALSE, FALSE, (double)step,
1465 mdatoms->tmass, enerd, &s_min->s, inputrec->fepvals, inputrec->expandedvals, s_min->s.box,
1466 NULL, NULL, vir, pres, NULL, mu_tot, constr);
1468 do_log = do_per_step(step, inputrec->nstlog);
1469 do_ene = do_per_step(step, inputrec->nstenergy);
1471 /* Prepare IMD energy record, if bIMD is TRUE. */
1472 IMD_fill_energy_record(inputrec->bIMD, inputrec->imd, enerd, step, TRUE);
1476 print_ebin_header(fplog, step, step, s_min->s.lambda[efptFEP]);
1478 print_ebin(mdoutf_get_fp_ene(outf), do_ene, FALSE, FALSE,
1479 do_log ? fplog : NULL, step, step, eprNORMAL,
1480 TRUE, mdebin, fcd, &(top_global->groups), &(inputrec->opts));
1483 /* Send energies and positions to the IMD client if bIMD is TRUE. */
1484 if (do_IMD(inputrec->bIMD, step, cr, TRUE, state_global->box, state_global->x, inputrec, 0, wcycle) && MASTER(cr))
1486 IMD_send_positions(inputrec->imd);
1489 /* Stop when the maximum force lies below tolerance.
1490 * If we have reached machine precision, converged is already set to true.
1492 converged = converged || (s_min->fmax < inputrec->em_tol);
1494 } /* End of the loop */
1496 /* IMD cleanup, if bIMD is TRUE. */
1497 IMD_finalize(inputrec->bIMD, inputrec->imd);
1501 step--; /* we never took that last step in this case */
1504 if (s_min->fmax > inputrec->em_tol)
1508 warn_step(stderr, inputrec->em_tol, step-1 == number_steps, FALSE);
1509 warn_step(fplog, inputrec->em_tol, step-1 == number_steps, FALSE);
1516 /* If we printed energy and/or logfile last step (which was the last step)
1517 * we don't have to do it again, but otherwise print the final values.
1521 /* Write final value to log since we didn't do anything the last step */
1522 print_ebin_header(fplog, step, step, s_min->s.lambda[efptFEP]);
1524 if (!do_ene || !do_log)
1526 /* Write final energy file entries */
1527 print_ebin(mdoutf_get_fp_ene(outf), !do_ene, FALSE, FALSE,
1528 !do_log ? fplog : NULL, step, step, eprNORMAL,
1529 TRUE, mdebin, fcd, &(top_global->groups), &(inputrec->opts));
1533 /* Print some stuff... */
1536 fprintf(stderr, "\nwriting lowest energy coordinates.\n");
1540 * For accurate normal mode calculation it is imperative that we
1541 * store the last conformation into the full precision binary trajectory.
1543 * However, we should only do it if we did NOT already write this step
1544 * above (which we did if do_x or do_f was true).
1546 do_x = !do_per_step(step, inputrec->nstxout);
1547 do_f = (inputrec->nstfout > 0 && !do_per_step(step, inputrec->nstfout));
1549 write_em_traj(fplog, cr, outf, do_x, do_f, ftp2fn(efSTO, nfile, fnm),
1550 top_global, inputrec, step,
1551 s_min, state_global, f_global);
1553 fnormn = s_min->fnorm/sqrt(state_global->natoms);
1557 print_converged(stderr, CG, inputrec->em_tol, step, converged, number_steps,
1558 s_min->epot, s_min->fmax, s_min->a_fmax, fnormn);
1559 print_converged(fplog, CG, inputrec->em_tol, step, converged, number_steps,
1560 s_min->epot, s_min->fmax, s_min->a_fmax, fnormn);
1562 fprintf(fplog, "\nPerformed %d energy evaluations in total.\n", neval);
1565 finish_em(cr, outf, walltime_accounting, wcycle);
1567 /* To print the actual number of steps we needed somewhere */
1568 walltime_accounting_set_nsteps_done(walltime_accounting, step);
1571 } /* That's all folks */
1574 double do_lbfgs(FILE *fplog, t_commrec *cr,
1575 int nfile, const t_filenm fnm[],
1576 const output_env_t gmx_unused oenv, gmx_bool bVerbose, gmx_bool gmx_unused bCompact,
1577 int gmx_unused nstglobalcomm,
1578 gmx_vsite_t *vsite, gmx_constr_t constr,
1579 int gmx_unused stepout,
1580 t_inputrec *inputrec,
1581 gmx_mtop_t *top_global, t_fcdata *fcd,
1584 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
1585 gmx_edsam_t gmx_unused ed,
1587 int gmx_unused repl_ex_nst, int gmx_unused repl_ex_nex, int gmx_unused repl_ex_seed,
1588 gmx_membed_t gmx_unused membed,
1589 real gmx_unused cpt_period, real gmx_unused max_hours,
1590 const char gmx_unused *deviceOptions,
1592 unsigned long gmx_unused Flags,
1593 gmx_walltime_accounting_t walltime_accounting)
1595 static const char *LBFGS = "Low-Memory BFGS Minimizer";
1597 gmx_localtop_t *top;
1598 gmx_enerdata_t *enerd;
1600 gmx_global_stat_t gstat;
1603 int ncorr, nmaxcorr, point, cp, neval, nminstep;
1604 double stepsize, gpa, gpb, gpc, tmp, minstep;
1605 real *rho, *alpha, *ff, *xx, *p, *s, *lastx, *lastf, **dx, **dg;
1606 real *xa, *xb, *xc, *fa, *fb, *fc, *xtmp, *ftmp;
1607 real a, b, c, maxdelta, delta;
1608 real diag, Epot0, Epot, EpotA, EpotB, EpotC;
1609 real dgdx, dgdg, sq, yr, beta;
1611 gmx_bool converged, first;
1614 gmx_bool do_log, do_ene, do_x, do_f, foundlower, *frozen;
1616 int start, end, number_steps;
1618 int i, k, m, n, nfmax, gf, step;
1625 gmx_fatal(FARGS, "Cannot do parallel L-BFGS Minimization - yet.\n");
1630 gmx_fatal(FARGS, "The combination of constraints and L-BFGS minimization is not implemented. Either do not use constraints, or use another minimizer (e.g. steepest descent).");
1633 n = 3*state->natoms;
1634 nmaxcorr = inputrec->nbfgscorr;
1636 /* Allocate memory */
1637 /* Use pointers to real so we dont have to loop over both atoms and
1638 * dimensions all the time...
1639 * x/f are allocated as rvec *, so make new x0/f0 pointers-to-real
1640 * that point to the same memory.
1653 snew(rho, nmaxcorr);
1654 snew(alpha, nmaxcorr);
1657 for (i = 0; i < nmaxcorr; i++)
1663 for (i = 0; i < nmaxcorr; i++)
1672 init_em(fplog, LBFGS, cr, inputrec,
1673 state, top_global, &ems, &top, &f, &f_global,
1674 nrnb, mu_tot, fr, &enerd, &graph, mdatoms, &gstat, vsite, constr,
1675 nfile, fnm, &outf, &mdebin, imdport, Flags);
1676 /* Do_lbfgs is not completely updated like do_steep and do_cg,
1677 * so we free some memory again.
1682 xx = (real *)state->x;
1686 end = mdatoms->homenr;
1688 /* Print to log file */
1689 print_em_start(fplog, cr, walltime_accounting, wcycle, LBFGS);
1691 do_log = do_ene = do_x = do_f = TRUE;
1693 /* Max number of steps */
1694 number_steps = inputrec->nsteps;
1696 /* Create a 3*natoms index to tell whether each degree of freedom is frozen */
1698 for (i = start; i < end; i++)
1700 if (mdatoms->cFREEZE)
1702 gf = mdatoms->cFREEZE[i];
1704 for (m = 0; m < DIM; m++)
1706 frozen[3*i+m] = inputrec->opts.nFreeze[gf][m];
1711 sp_header(stderr, LBFGS, inputrec->em_tol, number_steps);
1715 sp_header(fplog, LBFGS, inputrec->em_tol, number_steps);
1720 construct_vsites(vsite, state->x, 1, NULL,
1721 top->idef.iparams, top->idef.il,
1722 fr->ePBC, fr->bMolPBC, cr, state->box);
1725 /* Call the force routine and some auxiliary (neighboursearching etc.) */
1726 /* do_force always puts the charge groups in the box and shifts again
1727 * We do not unshift, so molecules are always whole
1732 evaluate_energy(fplog, cr,
1733 top_global, &ems, top,
1734 inputrec, nrnb, wcycle, gstat,
1735 vsite, constr, fcd, graph, mdatoms, fr,
1736 mu_tot, enerd, vir, pres, -1, TRUE);
1741 /* Copy stuff to the energy bin for easy printing etc. */
1742 upd_mdebin(mdebin, FALSE, FALSE, (double)step,
1743 mdatoms->tmass, enerd, state, inputrec->fepvals, inputrec->expandedvals, state->box,
1744 NULL, NULL, vir, pres, NULL, mu_tot, constr);
1746 print_ebin_header(fplog, step, step, state->lambda[efptFEP]);
1747 print_ebin(mdoutf_get_fp_ene(outf), TRUE, FALSE, FALSE, fplog, step, step, eprNORMAL,
1748 TRUE, mdebin, fcd, &(top_global->groups), &(inputrec->opts));
1752 /* This is the starting energy */
1753 Epot = enerd->term[F_EPOT];
1759 /* Set the initial step.
1760 * since it will be multiplied by the non-normalized search direction
1761 * vector (force vector the first time), we scale it by the
1762 * norm of the force.
1767 fprintf(stderr, "Using %d BFGS correction steps.\n\n", nmaxcorr);
1768 fprintf(stderr, " F-max = %12.5e on atom %d\n", fmax, nfmax+1);
1769 fprintf(stderr, " F-Norm = %12.5e\n", fnorm/sqrt(state->natoms));
1770 fprintf(stderr, "\n");
1771 /* and copy to the log file too... */
1772 fprintf(fplog, "Using %d BFGS correction steps.\n\n", nmaxcorr);
1773 fprintf(fplog, " F-max = %12.5e on atom %d\n", fmax, nfmax+1);
1774 fprintf(fplog, " F-Norm = %12.5e\n", fnorm/sqrt(state->natoms));
1775 fprintf(fplog, "\n");
1779 for (i = 0; i < n; i++)
1783 dx[point][i] = ff[i]; /* Initial search direction */
1791 stepsize = 1.0/fnorm;
1794 /* Start the loop over BFGS steps.
1795 * Each successful step is counted, and we continue until
1796 * we either converge or reach the max number of steps.
1801 /* Set the gradient from the force */
1803 for (step = 0; (number_steps < 0 || (number_steps >= 0 && step <= number_steps)) && !converged; step++)
1806 /* Write coordinates if necessary */
1807 do_x = do_per_step(step, inputrec->nstxout);
1808 do_f = do_per_step(step, inputrec->nstfout);
1813 mdof_flags |= MDOF_X;
1818 mdof_flags |= MDOF_F;
1823 mdof_flags |= MDOF_IMD;
1826 mdoutf_write_to_trajectory_files(fplog, cr, outf, mdof_flags,
1827 top_global, step, (real)step, state, state, f, f);
1829 /* Do the linesearching in the direction dx[point][0..(n-1)] */
1831 /* pointer to current direction - point=0 first time here */
1834 /* calculate line gradient */
1835 for (gpa = 0, i = 0; i < n; i++)
1840 /* Calculate minimum allowed stepsize, before the average (norm)
1841 * relative change in coordinate is smaller than precision
1843 for (minstep = 0, i = 0; i < n; i++)
1853 minstep = GMX_REAL_EPS/sqrt(minstep/n);
1855 if (stepsize < minstep)
1861 /* Store old forces and coordinates */
1862 for (i = 0; i < n; i++)
1871 for (i = 0; i < n; i++)
1876 /* Take a step downhill.
1877 * In theory, we should minimize the function along this direction.
1878 * That is quite possible, but it turns out to take 5-10 function evaluations
1879 * for each line. However, we dont really need to find the exact minimum -
1880 * it is much better to start a new BFGS step in a modified direction as soon
1881 * as we are close to it. This will save a lot of energy evaluations.
1883 * In practice, we just try to take a single step.
1884 * If it worked (i.e. lowered the energy), we increase the stepsize but
1885 * the continue straight to the next BFGS step without trying to find any minimum.
1886 * If it didn't work (higher energy), there must be a minimum somewhere between
1887 * the old position and the new one.
1889 * Due to the finite numerical accuracy, it turns out that it is a good idea
1890 * to even accept a SMALL increase in energy, if the derivative is still downhill.
1891 * This leads to lower final energies in the tests I've done. / Erik
1896 c = a + stepsize; /* reference position along line is zero */
1898 /* Check stepsize first. We do not allow displacements
1899 * larger than emstep.
1905 for (i = 0; i < n; i++)
1908 if (delta > maxdelta)
1913 if (maxdelta > inputrec->em_stepsize)
1918 while (maxdelta > inputrec->em_stepsize);
1920 /* Take a trial step */
1921 for (i = 0; i < n; i++)
1923 xc[i] = lastx[i] + c*s[i];
1927 /* Calculate energy for the trial step */
1928 ems.s.x = (rvec *)xc;
1930 evaluate_energy(fplog, cr,
1931 top_global, &ems, top,
1932 inputrec, nrnb, wcycle, gstat,
1933 vsite, constr, fcd, graph, mdatoms, fr,
1934 mu_tot, enerd, vir, pres, step, FALSE);
1937 /* Calc derivative along line */
1938 for (gpc = 0, i = 0; i < n; i++)
1940 gpc -= s[i]*fc[i]; /* f is negative gradient, thus the sign */
1942 /* Sum the gradient along the line across CPUs */
1945 gmx_sumd(1, &gpc, cr);
1948 /* This is the max amount of increase in energy we tolerate */
1949 tmp = sqrt(GMX_REAL_EPS)*fabs(EpotA);
1951 /* Accept the step if the energy is lower, or if it is not significantly higher
1952 * and the line derivative is still negative.
1954 if (EpotC < EpotA || (gpc < 0 && EpotC < (EpotA+tmp)))
1957 /* Great, we found a better energy. Increase step for next iteration
1958 * if we are still going down, decrease it otherwise
1962 stepsize *= 1.618034; /* The golden section */
1966 stepsize *= 0.618034; /* 1/golden section */
1971 /* New energy is the same or higher. We will have to do some work
1972 * to find a smaller value in the interval. Take smaller step next time!
1975 stepsize *= 0.618034;
1978 /* OK, if we didn't find a lower value we will have to locate one now - there must
1979 * be one in the interval [a=0,c].
1980 * The same thing is valid here, though: Don't spend dozens of iterations to find
1981 * the line minimum. We try to interpolate based on the derivative at the endpoints,
1982 * and only continue until we find a lower value. In most cases this means 1-2 iterations.
1984 * I also have a safeguard for potentially really patological functions so we never
1985 * take more than 20 steps before we give up ...
1987 * If we already found a lower value we just skip this step and continue to the update.
1996 /* Select a new trial point.
1997 * If the derivatives at points a & c have different sign we interpolate to zero,
1998 * otherwise just do a bisection.
2001 if (gpa < 0 && gpc > 0)
2003 b = a + gpa*(a-c)/(gpc-gpa);
2010 /* safeguard if interpolation close to machine accuracy causes errors:
2011 * never go outside the interval
2013 if (b <= a || b >= c)
2018 /* Take a trial step */
2019 for (i = 0; i < n; i++)
2021 xb[i] = lastx[i] + b*s[i];
2025 /* Calculate energy for the trial step */
2026 ems.s.x = (rvec *)xb;
2028 evaluate_energy(fplog, cr,
2029 top_global, &ems, top,
2030 inputrec, nrnb, wcycle, gstat,
2031 vsite, constr, fcd, graph, mdatoms, fr,
2032 mu_tot, enerd, vir, pres, step, FALSE);
2037 for (gpb = 0, i = 0; i < n; i++)
2039 gpb -= s[i]*fb[i]; /* f is negative gradient, thus the sign */
2042 /* Sum the gradient along the line across CPUs */
2045 gmx_sumd(1, &gpb, cr);
2048 /* Keep one of the intervals based on the value of the derivative at the new point */
2051 /* Replace c endpoint with b */
2055 /* swap coord pointers b/c */
2065 /* Replace a endpoint with b */
2069 /* swap coord pointers a/b */
2079 * Stop search as soon as we find a value smaller than the endpoints,
2080 * or if the tolerance is below machine precision.
2081 * Never run more than 20 steps, no matter what.
2085 while ((EpotB > EpotA || EpotB > EpotC) && (nminstep < 20));
2087 if (fabs(EpotB-Epot0) < GMX_REAL_EPS || nminstep >= 20)
2089 /* OK. We couldn't find a significantly lower energy.
2090 * If ncorr==0 this was steepest descent, and then we give up.
2091 * If not, reset memory to restart as steepest descent before quitting.
2103 /* Search in gradient direction */
2104 for (i = 0; i < n; i++)
2106 dx[point][i] = ff[i];
2108 /* Reset stepsize */
2109 stepsize = 1.0/fnorm;
2114 /* Select min energy state of A & C, put the best in xx/ff/Epot
2120 for (i = 0; i < n; i++)
2131 for (i = 0; i < n; i++)
2145 for (i = 0; i < n; i++)
2153 /* Update the memory information, and calculate a new
2154 * approximation of the inverse hessian
2157 /* Have new data in Epot, xx, ff */
2158 if (ncorr < nmaxcorr)
2163 for (i = 0; i < n; i++)
2165 dg[point][i] = lastf[i]-ff[i];
2166 dx[point][i] *= stepsize;
2171 for (i = 0; i < n; i++)
2173 dgdg += dg[point][i]*dg[point][i];
2174 dgdx += dg[point][i]*dx[point][i];
2179 rho[point] = 1.0/dgdx;
2182 if (point >= nmaxcorr)
2188 for (i = 0; i < n; i++)
2195 /* Recursive update. First go back over the memory points */
2196 for (k = 0; k < ncorr; k++)
2205 for (i = 0; i < n; i++)
2207 sq += dx[cp][i]*p[i];
2210 alpha[cp] = rho[cp]*sq;
2212 for (i = 0; i < n; i++)
2214 p[i] -= alpha[cp]*dg[cp][i];
2218 for (i = 0; i < n; i++)
2223 /* And then go forward again */
2224 for (k = 0; k < ncorr; k++)
2227 for (i = 0; i < n; i++)
2229 yr += p[i]*dg[cp][i];
2233 beta = alpha[cp]-beta;
2235 for (i = 0; i < n; i++)
2237 p[i] += beta*dx[cp][i];
2247 for (i = 0; i < n; i++)
2251 dx[point][i] = p[i];
2261 /* Test whether the convergence criterion is met */
2262 get_f_norm_max(cr, &(inputrec->opts), mdatoms, f, &fnorm, &fmax, &nfmax);
2264 /* Print it if necessary */
2269 fprintf(stderr, "\rStep %d, Epot=%12.6e, Fnorm=%9.3e, Fmax=%9.3e (atom %d)\n",
2270 step, Epot, fnorm/sqrt(state->natoms), fmax, nfmax+1);
2272 /* Store the new (lower) energies */
2273 upd_mdebin(mdebin, FALSE, FALSE, (double)step,
2274 mdatoms->tmass, enerd, state, inputrec->fepvals, inputrec->expandedvals, state->box,
2275 NULL, NULL, vir, pres, NULL, mu_tot, constr);
2276 do_log = do_per_step(step, inputrec->nstlog);
2277 do_ene = do_per_step(step, inputrec->nstenergy);
2280 print_ebin_header(fplog, step, step, state->lambda[efptFEP]);
2282 print_ebin(mdoutf_get_fp_ene(outf), do_ene, FALSE, FALSE,
2283 do_log ? fplog : NULL, step, step, eprNORMAL,
2284 TRUE, mdebin, fcd, &(top_global->groups), &(inputrec->opts));
2287 /* Send x and E to IMD client, if bIMD is TRUE. */
2288 if (do_IMD(inputrec->bIMD, step, cr, TRUE, state->box, state->x, inputrec, 0, wcycle) && MASTER(cr))
2290 IMD_send_positions(inputrec->imd);
2293 /* Stop when the maximum force lies below tolerance.
2294 * If we have reached machine precision, converged is already set to true.
2297 converged = converged || (fmax < inputrec->em_tol);
2299 } /* End of the loop */
2301 /* IMD cleanup, if bIMD is TRUE. */
2302 IMD_finalize(inputrec->bIMD, inputrec->imd);
2306 step--; /* we never took that last step in this case */
2309 if (fmax > inputrec->em_tol)
2313 warn_step(stderr, inputrec->em_tol, step-1 == number_steps, FALSE);
2314 warn_step(fplog, inputrec->em_tol, step-1 == number_steps, FALSE);
2319 /* If we printed energy and/or logfile last step (which was the last step)
2320 * we don't have to do it again, but otherwise print the final values.
2322 if (!do_log) /* Write final value to log since we didn't do anythin last step */
2324 print_ebin_header(fplog, step, step, state->lambda[efptFEP]);
2326 if (!do_ene || !do_log) /* Write final energy file entries */
2328 print_ebin(mdoutf_get_fp_ene(outf), !do_ene, FALSE, FALSE,
2329 !do_log ? fplog : NULL, step, step, eprNORMAL,
2330 TRUE, mdebin, fcd, &(top_global->groups), &(inputrec->opts));
2333 /* Print some stuff... */
2336 fprintf(stderr, "\nwriting lowest energy coordinates.\n");
2340 * For accurate normal mode calculation it is imperative that we
2341 * store the last conformation into the full precision binary trajectory.
2343 * However, we should only do it if we did NOT already write this step
2344 * above (which we did if do_x or do_f was true).
2346 do_x = !do_per_step(step, inputrec->nstxout);
2347 do_f = !do_per_step(step, inputrec->nstfout);
2348 write_em_traj(fplog, cr, outf, do_x, do_f, ftp2fn(efSTO, nfile, fnm),
2349 top_global, inputrec, step,
2354 print_converged(stderr, LBFGS, inputrec->em_tol, step, converged,
2355 number_steps, Epot, fmax, nfmax, fnorm/sqrt(state->natoms));
2356 print_converged(fplog, LBFGS, inputrec->em_tol, step, converged,
2357 number_steps, Epot, fmax, nfmax, fnorm/sqrt(state->natoms));
2359 fprintf(fplog, "\nPerformed %d energy evaluations in total.\n", neval);
2362 finish_em(cr, outf, walltime_accounting, wcycle);
2364 /* To print the actual number of steps we needed somewhere */
2365 walltime_accounting_set_nsteps_done(walltime_accounting, step);
2368 } /* That's all folks */
2371 double do_steep(FILE *fplog, t_commrec *cr,
2372 int nfile, const t_filenm fnm[],
2373 const output_env_t gmx_unused oenv, gmx_bool bVerbose, gmx_bool gmx_unused bCompact,
2374 int gmx_unused nstglobalcomm,
2375 gmx_vsite_t *vsite, gmx_constr_t constr,
2376 int gmx_unused stepout,
2377 t_inputrec *inputrec,
2378 gmx_mtop_t *top_global, t_fcdata *fcd,
2379 t_state *state_global,
2381 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
2382 gmx_edsam_t gmx_unused ed,
2384 int gmx_unused repl_ex_nst, int gmx_unused repl_ex_nex, int gmx_unused repl_ex_seed,
2385 gmx_membed_t gmx_unused membed,
2386 real gmx_unused cpt_period, real gmx_unused max_hours,
2387 const char gmx_unused *deviceOptions,
2389 unsigned long gmx_unused Flags,
2390 gmx_walltime_accounting_t walltime_accounting)
2392 const char *SD = "Steepest Descents";
2393 em_state_t *s_min, *s_try;
2395 gmx_localtop_t *top;
2396 gmx_enerdata_t *enerd;
2398 gmx_global_stat_t gstat;
2400 real stepsize, constepsize;
2404 gmx_bool bDone, bAbort, do_x, do_f;
2409 int steps_accepted = 0;
2413 s_min = init_em_state();
2414 s_try = init_em_state();
2416 /* Init em and store the local state in s_try */
2417 init_em(fplog, SD, cr, inputrec,
2418 state_global, top_global, s_try, &top, &f, &f_global,
2419 nrnb, mu_tot, fr, &enerd, &graph, mdatoms, &gstat, vsite, constr,
2420 nfile, fnm, &outf, &mdebin, imdport, Flags);
2422 /* Print to log file */
2423 print_em_start(fplog, cr, walltime_accounting, wcycle, SD);
2425 /* Set variables for stepsize (in nm). This is the largest
2426 * step that we are going to make in any direction.
2428 ustep = inputrec->em_stepsize;
2431 /* Max number of steps */
2432 nsteps = inputrec->nsteps;
2436 /* Print to the screen */
2437 sp_header(stderr, SD, inputrec->em_tol, nsteps);
2441 sp_header(fplog, SD, inputrec->em_tol, nsteps);
2444 /**** HERE STARTS THE LOOP ****
2445 * count is the counter for the number of steps
2446 * bDone will be TRUE when the minimization has converged
2447 * bAbort will be TRUE when nsteps steps have been performed or when
2448 * the stepsize becomes smaller than is reasonable for machine precision
2453 while (!bDone && !bAbort)
2455 bAbort = (nsteps >= 0) && (count == nsteps);
2457 /* set new coordinates, except for first step */
2460 do_em_step(cr, inputrec, mdatoms, fr->bMolPBC,
2461 s_min, stepsize, s_min->f, s_try,
2462 constr, top, nrnb, wcycle, count);
2465 evaluate_energy(fplog, cr,
2466 top_global, s_try, top,
2467 inputrec, nrnb, wcycle, gstat,
2468 vsite, constr, fcd, graph, mdatoms, fr,
2469 mu_tot, enerd, vir, pres, count, count == 0);
2473 print_ebin_header(fplog, count, count, s_try->s.lambda[efptFEP]);
2478 s_min->epot = s_try->epot + 1;
2481 /* Print it if necessary */
2486 fprintf(stderr, "Step=%5d, Dmax= %6.1e nm, Epot= %12.5e Fmax= %11.5e, atom= %d%c",
2487 count, ustep, s_try->epot, s_try->fmax, s_try->a_fmax+1,
2488 (s_try->epot < s_min->epot) ? '\n' : '\r');
2491 if (s_try->epot < s_min->epot)
2493 /* Store the new (lower) energies */
2494 upd_mdebin(mdebin, FALSE, FALSE, (double)count,
2495 mdatoms->tmass, enerd, &s_try->s, inputrec->fepvals, inputrec->expandedvals,
2496 s_try->s.box, NULL, NULL, vir, pres, NULL, mu_tot, constr);
2498 /* Prepare IMD energy record, if bIMD is TRUE. */
2499 IMD_fill_energy_record(inputrec->bIMD, inputrec->imd, enerd, count, TRUE);
2501 print_ebin(mdoutf_get_fp_ene(outf), TRUE,
2502 do_per_step(steps_accepted, inputrec->nstdisreout),
2503 do_per_step(steps_accepted, inputrec->nstorireout),
2504 fplog, count, count, eprNORMAL, TRUE,
2505 mdebin, fcd, &(top_global->groups), &(inputrec->opts));
2510 /* Now if the new energy is smaller than the previous...
2511 * or if this is the first step!
2512 * or if we did random steps!
2515 if ( (count == 0) || (s_try->epot < s_min->epot) )
2519 /* Test whether the convergence criterion is met... */
2520 bDone = (s_try->fmax < inputrec->em_tol);
2522 /* Copy the arrays for force, positions and energy */
2523 /* The 'Min' array always holds the coords and forces of the minimal
2525 swap_em_state(s_min, s_try);
2531 /* Write to trn, if necessary */
2532 do_x = do_per_step(steps_accepted, inputrec->nstxout);
2533 do_f = do_per_step(steps_accepted, inputrec->nstfout);
2534 write_em_traj(fplog, cr, outf, do_x, do_f, NULL,
2535 top_global, inputrec, count,
2536 s_min, state_global, f_global);
2540 /* If energy is not smaller make the step smaller... */
2543 if (DOMAINDECOMP(cr) && s_min->s.ddp_count != cr->dd->ddp_count)
2545 /* Reload the old state */
2546 em_dd_partition_system(fplog, count, cr, top_global, inputrec,
2547 s_min, top, mdatoms, fr, vsite, constr,
2552 /* Determine new step */
2553 stepsize = ustep/s_min->fmax;
2555 /* Check if stepsize is too small, with 1 nm as a characteristic length */
2557 if (count == nsteps || ustep < 1e-12)
2559 if (count == nsteps || ustep < 1e-6)
2564 warn_step(stderr, inputrec->em_tol, count == nsteps, constr != NULL);
2565 warn_step(fplog, inputrec->em_tol, count == nsteps, constr != NULL);
2570 /* Send IMD energies and positions, if bIMD is TRUE. */
2571 if (do_IMD(inputrec->bIMD, count, cr, TRUE, state_global->box, state_global->x, inputrec, 0, wcycle) && MASTER(cr))
2573 IMD_send_positions(inputrec->imd);
2577 } /* End of the loop */
2579 /* IMD cleanup, if bIMD is TRUE. */
2580 IMD_finalize(inputrec->bIMD, inputrec->imd);
2582 /* Print some shit... */
2585 fprintf(stderr, "\nwriting lowest energy coordinates.\n");
2587 write_em_traj(fplog, cr, outf, TRUE, inputrec->nstfout, ftp2fn(efSTO, nfile, fnm),
2588 top_global, inputrec, count,
2589 s_min, state_global, f_global);
2591 fnormn = s_min->fnorm/sqrt(state_global->natoms);
2595 print_converged(stderr, SD, inputrec->em_tol, count, bDone, nsteps,
2596 s_min->epot, s_min->fmax, s_min->a_fmax, fnormn);
2597 print_converged(fplog, SD, inputrec->em_tol, count, bDone, nsteps,
2598 s_min->epot, s_min->fmax, s_min->a_fmax, fnormn);
2601 finish_em(cr, outf, walltime_accounting, wcycle);
2603 /* To print the actual number of steps we needed somewhere */
2604 inputrec->nsteps = count;
2606 walltime_accounting_set_nsteps_done(walltime_accounting, count);
2609 } /* That's all folks */
2612 double do_nm(FILE *fplog, t_commrec *cr,
2613 int nfile, const t_filenm fnm[],
2614 const output_env_t gmx_unused oenv, gmx_bool bVerbose, gmx_bool gmx_unused bCompact,
2615 int gmx_unused nstglobalcomm,
2616 gmx_vsite_t *vsite, gmx_constr_t constr,
2617 int gmx_unused stepout,
2618 t_inputrec *inputrec,
2619 gmx_mtop_t *top_global, t_fcdata *fcd,
2620 t_state *state_global,
2622 t_nrnb *nrnb, gmx_wallcycle_t wcycle,
2623 gmx_edsam_t gmx_unused ed,
2625 int gmx_unused repl_ex_nst, int gmx_unused repl_ex_nex, int gmx_unused repl_ex_seed,
2626 gmx_membed_t gmx_unused membed,
2627 real gmx_unused cpt_period, real gmx_unused max_hours,
2628 const char gmx_unused *deviceOptions,
2630 unsigned long gmx_unused Flags,
2631 gmx_walltime_accounting_t walltime_accounting)
2633 const char *NM = "Normal Mode Analysis";
2635 int natoms, atom, d;
2638 gmx_localtop_t *top;
2639 gmx_enerdata_t *enerd;
2641 gmx_global_stat_t gstat;
2643 real t, t0, lambda, lam0;
2648 gmx_bool bSparse; /* use sparse matrix storage format */
2650 gmx_sparsematrix_t * sparse_matrix = NULL;
2651 real * full_matrix = NULL;
2652 em_state_t * state_work;
2654 /* added with respect to mdrun */
2655 int i, j, k, row, col;
2656 real der_range = 10.0*sqrt(GMX_REAL_EPS);
2662 gmx_fatal(FARGS, "Constraints present with Normal Mode Analysis, this combination is not supported");
2665 state_work = init_em_state();
2667 /* Init em and store the local state in state_minimum */
2668 init_em(fplog, NM, cr, inputrec,
2669 state_global, top_global, state_work, &top,
2671 nrnb, mu_tot, fr, &enerd, &graph, mdatoms, &gstat, vsite, constr,
2672 nfile, fnm, &outf, NULL, imdport, Flags);
2674 natoms = top_global->natoms;
2682 "NOTE: This version of Gromacs has been compiled in single precision,\n"
2683 " which MIGHT not be accurate enough for normal mode analysis.\n"
2684 " Gromacs now uses sparse matrix storage, so the memory requirements\n"
2685 " are fairly modest even if you recompile in double precision.\n\n");
2689 /* Check if we can/should use sparse storage format.
2691 * Sparse format is only useful when the Hessian itself is sparse, which it
2692 * will be when we use a cutoff.
2693 * For small systems (n<1000) it is easier to always use full matrix format, though.
2695 if (EEL_FULL(fr->eeltype) || fr->rlist == 0.0)
2697 md_print_info(cr, fplog, "Non-cutoff electrostatics used, forcing full Hessian format.\n");
2700 else if (top_global->natoms < 1000)
2702 md_print_info(cr, fplog, "Small system size (N=%d), using full Hessian format.\n", top_global->natoms);
2707 md_print_info(cr, fplog, "Using compressed symmetric sparse Hessian format.\n");
2713 sz = DIM*top_global->natoms;
2715 fprintf(stderr, "Allocating Hessian memory...\n\n");
2719 sparse_matrix = gmx_sparsematrix_init(sz);
2720 sparse_matrix->compressed_symmetric = TRUE;
2724 snew(full_matrix, sz*sz);
2728 /* Initial values */
2729 t0 = inputrec->init_t;
2730 lam0 = inputrec->fepvals->init_lambda;
2738 /* Write start time and temperature */
2739 print_em_start(fplog, cr, walltime_accounting, wcycle, NM);
2741 /* fudge nr of steps to nr of atoms */
2742 inputrec->nsteps = natoms*2;
2746 fprintf(stderr, "starting normal mode calculation '%s'\n%d steps.\n\n",
2747 *(top_global->name), (int)inputrec->nsteps);
2750 nnodes = cr->nnodes;
2752 /* Make evaluate_energy do a single node force calculation */
2754 evaluate_energy(fplog, cr,
2755 top_global, state_work, top,
2756 inputrec, nrnb, wcycle, gstat,
2757 vsite, constr, fcd, graph, mdatoms, fr,
2758 mu_tot, enerd, vir, pres, -1, TRUE);
2759 cr->nnodes = nnodes;
2761 /* if forces are not small, warn user */
2762 get_state_f_norm_max(cr, &(inputrec->opts), mdatoms, state_work);
2764 md_print_info(cr, fplog, "Maximum force:%12.5e\n", state_work->fmax);
2765 if (state_work->fmax > 1.0e-3)
2767 md_print_info(cr, fplog,
2768 "The force is probably not small enough to "
2769 "ensure that you are at a minimum.\n"
2770 "Be aware that negative eigenvalues may occur\n"
2771 "when the resulting matrix is diagonalized.\n\n");
2774 /***********************************************************
2776 * Loop over all pairs in matrix
2778 * do_force called twice. Once with positive and
2779 * once with negative displacement
2781 ************************************************************/
2783 /* Steps are divided one by one over the nodes */
2784 for (atom = cr->nodeid; atom < natoms; atom += nnodes)
2787 for (d = 0; d < DIM; d++)
2789 x_min = state_work->s.x[atom][d];
2791 state_work->s.x[atom][d] = x_min - der_range;
2793 /* Make evaluate_energy do a single node force calculation */
2795 evaluate_energy(fplog, cr,
2796 top_global, state_work, top,
2797 inputrec, nrnb, wcycle, gstat,
2798 vsite, constr, fcd, graph, mdatoms, fr,
2799 mu_tot, enerd, vir, pres, atom*2, FALSE);
2801 for (i = 0; i < natoms; i++)
2803 copy_rvec(state_work->f[i], fneg[i]);
2806 state_work->s.x[atom][d] = x_min + der_range;
2808 evaluate_energy(fplog, cr,
2809 top_global, state_work, top,
2810 inputrec, nrnb, wcycle, gstat,
2811 vsite, constr, fcd, graph, mdatoms, fr,
2812 mu_tot, enerd, vir, pres, atom*2+1, FALSE);
2813 cr->nnodes = nnodes;
2815 /* x is restored to original */
2816 state_work->s.x[atom][d] = x_min;
2818 for (j = 0; j < natoms; j++)
2820 for (k = 0; (k < DIM); k++)
2823 -(state_work->f[j][k] - fneg[j][k])/(2*der_range);
2831 #define mpi_type MPI_DOUBLE
2833 #define mpi_type MPI_FLOAT
2835 MPI_Send(dfdx[0], natoms*DIM, mpi_type, MASTERNODE(cr), cr->nodeid,
2836 cr->mpi_comm_mygroup);
2841 for (node = 0; (node < nnodes && atom+node < natoms); node++)
2847 MPI_Recv(dfdx[0], natoms*DIM, mpi_type, node, node,
2848 cr->mpi_comm_mygroup, &stat);
2853 row = (atom + node)*DIM + d;
2855 for (j = 0; j < natoms; j++)
2857 for (k = 0; k < DIM; k++)
2863 if (col >= row && dfdx[j][k] != 0.0)
2865 gmx_sparsematrix_increment_value(sparse_matrix,
2866 row, col, dfdx[j][k]);
2871 full_matrix[row*sz+col] = dfdx[j][k];
2878 if (bVerbose && fplog)
2883 /* write progress */
2884 if (MASTER(cr) && bVerbose)
2886 fprintf(stderr, "\rFinished step %d out of %d",
2887 min(atom+nnodes, natoms), natoms);
2894 fprintf(stderr, "\n\nWriting Hessian...\n");
2895 gmx_mtxio_write(ftp2fn(efMTX, nfile, fnm), sz, sz, full_matrix, sparse_matrix);
2898 finish_em(cr, outf, walltime_accounting, wcycle);
2900 walltime_accounting_set_nsteps_done(walltime_accounting, natoms*2);