votca 2024.1-dev
Loading...
Searching...
No Matches
csg_imc_solve.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 <fstream>
20
21// VOTCA includes
22#include <votca/tools/table.h>
23
24// Local VOTCA includes
25#include "votca/csg/imcio.h"
26
27// Local private VOTCA includes
28#include "csg_imc_solve.h"
29
30int main(int argc, char** argv) {
31 CG_IMC_solve app;
32 return app.Exec(argc, argv);
33}
34
36 namespace propt = boost::program_options;
37
38 AddProgramOptions()("regularization,r",
39 propt::value<double>()->default_value(0.0),
40 "regularization factor");
41
42 AddProgramOptions()("imcfile,i", boost::program_options::value<std::string>(),
43 "imc statefile");
44
45 AddProgramOptions()("gmcfile,g", boost::program_options::value<std::string>(),
46 "gmc statefile");
47
48 AddProgramOptions()("idxfile,n", boost::program_options::value<std::string>(),
49 "idx statefile");
50}
51
53 CheckRequired("imcfile", "Missing imcfile");
54 CheckRequired("gmcfile", "Missing gmcfile");
55 CheckRequired("idxfile", "Missing idxfile");
56 return true;
57}
58
60 std::string imcfile = OptionsMap()["imcfile"].as<std::string>();
61 std::string gmcfile = OptionsMap()["gmcfile"].as<std::string>();
62
63 double reg = OptionsMap()["regularization"].as<double>();
64
65 Eigen::MatrixXd A = votca::csg::imcio_read_matrix(gmcfile);
67 B.Load(imcfile);
68
70 x.resize(B.size());
71 x.x() = B.x();
72
73 // Calculating https://en.wikipedia.org/wiki/Tikhonov_regularization
74
75 Eigen::MatrixXd ATA = A.transpose() * A;
76
77 // We could add and invert but for stability reasons we will diagonalize the
78 // matrix which is symmetric by construction
79
80 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(ATA);
81
82 Eigen::VectorXd inv_diag = Eigen::VectorXd::Zero(es.eigenvalues().size());
83 double etol = 1e-12;
84
85 votca::Index numerics_unstable = 0;
86 // Constructing pseudoinverse if not stable
87 // https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse
88 for (votca::Index i = 0; i < es.eigenvalues().size(); i++) {
89 if (std::abs(es.eigenvalues()[i] + reg) < etol) {
90 numerics_unstable++;
91 } else {
92 inv_diag[i] = 1 / (es.eigenvalues()[i] + reg);
93 }
94 }
95
96 if (numerics_unstable > 0) {
97 std::cout
98 << "Regularisation parameter was to small, a pseudo inverse was "
99 "constructed instead.\n Use a larger regularisation parameter R."
100 " Smallest (eigenvalue+R)="
101 << (es.eigenvalues().array() + reg).abs().minCoeff() << " Found "
102 << numerics_unstable << " eigenvalues of " << es.eigenvalues().size()
103 << " below " << etol << std::endl;
104 }
105
106 Eigen::MatrixXd inverse =
107 es.eigenvectors() * inv_diag.asDiagonal() * es.eigenvectors().transpose();
108
109 x.y() = -inverse * A.transpose() * B.y();
110
111 std::string idxfile = OptionsMap()["idxfile"].as<std::string>();
112 std::vector<std::pair<std::string, votca::tools::RangeParser> > ranges =
114 for (std::pair<std::string, votca::tools::RangeParser>& range : ranges) {
116 for (votca::Index r : range.second) {
117 tbl.push_back(x.x(r - 1), x.y(r - 1), 'i');
118 }
119 tbl.Save(range.first + ".dpot.imc");
120 }
121}
Solves linear system for IMCS.
void Initialize() override
Initialize application data.
void Run() override
Main body of application.
bool EvaluateOptions() override
Process command line options.
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 store tables like rdfs, tabulated potentials, etc
Definition table.h:36
double & x(Index i)
Definition table.h:44
Index size() const
Definition table.h:42
void Load(std::string filename)
Definition table.cc:47
double & y(Index i)
Definition table.h:45
void Save(std::string filename) const
Definition table.cc:59
void push_back(double x, double y, char flags=' ')
Definition table.cc:235
int main(int argc, char **argv)
std::vector< std::pair< std::string, tools::RangeParser > > imcio_read_index(const std::string &filename)
Definition imcio.cc:147
Eigen::MatrixXd imcio_read_matrix(const std::string &filename)
Definition imcio.cc:113
Eigen::Index Index
Definition types.h:26