aboutsummaryrefslogtreecommitdiffstats
path: root/csci5451/ass1p6.c
blob: d87edddedde8084db35b29e3b82cb39e1fe3b873 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
//  HEAT TRANSFER SIMULATION
//
//  Simple physical simulation of a rod connected at the left and right
//  ends to constant temperature heat/cold sources.  All positions on
//  the rod are set to an initial temperature.  Each time step, that
//  temperature is altered by computing the difference between a cells
//  temperature and its left and right neighbors.  A constant k
//  (thermal conductivity) adjusts these differences before altering
//  the heat at a cell.  Use the following model to compute the heat
//  for a position on the rod according to the finite difference
//  method.
//
//     left_diff  = H[t][p] - H[t][p-1];
//     right_diff = H[t][p] - H[t][p+1];
//     delta = -k*( left_diff + right_diff )
//     H[t+1][p] = H[t][p] + delta
//
//  Substituting the above, one can get the following
//
//    H[t+1][p] = H[t][p] + k*H[t][p-1] - 2*k*H[t][p] + k*H[t][p+1]
//
//  The matrix H is computed for all time steps and all positions on
//  the rod and displayed after running the simulation.  The simulation
//  is run for a fixed number of time steps rather than until
//  temperatures reach steady state.

int main(int argc, char **argv)
{
  int max_time = 50;          // Number of time steps to simulate
  int width = 20;             // Number of cells in the rod
  double initial_temp = 50.0; // Initial temp of internal cells
  double L_bound_temp = 20.0; // Constant temp at Left end of rod
  double R_bound_temp = 80.0; // Constant temp at Right end of rod
  double k = 0.5;             // thermal conductivity constant
  double **H;                 // 2D array of temps at times/locations

  // Allocate memory
  H = malloc(sizeof(double *) * max_time);
  int t, p;
  for (t = 0; t < max_time; t++)
  {
    H[t] = malloc(sizeof(double *) * width);
  }

  // Initialize constant left/right boundary temperatures
  for (t = 0; t < max_time; t++)
  {
    H[t][0] = L_bound_temp;
    H[t][width - 1] = R_bound_temp;
  }

  // Initialize temperatures at time 0
  t = 0;
  for (p = 1; p < width - 1; p++)
  {
    H[t][p] = initial_temp;
  }

  // Simulate the temperature changes for internal cells
  for (t = 0; t < max_time - 1; t++)
  {
    for (p = 1; p < width - 1; p++)
    {
      double left_diff = H[t][p] - H[t][p - 1];
      double right_diff = H[t][p] - H[t][p + 1];
      double delta = -k * (left_diff + right_diff);
      H[t + 1][p] = H[t][p] + delta;
    }
  }

  // Print results
  printf("Temperature results for 1D rod\n");
  printf("Time step increases going down rows\n");
  printf("Position on rod changes going accross columns\n");

  // Column headers
  printf("%3s| ", "");
  for (p = 0; p < width; p++)
  {
    printf("%5d ", p);
  }
  printf("\n");
  printf("%3s+-", "---");
  for (p = 0; p < width; p++)
  {
    printf("------");
  }
  printf("\n");
  // Row headers and data
  for (t = 0; t < max_time; t++)
  {
    printf("%3d| ", t);
    for (p = 0; p < width; p++)
    {
      printf("%5.1f ", H[t][p]);
    }
    printf("\n");
  }

  return 0;
}