Scine::Swoose  2.1.0
This is the SCINE module Swoose.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
DatabaseHelper.h
Go to the documentation of this file.
1 
8 #ifndef MMPARAMETRIZATION_DATABASEHELPER_H
9 #define MMPARAMETRIZATION_DATABASEHELPER_H
10 
11 #include <memory>
12 #include <string>
13 #include <unordered_set>
14 #include <vector>
15 
16 namespace Scine {
17 
18 namespace Core {
19 struct Log;
20 } // namespace Core
21 
22 namespace Utils {
23 class Settings;
24 } // namespace Utils
25 
26 namespace Database {
27 class Manager;
28 class ID;
29 class Collection;
30 class Calculation;
31 } // namespace Database
32 
33 namespace MMParametrization {
34 struct ParametrizationData;
35 
37  public:
41  DatabaseHelper(ParametrizationData& data, const std::shared_ptr<Utils::Settings>& settings, Core::Log& log);
49  void dropDatabase();
55 
56  private:
57  /*
58  * @brief Resets all 'analyzed' calculations in the database to either 'complete' or 'failed' depending on
59  * the present results. Furthermore, this function detects and stores which of the fragments already have
60  * their Hessian calculations (and if needed atomic charges) in the database.
61  */
62  void resetAnalyzedCalculationsAndDetectExistingSubsequentCalculations();
63  /*
64  * @brief Returns whether a given analyzed calculation has failed.
65  * @throws std::runtime_error If given calculations does not have the status 'analyzed'.
66  */
67  bool analyzedCalculationsHasFailed(const Database::Calculation& calculation) const;
68  /*
69  * @brief This function adds the structure optimization calculations and the bond orders calculations
70  * to the corresponding collection in the database.
71  */
72  void addCalculationsToDatabaseForUnoptimizedStructures();
73  /*
74  * @brief This function collects the results of the reference calculations once they are finished.
75  * Furthermore, it submits Hessian and atomic charges calculations if a structure optimizations is done.
76  */
77  void collectResultsAndSubmitSubsequentCalculations();
78  /*
79  * @brief Iterates over all completed calculations in the database and collects the results.
80  * Furthermore, it submits Hessian and atomic charges calculations if a structure optimizations is done.
81  *
82  * @return The number of newly submitted calculations.
83  */
84  int handleCompletedCalculations(std::shared_ptr<Database::Collection> calculations,
85  std::shared_ptr<Database::Collection> structures,
86  std::shared_ptr<Database::Collection> properties);
87  /*
88  * @brief Iterates over all failed calculations in the database and handles those cases.
89  * Furthermore, it submits Hessian and atomic charges calculations if a structure optimizations is done.
90  *
91  * @return The number of newly submitted calculations.
92  */
93  int handleFailedCalculations(std::shared_ptr<Database::Collection> calculations,
94  std::shared_ptr<Database::Collection> structures);
95 
96  // Adds the structures of the subsystems to the corresponding collection in the database.
97  void addStructuresToDatabase();
98  /*
99  * @brief Internally sets the correct name of the Hessian order.
100  */
101  void setNameOfHessianOrder();
102  /*
103  * @brief Internally sets the correct name of the structure optimization order.
104  */
105  void setNameOfStructureOptimizationOrder();
106  /*
107  * @brief Internally sets the correct name of the bond orders order.
108  */
109  void setNameOfBondOrdersOrder();
110  /*
111  * @brief Internally sets the correct name of the atomic charges order.
112  */
113  void setNameOfAtomicChargesOrder();
114  /*
115  * @brief Decides whether atomic charges must be calculated in a separate calculation.
116  */
117  bool mustCalculateAtomicChargesSeparately() const;
118  /*
119  * @brief Fills the vector of priorities for all the calculations (one priority per fragment).
120  */
121  void evaluatePriorities();
122  /*
123  * @brief Returns whether the optimized structure with the given
124  * structure index is valid compared to the unoptimized one.
125  */
126  bool optimizedStructureIsValid(int structureIndex) const;
127  /*
128  * Vector of database IDs corresponding to the structures in the database.
129  */
130  std::vector<std::unique_ptr<Database::ID>> structureIDs_;
131  // Number of subsystems.
132  int numberOfStructures_;
133  // Sleep time in between database operations in seconds
134  int sleepTime_;
135  // Whether to calculate bond orders to refine the initial connectivity.
136  bool refineConnectivity_;
137  // Whether to use Gaussian as a program for calculating the CM5 charges.
138  bool useGaussian_;
139  // Whether to terminate the reference data generation early when enough data is collected
140  bool earlyTerminationEnabled_;
141  /*
142  * Tracks the failed calculations for each fragment in a vector in the following way:
143  * - multiple of 2 if Hessian failed
144  * - multiple of 3 if atomic charges failed
145  * - multiple of 5 if bond orders failed
146  *
147  * This allows to reconstruct which calculations already failed later, to decide whether the parametrization
148  * cannot be completed anymore. Whether or not structure optimization failed is not important to track because
149  * if a structure optimization fails, then the Hessian also fails immediately.
150  */
151  std::vector<int> failedCalculationsScoreForEachFragment_;
152  // Tracks the database IDs of the failed calculations for each fragment.
153  std::vector<std::vector<std::string>> failedCalculationsForEachFragment_;
154  // Whether an existing database is exploited
155  bool reuseDatabase_;
156  // In the case of reusing an existing database, this keeps track of existing Hessian calculations.
157  std::unordered_set<int> fragmentsWithHessianCalculationsInDatabase_;
158  // In the case of reusing an existing database, this keeps track of existing atomic charges calculations.
159  std::unordered_set<int> fragmentsWithAtomicChargesCalculationsInDatabase_;
160  // Reference Program
161  std::string referenceProgram_;
162  // Reference Method
163  std::string referenceMethod_;
164  // Reference Basis Set
165  std::string referenceBasisSet_;
166  // Name of the structure optimization order
167  std::string nameOfStructureOptimizationOrder_;
168  // Name of the Hessian calculation order
169  std::string nameOfHessianOrder_;
170  // Name of the bond orders order
171  std::string nameOfBondOrdersOrder_;
172  // Name of the atomic charges order (empty if no such calculations are performed)
173  std::string nameOfAtomicChargesOrder_;
174  // The vector of priorities of each fragment
175  std::vector<int> priorities_;
176  // The data used within all MM parametrization classes
177  ParametrizationData& data_;
178  // The settings
179  std::shared_ptr<Utils::Settings> settings_;
180  // The database
181  std::unique_ptr<Database::Manager> database_;
182  // The logger.
183  Core::Log& log_;
184 };
185 
186 } // namespace MMParametrization
187 } // namespace Scine
188 
189 #endif // MMPARAMETRIZATION_DATABASEHELPER_H
Definition: DatabaseHelper.h:36
void runCalculationsAndCollectResults()
Runs structure optimizations and other reference calculations (Hessians, charges, bond orders) and sa...
Definition: DatabaseHelper.cpp:95
This struct holds all objects used inside the MM parametrization algorithm.
Definition: ParametrizationData.h:29
DatabaseHelper(ParametrizationData &data, const std::shared_ptr< Utils::Settings > &settings, Core::Log &log)
Constructor.
Definition: DatabaseHelper.cpp:40
void dropDatabase()
Deletes the database.
Definition: DatabaseHelper.cpp:554