last bunch of changes before jump to new version
[qmc.git] / compute_weights.cpp
blob0c35a8acab611362aefe944551728d22b1951e30
1 /*
2 * Copyright (c) 2009 Mauro Iazzi
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use,
8 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following
11 * conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <string>
27 #include <vector>
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <cmath>
32 #include <cstdlib>
34 using namespace std;
36 void analyze_data_file (std::string filename, std::ostream& out, const int len, double c, bool use_log = true) {
37 int n = 0;
38 double *v = new double[len];
39 int i = 0;
40 for (int j=0;j<len;j++) v[i] = 0;
41 std::string line_string = "";
42 std::string state = "";
43 std::ifstream f(filename.c_str(), std::fstream::in);
44 while (!f.eof()) {
45 double energy = 0.0;
46 double weight = 0.0;
47 getline(f, line_string);
48 std::stringstream line(line_string);
49 if (line_string[0]=='#') {
50 out << line_string << "\n";
51 } else if (line_string.size()!=0) {
52 double nw = use_log?0.0:1.0;
53 line >> state >> energy >> weight;
54 if (use_log) {
55 weight = std::log(weight) - c;
56 } else {
57 weight = weight * std::exp(-c);
59 out << state << " " << energy;
60 v[i] = weight;
61 for (int j=0;j<len;j++) {
62 if (use_log) {
63 nw += v[(len+i-j)%len];
64 } else {
65 nw *= v[(len+i-j)%len];
68 out << " " << (use_log?std::exp(nw):nw);
69 out << "\n";
70 i = (i+1) % len;
73 f.close();
74 delete v;
77 int main (int argc, char **argv) {
78 string len_s = "1000";
79 int len = 1000;
80 double corr = 0.0;
81 vector<string> files;
82 for (int i=1;i<argc;i++) {
83 if (argv[i][0]=='-') {
84 switch (argv[i][1]) {
85 case 'c':
86 corr = atof(argv[++i]);
87 break;
88 case 'l':
89 len = atoi(argv[++i]);
90 len_s = argv[i];
91 break;
93 } else {
94 files.push_back(argv[i]);
97 for (vector<string>::iterator iter = files.begin();iter != files.end();iter++) {
98 string ofn = *iter;
99 ofn.append("-l-");
100 ofn.append(len_s);
101 fstream o(ofn.c_str(), fstream::out);
102 o.precision(10);
103 o << "#!prepend " << len_s << "\n";
104 analyze_data_file(*iter, o, len, corr);
105 o.close();
107 return 0;