SpaceQuester

Untitled

Sep 4th, 2018
398
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.84 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*1
  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 = 2; // 10
  41. double g_astro = 3; // 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 = 0.2; // 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.2) / 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) * A_N[in][j] * g_syn * (1 + g_astro * Ca(in)) * (E_syn[j] - V(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 FillA_N()
  462. {
  463. A_N[0][0] = 0;
  464. A_N[0][1] = 1;
  465. A_N[1][0] = 1;
  466. A_N[1][1] = 0;
  467. }
  468.  
  469. int main(int argc, char *argv[])
  470. {
  471. sscanf(argv[1], "%lf", &g_syn);
  472.  
  473. FILE *fp_g_syn;
  474. fp_g_syn = fopen("g_syn.txt", "w");
  475. fprintf(fp_g_syn, "%f\t", g_syn);
  476. fclose(fp_g_syn);
  477.  
  478. double g_syn_real;
  479. g_syn_real = 1 / (0.2 * Node_count) * g_syn;
  480. printf("g_syn_real = %f\n", g_syn_real);
  481.  
  482. FILE *fp0;
  483. FILE *fp_I_stim;
  484. FILE *fp_Ca;
  485. FILE *fp_IP3;
  486. //FILE *fp_z;
  487. FILE *fp_G;
  488. FILE *fp_V;
  489. //FILE *fp_m;
  490. //FILE *fp_n;
  491. //FILE *fp_h;
  492. //FILE *fp_V_spikes;
  493. FILE *fp_Esyn;
  494. srand(time(NULL));
  495.  
  496. //for (int i = 0; i < Node_count; i++)
  497. // V_old_length[i] = 0;
  498.  
  499. A_A = malloc(Node_count * sizeof(double));
  500. for (int i = 0; i < Node_count; i++)
  501. A_A[i] = malloc(Node_count * sizeof(double));
  502.  
  503. B_A = malloc(Node_count * sizeof(double));
  504. for (int i = 0; i < Node_count; i++)
  505. B_A[i] = malloc(Node_count * sizeof(double));
  506.  
  507. C_A = malloc(Node_count * sizeof(double));
  508.  
  509. A_N = malloc(Node_count * sizeof(double));
  510. for (int i = 0; i < Node_count; i++)
  511. A_N[i] = malloc(Node_count * sizeof(double));
  512.  
  513. B_N = malloc(Node_count * sizeof(double));
  514. for (int i = 0; i < Node_count; i++)
  515. B_N[i] = malloc(Node_count * sizeof(double));
  516.  
  517. C_N = malloc(Node_count * sizeof(double));
  518.  
  519. FillA_N();
  520.  
  521. /*fp0 = fopen("tau.txt", "w+");
  522. for (int i = 0; i < Node_count; i++)
  523. {
  524. for (int j = 0; j < Node_count; j++)
  525. {
  526. fprintf(fp0, "%f\t", tau[i][j] / ms_to_step);
  527. }
  528. fprintf(fp0, "\n");
  529. }
  530. fclose(fp0);*/
  531.  
  532. //setlocale(LC_NUMERIC, "French_Canada.1252");
  533. fp0 = fopen("test_Poisson.txt", "w+");
  534. for (int i = 0; i < 1000; i++)
  535. fprintf(fp0, "%f\n", nextTime(Freq));
  536. fclose(fp0);
  537.  
  538. /*for (int i = 0; i < 6; i++)
  539. {
  540. v_4[i] = 0.6;
  541. }*/
  542. for (int i = 0; i < Node_count; i++)
  543. {
  544. v_4[i] = 0.6; // 0.4
  545. }
  546.  
  547. // Initial values
  548. /*for (int i = 0; i < Equations_count; i++)
  549. {
  550. f[i] = 0;
  551. }
  552.  
  553. for (int i = 0; i < Equations_count; i++)
  554. {
  555. I_app[i] = RandomD(9, 40);
  556. }*/
  557.  
  558. for (int i = 0; i < Node_count; i++) // init array for all nodes
  559. {
  560. SetG(i, 0); // G
  561. }
  562.  
  563. //double percent_stable_state = 0.4; // 0.40
  564. double eps_persent = 0.05; //0.05
  565.  
  566. double Ca0 = 0.07;
  567. double IP30 = 0.16;
  568. double z0 = 0.67;
  569.  
  570. // Initial values at t = 0
  571. /*for (int i = 0; i < Node_count; i++)
  572. {
  573. SetCa(i, Ca0); // Ca
  574. SetIP3(i, IP30); // IP3
  575. Setz(i, z0); // z
  576. }*/
  577. for (int i = 0; i < Node_count; i++)
  578. {
  579. SetCa(i, Ca0 + RandomD(-Ca0 * eps_persent, Ca0 * eps_persent)); // Ca
  580. SetIP3(i, IP30 + RandomD(-IP30 * eps_persent, IP30 * eps_persent)); // IP3
  581. Setz(i, z0 + RandomD(-z0 * eps_persent, z0 * eps_persent)); // z
  582. }
  583.  
  584. /*for (int i = 0; i < Node_count; i++) // init array for all nodes
  585. {
  586. SetV(i, V1); // V
  587. Setm(i, m1); // m
  588. Setn(i, n1); // n
  589. Seth(i, h1); // h
  590. }*/
  591.  
  592. /*double V0 = -58.7085;
  593. double m0 = 0.0953;
  594. double n0 = 0.000913;
  595. double h0 = 0.3662;
  596.  
  597. double V1 = 14.8409;
  598. double m1 = 0.9174;
  599. double n1 = 0.0140;
  600. double h1 = 0.0539;
  601.  
  602. for (int i = 0; i < Node_count; i++) // init only for neurons
  603. {
  604. double random = RandomD(0, 1);
  605.  
  606. SetV(i, random < percent_stable_state ? V0 + RandomD(-V0 * eps_persent, V0 * eps_persent) : V1 + RandomD(-V1 * eps_persent, V1 * eps_persent)); // V
  607. Setm(i, random < percent_stable_state ? m0 + RandomD(-m0 * eps_persent, m0 * eps_persent) : m1 + RandomD(-m1 * eps_persent, m1 * eps_persent)); // m
  608. Setn(i, random < percent_stable_state ? n0 + RandomD(-n0 * eps_persent, n0 * eps_persent) : n1 + RandomD(-n1 * eps_persent, n1 * eps_persent)); // n
  609. Seth(i, random < percent_stable_state ? h0 + RandomD(-h0 * eps_persent, h0 * eps_persent) : h1 + RandomD(-h1 * eps_persent, h1 * eps_persent)); // h
  610. }*/
  611.  
  612. for (int i = 0; i < Node_count; i++) // init only for neurons
  613. {
  614. SetV(i, RandomD(-80, 20)); // V
  615. Setm(i, RandomD(0, 1)); // m
  616. Setn(i, RandomD(0, 1)); // n
  617. Seth(i, RandomD(0, 1)); // h
  618. }
  619.  
  620. for (int i = 0; i < Node_count; i++)
  621. {
  622. I_app[i] = 1.05; // init for neurons; Bifurcation point: I_app = 0.82; I_app_up = 1.04 // 0.81 or 0.93 or 1.05
  623. }
  624.  
  625. /*double percent_excitable = 0.8; // 0.8
  626.  
  627. double E_syn0 = 0;
  628. double E_syn1 = -90;
  629.  
  630. fp_Esyn = fopen("results_Esyn.txt", "w+");
  631. for (int i = 0; i < Node_count; i++)
  632. {
  633. double random = RandomD(0, 1);
  634.  
  635. E_syn[i] = random < percent_excitable ? E_syn0 : E_syn1;
  636. fprintf(fp_Esyn, "%f\n", E_syn[i]);
  637. //printf("i = %d\t E_syn = %f\n", i, E_syn[i]);
  638. }
  639. fclose(fp_Esyn);*/
  640.  
  641. E_syn[0] = 0;
  642. E_syn[1] = 0;
  643.  
  644. for (int i = 0; i < Node_count; i++)
  645. {
  646. GenerateRandomMeander(i, 0);
  647. last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  648. }
  649.  
  650. const double t_start = 0;
  651. const double t_max = 240.0; // 100 msec = 0.1 sec // 240
  652. const double dt = 0.000025; // 0.01 msec = 0.00001 sec; 0.1 msec = 0.0001 sec; 1 msec = 0.001 sec
  653.  
  654. double t = t_start;
  655.  
  656. //fp0 = fopen("results.txt", "w+");
  657. //setlocale(LC_NUMERIC, "French_Canada.1252");
  658.  
  659. clock_t start_rk4, end_rk4;
  660. start_rk4 = clock();
  661. int lastPercent = -1;
  662.  
  663. //FillVOldFromCurrent();
  664.  
  665. fp_I_stim = fopen("results_I_stim.txt", "w+");
  666. fp_Ca = fopen("results_Ca.txt", "w+");
  667. fp_IP3 = fopen("results_IP3.txt", "w+");
  668. //fp_z = fopen("results_z.txt", "w+");
  669. fp_G = fopen("results_G.txt", "w+");
  670. fp_V = fopen("results_V.txt", "w+");
  671. //fp_m = fopen("results_m.txt", "w+");
  672. //fp_n = fopen("results_n.txt", "w+");
  673. //fp_h = fopen("results_h.txt", "w+");
  674. //fp_V_spikes = fopen("results_V_spikes.txt", "w+");
  675.  
  676. while (t < t_max || Approximately(t, t_max))
  677. {
  678. fprintf(fp_I_stim, "%f\t", t);
  679. fprintf(fp_Ca, "%f\t", t);
  680. fprintf(fp_IP3, "%f\t", t);
  681. //fprintf(fp_z, "%f\t", t);
  682. fprintf(fp_G, "%f\t", t);
  683. fprintf(fp_V, "%f\t", t);
  684. //fprintf(fp_m, "%f\t", t);
  685. //fprintf(fp_n, "%f\t", t);
  686. //fprintf(fp_h, "%f\t", t);
  687. //fprintf(fp_V_spikes, "%f\t", t);
  688.  
  689. for (int i = 0; i < Node_count; i++)
  690. {
  691. if (t > last_meander_end[i])
  692. {
  693. GenerateRandomMeander(i, t);
  694. last_meander_end[i] = Meander_start_from_zero[i] + Duration;
  695. }
  696.  
  697. fprintf(fp_I_stim, "%f\t", I_stim(i, t));
  698. }
  699. fprintf(fp_I_stim, "\n");
  700.  
  701. for (int i = 0; i < Equations_count; i += Equations_per_node)
  702. fprintf(fp_Ca, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // Ca
  703.  
  704. for (int i = 1; i < Equations_count; i += Equations_per_node)
  705. fprintf(fp_IP3, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // IP3
  706.  
  707. //for (int i = 2; i < Equations_count; i += Equations_per_node)
  708. // fprintf(fp_z, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // z
  709.  
  710. for (int i = 3; i < Equations_count; i += Equations_per_node)
  711. fprintf(fp_G, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // G
  712.  
  713. for (int i = 4; i < Equations_count; i += Equations_per_node)
  714. fprintf(fp_V, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // V
  715.  
  716. //for (int i = 5; i < Equations_count; i += Equations_per_node)
  717. // fprintf(fp_m, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // m
  718.  
  719. //for (int i = 6; i < Equations_count; i += Equations_per_node)
  720. // fprintf(fp_n, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // n
  721.  
  722. //for (int i = 7; i < Equations_count; i += Equations_per_node)
  723. // fprintf(fp_h, i == Equations_count - 1 ? "%f" : "%f\t", f[i]); // h
  724.  
  725. fprintf(fp_Ca, "\n");
  726. fprintf(fp_IP3, "\n");
  727. //fprintf(fp_z, "\n");
  728. fprintf(fp_G, "\n");
  729. fprintf(fp_V, "\n");
  730. //fprintf(fp_m, "\n");
  731. //fprintf(fp_n, "\n");
  732. //fprintf(fp_h, "\n");
  733.  
  734. double f_next[Equations_count];
  735.  
  736. RungeKutta(t, dt, f, f_next);
  737.  
  738. /*for (int i = 0; i < Equations_count; i += Equations_per_node)
  739. {
  740. double diff = f_next[i] - f[i];
  741.  
  742. fprintf(fp_V_spikes, i == Equations_count - 1 ? "%d" : "%d\t", diff < 0 && f_diff[i] > 0 && f[i] > -10 ? 1 : 0);
  743.  
  744. f_diff[i] = diff;
  745. }*/
  746.  
  747. //fprintf(fp_V_spikes, "\n");
  748.  
  749. CopyArray(f_next, f, Equations_count);
  750.  
  751. t += dt;
  752.  
  753. int percent = (int)(100 * (t - t_start) / (t_max - t_start));
  754. if (percent != lastPercent)
  755. {
  756. printf("Progress: %d%%\n", percent);
  757. lastPercent = percent;
  758. }
  759.  
  760. //printf("V(24) = %f\t V_old(24) = %f\n", f[24*4], V_old(24));
  761. //UpdateVOld();
  762. }
  763.  
  764. fclose(fp_I_stim);
  765. fclose(fp_Ca);
  766. fclose(fp_IP3);
  767. //fclose(fp_z);
  768. fclose(fp_G);
  769. fclose(fp_V);
  770. //fclose(fp_m);
  771. //fclose(fp_n);
  772. //fclose(fp_h);
  773. //fclose(fp_V_spikes);
  774.  
  775. end_rk4 = clock();
  776. double extime_rk4 = (double)(end_rk4 - start_rk4) / CLOCKS_PER_SEC;
  777. int minutes = (int)extime_rk4 / 60;
  778. int seconds = (int)extime_rk4 % 60;
  779. printf("\nExecution time is: %d minutes %d seconds\n ", minutes, seconds);
  780.  
  781. fp0 = fopen("time_exec.txt", "w+");
  782. fprintf(fp0, "%f\n", extime_rk4);
  783. fclose(fp0);
  784.  
  785. for (int i = 0; i < Node_count; i++)
  786. free(A_A[i]);
  787. free(A_A);
  788.  
  789. for (int i = 0; i < Node_count; i++)
  790. free(B_A[i]);
  791. free(B_A);
  792.  
  793. free(C_A);
  794.  
  795. for (int i = 0; i < Node_count; i++)
  796. free(A_N[i]);
  797. free(A_N);
  798.  
  799. for (int i = 0; i < Node_count; i++)
  800. free(B_N[i]);
  801. free(B_N);
  802.  
  803. free(C_N);
  804. }
Advertisement
Add Comment
Please, Sign In to add comment