OpenMD 3.0
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
ChargeHistogram.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/*
46 * Calculates average charge profile for selected atom.
47 * Created by Hemanta Bhattarai on 05/10/19.
48 * @author Hemanta Bhattarai
49 */
50
51#include "applications/staticProps/ChargeHistogram.hpp"
52
53#include <algorithm>
54#include <fstream>
55#include <numeric>
56
57#include "io/DumpReader.hpp"
59#include "types/FixedChargeAdapter.hpp"
60#include "types/FluctuatingChargeAdapter.hpp"
61#include "utils/simError.h"
62
63namespace OpenMD {
64
65 ChargeHistogram::ChargeHistogram(SimInfo* info, const std::string& filename,
66 const std::string& sele, int nbins) :
67 StaticAnalyser(info, filename, nbins),
68 selectionScript_(sele), evaluator_(info), seleMan_(info), nBins_(nbins) {
69 evaluator_.loadScriptString(sele);
70 if (!evaluator_.isDynamic()) {
71 seleMan_.setSelectionSet(evaluator_.evaluate());
72 }
73
74 setOutputName(getPrefix(filename) + ".Chargehist");
75 }
76
77 void ChargeHistogram::process() {
78 StuntDouble* sd;
79 int ii;
80
81 if (evaluator_.isDynamic()) {
82 seleMan_.setSelectionSet(evaluator_.evaluate());
83 }
84
85 DumpReader reader(info_, dumpFilename_);
86 int nFrames = reader.getNFrames();
87 nProcessed_ = nFrames / step_;
88 vector<RealType> charge;
89
90 nProcessed_ = nFrames / step_;
91
92 for (int istep = 0; istep < nFrames; istep += step_) {
93 reader.readFrame(istep);
94 currentSnapshot_ = info_->getSnapshotManager()->getCurrentSnapshot();
95
96 for (sd = seleMan_.beginSelected(ii); sd != NULL;
97 sd = seleMan_.nextSelected(ii)) {
98 RealType q = 0.0;
99 Atom* atom = static_cast<Atom*>(sd);
100
101 AtomType* atomType = atom->getAtomType();
102
104 if (fca.isFixedCharge()) { q += fca.getCharge(); }
105
107 if (fqa.isFluctuatingCharge()) { q += atom->getFlucQPos(); }
108
109 charge.push_back(q);
110 }
111 }
112
113 if (charge.empty()) {
114 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
115 "Selected atom not found.\n");
116 painCave.isFatal = 1;
117 simError();
118 }
119
120 std::sort(charge.begin(), charge.end());
121
122 RealType min = charge.front();
123 RealType max = charge.back();
124
125 RealType delta_charge = (max - min) / (nBins_);
126
127 if (delta_charge == 0) {
128 bincenter_.push_back(min);
129 histList_.push_back(1);
130 } else {
131 // fill the center for histogram
132 for (int j = 0; j < nBins_ + 3; ++j) {
133 bincenter_.push_back(min + (j - 1) * delta_charge);
134 histList_.push_back(0);
135 }
136 // filling up the histogram whith the densities
137 int bin_center_pos = 0;
138 vector<RealType>::iterator index;
139 RealType charge_length = static_cast<RealType>(charge.size());
140
141 bool hist_update;
142 for (index = charge.begin(); index < charge.end(); index++) {
143 hist_update = true;
144 while (hist_update) {
145 if (*index >= bincenter_[bin_center_pos] &&
146 *index < bincenter_[bin_center_pos + 1]) {
147 histList_[bin_center_pos] += 1.0 / charge_length;
148 hist_update = false;
149 } else {
150 bin_center_pos++;
151 hist_update = true;
152 }
153 }
154 }
155 }
156 writeCharge();
157 }
158
159 void ChargeHistogram::writeCharge() {
160 std::ofstream rdfStream(outputFilename_.c_str());
161 if (rdfStream.is_open()) {
162 rdfStream << "#Charges for selection\n";
163 rdfStream << "#nFrames:\t" << nProcessed_ << "\n";
164 rdfStream << "#selection: (" << selectionScript_ << ")\n";
165 rdfStream << "#"
166 << "Bin_center"
167 << "\tcount\n";
168 for (unsigned int i = 0; i < histList_.size(); ++i) {
169 rdfStream << bincenter_[i] << "\t" << histList_[i] << "\n";
170 }
171 } else {
172 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
173 "ChargeHistogram: unable to open %s\n", outputFilename_.c_str());
174 painCave.isFatal = 1;
175 simError();
176 }
177 rdfStream.close();
178 }
179} // namespace OpenMD
AtomType is what OpenMD looks to for unchanging data about an atom.
Definition AtomType.hpp:66
bool isDynamic()
Tests if the result from evaluation of script is dynamic.
StuntDouble * nextSelected(int &i)
Finds the next selected StuntDouble in the selection.
StuntDouble * beginSelected(int &i)
Finds the first selected StuntDouble in the selection.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:93
SnapshotManager * getSnapshotManager()
Returns the snapshot manager.
Definition SimInfo.hpp:248
Snapshot * getCurrentSnapshot()
Returns the pointer of current snapshot.
"Don't move, or you're dead! Stand up! Captain, we've got them!"
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)