public-api.cpp 40.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>
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
48
49
50
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);
    fmt::print(output->os, "{},{}\n", t, msg.to_real_64(0));
}

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
72
        expect(irt::is_detected_v<irt::transition_function_t, irt::generator> ==
               false);
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
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
668
669
670
671
672
673
674
        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 = [] {
        {
            irt::message v8(irt::i8(0), irt::i8(1), irt::i8(2), irt::i8(3));
            expect(v8.to_integer_8(0) == 0);
            expect(v8.to_integer_8(1) == 1);
            expect(v8.to_integer_8(2) == 2);
            expect(v8.to_integer_8(3) == 3);
            expect(v8.size() == 4_ul);
        }
        {
            irt::message v32(0, 1, 2, 3);
            expect(v32.to_integer_32(0) == 0);
            expect(v32.to_integer_32(1) == 1);
            expect(v32.to_integer_32(2) == 2);
            expect(v32.to_integer_32(3) == 3);
            expect(v32.size() == 4_ul);
        }
        {
            irt::message v64(
              irt::i64(0), irt::i64(1), irt::i64(2), irt::i64(3));
            expect(v64.to_integer_64(0) == 0);
            expect(v64.to_integer_64(1) == 1);
            expect(v64.to_integer_64(2) == 2);
            expect(v64.to_integer_64(3) == 3);
            expect(v64.size() == 4_ul);
        }
        {
            irt::message vfloat(0.0f, 1.0f, 2.0f, 3.0f);
            expect(vfloat.to_real_32(0) == 0.0_f);
            expect(vfloat.to_real_32(1) == 1.0_f);
            expect(vfloat.to_real_32(2) == 2.0_f);
            expect(vfloat.to_real_32(3) == 3.0_f);
            expect(vfloat.size() == 4_ul);
        }
        {
            irt::message vdouble(0.0, 1.0, 2.0, 3.0);
            expect(vdouble.to_real_64(0) == 0.0);
            expect(vdouble.to_real_64(1) == 1.0);
            expect(vdouble.to_real_64(2) == 2.0);
            expect(vdouble.to_real_64(3) == 3.0);
            expect(vdouble.size() == 4_ul);
        }
    };

    "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
675
676
677
678
679
680
681
682
683
684
685
    "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
686
687
        c1.default_value = 0.0;
        c2.default_value = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
688
689
690
691
692
693
694
695
696
697

        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
698
699
        expect(sim.initialize(t) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
700
701
702
703
704
705
706
707
708
709
        irt::status st;

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

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

710
711
712
713
714
715
716
717
718
719
720
721
    "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();

722
723
        // auto& value = sim.messages.alloc(3.0);
        // auto& threshold = sim.messages.alloc(0.0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
724
        //
725
726
        // c1.init[0] = sim.messages.get_id(value);
        // cross1.init[0] = sim.messages.get_id(threshold);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
727
728
729

        c1.default_value = 3.0;
        cross1.default_threshold = 0.0;
730
731
732

        expect(sim.models.can_alloc(3));
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
733
734
        expect(
          irt::is_success(sim.alloc(cross1, sim.cross_models.get_id(cross1))));
735
736
737
738
739
740
741
742
        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
743
744
        expect(sim.initialize(t) == irt::status::success);

745
746
        irt::status st;

747
        do {
748
749
750
751
752
753
754
            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
755
756
757
758
759
760
761
762
763
764
765
    "generator_counter_simluation"_test = [] {
        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));
766
767
        expect(
          irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
768
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
769
770
771
772
773
774
775
776
777

        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
778
779
        expect(sim.initialize(t) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
780
781
782
783
784
785
786
787
788
789
        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
            expect(cnt.number <= static_cast<long unsigned>(t));
        } while (t < sim.end);

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

Gauthier Quesnel's avatar
Gauthier Quesnel committed
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
    "generator_counter_and_copy_simluation"_test = [] {
        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));
        expect(
          irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));

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

        irt::model_id sources[2], destinations[2];
        sources[0] = gen.id;
        sources[1] = cnt.id;
        destinations[0] = static_cast<irt::model_id>(0);
        destinations[1] = static_cast<irt::model_id>(0);

        expect(sim.copy(std::begin(sources),
                        std::end(sources),
                        std::begin(destinations)) == irt::status::success);

        expect(destinations[0] != static_cast<irt::model_id>(0));
        expect(destinations[1] != static_cast<irt::model_id>(0));
        expect(destinations[0] != sources[0]);
        expect(destinations[1] != sources[0]);
        expect(destinations[0] != sources[1]);
        expect(destinations[1] != sources[1]);

        irt::model& gen_copy = sim.models.get(destinations[0]);
        expect(gen_copy.type == irt::dynamics_type::generator);
        irt::model& cnt_copy = sim.models.get(destinations[1]);
        expect(cnt_copy.type == irt::dynamics_type::counter);

        irt::time t = 0.0;
        expect(sim.initialize(t) == irt::status::success);
        expect(sim.models.size() == 4);

        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
            expect(cnt.number <= static_cast<long unsigned>(t));
        } while (t < 10.0);

        expect(cnt.number == 9_ul);

        irt::counter* dyn_cnt_copy = sim.counter_models.try_to_get(cnt_copy.id);
        expect(dyn_cnt_copy != nullptr);
        expect(dyn_cnt_copy->number == 9_ul);
    };

    "generators_counter"_test = [] {
        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));
        expect(
          irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
862
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
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

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

        irt::model_id sources[1], destinations[1];
        sources[0] = gen.id;
        destinations[0] = static_cast<irt::model_id>(0);

        expect(sim.copy(std::begin(sources),
                        std::end(sources),
                        std::begin(destinations)) == irt::status::success);

        expect(destinations[0] != static_cast<irt::model_id>(0));
        expect(destinations[0] != sources[0]);

        destinations[0] = static_cast<irt::model_id>(0);

        expect(sim.copy(std::begin(sources),
                        std::end(sources),
                        std::begin(destinations)) == irt::status::success);

        expect(destinations[0] != static_cast<irt::model_id>(0));
        expect(destinations[0] != sources[0]);

        irt::time t = 0.0;
        expect(sim.initialize(t) == irt::status::success);
        expect(sim.models.size() == 4);

        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
            expect(cnt.number <= static_cast<long unsigned>(3 * t));
        } while (t < 10.0);

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

K-H-Ismail's avatar
K-H-Ismail committed
901
902
903
904
905
906
907
908
909
910
    "time_func"_test = [] {
        irt::simulation sim;

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

911
        time_fun.default_f = &irt::square_time_function;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
912

K-H-Ismail's avatar
K-H-Ismail committed
913
        expect(sim.models.can_alloc(2));
914
915
        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
916
917
918
919
        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
920
921
        irt::time t{ 0 };
        !expect(sim.initialize(t) == irt::status::success);
K-H-Ismail's avatar
K-H-Ismail committed
922
        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
923
924
925
            auto st = sim.run(t);
            !expect(irt::is_success(st));
            expect(time_fun.value == t * t);
K-H-Ismail's avatar
K-H-Ismail committed
926
            expect(cnt.number <= static_cast<long unsigned>(t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
927
        } while (t < 30);
K-H-Ismail's avatar
K-H-Ismail committed
928
929
    };

930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
    "lotka_volterra_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        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();

Gauthier Quesnel's avatar
Gauthier Quesnel committed
947
        integrator_a.default_current_value = 18.0;
948

949
950
        quantifier_a.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
951
952
953
        quantifier_a.default_zero_init_offset = true;
        quantifier_a.default_step_size = 0.01;
        quantifier_a.default_past_length = 3;
954

Gauthier Quesnel's avatar
Gauthier Quesnel committed
955
        integrator_b.default_current_value = 7.0;
956

957
958
        quantifier_b.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
959
960
961
        quantifier_b.default_zero_init_offset = true;
        quantifier_b.default_step_size = 0.01;
        quantifier_b.default_past_length = 3;
962

Gauthier Quesnel's avatar
Gauthier Quesnel committed
963
964
965
966
967
968
        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;
969
970
971
972
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

        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(
          integrator_a, sim.integrator_models.get_id(integrator_a), "int_a")));
        !expect(irt::is_success(sim.alloc(
          integrator_b, sim.integrator_models.get_id(integrator_b), "int_b")));
        !expect(irt::is_success(sim.alloc(
          quantifier_a, sim.quantifier_models.get_id(quantifier_a), "qua_a")));
        !expect(irt::is_success(sim.alloc(
          quantifier_b, sim.quantifier_models.get_id(quantifier_b), "qua_b")));

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

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

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

1015
        irt::dot_writer dw(std::cout);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1016
        dw(sim);
1017

1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
        file_output fo_a("lotka-volterra_a.csv");
        file_output fo_b("lotka-volterra_b.csv");
        auto& obs_a = sim.observers.alloc(0.01, "A", static_cast<void*>(&fo_a));
        auto& obs_b = sim.observers.alloc(0.01, "B", static_cast<void*>(&fo_b));
        obs_a.initialize = &file_output_initialize;
        obs_a.observe = &file_output_observe;
        obs_b.initialize = &file_output_initialize;
        obs_b.observe = &file_output_observe;

        sim.models.get(integrator_a.id).obs_id = sim.observers.get_id(obs_a);
        sim.models.get(integrator_b.id).obs_id = sim.observers.get_id(obs_b);
        expect(fo_a.os != nullptr);
        expect(fo_b.os != nullptr);

1032
        irt::time t = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1033
1034

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

        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1038
            auto st = sim.run(t);
1039
1040
            expect(st == irt::status::success);
        } while (t < 15.0);
1041
    };
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1042

1043
    "izhikevitch_simulation"_test = [] {
1044
        irt::simulation sim;
1045

1046
        expect(irt::is_success(sim.init(64lu, 256lu)));
1047
        expect(sim.constant_models.can_alloc(3));
1048
1049
1050
1051
1052
1053
1054
        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));

K-H-Ismail's avatar
K-H-Ismail committed
1055
1056
        auto& constant = sim.constant_models.alloc();
        auto& constant2 = sim.constant_models.alloc();
1057
        auto& constant3 = sim.constant_models.alloc();
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
        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();

1070
1071
1072
1073
        double a = 0.2;
        double b = 2.0;
        double c = -56.0;
        double d = -16.0;
1074
        double I = -99.0;
1075
1076
        double vt = 30.0;

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1077
1078
        constant.default_value = 1.0;
        constant2.default_value = c;
1079
1080
1081
1082
        constant3.default_value = I;

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

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1084
1085
        integrator_a.default_current_value = 0.0;

1086
1087
        quantifier_a.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1088
1089
1090
1091
1092
1093
        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;

1094
1095
        quantifier_b.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
        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;
1106
        sum_b.default_input_coeffs[1] = a * b;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1107
1108
1109
1110
1111
1112
1113
        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;

1114
        expect(sim.models.can_alloc(14));
1115
        !expect(irt::is_success(
1116
          sim.alloc(constant3, sim.constant_models.get_id(constant3), "tfun")));
