SpaceQuester

Untitled

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