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,2015,2016,2017 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
39 * \brief Defines LINCS code.
41 * \author Berk Hess <hess@kth.se>
42 * \author Mark Abraham <mark.j.abraham@gmail.com>
43 * \ingroup module_mdlib
58 #include "gromacs/domdec/domdec.h"
59 #include "gromacs/domdec/domdec_struct.h"
60 #include "gromacs/gmxlib/nrnb.h"
61 #include "gromacs/math/functions.h"
62 #include "gromacs/math/paddedvector.h"
63 #include "gromacs/math/units.h"
64 #include "gromacs/math/vec.h"
65 #include "gromacs/mdlib/constr.h"
66 #include "gromacs/mdlib/gmx_omp_nthreads.h"
67 #include "gromacs/mdrunutility/multisim.h"
68 #include "gromacs/mdtypes/commrec.h"
69 #include "gromacs/mdtypes/inputrec.h"
70 #include "gromacs/mdtypes/md_enums.h"
71 #include "gromacs/mdtypes/mdatom.h"
72 #include "gromacs/pbcutil/pbc.h"
73 #include "gromacs/pbcutil/pbc_simd.h"
74 #include "gromacs/simd/simd.h"
75 #include "gromacs/simd/simd_math.h"
76 #include "gromacs/simd/vector_operations.h"
77 #include "gromacs/topology/mtop_util.h"
78 #include "gromacs/utility/alignedallocator.h"
79 #include "gromacs/utility/arrayref.h"
80 #include "gromacs/utility/basedefinitions.h"
81 #include "gromacs/utility/bitmask.h"
82 #include "gromacs/utility/cstringutil.h"
83 #include "gromacs/utility/exceptions.h"
84 #include "gromacs/utility/fatalerror.h"
85 #include "gromacs/utility/gmxomp.h"
86 #include "gromacs/utility/listoflists.h"
87 #include "gromacs/utility/pleasecite.h"
89 using namespace gmx; // TODO: Remove when this file is moved into gmx namespace
94 //! Indices of the two atoms involved in a single constraint
97 //! \brief Constructor, does not initialize to catch bugs and faster construction
106 //! Unit of work within LINCS.
109 //! First constraint for this task.
111 //! b1-1 is the last constraint for this task.
113 //! The number of constraints in triangles.
115 //! The list of triangle constraints.
116 std::vector<int> triangle;
117 //! The bits tell if the matrix element should be used.
118 std::vector<int> tri_bits;
119 //! Constraint index for updating atom data.
120 std::vector<int> ind;
121 //! Constraint index for updating atom data.
122 std::vector<int> ind_r;
123 //! Temporary variable for virial calculation.
124 tensor vir_r_m_dr = { { 0 } };
125 //! Temporary variable for lambda derivative.
129 /*! \brief Data for LINCS algorithm.
134 //! The global number of constraints.
136 //! The global number of flexible constraints.
138 //! The global number of constraints in triangles.
139 int ncg_triangle = 0;
140 //! The number of iterations.
142 //! The order of the matrix expansion.
144 //! The maximum number of constraints connected to a single atom.
147 //! The number of real constraints.
149 //! The number of constraints including padding for SIMD.
151 //! The number of constraint connections.
153 //! The FE lambda value used for filling blc and blmf.
155 //! mapping from topology to LINCS constraints.
156 std::vector<int> con_index;
157 //! The reference distance in topology A.
158 std::vector<real, AlignedAllocator<real>> bllen0;
159 //! The reference distance in top B - the r.d. in top A.
160 std::vector<real, AlignedAllocator<real>> ddist;
161 //! The atom pairs involved in the constraints.
162 std::vector<AtomPair> atoms;
163 //! 1/sqrt(invmass1 invmass2).
164 std::vector<real, AlignedAllocator<real>> blc;
165 //! As blc, but with all masses 1.
166 std::vector<real, AlignedAllocator<real>> blc1;
167 //! Index into blbnb and blmf.
168 std::vector<int> blnr;
169 //! List of constraint connections.
170 std::vector<int> blbnb;
171 //! The local number of constraints in triangles.
173 //! The number of constraint connections in triangles.
174 int ncc_triangle = 0;
175 //! Communicate before each LINCS interation.
176 bool bCommIter = false;
177 //! Matrix of mass factors for constraint connections.
178 std::vector<real> blmf;
179 //! As blmf, but with all masses 1.
180 std::vector<real> blmf1;
181 //! The reference bond length.
182 std::vector<real, AlignedAllocator<real>> bllen;
183 //! The local atom count per constraint, can be NULL.
184 std::vector<int> nlocat;
186 /*! \brief The number of tasks used for LINCS work.
188 * \todo This is mostly used to loop over \c task, which would
189 * be nicer to do with range-based for loops, but the thread
190 * index is used for constructing bit masks and organizing the
191 * virial output buffer, so other things need to change,
194 /*! \brief LINCS thread division */
195 std::vector<Task> task;
196 //! Atom flags for thread parallelization.
197 std::vector<gmx_bitmask_t> atf;
198 //! Are the LINCS tasks interdependent?
199 bool bTaskDep = false;
200 //! Are there triangle constraints that cross task borders?
201 bool bTaskDepTri = false;
202 //! Arrays for temporary storage in the LINCS algorithm.
204 PaddedVector<gmx::RVec> tmpv;
205 std::vector<real> tmpncc;
206 std::vector<real, AlignedAllocator<real>> tmp1;
207 std::vector<real, AlignedAllocator<real>> tmp2;
208 std::vector<real, AlignedAllocator<real>> tmp3;
209 std::vector<real, AlignedAllocator<real>> tmp4;
211 //! The Lagrange multipliers times -1.
212 std::vector<real, AlignedAllocator<real>> mlambda;
213 //! Storage for the constraint RMS relative deviation output.
214 std::array<real, 2> rmsdData = { { 0 } };
217 /*! \brief Define simd_width for memory allocation used for SIMD code */
218 #if GMX_SIMD_HAVE_REAL
219 static const int simd_width = GMX_SIMD_REAL_WIDTH;
221 static const int simd_width = 1;
224 ArrayRef<real> lincs_rmsdData(Lincs* lincsd)
226 return lincsd->rmsdData;
229 real lincs_rmsd(const Lincs* lincsd)
231 if (lincsd->rmsdData[0] > 0)
233 return std::sqrt(lincsd->rmsdData[1] / lincsd->rmsdData[0]);
241 /*! \brief Do a set of nrec LINCS matrix multiplications.
243 * This function will return with up to date thread-local
244 * constraint data, without an OpenMP barrier.
246 static void lincs_matrix_expand(const Lincs& lincsd,
248 gmx::ArrayRef<const real> blcc,
249 gmx::ArrayRef<real> rhs1,
250 gmx::ArrayRef<real> rhs2,
251 gmx::ArrayRef<real> sol)
253 gmx::ArrayRef<const int> blnr = lincsd.blnr;
254 gmx::ArrayRef<const int> blbnb = lincsd.blbnb;
256 const int b0 = li_task.b0;
257 const int b1 = li_task.b1;
258 const int nrec = lincsd.nOrder;
260 for (int rec = 0; rec < nrec; rec++)
266 for (int b = b0; b < b1; b++)
272 for (n = blnr[b]; n < blnr[b + 1]; n++)
274 mvb = mvb + blcc[n] * rhs1[blbnb[n]];
277 sol[b] = sol[b] + mvb;
280 std::swap(rhs1, rhs2);
281 } /* nrec*(ncons+2*nrtot) flops */
283 if (lincsd.ntriangle > 0)
285 /* Perform an extra nrec recursions for only the constraints
286 * involved in rigid triangles.
287 * In this way their accuracy should come close to those of the other
288 * constraints, since traingles of constraints can produce eigenvalues
289 * around 0.7, while the effective eigenvalue for bond constraints
290 * is around 0.4 (and 0.7*0.7=0.5).
295 /* We need a barrier here, since other threads might still be
296 * reading the contents of rhs1 and/o rhs2.
297 * We could avoid this barrier by introducing two extra rhs
298 * arrays for the triangle constraints only.
303 /* Constraints involved in a triangle are ensured to be in the same
304 * LINCS task. This means no barriers are required during the extra
305 * iterations for the triangle constraints.
307 gmx::ArrayRef<const int> triangle = li_task.triangle;
308 gmx::ArrayRef<const int> tri_bits = li_task.tri_bits;
310 for (int rec = 0; rec < nrec; rec++)
312 for (int tb = 0; tb < li_task.ntriangle; tb++)
314 int b, bits, nr0, nr1, n;
322 for (n = nr0; n < nr1; n++)
324 if (bits & (1 << (n - nr0)))
326 mvb = mvb + blcc[n] * rhs1[blbnb[n]];
330 sol[b] = sol[b] + mvb;
333 std::swap(rhs1, rhs2);
334 } /* nrec*(ntriangle + ncc_triangle*2) flops */
336 if (lincsd.bTaskDepTri)
338 /* The constraints triangles are decoupled from each other,
339 * but constraints in one triangle cross thread task borders.
340 * We could probably avoid this with more advanced setup code.
347 //! Update atomic coordinates when an index is not required.
348 static void lincs_update_atoms_noind(int ncons,
349 gmx::ArrayRef<const AtomPair> atoms,
351 gmx::ArrayRef<const real> fac,
352 gmx::ArrayRef<const gmx::RVec> r,
356 if (invmass != nullptr)
358 for (int b = 0; b < ncons; b++)
360 int i = atoms[b].index1;
361 int j = atoms[b].index2;
362 real mvb = preFactor * fac[b];
363 real im1 = invmass[i];
364 real im2 = invmass[j];
365 real tmp0 = r[b][0] * mvb;
366 real tmp1 = r[b][1] * mvb;
367 real tmp2 = r[b][2] * mvb;
368 x[i][0] -= tmp0 * im1;
369 x[i][1] -= tmp1 * im1;
370 x[i][2] -= tmp2 * im1;
371 x[j][0] += tmp0 * im2;
372 x[j][1] += tmp1 * im2;
373 x[j][2] += tmp2 * im2;
374 } /* 16 ncons flops */
378 for (int b = 0; b < ncons; b++)
380 int i = atoms[b].index1;
381 int j = atoms[b].index2;
382 real mvb = preFactor * fac[b];
383 real tmp0 = r[b][0] * mvb;
384 real tmp1 = r[b][1] * mvb;
385 real tmp2 = r[b][2] * mvb;
396 //! Update atomic coordinates when an index is required.
397 static void lincs_update_atoms_ind(gmx::ArrayRef<const int> ind,
398 gmx::ArrayRef<const AtomPair> atoms,
400 gmx::ArrayRef<const real> fac,
401 gmx::ArrayRef<const gmx::RVec> r,
405 if (invmass != nullptr)
409 int i = atoms[b].index1;
410 int j = atoms[b].index2;
411 real mvb = preFactor * fac[b];
412 real im1 = invmass[i];
413 real im2 = invmass[j];
414 real tmp0 = r[b][0] * mvb;
415 real tmp1 = r[b][1] * mvb;
416 real tmp2 = r[b][2] * mvb;
417 x[i][0] -= tmp0 * im1;
418 x[i][1] -= tmp1 * im1;
419 x[i][2] -= tmp2 * im1;
420 x[j][0] += tmp0 * im2;
421 x[j][1] += tmp1 * im2;
422 x[j][2] += tmp2 * im2;
423 } /* 16 ncons flops */
429 int i = atoms[b].index1;
430 int j = atoms[b].index2;
431 real mvb = preFactor * fac[b];
432 real tmp0 = r[b][0] * mvb;
433 real tmp1 = r[b][1] * mvb;
434 real tmp2 = r[b][2] * mvb;
441 } /* 16 ncons flops */
445 //! Update coordinates for atoms.
446 static void lincs_update_atoms(Lincs* li,
449 gmx::ArrayRef<const real> fac,
450 gmx::ArrayRef<const gmx::RVec> r,
456 /* Single thread, we simply update for all constraints */
457 lincs_update_atoms_noind(li->nc_real, li->atoms, preFactor, fac, r, invmass, x);
461 /* Update the atom vector components for our thread local
462 * constraints that only access our local atom range.
463 * This can be done without a barrier.
465 lincs_update_atoms_ind(li->task[th].ind, li->atoms, preFactor, fac, r, invmass, x);
467 if (!li->task[li->ntask].ind.empty())
469 /* Update the constraints that operate on atoms
470 * in multiple thread atom blocks on the master thread.
475 lincs_update_atoms_ind(li->task[li->ntask].ind, li->atoms, preFactor, fac, r, invmass, x);
481 #if GMX_SIMD_HAVE_REAL
482 //! Helper function so that we can run TSAN with SIMD support (where implemented).
484 static inline void gmx_simdcall gatherLoadUTransposeTSANSafe(const real* base,
485 const std::int32_t* offset,
490 # if (CMAKE_BUILD_TYPE == CMAKE_BUILD_TYPE_TSAN) && GMX_SIMD_X86_AVX2_256
491 // This function is only implemented in this case
492 gatherLoadUTransposeSafe<align>(base, offset, v0, v1, v2);
494 gatherLoadUTranspose<align>(base, offset, v0, v1, v2);
498 /*! \brief Calculate the constraint distance vectors r to project on from x.
500 * Determine the right-hand side of the matrix equation using quantity f.
501 * This function only differs from calc_dr_x_xp_simd below in that
502 * no constraint length is subtracted and no PBC is used for f. */
503 static void gmx_simdcall calc_dr_x_f_simd(int b0,
505 gmx::ArrayRef<const AtomPair> atoms,
506 const rvec* gmx_restrict x,
507 const rvec* gmx_restrict f,
508 const real* gmx_restrict blc,
509 const real* pbc_simd,
510 rvec* gmx_restrict r,
511 real* gmx_restrict rhs,
512 real* gmx_restrict sol)
514 assert(b0 % GMX_SIMD_REAL_WIDTH == 0);
516 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset2[GMX_SIMD_REAL_WIDTH];
518 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
523 for (int bs = b0; bs < b1; bs += GMX_SIMD_REAL_WIDTH)
525 SimdReal x0_S, y0_S, z0_S;
526 SimdReal x1_S, y1_S, z1_S;
527 SimdReal rx_S, ry_S, rz_S, n2_S, il_S;
528 SimdReal fx_S, fy_S, fz_S, ip_S, rhs_S;
529 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset0[GMX_SIMD_REAL_WIDTH];
530 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset1[GMX_SIMD_REAL_WIDTH];
532 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
534 offset0[i] = atoms[bs + i].index1;
535 offset1[i] = atoms[bs + i].index2;
538 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(x), offset0, &x0_S, &y0_S, &z0_S);
539 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(x), offset1, &x1_S, &y1_S, &z1_S);
544 pbc_correct_dx_simd(&rx_S, &ry_S, &rz_S, pbc_simd);
546 n2_S = norm2(rx_S, ry_S, rz_S);
547 il_S = invsqrt(n2_S);
553 transposeScatterStoreU<3>(reinterpret_cast<real*>(r + bs), offset2, rx_S, ry_S, rz_S);
555 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(f), offset0, &x0_S, &y0_S, &z0_S);
556 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(f), offset1, &x1_S, &y1_S, &z1_S);
561 ip_S = iprod(rx_S, ry_S, rz_S, fx_S, fy_S, fz_S);
563 rhs_S = load<SimdReal>(blc + bs) * ip_S;
565 store(rhs + bs, rhs_S);
566 store(sol + bs, rhs_S);
569 #endif // GMX_SIMD_HAVE_REAL
571 /*! \brief LINCS projection, works on derivatives of the coordinates. */
572 static void do_lincsp(ArrayRefWithPadding<const RVec> xPadded,
573 ArrayRefWithPadding<RVec> fPadded,
579 ConstraintVariable econq,
584 const int b0 = lincsd->task[th].b0;
585 const int b1 = lincsd->task[th].b1;
587 gmx::ArrayRef<const AtomPair> atoms = lincsd->atoms;
588 gmx::ArrayRef<gmx::RVec> r = lincsd->tmpv;
589 gmx::ArrayRef<const int> blnr = lincsd->blnr;
590 gmx::ArrayRef<const int> blbnb = lincsd->blbnb;
592 gmx::ArrayRef<const real> blc;
593 gmx::ArrayRef<const real> blmf;
594 if (econq != ConstraintVariable::Force)
596 /* Use mass-weighted parameters */
602 /* Use non mass-weighted parameters */
604 blmf = lincsd->blmf1;
606 gmx::ArrayRef<real> blcc = lincsd->tmpncc;
607 gmx::ArrayRef<real> rhs1 = lincsd->tmp1;
608 gmx::ArrayRef<real> rhs2 = lincsd->tmp2;
609 gmx::ArrayRef<real> sol = lincsd->tmp3;
611 const rvec* x = as_rvec_array(xPadded.paddedArrayRef().data());
612 rvec* f = as_rvec_array(fPadded.paddedArrayRef().data());
614 #if GMX_SIMD_HAVE_REAL
615 /* This SIMD code does the same as the plain-C code after the #else.
616 * The only difference is that we always call pbc code, as with SIMD
617 * the overhead of pbc computation (when not needed) is small.
619 alignas(GMX_SIMD_ALIGNMENT) real pbc_simd[9 * GMX_SIMD_REAL_WIDTH];
621 /* Convert the pbc struct for SIMD */
622 set_pbc_simd(pbc, pbc_simd);
624 /* Compute normalized x i-j vectors, store in r.
625 * Compute the inner product of r and xp i-j and store in rhs1.
627 calc_dr_x_f_simd(b0, b1, atoms, x, f, blc.data(), pbc_simd, as_rvec_array(r.data()),
628 rhs1.data(), sol.data());
630 #else // GMX_SIMD_HAVE_REAL
632 /* Compute normalized i-j vectors */
635 for (int b = b0; b < b1; b++)
639 pbc_dx_aiuc(pbc, x[atoms[b].index1], x[atoms[b].index2], dx);
645 for (int b = b0; b < b1; b++)
649 rvec_sub(x[atoms[b].index1], x[atoms[b].index2], dx);
651 } /* 16 ncons flops */
654 for (int b = b0; b < b1; b++)
656 int i = atoms[b].index1;
657 int j = atoms[b].index2;
659 * (r[b][0] * (f[i][0] - f[j][0]) + r[b][1] * (f[i][1] - f[j][1])
660 + r[b][2] * (f[i][2] - f[j][2]));
666 #endif // GMX_SIMD_HAVE_REAL
668 if (lincsd->bTaskDep)
670 /* We need a barrier, since the matrix construction below
671 * can access entries in r of other threads.
676 /* Construct the (sparse) LINCS matrix */
677 for (int b = b0; b < b1; b++)
679 for (int n = blnr[b]; n < blnr[b + 1]; n++)
681 blcc[n] = blmf[n] * ::iprod(r[b], r[blbnb[n]]);
684 /* Together: 23*ncons + 6*nrtot flops */
686 lincs_matrix_expand(*lincsd, lincsd->task[th], blcc, rhs1, rhs2, sol);
687 /* nrec*(ncons+2*nrtot) flops */
689 if (econq == ConstraintVariable::Deriv_FlexCon)
691 /* We only want to constraint the flexible constraints,
692 * so we mask out the normal ones by setting sol to 0.
694 for (int b = b0; b < b1; b++)
696 if (!(lincsd->bllen0[b] == 0 && lincsd->ddist[b] == 0))
703 /* We multiply sol by blc, so we can use lincs_update_atoms for OpenMP */
704 for (int b = b0; b < b1; b++)
709 /* When constraining forces, we should not use mass weighting,
710 * so we pass invmass=NULL, which results in the use of 1 for all atoms.
712 lincs_update_atoms(lincsd, th, 1.0, sol, r, (econq != ConstraintVariable::Force) ? invmass : nullptr,
713 as_rvec_array(fp.data()));
718 for (int b = b0; b < b1; b++)
720 dhdlambda -= sol[b] * lincsd->ddist[b];
723 lincsd->task[th].dhdlambda = dhdlambda;
728 /* Constraint virial,
729 * determines sum r_bond x delta f,
730 * where delta f is the constraint correction
731 * of the quantity that is being constrained.
733 for (int b = b0; b < b1; b++)
735 const real mvb = lincsd->bllen[b] * sol[b];
736 for (int i = 0; i < DIM; i++)
738 const real tmp1 = mvb * r[b][i];
739 for (int j = 0; j < DIM; j++)
741 rmdf[i][j] += tmp1 * r[b][j];
744 } /* 23 ncons flops */
748 #if GMX_SIMD_HAVE_REAL
750 /*! \brief Calculate the constraint distance vectors r to project on from x.
752 * Determine the right-hand side of the matrix equation using coordinates xp. */
753 static void gmx_simdcall calc_dr_x_xp_simd(int b0,
755 gmx::ArrayRef<const AtomPair> atoms,
756 const rvec* gmx_restrict x,
757 const rvec* gmx_restrict xp,
758 const real* gmx_restrict bllen,
759 const real* gmx_restrict blc,
760 const real* pbc_simd,
761 rvec* gmx_restrict r,
762 real* gmx_restrict rhs,
763 real* gmx_restrict sol)
765 assert(b0 % GMX_SIMD_REAL_WIDTH == 0);
766 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset2[GMX_SIMD_REAL_WIDTH];
768 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
773 for (int bs = b0; bs < b1; bs += GMX_SIMD_REAL_WIDTH)
775 SimdReal x0_S, y0_S, z0_S;
776 SimdReal x1_S, y1_S, z1_S;
777 SimdReal rx_S, ry_S, rz_S, n2_S, il_S;
778 SimdReal rxp_S, ryp_S, rzp_S, ip_S, rhs_S;
779 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset0[GMX_SIMD_REAL_WIDTH];
780 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset1[GMX_SIMD_REAL_WIDTH];
782 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
784 offset0[i] = atoms[bs + i].index1;
785 offset1[i] = atoms[bs + i].index2;
788 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(x), offset0, &x0_S, &y0_S, &z0_S);
789 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(x), offset1, &x1_S, &y1_S, &z1_S);
794 pbc_correct_dx_simd(&rx_S, &ry_S, &rz_S, pbc_simd);
796 n2_S = norm2(rx_S, ry_S, rz_S);
797 il_S = invsqrt(n2_S);
803 transposeScatterStoreU<3>(reinterpret_cast<real*>(r + bs), offset2, rx_S, ry_S, rz_S);
805 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(xp), offset0, &x0_S, &y0_S, &z0_S);
806 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(xp), offset1, &x1_S, &y1_S, &z1_S);
812 pbc_correct_dx_simd(&rxp_S, &ryp_S, &rzp_S, pbc_simd);
814 ip_S = iprod(rx_S, ry_S, rz_S, rxp_S, ryp_S, rzp_S);
816 rhs_S = load<SimdReal>(blc + bs) * (ip_S - load<SimdReal>(bllen + bs));
818 store(rhs + bs, rhs_S);
819 store(sol + bs, rhs_S);
822 #endif // GMX_SIMD_HAVE_REAL
824 /*! \brief Determine the distances and right-hand side for the next iteration. */
825 gmx_unused static void calc_dist_iter(int b0,
827 gmx::ArrayRef<const AtomPair> atoms,
828 const rvec* gmx_restrict xp,
829 const real* gmx_restrict bllen,
830 const real* gmx_restrict blc,
833 real* gmx_restrict rhs,
834 real* gmx_restrict sol,
837 for (int b = b0; b < b1; b++)
843 pbc_dx_aiuc(pbc, xp[atoms[b].index1], xp[atoms[b].index2], dx);
847 rvec_sub(xp[atoms[b].index1], xp[atoms[b].index2], dx);
849 real len2 = len * len;
850 real dlen2 = 2 * len2 - ::norm2(dx);
851 if (dlen2 < wfac * len2)
853 /* not race free - see detailed comment in caller */
859 mvb = blc[b] * (len - dlen2 * gmx::invsqrt(dlen2));
867 } /* 20*ncons flops */
870 #if GMX_SIMD_HAVE_REAL
871 /*! \brief As calc_dist_iter(), but using SIMD intrinsics. */
872 static void gmx_simdcall calc_dist_iter_simd(int b0,
874 gmx::ArrayRef<const AtomPair> atoms,
875 const rvec* gmx_restrict x,
876 const real* gmx_restrict bllen,
877 const real* gmx_restrict blc,
878 const real* pbc_simd,
880 real* gmx_restrict rhs,
881 real* gmx_restrict sol,
884 SimdReal min_S(GMX_REAL_MIN);
886 SimdReal wfac_S(wfac);
889 assert(b0 % GMX_SIMD_REAL_WIDTH == 0);
891 /* Initialize all to FALSE */
892 warn_S = (two_S < setZero());
894 for (int bs = b0; bs < b1; bs += GMX_SIMD_REAL_WIDTH)
896 SimdReal x0_S, y0_S, z0_S;
897 SimdReal x1_S, y1_S, z1_S;
898 SimdReal rx_S, ry_S, rz_S, n2_S;
899 SimdReal len_S, len2_S, dlen2_S, lc_S, blc_S;
900 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset0[GMX_SIMD_REAL_WIDTH];
901 alignas(GMX_SIMD_ALIGNMENT) std::int32_t offset1[GMX_SIMD_REAL_WIDTH];
903 for (int i = 0; i < GMX_SIMD_REAL_WIDTH; i++)
905 offset0[i] = atoms[bs + i].index1;
906 offset1[i] = atoms[bs + i].index2;
909 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(x), offset0, &x0_S, &y0_S, &z0_S);
910 gatherLoadUTransposeTSANSafe<3>(reinterpret_cast<const real*>(x), offset1, &x1_S, &y1_S, &z1_S);
916 pbc_correct_dx_simd(&rx_S, &ry_S, &rz_S, pbc_simd);
918 n2_S = norm2(rx_S, ry_S, rz_S);
920 len_S = load<SimdReal>(bllen + bs);
921 len2_S = len_S * len_S;
923 dlen2_S = fms(two_S, len2_S, n2_S);
925 warn_S = warn_S || (dlen2_S < (wfac_S * len2_S));
927 /* Avoid 1/0 by taking the max with REAL_MIN.
928 * Note: when dlen2 is close to zero (90 degree constraint rotation),
929 * the accuracy of the algorithm is no longer relevant.
931 dlen2_S = max(dlen2_S, min_S);
933 lc_S = fnma(dlen2_S, invsqrt(dlen2_S), len_S);
935 blc_S = load<SimdReal>(blc + bs);
939 store(rhs + bs, lc_S);
940 store(sol + bs, lc_S);
948 #endif // GMX_SIMD_HAVE_REAL
950 //! Implements LINCS constraining.
951 static void do_lincs(ArrayRefWithPadding<const RVec> xPadded,
952 ArrayRefWithPadding<RVec> xpPadded,
967 const rvec* x = as_rvec_array(xPadded.paddedArrayRef().data());
968 rvec* xp = as_rvec_array(xpPadded.paddedArrayRef().data());
969 rvec* gmx_restrict v = as_rvec_array(vRef.data());
971 const int b0 = lincsd->task[th].b0;
972 const int b1 = lincsd->task[th].b1;
974 gmx::ArrayRef<const AtomPair> atoms = lincsd->atoms;
975 gmx::ArrayRef<gmx::RVec> r = lincsd->tmpv;
976 gmx::ArrayRef<const int> blnr = lincsd->blnr;
977 gmx::ArrayRef<const int> blbnb = lincsd->blbnb;
978 gmx::ArrayRef<const real> blc = lincsd->blc;
979 gmx::ArrayRef<const real> blmf = lincsd->blmf;
980 gmx::ArrayRef<const real> bllen = lincsd->bllen;
981 gmx::ArrayRef<real> blcc = lincsd->tmpncc;
982 gmx::ArrayRef<real> rhs1 = lincsd->tmp1;
983 gmx::ArrayRef<real> rhs2 = lincsd->tmp2;
984 gmx::ArrayRef<real> sol = lincsd->tmp3;
985 gmx::ArrayRef<real> blc_sol = lincsd->tmp4;
986 gmx::ArrayRef<real> mlambda = lincsd->mlambda;
987 gmx::ArrayRef<const int> nlocat = lincsd->nlocat;
989 #if GMX_SIMD_HAVE_REAL
991 /* This SIMD code does the same as the plain-C code after the #else.
992 * The only difference is that we always call pbc code, as with SIMD
993 * the overhead of pbc computation (when not needed) is small.
995 alignas(GMX_SIMD_ALIGNMENT) real pbc_simd[9 * GMX_SIMD_REAL_WIDTH];
997 /* Convert the pbc struct for SIMD */
998 set_pbc_simd(pbc, pbc_simd);
1000 /* Compute normalized x i-j vectors, store in r.
1001 * Compute the inner product of r and xp i-j and store in rhs1.
1003 calc_dr_x_xp_simd(b0, b1, atoms, x, xp, bllen.data(), blc.data(), pbc_simd,
1004 as_rvec_array(r.data()), rhs1.data(), sol.data());
1006 #else // GMX_SIMD_HAVE_REAL
1010 /* Compute normalized i-j vectors */
1011 for (int b = b0; b < b1; b++)
1014 pbc_dx_aiuc(pbc, x[atoms[b].index1], x[atoms[b].index2], dx);
1017 pbc_dx_aiuc(pbc, xp[atoms[b].index1], xp[atoms[b].index2], dx);
1018 real mvb = blc[b] * (::iprod(r[b], dx) - bllen[b]);
1025 /* Compute normalized i-j vectors */
1026 for (int b = b0; b < b1; b++)
1028 int i = atoms[b].index1;
1029 int j = atoms[b].index2;
1030 real tmp0 = x[i][0] - x[j][0];
1031 real tmp1 = x[i][1] - x[j][1];
1032 real tmp2 = x[i][2] - x[j][2];
1033 real rlen = gmx::invsqrt(tmp0 * tmp0 + tmp1 * tmp1 + tmp2 * tmp2);
1034 r[b][0] = rlen * tmp0;
1035 r[b][1] = rlen * tmp1;
1036 r[b][2] = rlen * tmp2;
1037 /* 16 ncons flops */
1040 * (r[b][0] * (xp[i][0] - xp[j][0]) + r[b][1] * (xp[i][1] - xp[j][1])
1041 + r[b][2] * (xp[i][2] - xp[j][2]) - bllen[b]);
1046 /* Together: 26*ncons + 6*nrtot flops */
1049 #endif // GMX_SIMD_HAVE_REAL
1051 if (lincsd->bTaskDep)
1053 /* We need a barrier, since the matrix construction below
1054 * can access entries in r of other threads.
1059 /* Construct the (sparse) LINCS matrix */
1060 for (int b = b0; b < b1; b++)
1062 for (int n = blnr[b]; n < blnr[b + 1]; n++)
1064 blcc[n] = blmf[n] * gmx::dot(r[b], r[blbnb[n]]);
1067 /* Together: 26*ncons + 6*nrtot flops */
1069 lincs_matrix_expand(*lincsd, lincsd->task[th], blcc, rhs1, rhs2, sol);
1070 /* nrec*(ncons+2*nrtot) flops */
1072 #if GMX_SIMD_HAVE_REAL
1073 for (int b = b0; b < b1; b += GMX_SIMD_REAL_WIDTH)
1075 SimdReal t1 = load<SimdReal>(blc.data() + b);
1076 SimdReal t2 = load<SimdReal>(sol.data() + b);
1077 store(mlambda.data() + b, t1 * t2);
1080 for (int b = b0; b < b1; b++)
1082 mlambda[b] = blc[b] * sol[b];
1084 #endif // GMX_SIMD_HAVE_REAL
1086 /* Update the coordinates */
1087 lincs_update_atoms(lincsd, th, 1.0, mlambda, r, invmass, xp);
1090 ******** Correction for centripetal effects ********
1095 wfac = std::cos(DEG2RAD * wangle);
1098 for (int iter = 0; iter < lincsd->nIter; iter++)
1100 if ((lincsd->bCommIter && DOMAINDECOMP(cr) && cr->dd->constraints))
1105 /* Communicate the corrected non-local coordinates */
1106 if (DOMAINDECOMP(cr))
1108 dd_move_x_constraints(cr->dd, box, xpPadded.unpaddedArrayRef(), ArrayRef<RVec>(), FALSE);
1113 else if (lincsd->bTaskDep)
1118 #if GMX_SIMD_HAVE_REAL
1119 calc_dist_iter_simd(b0, b1, atoms, xp, bllen.data(), blc.data(), pbc_simd, wfac,
1120 rhs1.data(), sol.data(), bWarn);
1122 calc_dist_iter(b0, b1, atoms, xp, bllen.data(), blc.data(), pbc, wfac, rhs1.data(),
1124 /* 20*ncons flops */
1125 #endif // GMX_SIMD_HAVE_REAL
1127 lincs_matrix_expand(*lincsd, lincsd->task[th], blcc, rhs1, rhs2, sol);
1128 /* nrec*(ncons+2*nrtot) flops */
1130 #if GMX_SIMD_HAVE_REAL
1131 for (int b = b0; b < b1; b += GMX_SIMD_REAL_WIDTH)
1133 SimdReal t1 = load<SimdReal>(blc.data() + b);
1134 SimdReal t2 = load<SimdReal>(sol.data() + b);
1135 SimdReal mvb = t1 * t2;
1136 store(blc_sol.data() + b, mvb);
1137 store(mlambda.data() + b, load<SimdReal>(mlambda.data() + b) + mvb);
1140 for (int b = b0; b < b1; b++)
1142 real mvb = blc[b] * sol[b];
1146 #endif // GMX_SIMD_HAVE_REAL
1148 /* Update the coordinates */
1149 lincs_update_atoms(lincsd, th, 1.0, blc_sol, r, invmass, xp);
1151 /* nit*ncons*(37+9*nrec) flops */
1155 /* Update the velocities */
1156 lincs_update_atoms(lincsd, th, invdt, mlambda, r, invmass, v);
1157 /* 16 ncons flops */
1160 if (!nlocat.empty() && (bCalcDHDL || bCalcVir))
1162 if (lincsd->bTaskDep)
1164 /* In lincs_update_atoms threads might cross-read mlambda */
1168 /* Only account for local atoms */
1169 for (int b = b0; b < b1; b++)
1171 mlambda[b] *= 0.5 * nlocat[b];
1178 for (int b = b0; b < b1; b++)
1180 /* Note that this this is dhdl*dt^2, the dt^2 factor is corrected
1181 * later after the contributions are reduced over the threads.
1183 dhdl -= lincsd->mlambda[b] * lincsd->ddist[b];
1185 lincsd->task[th].dhdlambda = dhdl;
1190 /* Constraint virial */
1191 for (int b = b0; b < b1; b++)
1193 real tmp0 = -bllen[b] * mlambda[b];
1194 for (int i = 0; i < DIM; i++)
1196 real tmp1 = tmp0 * r[b][i];
1197 for (int j = 0; j < DIM; j++)
1199 vir_r_m_dr[i][j] -= tmp1 * r[b][j];
1202 } /* 22 ncons flops */
1206 * 26*ncons + 6*nrtot + nrec*(ncons+2*nrtot)
1207 * + nit * (20*ncons + nrec*(ncons+2*nrtot) + 17 ncons)
1209 * (26+nrec)*ncons + (6+2*nrec)*nrtot
1210 * + nit * ((37+nrec)*ncons + 2*nrec*nrtot)
1212 * (63+nrec)*ncons + (6+4*nrec)*nrtot
1216 /*! \brief Sets the elements in the LINCS matrix for task task. */
1217 static void set_lincs_matrix_task(Lincs* li, Task* li_task, const real* invmass, int* ncc_triangle, int* nCrossTaskTriangles)
1219 /* Construct the coupling coefficient matrix blmf */
1220 li_task->ntriangle = 0;
1222 *nCrossTaskTriangles = 0;
1223 for (int i = li_task->b0; i < li_task->b1; i++)
1225 const int a1 = li->atoms[i].index1;
1226 const int a2 = li->atoms[i].index2;
1227 for (int n = li->blnr[i]; n < li->blnr[i + 1]; n++)
1229 const int k = li->blbnb[n];
1231 /* If we are using multiple, independent tasks for LINCS,
1232 * the calls to check_assign_connected should have
1233 * put all connected constraints in our task.
1235 assert(li->bTaskDep || (k >= li_task->b0 && k < li_task->b1));
1238 if (a1 == li->atoms[k].index1 || a2 == li->atoms[k].index2)
1248 if (a1 == li->atoms[k].index1 || a1 == li->atoms[k].index2)
1258 li->blmf[n] = sign * invmass[center] * li->blc[i] * li->blc[k];
1259 li->blmf1[n] = sign * 0.5;
1260 if (li->ncg_triangle > 0)
1262 /* Look for constraint triangles */
1263 for (int nk = li->blnr[k]; nk < li->blnr[k + 1]; nk++)
1265 const int kk = li->blbnb[nk];
1266 if (kk != i && kk != k && (li->atoms[kk].index1 == end || li->atoms[kk].index2 == end))
1268 /* Check if the constraints in this triangle actually
1269 * belong to a different task. We still assign them
1270 * here, since it's convenient for the triangle
1271 * iterations, but we then need an extra barrier.
1273 if (k < li_task->b0 || k >= li_task->b1 || kk < li_task->b0 || kk >= li_task->b1)
1275 (*nCrossTaskTriangles)++;
1278 if (li_task->ntriangle == 0 || li_task->triangle[li_task->ntriangle - 1] < i)
1280 /* Add this constraint to the triangle list */
1281 li_task->triangle[li_task->ntriangle] = i;
1282 li_task->tri_bits[li_task->ntriangle] = 0;
1283 li_task->ntriangle++;
1284 if (li->blnr[i + 1] - li->blnr[i]
1285 > static_cast<int>(sizeof(li_task->tri_bits[0]) * 8 - 1))
1288 "A constraint is connected to %d constraints, this is "
1289 "more than the %zu allowed for constraints participating "
1291 li->blnr[i + 1] - li->blnr[i],
1292 sizeof(li_task->tri_bits[0]) * 8 - 1);
1295 li_task->tri_bits[li_task->ntriangle - 1] |= (1 << (n - li->blnr[i]));
1304 /*! \brief Sets the elements in the LINCS matrix. */
1305 static void set_lincs_matrix(Lincs* li, real* invmass, real lambda)
1307 const real invsqrt2 = 0.7071067811865475244;
1309 for (int i = 0; (i < li->nc); i++)
1311 const int a1 = li->atoms[i].index1;
1312 const int a2 = li->atoms[i].index2;
1313 li->blc[i] = gmx::invsqrt(invmass[a1] + invmass[a2]);
1314 li->blc1[i] = invsqrt2;
1317 /* Construct the coupling coefficient matrix blmf */
1318 int ntriangle = 0, ncc_triangle = 0, nCrossTaskTriangles = 0;
1319 #pragma omp parallel for reduction(+: ntriangle, ncc_triangle, nCrossTaskTriangles) num_threads(li->ntask) schedule(static)
1320 for (int th = 0; th < li->ntask; th++)
1324 set_lincs_matrix_task(li, &li->task[th], invmass, &ncc_triangle, &nCrossTaskTriangles);
1325 ntriangle += li->task[th].ntriangle;
1327 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
1329 li->ntriangle = ntriangle;
1330 li->ncc_triangle = ncc_triangle;
1331 li->bTaskDepTri = (nCrossTaskTriangles > 0);
1335 fprintf(debug, "The %d constraints participate in %d triangles\n", li->nc, li->ntriangle);
1336 fprintf(debug, "There are %d constraint couplings, of which %d in triangles\n", li->ncc,
1338 if (li->ntriangle > 0 && li->ntask > 1)
1341 "%d constraint triangles contain constraints assigned to different tasks\n",
1342 nCrossTaskTriangles);
1347 * so we know with which lambda value the masses have been set.
1349 li->matlam = lambda;
1352 //! Finds all triangles of atoms that share constraints to a central atom.
1353 static int count_triangle_constraints(const InteractionLists& ilist, const ListOfLists<int>& at2con)
1355 const int ncon1 = ilist[F_CONSTR].size() / 3;
1356 const int ncon_tot = ncon1 + ilist[F_CONSTRNC].size() / 3;
1358 gmx::ArrayRef<const int> ia1 = ilist[F_CONSTR].iatoms;
1359 gmx::ArrayRef<const int> ia2 = ilist[F_CONSTRNC].iatoms;
1361 int ncon_triangle = 0;
1362 for (int c0 = 0; c0 < ncon_tot; c0++)
1364 bool bTriangle = FALSE;
1365 const int* iap = constr_iatomptr(ia1, ia2, c0);
1366 const int a00 = iap[1];
1367 const int a01 = iap[2];
1368 for (const int c1 : at2con[a01])
1372 const int* iap = constr_iatomptr(ia1, ia2, c1);
1373 const int a10 = iap[1];
1374 const int a11 = iap[2];
1384 for (const int c2 : at2con[ac1])
1386 if (c2 != c0 && c2 != c1)
1388 const int* iap = constr_iatomptr(ia1, ia2, c2);
1389 const int a20 = iap[1];
1390 const int a21 = iap[2];
1391 if (a20 == a00 || a21 == a00)
1405 return ncon_triangle;
1408 //! Finds sequences of sequential constraints.
1409 static bool more_than_two_sequential_constraints(const InteractionLists& ilist, const ListOfLists<int>& at2con)
1411 const int ncon1 = ilist[F_CONSTR].size() / 3;
1412 const int ncon_tot = ncon1 + ilist[F_CONSTRNC].size() / 3;
1414 gmx::ArrayRef<const int> ia1 = ilist[F_CONSTR].iatoms;
1415 gmx::ArrayRef<const int> ia2 = ilist[F_CONSTRNC].iatoms;
1417 for (int c = 0; c < ncon_tot; c++)
1419 const int* iap = constr_iatomptr(ia1, ia2, c);
1420 const int a1 = iap[1];
1421 const int a2 = iap[2];
1422 /* Check if this constraint has constraints connected at both atoms */
1423 if (at2con[a1].ssize() > 1 && at2con[a2].ssize() > 1)
1432 Lincs* init_lincs(FILE* fplog,
1433 const gmx_mtop_t& mtop,
1434 int nflexcon_global,
1435 ArrayRef<const ListOfLists<int>> atomToConstraintsPerMolType,
1440 // TODO this should become a unique_ptr
1442 bool bMoreThanTwoSeq;
1446 fprintf(fplog, "\nInitializing%s LINear Constraint Solver\n", bPLINCS ? " Parallel" : "");
1451 li->ncg = gmx_mtop_ftype_count(mtop, F_CONSTR) + gmx_mtop_ftype_count(mtop, F_CONSTRNC);
1452 li->ncg_flex = nflexcon_global;
1455 li->nOrder = nProjOrder;
1457 li->max_connect = 0;
1458 for (size_t mt = 0; mt < mtop.moltype.size(); mt++)
1460 const auto& at2con = atomToConstraintsPerMolType[mt];
1461 for (int a = 0; a < mtop.moltype[mt].atoms.nr; a++)
1463 li->max_connect = std::max(li->max_connect, int(at2con[a].ssize()));
1467 li->ncg_triangle = 0;
1468 bMoreThanTwoSeq = FALSE;
1469 for (const gmx_molblock_t& molb : mtop.molblock)
1471 const gmx_moltype_t& molt = mtop.moltype[molb.type];
1472 const auto& at2con = atomToConstraintsPerMolType[molb.type];
1474 li->ncg_triangle += molb.nmol * count_triangle_constraints(molt.ilist, at2con);
1476 if (!bMoreThanTwoSeq && more_than_two_sequential_constraints(molt.ilist, at2con))
1478 bMoreThanTwoSeq = TRUE;
1482 /* Check if we need to communicate not only before LINCS,
1483 * but also before each iteration.
1484 * The check for only two sequential constraints is only
1485 * useful for the common case of H-bond only constraints.
1486 * With more effort we could also make it useful for small
1487 * molecules with nr. sequential constraints <= nOrder-1.
1489 li->bCommIter = (bPLINCS && (li->nOrder < 1 || bMoreThanTwoSeq));
1491 if (debug && bPLINCS)
1493 fprintf(debug, "PLINCS communication before each iteration: %d\n", static_cast<int>(li->bCommIter));
1496 /* LINCS can run on any number of threads.
1497 * Currently the number is fixed for the whole simulation,
1498 * but it could be set in set_lincs().
1499 * The current constraint to task assignment code can create independent
1500 * tasks only when not more than two constraints are connected sequentially.
1502 li->ntask = gmx_omp_nthreads_get(emntLINCS);
1503 li->bTaskDep = (li->ntask > 1 && bMoreThanTwoSeq);
1506 fprintf(debug, "LINCS: using %d threads, tasks are %sdependent\n", li->ntask,
1507 li->bTaskDep ? "" : "in");
1515 /* Allocate an extra elements for "task-overlap" constraints */
1516 li->task.resize(li->ntask + 1);
1519 if (bPLINCS || li->ncg_triangle > 0)
1521 please_cite(fplog, "Hess2008a");
1525 please_cite(fplog, "Hess97a");
1530 fprintf(fplog, "The number of constraints is %d\n", li->ncg);
1534 "There are constraints between atoms in different decomposition domains,\n"
1535 "will communicate selected coordinates each lincs iteration\n");
1537 if (li->ncg_triangle > 0)
1540 "%d constraints are involved in constraint triangles,\n"
1541 "will apply an additional matrix expansion of order %d for couplings\n"
1542 "between constraints inside triangles\n",
1543 li->ncg_triangle, li->nOrder);
1550 void done_lincs(Lincs* li)
1555 /*! \brief Sets up the work division over the threads. */
1556 static void lincs_thread_setup(Lincs* li, int natoms)
1558 li->atf.resize(natoms);
1560 gmx::ArrayRef<gmx_bitmask_t> atf = li->atf;
1562 /* Clear the atom flags */
1563 for (gmx_bitmask_t& mask : atf)
1565 bitmask_clear(&mask);
1568 if (li->ntask > BITMASK_SIZE)
1570 gmx_fatal(FARGS, "More than %d threads is not supported for LINCS.", BITMASK_SIZE);
1573 for (int th = 0; th < li->ntask; th++)
1575 const Task* li_task = &li->task[th];
1577 /* For each atom set a flag for constraints from each */
1578 for (int b = li_task->b0; b < li_task->b1; b++)
1580 bitmask_set_bit(&atf[li->atoms[b].index1], th);
1581 bitmask_set_bit(&atf[li->atoms[b].index2], th);
1585 #pragma omp parallel for num_threads(li->ntask) schedule(static)
1586 for (int th = 0; th < li->ntask; th++)
1594 li_task = &li->task[th];
1596 bitmask_init_low_bits(&mask, th);
1598 li_task->ind.clear();
1599 li_task->ind_r.clear();
1600 for (b = li_task->b0; b < li_task->b1; b++)
1602 /* We let the constraint with the lowest thread index
1603 * operate on atoms with constraints from multiple threads.
1605 if (bitmask_is_disjoint(atf[li->atoms[b].index1], mask)
1606 && bitmask_is_disjoint(atf[li->atoms[b].index2], mask))
1608 /* Add the constraint to the local atom update index */
1609 li_task->ind.push_back(b);
1613 /* Add the constraint to the rest block */
1614 li_task->ind_r.push_back(b);
1618 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
1621 /* We need to copy all constraints which have not be assigned
1622 * to a thread to a separate list which will be handled by one thread.
1624 Task* li_m = &li->task[li->ntask];
1627 for (int th = 0; th < li->ntask; th++)
1629 const Task& li_task = li->task[th];
1631 for (int ind_r : li_task.ind_r)
1633 li_m->ind.push_back(ind_r);
1638 fprintf(debug, "LINCS thread %d: %zu constraints\n", th, li_task.ind.size());
1644 fprintf(debug, "LINCS thread r: %zu constraints\n", li_m->ind.size());
1648 //! Assign a constraint.
1649 static void assign_constraint(Lincs* li,
1650 int constraint_index,
1655 const ListOfLists<int>& at2con)
1661 /* Make an mapping of local topology constraint index to LINCS index */
1662 li->con_index[constraint_index] = con;
1664 li->bllen0[con] = lenA;
1665 li->ddist[con] = lenB - lenA;
1666 /* Set the length to the topology A length */
1667 li->bllen[con] = lenA;
1668 li->atoms[con].index1 = a1;
1669 li->atoms[con].index2 = a2;
1671 /* Make space in the constraint connection matrix for constraints
1672 * connected to both end of the current constraint.
1674 li->ncc += at2con[a1].ssize() - 1 + at2con[a2].ssize() - 1;
1676 li->blnr[con + 1] = li->ncc;
1678 /* Increase the constraint count */
1682 /*! \brief Check if constraint with topology index constraint_index is connected
1683 * to other constraints, and if so add those connected constraints to our task. */
1684 static void check_assign_connected(Lincs* li,
1685 gmx::ArrayRef<const int> iatom,
1686 const InteractionDefinitions& idef,
1690 const ListOfLists<int>& at2con)
1692 /* Currently this function only supports constraint groups
1693 * in which all constraints share at least one atom
1694 * (e.g. H-bond constraints).
1695 * Check both ends of the current constraint for
1696 * connected constraints. We need to assign those
1699 for (int end = 0; end < 2; end++)
1701 const int a = (end == 0 ? a1 : a2);
1703 for (const int cc : at2con[a])
1705 /* Check if constraint cc has not yet been assigned */
1706 if (li->con_index[cc] == -1)
1708 const int type = iatom[cc * 3];
1709 const real lenA = idef.iparams[type].constr.dA;
1710 const real lenB = idef.iparams[type].constr.dB;
1712 if (bDynamics || lenA != 0 || lenB != 0)
1714 assign_constraint(li, cc, iatom[3 * cc + 1], iatom[3 * cc + 2], lenA, lenB, at2con);
1721 /*! \brief Check if constraint with topology index constraint_index is involved
1722 * in a constraint triangle, and if so add the other two constraints
1723 * in the triangle to our task. */
1724 static void check_assign_triangle(Lincs* li,
1725 gmx::ArrayRef<const int> iatom,
1726 const InteractionDefinitions& idef,
1728 int constraint_index,
1731 const ListOfLists<int>& at2con)
1733 int nca, cc[32], ca[32];
1734 int c_triangle[2] = { -1, -1 };
1737 for (const int c : at2con[a1])
1739 if (c != constraint_index)
1743 aa1 = iatom[c * 3 + 1];
1744 aa2 = iatom[c * 3 + 2];
1760 for (const int c : at2con[a2])
1762 if (c != constraint_index)
1766 aa1 = iatom[c * 3 + 1];
1767 aa2 = iatom[c * 3 + 2];
1770 for (i = 0; i < nca; i++)
1774 c_triangle[0] = cc[i];
1781 for (i = 0; i < nca; i++)
1785 c_triangle[0] = cc[i];
1793 if (c_triangle[0] >= 0)
1797 for (end = 0; end < 2; end++)
1799 /* Check if constraint c_triangle[end] has not yet been assigned */
1800 if (li->con_index[c_triangle[end]] == -1)
1805 i = c_triangle[end] * 3;
1807 lenA = idef.iparams[type].constr.dA;
1808 lenB = idef.iparams[type].constr.dB;
1810 if (bDynamics || lenA != 0 || lenB != 0)
1812 assign_constraint(li, c_triangle[end], iatom[i + 1], iatom[i + 2], lenA, lenB, at2con);
1819 //! Sets matrix indices.
1820 static void set_matrix_indices(Lincs* li, const Task& li_task, const ListOfLists<int>& at2con, bool bSortMatrix)
1822 for (int b = li_task.b0; b < li_task.b1; b++)
1824 const int a1 = li->atoms[b].index1;
1825 const int a2 = li->atoms[b].index2;
1827 int i = li->blnr[b];
1828 for (const int constraint : at2con[a1])
1830 const int concon = li->con_index[constraint];
1833 li->blbnb[i++] = concon;
1836 for (const int constraint : at2con[a2])
1838 const int concon = li->con_index[constraint];
1841 li->blbnb[i++] = concon;
1847 /* Order the blbnb matrix to optimize memory access */
1848 std::sort(li->blbnb.begin() + li->blnr[b], li->blbnb.begin() + li->blnr[b + 1]);
1853 void set_lincs(const InteractionDefinitions& idef, const t_mdatoms& md, bool bDynamics, const t_commrec* cr, Lincs* li)
1858 /* Zero the thread index ranges.
1859 * Otherwise without local constraints we could return with old ranges.
1861 for (int i = 0; i < li->ntask; i++)
1865 li->task[i].ind.clear();
1869 li->task[li->ntask].ind.clear();
1872 /* This is the local topology, so there are only F_CONSTR constraints */
1873 if (idef.il[F_CONSTR].empty())
1875 /* There are no constraints,
1876 * we do not need to fill any data structures.
1883 fprintf(debug, "Building the LINCS connectivity\n");
1887 if (DOMAINDECOMP(cr))
1889 if (cr->dd->constraints)
1893 dd_get_constraint_range(cr->dd, &start, &natoms);
1897 natoms = dd_numHomeAtoms(*cr->dd);
1905 const ListOfLists<int> at2con =
1906 make_at2con(natoms, idef.il, idef.iparams, flexibleConstraintTreatment(bDynamics));
1908 const int ncon_tot = idef.il[F_CONSTR].size() / 3;
1910 /* Ensure we have enough padding for aligned loads for each thread */
1911 const int numEntries = ncon_tot + li->ntask * simd_width;
1912 li->con_index.resize(numEntries);
1913 li->bllen0.resize(numEntries);
1914 li->ddist.resize(numEntries);
1915 li->atoms.resize(numEntries);
1916 li->blc.resize(numEntries);
1917 li->blc1.resize(numEntries);
1918 li->blnr.resize(numEntries + 1);
1919 li->bllen.resize(numEntries);
1920 li->tmpv.resizeWithPadding(numEntries);
1921 if (DOMAINDECOMP(cr))
1923 li->nlocat.resize(numEntries);
1925 li->tmp1.resize(numEntries);
1926 li->tmp2.resize(numEntries);
1927 li->tmp3.resize(numEntries);
1928 li->tmp4.resize(numEntries);
1929 li->mlambda.resize(numEntries);
1931 gmx::ArrayRef<const int> iatom = idef.il[F_CONSTR].iatoms;
1933 li->blnr[0] = li->ncc;
1935 /* Assign the constraints for li->ntask LINCS tasks.
1936 * We target a uniform distribution of constraints over the tasks.
1937 * Note that when flexible constraints are present, but are removed here
1938 * (e.g. because we are doing EM) we get imbalance, but since that doesn't
1939 * happen during normal MD, that's ok.
1942 /* Determine the number of constraints we need to assign here */
1943 int ncon_assign = ncon_tot;
1946 /* With energy minimization, flexible constraints are ignored
1947 * (and thus minimized, as they should be).
1949 ncon_assign -= countFlexibleConstraints(idef.il, idef.iparams);
1952 /* Set the target constraint count per task to exactly uniform,
1953 * this might be overridden below.
1955 int ncon_target = (ncon_assign + li->ntask - 1) / li->ntask;
1957 /* Mark all constraints as unassigned by setting their index to -1 */
1958 for (int con = 0; con < ncon_tot; con++)
1960 li->con_index[con] = -1;
1964 for (int th = 0; th < li->ntask; th++)
1968 li_task = &li->task[th];
1970 #if GMX_SIMD_HAVE_REAL
1971 /* With indepedent tasks we likely have H-bond constraints or constraint
1972 * pairs. The connected constraints will be pulled into the task, so the
1973 * constraints per task will often exceed ncon_target.
1974 * Triangle constraints can also increase the count, but there are
1975 * relatively few of those, so we usually expect to get ncon_target.
1979 /* We round ncon_target to a multiple of GMX_SIMD_WIDTH,
1980 * since otherwise a lot of operations can be wasted.
1981 * There are several ways to round here, we choose the one
1982 * that alternates block sizes, which helps with Intel HT.
1984 ncon_target = ((ncon_assign * (th + 1)) / li->ntask - li->nc_real + GMX_SIMD_REAL_WIDTH - 1)
1985 & ~(GMX_SIMD_REAL_WIDTH - 1);
1987 #endif // GMX_SIMD==2 && GMX_SIMD_HAVE_REAL
1989 /* Continue filling the arrays where we left off with the previous task,
1990 * including padding for SIMD.
1992 li_task->b0 = li->nc;
1994 gmx::ArrayRef<const t_iparams> iparams = idef.iparams;
1996 while (con < ncon_tot && li->nc - li_task->b0 < ncon_target)
1998 if (li->con_index[con] == -1)
2003 type = iatom[3 * con];
2004 a1 = iatom[3 * con + 1];
2005 a2 = iatom[3 * con + 2];
2006 lenA = iparams[type].constr.dA;
2007 lenB = iparams[type].constr.dB;
2008 /* Skip the flexible constraints when not doing dynamics */
2009 if (bDynamics || lenA != 0 || lenB != 0)
2011 assign_constraint(li, con, a1, a2, lenA, lenB, at2con);
2013 if (li->ntask > 1 && !li->bTaskDep)
2015 /* We can generate independent tasks. Check if we
2016 * need to assign connected constraints to our task.
2018 check_assign_connected(li, iatom, idef, bDynamics, a1, a2, at2con);
2020 if (li->ntask > 1 && li->ncg_triangle > 0)
2022 /* Ensure constraints in one triangle are assigned
2025 check_assign_triangle(li, iatom, idef, bDynamics, con, a1, a2, at2con);
2033 li_task->b1 = li->nc;
2037 /* Copy the last atom pair indices and lengths for constraints
2038 * up to a multiple of simd_width, such that we can do all
2039 * SIMD operations without having to worry about end effects.
2043 li->nc = ((li_task->b1 + simd_width - 1) / simd_width) * simd_width;
2044 last = li_task->b1 - 1;
2045 for (i = li_task->b1; i < li->nc; i++)
2047 li->atoms[i] = li->atoms[last];
2048 li->bllen0[i] = li->bllen0[last];
2049 li->ddist[i] = li->ddist[last];
2050 li->bllen[i] = li->bllen[last];
2051 li->blnr[i + 1] = li->blnr[last + 1];
2055 /* Keep track of how many constraints we assigned */
2056 li->nc_real += li_task->b1 - li_task->b0;
2060 fprintf(debug, "LINCS task %d constraints %d - %d\n", th, li_task->b0, li_task->b1);
2064 assert(li->nc_real == ncon_assign);
2068 /* Without DD we order the blbnb matrix to optimize memory access.
2069 * With DD the overhead of sorting is more than the gain during access.
2071 bSortMatrix = !DOMAINDECOMP(cr);
2073 li->blbnb.resize(li->ncc);
2075 #pragma omp parallel for num_threads(li->ntask) schedule(static)
2076 for (int th = 0; th < li->ntask; th++)
2080 Task& li_task = li->task[th];
2082 if (li->ncg_triangle > 0)
2084 /* This is allocating too much, but it is difficult to improve */
2085 li_task.triangle.resize(li_task.b1 - li_task.b0);
2086 li_task.tri_bits.resize(li_task.b1 - li_task.b0);
2089 set_matrix_indices(li, li_task, at2con, bSortMatrix);
2091 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
2094 if (cr->dd == nullptr)
2096 /* Since the matrix is static, we should free some memory */
2097 li->blbnb.resize(li->ncc);
2100 li->blmf.resize(li->ncc);
2101 li->blmf1.resize(li->ncc);
2102 li->tmpncc.resize(li->ncc);
2104 gmx::ArrayRef<const int> nlocat_dd = dd_constraints_nlocalatoms(cr->dd);
2105 if (!nlocat_dd.empty())
2107 /* Convert nlocat from local topology to LINCS constraint indexing */
2108 for (con = 0; con < ncon_tot; con++)
2110 li->nlocat[li->con_index[con]] = nlocat_dd[con];
2120 fprintf(debug, "Number of constraints is %d, padded %d, couplings %d\n", li->nc_real,
2126 lincs_thread_setup(li, md.nr);
2129 set_lincs_matrix(li, md.invmass, md.lambda);
2132 //! Issues a warning when LINCS constraints cannot be satisfied.
2133 static void lincs_warning(gmx_domdec_t* dd,
2134 ArrayRef<const RVec> x,
2135 ArrayRef<const RVec> xprime,
2138 gmx::ArrayRef<const AtomPair> atoms,
2139 gmx::ArrayRef<const real> bllen,
2144 real wfac = std::cos(DEG2RAD * wangle);
2147 "bonds that rotated more than %g degrees:\n"
2148 " atom 1 atom 2 angle previous, current, constraint length\n",
2151 for (int b = 0; b < ncons; b++)
2153 const int i = atoms[b].index1;
2154 const int j = atoms[b].index2;
2159 pbc_dx_aiuc(pbc, x[i], x[j], v0);
2160 pbc_dx_aiuc(pbc, xprime[i], xprime[j], v1);
2164 rvec_sub(x[i], x[j], v0);
2165 rvec_sub(xprime[i], xprime[j], v1);
2169 real cosine = ::iprod(v0, v1) / (d0 * d1);
2172 fprintf(stderr, " %6d %6d %5.1f %8.4f %8.4f %8.4f\n", ddglatnr(dd, i),
2173 ddglatnr(dd, j), RAD2DEG * std::acos(cosine), d0, d1, bllen[b]);
2174 if (!std::isfinite(d1))
2176 gmx_fatal(FARGS, "Bond length not finite.");
2182 if (*warncount > maxwarn)
2184 too_many_constraint_warnings(econtLINCS, *warncount);
2188 //! Status information about how well LINCS satisified the constraints in this domain
2189 struct LincsDeviations
2191 //! The maximum over all bonds in this domain of the relative deviation in bond lengths
2192 real maxDeviation = 0;
2193 //! Sum over all bonds in this domain of the squared relative deviation
2194 real sumSquaredDeviation = 0;
2195 //! Index of bond with max deviation
2196 int indexOfMaxDeviation = -1;
2197 //! Number of bonds constrained in this domain
2198 int numConstraints = 0;
2201 //! Determine how well the constraints have been satisfied.
2202 static LincsDeviations makeLincsDeviations(const Lincs& lincsd, ArrayRef<const RVec> x, const t_pbc* pbc)
2204 LincsDeviations result;
2205 const ArrayRef<const AtomPair> atoms = lincsd.atoms;
2206 const ArrayRef<const real> bllen = lincsd.bllen;
2207 const ArrayRef<const int> nlocat = lincsd.nlocat;
2209 for (int task = 0; task < lincsd.ntask; task++)
2211 for (int b = lincsd.task[task].b0; b < lincsd.task[task].b1; b++)
2216 pbc_dx_aiuc(pbc, x[atoms[b].index1], x[atoms[b].index2], dx);
2220 rvec_sub(x[atoms[b].index1], x[atoms[b].index2], dx);
2222 real r2 = ::norm2(dx);
2223 real len = r2 * gmx::invsqrt(r2);
2224 real d = std::abs(len / bllen[b] - 1.0_real);
2225 if (d > result.maxDeviation && (nlocat.empty() || nlocat[b]))
2227 result.maxDeviation = d;
2228 result.indexOfMaxDeviation = b;
2232 result.sumSquaredDeviation += d * d;
2233 result.numConstraints++;
2237 result.sumSquaredDeviation += nlocat[b] * d * d;
2238 result.numConstraints += nlocat[b];
2243 if (!nlocat.empty())
2245 result.numConstraints /= 2;
2246 result.sumSquaredDeviation *= 0.5;
2251 bool constrain_lincs(bool computeRmsd,
2252 const t_inputrec& ir,
2255 const t_mdatoms& md,
2256 const t_commrec* cr,
2257 const gmx_multisim_t* ms,
2258 ArrayRefWithPadding<const RVec> xPadded,
2259 ArrayRefWithPadding<RVec> xprimePadded,
2260 ArrayRef<RVec> min_proj,
2269 ConstraintVariable econq,
2276 /* This boolean should be set by a flag passed to this routine.
2277 * We can also easily check if any constraint length is changed,
2278 * if not dH/dlambda=0 and we can also set the boolean to FALSE.
2280 bool bCalcDHDL = (ir.efep != efepNO && dvdlambda != nullptr);
2282 if (lincsd->nc == 0 && cr->dd == nullptr)
2286 lincsd->rmsdData = { { 0 } };
2292 ArrayRef<const RVec> x = xPadded.unpaddedArrayRef();
2293 ArrayRef<RVec> xprime = xprimePadded.unpaddedArrayRef();
2295 if (econq == ConstraintVariable::Positions)
2297 /* We can't use bCalcDHDL here, since NULL can be passed for dvdlambda
2298 * also with efep!=fepNO.
2300 if (ir.efep != efepNO)
2302 if (md.nMassPerturbed && lincsd->matlam != md.lambda)
2304 set_lincs_matrix(lincsd, md.invmass, md.lambda);
2307 for (int i = 0; i < lincsd->nc; i++)
2309 lincsd->bllen[i] = lincsd->bllen0[i] + lambda * lincsd->ddist[i];
2313 if (lincsd->ncg_flex)
2315 /* Set the flexible constraint lengths to the old lengths */
2318 for (int i = 0; i < lincsd->nc; i++)
2320 if (lincsd->bllen[i] == 0)
2323 pbc_dx_aiuc(pbc, x[lincsd->atoms[i].index1], x[lincsd->atoms[i].index2], dx);
2324 lincsd->bllen[i] = norm(dx);
2330 for (int i = 0; i < lincsd->nc; i++)
2332 if (lincsd->bllen[i] == 0)
2334 lincsd->bllen[i] = std::sqrt(
2335 distance2(x[lincsd->atoms[i].index1], x[lincsd->atoms[i].index2]));
2341 const bool printDebugOutput = ((debug != nullptr) && lincsd->nc > 0);
2342 if (printDebugOutput)
2344 LincsDeviations deviations = makeLincsDeviations(*lincsd, xprime, pbc);
2345 fprintf(debug, " Rel. Constraint Deviation: RMS MAX between atoms\n");
2346 fprintf(debug, " Before LINCS %.6f %.6f %6d %6d\n",
2347 std::sqrt(deviations.sumSquaredDeviation / deviations.numConstraints),
2348 deviations.maxDeviation,
2349 ddglatnr(cr->dd, lincsd->atoms[deviations.indexOfMaxDeviation].index1),
2350 ddglatnr(cr->dd, lincsd->atoms[deviations.indexOfMaxDeviation].index2));
2353 /* This bWarn var can be updated by multiple threads
2354 * at the same time. But as we only need to detect
2355 * if a warning occurred or not, this is not an issue.
2359 /* The OpenMP parallel region of constrain_lincs for coords */
2360 #pragma omp parallel num_threads(lincsd->ntask)
2364 int th = gmx_omp_get_thread_num();
2366 clear_mat(lincsd->task[th].vir_r_m_dr);
2368 do_lincs(xPadded, xprimePadded, box, pbc, lincsd, th, md.invmass, cr, bCalcDHDL,
2369 ir.LincsWarnAngle, &bWarn, invdt, v, bCalcVir,
2370 th == 0 ? vir_r_m_dr : lincsd->task[th].vir_r_m_dr);
2372 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
2375 if (computeRmsd || printDebugOutput || bWarn)
2377 LincsDeviations deviations = makeLincsDeviations(*lincsd, xprime, pbc);
2381 // This is reduced across domains in compute_globals and
2382 // reported to the log file.
2383 lincsd->rmsdData[0] = deviations.numConstraints;
2384 lincsd->rmsdData[1] = deviations.sumSquaredDeviation;
2388 // This is never read
2389 lincsd->rmsdData = { { 0 } };
2391 if (printDebugOutput)
2393 fprintf(debug, " After LINCS %.6f %.6f %6d %6d\n\n",
2394 std::sqrt(deviations.sumSquaredDeviation / deviations.numConstraints),
2395 deviations.maxDeviation,
2396 ddglatnr(cr->dd, lincsd->atoms[deviations.indexOfMaxDeviation].index1),
2397 ddglatnr(cr->dd, lincsd->atoms[deviations.indexOfMaxDeviation].index2));
2402 if (maxwarn < INT_MAX)
2404 std::string simMesg;
2407 simMesg += gmx::formatString(" in simulation %d", ms->sim);
2411 ", time %g (ps) LINCS WARNING%s\n"
2412 "relative constraint deviation after LINCS:\n"
2413 "rms %.6f, max %.6f (between atoms %d and %d)\n",
2414 step, ir.init_t + step * ir.delta_t, simMesg.c_str(),
2415 std::sqrt(deviations.sumSquaredDeviation / deviations.numConstraints),
2416 deviations.maxDeviation,
2417 ddglatnr(cr->dd, lincsd->atoms[deviations.indexOfMaxDeviation].index1),
2418 ddglatnr(cr->dd, lincsd->atoms[deviations.indexOfMaxDeviation].index2));
2420 lincs_warning(cr->dd, x, xprime, pbc, lincsd->nc, lincsd->atoms, lincsd->bllen,
2421 ir.LincsWarnAngle, maxwarn, warncount);
2423 bOK = (deviations.maxDeviation < 0.5);
2427 if (lincsd->ncg_flex)
2429 for (int i = 0; (i < lincsd->nc); i++)
2431 if (lincsd->bllen0[i] == 0 && lincsd->ddist[i] == 0)
2433 lincsd->bllen[i] = 0;
2440 /* The OpenMP parallel region of constrain_lincs for derivatives */
2441 #pragma omp parallel num_threads(lincsd->ntask)
2445 int th = gmx_omp_get_thread_num();
2447 do_lincsp(xPadded, xprimePadded, min_proj, pbc, lincsd, th, md.invmass, econq,
2448 bCalcDHDL, bCalcVir, th == 0 ? vir_r_m_dr : lincsd->task[th].vir_r_m_dr);
2450 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
2456 /* Reduce the dH/dlambda contributions over the threads */
2461 for (th = 0; th < lincsd->ntask; th++)
2463 dhdlambda += lincsd->task[th].dhdlambda;
2465 if (econq == ConstraintVariable::Positions)
2467 /* dhdlambda contains dH/dlambda*dt^2, correct for this */
2468 /* TODO This should probably use invdt, so that sd integrator scaling works properly */
2469 dhdlambda /= ir.delta_t * ir.delta_t;
2471 *dvdlambda += dhdlambda;
2474 if (bCalcVir && lincsd->ntask > 1)
2476 for (int i = 1; i < lincsd->ntask; i++)
2478 m_add(vir_r_m_dr, lincsd->task[i].vir_r_m_dr, vir_r_m_dr);
2482 /* count assuming nit=1 */
2483 inc_nrnb(nrnb, eNR_LINCS, lincsd->nc_real);
2484 inc_nrnb(nrnb, eNR_LINCSMAT, (2 + lincsd->nOrder) * lincsd->ncc);
2485 if (lincsd->ntriangle > 0)
2487 inc_nrnb(nrnb, eNR_LINCSMAT, lincsd->nOrder * lincsd->ncc_triangle);
2491 inc_nrnb(nrnb, eNR_CONSTR_V, lincsd->nc_real * 2);
2495 inc_nrnb(nrnb, eNR_CONSTR_VIR, lincsd->nc_real);