public-api.cpp 94.5 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>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
6
#include <irritator/io.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
7
8
9
10
11

#include <boost/ut.hpp>

#include <fmt/format.h>

12
#include <iostream>
13

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
struct file_output
{
    file_output(const char* file_path) noexcept
      : os(std::fopen(file_path, "w"))
    {}

    ~file_output() noexcept
    {
        if (os)
            std::fclose(os);
    }

    std::FILE* os = nullptr;
};

void
file_output_initialize(const irt::observer& obs, const irt::time /*t*/) noexcept
{
    if (!obs.user_data)
        return;

    auto* output = reinterpret_cast<file_output*>(obs.user_data);
    fmt::print(output->os, "t,{}\n", obs.name.c_str());
}

void
file_output_observe(const irt::observer& obs,
                    const irt::time t,
                    const irt::message& msg) noexcept
{
    if (!obs.user_data)
        return;

    auto* output = reinterpret_cast<file_output*>(obs.user_data);
48
    fmt::print(output->os, "{},{}\n", t, msg.real[0]);
49
50
}

Gauthier Quesnel's avatar
Gauthier Quesnel committed
51
52
53
54
55
56
57
58
59
60
61
62
int
main()
{
    using namespace boost::ut;

    "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);
63
        expect(irt::is_detected_v<irt::has_input_port_t, irt::counter> == true);
64
65
        expect(irt::is_detected_v<irt::has_output_port_t, irt::counter> ==
               false);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
66

67
68
        expect(irt::is_detected_v<irt::initialize_function_t, irt::generator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
69
70
        expect(irt::is_detected_v<irt::lambda_function_t, irt::generator> ==
               true);
71
        expect(irt::is_detected_v<irt::transition_function_t, irt::generator> ==
72
               true);
73
74
75
76
        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
77
78
79
80
81
82
83

        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);
84
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_2> == true);
85
86
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_2> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
87
88
89
90
91
92
93

        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);
94
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_3> == true);
95
96
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_3> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
97
98
99
100
101
102
103

        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);
104
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_4> == true);
105
106
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_4> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
107
108
109

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_2> ==
               true);
110
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
111
112
113
        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);
114
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
115
116
117

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_3> ==
               true);
118
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
119
120
121
        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);
122
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
123
124
125

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_4> ==
               true);
126
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
127
128
129
        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);
130
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
131
132
133
134
135
136
137
138
139

        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);
140
141
142
143
        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
144
145
146
147
148
149
150
151
152

        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);
153
154
155
156
        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
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
    };

    "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);
    };

    "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);

        f1.append("ok");
        expect(f1 == "ok");
        expect(f1.size() == 2_ul);

        f1.append("ok");
        expect(f1 == "okok");
        expect(f1.size() == 4_ul);

        f1.append("1234");
        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);
        }
    };

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
    "array_api"_test = [] {
        struct position
        {
            position() noexcept = default;

            position(int x_, int y_)
              : x(x_)
              , y(y_)
            {}

            int x = 0, y = 0;
        };

        irt::array<position> positions;
        !expect(irt::status::success == positions.init(16u));

        expect(positions.size() == 16_u);
        expect(positions.capacity() == 16u);

        for (int i = 0; i != 16; ++i) {
            positions[i].x = i;
            positions[i].y = i;
        }

        for (int i = 0; i != 16; ++i) {
            expect(positions[i].x == i);
            expect(positions[i].y == i);
        }
    };

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
    "vector_api"_test = [] {
        struct position
        {
            position() noexcept = default;

            position(int x_, int y_)
              : x(x_)
              , y(y_)
            {}

            int x = 0, y = 0;
        };

        irt::vector<position> positions;
        !expect(irt::status::success == positions.init(16u));

        expect(positions.size() == 0_u);
        expect(positions.capacity() == 16u);

        {
            expect(positions.can_alloc(1u));
            auto it = positions.emplace_back(0, 1);
            expect(positions.size() == 1_u);
            expect(positions.begin() == it);
            expect(it->x == 0);
            expect(it->y == 1);
        }

        {
            auto ret = positions.try_emplace_back(2, 3);
            expect(ret.first == true);
            expect(ret.second == positions.begin() + 1);
            expect(positions.size() == 2_u);
            expect(positions.begin()->x == 0);
            expect(positions.begin()->y == 1);
            expect((positions.begin() + 1)->x == 2);
            expect((positions.begin() + 1)->y == 3);
        }

        {
            auto it = positions.erase_and_swap(positions.begin());
            expect(positions.size() == 1_u);
            expect(it == positions.begin());
            expect(it->x == 2);
            expect(it->y == 3);
        }

        positions.clear();
        expect(positions.size() == 0_ul);
        expect(positions.capacity() == 16_u);

        expect(positions.can_alloc(16u));
        for (int i = 0; i != 16; ++i)
            positions.emplace_back(i, i);

        auto ret = positions.try_emplace_back(16, 16);
        expect(ret.first == false);

        expect(positions.size() == 16_ul);
        expect(positions.capacity() == 16_u);

        auto it = positions.begin();
        while (it != positions.end())
            it = positions.erase_and_swap(it);

        expect(positions.size() == 0_ul);
        expect(positions.capacity() == 16_u);
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
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
467
468
    "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);
469
        expect(array.capacity() == 3);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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
        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 = [] {
        {
527
528
529
530
531
            irt::message vdouble;
            expect(vdouble.real[0] == 0.0);
            expect(vdouble.real[1] == 0.0);
            expect(vdouble.real[2] == 0.0);
            expect(vdouble.size() == 0_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
532
        }
533

Gauthier Quesnel's avatar
Gauthier Quesnel committed
534
        {
535
536
            irt::message vdouble(1.0);
            expect(vdouble[0] == 1.0);
537
538
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 0.0);
539
            expect(vdouble.size() == 1_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
540
        }
541

Gauthier Quesnel's avatar
Gauthier Quesnel committed
542
        {
543
544
545
            irt::message vdouble(0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 1.0);
546
            expect(vdouble[2] == 0.0);
547
            expect(vdouble.size() == 2_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
548
        }
549

Gauthier Quesnel's avatar
Gauthier Quesnel committed
550
        {
551
552
553
554
555
            irt::message vdouble(1.0, 2.0, 3.0);
            expect(vdouble[0] == 1.0);
            expect(vdouble[1] == 2.0);
            expect(vdouble[2] == 3.0);
            expect(vdouble.size() == 3_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
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
        }
    };

    "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();
        }
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
665
666
667
668
669
670
671
672
673
674
675
    "constant_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.constant_models.can_alloc(2));
        expect(sim.counter_models.can_alloc(1));

        auto& cnt = sim.counter_models.alloc();
        auto& c1 = sim.constant_models.alloc();
        auto& c2 = sim.constant_models.alloc();

Gauthier Quesnel's avatar
Gauthier Quesnel committed
676
677
        c1.default_value = 0.0;
        c2.default_value = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
678
679
680
681
682
683
684
685
686
687

        expect(sim.models.can_alloc(3));
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
        expect(irt::is_success(sim.alloc(c1, sim.constant_models.get_id(c1))));
        expect(irt::is_success(sim.alloc(c2, sim.constant_models.get_id(c2))));

        expect(sim.connect(c1.y[0], cnt.x[0]) == irt::status::success);
        expect(sim.connect(c2.y[0], cnt.x[0]) == irt::status::success);

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

Gauthier Quesnel's avatar
Gauthier Quesnel committed
690
691
692
693
694
695
696
697
698
699
        irt::status st;

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

        expect(cnt.number == 2_ul);
    };

700
701
702
703
704
705
706
707
708
709
710
711
    "cross_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.counter_models.can_alloc(1));
        expect(sim.cross_models.can_alloc(1));
        expect(sim.constant_models.can_alloc(1));

        auto& cnt = sim.counter_models.alloc();
        auto& cross1 = sim.cross_models.alloc();
        auto& c1 = sim.constant_models.alloc();

712
713
        // auto& value = sim.messages.alloc(3.0);
        // auto& threshold = sim.messages.alloc(0.0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
714
        //
715
716
        // c1.init[0] = sim.messages.get_id(value);
        // cross1.init[0] = sim.messages.get_id(threshold);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
717
718
719

        c1.default_value = 3.0;
        cross1.default_threshold = 0.0;
720
721
722

        expect(sim.models.can_alloc(3));
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
723
724
        expect(
          irt::is_success(sim.alloc(cross1, sim.cross_models.get_id(cross1))));
725
726
727
728
729
730
731
732
        expect(irt::is_success(sim.alloc(c1, sim.constant_models.get_id(c1))));

        expect(sim.connect(c1.y[0], cross1.x[0]) == irt::status::success);
        expect(sim.connect(c1.y[0], cross1.x[1]) == irt::status::success);
        expect(sim.connect(c1.y[0], cross1.x[2]) == irt::status::success);
        expect(sim.connect(cross1.y[0], cnt.x[0]) == irt::status::success);

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

735
736
        irt::status st;

737
        do {
738
739
740
741
742
743
744
            st = sim.run(t);
            expect(irt::is_success(st));
        } while (t < sim.end);

        expect(cnt.number == 2_ul);
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
745
    "generator_counter_simluation"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
746
        fmt::print("generator_counter_simluation\n");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
747
748
749
750
751
752
753
754
755
756
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.generator_models.can_alloc(1));
        expect(sim.counter_models.can_alloc(1));

        auto& gen = sim.generator_models.alloc();
        auto& cnt = sim.counter_models.alloc();

        expect(sim.models.can_alloc(2));
757
758
        expect(
          irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
759
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
760
761
762
763
764
765
766
767
768

        expect(sim.connect(gen.y[0], cnt.x[0]) == irt::status::success);

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

        sim.end = 10.0;

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

Gauthier Quesnel's avatar
Gauthier Quesnel committed
771
772
773
774
775
        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
776
            expect(cnt.number <= static_cast<irt::i64>(t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
777
778
779
780
        } while (t < sim.end);

        expect(cnt.number == 9_ul);
    };
781

K-H-Ismail's avatar
K-H-Ismail committed
782
    "time_func"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
783
        fmt::print("time_func\n");
K-H-Ismail's avatar
K-H-Ismail committed
784
        irt::simulation sim;
785
        const double duration = 30;
K-H-Ismail's avatar
K-H-Ismail committed
786
787
788
789
790
791
792
793

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.time_func_models.can_alloc(1));
        expect(sim.counter_models.can_alloc(1));

        auto& time_fun = sim.time_func_models.alloc();
        auto& cnt = sim.counter_models.alloc();

794
        time_fun.default_f = &irt::square_time_function;
795
        time_fun.default_sigma = 0.1;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
796

K-H-Ismail's avatar
K-H-Ismail committed
797
        expect(sim.models.can_alloc(2));
798
799
        expect(irt::is_success(
          sim.alloc(time_fun, sim.time_func_models.get_id(time_fun))));
K-H-Ismail's avatar
K-H-Ismail committed
800
801
802
803
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));

        expect(sim.connect(time_fun.y[0], cnt.x[0]) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
804
805
        irt::time t{ 0 };
        !expect(sim.initialize(t) == irt::status::success);
806
        double c = 0.0;
K-H-Ismail's avatar
K-H-Ismail committed
807
        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
808
809
810
            auto st = sim.run(t);
            !expect(irt::is_success(st));
            expect(time_fun.value == t * t);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
811
            c++;
812
        } while (t < duration);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
813
        expect(c == 2.0 * duration / time_fun.default_sigma - 1.0);
814
815
816
    };

    "time_func_sin"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
817
        fmt::print("time_func_sin\n");
818
819
820
        const double pi = std::acos(-1);
        const double f0 = 0.1;
        const double duration = 30;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
821
        irt::simulation sim;
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.time_func_models.can_alloc(1));
        expect(sim.counter_models.can_alloc(1));

        auto& time_fun = sim.time_func_models.alloc();
        auto& cnt = sim.counter_models.alloc();

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

        expect(sim.models.can_alloc(2));
        expect(irt::is_success(
          sim.alloc(time_fun, sim.time_func_models.get_id(time_fun))));
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));

        expect(sim.connect(time_fun.y[0], cnt.x[0]) == irt::status::success);

        irt::time t{ 0 };
        !expect(sim.initialize(t) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
842
        double c = 0;
843
844
845
846
847
        do {

            auto st = sim.run(t);
            !expect(irt::is_success(st));
            expect(time_fun.value == std::sin(2 * pi * f0 * t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
848
            c++;
849
        } while (t < duration);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
850
        expect(c == 2.0 * duration / time_fun.default_sigma - 1.0);
851
852
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
853
854
    "lotka_volterra_simulation"_test = [] {
        fmt::print("lotka_volterra_simulation\n");
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
        irt::simulation sim;

        expect(irt::is_success(sim.init(32lu, 512lu)));
        expect(sim.adder_2_models.can_alloc(2));
        expect(sim.mult_2_models.can_alloc(2));
        expect(sim.integrator_models.can_alloc(2));
        expect(sim.quantifier_models.can_alloc(2));

        auto& sum_a = sim.adder_2_models.alloc();
        auto& sum_b = sim.adder_2_models.alloc();
        auto& product = sim.mult_2_models.alloc();
        auto& integrator_a = sim.integrator_models.alloc();
        auto& integrator_b = sim.integrator_models.alloc();
        auto& quantifier_a = sim.quantifier_models.alloc();
        auto& quantifier_b = sim.quantifier_models.alloc();

        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;

        expect(sim.models.can_alloc(10));
        !expect(irt::is_success(
          sim.alloc(sum_a, sim.adder_2_models.get_id(sum_a), "sum_a")));
        !expect(irt::is_success(
          sim.alloc(sum_b, sim.adder_2_models.get_id(sum_b), "sum_b")));
        !expect(irt::is_success(
          sim.alloc(product, sim.mult_2_models.get_id(product), "prod")));
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
902
          integrator_a, sim.integrator_models.get_id(integrator_a), "int_a")));
903
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
904
          integrator_b, sim.integrator_models.get_id(integrator_b), "int_b")));
