diffusion_v0.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // 包括库 --- Include libraries
  2. #include <stdio.h>
  3. #include <math.h>
  4. // 定义 --- Definitions
  5. #define N 256 // 空间网格数 --- Number of spatial grid points
  6. #define T 255 // 时间步数 --- Time steps to be simulated (integrated)
  7. #define dt 1 // 时间步长 --- Temporal length scale (dt)
  8. #define dx 1 // 空间步长 --- Spatial length scale (dx
  9. #define alpha 0.5 // 扩散系数 --- Diffusion coefficient
  10. // 全球结构 --- Global structures
  11. struct simulation_data{
  12. double u[N]; // u: 为当前时刻的浓度分
  13. // --- To store the concentration distribution at the current moment
  14. int generation; // generation: 模拟迭代 --- simulation iteration
  15. } state;
  16. // 打印功能 --- Print function (truncated)
  17. void printGrid(double grid[N], int time) {
  18. // 代 --- generations
  19. printf("[dt x %d]\t", time);
  20. // 最左边3个格点的状态 --- state of the 3 left-most lattice sites
  21. for (int i = 0; i < 3;i++){
  22. printf("%.2f ", grid[i]);
  23. printf("\t");
  24. }
  25. printf("...\t");
  26. // 三个中心格点的状态 --- state of the 3 center lattice sites
  27. for (int i = floor(N/2)-1 ; i < floor(N/2)+2;i++){
  28. printf("%.2f ", grid[i]);
  29. printf("\t");
  30. }
  31. printf("...\t");
  32. // 3个最右边格点的状态 --- state of the 3 right-most lattice sites
  33. for (int i = N-3; i < N;i++){
  34. printf("%.2f ", grid[i]);
  35. printf("\t");
  36. }
  37. printf("\n");
  38. }
  39. // 初始化功能 --- Initialization function
  40. void init_lattice(void){
  41. // 初始化浓度分布 --- Initialize concentration distribution
  42. for (int i = 0; i < N; i++) {
  43. if (i >= 45*N/100 && i <= 55*N/100) {
  44. state.u[i] = 100.0;
  45. } else {
  46. state.u[i] = 0.0;
  47. }
  48. }
  49. // 初始化时间 --- Initialize time
  50. state.generation = 0;
  51. }
  52. // 更新功能 --- Update function (to increment one iteration/generation)
  53. void update_lattice(void){
  54. // u_new: 为下一个时刻的浓度分布
  55. double u_new[N];// --- To store the concentration distribution at the next moment
  56. // 强制边界条件 --- Enforce boundary condition
  57. u_new[0] = state.u[0];
  58. u_new[N] = state.u[N];
  59. // 内部节点计算 --- Internal node calculation
  60. for (int i = 1; i < N; i++) {
  61. u_new[i] = state.u[i] + alpha * dt / (dx*dx) * (state.u[i+1] - 2*state.u[i] + state.u[i-1]);
  62. }
  63. // 更新浓度分布 --- Update concentration distribution
  64. for (int i = 0; i < N; i++) {
  65. state.u[i] = u_new[i];
  66. }
  67. //printGrid(state.u,state.generation);
  68. state.generation ++;
  69. }
  70. // 模拟 --- simulation
  71. void integrate_simulation(void){
  72. // 初始化模拟 --- Initialize simulation
  73. init_lattice();
  74. printGrid(state.u, state.generation);
  75. // 迭代计算 --- Iterative calculation
  76. for (int t = 1; t <= T; t++) {
  77. update_lattice();
  78. printGrid(state.u, state.generation);
  79. }
  80. }
  81. // 主要功能 --- Main function
  82. int main(int argc, char **argv) {
  83. integrate_simulation();
  84. return 0;
  85. }