public-api.cpp 89.3 KB
Newer Older
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1
2
3
4
5
// 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)

#include <irritator/core.hpp>
6
#include <irritator/examples.hpp>
7
#include <irritator/external_source.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
8
#include <irritator/io.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
9
10
11
12
13

#include <boost/ut.hpp>

#include <fmt/format.h>

14
#include <iostream>
15
#include <random>
16
#include <sstream>
17

18
19
#include <cstdio>

20
21
struct file_output
{
22
23
24
25
26
27
28
29
    std::FILE* os = nullptr;
    std::string filename;

    file_output(const std::string_view name)
      : filename(name)
    {
        os = std::fopen(filename.c_str(), "w");
    }
30

31
    ~file_output()
32
33
34
35
36
    {
        if (os)
            std::fclose(os);
    }

37
    void operator()(const irt::observer& obs,
38
                    const irt::dynamics_type /*type*/,
39
                    const irt::time /*tl*/,
40
                    const irt::time t,
41
42
43
44
45
46
                    const irt::observer::status s) noexcept
    {
        switch (s) {
        case irt::observer::status::initialize:
            fmt::print(os, "t,{}\n", obs.name.c_str());
            break;
47

48
49
50
51
52
        case irt::observer::status::run:
            fmt::print(os, "{},{}\n", t, obs.msg.real[0]);
            break;

        case irt::observer::status::finalize:
53
            fmt::print(os, "{},{}\n", t, obs.msg.real[0]);
54
55
56
57
            break;
        }
    }
};
58

Gauthier Quesnel's avatar
Gauthier Quesnel committed
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
bool function_ref_called = false;

void
function_ref_f()
{
    function_ref_called = true;
}

struct function_ref_class
{
    bool baz_called = false;

    void baz()
    {
        baz_called = true;
    }

    bool qux_called = false;

    void qux()
    {
        qux_called = true;
    }
};

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
static void empty_fun(irt::model_id /*id*/) noexcept
{}

static irt::status
run_simulation(irt::simulation& sim, const double duration)
{
    using namespace boost::ut;

    irt::time t = 0.0;

    expect(sim.initialize(t) == irt::status::success);

    do {
        auto status = sim.run(t);
        expect(status == irt::status::success);
    } while (t < duration);

    return irt::status::success;
}

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
struct global_alloc
{
    size_t allocation_size = 0;
    int allocation_number = 0;

    void* operator()(size_t size)
    {
        allocation_size += size;
        allocation_number++;

        return std::malloc(size);
    }
};

struct global_free
{
    int free_number = 0;

122
    void operator()(void* ptr)
123
124
125
126
127
128
129
130
    {
        free_number++;

        if (ptr)
            std::free(ptr);
    }
};

131
static void* null_alloc(size_t /*sz*/)
132
133
134
135
{
    return nullptr;
}

136
137
138
static void
null_free(void*)
{}
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
inline int
make_input_node_id(const irt::model_id mdl, const int port) noexcept
{
    fmt::print("make_input_node_id({},{})\n", static_cast<irt::u64>(mdl), port);
    irt_assert(port >= 0 && port < 8);

    irt::u32 index = irt::get_index(mdl);
    irt_assert(index < 268435456u);

    fmt::print("{0:32b} <- index\n", index);
    fmt::print("{0:32b} <- port\n", port);

    irt::u32 port_index = static_cast<irt::u32>(port) << 28u;
    fmt::print("{0:32b} <- port_index\n", port_index);

    index |= port_index;
    fmt::print("{0:32b} <- index final\n", index);

    return static_cast<int>(index);
}

inline int
make_output_node_id(const irt::model_id mdl, const int port) noexcept
{
    fmt::print("make_output_node_id({},{})\n", static_cast<irt::u64>(mdl), port);
    irt_assert(port >= 0 && port < 8);

    irt::u32 index = irt::get_index(mdl);
    irt_assert(index < 268435456u);

    fmt::print("{0:32b} <- index\n", index);
    fmt::print("{0:32b} <- port\n", port);
    fmt::print("{0:32b} <- port + 8u\n", 8u + port);

    irt::u32 port_index = static_cast<irt::u32>(8u + port) << 28u;
    fmt::print("{0:32b} <- port_index\n", port_index);

    index |= port_index;
    fmt::print("{0:32b} <- index final\n", index);

    return static_cast<int>(index);
}

inline std::pair<irt::u32, irt::u32>
get_model_input_port(const int node_id) noexcept
{
    fmt::print("get_model_input_port {}\n", node_id);

    const irt::u32 real_node_id = static_cast<irt::u32>(node_id);
    fmt::print("{0:32b} <- real_node_id\n", real_node_id);

    irt::u32 port = real_node_id >> 28u;
    fmt::print("{0:32b} <- port\n", port);
    irt_assert(port < 8u);

    constexpr irt::u32 mask = ~(15u << 28u);
    fmt::print("{0:32b} <- mask\n", mask);
    irt::u32 index = real_node_id & mask;
    fmt::print("{0:32b} <- real_node_id & mask\n", index);

    fmt::print("index: {} port: {}\n", index, port);
    return std::make_pair(index, port);
}

inline std::pair<irt::u32, irt::u32>
get_model_output_port(const int node_id) noexcept
{
    fmt::print("get_model_output_port {}\n", node_id);

    const irt::u32 real_node_id = static_cast<irt::u32>(node_id);
    fmt::print("{0:32b} <- real_node_id\n", real_node_id);

    irt::u32 port = real_node_id >> 28u;
    fmt::print("{0:32b} <- port\n", port);

    irt_assert(port >= 8u && port < 16u);
    port -= 8u;
    fmt::print("{0:32b} <- port - 8u\n", port);
    irt_assert(port < 8u);

    constexpr irt::u32 mask = ~(15u << 28u);
    fmt::print("{0:32b} <- mask\n", mask);

    irt::u32 index = real_node_id & mask;
    fmt::print("{0:32b} <- real_node_id & mask\n", index);

    fmt::print("index: {} port: {}\n", index, port);
    return std::make_pair(index, port);
}

Gauthier Quesnel's avatar
Gauthier Quesnel committed
230
231
232
233
234
int
main()
{
    using namespace boost::ut;

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
    "model-id-port-node-id"_test = [] {
        auto i = make_input_node_id(irt::model_id{ 50 }, 7);
        auto j = make_output_node_id(irt::model_id{ 50 }, 3);
        auto k1 = make_input_node_id(irt::model_id{ 268435455 }, 0);
        auto k2 = make_output_node_id(irt::model_id{ 268435455 }, 0);
        auto k3 = make_input_node_id(irt::model_id{ 268435455 }, 7);
        auto k4 = make_output_node_id(irt::model_id{ 268435455 }, 7);

        expect(i != j);

        auto ni = get_model_input_port(i);
        auto nj = get_model_output_port(j);
        auto nk1 = get_model_input_port(k1);
        auto nk2 = get_model_output_port(k2);
        auto nk3 = get_model_input_port(k3);
        auto nk4 = get_model_output_port(k4);

        expect(ni.first == 50u);
        expect(ni.second == 7u);
        expect(nj.first == 50u);
        expect(nj.second == 3u);
        expect(nk1.first == 268435455u);
        expect(nk1.second == 0u);
        expect(nk2.first == 268435455u);
        expect(nk2.second == 0u);
        expect(nk3.first == 268435455u);
        expect(nk3.second == 7u);
        expect(nk4.first == 268435455u);
        expect(nk4.second == 7u);
    };

    "sizeof"_test = [] {
        fmt::print("none {}\n", sizeof(irt::none));
        fmt::print("qss1_integrator {}\n", sizeof(irt::qss1_integrator));
        fmt::print("qss1_multiplier {}\n", sizeof(irt::qss1_multiplier));
        fmt::print("qss1_cross {}\n", sizeof(irt::qss1_cross));
        fmt::print("qss1_power {}\n", sizeof(irt::qss1_power));
        fmt::print("qss1_square {}\n", sizeof(irt::qss1_square));
        fmt::print("qss1_sum_2 {}\n", sizeof(irt::qss1_sum_2));
        fmt::print("qss1_sum_3 {}\n", sizeof(irt::qss1_sum_3));
        fmt::print("qss1_sum_4 {}\n", sizeof(irt::qss1_sum_4));
        fmt::print("qss1_wsum_2 {}\n", sizeof(irt::qss1_wsum_2));
        fmt::print("qss1_wsum_3 {}\n", sizeof(irt::qss1_wsum_3));
        fmt::print("qss1_wsum_4 {}\n", sizeof(irt::qss1_wsum_4));
        fmt::print("qss2_integrator {}\n", sizeof(irt::qss2_integrator));
        fmt::print("qss2_multiplier {}\n", sizeof(irt::qss2_multiplier));
        fmt::print("qss2_cross {}\n", sizeof(irt::qss2_cross));
        fmt::print("qss2_power {}\n", sizeof(irt::qss2_power));
        fmt::print("qss2_square {}\n", sizeof(irt::qss2_square));
        fmt::print("qss2_sum_2 {}\n", sizeof(irt::qss2_sum_2));
        fmt::print("qss2_sum_3 {}\n", sizeof(irt::qss2_sum_3));
        fmt::print("qss2_sum_4 {}\n", sizeof(irt::qss2_sum_4));
        fmt::print("qss2_wsum_2 {}\n", sizeof(irt::qss2_wsum_2));
        fmt::print("qss2_wsum_3 {}\n", sizeof(irt::qss2_wsum_3));
        fmt::print("qss2_wsum_4 {}\n", sizeof(irt::qss2_wsum_4));
        fmt::print("qss3_integrator {}\n", sizeof(irt::qss3_integrator));
        fmt::print("qss3_multiplier {}\n", sizeof(irt::qss3_multiplier));
        fmt::print("qss3_power {}\n", sizeof(irt::qss3_power));
        fmt::print("qss3_square {}\n", sizeof(irt::qss3_square));
        fmt::print("qss3_cross {}\n", sizeof(irt::qss3_cross));
        fmt::print("qss3_sum_2 {}\n", sizeof(irt::qss3_sum_2));
        fmt::print("qss3_sum_3 {}\n", sizeof(irt::qss3_sum_3));
        fmt::print("qss3_sum_4 {}\n", sizeof(irt::qss3_sum_4));
        fmt::print("qss3_wsum_2 {}\n", sizeof(irt::qss3_wsum_2));
        fmt::print("qss3_wsum_3 {}\n", sizeof(irt::qss3_wsum_3));
        fmt::print("qss3_wsum_4 {}\n", sizeof(irt::qss3_wsum_4));
        fmt::print("integrator {}\n", sizeof(irt::integrator));
        fmt::print("quantifier {}\n", sizeof(irt::quantifier));
        fmt::print("adder_2 {}\n", sizeof(irt::adder_2));
        fmt::print("adder_3 {}\n", sizeof(irt::adder_3));
        fmt::print("adder_4 {}\n", sizeof(irt::adder_4));
        fmt::print("mult_2 {}\n", sizeof(irt::mult_2));
        fmt::print("mult_3 {}\n", sizeof(irt::mult_3));
        fmt::print("mult_4 {}\n", sizeof(irt::mult_4));
        fmt::print("counter {}\n", sizeof(irt::counter));
        fmt::print("buffer {}\n", sizeof(irt::buffer));
        fmt::print("generator {}\n", sizeof(irt::generator));
        fmt::print("constant {}\n", sizeof(irt::constant));
        fmt::print("cross {}\n", sizeof(irt::cross));
        fmt::print("time_func {}\n", sizeof(irt::time_func));
        fmt::print("accumulator {}\n", sizeof(irt::accumulator_2));
        fmt::print("flow {}\n", sizeof(irt::flow));
317
        fmt::print("model {}\n", sizeof(irt::model));
318
319
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
320
321
322
323
324
325
326
    "model_constepxr"_test = [] {
        expect(irt::is_detected_v<irt::initialize_function_t, irt::counter> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::counter> ==
               false);
        expect(irt::is_detected_v<irt::transition_function_t, irt::counter> ==
               true);
327
        expect(irt::is_detected_v<irt::has_input_port_t, irt::counter> == true);
328
329
        expect(irt::is_detected_v<irt::has_output_port_t, irt::counter> ==
               false);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
330

331
332
        expect(irt::is_detected_v<irt::initialize_function_t, irt::generator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
333
334
        expect(irt::is_detected_v<irt::lambda_function_t, irt::generator> ==
               true);
335
        expect(irt::is_detected_v<irt::transition_function_t, irt::generator> ==
336
               true);
337
338
339
340
        expect(irt::is_detected_v<irt::has_input_port_t, irt::generator> ==
               false);
        expect(irt::is_detected_v<irt::has_output_port_t, irt::generator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
341
342
343
344
345
346
347

        expect(irt::is_detected_v<irt::initialize_function_t, irt::adder_2> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::adder_2> ==
               true);
        expect(irt::is_detected_v<irt::transition_function_t, irt::adder_2> ==
               true);
348
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_2> == true);
349
350
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_2> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
351
352
353
354
355
356
357

        expect(irt::is_detected_v<irt::initialize_function_t, irt::adder_3> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::adder_3> ==
               true);
        expect(irt::is_detected_v<irt::transition_function_t, irt::adder_3> ==
               true);
358
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_3> == true);
359
360
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_3> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
361
362
363
364
365
366
367

        expect(irt::is_detected_v<irt::initialize_function_t, irt::adder_4> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::adder_4> ==
               true);
        expect(irt::is_detected_v<irt::transition_function_t, irt::adder_4> ==
               true);
368
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_4> == true);
369
370
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_4> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
371
372
373

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_2> ==
               true);
374
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
375
376
377
        expect(irt::is_detected_v<irt::transition_function_t, irt::mult_2> ==
               true);
        expect(irt::is_detected_v<irt::has_input_port_t, irt::mult_2> == true);
378
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
379
380
381

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_3> ==
               true);