905
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
906
          quantifier_a, sim.quantifier_models.get_id(quantifier_a), "qua_a")));
907
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
908
          quantifier_b, sim.quantifier_models.get_id(quantifier_b), "qua_b")));
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926

        !expect(sim.models.size() == 7_ul);

        expect(sim.connect(sum_a.y[0], integrator_a.x[1]) ==
               irt::status::success);
        expect(sim.connect(sum_b.y[0], integrator_b.x[1]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], sum_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_b.x[0]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], product.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], product.x[1]) ==
               irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
927
928
        expect(sim.connect(product.y[0], sum_a.x[1]) == irt::status::success);
        expect(sim.connect(product.y[0], sum_b.x[1]) == irt::status::success);
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
956
957
958
959
960
961
962
963
964
965
966
967
968

        expect(sim.connect(quantifier_a.y[0], integrator_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(quantifier_b.y[0], integrator_b.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_a.y[0], quantifier_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], quantifier_b.x[0]) ==
               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);

        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);
        auto& obs_b = sim.observers.alloc(0.01,
                                          "B",
                                          static_cast<void*>(&fo_b),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator_a.id), obs_a);
        sim.observe(sim.models.get(integrator_b.id), obs_b);

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
        !expect(sim.sched.size() == 7_ul);

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

971
    "izhikevitch_simulation"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
972
        fmt::print("izhikevitch_simulation\n");
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
        irt::simulation sim;

        expect(irt::is_success(sim.init(64lu, 256lu)));
        expect(sim.constant_models.can_alloc(3));
        expect(sim.adder_2_models.can_alloc(3));
        expect(sim.adder_4_models.can_alloc(1));
        expect(sim.mult_2_models.can_alloc(1));
        expect(sim.integrator_models.can_alloc(2));
        expect(sim.quantifier_models.can_alloc(2));
        expect(sim.cross_models.can_alloc(2));

        auto& constant = sim.constant_models.alloc();
        auto& constant2 = sim.constant_models.alloc();
        auto& constant3 = sim.constant_models.alloc();
        auto& sum_a = sim.adder_2_models.alloc();
        auto& sum_b = sim.adder_2_models.alloc();
        auto& sum_c = sim.adder_4_models.alloc();
        auto& sum_d = sim.adder_2_models.alloc();
        auto& product = sim.mult_2_models.alloc();
        auto& integrator_a = sim.integrator_models.alloc();
        auto& integrator_b = sim.integrator_models.alloc();
        auto& quantifier_a = sim.quantifier_models.alloc();
        auto& quantifier_b = sim.quantifier_models.alloc();
        auto& cross = sim.cross_models.alloc();
        auto& cross2 = sim.cross_models.alloc();

        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;

        expect(sim.models.can_alloc(14));
        !expect(irt::is_success(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1045
          sim.alloc(constant3, sim.constant_models.get_id(constant3), "tfun")));
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
        !expect(irt::is_success(
          sim.alloc(constant, sim.constant_models.get_id(constant), "1.0")));
        !expect(irt::is_success(sim.alloc(
          constant2, sim.constant_models.get_id(constant2), "-56.0")));

        !expect(irt::is_success(
          sim.alloc(sum_a, sim.adder_2_models.get_id(sum_a), "sum_a")));
        !expect(irt::is_success(
          sim.alloc(sum_b, sim.adder_2_models.get_id(sum_b), "sum_b")));
        !expect(irt::is_success(
          sim.alloc(sum_c, sim.adder_4_models.get_id(sum_c), "sum_c")));
        !expect(irt::is_success(
          sim.alloc(sum_d, sim.adder_2_models.get_id(sum_d), "sum_d")));

        !expect(irt::is_success(
          sim.alloc(product, sim.mult_2_models.get_id(product), "prod")));
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1063
          integrator_a, sim.integrator_models.get_id(integrator_a), "int_a")));
1064
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1065
          integrator_b, sim.integrator_models.get_id(integrator_b), "int_b")));
1066
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1067
          quantifier_a, sim.quantifier_models.get_id(quantifier_a), "qua_a")));
