SpaceQuester

Untitled

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