SpaceQuester

Untitled

Feb 19th, 2017
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 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.  
  8. #define N 1000
  9. #define K 5
  10.  
  11. const int w_rand_min = 5;
  12. const int w_rand_max = 6;
  13.  
  14. const int theta_rand_min = 0;
  15. const int theta_rand_max = 2 * M_PI;
  16.  
  17. double A[N][N];
  18. double w[N];
  19. double theta[N];
  20.  
  21. double kuramoto(int i, double theta[N])
  22. {
  23. double sum = 0;
  24.  
  25. for (int j = 0; j < N; j++)
  26. sum += A[i][j] * sin(theta[j] - theta[i]);
  27.  
  28. return w[i] + sum * (double)K / N;
  29. }
  30.  
  31. void RungeKutta(double dt, double theta[N], double theta_next[N])
  32. {
  33. double k[N][4];
  34.  
  35. // k1
  36. for (int i = 0; i < N; i++)
  37. k[i][0] = kuramoto(i, theta) * dt;
  38.  
  39. double theta_k1[N];
  40. for (int i = 0; i < N; i++)
  41. theta_k1[i] = theta[i] + k[i][0] / 2;
  42.  
  43. // k2
  44. for (int i = 0; i < N; i++)
  45. k[i][1] = kuramoto(i, theta_k1) * dt;
  46.  
  47. double theta_k2[N];
  48. for (int i = 0; i < N; i++)
  49. theta_k2[i] = theta[i] + k[i][1] / 2;
  50.  
  51. // k3
  52. for (int i = 0; i < N; i++)
  53. k[i][2] = kuramoto(i, theta_k2) * dt;
  54.  
  55. double theta_k3[N];
  56. for (int i = 0; i < N; i++)
  57. theta_k3[i] = theta[i] + k[i][2] / 2;
  58.  
  59. // k4
  60. for (int i = 0; i < N; i++)
  61. k[i][3] = kuramoto(i, theta_k3) * dt;
  62.  
  63. for (int i = 0; i < N; i++)
  64. theta_next[i] = theta[i] + (k[i][0] + 2 * k[i][1] + 2 * k[i][2] + k[i][3]) / 6;
  65. }
  66.  
  67. void CopyArray(double source[N], double target[N])
  68. {
  69. for (int i = 0; i < N; i++)
  70. target[i] = source[i];
  71. }
  72.  
  73. void main()
  74. {
  75. srand(time(NULL));
  76.  
  77. for (int i = 0; i < N; i++)
  78. {
  79. w[i] = ((double)rand() / RAND_MAX) * (w_rand_max - w_rand_min) + w_rand_min;
  80. }
  81.  
  82. /*w[0] = 5;
  83. w[1] = 5;
  84. w[2] = 5;
  85. w[3] = 5;
  86. w[4] = 5;*/
  87.  
  88. for (int i = 0; i < N; i++)
  89. {
  90. theta[i] = ((double)rand() / RAND_MAX) * (theta_rand_max - theta_rand_min) + theta_rand_min;
  91. }
  92.  
  93. /*theta[0] = 0;
  94. theta[1] = M_PI / 2;
  95. theta[2] = M_PI;
  96. theta[3] = ((double)3 / 2) * M_PI;
  97. theta[4] = 2 * M_PI;*/
  98.  
  99. /*{
  100. A[0][0] = 0;
  101. A[0][1] = 1;
  102. A[0][2] = 0;
  103. A[0][3] = 0;
  104. A[0][4] = 1;
  105.  
  106. A[1][0] = 1;
  107. A[1][1] = 0;
  108. A[1][2] = 1;
  109. A[1][3] = 0;
  110. A[1][4] = 0;
  111.  
  112. A[2][0] = 0;
  113. A[2][1] = 1;
  114. A[2][2] = 0;
  115. A[2][3] = 1;
  116. A[2][4] = 0;
  117.  
  118. A[3][0] = 0;
  119. A[3][1] = 0;
  120. A[3][2] = 1;
  121. A[3][3] = 0;
  122. A[3][4] = 1;
  123.  
  124. A[4][0] = 1;
  125. A[4][1] = 0;
  126. A[4][2] = 0;
  127. A[4][3] = 1;
  128. A[4][4] = 0;
  129. }*/
  130.  
  131. /*{
  132. A[0][0] = 1;
  133. A[0][1] = 1;
  134. A[0][2] = 1;
  135. A[0][3] = 1;
  136. A[0][4] = 1;
  137.  
  138. A[1][0] = 1;
  139. A[1][1] = 1;
  140. A[1][2] = 1;
  141. A[1][3] = 1;
  142. A[1][4] = 1;
  143.  
  144. A[2][0] = 1;
  145. A[2][1] = 1;
  146. A[2][2] = 1;
  147. A[2][3] = 1;
  148. A[2][4] = 1;
  149.  
  150. A[3][0] = 1;
  151. A[3][1] = 1;
  152. A[3][2] = 1;
  153. A[3][3] = 1;
  154. A[3][4] = 1;
  155.  
  156. A[4][0] = 1;
  157. A[4][1] = 1;
  158. A[4][2] = 1;
  159. A[4][3] = 1;
  160. A[4][4] = 1;
  161. }*/
  162.  
  163. for (int i = 0; i < N; i++)
  164. {
  165. for (int j = 0; j < N; j++)
  166. {
  167. A[i][j] = 1;
  168. }
  169. }
  170.  
  171.  
  172. const double t_start = 0;
  173. const double t_max = 10;
  174. const double dt = 0.01;
  175.  
  176. double t = t_start;
  177.  
  178. FILE *fp;
  179. fp = fopen("results.txt", "w+");
  180. //setlocale(LC_NUMERIC, "French_Canada.1252");
  181.  
  182. while (t <= t_max + dt)
  183. {
  184. fprintf(fp, "%f\t", t);
  185. for (int i = 0; i < N; i++)
  186. {
  187. fprintf(fp, i == N - 1 ? "%f" : "%f\t", theta[i]);
  188. }
  189. fprintf(fp, "\n");
  190.  
  191. double theta_next[N];
  192.  
  193. RungeKutta(dt, theta, theta_next);
  194. CopyArray(theta_next, theta);
  195.  
  196. t += dt;
  197. }
  198.  
  199. fclose(fp);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment