OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ZConsWriter.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/ZConsWriter.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 ZConsWriter::ZConsWriter(SimInfo* info, const std::string& filename) :
62 info_(info) {
63 // use a primary - secondary model, only the primary node writes
64 // to disk
65#ifdef IS_MPI
66 if (worldRank == 0) {
67#endif
68 output_.open(filename.c_str());
69
70 if (!output_) {
71 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
72 "Could not open %s for z constrain output_ \n",
73 filename.c_str());
74 painCave.isFatal = 1;
75 simError();
76 }
77
78 output_ << "//time(fs)" << std::endl;
79 output_ << "//number of fixed z-constrain molecules" << std::endl;
80 output_ << "//global Index of molecule\tzconstrain force\tcurrentZPos"
81 << std::endl;
82#ifdef IS_MPI
83 }
84#endif
85 }
86
87 ZConsWriter::~ZConsWriter() {
88#ifdef IS_MPI
89 if (worldRank == 0) {
90#endif
91 output_.close();
92#ifdef IS_MPI
93 }
94#endif
95 }
96
97 void ZConsWriter::writeFZ(const std::list<ZconstraintMol>& fixedZmols) {
98#ifndef IS_MPI
99 output_ << info_->getSnapshotManager()->getCurrentSnapshot()->getTime()
100 << std::endl;
101 output_ << fixedZmols.size() << std::endl;
102
103 std::list<ZconstraintMol>::const_iterator i;
104 for (i = fixedZmols.begin(); i != fixedZmols.end(); ++i) {
105 output_ << i->mol->getGlobalIndex() << "\t" << i->fz << "\t" << i->zpos
106 << "\t" << i->param.zTargetPos << std::endl;
107 }
108#else
109
110 const int primaryNode = 0;
111 int nproc;
112 int myNode;
113 MPI_Comm_size(MPI_COMM_WORLD, &nproc);
114 MPI_Comm_rank(MPI_COMM_WORLD, &myNode);
115
116 std::vector<int> tmpNFixedZmols(nproc, 0);
117 std::vector<int> nFixedZmolsInProc(nproc, 0);
118 tmpNFixedZmols[myNode] = fixedZmols.size();
119
120 // do MPI_ALLREDUCE to exchange the total number of atoms,
121 // rigidbodies and cutoff groups
122 MPI_Allreduce(&tmpNFixedZmols[0], &nFixedZmolsInProc[0], nproc, MPI_INT,
123 MPI_SUM, MPI_COMM_WORLD);
124
125 MPI_Status ierr;
126 int zmolIndex;
127 RealType data[3];
128
129 if (myNode == primaryNode) {
130 std::vector<ZconsData> zconsData;
131 ZconsData tmpData;
132 for (int i = 0; i < nproc; ++i) {
133 if (i == primaryNode) {
134 std::list<ZconstraintMol>::const_iterator j;
135 for (j = fixedZmols.begin(); j != fixedZmols.end(); ++j) {
136 tmpData.zmolIndex = j->mol->getGlobalIndex();
137 tmpData.zforce = j->fz;
138 tmpData.zpos = j->zpos;
139 tmpData.zconsPos = j->param.zTargetPos;
140 zconsData.push_back(tmpData);
141 }
142
143 } else {
144 for (int k = 0; k < nFixedZmolsInProc[i]; ++k) {
145 MPI_Recv(&zmolIndex, 1, MPI_INT, i, 0, MPI_COMM_WORLD, &ierr);
146 MPI_Recv(data, 3, MPI_REALTYPE, i, 0, MPI_COMM_WORLD, &ierr);
147 tmpData.zmolIndex = zmolIndex;
148 tmpData.zforce = data[0];
149 tmpData.zpos = data[1];
150 tmpData.zconsPos = data[2];
151 zconsData.push_back(tmpData);
152 }
153 }
154 }
155
156 output_ << info_->getSnapshotManager()->getCurrentSnapshot()->getTime()
157 << std::endl;
158 output_ << zconsData.size() << std::endl;
159
160 std::vector<ZconsData>::iterator l;
161 for (l = zconsData.begin(); l != zconsData.end(); ++l) {
162 output_ << l->zmolIndex << "\t" << l->zforce << "\t" << l->zpos << "\t"
163 << l->zconsPos << std::endl;
164 }
165
166 } else {
167 std::list<ZconstraintMol>::const_iterator j;
168 for (j = fixedZmols.begin(); j != fixedZmols.end(); ++j) {
169 zmolIndex = j->mol->getGlobalIndex();
170 data[0] = j->fz;
171 data[1] = j->zpos;
172 data[2] = j->param.zTargetPos;
173 MPI_Send(&zmolIndex, 1, MPI_INT, primaryNode, 0, MPI_COMM_WORLD);
174 MPI_Send(data, 3, MPI_REALTYPE, primaryNode, 0, MPI_COMM_WORLD);
175 }
176 }
177#endif
178 }
179} // 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.