382
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
383
384
385
        expect(irt::is_detected_v<irt::transition_function_t, irt::mult_3> ==
               true);
        expect(irt::is_detected_v<irt::has_input_port_t, irt::mult_3> == true);
386
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
387
388
389

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_4> ==
               true);
390
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
391
392
393
        expect(irt::is_detected_v<irt::transition_function_t, irt::mult_4> ==
               true);
        expect(irt::is_detected_v<irt::has_input_port_t, irt::mult_4> == true);
394
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
395
396
397
398
399
400
401
402
403

        expect(
          irt::is_detected_v<irt::initialize_function_t, irt::integrator> ==
          true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::integrator> ==
               true);
        expect(
          irt::is_detected_v<irt::transition_function_t, irt::integrator> ==
          true);
404
405
406
407
        expect(irt::is_detected_v<irt::has_input_port_t, irt::integrator> ==
               true);
        expect(irt::is_detected_v<irt::has_output_port_t, irt::integrator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
408
409
410
411
412
413
414
415
416

        expect(
          irt::is_detected_v<irt::initialize_function_t, irt::quantifier> ==
          true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::quantifier> ==
               true);
        expect(
          irt::is_detected_v<irt::transition_function_t, irt::quantifier> ==
          true);
417
418
419
420
        expect(irt::is_detected_v<irt::has_input_port_t, irt::quantifier> ==
               true);
        expect(irt::is_detected_v<irt::has_output_port_t, irt::quantifier> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
421
422
423
424
425
426
427
428
429
430
431
432
    };

    "status"_test = [] {
        irt::status s1 = irt::status::success;
        expect(irt::is_success(s1) == true);
        expect(irt::is_bad(s1) == false);

        irt::status s2 = irt::status::block_allocator_not_enough_memory;
        expect(irt::is_success(s2) == false);
        expect(irt::is_bad(s2) == true);
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
    "function_ref"_test = [] {
        {
            irt::function_ref<void(void)> fr = function_ref_f;
            fr();
            expect(function_ref_called == true);
        }

        {
            function_ref_class o;
            auto x = &function_ref_class::baz;
            irt::function_ref<void(function_ref_class&)> fr = x;
            fr(o);
            expect(o.baz_called);
            x = &function_ref_class::qux;
            fr = x;
            fr(o);
            expect(o.qux_called);
        }

        {
            auto x = [] { return 42; };
            irt::function_ref<int()> fr = x;
            expect(fr() == 42);
        }

        {
            int i = 0;
            auto x = [&i] { i = 42; };
            irt::function_ref<void()> fr = x;
            fr();
            expect(i == 42);
        }
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
467
468
469
470
471
472
473
474
475
476
477
478
479
    "time"_test = [] {
        expect(irt::time_domain<irt::time>::infinity >
               irt::time_domain<irt::time>::zero);
        expect(irt::time_domain<irt::time>::zero >
               irt::time_domain<irt::time>::negative_infinity);
    };

    "small_string"_test = [] {
        irt::small_string<8> f1;
        expect(f1.capacity() == 8_ul);
        expect(f1 == "");
        expect(f1.size() == 0_ul);

480
        f1 = "ok";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
481
482
483
        expect(f1 == "ok");
        expect(f1.size() == 2_ul);

484
        f1 = "okok";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
485
486
487
        expect(f1 == "okok");
        expect(f1.size() == 4_ul);

488
        f1 = "okok123456";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
        expect(f1 == "okok123");
        expect(f1.size() == 7_ul);

        irt::small_string<8> f2(f1);
        expect(f2 == "okok123");
        expect(f2.size() == 7_ul);

        expect(f1.c_str() != f2.c_str());

        irt::small_string<8> f3("012345678");
        expect(f3 == "0123456");
        expect(f3.size() == 7_ul);

        f3.clear();
        expect(f3 == "");
        expect(f3.size() == 0_ul);

        f3 = f2;
        expect(f3 == "okok123");
        expect(f3.size() == 7_ul);
    };

    "list"_test = [] {
        irt::flat_list<int>::allocator_type allocator;
        expect(is_success(allocator.init(32)));

        irt::flat_list<int> lst(&allocator);

        lst.emplace_front(5);
        lst.emplace_front(4);
        lst.emplace_front(3);
        lst.emplace_front(2);
        lst.emplace_front(1);

        {
            int i = 1;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }

        lst.pop_front();

        {
            int i = 2;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }
    };

    "double_list"_test = [] {
        irt::flat_double_list<int>::allocator_type allocator;
        expect(is_success(allocator.init(32)));

        irt::flat_double_list<int> lst(&allocator);

        expect(lst.empty());
        expect(lst.begin() == lst.end());

        lst.emplace_front(0);
        expect(lst.begin() == --lst.end());
        expect(++lst.begin() == lst.end());

        lst.clear();
        expect(lst.empty());
        expect(lst.begin() == lst.end());

        lst.emplace_front(5);
        lst.emplace_front(4);
        lst.emplace_front(3);
        lst.emplace_front(2);
        lst.emplace_front(1);
        lst.emplace_back(6);
        lst.emplace_back(7);
        lst.emplace_back(8);

        {
            int i = 1;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }

        lst.pop_front();

        {
            int i = 2;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }

        {
            auto it = lst.begin();
            expect(*it == 2);

            --it;
            expect(it == lst.end());

            --it;
            expect(it == --lst.end());
        }

        {
            auto it = lst.end();
            expect(it == lst.end());

            --it;
            expect(*it == 8);

            --it;
            expect(*it == 7);
        }
    };

    "data_array_api"_test = [] {
        struct position
        {
            position() = default;
            constexpr position(float x_)
              : x(x_)
            {}

            float x;
        };

        enum class position_id : std::uint64_t
        {
        };

        irt::data_array<position, position_id> array;

        expect(array.max_size() == 0);
        expect(array.max_used() == 0);
        expect(array.capacity() == 0);
        expect(array.next_key() == 1);
        expect(array.is_free_list_empty());

        bool is_init = irt::is_success(array.init(3));

        expect(array.max_size() == 0);
        expect(array.max_used() == 0);
        expect(array.capacity() == 3);
        expect(array.next_key() == 1);
        expect(array.is_free_list_empty());

        expect(is_init);

        {
            auto& first = array.alloc();
            first.x = 0.f;
            expect(array.max_size() == 1);
            expect(array.max_used() == 1);
            expect(array.capacity() == 3);
            expect(array.next_key() == 2);
            expect(array.is_free_list_empty());

            auto& second = array.alloc();
            expect(array.max_size() == 2);
            expect(array.max_used() == 2);
            expect(array.capacity() == 3);
            expect(array.next_key() == 3);
            expect(array.is_free_list_empty());

            second.x = 1.f;

            auto& third = array.alloc();
            expect(array.max_size() == 3);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(array.is_free_list_empty());

            third.x = 2.f;

            expect(array.full());
        }

        array.clear();

        expect(array.max_size() == 0);
        expect(array.max_used() == 0);
668
        expect(array.capacity() == 3);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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
        expect(array.next_key() == 1);
        expect(array.is_free_list_empty());

        is_init = irt::is_success(array.init(3));

        {
            auto& d1 = array.alloc(1.f);
            auto& d2 = array.alloc(2.f);
            auto& d3 = array.alloc(3.f);

            expect(array.max_size() == 3);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(array.is_free_list_empty());

            array.free(d1);

            expect(array.max_size() == 2);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(!array.is_free_list_empty());

            array.free(d2);

            expect(array.max_size() == 1);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(!array.is_free_list_empty());

            array.free(d3);
            expect(array.max_size() == 0);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(!array.is_free_list_empty());

            auto& n1 = array.alloc();
            auto& n2 = array.alloc();
            auto& n3 = array.alloc();

            expect(irt::get_index(array.get_id(n1)) == 2_u);
            expect(irt::get_index(array.get_id(n2)) == 1_u);
            expect(irt::get_index(array.get_id(n3)) == 0_u);

            expect(array.max_size() == 3);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 7);
            expect(array.is_free_list_empty());
        }
    };

    "message"_test = [] {
        {
726
            irt::message vdouble;
727
728
729
730
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 0.0);
            expect(vdouble[3] == 0.0);
731
            expect(vdouble.size() == 0_ul);
732
            expect(vdouble.ssize() == 0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
733
        }
734

Gauthier Quesnel's avatar
Gauthier Quesnel committed
735
        {
736
737
            irt::message vdouble(1.0);
            expect(vdouble[0] == 1.0);
738
739
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 0.0);
740
            expect(vdouble[3] == 0.0);
741
            expect(vdouble.size() == 1_ul);
742
            expect(vdouble.ssize() == 1);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
743
        }
744

Gauthier Quesnel's avatar
Gauthier Quesnel committed
745
        {
746
747
748
            irt::message vdouble(0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 1.0);
749
            expect(vdouble[2] == 0.0);
750
            expect(vdouble[3] == 0.0);
751
            expect(vdouble.size() == 2_ul);
752
            expect(vdouble.ssize() == 2);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
753
        }
754

Gauthier Quesnel's avatar
Gauthier Quesnel committed
755
        {
756
757
758
759
760
            irt::message vdouble(0.0, 0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 1.0);
            expect(vdouble[3] == 0.0);
761
            expect(vdouble.size() == 3_ul);
762
763
764
765
766
767
768
769
770
771
772
            expect(vdouble.ssize() == 3);
        }

        {
            irt::message vdouble(0.0, 0.0, 0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 0.0);
            expect(vdouble[3] == 1.0);
            expect(vdouble.size() == 4_ul);
            expect(vdouble.ssize() == 4);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
        }
    };

    "heap_order"_test = [] {
        irt::heap h;
        h.init(4u);

        irt::heap::handle i1 = h.insert(0.0, irt::model_id{ 0 });
        irt::heap::handle i2 = h.insert(1.0, irt::model_id{ 1 });
        irt::heap::handle i3 = h.insert(-1.0, irt::model_id{ 2 });
        irt::heap::handle i4 = h.insert(2.0, irt::model_id{ 3 });
        expect(h.full());

        expect(i1->tn == 0.0_d);
        expect(i2->tn == 1.0_d);
        expect(i3->tn == -1.0_d);
        expect(i4->tn == 2.0_d);

        expect(h.top() == i3);
        h.pop();
        expect(h.top() == i1);
        h.pop();
        expect(h.top() == i2);
        h.pop();
        expect(h.top() == i4);
        h.pop();

        expect(h.empty());
        expect(!h.full());
    };

    "heap_insert_pop"_test = [] {
        irt::heap h;
        h.init(4u);

        irt::heap::handle i1 = h.insert(0.0, irt::model_id{ 0 });
        irt::heap::handle i2 = h.insert(1.0, irt::model_id{ 1 });
        irt::heap::handle i3 = h.insert(-1.0, irt::model_id{ 2 });
        irt::heap::handle i4 = h.insert(2.0, irt::model_id{ 3 });

        expect(i1 != nullptr);
        expect(i2 != nullptr);
        expect(i3 != nullptr);
        expect(i4 != nullptr);

        expect(!h.empty());
        expect(h.top() == i3);

        h.pop(); // remove i3
        h.pop(); // rmeove i1

        expect(h.top() == i2);

        i3->tn = -10.0;
        h.insert(i3);

        i1->tn = -1.0;
        h.insert(i1);

        expect(h.top() == i3);
        h.pop();

        expect(h.top() == i1);
        h.pop();

        expect(h.top() == i2);
        h.pop();

        expect(h.top() == i4);
        h.pop();

        expect(h.empty());
    };

    "heap_with_equality"_test = [] {
        irt::heap h;
        h.init(256u);

        for (double t = 0; t < 100.0; ++t)
            h.insert(t, irt::model_id{ static_cast<unsigned>(t) });

        expect(h.size() == 100_ul);

        h.insert(50.0, irt::model_id{ 502 });
        h.insert(50.0, irt::model_id{ 503 });
        h.insert(50.0, irt::model_id{ 504 });

        expect(h.size() == 103_ul);

        for (double t = 0.0; t < 50.0; ++t) {
            expect(h.top()->tn == t);
            h.pop();
        }

        expect(h.top()->tn == 50.0_d);
        h.pop();
        expect(h.top()->tn == 50.0_d);
        h.pop();
        expect(h.top()->tn == 50.0_d);
        h.pop();
        expect(h.top()->tn == 50.0_d);
        h.pop();

        for (double t = 51.0; t < 100.0; ++t) {
            expect(h.top()->tn == t);
            h.pop();
        }
    };

882
883
884
885
    "simulation-dispatch"_test = [] {
        irt::simulation sim;
        sim.init(64u, 256u);
        auto& dyn1 = sim.alloc<irt::none>();
886
887
        (void)sim.alloc<irt::qss1_integrator>();
        (void)sim.alloc<irt::qss1_multiplier>();
888

889
        auto& mdl = irt::get_model(dyn1);
890

891
892
        sim.dispatch(mdl,
                     []([[maybe_unused]] auto& dyns) { std::cout << "ok"; });
893

894
895
896
897
898
        auto ret =
          sim.dispatch(mdl, []([[maybe_unused]] const auto& dyns) -> int {
              std::cout << "ok";
              return 1;
          });
899
900
901

        expect(ret == 1);

902
        auto ret_2 = sim.dispatch(
903
904
          mdl,
          []([[maybe_unused]] const auto& dyns, int v1, double v2) {
905
906
907
908
909
910
911
912
913
              std::cout << "ok" << v1 << ' ' << v2;
              return v2 + v1;
          },
          123,
          456.0);

        expect(ret_2 == 579.0);
    };

914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
    "input_output"_test = [] {
        std::string str;
        str.reserve(4096u);

        {
            irt::simulation sim;
            expect(irt::is_success(sim.init(64lu, 4096lu)));

            sim.alloc<irt::none>();
            sim.alloc<irt::qss1_integrator>();
            sim.alloc<irt::qss1_multiplier>();
            sim.alloc<irt::qss1_cross>();
            sim.alloc<irt::qss1_power>();
            sim.alloc<irt::qss1_square>();
            sim.alloc<irt::qss1_sum_2>();
            sim.alloc<irt::qss1_sum_3>();
            sim.alloc<irt::qss1_sum_4>();
            sim.alloc<irt::qss1_wsum_2>();
            sim.alloc<irt::qss1_wsum_3>();
            sim.alloc<irt::qss1_wsum_4>();
            sim.alloc<irt::qss2_integrator>();
            sim.alloc<irt::qss2_multiplier>();
            sim.alloc<irt::qss2_cross>();
            sim.alloc<irt::qss2_power>();
            sim.alloc<irt::qss2_square>();
            sim.alloc<irt::qss2_sum_2>();
            sim.alloc<irt::qss2_sum_3>();
            sim.alloc<irt::qss2_sum_4>();
            sim.alloc<irt::qss2_wsum_2>();
            sim.alloc<irt::qss2_wsum_3>();
            sim.alloc<irt::qss2_wsum_4>();
            sim.alloc<irt::qss3_integrator>();
            sim.alloc<irt::qss3_multiplier>();
            sim.alloc<irt::qss3_power>();
            sim.alloc<irt::qss3_square>();
            sim.alloc<irt::qss3_cross>();
            sim.alloc<irt::qss3_sum_2>();
            sim.alloc<irt::qss3_sum_3>();
            sim.alloc<irt::qss3_sum_4>();
            sim.alloc<irt::qss3_wsum_2>();
            sim.alloc<irt::qss3_wsum_3>();
            sim.alloc<irt::qss3_wsum_4>();
956
957
            sim.alloc<irt::integrator>();
            sim.alloc<irt::quantifier>();
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
            sim.alloc<irt::adder_2>();
            sim.alloc<irt::adder_3>();
            sim.alloc<irt::adder_4>();
            sim.alloc<irt::mult_2>();
            sim.alloc<irt::mult_3>();
            sim.alloc<irt::mult_4>();
            sim.alloc<irt::counter>();
            sim.alloc<irt::generator>();
            sim.alloc<irt::constant>();
            sim.alloc<irt::cross>();
            sim.alloc<irt::time_func>();
            sim.alloc<irt::accumulator_2>();
            sim.alloc<irt::flow>();

            std::ostringstream os;
            irt::writer w(os);

            expect(irt::is_success(w(sim)));
            str = os.str();
        }

        expect(!str.empty());
        fmt::print(str);

        {
            std::istringstream is(str);

            irt::simulation sim;
            expect(irt::is_success(sim.init(64lu, 32lu)));

            irt::reader r(is);
            expect(irt::is_success(r(sim)));

991
            expect(sim.models.size() == 49);
992
        }
993
994
995
996
997
998
999
1000
1001

        {
            std::istringstream is(str);
            int i = 0;

            irt::simulation sim;
            expect(irt::is_success(sim.init(64lu, 32lu)));

            irt::reader r(is);
1002
1003
            expect(
              irt::is_success(r(sim, [&i](irt::model_id /*id*/) { ++i; })));
1004
1005
1006
1007
            expect(i == 49);

            expect(sim.models.size() == 49);
        }
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

        {
            std::string string_error{ "1\n0 qss1_integrator A B C\n" };
            std::istringstream is{ string_error };
            irt::simulation sim;
            expect(irt::is_success(sim.init(64lu, 32lu)));

            irt::is_fatal_breakpoint = false;

            irt::reader r(is);
            expect(irt::is_bad(r(sim)));
            expect(r.line_error == 2);
            expect(r.column_error == 17);
            expect(r.model_error == 0);
            expect(r.connection_error == 0);

            irt::is_fatal_breakpoint = true;
        }
1026
1027
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1028
1029
1030
1031
    "constant_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
1032
        expect(sim.can_alloc(3));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1033

1034
1035
1036
        auto& cnt = sim.alloc<irt::counter>();
        auto& c1 = sim.alloc<irt::constant>();
        auto& c2 = sim.alloc<irt::constant>();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1037

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1038
1039
        c1.default_value = 0.0;
        c2.default_value = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1040

1041
1042
        expect(sim.connect(c1, 0, cnt, 0) == irt::status::success);
        expect(sim.connect(c2, 0, cnt, 0) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1043
1044

        irt::time t = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1045
1046
        expect(sim.initialize(t) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1047
1048
1049
1050
1051
1052
1053
        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
        } while (t < sim.end);

1054
        expect(cnt.number == static_cast<irt::i64>(2));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1055
1056
    };

1057
1058
1059
1060
    "cross_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
1061
        expect(sim.can_alloc(3));
1062

1063
1064
1065
        auto& cnt = sim.alloc<irt::counter>();
        auto& cross1 = sim.alloc<irt::cross>();
        auto& c1 = sim.alloc<irt::constant>();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1066
1067
1068

        c1.default_value = 3.0;
        cross1.default_threshold = 0.0;
1069

1070
1071
1072
1073
        expect(sim.connect(c1, 0, cross1, 0) == irt::status::success);
        expect(sim.connect(c1, 0, cross1, 1) == irt::status::success);
        expect(sim.connect(c1, 0, cross1, 2) == irt::status::success);
        expect(sim.connect(cross1, 0, cnt, 0) == irt::status::success);
1074
1075

        irt::time t = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1076
1077
        expect(sim.initialize(t) == irt::status::success);

1078
1079
        irt::status st;

1080
        do {
1081
1082
1083
1084
            st = sim.run(t);
            expect(irt::is_success(st));
        } while (t < sim.end);

1085
        expect(cnt.number == static_cast<irt::i64>(2));
1086
1087
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1088
    "generator_counter_simluation"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1089
        fmt::print("generator_counter_simluation\n");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1090
1091
1092
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
1093
        expect(sim.can_alloc(2));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1094

1095
1096
        auto& gen = sim.alloc<irt::generator>();
        auto& cnt = sim.alloc<irt::counter>();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1097

1098
        expect(sim.connect(gen, 0, cnt, 0) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1099
1100
1101
1102

        expect(sim.begin == irt::time_domain<irt::time>::zero);
        expect(sim.end == irt::time_domain<irt::time>::infinity);

1103
1104
        double data[2] = { 0.0, 1.0 };

1105
        {
1106
1107
1108
1109
            gen.default_value_source.data = &data[0];
            gen.default_value_source.index = 0;
            gen.default_value_source.size = 1;
            gen.default_value_source.expand = [](auto& src) {
1110
1111
1112
                src.index = 0;
                return true;
            };
1113
1114
1115
        }

        {
1116
1117
1118
1119
            gen.default_ta_source.data = &data[1];
            gen.default_ta_source.index = 0;
            gen.default_ta_source.size = 1;
            gen.default_ta_source.expand = [](auto& src) {
1120
1121
1122
                src.index = 0;
                return true;
            };
1123
1124
        }

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1125
1126
1127
        sim.end = 10.0;

        irt::time t = sim.begin;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1128
1129
        expect(sim.initialize(t) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1130
1131
1132
1133
1134
        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
1135
            expect(cnt.number <= static_cast<irt::i64>(t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1136
1137
        } while (t < sim.end);

1138
        expect(cnt.number == static_cast<irt::i64>(9));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1139
    };
1140

K-H-Ismail's avatar
K-H-Ismail committed
1141
    "time_func"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1142
        fmt::print("time_func\n");
K-H-Ismail's avatar
K-H-Ismail committed
1143
        irt::simulation sim;
1144
        const double duration = 30;
K-H-Ismail's avatar
K-H-Ismail committed
1145
1146

        expect(irt::is_success(sim.init(16lu, 256lu)));
1147
        expect(sim.can_alloc(2));
1148

1149
1150
        auto& time_fun = sim.alloc<irt::time_func>();
        auto& cnt = sim.alloc<irt::counter>();
K-H-Ismail's avatar
K-H-Ismail committed
1151

1152
        time_fun.default_f = &irt::square_time_function;
1153
        time_fun.default_sigma = 0.1;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1154

1155
        expect(sim.connect(time_fun, 0, cnt, 0) == irt::status::success);
K-H-Ismail's avatar
K-H-Ismail committed
1156

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1157
        irt::time t{ 0 };
1158
        expect(sim.initialize(t) == irt::status::success);
1159
        double c = 0.0;
K-H-Ismail's avatar
K-H-Ismail committed
1160
        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1161
            auto st = sim.run(t);
1162
            expect((irt::is_success(st)) == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1163
            expect(time_fun.value == t * t);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1164
            c++;
1165
        } while (t < duration);
1166
1167
1168

        const auto value = (2.0 * duration / time_fun.default_sigma - 1.0);
        expect(c == value);
1169
1170
1171
    };

    "time_func_sin"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1172
        fmt::print("time_func_sin\n");
1173
1174
1175
        const double pi = std::acos(-1);
        const double f0 = 0.1;
        const double duration = 30;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1176
        irt::simulation sim;
1177
1178

        expect(irt::is_success(sim.init(16lu, 256lu)));
1179
        expect(sim.can_alloc(2));
1180

1181
1182
        auto& time_fun = sim.alloc<irt::time_func>();
        auto& cnt = sim.alloc<irt::counter>();
1183
1184
1185
1186

        time_fun.default_f = &irt::sin_time_function;
        time_fun.default_sigma = 0.1;

1187
        expect(sim.connect(time_fun, 0, cnt, 0) == irt::status::success);
1188
1189

        irt::time t{ 0 };
1190
        expect(sim.initialize(t) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1191
        double c = 0;
1192
1193
1194
        do {

            auto st = sim.run(t);
1195
            expect((irt::is_success(st)) >> fatal);
1196
            expect(time_fun.value == std::sin(2 * pi * f0 * t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1197
            c++;
1198
        } while (t < duration);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1199
        expect(c == 2.0 * duration / time_fun.default_sigma - 1.0);
1200
1201
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1202
1203
    "lotka_volterra_simulation"_test = [] {
        fmt::print("lotka_volterra_simulation\n");
1204
1205
1206
        irt::simulation sim;

        expect(irt::is_success(sim.init(32lu, 512lu)));
1207
1208
1209
1210
1211
1212
1213
1214
1215
        expect(sim.can_alloc(8));

        auto& sum_a = sim.alloc<irt::adder_2>();
        auto& sum_b = sim.alloc<irt::adder_2>();
        auto& product = sim.alloc<irt::mult_2>();
        auto& integrator_a = sim.alloc<irt::integrator>();
        auto& integrator_b = sim.alloc<irt::integrator>();
        auto& quantifier_a = sim.alloc<irt::quantifier>();
        auto& quantifier_b = sim.alloc<irt::quantifier>();
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239

        integrator_a.default_current_value = 18.0;

        quantifier_a.default_adapt_state =
          irt::quantifier::adapt_state::possible;
        quantifier_a.default_zero_init_offset = true;
        quantifier_a.default_step_size = 0.01;
        quantifier_a.default_past_length = 3;

        integrator_b.default_current_value = 7.0;

        quantifier_b.default_adapt_state =
          irt::quantifier::adapt_state::possible;
        quantifier_b.default_zero_init_offset = true;
        quantifier_b.default_step_size = 0.01;
        quantifier_b.default_past_length = 3;

        product.default_input_coeffs[0] = 1.0;
        product.default_input_coeffs[1] = 1.0;
        sum_a.default_input_coeffs[0] = 2.0;
        sum_a.default_input_coeffs[1] = -0.4;
        sum_b.default_input_coeffs[0] = -1.0;
        sum_b.default_input_coeffs[1] = 0.1;

1240
        expect((sim.models.size() == 7_ul) >> fatal);
1241

1242
1243
        expect(sim.connect(sum_a, 0, integrator_a, 1) == irt::status::success);
        expect(sim.connect(sum_b, 0, integrator_b, 1) == irt::status::success);
1244

1245
1246
        expect(sim.connect(integrator_a, 0, sum_a, 0) == irt::status::success);
        expect(sim.connect(integrator_b, 0, sum_b, 0) == irt::status::success);
1247

1248
        expect(sim.connect(integrator_a, 0, product, 0) ==
1249
               irt::status::success);
1250
        expect(sim.connect(integrator_b, 0, product, 1) ==
1251
1252
               irt::status::success);

1253
1254
        expect(sim.connect(product, 0, sum_a, 1) == irt::status::success);
        expect(sim.connect(product, 0, sum_b, 1) == irt::status::success);
1255

1256
        expect(sim.connect(quantifier_a, 0, integrator_a, 0) ==
1257
               irt::status::success);
1258
        expect(sim.connect(quantifier_b, 0, integrator_b, 0) ==
1259
               irt::status::success);
1260
        expect(sim.connect(integrator_a, 0, quantifier_a, 0) ==
1261
               irt::status::success);
1262
        expect(sim.connect(integrator_b, 0, quantifier_b, 0) ==
1263
1264
1265
1266
1267
1268
1269
               irt::status::success);

        file_output fo_a("lotka-volterra_a.csv");
        file_output fo_b("lotka-volterra_b.csv");
        expect(fo_a.os != nullptr);
        expect(fo_b.os != nullptr);

1270
1271
        auto& obs_a = sim.observers.alloc("A", fo_a);
        auto& obs_b = sim.observers.alloc("B", fo_b);
1272

1273
1274
        sim.observe(irt::get_model(integrator_a), obs_a);
        sim.observe(irt::get_model(integrator_b), obs_b);
1275
1276
1277
1278

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
1279
        expect((sim.sched.size() == 7_ul) >> fatal);
1280
1281
1282
1283
1284

        do {
            auto st = sim.run(t);
            expect(st == irt::status::success);
        } while (t < 15.0);
K-H-Ismail's avatar
K-H-Ismail committed
1285
1286
    };

1287
    "izhikevitch_simulation"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1288
        fmt::print("izhikevitch_simulation\n");
1289
1290
1291
        irt::simulation sim;

        expect(irt::is_success(sim.init(64lu, 256lu)));
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
        expect(sim.models.can_alloc(14));

        auto& constant = sim.alloc<irt::constant>();
        auto& constant2 = sim.alloc<irt::constant>();
        auto& constant3 = sim.alloc<irt::constant>();
        auto& sum_a = sim.alloc<irt::adder_2>();
        auto& sum_b = sim.alloc<irt::adder_2>();
        auto& sum_c = sim.alloc<irt::adder_4>();
        auto& sum_d = sim.alloc<irt::adder_2>();
        auto& product = sim.alloc<irt::mult_2>();
        auto& integrator_a = sim.alloc<irt::integrator>();
        auto& integrator_b = sim.alloc<irt::integrator>();
        auto& quantifier_a = sim.alloc<irt::quantifier>();
        auto& quantifier_b = sim.alloc<irt::quantifier>();
        auto& cross = sim.alloc<irt::cross>();
        auto& cross2 = sim.alloc<irt::cross>();
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352

        double a = 0.2;
        double b = 2.0;
        double c = -56.0;
        double d = -16.0;
        double I = -99.0;
        double vt = 30.0;

        constant.default_value = 1.0;
        constant2.default_value = c;
        constant3.default_value = I;

        cross.default_threshold = vt;
        cross2.default_threshold = vt;

        integrator_a.default_current_value = 0.0;

        quantifier_a.default_adapt_state =
          irt::quantifier::adapt_state::possible;
        quantifier_a.default_zero_init_offset = true;
        quantifier_a.default_step_size = 0.01;
        quantifier_a.default_past_length = 3;

        integrator_b.default_current_value = 0.0;

        quantifier_b.default_adapt_state =
          irt::quantifier::adapt_state::possible;
        quantifier_b.default_zero_init_offset = true;
        quantifier_b.default_step_size = 0.01;
        quantifier_b.default_past_length = 3;

        product.default_input_coeffs[0] = 1.0;
        product.default_input_coeffs[1] = 1.0;

        sum_a.default_input_coeffs[0] = 1.0;
        sum_a.default_input_coeffs[1] = -1.0;
        sum_b.default_input_coeffs[0] = -a;
        sum_b.default_input_coeffs[1] = a * b;
        sum_c.default_input_coeffs[0] = 0.04;
        sum_c.default_input_coeffs[1] = 5.0;
        sum_c.default_input_coeffs[2] = 140.0;
        sum_c.default_input_coeffs[3] = 1.0;
        sum_d.default_input_coeffs[0] = 1.0;
        sum_d.default_input_coeffs[1] = d;

1353
        expect((sim.models.size() == 14_ul) >> fatal);
1354

1355
1356
1357
        expect(sim.connect(integrator_a, 0, cross, 0) == irt::status::success);
        expect(sim.connect(constant2, 0, cross, 1) == irt::status::success);
        expect(sim.connect(integrator_a, 0, cross, 2) == irt::status::success);
1358

1359
1360
1361
1362
1363
1364
        expect(sim.connect(cross, 0, quantifier_a, 0) == irt::status::success);
        expect(sim.connect(cross, 0, product, 0) == irt::status::success);
        expect(sim.connect(cross, 0, product, 1) == irt::status::success);
        expect(sim.connect(product, 0, sum_c, 0) == irt::status::success);
        expect(sim.connect(cross, 0, sum_c, 1) == irt::status::success);
        expect(sim.connect(cross, 0, sum_b, 1) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1365

1366
1367
        expect(sim.connect(constant, 0, sum_c, 2) == irt::status::success);
        expect(sim.connect(constant3, 0, sum_c, 3) == irt::status::success);
1368

1369
1370
1371
1372
1373
        expect(sim.connect(sum_c, 0, sum_a, 0) == irt::status::success);
        expect(sim.connect(cross2, 0, sum_a, 1) == irt::status::success);
        expect(sim.connect(sum_a, 0, integrator_a, 1) == irt::status::success);
        expect(sim.connect(cross, 0, integrator_a, 2) == irt::status::success);
        expect(sim.connect(quantifier_a, 0, integrator_a, 0) ==
1374
1375
               irt::status::success);

1376
1377
1378
        expect(sim.connect(cross2, 0, quantifier_b, 0) == irt::status::success);
        expect(sim.connect(cross2, 0, sum_b, 0) == irt::status::success);
        expect(sim.connect(quantifier_b, 0, integrator_b, 0) ==
1379
               irt::status::success);
1380
        expect(sim.connect(sum_b, 0, integrator_b, 1) == irt::status::success);
1381

1382
1383
1384
1385
1386
1387
        expect(sim.connect(cross2, 0, integrator_b, 2) == irt::status::success);
        expect(sim.connect(integrator_a, 0, cross2, 0) == irt::status::success);
        expect(sim.connect(integrator_b, 0, cross2, 2) == irt::status::success);
        expect(sim.connect(sum_d, 0, cross2, 1) == irt::status::success);
        expect(sim.connect(integrator_b, 0, sum_d, 0) == irt::status::success);
        expect(sim.connect(constant, 0, sum_d, 1) == irt::status::success);
1388
1389
1390
1391

        file_output fo_a("izhikevitch_a.csv");
        expect(fo_a.os != nullptr);

1392
        auto& obs_a = sim.observers.alloc("A", fo_a);
1393
1394
1395

        file_output fo_b("izhikevitch_b.csv");
        expect(fo_b.os != nullptr);
1396
        auto& obs_b = sim.observers.alloc("B", fo_b);
1397

1398
1399
        sim.observe(irt::get_model(integrator_a), obs_a);
        sim.observe(irt::get_model(integrator_b), obs_b