# common.py import numpy as np from Bar import Bar def fdm_heat_extraction(t_0, t_1, dx, bar:'Bar', order=2): '''Get the heat conduction at the point t_1 using Taylor series''' if order == 1: return -1 * bar.k * bar.area * (t_1 - t_0) / dx elif order == 2: return -1 * bar.k * bar.area * (((t_1 - t_0) / dx) + (bar.alpha**2 * dx * t_1 / 2)) elif order == 4: return -1 * bar.k * bar.area * ((144*t_1 - 144*t_0 + 72*dx**2*bar.alpha**2*t_1 + 6*dx**4*bar.alpha**4*t_1) / (144 * dx + 24*dx**3 * bar.alpha**2)) def fem_heat_extraction(t_0, t_1, dx, bar:'Bar', order=2): '''Get the heat conduction at the point t_1 using FEM equation''' if order == 2: # term_1 = (-1/dx + bar.alpha**2*dx/6) * t_0 # term_2 = (1/dx + 2*bar.alpha**2*dx/6) * t_1 term_1 = (t_1 - t_0) / dx term_2 = bar.alpha**2 * dx * t_1 / 2 return -1 * bar.k * bar.area * (term_1 + term_2) elif order == 4: term_1 = (-1/dx + bar.alpha**2*dx/6) * t_0 term_2 = (1/dx + 2*bar.alpha**2*dx/6) * t_1 return -1 * bar.k * bar.area * (term_1 + term_2) def heat_extraction(t_0, t_1, dx, bar:'Bar', order=2, method="FDM"): if method == "FDM": return fdm_heat_extraction(t_0, t_1, dx, bar, order) elif method == "FEM": return fdm_heat_extraction(t_0, t_1, dx, bar, order) def calc_error(exact, q_1): return np.abs((exact - q_1) / exact) def calc_beta(exact, q_1, q_2, dx_1, dx_2): return np.log(np.abs((exact - q_1)/(exact - q_2))) / np.log(dx_1 / dx_2) def calc_extrapolated(q1, q2, q3, tolerance=1e-10): '''Calculate Richardson extrapolation, returns NaN if denominator is too small.''' numerator = q1 * q3 - q2**2 denominator = q1 + q3 - 2 * q2 if abs(denominator) < tolerance: return float('NaN') # Return NaN if denominator is close to zero return numerator / denominator