|
10 | 10 | #include <cmath> |
11 | 11 | #include <cstdlib> |
12 | 12 | #include <cstring> |
| 13 | +#include <cctype> |
| 14 | +#include <stdexcept> |
13 | 15 |
|
14 | 16 | namespace dso { |
15 | 17 |
|
@@ -75,27 +77,25 @@ class RealHarmonics { |
75 | 77 | } |
76 | 78 |
|
77 | 79 | int num_harmonics() const noexcept { return m_num_harmonics; } |
78 | | - |
| 80 | + |
79 | 81 | const double *operator()(int i) const noexcept { |
80 | 82 | return m_mem + i * DBLS_IN_TERM; |
81 | 83 | } |
82 | 84 | double *operator()(int i) noexcept { return m_mem + i * DBLS_IN_TERM; } |
83 | 85 |
|
84 | 86 | /** @brief Construct instance given the number of harmonics. */ |
85 | | - RealHarmonics(int num_harmonics = 0) noexcept |
| 87 | + explicit RealHarmonics(int num_harmonics = 0) noexcept |
86 | 88 | : m_num_harmonics(num_harmonics), |
87 | 89 | m_capacity(num_harmonics > MIN_HARMONIC_TERMS ? num_harmonics |
88 | 90 | : MIN_HARMONIC_TERMS), |
89 | 91 | m_mem((double *)std::malloc(m_capacity * DBLS_IN_TERM * |
90 | 92 | sizeof(double))) {}; |
91 | 93 |
|
92 | 94 | /** @brief Construct instance given a harmonic. */ |
93 | | - RealHarmonics(double freq, double amp_sin, |
94 | | - double amp_cos) noexcept |
95 | | - : m_num_harmonics(1) |
96 | | - , m_capacity(MIN_HARMONIC_TERMS) |
97 | | - , m_mem((double*)std::malloc(m_capacity * DBLS_IN_TERM * sizeof(double))) |
98 | | - { |
| 95 | + explicit RealHarmonics(double freq, double amp_sin, double amp_cos) noexcept |
| 96 | + : m_num_harmonics(1), m_capacity(MIN_HARMONIC_TERMS), |
| 97 | + m_mem( |
| 98 | + (double *)std::malloc(m_capacity * DBLS_IN_TERM * sizeof(double))) { |
99 | 99 | m_mem[0] = freq; |
100 | 100 | m_mem[1] = amp_sin; |
101 | 101 | m_mem[2] = amp_cos; |
@@ -172,30 +172,89 @@ class RealHarmonics { |
172 | 172 | * id (code) to mark the site name. |
173 | 173 | */ |
174 | 174 | class SiteRealHarmonics { |
175 | | - RealHarmonics mhr; |
176 | | - char msite[5]={'\0'}; |
| 175 | + /* harmonics for X or N component */ |
| 176 | + RealHarmonics mhr_xn; |
| 177 | + /* harmonics for Y or E component */ |
| 178 | + RealHarmonics mhr_ye; |
| 179 | + /* harmonics for Z or U component */ |
| 180 | + RealHarmonics mhr_zu; |
| 181 | + /* site 4-char id */ |
| 182 | + char msite[5] = {'\0'}; |
| 183 | + /* ref. system: can be either cartesian 'C' or topocentric 'T' */ |
| 184 | + char mrsys = 'C'; |
| 185 | + |
177 | 186 | public: |
178 | | - SiteRealHarmonics(const char *name = nullptr) noexcept : mhr() { |
179 | | - if (name) std::strncpy(msite, name, 4); |
| 187 | + explicit SiteRealHarmonics(const char *name = nullptr) noexcept : mhr_xn(), mhr_ye(), mhr_zu() { |
| 188 | + if (name) |
| 189 | + std::strncpy(msite, name, 4); |
180 | 190 | } |
181 | | - SiteRealHarmonics(const char* name, double freq, double Asin, double Acos) noexcept |
182 | | - : mhr(freq, Asin, Acos) |
183 | | - { |
| 191 | + explicit SiteRealHarmonics(char sys_ct, const char *name = nullptr) : |
| 192 | + mhr_xn(), mhr_ye(), mhr_zu(), mrsys(std::toupper(sys_ct)) { |
| 193 | + if (mrsys != 'C' && mrsys != 'T') { |
| 194 | + std::string errmsg = "[ERROR] Invalid harmonic component " + std::string{sys_ct} + "; can either be \'C\' for cartesian or \'T\' for neu/topocentric (traceback: " + std::string(__func__) + ")\n"; |
| 195 | + throw std::runtime_error(errmsg); |
| 196 | + } |
| 197 | + if (name) |
| 198 | + std::strncpy(msite, name, 4); |
| 199 | + } |
| 200 | + SiteRealHarmonics(const char *name, int num_freqs) noexcept |
| 201 | + : mhr_xn(num_freqs), mhr_ye(num_freqs), mhr_zu(num_freqs) { |
184 | 202 | #pragma GCC diagnostic push |
185 | 203 | #pragma GCC diagnostic ignored "-Wstringop-truncation" |
186 | 204 | if (name) |
187 | 205 | std::strncpy(msite, name, 4); |
188 | 206 | #pragma GCC diagnostic pop |
189 | 207 | } |
190 | | - const char *site_name() const noexcept {return msite;} |
191 | | - char *site_name() noexcept {return msite;} |
192 | | - const RealHarmonics &harmonics() const noexcept {return mhr;} |
193 | | - RealHarmonics &harmonics() noexcept {return mhr;} |
194 | | - int add_harmonic(double freq, double amp_sin = 0e0, |
195 | | - double amp_cos = 0e0) noexcept |
196 | | - { |
197 | | - return mhr.add_harmonic(freq, amp_sin, amp_cos); |
| 208 | + const char *site_name() const noexcept { return msite; } |
| 209 | + char *site_name() noexcept { return msite; } |
| 210 | + const RealHarmonics &harmonics(char rcmp) const { |
| 211 | + char cmp = |
| 212 | + static_cast<char>(std::tolower(static_cast<unsigned char>(rcmp))); |
| 213 | + switch (mrsys) { |
| 214 | + case 'C': |
| 215 | + switch (cmp) { |
| 216 | + case 'x': return mhr_xn; |
| 217 | + case 'y': return mhr_ye; |
| 218 | + case 'z': return mhr_zu; |
| 219 | + } |
| 220 | + break; |
| 221 | + case 'T': |
| 222 | + switch (cmp) { |
| 223 | + case 'n': return mhr_xn; |
| 224 | + case 'e': return mhr_ye; |
| 225 | + case 'u': return mhr_zu; |
| 226 | + } |
| 227 | + break; |
| 228 | + } |
| 229 | + std::string errmsg = "[ERROR] Invalid harmonic component " + std::string{rcmp} + " for harmonics of type " + std::string{mrsys} + " (traceback: " + std::string(__func__) + ")\n"; |
| 230 | + throw std::runtime_error(errmsg); |
| 231 | + } |
| 232 | + RealHarmonics &harmonics(char rcmp) { |
| 233 | + char cmp = |
| 234 | + static_cast<char>(std::tolower(static_cast<unsigned char>(rcmp))); |
| 235 | + switch (mrsys) { |
| 236 | + case 'C': |
| 237 | + switch (cmp) { |
| 238 | + case 'x': return mhr_xn; |
| 239 | + case 'y': return mhr_ye; |
| 240 | + case 'z': return mhr_zu; |
| 241 | + } |
| 242 | + break; |
| 243 | + case 'T': |
| 244 | + switch (cmp) { |
| 245 | + case 'n': return mhr_xn; |
| 246 | + case 'e': return mhr_ye; |
| 247 | + case 'u': return mhr_zu; |
| 248 | + } |
| 249 | + break; |
| 250 | + } |
| 251 | + std::string errmsg = "[ERROR] Invalid harmonic component " + std::string{rcmp} + " for harmonics of type " + std::string{mrsys} + " (traceback: " + std::string(__func__) + ")\n"; |
| 252 | + throw std::runtime_error(errmsg); |
198 | 253 | } |
| 254 | + //int add_harmonic(double freq, double amp_sin = 0e0, |
| 255 | + // double amp_cos = 0e0) noexcept { |
| 256 | + // return mhr.add_harmonic(freq, amp_sin, amp_cos); |
| 257 | + //} |
199 | 258 | }; /* class SiteRealHarmonics */ |
200 | 259 |
|
201 | 260 | } /* namespace dso */ |
|
0 commit comments