1 | // $Id: test_wead.cc 92 2004-05-29 22:44:39Z jari $ |
---|
2 | |
---|
3 | #include <iostream> |
---|
4 | #include <fstream> |
---|
5 | #include <string> |
---|
6 | |
---|
7 | #include "FileIO.h" |
---|
8 | #include "matrix.h" |
---|
9 | #include "Wead.h" |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | // class for command line options. |
---|
14 | class Parameter { |
---|
15 | public: |
---|
16 | Parameter(const Parameter&); // not implemented |
---|
17 | Parameter(const int argc,const char *argv[]); |
---|
18 | |
---|
19 | inline const std::string& matrix(void) const { return matrix_file_; } |
---|
20 | inline const std::string& flag(void) const { return flag_file_; } |
---|
21 | inline int neighbours(void) const { return neighbours_; } |
---|
22 | |
---|
23 | private: |
---|
24 | void analyse(void); |
---|
25 | void defaults(void); |
---|
26 | |
---|
27 | std::string flag_file_; |
---|
28 | std::string matrix_file_; |
---|
29 | int neighbours_; |
---|
30 | }; |
---|
31 | |
---|
32 | Parameter::Parameter(const int argc,const char *argv[]) |
---|
33 | { |
---|
34 | using namespace std; |
---|
35 | // check if help requested |
---|
36 | for (int i=1; i<argc; i++) |
---|
37 | if (string(argv[i])==string("-help") || string(argv[i])==string("-h")) { |
---|
38 | cout << "Please write some help information\n"; |
---|
39 | exit(0); // always exit after printing help |
---|
40 | } |
---|
41 | |
---|
42 | defaults(); |
---|
43 | |
---|
44 | for (int i=1; i<argc; i++) { |
---|
45 | bool ok=false; |
---|
46 | string myargv(argv[i]); |
---|
47 | if (myargv==string("-matrix")) |
---|
48 | if ((i+1)<argc) { |
---|
49 | matrix_file_=argv[++i]; |
---|
50 | ok=true; |
---|
51 | } |
---|
52 | if (myargv==string("-flag")) |
---|
53 | if ((i+1)<argc) { |
---|
54 | flag_file_=argv[++i]; |
---|
55 | ok=true; |
---|
56 | } |
---|
57 | if (myargv==string("-neighbours")) |
---|
58 | if ((i+1)<argc) { |
---|
59 | neighbours_=atoi(argv[++i]); |
---|
60 | ok=true; |
---|
61 | } |
---|
62 | |
---|
63 | if (!ok) |
---|
64 | std::cerr << "# Parameter::Parameter Invalid option: " |
---|
65 | << argv[i] << '\n'; |
---|
66 | } |
---|
67 | |
---|
68 | analyse(); |
---|
69 | } |
---|
70 | |
---|
71 | void Parameter::analyse(void) |
---|
72 | { |
---|
73 | using namespace theplu::cpptools; |
---|
74 | bool ok=true; |
---|
75 | if (FileIO().access_rights(matrix(),"r")) { |
---|
76 | std::cerr << "Cannot access matrix file " << matrix() << std::endl; |
---|
77 | ok=false; |
---|
78 | } |
---|
79 | if (FileIO().access_rights(flag(),"r")) { |
---|
80 | std::cerr << "Cannot access flag file " << flag() << std::endl; |
---|
81 | ok=false; |
---|
82 | } |
---|
83 | if (!ok) |
---|
84 | std::exit(-1); |
---|
85 | } |
---|
86 | |
---|
87 | void Parameter::defaults(void) |
---|
88 | { |
---|
89 | flag_file_="data/wead_flag.data"; |
---|
90 | matrix_file_="data/wead_matrix.data"; |
---|
91 | neighbours_=3; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | int main(const int argc,const char* argv[]) |
---|
97 | { |
---|
98 | Parameter option(argc,argv); |
---|
99 | std::ifstream matrix_stream(option.matrix().c_str()); |
---|
100 | std::ifstream flag_stream(option.flag().c_str()); |
---|
101 | theplu::cpptools::Wead wead(matrix_stream,flag_stream,option.neighbours()); |
---|
102 | wead.estimate(); |
---|
103 | std::ifstream control_stream("data/wead_result.data"); |
---|
104 | theplu::gslapi::matrix control(control_stream); |
---|
105 | control-=wead.matrix(); |
---|
106 | for (unsigned int i=0; i<control.rows(); i++) |
---|
107 | for (unsigned int j=0; j<control.columns(); j++) |
---|
108 | // Jari, should we use GSL defined round off errors? Anyway, the |
---|
109 | // hardcoded number below should be changed. |
---|
110 | if (fabs(control(i,j))>5e-13) |
---|
111 | exit(-1); // calculation result out of accepted error bounds |
---|
112 | exit(0); // normal exit |
---|
113 | } |
---|