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
35#include <basics/DataTypes.h>
36#include <filesystem>
37#include <fstream>
38#include <ios>
39#include <stdexcept>
40#include <string>
41#include <vector>
42
43namespace vf::gpu {
44
45template <typename T>
46inline std::string nameComponent(const std::string& name, T value)
47{
48 return "_" + name + "_" + std::to_string(value);
49}
50
51inline std::string makeParallelFileName(const std::string& probeName, int id, int t)
52{
53 return probeName + "_bin" + nameComponent("ID", id) + nameComponent("t", t) + ".vtk";
54}
55
56inline std::string makeGridFileName(const std::string& probeName, int level, int id, int t, uint part)
57{
58 return probeName + "_bin" + nameComponent("lev", level) + nameComponent("ID", id) + nameComponent<int>("Part", part) +
59 nameComponent("t", t) + ".vtk";
60}
61
62inline std::string makeTimeseriesFileName(const std::string& probeName, int level, int id)
63{
64 return probeName + "_timeseries" + nameComponent("lev", level) + nameComponent("ID", id) + ".txt";
65}
66
67template <typename T>
68constexpr inline T computeNewTimeAverage(T oldAverage, T newValue, uint numberOfTimesteps)
69{
70 return oldAverage + (newValue - oldAverage) / numberOfTimesteps;
71}
72
73inline void writeTimeseriesFileHeader(const std::string& fileName, int numberOfPoints,
74 std::vector<std::string>& variableNames, const real* coordsX, const real* coordsY,
75 const real* coordsZ)
76{
77 std::filesystem::create_directories(std::filesystem::path(fileName).parent_path());
78 std::ofstream out(fileName.c_str(), std::ios::out | std::ios::binary);
79
80 if (!out.is_open())
81 throw std::runtime_error("Could not open timeseries file " + fileName + "!");
82
83 out << "TimeseriesOutput \n";
84 out << "Quantities: ";
85 for (const std::string& name : variableNames)
86 out << name << ", ";
87 out << "\n";
88 out << "Number of points in this file: \n";
89 out << numberOfPoints << "\n";
90 out << "Positions: x, y, z\n";
91 for (int i = 0; i < numberOfPoints; i++)
92 out << coordsX[i] << ", " << coordsY[i] << ", " << coordsZ[i] << "\n";
93
94 out.close();
95}
96
107inline void appendDataToTimeseriesFile(const std::string& fileName, std::vector<std::vector<real>>& data)
108{
109 std::ofstream out(fileName.c_str(), std::ios::app | std::ios::binary);
110 for (auto& timestepData : data) {
111 out.write((char*)timestepData.data(), sizeof(real) * timestepData.size());
112 }
113 out.close();
114}
115
116}
117
118#endif
119
120
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:68
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:73
std::string makeTimeseriesFileName(const std::string &probeName, int level, int id)
Definition Utilities.h:62
std::string makeGridFileName(const std::string &probeName, int level, int id, int t, uint part)
Definition Utilities.h:56
std::string nameComponent(const std::string &name, T value)
Definition Utilities.h:46
std::string makeParallelFileName(const std::string &probeName, int id, int t)
Definition Utilities.h:51
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:107