Advertisement
SpaceQuester

Untitled

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