55#include "applications/dynamicProps/EnergyCorrFunc.hpp"
56#include "utils/Constants.hpp"
57#include "utils/Revision.hpp"
59#include "brains/Thermo.hpp"
65 EnergyCorrFunc::EnergyCorrFunc(
SimInfo* info,
const std::string& filename,
66 const std::string& sele1,
67 const std::string& sele2,
68 long long int memSize)
77 setCorrFuncType(
"EnergyCorrFunc");
78 setOutputName(
getPrefix(dumpFilename_) +
".moment");
79 histogram_.resize(nTimeBins_);
80 count_.resize(nTimeBins_);
83 void EnergyCorrFunc::correlateFrames(
int frame1,
int frame2) {
84 SimInfo::MoleculeIterator mi1;
85 SimInfo::MoleculeIterator mi2;
88 Molecule::AtomIterator ai1;
89 Molecule::AtomIterator ai2;
92 std::vector<RealType> particleEnergies1;
93 std::vector<RealType> particleEnergies2;
94 std::vector<Vector3d> atomPositions1;
95 std::vector<Vector3d> atomPositions2;
96 int thisAtom1, thisAtom2;
98 Snapshot* snapshot1 = bsMan_->getSnapshot(frame1);
99 Snapshot* snapshot2 = bsMan_->getSnapshot(frame2);
100 assert(snapshot1 && snapshot2);
102 RealType time1 = snapshot1->getTime();
103 RealType time2 = snapshot2->getTime();
105 int timeBin = int ((time2 - time1) /deltaTime_ + 0.5);
109 particleEnergies1 = E_a_[frame1];
110 particleEnergies2 = E_a_[frame2];
112 atomPositions1.clear();
113 for (mol1 = info_->beginMolecule(mi1); mol1 != NULL;
114 mol1 = info_->nextMolecule(mi1)) {
115 for(atom1 = mol1->beginAtom(ai1); atom1 != NULL;
116 atom1 = mol1->nextAtom(ai1)) {
117 atomPositions1.push_back(atom1->
getPos(frame1));
121 atomPositions2.clear();
122 for (mol2 = info_->beginMolecule(mi2); mol2 != NULL;
123 mol2 = info_->nextMolecule(mi2)) {
124 for(atom2 = mol2->beginAtom(ai2); atom2 != NULL;
125 atom2 = mol2->nextAtom(ai2)) {
126 atomPositions2.push_back(atom2->
getPos(frame2));
132 for (mol1 = info_->beginMolecule(mi1); mol1 != NULL;
133 mol1 = info_->nextMolecule(mi1)) {
134 for(atom1 = mol1->beginAtom(ai1); atom1 != NULL;
135 atom1 = mol1->nextAtom(ai1)) {
137 Vector3d r1 = atomPositions1[thisAtom1];
138 RealType energy1 = particleEnergies1[thisAtom1] - AvgE_a_[thisAtom1];
142 for (mol2 = info_->beginMolecule(mi2); mol2 != NULL;
143 mol2 = info_->nextMolecule(mi2)) {
144 for(atom2 = mol2->beginAtom(ai2); atom2 != NULL;
145 atom2 = mol2->nextAtom(ai2)) {
147 Vector3d r2 = atomPositions2[thisAtom2];
148 RealType energy2 = particleEnergies2[thisAtom2] - AvgE_a_[thisAtom2];
151 RealType Eprod = energy2*energy1;
153 histogram_[timeBin][0] += deltaPos.
x()*deltaPos.
x() * Eprod;
154 histogram_[timeBin][1] += deltaPos.
y()*deltaPos.
y() * Eprod;
155 histogram_[timeBin][2] += deltaPos.
z()*deltaPos.
z() * Eprod;
169 void EnergyCorrFunc::postCorrelate() {
170 for (
unsigned int i =0 ; i < nTimeBins_; ++i) {
172 histogram_[i] /= count_[i];
177 void EnergyCorrFunc::preCorrelate() {
179 std::fill(histogram_.begin(), histogram_.end(), 0.0);
181 std::fill(count_.begin(), count_.end(), 0);
183 SimInfo::MoleculeIterator mi;
185 Molecule::AtomIterator ai;
187 std::vector<RealType > particleEnergies;
193 int nblocks = bsMan_->getNBlocks();
194 bool firsttime =
true;
195 for (
int i = 0; i < nblocks; ++i) {
196 bsMan_->loadBlock(i);
197 assert(bsMan_->isBlockActive(i));
198 SnapshotBlock block1 = bsMan_->getSnapshotBlock(i);
199 for (
int j = block1.first; j < block1.second; ++j) {
203 forceMan->calcForces();
207 for (mol = info_->beginMolecule(mi); mol != NULL;
208 mol = info_->nextMolecule(mi)) {
209 for(atom = mol->beginAtom(ai); atom != NULL;
210 atom = mol->nextAtom(ai)) {
211 RealType mass = atom->
getMass();
213 RealType kinetic = mass * (vel[0]*vel[0] + vel[1]*vel[1] +
214 vel[2]*vel[2]) / Constants::energyConvert;
216 RealType eatom = (kinetic + potential)/2.0;
217 particleEnergies.push_back(eatom);
220 AvgE_a_.push_back(eatom);
223 AvgE_a_[index] += eatom;
229 E_a_.push_back(particleEnergies);
232 bsMan_->unloadBlock(i);
235 int nframes = bsMan_->getNFrames();
236 for (
unsigned int i = 0; i < AvgE_a_.size(); i++){
237 AvgE_a_[i] /= nframes;
242 void EnergyCorrFunc::writeCorrelate() {
243 std::ofstream ofs(getOutputFileName().c_str());
248 ofs <<
"# " << getCorrFuncType() <<
"\n";
249 ofs <<
"# OpenMD " << r.getFullRevision() <<
"\n";
250 ofs <<
"# " << r.getBuildDate() <<
"\n";
251 ofs <<
"# selection script1: \"" << selectionScript1_ ;
252 ofs <<
"\"\tselection script2: \"" << selectionScript2_ <<
"\"\n";
253 if (!paramString_.empty())
254 ofs <<
"# parameters: " << paramString_ <<
"\n";
255 ofs <<
"#time\tK_x\tK_y\tK_z\n";
257 for (
unsigned int i = 0; i < nTimeBins_; ++i) {
258 ofs << time_[i] <<
"\t" <<
259 histogram_[i].x() <<
"\t" <<
260 histogram_[i].y() <<
"\t" <<
261 histogram_[i].z() <<
"\t" <<
"\n";
265 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
266 "EnergyCorrFunc::writeCorrelate Error: fail to open %s\n",
267 getOutputFileName().c_str());
268 painCave.isFatal = 1;
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...
The Snapshot class is a repository storing dynamic data during a Simulation.
Vector3d getVel()
Returns the current velocity of this stuntDouble.
RealType getMass()
Returns the mass of this stuntDouble.
Vector3d getPos()
Returns the current position of this stuntDouble.
RealType getParticlePot()
Returns the current particlePot of this stuntDouble.
Real & z()
Returns reference of the third element of Vector3.
Real & x()
Returns reference of the first element of Vector3.
Real & y()
Returns reference of the second element of Vector3.
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
std::string getPrefix(const std::string &str)