|
| 1 | +#include "OpenFoamParameters.h" |
| 2 | +#include <iostream> |
| 3 | +#include <fstream> |
| 4 | +#include <filesystem> |
| 5 | + |
| 6 | +namespace OpenFoamTUI |
| 7 | +{ |
| 8 | + |
| 9 | + namespace fs = std::filesystem; |
| 10 | + |
| 11 | + void OpenFoamParameters::writeOpenFoamFiles(const std::string &output_dir) const |
| 12 | + { |
| 13 | + std::cout << "Writing OpenFoam files to: " << output_dir << std::endl; |
| 14 | + |
| 15 | + fs::path case_path = output_dir; |
| 16 | + fs::create_directories(case_path); |
| 17 | + fs::create_directories(case_path / "0"); |
| 18 | + fs::create_directories(case_path / "constant"); |
| 19 | + fs::create_directories(case_path / "system"); |
| 20 | + |
| 21 | + // Write controlDict |
| 22 | + std::ofstream controlDictFile(case_path / "system" / "controlDict"); |
| 23 | + controlDictFile << |
| 24 | +R"(/*--------------------------------*- C++ -*----------------------------------*\ |
| 25 | +| ========= | | |
| 26 | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | |
| 27 | +| \\ / O peration | Version: v2312 | |
| 28 | +| \\ / A nd | Website: www.OpenFOAM.com | |
| 29 | +| \\/ M anipulation | | |
| 30 | +*-------------------------------------------------------------------------------*/ |
| 31 | +FoamFile |
| 32 | +{ |
| 33 | + format ascii; |
| 34 | + class dictionary; |
| 35 | + location "system"; |
| 36 | + object controlDict; |
| 37 | +} |
| 38 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 39 | +
|
| 40 | +application )" << solverType |
| 41 | + << R"(; |
| 42 | +
|
| 43 | +startFrom startTime; |
| 44 | +
|
| 45 | +startTime )" << startTime |
| 46 | + << R"(; |
| 47 | +
|
| 48 | +stopAt endTime; |
| 49 | +
|
| 50 | +endTime )" << endTime |
| 51 | + << R"(; |
| 52 | +
|
| 53 | +deltaT )" << deltaT |
| 54 | + << R"(; |
| 55 | +
|
| 56 | +writeControl timeStep; |
| 57 | +
|
| 58 | +writeInterval )" << writeInterval |
| 59 | + << R"(; |
| 60 | +
|
| 61 | +purgeWrite 0; |
| 62 | +
|
| 63 | +writeFormat ascii; |
| 64 | +
|
| 65 | +writePrecision 6; |
| 66 | +
|
| 67 | +writeCompression off; |
| 68 | +
|
| 69 | +runTimeModifiable true; |
| 70 | +
|
| 71 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 72 | +)"; |
| 73 | + controlDictFile.close(); |
| 74 | + |
| 75 | + // Write transportProperties |
| 76 | + std::ofstream transportPropertiesFile(case_path / "constant" / "transportProperties"); |
| 77 | + transportPropertiesFile << |
| 78 | +R"(/*--------------------------------*- C++ -*----------------------------------*\ |
| 79 | +| ========= | | |
| 80 | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | |
| 81 | +| \\ / O peration | Version: v2312 | |
| 82 | +| \\ / A nd | Website: www.OpenFOAM.com | |
| 83 | +| \\/ M anipulation | | |
| 84 | +*-------------------------------------------------------------------------------*\ |
| 85 | +FoamFile |
| 86 | +{ |
| 87 | + format ascii; |
| 88 | + class dictionary; |
| 89 | + location "constant"; |
| 90 | + object transportProperties; |
| 91 | +} |
| 92 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 93 | +
|
| 94 | +)" << (flowType == "Incompressible" ? "nu nu [0 2 -1 0 0 0 0] " + std::to_string(nu_value) + ";" : "") |
| 95 | + << "\n"; |
| 96 | + transportPropertiesFile.close(); |
| 97 | + |
| 98 | + // Write 0/U |
| 99 | + std::ofstream UFile(case_path / "0" / "U"); |
| 100 | + UFile << R"(FoamFile |
| 101 | +{ |
| 102 | + format ascii; |
| 103 | + class volVectorField; |
| 104 | + object U; |
| 105 | +} |
| 106 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 107 | +
|
| 108 | +dimensions [0 1 -1 0 0 0 0]; |
| 109 | +
|
| 110 | +internalField uniform ()" |
| 111 | + << U_x << " " << U_y << " " << U_z << R"(); |
| 112 | +
|
| 113 | +boundaryField |
| 114 | +{ |
| 115 | + // Placeholder for boundary conditions |
| 116 | + defaultFaces |
| 117 | + { |
| 118 | + type empty; |
| 119 | + } |
| 120 | +} |
| 121 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 122 | +)"; |
| 123 | + UFile.close(); |
| 124 | + |
| 125 | + // Write 0/p |
| 126 | + std::ofstream pFile(case_path / "0" / "p"); |
| 127 | + pFile << R"(FoamFile |
| 128 | +{ |
| 129 | + format ascii; |
| 130 | + class volScalarField; |
| 131 | + object p; |
| 132 | +} |
| 133 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 134 | +
|
| 135 | +dimensions [0 2 -2 0 0 0 0]; |
| 136 | +
|
| 137 | +internalField uniform )" |
| 138 | + << p_init << R"(; |
| 139 | +
|
| 140 | +boundaryField |
| 141 | +{ |
| 142 | + // Placeholder for boundary conditions |
| 143 | + defaultFaces |
| 144 | + { |
| 145 | + type empty; |
| 146 | + } |
| 147 | +} |
| 148 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 149 | +)"; |
| 150 | + pFile.close(); |
| 151 | + |
| 152 | + // Write 0/T (if compressible) |
| 153 | + if (flowType == "Compressible") |
| 154 | + { |
| 155 | + std::ofstream TFile(case_path / "0" / "T"); |
| 156 | + TFile << R"(FoamFile |
| 157 | +{ |
| 158 | + format ascii; |
| 159 | + class volScalarField; |
| 160 | + object T; |
| 161 | +} |
| 162 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 163 | +
|
| 164 | +dimensions [0 0 0 1 0 0 0]; |
| 165 | +
|
| 166 | +internalField uniform )" |
| 167 | + << T_init << R"(; |
| 168 | +
|
| 169 | +boundaryField |
| 170 | +{ |
| 171 | + // Placeholder for boundary conditions |
| 172 | + defaultFaces |
| 173 | + { |
| 174 | + type empty; |
| 175 | + } |
| 176 | +} |
| 177 | +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
| 178 | +)"; |
| 179 | + TFile.close(); |
| 180 | + } |
| 181 | + |
| 182 | + } // namespace OpenFoamTUI |
| 183 | + |
| 184 | + } // namespace OpenFoamTUI |
| 185 | + |
| 186 | + // Move the definition of readOpenFoamFiles outside of writeOpenFoamFiles |
| 187 | + namespace OpenFoamTUI |
| 188 | + { |
| 189 | + bool OpenFoamParameters::readOpenFoamFiles(const std::string &case_dir) |
| 190 | + { |
| 191 | + std::cout << "Reading OpenFoam files from: " << case_dir << std::endl; |
| 192 | + |
| 193 | + fs::path case_path = case_dir; |
| 194 | + if (!fs::exists(case_path) || !fs::is_directory(case_path)) |
| 195 | + { |
| 196 | + std::cerr << "Error: Case directory not found: " << case_dir << std::endl; |
| 197 | + return false; |
| 198 | + } |
| 199 | + |
| 200 | + caseName = case_path.filename().string(); |
| 201 | + |
| 202 | + // Read controlDict |
| 203 | + fs::path controlDictPath = case_path / "system" / "controlDict"; |
| 204 | + if (fs::exists(controlDictPath)) |
| 205 | + { |
| 206 | + std::ifstream controlDictFile(controlDictPath); |
| 207 | + std::string line; |
| 208 | + while (std::getline(controlDictFile, line)) |
| 209 | + { |
| 210 | + if (line.find("application") != std::string::npos) |
| 211 | + { |
| 212 | + size_t start = line.find("application") + std::string("application").length(); |
| 213 | + size_t end = line.find(";", start); |
| 214 | + solverType = line.substr(start, end - start); |
| 215 | + solverType.erase(0, solverType.find_first_not_of(" \t")); // Trim leading whitespace |
| 216 | + solverType.erase(solverType.find_last_not_of(" \t") + 1); // Trim trailing whitespace |
| 217 | + } |
| 218 | + else if (line.find("startTime") != std::string::npos && line.find("startFrom") == std::string::npos) |
| 219 | + { |
| 220 | + size_t start = line.find("startTime") + std::string("startTime").length(); |
| 221 | + size_t end = line.find(";", start); |
| 222 | + startTime = std::stod(line.substr(start, end - start)); |
| 223 | + } |
| 224 | + else if (line.find("endTime") != std::string::npos) |
| 225 | + { |
| 226 | + size_t start = line.find("endTime") + std::string("endTime").length(); |
| 227 | + size_t end = line.find(";", start); |
| 228 | + endTime = std::stod(line.substr(start, end - start)); |
| 229 | + } |
| 230 | + else if (line.find("deltaT") != std::string::npos) |
| 231 | + { |
| 232 | + size_t start = line.find("deltaT") + std::string("deltaT").length(); |
| 233 | + size_t end = line.find(";", start); |
| 234 | + deltaT = std::stod(line.substr(start, end - start)); |
| 235 | + } |
| 236 | + else if (line.find("writeInterval") != std::string::npos) |
| 237 | + { |
| 238 | + size_t start = line.find("writeInterval") + std::string("writeInterval").length(); |
| 239 | + size_t end = line.find(";", start); |
| 240 | + writeInterval = std::stod(line.substr(start, end - start)); |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + // Read transportProperties |
| 246 | + fs::path transportPropertiesPath = case_path / "constant" / "transportProperties"; |
| 247 | + if (fs::exists(transportPropertiesPath)) |
| 248 | + { |
| 249 | + std::ifstream transportPropertiesFile(transportPropertiesPath); |
| 250 | + std::string line; |
| 251 | + while (std::getline(transportPropertiesFile, line)) |
| 252 | + { |
| 253 | + if (line.find("nu") != std::string::npos) |
| 254 | + { |
| 255 | + size_t start = line.find("nu") + std::string("nu").length(); |
| 256 | + size_t end = line.find(";", start); |
| 257 | + nu_value = std::stod(line.substr(start, end - start)); |
| 258 | + flowType = "Incompressible"; // Assume incompressible if nu is found |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + std::cout << "Case parameters read: " << caseName << std::endl; |
| 264 | + return true; |
| 265 | + } |
| 266 | + } // namespace OpenFoamTUI |
0 commit comments