1068
        !expect(irt::is_success(sim.alloc(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1069
          quantifier_b, sim.quantifier_models.get_id(quantifier_b), "qua_b")));
1070
1071
1072
1073
1074
1075
1076
1077
1078
        !expect(irt::is_success(
          sim.alloc(cross, sim.cross_models.get_id(cross), "cross")));
        !expect(irt::is_success(
          sim.alloc(cross2, sim.cross_models.get_id(cross2), "cross2")));

        !expect(sim.models.size() == 14_ul);

        expect(sim.connect(integrator_a.y[0], cross.x[0]) ==
               irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1079
1080
        expect(sim.connect(constant2.y[0], cross.x[1]) == irt::status::success);
        expect(sim.connect(integrator_a.y[0], cross.x[2]) ==
1081
1082
1083
1084
               irt::status::success);

        expect(sim.connect(cross.y[0], quantifier_a.x[0]) ==
               irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1085
1086
1087
1088
1089
1090
1091
1092
        expect(sim.connect(cross.y[0], product.x[0]) == irt::status::success);
        expect(sim.connect(cross.y[0], product.x[1]) == irt::status::success);
        expect(sim.connect(product.y[0], sum_c.x[0]) == irt::status::success);
        expect(sim.connect(cross.y[0], sum_c.x[1]) == irt::status::success);
        expect(sim.connect(cross.y[0], sum_b.x[1]) == irt::status::success);

        expect(sim.connect(constant.y[0], sum_c.x[2]) == irt::status::success);
        expect(sim.connect(constant3.y[0], sum_c.x[3]) == irt::status::success);
1093
1094

        expect(sim.connect(sum_c.y[0], sum_a.x[0]) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1095
1096
        // expect(sim.connect(integrator_b.y[0], sum_a.x[1]) ==
        // irt::status::success);
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
        expect(sim.connect(cross2.y[0], sum_a.x[1]) == irt::status::success);
        expect(sim.connect(sum_a.y[0], integrator_a.x[1]) ==
               irt::status::success);
        expect(sim.connect(cross.y[0], integrator_a.x[2]) ==
               irt::status::success);
        expect(sim.connect(quantifier_a.y[0], integrator_a.x[0]) ==
               irt::status::success);

        expect(sim.connect(cross2.y[0], quantifier_b.x[0]) ==
               irt::status::success);
        expect(sim.connect(cross2.y[0], sum_b.x[0]) == irt::status::success);
        expect(sim.connect(quantifier_b.y[0], integrator_b.x[0]) ==
               irt::status::success);
        expect(sim.connect(sum_b.y[0], integrator_b.x[1]) ==
               irt::status::success);

        expect(sim.connect(cross2.y[0], integrator_b.x[2]) ==
               irt::status::success);
        expect(sim.connect(integrator_a.y[0], cross2.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], cross2.x[2]) ==
               irt::status::success);
        expect(sim.connect(sum_d.y[0], cross2.x[1]) == irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_d.x[0]) ==
               irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1122
        expect(sim.connect(constant.y[0], sum_d.x[1]) == irt::status::success);
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
1152
1153
1154
1155
1156
1157

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

        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          &file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        file_output fo_b("izhikevitch_b.csv");
        expect(fo_b.os != nullptr);
        auto& obs_b = sim.observers.alloc(0.01,
                                          "B",
                                          static_cast<void*>(&fo_b),
                                          &file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator_a.id), obs_a);
        sim.observe(sim.models.get(integrator_b.id), obs_b);

        irt::time t = 0.0;

        expect(irt::status::success == sim.initialize(t));
        !expect(sim.sched.size() == 14_ul);

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

    "lotka_volterra_simulation_qss1"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1158
        fmt::print("lotka_volterra_simulation_qss1\n");
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
        irt::simulation sim;

        expect(irt::is_success(sim.init(32lu, 512lu)));
        expect(sim.qss1_wsum_2_models.can_alloc(2));
        expect(sim.qss1_multiplier_models.can_alloc(2));
        expect(sim.qss1_integrator_models.can_alloc(2));

        auto& sum_a = sim.qss1_wsum_2_models.alloc();
        auto& sum_b = sim.qss1_wsum_2_models.alloc();
        auto& product = sim.qss1_multiplier_models.alloc();
        auto& integrator_a = sim.qss1_integrator_models.alloc();
        auto& integrator_b = sim.qss1_integrator_models.alloc();

        integrator_a.default_X = 18.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1173
        integrator_a.default_dQ = 0.1;
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184

        integrator_b.default_X = 7.0;
        integrator_b.default_dQ = 0.1;

        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;

        expect(sim.models.can_alloc(10));
        !expect(irt::is_success(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1185
          sim.alloc(sum_a, sim.qss1_wsum_2_models.get_id(sum_a), "sum_a")));
1186
        !expect(irt::is_success(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1187
1188
1189
          sim.alloc(sum_b, sim.qss1_wsum_2_models.get_id(sum_b), "sum_b")));
        !expect(irt::is_success(sim.alloc(
          product, sim.qss1_multiplier_models.get_id(product), "prod")));
1190
        !expect(irt::is_success(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1191
1192
1193
          sim.alloc(integrator_a,
                    sim.qss1_integrator_models.get_id(integrator_a),
                    "int_a")));
1194
        !expect(irt::is_success(
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1195
1196
1197
          sim.alloc(integrator_b,
                    sim.qss1_integrator_models.get_id(integrator_b),
                    "int_b")));
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215

        !expect(sim.models.size() == 5_ul);

        expect(sim.connect(sum_a.y[0], integrator_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(sum_b.y[0], integrator_b.x[0]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], sum_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_b.x[0]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], product.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], product.x[1]) ==
               irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1216
