create a for loop that incorporates cyclic transient heat conduction in a plane wall in javascript

Here is an example code for a for loop that incorporates cyclic transient heat conduction in a plane wall using finite difference method in JavaScript:

index.tsx
// Define required parameters
const alpha = 1.1727E-4;  // thermal diffusivity in m^2/s
const k = 0.152;  // thermal conductivity in W/(m*K)
const rho = 1133.0;  // density in kg/m^3
const Cp = 1525.0;  // specific heat capacity in J/(kg*K)
const t = 7200.0;  // time step in seconds
const L = 0.02;  // wall thickness in meters
const dx = 0.01;  // grid size in meters
const dt = 0.1;  // time interval in seconds
const Tl = 25.0;  // left wall temperature in Celsius
const Tr = 200.0;  // right wall temperature in Celsius
const Tw0 = 100.0;  // initial wall temperature in Celsius

// Define calculated parameters
const Fo = alpha * dt / (dx * dx);  // Fourier number
const B = k / (rho * Cp);  // thermal diffusivity factor
const N = Math.floor(L / dx) + 1;  // number of nodes
const M = Math.floor(t / dt) + 1;  // number of time steps

// Initialize temperature matrix with initial conditions
let Tw = [];
for (let i = 0; i < N; i++) {
  Tw[i] = [];
  Tw[i][0] = Tw0;
}
Tw[0][0] = Tl;
Tw[N-1][0] = Tr;

// Compute temperature using finite difference method
for (let j = 1; j < M; j++) {
  for (let i = 1; i < N-1; i++) {
    Tw[i][j] = Tw[i][j-1] + Fo * (Tw[i-1][j-1] - 2*Tw[i][j-1] + Tw[i+1][j-1]) +
               B * (Tw[i-1][j-1] - Tw[i][j-1]) - B * (Tw[i][j-1] - Tw[i+1][j-1]);
  }
  // Apply cyclic boundary conditions
  Tw[0][j] = Tw[N-2][j];
  Tw[N-1][j] = Tw[1][j];
}

// Print the temperature matrix
console.table(Tw);
1435 chars
42 lines

In this code, alpha is the thermal diffusivity, k is the thermal conductivity, rho is the density, Cp is the specific heat capacity, t is the total simulation time, L is the wall thickness, dx is the grid size, dt is the time interval, Tl is the left wall temperature, Tr is the right wall temperature, and Tw0 is the initial wall temperature.

The Tw matrix represents the temperature distribution across the wall, with rows representing positions along the wall and columns representing time steps. The Fo factor is the Fourier number, which represents the ratio of heat conduction to heat storage. The B factor is the thermal diffusivity factor, which depends on the physical properties of the material. The N and M variables represent the number of nodes and time steps, respectively.

The for loop iterates over the spatial nodes and simulated time steps to compute the temperature using the finite difference method. The cyclic boundary conditions on the left and right boundaries are applied separately outside the inner for loop. Finally, the console.table() function is used to print the Tw matrix.

related categories

gistlibby LogSnag