votca 2024.2-dev
Loading...
Searching...
No Matches
fluctuations.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009 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// Third party includes
19#include <boost/program_options.hpp>
20
21// VOTCA includes
24
25// Local VOTCA includes
26#include <votca/csg/cgengine.h>
28
29using namespace std;
30using namespace votca::csg;
31
33 string ProgramName() override { return "fluctuations"; }
34 void HelpText(ostream &out) override {
35 out << "calculate density fluctuations in subvolumes of the simulation "
36 "box.";
37 out << "Subolumes can be either cubic slabs in dimensions (x|y|z) or "
38 "spherical";
39 out << "slabs with respect to either the center of box or a reference "
40 "molecule";
41 }
42
43 // some program options are added here
44
45 void Initialize() override {
47 // add program option to pick molecule
48 AddProgramOptions("Fluctuation options")(
49 "filter",
50 boost::program_options::value<string>(&filter_)->default_value("*"),
51 "filter molecule names")("rmax",
52 boost::program_options::value<double>(),
53 "maximal distance to be considered")(
54 "rmin",
55 boost::program_options::value<double>(&rmin_)->default_value(0.0),
56 "minimal distance to be considered")(
57 "refmol",
58 boost::program_options::value<string>(&refmol_)->default_value(""),
59 "Reference molecule")(
60 "nbin",
61 boost::program_options::value<votca::Index>(&nbins_)->default_value(
62 100),
63 "Number of bins")(
64 "geometry", boost::program_options::value<string>(),
65 "(sphere|x|y|z) Take radial or x, y, z slabs from rmin to rmax")(
66 "outfile",
67 boost::program_options::value<string>(&outfilename_)
68 ->default_value("fluctuations.dat"),
69 "Output file");
70 }
71 bool EvaluateOptions() override {
73 CheckRequired("rmax");
74 CheckRequired("geometry");
75 return true;
76 }
77
78 // we want to process a trajectory
79 bool DoTrajectory() override { return true; }
80 bool DoMapping() override { return true; }
81
82 void BeginEvaluate(Topology *top, Topology *) override {
83 filter_ = OptionsMap()["filter"].as<string>();
84 refmol_ = OptionsMap()["refmol"].as<string>();
85 rmin_ = OptionsMap()["rmin"].as<double>();
86 rmax_ = OptionsMap()["rmax"].as<double>();
87 nbins_ = OptionsMap()["nbin"].as<votca::Index>();
88 outfilename_ = OptionsMap()["outfile"].as<string>();
89 geometryinput_ = OptionsMap()["geometry"].as<string>();
90 nframes_ = 0;
91
92 do_spherical_ = false;
93
94 if (geometryinput_ == "sphere") {
95 cout << "Doing spherical slabs" << endl;
96 do_spherical_ = true;
97 } else if (geometryinput_ == "x") {
98 cout << "Doing slabs along x-axis" << endl;
99 dim_ = 0;
100 } else if (geometryinput_ == "y") {
101 cout << "Doing slabs along y-axis" << endl;
102 dim_ = 1;
103 } else if (geometryinput_ == "z") {
104 cout << "Doing slabs along z-axis" << endl;
105 dim_ = 2;
106 } else {
107 throw std::runtime_error("Unrecognized geometry option. (sphere|x|y|z)");
108 }
109
110 N_avg_ = Eigen::VectorXd::Zero(nbins_);
111 N_sq_avg_ = Eigen::VectorXd::Zero(nbins_);
112
113 if (do_spherical_) {
114 cout << "Calculating fluctions for " << rmin_ << "<r<" << rmax_;
115 cout << "using " << nbins_ << " bins" << endl;
116 } else {
117 cout << "Calculating fluctions for " << rmin_ << "<" << geometryinput_
118 << "<" << rmax_;
119 cout << "using " << nbins_ << " bins" << endl;
120 }
121
122 if (refmol_ == "" && do_spherical_) {
123 Eigen::Matrix3d box = top->getBox();
124 ref_ = box.rowwise().sum() / 2;
125
126 cout << "Reference is center of box " << ref_ << endl;
127 }
128
130 if (!outfile_) {
131 throw runtime_error("cannot open" + outfilename_ + " for output");
132 }
133 }
134
135 // write out results in EndEvaluate
136 void EndEvaluate() override;
137 // do calculation in this function
138 void EvalConfiguration(Topology *conf, Topology *top_ref) override;
139
140 protected:
141 // number of particles in dV
143 Eigen::VectorXd N_avg_;
144 // sqare
145 Eigen::VectorXd N_sq_avg_;
146 string filter_;
147 string refmol_;
148 double rmax_;
149 double rmin_;
150 Eigen::Vector3d ref_;
153 ofstream outfile_;
157};
158
159int main(int argc, char **argv) {
160 CsgFluctuations app;
161
162 return app.Exec(argc, argv);
163}
164
166
167 if (refmol_ != "") {
168 for (const auto &bead : conf->Beads()) {
169 if (votca::tools::wildcmp(refmol_, bead.getName())) {
170 ref_ = bead.getPos();
171 cout << " Solute pos " << ref_ << endl;
172 }
173 }
174 }
175
178
179 /* check how many molecules are in each bin*/
180 for (const auto &bead : conf->Beads()) {
181 if (!votca::tools::wildcmp(filter_, bead.getName())) {
182 continue;
183 }
184 double r = 0;
185 if (do_spherical_) {
186 Eigen::Vector3d eR = bead.getPos() - ref_;
187 r = eR.norm();
188 } else {
189 Eigen::Vector3d eR = bead.getPos();
190 if (dim_ == 0) {
191 r = eR.x();
192 } else if (dim_ == 1) {
193 r = eR.y();
194 } else if (dim_ == 2) {
195 r = eR.z();
196 }
197 }
198 hist.Process(r);
199 }
200
201 /* update averages*/
202 N_avg_ += hist.data().y();
203 N_sq_avg_ += hist.data().y().cwiseAbs2();
204
205 nframes_++;
206}
207
208// output everything when processing frames is done
210 cout << "Writing results to " << outfilename_ << endl;
211 outfile_ << "# radius number_fluct avg_number" << endl;
212
213 for (votca::Index i = 0; i < nbins_; i++) {
214 N_avg_[i] /= (double)nframes_;
215 N_sq_avg_[i] /= (double)nframes_;
216 }
217 for (votca::Index i = 0; i < nbins_; i++) {
218 outfile_ << rmin_ + (double)i * (rmax_ - rmin_) / (double)nbins_ << " ";
219 outfile_ << (N_sq_avg_[i] - N_avg_[i] * N_avg_[i]) / N_avg_[i]
220 << " "; // fluctuation
221 outfile_ << N_avg_[i] << endl;
222 }
223}
224
225// add our user program options
bool EvaluateOptions() override
Process command line options.
bool DoMapping() override
overload and return true to enable mapping command line options
bool DoTrajectory() override
overload and return true to enable trajectory command line options
void HelpText(ostream &out) override
help text of application without version information
Eigen::Vector3d ref_
votca::Index nframes_
votca::Index dim_
Eigen::VectorXd N_avg_
Eigen::VectorXd N_sq_avg_
votca::Index nbins_
void Initialize() override
Initialize application data.
void EndEvaluate() override
called after the last frame
void BeginEvaluate(Topology *top, Topology *) override
called before the first frame
void EvalConfiguration(Topology *conf, Topology *top_ref) override
string ProgramName() override
program name
bool EvaluateOptions() override
Process command line options.
void Initialize() override
Initialize application data.
topology of the whole system
Definition topology.h:81
BeadContainer & Beads()
Definition topology.h:169
const Eigen::Matrix3d & getBox() const
Definition topology.h:298
int Exec(int argc, char **argv)
executes the program
boost::program_options::variables_map & OptionsMap()
get available program options & descriptions
boost::program_options::options_description_easy_init AddProgramOptions(const std::string &group="")
add option for command line
void CheckRequired(const std::string &option_name, const std::string &error_msg="")
Check weather required option is set.
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
int main(int argc, char **argv)
STL namespace.
int wildcmp(const char *wild, const char *string)
Definition tokenizer.cc:28
Eigen::Index Index
Definition types.h:26