mmCEsim 0.3.0
mmWave Channel Estimation Simulation
export.h
Go to the documentation of this file.
1
12#ifndef _EXPORT_H_
13#define _EXPORT_H_
14
15#include "_boost_config.h"
16#include "cli_options.h"
17#include "error_code.h"
18#include "export/alg.h"
20#include "export/keywords.h"
21#include "export/lang.h"
22#include "export/shared_info.h"
23#include "export/value_vec.h"
24#include "fmt.h"
25#include "read.h"
26#include "term.h"
27#include "utils.h"
28#include <algorithm>
29#include <any>
30#include <boost/algorithm/string.hpp>
31#include <ctime>
32#include <exception>
33#include <filesystem>
34#include <fstream>
35#include <iostream>
36#include <map>
37#include <regex>
38#include <sstream>
39#include <tuple>
40#include <type_traits>
41#include <vector>
42
43class Export {
44 public:
45 enum DType : unsigned {
46 INT = 1,
47 DOUBLE = 2,
48 STRING = 4,
49 BOOL = 8,
50 CHAR = 16,
51 SEQ = 32,
52 MAP = 64,
53 NUL = 1024,
54 UNDEF = 2048
55 };
56
62 enum class NodeRole {
63 Tx,
64 Rx,
65 RIS,
66 };
67
68 Export(CLI_Options& opt, Shared_Info* const info = nullptr);
69
70 Export(CLI_Options& opt, const YAML::Node& config, const YAML_Errors& errors, Shared_Info* const info = nullptr);
71
72 ~Export();
73
75
76 static YAML_Errors exportCode(CLI_Options& opt, Shared_Info* const info = nullptr);
77
78 static YAML_Errors exportCode(CLI_Options& opt, const YAML::Node& config, const YAML_Errors& errors,
79 Shared_Info* const info = nullptr);
80
81 private:
82 std::ofstream& _f();
83
84 std::string _langStr() const;
85
86 std::string _langName() const;
87
88 std::string _langExtension() const;
89
90 std::string _langHeaderExtension() const;
91
92 std::string _langMmcesimExtension() const;
93
94 std::string _langCommentSymbol() const;
95
96 void _info(const std::string& str) const;
97
98 // error message can be specified later
99 bool _preCheck(const YAML::Node& node, unsigned allowed_type, bool mattered = true);
100
101 template <typename T>
102 T _as(const YAML::Node& n, bool mattered = true);
103
104 std::string _asStr(const YAML::Node& n, bool mattered = true);
105
106 void _setLatestError(const std::string& str);
107
108 void _setLang();
109
110 bool _isKeyword(const std::string& str) const;
111
112 std::tuple<bool, std::string, std::string> _setChannelGains(const YAML::Node& n);
113
114 std::string _asVarName(const std::string& str) const;
115
116 std::ofstream& _wComment();
117
118 void _topComment();
119
120 void _beginning();
121
122 void _generateChannels();
123
124 void _generateConstants();
125
126 void _algorithms();
127
128 void _sounding();
129
130 void _estimation(const Macro& macro, int job_cnt = -1);
131
132 void _reporting();
133
134 void _ending();
135
136 bool _loadALG();
137
138 bool _setCascadedChannel();
139
140 bool _setDataParams();
141
148 bool _setVarNames();
149
167 void _generateBF(unsigned Nt_B);
168
169 unsigned _getTestNum(const YAML::Node& n);
170
178 std::tuple<unsigned, unsigned, unsigned, unsigned, unsigned, unsigned> _getSize(const YAML::Node& n);
179
180 void _checkALGdependency(std::vector<std::string>& algs, bool logged = true);
181
183 YAML::Node _config;
186 std::ofstream* _f_ptr = nullptr;
188 std::vector<int> _transmitters;
189 std::vector<int> _receivers;
190 struct DataParams {
191 unsigned max_test_num = 0;
192 unsigned max_noise_size = 0;
194 std::string _cascaded_channel;
195 std::string _received_signal;
196 std::string _noise;
198 std::vector<std::string> _beamforming_RIS;
199 std::map<std::string, std::string> _beamforming;
201 std::vector<std::tuple<std::string, std::any, bool>> _constants;
202
203 const int _MAX_TX = 1;
204 const int _MAX_RX = 1;
205
207};
208
209template <typename T>
210inline T Export::_as(const YAML::Node& n, bool mattered) {
211 bool l;
212 if (std::is_same_v<T, int>) {
213 l = _preCheck(n, DType::INT, mattered);
214 } else if (std::is_same_v<T, double>) {
215 l = _preCheck(n, DType::DOUBLE, mattered);
216 } else if (std::is_same_v<T, std::string>) {
217 l = _preCheck(n, DType::STRING, mattered);
218 } else if (std::is_same_v<T, bool>) {
219 l = _preCheck(n, DType::BOOL, mattered);
220 } else if (std::is_same_v<T, char>) {
221 l = _preCheck(n, DType::CHAR, mattered);
222 }
223 if (l) return n.as<T>();
224 else if (mattered) throw("Invalid!");
225 else return T();
226}
227
228inline std::ofstream& Export::_f() { return *_f_ptr; }
229
230inline std::string Export::_langStr() const {
231 if (lang == Lang::CPP) return "cpp";
232 else if (lang == Lang::MATLAB) return "matlab";
233 else if (lang == Lang::OCTAVE) return "octave";
234 else if (lang == Lang::PY) return "py";
235 else if (lang == Lang::IPYNB) return "ipynb";
236 else return "Impossible branch in \"Export::_langStr()\"!";
237}
238
239inline std::string Export::_langName() const {
240 if (lang == Lang::CPP) return "C++ (with Armadillo library)";
241 else if (lang == Lang::MATLAB) return "MATLAB";
242 else if (lang == Lang::OCTAVE) return "GNU Octave";
243 else if (lang == Lang::PY) return "Python (with NumPy library)";
244 else if (lang == Lang::IPYNB) return "IPyNb (with NumPy library)";
245 else return "Impossible branch in \"Export::_langName()\"!";
246}
247
248inline std::string Export::_langExtension() const {
249 if (lang == Lang::CPP) return "cpp";
250 else if (lang == Lang::MATLAB || lang == Lang::OCTAVE) return "m";
251 else if (lang == Lang::PY) return "py";
252 else if (lang == Lang::IPYNB) return "ipynb";
253 else return "Impossible branch in \"Export::_langExtension()\"!";
254}
255
256inline std::string Export::_langHeaderExtension() const {
257 if (lang == Lang::CPP) return "h";
258 else if (lang == Lang::MATLAB || lang == Lang::OCTAVE) return "m";
259 else if (lang == Lang::PY) return "py";
260 else if (lang == Lang::IPYNB) return "ipynb";
261 else return "Impossible branch in \"Export::_langHeaderExtension()\"!";
262}
263
264inline std::string Export::_langMmcesimExtension() const {
265 if (lang == Lang::CPP) return "mmcesim-cpp";
266 else if (lang == Lang::MATLAB || lang == Lang::OCTAVE) return "mmcesim-m";
267 else if (lang == Lang::PY || lang == Lang::IPYNB) return "mmcesim-py";
268 else return "Impossible branch in \"Export::_langMmcesimExtension()\"!";
269}
270
271inline std::string Export::_langCommentSymbol() const {
272 if (lang == Lang::CPP) return "//";
273 else if (lang == Lang::MATLAB || lang == Lang::OCTAVE) return "%";
274 else if (lang == Lang::PY || lang == Lang::IPYNB) return "#";
275 else return "Impossible branch in \"Export::_langCommentSymbol()\"!";
276}
277
278inline void Export::_info(const std::string& str) const { std::cout << "[mmCEsim] export $ " << str << std::endl; }
279
280inline std::string Export::_asStr(const YAML::Node& n, bool mattered) { return _as<std::string>(n, mattered); }
281
282inline void Export::_setLatestError(const std::string& str) {
283 assert((!_errors.empty() && "Check if errors are empty when trying to edit the last record."));
284 (_errors.end() - 1)->msg = str;
285}
286
287inline bool Export::_isKeyword(const std::string& str) const {
288 if (lang == Lang::CPP) return contains(CPP_Keywords, str);
290 if (lang == Lang::PY || lang == Lang::IPYNB) return contains(PY_Keywords, str);
291 else return false; // though impossible here
292}
293
294// TODO: use this function in the future
295inline std::string Export::_asVarName(const std::string& str) const {
296 if (_isKeyword(str)) return str + "_";
297 else return str;
298}
299
300inline std::ofstream& Export::_wComment() {
301 _f() << _langCommentSymbol() << ' ';
302 return _f();
303}
304
305inline unsigned Export::_getTestNum(const YAML::Node& n) {
306 if (auto&& num = n["test_num"]; _preCheck(num, DType::INT, false)) {
307 return num.as<unsigned>();
308 } else return 500; // default value
309}
310
311#endif
Boost Configurations.
Algorithm Parser (.alg Language)
Graph Analysis for Complex Cascaded Channels.
Definition: channel_graph.h:22
Definition: export.h:43
std::string _noise
Definition: export.h:196
std::vector< int > _receivers
Definition: export.h:189
std::string _beamforming_F
Definition: export.h:197
bool _setDataParams()
Definition: export.cpp:1106
std::string _langName() const
Definition: export.h:239
Shared_Info *const _s_info
Definition: export.h:187
void _topComment()
Definition: export.cpp:249
struct Export::DataParams _data_params
void _info(const std::string &str) const
Definition: export.h:278
Export(CLI_Options &opt, Shared_Info *const info=nullptr)
Definition: export.cpp:30
std::string _beamforming_W
Definition: export.h:197
void _sounding()
Definition: export.cpp:446
std::string _langMmcesimExtension() const
Definition: export.h:264
std::vector< std::tuple< std::string, std::any, bool > > _constants
Definition: export.h:201
void _generateConstants()
Definition: export.cpp:398
bool _preCheck(const YAML::Node &node, unsigned allowed_type, bool mattered=true)
Definition: export.cpp:119
T _as(const YAML::Node &n, bool mattered=true)
Definition: export.h:210
std::string _received_signal
Definition: export.h:195
std::string _langExtension() const
Definition: export.h:248
std::tuple< unsigned, unsigned, unsigned, unsigned, unsigned, unsigned > _getSize(const YAML::Node &n)
Get the size of the node (Tx/Rx/RIS).
Definition: export.cpp:1224
void _setLang()
Definition: export.cpp:170
bool _already_error_before_export
Definition: export.h:185
Channel_Graph _channel_graph
Definition: export.h:200
std::ofstream & _wComment()
Definition: export.h:300
void _generateBF(unsigned Nt_B)
Generate RIS reflection matrices.
Definition: export.cpp:1154
void _beginning()
Definition: export.cpp:299
void _setLatestError(const std::string &str)
Definition: export.h:282
std::string _asStr(const YAML::Node &n, bool mattered=true)
Definition: export.h:280
std::vector< int > _transmitters
Definition: export.h:188
std::string _langStr() const
Definition: export.h:230
std::string _asVarName(const std::string &str) const
Definition: export.h:295
std::tuple< bool, std::string, std::string > _setChannelGains(const YAML::Node &n)
Definition: export.cpp:223
void _ending()
Definition: export.cpp:905
CLI_Options & _opt
Definition: export.h:182
std::string _langCommentSymbol() const
Definition: export.h:271
bool _loadALG()
Definition: export.cpp:909
std::vector< std::string > _beamforming_RIS
Definition: export.h:198
void _algorithms()
Definition: export.cpp:410
void _checkALGdependency(std::vector< std::string > &algs, bool logged=true)
Definition: export.cpp:1273
std::string _langHeaderExtension() const
Definition: export.h:256
std::map< std::string, std::string > _beamforming
Definition: export.h:199
const int _MAX_RX
Definition: export.h:204
NodeRole
Role of each node.
Definition: export.h:62
unsigned _getTestNum(const YAML::Node &n)
Definition: export.h:305
YAML::Node _config
Definition: export.h:183
std::ofstream & _f()
Definition: export.h:228
std::ofstream * _f_ptr
Definition: export.h:186
YAML_Errors _errors
Definition: export.h:184
DType
Definition: export.h:45
@ STRING
string
Definition: export.h:48
@ SEQ
sequence
Definition: export.h:51
@ INT
int
Definition: export.h:46
@ CHAR
map
Definition: export.h:50
@ BOOL
bool
Definition: export.h:49
@ NUL
null
Definition: export.h:53
@ DOUBLE
double
Definition: export.h:47
@ UNDEF
undefined
Definition: export.h:54
@ MAP
map
Definition: export.h:52
void _generateChannels()
Definition: export.cpp:308
bool _setCascadedChannel()
Definition: export.cpp:998
~Export()
Definition: export.cpp:82
void _estimation(const Macro &macro, int job_cnt=-1)
Definition: export.cpp:681
std::string _cascaded_channel
Definition: export.h:194
const int _MAX_TX
Definition: export.h:203
bool _isKeyword(const std::string &str) const
Definition: export.h:287
Lang lang
Definition: export.h:206
bool _setVarNames()
Set the variable names defined by users.
Definition: export.cpp:1128
void _reporting()
Definition: export.cpp:708
YAML_Errors exportCode()
Definition: export.cpp:89
Storage of Command Line Options.
Error Codes (Including Warnings)
Extending {fmt}.
Keywords List for C++, MATLAB and Python.
static std::array PY_Keywords
Definition: keywords.h:92
static std::array CPP_Keywords
Definition: keywords.h:23
static std::array MATLAB_Keywords
Definition: keywords.h:80
Export Languages.
Lang
Export language.
Definition: lang.h:20
@ PY
Python with NumPy library.
@ IPYNB
IPyNb with NumPy library.
@ CPP
C++ with Armadillo library.
@ MATLAB
MATLAB.
@ OCTAVE
GNU Octave.
str
Definition: version_bump.py:14
Read YAML Configuration.
std::vector< YAML_Error > YAML_Errors
Definition: read.h:36
Shared Information Between 'Export' and 'Simulate'.
Command line options.
Definition: cli_options.h:22
Definition: export.h:190
unsigned max_test_num
Definition: export.h:191
unsigned max_noise_size
Definition: export.h:192
Definition: macro.h:62
Definition: shared_info.h:17
Terminal Color and Style Control.
Utilities.
static bool contains(const T &container, const typename T::value_type value)
Definition: utils.h:133
Value Vector in .sim File.