-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1796 lines (1587 loc) · 72.6 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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
469
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// #define WITH_ZLIB
#define WITH_BLOSC
#include "read_ppm.hpp"
#include <filesystem> // to delete previous zarr files
#include "nlohmann/json.hpp"
#include "xtensor/xarray.hpp"
// #include "xtensor-zarr/xzarr_hierarchy.hpp"
// #include "xtensor-zarr/xzarr_file_system_store.hpp"
// #include <xtensor/xarray.hpp>
// #include <xtensor/xio.hpp>
// #include <xtensor/xview.hpp>
// factory functions to create files, groups and datasets
#include "z5/factory.hxx"
// handles for z5 filesystem objects
#include "z5/filesystem/metadata.hxx"
#include "z5/filesystem/handle.hxx"
// io for xtensor multi-arrays
#include "z5/multiarray/xtensor_access.hxx"
// attribute functionality
#include "z5/attributes.hxx"
#include <memory>
#include <string>
#include <random>
#include <iterator>
#include <grpcpp/grpcpp.h>
#include <google/protobuf/empty.pb.h>
#include "fentonControl.grpc.pb.h"
#include "npy.hpp"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::FentonControl;
using helloworld::HelloReply2;
using helloworld::HelloRequest;
using helloworld::VmReply;
// using helloworld::StopSim;
#include <chrono>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
// #include <Windows.h>
#include <thread>
#include <queue>
#include <mutex>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow *window;
// #include <glm/glm.hpp>
// using namespace glm;
#include "shader.hpp"
// using namespace std;
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods);
void RunServer(std::unique_ptr<grpc::Server> *serverPtr);
void APIENTRY glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum severity,
GLsizei length, const char *message, const void *userParam);
bool lbutton_down = false;
bool enableGjModel = true;
bool increaseGj = false;
bool decreaseGj = false;
bool increaseGjZone = false;
bool decreaseGjZone = false;
bool increaseSensitivity = false;
bool decreaseSensitivity = false;
bool increaseAnis = false;
bool decreaseAnis = false;
std::mutex mtx;
std::unique_ptr<grpc::Server> *serverPtr;
// grpc control interface
bool paused = false;
clock_t t_start, t_end;
using namespace std;
queue<int> queueCmd;
queue<float *> queueReply; // kai float
// queue<double *> queueReply; // kai double
enum LoadFrom { file, manual};
// LoadFrom loadFrom = LoadFrom::manual;
LoadFrom loadFrom = LoadFrom::file;
// #define PPM_FILE R"(/home/lab/Pictures/trink.ppm)")
// #define PPM_FILE R"(/home/lab/Pictures/vct_773px.ppm)")
// #define PPM_FILE R"(/home/lab/Pictures/tankus_500x250.ppm)")
// #define PPM_FILE R"(/home/lab/Pictures/test_kliutis_1000x1000.ppm)") // veikia ||
// #define PPM_FILE R"(/home/lab/Pictures/real_vct_773px.ppm)") // pirmas real bandymas
// #define PPM_FILE R"(/home/lab/Pictures/real_2_vct_477x241px.ppm)") // pirmas real bandymas, veikia - fibril block
// #define PPM_FILE R"(/home/lab/Documents/10 imgs/10s.ppm)") // 10 bandymu (2022-06-01)
// #define PPM_FILE R"(/home/lab/Pictures/real 4 500x250 fibril.ppm)") // bandymas - fibril block
// #define PPM_FILE R"(/home/lab/Pictures/real 3 500x250.ppm)") // pirmas real bandymas
// #define PPM_FILE R"(/home/lab/Pictures/real 3 500x250 drift.ppm)") // gaunas drift
// #define PPM_FILE R"(/home/lab/Pictures/real 3 500x250 drift_test_all4345_trink.ppm)") // gaunas drift - all 4345 test
// const size_t sizeX = 477; // lasteliu turi buti lyginis skaicius, kad veiktu periodic boudaries
// const size_t sizeY = 241;
const int debugLevel = 0;
size_t sizeX = 500; // lasteliu turi buti lyginis skaicius, kad veiktu periodic boudaries
size_t sizeY = 250;
const size_t workGroupSizeX = 32; //32
const size_t workGroupSizeY = 32; //32
const size_t itersInFrame = 50; // dt_reg = dt_sim * itersInFrame
// **** Simulacijos trukme
// const float t_max = 160000.f; //ms
float t_max = 20050.f; //cv meas 40000.f; //ms
const size_t frame_chunk_size = 50; // cv meas 500; // kiek i_frame itaraciju vienam chunk
// Open a window and create its OpenGL context
int scale = 600;
int width = scale * sizeX / sizeY; //*2 kai 2 2d grafikai
int height = scale;
//uniform parameters
struct Par {
float dt = 0.02; //ms //kai float
} par;
//Shader storege buffer. For compute shader
float *ssb_u_host = new float[sizeX * sizeY];
float *ssb_v_host = new float[sizeX * sizeY];
float *ssb_w_host = new float[sizeX * sizeY];
float *ssb_J_ion_host = new float[sizeX * sizeY];
float *ssb_gj_host = new float[sizeX * sizeY * 3]; // *3: W,NW,NE
float *ssb_gj_par_host = new float[sizeX * sizeY * 3 * 7 * 2]; // *3: W,NW,NE; *7: parameters count; *2:gates count
float *ssb_gj_p_host = new float[sizeX * sizeY * 3 * 4]; // markov state matices for each gj
float *ssb_J_gj_host = new float[sizeX * sizeY * 3]; // *3: W,NW,NE
// reg:
float *ssb_u_reg_host = new float[frame_chunk_size*sizeX * sizeY]; // kolkas neisvedama?
float *ssb_gj_reg_host = new float[frame_chunk_size * sizeX * sizeY * 3];
const int dbg_size = 10;
float *ssb_dbg_host = new float[frame_chunk_size * dbg_size]; // 10 laisvu vietu
GLuint ssbo_u, ssbo_v, ssbo_w, ssbo_J_ion, ssbo_J_gj, ssbo_u_reg, ssbo_gj, ssbo_gj_reg, ssbo_gj_par, ssbo_gj_p, ssbo_dbg;
std::mt19937 rng;
int main(int argc, char *argv[])
{
rng.seed(123); // for initializing heterotypic GJs distribution in tissue
std::uniform_int_distribution<uint32_t> uint_dist100(0,100);
cout << "Starting fentonKarma..\n";
if (argc < 4 + 1) {
printf("Use syntax: fentonOpenGL <infile.ppm> <outZarr.zr> <time> <gj_mode(4sm/const)>\n");
return 0;
}
char *ppm_file = argv[1];
char *outZarrName = argv[2];
int t_max = stoi(argv[3]) + itersInFrame*par.dt; // +.. del to kad baigtu lygiai ties t_max (gaunasi [0...t_max])
enableGjModel = strcmp(argv[4],"4sm") ? false : true;
printf("argc = %d, ", argc);
printf("ppm_file = %s, ", argv[1]);
printf("outZarrDir = %s, ", argv[2]);
printf("t_max = %s, ", argv[3]);
printf("gj_mode = %s\n", argv[4]);
unsigned int imageSizeX = 0, imageSizeY = 0;
std::vector<RGB> image;
if (loadFrom == file) {
readPPM(image, imageSizeX, imageSizeY, std::string(ppm_file));
// for (int i=0; i < imageSizeX*imageSizeY; i++) {
// std::cout << (int)image[i].R << " ";
// }
sizeX = imageSizeX;
sizeY = imageSizeY;
// if (imageSizeX != sizeX || imageSizeY != sizeY) {
// cerr << "\\nerror form main(), after readPPM(): imageSizeX != sizeX || imageSizeY != sizeY\n"
// << "\nexiting..\n";
// return -1;
// }
printf("file loaded = %s\n", ppm_file);
}
printf("sizeX = %d\n", sizeX);
printf("sizeY = %d\n", sizeY);
// TODO: only for windows
// HWND consoleWindow = GetConsoleWindow();
// SetWindowPos(consoleWindow, 0, 0, 500, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
std::thread serverThread(RunServer, serverPtr); //TODO: clean server shutdown
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
window = glfwCreateWindow(width, height, "Playground", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
getchar();
glfwTerminate();
return -1;
}
glfwSetWindowPos(window, 0, 0);
glfwMakeContextCurrent(window);
if (debugLevel > 0)
cout << "\nGPU: " << glGetString(GL_VENDOR) << ", " << glGetString(GL_RENDERER) << endl;
// Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -1;
}
int flags;
glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
if (flags & GL_CONTEXT_FLAG_DEBUG_BIT)
{
// initialize debug output
if (debugLevel > 0)
cout << "Initialize debug output.\n";
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(glDebugOutput, nullptr);
// glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, GL_TRUE);
glDebugMessageControl(GL_DEBUG_SOURCE_API,
GL_DEBUG_TYPE_ERROR,
GL_DEBUG_SEVERITY_HIGH,
0, nullptr, GL_TRUE);
if (debugLevel > 0)
cout << "Initialize debug output. Done.\n";
}
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetKeyCallback(window, key_callback);
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
// Create and compile our GLSL program from the shaders
//ShProgs shIDs = LoadShaders("SimpleVertexShader.vert", "SimpleFragmentShader.frag", "compShader1.comp", "compShader2.comp");
#ifdef _WIN32
GLuint kernel1 = LoadComputeShader("C:\\OpenGL\\fentonGjOpenGL\\compShader1.comp", workGroupSizeX, workGroupSizeY);
GLuint kernel2 = LoadComputeShader("C:/OpenGL/fentonGjOpenGL/compShader2.comp", workGroupSizeX, workGroupSizeY);
GLuint vertFragShadersID = LoadVertexFragmentShaders("C:/OpenGL/fentonGjOpenGL/SimpleVertexShader.vert", "C:/OpenGL/fentonGjOpenGL/SimpleFragmentShader.frag");
GLuint line2DvertFragShadersID = LoadVertexFragmentShaders("C:/OpenGL/fentonGjOpenGL/linePlot.vert", "C:/OpenGL/fentonGjOpenGL/linePlot.frag");
#else
GLuint kernel1 = LoadComputeShader("/home/lab/Documents/fentonOpenGL/compShader1.comp", workGroupSizeX, workGroupSizeY);
GLuint kernel2 = LoadComputeShader("/home/lab/Documents/fentonOpenGL/compShader2.comp", workGroupSizeX, workGroupSizeY);
GLuint vertFragShadersID = LoadVertexFragmentShaders("/home/lab/Documents/fentonOpenGL/SimpleVertexShader.vert", "/home/lab/Documents/fentonOpenGL/SimpleFragmentShader.frag");
GLuint line2DvertFragShadersID = LoadVertexFragmentShaders("/home/lab/Documents/fentonOpenGL/linePlot.vert", "/home/lab/Documents/fentonOpenGL/linePlot.frag");
#endif
// initial conditions
for (int x = 0; x < sizeX; x++)
for (int y = 0; y < sizeY; y++)
{
// // real 3 bandymai
// const float offset_x = 0;
// const float offset_y = 0.;
// ssb_u_host[y * sizeX + x] = x > 200-offset_x && x < 360-offset_x && y > 0 - offset_y && y < 250 - offset_y ? 0.01 : 0.; // normal
// ssb_v_host[y * sizeX + x] = x > 200-offset_x && x < 360-offset_x && y > 0 - offset_y && y < 250 - offset_y ? 0.01 : 1.; //0.999970794;
// ssb_w_host[y * sizeX + x] = x > 200-offset_x && x < 360-offset_x && y > 0 - offset_y && y < 250 - offset_y ? 0.01 : 1.; //0.999938905;
// ssb_u_host[y * sizeX + x] = x > 340-offset_x && x < 360-offset_x && y > 20 - offset_y && y < 250 - offset_y ? 1. : 0.; // pridedam suzadinama zona ant virsaus
// real 2 bandymas veikia
// ssb_u_host[y * sizeX + x] = x > 300 && x < 330 && y > 140 && y < 210 ? 1. : 0.; // normal
// ssb_v_host[y * sizeX + x] = x > 240 && x < 320 && y > 40 && y < 210 ? 0.01 : 1.; //0.999970794;
// ssb_w_host[y * sizeX + x] = x > 240 && x < 320 && y > 40 && y < 210 ? 0.01 : 1.; //0.999938905;
// // // // fibril 477x241
// bool zona1 = x > 300 && x < 330 && y > 140 && y < 210; // suzadinimo
// bool zona2 = x < 300; // staciakampis aplink
// ssb_u_host[y * sizeX + x] = zona1 ? 1. : 0.; // normal
// ssb_v_host[y * sizeX + x] = zona2 && !zona1 ? 0.01 : .99; //0.999970794;
// ssb_w_host[y * sizeX + x] = zona2 && !zona1 ? 0.01 : .99; //0.999938905;
// // real 1 bandymas
// ssb_u_host[y * sizeX + x] = x > 515 && x < 575 && y > 605 && y < 610 ? 1. : 0.; // normal
// ssb_v_host[y * sizeX + x] = x > 515 && x < 575 && y > 595 && y < 605 ? 0.01 : 1.; //0.999970794;
// ssb_w_host[y * sizeX + x] = x > 515 && x < 575 && y > 595 && y < 605 ? 0.01 : 1.; //0.999938905;
// veikia || formos
// ssb_u_host[y * sizeX + x] = x > 180 && x < 200 && y > 270 && y < 300 ? 1. : 0.; // normal
// ssb_v_host[y * sizeX + x] = x > 170 && x < 180 && y > 270 && y < 300 ? 0.01 : 1.; //0.999970794;
// ssb_w_host[y * sizeX + x] = x > 170 && x < 180 && y > 270 && y < 300 ? 0.01 : 1.; //0.999938905;
// trink
// ssb_u_host[y * sizeX + x] = x < 40 && y > 400 ? 1. : 0.; // normal
// ssb_v_host[y * sizeX + x] = x < 400 && y < 450 ? 0.01 : 1.; //0.999970794;
// ssb_w_host[y * sizeX + x] = x < 400 && y < 450 ? 0.01 : 1.; //0.999938905;
// normal
ssb_u_host[y * sizeX + x] = 0; //
ssb_v_host[y * sizeX + x] = 1.; //0.999970794;
ssb_w_host[y * sizeX + x] = 1.; //0.999938905;
// ssb_u_host[y * sizeX + x] = x > 10 && x < 20 ? 1. : 0.; // rotor bandymai
ssb_J_ion_host[y * sizeX + x] = 0;
ssb_J_gj_host[y * sizeX * 3 + x * 3] = 0.f; //W
ssb_J_gj_host[y * sizeX * 3 + x * 3 + 1] = 0.f; //NW
ssb_J_gj_host[y * sizeX * 3 + x * 3 + 2] = 0.f; //NE
// ssb_gj_host[y * sizeX * 3 + x * 3] = .2e-6f; //W
// ssb_gj_host[y * sizeX * 3 + x * 3 + 1] = .05e-6f; //NW
// ssb_gj_host[y * sizeX * 3 + x * 3 + 2] = .05e-6f; //NE
ssb_gj_host[y * sizeX * 3 + x * 3] = 4e-8f; //W
ssb_gj_host[y * sizeX * 3 + x * 3 + 1] = 4e-8f; //NW
ssb_gj_host[y * sizeX * 3 + x * 3 + 2] = 4e-8f; //NE
//0.1
// float gmax = 8.5e-7f;
// float A = 4.f; //anisotropy
// float pars[6] = {0.1522, 0.0320, 0.2150, -34.2400, gmax, gmax / 10.f}; // lambda, alfa, beta, V0, Go, Gc
// float g_both = 50e-9f;
// float g_both = 50e-9f;
// float g_both = 8e-9f;
float A = 1.f; //anisotropy 1 vs 2// paskutinai exp - 1
float gmax43 = 300e-9f; //600 300
float gmax4543 = 90e-9f;//47 - kai linija 64 46 50-gaunas // paskutiniai exp - 100
// float gmax43 = 300e-9f; // susidaro prie 75-120-240 pulso
// float gmax4543 = 45e-9f;
//cx43
float pars43[6] = {0.1522, 0.0320, 0.2150, -34.2400, gmax43, gmax43 / 10.f}; // lambda, alfa, beta, V0, Go, Gc
float par43[14] = { //init, paskui keisim anis.
pars43[0], pars43[1], pars43[2], pars43[3], -1, pars43[4], pars43[5], // % left side // lambda, alfa, beta, V0, Pol, Go, Gc
pars43[0], pars43[1], pars43[2], pars43[3], -1, pars43[4], pars43[5]}; // % right side
// % Vj gating parameters of Cx43EGFP/Cx45 gap junction
// % lamda A_alfa A_beta V_0 P_g G_o G_c
// par = [ 0.0656 0.0346 0.1922 -30.6046 -1 2.396*2*gmax 2.396*2*0.0773*gmax; % Cx43 side
// 0.1005 0.0390 0.0694 -14.4894 -1 2*gmax 2*0.0157*gmax]; % Cx45 side
// limit = 4.5508; % threshold transition rate
// Cx43/45 hetero, mindaugo 2021-06-28
// double gmax = 1.0e-9; //.e-9;
// gmax4543 *= 1e-11;
// // seni
// float gmax4543 = g_both*0.4;
// double par4543[14] = {0.0656, 0.0346, 0.1922, 30.6046, 1, 2.396 * 2 * gmax4543, 2.396 * 2 * 0.0773 * gmax4543, // left side
// 0.1005, 0.0390, 0.0694, 14.4894, 1, 2 * gmax4543, 2 * 0.0157 * gmax4543}; // right side
// MS 2021-09-15
// float gmax4543 = 1.0f;
// float gmax4543 = g_both*3.15;
// double par4543[14] = {0.0462, 0.0264 , 0.1525, -7.8248, -1, gmax4543, 0.0033 * gmax4543, // left side
// 0.0567, 0.0159, 0.3085, -15.0997, -1, gmax4543 * 0.158, 0.0004 * gmax4543 * 0.158}; // right side, 0.158 pagal 45 : 43 open busenu laidumu santyki
// {"lambda1":0.7083354891481388,"A_alpha1":0.35398071557426974,"A_beta1":0.1446391702724877,"V01":-75.12289637286416,"polarity1":-1,"Go1":1,"Gc1":0.024245815743767676,
// "lambda2":0.04123104898401486,"A_alpha2":0.03370471203601321,"A_beta2":0.133229953423052,"V02":-15.266576202701913,"polarity2":-1,"Go2":0.158,"Gc2":0.010556548377678457,"limit":2.1815270775900486}
// // Cx43/45 hetero, KM 2021-10-06 'par + card(25proc)+70_hyst+-80_step -V0 v2.json'
// // float gmax4543 = 1.0f;
// float gmax4543 = g_both*0.5*1.47*3.72*0.6;
// double par4543[14] = {0.7083354891481388, 0.35398071557426974 , 0.1446391702724877, -75.12289637286416, -1, gmax4543, gmax4543*0.024245815743767676, // left side
// 0.04123104898401486, 0.03370471203601321, 0.133229953423052, -15.266576202701913, -1, gmax4543*0.158, gmax4543 * 0.010556548377678457};
// MS 2021-11-04
// float gmax4543 = g_both*0.8;
double par4543[14] = {0.0257, 0.0218, 0.0899, -37.5, -1, gmax4543*0.8876, gmax4543*0.07,
0.0525, 0.039, 0.0993, -5.22, -1, gmax4543*0.816, gmax4543*0.0125};
double limit = 4.83; // <--- TODO: kolkas limit reikia rankiniu budu ivesti i compShader1
// // KM 2021-10-29
// float gmax4543 = g_both*0.5;
// double par4543[14] = {0.0257, 0.0218, 0.0899, -37.5, -1, gmax4543*0.8876, gmax4543*0.07,
// 0.0525, 0.039, 0.0993, -5.22, -1, gmax4543*0.816, gmax4543*0.0125};
// double limit = 4.83; // <--- TODO: kolkas limit reikia rankiniu budu ivesti i compShader1
// // MS 2021-10-16
// float par_mind[13] = {0.0943, 0.1054, 0.0416, -1.7565, 0.0377, 0.0403, 0.0336, 0.2035, -18.3999, 0.0262, 31.4403, 98.1429, 0.1135};
// float gmax4543 = g_both*0.015;
// double par4543[14] = {par_mind[5], par_mind[6], par_mind[7], par_mind[8], -1, gmax4543*par_mind[11], gmax4543*par_mind[11]*par_mind[9],
// par_mind[0], par_mind[1], par_mind[2], par_mind[3], -1, gmax4543*par_mind[10], gmax4543*par_mind[10]*par_mind[4]};
// double limit = par_mind[12]; // <--- TODO: kolkas limit reikia rankiniu budu ivesti i compShader1
// // Cx43/45 hetero bkp
// // double gmax = 1.0e-9; //.e-9;
// float gmax4543 = g_both*0.6;
// // gmax4543 *= 1e-11;
// double par4543[14] = {0.0001, 0.0239, 0.0912, -19.0232, -1, 4 * 2 * gmax4543, 4 * 2 * 0.0773 * gmax4543, // left side
// 0.1115, 0.0179, 0.1051, -1.6781, -1, 2 * gmax4543, 2 * 0.0112 * gmax4543}; // right side
// nustatom PJ param(g dar bus keiciamas)
for (int i = 0; i < 3; i++) //W,NW,NE counter
{
for (int ii = 0; ii < 4; ii++)
ssb_gj_p_host[y * sizeX * 3 * 4 + x * 3 * 4 + i * 4 + ii] = 0.f;
for (int ii = 0; ii < 7 * 2; ii++) //copy all parameters of all gates
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par43[ii]; // visi par
}
// pradzioj laikom kad visos PJ nelaidzios
for (int sel = 0; sel < 3; sel++)
{
float gjOpen = 1.e-30f;
float gjClosed = 1.e-30f;
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 5] = gjOpen; //Go1 set anis
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 6] = gjClosed; //Gc1 set anis
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 12] = gjOpen; //Go2 set anis
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 13] = gjClosed; //Gc2 set anis
}
// pridedam laidzia zona
// if (x < y && (-x + sizeY) > y || x < y && (-x + sizeY) > y) // zonos matmenys
// triangles lines & middle channel box
// float k = 1.f;
float k = 1.f;
int middleBoxHeight = 6;////sizeY / 2;
int topY = sizeY / 2 + middleBoxHeight/2;
int bottomY = sizeY / 2 - middleBoxHeight/2;
// if ( // trikampe zona
// x * k < y // right bottom
// && (-x * k + sizeY) > y // right top
// // ||
// // (-x + sizeY) * k < y // left bottom
// // && (x - sizeX) * k + sizeY > y // left top
// ||
// y < topY && y > bottomY // middle channel box
// )
{
float storis = 4.f;
for (int i = 0; i < 3; i++) //W,NW,NE counter
{
// const float cx_change_rate = 0.9;
// bool is4345 = uint_dist100(rng) <= 60 * ( (float)x/sizeX*cx_change_rate );
bool isNonCond = uint_dist100(rng) >= 60; // cx43 ar cx4345 heterotyp. tikymybe
bool is4345 = uint_dist100(rng) >= 40; // cx43 ar cx4345 heterotyp. tikymybe
bool orientation = uint_dist100(rng) >= 50; // orientacija erdveje issibarsciusi vienodomis tikimybemis
for (int ii = 0; ii < 7 * 2; ii++) //copy all parameters of all gates
{
if (loadFrom == LoadFrom::file)
switch(image[y*imageSizeX+x].R) { // set gj accoring input image
case 0: // no-cond
break;
case 128: // 4345 !!!!!
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par4543[ii];
break;
case 255: // high cond (43)
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par43[ii];
break;
default:
// cerr << "switch(image[y*imageSizeX+x].R) ... unknown .R value: " << image[y*imageSizeX+x].R
// << "\nexiting...\n";
// return 0;
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par4543[ii];
break;
}
else {
// if (x > 32 - storis / 2.f && x < 32 + storis / 2.f) //heterotypic box, Cx43-45 zona // {// "brick wall", hetero-line: W, NW - cx4345 p(+); NE - cx4345 p(-)
// if (x > 32 - storis / 2.f && x < 32 + storis / 2.f && uint_dist10(rng) > 7) //heterotypic box, Cx43-45 zona // {// "brick wall", hetero-line: W, NW - cx4345 p(+); NE - cx4345 p(-)
// if (x > 32 - storis / 2.f && is4345) // su random distribution
// if (x >= sizeX-1) { // add non conductive line
// }
// else
// if (!isNonCond) // Cx43 zona
// {
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par43[ii];
// }
// if (x > 32 - storis / 2.f && x < 32 + storis / 2.f) //heterotypic box, Cx43-45 zona // {// "brick wall", hetero-line: W, NW - cx4345 p(+); NE - cx4345 p(-)
// if (x > 30 ) //heterotypic box, Cx43-45 zona
if (x > 10 && (x % 20 > 0) && (x % 20 <= 2) ) //kas 20 po 2 last, Cx43-45 zona straipsniui
// if (x > 10 && (x % 20 > 0) && (x % 20 <= 10) ) //cv bandymai, Cx43-45 zona
// else if (is4345 && ii != 2) //reentry tyrimams Cx43-45 zona // {// "brick wall", hetero-line: W, NW - cx4345 p(+); NE - cx4345 p(-)
// if (x >= 70 && x <= 72 && y >= 75-50 && y < 75+50)
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par4543[ii];
// if (i == 2) //NE,
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par4543[ii];;
// }
else
// tiesiog visa zona cx4345
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par4543[ii];
// // tiesiog visa zona cx43
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + ii] = par43[ii];
}
}
// if (isNonCond) {
// float gjOpen = 1.e-30f;
// float gjClosed = 1.e-30f;
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 5] = gjOpen; //Go1 set anis
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 6] = gjClosed; //Gc1 set anis
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 12] = gjOpen; //Go2 set anis
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 13] = gjClosed; //Gc2 set anis
// }
// NW junciu apsukimas, kad tolygiai einu left->right cx43->cx45, (zr. uzrasus "zadinimo eiliskumas")
if (i == 1) { // NW
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 3] *= -1.f; // V01
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 4] *= -1.f; // Pol1
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 10] *= -1.f; // V02
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 11] *= -1.f; // Pol2
}
// if (orientation) { // orientacijos erdveje issibarstymas
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 3] *= -1.f; // V01
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 4] *= -1.f; // Pol1
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 10] *= -1.f; // V02
// ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + i * 7 * 2 + 11] *= -1.f; // Pol2
// }
}
// printf("%d ", rand());
int sel = 0; // select W
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 5] *= A; //Go1 set anis
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 6] *= A; //Gc1 set anis
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 12] *= A; //Go2 set anis
ssb_gj_par_host[y * sizeX * 3 * 7 * 2 + x * 3 * 7 * 2 + sel * 7 * 2 + 13] *= A; //Gc2 set anis
}
//for (int i = 0; i < 1; i++)
//ssb_dbg_host[y * sizeX * 1 + x * 1 + i] = 0;
}
for (int i = 0; i < frame_chunk_size; i++)
{
for (int y=0; y < sizeY; y++)
for (int x=0; x < sizeX; x++) {
ssb_u_reg_host[i*sizeX*sizeY + y*sizeX + x] = y*(i+1);//.f; //sin(float(i) / 100);
ssb_gj_reg_host[i*sizeX*sizeY*3 + y*sizeX*3 + x*3 + 0] = 0;//i+1;
ssb_gj_reg_host[i*sizeX*sizeY*3 + y*sizeX*3 + x*3 + 1] = 0;//2*(i+1);
ssb_gj_reg_host[i*sizeX*sizeY*3 + y*sizeX*3 + x*3 + 2] = 0;//3*(i+1);
}
// ssb_dbg_host[i * dbg_size + 0] = 0.f;
// ssb_dbg_host[i * dbg_size + 1] = 0.f;
// ssb_dbg_host[i * dbg_size + 2] = 0.f;
// ssb_dbg_host[i * dbg_size + 3] = 0.f;
// ssb_dbg_host[i * dbg_size + 4] = 0.f;
// ssb_dbg_host[i * dbg_size + 5] = 0.f;
// ssb_dbg_host[i * dbg_size + 6] = 0.f;
// ssb_dbg_host[i * dbg_size + 7] = 0.f;
}
// GLuint ssbo_u, ssbo_v, ssbo_w, ssbo_J_ion, ssbo_J_gj, ssbo_u_reg, ssbo_gj, ssbo_gj_reg, ssbo_gj_par, ssbo_gj_p, ssbo_dbg;
glGenBuffers(1, &ssbo_u);
glGenBuffers(1, &ssbo_v);
glGenBuffers(1, &ssbo_w);
glGenBuffers(1, &ssbo_J_ion);
glGenBuffers(1, &ssbo_J_gj);
glGenBuffers(1, &ssbo_u_reg);
glGenBuffers(1, &ssbo_gj);
glGenBuffers(1, &ssbo_gj_reg);
glGenBuffers(1, &ssbo_gj_par);
glGenBuffers(1, &ssbo_gj_p);
glGenBuffers(1, &ssbo_dbg);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_u);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeX * sizeY * sizeof(ssb_u_host[0]), ssb_u_host, GL_DYNAMIC_COPY); //sizeof(data) only works for statically sized C/C++ arrays.
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo_u);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_v);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeX * sizeY * sizeof(ssb_v_host[0]), ssb_v_host, GL_DYNAMIC_COPY); //sizeof(data) only works for statically sized C/C++ arrays.
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, ssbo_v);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_w);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeX * sizeY * sizeof(ssb_w_host[0]), ssb_w_host, GL_DYNAMIC_COPY); //sizeof(data) only works for statically sized C/C++ arrays.
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, ssbo_w);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_J_ion);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeX * sizeY * sizeof(ssb_J_ion_host[0]), ssb_J_ion_host, GL_DYNAMIC_COPY); //sizeof(data) only works for statically sized C/C++ arrays.
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, ssbo_J_ion);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_J_gj);
glBufferData(GL_SHADER_STORAGE_BUFFER, 3 * sizeX * sizeY * sizeof(ssb_J_gj_host[0]), ssb_J_gj_host, GL_DYNAMIC_COPY); //sizeof(data) only works for statically sized C/C++ arrays.
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, ssbo_J_gj);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_u_reg);
glBufferData(GL_SHADER_STORAGE_BUFFER, frame_chunk_size * sizeX*sizeY * sizeof(ssb_u_reg_host[0]), ssb_u_reg_host, GL_STREAM_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 9, ssbo_u_reg);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_gj);
glBufferData(GL_SHADER_STORAGE_BUFFER, 3 * sizeX * sizeY * sizeof(ssb_gj_host[0]), ssb_gj_host, GL_DYNAMIC_COPY); //
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 10, ssbo_gj);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_gj_reg);
glBufferData(GL_SHADER_STORAGE_BUFFER, 3 * frame_chunk_size * sizeX*sizeY * sizeof(ssb_gj_reg_host[0]), ssb_gj_reg_host, GL_STREAM_COPY); // TODO: GL_STREAM_COPY vs GL_DYNAMIC_COPY
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 11, ssbo_gj_reg);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_gj_par);
glBufferData(GL_SHADER_STORAGE_BUFFER, 3 * 2 * 7 * sizeX * sizeY * sizeof(ssb_gj_par_host[0]), ssb_gj_par_host, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 12, ssbo_gj_par);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_gj_p);
glBufferData(GL_SHADER_STORAGE_BUFFER, 3 * 4 * sizeX * sizeY * sizeof(ssb_gj_p_host[0]), ssb_gj_p_host, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 13, ssbo_gj_p);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_dbg);
glBufferData(GL_SHADER_STORAGE_BUFFER, frame_chunk_size * sizeof(ssb_dbg_host[0]), ssb_dbg_host, GL_DYNAMIC_COPY); //sizeof(data) only works for statically sized C/C++ arrays.
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 15, ssbo_dbg);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); //unbind
glUseProgram(kernel1); //glUseProgram(shIDs.compID1);
//GLint location = glGetUniformLocation(shIDs.compID1, "sizeXY"); printf("location %d \n", location); if (location == -1) { printf("Could not locate uniform location in CS. "); }
glUniform2i(20, sizeX, sizeY);
//location = glGetUniformLocation(shIDs.compID1, "dt_sim"); printf("location %d \n", location); if (location == -1) { printf("Could not locate uniform location in CS. "); }
glUniform1f(21, par.dt); // kai float
// glUniform1d(21, par.dt); // kai double
GLint location = glGetUniformLocation(kernel1, "gjModelEnabled"); //galima ir su location
glUniform1i(location, enableGjModel);
glUseProgram(kernel2); //glUseProgram(shIDs.compID2);
glUniform2i(20, sizeX, sizeY);
// glUniform1d(21, par.dt); //kai double
glUniform1f(21, par.dt); //kai float
glUseProgram(vertFragShadersID); //glUseProgram(shIDs.frVeID);
glUniform2i(20, sizeX, sizeY);
glUseProgram(line2DvertFragShadersID); //glUseProgram(shIDs.frVeID);
glUniform2i(20, sizeX, sizeY);
glUniform1ui(glGetUniformLocation(line2DvertFragShadersID, "frame_chunk_size"), frame_chunk_size);
float *vertices = new float[sizeX * sizeY * 4]; //*4 because xy screen, and xy cell nr.
unsigned int *conn1 = new unsigned int[(sizeX - 1) * (sizeY - 1) * 3];
unsigned int *conn2 = new unsigned int[(sizeX - 1) * (sizeY - 1) * 3];
unsigned int *conn = new unsigned int[(sizeX - 1) * (sizeY - 1) * 6];
for (unsigned int y = 0; y < sizeY; y++)
for (unsigned int x = 0; x < sizeX; x++)
{
if ((y + 1) % 2)
vertices[y * 4 * sizeX + x * 4] = (float)x / sizeX; //X screen
else
vertices[y * 4 * sizeX + x * 4] = (float)(x + 0.5) / sizeX; //X screen
vertices[y * 4 * sizeX + x * 4 + 1] = (float)y / sizeY; //Y screen
((unsigned int *)(vertices))[y * 4 * sizeX + x * 4 + 2] = x; //x cell nr. (int), konvertuojam i unsigned int, nes float ir int uzima tiek pat (32b)
((unsigned int *)(vertices))[y * 4 * sizeX + x * 4 + 3] = y; //y cell nr. (int)
}
for (unsigned int y = 0; y < sizeY - 1; y++)
for (unsigned int x = 0; x < sizeX - 1; x++)
{
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2] = y * sizeX + x; // size-1 nes paskutinis taskas neturi trikampio
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 1] = y * sizeX + x + 1; //2-tra virsune
if (y % 2 == 0)
{
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 2] = (y + 1) * sizeX + x; //3-cia virsune
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 3] = y * sizeX + x + 1;
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 4] = (y + 1) * sizeX + x + 1;
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 5] = (y + 1) * sizeX + x;
}
else
{
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 2] = (y + 1) * sizeX + x + 1;
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 3] = y * sizeX + x;
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 4] = (y + 1) * sizeX + x + 1;
conn[y * 3 * 2 * (sizeX - 1) + x * 3 * 2 + 5] = (y + 1) * sizeX + x;
}
}
//*** prepare buffers for drawing hex map
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); // svarbu, nes pagal ji piesiama (VBO tik storage)
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeX * sizeY * 4 * sizeof(float), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (sizeX - 1) * (sizeY - 1) * 2 * 3 * sizeof(unsigned int), conn, GL_STATIC_DRAW);
// position on screen (xy) attribute
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
// cell nr (xy) attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(int), (void *)(2 * sizeof(int))); // saugom int reiksmes float masyve, nes uzima tiek pat 32b
glEnableVertexAttribArray(1);
//glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, VBO); // eksperimentai su vertex array modifikavimu per ssb
//*** prepare buffers for drawing 2d line plot
unsigned int VBO2, VBO3, VAO2, VAO3, EBO2;
glGenVertexArrays(1, &VAO2);
glGenVertexArrays(1, &VAO3);
glGenBuffers(1, &VBO2);
glGenBuffers(1, &VBO3);
glGenBuffers(1, &EBO2);
glBindVertexArray(VAO2);
glBindBuffer(GL_ARRAY_BUFFER, VBO2);
//unsigned int elem[] = { 1 };
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO2);
//glBufferData(GL_ELEMENT_ARRAY_BUFFER, 1 * sizeof(unsigned int), elem, GL_STATIC_DRAW);
const int line_points_count = 2000; //width
GLfloat line[line_points_count];
for (int i = 0; i < line_points_count; i++)
{
line[i] = (i - line_points_count / 2) / float(line_points_count / 2);
}
glBufferData(GL_ARRAY_BUFFER, sizeof line, line, GL_STATIC_DRAW);
GLint attribute_coord1d = glGetAttribLocation(line2DvertFragShadersID, "coord1d");
if (attribute_coord1d == -1)
{
printf("Could not locate \"attribute_coord1d\" in line2d shader. ");
}
glEnableVertexAttribArray(attribute_coord1d);
glVertexAttribPointer(
attribute_coord1d, // attribute
1, // number of elements per vertex, here just x
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no space between values
0 // use the vertex buffer object
);
glBindVertexArray(VAO3);
glBindBuffer(GL_ARRAY_BUFFER, VBO3);
GLfloat line2[line_points_count];
for (int i = 0; i < line_points_count; i++)
{
line2[i] = (i - line_points_count / 2) / float(line_points_count / 2);
}
glBufferData(GL_ARRAY_BUFFER, sizeof line2, line2, GL_STATIC_DRAW);
attribute_coord1d = glGetAttribLocation(line2DvertFragShadersID, "coord1d");
if (attribute_coord1d == -1)
{
printf("Could not locate \"attribute_coord1d\" in line2d shader. ");
}
glEnableVertexAttribArray(attribute_coord1d);
glVertexAttribPointer(
attribute_coord1d, // attribute
1, // number of elements per vertex, here just x
GL_FLOAT, // the type of each element
GL_FALSE, // take our values as-is
0, // no space between values
0 // use the vertex buffer object
);
glBindVertexArray(0);
//***
//some info print
//int work_grp_cnt[3];
//glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 0, &work_grp_cnt[0]);
//glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 1, &work_grp_cnt[1]);
//glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_COUNT, 2, &work_grp_cnt[2]);
//printf("max global (total) work group size x:%i y:%i z:%i\n",
// work_grp_cnt[0], work_grp_cnt[1], work_grp_cnt[2]);
//int work_grp_size[3];
//glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &work_grp_size[0]);
//glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 1, &work_grp_size[1]);
//glGetIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 2, &work_grp_size[2]);
//printf("max local (in one shader) work group sizes x:%i y:%i z:%i\n",
// work_grp_size[0], work_grp_size[1], work_grp_size[2]);
//int work_grp_inv;
//glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &work_grp_inv);
//printf("max local work group invocations %i\n", work_grp_inv);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
//width = sizeX*10; height = sizeY*10;
//glViewport(0, 0, width, height);
//glMatrixMode(GL_PROJECTION);
//float aspect = (float)width / (float)height;
//glOrtho(-aspect, aspect, -1, 1, -1, 1);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//glDrawElements(GL_TRIANGLES, (sizeX - 1) * (sizeY - 1) * 2 * 3, GL_UNSIGNED_INT, 0);
//
// ***** Zarr ****
//
// std::filesystem::remove_all("data1.zr");
#ifdef _WIN32
std::filesystem::remove_all("d:\data_tmp1.zr");
// z5::filesystem::handle::File f("data1.zr", z5::FileMode::modes::w);
z5::filesystem::handle::File f("d:\data_tmp1.zr", z5::FileMode::modes::w);
#else
// std::filesystem::remove_all("data_latest.zr");
std::filesystem::remove_all(outZarrName);
// z5::filesystem::handle::File f("data1.zr", z5::FileMode::modes::w);
// z5::filesystem::handle::File f("data_latest.zr", z5::FileMode::modes::w);
z5::filesystem::handle::File f(outZarrName, z5::FileMode::modes::w);
#endif
// create the file in zarr format
const bool createAsZarr = true;
z5::createFile(f, createAsZarr);
// create a new zarr dataset
size_t t_max_ceil_to_chunks = ceil(t_max / (double)frame_chunk_size) * frame_chunk_size;
const size_t i_frame_max = t_max * 1. / par.dt / itersInFrame; //6000e3; // dt*iter_in_frame= 0.02*10=0.2 ms per frame
// tmp vars for float32->uint8
unsigned char *u_host_uchar8 = new unsigned char[frame_chunk_size * sizeX * sizeY];
unsigned char *gj_host_uchar8 = new unsigned char[frame_chunk_size * sizeX * sizeY * 3]; //bandymams trink
float *gj_host_float32 = new float[frame_chunk_size * sizeX * sizeY * 3];
// unsigned char gj_host_uchar8[frame_chunk_size * sizeX * sizeY * 3] {};
//
// u
//
const std::string dsName = "u";
std::vector<size_t> shape = {i_frame_max, sizeY, sizeX};
std::vector<size_t> chunks = {i_frame_max / 100 + 1, sizeY, sizeX};
#ifdef WITH_BLOSC
if (debugLevel > 0)
printf("\n ***** defined WITH_BOSC***************\n");
#endif
z5::types::CompressionOptions copts;
copts["codec"] = std::string("blosclz");
// z5::types::defaultCompressionOptions(z5::types::Compressor::blosc, copts, true);
auto u_ds = z5::createDataset(f, dsName, "uint8", shape, chunks, "blosc", copts);
// z5::Metadata fMeta(true);
// z5::filesystem::writeMetadata(f, fMeta);
// auto u_ds = z5::createDataset(f, dsName, "uint8", shape, chunks, "blosc");
// auto u_ds = z5::createDataset(f, dsName, "uint8", shape, chunks, "zlib");
//
// gj
//
const std::string dsName1 = "gj";
std::vector<size_t> shape1 = {i_frame_max, sizeY, sizeX, 3};
std::vector<size_t> chunks1 = {i_frame_max / 100 + 1, sizeY, sizeX, 3};
z5::types::CompressionOptions copts1;
copts1["codec"] = std::string("blosclz");
// z5::types::defaultCompressionOptions(z5::types::Compressor::blosc, copts1, true);
// auto gj_ds = z5::createDataset(f, dsName1, "uint8", shape1, chunks1, "blosc", copts1);
auto gj_ds = z5::createDataset(f, dsName1, "float32", shape1, chunks1, "blosc", copts1);
// auto gj_ds = z5::createDataset(f, dsName1, "uint8", shape1, chunks1, "blosc");
// auto gj_ds = z5::createDataset(f, dsName1, "uint8", shape1, chunks1, "zlib");
// for attribute writing
const auto dsHandle = z5::filesystem::handle::Dataset(f, dsName); // y
// const auto dsHandle1 = z5::filesystem::handle::Dataset(f, dsName1); // gj
nlohmann::json attributesIn;
uint32_t i = 0;
uint32_t i_frame = 0;
auto current_frame_chunk = 0;
t_start = clock();
do // main Loop
{
glfwPollEvents();
if (!paused)
{
mtx.lock();
glfwMakeContextCurrent(window);
glClear(GL_COLOR_BUFFER_BIT);
//Sleep(50);
// printf("\rframe %d, iter %d, simul %.2f ms", i_frame, i, i * .02f);
auto i_frame_in_current_chunk = i_frame % frame_chunk_size;
// printf("\riter %d, iter_in_chunk %d, simul %.2f ms", i, i_frame_in_current_chunk, i * .02f);
for (int a = 0; a < itersInFrame; a++) //i loop
{ // launch compute shaders!
// glMemoryBarrier(GL_ALL_BARRIER_BITS);
glUseProgram(kernel1); //glUseProgram(shIDs.compID1);
glUniform1ui(22, i); //butina, mc36ss, ---nebutina, debug
glUniform1ui(25, i_frame_in_current_chunk); // i_frame_in_current_chunk reikia gj_reg isvedimui
glMemoryBarrier(GL_ALL_BARRIER_BITS);
// glDispatchCompute((GLuint)(sizeX + workGroupSize) / workGroupSize, (GLuint)(sizeY + workGroupSize) / workGroupSize, 1); // + workGroupSize - 1) tam kad butu "divide round up"
glDispatchCompute((GLuint)(sizeX + workGroupSizeX -1) / workGroupSizeX, (GLuint)(sizeY + workGroupSizeY -1) / workGroupSizeY, 1); // SVARBU, bug fixed : .. - 1 nereikia, nes tada uzsiriecia prie periodic boundaries
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glUseProgram(kernel2); //glUseProgram(shIDs.compID2);
glUniform1ui(22, i);
glUniform1ui(25, i_frame_in_current_chunk);// reikia u_reg isvedimui
glMemoryBarrier(GL_ALL_BARRIER_BITS);
// glDispatchCompute((GLuint)(sizeX + workGroupSize) / workGroupSize, (GLuint)(sizeY + workGroupSize) / workGroupSize, 1);
glDispatchCompute((GLuint)(sizeX + workGroupSizeX-1 ) / workGroupSizeX, (GLuint)(sizeY + workGroupSizeY -1) / workGroupSizeY, 1);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
i++;
// // for dbg
// glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_dbg);
// glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, 3*i_frame_max * sizeof(float), ssb_dbg_host);
// glMemoryBarrier(GL_ALL_BARRIER_BITS);
// // for (int a = 0; a < 4; a++) {
// // printf("\n");
// // for (int j = 0; j < 4; j++) {
// // printf(" a%dj%d: %f", a, j, ssb_dbg_host[a*4+j]);
// // /*if ((a + 1) % 36 == 0)
// // printf("\nj=%d/n",j);*/
// // }
// // }
// ofstream myfile;
// myfile.open("trink.txt");
// myfile << "t,vj,gj" << endl;
// for (int i = 0; i < 10000; i++) {
// myfile << ssb_dbg_host[i*3+0] << ',' << ssb_dbg_host[i*3+1] << ',' << ssb_dbg_host[i*3+2] << endl;
// }
// myfile.close();
// cout << "Done.";
// //if (i == 4)
// exit(0);//return 0;
}
if ((i_frame + 1) % frame_chunk_size == 0)
{
printf("\riter %d, iter_in_chunk %d, simul %.2f ms", i, i_frame_in_current_chunk, i * .02f);
fflush(stdout);
// perkelta update kas chunk
glUseProgram(vertFragShadersID); //;glUseProgram(shIDs.frVeID);
//glPointSize(5.f);
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //wireframe mode
glBindVertexArray(VAO);
glUniform1ui(26, 1); //whatToPlot: 1 u, 2 vj, 3 gj
glDrawElements(GL_TRIANGLES, (sizeX - 1) * (sizeY - 1) * 2 * 3, GL_UNSIGNED_INT, 0);
glUniform1ui(26, 2); //whatToPlot: 1 u, 2 vj, 3 gj
glDrawElements(GL_TRIANGLES, (sizeX - 1) * (sizeY - 1) * 2 * 3, GL_UNSIGNED_INT, 0);
glUniform1ui(26, 3); //whatToPlot: 1 u, 2 vj, 3 gj
glDrawElements(GL_TRIANGLES, (sizeX - 1) * (sizeY - 1) * 2 * 3, GL_UNSIGNED_INT, 0);
// Draw line graphs
glUseProgram(line2DvertFragShadersID);
glUniform1ui(25, i_frame_in_current_chunk);
glUniform1ui(26, 1); //whatToPlot: 1 u, 2 gj
// glMemoryBarrier(GL_ALL_BARRIER_BITS);
glBindVertexArray(VAO2);
glDrawArrays(GL_LINE_STRIP, 0, line_points_count);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glUniform1ui(26, 2); //whatToPlot: 1 u, 2 gj
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glDrawArrays(GL_LINE_STRIP, 0, line_points_count);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
// END perkelta update kas chunk
size_t last_iter = current_frame_chunk * frame_chunk_size;
glMemoryBarrier(GL_ALL_BARRIER_BITS);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_u_reg);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, frame_chunk_size * sizeX * sizeY * sizeof(ssb_u_reg_host[0]), ssb_u_reg_host);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo_gj_reg);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, frame_chunk_size * sizeX * sizeY * 3 * sizeof(ssb_gj_reg_host[0]), ssb_gj_reg_host);
glMemoryBarrier(GL_ALL_BARRIER_BITS);
std::async(std::launch::async, [&] {
// write attrs
attributesIn["frames_count"] = last_iter;
attributesIn["dt_reg"] = itersInFrame * par.dt;
z5::writeAttributes(dsHandle, attributesIn);
// z5::writeAttributes(dsHandle1, attributesIn);
//convert float32->uint8 for better compresson
#pragma omp parallel for
for (int a = 0; a < frame_chunk_size * sizeX * sizeY; a++)
u_host_uchar8[a] = (ssb_u_reg_host[a]+85.)/(85.+20.)*255.; //padarom kad butu per visa uchar ilgi -85 iki 20mV -> 0..255
#pragma omp parallel for
for (int a = 0; a < frame_chunk_size * sizeX * sizeY * 3; a++) { // tas pats su gj, tik isskiriam ribas <15,15-30,>30 nS
if (ssb_gj_reg_host[a] > 30.e-9f)
gj_host_uchar8[a] = 255;
else if (ssb_gj_reg_host[a] < 10.e-9f)
gj_host_uchar8[a] = 0;