1217
        expect(sim.connect(product.y[0], sum_a.x[1]) == irt::status::success);
        expect(sim.connect(product.y[0], sum_b.x[1]) == irt::status::success);
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251

        file_output fo_a("lotka-volterra-qss1_a.csv");
        file_output fo_b("lotka-volterra-qss1_b.csv");
        expect(fo_a.os != nullptr);
        expect(fo_b.os != nullptr);

        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);
        auto& obs_b = sim.observers.alloc(0.01,
                                          "B",
                                          static_cast<void*>(&fo_b),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator_a.id), obs_a);
        sim.observe(sim.models.get(integrator_b.id), obs_b);

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
        !expect(sim.sched.size() == 5_ul);

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

    "lotka_volterra_simulation_qss2"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1252
        fmt::print("lotka_volterra_simulation_qss2\n");
1253
1254
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
        irt::simulation sim;

        expect(irt::is_success(sim.init(32lu, 512lu)));
        expect(sim.qss2_wsum_2_models.can_alloc(2));
        expect(sim.qss2_multiplier_models.can_alloc(2));
        expect(sim.qss2_integrator_models.can_alloc(2));

        auto& sum_a = sim.qss2_wsum_2_models.alloc();
        auto& sum_b = sim.qss2_wsum_2_models.alloc();
        auto& product = sim.qss2_multiplier_models.alloc();
        auto& integrator_a = sim.qss2_integrator_models.alloc();
        auto& integrator_b = sim.qss2_integrator_models.alloc();

        integrator_a.default_X = 18.0;
        integrator_a.default_dQ = 0.1;

        integrator_b.default_X = 7.0;
        integrator_b.default_dQ = 0.1;

        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;

        expect(sim.models.can_alloc(10));
        !expect(irt::is_success(
          sim.alloc(sum_a, sim.qss2_wsum_2_models.get_id(sum_a), "sum_a")));
        !expect(irt::is_success(
          sim.alloc(sum_b, sim.qss2_wsum_2_models.get_id(sum_b), "sum_b")));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
        !expect(irt::is_success(sim.alloc(
          product, sim.qss2_multiplier_models.get_id(product), "prod")));
        !expect(irt::is_success(
          sim.alloc(integrator_a,
                    sim.qss2_integrator_models.get_id(integrator_a),
                    "int_a")));
        !expect(irt::is_success(
          sim.alloc(integrator_b,
                    sim.qss2_integrator_models.get_id(integrator_b),
                    "int_b")));
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309

        !expect(sim.models.size() == 5_ul);

        expect(sim.connect(sum_a.y[0], integrator_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(sum_b.y[0], integrator_b.x[0]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], sum_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_b.x[0]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], product.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], product.x[1]) ==
               irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1310
