create a temperature distribution with a plane wall being cooled by a liquid on the right side and being heated by a heat flux for 0.2 seconds every 1 second on the left side in javascript

To create a temperature distribution with a plane wall being cooled by a liquid on the right side and being heated by a heat flux for 0.2 seconds every 1 second on the left side in javascript, we can use the following code:

index.tsx
// Define variables
const L = 1; // wall thickness in meters
const k = 0.5; // thermal conductivity of wall in W/mK
const rho = 1000; // density of wall in kg/m^3
const Cp = 1000; // specific heat capacity of wall in J/kgK
const Tf = 20; // temperature of fluid in Celsius
const h = 100; // convective heat transfer coefficient in W/m^2K
const q = 1000; // heat flux on left side in W/m^2
const t = 60; // time in seconds
const dx = 0.01; // spatial step
const dt = 0.1; // time step
const alpha = k/(rho*Cp); // thermal diffusivity

// Set up initial temperature distribution
let T = []; // temperature array
for (let i = 0; i <= L/dx; i++) {
  T.push(0);
}
T[Math.floor(0.1/dx)] = 1000; // heat flux region on left side

// Define function to update temperature distribution
function updateTemp() {
  // Set boundary conditions
  T[0] = T[1] + dx*q/k; // left boundary
  T[T.length-1] = Tf; // right boundary
  
  // Update temperature distribution using forward-time central-space scheme
  for (let i = 1; i < T.length-1; i++) {
    T[i] += alpha*dt/(dx*dx)*(T[i+1]-2*T[i]+T[i-1]);
  }
}

// Simulate temperature distribution over time
for (let i = 0; i <= t/dt; i++) {
  if (i % 10 == 0) {
    T[Math.floor(0.8/dx)] = 0; // turn off heat flux region on left side
  } else if (i % 5 == 0) {
    T[Math.floor(0.8/dx)] = 1000; // turn on heat flux region on left side
  }
  updateTemp();
}

// Plot temperature distribution
// Use your preferred plotting library
// Example: plotly.js
const x = [];
for (let i = 0; i <= L; i+=dx) {
  x.push(i);
}
const trace1 = {
  x: x,
  y: T,
  type: 'scatter'
};
const data = [trace1];
Plotly.newPlot('myDiv', data);
1656 chars
57 lines

This code uses the forward-time central-space scheme to numerically solve the heat equation for the temperature distribution over time. The heat flux region on the left side is turned on and off every 0.2 seconds. Finally, the temperature distribution is plotted using a plotting library.

gistlibby LogSnag