votca 2026-dev
Loading...
Searching...
No Matches
networkgen.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2026 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Standard includes
21#include <algorithm>
22#include <cmath>
23#include <fstream>
24#include <iomanip>
25#include <numeric>
26#include <sstream>
27
28// VOTCA includes
32
33// Local VOTCA includes
35
36// Local private VOTCA includes
37#include "networkgen.h"
38
39namespace votca {
40namespace xtp {
41
43 statefile_ = options.ifExistsReturnElseReturnDefault<std::string>(
44 ".statefile", job_name_ + ".hdf5");
46 options.ifExistsReturnElseReturnDefault<std::string>(".energyfile", "");
47 parameterfile_ = options.get(".parameters").as<std::string>();
48 sites_ = options.ifExistsReturnElseReturnDefault<Index>(".sites", 0);
49 if (options.exists(".box") &&
50 !options.get(".box").as<std::string>().empty()) {
51 box_ = options.get(".box").as<std::vector<double>>();
52 if (box_.size() != 3) {
53 throw std::runtime_error("networkgen: box needs three lengths");
54 }
55 }
56 seed_ = options.ifExistsReturnElseReturnDefault<Index>(".seed", 0);
57 temperature_ = options.get(".temperature").as<double>();
58 trap_factor_ = options.get(".trapfactor").as<double>();
59}
60
62 log_.setReportLevel(Log::current_level);
63 log_.setMultithreading(true);
64 log_.setCommonPreface("\n... ...");
65
67 throw std::runtime_error("networkgen: " + statefile_ +
68 " exists already, remove it or choose another "
69 "statefile name");
70 }
71 tools::Property file;
74 StochasticNetwork::FromProperty(file.get("stochasticnetwork"));
75 const Index fitted_sites = opt.sites;
76 const Eigen::Vector3d fitted_box = opt.box;
77 if (sites_ > 0) {
78 opt.sites = sites_;
79 }
80 if (!box_.empty()) {
81 opt.box = Eigen::Vector3d(box_[0], box_[1], box_[2]) * tools::conv::nm2bohr;
82 } else if (fitted_sites > 0 && fitted_box.minCoeff() > 0.0) {
83 // the density and aspect ratio of the fitted system
84 opt.box = fitted_box * std::cbrt(double(opt.sites) / double(fitted_sites));
85 } else {
86 throw std::runtime_error(
87 "networkgen: give box, the parameter file has no box and sites");
88 }
89 if (seed_ > 0) {
90 opt.seed = std::uint64_t(seed_);
91 }
93 Eigen::Vector3d box_nm = opt.box * tools::conv::bohr2nm;
95 << "Stochastic network from " << parameterfile_ << ": " << opt.sites
96 << " sites in a box of " << box_nm.x() << " x " << box_nm.y() << " x "
97 << box_nm.z() << " nm^3, seed " << opt.seed << std::flush;
98
99 StochasticNetwork model(opt);
100 Topology top;
101 model.BuildTopology(top);
102
104 << "Point process: " << model.Iterations() << " iterations; "
105 << top.NBList().size() << " pairs, "
106 << 2.0 * double(top.NBList().size()) / double(model.NumberOfSites())
107 << " neighbours per site" << std::flush;
108 if (!opt.traps.empty()) {
109 std::ostringstream t;
110 t << "Traps: " << model.Traps().size() << " inserted (" << opt.traps.size()
111 << " template(s), densities x " << trap_factor_ << ")";
112 for (std::size_t k = 0; k < opt.traps.size(); ++k) {
113 const auto& tr = opt.traps[k];
114 Index count =
115 Index(std::count_if(model.Traps().begin(), model.Traps().end(),
116 [k](const StochasticNetwork::InsertedTrap& x) {
117 return x.type == Index(k);
118 }));
119 t << "\n template " << k << ": "
120 << opt.carriers[tr.carrier].state.ToString() << " trap of "
121 << tr.offsets.size() << " site(s), deepest "
122 << tr.offsets[0][tr.carrier] * tools::conv::hrt2ev
123 << " eV from the bulk mean, density " << tr.density * trap_factor_
124 << " per site: " << count << " inserted";
125 }
126 XTP_LOG(Log::error, log_) << t.str() << std::flush;
127 }
128 const double kT = tools::conv::kB * temperature_; // eV
129 for (std::size_t c = 0; c < opt.carriers.size(); ++c) {
130 const std::vector<double>& e = model.SiteEnergies(Index(c));
131 double mean = std::accumulate(e.begin(), e.end(), 0.0) / double(e.size());
132 double var = std::accumulate(e.begin(), e.end(), 0.0,
133 [mean](double acc, double x) {
134 return acc + (x - mean) * (x - mean);
135 }) /
136 double(e.size());
137 double sigma = std::sqrt(var) * tools::conv::hrt2ev;
138 // Gaussian DOS: occupied states at mean - sigma^2/kT need
139 // (sigma/kT)^2 <= -5.7 + 1.05 ln N (Baumeier et al., PRB 86, 184202)
140 double s2 = (sigma / kT) * (sigma / kT);
141 double nmin = std::exp((s2 + 5.7) / 1.05);
143 << "Carrier " << opt.carriers[c].state.ToString()
144 << ": site energies mean " << mean * tools::conv::hrt2ev
145 << " eV, standard deviation " << sigma << " eV = " << sigma / kT
146 << " kT at " << temperature_ << " K; the density of occupied states "
147 << (double(opt.sites) >= nmin ? "is" : "is NOT")
148 << " sampled (needs about " << std::setprecision(2) << std::scientific
149 << nmin << " sites)" << std::defaultfloat << std::setprecision(6)
150 << std::flush;
151 }
152
153 StateSaver saver(statefile_);
154 saver.WriteFrame(top);
155 XTP_LOG(Log::error, log_) << "Wrote " << statefile_ << std::flush;
156
157 if (!energyfile_.empty()) {
158 std::ofstream out(energyfile_);
159 out << "# segment id, x, y, z [nm], site energies [eV] of";
160 for (const auto& c : opt.carriers) {
161 out << " " << c.state.ToString();
162 }
163 out << "\n" << std::setprecision(12);
164 const std::vector<Eigen::Vector3d>& pos = model.Positions();
165 for (Index i = 0; i < model.NumberOfSites(); ++i) {
166 Eigen::Vector3d p = pos[i] * tools::conv::bohr2nm;
167 out << i << " " << p.x() << " " << p.y() << " " << p.z();
168 for (std::size_t c = 0; c < opt.carriers.size(); ++c) {
169 out << " " << model.SiteEnergies(Index(c))[i] * tools::conv::hrt2ev;
170 }
171 out << "\n";
172 }
174 << "Wrote site energies to " << energyfile_ << std::flush;
175 }
176 return true;
177}
178
179} // namespace xtp
180} // namespace votca
Index size() const
Definition pairlist.h:53
class to manage program options with xml serialization functionality
Definition property.h:55
Property & get(const std::string &key)
get existing property
Definition property.cc:79
bool exists(const std::string &key) const
check whether property exists
Definition property.cc:122
T as() const
return value as type
Definition property.h:283
T ifExistsReturnElseReturnDefault(const std::string &key, T defaultvalue) const
Definition property.h:332
void LoadFromXML(std::string filename)
Definition property.cc:238
std::string statefile_
Definition networkgen.h:49
std::string parameterfile_
Definition networkgen.h:48
void ParseOptions(const tools::Property &options) override
Definition networkgen.cc:42
bool Run() override
Definition networkgen.cc:61
std::string energyfile_
Definition networkgen.h:50
std::vector< double > box_
Definition networkgen.h:52
std::string job_name_
Definition qmtool.h:50
void WriteFrame(const Topology &top)
Definition statesaver.cc:44
Stochastic network model of a molecular charge transport network (Baumeier, Stenzel,...
void BuildTopology(Topology &top) const
fill an empty topology (segments with one pseudo atom, box, pairs)
const std::vector< double > & SiteEnergies(Index c) const
site energies of carrier c (index into Options::carriers)
Index Iterations() const
iterations the point process needed
const std::vector< Eigen::Vector3d > & Positions() const
const std::vector< InsertedTrap > & Traps() const
static Options FromProperty(const tools::Property &p)
Container for segments and box and atoms.
Definition topology.h:41
QMNBList & NBList()
Definition topology.h:70
#define XTP_LOG(level, log)
Definition logger.h:40
const double kB
Definition constants.h:40
const double bohr2nm
Definition constants.h:46
const double hrt2ev
Definition constants.h:53
const double nm2bohr
Definition constants.h:47
bool FileExists(const std::string &filename)
Definition filesystem.cc:50
Charge transport classes.
Definition ERIs.h:28
Provides a means for comparing floating point numbers.
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
static Level current_level
Definition globals.h:30
inserted traps: template index and sites (deepest first)
std::vector< Trap > traps
trap templates, inserted after the Gaussian energies
double trap_factor
multiplies all trap densities (0: no traps)