aboutsummaryrefslogtreecommitdiff
path: root/src/specs.cpp
blob: 7e2ccfbfbe6c26a13bf63364971203b134147202 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "bitstream_source.h"
#include "optical_signal.h"
#include "sysc_utils.h"
#include <specs.h>
#include "spx_module.h"
#include "value_list_source.h"
#include "electrical_value_list_source.h"

#include <systemc.h>
#include <optical_output_port.h>
#include <spx_module.h>
#include <cw_source.h>
#include <probe.h>
#include <detector.h>
#include <power_meter.h>
#include <generic_transmission_device.h>

using std::string;

SPECSConfig specsGlobalConfig { "SPX" };

SPECSConfig::SPECSConfig(sc_module_name name)
    : sc_module(name)
{
    drop_all_events = false;
    // Simulation options
    engine_timescale = HUNDRED_FS;
    simulation_mode = OpticalOutputPortMode::DEFAULT;
    analysis_type = AnalysisType::TIME_DOMAIN;

    // Port options
    default_abstol = 1e-8;
    default_reltol = 1e-4;
    default_resolution_multiplier = 1;

    // Trace options
    trace_filename = "traces/delete_me";
    default_trace_file = nullptr;
    trace_all_optical_nets = 1;
}

void SPECSConfig::runAnalysis()
{
    applyEngineResolution();
    prepareSimulation();

    switch (analysis_type) {
        case CW_OPERATING_POINT:
            runOPAnalysis();
            break;
        case CW_SWEEP:
            runDCAnalysis();
            break;
        case TIME_DOMAIN:
            runTRANAnalysis();
            break;
        default:
            cerr << "Undefined Analysis type";
            sc_stop();
    }
}

void SPECSConfig::runOPAnalysis()
{
    auto all_probes = sc_get_all_module_by_type<Probe>();
    auto all_mlprobes = sc_get_all_module_by_type<MLambdaProbe>();
    auto all_photodetectors = sc_get_all_module_by_type<Detector>();
    auto all_oop = sc_get_all_module_by_type<OpticalOutputPort>();
    auto all_cws = sc_get_all_module_by_type<CWSource>();

    // Run to initialize all threads and register first values
    sc_start();

    // Set values of signals according to NODESET directive
    for (auto &nodeset_order : nodeset_orders)
    {
        nodeset_order.second.first->write(nodeset_order.second.second);
    }

    // Disable probes for OP point
    for (auto probe: all_probes) {
        probe->enable = sc_logic(0);
    }

    // Disable ML probes for OP point
    for (auto mlprobe: all_mlprobes) {
        mlprobe->enable = sc_logic(0);
    }

    // Disable photodetectors
    for (auto pdet: all_photodetectors) {
        pdet->enable = sc_logic(0);
    }

    // Set all ports mode to NO_DELAY
    for (auto oop: all_oop) {
        oop->m_mode = OpticalOutputPortMode::NO_DELAY;
    }

    // Activate all CW sources
    for (auto cws: all_cws)
        cws->enable = sc_logic(1);

    // Run operating point simulation
    sc_start();

    // Print results on the command line
    printOPAnalysisResult();
}

void SPECSConfig::runDCAnalysis()
{
    auto all_probes = sc_get_all_module_by_type<Probe>();
    auto all_mlprobes = sc_get_all_module_by_type<MLambdaProbe>();
    auto all_photodetectors = sc_get_all_module_by_type<Detector>();
    auto all_oop = sc_get_all_module_by_type<OpticalOutputPort>();
    auto all_sig = sc_get_all_object_by_type<sc_signal<OpticalSignal, SC_MANY_WRITERS>>();
    auto all_cws = sc_get_all_module_by_type<CWSource>();

    // Run to initialize all threads and register first values
    sc_start();

    // Enable normal probes
    for (auto probe: all_probes) {
        probe->enable = sc_logic(1);
    }

    // Disable ML probes
    for (auto mlprobe: all_mlprobes) {
        mlprobe->enable = sc_logic(0);
    }

    // Enable photodetectors
    for (auto pdet: all_photodetectors) {
        // Remark: Photodetectors could in fact be left enabled ?
        // Would that serve any purpose though...?
        pdet->enable = sc_logic(0);
    }

    // Set all ports mode to NO_DELAY
    for (auto oop: all_oop) {
        oop->m_mode = OpticalOutputPortMode::NO_DELAY;
    }

    // Activate CW sources
    for (auto cws: all_cws) {
        cws->enable = sc_logic(1);
    }

    const auto &order = *cw_sweep_orders.begin();
    wavelengths_vector.reserve(order.second.second.size());
    cout << "Starting sweep on " << order.first;
    cout << " (" << order.second.second.size() << " points)" << endl;
    for (const auto &val : order.second.second)
    {
        // Apply sweep param
        order.second.first(val);

        //cout << order.first << " = " << val << endl;

        // Reset OOP and set internal signals to new wavelength
        for (auto oop: all_oop) {
            #if 1 // reset OOP
            oop->reset();
            // TODO: also reset photodetectors?
            #elif 0 // remove previous wavelengths from OOP but keep value
            if (wavelengths_vector.size() >= 2)
            {
                oop->swap_wavelengths(wavelengths_vector.size()-1, wavelengths_vector.size()-2);
                oop->delete_wavelength(wavelengths_vector.size()-2);
                oop->m_skip_next_convergence_check = true;
            }
            #else
            // do nothing
            #endif
        }

        // run simulation and advance one tick
        sc_start(sc_time::from_value(1));

        // Reset CW sources
        for (auto cws: all_cws)
        {
            cws->reset.write(sc_logic(1));
        }

        //printOPAnalysisResult();
    }
}

