#include // For std::setprecision void cell_writeVTKFile(const fields &f, const solverConfig &cfg) { std::string filename = "colocated_results.vtk"; std::ofstream vtkFile(filename); if (!vtkFile.is_open()) { std::cerr << "Error opening VTK file!" << std::endl; return; } // Force consistent decimal formatting (avoid locale issues) vtkFile << std::fixed << std::setprecision(6); // VTK Header vtkFile << "# vtk DataFile Version 3.0\n"; vtkFile << "15x15 Cell-Centered Data\n"; vtkFile << "ASCII\n"; vtkFile << "DATASET RECTILINEAR_GRID\n"; // Grid dimensions (NODES, not cells) vtkFile << "DIMENSIONS " << (cfg.Nx + 1) << " " << (cfg.Ny + 1) << " 1\n"; // Node coordinates (must have Nx+1 and Ny+1 entries) vtkFile << "X_COORDINATES " << (cfg.Nx + 1) << " float\n"; for (int i = 0; i <= cfg.Nx; i++) { vtkFile << f.x[i] << "\n"; // Ensure f.px has Nx+1 entries } vtkFile << "Y_COORDINATES " << (cfg.Ny + 1) << " float\n"; for (int j = 0; j <= cfg.Ny; j++) { vtkFile << f.y[j] << "\n"; // Ensure f.py has Ny+1 entries } vtkFile << "Z_COORDINATES 1 float\n0.0\n"; // CELL_DATA for 15x15 cells vtkFile << "CELL_DATA " << (cfg.Nx * cfg.Ny) << "\n"; // Pressure (15x15) vtkFile << "SCALARS pressure float 1\n"; vtkFile << "LOOKUP_TABLE default\n"; for (int j = 0; j < cfg.Ny; j++) { for (int i = 0; i < cfg.Nx; i++) { vtkFile << f.p[j][i] << "\n"; } } // Velocity components (15x15) vtkFile << "VECTORS velocity float\n"; for (int j = 0; j < cfg.Ny; j++) { for (int i = 0; i < cfg.Nx; i++) { vtkFile << f.u[j][i] << " " << f.v[j][i] << " 0.0\n"; } } vtkFile.close(); std::cout << "VTK file successfully written: " << filename << std::endl; }