-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyze.cpp
160 lines (133 loc) · 4.36 KB
/
analyze.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "analyze.h"
#include <iostream>
#include <cstdlib>
#include "solver.h"
#include "dataimporter.h"
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/vector.hpp>
#include "boost_serialization_eigen.h"
#include <fstream>
#include "gnuplot-iostream.h"
#include <algorithm>
#include <numeric>
#include <string>
struct AnalyzeSet
{
Eigen::VectorXd params;
std::string name;
Eigen::VectorXd delta;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & params;
ar & name;
ar & delta;
}
};
void load(std::vector<AnalyzeSet> & results)
{
std::ifstream ifs("file.txt", std::ofstream::binary);
boost::archive::text_iarchive ia(ifs);
ia>>results;
}
void calcAndSave(TaskData taskDataOrig, std::vector<AnalyzeSet> & results)
{
std::vector<std::string> names{"QOil1", "QOil2", "QOil4", "QWater1", "QWater2"};
std::vector<std::unique_ptr<IRegressionModel>> models;
models.push_back(std::make_unique<RegressionModelLn1>(taskDataOrig));
models.push_back(std::make_unique<RegressionModelLn3>(taskDataOrig));
models.push_back(std::make_unique<RegressionModelLn4>(taskDataOrig));
TaskDataHelper::SwapOilWater(taskDataOrig);
models.push_back(std::make_unique<RegressionModelLn1>(taskDataOrig));
models.push_back(std::make_unique<RegressionModelLn2>(taskDataOrig));
Solver::SolverParams sp;
sp.verbose = 2;
sp.enableNormalizer = true;
sp.nMaxIter = 25;
try
{
for(size_t i=0; i<models.size(); ++i)
{
std::cout<<i<<std::endl;
Solver solver(std::move(models[i]));
solver.SolverInit(sp);
if(!solver.Solve())
//throw std::logic_error("Can' solve");
std::cout<<"not solved"<<std::endl;
results.push_back({solver.GetResult(), names[i], solver.GetWorkingSet().yMinusF});
}
}
catch(std::logic_error &e)
{
std::cout<<e.what()<<std::endl;
}
std::cout<<"Analyze calculation is finished"<<std::endl;
{
std::cout<<"Writing data to file"<<std::endl;//TODO: read data Q(t)
std::ofstream ofs("file.txt", std::ofstream::binary);
boost::archive::text_oarchive oa(ofs);
oa<<results;
}
}
typedef std::vector<double> dvec;
void Analyzer::Analyze(const std::string & filename)
{
CSVDataImporter dataImporter;
TaskData taskDataOrig = dataImporter.read(filename);
OptimizedTaskData oTD(taskDataOrig);
std::vector<AnalyzeSet> results;
bool isSuccessffullRead = false;
try
{
load(results);
isSuccessffullRead = true;
}
catch(std::exception &e)
{
std::cout<<e.what()<<std::endl;
}
if(!isSuccessffullRead)
{
calcAndSave(taskDataOrig, results);
}
// get timeVec and maxT
dvec time;
double maxT = 0;
for(auto & hole: oTD.holes)
{
double t = hole.sumT.back();
maxT = maxT>t ? maxT : t;
}
const double tStep = 100;
for(size_t i = 0; i<maxT/tStep; ++i)
time.push_back(i*tStep);
auto drawFunc = [&time](auto func, Eigen::VectorXd params, std::string name){
std::cout<<params.transpose()<<std::endl;
Gnuplot gp;
gp<<"set terminal postscript eps enhanced color font 'Helvetica,10'"<<std::endl;
gp<<"set output '"<<name<<".ps'"<<std::endl;
dvec val;
for(size_t i=0; i<time.size(); ++i)
val.push_back(func(params, time[i]));
gp << "plot '-' tit '"<<name<<"'"<<std::endl;
gp.send1d(boost::make_tuple(time, val));
};
drawFunc(Function1::CalcFT, results[0].params.tail(Function1::nParams), std::string("func_")+results[0].name);
drawFunc(Function2::CalcFT, results[1].params.tail(Function2::nParams), std::string("func_")+results[1].name);
drawFunc(Function4::CalcFT, results[2].params.tail(Function4::nParams), std::string("func_")+results[2].name);
drawFunc(Function1::CalcFT, results[3].params.tail(Function1::nParams), std::string("func_")+results[3].name);
drawFunc(Function2::CalcFT, results[4].params.tail(Function2::nParams), std::string("func_")+results[4].name);
auto saveToCSV = [](const auto & arr, std::string name){
std::ofstream ofs(name);
for(size_t i = 0; i< arr.size(); ++i)
ofs<<arr[i]<<","<<std::endl;
};
for(const AnalyzeSet & result: results)
{
std::ofstream ofs(result.name + "_params.txt");
ofs<<result.params;
saveToCSV(result.delta, std::string("delta_") + result.name +".csv");
}
};