io.hpp 43.2 KB
Newer Older
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1
2
3
4
5
6
7
8
9
10
// Copyright (c) 2020 INRA Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef ORG_VLEPROJECT_IRRITATOR_IO_2020
#define ORG_VLEPROJECT_IRRITATOR_IO_2020

#include <irritator/core.hpp>

#include <algorithm>
11
12
#include <istream>
#include <ostream>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
13
#include <vector>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
14
15
16

namespace irt {

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
static inline const char* dynamics_type_names[] = { "none",
                                                    "qss1_integrator",
                                                    "qss1_multiplier",
                                                    "qss1_cross",
                                                    "qss1_power",
                                                    "qss1_square",
                                                    "qss1_sum_2",
                                                    "qss1_sum_3",
                                                    "qss1_sum_4",
                                                    "qss1_wsum_2",
                                                    "qss1_wsum_3",
                                                    "qss1_wsum_4",
                                                    "qss2_integrator",
                                                    "qss2_multiplier",
                                                    "qss2_cross",
                                                    "qss2_power",
                                                    "qss2_square",
                                                    "qss2_sum_2",
                                                    "qss2_sum_3",
                                                    "qss2_sum_4",
                                                    "qss2_wsum_2",
                                                    "qss2_wsum_3",
                                                    "qss2_wsum_4",
                                                    "qss3_integrator",
                                                    "qss3_multiplier",
                                                    "qss3_cross",
                                                    "qss3_power",
                                                    "qss3_square",
                                                    "qss3_sum_2",
                                                    "qss3_sum_3",
                                                    "qss3_sum_4",
                                                    "qss3_wsum_2",
                                                    "qss3_wsum_3",
                                                    "qss3_wsum_4",
                                                    "integrator",
                                                    "quantifier",
                                                    "adder_2",
                                                    "adder_3",
                                                    "adder_4",
                                                    "mult_2",
                                                    "mult_3",
                                                    "mult_4",
                                                    "counter",
                                                    "generator",
                                                    "constant",
                                                    "cross",
                                                    "time_func",
                                                    "accumulator_2",
                                                    "flow" };

static_assert(std::size(dynamics_type_names) ==
              static_cast<sz>(dynamics_type_size()));

static inline const char* str_empty[] = { "" };
static inline const char* str_integrator[] = { "x-dot", "reset" };
static inline const char* str_adaptative_integrator[] = { "quanta",
                                                          "x-dot",
                                                          "reset" };
static inline const char* str_in_1[] = { "in" };
static inline const char* str_in_2[] = { "in-1", "in-2" };
static inline const char* str_in_3[] = { "in-1", "in-2", "in-3" };
static inline const char* str_in_4[] = { "in-1", "in-2", "in-3", "in-4" };
static inline const char* str_value_if_else[] = { "value",
                                                  "if",
                                                  "else",
                                                  "threshold" };
static inline const char* str_in_2_nb_2[] = { "in-1", "in-2", "nb-1", "nb-2" };

template<typename Dynamics>
static constexpr const char**
get_input_port_names() noexcept
{
    if constexpr (std::is_same_v<Dynamics, none>)
        return str_empty;

    if constexpr (std::is_same_v<Dynamics, qss1_integrator> ||
                  std::is_same_v<Dynamics, qss2_integrator> ||
                  std::is_same_v<Dynamics, qss3_integrator>)
        return str_integrator;

    if constexpr (std::is_same_v<Dynamics, qss1_multiplier> ||
                  std::is_same_v<Dynamics, qss1_sum_2> ||
                  std::is_same_v<Dynamics, qss1_wsum_2> ||
                  std::is_same_v<Dynamics, qss2_multiplier> ||
                  std::is_same_v<Dynamics, qss2_sum_2> ||
                  std::is_same_v<Dynamics, qss2_wsum_2> ||
                  std::is_same_v<Dynamics, qss3_multiplier> ||
                  std::is_same_v<Dynamics, qss3_sum_2> ||
                  std::is_same_v<Dynamics, qss3_wsum_2> ||
                  std::is_same_v<Dynamics, adder_2> ||
                  std::is_same_v<Dynamics, mult_2>)
        return str_in_2;

    if constexpr (std::is_same_v<Dynamics, qss1_sum_3> ||
                  std::is_same_v<Dynamics, qss1_wsum_3> ||
                  std::is_same_v<Dynamics, qss2_sum_3> ||
                  std::is_same_v<Dynamics, qss2_wsum_3> ||
                  std::is_same_v<Dynamics, qss3_sum_3> ||
                  std::is_same_v<Dynamics, qss3_wsum_3> ||
                  std::is_same_v<Dynamics, adder_3> ||
                  std::is_same_v<Dynamics, mult_3>)
        return str_in_3;

    if constexpr (std::is_same_v<Dynamics, qss1_sum_4> ||
                  std::is_same_v<Dynamics, qss1_wsum_4> ||
                  std::is_same_v<Dynamics, qss2_sum_4> ||
                  std::is_same_v<Dynamics, qss2_wsum_4> ||
                  std::is_same_v<Dynamics, qss3_sum_4> ||
                  std::is_same_v<Dynamics, qss3_wsum_4> ||
                  std::is_same_v<Dynamics, adder_4> ||
                  std::is_same_v<Dynamics, mult_4>)
        return str_in_4;

    if constexpr (std::is_same_v<Dynamics, integrator>)
        return str_adaptative_integrator;

    if constexpr (std::is_same_v<Dynamics, quantifier> ||
                  std::is_same_v<Dynamics, counter> ||
                  std::is_same_v<Dynamics, qss1_power> ||
                  std::is_same_v<Dynamics, qss2_power> ||
                  std::is_same_v<Dynamics, qss3_power> ||
                  std::is_same_v<Dynamics, qss1_square> ||
                  std::is_same_v<Dynamics, qss2_square> ||
                  std::is_same_v<Dynamics, qss3_square>)
        return str_in_1;

    if constexpr (std::is_same_v<Dynamics, generator> ||
                  std::is_same_v<Dynamics, constant> ||
                  std::is_same_v<Dynamics, time_func> ||
                  std::is_same_v<Dynamics, flow>)
        return str_empty;

    if constexpr (std::is_same_v<Dynamics, qss1_cross> ||
                  std::is_same_v<Dynamics, qss2_cross> ||
                  std::is_same_v<Dynamics, qss3_cross> ||
                  std::is_same_v<Dynamics, cross>)
        return str_value_if_else;

    if constexpr (std::is_same_v<Dynamics, accumulator_2>)
        return str_in_2_nb_2;

    irt_unreachable();
}

static constexpr const char**
get_input_port_names(const dynamics_type type) noexcept
{
    switch (type) {
    case dynamics_type::none:
        return str_empty;

    case dynamics_type::qss1_integrator:
    case dynamics_type::qss2_integrator:
    case dynamics_type::qss3_integrator:
        return str_integrator;

    case dynamics_type::qss1_multiplier:
    case dynamics_type::qss1_sum_2:
    case dynamics_type::qss1_wsum_2:
    case dynamics_type::qss2_multiplier:
    case dynamics_type::qss2_sum_2:
    case dynamics_type::qss2_wsum_2:
    case dynamics_type::qss3_multiplier:
    case dynamics_type::qss3_sum_2:
    case dynamics_type::qss3_wsum_2:
    case dynamics_type::adder_2:
    case dynamics_type::mult_2:
        return str_in_2;

    case dynamics_type::qss1_sum_3:
    case dynamics_type::qss1_wsum_3:
    case dynamics_type::qss2_sum_3:
    case dynamics_type::qss2_wsum_3:
    case dynamics_type::qss3_sum_3:
    case dynamics_type::qss3_wsum_3:
    case dynamics_type::adder_3:
    case dynamics_type::mult_3:
        return str_in_3;

    case dynamics_type::qss1_sum_4:
    case dynamics_type::qss1_wsum_4:
    case dynamics_type::qss2_sum_4:
    case dynamics_type::qss2_wsum_4:
    case dynamics_type::qss3_sum_4:
    case dynamics_type::qss3_wsum_4:
    case dynamics_type::adder_4:
    case dynamics_type::mult_4:
        return str_in_4;

    case dynamics_type::integrator:
        return str_adaptative_integrator;

    case dynamics_type::quantifier:
    case dynamics_type::counter:
    case dynamics_type::qss1_power:
    case dynamics_type::qss2_power:
    case dynamics_type::qss3_power:
    case dynamics_type::qss1_square:
    case dynamics_type::qss2_square:
    case dynamics_type::qss3_square:
        return str_in_1;

    case dynamics_type::generator:
    case dynamics_type::constant:
    case dynamics_type::time_func:
    case dynamics_type::flow:
        return str_empty;

    case dynamics_type::qss1_cross:
    case dynamics_type::qss2_cross:
    case dynamics_type::qss3_cross:
    case dynamics_type::cross:
        return str_value_if_else;

    case dynamics_type::accumulator_2:
        return str_in_2_nb_2;
    }

    irt_unreachable();
}

static inline const char* str_out_1[] = { "out" };
static inline const char* str_out_cross[] = { "if-value",
                                              "else-value",
                                              "event" };

template<typename Dynamics>
static constexpr const char**
get_output_port_names() noexcept
{
    if constexpr (std::is_same_v<Dynamics, none>)
        return str_empty;

    if constexpr (std::is_same_v<Dynamics, qss1_integrator> ||
                  std::is_same_v<Dynamics, qss1_multiplier> ||
                  std::is_same_v<Dynamics, qss1_power> ||
                  std::is_same_v<Dynamics, qss1_square> ||
                  std::is_same_v<Dynamics, qss1_sum_2> ||
                  std::is_same_v<Dynamics, qss1_sum_3> ||
                  std::is_same_v<Dynamics, qss1_sum_4> ||
                  std::is_same_v<Dynamics, qss1_wsum_2> ||
                  std::is_same_v<Dynamics, qss1_wsum_3> ||
                  std::is_same_v<Dynamics, qss1_wsum_4> ||
                  std::is_same_v<Dynamics, qss2_integrator> ||
                  std::is_same_v<Dynamics, qss2_multiplier> ||
                  std::is_same_v<Dynamics, qss2_power> ||
                  std::is_same_v<Dynamics, qss2_square> ||
                  std::is_same_v<Dynamics, qss2_sum_2> ||
                  std::is_same_v<Dynamics, qss2_sum_3> ||
                  std::is_same_v<Dynamics, qss2_sum_4> ||
                  std::is_same_v<Dynamics, qss2_wsum_2> ||
                  std::is_same_v<Dynamics, qss2_wsum_3> ||
                  std::is_same_v<Dynamics, qss2_wsum_4> ||
                  std::is_same_v<Dynamics, qss3_integrator> ||
                  std::is_same_v<Dynamics, qss3_multiplier> ||
                  std::is_same_v<Dynamics, qss3_power> ||
                  std::is_same_v<Dynamics, qss3_square> ||
                  std::is_same_v<Dynamics, qss3_sum_2> ||
                  std::is_same_v<Dynamics, qss3_sum_3> ||
                  std::is_same_v<Dynamics, qss3_sum_4> ||
                  std::is_same_v<Dynamics, qss3_wsum_2> ||
                  std::is_same_v<Dynamics, qss3_wsum_3> ||
                  std::is_same_v<Dynamics, qss3_wsum_4> ||
                  std::is_same_v<Dynamics, integrator> ||
                  std::is_same_v<Dynamics, quantifier> ||
                  std::is_same_v<Dynamics, adder_2> ||
                  std::is_same_v<Dynamics, adder_3> ||
                  std::is_same_v<Dynamics, adder_4> ||
                  std::is_same_v<Dynamics, mult_2> ||
                  std::is_same_v<Dynamics, mult_3> ||
                  std::is_same_v<Dynamics, mult_4> ||
                  std::is_same_v<Dynamics, counter> ||
                  std::is_same_v<Dynamics, generator> ||
                  std::is_same_v<Dynamics, constant> ||
                  std::is_same_v<Dynamics, time_func> ||
                  std::is_same_v<Dynamics, flow>)
        return str_out_1;

    if constexpr (std::is_same_v<Dynamics, cross> ||
                  std::is_same_v<Dynamics, qss1_cross> ||
                  std::is_same_v<Dynamics, qss2_cross> ||
                  std::is_same_v<Dynamics, qss3_cross>)
        return str_out_cross;

    if constexpr (std::is_same_v<Dynamics, accumulator_2>)
        return str_empty;

    irt_unreachable();
}

static constexpr const char**
get_output_port_names(const dynamics_type type) noexcept
{
    switch (type) {
    case dynamics_type::none:
        return str_empty;

    case dynamics_type::qss1_integrator:
    case dynamics_type::qss1_multiplier:
    case dynamics_type::qss1_power:
    case dynamics_type::qss1_square:
    case dynamics_type::qss1_sum_2:
    case dynamics_type::qss1_sum_3:
    case dynamics_type::qss1_sum_4:
    case dynamics_type::qss1_wsum_2:
    case dynamics_type::qss1_wsum_3:
    case dynamics_type::qss1_wsum_4:
    case dynamics_type::qss2_integrator:
    case dynamics_type::qss2_multiplier:
    case dynamics_type::qss2_power:
    case dynamics_type::qss2_square:
    case dynamics_type::qss2_sum_2:
    case dynamics_type::qss2_sum_3:
    case dynamics_type::qss2_sum_4:
    case dynamics_type::qss2_wsum_2:
    case dynamics_type::qss2_wsum_3:
    case dynamics_type::qss2_wsum_4:
    case dynamics_type::qss3_integrator:
    case dynamics_type::qss3_multiplier:
    case dynamics_type::qss3_power:
    case dynamics_type::qss3_square:
    case dynamics_type::qss3_sum_2:
    case dynamics_type::qss3_sum_3:
    case dynamics_type::qss3_sum_4:
    case dynamics_type::qss3_wsum_2:
    case dynamics_type::qss3_wsum_3:
    case dynamics_type::qss3_wsum_4:
    case dynamics_type::integrator:
    case dynamics_type::quantifier:
    case dynamics_type::adder_2:
    case dynamics_type::adder_3:
    case dynamics_type::adder_4:
    case dynamics_type::mult_2:
    case dynamics_type::mult_3:
    case dynamics_type::mult_4:
    case dynamics_type::counter:
    case dynamics_type::generator:
    case dynamics_type::constant:
    case dynamics_type::time_func:
    case dynamics_type::flow:
        return str_out_1;

    case dynamics_type::cross:
    case dynamics_type::qss1_cross:
    case dynamics_type::qss2_cross:
    case dynamics_type::qss3_cross:
        return str_out_cross;

    case dynamics_type::accumulator_2:
        return str_empty;
    }

    irt_unreachable();
}

Gauthier Quesnel's avatar
Gauthier Quesnel committed
372
373
374
class reader
{
private:
375
    std::istream& is;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
376

377
    std::vector<model_id> map;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
378
379
    int model_error = 0;
    int connection_error = 0;
380
    int model_number = 0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
381

382
383
384
    char temp_1[32];
    char temp_2[32];

Gauthier Quesnel's avatar
Gauthier Quesnel committed
385
public:
386
387
    reader(std::istream& is_) noexcept
      : is(is_)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
388
389
    {}

390
    ~reader() noexcept = default;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
391
392
393

    status operator()(simulation& sim) noexcept
    {
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
        irt_return_if_bad(do_read_model_number());

        for (int i = 0; i != model_number; ++i, ++model_error) {
            int id;
            do_read_model(sim, &id);
        }

        irt_return_if_bad(do_read_connections(sim));

        return status::success;
    }

    template<typename CallBackFunction>
    status operator()(simulation& sim, CallBackFunction f) noexcept
    {
        irt_return_if_bad(do_read_model_number());

        for (int i = 0; i != model_number; ++i, ++model_error) {
            int id;
            irt_return_if_bad(do_read_model(sim, &id));
            f(map[id]);
        }

        irt_return_if_bad(do_read_connections(sim));

        return status::success;
    }

private:
    status do_read_model_number() noexcept
    {
        model_number = 0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
426

427
        irt_return_if_fail((is >> model_number), status::io_file_format_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
428
429
430
        irt_return_if_fail(model_number > 0,
                           status::io_file_format_model_number_error);

431
432
433
434
435
        try {
            map.resize(model_number, model_id{ 0 });
        } catch (const std::bad_alloc& /*e*/) {
            return status::io_not_enough_memory;
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
436

437
438
        return status::success;
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
439

440
    status do_read_model(simulation& sim, int* id) noexcept
441
442
443
    {
        irt_return_if_fail((is >> *id >> temp_1),
                           status::io_file_format_model_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
444

445
446
        irt_return_if_fail(0 <= *id && *id < model_number,
                           status::io_file_format_model_error);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
447

448
        irt_return_if_bad(do_read_dynamics(sim, *id, temp_1));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
449

450
451
452
453
454
        return status::success;
    }

    status do_read_connections(simulation& sim) noexcept
    {
455
        while (is) {
456
            int mdl_src_id, port_src_index, mdl_dst_id, port_dst_index;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
457

458
            if (!(is >> mdl_src_id >> port_src_index >> mdl_dst_id >>
459
460
461
                  port_dst_index)) {
                if (is.eof())
                    break;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
462

463
464
                irt_bad_return(status::io_file_format_error);
            }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
465

466
467
468
469
470
471
            irt_return_if_fail(0 <= mdl_src_id && mdl_src_id < model_number,
                               status::io_file_format_model_error);
            irt_return_if_fail(0 <= mdl_dst_id && mdl_dst_id < model_number,
                               status::io_file_format_model_error);

            auto* mdl_src = sim.models.try_to_get(map[mdl_src_id]);
472
            irt_return_if_fail(mdl_src, status::io_file_format_model_unknown);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
473

474
            auto* mdl_dst = sim.models.try_to_get(map[mdl_dst_id]);
475
            irt_return_if_fail(mdl_dst, status::io_file_format_model_unknown);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
476

477
478
            output_port_id output_port;
            input_port_id input_port;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
479

480
481
482
483
            irt_return_if_bad(
              sim.get_output_port_id(*mdl_src, port_src_index, &output_port));
            irt_return_if_bad(
              sim.get_input_port_id(*mdl_dst, port_dst_index, &input_port));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
484

485
486
            irt_return_if_bad(sim.connect(output_port, input_port));
            ++connection_error;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
487
488
489
490
491
        }

        return status::success;
    }

492
493
    bool convert(const std::string_view dynamics_name,
                 dynamics_type* type) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
494
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
        struct string_to_type
        {
            constexpr string_to_type(const std::string_view n,
                                     const dynamics_type t)
              : name(n)
              , type(t)
            {}

            const std::string_view name;
            dynamics_type type;
        };

        static constexpr string_to_type table[] = {
            { "accumulator_2", dynamics_type::accumulator_2 },
            { "adder_2", dynamics_type::adder_2 },
            { "adder_3", dynamics_type::adder_3 },
            { "adder_4", dynamics_type::adder_4 },
            { "constant", dynamics_type::constant },
            { "counter", dynamics_type::counter },
            { "cross", dynamics_type::cross },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
515
            { "flow", dynamics_type::flow },
516
            { "generator", dynamics_type::generator },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
517
518
519
520
521
            { "integrator", dynamics_type::integrator },
            { "mult_2", dynamics_type::mult_2 },
            { "mult_3", dynamics_type::mult_3 },
            { "mult_4", dynamics_type::mult_4 },
            { "none", dynamics_type::none },
522
            { "qss1_cross", dynamics_type::qss1_cross },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
523
            { "qss1_integrator", dynamics_type::qss1_integrator },
524
            { "qss1_multiplier", dynamics_type::qss1_multiplier },
525
526
            { "qss1_power", dynamics_type::qss1_power },
            { "qss1_square", dynamics_type::qss1_square },
527
528
529
530
531
532
            { "qss1_sum_2", dynamics_type::qss1_sum_2 },
            { "qss1_sum_3", dynamics_type::qss1_sum_3 },
            { "qss1_sum_4", dynamics_type::qss1_sum_4 },
            { "qss1_wsum_2", dynamics_type::qss1_wsum_2 },
            { "qss1_wsum_3", dynamics_type::qss1_wsum_3 },
            { "qss1_wsum_4", dynamics_type::qss1_wsum_4 },
533
            { "qss2_cross", dynamics_type::qss2_cross },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
534
535
            { "qss2_integrator", dynamics_type::qss2_integrator },
            { "qss2_multiplier", dynamics_type::qss2_multiplier },
536
537
            { "qss2_power", dynamics_type::qss2_power },
            { "qss2_square", dynamics_type::qss2_square },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
538
539
540
541
542
543
            { "qss2_sum_2", dynamics_type::qss2_sum_2 },
            { "qss2_sum_3", dynamics_type::qss2_sum_3 },
            { "qss2_sum_4", dynamics_type::qss2_sum_4 },
            { "qss2_wsum_2", dynamics_type::qss2_wsum_2 },
            { "qss2_wsum_3", dynamics_type::qss2_wsum_3 },
            { "qss2_wsum_4", dynamics_type::qss2_wsum_4 },
544
            { "qss3_cross", dynamics_type::qss3_cross },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
545
546
            { "qss3_integrator", dynamics_type::qss3_integrator },
            { "qss3_multiplier", dynamics_type::qss3_multiplier },
547
548
            { "qss3_power", dynamics_type::qss3_power },
            { "qss3_square", dynamics_type::qss3_square },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
549
550
551
552
553
554
            { "qss3_sum_2", dynamics_type::qss3_sum_2 },
            { "qss3_sum_3", dynamics_type::qss3_sum_3 },
            { "qss3_sum_4", dynamics_type::qss3_sum_4 },
            { "qss3_wsum_2", dynamics_type::qss3_wsum_2 },
            { "qss3_wsum_3", dynamics_type::qss3_wsum_3 },
            { "qss3_wsum_4", dynamics_type::qss3_wsum_4 },
555
            { "quantifier", dynamics_type::quantifier },
Gauthier Quesnel's avatar
Gauthier Quesnel committed
556
557
558
            { "time_func", dynamics_type::time_func }
        };

559
560
        static_assert(std::size(table) ==
                      static_cast<sz>(dynamics_type_size()));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
561
562
563
564
565
566
567
568
569
570

        const auto it =
          std::lower_bound(std::begin(table),
                           std::end(table),
                           dynamics_name,
                           [](const string_to_type& l,
                              const std::string_view r) { return l.name < r; });

        if (it != std::end(table) && it->name == dynamics_name) {
            *type = it->type;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
571
572
573
574
575
576
            return true;
        }

        return false;
    }

577
578
579
    status do_read_dynamics(simulation& sim,
                            int id,
                            const char* dynamics_name) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
580
581
582
583
584
585
    {
        dynamics_type type;

        irt_return_if_fail(convert(dynamics_name, &type),
                           status::io_file_format_dynamics_unknown);

586
587
        model_id mdl_id = static_cast<model_id>(0);
        auto ret = sim.dispatch(type, [this, &sim, &mdl_id](auto& dyn_models) {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
588
589
590
591
592
            irt_return_if_fail(dyn_models.can_alloc(1),
                               status::io_file_format_dynamics_limit_reach);
            auto& dyn = dyn_models.alloc();
            auto dyn_id = dyn_models.get_id(dyn);

593
            sim.alloc(dyn, dyn_id);
594
            mdl_id = dyn.id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
595
596
597
598
599
600
601

            irt_return_if_fail(read(dyn),
                               status::io_file_format_dynamics_init_error);

            return status::success;
        });

Gauthier Quesnel's avatar
Gauthier Quesnel committed
602
603
        irt_return_if_bad(ret);

604
        map[id] = mdl_id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
605
606

        return status::success;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
607
608
609
610
611
612
613
    }

    bool read(none& /*dyn*/) noexcept
    {
        return true;
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
    bool read(qss1_integrator& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

    bool read(qss2_integrator& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
630
631
632
633
634
635
636
637
    bool read(qss3_integrator& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_X));
        double& x2 = *(const_cast<double*>(&dyn.default_dQ));

        return !!(is >> x1 >> x2);
    }

638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
    bool read(qss1_multiplier& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_sum_2& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_sum_3& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_sum_4& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss1_wsum_2& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

    bool read(qss1_wsum_3& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

    bool read(qss1_wsum_4& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
684

Gauthier Quesnel's avatar
Gauthier Quesnel committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
    bool read(qss2_multiplier& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_sum_2& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_sum_3& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_sum_4& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_wsum_2& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

    bool read(qss2_wsum_3& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

    bool read(qss2_wsum_4& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
    bool read(qss3_multiplier& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_sum_2& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_sum_3& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_sum_4& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_wsum_2& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));

        return !!(is >> x1 >> x2);
    }

    bool read(qss3_wsum_3& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3);
    }

    bool read(qss3_wsum_4& dyn) noexcept
    {
        double& x1 = *(const_cast<double*>(&dyn.default_input_coeffs[0]));
        double& x2 = *(const_cast<double*>(&dyn.default_input_coeffs[1]));
        double& x3 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));
        double& x4 = *(const_cast<double*>(&dyn.default_input_coeffs[2]));

        return !!(is >> x1 >> x2 >> x3 >> x4);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
779
780
    bool read(integrator& dyn) noexcept
    {
781
        return !!(is >> dyn.default_current_value >> dyn.default_reset_value);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
782
783
784
785
    }

    bool read(quantifier& dyn) noexcept
    {
786
787
        if (!(is >> dyn.default_step_size >> dyn.default_past_length >>
              temp_1 >> temp_2))
Gauthier Quesnel's avatar
Gauthier Quesnel committed
788
789
            return false;

790
        if (std::strcmp(temp_1, "possible") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
791
            dyn.default_adapt_state = quantifier::adapt_state::possible;
792
        else if (std::strcmp(temp_1, "impossible") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
793
            dyn.default_adapt_state = quantifier::adapt_state::impossible;
794
        else if (std::strcmp(temp_1, "done") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
795
796
797
798
            dyn.default_adapt_state = quantifier::adapt_state::done;
        else
            return false;

799
        if (std::strcmp(temp_2, "true") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
800
            dyn.default_zero_init_offset = true;
801
        else if (std::strcmp(temp_2, "false") == 0)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
802
803
804
805
806
807
808
809
810
            dyn.default_zero_init_offset = false;
        else
            return false;

        return true;
    }

    bool read(adder_2& dyn) noexcept
    {
811
812
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
813
814
815
816
    }

    bool read(adder_3& dyn) noexcept
    {
817
818
819
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_input_coeffs[0] >>
                  dyn.default_input_coeffs[1] >> dyn.default_input_coeffs[2]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
820
821
822
823
    }

    bool read(adder_4& dyn) noexcept
    {
824
825
826
827
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_values[3] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1] >>
                  dyn.default_input_coeffs[2] >> dyn.default_input_coeffs[3]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
828
829
830
831
    }

    bool read(mult_2& dyn) noexcept
    {
832
833
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
834
835
836
837
    }

    bool read(mult_3& dyn) noexcept
    {
838
839
840
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_input_coeffs[0] >>
                  dyn.default_input_coeffs[1] >> dyn.default_input_coeffs[2]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
841
842
843
844
    }

    bool read(mult_4& dyn) noexcept
    {
845
846
847
848
        return !!(is >> dyn.default_values[0] >> dyn.default_values[1] >>
                  dyn.default_values[2] >> dyn.default_values[3] >>
                  dyn.default_input_coeffs[0] >> dyn.default_input_coeffs[1] >>
                  dyn.default_input_coeffs[2] >> dyn.default_input_coeffs[3]);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
849
850
851
852
853
854
855
    }

    bool read(counter& /*dyn*/) noexcept
    {
        return true;
    }

856
    bool read(generator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
857
    {
858
        return !!(is >> dyn.default_value >> dyn.default_period >>
859
                  dyn.default_offset);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
860
861
862
863
    }

    bool read(constant& dyn) noexcept
    {
864
        return !!(is >> dyn.default_value);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
865
866
    }

867
868
869
870
871
872
873
874
875
876
    bool read(qss1_cross& dyn) noexcept
    {
        return !!(is >> dyn.default_threshold);
    }

    bool read(qss2_cross& dyn) noexcept
    {
        return !!(is >> dyn.default_threshold);
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
877
878
879
880
881
    bool read(qss3_cross& dyn) noexcept
    {
        return !!(is >> dyn.default_threshold);
    }

882
    bool read(qss1_power& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
883
    {
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
        return !!(is >> dyn.default_n);
    }

    bool read(qss2_power& dyn) noexcept
    {
        return !!(is >> dyn.default_n);
    }

    bool read(qss3_power& dyn) noexcept
    {
        return !!(is >> dyn.default_n);
    }

    bool read(qss1_square& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss2_square& /*dyn*/) noexcept
    {
        return true;
    }

    bool read(qss3_square& /*dyn*/) noexcept
    {
        return true;
    }

912
    bool read(cross& dyn) noexcept
913
    {
914
        return !!(is >> dyn.default_threshold);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
915
916
    }

917
918
919
920
921
    bool read(accumulator_2& /*dyn*/) noexcept
    {
        return true;
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
922
923
    bool read(time_func& dyn) noexcept
    {
924
        if (!(is >> temp_1))
Gauthier Quesnel's avatar
Gauthier Quesnel committed
925
926
            return false;

927
        if (std::strcmp(temp_1, "square") == 0)
928
            dyn.default_f = &square_time_function;
929
        else
930
            dyn.default_f = &time_function;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
931
932
933

        return true;
    }
934
935
936

    bool read(flow& dyn) noexcept
    {
937
        return !!(is >> dyn.default_samplerate);
938
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
939
940
941
942
};

struct writer
{
943
    std::ostream& os;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
944

945
    std::vector<model_id> map;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
946

947
948
    writer(std::ostream& os_) noexcept
      : os(os_)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
949
950
951
952
    {}

    status operator()(const simulation& sim) noexcept
    {
953
        os << sim.models.size() << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
954

955
956
957
958
959
        try {
            map.resize(sim.models.size(), model_id{ 0 });
        } catch (const std::bad_alloc& /*e*/) {
            return status::io_not_enough_memory;
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
960

Gauthier Quesnel's avatar
Gauthier Quesnel committed
961
        model* mdl = nullptr;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
962
        int id = 0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
963
964
965
        while (sim.models.next(mdl)) {
            const auto mdl_id = sim.models.get_id(mdl);

966
            os << id << ' ';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
967
            map[id] = mdl_id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
968
969
970
971
972

            sim.dispatch(mdl->type, [this, mdl](auto& dyn_models) {
                write(dyn_models.get(mdl->id));
                return status::success;
            });
Gauthier Quesnel's avatar
Gauthier Quesnel committed
973
974

            ++id;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
975
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
976

Gauthier Quesnel's avatar
Gauthier Quesnel committed
977
978
979
980
981
982
        irt::output_port* out = nullptr;
        while (sim.output_ports.next(out)) {
            for (auto dst : out->connections) {
                if (auto* in = sim.input_ports.try_to_get(dst); in) {
                    auto* mdl_src = sim.models.try_to_get(out->model);
                    auto* mdl_dst = sim.models.try_to_get(in->model);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
983

Gauthier Quesnel's avatar
Gauthier Quesnel committed
984
985
                    if (!(mdl_src && mdl_dst))
                        continue;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
986

Gauthier Quesnel's avatar
Gauthier Quesnel committed
987
988
                    int src_index = -1;
                    int dst_index = -1;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
989

Gauthier Quesnel's avatar
Gauthier Quesnel committed
990
                    irt_return_if_bad(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
991
                      sim.get_input_port_index(*mdl_dst, dst, &dst_index));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
992

Gauthier Quesnel's avatar
Gauthier Quesnel committed
993
                    irt_return_if_bad(sim.get_output_port_index(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
994
995
996
997
998
999
1000
                      *mdl_src, sim.output_ports.get_id(out), &src_index));

                    auto it_out = std::find(map.begin(), map.end(), out->model);
                    auto it_in = std::find(map.begin(), map.end(), in->model);

                    assert(it_out != map.end());
                    assert(it_in != map.end());
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1001

1002
                    os << std::distance(map.begin(), it_out) << ' ' << src_index
1003
1004
                       << ' ' << std::distance(map.begin(), it_in) << ' '
                       << dst_index << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1005
1006
1007
1008
1009
1010
1011
1012
                }
            }
        }

        return status::success;
    }

private:
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1013
    void write(const none& /*dyn*/) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1014
    {
1015
        os << "none\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1016
1017
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
    void write(const qss1_integrator& dyn) noexcept
    {
        os << "qss1_integrator " << dyn.default_X << ' ' << dyn.default_dQ
           << '\n';
    }

    void write(const qss2_integrator& dyn) noexcept
    {
        os << "qss2_integrator " << dyn.default_X << ' ' << dyn.default_dQ
           << '\n';
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1030
1031
1032
1033
1034
1035
    void write(const qss3_integrator& dyn) noexcept
    {
        os << "qss3_integrator " << dyn.default_X << ' ' << dyn.default_dQ
           << '\n';
    }

1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
    void write(const qss1_multiplier& /*dyn*/) noexcept
    {
        os << "qss1_multiplier\n";
    }

    void write(const qss1_sum_2& /*dyn*/) noexcept
    {
        os << "qss1_sum_2\n";
    }

    void write(const qss1_sum_3& /*dyn*/) noexcept
    {
        os << "qss1_sum_3\n";
    }

    void write(const qss1_sum_4& /*dyn*/) noexcept
    {
        os << "qss1_sum_4\n";
    }

    void write(const qss1_wsum_2& dyn) noexcept
    {
        os << "qss1_wsum_2 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
    }

    void write(const qss1_wsum_3& dyn) noexcept
    {
        os << "qss1_wsum_3 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << '\n';
    }

    void write(const qss1_wsum_4& dyn) noexcept
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1071
        os << "qss1_wsum_4 " << dyn.default_input_coeffs[0] << ' '
1072
1073
1074
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1075

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
    void write(const qss2_multiplier& /*dyn*/) noexcept
    {
        os << "qss2_multiplier\n";
    }

    void write(const qss2_sum_2& /*dyn*/) noexcept
    {
        os << "qss2_sum_2\n";
    }

    void write(const qss2_sum_3& /*dyn*/) noexcept
    {
        os << "qss2_sum_3\n";
    }

    void write(const qss2_sum_4& /*dyn*/) noexcept
    {
        os << "qss2_sum_4\n";
    }

    void write(const qss2_wsum_2& dyn) noexcept
    {
        os << "qss2_wsum_2 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
    }

    void write(const qss2_wsum_3& dyn) noexcept
    {
        os << "qss2_wsum_3 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << '\n';
    }

    void write(const qss2_wsum_4& dyn) noexcept
    {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
        os << "qss2_wsum_4 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
    }

    void write(const qss3_multiplier& /*dyn*/) noexcept
    {
        os << "qss3_multiplier\n";
    }

    void write(const qss3_sum_2& /*dyn*/) noexcept
    {
        os << "qss3_sum_2\n";
    }

    void write(const qss3_sum_3& /*dyn*/) noexcept
    {
        os << "qss3_sum_3\n";
    }

    void write(const qss3_sum_4& /*dyn*/) noexcept
    {
        os << "qss3_sum_4\n";
    }

    void write(const qss3_wsum_2& dyn) noexcept
    {
        os << "qss3_wsum_2 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
    }

    void write(const qss3_wsum_3& dyn) noexcept
    {
        os << "qss3_wsum_3 " << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << '\n';
    }

    void write(const qss3_wsum_4& dyn) noexcept
    {
        os << "qss3_wsum_4 " << dyn.default_input_coeffs[0] << ' '
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1152
1153
1154
1155
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1156
1157
    void write(const integrator& dyn) noexcept
    {
1158
1159
        os << "integrator " << dyn.default_current_value << ' '
           << dyn.default_reset_value << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1160
1161
1162
1163
    }

    void write(const quantifier& dyn) noexcept
    {
1164
1165
1166
1167
1168
1169
1170
1171
1172
        os << "quantifier " << dyn.default_step_size << ' '
           << dyn.default_past_length << ' '
           << ((dyn.default_adapt_state == quantifier::adapt_state::possible)
                 ? "possible "
                 : dyn.default_adapt_state ==
                       quantifier::adapt_state::impossible
                     ? "impossibe "
                     : "done ")
           << (dyn.default_zero_init_offset == true ? "true\n" : "false\n");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1173
1174
1175
1176
    }

    void write(const adder_2& dyn) noexcept
    {
1177
1178
1179
        os << "adder_2 " << dyn.default_values[0] << ' '
           << dyn.default_values[1] << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1180
1181
1182
1183
    }

    void write(const adder_3& dyn) noexcept
    {
1184
1185
1186
1187
        os << "adder_3 " << dyn.default_values[0] << ' '
           << dyn.default_values[1] << ' ' << dyn.default_values[2] << ' '
           << dyn.default_input_coeffs[0] << ' ' << dyn.default_input_coeffs[1]
           << ' ' << dyn.default_input_coeffs[2] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1188
1189
1190
1191
    }

    void write(const adder_4& dyn) noexcept
    {
1192
1193
1194
1195
1196
        os << "adder_4 " << dyn.default_values[0] << ' '
           << dyn.default_values[1] << ' ' << dyn.default_values[2] << ' '
           << dyn.default_values[3] << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1197
1198
1199
1200
    }

    void write(const mult_2& dyn) noexcept
    {
1201
1202
1203
        os << "mult_2 " << dyn.default_values[0] << ' ' << dyn.default_values[1]
           << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1204
1205
1206
1207
    }

    void write(const mult_3& dyn) noexcept
    {
1208
1209
1210
1211
        os << "mult_3 " << dyn.default_values[0] << ' ' << dyn.default_values[1]
           << ' ' << dyn.default_values[2] << ' ' << dyn.default_input_coeffs[0]
           << ' ' << dyn.default_input_coeffs[1] << ' '
           << dyn.default_input_coeffs[2] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1212
1213
1214
1215
    }

    void write(const mult_4& dyn) noexcept
    {
1216
1217
1218
1219
1220
        os << "mult_4 " << dyn.default_values[0] << ' ' << dyn.default_values[1]
           << ' ' << dyn.default_values[2] << ' ' << dyn.default_values[3]
           << ' ' << dyn.default_input_coeffs[0] << ' '
           << dyn.default_input_coeffs[1] << ' ' << dyn.default_input_coeffs[2]
           << ' ' << dyn.default_input_coeffs[3] << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1221
1222
1223
1224
    }

    void write(const counter& /*dyn*/) noexcept
    {
1225
        os << "counter\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1226
1227
    }

1228
    void write(const generator& dyn) noexcept
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1229
    {
1230
1231
1232
        os << "generator " << dyn.default_value << ' ' << dyn.default_period
           << ' ' << dyn.default_offset << '\n';
        ;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1233
1234
1235
1236
    }

    void write(const constant& dyn) noexcept
    {
1237
        os << "constant " << dyn.default_value << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1238
1239
    }

1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
    void write(const qss1_cross& dyn) noexcept
    {
        os << "qss1_cross " << dyn.default_threshold << '\n';
    }

    void write(const qss2_cross& dyn) noexcept
    {
        os << "qss2_cross " << dyn.default_threshold << '\n';
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1250
1251
1252
1253
1254
    void write(const qss3_cross& dyn) noexcept
    {
        os << "qss3_cross " << dyn.default_threshold << '\n';
    }

1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
    void write(const qss1_power& dyn) noexcept
    {
        os << "qss1_power " << dyn.default_n << '\n';
    }

    void write(const qss2_power& dyn) noexcept
    {
        os << "qss2_power " << dyn.default_n << '\n';
    }

    void write(const qss3_power& dyn) noexcept
    {
        os << "qss3_power " << dyn.default_n << '\n';
    }

    void write(const qss1_square& /*dyn*/) noexcept
    {
        os << "qss1_square\n";
    }

    void write(const qss2_square& /*dyn*/) noexcept
    {
        os << "qss2_square\n";
    }

    void write(const qss3_square& /*dyn*/) noexcept
    {
        os << "qss3_square\n";
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1285
1286
    void write(const cross& dyn) noexcept
    {
1287
        os << "cross " << dyn.default_threshold << '\n';
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1288
1289
    }

1290
1291
1292
1293
1294
    void write(const accumulator_2& /*dyn*/) noexcept
    {
        os << "accumulator_2\n";
    }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1295
1296
    void write(const time_func& dyn) noexcept
    {
1297
1298
        os << "time_func "
           << (dyn.default_f == &time_function ? "time\n" : "square\n");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1299
    }
1300
1301
1302

    void write(const flow& dyn) noexcept
    {
1303
        os << "flow " << dyn.default_samplerate << '\n';
1304
    }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1305
1306
1307
1308
1309
};

class dot_writer
{
private:
1310
    std::ostream& os;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1311
1312

public:
1313
1314
    dot_writer(std::ostream& os_)
      : os(os_)
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1315
1316
1317
1318
    {}

    void operator()(const simulation& sim) noexcept
    {
1319
        os << "digraph graphname {\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331

        irt::output_port* output_port = nullptr;
        while (sim.output_ports.next(output_port)) {
            for (const irt::input_port_id dst : output_port->connections) {
                if (auto* input_port = sim.input_ports.try_to_get(dst);
                    input_port) {
                    auto* mdl_src = sim.models.try_to_get(output_port->model);
                    auto* mdl_dst = sim.models.try_to_get(input_port->model);

                    if (!(mdl_src && mdl_dst))
                        continue;

1332
                    os << irt::get_key(output_port->model);
1333
                    os << " -> ";
1334
                    os << irt::get_key(input_port->model);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1335

1336
                    os << " [label=\"";
1337
                    os << irt::get_key(sim.output_ports.get_id(*output_port));
1338
                    os << " - ";
1339
                    os << irt::get_key(sim.input_ports.get_id(*input_port));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1340

1341
                    os << "\"];\n";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
                }
            }
        }
    }
};

} // namespace irt

#endif