OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
thermalizer.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
48#include <cmath>
49#include <cstdio>
50#include <cstdlib>
51#include <cstring>
52#include <fstream>
53#include <iostream>
54#include <map>
55#include <memory>
56#include <string>
57
59#include "brains/Register.hpp"
60#include "brains/SimCreator.hpp"
61#include "brains/SimInfo.hpp"
62#include "brains/Thermo.hpp"
63#include "brains/Velocitizer.hpp"
64#include "io/DumpReader.hpp"
65#include "io/DumpWriter.hpp"
66#include "thermalizerCmd.hpp"
67#include "utils/StringUtils.hpp"
68
69using namespace OpenMD;
70
71int main(int argc, char* argv[]) {
72 gengetopt_args_info args_info;
73 std::string inputFileName;
74 std::string outputFileName;
75
76 // parse command line arguments
77 if (cmdline_parser(argc, argv, &args_info) != 0) {
79 exit(1);
80 }
81
82 // get input file name
83 if (args_info.input_given) {
84 inputFileName = args_info.input_arg;
85 } else {
86 if (args_info.inputs_num) {
87 inputFileName = args_info.inputs[0];
88 } else {
89 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
90 "No input file name was specified on the command line");
91 painCave.severity = OPENMD_ERROR;
92 painCave.isFatal = 1;
93 simError();
94 }
95 }
96
97 // get output file name
98 outputFileName = args_info.output_arg;
99
100 if (!outputFileName.compare(inputFileName)) {
101 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
102 "Input and Output File names should be different!");
103 painCave.severity = OPENMD_ERROR;
104 painCave.isFatal = 1;
105 simError();
106 }
107
108 // Parse the input file, set up the system, and read the last frame:
109 SimCreator creator;
110 SimInfo* info = creator.createSim(inputFileName, true);
111
112 // Important utility classes for computing system properties:
113 Thermo thermo(info);
114
115 std::unique_ptr<Velocitizer> veloSet {std::make_unique<Velocitizer>(info)};
116
117 ForceManager* forceMan = new ForceManager(info);
118
119 // Just in case we were passed a system that is on the move:
120 veloSet->removeComDrift();
121 forceMan->calcForces();
122
123 RealType instPE = thermo.getPotential();
124 RealType instKE = thermo.getKinetic();
125
126 // Now that we have the information from the current frame, advance
127 // the snapshot to make a modified frame:
128 info->getSnapshotManager()->advance();
129
130 // Create DumpWriter to hold the modified frame:
131 DumpWriter* writer = new DumpWriter(info, outputFileName);
132 if (writer == NULL) {
133 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
134 "error in creating DumpWriter");
135 painCave.severity = OPENMD_ERROR;
136 painCave.isFatal = 1;
137 simError();
138 }
139
140 // If resampling temperature, we call the randomizer method:
141 if (args_info.temperature_given) {
142 RealType temperature = args_info.temperature_arg;
143
144 if (temperature < 0.0) {
145 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
146 "Temperatures must be positive numbers.");
147 painCave.severity = OPENMD_ERROR;
148 painCave.isFatal = 1;
149 simError();
150 }
151
152 veloSet->randomize(temperature);
153 }
154
155 // If resampling charge temperature, we call the randomizeChargeVelocity
156 // method
157 if (args_info.chargetemperature_given) {
158 RealType charge_temperature = args_info.chargetemperature_arg;
159
160 if (charge_temperature < 0.0) {
161 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
162 "Temperatures must be positive numbers.");
163 painCave.severity = OPENMD_ERROR;
164 painCave.isFatal = 1;
165 simError();
166 }
167
168 veloSet->randomizeChargeVelocity(charge_temperature);
169 }
170
171 // If scaling total energy, scale only the kinetic:
172 if (args_info.energy_given) {
173 RealType energy = args_info.energy_arg;
174 RealType epsilon = 1e-6;
175 RealType lambda = 0.0;
176
177 if (energy < instPE) {
178 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
179 "Energy must be larger than current potential energy.");
180 painCave.severity = OPENMD_ERROR;
181 painCave.isFatal = 1;
182 simError();
183 } else {
184 if (instKE >= epsilon) {
185 lambda = sqrt((energy - instPE) / instKE);
186 veloSet->scale(lambda);
187 }
188 // If the current kinetic energy is close to zero, we will
189 // sample velocities from a 10K distribution and then
190 // subsequently scale from 10K to the desired energy.
191 else {
192 veloSet->randomize(10.0);
193 instKE = thermo.getKinetic();
194 lambda = sqrt((energy - instPE) / instKE);
195 veloSet->scale(lambda);
196 }
197 }
198 }
199
200 writer->writeDump();
201 // deleting the writer will put the closing at the end of the dump file.
202 delete writer;
203
204 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
205 "A new OpenMD file called \"%s\" has been generated.\n",
206 outputFileName.c_str());
207 painCave.isFatal = 0;
208 painCave.severity = OPENMD_INFO;
209 simError();
210 return 0;
211}
void cmdline_parser_print_help(void)
Print the help.
ForceManager is responsible for calculating both the short range (bonded) interactions and long range...
The only responsibility of SimCreator is to parse the meta-data file and create a SimInfo instance ba...
SimInfo * createSim(const std::string &mdFileName, bool loadInitCoords=true)
Setup Simulation.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
SnapshotManager * getSnapshotManager()
Returns the snapshot manager.
Definition SimInfo.hpp:251
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Where the command line options are stored.
double chargetemperature_arg
charge temperature (K).
unsigned int chargetemperature_given
Whether chargetemperature was given.
unsigned inputs_num
unamed options number
unsigned int energy_given
Whether energy was given.
char * output_arg
output file name.
char ** inputs
unamed options (options without names)
char * input_arg
input dump file.
double temperature_arg
temperature (in Kelvin (default='300').
Definition HydroCmd.hpp:70
double energy_arg
energy (kcal/mol).
unsigned int temperature_given
Whether temperature was given.
Definition HydroCmd.hpp:85
unsigned int input_given
Whether input was given.
The header file for the command line option parser generated by GNU Gengetopt version 2....