void SPECSConfig::runTRANAnalysis()
{
    auto all_probes = sc_get_all_module_by_type<Probe>();
    auto all_mlprobes = sc_get_all_module_by_type<MLambdaProbe>();
    auto all_photodetectors = sc_get_all_module_by_type<Detector>();
    auto all_oop = sc_get_all_module_by_type<OpticalOutputPort>();
    auto all_vl_src = sc_get_all_module_by_type<VLSource>();
    auto all_evl_src = sc_get_all_module_by_type<EVLSource>();

    // Run OP analysis
    runOPAnalysis();

    // Set values of signals according to IC directive
    for (auto &ic_order : ic_orders)
    {
        //ic_order.second.first->write(ic_order.second.second);
    }

    // Enable probes
    for (auto probe: all_probes) {
        probe->enable = sc_logic(1);
    }

    // Enable ML probes
    for (auto mlprobe: all_mlprobes) {
        mlprobe->enable = sc_logic(1);
    }

    // Enable photodetectors
    for (auto pdet: all_photodetectors) {
        pdet->enable = sc_logic(1);
    }

    // Re-apply configuration of all ports
    for (auto oop: all_oop) {
        oop->applyConfig();
    }

    // Activate TRAN sources
    for (auto src: all_vl_src)
        src->enable = sc_logic(1);
    for (auto src: all_evl_src)
        src->enable = sc_logic(1);

    // Strart TRAN simulation
    if (isfinite(tran_duration))
        sc_start(tran_duration, SC_SEC);
    else
        sc_start();

    cout << "Simulated " << sc_time_stamp() << endl;
}

void SPECSConfig::applyDefaultOpticalOutputPortConfig() {
    //assert(!oop_configs.empty());
    auto oop_default_config = make_shared<OpticalOutputPortConfig>();
    oop_default_config->m_mode = simulation_mode;
    oop_default_config->m_abstol = default_abstol;
    oop_default_config->m_reltol = default_reltol;
    oop_default_config->m_timestep_value = default_resolution_multiplier; // relative to systemc timestep

    // apply default config to all optical output ports which don't have one
    auto all_oop = sc_get_all_module_by_type<OpticalOutputPort>();
    for (auto oop: all_oop) {
        if(!oop->getConfig().get())
            oop->setConfig(oop_default_config);
        // FIXME: only works for first call to sc_start !!
    }
}

void SPECSConfig::applyDefaultTraceFileToAllSignals() {
    if (!default_trace_file)
        return;

    auto all_probes = sc_get_all_object_by_type<Probe>();
    for (auto p: all_probes) {
        p->setTraceFile(default_trace_file);
    }

    // auto all_mlprobes = sc_get_all_object_by_type<MLambdaProbe>();
    // for (auto p: all_mlprobes) {
    //     p->m_Tf = default_trace_file;
    //     p->prepare();
    // }

    auto all_mlambda_probes = sc_get_all_object_by_type<MLambdaProbe>();
    for (auto p: all_mlambda_probes) {
        p->setTraceFile(default_trace_file);
    }

    if (trace_all_optical_nets)
    {
        auto all_optical_sigs = sc_get_all_object_by_type<sc_signal<OpticalSignal, SC_MANY_WRITERS>>();
        for (auto &sig: all_optical_sigs) {
            string signame = sig->name();

            // Check if the signal is explicitly marked as uninteresting
            stringstream ss (signame);
            string base_signame;
            while (getline (ss, base_signame, '/')) {}
            if (base_signame.empty() || base_signame[0] == '_')
                continue;
            //cout << signame << endl;

            auto p = make_shared<Probe>(("PROBE{" + signame + "}").c_str(), true, true, true, true);
            p->setTraceFile(default_trace_file);
            p->p_in(*sig);
            additional_objects.push_back(p);
        }
    }
    // TODO: refactor ↓
    auto all_pdets = sc_get_all_object_by_type<Detector>();
    for (auto &pdet: all_pdets) {
        string detname = pdet->name();
        cout << detname << endl;
        pdet->trace(default_trace_file);
    }

    auto all_pwr_meters = sc_get_all_object_by_type<PowerMeter>();
    for (auto &pwr_meter: all_pwr_meters) {
        string pwr_meter_name = pwr_meter->name();
        cout << pwr_meter_name << endl;
        pwr_meter->trace(default_trace_file);
    }
}

string SPECSConfig::analysisTypeDesc() const
{
    switch (analysis_type) {
        case CW_OPERATING_POINT:
            return "CW OPERATING POINT";
        case CW_SWEEP:
            return "CW SWEEP";
        case TIME_DOMAIN:
            return "TIME DOMAIN";
        default:
            return "UNDEFINED";
    }
}

void SPECSConfig::printConfig() const
{
    cout << "Current SPECS config: " << endl;
    cout << "- simulation mode: " << oopPortMode2str(simulation_mode) << endl;
    cout << "- analysis type: " << analysisTypeDesc() << endl;
    cout << "- engine timescale (log10 of s): " << engine_timescale << endl;
    cout << "- abstol: " << default_abstol << endl;
    cout << "- default reltol: " << default_reltol << endl;
    cout << "- resolution multiplier: " << default_resolution_multiplier << endl;
    cout << "- trace all optical nets: " << trace_all_optical_nets << endl;
}

void SPECSConfig::printOPAnalysisResult() const
{
    cout << "╔═══════════════════╗" << endl;
    cout << "║  OPERATING POINT  ║" << endl;
    cout << "╠═══════════════════╣" << endl;
    cout << "║                   ╨───┄┄" << endl;
    auto all_optical_sigs = sc_get_all_object_by_type<sc_signal<OpticalSignal, SC_MANY_WRITERS>>();
    for (auto &sig: all_optical_sigs) {
        string signame = sig->name();
        cout << "║ ";
        cout << "- " << signame << " (OPTICAL)" << " = " << sig->read() << endl;
    }
    cout << "║                   ╥───┄┄" << endl;
    cout << "╚═══════════════════╝" << endl;
}

void SPECSConfig::prepareSimulation() {
    auto all_spx_mod = sc_get_all_object_by_type<spx_module>();
    for (auto &mod : all_spx_mod)
        mod->init();
    printConfig();
    verifyConfig();

    { // open default trace_file
        if (!default_trace_file && trace_filename.size())
            default_trace_file = sc_create_vcd_trace_file(trace_filename.c_str());

        default_trace_file->set_time_unit(std::pow(10, 15 + engine_timescale), SC_FS);
    }

    applyDefaultOpticalOutputPortConfig();
    applyDefaultTraceFileToAllSignals();
    if (default_trace_file)
        sc_trace(default_trace_file, *this, "");
}

inline void sc_trace(sc_trace_file *tf, const SPECSConfig &s, string parent_tree)
{
    parent_tree += (parent_tree.size() ? "." : "");
    sc_trace(tf, s.default_abstol, parent_tree + "abstol");
    sc_trace(tf, s.default_reltol, parent_tree + "reltol");
    sc_trace(tf, (int&)s.default_resolution_multiplier, parent_tree + "resolution_multiplier");
    sc_trace(tf, (int&)s.engine_timescale, parent_tree + "engine_timescale");
    sc_trace(tf, (int&)s.simulation_mode, parent_tree + "simulation_mode");
    sc_trace(tf, (int&)s.analysis_type, parent_tree + "analysis_type");
    sc_trace(tf, s.trace_all_optical_nets, parent_tree + "trace_all_optical_nets");
    sc_trace(tf, s.verbose_component_initialization, parent_tree + "verbose_component_initialization");
}