SpaceQuester

Untitled

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