VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
Utilities.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
32
33#ifndef SAMPLE_UTILITIES_H
34#define SAMPLE_UTILITIES_H
36#include <basics/DataTypes.h>
37#include <filesystem>
38#include <fstream>
39#include <ios>
40#include <stdexcept>
41#include <string>
42#include <vector>
43
44namespace vf::gpu {
45
46template <typename T>
47inline std::string nameComponent(const std::string& name, T value)
48{
49 return "_" + name + "_" + StringUtil::toString<T>(value);
50}
51
52inline std::string makeParallelFileName(const std::string& probeName, int id, int t)
53{
54 return probeName + "_bin" + nameComponent("ID", id) + nameComponent("t", t) + ".vtk";
55}
56
57inline std::string makeGridFileName(const std::string& probeName, int level, int id, int t, uint part)
58{
59 return probeName + "_bin" + nameComponent("lev", level) + nameComponent("ID", id) + nameComponent<int>("Part", part) +
60 nameComponent("t", t) + ".vtk";
61}
62
63inline std::string makeTimeseriesFileName(const std::string& probeName, int level, int id)
64{
65 return probeName + "_timeseries" + nameComponent("lev", level) + nameComponent("ID", id) + ".txt";
66}
67
68template <typename T>
69constexpr inline T computeNewTimeAverage(T oldAverage, T newValue, uint numberOfTimesteps)
70{
71 return oldAverage + (newValue - oldAverage) / numberOfTimesteps;
72}
73
74inline void writeTimeseriesFileHeader(const std::string& fileName, int numberOfPoints,
75 std::vector<std::string>& variableNames, const real* coordsX, const real* coordsY,
76 const real* coordsZ)
77{
78 std::filesystem::create_directories(std::filesystem::path(fileName).parent_path());
79 std::ofstream out(fileName.c_str(), std::ios::out | std::ios::binary);
80
81 if (!out.is_open())
82 throw std::runtime_error("Could not open timeseries file " + fileName + "!");
83
84 out << "TimeseriesOutput \n";
85 out << "Quantities: ";
86 for (const std::string& name : variableNames)
87 out << name << ", ";
88 out << "\n";
89 out << "Number of points in this file: \n";
90 out << numberOfPoints << "\n";
91 out << "Positions: x, y, z\n";
92 for (int i = 0; i < numberOfPoints; i++)
93 out << coordsX[i] << ", " << coordsY[i] << ", " << coordsZ[i] << "\n";
94
95 out.close();
96}
97
108inline void appendDataToTimeseriesFile(const std::string& fileName, std::vector<std::vector<real>>& data)
109{
110 std::ofstream out(fileName.c_str(), std::ios::app | std::ios::binary);
111 for (auto& timestepData : data) {
112 out.write((char*)timestepData.data(), sizeof(real) * timestepData.size());
113 }
114 out.close();
115}
116
117}
118
119#endif
120
121
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
unsigned int uint
Definition DataTypes.h:47
constexpr T computeNewTimeAverage(T oldAverage, T newValue, uint numberOfTimesteps)
Definition Utilities.h:69
void writeTimeseriesFileHeader(const std::string &fileName, int numberOfPoints, std::vector< std::string > &variableNames, const real *coordsX, const real *coordsY, const real *coordsZ)
Definition Utilities.h:74
std::string makeTimeseriesFileName(const std::string &probeName, int level, int id)
Definition Utilities.h:63
std::string makeGridFileName(const std::string &probeName, int level, int id, int t, uint part)
Definition Utilities.h:57
std::string nameComponent(const std::string &name, T value)
Definition Utilities.h:47
std::string makeParallelFileName(const std::string &probeName, int id, int t)
Definition Utilities.h:52
void appendDataToTimeseriesFile(const std::string &fileName, std::vector< std::vector< real > > &data)
Write data to timeseries file, that can be read by TimeseriesFileReader. Layout of the file is: Times...
Definition Utilities.h:108