1D Heat Equation Solver 1.0
Computational Methods Assignment 2025
Loading...
Searching...
No Matches
io.cpp
Go to the documentation of this file.
1#include "io.hpp"
2
3#include <filesystem>
4#include <fstream>
5#include <iomanip>
6#include <stdexcept>
7
8namespace fs = std::filesystem;
9
10/**
11 * @brief Save a temperature profile (x, T) to a CSV file.
12 *
13 * This function automatically creates the directory structure if missing.
14 * Example output path: "result/dufort_frankel/t_0.10.csv"
15 *
16 * @param path Relative or absolute path to the file.
17 * @param x Spatial coordinates [cm].
18 * @param T Temperature values [°C].
19 */
20void save_profile_csv(const std::string& path,
21 const std::vector<double>& x,
22 const std::vector<double>& T)
23{
24 // Validate input sizes
25 if (x.size() != T.size())
26 throw std::runtime_error("save_profile_csv: vector size mismatch");
27
28 // --- Ensure the directory exists (Resolved Conflict) ---
29 // We use the safer check from the 'dev' branch.
30 fs::path fullPath(path);
31 if (fullPath.has_parent_path())
32 {
33 fs::create_directories(fullPath.parent_path());
34 }
35
36 // --- Open file for writing ---
37 std::ofstream file(fullPath);
38 if (!file)
39 throw std::runtime_error("save_profile_csv: cannot open file " + fullPath.string());
40
41 // --- Write header and data ---
42 file << "x_cm,T_C\n";
43 file << std::fixed << std::setprecision(6);
44 for (std::size_t i = 0; i < x.size(); ++i)
45 file << x[i] << "," << T[i] << "\n";
46}
void save_profile_csv(const std::string &path, const std::vector< double > &x, const std::vector< double > &T)
Save a temperature profile (x, T) to a CSV file.
Definition io.cpp:20
CSV utilities.