1117
        !expect(irt::is_success(
1118
1119
1120
          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")));
1121
1122
1123
1124
1125
1126
1127

        !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")));
1128
1129
        !expect(irt::is_success(
          sim.alloc(sum_d, sim.adder_2_models.get_id(sum_d), "sum_d")));
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140

        !expect(irt::is_success(
          sim.alloc(product, sim.mult_2_models.get_id(product), "prod")));
        !expect(irt::is_success(sim.alloc(
          integrator_a, sim.integrator_models.get_id(integrator_a), "int_a")));
        !expect(irt::is_success(sim.alloc(
          integrator_b, sim.integrator_models.get_id(integrator_b), "int_b")));
        !expect(irt::is_success(sim.alloc(
          quantifier_a, sim.quantifier_models.get_id(quantifier_a), "qua_a")));
        !expect(irt::is_success(sim.alloc(
          quantifier_b, sim.quantifier_models.get_id(quantifier_b), "qua_b")));
1141
1142
        !expect(irt::is_success(
          sim.alloc(cross, sim.cross_models.get_id(cross), "cross")));
1143
1144
        !expect(irt::is_success(
          sim.alloc(cross2, sim.cross_models.get_id(cross2), "cross2")));
1145

1146
        !expect(sim.models.size() == 14_ul);
1147
1148
1149

        expect(sim.connect(integrator_a.y[0], cross.x[0]) ==
               irt::status::success);
1150
        expect(sim.connect(constant2.y[0], cross.x[1]) == irt::status::success);
1151
1152
1153
        expect(sim.connect(integrator_a.y[0], cross.x[2]) ==
               irt::status::success);

1154
        expect(sim.connect(cross.y[0], quantifier_a.x[0]) ==
1155
               irt::status::success);
1156
1157
1158
1159
1160
        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);
1161

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

1165
1166
        expect(sim.connect(sum_c.y[0], sum_a.x[0]) == irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_a.x[1]) ==
1167
               irt::status::success);
1168
        expect(sim.connect(cross2.y[0], sum_a.x[1]) == irt::status::success);
1169
        expect(sim.connect(sum_a.y[0], integrator_a.x[1]) ==
1170
               irt::status::success);
1171
        expect(sim.connect(cross.y[0], integrator_a.x[2]) ==
1172
1173
1174
1175
               irt::status::success);
        expect(sim.connect(quantifier_a.y[0], integrator_a.x[0]) ==
               irt::status::success);

1176
        expect(sim.connect(cross2.y[0], quantifier_b.x[0]) ==
1177
               irt::status::success);
1178
        expect(sim.connect(cross2.y[0], sum_b.x[0]) == irt::status::success);
1179
        expect(sim.connect(quantifier_b.y[0], integrator_b.x[0]) ==
1180
               irt::status::success);
1181
        expect(sim.connect(sum_b.y[0], integrator_b.x[1]) ==
1182
1183
               irt::status::success);

1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
        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);
        expect(sim.connect(constant.y[0], sum_d.x[1]) == irt::status::success);

1195
        irt::dot_writer dw(std::cout);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1196
        dw(sim);
1197

1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
        file_output fo_a("izhikevitch_a.csv");
        file_output fo_b("izhikevitch_b.csv");
        auto& obs_a = sim.observers.alloc(0.01, "A", static_cast<void*>(&fo_a));
        auto& obs_b = sim.observers.alloc(0.01, "B", static_cast<void*>(&fo_b));
        obs_a.initialize = &file_output_initialize;
        obs_a.observe = &file_output_observe;
        obs_b.initialize = &file_output_initialize;
        obs_b.observe = &file_output_observe;

        sim.models.get(integrator_a.id).obs_id = sim.observers.get_id(obs_a);
        sim.models.get(integrator_b.id).obs_id = sim.observers.get_id(obs_b);
        expect(fo_a.os != nullptr);
        expect(fo_b.os != nullptr);

1212
        irt::time t = 0.0;
1213
1214

        expect(irt::status::success == sim.initialize(t));
1215
        !expect(sim.sched.size() == 14_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1216

1217
        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1218
            irt::status st = sim.run(t);
1219
            expect(st == irt::status::success);
1220
        } while (t < 120);
1221
    };
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1222
}