SpaceQuester

Untitled

Sep 1st, 2021
1,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 56.83 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #define _USE_MATH_DEFINES
  4. #include "math.h"
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <locale.h>
  8. #include <time.h>
  9. #include <stdbool.h>
  10. #include <list>
  11. #include <omp.h>
  12. #include <vector>
  13. #include <complex>
  14. #include <iostream>
  15. #include <valarray>
  16.  
  17. using namespace std;
  18.  
  19. #define Node_count 200 // 200 Default
  20.  
  21. int MaxDeep = 50; // 50 Default
  22.  
  23. #define Equations_per_node 12 // !!! 12 - Don't change !!!
  24. #define Equations_count Node_count * Equations_per_node
  25.  
  26. double* f;
  27. double* f_diff;
  28.  
  29. double** k;
  30. double* phi_k1;
  31. double* phi_k2;
  32. double* phi_k3;
  33.  
  34. bool enable_I_syn_out = false;
  35.  
  36. bool write_file = false;
  37. bool omp_enable = false;
  38.  
  39. double c_0 = 2; // uM
  40. double c_1 = 0.185;
  41. double v_1 = 6; // s^-1
  42. double v_2 = 0.11; // s^-1
  43. double v_3 = 2.2; // uM/s
  44. double* v_4; // uM/s - Controling parameter // 0.5 //double v_4[Node_count]; // uM/s - Controling parameter //0.495
  45. double v_5 = 0.025; // uM/s
  46. double v_6 = 0.2;  // uM/s
  47. double k_1 = 0.5; // s^-1
  48. double k_2 = 1; // uM
  49. double k_3 = 0.1;
  50. double k_4 = 1.1; // uM/s
  51. double a_2 = 0.14; // uM/s
  52. double d_1 = 0.13; // uM
  53. double d_2 = 1.049; // uM
  54. double d_3 = 0.9434; // uM
  55. double d_5 = 0.082; // uM
  56. double alpha = 0.8;
  57. double tau_IP3 = 7.143; // s
  58. double IP3_star = 0.16; // uM
  59. double d_Ca = 0.001; // 0.001
  60. double d_IP3 = 0.2; // 0.12
  61. double alpha_Glu = 2; // 2
  62. double g_astro; // 3
  63.  
  64. // https://neuronaldynamics.epfl.ch/online/Ch2.S2.html
  65. double C_m = 1; // muF/cm^2
  66. double g_K = 35; // mS/cm^2
  67. double g_Na = 40; // mS/cm^2
  68. double g_L = 0.3; // mS/cm^2
  69. double E_K = -77; // mV
  70. double E_Na = 55; // mV
  71. double E_L = -65; // mV
  72.  
  73. double C_m_P = 1; // muF/cm^2
  74. double g_K_P = 35; // mS/cm^2
  75. double g_Na_P = 40; // mS/cm^2
  76. double g_L_P = 0.3; // mS/cm^2
  77. double E_K_P = -77; // mV
  78. double E_Na_P = 55; // mV
  79. double E_L_P = -65; // mV
  80.  
  81. double p_rewir;
  82. double p_inhib;
  83.  
  84. double I_app_min;
  85. double I_app_max;
  86.  
  87. double g_syn;
  88. double k_syn = 0.2;
  89. double* E_syn;
  90.  
  91. double g_syn_P;
  92. double k_syn_P = 0.2;
  93. double* E_syn_P;
  94.  
  95. double alpha_G_P = 25; //s^-1
  96. double beta_G_P = 500; //s^-1
  97.  
  98. double* I_app;
  99. double* I_app_P;
  100.  
  101. double** A_A;
  102. double** B_A;
  103. double* C_A;
  104.  
  105. double** A_N;
  106. double** B_N;
  107. double* C_N;
  108.  
  109. double** A_N_P;
  110. double** B_N_P;
  111. double* C_N_P;
  112.  
  113. list<double>* V_spikes;
  114. list<double>* V_spikes_Freq;
  115.  
  116. FILE* fp_I_syn;
  117.  
  118. double** tau;
  119.  
  120. #define tau_min  2 // ms
  121. #define tau_max 12 // ms
  122.  
  123. #define ms_to_step 40 // (0.001 / dt) !!! Don't forget !!!
  124.  
  125. #define Max_delay tau_max * ms_to_step
  126. double** V_old_array;
  127.  
  128. double Poisson_Freq; // Hz
  129. //const double Min_magintude = -0.20; // muA/cm^2
  130. double Max_magnitude = 2.5; // muA/cm^2 // 0.20
  131. const double Duration = 0.002; // sec
  132.  
  133. double* Meander_start_from_zero;
  134. double* Meander_width;
  135. double* Meander_height;
  136. double* Meander_interval;
  137. double* last_meander_end;
  138.  
  139. typedef std::complex<double> Complex;
  140. typedef std::valarray<Complex> CArray;
  141.  
  142. void fft(CArray& x)
  143. {
  144.     // DFT
  145.     unsigned int N = x.size(), k = N, n;
  146.     double thetaT = 3.14159265358979323846264338328L / N;
  147.     Complex phiT = Complex(cos(thetaT), -sin(thetaT)), T;
  148.     while (k > 1)
  149.     {
  150.         n = k;
  151.         k >>= 1;
  152.         phiT = phiT * phiT;
  153.         T = 1.0L;
  154.         for (unsigned int l = 0; l < k; l++)
  155.         {
  156.             for (unsigned int a = l; a < N; a += n)
  157.             {
  158.                 unsigned int b = a + k;
  159.                 Complex t = x[a] - x[b];
  160.                 x[a] += x[b];
  161.                 x[b] = t * T;
  162.             }
  163.             T *= phiT;
  164.         }
  165.     }
  166.     // Decimate
  167.     unsigned int m = (unsigned int)log2(N);
  168.     for (unsigned int a = 0; a < N; a++)
  169.     {
  170.         unsigned int b = a;
  171.         // Reverse bits
  172.         b = (((b & 0xaaaaaaaa) >> 1) | ((b & 0x55555555) << 1));
  173.         b = (((b & 0xcccccccc) >> 2) | ((b & 0x33333333) << 2));
  174.         b = (((b & 0xf0f0f0f0) >> 4) | ((b & 0x0f0f0f0f) << 4));
  175.         b = (((b & 0xff00ff00) >> 8) | ((b & 0x00ff00ff) << 8));
  176.         b = ((b >> 16) | (b << 16)) >> (32 - m);
  177.         if (b > a)
  178.         {
  179.             Complex t = x[a];
  180.             x[a] = x[b];
  181.             x[b] = t;
  182.         }
  183.     }
  184.     //// Normalize (This section make it not working correctly)
  185.     //Complex f = 1.0 / sqrt(N);
  186.     //for (unsigned int i = 0; i < N; i++)
  187.     //  x[i] *= f;
  188. }
  189.  
  190. struct Interval
  191. {
  192.     double T_start;
  193.     double T_end;
  194. };
  195.  
  196. enum Ca_state
  197. {
  198.     Wait_for_up,
  199.     Wait_for_down
  200. };
  201.  
  202. Ca_state ca_all_state[Node_count];
  203.  
  204. vector<Interval> Ca_all_intervals[Node_count];
  205. vector<Interval> Ca_intervals;
  206.  
  207. //bool thread_count_printed = false;
  208.  
  209. double I_stim(int i, double t)
  210. {
  211.     if (t < Meander_start_from_zero[i])
  212.         return 0;
  213.  
  214.     t -= Meander_start_from_zero[i];
  215.     t = fmod(t, Meander_width[i] + Meander_interval[i]);
  216.  
  217.     return t < Meander_width[i] ? Meander_height[i] : 0;
  218. }
  219.  
  220. double Ca(int i)
  221. {
  222.     return f[i * Equations_per_node];
  223. }
  224.  
  225. void SetCa(int i, double value)
  226. {
  227.     f[i * Equations_per_node] = value;
  228. }
  229.  
  230. double IP3(int i)
  231. {
  232.     return f[i * Equations_per_node + 1];
  233. }
  234.  
  235. void SetIP3(int i, double value)
  236. {
  237.     f[i * Equations_per_node + 1] = value;
  238. }
  239.  
  240. double z(int i)
  241. {
  242.     return f[i * Equations_per_node + 2];
  243. }
  244.  
  245. void Setz(int i, double value)
  246. {
  247.     f[i * Equations_per_node + 2] = value;
  248. }
  249.  
  250. double G_P(int i)
  251. {
  252.     return f[i * Equations_per_node + 3];
  253. }
  254.  
  255. void SetG_P(int i, double value)
  256. {
  257.     f[i * Equations_per_node + 3] = value;
  258. }
  259.  
  260. double V(int i)
  261. {
  262.     return f[i * Equations_per_node + 4];
  263. }
  264.  
  265. void SetV(int i, double value)
  266. {
  267.     f[i * Equations_per_node + 4] = value;
  268. }
  269.  
  270. double m(int i)
  271. {
  272.     return f[i * Equations_per_node + 5];
  273. }
  274.  
  275. void Setm(int i, double value)
  276. {
  277.     f[i * Equations_per_node + 5] = value;
  278. }
  279.  
  280. double n(int i)
  281. {
  282.     return f[i * Equations_per_node + 6];
  283. }
  284.  
  285. void Setn(int i, double value)
  286. {
  287.     f[i * Equations_per_node + 6] = value;
  288. }
  289.  
  290. double h(int i)
  291. {
  292.     return f[i * Equations_per_node + 7];
  293. }
  294.  
  295. void Seth(int i, double value)
  296. {
  297.     f[i * Equations_per_node + 7] = value;
  298. }
  299.  
  300. ///
  301.  
  302. double V_P(int i)
  303. {
  304.     return f[i * Equations_per_node + 8];
  305. }
  306.  
  307. void SetV_P(int i, double value)
  308. {
  309.     f[i * Equations_per_node + 8] = value;
  310. }
  311.  
  312. double m_P(int i)
  313. {
  314.     return f[i * Equations_per_node + 9];
  315. }
  316.  
  317. void Setm_P(int i, double value)
  318. {
  319.     f[i * Equations_per_node + 9] = value;
  320. }
  321.  
  322. double n_P(int i)
  323. {
  324.     return f[i * Equations_per_node + 10];
  325. }
  326.  
  327. void Setn_P(int i, double value)
  328. {
  329.     f[i * Equations_per_node + 10] = value;
  330. }
  331.  
  332. double h_P(int i)
  333. {
  334.     return f[i * Equations_per_node + 11];
  335. }
  336.  
  337. void Seth_P(int i, double value)
  338. {
  339.     f[i * Equations_per_node + 11] = value;
  340. }
  341.  
  342. double V_old(int i, int delay)
  343. {
  344.     return V_old_array[i][Max_delay - 1 - delay];
  345. }
  346.  
  347. int RandomI(int min, int max)
  348. {
  349.     return ((double)rand() / (RAND_MAX - 1)) * (max - min) + min;
  350. }
  351.  
  352. double RandomD(double min, double max)
  353. {
  354.     return ((double)rand() / RAND_MAX) * (max - min) + min;
  355. }
  356.  
  357. double J_channel(double* f, int i)
  358. {
  359.     return c_1 * v_1 * pow(IP3(i), 3) * pow(Ca(i), 3) * pow(z(i), 3) * (c_0 / c_1 - (1 + 1 / c_1) * Ca(i)) / pow((IP3(i) + d_1) * (Ca(i) + d_5), 3);
  360. }
  361.  
  362. double J_PLC(double* f, int i)
  363. {
  364.     return v_4[i] * (Ca(i) + (1 - alpha) * k_4) / (Ca(i) + k_4);
  365. }
  366.  
  367. double J_leak(double* f, int i)
  368. {
  369.     return c_1 * v_2 * (c_0 / c_1 - (1 + 1 / c_1) * Ca(i));
  370. }
  371.  
  372. double J_pump(double* f, int i)
  373. {
  374.     return v_3 * pow(Ca(i), 2) / (pow(k_3, 2) + pow(Ca(i), 2));
  375. }
  376.  
  377. double J_in(double* f, int i)
  378. {
  379.     return v_5 + v_6 * pow(IP3(i), 2) / (pow(k_2, 2) + pow(IP3(i), 2));
  380. }
  381.  
  382. double J_out(double* f, int i)
  383. {
  384.     return k_1 * Ca(i);
  385. }
  386.  
  387. double J_Glu(double* f, int i)
  388. {
  389.     /*double J = 0;
  390.     if (E_syn_P[i] == 0)
  391.     {
  392.         J += alpha_Glu / (1 + exp(-(G_P(i) - 0.25) / 0.01));
  393.     }
  394.     return J;*/
  395.  
  396.     return alpha_Glu / (1 + exp(-(G_P(i) - 0.25) / 0.01));
  397. }
  398.  
  399. double alpha_m(double* f, int i)
  400. {
  401.     return 0.182 * (V(i) + 35) / (1 - exp(-(V(i) + 35) / 9));
  402. }
  403.  
  404. double beta_m(double* f, int i)
  405. {
  406.     return -0.124 * (V(i) + 35) / (1 - exp((V(i) + 35) / 9));
  407. }
  408.  
  409. double alpha_n(double* f, int i)
  410. {
  411.     return 0.02 * (V(i) - 25) / (1 - exp(-(V(i) - 25) / 9));
  412. }
  413.  
  414. double beta_n(double* f, int i)
  415. {
  416.     return -0.002 * (V(i) - 25) / (1 - exp((V(i) - 25) / 9));
  417. }
  418.  
  419. double alpha_h(double* f, int i)
  420. {
  421.     return 0.25 * exp(-(V(i) + 90) / 12);
  422. }
  423.  
  424. double beta_h(double* f, int i)
  425. {
  426.     return 0.25 * exp((V(i) + 62) / 6) / exp((V(i) + 90) / 12);
  427. }
  428.  
  429. //
  430.  
  431. double alpha_m_P(double* f, int i)
  432. {
  433.     return 0.182 * (V_P(i) + 35) / (1 - exp(-(V_P(i) + 35) / 9));
  434. }
  435.  
  436. double beta_m_P(double* f, int i)
  437. {
  438.     return -0.124 * (V_P(i) + 35) / (1 - exp((V_P(i) + 35) / 9));
  439. }
  440.  
  441. double alpha_n_P(double* f, int i)
  442. {
  443.     return 0.02 * (V_P(i) - 25) / (1 - exp(-(V_P(i) - 25) / 9));
  444. }
  445.  
  446. double beta_n_P(double* f, int i)
  447. {
  448.     return -0.002 * (V_P(i) - 25) / (1 - exp((V_P(i) - 25) / 9));
  449. }
  450.  
  451. double alpha_h_P(double* f, int i)
  452. {
  453.     return 0.25 * exp(-(V_P(i) + 90) / 12);
  454. }
  455.  
  456. double beta_h_P(double* f, int i)
  457. {
  458.     return 0.25 * exp((V_P(i) + 62) / 6) / exp((V_P(i) + 90) / 12);
  459. }
  460.  
  461. double FindBurstFreq(double dt, double t_start, double t_end)
  462. {
  463.     int window_length = 0.03 / dt;
  464.  
  465.     int steps_count = (t_end - t_start) / dt;
  466.  
  467.     int pow_of_2 = log2(steps_count);
  468.  
  469.     if (pow_of_2 > 22)
  470.         pow_of_2 = 22;
  471.  
  472.     int fft_N = pow(2, pow_of_2);
  473.  
  474.     Complex* complex = new Complex[fft_N];
  475.     int fft_index = 0;
  476.  
  477.     for (int i = 0; i < Node_count; i++)
  478.     {
  479.         list<double>::iterator it_V_spikes = V_spikes[i].begin();
  480.  
  481.         while (it_V_spikes != V_spikes[i].end())
  482.         {
  483.             if (*it_V_spikes > t_end)
  484.                 break;
  485.  
  486.             if (*it_V_spikes >= t_start)
  487.             {
  488.                 int minStepIndex = ((*it_V_spikes - t_start) - (window_length / 2) * dt) / dt;
  489.                 int maxStepIndex = ((*it_V_spikes - t_start) + (window_length / 2) * dt) / dt;
  490.  
  491.                 if (maxStepIndex > fft_N - 1)
  492.                     maxStepIndex = fft_N - 1;
  493.  
  494.                 for (int j = minStepIndex; j <= maxStepIndex; j++)
  495.                     complex[j] += Complex(1, 0);
  496.             }
  497.  
  498.             it_V_spikes++;
  499.         }
  500.     }
  501.     /*
  502.     for (int j = 0; j < fft_N; j++)
  503.     {
  504.         int spikes_count_all = 0;
  505.  
  506.         double t_center = j * dt + t_start;
  507.         double t_min = t_center - (window_length / 2) * dt;
  508.         double t_max = t_center + (window_length / 2) * dt;
  509.  
  510.         for (int i = 0; i < Node_count; i++)
  511.         {
  512.             int spikes_count = 0;
  513.  
  514.             list<double>::iterator it_V_spikes = V_spikes[i].begin();
  515.  
  516.             if (*it_V_spikes <= t_max)
  517.             {
  518.                 while (it_V_spikes != V_spikes[i].end())
  519.                 {
  520.                     if (*it_V_spikes > t_max)
  521.                         break;
  522.  
  523.                     if (*it_V_spikes >= t_min && *it_V_spikes <= t_max)
  524.                         spikes_count++;
  525.  
  526.                     it_V_spikes++;
  527.                 }
  528.  
  529.                 spikes_count_all += spikes_count;
  530.             }
  531.         }
  532.  
  533.         complex[j] = Complex(spikes_count_all, 0);
  534.  
  535.  
  536.         //fprintf(fp0, "%f\t%d\n", j * dt, spikes_count_all);
  537.     }
  538.     */
  539.     CArray data(complex, fft_N);
  540.     fft(data);
  541.  
  542.     double sampleRate = 1 / dt;
  543.  
  544.     int max_freq_index = -1;
  545.     double max_freq_magnitude = 0;
  546.  
  547.     for (int i = 1; i < fft_N / 2; i++)
  548.     {
  549.         double magnitude = sqrt(data[i].real() * data[i].real() + data[i].imag() * data[i].imag());
  550.  
  551.         if (max_freq_index == -1 || magnitude > max_freq_magnitude)
  552.         {
  553.             max_freq_index = i;
  554.             max_freq_magnitude = magnitude;
  555.         }
  556.  
  557.         //fprintf(fp1, "%f\t%10.0f\n", (double)i * sampleRate / fft_N, magnitude);
  558.     }
  559.  
  560.     return max_freq_index * sampleRate / fft_N;
  561. }
  562.  
  563. double UllahJung_HodgkinHuxley(int i, double* f, double t)
  564. {
  565.     int in = i / Equations_per_node;
  566.     int il = i % Equations_per_node;
  567.  
  568.     switch (il)
  569.     {
  570.     case 0: // Ca
  571.     {
  572.         double sum_1 = 0;
  573.  
  574.         /*for (int j = 0; j < Node_count; j++)
  575.         {
  576.           sum_1 += d_Ca * (Ca(j) - Ca(in));
  577.         }*/
  578.  
  579.         for (int j = 0; j < C_A[in]; j++)
  580.         {
  581.             sum_1 += d_Ca * (Ca((int)B_A[in][j]) - Ca(in));
  582.         }
  583.  
  584.         return J_channel(f, in) - J_pump(f, in) + J_leak(f, in) + J_in(f, in) - J_out(f, in) + sum_1;
  585.     }
  586.  
  587.     case 1: // IP3
  588.     {
  589.         double sum_2 = 0;
  590.  
  591.         /*for (int j = 0; j < Node_count; j++)
  592.         {
  593.         sum_2 += d_IP3 * (IP3(j) - IP3(in));
  594.         }*/
  595.  
  596.         for (int j = 0; j < C_A[in]; j++)
  597.         {
  598.             sum_2 += d_IP3 * (IP3((int)B_A[in][j]) - IP3(in));
  599.         }
  600.  
  601.         return (IP3_star - IP3(in)) / tau_IP3 + J_PLC(f, in) + sum_2 + J_Glu(f, in);
  602.     }
  603.  
  604.     case 2: // z
  605.     {
  606.         return a_2 * (d_2 * (IP3(in) + d_1) / (IP3(in) + d_3) * (1 - z(in)) - Ca(in) * z(in));
  607.     }
  608.  
  609.     case 3: // G_P
  610.     {
  611.         return -alpha_G_P * G_P(in) + beta_G_P * (1 / (1 + exp(-V_P(in) / 0.5)));
  612.     }
  613.  
  614.     case 4: // V
  615.     {
  616.         double I_syn = 0;
  617.         double I_syn_P = 0;
  618.  
  619.         /*for (int j = 0; j < Node_count; j++)
  620.         {
  621.         //sum += A[in][j] * g_syn * (V(in) - V_old(j, tau[in][j]));
  622.         //sum += A[in][j] * g_syn * (V(j) - V(in));
  623.         //sum += A[in][j] * g_syn * (V(in) - E_syn[in]) / (1 + exp(-V_old(j, tau[in][j]) / k_syn));
  624.         //sum += 1 / (0.2 * Node_count_half) * A[in][j] * g_syn * (V(in) - E_syn[in]) / (1 + exp(-V(j) / k_syn)); // i up, j down
  625.         //sum += 1 / (0.2 * Node_count_half) * A[in][j] * g_syn * (V(j) - E_syn[j]) / (1 + exp(-V(in) / k_syn)); // j up, i down
  626.           I_syn += A_N[in][j] * g_syn * (E_syn[in] - V(in)) / (1 + exp(-(V(j) / k_syn)));
  627.         //printf("in = %d\t Node_count = %d\t A[in][j] = %f\t V(in) = %f\t E_syn[in] = %f\t sum = %f\n", in, j, A[in][j], V(in), E_syn[in], sum);
  628.       }*/
  629.  
  630.       /*for (int j = 0; j < C[in]; j++)
  631.       {
  632.       sum += sigma[in][(int)B[in][j]] * (V((int)B[in][j]) - V(in));
  633.       }*/
  634.  
  635.         for (int j = 0; j < C_N[in]; j++)
  636.         {
  637.             if ((1 + g_astro * Ca(in)) > 0)
  638.             {
  639.                 if (Ca(in) >= 0.3)
  640.                 {
  641.                     I_syn += g_syn * (1 + g_astro * Ca(in)) * (V(in) - E_syn[(int)B_N[in][j]]) / (1 + exp(-(V((int)B_N[in][j]) / k_syn))); // версия с V (без V_old)
  642.                 }
  643.                 else
  644.                 {
  645.                     I_syn += g_syn * (V(in) - E_syn[(int)B_N[in][j]]) / (1 + exp(-(V((int)B_N[in][j]) / k_syn))); // версия с V (без V_old)
  646.                 }
  647.             }
  648.             //I_syn += g_syn * (V(in) - E_syn[(int)B_N[in][j]]) / (1 + exp(-(V((int)B_N[in][j]) / k_syn))); // версия с V (без V_old)
  649.             //I_syn += g_syn * (1 + g_astro * Ca(in)) * (E_syn[in] - V(in)) / (1 + exp(-(V_old((int)B_N[in][j], tau[in][(int)B_N[in][j]]) / k_syn)); // версия с V_old
  650.             //I_syn += g_syn * (1 + g_astro * Ca(in)) * (E_syn[in] - V(in)) / (1 + exp(-(V(j) / k_syn))); // версия без с V (без V_old)
  651.             //I_syn += g_syn * (E_syn[in] - V(in)) / (1 + exp(-(V(j) / k_syn))); // версия без с V (без V_old), упрощенная версия
  652.             // sum_3 += g_syn * (1 + g_astro * Ca(in)) * (E_syn[i] - V(i)) / (1 + exp(-(V(j) / k_syn))); // образец из старой версии
  653.             //I_syn += g_syn * (E_syn[in] - V(in)) / (1 + exp(-(V_old((int)B_N[in][j], tau[in][(int)B_N[in][j]])) / k_syn));*/
  654.           //I_syn += g_syn * (E_syn[(int)B_N[in][j]] - V(in)) / (1 + exp(-(V(j) / k_syn))); // версия с V (без V_old)
  655.           //I_syn += g_syn * (E_syn[(int)B_N[in][j]] - V(in)) / (1 + exp(-(V((int)B_N[in][j]) / k_syn))); // версия с V (без V_old), упрощенная версия !!!
  656.           //I_syn += g_syn * (E_syn[(int)B_N[in][j]] - V(in)) / (1 + exp(-(V_old((int)B_N[in][j], tau[in][(int)B_N[in][j]]) / k_syn))); // версия с V (без V_old), упрощенная версия !!!
  657.         //sum += g_syn * (V((int)B[in][j]) - V(in)); // устаревшая часть, нужна для проверки разностной схемы
  658.         //sum += A[in][j] * g_syn * (V(in) - E_syn[in]) / (1 + exp(-V_old((int)B[in][j]) / k_syn)); // устаревшая часть
  659.         //sum += A[in][(int)B[in][j]] * g_syn * (V(in) - E_syn[in]) / (1 + exp(-V_old((int)B[in][j], tau[in][(int)B[in][j]]) / k_syn));
  660.         //sum += A[in][(int)B[in][j]] * g_syn * (V((int)B[in][j]) - E_syn[(int)B[in][j]]) / (1 + exp(-V_old(in, tau[in][(int)B[in][j]]) / k_syn));
  661.         //sum += 1 / (0.2 * Node_count_half) * A[in][(int)B[in][j]] * g_syn * (V((int)B[in][j]) - E_syn[(int)B[in][j]]) / (1 + exp(-V(in) / k_syn)); // j up, i down
  662.         //sum += 1 / (0.2 * Node_count) * /*(int)A_N[in][(int)B_N[in][j]] * */ g_syn * (1 + g_astro * Ca(in)) * (V(in) - E_syn[in]) / (1 + exp(-V((int)B_N[in][j]) / k_syn)); // i up, j down
  663.           // i up, j down
  664.           /*printf("i = %d\t j = %d\t A[i, j] = %d\n", in, (int)B_N[in][j], (int)A_N[in][(int)B_N[in][j]]);*/
  665.         /*printf("i = %d\t V_old = %f\t exp = %f\n", in, Vold, ee);*/
  666.         }
  667.         /*printf("i = %d\t sum = %f\n", in, sum);*/
  668.  
  669.         /*if ((1 + g_astro * Ca(in)) > 0)
  670.         {
  671.             if (Ca(in) >= 0.3)
  672.             {
  673.                 I_syn_P += g_syn_P * (1 + g_astro * Ca(in)) * (V_P(in) - E_syn_P[in]) / (1 + exp(-(V_P(in) / k_syn_P))); // версия с V (без V_old)
  674.             }
  675.             else
  676.             {
  677.                 I_syn_P += g_syn_P * (V_P(in) - E_syn_P[in]) / (1 + exp(-(V_P(in) / k_syn_P))); // версия с V (без V_old)
  678.             }
  679.         }*/
  680.         I_syn_P += g_syn_P * (V_P(in) - E_syn_P[in]) / (1 + exp(-(V_P(in) / k_syn_P))); // версия с V (без V_old)
  681.  
  682.         if (enable_I_syn_out)
  683.             fprintf(fp_I_syn, i == Equations_count - 1 ? "%f" : "%f\t", I_syn);
  684.  
  685.         return 1000 * ((g_Na * pow(m(in), 3) * h(in) * (E_Na - V(in)) + g_K * n(in) * (E_K - V(in)) + g_L * (E_L - V(in)) + I_app[in] + I_syn + I_syn_P) / C_m); // V
  686.     }
  687.  
  688.     case 5: // m
  689.     {
  690.         return 1000 * (alpha_m(f, in) * (1 - m(in)) - beta_m(f, in) * m(in)); // m
  691.     }
  692.  
  693.     case 6: // n
  694.     {
  695.         return 1000 * (alpha_n(f, in) * (1 - n(in)) - beta_n(f, in) * n(in)); // n
  696.     }
  697.  
  698.     case 7: // h
  699.     {
  700.         return 1000 * (alpha_h(f, in) * (1 - h(in)) - beta_h(f, in) * h(in)); // h
  701.     }
  702.  
  703.     case 8: // V_P
  704.     {
  705.         return 1000 * ((g_Na_P * pow(m_P(in), 3) * h_P(in) * (E_Na_P - V_P(in)) + g_K_P * n_P(in) * (E_K_P - V_P(in)) + g_L_P * (E_L_P - V_P(in)) + I_app_P[in] + I_stim(in, t)) / C_m_P); // V_P
  706.     }
  707.  
  708.     case 9: // m_P
  709.     {
  710.         return 1000 * (alpha_m_P(f, in) * (1 - m_P(in)) - beta_m_P(f, in) * m_P(in)); // m_P
  711.     }
  712.  
  713.     case 10: // n_P
  714.     {
  715.         return 1000 * (alpha_n_P(f, in) * (1 - n_P(in)) - beta_n_P(f, in) * n_P(in)); // n_P
  716.     }
  717.  
  718.     case 11: // h_P
  719.     {
  720.         return 1000 * (alpha_h_P(f, in) * (1 - h_P(in)) - beta_h_P(f, in) * h_P(in)); // h_P
  721.     }
  722.     }
  723.  
  724.     return 0;
  725. }
  726.  
  727. void RungeKutta(double t, double dt, double* f, double* f_next)
  728. {
  729.     // k1
  730. #pragma omp parallel for
  731.     for (int i = 0; i < Equations_count; i++)
  732.     {
  733.         //if (!thread_count_printed)
  734.         //{
  735.         //  thread_count_printed = true;
  736.         //  printf("Threads = %d\n", omp_get_num_threads());
  737.         //}
  738.  
  739.         k[i][0] = UllahJung_HodgkinHuxley(i, f, t) * dt;
  740.         phi_k1[i] = f[i] + k[i][0] / 2;
  741.         k[i][1] = UllahJung_HodgkinHuxley(i, phi_k1, t) * dt;
  742.         phi_k2[i] = f[i] + k[i][1] / 2;
  743.         k[i][2] = UllahJung_HodgkinHuxley(i, phi_k2, t) * dt;
  744.         phi_k3[i] = f[i] + k[i][2] / 2;
  745.         k[i][3] = UllahJung_HodgkinHuxley(i, phi_k3, t) * dt;
  746.         f_next[i] = f[i] + (k[i][0] + 2 * k[i][1] + 2 * k[i][2] + k[i][3]) / 6;
  747.     }
  748.  
  749.     //for (int i = 0; i < Equations_count; i++)
  750.     //  phi_k1[i] = f[i] + k[i][0] / 2;
  751.  
  752.     // k2
  753.     //for (int i = 0; i < Equations_count; i++)
  754.     //  k[i][1] = UllahJung_HodgkinHuxley(i, phi_k1, t) * dt;
  755.  
  756.  
  757.     //for (int i = 0; i < Equations_count; i++)
  758.     //  phi_k2[i] = f[i] + k[i][1] / 2;
  759.  
  760.     // k3
  761.     //for (int i = 0; i < Equations_count; i++)
  762.     //  k[i][2] = UllahJung_HodgkinHuxley(i, phi_k2, t) * dt;
  763.  
  764.  
  765.     //for (int i = 0; i < Equations_count; i++)
  766.     //  phi_k3[i] = f[i] + k[i][2] / 2;
  767.  
  768.     //enable_I_syn_out = true;
  769.  
  770.     // k4
  771.     //for (int i = 0; i < Equations_count; i++)
  772.     //  k[i][3] = UllahJung_HodgkinHuxley(i, phi_k3, t) * dt;
  773.  
  774.     //enable_I_syn_out = false;
  775.  
  776.     //for (int i = 0; i < Equations_count; i++)
  777.     //  f_next[i] = f[i] + (k[i][0] + 2 * k[i][1] + 2 * k[i][2] + k[i][3]) / 6;
  778. }
  779.  
  780. void CopyArray(double* source, double* target, int N)
  781. {
  782.     for (int i = 0; i < N; i++)
  783.         target[i] = source[i];
  784. }
  785.  
  786. bool Approximately(double a, double b)
  787. {
  788.     if (a < 0)
  789.         a = -a;
  790.  
  791.     if (b < 0)
  792.         b = -b;
  793.  
  794.     return a - b <= 0.000001;
  795. }
  796.  
  797. //bool CheckSameLine(int i, int j)
  798. //{
  799. //  return i / Node_wire_width == j / Node_wire_width;
  800. //}
  801. //
  802. //bool IsWireNeighbors(int i, int j)
  803. //{
  804. //  if (CheckSameLine(i, j) && (i == j - 1 || i == j + 1))
  805. //      return true;
  806. //
  807. //  if (i == j - Node_wire_width || i == j + Node_wire_width)
  808. //      return true;
  809. //
  810. //  return false;
  811. //}
  812.  
  813. // http://preshing.com/20111007/how-to-generate-random-timings-for-a-poisson-process/
  814. double nextTime(double rateParameter)
  815. {
  816.     return -log(1.0 - (double)rand() / (RAND_MAX)) / rateParameter;
  817. }
  818.  
  819. void GenerateRandomMeander(int i, double min_start_time)
  820. {
  821.     double offset = nextTime(Poisson_Freq);
  822.  
  823.     if (offset < 0)
  824.     {
  825.         int a = 0;
  826.     }
  827.  
  828.     Meander_start_from_zero[i] = min_start_time + offset;
  829.     Meander_width[i] = Duration;
  830.     //Meander_height[i] = RandomD(-Max_magnitude, Max_magnitude);
  831.     Meander_height[i] = RandomD(0, Max_magnitude);
  832.     //Meander_height[i] = Max_magnitude;
  833. }
  834.  
  835. void FillAMatrixZero()
  836. {
  837.     for (int i = 0; i < Node_count; i++)
  838.     {
  839.         for (int j = 0; j < Node_count; j++)
  840.         {
  841.             A_A[i][j] = 0;
  842.             A_N[i][j] = 0;
  843.             A_N_P[i][j] = 0;
  844.         }
  845.     }
  846. }
  847.  
  848. void FillAstrociteMatrix()
  849. {
  850.     for (int i = 0; i < Node_count; i++)
  851.     {
  852.         for (int j = 0; j < Node_count; j++)
  853.         {
  854.             if (i == j)
  855.             {
  856.                 A_A[i][j] = 0;
  857.                 continue;
  858.             }
  859.  
  860.             if (i > j)
  861.             {
  862.                 A_A[i][j] = A_A[j][i];
  863.                 continue;
  864.             }
  865.  
  866.             if (i == 0 && j == Node_count - 1)
  867.             {
  868.                 A_A[i][j] = 1;
  869.                 continue;
  870.             }
  871.  
  872.             if (i == Node_count - 1 && j == 0)
  873.             {
  874.                 A_A[i][j] = 1;
  875.                 continue;
  876.             }
  877.  
  878.             if (i == j - 1 || i == j + 1)
  879.             {
  880.                 A_A[i][j] = 1;
  881.                 continue;
  882.             }
  883.         }
  884.     }
  885.     //A_A[0][1] = 0; // only for debug. diffusion Ca test
  886.     //A_A[1][0] = 0; // only for debug. diffusion Ca test
  887.     //A_A[1][3] = 0;
  888.     //A_A[3][1] = 0;
  889.     //A_A[0][2] = 0;
  890.     //A_A[2][0] = 0;
  891. }
  892.  
  893. void FillBCMatrix_A()
  894. {
  895.     for (int i = 0; i < Node_count; i++)
  896.     {
  897.         int bIndex = 0;
  898.         C_A[i] = 0;
  899.         for (int j = 0; j < Node_count; j++)
  900.         {
  901.             if (A_A[i][j] == 1)
  902.             {
  903.                 B_A[i][bIndex] = j;
  904.                 bIndex++;
  905.                 C_A[i]++;
  906.             }
  907.         }
  908.     }
  909. }
  910.  
  911. void FillBCMatrix_N()
  912. {
  913.     for (int i = 0; i < Node_count; i++)
  914.     {
  915.         int bIndex = 0;
  916.         C_N[i] = 0;
  917.         for (int j = 0; j < Node_count; j++)
  918.         {
  919.             if (A_N[i][j] == 1)
  920.             {
  921.                 B_N[i][bIndex] = j;
  922.                 bIndex++;
  923.                 C_N[i]++;
  924.             }
  925.         }
  926.     }
  927. }
  928.  
  929. void FillBCMatrix_N_P()
  930. {
  931.     for (int i = 0; i < Node_count; i++)
  932.     {
  933.         int bIndex = 0;
  934.         C_N_P[i] = 0;
  935.         for (int j = 0; j < Node_count; j++)
  936.         {
  937.             if (A_N_P[i][j] == 1)
  938.             {
  939.                 B_N_P[i][bIndex] = j;
  940.                 bIndex++;
  941.                 C_N_P[i]++;
  942.             }
  943.         }
  944.     }
  945. }
  946.  
  947. bool IsWireNeighbors(int i, int j, int deep)
  948. {
  949.     int j_border_left = j - deep < 0 ? j + Node_count : j;
  950.     int j_border_right = j + deep >= Node_count ? j - Node_count : j;
  951.  
  952.     if (i == j_border_left - deep || i == j_border_right + deep)
  953.     {
  954.         return true;
  955.     }
  956.  
  957.     return false;
  958. }
  959.  
  960. void FillNeuronMatrix()
  961. {
  962.     for (int i = 0; i < Node_count; i++)
  963.     {
  964.         for (int j = 0; j < Node_count; j++)
  965.         {
  966.             if (i == j)
  967.             {
  968.                 A_N[i][j] = 0;
  969.                 continue;
  970.             }
  971.  
  972.             if (i > j)
  973.             {
  974.                 A_N[i][j] = A_N[j][i];
  975.                 continue;
  976.             }
  977.  
  978.             for (int deep = 1; deep <= MaxDeep; deep++)
  979.             {
  980.                 if (IsWireNeighbors(i, j, deep))
  981.                     A_N[i][j] = 1;
  982.             }
  983.         }
  984.     }
  985. }
  986.  
  987. void RandomizeNeuronMatrix()
  988. {
  989.     srand(time(NULL));
  990.  
  991.     for (int i = 0; i < Node_count; i++)
  992.     {
  993.         //if (i == Node_count / 2)
  994.         //  srand(time(NULL));
  995.  
  996.         for (int link = 0; link < MaxDeep * 2; link++)
  997.         {
  998.             double x = RandomD(0, 1);
  999.  
  1000.             if (x > p_rewir)
  1001.                 continue;
  1002.  
  1003.             int rndJ;
  1004.  
  1005.             do
  1006.             {
  1007.                 rndJ = RandomI(0, Node_count);
  1008.             } while (i == rndJ || A_N[i][rndJ] == 1);
  1009.  
  1010.             int rndJ_last;
  1011.  
  1012.             do
  1013.             {
  1014.                 rndJ_last = RandomI(i - MaxDeep - 1, i + MaxDeep + 1);
  1015.  
  1016.                 if (rndJ_last < 0)
  1017.                     rndJ_last += Node_count;
  1018.                 else if (rndJ_last >= Node_count)
  1019.                     rndJ_last -= Node_count;
  1020.  
  1021.             } while (i == rndJ_last || A_N[i][rndJ_last] == 0);
  1022.  
  1023.             A_N[i][rndJ_last] = 0;
  1024.  
  1025.             A_N[i][rndJ] = 1;
  1026.         }
  1027.     }
  1028. }
  1029.  
  1030. void FillNeuronPoissonMatrix()
  1031. {
  1032.     for (int i = 0; i < Node_count; i++)
  1033.     {
  1034.         for (int j = 0; j < Node_count; j++)
  1035.         {
  1036.             A_N_P[i][j] = 0;
  1037.         }
  1038.     }
  1039. }
  1040.  
  1041. void FillVOldFromCurrent()
  1042. {
  1043.     for (int i = 0; i < Node_count; i++)
  1044.         for (int j = 0; j < Max_delay; j++)
  1045.             V_old_array[i][j] = V(i);
  1046. }
  1047.  
  1048. void UpdateVOld()
  1049. {
  1050.     for (int i = 0; i < Node_count; i++)
  1051.     {
  1052.         for (int j = 1; j < Max_delay; j++)
  1053.             V_old_array[i][j - 1] = V_old_array[i][j];
  1054.  
  1055.         V_old_array[i][Max_delay - 1] = V(i);
  1056.     }
  1057. }
  1058.  
  1059. //void FillFullTauMatrix()
  1060. //{
  1061. //  for (int i = 0; i < Node_count; i++)
  1062. //  {
  1063. //      for (int j = 0; j < Node_count; j++)
  1064. //      {
  1065. //          if (i < Node_count || j < Node_count)
  1066. //          {
  1067. //              tau[i][j] = 0;
  1068. //              continue;
  1069. //          }
  1070. //
  1071. //          int i_neuron = i - Node_count;
  1072. //          int j_neuron = j - Node_count;
  1073. //
  1074. //          int i_wire_x = i_neuron / Node_wire_width;
  1075. //          int i_wire_y = i_neuron % Node_wire_width;
  1076. //
  1077. //          int j_wire_x = j_neuron / Node_wire_width;
  1078. //          int j_wire_y = j_neuron % Node_wire_width;
  1079. //
  1080. //          double distance_max = sqrt(2.) * (Node_wire_width - 1);
  1081. //          double distance = sqrt((i_wire_x - j_wire_x) * (i_wire_x - j_wire_x) + (i_wire_y - j_wire_y) * (i_wire_y - j_wire_y));
  1082. //
  1083. //          tau[i][j] = (tau_min + distance / (distance_max) * (tau_max - tau_min)) * ms_to_step;
  1084. //      }
  1085. //  }
  1086. //}
  1087. //
  1088. //void FillTauMatrix()
  1089. //{
  1090. //  for (int i = 0; i < Node_count; i++)
  1091. //  {
  1092. //      for (int j = 0; j < Node_count; j++)
  1093. //      {
  1094. //          if (i == j || A_N[i][j] == 0)
  1095. //          {
  1096. //              tau[i][j] = 0;
  1097. //              continue;
  1098. //          }
  1099. //
  1100. //          int i_neuron = i;
  1101. //          int j_neuron = j;
  1102. //
  1103. //          int i_wire_x = i_neuron / Node_wire_width;
  1104. //          int i_wire_y = i_neuron % Node_wire_width;
  1105. //
  1106. //          int j_wire_x = j_neuron / Node_wire_width;
  1107. //          int j_wire_y = j_neuron % Node_wire_width;
  1108. //
  1109. //          double distance_max = sqrt(2.) * (Node_wire_width - 1);
  1110. //          double distance = sqrt((i_wire_x - j_wire_x) * (i_wire_x - j_wire_x) + (i_wire_y - j_wire_y) * (i_wire_y - j_wire_y));
  1111. //
  1112. //          double t = (distance - 1) / (distance_max - 1);
  1113. //          tau[i][j] = (tau_min + t * (tau_max - tau_min)) * ms_to_step;
  1114. //      }
  1115. //  }
  1116. //}
  1117.  
  1118. int main(int argc, char* argv[])
  1119. {
  1120.     // run like: UJ_HH_Ring_acc.out 250 0.2 0.4 0.05 3.0 1.05 1.50 // (1) p_rewir (2) g_syn (3) g_astro (4) Max_magnitude (5) g_syn_P (6) Poisson_Freq
  1121.     /*sscanf(argv[1], "%d", &Node_count);
  1122.     FILE* fp_Node_count;
  1123.     fp_Node_count = fopen("Node_count.txt", "w");
  1124.     fprintf(fp_Node_count, "%d\t", Node_count);
  1125.     fclose(fp_Node_count);
  1126.     printf("Node_count = %d\n", Node_count);*/
  1127.  
  1128.     sscanf(argv[1], "%lf", &p_rewir);
  1129.     FILE* fp_p_rewir;
  1130.     fp_p_rewir = fopen("p_rewir.txt", "w");
  1131.     fprintf(fp_p_rewir, "%f\t", p_rewir);
  1132.     fclose(fp_p_rewir);
  1133.     printf("p_rewir = %f\n", p_rewir);
  1134.  
  1135.     /*sscanf(argv[3], "%lf", &p_inhib);
  1136.     FILE* fp_p_inhib;
  1137.     fp_p_inhib = fopen("p_inhib.txt", "w");
  1138.     fprintf(fp_p_inhib, "%f\t", p_inhib);
  1139.     fclose(fp_p_inhib);
  1140.     printf("p_inhib = %f\n", p_inhib);*/
  1141.  
  1142.     sscanf(argv[2], "%lf", &g_syn);
  1143.     FILE* fp_g_syn;
  1144.     fp_g_syn = fopen("g_syn.txt", "w");
  1145.     fprintf(fp_g_syn, "%f\t", g_syn);
  1146.     fclose(fp_g_syn);
  1147.     printf("g_syn = %f\n", g_syn);
  1148.  
  1149.     sscanf(argv[3], "%lf", &g_astro);
  1150.     FILE* fp_g_astro;
  1151.     fp_g_astro = fopen("g_astro.txt", "w");
  1152.     fprintf(fp_g_astro, "%f\t", g_astro);
  1153.     fclose(fp_g_astro);
  1154.     printf("g_astro = %f\n", g_astro);
  1155.  
  1156.     sscanf(argv[4], "%lf", &g_syn_P);
  1157.     FILE* fp_g_syn_P;
  1158.     fp_g_syn_P = fopen("g_syn_P.txt", "w");
  1159.     fprintf(fp_g_syn_P, "%f\t", g_syn_P);
  1160.     fclose(fp_g_syn_P);
  1161.     printf("g_syn_P = %f\n", g_syn_P);
  1162.  
  1163.     sscanf(argv[5], "%lf", &Poisson_Freq);
  1164.     FILE* fp_Poisson_Freq;
  1165.     fp_Poisson_Freq = fopen("Poisson_Freq.txt", "w");
  1166.     fprintf(fp_Poisson_Freq, "%f\t", Poisson_Freq);
  1167.     fclose(fp_Poisson_Freq);
  1168.     printf("Poisson_Freq = %f\n", Poisson_Freq);
  1169.  
  1170.     int write = 0;
  1171.     sscanf(argv[6], "%i", &write);
  1172.     write_file = write;
  1173.     printf("write_file = %i\n", write_file);
  1174.  
  1175.     int omp = 0;
  1176.     sscanf(argv[7], "%i", &omp);
  1177.     omp_enable = omp;
  1178.     printf("omp_enable = %i\n", omp_enable);
  1179.  
  1180.     //sscanf(argv[6], "%lf", &I_app_min);
  1181.     //sscanf(argv[7], "%lf", &I_app_max);
  1182.  
  1183.     for (int i = 0; i < Node_count; i++)
  1184.         ca_all_state[i] = Wait_for_up;
  1185.  
  1186.     f = new double[Equations_count];
  1187.     f_diff = new double[Equations_count];
  1188.     v_4 = new double[Node_count];
  1189.     E_syn = new double[Node_count];
  1190.     I_app = new double[Node_count];
  1191.     E_syn_P = new double[Node_count];
  1192.     I_app_P = new double[Node_count];
  1193.     V_spikes = new list<double>[Node_count];
  1194.     V_spikes_Freq = new list<double>[Node_count];
  1195.  
  1196.     Meander_start_from_zero = new double[Node_count];
  1197.     Meander_width = new double[Node_count];
  1198.     Meander_height = new double[Node_count];
  1199.     Meander_interval = new double[Node_count];
  1200.     last_meander_end = new double[Node_count];
  1201.  
  1202.     tau = new double* [Node_count];
  1203.     for (int i = 0; i < Node_count; i++)
  1204.         tau[i] = new double[Node_count];
  1205.  
  1206.     V_old_array = new double* [Node_count];
  1207.     for (int i = 0; i < Node_count; i++)
  1208.         V_old_array[i] = new double[Max_delay];
  1209.  
  1210.     FILE* fp0;
  1211.     FILE* fp_I_stim = 0;
  1212.     FILE* fp_Ca = 0;
  1213.     FILE* fp_IP3 = 0;
  1214.     //FILE *fp_z;
  1215.     //FILE* fp_G_P;
  1216.     FILE* fp_V = 0;
  1217.     FILE* fp_V_P = 0;
  1218.     //FILE *fp_m;
  1219.     //FILE *fp_n;
  1220.     //FILE *fp_h;
  1221.     FILE* fp_V_spikes = 0;
  1222.     FILE* fp_Esyn;
  1223.     FILE* fp_Esyn_P;
  1224.  
  1225.     //FILE* fp_res;
  1226.     srand(time(NULL));
  1227.  
  1228.     //for (int i = 0; i < Node_count; i++)
  1229.     //  V_old_length[i] = 0;
  1230.  
  1231.     A_A = new double* [Node_count];
  1232.     for (int i = 0; i < Node_count; i++)
  1233.         A_A[i] = new double[Node_count];
  1234.  
  1235.     B_A = new double* [Node_count];
  1236.     for (int i = 0; i < Node_count; i++)
  1237.         B_A[i] = new double[Node_count];
  1238.  
  1239.     C_A = new double[Node_count];
  1240.  
  1241.     A_N = new double* [Node_count];
  1242.     for (int i = 0; i < Node_count; i++)
  1243.         A_N[i] = new double[Node_count];
  1244.  
  1245.     B_N = new double* [Node_count];
  1246.     for (int i = 0; i < Node_count; i++)
  1247.         B_N[i] = new double[Node_count];
  1248.  
  1249.     C_N = new double[Node_count];
  1250.  
  1251.     A_N_P = new double* [Node_count];
  1252.     for (int i = 0; i < Node_count; i++)
  1253.         A_N_P[i] = new double[Node_count];
  1254.  
  1255.     B_N_P = new double* [Node_count];
  1256.     for (int i = 0; i < Node_count; i++)
  1257.         B_N_P[i] = new double[Node_count];
  1258.  
  1259.     C_N_P = new double[Node_count];
  1260.  
  1261.     FillAMatrixZero();
  1262.     FillAstrociteMatrix();
  1263.     FillNeuronMatrix();
  1264.     RandomizeNeuronMatrix();
  1265.     FillNeuronPoissonMatrix();
  1266.     FillBCMatrix_A();
  1267.     FillBCMatrix_N();
  1268.     FillBCMatrix_N_P();
  1269.     //FillTauMatrix();
  1270.  
  1271.     fp0 = fopen("A_A.txt", "w+");
  1272.     for (int i = 0; i < Node_count; i++)
  1273.     {
  1274.         for (int j = 0; j < Node_count; j++)
  1275.         {
  1276.             fprintf(fp0, "%d\t", (int)A_A[i][j]);
  1277.         }
  1278.         fprintf(fp0, "\n");
  1279.     }
  1280.     fclose(fp0);
  1281.  
  1282.     fp0 = fopen("A_N.txt", "w+");
  1283.     for (int i = 0; i < Node_count; i++)
  1284.     {
  1285.         for (int j = 0; j < Node_count; j++)
  1286.         {
  1287.             fprintf(fp0, "%d\t", (int)A_N[i][j]);
  1288.         }
  1289.         fprintf(fp0, "\n");
  1290.     }
  1291.     fclose(fp0);
  1292.  
  1293.     fp0 = fopen("tau.txt", "w+");
  1294.     for (int i = 0; i < Node_count; i++)
  1295.     {
  1296.         for (int j = 0; j < Node_count; j++)
  1297.         {
  1298.             fprintf(fp0, "%f\t", tau[i][j] / ms_to_step);
  1299.         }
  1300.         fprintf(fp0, "\n");
  1301.     }
  1302.     fclose(fp0);
  1303.  
  1304.     // Write to file number of links for each neuron
  1305.     fp0 = fopen("links.txt", "w+");
  1306.     for (int i = 0; i < Node_count; i++)
  1307.     {
  1308.         int links_count = 0;
  1309.         for (int j = 0; j < Node_count; j++)
  1310.         {
  1311.             if (A_N[i][j] == 1)
  1312.             {
  1313.                 links_count++;
  1314.             }
  1315.         }
  1316.         fprintf(fp0, "%d\n", (int)links_count);
  1317.     }
  1318.     fclose(fp0);
  1319.  
  1320.     //setlocale(LC_NUMERIC, "French_Canada.1252");
  1321.     fp0 = fopen("test_Poisson.txt", "w+");
  1322.     for (int i = 0; i < 1000; i++)
  1323.         fprintf(fp0, "%f\n", nextTime(Poisson_Freq));
  1324.     fclose(fp0);
  1325.  
  1326.     fp0 = fopen("B_A.txt", "w+");
  1327.     for (int i = 0; i < Node_count; i++)
  1328.     {
  1329.         for (int j = 0; j < C_A[i]; j++)
  1330.         {
  1331.             fprintf(fp0, "%d\t", (int)B_A[i][j]);
  1332.         }
  1333.         fprintf(fp0, "\n");
  1334.     }
  1335.     fclose(fp0);
  1336.  
  1337.     fp0 = fopen("B_N.txt", "w+");
  1338.     for (int i = 0; i < Node_count; i++)
  1339.     {
  1340.         for (int j = 0; j < C_N[i]; j++)
  1341.         {
  1342.             fprintf(fp0, "%d\t", (int)B_N[i][j]);
  1343.         }
  1344.         fprintf(fp0, "\n");
  1345.     }
  1346.     fclose(fp0);
  1347.  
  1348.     fp0 = fopen("C_A.txt", "w+");
  1349.     for (int i = 0; i < Node_count; i++)
  1350.     {
  1351.         fprintf(fp0, "%d\n", (int)C_A[i]);
  1352.     }
  1353.     fclose(fp0);
  1354.  
  1355.     fp0 = fopen("C_N.txt", "w+");
  1356.     for (int i = 0; i < Node_count; i++)
  1357.     {
  1358.         fprintf(fp0, "%d\n", (int)C_N[i]);
  1359.     }
  1360.     fclose(fp0);
  1361.  
  1362.     /*for (int i = 0; i < 6; i++)
  1363.     {
  1364.         v_4[i] = 0.6;
  1365.     }*/
  1366.     for (int i = 0; i < Node_count; i++)
  1367.     {
  1368.         v_4[i] = 0.4; // 0.4
  1369.     }
  1370.  
  1371.     // Initial values
  1372.     /*for (int i = 0; i < Equations_count; i++)
  1373.     {
  1374.       f[i] = 0;
  1375.   }*/
  1376.  
  1377.   //I_app_min = 1.1;
  1378.   //I_app_max = 1.5;
  1379.  
  1380.     FILE* fp_I_app;
  1381.     fp_I_app = fopen("I_app.txt", "w");
  1382.     for (int i = 0; i < Node_count; i++)
  1383.     {
  1384.         I_app[i] = 0.7; //RandomD(I_app_min, I_app_max);
  1385.         fprintf(fp_I_app, "%f\n", I_app[i]);
  1386.     }
  1387.     fclose(fp_I_app);
  1388.  
  1389.     FILE* fp_I_app_P;
  1390.     fp_I_app_P = fopen("I_app_P.txt", "w");
  1391.     for (int i = 0; i < Node_count; i++)
  1392.     {
  1393.         I_app_P[i] = 0.7;
  1394.         fprintf(fp_I_app_P, "%f\n", I_app_P[i]);
  1395.     }
  1396.     fclose(fp_I_app_P);
  1397.  
  1398.     for (int i = 0; i < Node_count; i++) // init array for all nodes
  1399.     {
  1400.         SetG_P(i, 0); // G_P
  1401.     }
  1402.  
  1403.     double percent_stable_state = 0.50; // 0.40
  1404.     double eps_persent = 0.05; //0.05
  1405.  
  1406.     double Ca0 = 0.07;
  1407.     double IP30 = 0.16;
  1408.     double z0 = 0.67;
  1409.  
  1410.     for (int i = 0; i < Node_count; i++)
  1411.     {
  1412.         /*SetCa(i, Ca0 + RandomD(-Ca0 * eps_persent, Ca0 * eps_persent)); // Ca
  1413.         SetIP3(i, IP30 + RandomD(-IP30 * eps_persent, IP30 * eps_persent)); // IP3
  1414.         Setz(i, z0 + RandomD(-z0 * eps_persent, z0 * eps_persent)); // z */
  1415.         SetCa(i, Ca0); // Ca
  1416.         SetIP3(i, IP30); // IP3
  1417.         Setz(i, z0); // z
  1418.     }
  1419.  
  1420.     double V0 = -58.7085;
  1421.     double m0 = 0.0953;
  1422.     double n0 = 0.000913;
  1423.     double h0 = 0.3662;
  1424.  
  1425.     double V1 = 14.8409;
  1426.     double m1 = 0.9174;
  1427.     double n1 = 0.0140;
  1428.     double h1 = 0.0539;
  1429.  
  1430.     /*for (int i = 0; i < Node_count; i++) // init only for neurons
  1431.     {
  1432.       double random = RandomD(0, 1);
  1433.  
  1434.       SetV(i, random < percent_stable_state ? V0 + RandomD(-V0 * eps_persent, V0 * eps_persent) : V1 + RandomD(-V1 * eps_persent, V1 * eps_persent)); // V
  1435.       Setm(i, random < percent_stable_state ? m0 + RandomD(-m0 * eps_persent, m0 * eps_persent) : m1 + RandomD(-m1 * eps_persent, m1 * eps_persent)); // m
  1436.       Setn(i, random < percent_stable_state ? n0 + RandomD(-n0 * eps_persent, n0 * eps_persent) : n1 + RandomD(-n1 * eps_persent, n1 * eps_persent)); // n
  1437.       Seth(i, random < percent_stable_state ? h0 + RandomD(-h0 * eps_persent, h0 * eps_persent) : h1 + RandomD(-h1 * eps_persent, h1 * eps_persent)); // h
  1438.     }*/
  1439.  
  1440.     /*for (int i = 0; i < Node_count; i++) // init only for neurons
  1441.     {*/
  1442.         //double random = RandomD(0, 1);
  1443.  
  1444.         /*SetV(i, random < percent_stable_state ? V0 : V1); // V
  1445.         Setm(i, random < percent_stable_state ? m0 : m1); // m
  1446.         Setn(i, random < percent_stable_state ? n0 : n1); // n
  1447.         Seth(i, random < percent_stable_state ? h0 : h1); // h*/
  1448.  
  1449.         //SetV(i, V0); // V
  1450.         //Setm(i, m0); // m
  1451.         //Setn(i, n0); // n
  1452.         //Seth(i, h0); // h
  1453.  
  1454.         /*SetV_P(i, random < percent_stable_state ? V0 : V1); // V
  1455.         Setm_P(i, random < percent_stable_state ? m0 : m1); // m
  1456.         Setn_P(i, random < percent_stable_state ? n0 : n1); // n
  1457.         Seth_P(i, random < percent_stable_state ? h0 : h1); // h*/
  1458.  
  1459.         //SetV_P(i, V0); // V
  1460.         //Setm_P(i, m0); // m
  1461.         //Setn_P(i, n0); // n
  1462.         //Seth_P(i, h0); // h
  1463.     //}
  1464.  
  1465.     for (int i = 0; i < Node_count; i++) // init only for neurons
  1466.     {
  1467.         SetV(i, RandomD(-80, 20)); // V
  1468.         Setm(i, RandomD(0, 1)); // m
  1469.         Setn(i, RandomD(0, 1)); // n
  1470.         Seth(i, RandomD(0, 1)); // h
  1471.         SetV_P(i, RandomD(-80, 20)); // V
  1472.         Setm_P(i, RandomD(0, 1)); // m
  1473.         Setn_P(i, RandomD(0, 1)); // n
  1474.         Seth_P(i, RandomD(0, 1)); // h
  1475.         //SetV(i, V1); // V
  1476.         //Setm(i, m1); // m
  1477.         //Setn(i, n1); // n
  1478.         //Seth(i, h1); // h
  1479.     }
  1480.  
  1481.     double E_syn0 = 0; // Excitatory neuron
  1482.     double E_syn1 = -90; // Inhibitory neuron
  1483.  
  1484.     fp_Esyn = fopen("results_E_syn.txt", "w+");
  1485.     for (int i = 0; i < Node_count; i++)
  1486.     {
  1487.         /*E_syn[i] = E_syn0;
  1488.  
  1489.         double x = RandomD(0, 1);
  1490.  
  1491.         if (x > p_inhib)
  1492.         {
  1493.             fprintf(fp_Esyn, "%f\n", E_syn[i]);
  1494.             continue;
  1495.         }*/
  1496.  
  1497.         E_syn[i] = E_syn1;
  1498.  
  1499.         fprintf(fp_Esyn, "%f\n", E_syn[i]);
  1500.  
  1501.         E_syn_P[i] = E_syn0;
  1502.     }
  1503.     fclose(fp_Esyn);
  1504.  
  1505.     for (int i = 0; i < Node_count; i++)
  1506.     {
  1507.         GenerateRandomMeander(i, 0);
  1508.         last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  1509.     }
  1510.  
  1511.     const double t_start = 0;
  1512.     const double t_max = 10; // 100 msec = 0.1 sec // 240 // 270
  1513.     //const double dt = 0.000002; // 0.01 msec = 0.00001 sec; 0.1 msec = 0.0001 sec; 1 msec = 0.001 sec // 0.000025
  1514.     const double dt = 0.00002; // 0.01 msec = 0.00001 sec; 0.1 msec = 0.0001 sec; 1 msec = 0.001 sec // 0.000025
  1515.  
  1516.     double t = t_start;
  1517.  
  1518.     //fp_res = fopen("matlab_res.txt", "a+");
  1519.     //fprintf(fp_res, "%f\t%f\t", Max_magintude, g_syn);
  1520.  
  1521.     //fp0 = fopen("results.txt", "w+");
  1522.     //setlocale(LC_NUMERIC, "French_Canada.1252");
  1523.  
  1524.     double start_rk4, end_rk4;
  1525.     clock_t omp_start_rk4, omp_end_rk4;
  1526.  
  1527.     if (omp_enable)
  1528.         omp_start_rk4 = omp_get_wtime();
  1529.     else
  1530.         start_rk4 = clock();
  1531.  
  1532.     int lastPercent = -1;
  1533.  
  1534.     //FillVOldFromCurrent();
  1535.  
  1536.     k = new double* [Equations_count];
  1537.     for (int i = 0; i < Equations_count; i++)
  1538.         k[i] = new double[4];
  1539.  
  1540.     phi_k1 = new double[Equations_count];
  1541.     phi_k2 = new double[Equations_count];
  1542.     phi_k3 = new double[Equations_count];
  1543.  
  1544.     if (write_file)
  1545.     {
  1546.         fp_I_stim = fopen("results_I_stim.txt", "w+");
  1547.         //fp_I_syn = fopen("results_I_syn.txt", "w+");
  1548.         fp_Ca = fopen("results_Ca.txt", "w+");
  1549.         //fp_IP3 = fopen("results_IP3.txt", "w+");
  1550.         //fp_z    = fopen("results_z.txt", "w+");
  1551.         //fp_G_P = fopen("results_G_P.txt", "w+");
  1552.         fp_V = fopen("results_V.txt", "w+");
  1553.         fp_V_P = fopen("results_V_P.txt", "w+");
  1554.         //fp_m = fopen("results_m.txt", "w+");
  1555.         //fp_n = fopen("results_n.txt", "w+");
  1556.         //fp_h = fopen("results_h.txt", "w+");
  1557.         fp_V_spikes = fopen("results_V_spikes.txt", "w+");
  1558.     }
  1559.  
  1560.     double* f_next = new double[Equations_count];
  1561.  
  1562.     while (t < t_max || Approximately(t, t_max))
  1563.     {
  1564.         if (write_file)
  1565.         {
  1566.             fprintf(fp_I_stim, "%f\t", t);
  1567.             fprintf(fp_Ca, "%f\t", t);
  1568.             //fprintf(fp_IP3, "%f\t", t);
  1569.             //fprintf(fp_z, "%f\t", t);
  1570.             //fprintf(fp_G, "%f\t", t);
  1571.             fprintf(fp_V, "%f\t", t);
  1572.             fprintf(fp_V_P, "%f\t", t);
  1573.             //fprintf(fp_m, "%f\t", t);
  1574.             //fprintf(fp_n, "%f\t", t);
  1575.             //fprintf(fp_h, "%f\t", t);
  1576.             //fprintf(fp_V_spikes, "%f\t", t);
  1577.         }
  1578.  
  1579.         for (int i = 0; i < Node_count; i++)
  1580.         {
  1581.             if (t > last_meander_end[i])
  1582.             {
  1583.                 GenerateRandomMeander(i, t);
  1584.                 last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  1585.             }
  1586.  
  1587.             if (write_file)
  1588.                 fprintf(fp_I_stim, "%f\t", I_stim(i, t));
  1589.         }
  1590.  
  1591.         if (write_file)
  1592.         {
  1593.             fprintf(fp_I_stim, "\n");
  1594.  
  1595.             for (int i = 0; i < Equations_count; i += Equations_per_node)
  1596.                 fprintf(fp_Ca, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // Ca
  1597.  
  1598.             //for (int i = 1; i < Equations_count; i += Equations_per_node)
  1599.             //  fprintf(fp_IP3, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // IP3
  1600.  
  1601.             //for (int i = 2; i < Equations_count; i += Equations_per_node)
  1602.             //  fprintf(fp_z, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // z
  1603.  
  1604.             //for (int i = 3; i < Equations_count; i += Equations_per_node)
  1605.             //  fprintf(fp_G_P, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // G
  1606.  
  1607.             for (int i = 4; i < Equations_count; i += Equations_per_node)
  1608.             {
  1609.                 if (isnan(f[i]))
  1610.                     return 1;
  1611.  
  1612.                 fprintf(fp_V, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // V
  1613.             }
  1614.  
  1615.             //for (int i = 5; i < Equations_count; i += Equations_per_node)
  1616.             //  fprintf(fp_m, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // m
  1617.  
  1618.             //for (int i = 6; i < Equations_count; i += Equations_per_node)
  1619.             //  fprintf(fp_n, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // n
  1620.  
  1621.             //for (int i = 7; i < Equations_count; i += Equations_per_node)
  1622.             //  fprintf(fp_h, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // h
  1623.  
  1624.             for (int i = 8; i < Equations_count; i += Equations_per_node)
  1625.                 fprintf(fp_V_P, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // V_P
  1626.  
  1627.             //for (int i = 9; i < Equations_count; i += Equations_per_node)
  1628.             //  fprintf(fp_m_P, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // m_P
  1629.  
  1630.             //for (int i = 10; i < Equations_count; i += Equations_per_node)
  1631.             //  fprintf(fp_n_P, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // n_P
  1632.  
  1633.             //for (int i = 11; i < Equations_count; i += Equations_per_node)
  1634.             //  fprintf(fp_h_P, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // h_P
  1635.  
  1636.             fprintf(fp_Ca, "\n");
  1637.             //fprintf(fp_IP3, "\n");
  1638.             //fprintf(fp_z, "\n");
  1639.             //fprintf(fp_G, "\n");
  1640.             fprintf(fp_V, "\n");
  1641.             //fprintf(fp_m, "\n");
  1642.             //fprintf(fp_n, "\n");
  1643.             //fprintf(fp_h, "\n");
  1644.  
  1645.             //fprintf(fp_G_P, "\n");
  1646.             fprintf(fp_V_P, "\n");
  1647.             //fprintf(fp_m_P, "\n");
  1648.             //fprintf(fp_n_P, "\n");
  1649.             //fprintf(fp_h_P, "\n");
  1650.         }
  1651.  
  1652.         RungeKutta(t, dt, f, f_next);
  1653.  
  1654. #pragma omp parallel for
  1655.         for (int i = 0; i < Node_count; i++)
  1656.         {
  1657.             int index = Equations_per_node * i + 4;
  1658.             double diff = f_next[index] - f[index];
  1659.  
  1660.             //fprintf(fp_V_spikes, i == Equations_count - 1 ? "%d" : "%d\t", diff < 0 && f_diff[index] > 0 && f[index] > -10 && (V_spikes[i].size() == 0 || t - V_spikes[i].back() > 0.001) ? 1 : 0);
  1661.  
  1662.             if (diff < 0 && f_diff[index] > 0 && f[index] > -10 && (V_spikes[i].size() == 0 || t - V_spikes[i].back() > 0.001))
  1663.             {
  1664.                 V_spikes[i].push_back(t);
  1665.             }
  1666.  
  1667.             f_diff[index] = diff;
  1668.         }
  1669.  
  1670.         //fprintf(fp_V_spikes, "\n");
  1671.  
  1672.         CopyArray(f_next, f, Equations_count);
  1673.  
  1674.         //printf("V(24) = %f\t V_old(24) = %f\n", f[24*4], V_old(24));
  1675.         //UpdateVOld();
  1676.  
  1677.         //fprintf(fp_I_syn, "\n");
  1678.  
  1679.         for (int i = 0; i < Node_count; i++)
  1680.         {
  1681.             if (ca_all_state[i] == Wait_for_up)
  1682.             {
  1683.                 if (Ca(i) > 0.3)
  1684.                 {
  1685.                     ca_all_state[i] = Wait_for_down;
  1686.                     Interval interval;
  1687.                     interval.T_start = t;
  1688.                     interval.T_end = -1;
  1689.                     Ca_all_intervals[i].push_back(interval);
  1690.                 }
  1691.             }
  1692.             else if (ca_all_state[i] == Wait_for_down)
  1693.             {
  1694.                 if (Ca(i) < 0.3)
  1695.                 {
  1696.                     Ca_all_intervals[i].back().T_end = t;
  1697.                     ca_all_state[i] = Wait_for_up;
  1698.                 }
  1699.             }
  1700.         }
  1701.  
  1702.         t += dt;
  1703.  
  1704.         int percent = (int)(100 * (t - t_start) / (t_max - t_start));
  1705.         if (percent != lastPercent)
  1706.         {
  1707.             printf("Progress: %d%%\n", percent);
  1708.             lastPercent = percent;
  1709.         }
  1710.     }
  1711.  
  1712.     delete[] f_next;
  1713.  
  1714.     double* V_mean_freqs = new double[Node_count];
  1715.     double* V_STD_freqs = new double[Node_count];
  1716.     //list<double> V_mean_freqs;
  1717.  
  1718.     int max_interval_count = 0;
  1719.  
  1720.     for (int i = 0; i < Node_count; i++)
  1721.     {
  1722.         if (Ca_all_intervals[i].size() > max_interval_count)
  1723.         {
  1724.             max_interval_count = Ca_all_intervals[i].size();
  1725.         }
  1726.     }
  1727.  
  1728.     for (int i = 0; i < max_interval_count; i++)
  1729.     {
  1730.         Interval mean_interval;
  1731.         mean_interval.T_start = 0;
  1732.         mean_interval.T_end = 0;
  1733.  
  1734.         int mean_count = 0;
  1735.  
  1736.         for (int j = 0; j < Node_count; j++)
  1737.         {
  1738.             if (Ca_all_intervals[j].size() > i && Ca_all_intervals[j][i].T_end > 0)
  1739.             {
  1740.                 mean_interval.T_start += Ca_all_intervals[j][i].T_start;
  1741.                 mean_interval.T_end += Ca_all_intervals[j][i].T_end;
  1742.  
  1743.                 mean_count++;
  1744.             }
  1745.         }
  1746.  
  1747.         mean_interval.T_start /= mean_count;
  1748.         mean_interval.T_end /= mean_count;
  1749.  
  1750.         Ca_intervals.push_back(mean_interval);
  1751.     }
  1752.  
  1753. #pragma omp parallel for
  1754.     for (int i = 0; i < Node_count; i++)
  1755.     {
  1756.         list<double>::iterator it_V_spikes = V_spikes[i].begin();
  1757.  
  1758.         while (it_V_spikes != V_spikes[i].end() && *it_V_spikes < 0.25 * t_max)
  1759.         {
  1760.             V_spikes[i].pop_front();
  1761.             it_V_spikes = V_spikes[i].begin();
  1762.         }
  1763.  
  1764.         list<double> V_freqs;
  1765.  
  1766.         it_V_spikes = V_spikes[i].begin();
  1767.  
  1768.         V_mean_freqs[i] = 0;
  1769.         V_STD_freqs[i] = 0;
  1770.  
  1771.         if (V_spikes[i].size() <= 1)
  1772.         {
  1773.             continue;
  1774.         }
  1775.         else
  1776.         {
  1777.             for (int j = 1; j < V_spikes[i].size(); j++)
  1778.             {
  1779.                 double first = *it_V_spikes;
  1780.                 advance(it_V_spikes, 1);
  1781.                 double next = *it_V_spikes;
  1782.  
  1783.                 double T = next - first;
  1784.                 V_freqs.push_back(1 / T);
  1785.             }
  1786.         }
  1787.  
  1788.         list<double>::iterator it_Freq = V_freqs.begin();
  1789.  
  1790.         for (int j = 0; j < V_freqs.size(); j++)
  1791.         {
  1792.             V_mean_freqs[i] += *it_Freq;
  1793.             advance(it_Freq, 1);
  1794.         }
  1795.  
  1796.         V_mean_freqs[i] /= V_freqs.size();
  1797.  
  1798.         it_Freq = V_freqs.begin();
  1799.  
  1800.         for (int j = 0; j < V_freqs.size(); j++)
  1801.         {
  1802.             V_STD_freqs[i] += pow(*it_Freq - V_mean_freqs[i], 2);
  1803.             advance(it_Freq, 1);
  1804.         }
  1805.  
  1806.         V_STD_freqs[i] /= V_freqs.size();
  1807.         V_STD_freqs[i] = sqrt(V_STD_freqs[i]);
  1808.     }
  1809.  
  1810.     fp0 = fopen("V_STD_freqs.txt", "w+");
  1811.     for (int i = 0; i < Node_count; i++)
  1812.     {
  1813.         fprintf(fp0, "%f\t", V_STD_freqs[i]);
  1814.     }
  1815.     fclose(fp0);
  1816.  
  1817.     double V_STD_mean_Freq = 0;
  1818.     double V_STD_mean_Freq_not_zero = 0;
  1819.     double V_STD_mean_Freq_not_zero_count = 0;
  1820.  
  1821.     for (int j = 0; j < Node_count; j++)
  1822.     {
  1823.         if (V_STD_freqs[j] != 0)
  1824.         {
  1825.             V_STD_mean_Freq_not_zero_count++;
  1826.             V_STD_mean_Freq += V_STD_freqs[j];
  1827.         }
  1828.     }
  1829.  
  1830.     V_STD_mean_Freq_not_zero = V_STD_mean_Freq / V_STD_mean_Freq_not_zero_count;
  1831.     V_STD_mean_Freq /= Node_count;
  1832.  
  1833.     fp0 = fopen("V_STD_mean_Freq.txt", "w+");
  1834.     fprintf(fp0, "%f\n", V_STD_mean_Freq);
  1835.     //fprintf(fp_res, "%f\t", V_STD_mean_Freq);
  1836.     fclose(fp0);
  1837.  
  1838.     fp0 = fopen("V_STD_mean_Freq_not_zero.txt", "w+");
  1839.     fprintf(fp0, "%f\n", V_STD_mean_Freq_not_zero);
  1840.     //fprintf(fp_res, "%f\t", V_STD_mean_Freq_not_zero);
  1841.     fclose(fp0);
  1842.  
  1843.     double V_mean_mean_Freq = 0;
  1844.     double V_mean_mean_Freq_not_zero = 0;
  1845.     double V_mean_mean_Freq_not_zero_count = 0;
  1846.  
  1847.     for (int j = 0; j < Node_count; j++)
  1848.     {
  1849.         if (V_mean_freqs[j] != 0)
  1850.         {
  1851.             V_mean_mean_Freq_not_zero_count++;
  1852.             V_mean_mean_Freq += V_mean_freqs[j];
  1853.         }
  1854.     }
  1855.  
  1856.     delete[] V_mean_freqs;
  1857.     delete[] V_STD_freqs;
  1858.  
  1859.     V_mean_mean_Freq_not_zero = V_mean_mean_Freq / V_mean_mean_Freq_not_zero_count;
  1860.     V_mean_mean_Freq /= Node_count;
  1861.  
  1862.     fp0 = fopen("V_mean_mean_Freq.txt", "w+");
  1863.     fprintf(fp0, "%f\n", V_mean_mean_Freq);
  1864.     fclose(fp0);
  1865.  
  1866.     fp0 = fopen("V_mean_mean_Freq_not_zero.txt", "w+");
  1867.     fprintf(fp0, "%f\n", V_mean_mean_Freq_not_zero);
  1868.     fclose(fp0);
  1869.  
  1870.     // CHUNKS
  1871.     double chunk_t_start = 0.25 * t_max;
  1872.     double chunk_t_step = 0.5;
  1873.     int chunk_step_count = (0.75 * t_max / chunk_t_step);
  1874.  
  1875.     double dt_chunk = 0.1 / V_mean_mean_Freq_not_zero; // 0.25
  1876.     int chunks_count = chunk_t_step / dt_chunk;
  1877.  
  1878.     double corr_aver_mean = 0, corr_aver_mean_not_zero = 0;
  1879.  
  1880.     FILE* fp_corr_not_zero = fopen("corr_not_zero.txt", "w+");
  1881.     FILE* fp_V_spikes_chunks = fopen("V_spikes_chunks.txt", "w+");
  1882.  
  1883.     vector<double> k_syn;
  1884.     vector<double> k_syn_time;
  1885.  
  1886.     for (int s = 0; s < chunk_step_count; s++)
  1887.     {
  1888.         if (chunks_count > 0)
  1889.         {
  1890.             int** V_spikes_chunks = new int* [Node_count];
  1891.  
  1892. #pragma omp parallel for
  1893.             for (int i = 0; i < Node_count; i++)
  1894.             {
  1895.                 V_spikes_chunks[i] = new int[chunks_count];
  1896.  
  1897.                 if (V_spikes[i].size() == 0)
  1898.                     for (int ch = 0; ch < chunks_count; ch++)
  1899.                         V_spikes_chunks[i][ch] = 0;
  1900.  
  1901.                 double ch_start = chunk_t_start + chunk_t_step * s;
  1902.                 double ch_end = ch_start + dt_chunk;
  1903.                 list<double>::iterator currentSpike = V_spikes[i].begin();
  1904.  
  1905.                 for (int ch = 0; ch < chunks_count; ch++)
  1906.                 {
  1907.                     while (currentSpike != V_spikes[i].end() && *currentSpike < ch_start)
  1908.                         currentSpike++;
  1909.  
  1910.                     if (currentSpike == V_spikes[i].end())
  1911.                         V_spikes_chunks[i][ch] = 0;
  1912.                     else
  1913.                         V_spikes_chunks[i][ch] = *currentSpike >= ch_start && *currentSpike <= ch_end;
  1914.  
  1915.                     ch_start += dt_chunk;
  1916.                     ch_end += dt_chunk;
  1917.                 }
  1918.             }
  1919.  
  1920.             char buffer[50];
  1921.             //sprintf(buffer, "V_spikes_chunks_%d.txt", s);
  1922.             //fp0 = fopen(buffer, "w+");
  1923.  
  1924.             for (int ch = 0; ch < chunks_count; ch++)
  1925.             {
  1926.                 for (int i = 0; i < Node_count; i++)
  1927.                     fprintf(fp_V_spikes_chunks, "%d\t", V_spikes_chunks[i][ch]);
  1928.  
  1929.                 fprintf(fp_V_spikes_chunks, "\n");
  1930.             }
  1931.  
  1932.             //fclose(fp0);
  1933.  
  1934.             double corr = 0;
  1935.             double corr_not_zero = 0;
  1936.             double counter = 0;
  1937.             double counter_not_zero = 0;
  1938.  
  1939.             for (int i = 0; i < Node_count; i++)
  1940.             {
  1941.                 for (int j = 0; j < Node_count; j++)
  1942.                 {
  1943.                     if (i == j)
  1944.                         continue;
  1945.  
  1946.                     int k1 = 0;
  1947.                     int k2 = 0;
  1948.                     int k3 = 0;
  1949.  
  1950.                     for (int l = 0; l < chunks_count; l++)
  1951.                     {
  1952.                         if (V_spikes_chunks[i][l] == 1 && V_spikes_chunks[j][l] == 1)
  1953.                             k1++;
  1954.  
  1955.                         k2 += V_spikes_chunks[i][l];
  1956.                         k3 += V_spikes_chunks[j][l];
  1957.                     }
  1958.  
  1959.                     if (k2 != 0 && k3 != 0)
  1960.                     {
  1961.                         corr += (double)k1 / sqrt((double)k2 * (double)k3);
  1962.                         counter_not_zero++;
  1963.                     }
  1964.  
  1965.                     counter++;
  1966.                 }
  1967.             }
  1968.  
  1969.             double corr_aver = corr / counter;
  1970.  
  1971.             double corr_aver_not_zero = corr;
  1972.  
  1973.             if (counter_not_zero != 0)
  1974.                 corr_aver_not_zero /= counter_not_zero;
  1975.  
  1976.             corr_aver_mean += corr_aver;
  1977.             corr_aver_mean_not_zero += corr_aver_not_zero;
  1978.  
  1979.             //sprintf(buffer, "corr_aver_%d.txt", s);
  1980.  
  1981.             //fp0 = fopen(buffer, "w+");
  1982.             //fprintf(fp0, "%f\n", corr_aver);
  1983.             //fclose(fp0);
  1984.  
  1985.             double ch_start = chunk_t_start + chunk_t_step * s;
  1986.             fprintf(fp_corr_not_zero, "%f\t%f\n", ch_start, corr_aver_not_zero);
  1987.  
  1988.             for (int i = 0; i < Node_count; i++)
  1989.                 delete[] V_spikes_chunks[i];
  1990.  
  1991.             delete[] V_spikes_chunks;
  1992.  
  1993.             k_syn.push_back(corr_aver_not_zero);
  1994.             k_syn_time.push_back(ch_start);
  1995.         }
  1996.         else
  1997.         {
  1998.             double ch_start = chunk_t_start + chunk_t_step * s;
  1999.             fprintf(fp_corr_not_zero, "%f\t%f\n", ch_start, 0.0);
  2000.         }
  2001.     }
  2002.  
  2003.     fclose(fp_corr_not_zero);
  2004.     fclose(fp_V_spikes_chunks);
  2005.  
  2006.     corr_aver_mean /= chunk_step_count;
  2007.     corr_aver_mean_not_zero /= chunk_step_count;
  2008.  
  2009.     double k_syn_local_max_mean = 0;
  2010.     int k_syn_local_max_mean_count = 0;
  2011.  
  2012.     double k_syn_local_min_mean = 0;
  2013.     int k_syn_local_min_mean_count = 0;
  2014.  
  2015.     FILE* fp_max = fopen("k_syn_local_max.txt", "w+");
  2016.     FILE* fp_min = fopen("k_syn_local_min.txt", "w+");
  2017.  
  2018.     for (int i = 0; i < Ca_intervals.size(); i++)
  2019.     {
  2020.         if (Ca_intervals[i].T_end < 0)
  2021.             break;
  2022.  
  2023.         {
  2024.             double k_syn_local_max = 0;
  2025.             bool firstValue = true;
  2026.  
  2027.             for (int j = 0; j < k_syn.size(); j++)
  2028.             {
  2029.                 if (k_syn_time[j] >= Ca_intervals[i].T_start && k_syn_time[j] <= Ca_intervals[i].T_end)
  2030.                 {
  2031.                     if (firstValue)
  2032.                     {
  2033.                         k_syn_local_max = k_syn[j];
  2034.                         firstValue = false;
  2035.                     }
  2036.                     else if (k_syn[j] > k_syn_local_max)
  2037.                     {
  2038.                         k_syn_local_max = k_syn[j];
  2039.                     }
  2040.                 }
  2041.             }
  2042.  
  2043.             if (!firstValue)
  2044.             {
  2045.                 k_syn_local_max_mean += k_syn_local_max;
  2046.                 k_syn_local_max_mean_count++;
  2047.  
  2048.                 fprintf(fp_max, "%f\n", k_syn_local_max);
  2049.             }
  2050.         }
  2051.  
  2052.         {
  2053.             double k_syn_local_min = 1;
  2054.             bool firstValue = true;
  2055.  
  2056.             for (int j = 0; j < k_syn.size(); j++)
  2057.             {
  2058.                 if (k_syn_time[j] >= Ca_intervals[i].T_start && k_syn_time[j] <= Ca_intervals[i].T_end)
  2059.                 {
  2060.                     if (firstValue)
  2061.                     {
  2062.                         k_syn_local_min = k_syn[j];
  2063.                         firstValue = false;
  2064.                     }
  2065.                     else if (k_syn[j] < k_syn_local_min)
  2066.                     {
  2067.                         k_syn_local_min = k_syn[j];
  2068.                     }
  2069.                 }
  2070.             }
  2071.  
  2072.             if (!firstValue)
  2073.             {
  2074.                 k_syn_local_min_mean += k_syn_local_min;
  2075.                 k_syn_local_min_mean_count++;
  2076.  
  2077.                 fprintf(fp_min, "%f\n", k_syn_local_min);
  2078.             }
  2079.         }
  2080.     }
  2081.  
  2082.     fclose(fp_max);
  2083.     fclose(fp_min);
  2084.  
  2085.     if (k_syn_local_max_mean_count != 0)
  2086.         k_syn_local_max_mean /= k_syn_local_max_mean_count;
  2087.  
  2088.     if (k_syn_local_min_mean_count != 0)
  2089.         k_syn_local_min_mean /= k_syn_local_min_mean_count;
  2090.  
  2091.  
  2092.  
  2093.     //fp0 = fopen("corr_aver_mean.txt", "w+");
  2094.     //fprintf(fp0, "%f\n", corr_aver_mean);
  2095.     //fclose(fp0);
  2096.  
  2097.     //fp0 = fopen("corr_aver_mean_not_zero.txt", "w+");
  2098.     //fprintf(fp0, "%f\n", corr_aver_mean_not_zero);
  2099.     //fclose(fp0);
  2100.     ////// CHUNKS END
  2101.  
  2102.     // 2
  2103.     double* V_freq_sync_time = new double[Node_count];
  2104.     double* V_freq_sync_time_relative = new double[Node_count];
  2105.  
  2106. #pragma omp parallel for
  2107.     for (int i = 0; i < Node_count; i++)
  2108.     {
  2109.         list<double>::iterator it_V_spikes = V_spikes[i].begin();
  2110.  
  2111.         while (it_V_spikes != V_spikes[i].end() && *it_V_spikes < 0.25 * t_max)
  2112.         {
  2113.             V_spikes[i].pop_front();
  2114.             it_V_spikes = V_spikes[i].begin();
  2115.         }
  2116.  
  2117.         list<double> V_freqs_normalized;
  2118.         list<double> V_freqs_time;
  2119.  
  2120.         it_V_spikes = V_spikes[i].begin();
  2121.  
  2122.         if (V_spikes[i].size() <= 1)
  2123.         {
  2124.             continue;
  2125.         }
  2126.         else
  2127.         {
  2128.             for (int j = 1; j < V_spikes[i].size(); j++)
  2129.             {
  2130.                 double first = *it_V_spikes;
  2131.                 advance(it_V_spikes, 1);
  2132.                 double next = *it_V_spikes;
  2133.  
  2134.                 double T = next - first;
  2135.                 V_freqs_normalized.push_back(1 / T - V_mean_mean_Freq);
  2136.                 V_freqs_time.push_back(next);
  2137.             }
  2138.         }
  2139.  
  2140.         list<double>::iterator it_Freq_normalized = V_freqs_normalized.begin();
  2141.         list<double>::iterator it_Freq_time = V_freqs_time.begin();
  2142.  
  2143.         V_freq_sync_time[i] = 0;
  2144.  
  2145.         for (int j = 1; j < V_freqs_normalized.size(); j++)
  2146.         {
  2147.             double Freq_normalized_last = *it_Freq_normalized;
  2148.             advance(it_Freq_normalized, 1);
  2149.             double Freq_normalized_next = *it_Freq_normalized;
  2150.  
  2151.             double Freq_time_last = *it_Freq_time;
  2152.             advance(it_Freq_time, 1);
  2153.             double Freq_time_next = *it_Freq_time;
  2154.  
  2155.             if (abs(Freq_normalized_last) <= 0.5 && abs(Freq_normalized_next) <= 0.5 && (Freq_time_next - Freq_time_last) <= 0.035)
  2156.                 V_freq_sync_time[i] += Freq_time_next - Freq_time_last;
  2157.         }
  2158.  
  2159.         V_freq_sync_time_relative[i] = V_freq_sync_time[i] / (t_max - (0.25 * t_max));
  2160.     }
  2161.  
  2162.     double V_mean_freq_sync_time_relative = 0;
  2163.  
  2164.     for (int j = 0; j < Node_count; j++)
  2165.     {
  2166.         V_mean_freq_sync_time_relative += V_freq_sync_time_relative[j];
  2167.     }
  2168.  
  2169.     V_mean_freq_sync_time_relative /= Node_count;
  2170.  
  2171.     fp0 = fopen("V_freq_sync_time_relative.txt", "w+");
  2172.     for (int i = 0; i < Node_count; i++)
  2173.     {
  2174.         fprintf(fp0, "%f\t", V_freq_sync_time_relative[i]);
  2175.     }
  2176.     fclose(fp0);
  2177.  
  2178.     fp0 = fopen("V_mean_freq_sync_time_relative.txt", "w+");
  2179.     fprintf(fp0, "%f\n", V_mean_freq_sync_time_relative);
  2180.     //fprintf(fp_res, "%f\n", V_mean_freq_sync_time_relative);
  2181.     fclose(fp0);
  2182.  
  2183.  
  2184.  
  2185.     // 3 BURSTS FREQ
  2186.  
  2187.     double Bursts_Freq = 0;
  2188.  
  2189.     if (g_astro == 0)
  2190.     {
  2191.         Bursts_Freq = FindBurstFreq(dt, 0.25 * t_max, t_max);
  2192.  
  2193.         /*
  2194.         fp0 = fopen("Spikes_sum.txt", "w+");
  2195.         FILE* fp1 = fopen("Spikes_freqs.txt", "w+");
  2196.  
  2197.         if (corr_aver_mean_not_zero >= 0.2)
  2198.         {
  2199.             //int window_length = (1 / V_mean_mean_Freq) / dt;
  2200.             int window_length = 0.03 / dt;
  2201.  
  2202.             int steps_count = t_max / dt;
  2203.  
  2204.             int pow_of_2 = log2(steps_count);
  2205.  
  2206.             if (pow_of_2 > 22)
  2207.                 pow_of_2 = 22;
  2208.  
  2209.             int fft_N = pow(2, pow_of_2);
  2210.  
  2211.             Complex* complex = new Complex[fft_N];
  2212.             int fft_index = 0;
  2213.  
  2214.             for (int j = (0.25 * t_max) / dt; j < steps_count; j++)
  2215.             {
  2216.                 int spikes_count_all = 0;
  2217.  
  2218.                 double t_center = j * dt;
  2219.                 double t_min = t_center - (window_length / 2) * dt;
  2220.                 double t_max = t_center + (window_length / 2) * dt;
  2221.  
  2222.                 for (int i = 0; i < Node_count; i++)
  2223.                 {
  2224.                     int spikes_count = 0;
  2225.  
  2226.                     list<double>::iterator it_V_spikes = V_spikes[i].begin();
  2227.  
  2228.                     if (*it_V_spikes <= t_max)
  2229.                     {
  2230.                         while (it_V_spikes != V_spikes[i].end())
  2231.                         {
  2232.                             if (*it_V_spikes > t_max)
  2233.                                 break;
  2234.  
  2235.                             if (*it_V_spikes >= t_min && *it_V_spikes <= t_max)
  2236.                                 spikes_count++;
  2237.  
  2238.                             it_V_spikes++;
  2239.                         }
  2240.  
  2241.                         spikes_count_all += spikes_count;
  2242.                     }
  2243.                 }
  2244.  
  2245.  
  2246.                 if (fft_index < fft_N)
  2247.                 {
  2248.                     //fft_values_re[fft_index] = sin(2 * M_PI * fft_index / 20);
  2249.  
  2250.                     complex[fft_index] = Complex(spikes_count_all, 0);
  2251.  
  2252.                     fft_index++;
  2253.                 }
  2254.  
  2255.                 fprintf(fp0, "%f\t%d\n", j * dt, spikes_count_all);
  2256.             }
  2257.  
  2258.             CArray data(complex, fft_N);
  2259.             fft(data);
  2260.  
  2261.             double sampleRate = 1 / dt;
  2262.  
  2263.             int max_freq_index = -1;
  2264.             double max_freq_magnitude = 0;
  2265.  
  2266.             for (int i = 1; i < fft_N / 2; i++)
  2267.             {
  2268.                 double magnitude = sqrt(data[i].real() * data[i].real() + data[i].imag() * data[i].imag());
  2269.  
  2270.                 if (max_freq_index == -1 || magnitude > max_freq_magnitude)
  2271.                 {
  2272.                     max_freq_index = i;
  2273.                     max_freq_magnitude = magnitude;
  2274.                 }
  2275.  
  2276.                 fprintf(fp1, "%f\t%10.0f\n", (double)i * sampleRate / fft_N, magnitude);
  2277.             }
  2278.  
  2279.             Bursts_Freq = max_freq_index * sampleRate / fft_N;
  2280.  
  2281.             fprintf(fp1, "\n\nFreq with max magnitude\t%f", Bursts_Freq);
  2282.  
  2283.             delete[] complex;
  2284.         }
  2285.  
  2286.         fclose(fp0);
  2287.         fclose(fp1);
  2288.         */
  2289.     }
  2290.     else
  2291.     {
  2292.         fp0 = fopen("interval_burst_freqs.txt", "w+");
  2293.  
  2294.         for (int i = 0; i < Ca_intervals.size(); i++)
  2295.         {
  2296.             double freq = FindBurstFreq(dt, Ca_intervals[i].T_start, Ca_intervals[i].T_end);
  2297.             fprintf(fp0, "%f\t%f\t%f\n", Ca_intervals[i].T_start, Ca_intervals[i].T_end, freq);
  2298.  
  2299.             Bursts_Freq += freq;
  2300.         }
  2301.  
  2302.         if (Ca_intervals.size() > 0)
  2303.             Bursts_Freq /= Ca_intervals.size();
  2304.  
  2305.         fclose(fp0);
  2306.     }
  2307.  
  2308.     if (write_file)
  2309.     {
  2310.         //fclose(fp_res);
  2311.         fclose(fp_I_stim);
  2312.         ///fclose(fp_I_syn);
  2313.         fclose(fp_Ca);
  2314.         //fclose(fp_IP3);
  2315.         //fclose(fp_z);
  2316.         fclose(fp_V);
  2317.         //fclose(fp_m);
  2318.         //fclose(fp_n);
  2319.         //fclose(fp_h);
  2320.         //fclose(fp_G_P);
  2321.         fclose(fp_V_P);
  2322.         //fclose(fp_m_P);
  2323.         //fclose(fp_n_P);
  2324.         //fclose(fp_h_P);
  2325.  
  2326.         fclose(fp_V_spikes);
  2327.     }
  2328.  
  2329.     double extime_rk4 = 0;
  2330.  
  2331.     if (omp_enable)
  2332.     {
  2333.         omp_end_rk4 = omp_get_wtime();
  2334.         extime_rk4 = (double)(omp_end_rk4 - omp_start_rk4);// / CLOCKS_PER_SEC;
  2335.     }
  2336.     else
  2337.     {
  2338.         end_rk4 = clock();
  2339.         extime_rk4 = (double)(end_rk4 - start_rk4);// / CLOCKS_PER_SEC;
  2340.     }
  2341.  
  2342.     int minutes = (int)extime_rk4 / 60;
  2343.     int seconds = (int)extime_rk4 % 60;
  2344.     printf("\nExecution time is: %d minutes %d seconds\n ", minutes, seconds);
  2345.  
  2346.     /*int nth;
  2347.     #pragma omp parallel
  2348.     {
  2349.         #pragma omp master
  2350.         nth = omp_get_num_threads();
  2351.     }*/
  2352.  
  2353.     fp0 = fopen("time_exec.txt", "a");
  2354.     //fprintf(fp0, "%d %lf\n", nth, extime_rk4);
  2355.     fprintf(fp0, "%lf\n", extime_rk4);
  2356.     fclose(fp0);
  2357.  
  2358.     /*fp0 = fopen("results_Poisson_Freq_g_syn_k_syn.txt", "a");
  2359.     fprintf(fp0, "%f\t%f\t%f\t%f\t%f\n", Poisson_Freq, g_syn, k_syn_local_min_mean, corr_aver_mean_not_zero, k_syn_local_max_mean);
  2360.     fclose(fp0);*/
  2361.  
  2362.     fp0 = fopen("results_Poisson_Freq_g_syn_Bursts_Freq.txt", "a");
  2363.     fprintf(fp0, "%f\t%f\t%f\t%f\t%f\t%f\n", Poisson_Freq, g_astro, g_syn, corr_aver_mean_not_zero, V_mean_mean_Freq, Bursts_Freq);
  2364.     fclose(fp0);
  2365.  
  2366.     return 0;
  2367.  
  2368.     for (int i = 0; i < Node_count; i++)
  2369.         delete[] A_A[i];
  2370.  
  2371.     delete[] A_A;
  2372.  
  2373.     for (int i = 0; i < Node_count; i++)
  2374.         delete[] B_A[i];
  2375.  
  2376.     delete[] B_A;
  2377.  
  2378.     delete[] C_A;
  2379.  
  2380.     for (int i = 0; i < Node_count; i++)
  2381.         delete[] A_N[i];
  2382.  
  2383.     delete[] A_N;
  2384.  
  2385.     for (int i = 0; i < Node_count; i++)
  2386.         delete[] B_N[i];
  2387.  
  2388.     delete[] B_N;
  2389.  
  2390.     delete[] C_N;
  2391.  
  2392.     delete[] A_N_P;
  2393.  
  2394.     for (int i = 0; i < Node_count; i++)
  2395.         delete[] B_N_P[i];
  2396.  
  2397.     delete[] B_N_P;
  2398.  
  2399.     delete[] C_N_P;
  2400.  
  2401.     delete[] f;
  2402.     delete[] f_diff;
  2403.     delete[] v_4;
  2404.     delete[] E_syn;
  2405.     delete[] I_app;
  2406.     delete[] V_spikes;
  2407.     delete[] V_spikes_Freq;
  2408.  
  2409.     delete[] Meander_start_from_zero;
  2410.     delete[] Meander_width;
  2411.     delete[] Meander_height;
  2412.     delete[] Meander_interval;
  2413.     delete[] last_meander_end;
  2414.     delete[] V_freq_sync_time;
  2415.  
  2416.     for (int i = 0; i < Node_count; i++)
  2417.         delete[] tau[i];
  2418.  
  2419.     delete[] tau;
  2420.  
  2421.     for (int i = 0; i < Node_count; i++)
  2422.         delete[] V_old_array[i];
  2423.  
  2424.     delete[] V_old_array;
  2425.  
  2426.     for (int i = 0; i < Equations_count; i++)
  2427.         delete[] k[i];
  2428.  
  2429.     delete[] k;
  2430.  
  2431.     delete[] phi_k1;
  2432.     delete[] phi_k2;
  2433.     delete[] phi_k3;
  2434. }
  2435.  
Advertisement
Add Comment
Please, Sign In to add comment