1311
        expect(sim.connect(product.y[0], sum_a.x[1]) == irt::status::success);
        expect(sim.connect(product.y[0], sum_b.x[1]) == irt::status::success);
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

        file_output fo_a("lotka-volterra-qss2_a.csv");
        file_output fo_b("lotka-volterra-qss2_b.csv");
        expect(fo_a.os != nullptr);
        expect(fo_b.os != nullptr);

        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);
        auto& obs_b = sim.observers.alloc(0.01,
                                          "B",
                                          static_cast<void*>(&fo_b),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator_a.id), obs_a);
        sim.observe(sim.models.get(integrator_b.id), obs_b);

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
        !expect(sim.sched.size() == 5_ul);

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

    "lif_simulation_qss"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
        fmt::print("lif_simulation_qss\n");
        irt::simulation sim;

        expect(irt::is_success(sim.init(32lu, 512lu)));
        expect(sim.adder_2_models.can_alloc(1));
        expect(sim.integrator_models.can_alloc(1));
        expect(sim.time_func_models.can_alloc(1));
        expect(sim.constant_models.can_alloc(1));
        expect(sim.cross_models.can_alloc(1));

        auto& sum = sim.adder_2_models.alloc();
        auto& quantifier = sim.quantifier_models.alloc();
        auto& integrator = sim.integrator_models.alloc();
        auto& I = sim.time_func_models.alloc();
        auto& constant_cross = sim.constant_models.alloc();
        auto& cross = sim.cross_models.alloc();

        double tau = 10.0;
        double Vt = 1.0;
        double V0 = 10.0;
        double Vr = -V0;

        sum.default_input_coeffs[0] = -1.0 / tau;
        sum.default_input_coeffs[1] = V0 / tau;

        constant_cross.default_value = Vr;

        integrator.default_current_value = 0.0;

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

        I.default_f = &irt::sin_time_function;
        I.default_sigma = quantifier.default_step_size;
        cross.default_threshold = Vt;

        expect(sim.models.can_alloc(10));
        !expect(irt::is_success(
          sim.alloc(sum, sim.adder_2_models.get_id(sum), "sum")));
        !expect(irt::is_success(sim.alloc(
          quantifier, sim.quantifier_models.get_id(quantifier), "qua")));
        !expect(irt::is_success(sim.alloc(
          integrator, sim.integrator_models.get_id(integrator), "int")));
        !expect(
          irt::is_success(sim.alloc(I, sim.time_func_models.get_id(I), "I")));
        !expect(
          irt::is_success(sim.alloc(constant_cross,
                                    sim.constant_models.get_id(constant_cross),
                                    "ctecro")));
        !expect(irt::is_success(
          sim.alloc(cross, sim.cross_models.get_id(cross), "cro")));

        !expect(sim.models.size() == 6_ul);

        // Connections

        expect(sim.connect(quantifier.y[0], integrator.x[0]) ==
               irt::status::success);
        expect(sim.connect(sum.y[0], integrator.x[1]) == irt::status::success);
        expect(sim.connect(cross.y[0], integrator.x[2]) ==
               irt::status::success);
        expect(sim.connect(cross.y[0], quantifier.x[0]) ==
               irt::status::success);
        expect(sim.connect(cross.y[0], sum.x[0]) == irt::status::success);
        expect(sim.connect(integrator.y[0], cross.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator.y[0], cross.x[2]) ==
               irt::status::success);
        expect(sim.connect(constant_cross.y[0], cross.x[1]) ==
               irt::status::success);
        expect(sim.connect(I.y[0], sum.x[1]) == irt::status::success);
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440

        file_output fo_a("lif-qss.csv");
        expect(fo_a.os != nullptr);

        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator.id), obs_a);

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
        !expect(sim.sched.size() == 6_ul);

        do {
            auto st = sim.run(t);
            expect(st == irt::status::success);
        } while (t < 100.0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1441
1442
    };

1443
    "lif_simulation_qss1"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
        fmt::print("lif_simulation_qss1\n");
        irt::simulation sim;

        expect(irt::is_success(sim.init(32lu, 512lu)));
        expect(sim.qss1_wsum_2_models.can_alloc(1));
        expect(sim.qss1_integrator_models.can_alloc(1));
        expect(sim.constant_models.can_alloc(2));
        expect(sim.qss1_cross_models.can_alloc(1));

        auto& sum = sim.qss1_wsum_2_models.alloc();
        auto& integrator = sim.qss1_integrator_models.alloc();
        auto& constant = sim.constant_models.alloc();
        auto& constant_cross = sim.constant_models.alloc();
        auto& cross = sim.qss1_cross_models.alloc();

        double tau = 10.0;
        double Vt = 1.0;
        double V0 = 10.0;
        double Vr = -V0;

        sum.default_input_coeffs[0] = -1.0 / tau;
        sum.default_input_coeffs[1] = V0 / tau;

        constant.default_value = 1.0;
        constant_cross.default_value = Vr;

        integrator.default_X = 0.0;
        integrator.default_dQ = 0.001;

        cross.default_threshold = Vt;

        expect(sim.models.can_alloc(10));
        !expect(irt::is_success(
          sim.alloc(sum, sim.qss1_wsum_2_models.get_id(sum), "sum")));
        !expect(irt::is_success(sim.alloc(
          integrator, sim.qss1_integrator_models.get_id(integrator), "int")));
        !expect(irt::is_success(
          sim.alloc(constant, sim.constant_models.get_id(constant), "cte")));
        !expect(
          irt::is_success(sim.alloc(constant_cross,
                                    sim.constant_models.get_id(constant_cross),
                                    "ctecro")));
        !expect(irt::is_success(
          sim.alloc(cross, sim.qss1_cross_models.get_id(cross), "cro")));

        !expect(sim.models.size() == 5_ul);

        // Connections

        // expect(sim.connect(cross.y[1], integrator.x[0]) ==
        // irt::status::success);
        expect(sim.connect(cross.y[0], integrator.x[1]) ==
               irt::status::success);
        expect(sim.connect(cross.y[1], sum.x[0]) == irt::status::success);
        expect(sim.connect(integrator.y[0], cross.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator.y[0], cross.x[2]) ==
               irt::status::success);
        expect(sim.connect(constant_cross.y[0], cross.x[1]) ==
               irt::status::success);
        expect(sim.connect(constant.y[0], sum.x[1]) == irt::status::success);
        expect(sim.connect(sum.y[0], integrator.x[0]) == irt::status::success);

        file_output fo_a("lif-qss1.csv");
        expect(fo_a.os != nullptr);

        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator.id), obs_a);

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
        !expect(sim.sched.size() == 5_ul);

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

