1D Heat Equation Solver 1.0
Computational Methods Assignment 2025
Loading...
Searching...
No Matches
analytical.cpp
Go to the documentation of this file.
2
3#include <cmath>
4#include <numbers> // C++20 for std::numbers::pi
5
6double Analytical::solve(double t, double x, const PhysParams& phys)
7{
8 // If t=0, return initial condition (handling discontinuity at boundaries if needed)
9 // But strictly speaking, at t=0, T=Tin everywhere inside.
10 if (t <= 0.0)
11 return phys.Tin;
12
13 // Constants
14 const double pi = std::numbers::pi;
15 const double L = phys.L_cm;
16 const double D = phys.D_cm2h;
17
18 double sum = 0.0;
19 const int max_k = 200; // Number of terms for convergence
20
21 for (int k = 0; k < max_k; ++k)
22 {
23 double term_k = 2.0 * k + 1.0;
24 double lambda = (term_k * pi) / L;
25
26 double sine_term = std::sin(lambda * x);
27 double exp_term = std::exp(-D * lambda * lambda * t);
28
29 sum += (1.0 / term_k) * sine_term * exp_term;
30 }
31
32 // T(x,t) = Tsur + (Tin - Tsur) * (4/pi) * sum
33 return phys.Tsur + (phys.Tin - phys.Tsur) * (4.0 / pi) * sum;
34}
35
36std::vector<double>
37Analytical::get_profile(double t, const std::vector<double>& x_grid, const PhysParams& phys)
38{
39 std::vector<double> T(x_grid.size());
40 for (size_t i = 0; i < x_grid.size(); ++i)
41 {
42 // Handle boundaries explicitly to avoid series artifacts (Gibbs phenomenon) at x=0, x=L
43 if (i == 0 || i == x_grid.size() - 1)
44 {
45 T[i] = phys.Tsur;
46 }
47 else
48 {
49 T[i] = solve(t, x_grid[i], phys);
50 }
51 }
52 return T;
53}
static std::vector< double > get_profile(double t, const std::vector< double > &x_grid, const PhysParams &phys)
Generate a full temperature profile for a set of grid points.
static double solve(double t, double x, const PhysParams &phys)
Compute the exact temperature at a given time and position.
Definition analytical.cpp:6
Physical parameters of the wall heat conduction problem.
Definition types.hpp:10
double Tin
Initial temperature [°C].
Definition types.hpp:13
double D_cm2h
Thermal diffusivity [cm^2/h].
Definition types.hpp:12
double L_cm
Wall thickness [cm].
Definition types.hpp:11
double Tsur
Surface (Dirichlet) temperature [°C].
Definition types.hpp:14