Skip to content

Broky64/Computational_Methods

Repository files navigation

🔥 1D Heat Conduction – Numerical Schemes in C++

C++ project (macOS + Linux) built with CMake.
Goal: numerically solve the 1‑D transient heat conduction equation using several finite‑difference schemes (explicit and implicit) and validate them against the analytical solution.


🧩 Physical Problem

A nickel‑steel wall (40% Ni) of L = 31 cm thickness is initially at a uniform temperature $T_{\text{in}} = 38~^\circ\mathrm{C}$.
Both surfaces are suddenly raised and held at $T_{\text{sur}} = 149~^\circ\mathrm{C}$.
Thermal diffusivity: $D = 93~\mathrm{cm^2/h}$.

The governing equation is the unsteady 1‑D heat equation: $$\frac{\partial T}{\partial t} =\ D \frac{\partial^2 T}{\partial x^2}$$

Boundary and initial conditions: $$T(0,t)=T(L,t)=T_\text{sur}, \qquad T(x,0)=T_\text{in}$$

Default discretization used in the coursework brief:

  • Spatial step: $\Delta x = 0.05\ \mathrm{cm}$
  • Time step (baseline): $\Delta t = 0.01\ \mathrm{h}$
  • Output times: $t = 0.0, 0.1, 0.2, 0.3, 0.4, 0.5\ \mathrm{h}$

🧱 Project Structure

include/    → C++ headers (.h / .hpp)
src/        → source files (.cpp)
output/     → generated CSV results for each method and time
docs/       → Doxygen documentation and UML diagrams
.github/    → CI configuration (macOS + Ubuntu)
tests/      → future unit tests (CTest)

⚙️ Prerequisites

  • CMake ≥ 3.20
  • g++ / clang++ with C++20 support
  • Doxygen + Graphviz (for documentation & UML)
  • Optional: VS Code + CMake Tools extension

🚀 Build & Run

At the project root:

cmake -S . -B build && cmake --build build && ./build/heat1d

💡 This single command:

  1. Configures the project (cmake -S . -B build)
  2. Compiles the sources
  3. Runs the executable heat1d

The executable prompts for numerical parameters (e.g., dx, dt) if interactive input is enabled. Otherwise, defaults from the coursework brief are used.


🐍 Python Plotting

A Python script is provided to visualize the results. It allows you to toggle visibility of different methods and adjust the plot scale interactively.

Prerequisites

  • Python 3
  • pandas
  • matplotlib

Install dependencies:

pip install pandas matplotlib

Usage

After running the C++ solver (which generates CSV files in results/), run:

python3 plot_results.py

This will open an interactive window showing the temperature profiles for all executed methods.


🧮 Implemented Numerical Methods

Method Type Summary
DuFort–Frankel Explicit (three‑level) Stable explicit scheme for diffusion; requires an initial FTCS bootstrap for the first step.
Richardson (CTCS) Explicit (three‑level) Central‑time central‑space; known to be unstable for diffusion but included for stability analysis requirements.
Laasonen (Simple Implicit) Implicit (FTCS) First‑order in time, unconditionally stable; linear tridiagonal system solved each step (Thomas algorithm).
Crank–Nicolson Implicit (trapezoidal) Second‑order accurate (time/space); solves a tridiagonal system each step; good balance of accuracy and stability.
Analytical Solution Closed‑form (Fourier series) Reference solution used for validation (qualitative and quantitative).

All solvers share a common Strategy interface (Method base class) and are orchestrated by a Solver driver which:

  • initializes/validates the grid and parameters,
  • applies Dirichlet boundary conditions at each time step,
  • exports results to CSV at the specified output times.

📊 Validation & Output

For each method:

  • Compute temperature profiles for all spatial nodes at t = 0.0 – 0.5 h, every 0.1 h.
  • Export results under output/<method_name>/t_<time>.csv with header:
    x_cm,T_C
    0.00,149.0
    0.05, ...
  • Compare against the analytical solution at the same (x, t):
    • Compute L₂ and L∞ error norms per output time.
    • Produce CSV tables (per method) in output/analysis/ for plotting/reporting.

🧠 Analytical Solution (for Validation)

The closed‑form series solution used for validation is: [ T(x,t) = T_\text{sur} ;+; 2,\bigl(T_\text{in} - T_\text{sur}\bigr), \sum_{m=1}^{\infty} e^{-D(m\pi/L)^2 t} ; \frac{1 - (-1)^m}{m\pi} ; \sin!\left(\frac{m\pi x}{L}\right). ]

The implementation offers a configurable truncation (number of modes) to balance accuracy and runtime for tables/plots in the report.


⏱️ Step‑Size Study (Laasonen)

With fixed ( \Delta x = 0.05~\mathrm{cm} ), run the Laasonen solver for: [ \Delta t \in {0.01,; 0.025,; 0.05,; 0.1},\mathrm{h} ] Measure CPU time and report accuracy vs. compute‑time trade‑offs. Export a summary CSV with timing and error metrics.


🧭 Project Milestones

Phase Status Description
Phase 0 – Setup & OO Design CMake project, folder structure, Grid, IO utilities, base Method interface and Solver.
Phase 1 – DuFort–Frankel Implement and validate DF (three‑level + FTCS bootstrap); export profiles at 0.1 h intervals.
Phase 2 – Richardson (CTCS) Implement CTCS (three‑level), reuse bootstrap, document observed instability characteristics.
Phase 3 – Laasonen (Implicit FTCS) Assemble and solve tridiagonal system (Thomas). Validate convergence/monotonicity; export profiles.
Phase 4 – Crank–Nicolson Implement trapezoidal discretization, reuse Thomas solver; validate results and export profiles.
Phase 5 – Analytical & Error Analysis Implement analytical series; compute L₂/L∞ errors per output time; export comparison tables.
Phase 6 – Step‑Size Study Evaluate Laasonen for multiple ( \Delta t ) (0.01–0.1 h); collect timings and error metrics.
Phase 7 – Stability & Properties Theoretical properties of each method; full Richardson stability derivation for the report appendix.
Phase 8 – Documentation & UML Doxygen config + concise inline comments; UML diagram of the architecture.
Phase 9 – Final Report & Submission 3000–6000 word report with numbered figures/tables; appendices (code + Doxygen + individual contributions).

Milestones and Issues are tracked in GitHub. Each exported dataset names the method and timestamp to simplify plotting.


🧪 Tests

When unit tests are added:

ctest --test-dir build

📘 Documentation

  • API documentation generated via Doxygen (config in docs/).
  • UML class diagram covering:
    • Grid (spatial/temporal discretization)
    • Method hierarchy (DuFort–Frankel, Richardson, Laasonen, Crank–Nicolson)
    • Solver (time‑marching controller)
    • IO utilities (CSV)
  • Concise, English comments inside headers/sources.

💻 Compatibility

✅ macOS
✅ Linux

Validated with GitHub Actions CI (Ubuntu + macOS).


👥 Authors

Developed as part of the
Computational Methods & C++ Assignment 2025 (ESTIA × Cranfield University).
Primary contributor: Paul Brocvielle (and collaborators).