SpaceQuester

Untitled

Dec 15th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.27 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 2*2
  10. #define Node_wire_width (int)sqrt(Node_count)
  11.  
  12. #define Equations_per_node 8 // !!! Don't change !!!
  13. #define Equations_count Node_count * Equations_per_node
  14.  
  15. double f[Equations_count];
  16. double f_diff[Equations_count];
  17.  
  18. bool enable_I_syn_out = false;
  19.  
  20. double c_0 = 2; // uM
  21. double c_1 = 0.185;
  22. double v_1 = 6; // s^-1
  23. double v_2 = 0.11; // s^-1
  24. double v_3 = 2.2; // uM/s
  25. double v_4[Node_count]; // uM/s - Controling parameter // 0.5 //double v_4[Node_count]; // uM/s - Controling parameter //0.495
  26. double v_5 = 0.025; // uM/s
  27. double v_6 = 0.2; // uM/s
  28. double k_1 = 0.5; // s^-1
  29. double k_2 = 1; // uM
  30. double k_3 = 0.1;
  31. double k_4 = 1.1; // uM/s
  32. double a_2 = 0.14; // uM/s
  33. double d_1 = 0.13; // uM
  34. double d_2 = 1.049; // uM
  35. double d_3 = 0.9434; // uM
  36. double d_5 = 0.082; // uM
  37. double alpha = 0.8;
  38. double tau_IP3 = 7.143; // s
  39. double IP3_star = 0.16; // uM
  40. double d_Ca = 0.001; // 0.001
  41. double d_IP3 = 0.2; // 0.12
  42. double alpha_Glu = 2; // 2 // 10
  43. double g_astro; // 3
  44.  
  45. double C_m = 1; // muF/cm^2
  46. double g_K = 35; // mS/cm^2
  47. double g_Na = 40; // mS/cm^2
  48. double g_L = 0.3; // mS/cm^2
  49. double E_K = -77; // mV
  50. double E_Na = 55; // mV
  51. double E_L = -65; // mV
  52.  
  53. double I_app_1;
  54. double I_app_2;
  55. double I_app_3;
  56. double I_app_4;
  57.  
  58. double g_syn;// 0.18 // 0.04 // 0.2 // 1.6
  59. double k_syn = 0.2; // 0.2
  60. double E_syn[Node_count];
  61.  
  62. double alpha_G = 25; //s^-1
  63. double beta_G = 500; //s^-1
  64.  
  65. double I_app[Node_count];
  66.  
  67. double** A_A;
  68. double** B_A;
  69. double* C_A;
  70.  
  71. double** A_N;
  72. double** B_N;
  73. double* C_N;
  74.  
  75. FILE *fp_I_syn;
  76.  
  77. double tau[Node_count][Node_count];
  78.  
  79. #define tau_min 2 // ms
  80. #define tau_max 12 // ms
  81.  
  82. #define ms_to_step 40 // (0.001 / dt) !!! Don't forget !!!
  83.  
  84. #define Max_delay tau_max * ms_to_step
  85. double V_old_array[Node_count][Max_delay];
  86.  
  87. const double Freq = 500; // Hz
  88. const double Min_magintude = -0.13; // pA
  89. const double Max_magintude = 8.0; // pA
  90. const double Duration = 0.001; // sec
  91.  
  92. double Meander_start_from_zero[Node_count];
  93. double Meander_width[Node_count];
  94. double Meander_height[Node_count];
  95. double Meander_interval[Node_count];
  96. double last_meander_end[Node_count];
  97.  
  98. double I_stim(int i, double t)
  99. {
  100. if (t < Meander_start_from_zero[i])
  101. return 0;
  102.  
  103. t -= Meander_start_from_zero[i];
  104. t = fmod(t, Meander_width[i] + Meander_interval[i]);
  105.  
  106. return t < Meander_width[i] ? Meander_height[i] : 0;
  107. }
  108.  
  109. double Ca(int i)
  110. {
  111. return f[i * Equations_per_node];
  112. }
  113.  
  114. void SetCa(int i, double value)
  115. {
  116. f[i * Equations_per_node] = value;
  117. }
  118.  
  119. double IP3(int i)
  120. {
  121. return f[i * Equations_per_node + 1];
  122. }
  123.  
  124. void SetIP3(int i, double value)
  125. {
  126. f[i * Equations_per_node + 1] = value;
  127. }
  128.  
  129. double z(int i)
  130. {
  131. return f[i * Equations_per_node + 2];
  132. }
  133.  
  134. void Setz(int i, double value)
  135. {
  136. f[i * Equations_per_node + 2] = value;
  137. }
  138.  
  139. double G(int i)
  140. {
  141. return f[i * Equations_per_node + 3];
  142. }
  143.  
  144. void SetG(int i, double value)
  145. {
  146. f[i * Equations_per_node + 3] = value;
  147. }
  148.  
  149. double V(int i)
  150. {
  151. return f[i * Equations_per_node + 4];
  152. }
  153.  
  154. void SetV(int i, double value)
  155. {
  156. f[i * Equations_per_node + 4] = value;
  157. }
  158.  
  159. double m(int i)
  160. {
  161. return f[i * Equations_per_node + 5];
  162. }
  163.  
  164. void Setm(int i, double value)
  165. {
  166. f[i * Equations_per_node + 5] = value;
  167. }
  168.  
  169. double n(int i)
  170. {
  171. return f[i * Equations_per_node + 6];
  172. }
  173.  
  174. void Setn(int i, double value)
  175. {
  176. f[i * Equations_per_node + 6] = value;
  177. }
  178.  
  179. double h(int i)
  180. {
  181. return f[i * Equations_per_node + 7];
  182. }
  183.  
  184. void Seth(int i, double value)
  185. {
  186. f[i * Equations_per_node + 7] = value;
  187. }
  188.  
  189. double V_old(int i, int delay)
  190. {
  191. return V_old_array[i][Max_delay - 1 - delay];
  192. }
  193.  
  194. int RandomI(int min, int max)
  195. {
  196. return ((double)rand() / (RAND_MAX - 1)) * (max - min) + min;
  197. }
  198.  
  199. double RandomD(double min, double max)
  200. {
  201. return ((double)rand() / RAND_MAX) * (max - min) + min;
  202. }
  203.  
  204. double J_channel(double* f, int i)
  205. {
  206. 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);
  207. }
  208.  
  209. double J_PLC(double* f, int i)
  210. {
  211. return v_4[i] * (Ca(i) + (1 - alpha) * k_4) / (Ca(i) + k_4);
  212. }
  213.  
  214. double J_leak(double* f, int i)
  215. {
  216. return c_1 * v_2 * (c_0 / c_1 - (1 + 1 / c_1) * Ca(i));
  217. }
  218.  
  219. double J_pump(double* f, int i)
  220. {
  221. return v_3 * pow(Ca(i), 2) / (pow(k_3, 2) + pow(Ca(i), 2));
  222. }
  223.  
  224. double J_in(double* f, int i)
  225. {
  226. return v_5 + v_6 * pow(IP3(i), 2) / (pow(k_2, 2) + pow(IP3(i), 2));
  227. }
  228.  
  229. double J_out(double* f, int i)
  230. {
  231. return k_1 * Ca(i);
  232. }
  233.  
  234. double J_Glu(double* f, int i)
  235. {
  236. if (E_syn[i] == 0)
  237. {
  238. /*printf("J_Glu = %f\n", alpha_Glu / (1 + exp(-(G(i) - 0.4) / 0.01)));*/
  239. return alpha_Glu / (1 + exp(-(G(i) - 0.25) / 0.01));
  240. }
  241.  
  242. return 0;
  243. //return alpha_Glu / (1 + exp(-(G(i) - 0.25) / 0.01));
  244. }
  245.  
  246. double alpha_m(double* f, int i)
  247. {
  248. return 0.182 * (V(i) + 35) / (1 - exp(-(V(i) + 35) / 9));
  249. }
  250.  
  251. double beta_m(double* f, int i)
  252. {
  253. return -0.124 * (V(i) + 35) / (1 - exp((V(i) + 35) / 9));
  254. }
  255.  
  256. double alpha_n(double* f, int i)
  257. {
  258. return 0.02 * (V(i) - 25) / (1 - exp(-(V(i) - 25) / 9));
  259. }
  260.  
  261. double beta_n(double* f, int i)
  262. {
  263. return -0.002 * (V(i) - 25) / (1 - exp((V(i) - 25) / 9));
  264. }
  265.  
  266. double alpha_h(double* f, int i)
  267. {
  268. return 0.25 * exp(-(V(i) + 90) / 12);
  269. }
  270.  
  271. double beta_h(double* f, int i)
  272. {
  273. return 0.25 * exp((V(i) + 62) / 6) / exp((V(i) + 90) / 12);
  274. }
  275.  
  276. double UllahJung_HodgkinHuxley(int i, double* f, double t)
  277. {
  278. int in = i / Equations_per_node;
  279. int il = i % Equations_per_node;
  280.  
  281. switch (il)
  282. {
  283. case 0: // Ca
  284. {
  285. double sum_1 = 0;
  286.  
  287. /*for (int j = 0; j < Node_count; j++)
  288. {
  289. sum_1 += d_Ca * (Ca(j) - Ca(in));
  290. }*/
  291.  
  292. for (int j = 0; j < C_A[in]; j++)
  293. {
  294. sum_1 += d_Ca * (Ca((int)B_A[in][j]) - Ca(in));
  295. }
  296.  
  297. return J_channel(f, in) - J_pump(f, in) + J_leak(f, in) + J_in(f, in) - J_out(f, in) + sum_1;
  298. }
  299.  
  300. case 1: // IP3
  301. {
  302. double sum_2 = 0;
  303.  
  304. /*for (int j = 0; j < Node_count; j++)
  305. {
  306. sum_2 += d_IP3 * (IP3(j) - IP3(in));
  307. }*/
  308.  
  309. for (int j = 0; j < C_A[in]; j++)
  310. {
  311. sum_2 += d_IP3 * (IP3((int)B_A[in][j]) - IP3(in));
  312. }
  313.  
  314. return (IP3_star - IP3(in)) / tau_IP3 + J_PLC(f, in) + sum_2 + J_Glu(f, in);
  315. }
  316.  
  317. case 2: // z
  318. {
  319. return a_2 * (d_2 * (IP3(in) + d_1) / (IP3(in) + d_3) * (1 - z(in)) - Ca(in) * z(in));
  320. }
  321.  
  322. case 3: // G
  323. {
  324. return -alpha_G * G(in) + beta_G * (1 / (1 + exp(-V(in) / 0.5)));
  325. }
  326.  
  327. case 4: // V
  328. {
  329. double I_syn = 0;
  330.  
  331. /*for (int j = 0; j < Node_count; j++)
  332. {
  333. //sum += A[in][j] * g_syn * (V(in) - V_old(j, tau[in][j]));
  334. //sum += A[in][j] * g_syn * (V(j) - V(in));
  335. //sum += A[in][j] * g_syn * (V(in) - E_syn[in]) / (1 + exp(-V_old(j, tau[in][j]) / k_syn));
  336. //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
  337. //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
  338. I_syn += A_N[in][j] * g_syn * (E_syn[in] - V(in)) / (1 + exp(-(V(j) / k_syn)));
  339. //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);
  340. }*/
  341.  
  342. /*for (int j = 0; j < C[in]; j++)
  343. {
  344. sum += sigma[in][(int)B[in][j]] * (V((int)B[in][j]) - V(in));
  345. }*/
  346.  
  347. for (int j = 0; j < C_N[in]; j++)
  348. {
  349. if ( (1 + g_astro * Ca(in)) > 0 )
  350. {
  351. if (Ca(in) >= 0.3)
  352. {
  353. I_syn += g_syn * (1 + g_astro * Ca(in)) * (E_syn[(int)B_N[in][j]] - V(in)) / (1 + exp(-(V((int)B_N[in][j]) / k_syn)));
  354. }
  355. else
  356. {
  357. I_syn += g_syn * (E_syn[(int)B_N[in][j]] - V(in)) / (1 + exp(-(V((int)B_N[in][j]) / k_syn)));
  358. }
  359. }
  360. }
  361.  
  362. if (enable_I_syn_out)
  363. fprintf(fp_I_syn, i == Equations_count - 1 ? "%f" : "%f\t", I_syn);
  364.  
  365. 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(35, t)*/ + I_syn) / C_m); // V
  366. }
  367.  
  368. case 5: // m
  369. {
  370. return 1000 * (alpha_m(f, in) * (1 - m(in)) - beta_m(f, in) * m(in)); // m
  371. }
  372.  
  373. case 6: // n
  374. {
  375. return 1000 * (alpha_n(f, in) * (1 - n(in)) - beta_n(f, in) * n(in)); // n
  376. }
  377.  
  378. case 7: // h
  379. {
  380. return 1000 * (alpha_h(f, in) * (1 - h(in)) - beta_h(f, in) * h(in)); // h
  381. }
  382. }
  383.  
  384. return 0;
  385. }
  386.  
  387. void RungeKutta(double t, double dt, double* f, double* f_next)
  388. {
  389. double k[Equations_count][4];
  390.  
  391. // k1
  392. for (int i = 0; i < Equations_count; i++)
  393. k[i][0] = UllahJung_HodgkinHuxley(i, f, t) * dt;
  394.  
  395. double phi_k1[Equations_count];
  396. for (int i = 0; i < Equations_count; i++)
  397. phi_k1[i] = f[i] + k[i][0] / 2;
  398.  
  399. // k2
  400. for (int i = 0; i < Equations_count; i++)
  401. k[i][1] = UllahJung_HodgkinHuxley(i, phi_k1, t) * dt;
  402.  
  403. double phi_k2[Equations_count];
  404. for (int i = 0; i < Equations_count; i++)
  405. phi_k2[i] = f[i] + k[i][1] / 2;
  406.  
  407. // k3
  408. for (int i = 0; i < Equations_count; i++)
  409. k[i][2] = UllahJung_HodgkinHuxley(i, phi_k2, t) * dt;
  410.  
  411. double phi_k3[Equations_count];
  412. for (int i = 0; i < Equations_count; i++)
  413. phi_k3[i] = f[i] + k[i][2] / 2;
  414.  
  415. enable_I_syn_out = true;
  416. // k4
  417. for (int i = 0; i < Equations_count; i++)
  418. k[i][3] = UllahJung_HodgkinHuxley(i, phi_k3, t) * dt;
  419. enable_I_syn_out = false;
  420.  
  421. for (int i = 0; i < Equations_count; i++)
  422. f_next[i] = f[i] + (k[i][0] + 2 * k[i][1] + 2 * k[i][2] + k[i][3]) / 6;
  423. }
  424.  
  425. void CopyArray(double* source, double* target, int N)
  426. {
  427. for (int i = 0; i < N; i++)
  428. target[i] = source[i];
  429. }
  430.  
  431. bool Approximately(double a, double b)
  432. {
  433. if (a < 0)
  434. a = -a;
  435.  
  436. if (b < 0)
  437. b = -b;
  438.  
  439. return a - b <= 0.000001;
  440. }
  441.  
  442. bool CheckSameLine(int i, int j)
  443. {
  444. return i / Node_wire_width == j / Node_wire_width;
  445. }
  446.  
  447. bool IsWireNeighbors(int i, int j)
  448. {
  449. if (CheckSameLine(i, j) && (i == j - 1 || i == j + 1))
  450. return true;
  451.  
  452. if (i == j - Node_wire_width || i == j + Node_wire_width)
  453. return true;
  454.  
  455. return false;
  456. }
  457.  
  458. // http://preshing.com/20111007/how-to-generate-random-timings-for-a-poisson-process/
  459. double nextTime(double rateParameter)
  460. {
  461. return -log(1.0 - (double)rand() / (RAND_MAX)) / rateParameter;
  462. }
  463.  
  464. void GenerateRandomMeander(int i, double min_start_time)
  465. {
  466. double offset = nextTime(Freq);
  467.  
  468. if (offset < 0)
  469. {
  470. int a = 0;
  471. }
  472.  
  473. Meander_start_from_zero[i] = min_start_time + offset;
  474. Meander_width[i] = Duration;
  475. Meander_height[i] = RandomD(Min_magintude, Max_magintude);
  476. }
  477.  
  478. void FillAMatrixZero()
  479. {
  480. for (int i = 0; i < Node_count; i++)
  481. {
  482. for (int j = 0; j < Node_count; j++)
  483. {
  484. A_N[i][j] = 0;
  485. }
  486. }
  487. }
  488.  
  489. void FillWireMatrix()
  490. {
  491. for (int i = 0; i < Node_count; i++)
  492. {
  493. for (int j = 0; j < Node_count; j++)
  494. {
  495. if (i == j)
  496. {
  497. A_A[i][j] = 0;
  498. continue;
  499. }
  500.  
  501. if (i > j)
  502. {
  503. A_A[i][j] = A_A[j][i];
  504. continue;
  505. }
  506.  
  507. A_A[i][j] = IsWireNeighbors(i, j) ? 1 : 0;
  508. }
  509. }
  510. //A_A[0][1] = 0; // only for debug. diffusion Ca test
  511. //A_A[1][0] = 0; // only for debug. diffusion Ca test
  512. A_A[1][3] = 0;
  513. A_A[3][1] = 0;
  514. A_A[0][2] = 0;
  515. A_A[2][0] = 0;
  516. }
  517.  
  518. void FillBCMatrix_A()
  519. {
  520. for (int i = 0; i < Node_count; i++)
  521. {
  522. int bIndex = 0;
  523. C_A[i] = 0;
  524. for (int j = 0; j < Node_count; j++)
  525. {
  526. if (A_A[i][j] == 1)
  527. {
  528. B_A[i][bIndex] = j;
  529. bIndex++;
  530. C_A[i]++;
  531. }
  532. }
  533. }
  534. }
  535.  
  536. void FillBCMatrix_N()
  537. {
  538. for (int i = 0; i < Node_count; i++)
  539. {
  540. int bIndex = 0;
  541. C_N[i] = 0;
  542. for (int j = 0; j < Node_count; j++)
  543. {
  544. if (A_N[i][j] == 1)
  545. {
  546. B_N[i][bIndex] = j;
  547. bIndex++;
  548. C_N[i]++;
  549. }
  550. }
  551. }
  552. }
  553.  
  554. void FillRandomMatrix()
  555. {
  556. A_N[0][1] = 0;
  557. A_N[1][0] = 1;
  558.  
  559. A_N[0][2] = 0;
  560. A_N[2][0] = 0;
  561.  
  562. A_N[0][3] = 0;
  563. A_N[3][0] = 0;
  564.  
  565. A_N[1][3] = 0;
  566. A_N[3][1] = 0;
  567.  
  568. A_N[2][3] = 0;
  569. A_N[3][2] = 1;
  570. //double p_links = 0.2; // 0.2
  571. // //for (int k = 0; k < 2 * Node_count_half; k++)
  572. //for (int k = 0; k < p_links * Node_count * (Node_count - 1); k++)
  573. //{
  574. // int i, j;
  575.  
  576. // do
  577. // {
  578. // i = RandomI(0, Node_count);
  579. // j = RandomI(0, Node_count);
  580. // } while ((i == j) || (A_N[i][j] == 1));
  581.  
  582. // A_N[i][j] = 1;
  583. // //A[j][i] = 1;
  584. //}
  585. }
  586.  
  587. void FillVOldFromCurrent()
  588. {
  589. for (int i = 0; i < Node_count; i++)
  590. for (int j = 0; j < Max_delay; j++)
  591. V_old_array[i][j] = V(i);
  592. }
  593.  
  594. void UpdateVOld()
  595. {
  596. for (int i = 0; i < Node_count; i++)
  597. {
  598. for (int j = 1; j < Max_delay; j++)
  599. V_old_array[i][j - 1] = V_old_array[i][j];
  600.  
  601. V_old_array[i][Max_delay - 1] = V(i);
  602. }
  603. }
  604.  
  605. void FillFullTauMatrix()
  606. {
  607. for (int i = 0; i < Node_count; i++)
  608. {
  609. for (int j = 0; j < Node_count; j++)
  610. {
  611. if (i < Node_count || j < Node_count)
  612. {
  613. tau[i][j] = 0;
  614. continue;
  615. }
  616.  
  617. int i_neuron = i - Node_count;
  618. int j_neuron = j - Node_count;
  619.  
  620. int i_wire_x = i_neuron / Node_wire_width;
  621. int i_wire_y = i_neuron % Node_wire_width;
  622.  
  623. int j_wire_x = j_neuron / Node_wire_width;
  624. int j_wire_y = j_neuron % Node_wire_width;
  625.  
  626. double distance_max = sqrt(2.) * (Node_wire_width - 1);
  627. 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));
  628.  
  629. tau[i][j] = (tau_min + distance / (distance_max) * (tau_max - tau_min)) * ms_to_step;
  630. }
  631. }
  632. }
  633.  
  634. void FillTauMatrix()
  635. {
  636. for (int i = 0; i < Node_count; i++)
  637. {
  638. for (int j = 0; j < Node_count; j++)
  639. {
  640. if (i == j || A_N[i][j] == 0)
  641. {
  642. tau[i][j] = 0;
  643. continue;
  644. }
  645.  
  646. int i_neuron = i;
  647. int j_neuron = j;
  648.  
  649. int i_wire_x = i_neuron / Node_wire_width;
  650. int i_wire_y = i_neuron % Node_wire_width;
  651.  
  652. int j_wire_x = j_neuron / Node_wire_width;
  653. int j_wire_y = j_neuron % Node_wire_width;
  654.  
  655. double distance_max = sqrt(2.) * (Node_wire_width - 1);
  656. 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));
  657.  
  658. double t = (distance - 1) / (distance_max - 1);
  659. tau[i][j] = (tau_min + t * (tau_max - tau_min)) * ms_to_step;
  660. }
  661. }
  662. }
  663.  
  664. int main(int argc, char *argv[])
  665. {
  666. sscanf(argv[1], "%lf", &g_syn);
  667. FILE *fp_g_syn;
  668. fp_g_syn = fopen("g_syn.txt", "w");
  669. fprintf(fp_g_syn, "%f\t", g_syn);
  670. fclose(fp_g_syn);
  671.  
  672. double g_syn_real;
  673. g_syn_real = /*1 / (0.2 * Node_count) */ g_syn;
  674. printf("g_syn_real = %f\n", g_syn_real);
  675.  
  676. sscanf(argv[2], "%lf", &g_astro);
  677. FILE *fp_g_astro;
  678. fp_g_astro = fopen("g_astro.txt", "w");
  679. fprintf(fp_g_astro, "%f\t", g_astro);
  680. fclose(fp_g_astro);
  681. printf("g_astro = %f\n", g_astro);
  682.  
  683. sscanf(argv[3], "%lf", &I_app_1);
  684. sscanf(argv[4], "%lf", &I_app_2);
  685. sscanf(argv[5], "%lf", &I_app_3);
  686. sscanf(argv[6], "%lf", &I_app_4);
  687.  
  688. FILE *fp_I_app;
  689. fp_I_app = fopen("I_app.txt", "w");
  690. fprintf(fp_I_app, "%f\n", I_app_1);
  691. fprintf(fp_I_app, "%f\n", I_app_2);
  692. fprintf(fp_I_app, "%f\n", I_app_3);
  693. fprintf(fp_I_app, "%f\n", I_app_4);
  694. fclose(fp_I_app);
  695.  
  696. FILE *fp0;
  697. FILE *fp_I_stim;
  698. FILE *fp_Ca;
  699. FILE *fp_IP3;
  700. //FILE *fp_z;
  701. FILE *fp_G;
  702. FILE *fp_V;
  703. //FILE *fp_m;
  704. //FILE *fp_n;
  705. //FILE *fp_h;
  706. //FILE *fp_V_spikes;
  707.  
  708. FILE *fp_Esyn;
  709. srand(time(NULL));
  710.  
  711. //for (int i = 0; i < Node_count; i++)
  712. // V_old_length[i] = 0;
  713.  
  714. A_A = malloc(Node_count * sizeof(double));
  715. for (int i = 0; i < Node_count; i++)
  716. A_A[i] = malloc(Node_count * sizeof(double));
  717.  
  718. B_A = malloc(Node_count * sizeof(double));
  719. for (int i = 0; i < Node_count; i++)
  720. B_A[i] = malloc(Node_count * sizeof(double));
  721.  
  722. C_A = malloc(Node_count * sizeof(double));
  723.  
  724. A_N = malloc(Node_count * sizeof(double));
  725. for (int i = 0; i < Node_count; i++)
  726. A_N[i] = malloc(Node_count * sizeof(double));
  727.  
  728. B_N = malloc(Node_count * sizeof(double));
  729. for (int i = 0; i < Node_count; i++)
  730. B_N[i] = malloc(Node_count * sizeof(double));
  731.  
  732. C_N = malloc(Node_count * sizeof(double));
  733.  
  734. FillAMatrixZero();
  735. FillWireMatrix();
  736. FillRandomMatrix();
  737. FillBCMatrix_A();
  738. FillBCMatrix_N();
  739. FillTauMatrix();
  740.  
  741. fp0 = fopen("A_A.txt", "w+");
  742. for (int i = 0; i < Node_count; i++)
  743. {
  744. for (int j = 0; j < Node_count; j++)
  745. {
  746. fprintf(fp0, "%d\t", (int)A_A[i][j]);
  747. }
  748. fprintf(fp0, "\n");
  749. }
  750. fclose(fp0);
  751.  
  752. fp0 = fopen("A_N.txt", "w+");
  753. for (int i = 0; i < Node_count; i++)
  754. {
  755. for (int j = 0; j < Node_count; j++)
  756. {
  757. fprintf(fp0, "%d\t", (int)A_N[i][j]);
  758. }
  759. fprintf(fp0, "\n");
  760. }
  761. fclose(fp0);
  762.  
  763. fp0 = fopen("tau.txt", "w+");
  764. for (int i = 0; i < Node_count; i++)
  765. {
  766. for (int j = 0; j < Node_count; j++)
  767. {
  768. fprintf(fp0, "%f\t", tau[i][j] / ms_to_step);
  769. }
  770. fprintf(fp0, "\n");
  771. }
  772. fclose(fp0);
  773.  
  774. //����� � ���� ����� ������ � ������� �������
  775. fp0 = fopen("links.txt", "w+");
  776. for (int i = 0; i < Node_count; i++)
  777. {
  778. int links_count = 0;
  779. for (int j = 0; j < Node_count; j++)
  780. {
  781. if (A_N[i][j] == 1)
  782. {
  783. links_count++;
  784. }
  785. }
  786. fprintf(fp0, "%d\n", (int)links_count);
  787. }
  788. fclose(fp0);
  789.  
  790. //setlocale(LC_NUMERIC, "French_Canada.1252");
  791. fp0 = fopen("test_Poisson.txt", "w+");
  792. for (int i = 0; i < 1000; i++)
  793. fprintf(fp0, "%f\n", nextTime(Freq));
  794. fclose(fp0);
  795.  
  796. fp0 = fopen("B_A.txt", "w+");
  797. for (int i = 0; i < Node_count; i++)
  798. {
  799. for (int j = 0; j < C_A[i]; j++)
  800. {
  801. fprintf(fp0, "%d\t", (int)B_A[i][j]);
  802. }
  803. fprintf(fp0, "\n");
  804. }
  805. fclose(fp0);
  806.  
  807. fp0 = fopen("B_N.txt", "w+");
  808. for (int i = 0; i < Node_count; i++)
  809. {
  810. for (int j = 0; j < C_N[i]; j++)
  811. {
  812. fprintf(fp0, "%d\t", (int)B_N[i][j]);
  813. }
  814. fprintf(fp0, "\n");
  815. }
  816. fclose(fp0);
  817.  
  818. fp0 = fopen("C_A.txt", "w+");
  819. for (int i = 0; i < Node_count; i++)
  820. {
  821. fprintf(fp0, "%d\n", (int)C_A[i]);
  822. }
  823. fclose(fp0);
  824.  
  825. fp0 = fopen("C_N.txt", "w+");
  826. for (int i = 0; i < Node_count; i++)
  827. {
  828. fprintf(fp0, "%d\n", (int)C_N[i]);
  829. }
  830. fclose(fp0);
  831.  
  832. /*for (int i = 0; i < 6; i++)
  833. {
  834. v_4[i] = 0.6;
  835. }*/
  836. for (int i = 0; i < Node_count; i++)
  837. {
  838. v_4[i] = 0.4; // 0.4
  839. }
  840.  
  841. // Initial values
  842. /*for (int i = 0; i < Equations_count; i++)
  843. {
  844. f[i] = 0;
  845. }
  846.  
  847. for (int i = 0; i < Equations_count; i++)
  848. {
  849. I_app[i] = RandomD(9, 40);
  850. }*/
  851.  
  852. for (int i = 0; i < Node_count; i++) // init array for all nodes
  853. {
  854. SetG(i, 0); // G
  855. }
  856.  
  857. double percent_stable_state = 0.4; // 0.40
  858. double eps_persent = 0.05; //0.05
  859.  
  860. double Ca0 = 0.07;
  861. double IP30 = 0.16;
  862. double z0 = 0.67;
  863.  
  864. // Initial values at t = 0
  865. /*for (int i = 0; i < Node_count; i++)
  866. {
  867. SetCa(i, Ca0); // Ca
  868. SetIP3(i, IP30); // IP3
  869. Setz(i, z0); // z
  870. }*/
  871. for (int i = 0; i < Node_count; i++)
  872. {
  873. /*SetCa(i, Ca0 + RandomD(-Ca0 * eps_persent, Ca0 * eps_persent)); // Ca
  874. SetIP3(i, IP30 + RandomD(-IP30 * eps_persent, IP30 * eps_persent)); // IP3
  875. Setz(i, z0 + RandomD(-z0 * eps_persent, z0 * eps_persent)); // z */
  876. SetCa(i, Ca0); // Ca
  877. SetIP3(i, IP30); // IP3
  878. Setz(i, z0); // z
  879. }
  880.  
  881. /*for (int i = 0; i < Node_count; i++) // init array for all nodes
  882. {
  883. SetV(i, V1); // V
  884. Setm(i, m1); // m
  885. Setn(i, n1); // n
  886. Seth(i, h1); // h
  887. }*/
  888.  
  889. double V0 = -58.7085;
  890. double m0 = 0.0953;
  891. double n0 = 0.000913;
  892. double h0 = 0.3662;
  893.  
  894. double V1 = 14.8409;
  895. double m1 = 0.9174;
  896. double n1 = 0.0140;
  897. double h1 = 0.0539;
  898.  
  899. /*for (int i = 0; i < Node_count; i++) // init only for neurons
  900. {
  901. double random = RandomD(0, 1);
  902.  
  903. SetV(i, random < percent_stable_state ? V0 + RandomD(-V0 * eps_persent, V0 * eps_persent) : V1 + RandomD(-V1 * eps_persent, V1 * eps_persent)); // V
  904. Setm(i, random < percent_stable_state ? m0 + RandomD(-m0 * eps_persent, m0 * eps_persent) : m1 + RandomD(-m1 * eps_persent, m1 * eps_persent)); // m
  905. Setn(i, random < percent_stable_state ? n0 + RandomD(-n0 * eps_persent, n0 * eps_persent) : n1 + RandomD(-n1 * eps_persent, n1 * eps_persent)); // n
  906. Seth(i, random < percent_stable_state ? h0 + RandomD(-h0 * eps_persent, h0 * eps_persent) : h1 + RandomD(-h1 * eps_persent, h1 * eps_persent)); // h
  907. }*/
  908.  
  909. for (int i = 0; i < Node_count; i++) // init only for neurons
  910. {
  911. /*SetV(i, RandomD(-80, 20)); // V
  912. Setm(i, RandomD(0, 1)); // m
  913. Setn(i, RandomD(0, 1)); // n
  914. Seth(i, RandomD(0, 1)); // h*/
  915. SetV(i, V0); // V
  916. Setm(i, m0); // m
  917. Setn(i, n0); // n
  918. Seth(i, h0); // h
  919. }
  920.  
  921. for (int i = 0; i < Node_count; i++)
  922. {
  923. I_app[i] = 1.0; // init for neurons; Bifurcation point: I_app = 0.82; I_app_up = 1.04 // 0.81 or 0.93 or 1.05
  924. }
  925. //I_app[0] = 1.20;
  926. //I_app[1] = 1.40;
  927. I_app[0] = I_app_1;
  928. I_app[1] = I_app_2;
  929. I_app[2] = I_app_3;
  930. I_app[3] = I_app_4;
  931.  
  932. double percent_excitable = 0.8; // 0.8
  933.  
  934. double E_syn0 = 0; // Excitatory neuron
  935. double E_syn1 = -90; // Inhibitory neuron
  936.  
  937. fp_Esyn = fopen("results_E_syn.txt", "w+");
  938. E_syn[0] = E_syn1;
  939. E_syn[1] = E_syn0;
  940. E_syn[2] = E_syn0;
  941. E_syn[3] = E_syn0;
  942. for (int i = 0; i < Node_count; i++)
  943. {
  944. //double random = RandomD(0, 1);
  945.  
  946. //E_syn[i] = random < percent_excitable ? E_syn0 : E_syn1;
  947. //E_syn[i] = E_syn0;
  948. fprintf(fp_Esyn, "%f\n", E_syn[i]);
  949. /*printf("i = %d\t E_syn = %f\n", i, E_syn[i]);*/
  950. }
  951. fclose(fp_Esyn);
  952.  
  953. for (int i = 0; i < Node_count; i++)
  954. {
  955. GenerateRandomMeander(i, 0);
  956. last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  957. }
  958.  
  959. const double t_start = 0;
  960. const double t_max = 240.0; // 100 msec = 0.1 sec // 240 // 270
  961. const double dt = 0.00005; // 0.01 msec = 0.00001 sec; 0.1 msec = 0.0001 sec; 1 msec = 0.001 sec
  962.  
  963. double t = t_start;
  964.  
  965. //fp0 = fopen("results.txt", "w+");
  966. //setlocale(LC_NUMERIC, "French_Canada.1252");
  967.  
  968. clock_t start_rk4, end_rk4;
  969. start_rk4 = clock();
  970. int lastPercent = -1;
  971.  
  972. FillVOldFromCurrent();
  973.  
  974. fp_I_stim = fopen("results_I_stim.txt", "w+");
  975. fp_I_syn = fopen("results_I_syn.txt", "w+");
  976. fp_Ca = fopen("results_Ca.txt", "w+");
  977. fp_IP3 = fopen("results_IP3.txt", "w+");
  978. //fp_z = fopen("results_z.txt", "w+");
  979. fp_G = fopen("results_G.txt", "w+");
  980. fp_V = fopen("results_V.txt", "w+");
  981. //fp_m = fopen("results_m.txt", "w+");
  982. //fp_n = fopen("results_n.txt", "w+");
  983. //fp_h = fopen("results_h.txt", "w+");
  984. //fp_V_spikes = fopen("results_V_spikes.txt", "w+");
  985.  
  986. while (t < t_max || Approximately(t, t_max))
  987. {
  988. fprintf(fp_I_stim, "%f\t", t);
  989. fprintf(fp_Ca, "%f\t", t);
  990. fprintf(fp_IP3, "%f\t", t);
  991. //fprintf(fp_z, "%f\t", t);
  992. fprintf(fp_G, "%f\t", t);
  993. fprintf(fp_V, "%f\t", t);
  994. //fprintf(fp_m, "%f\t", t);
  995. //fprintf(fp_n, "%f\t", t);
  996. //fprintf(fp_h, "%f\t", t);
  997. //fprintf(fp_V_spikes, "%f\t", t);
  998.  
  999. for (int i = 0; i < Node_count; i++)
  1000. {
  1001. if (t > last_meander_end[i])
  1002. {
  1003. GenerateRandomMeander(i, t);
  1004. last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  1005. }
  1006.  
  1007. fprintf(fp_I_stim, "%f\t", I_stim(i, t));
  1008. }
  1009. fprintf(fp_I_stim, "\n");
  1010.  
  1011. for (int i = 0; i < Equations_count; i += Equations_per_node)
  1012. fprintf(fp_Ca, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // Ca
  1013.  
  1014. for (int i = 1; i < Equations_count; i += Equations_per_node)
  1015. fprintf(fp_IP3, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // IP3
  1016.  
  1017. //for (int i = 2; i < Equations_count; i += Equations_per_node)
  1018. // fprintf(fp_z, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // z
  1019.  
  1020. for (int i = 3; i < Equations_count; i += Equations_per_node)
  1021. fprintf(fp_G, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // G
  1022.  
  1023. for (int i = 4; i < Equations_count; i += Equations_per_node)
  1024. fprintf(fp_V, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // V
  1025.  
  1026. //for (int i = 5; i < Equations_count; i += Equations_per_node)
  1027. // fprintf(fp_m, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // m
  1028.  
  1029. //for (int i = 6; i < Equations_count; i += Equations_per_node)
  1030. // fprintf(fp_n, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // n
  1031.  
  1032. //for (int i = 7; i < Equations_count; i += Equations_per_node)
  1033. // fprintf(fp_h, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // h
  1034.  
  1035. fprintf(fp_Ca, "\n");
  1036. fprintf(fp_IP3, "\n");
  1037. //fprintf(fp_z, "\n");
  1038. fprintf(fp_G, "\n");
  1039. fprintf(fp_V, "\n");
  1040. //fprintf(fp_m, "\n");
  1041. //fprintf(fp_n, "\n");
  1042. //fprintf(fp_h, "\n");
  1043.  
  1044.  
  1045. double f_next[Equations_count];
  1046.  
  1047. RungeKutta(t, dt, f, f_next);
  1048.  
  1049. /*for (int i = 0; i < Equations_count; i += Equations_per_node)
  1050. {
  1051. double diff = f_next[i] - f[i];
  1052.  
  1053. fprintf(fp_V_spikes, i == Equations_count - 1 ? "%d" : "%d\t", diff < 0 && f_diff[i] > 0 && f[i] > -10 ? 1 : 0);
  1054.  
  1055. f_diff[i] = diff;
  1056. }*/
  1057.  
  1058. //fprintf(fp_V_spikes, "\n");
  1059.  
  1060. CopyArray(f_next, f, Equations_count);
  1061.  
  1062. t += dt;
  1063.  
  1064. int percent = (int)(100 * (t - t_start) / (t_max - t_start));
  1065. if (percent != lastPercent)
  1066. {
  1067. printf("Progress: %d%%\n", percent);
  1068. lastPercent = percent;
  1069. }
  1070.  
  1071. //printf("V(24) = %f\t V_old(24) = %f\n", f[24*4], V_old(24));
  1072. UpdateVOld();
  1073.  
  1074. fprintf(fp_I_syn, "\n");
  1075. }
  1076.  
  1077. fclose(fp_I_stim);
  1078. fclose(fp_I_syn);
  1079. fclose(fp_Ca);
  1080. fclose(fp_IP3);
  1081. //fclose(fp_z);
  1082. fclose(fp_G);
  1083. fclose(fp_V);
  1084. //fclose(fp_m);
  1085. //fclose(fp_n);
  1086. //fclose(fp_h);
  1087. //fclose(fp_V_spikes);
  1088.  
  1089. end_rk4 = clock();
  1090. double extime_rk4 = (double)(end_rk4 - start_rk4) / CLOCKS_PER_SEC;
  1091. int minutes = (int)extime_rk4 / 60;
  1092. int seconds = (int)extime_rk4 % 60;
  1093. printf("\nExecution time is: %d minutes %d seconds\n ", minutes, seconds);
  1094.  
  1095. fp0 = fopen("time_exec.txt", "w+");
  1096. fprintf(fp0, "%f\n", extime_rk4);
  1097. fclose(fp0);
  1098.  
  1099. for (int i = 0; i < Node_count; i++)
  1100. free(A_A[i]);
  1101. free(A_A);
  1102.  
  1103. for (int i = 0; i < Node_count; i++)
  1104. free(B_A[i]);
  1105. free(B_A);
  1106.  
  1107. free(C_A);
  1108.  
  1109. for (int i = 0; i < Node_count; i++)
  1110. free(A_N[i]);
  1111. free(A_N);
  1112.  
  1113. for (int i = 0; i < Node_count; i++)
  1114. free(B_N[i]);
  1115. free(B_N);
  1116.  
  1117. free(C_N);
  1118. }
  1119.  
Add Comment
Please, Sign In to add comment