SYCL: Avoid using no_init read accessor in rocFFT
[alexxy/gromacs.git] / src / gromacs / mdlib / boxdeformation.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019,2020,2021, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \internal \file
36  * \brief Defines the box deformation code.
37  *
38  * \todo The .mdp specification should have a boolean for this module.
39  *
40  * \todo grompp should set up fields in the tpr file that carry the
41  * information about the original box, then the deform module would
42  * can be build alongside the update module, rather than need to be
43  * set up before the checkpoint is read.
44  *
45  * \author Berk Hess <hess@kth.se>
46  * \author Mark Abraham <mark.j.abraham@gmail.com>
47  * \ingroup module_mdlib
48  */
49 #include "gmxpre.h"
50
51 #include "boxdeformation.h"
52
53 #include <memory>
54
55 #include "gromacs/gmxlib/network.h"
56 #include "gromacs/math/invertmatrix.h"
57 #include "gromacs/math/vec.h"
58 #include "gromacs/mdtypes/commrec.h"
59 #include "gromacs/mdtypes/inputrec.h"
60 #include "gromacs/mdtypes/md_enums.h"
61 #include "gromacs/utility/arrayref.h"
62 #include "gromacs/utility/exceptions.h"
63
64 namespace gmx
65 {
66
67 std::unique_ptr<BoxDeformation> prepareBoxDeformation(const matrix&     initialBox,
68                                                       DDRole            ddRole,
69                                                       NumRanks          numRanks,
70                                                       MPI_Comm          communicator,
71                                                       const t_inputrec& inputrec)
72 {
73     if (!inputrecDeform(&inputrec))
74     {
75         return nullptr;
76     }
77     if (!EI_DYNAMICS(inputrec.eI))
78     {
79         GMX_THROW(NotImplementedError(
80                 "Box deformation is only supported with dynamical integrators"));
81     }
82
83     matrix box;
84     // Only the rank that read the tpr has the global state, and thus
85     // the initial box, so we pass that around.
86     // (numRanks != NumRanks::Multiple helps clang static analyzer to
87     // understand that box is defined in all cases)
88     if (ddRole == DDRole::Master || numRanks != NumRanks::Multiple)
89     {
90         copy_mat(initialBox, box);
91     }
92     if (numRanks == NumRanks::Multiple)
93     {
94         gmx_bcast(sizeof(box), box, communicator);
95     }
96
97     return std::make_unique<BoxDeformation>(inputrec.delta_t, inputrec.init_step, inputrec.deform, box);
98 }
99
100 BoxDeformation::BoxDeformation(double        timeStep,
101                                int64_t       initialStep,
102                                const tensor& deformationTensor,
103                                const matrix& referenceBox) :
104     timeStep_(timeStep), initialStep_(initialStep)
105 {
106     copy_mat(deformationTensor, deformationTensor_);
107     copy_mat(referenceBox, referenceBox_);
108 }
109
110 void BoxDeformation::apply(ArrayRef<RVec> x, matrix box, int64_t step)
111 {
112     matrix updatedBox, invbox, mu;
113
114     double elapsedTime = (step + 1 - initialStep_) * timeStep_;
115     copy_mat(box, updatedBox);
116     for (int i = 0; i < DIM; i++)
117     {
118         for (int j = 0; j < DIM; j++)
119         {
120             if (deformationTensor_[i][j] != 0)
121             {
122                 updatedBox[i][j] = referenceBox_[i][j] + elapsedTime * deformationTensor_[i][j];
123             }
124         }
125     }
126     /* We correct the off-diagonal elements,
127      * which can grow indefinitely during shearing,
128      * so the shifts do not get messed up.
129      */
130     for (int i = 1; i < DIM; i++)
131     {
132         for (int j = i - 1; j >= 0; j--)
133         {
134             while (updatedBox[i][j] - box[i][j] > 0.5 * updatedBox[j][j])
135             {
136                 rvec_dec(updatedBox[i], updatedBox[j]);
137             }
138             while (updatedBox[i][j] - box[i][j] < -0.5 * updatedBox[j][j])
139             {
140                 rvec_inc(updatedBox[i], updatedBox[j]);
141             }
142         }
143     }
144     invertBoxMatrix(box, invbox);
145     // Return the updated box
146     copy_mat(updatedBox, box);
147     mmul_ur0(box, invbox, mu);
148
149     for (auto& thisX : x)
150     {
151         thisX[XX] = mu[XX][XX] * thisX[XX] + mu[YY][XX] * thisX[YY] + mu[ZZ][XX] * thisX[ZZ];
152         thisX[YY] = mu[YY][YY] * thisX[YY] + mu[ZZ][YY] * thisX[ZZ];
153         thisX[ZZ] = mu[ZZ][ZZ] * thisX[ZZ];
154     }
155 }
156
157 } // namespace gmx