votca 2025.1-dev
Loading...
Searching...
No Matches
density.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2025 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// VOTCA includes
22
23// Local VOTCA includes
25
26using namespace std;
27using namespace votca::csg;
28
30 string ProgramName() override { return "csg_density"; }
31 void HelpText(ostream &out) override {
32 out << "Calculates the mass density distribution along a box axis or "
33 "radial density profile from reference point";
34 }
35
36 // some program options are added here
37 void Initialize() override;
38
39 // we want to process a trajectory
40 bool DoTrajectory() override { return true; }
41 bool DoMapping() override { return true; }
42 bool DoMappingDefault(void) override { return false; }
43
44 // write out results in EndEvaluate
45 void EndEvaluate() override;
46 void BeginEvaluate(Topology *top, Topology *top_atom) override;
47 void EvalConfiguration(Topology *top, Topology *top_ref) override;
48
49 bool EvaluateOptions() override {
51 CheckRequired("out", "no output topology specified");
52 CheckRequired("trj", "no trajectory file specified");
53 return true;
54 };
55
56 protected:
57 string filter_, out_;
59 string dens_type_;
60 double rmax_;
62 double scale_;
63 double step_;
67 Eigen::Vector3d ref_;
68 Eigen::Vector3d axis_;
69 string axisname_;
70 string molname_;
71 double area_;
72 void WriteDensity(votca::Index nframes, const string &suffix = "");
73};
74
75int main(int argc, char **argv) {
76 CsgDensityApp app;
77 return app.Exec(argc, argv);
78}
79
81
82 Eigen::Matrix3d box = top->getBox();
83 Eigen::Vector3d a = box.col(0);
84 Eigen::Vector3d b = box.col(1);
85 Eigen::Vector3d c = box.col(2);
86
87 dist_.setPeriodic(true);
88 axis_ = Eigen::Vector3d::Zero();
89 area_ = 0;
90 if (axisname_ == "x") {
91 axis_ = Eigen::Vector3d::UnitX();
92 rmax_ = a.norm();
93 area_ = b.cross(c).norm();
94 } else if (axisname_ == "y") {
95 axis_ = Eigen::Vector3d::UnitY();
96 rmax_ = b.norm();
97 area_ = a.cross(c).norm();
98 } else if (axisname_ == "z") {
99 axis_ = Eigen::Vector3d::UnitZ();
100 rmax_ = c.norm();
101 area_ = a.cross(b).norm();
102 } else if (axisname_ == "r") {
103 dist_.setPeriodic(false);
104 rmax_ = min(min((a / 2).norm(), (b / 2).norm()), (c / 2).norm());
105 } else {
106 throw std::runtime_error("unknown axis type");
107 }
108
109 if (OptionsMap().count("rmax")) {
110 rmax_ = OptionsMap()["rmax"].as<double>();
111 }
112
113 if (OptionsMap().count("block-length")) {
114 block_length_ = OptionsMap()["block-length"].as<votca::Index>();
115 } else {
116 block_length_ = 0;
117 }
118
119 if (axisname_ == "r") {
120 if (!OptionsMap().count("ref")) {
121 ref_ = a / 2 + b / 2 + c / 2;
122 }
123 cout << "Using referece point: " << ref_ << endl;
124 } else if (OptionsMap().count("ref")) {
125 throw std::runtime_error(
126 "reference center can only be used in case of spherical density");
127 }
128
129 nbin_ = (votca::Index)floor(rmax_ / step_);
130 dist_.Initialize(0, rmax_, nbin_);
131
132 cout << "rmax: " << rmax_ << endl;
133 cout << "axis: " << axisname_ << endl;
134 cout << "Bins: " << nbin_ << endl;
135 frames_ = 0;
136 nblock_ = 0;
137}
138
140 // loop over all molecules
141 bool did_something = false;
142 for (const auto &mol : top->Molecules()) {
143 if (!votca::tools::wildcmp(molname_, mol.getName())) {
144 continue;
145 }
146 votca::Index N = mol.BeadCount();
147 for (votca::Index i = 0; i < N; i++) {
148 const Bead *b = mol.getBead(i);
150 continue;
151 }
152 double r;
153 if (axisname_ == "r") {
154 r = (top->BCShortestConnection(ref_, b->getPos()).norm());
155 } else {
156 r = b->getPos().dot(axis_);
157 }
158 if (dens_type_ == "mass") {
159 dist_.Process(r, b->getMass());
160 } else {
161 dist_.Process(r, 1.0);
162 }
163 did_something = true;
164 }
165 }
166 frames_++;
167 if (!did_something) {
168 throw std::runtime_error("No molecule in selection");
169 }
170 if (block_length_ != 0) {
171 if ((nframes_ % block_length_) == 0) {
172 nblock_++;
173 string suffix = string("_") + boost::lexical_cast<string>(nblock_);
175 dist_.Clear();
176 }
177 }
178}
179
180// output everything when processing frames is done
181void CsgDensityApp::WriteDensity(votca::Index nframes, const string &suffix) {
182 if (axisname_ == "r") {
183 dist_.data().y() =
184 scale_ /
185 (double(nframes) * rmax_ / (double)nbin_ * 4 * votca::tools::conv::Pi) *
186 dist_.data().y().cwiseQuotient(dist_.data().x().cwiseAbs2());
187
188 } else {
189 dist_.data().y() = scale_ /
190 ((double)nframes * area_ * rmax_ / (double)nbin_) *
191 dist_.data().y();
192 }
193 dist_.data().Save(out_ + suffix);
194}
195
196namespace Eigen {
197std::istream &operator>>(std::istream &in, Vector3d &v) {
198 char c;
199 in.get(c);
200 if (c != '[') {
201 throw std::runtime_error("error, invalid character in vector string");
202 }
203
204 std::string str;
205 while (in.good()) {
206 in.get(c);
207 if (c == ']') { // found end of vector
208 std::vector<double> d =
209 votca::tools::Tokenizer(str, ",").ToVector<double>();
210 if (d.size() != 3) {
211 throw std::runtime_error("error, invalid number of entries in vector");
212 }
213 v.x() = d[0];
214 v.y() = d[1];
215 v.z() = d[2];
216 return in;
217 }
218 str += c;
219 }
220 throw std::runtime_error(
221 "did not find closing bracket in string to vec conversion");
222
223 return in;
224}
225} // namespace Eigen
226
228 if (block_length_ == 0) {
230 }
231}
232
233// add our user program options
236 // add program option to pick molecule
237 AddProgramOptions("Specific options:")(
238 "type",
239 boost::program_options::value<string>(&dens_type_)->default_value("mass"),
240 "density type: mass or number")(
241 "axis",
242 boost::program_options::value<string>(&axisname_)->default_value("r"),
243 "[x|y|z|r] density axis (r=spherical)")(
244 "step",
245 boost::program_options::value<double>(&step_)->default_value(0.01),
246 "spacing of density")("block-length",
247 boost::program_options::value<votca::Index>(),
248 " write blocks of this length, the averages are "
249 "cleared after every write")(
250 "out", boost::program_options::value<string>(&out_), "Output file")(
251 "rmax", boost::program_options::value<double>(),
252 "rmax (default for [r] =min of all box vectors/2, else l )")(
253 "scale",
254 boost::program_options::value<double>(&scale_)->default_value(1.0),
255 "scale factor for the density")(
256 "molname",
257 boost::program_options::value<string>(&molname_)->default_value("*"),
258 "molname")(
259 "filter",
260 boost::program_options::value<string>(&filter_)->default_value("*"),
261 "filter bead names")(
262 "ref", boost::program_options::value<Eigen::Vector3d>(&ref_),
263 "reference zero point");
264}
void Initialize() override
Initialize application data.
Definition density.cc:234
void EvalConfiguration(Topology *top, Topology *top_ref) override
Definition density.cc:139
votca::Index nblock_
Definition density.cc:65
void HelpText(ostream &out) override
help text of application without version information
Definition density.cc:31
Eigen::Vector3d axis_
Definition density.cc:68
Eigen::Vector3d ref_
Definition density.cc:67
void WriteDensity(votca::Index nframes, const string &suffix="")
Definition density.cc:181
bool EvaluateOptions() override
Process command line options.
Definition density.cc:49
string filter_
Definition density.cc:57
string ProgramName() override
program name
Definition density.cc:30
double step_
Definition density.cc:63
string molname_
Definition density.cc:70
string dens_type_
Definition density.cc:59
bool DoTrajectory() override
overload and return true to enable trajectory command line options
Definition density.cc:40
void BeginEvaluate(Topology *top, Topology *top_atom) override
called before the first frame
Definition density.cc:80
string axisname_
Definition density.cc:69
bool DoMappingDefault(void) override
if DoMapping is true, will by default require mapping or not
Definition density.cc:42
void EndEvaluate() override
called after the last frame
Definition density.cc:227
bool DoMapping() override
overload and return true to enable mapping command line options
Definition density.cc:41
double area_
Definition density.cc:71
votca::tools::Histogram dist_
Definition density.cc:58
double rmax_
Definition density.cc:60
votca::Index frames_
Definition density.cc:64
string out_
Definition density.cc:57
votca::Index block_length_
Definition density.cc:66
double scale_
Definition density.cc:62
votca::Index nbin_
Definition density.cc:61
virtual const Eigen::Vector3d & getPos() const
Definition basebead.h:166
std::string getName() const
Gets the name of the bead.
Definition basebead.h:58
virtual const double & getMass() const noexcept
Definition basebead.h:104
information about a bead
Definition bead.h:50
bool EvaluateOptions() override
Process command line options.
void Initialize() override
Initialize application data.
topology of the whole system
Definition topology.h:81
const Eigen::Matrix3d & getBox() const
Definition topology.h:298
Eigen::Vector3d BCShortestConnection(const Eigen::Vector3d &r_i, const Eigen::Vector3d &r_j) const
calculate shortest vector connecting two points
Definition topology.cc:238
MoleculeContainer & Molecules()
Definition topology.h:182
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
Definition histogram.h:77
break string into words
Definition tokenizer.h:72
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
int main(int argc, char **argv)
Definition density.cc:75
std::istream & operator>>(std::istream &in, Vector3d &v)
Definition density.cc:197
STL namespace.
const double Pi
Definition constants.h:36
int wildcmp(const char *wild, const char *string)
Definition tokenizer.cc:28
Eigen::Index Index
Definition types.h:26