blob: 704dfd62b1e8dafd4ada349d2fdeb1cfd1ddb875 (
plain)
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
|
#include <optical_signal.h>
#include <specs.h>
using namespace std::complex_literals;
unsigned int OpticalSignal::nextId = 0;
OpticalSignal &OpticalSignal::operator+=(const OpticalSignal &rhs)
{
// cout << "Summing two signals: " << endl;
// cout << "\t" << *this << endl;
// cout << "\t" << rhs << endl;
auto this_wavelength = this->getWavelength();
auto rhs_wavelength = rhs.getWavelength();
if (this_wavelength == rhs_wavelength)
m_field += rhs.m_field;
else if (isnan(rhs_wavelength))
(void)rhs; // nothing to do
else if (isnan(this_wavelength))
*this = rhs;
else if (specsGlobalConfig.simulation_mode == FREQUENCY_DOMAIN)
{
// TODO: move that somewhere else ???
// Replace with rhs
if (rhs_wavelength > this_wavelength)
*this = rhs;
}
else
cerr << "Attempted to sum signals of different wavelengths, but current"
<< "simulation mode doesn't allow it (" << __FUNCTION__ << ")" << endl;
// cout << "\t--> " << *this << endl;
return *this;
}
OpticalSignal &OpticalSignal::operator-=(const OpticalSignal &rhs)
{
// cout << "Substracting two signals: " << endl;
// cout << "\t" << *this << endl;
// cout << "\t" << rhs << endl;
auto this_wavelength = this->getWavelength();
auto rhs_wavelength = rhs.getWavelength();
if (this_wavelength == rhs_wavelength)
m_field -= rhs.m_field;
else if (isnan(rhs_wavelength))
(void)rhs; // nothing to do
else if (isnan(this_wavelength))
*this = rhs;
else if (specsGlobalConfig.simulation_mode == FREQUENCY_DOMAIN)
{
// TODO: move that somewhere else ???
// Replace with rhs
if (rhs_wavelength > this_wavelength)
*this = rhs;
}
else
cerr << "Attempted to substract signals of different wavelengths, but current"
<< "simulation mode doesn't allow it (" << __FUNCTION__ << ")" << endl;
// cout << "\t--> " << *this << endl;
return *this;
}
OpticalSignal OpticalSignal::sumSignals(OpticalSignal s0, OpticalSignal s1)
{
cerr << "Use of deprecated" << __FUNCTION__<< endl;
// Maybe doing just the sum is faster than this
if (s0.m_field == 0.0) {
OpticalSignal os_sum = s1;
os_sum.getNewId();
return os_sum;
}
if (s1.m_field == (complex<double>) 0) {
OpticalSignal os_sum = s0;
os_sum.getNewId();
return os_sum;
}
if (s0.getWavelength() != s1.getWavelength())
{
bool ignore_wavelength_error = false;
if (ignore_wavelength_error)
{
cerr << "Signals have different wavelengths:" << std::endl;
cerr << "\t" << s0.getWavelength() << "(" << norm(s0.m_field) << ")"
<< "\t" << s1.getWavelength() << "(" << norm(s1.m_field) << ")"
<< std::endl;
sc_stop();
} else {
OpticalSignal os_sum = s0;
if (norm(s1.m_field) > norm(s0.m_field))
os_sum = s1;
os_sum.getNewId();
return os_sum;
}
}
complex<double> Asum = s0.m_field + s1.m_field;
OpticalSignal os_sum = s0;
os_sum.m_field = Asum;
os_sum.getNewId();
return os_sum;
}
uint32_t OpticalSignal::getIDFromWavelength(const double &wavelength)
{
auto it = find (specsGlobalConfig.wavelengths_vector.cbegin(),
specsGlobalConfig.wavelengths_vector.cend(),
wavelength);
uint32_t index;
if (it != specsGlobalConfig.wavelengths_vector.cend())
{ // Found
index = it - specsGlobalConfig.wavelengths_vector.cbegin();
}
else
{ // Didn't find, has to add it to the vector
specsGlobalConfig.wavelengths_vector.push_back(wavelength);
index = specsGlobalConfig.wavelengths_vector.size() - 1;
// -1 because of zero indexing
}
if (index == (uint32_t)(-1))
{
cerr << "Error: too many wavelengths being used ." << endl;
exit(1);
}
return index;
}
double OpticalSignal::getWavelength() const
{
if (m_wavelength_id >= specsGlobalConfig.wavelengths_vector.size())
{
cerr << "Wavelength not found in global vector." << endl;
exit(1);
}
return specsGlobalConfig.wavelengths_vector[m_wavelength_id];
}
double OpticalSignal::getWavelength(const uint32_t &wavelength_id)
{
if (wavelength_id >= specsGlobalConfig.wavelengths_vector.size())
{
cerr << "Wavelength not found in global vector." << endl;
exit(1);
}
return specsGlobalConfig.wavelengths_vector[wavelength_id];
}
|