1530
    "lif_simulation_qss2"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1531
        fmt::print("lif_simulation_qss2\n");
1532
        irt::simulation sim;
1533

1534
1535
1536
1537
1538
        expect(irt::is_success(sim.init(32lu, 512lu)));
        expect(sim.qss2_wsum_2_models.can_alloc(1));
        expect(sim.qss2_integrator_models.can_alloc(1));
        expect(sim.constant_models.can_alloc(2));
        expect(sim.qss2_cross_models.can_alloc(1));
1539

1540
1541
1542
1543
1544
        auto& sum = sim.qss2_wsum_2_models.alloc();
        auto& integrator = sim.qss2_integrator_models.alloc();
        auto& constant = sim.constant_models.alloc();
        auto& constant_cross = sim.constant_models.alloc();
        auto& cross = sim.qss2_cross_models.alloc();
1545

1546
        double tau = 10.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1547
1548
1549
        double Vt = 1.0;
        double V0 = 10.0;
        double Vr = -V0;
1550

1551
1552
        sum.default_input_coeffs[0] = -1.0 / tau;
        sum.default_input_coeffs[1] = V0 / tau;
1553

1554
1555
        constant.default_value = 1.0;
        constant_cross.default_value = Vr;
1556

1557
1558
        integrator.default_X = 0.0;
        integrator.default_dQ = 0.001;
1559

1560
        cross.default_threshold = Vt;
1561

1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
        expect(sim.models.can_alloc(10));
        !expect(irt::is_success(
          sim.alloc(sum, sim.qss2_wsum_2_models.get_id(sum), "sum")));
        !expect(irt::is_success(sim.alloc(
          integrator, sim.qss2_integrator_models.get_id(integrator), "int")));
        !expect(irt::is_success(
          sim.alloc(constant, sim.constant_models.get_id(constant), "cte")));
        !expect(
          irt::is_success(sim.alloc(constant_cross,
                                    sim.constant_models.get_id(constant_cross),
                                    "ctecro")));
        !expect(irt::is_success(
          sim.alloc(cross, sim.qss2_cross_models.get_id(cross), "cro")));
1575

1576
        !expect(sim.models.size() == 5_ul);
1577

1578
        // Connections
1579

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1580
1581
        // expect(sim.connect(cross.y[1], integrator.x[0]) ==
        // irt::status::success);
1582
1583
        expect(sim.connect(cross.y[0], integrator.x[1]) ==
               irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1584
1585
        expect(sim.connect(cross.y[1], sum.x[0]) == irt::status::success);

1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
        expect(sim.connect(integrator.y[0], cross.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator.y[0], cross.x[2]) ==
               irt::status::success);
        expect(sim.connect(constant_cross.y[0], cross.x[1]) ==
               irt::status::success);
        expect(sim.connect(constant.y[0], sum.x[1]) == irt::status::success);
        expect(sim.connect(sum.y[0], integrator.x[0]) == irt::status::success);

        file_output fo_a("lif-qss2.csv");
1596
1597
        expect(fo_a.os != nullptr);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1598
        auto& obs_a = sim.observers.alloc(0.001,
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator.id), obs_a);

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
        !expect(sim.sched.size() == 5_ul);

        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1613