Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2026 The plumed team 3 : (see the PEOPLE file at the root of the distribution for a list of names) 4 : 5 : See http://www.plumed.org for more information. 6 : 7 : This file is part of plumed, version 2. 8 : 9 : plumed is free software: you can redistribute it and/or modify 10 : it under the terms of the GNU Lesser General Public License as published by 11 : the Free Software Foundation, either version 3 of the License, or 12 : (at your option) any later version. 13 : 14 : plumed 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 17 : GNU Lesser General Public License for more details. 18 : 19 : You should have received a copy of the GNU Lesser General Public License 20 : along with plumed. If not, see <http://www.gnu.org/licenses/>. 21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ 22 : #ifndef __PLUMED_bias_MetaDRestart_h 23 : #define __PLUMED_bias_MetaDRestart_h 24 : 25 : #include <string> 26 : #include <vector> 27 : 28 : namespace PLMD { 29 : 30 : class Action; 31 : class Keywords; 32 : class OFile; 33 : class Value; 34 : 35 : namespace bias { 36 : 37 : class MetaDBiasGrid; 38 : class MetaDHillSchedule; 39 : class MetaDHillsFiles; 40 : 41 : /// The multiple walkers of METAD and PBMETAD: they share their hills through the hills files (WALKERS_N), each 42 : /// walker reading the files of the others every WALKERS_RSTRIDE steps, or through MPI (WALKERS_MPI), the walkers 43 : /// being the replicas, which all add the hills of all of them and write them in the same files. 44 368 : struct MetaDWalkers { 45 : /// number of walkers sharing hills through files (WALKERS_N) 46 : int n=1; 47 : /// directory with the hills files (WALKERS_DIR; with WALKERS_MPI the directory of the first replica, if not given) 48 : std::string dir; 49 : /// id of this walker (WALKERS_ID) 50 : int id=0; 51 : /// stride for reading the hills files of the other walkers (WALKERS_RSTRIDE) 52 : int rstride=1; 53 : /// MPI version (WALKERS_MPI) 54 : bool mpi=false; 55 : /// number of MPI walkers, and index of this one (the replica index) 56 : unsigned mpi_nw=0; 57 : unsigned mpi_id=0; 58 : static void registerKeywords(Keywords& keys); 59 : /// parse the WALKERS_* keywords 60 : void parse(Action& action); 61 : /// check the keywords, write them on the log, and set the MPI walkers 62 : void setup(Action& action); 63 : /// the walkers share files (hills and grids), which are written and read without the suffix of the replica 64 : bool shareFiles() const { 65 982 : return n>1 || mpi; 66 : } 67 : /// with WALKERS_MPI the shared files are written by the first walker only 68 : bool writesFiles() const { 69 657 : return !(mpi && mpi_id>0); 70 : } 71 : /// with WALKERS_N, the steps when the hills files of the other walkers are read (and the own one flushed) 72 : bool isReadStep(long long int step) const { 73 127737 : return n>1 && step%rstride==0; 74 : } 75 : /// the name of the file of a walker: name.walker with WALKERS_N, in dir if given (WALKERS_N or WALKERS_MPI) 76 : std::string getFileName(const std::string& name, int walker) const; 77 : /// the current directory of the first replica, so that all the walkers use the same files (the replicas 78 : /// can run in different directories); to be called by all the processes of all the replicas 79 : std::string firstReplicaDirectory(Action& action) const; 80 : /// with WALKERS_MPI and RESTART, an error if the walkers did not all make the same choice (e.g. reading a grid); 81 : /// to be called by all the processes of all the replicas 82 : void checkSameChoice(Action& action, bool choice, const std::string& what) const; 83 : /// open a file written by this walker (hills or grid): shared files are opened without the suffix of the replica, 84 : /// and the walkers that do not write them write on /dev/null 85 : void openOutput(Action& action, OFile& file, const std::string& name) const; 86 : }; 87 : 88 : /// Read the bias from a grid file (METAD: GRID_RFILE, PBMETAD: one of GRID_RFILES), on the lattice of the grid given 89 : /// in input (see MetaDBiasGrid::read). With WALKERS_MPI, a relative path is in the directory of the first replica, so 90 : /// that all the walkers read the same file. Returns the numbers of hills of the other walkers in the grid (WALKERS_N, 91 : /// see MetaDHillsFiles::getGridComment), or an empty vector. 92 : std::vector<long long> readBiasGrid(Action& action, const MetaDWalkers& walkers, MetaDBiasGrid& grid, const std::string& filename, 93 : const std::string& funcl, const std::vector<Value*>& args, const std::vector<std::string>& gmin, 94 : const std::vector<std::string>& gmax, const std::vector<unsigned>& gbin, bool sparse, bool spline, 95 : bool nobounds); 96 : 97 : /// The hills files of a bias when the action starts (METAD: one, PBMETAD: one for each family) 98 914 : struct MetaDHillsInput { 99 : /// the hills files, with the bias to which their hills are added (see MetaDHillsFiles::setBias) 100 : MetaDHillsFiles* files=nullptr; 101 : /// the name of the hills file (FILE) 102 : std::string name; 103 : /// the numbers of hills of the other walkers in the grid read from file (see readBiasGrid) 104 : std::vector<long long> gridWalkerHills; 105 : }; 106 : 107 : /// Open the hills files of all the walkers when the action starts, and read them: 108 : /// - with RESTART, the hills of all the files are added to the bias (a missing file is an error); 109 : /// - with RESTART and the bias read from grid files, the hills files of this walker are scanned, for the time of the 110 : /// last hill and for the statistics of ADAPTIVE=DIFF, and the hills of the other walkers that are not in the grids 111 : /// are added (WALKERS_N: their numbers are in the grid files; for older grid files, those up to the last hill of 112 : /// this walker are assumed to be in the grids). 113 : /// The time of the last hill of the files of this walker is given to schedule (with WALKERS_MPI, the same for all the 114 : /// walkers). Then the incomplete last line of these files (an interrupted run) is removed, and they are closed, to be 115 : /// written. With WALKERS_MPI and no WALKERS_DIR, the files with a relative path are in the directory of the first 116 : /// replica (walkers.dir is set). To be called by all the processes of all the replicas. 117 : void readHillsAtStart(Action& action, MetaDWalkers& walkers, const std::vector<MetaDHillsInput>& inputs, bool restartedFromGrid, 118 : MetaDHillSchedule& schedule); 119 : 120 : /// WALKERS_N: add the hills that the other walkers have written in their files since the last read 121 : /// (the files that did not exist yet are opened, if they exist now) 122 : void readOtherWalkersHills(Action& action, const MetaDWalkers& walkers, const std::vector<MetaDHillsFiles*>& files); 123 : 124 : } 125 : } 126 : 127 : #endif