1D Heat Equation Solver 1.0
Computational Methods Assignment 2025
Loading...
Searching...
No Matches
solver.hpp
Go to the documentation of this file.
1#pragma once
2/**
3 * @file solver.hpp
4 * @brief High-level solver orchestrating BCs and time integration.
5 */
6
7#include "types.hpp"
8#include "grid.hpp"
9#include "method.hpp"
10#include <functional>
11#include <memory>
12#include <vector>
13
15public:
16 /**
17 * @brief Construct the solver with physical and numerical parameters and a scheme kind.
18 * @param phys Physical parameters (D [cm^2/h], L [cm], Tin, Tsur).
19 * @param num Numerical parameters (dx [cm], dt [h], tEnd [h], outEvery [h]).
20 * @param scheme Selected time-stepping scheme (e.g. DuFort-Frankel).
21 */
22 HeatSolver(const PhysParams& phys, const NumParams& num, SchemeKind scheme);
23
24 /**
25 * @brief Run the simulation from t=0 to t=tEnd.
26 * @param onDump Callback executed at t=0 and then every outEvery hours:
27 * onDump(time, grid, T).
28 * Use it to write CSV, compute errors, etc.
29 */
30 void run(const std::function<void(double,const Grid&,const std::vector<double>&)>& onDump);
31
32private:
33 PhysParams phys_; ///< Physical parameters (copied for convenience).
34 NumParams num_; ///< Numerical parameters (copied for convenience).
35 Grid grid_; ///< Spatial grid built from phys/num input.
36 std::unique_ptr<Method> method_; ///< Selected time integration method.
37 bool uses_prev_step_ = false; ///< True when the method requires T^{n-1}.
38
39 std::vector<double> T_; ///< Current solution (typically holds T^n).
40 std::vector<double> Tprev_; ///< Previous solution (stores T^{n-1} when required).
41 std::vector<double> next_; ///< Buffer used to store the next time layer T^{n+1}.
42
43 /**
44 * @brief Apply Dirichlet boundary conditions (Tsur) at both ends of the vector.
45 * @param T Temperature field updated in-place
46 */
47 void apply_dirichlet(std::vector<double>& T) const;
48
49 /**
50 * @brief Initialize the temperature field with Tin and enforce boundary conditions.
51 */
52 void init_field();
53
54 /**
55 * @brief Perform a warm-up step when the method needs two previous time levels.
56 *
57 * We call the scheme once with T^{n-1} = T^{n} = T^0 so that T_ holds T^1
58 * while Tprev_ keeps a copy of T^0 (needed by three-level methods).
59 */
61};
std::vector< double > Tprev_
Previous solution (stores T^{n-1} when required).
Definition solver.hpp:40
PhysParams phys_
Physical parameters (copied for convenience).
Definition solver.hpp:33
void apply_dirichlet(std::vector< double > &T) const
Apply Dirichlet boundary conditions (Tsur) at both ends of the vector.
Definition solver.cpp:34
std::vector< double > next_
Buffer used to store the next time layer T^{n+1}.
Definition solver.hpp:41
NumParams num_
Numerical parameters (copied for convenience).
Definition solver.hpp:34
std::unique_ptr< Method > method_
Selected time integration method.
Definition solver.hpp:36
void bootstrap_if_needed()
Perform a warm-up step when the method needs two previous time levels.
Definition solver.cpp:47
void run(const std::function< void(double, const Grid &, const std::vector< double > &)> &onDump)
Run the simulation from t=0 to t=tEnd.
Definition solver.cpp:83
HeatSolver(const PhysParams &phys, const NumParams &num, SchemeKind scheme)
Construct the solver with physical and numerical parameters and a scheme kind.
Definition solver.cpp:10
Grid grid_
Spatial grid built from phys/num input.
Definition solver.hpp:35
std::vector< double > T_
Current solution (typically holds T^n).
Definition solver.hpp:39
void init_field()
Initialize the temperature field with Tin and enforce boundary conditions.
Definition solver.cpp:41
bool uses_prev_step_
True when the method requires T^{n-1}.
Definition solver.hpp:37
Uniform 1D spatial grid.
SchemeKind
Enumeration of the numerical schemes available in the solver.
Definition method.hpp:13
Uniform 1D grid on x ∈ [0, L], including both boundary nodes.
Definition grid.hpp:15
Numerical parameters controlling the discretization and output.
Definition types.hpp:20
Physical parameters of the wall heat conduction problem.
Definition types.hpp:10
Basic physical and numerical parameter types for the 1D heat problem.