| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: Apache-2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
#ifndef IO_H_ |
| 8 |
#define IO_H_ |
| 9 |
|
| 10 |
#include "HeatTransfer.h" |
| 11 |
#include "Settings.h" |
| 12 |
|
| 13 |
#include <mpi.h> |
| 14 |
|
| 15 |
class IO |
| 16 |
{ |
| 17 |
public: |
| 18 |
IO(const Settings &s, MPI_Comm comm); |
| 19 |
~IO(); |
| 20 |
void write(int step, const HeatTransfer &ht, const Settings &s, MPI_Comm comm); |
| 21 |
|
| 22 |
private: |
| 23 |
std::string m_outputfilename; |
| 24 |
|
| 25 |
// Generate a file name from the outputfile string and the arguments |
| 26 |
// default is add suffix if not already there |
| 27 |
// if rank and step is specified, it will create a unique file name for that |
| 28 |
// rank and step |
| 29 |
std::string MakeFilename(const std::string &outputfile, const std::string &suffix, |
| 30 |
int rank = -1, int step = -1) |
| 31 |
{ |
| 32 |
std::string name; |
| 33 |
const size_t ss = outputfile.size(); |
| 34 |
if (rank == -1 && step == -1) |
| 35 |
{ |
| 36 |
// add suffix if not present already |
| 37 |
name = outputfile; |
| 38 |
|
| 39 |
// if it is a short filename, add directly |
| 40 |
if (ss <= suffix.size()) |
| 41 |
{ |
| 42 |
name += suffix; |
| 43 |
} |
| 44 |
|
| 45 |
// otherwise check whether suffix is already there |
| 46 |
if ((ss > suffix.size()) && outputfile.find(suffix) != ss - suffix.size()) |
| 47 |
{ |
| 48 |
name += suffix; |
| 49 |
} |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
// we need a unique name here |
| 54 |
name = outputfile; |
| 55 |
if ((ss > suffix.size()) && outputfile.find(suffix) == ss - suffix.size()) |
| 56 |
{ |
| 57 |
name = outputfile.substr(0, ss - suffix.size()); |
| 58 |
} |
| 59 |
if (rank >= 0) |
| 60 |
{ |
| 61 |
std::string rs = std::to_string(rank); |
| 62 |
name += "." + rs; |
| 63 |
} |
| 64 |
if (step >= 0) |
| 65 |
{ |
| 66 |
std::string ts = std::to_string(step); |
| 67 |
name += "." + ts; |
| 68 |
} |
| 69 |
name += suffix; |
| 70 |
} |
| 71 |
return name; |
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
#endif /* IO_H_ */ |