Apply re-formatting to C++ in src/ tree.
[alexxy/gromacs.git] / src / gromacs / modularsimulator / domdechelper.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, 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 domain decomposition helper for the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  */
41
42 #include "gmxpre.h"
43
44 #include "domdechelper.h"
45
46 #include "gromacs/domdec/collect.h"
47 #include "gromacs/domdec/partition.h"
48 #include "gromacs/mdtypes/commrec.h"
49 #include "gromacs/mdtypes/inputrec.h"
50 #include "gromacs/mdtypes/state.h"
51 #include "gromacs/pbcutil/pbc.h"
52
53 #include "freeenergyperturbationdata.h"
54 #include "statepropagatordata.h"
55 #include "topologyholder.h"
56
57 namespace gmx
58 {
59 DomDecHelper::DomDecHelper(bool                            isVerbose,
60                            int                             verbosePrintInterval,
61                            StatePropagatorData*            statePropagatorData,
62                            FreeEnergyPerturbationData*     freeEnergyPerturbationData,
63                            TopologyHolder*                 topologyHolder,
64                            CheckBondedInteractionsCallback checkBondedInteractionsCallback,
65                            int                             nstglobalcomm,
66                            FILE*                           fplog,
67                            t_commrec*                      cr,
68                            const MDLogger&                 mdlog,
69                            Constraints*                    constr,
70                            t_inputrec*                     inputrec,
71                            MDAtoms*                        mdAtoms,
72                            t_nrnb*                         nrnb,
73                            gmx_wallcycle*                  wcycle,
74                            t_forcerec*                     fr,
75                            VirtualSitesHandler*            vsite,
76                            ImdSession*                     imdSession,
77                            pull_t*                         pull_work) :
78     nextNSStep_(-1),
79     isVerbose_(isVerbose),
80     verbosePrintInterval_(verbosePrintInterval),
81     nstglobalcomm_(nstglobalcomm),
82     statePropagatorData_(statePropagatorData),
83     freeEnergyPerturbationData_(freeEnergyPerturbationData),
84     topologyHolder_(topologyHolder),
85     checkBondedInteractionsCallback_(std::move(checkBondedInteractionsCallback)),
86     fplog_(fplog),
87     cr_(cr),
88     mdlog_(mdlog),
89     constr_(constr),
90     inputrec_(inputrec),
91     mdAtoms_(mdAtoms),
92     nrnb_(nrnb),
93     wcycle_(wcycle),
94     fr_(fr),
95     vsite_(vsite),
96     imdSession_(imdSession),
97     pull_work_(pull_work)
98 {
99     GMX_ASSERT(DOMAINDECOMP(cr), "Domain decomposition Helper constructed in non-DD simulation");
100 }
101
102 void DomDecHelper::setup()
103 {
104     // constant choices for this call to dd_partition_system
105     const bool     verbose       = false;
106     const bool     isMasterState = true;
107     const int      nstglobalcomm = 1;
108     gmx_wallcycle* wcycle        = nullptr;
109
110     // Distribute the charge groups over the nodes from the master node
111     partitionSystem(verbose,
112                     isMasterState,
113                     nstglobalcomm,
114                     wcycle,
115                     statePropagatorData_->localState(),
116                     statePropagatorData_->globalState());
117 }
118
119 void DomDecHelper::run(Step step, Time gmx_unused time)
120 {
121     if (step != nextNSStep_ || (step == inputrec_->init_step && inputrec_->bContinuation))
122     {
123         return;
124     }
125     std::unique_ptr<t_state> localState  = statePropagatorData_->localState();
126     t_state*                 globalState = statePropagatorData_->globalState();
127
128     // constant choices for this call to dd_partition_system
129     const bool verbose = isVerbose_ && (step % verbosePrintInterval_ == 0 || step == inputrec_->init_step);
130     bool isMasterState = false;
131
132     // Correct the new box if it is too skewed
133     if (inputrecDynamicBox(inputrec_))
134     {
135         // TODO: Correcting the box is done here (if using DD) or in ForceElement (non-DD simulations).
136         //       Think about unifying this responsibility, could this be done in one place?
137         if (correct_box(fplog_, step, localState->box))
138         {
139             isMasterState = true;
140         }
141     }
142     if (isMasterState)
143     {
144         dd_collect_state(cr_->dd, localState.get(), globalState);
145     }
146
147     // Distribute the charge groups over the nodes from the master node
148     partitionSystem(verbose, isMasterState, nstglobalcomm_, wcycle_, std::move(localState), globalState);
149 }
150
151 void DomDecHelper::partitionSystem(bool                     verbose,
152                                    bool                     isMasterState,
153                                    int                      nstglobalcomm,
154                                    gmx_wallcycle*           wcycle,
155                                    std::unique_ptr<t_state> localState,
156                                    t_state*                 globalState)
157 {
158     ForceBuffers* forcePointer = statePropagatorData_->forcePointer();
159
160     // Distribute the charge groups over the nodes from the master node
161     dd_partition_system(fplog_,
162                         mdlog_,
163                         inputrec_->init_step,
164                         cr_,
165                         isMasterState,
166                         nstglobalcomm,
167                         globalState,
168                         topologyHolder_->globalTopology(),
169                         inputrec_,
170                         imdSession_,
171                         pull_work_,
172                         localState.get(),
173                         forcePointer,
174                         mdAtoms_,
175                         topologyHolder_->localTopology_.get(),
176                         fr_,
177                         vsite_,
178                         constr_,
179                         nrnb_,
180                         wcycle,
181                         verbose);
182     topologyHolder_->updateLocalTopology();
183     checkBondedInteractionsCallback_();
184     statePropagatorData_->setLocalState(std::move(localState));
185     if (freeEnergyPerturbationData_)
186     {
187         freeEnergyPerturbationData_->updateMDAtoms();
188     }
189 }
190
191 std::optional<SignallerCallback> DomDecHelper::registerNSCallback()
192 {
193     return [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; };
194 }
195
196 } // namespace gmx