OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ConstraintWriter.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 "io/ConstraintWriter.hpp"
49
50#include <algorithm>
51#include <iostream>
52#include <vector>
53
54#ifdef IS_MPI
55#include <mpi.h>
56#endif
57
58#include "utils/simError.h"
59
60namespace OpenMD {
61 ConstraintWriter::ConstraintWriter(SimInfo* info,
62 const std::string& filename) :
63 info_(info) {
64 // use a primary - secondary model, only the primary node writes
65 // to disk
66#ifdef IS_MPI
67 if (worldRank == 0) {
68#endif
69 output_.open(filename.c_str());
70
71 if (!output_) {
72 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
73 "Could not open %s for Constraint output\n", filename.c_str());
74 painCave.isFatal = 1;
75 simError();
76 }
77
78 output_ << "#time(fs)\t"
79 << "Index of atom 1\t"
80 << "Index of atom 2\tconstraint force" << std::endl;
81
82#ifdef IS_MPI
83 }
84#endif
85 }
86
87 ConstraintWriter::~ConstraintWriter() {
88#ifdef IS_MPI
89 if (worldRank == 0) {
90#endif
91 output_.close();
92#ifdef IS_MPI
93 }
94#endif
95 }
96
97 void ConstraintWriter::writeConstraintForces(
98 const std::list<ConstraintPair*>& constraints) {
99#ifndef IS_MPI
100 std::list<ConstraintPair*>::const_iterator i;
101 for (i = constraints.begin(); i != constraints.end(); ++i) {
102 if ((*i)->getPrintForce()) {
103 output_ << info_->getSnapshotManager()->getCurrentSnapshot()->getTime()
104 << "\t" << (*i)->getConsElem1()->getGlobalIndex() << "\t"
105 << (*i)->getConsElem2()->getGlobalIndex() << "\t"
106 << (*i)->getConstraintForce() << std::endl;
107 }
108 }
109#else
110
111 const int primaryNode = 0;
112 int nproc;
113 int myNode;
114 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
115 MPI_Comm_rank(MPI_COMM_WORLD, &myNode);
116
117 std::vector<int> nConstraints(nproc, 0);
118 nConstraints[myNode] = constraints.size();
119
120 // do MPI_ALLREDUCE to exchange the total number of constraints:
121 MPI_Allreduce(MPI_IN_PLACE, &nConstraints[0], nproc, MPI_INT, MPI_SUM,
122 MPI_COMM_WORLD);
123
124 MPI_Status ierr;
125 int atom1, atom2, doPrint;
126 RealType force;
127
128 if (myNode == primaryNode) {
129 std::vector<ConstraintData> constraintData;
130 ConstraintData tmpData;
131 for (int i = 0; i < nproc; ++i) {
132 if (i == primaryNode) {
133 std::list<ConstraintPair*>::const_iterator j;
134 for (j = constraints.begin(); j != constraints.end(); ++j) {
135 tmpData.atom1 = (*j)->getConsElem1()->getGlobalIndex();
136 tmpData.atom2 = (*j)->getConsElem2()->getGlobalIndex();
137 tmpData.constraintForce = (*j)->getConstraintForce();
138 tmpData.printForce = (*j)->getPrintForce();
139 constraintData.push_back(tmpData);
140 }
141
142 } else {
143 for (int k = 0; k < nConstraints[i]; ++k) {
144 MPI_Recv(&atom1, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &ierr);
145 MPI_Recv(&atom2, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &ierr);
146 MPI_Recv(&force, 1, MPI_REALTYPE, i, 0, MPI_COMM_WORLD, &ierr);
147 MPI_Recv(&doPrint, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &ierr);
148
149 tmpData.atom1 = atom1;
150 tmpData.atom2 = atom2;
151 tmpData.constraintForce = force;
152 tmpData.printForce = (doPrint == 0) ? false : true;
153 constraintData.push_back(tmpData);
154 }
155 }
156 }
157
158 std::vector<ConstraintData>::iterator l;
159 for (l = constraintData.begin(); l != constraintData.end(); ++l) {
160 if (l->printForce) {
161 output_
162 << info_->getSnapshotManager()->getCurrentSnapshot()->getTime()
163 << "\t" << l->atom1 << "\t" << l->atom2 << "\t"
164 << l->constraintForce << std::endl;
165 }
166 }
167 } else {
168 std::list<ConstraintPair*>::const_iterator j;
169 for (j = constraints.begin(); j != constraints.end(); ++j) {
170 int atom1 = (*j)->getConsElem1()->getGlobalIndex();
171 int atom2 = (*j)->getConsElem2()->getGlobalIndex();
172 RealType constraintForce = (*j)->getConstraintForce();
173 int printForce = (int)(*j)->getPrintForce();
174
175 MPI_Send(&atom1, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
176 MPI_Send(&atom2, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
177 MPI_Send(&constraintForce, 1, MPI_REALTYPE, primaryNode, 0,
178 MPI_COMM_WORLD);
179 MPI_Send(&printForce, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
180 }
181 }
182#endif
183 }
184} // namespace OpenMD
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.