votca 2024.2-dev
Loading...
Searching...
No Matches
orientcorr.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2021 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <cstdlib>
20#include <memory>
21
22// VOTCA includes
24
25// Local VOTCA includes
26#include <votca/csg/beadlist.h>
28#include <votca/csg/nblist.h>
30
31using namespace std;
32using namespace votca::csg;
33
35
36 string ProgramName() override { return "orientcorr"; }
37
38 void HelpText(ostream &out) override {
39 out << "Calculates the orientational correlation function\n"
40 "<3/2*u(0)*u(r) - 1/2>\n"
41 "for a polymer melt, where u is the vector pointing along a bond "
42 "and \n"
43 "r the distance between bond segments (centered on middle of "
44 "bond).\n\n"
45 "The output is correlation.dat (with intra-molecular contributions) "
46 "and\n"
47 "correlation_excl.dat, where inter-molecular contributions are "
48 "excluded.";
49 }
50
51 void Initialize() override;
52
53 bool DoTrajectory() override { return true; }
54 // do a threaded analyzis, splitting in time domain
55 bool DoThreaded() override { return true; }
56 // we don't care about the order of the analyzed frames
57 bool SynchronizeThreads() override { return false; }
58
59 // called before groing through all frames
60 void BeginEvaluate(Topology *top, Topology *top_ref) override;
61 // called after all frames were parsed
62 void EndEvaluate() override;
63
64 // creates a worker for a thread
65 std::unique_ptr<CsgApplication::Worker> ForkWorker(void) override;
66 // merge data of worker into main
67 void MergeWorker(Worker *worker) override;
68
69 public:
70 // helper class to choose nbsearch algorithm
71 static std::unique_ptr<NBList> CreateNBSearch();
72
73 protected:
78 static string nbmethod_;
79 double cut_off_;
81};
82
84
85// Earch thread has a worker and analysis data
87 public:
88 ~MyWorker() override = default;
89
90 // evaluate the current frame
91 void EvalConfiguration(Topology *top, Topology *top_ref) override;
92
93 // callback if neighborsearch finds a pair
94 bool FoundPair(Bead *b1, Bead *b2, const Eigen::Vector3d &r,
95 const double dist);
96
97 // accumulator of the 3/2*u(0)u(r) - 1/2
99 // number of hits for each bin
101 // accumulator of the 3/2*u(0)u(r) - 1/2, only inter-molecular
103 // number of hits for each bin, only inter-molecular
105 double cut_off_;
106};
107
108int main(int argc, char **argv) {
109 OrientCorrApp app;
110 return app.Exec(argc, argv);
111}
112
113// add some program options
115 // add all standard application options
117 // some application specific options
118 AddProgramOptions("Neighbor search options")(
119 "cutoff,c",
120 boost::program_options::value<double>(&cut_off_)->default_value(1.0),
121 "cutoff for the neighbor search")(
122 "nbins",
123 boost::program_options::value<votca::Index>(&nbins_)->default_value(40),
124 "number of bins for the grid")(
125 "nbmethod",
126 boost::program_options::value<string>(&nbmethod_)->default_value("grid"),
127 "neighbor search algorithm (simple or "
128 "grid)");
129}
130
131std::unique_ptr<NBList> OrientCorrApp::CreateNBSearch() {
132 if (nbmethod_ == "simple") {
133 return std::make_unique<NBList>();
134 }
135 if (nbmethod_ == "grid") {
136 return std::make_unique<NBListGrid>();
137 }
138
139 throw std::runtime_error(
140 "unknown neighbor search method, use simple or grid");
141 return nullptr;
142}
143
144// initialize the histograms
151
152// creates worker for each thread
153std::unique_ptr<CsgApplication::Worker> OrientCorrApp::ForkWorker() {
154 auto worker = std::make_unique<MyWorker>();
155 worker->cut_off_ = cut_off_;
156 worker->cor_.Initialize(0, worker->cut_off_, nbins_);
157 worker->count_.Initialize(0, worker->cut_off_, nbins_);
158 worker->cor_excl_.Initialize(0, worker->cut_off_, nbins_);
159 worker->count_excl_.Initialize(0, worker->cut_off_, nbins_);
160 return worker;
161}
162
163// evaluates a frame
165
166 // first genearate a mapped topology
167 // the beads are sitting on the bonds and have an orientation which
168 // is pointing along bond direction
169 Topology mapped;
170 cout << "generating mapped topology...";
171
172 // copy box size
173 mapped.setBox(top->getBox());
174
175 // loop over all molecules
176 for (const auto &mol_src : top->Molecules()) {
177 // create a molecule in mapped topology
178 Molecule *mol = mapped.CreateMolecule(mol_src.getName());
179 // loop over beads in molecule
180 for (votca::Index i = 0; i < mol_src.BeadCount() - 1; ++i) {
181 // create a bead in mapped topology
182 string bead_type = "A";
183 if (mapped.BeadTypeExist(bead_type) == false) {
184 mapped.RegisterBeadType(bead_type);
185 }
186 Bead *b =
187 mapped.CreateBead(Bead::ellipsoidal, "A", bead_type, 1, 0.0, 0.0);
188 Eigen::Vector3d p1 = mol_src.getBead(i)->getPos();
189 Eigen::Vector3d p2 = mol_src.getBead(i + 1)->getPos();
190 // position is in middle of bond
191 Eigen::Vector3d pos = 0.5 * (p1 + p2);
192 // orientation pointing along bond
193 Eigen::Vector3d v = p2 - p1;
194 v.normalize();
195 b->setPos(pos);
196 b->setV(v);
197 mol->AddBead(b, "A");
198 }
199 }
200 cout << "done\n";
201
202 // the neighbor search only finds pairs, add self-self correlation parts here
203 cor_.Process(0.0f, (double)mapped.BeadCount());
204 count_.Process(0.0f, (double)mapped.BeadCount());
205
206 // search for all beads
207 BeadList b;
208 b.Generate(mapped, "*");
209
210 // create/initialize neighborsearch
211 std::unique_ptr<NBList> nb = OrientCorrApp::CreateNBSearch();
212 nb->setCutoff(cut_off_);
213
214 // set callback for each pair found
215 nb->SetMatchFunction(this, &MyWorker::FoundPair);
216
217 // execute the search
218 nb->Generate(b);
219}
220
221// process a pair, since return value is falsed, pairs are not cached which
222// saves a lot of memory for the big systems
223bool MyWorker::FoundPair(Bead *b1, Bead *b2, const Eigen::Vector3d &,
224 const double dist) {
225 double tmp = b1->getV().dot(b2->getV());
226 double P2 = 3. / 2. * tmp * tmp - 0.5;
227
228 // calculate average without exclusions
229 cor_.Process(dist, P2);
230 count_.Process(dist);
231
232 if (b1->getMoleculeId() == b2->getMoleculeId()) {
233 return false;
234 }
235
236 // calculate average with excluding intramolecular contributions
237 cor_excl_.Process(dist, P2);
238 count_excl_.Process(dist);
239
240 return false;
241}
242
243// merge analysed data of a worker into main applications
245 MyWorker *myWorker = dynamic_cast<MyWorker *>(worker);
246 cor_.data().y() += myWorker->cor_.data().y();
247 count_.data().y() += myWorker->count_.data().y();
248 cor_excl_.data().y() += myWorker->cor_excl_.data().y();
249 count_excl_.data().y() += myWorker->count_excl_.data().y();
250}
251
252// write out the data
254 cor_.data().y() = cor_.data().y().cwiseQuotient(count_.data().y());
255 cor_.data().Save("correlation.dat");
256
257 cor_excl_.data().y() =
258 cor_excl_.data().y().cwiseQuotient(count_excl_.data().y());
259 cor_excl_.data().Save("correlation_excl.dat");
260}
void EvalConfiguration(Topology *top, Topology *top_ref) override
overload with the actual computation
votca::tools::HistogramNew count_excl_
bool FoundPair(Bead *b1, Bead *b2, const Eigen::Vector3d &r, const double dist)
~MyWorker() override=default
votca::tools::HistogramNew cor_excl_
votca::tools::HistogramNew count_
votca::tools::HistogramNew cor_
Definition orientcorr.cc:98
double cut_off_
string ProgramName() override
program name
Definition orientcorr.cc:36
static string nbmethod_
Definition orientcorr.cc:78
void Initialize() override
Initialize application data.
bool SynchronizeThreads() override
Definition orientcorr.cc:57
void MergeWorker(Worker *worker) override
void HelpText(ostream &out) override
help text of application without version information
Definition orientcorr.cc:38
bool DoThreaded() override
Definition orientcorr.cc:55
void BeginEvaluate(Topology *top, Topology *top_ref) override
called before the first frame
bool DoTrajectory() override
overload and return true to enable trajectory command line options
Definition orientcorr.cc:53
static std::unique_ptr< NBList > CreateNBSearch()
votca::Index nbins_
Definition orientcorr.cc:80
votca::tools::HistogramNew cor_excl_
Definition orientcorr.cc:76
std::unique_ptr< CsgApplication::Worker > ForkWorker(void) override
void EndEvaluate() override
called after the last frame
votca::tools::HistogramNew cor_
Definition orientcorr.cc:74
double cut_off_
Definition orientcorr.cc:79
votca::tools::HistogramNew count_
Definition orientcorr.cc:75
votca::tools::HistogramNew count_excl_
Definition orientcorr.cc:77
virtual const Eigen::Vector3d & getPos() const
Definition basebead.h:166
virtual void setPos(const Eigen::Vector3d &bead_position)
Definition basebead.h:161
Index getMoleculeId() const noexcept
Get the id of the molecule the bead is a part of, if the molecule id has not been set return topology...
Definition basebead.h:78
Index Generate(Topology &top, const std::string &select)
Select all beads of type "select".
Definition beadlist.cc:30
information about a bead
Definition bead.h:50
const Eigen::Vector3d & getV() const
get second orientation vector of bead
Definition bead.h:341
void setV(const Eigen::Vector3d &v)
set second orientation vector of bead
Definition bead.h:336
Worker, derived from Thread, does the work.
void Initialize() override
Initialize application data.
information about molecules
Definition molecule.h:45
void AddBead(Bead *bead, const std::string &name)
Add a bead to the molecule.
Definition molecule.cc:29
topology of the whole system
Definition topology.h:81
void setBox(const Eigen::Matrix3d &box, BoundaryCondition::eBoxtype boxtype=BoundaryCondition::typeAuto)
Definition topology.h:272
bool BeadTypeExist(std::string type) const
Determine if a bead type exists.
Definition topology.cc:210
const Eigen::Matrix3d & getBox() const
Definition topology.h:298
Bead * CreateBead(Bead::Symmetry symmetry, std::string name, std::string type, Index resnr, double m, double q)
Creates a new Bead.
Definition topology.h:441
Index BeadCount() const
Definition topology.h:150
void RegisterBeadType(std::string type)
Register the bead type with the topology object.
Definition topology.cc:214
Molecule * CreateMolecule(std::string name)
Creates a new molecule.
Definition topology.h:449
MoleculeContainer & Molecules()
Definition topology.h:182
int Exec(int argc, char **argv)
executes the program
boost::program_options::options_description_easy_init AddProgramOptions(const std::string &group="")
add option for command line
class to generate histograms
Table & data()
get access to content of histogram
void Initialize(double min, double max, Index nbins)
Initialize the HistogramNew.
void Process(const double &v, double scale=1.0)
process a data point
double & y(Index i)
Definition table.h:45
void Save(std::string filename) const
Definition table.cc:59
STL namespace.
Eigen::Index Index
Definition types.h:26
int main(int argc, char **argv)