OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
FluctuatingChargeObjectiveFunction.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 "flucq/FluctuatingChargeObjectiveFunction.hpp"
49
50#ifdef IS_MPI
51#include <mpi.h>
52#endif
53
54namespace OpenMD {
55
56 FluctuatingChargeObjectiveFunction::FluctuatingChargeObjectiveFunction(
57 SimInfo* info, ForceManager* forceMan,
58 FluctuatingChargeConstraints* fqConstraints) :
59 info_(info),
60 forceMan_(forceMan), fqConstraints_(fqConstraints), thermo(info) {}
61
63 const DynamicVector<RealType>& x) {
64 setCoor(x);
65 forceMan_->calcForces();
66 fqConstraints_->applyConstraints();
67 return thermo.getPotential();
68 }
69
72 setCoor(x);
73 forceMan_->calcForces();
74 fqConstraints_->applyConstraints();
75 getGrad(grad);
76 }
77
80 setCoor(x);
81 forceMan_->calcForces();
82 fqConstraints_->applyConstraints();
83 getGrad(grad);
84 return thermo.getPotential();
85 }
86
87 void FluctuatingChargeObjectiveFunction::setCoor(
88 const DynamicVector<RealType>& x) const {
89 SimInfo::MoleculeIterator i;
90 Molecule::FluctuatingChargeIterator j;
91 Molecule* mol;
92 Atom* atom;
93
94 info_->getSnapshotManager()->advance();
95
96 int index;
97#ifdef IS_MPI
98 index = displacements_[myrank_];
99#else
100 index = 0;
101#endif
102
103 for (mol = info_->beginMolecule(i); mol != NULL;
104 mol = info_->nextMolecule(i)) {
105 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
106 atom = mol->nextFluctuatingCharge(j)) {
107 atom->setFlucQPos(x[index++]);
108 }
109 }
110 }
111
112 void FluctuatingChargeObjectiveFunction::getGrad(
114 SimInfo::MoleculeIterator i;
115 Molecule::FluctuatingChargeIterator j;
116 Molecule* mol;
117 Atom* atom;
118 grad.setZero();
119
120 int index;
121#ifdef IS_MPI
122 index = displacements_[myrank_];
123#else
124 index = 0;
125#endif
126
127 for (mol = info_->beginMolecule(i); mol != NULL;
128 mol = info_->nextMolecule(i)) {
129 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
130 atom = mol->nextFluctuatingCharge(j)) {
131 grad[index++] = -atom->getFlucQFrc();
132 }
133 }
134
135#ifdef IS_MPI
136 MPI_Allreduce(MPI_IN_PLACE, &grad[0], nFlucQ_, MPI_REALTYPE, MPI_SUM,
137 MPI_COMM_WORLD);
138#endif
139 }
140
142 FluctuatingChargeObjectiveFunction::setInitialCoords() {
143#ifdef IS_MPI
144 MPI_Comm_size(MPI_COMM_WORLD, &nproc_);
145 MPI_Comm_rank(MPI_COMM_WORLD, &myrank_);
146 std::vector<int> flucqOnProc_(nproc_, 0);
147
148 displacements_.clear();
149 displacements_.resize(nproc_, 0);
150#endif
151
152 SimInfo::MoleculeIterator i;
153 Molecule::FluctuatingChargeIterator j;
154 Molecule* mol;
155 Atom* atom;
156
157 nFlucQ_ = 0;
158
159 for (mol = info_->beginMolecule(i); mol != NULL;
160 mol = info_->nextMolecule(i)) {
161 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
162 atom = mol->nextFluctuatingCharge(j)) {
163 nFlucQ_++;
164 }
165 }
166
167#ifdef IS_MPI
168 MPI_Allgather(&nFlucQ_, 1, MPI_INT, &flucqOnProc_[0], 1, MPI_INT,
169 MPI_COMM_WORLD);
170
171 nFlucQ_ = 0;
172 for (int iproc = 0; iproc < nproc_; iproc++) {
173 nFlucQ_ += flucqOnProc_[iproc];
174 }
175
176 displacements_[0] = 0;
177 for (int iproc = 1; iproc < nproc_; iproc++) {
178 displacements_[iproc] =
179 displacements_[iproc - 1] + flucqOnProc_[iproc - 1];
180 }
181#endif
182
183 DynamicVector<RealType> initCoords(nFlucQ_);
184
185 int index;
186#ifdef IS_MPI
187 index = displacements_[myrank_];
188#else
189 index = 0;
190#endif
191
192 for (mol = info_->beginMolecule(i); mol != NULL;
193 mol = info_->nextMolecule(i)) {
194 for (atom = mol->beginFluctuatingCharge(j); atom != NULL;
195 atom = mol->nextFluctuatingCharge(j)) {
196 initCoords[index++] = atom->getFlucQPos();
197 }
198 }
199
200#ifdef IS_MPI
201 MPI_Allgatherv(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, &initCoords[0],
202 &flucqOnProc_[0], &displacements_[0], MPI_REALTYPE,
203 MPI_COMM_WORLD);
204#endif
205
206 return initCoords;
207 }
208} // namespace OpenMD
Dynamically-sized vector class.
void setZero()
zero out the vector
virtual RealType value(const DynamicVector< RealType > &x)
method to overload to compute the objective function value in x
virtual void gradient(DynamicVector< RealType > &grad, const DynamicVector< RealType > &x)
method to overload to compute grad_f, the first derivative of
virtual RealType valueAndGradient(DynamicVector< RealType > &grad, const DynamicVector< RealType > &x)
method to overload to compute grad_f, the first derivative
ForceManager is responsible for calculating both the short range (bonded) interactions and long range...
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
Molecule * beginMolecule(MoleculeIterator &i)
Returns the first molecule in this SimInfo and intialize the iterator.
Definition SimInfo.cpp:243
Molecule * nextMolecule(MoleculeIterator &i)
Returns the next avaliable Molecule based on the iterator.
Definition SimInfo.cpp:248
SnapshotManager * getSnapshotManager()
Returns the snapshot manager.
Definition SimInfo.hpp:251
void setFlucQPos(RealType charge)
Sets the current fluctuating charge of this stuntDouble.
RealType getFlucQFrc()
Returns the current charge force of this stuntDouble.
RealType getFlucQPos()
Returns the current fluctuating charge of this stuntDouble.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.