Advertisement
SpaceQuester

Untitled

Jun 29th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 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 Neuron_count 20
  10. #define Neuron_count_half Neuron_count / 2
  11.  
  12. #define Equations_per_neuron 4
  13. #define Equations_count Neuron_count * Equations_per_neuron
  14.  
  15. double f[Equations_count];
  16.  
  17. double C_m = 1;
  18.  
  19. double g_K = 36;
  20. double g_Na = 120;
  21. double g_L = 0.3;
  22.  
  23. double E_K = -77;
  24. double E_Na = 55;
  25. double E_L = -54.4;
  26.  
  27. double I_app[Equations_count];
  28.  
  29. const double sigma_G = 0.3;
  30. const double sigma_GN = 0.5;
  31. const double sigma_N = 0.0;
  32.  
  33. double** A;
  34. double** B;
  35. double* C;
  36. double** sigma;
  37.  
  38. double V(int i)
  39. {
  40. return f[i * 4];
  41. }
  42.  
  43. double m(int i)
  44. {
  45. return f[i * 4 + 1];
  46. }
  47.  
  48. double n(int i)
  49. {
  50. return f[i * 4 + 2];
  51. }
  52.  
  53. double h(int i)
  54. {
  55. return f[i * 4 + 3];
  56. }
  57.  
  58. int RandomI(int min, int max)
  59. {
  60. return ((double)rand() / (RAND_MAX - 1)) * (max - min) + min;
  61. }
  62.  
  63. double RandomD(double min, double max)
  64. {
  65. return ((double)rand() / RAND_MAX) * (max - min) + min;
  66. }
  67.  
  68. double alpha_n(double* f, int i)
  69. {
  70. return 0.01 * (V(i) + 55) / (1 - exp(-(V(i) + 55) / 10));
  71. }
  72.  
  73. double beta_n(double* f, int i)
  74. {
  75. return 0.125 * exp(-(V(i) + 65) / 80);
  76. }
  77.  
  78. double alpha_m(double* f, int i)
  79. {
  80. return 0.1 * (V(i) + 40) / (1 - exp(-(V(i) + 40) / 10));
  81. }
  82.  
  83. double beta_m(double* f, int i)
  84. {
  85. return 4 * exp(-(V(i) + 65) / 18);
  86. }
  87.  
  88. double alpha_h(double* f, int i)
  89. {
  90. return 0.07 * exp(-(V(i) + 65) / 20);
  91. }
  92.  
  93. double beta_h(double* f, int i)
  94. {
  95. return 1 / (exp(-(V(i) + 35) / 10) + 1);
  96. }
  97.  
  98. double HodgkinHuxley(int i, double* f)
  99. {
  100. int in = i / 4;
  101. int il = i % 4;
  102.  
  103. switch (il)
  104. {
  105. case 0:
  106. {
  107. double sum = 0;
  108.  
  109. /*for (int j = 0; j < Neuron_count; j++)
  110. {
  111. sum += sigma[in][j] * A[in][j] * (V(j) - V(in));
  112. }*/
  113.  
  114. for (int j = 0; j < C[in]; j++)
  115. {
  116. sum += sigma[in][(int)B[in][j]] * (V((int)B[in][j]) - V(in));
  117. }
  118.  
  119. return (g_Na * m(il) * m(il) * m(il) * h(il) * (E_Na - V(il)) + g_K * n(il) * n(il) * n(il) * n(il) * (E_K - V(il)) + g_L * (E_L - V(il)) + I_app[il] + sum) / C_m;
  120. }
  121.  
  122. case 1:
  123. return alpha_m(f, il) * (1 - m(il)) - beta_m(f, il) * m(il);
  124.  
  125. case 2:
  126. return alpha_n(f, il) * (1 - n(il)) - beta_n(f, il) * n(il);
  127.  
  128. case 3:
  129. return alpha_h(f, il) * (1 - h(il)) - beta_h(f, il) * h(il);
  130. }
  131.  
  132. return 0;
  133. }
  134.  
  135. void RungeKutta(double dt, double* f, double* f_next)
  136. {
  137. double k[Equations_count][4];
  138.  
  139. // k1
  140. for (int i = 0; i < Equations_count; i++)
  141. k[i][0] = HodgkinHuxley(i, f) * dt;
  142.  
  143. double phi_k1[Equations_count];
  144. for (int i = 0; i < Equations_count; i++)
  145. phi_k1[i] = f[i] + k[i][0] / 2;
  146.  
  147. // k2
  148. for (int i = 0; i < Equations_count; i++)
  149. k[i][1] = HodgkinHuxley(i, phi_k1) * dt;
  150.  
  151. double phi_k2[Equations_count];
  152. for (int i = 0; i < Equations_count; i++)
  153. phi_k2[i] = f[i] + k[i][1] / 2;
  154.  
  155. // k3
  156. for (int i = 0; i < Equations_count; i++)
  157. k[i][2] = HodgkinHuxley(i, phi_k2) * dt;
  158.  
  159. double phi_k3[Equations_count];
  160. for (int i = 0; i < Equations_count; i++)
  161. phi_k3[i] = f[i] + k[i][2] / 2;
  162.  
  163. // k4
  164. for (int i = 0; i < Equations_count; i++)
  165. k[i][3] = HodgkinHuxley(i, phi_k3) * dt;
  166.  
  167. for (int i = 0; i < Equations_count; i++)
  168. f_next[i] = f[i] + (k[i][0] + 2 * k[i][1] + 2 * k[i][2] + k[i][3]) / 6;
  169. }
  170.  
  171. void CopyArray(double* source, double* target, int N)
  172. {
  173. for (int i = 0; i < N; i++)
  174. target[i] = source[i];
  175. }
  176.  
  177. bool Approximately(double a, double b)
  178. {
  179. if (a < 0)
  180. a = -a;
  181.  
  182. if (b < 0)
  183. b = -b;
  184.  
  185. return a - b <= 0.000001;
  186. }
  187.  
  188. void FillAMatrixZero()
  189. {
  190. for (int i = 0; i < Neuron_count; i++)
  191. {
  192. for (int j = 0; j < Neuron_count; j++)
  193. {
  194. A[i][j] = 0;
  195. }
  196. }
  197. }
  198.  
  199. void FillBCMatrix()
  200. {
  201. for (int i = 0; i < Neuron_count; i++)
  202. {
  203. int bIndex = 0;
  204. C[i] = 0;
  205. for (int j = 0; j < Neuron_count; j++)
  206. {
  207. if (A[i][j] == 1)
  208. {
  209. B[i][bIndex] = j;
  210. bIndex++;
  211. C[i]++;
  212. }
  213. }
  214. }
  215. }
  216.  
  217. void FillSigmaMatrix()
  218. {
  219. for (int i = 0; i < Neuron_count; i++)
  220. {
  221. for (int j = 0; j < Neuron_count; j++)
  222. {
  223. if ((i >= 0 && i < Neuron_count_half) && (j >= 0 || j < Neuron_count_half))
  224. {
  225. sigma[i][j] = sigma_G;
  226. }
  227. if ((((i >= Neuron_count_half && i < Neuron_count) && (j >= 0 && j < Neuron_count_half)) || ((i >= 0 && i < Neuron_count_half) && (j >= Neuron_count_half && j < Neuron_count))))
  228. {
  229. sigma[i][j] = sigma_GN;
  230. }
  231. if ((i >= Neuron_count_half && i < Neuron_count) && (j >= Neuron_count_half && j < Neuron_count))
  232. {
  233. sigma[i][j] = sigma_N;
  234. }
  235. }
  236. }
  237. }
  238.  
  239. void FillRandomMatrix()
  240. {
  241. for (int k = 0; k < 2 * Neuron_count_half; k++)
  242. {
  243. int i, j;
  244.  
  245. do
  246. {
  247. i = RandomI(Neuron_count_half, Neuron_count);
  248. j = RandomI(Neuron_count_half, Neuron_count);
  249. } while ((i == j) || (A[i][j] == 1));
  250.  
  251. A[i][j] = 1;
  252. A[j][i] = 1;
  253. }
  254. }
  255.  
  256. int main(int argc, char *argv[])
  257. {
  258. FILE *fp0;
  259. srand(time(NULL));
  260.  
  261. A = malloc(Neuron_count * sizeof(double));
  262. for (int i = 0; i < Neuron_count; i++)
  263. A[i] = malloc(Neuron_count * sizeof(double));
  264.  
  265. B = malloc(Neuron_count * sizeof(double));
  266. for (int i = 0; i < Neuron_count; i++)
  267. B[i] = malloc(Neuron_count * sizeof(double));
  268.  
  269. C = malloc(Neuron_count * sizeof(double));
  270.  
  271. sigma = malloc(Neuron_count * sizeof(double));
  272. for (int i = 0; i < Neuron_count; i++)
  273. sigma[i] = malloc(Neuron_count * sizeof(double));
  274.  
  275. FillAMatrixZero();
  276. FillRandomMatrix();
  277. FillBCMatrix();
  278. FillSigmaMatrix();
  279.  
  280. fp0 = fopen("A.txt", "w+");
  281. for (int i = 0; i < Neuron_count; i++)
  282. {
  283. for (int j = 0; j < Neuron_count; j++)
  284. {
  285. fprintf(fp0, "%d\t", (int)A[i][j]);
  286. }
  287. fprintf(fp0, "\n");
  288. }
  289. fclose(fp0);
  290.  
  291. //Пишем в файл число связей у каждого осциллятора в четвертом квадранте
  292. fp0 = fopen("links.txt", "w+");
  293. for (int i = Neuron_count_half; i < Neuron_count; i++)
  294. {
  295. int links_count = 0;
  296. for (int j = Neuron_count_half; j < Neuron_count; j++)
  297. {
  298. if (A[i][j] == 1)
  299. {
  300. links_count++;
  301. }
  302. }
  303. fprintf(fp0, "%d\n", (int)links_count);
  304.  
  305. }
  306. fclose(fp0);
  307.  
  308. fp0 = fopen("B.txt", "w+");
  309.  
  310. for (int i = 0; i < Neuron_count; i++)
  311. {
  312. for (int j = 0; j < C[i]; j++)
  313. {
  314. fprintf(fp0, "%d\t", (int)B[i][j]);
  315. }
  316. fprintf(fp0, "\n");
  317. }
  318.  
  319. fclose(fp0);
  320.  
  321. fp0 = fopen("C.txt", "w+");
  322.  
  323. for (int i = 0; i < Neuron_count; i++)
  324. {
  325. fprintf(fp0, "%d\n", (int)C[i]);
  326. }
  327.  
  328. fclose(fp0);
  329.  
  330. fp0 = fopen("sigma.txt", "w+");
  331. for (int i = 0; i < Neuron_count; i++)
  332. {
  333. for (int j = 0; j < Neuron_count; j++)
  334. {
  335. fprintf(fp0, "%f\t", sigma[i][j]);
  336. }
  337. fprintf(fp0, "\n");
  338. }
  339.  
  340. fclose(fp0);
  341.  
  342. // Initial values
  343. for (int i = 0; i < Equations_count; i++)
  344. f[i] = 0;
  345.  
  346. for (int i = 0; i < Equations_count; i++)
  347. I_app[i] = RandomD(10, 50);
  348.  
  349. const double t_start = 0;
  350. const double t_max = 500;
  351. const double dt = 0.01;
  352.  
  353. double t = t_start;
  354.  
  355. fp0 = fopen("results.txt", "w+");
  356. //setlocale(LC_NUMERIC, "French_Canada.1252");
  357.  
  358. clock_t start_rk4, end_rk4;
  359. start_rk4 = clock();
  360. int lastPercent = -1;
  361.  
  362. while (t < t_max || Approximately(t, t_max))
  363. {
  364. fprintf(fp0, "%f\t", t);
  365.  
  366. for (int i = 0; i < Equations_count; i += 4)
  367. fprintf(fp0, i == Equations_count - 1 ? "%f" : "%f\t", f[i]);
  368.  
  369. fprintf(fp0, "\n");
  370.  
  371. double f_next[Equations_count];
  372.  
  373. RungeKutta(dt, f, f_next);
  374. CopyArray(f_next, f, Equations_count);
  375.  
  376. t += dt;
  377.  
  378. int percent = (int)(100 * (t - t_start) / (t_max - t_start));
  379. if (percent != lastPercent)
  380. {
  381. printf("Progress: %d%%\n", percent);
  382. lastPercent = percent;
  383. }
  384. }
  385.  
  386. fclose(fp0);
  387.  
  388. end_rk4 = clock();
  389. double extime_rk4 = (double)(end_rk4 - start_rk4) / CLOCKS_PER_SEC;
  390. int minutes = (int)extime_rk4 / 60;
  391. int seconds = (int)extime_rk4 % 60;
  392. printf("\nExecution time is: %d minutes %d seconds\n ", minutes, seconds);
  393.  
  394. fp0 = fopen("time_exec.txt", "w+");
  395. fprintf(fp0, "%f\n", extime_rk4);
  396. fclose(fp0);
  397.  
  398. for (int i = 0; i < Neuron_count; i++)
  399. free(A[i]);
  400. free(A);
  401.  
  402. for (int i = 0; i < Neuron_count; i++)
  403. free(B[i]);
  404. free(B);
  405.  
  406. for (int i = 0; i < Neuron_count; i++)
  407. free(sigma[i]);
  408. free(sigma);
  409.  
  410. free(C);
  411. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement