67cc9d52e138907f131c1c2237c6ab7b39b36c82
[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 "statepropagatordata.h"
54 #include "topologyholder.h"
55
56 namespace gmx
57 {
58 DomDecHelper::DomDecHelper(bool                               isVerbose,
59                            int                                verbosePrintInterval,
60                            StatePropagatorData*               statePropagatorData,
61                            TopologyHolder*                    topologyHolder,
62                            CheckBondedInteractionsCallbackPtr checkBondedInteractionsCallback,
63                            int                                nstglobalcomm,
64                            FILE*                              fplog,
65                            t_commrec*                         cr,
66                            const MDLogger&                    mdlog,
67                            Constraints*                       constr,
68                            t_inputrec*                        inputrec,
69                            MDAtoms*                           mdAtoms,
70                            t_nrnb*                            nrnb,
71                            gmx_wallcycle*                     wcycle,
72                            t_forcerec*                        fr,
73                            gmx_vsite_t*                       vsite,
74                            ImdSession*                        imdSession,
75                            pull_t*                            pull_work) :
76     nextNSStep_(-1),
77     isVerbose_(isVerbose),
78     verbosePrintInterval_(verbosePrintInterval),
79     nstglobalcomm_(nstglobalcomm),
80     statePropagatorData_(statePropagatorData),
81     topologyHolder_(topologyHolder),
82     checkBondedInteractionsCallback_(std::move(checkBondedInteractionsCallback)),
83     fplog_(fplog),
84     cr_(cr),
85     mdlog_(mdlog),
86     constr_(constr),
87     inputrec_(inputrec),
88     mdAtoms_(mdAtoms),
89     nrnb_(nrnb),
90     wcycle_(wcycle),
91     fr_(fr),
92     vsite_(vsite),
93     imdSession_(imdSession),
94     pull_work_(pull_work)
95 {
96     GMX_ASSERT(DOMAINDECOMP(cr), "Domain decomposition Helper constructed in non-DD simulation");
97 }
98
99 void DomDecHelper::setup()
100 {
101     std::unique_ptr<t_state> localState   = statePropagatorData_->localState();
102     t_state*                 globalState  = statePropagatorData_->globalState();
103     PaddedHostVector<RVec>*  forcePointer = statePropagatorData_->forcePointer();
104
105     // constant choices for this call to dd_partition_system
106     const bool     verbose       = false;
107     const bool     isMasterState = true;
108     const int      nstglobalcomm = 1;
109     gmx_wallcycle* wcycle        = nullptr;
110
111     // Distribute the charge groups over the nodes from the master node
112     dd_partition_system(fplog_, mdlog_, inputrec_->init_step, cr_, isMasterState, nstglobalcomm,
113                         globalState, topologyHolder_->globalTopology(), inputrec_, imdSession_,
114                         pull_work_, localState.get(), forcePointer, mdAtoms_,
115                         topologyHolder_->localTopology_.get(), fr_, vsite_, constr_, nrnb_, wcycle,
116                         verbose);
117     topologyHolder_->updateLocalTopology();
118     (*checkBondedInteractionsCallback_)();
119     statePropagatorData_->setLocalState(std::move(localState));
120 }
121
122 void DomDecHelper::run(Step step, Time gmx_unused time)
123 {
124     if (step != nextNSStep_ || (step == inputrec_->init_step && inputrec_->bContinuation))
125     {
126         return;
127     }
128     std::unique_ptr<t_state> localState   = statePropagatorData_->localState();
129     t_state*                 globalState  = statePropagatorData_->globalState();
130     PaddedHostVector<RVec>*  forcePointer = statePropagatorData_->forcePointer();
131
132     // constant choices for this call to dd_partition_system
133     const bool verbose = isVerbose_ && (step % verbosePrintInterval_ == 0 || step == inputrec_->init_step);
134     bool isMasterState = false;
135
136     // Correct the new box if it is too skewed
137     if (inputrecDynamicBox(inputrec_))
138     {
139         // TODO: Correcting the box is done here (if using DD) or in ForceElement (non-DD simulations).
140         //       Think about unifying this responsibility, could this be done in one place?
141         if (correct_box(fplog_, step, localState->box))
142         {
143             isMasterState = true;
144         }
145     }
146     if (isMasterState)
147     {
148         dd_collect_state(cr_->dd, localState.get(), globalState);
149     }
150
151     // Distribute the charge groups over the nodes from the master node
152     dd_partition_system(fplog_, mdlog_, step, cr_, isMasterState, nstglobalcomm_, globalState,
153                         topologyHolder_->globalTopology(), inputrec_, imdSession_, pull_work_,
154                         localState.get(), forcePointer, mdAtoms_, topologyHolder_->localTopology_.get(),
155                         fr_, vsite_, constr_, nrnb_, wcycle_, verbose);
156     topologyHolder_->updateLocalTopology();
157     (*checkBondedInteractionsCallback_)();
158     statePropagatorData_->setLocalState(std::move(localState));
159 }
160
161 SignallerCallbackPtr DomDecHelper::registerNSCallback()
162 {
163     return std::make_unique<SignallerCallback>(
164             [this](Step step, Time gmx_unused time) { this->nextNSStep_ = step; });
165 }
166
167 } // namespace gmx