SpaceQuester

Untitled

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