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