OpenMD 3.1
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 appropriate papers when you publish your
33 * work. Good starting points are:
34 *
35 * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
36 * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
37 * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
38 * [4] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
39 * [5] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
40 * [6] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
41 * [7] Lamichhane, Newman & Gezelter, J. Chem. Phys. 141, 134110 (2014).
42 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
43 */
44
45#include <cmath>
46#include <cstdio>
47#include <cstdlib>
48#include <cstring>
49#include <fstream>
50#include <iostream>
51#include <map>
52#include <memory>
53#include <string>
54
56#include "brains/Register.hpp"
57#include "brains/SimCreator.hpp"
58#include "brains/SimInfo.hpp"
59#include "brains/Thermo.hpp"
60#include "brains/Velocitizer.hpp"
61#include "io/DumpReader.hpp"
62#include "io/DumpWriter.hpp"
63#include "thermalizerCmd.hpp"
64#include "utils/StringUtils.hpp"
65
66using namespace OpenMD;
67
68int main(int argc, char* argv[]) {
69 gengetopt_args_info args_info;
70 std::string inputFileName;
71 std::string outputFileName;
72
73 // parse command line arguments
74 if (cmdline_parser(argc, argv, &args_info) != 0) {
75 cmdline_parser_print_help();
76 exit(1);
77 }
78
79 // get input file name
80 if (args_info.input_given) {
81 inputFileName = args_info.input_arg;
82 } else {
83 if (args_info.inputs_num) {
84 inputFileName = args_info.inputs[0];
85 } else {
86 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
87 "No input file name was specified on the command line");
88 painCave.severity = OPENMD_ERROR;
89 painCave.isFatal = 1;
90 simError();
91 }
92 }
93
94 // get output file name
95 outputFileName = args_info.output_arg;
96
97 if (!outputFileName.compare(inputFileName)) {
98 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
99 "Input and Output File names should be different!");
100 painCave.severity = OPENMD_ERROR;
101 painCave.isFatal = 1;
102 simError();
103 }
104
105 // Parse the input file, set up the system, and read the last frame:
106 SimCreator creator;
107 SimInfo* info = creator.createSim(inputFileName, true);
108 // communicate velocity information onto the atoms:
109 info->update();
110
111 // Important utility classes for computing system properties:
112 Thermo thermo(info);
113
114 std::unique_ptr<Velocitizer> veloSet {std::make_unique<Velocitizer>(info)};
115
116 ForceManager* forceMan = new ForceManager(info);
117
118 // Just in case we were passed a system that is on the move:
119 veloSet->removeComDrift();
120 forceMan->calcForces();
121
122 RealType instPE = thermo.getPotential();
123 RealType instKE = thermo.getKinetic();
124
125 // Now that we have the information from the current frame, advance
126 // the snapshot to make a modified frame:
127 info->getSnapshotManager()->advance();
128
129 // Create DumpWriter to hold the modified frame:
130 DumpWriter* writer = new DumpWriter(info, outputFileName);
131 if (writer == NULL) {
132 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
133 "error in creating DumpWriter");
134 painCave.severity = OPENMD_ERROR;
135 painCave.isFatal = 1;
136 simError();
137 }
138
139 // If resampling temperature, we call the randomizer method:
140 if (args_info.temperature_given) {
141 RealType temperature = args_info.temperature_arg;
142
143 if (temperature < 0.0) {
144 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
145 "Temperatures must be positive numbers.");
146 painCave.severity = OPENMD_ERROR;
147 painCave.isFatal = 1;
148 simError();
149 }
150
151 veloSet->randomize(temperature);
152 }
153
154 // If resampling charge temperature, we call the randomizeChargeVelocity
155 // method
156 if (args_info.chargetemperature_given) {
157 RealType charge_temperature = args_info.chargetemperature_arg;
158
159 if (charge_temperature < 0.0) {
160 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
161 "Temperatures must be positive numbers.");
162 painCave.severity = OPENMD_ERROR;
163 painCave.isFatal = 1;
164 simError();
165 }
166
167 veloSet->randomizeChargeVelocity(charge_temperature);
168 }
169
170 // If scaling total energy, scale only the kinetic:
171 if (args_info.energy_given) {
172 RealType energy = args_info.energy_arg;
173 RealType epsilon = 1e-6;
174 RealType lambda = 0.0;
175
176 if (energy < instPE) {
177 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
178 "Energy must be larger than current potential energy.");
179 painCave.severity = OPENMD_ERROR;
180 painCave.isFatal = 1;
181 simError();
182 } else {
183 if (instKE >= epsilon) {
184 lambda = sqrt((energy - instPE) / instKE);
185 veloSet->scale(lambda);
186 }
187 // If the current kinetic energy is close to zero, we will
188 // sample velocities from a 10K distribution and then
189 // subsequently scale from 10K to the desired energy.
190 else {
191 veloSet->randomize(10.0);
192 instKE = thermo.getKinetic();
193 lambda = sqrt((energy - instPE) / instKE);
194 veloSet->scale(lambda);
195 }
196 }
197 }
198
199 writer->writeDump();
200 // deleting the writer will put the closing at the end of the dump file.
201 delete writer;
202
203 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
204 "A new OpenMD file called \"%s\" has been generated.\n",
205 outputFileName.c_str());
206 painCave.isFatal = 0;
207 painCave.severity = OPENMD_INFO;
208 simError();
209 return 0;
210}
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:93
void update()
update
Definition SimInfo.cpp:697
SnapshotManager * getSnapshotManager()
Returns the snapshot manager.
Definition SimInfo.hpp:248
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....