Advertisement
SpaceQuester

Untitled

Feb 16th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.84 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include "math.h"
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <locale.h>
  6. #include <time.h>
  7. #include <stdbool.h>
  8.  
  9. #define Node_count 5*5*2 // должно быть таким, что бы из этого числа, разделенного на два, извлекался корень
  10. #define Node_count_half Node_count / 2
  11. #define Node_wire_width (int)sqrt(Node_count_half)
  12.  
  13. #define Equations_per_node 4
  14. #define Equations_count Node_count * Equations_per_node
  15.  
  16. double f[Equations_count];
  17. double f_diff[Equations_count];
  18.  
  19. double C_m = 1; // muF/cm^2
  20. double g_K = 35; // mS/cm^2
  21. double g_Na = 40; // mS/cm^2
  22. double g_L = 0.3; // mS/cm^2
  23. double E_K = -77; // mV
  24. double E_Na = 55; // mV
  25. double E_L = -65; // mV
  26.  
  27. double g_syn = 0.08; // 0.1 // 0.04 // 0.2
  28. double k_syn = 0.2; // 0.2
  29. double E_syn[Node_count];
  30.  
  31. double I_app[Node_count];
  32.  
  33. /*const double sigma_G = 0.0;
  34. const double sigma_GN = 0.0;
  35. const double sigma_N = 1.0;*/
  36.  
  37. double** A;
  38. double** B;
  39. double* C;
  40. double** sigma;
  41. double tau[Node_count][Node_count];
  42.  
  43. #define tau_min 2 // ms
  44. #define tau_max 12 // ms
  45.  
  46. #define ms_to_step 200 // (0.001 / dt) !!! Don't forget !!!
  47.  
  48. #define Max_delay tau_max * ms_to_step
  49. double V_old_array[Node_count][Max_delay];
  50.  
  51. const double Freq = 1300; // Hz
  52. const double Min_magintude = -0.00; // pA
  53. const double Max_magintude = 0.1; // pA
  54. const double Duration = 0.001; // sec
  55.  
  56. double Meander_start_from_zero[Node_count];
  57. double Meander_width[Node_count];
  58. double Meander_height[Node_count];
  59. double Meander_interval[Node_count];
  60. double last_meander_end[Node_count];
  61.  
  62. double I_stim(int i, double t)
  63. {
  64. if (t < Meander_start_from_zero[i])
  65. return 0;
  66.  
  67. t -= Meander_start_from_zero[i];
  68. t = fmod(t, Meander_width[i] + Meander_interval[i]);
  69.  
  70. return t < Meander_width[i] ? Meander_height[i] : 0;
  71. }
  72.  
  73. double V(int i)
  74. {
  75. return f[i * 4];
  76. }
  77.  
  78. void SetV(int i, double value)
  79. {
  80. f[i * 4] = value;
  81. }
  82.  
  83. double m(int i)
  84. {
  85. return f[i * 4 + 1];
  86. }
  87.  
  88. void Setm(int i, double value)
  89. {
  90. f[i * 4 + 1] = value;
  91. }
  92.  
  93. double n(int i)
  94. {
  95. return f[i * 4 + 2];
  96. }
  97.  
  98. void Setn(int i, double value)
  99. {
  100. f[i * 4 + 2] = value;
  101. }
  102.  
  103. double h(int i)
  104. {
  105. return f[i * 4 + 3];
  106. }
  107.  
  108. void Seth(int i, double value)
  109. {
  110. f[i * 4 + 3] = value;
  111. }
  112.  
  113. double V_old(int i, int delay)
  114. {
  115. return V_old_array[i][Max_delay - 1 - delay];
  116. }
  117.  
  118. int RandomI(int min, int max)
  119. {
  120. return ((double)rand() / (RAND_MAX - 1)) * (max - min) + min;
  121. }
  122.  
  123. double RandomD(double min, double max)
  124. {
  125. return ((double)rand() / RAND_MAX) * (max - min) + min;
  126. }
  127.  
  128. double alpha_m(double* f, int i)
  129. {
  130. return 0.182 * (V(i) + 35) / (1 - exp(-(V(i) + 35) / 9));
  131. }
  132.  
  133. double beta_m(double* f, int i)
  134. {
  135. return -0.124 * (V(i) + 35) / (1 - exp((V(i) + 35) / 9));
  136. }
  137.  
  138. double alpha_n(double* f, int i)
  139. {
  140. return 0.02 * (V(i) - 25) / (1 - exp(-(V(i) - 25) / 9));
  141. }
  142.  
  143. double beta_n(double* f, int i)
  144. {
  145. return -0.002 * (V(i) - 25) / (1 - exp((V(i) - 25) / 9));
  146. }
  147.  
  148. double alpha_h(double* f, int i)
  149. {
  150. return 0.25 * exp(-(V(i) + 90) / 12);
  151. }
  152.  
  153. double beta_h(double* f, int i)
  154. {
  155. return 0.25 * exp((V(i) + 62) / 6) / exp((V(i) + 90) / 12);
  156. }
  157.  
  158. double HodgkinHuxley(int i, double* f, double t)
  159. {
  160. int in = i / 4;
  161. int il = i % 4;
  162.  
  163. switch (il)
  164. {
  165. case 0:
  166. {
  167. double sum = 0;
  168. //double ee = 0;
  169. //double Vold = 0;
  170.  
  171. /*for (int j = 0; j < Node_count; j++)
  172. {
  173. //sum += A[in][j] * g_syn * (V(in) - V(j));
  174. sum += A[in][j] * g_syn * (V(in) - E_syn[in]) / (1 + exp(-V_old(j, tau[in][j]) / k_syn));
  175. }*/
  176.  
  177. /*for (int j = 0; j < C[in]; j++)
  178. {
  179. sum += sigma[in][(int)B[in][j]] * (V((int)B[in][j]) - V(in));
  180. }*/
  181.  
  182. for (int j = 0; j < C[in]; j++)
  183. {
  184. //sum += g_syn * (V((int)B[in][j]) - V(in)); // устаревшая часть, нужна для проверки разностной схемы
  185. //sum += A[in][j] * g_syn * (V(in) - E_syn[in]) / (1 + exp(-V_old((int)B[in][j]) / k_syn)); // устаревшая часть
  186. 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));
  187.  
  188. //Vold = V_old((int)B[in][j], tau[in][(int)B[in][j]]);
  189. //ee = exp(-V_old((int)B[in][j], tau[in][(int)B[in][j]]) / k_syn);
  190. //printf("i = %d\t V_old = %f\t exp = %f\n", in, Vold, ee);
  191. }
  192. //printf("i = %d\t sum = %f\n", in, sum);
  193.  
  194. 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_stim(in, t) + sum ) / C_m); // V
  195. }
  196.  
  197. case 1:
  198. {
  199. return 1000 * (alpha_m(f, in) * (1 - m(in)) - beta_m(f, in) * m(in)); // m
  200. }
  201.  
  202. case 2:
  203. {
  204. return 1000 * (alpha_n(f, in) * (1 - n(in)) - beta_n(f, in) * n(in)); // n
  205. }
  206.  
  207. case 3:
  208. {
  209. return 1000 * (alpha_h(f, in) * (1 - h(in)) - beta_h(f, in) * h(in)); // h
  210. }
  211. }
  212.  
  213. return 0;
  214. }
  215.  
  216. void RungeKutta(double t, double dt, double* f, double* f_next)
  217. {
  218. double k[Equations_count][4];
  219.  
  220. // k1
  221. for (int i = 0; i < Equations_count; i++)
  222. k[i][0] = HodgkinHuxley(i, f, t) * dt;
  223.  
  224. double phi_k1[Equations_count];
  225. for (int i = 0; i < Equations_count; i++)
  226. phi_k1[i] = f[i] + k[i][0] / 2;
  227.  
  228. // k2
  229. for (int i = 0; i < Equations_count; i++)
  230. k[i][1] = HodgkinHuxley(i, phi_k1, t) * dt;
  231.  
  232. double phi_k2[Equations_count];
  233. for (int i = 0; i < Equations_count; i++)
  234. phi_k2[i] = f[i] + k[i][1] / 2;
  235.  
  236. // k3
  237. for (int i = 0; i < Equations_count; i++)
  238. k[i][2] = HodgkinHuxley(i, phi_k2, t) * dt;
  239.  
  240. double phi_k3[Equations_count];
  241. for (int i = 0; i < Equations_count; i++)
  242. phi_k3[i] = f[i] + k[i][2] / 2;
  243.  
  244. // k4
  245. for (int i = 0; i < Equations_count; i++)
  246. k[i][3] = HodgkinHuxley(i, phi_k3, t) * dt;
  247.  
  248. for (int i = 0; i < Equations_count; i++)
  249. f_next[i] = f[i] + (k[i][0] + 2 * k[i][1] + 2 * k[i][2] + k[i][3]) / 6;
  250. }
  251.  
  252. void CopyArray(double* source, double* target, int N)
  253. {
  254. for (int i = 0; i < N; i++)
  255. target[i] = source[i];
  256. }
  257.  
  258. bool Approximately(double a, double b)
  259. {
  260. if (a < 0)
  261. a = -a;
  262.  
  263. if (b < 0)
  264. b = -b;
  265.  
  266. return a - b <= 0.000001;
  267. }
  268.  
  269. // http://preshing.com/20111007/how-to-generate-random-timings-for-a-poisson-process/
  270. double nextTime(double rateParameter)
  271. {
  272. return -log(1.0 - (double)rand() / (RAND_MAX)) / rateParameter;
  273. }
  274.  
  275. void GenerateRandomMeander(int i, double min_start_time)
  276. {
  277. double offset = nextTime(Freq);
  278.  
  279. if (offset < 0)
  280. {
  281. int a = 0;
  282. }
  283.  
  284. Meander_start_from_zero[i] = min_start_time + offset;
  285. Meander_width[i] = Duration;
  286. Meander_height[i] = RandomD(Min_magintude, Max_magintude);
  287. }
  288.  
  289. void FillAMatrixZero()
  290. {
  291. for (int i = 0; i < Node_count; i++)
  292. {
  293. for (int j = 0; j < Node_count; j++)
  294. {
  295. A[i][j] = 0;
  296. }
  297. }
  298. }
  299.  
  300. void FillBCMatrix()
  301. {
  302. for (int i = 0; i < Node_count; i++)
  303. {
  304. int bIndex = 0;
  305. C[i] = 0;
  306. for (int j = 0; j < Node_count; j++)
  307. {
  308. if (A[i][j] == 1)
  309. {
  310. B[i][bIndex] = j;
  311. bIndex++;
  312. C[i]++;
  313. }
  314. }
  315. }
  316. }
  317.  
  318. /*void FillSigmaMatrix()
  319. {
  320. for (int i = 0; i < Node_count; i++)
  321. {
  322. for (int j = 0; j < Node_count; j++)
  323. {
  324. if ((i >= 0 && i < Node_count_half) && (j >= 0 || j < Node_count_half))
  325. {
  326. sigma[i][j] = sigma_G;
  327. }
  328. if ((((i >= Node_count_half && i < Node_count) && (j >= 0 && j < Node_count_half)) || ((i >= 0 && i < Node_count_half) && (j >= Node_count_half && j < Node_count))))
  329. {
  330. sigma[i][j] = sigma_GN;
  331. }
  332. if ((i >= Node_count_half && i < Node_count) && (j >= Node_count_half && j < Node_count))
  333. {
  334. sigma[i][j] = sigma_N;
  335. }
  336. }
  337. }
  338. }*/
  339.  
  340. void FillRandomMatrix()
  341. {
  342. double p_links = 0.2; // 0.2
  343. //for (int k = 0; k < 2 * Node_count_half; k++)
  344. for (int k = 0; k < p_links * Node_count_half * (Node_count_half - 1); k++)
  345. {
  346. int i, j;
  347.  
  348. do
  349. {
  350. i = RandomI(Node_count_half, Node_count);
  351. j = RandomI(Node_count_half, Node_count);
  352. } while ((i == j) || (A[i][j] == 1));
  353.  
  354. A[i][j] = 1;
  355. //A[j][i] = 1;
  356. }
  357. }
  358.  
  359. void FillVOldFromCurrent()
  360. {
  361. for (int i = 0; i < Node_count; i++)
  362. for (int j = 0; j < Max_delay; j++)
  363. V_old_array[i][j] = V(i);
  364. }
  365.  
  366. void UpdateVOld()
  367. {
  368. for (int i = 0; i < Node_count; i++)
  369. {
  370. for (int j = 1; j < Max_delay; j++)
  371. V_old_array[i][j - 1] = V_old_array[i][j];
  372.  
  373. V_old_array[i][Max_delay - 1] = V(i);
  374. }
  375. }
  376.  
  377. /*void FillFullTauMatrix()
  378. {
  379. for (int i = 0; i < Node_count; i++)
  380. {
  381. for (int j = 0; j < Node_count; j++)
  382. {
  383. if (i < Node_count_half || j < Node_count_half)
  384. {
  385. tau[i][j] = 0;
  386. continue;
  387. }
  388.  
  389. int i_neuron = i - Node_count_half;
  390. int j_neuron = j - Node_count_half;
  391.  
  392. int i_wire_x = i_neuron / Node_wire_width;
  393. int i_wire_y = i_neuron % Node_wire_width;
  394.  
  395. int j_wire_x = j_neuron / Node_wire_width;
  396. int j_wire_y = j_neuron % Node_wire_width;
  397.  
  398. double distance_max = sqrt(2.) * (Node_wire_width - 1);
  399. 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));
  400.  
  401. tau[i][j] = (tau_min + distance / (distance_max) * (tau_max - tau_min)) * ms_to_step;
  402. }
  403. }
  404. }*/
  405.  
  406. void FillTauMatrix()
  407. {
  408. for (int i = 0; i < Node_count; i++)
  409. {
  410. for (int j = 0; j < Node_count; j++)
  411. {
  412. if (i < Node_count_half || j < Node_count_half || i == j || A[i][j] == 0)
  413. {
  414. tau[i][j] = 0;
  415. continue;
  416. }
  417.  
  418. int i_neuron = i - Node_count_half;
  419. int j_neuron = j - Node_count_half;
  420.  
  421. int i_wire_x = i_neuron / Node_wire_width;
  422. int i_wire_y = i_neuron % Node_wire_width;
  423.  
  424. int j_wire_x = j_neuron / Node_wire_width;
  425. int j_wire_y = j_neuron % Node_wire_width;
  426.  
  427. double distance_max = sqrt(2.) * (Node_wire_width - 1);
  428. 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));
  429.  
  430. double t = (distance - 1) / (distance_max - 1);
  431. tau[i][j] = (tau_min + t * (tau_max - tau_min)) * ms_to_step;
  432. }
  433. }
  434. }
  435.  
  436. int main(int argc, char *argv[])
  437. {
  438. FILE *fp0;
  439. //FILE *fp_Ca;
  440. //FILE *fp_IP3;
  441. //FILE *fp_z;
  442. //FILE *fp_G;
  443. FILE *fp_I_stim;
  444. FILE *fp_V;
  445. FILE *fp_m;
  446. FILE *fp_n;
  447. FILE *fp_h;
  448. FILE *fp_V_spikes;
  449. srand(time(NULL));
  450.  
  451. //for (int i = 0; i < Node_count; i++)
  452. // V_old_length[i] = 0;
  453.  
  454. A = malloc(Node_count * sizeof(double));
  455. for (int i = 0; i < Node_count; i++)
  456. A[i] = malloc(Node_count * sizeof(double));
  457.  
  458. B = malloc(Node_count * sizeof(double));
  459. for (int i = 0; i < Node_count; i++)
  460. B[i] = malloc(Node_count * sizeof(double));
  461.  
  462. C = malloc(Node_count * sizeof(double));
  463.  
  464. sigma = malloc(Node_count * sizeof(double));
  465. for (int i = 0; i < Node_count; i++)
  466. sigma[i] = malloc(Node_count * sizeof(double));
  467.  
  468. FillAMatrixZero();
  469. FillRandomMatrix();
  470. FillBCMatrix();
  471. //FillSigmaMatrix();
  472. FillTauMatrix();
  473.  
  474. fp0 = fopen("A.txt", "w+");
  475. for (int i = 0; i < Node_count; i++)
  476. {
  477. for (int j = 0; j < Node_count; j++)
  478. {
  479. fprintf(fp0, "%d\t", (int)A[i][j]);
  480. }
  481. fprintf(fp0, "\n");
  482. }
  483. fclose(fp0);
  484.  
  485. fp0 = fopen("tau.txt", "w+");
  486. for (int i = 0; i < Node_count; i++)
  487. {
  488. for (int j = 0; j < Node_count; j++)
  489. {
  490. fprintf(fp0, "%f\t", tau[i][j] / ms_to_step);
  491. }
  492. fprintf(fp0, "\n");
  493. }
  494. fclose(fp0);
  495.  
  496. //Пишем в файл число связей у каждого осциллятора в четвертом квадранте
  497. fp0 = fopen("links.txt", "w+");
  498. for (int i = Node_count_half; i < Node_count; i++)
  499. {
  500. int links_count = 0;
  501. for (int j = Node_count_half; j < Node_count; j++)
  502. {
  503. if (A[i][j] == 1)
  504. {
  505. links_count++;
  506. }
  507. }
  508. fprintf(fp0, "%d\n", (int)links_count);
  509.  
  510. }
  511. fclose(fp0);
  512.  
  513. //setlocale(LC_NUMERIC, "French_Canada.1252");
  514. fp0 = fopen("test_Poisson.txt", "w+");
  515. for (int i = 0; i < 10000; i++)
  516. fprintf(fp0, "%f\n", nextTime(Freq));
  517. fclose(fp0);
  518.  
  519. fp0 = fopen("B.txt", "w+");
  520. for (int i = 0; i < Node_count; i++)
  521. {
  522. for (int j = 0; j < C[i]; j++)
  523. {
  524. fprintf(fp0, "%d\t", (int)B[i][j]);
  525. }
  526. fprintf(fp0, "\n");
  527. }
  528. fclose(fp0);
  529.  
  530. fp0 = fopen("C.txt", "w+");
  531. for (int i = 0; i < Node_count; i++)
  532. {
  533. fprintf(fp0, "%d\n", (int)C[i]);
  534. }
  535. fclose(fp0);
  536.  
  537. /*fp0 = fopen("sigma.txt", "w+");
  538. for (int i = 0; i < Node_count; i++)
  539. {
  540. for (int j = 0; j < Node_count; j++)
  541. {
  542. fprintf(fp0, "%f\t", sigma[i][j]);
  543. }
  544. fprintf(fp0, "\n");
  545. }
  546. fclose(fp0);*/
  547.  
  548. // Initial values
  549. /*for (int i = 0; i < Equations_count; i++)
  550. {
  551. f[i] = 0;
  552. }
  553.  
  554. for (int i = 0; i < Equations_count; i++)
  555. {
  556. I_app[i] = RandomD(9, 40);
  557. }*/
  558.  
  559. double percent_stable_state = 0.40; // 0.40
  560.  
  561. double V0 = -58.7085;
  562. double m0 = 0.0953;
  563. double n0 = 0.000913;
  564. double h0 = 0.3662;
  565.  
  566. double V1 = 14.8409;
  567. double m1 = 0.9174;
  568. double n1 = 0.0140;
  569. double h1 = 0.0539;
  570.  
  571. for (int i = 0; i < Node_count; i++) // init array for all neurons
  572. {
  573. SetV(i, 0); // V
  574. Setm(i, 0); // m
  575. Setn(i, 0); // n
  576. Seth(i, 0); // h
  577. }
  578. for (int i = Node_count_half; i < Node_count; i++) // init only neuron nodes
  579. {
  580. double random = RandomD(0, 1);
  581.  
  582. SetV(i, random < percent_stable_state ? V0 : V1); // V
  583. Setm(i, random < percent_stable_state ? m0 : m1); // m
  584. Setn(i, random < percent_stable_state ? n0 : n1); // n
  585. Seth(i, random < percent_stable_state ? h0 : h1); // h
  586. }
  587.  
  588. for (int i = 0; i < Node_count_half; i++)
  589. {
  590. I_app[i] = 0; // init for 1st half array
  591. }
  592.  
  593. for (int i = Node_count_half; i < Node_count; i++)
  594. {
  595. I_app[i] = 0.8; // init for neurons; Bifurcation point: I_app = 0.82; I_app_up = 1.04
  596. }
  597.  
  598. double percent_excitable = 0.8; // 0.8
  599. double E_syn0 = 0;
  600. double E_syn1 = -90;
  601.  
  602. for (int i = 0; i < Node_count; i++)
  603. {
  604. E_syn[i] = 0; // init for neurons all array
  605. }
  606.  
  607. for (int i = Node_count_half; i < Node_count; i++)
  608. {
  609. double random = RandomD(0, 1);
  610. E_syn[i] = random < percent_excitable ? E_syn0 : E_syn1;
  611. //printf("i = %d\t E_syn = %f\n", i, E_syn[i]);
  612. }
  613.  
  614. for (int i = 0; i < Node_count; i++)
  615. {
  616. GenerateRandomMeander(i, 0);
  617. last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  618. }
  619.  
  620. const double t_start = 0;
  621. const double t_max = 40.0; // 100 msec = 0.1 sec
  622. const double dt = 0.000005; // 0.01 msec = 0.00001 sec; 1 msec = 0.001 sec
  623.  
  624. double t = t_start;
  625.  
  626. //fp0 = fopen("results.txt", "w+");
  627. //setlocale(LC_NUMERIC, "French_Canada.1252");
  628.  
  629. clock_t start_rk4, end_rk4;
  630. start_rk4 = clock();
  631. int lastPercent = -1;
  632.  
  633. FillVOldFromCurrent();
  634.  
  635. //fp_Ca = fopen("results_Ca.txt", "w+");
  636. //fp_IP3 = fopen("results_IP3.txt", "w+");
  637. //fp_z = fopen("results_z.txt", "w+");
  638. //fp_G = fopen("results_G.txt", "w+");
  639. fp_I_stim = fopen("results_I_stim.txt", "w+");
  640. fp_V = fopen("results_V.txt", "w+");
  641. fp_m = fopen("results_m.txt", "w+");
  642. fp_n = fopen("results_n.txt", "w+");
  643. fp_h = fopen("results_h.txt", "w+");
  644. fp_V_spikes = fopen("results_V_spikes.txt", "w+");
  645.  
  646. while (t < t_max || Approximately(t, t_max))
  647. {
  648. //fprintf(fp_Ca, "%f\t", t);
  649. //fprintf(fp_IP3, "%f\t", t);
  650. //fprintf(fp_z, "%f\t", t);
  651. //fprintf(fp_G, "%f\t", t);
  652. fprintf(fp_I_stim, "%f\t", t);
  653. fprintf(fp_V, "%f\t", t);
  654. fprintf(fp_m, "%f\t", t);
  655. fprintf(fp_n, "%f\t", t);
  656. fprintf(fp_h, "%f\t", t);
  657. fprintf(fp_V_spikes, "%f\t", t);
  658.  
  659. for (int i = 0; i < Node_count; i++)
  660. {
  661. if (t > last_meander_end[i])
  662. {
  663. GenerateRandomMeander(i, t);
  664. last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  665. }
  666.  
  667. fprintf(fp_I_stim, "%f\t", I_stim(i, t));
  668. }
  669. fprintf(fp_I_stim, "\n");
  670.  
  671. // Зашито на будущее
  672. //for (int i = 0; i < Equations_count; i += 8)
  673. // fprintf(fp_Ca, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // Ca
  674.  
  675. //for (int i = 1; i < Equations_count; i += 8)
  676. // fprintf(fp_IP3, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // IP3
  677.  
  678. //for (int i = 2; i < Equations_count; i += 8)
  679. // fprintf(fp_z, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // z
  680.  
  681. //for (int i = 3; i < Equations_count; i += 8)
  682. // fprintf(fp_G, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // G
  683.  
  684. for (int i = 0; i < Equations_count; i += 4)
  685. fprintf(fp_V, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // V
  686.  
  687. for (int i = 1; i < Equations_count; i += 4)
  688. fprintf(fp_m, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // m
  689.  
  690. for (int i = 2; i < Equations_count; i += 4)
  691. fprintf(fp_n, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // n
  692.  
  693. for (int i = 3; i < Equations_count; i += 4)
  694. fprintf(fp_h, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // h
  695.  
  696. //fprintf(fp_Ca, "\n");
  697. //fprintf(fp_IP3, "\n");
  698. //fprintf(fp_z, "\n");
  699. //fprintf(fp_G, "\n");
  700. fprintf(fp_V, "\n");
  701. fprintf(fp_m, "\n");
  702. fprintf(fp_n, "\n");
  703. fprintf(fp_h, "\n");
  704.  
  705. double f_next[Equations_count];
  706.  
  707. RungeKutta(t, dt, f, f_next);
  708.  
  709. for (int i = 0; i < Equations_count; i += 4)
  710. {
  711. double diff = f_next[i] - f[i];
  712.  
  713. fprintf(fp_V_spikes, i == Equations_count - 1 ? "%d" : "%d\t", diff < 0 && f_diff[i] > 0 && f[i] > 0 ? 1 : 0);
  714.  
  715. f_diff[i] = diff;
  716. }
  717.  
  718. fprintf(fp_V_spikes, "\n");
  719.  
  720. CopyArray(f_next, f, Equations_count);
  721.  
  722. t += dt;
  723.  
  724. int percent = (int)(100 * (t - t_start) / (t_max - t_start));
  725. if (percent != lastPercent)
  726. {
  727. printf("Progress: %d%%\n", percent);
  728. lastPercent = percent;
  729. }
  730.  
  731. //printf("V(24) = %f\t V_old(24) = %f\n", f[24*4], V_old(24));
  732. UpdateVOld();
  733. }
  734.  
  735. //fclose(fp_Ca);
  736. //fclose(fp_IP3);
  737. //fclose(fp_z);
  738. //fclose(fp_G);
  739. fclose(fp_I_stim);
  740. fclose(fp_V);
  741. fclose(fp_m);
  742. fclose(fp_n);
  743. fclose(fp_h);
  744. fclose(fp_V_spikes);
  745.  
  746. end_rk4 = clock();
  747. double extime_rk4 = (double)(end_rk4 - start_rk4) / CLOCKS_PER_SEC;
  748. int minutes = (int)extime_rk4 / 60;
  749. int seconds = (int)extime_rk4 % 60;
  750. printf("\nExecution time is: %d minutes %d seconds\n ", minutes, seconds);
  751.  
  752. fp0 = fopen("time_exec.txt", "w+");
  753. fprintf(fp0, "%f\n", extime_rk4);
  754. fclose(fp0);
  755.  
  756. for (int i = 0; i < Node_count; i++)
  757. free(A[i]);
  758. free(A);
  759.  
  760. for (int i = 0; i < Node_count; i++)
  761. free(B[i]);
  762. free(B);
  763.  
  764. for (int i = 0; i < Node_count; i++)
  765. free(sigma[i]);
  766. free(sigma);
  767.  
  768. free(C);
  769. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement