VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
DistributionDebugWriter.cpp
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//
33//=======================================================================================
35
37
38#include <logger/Logger.h>
39
40#include <lbm/constants/D3Q27.h>
41
43#include "FilePartCalculator.h"
44#include "Parameter/Parameter.h"
45#include "WriterUtilities.h"
47
48using namespace vf::lbm::dir;
49
50namespace vf::gpu {
51
53{
54 for (int level = para.getCoarse(); level <= para.getFine(); level++) {
56 }
57}
58
59void createFileNames(std::vector<std::string>& fileNames, uint numberOfParts, uint level, uint timestep, const Parameter& para)
60{
61 for (uint i = 1; i <= numberOfParts; i++) {
62 fileNames.push_back(para.getFName() + "_bin_distributions" +
64 }
65}
66
67void createNodeDataNames(std::vector<std::string>& nodeDataNames)
68{
69 nodeDataNames.resize(NUMBER_Of_DIRECTIONS);
70
71 for (uint dir = STARTDIR; dir <= ENDDIR; dir++) {
72 const size_t minLenghtOfNumberString = 2; // the number is padded with zeros to this length
73 const auto numberString = std::to_string(dir);
74 nodeDataNames[dir] =
75 "f_" + std::string(minLenghtOfNumberString - std::min(minLenghtOfNumberString, numberString.length()), '0') +
76 numberString + " [" + vf::lbm::dir::directionNames.at(dir) + "]";
77 }
78}
79
81{
82 const LBMSimulationParameter& parH = para.getParHostAsReference(level);
84
85 std::vector<std::string> fileNames;
86 createFileNames(fileNames, numberOfParts, level, timestep, para);
87
88 std::vector<std::string> nodeDataNames;
89 createNodeDataNames(nodeDataNames);
90
92 uint startPosition;
93 uint endPosition;
94 std::array<uint, 8> indicesOfOct;
95 std::array<uint, 8> relativePosInPart;
97
98 Distributions27 distributions = parH.distributions;
99
100 if (distributions.f[0] == nullptr)
101 throw std::runtime_error("Distributions (distributions.f[0]) at level " + std::to_string(level) +
102 " are not allocated on the host. Can't write distributions.");
103
104 vf::gpu::getPointersToDistributions(distributions, distributions.f[0], parH.numberOfNodes, timestep % 2 == 0);
105
106 for (unsigned int part = 0; part < numberOfParts; part++) {
109 endPosition = startPosition + sizeOfNodes;
110
111 std::vector<UbTupleFloat3> nodes(sizeOfNodes);
112 std::vector<UbTupleUInt8> cells;
113 std::vector<std::vector<double>> nodeData(nodeDataNames.size());
114 for (uint i = 0; i < (uint)nodeDataNames.size(); i++)
115 nodeData[i].resize(sizeOfNodes);
116
117 for (unsigned int pos = startPosition; pos < endPosition; pos++) {
118
119 if (parH.typeOfGridNode[pos] != GEO_FLUID)
120 continue;
121
122 relativePositionInPart = pos - startPosition;
123
125 makeUbTuple((float)parH.coordinateX[pos], (float)parH.coordinateY[pos], (float)parH.coordinateZ[pos]);
126
127 vf::gpu::ListIndices neighborIndices(pos, parH.neighborX, parH.neighborY, parH.neighborZ);
128 real fLocal[27];
129 vf::gpu::getPreCollisionDistribution(fLocal, distributions, neighborIndices);
130
131 for (uint dir = STARTDIR; dir <= ENDDIR; dir++) {
133 }
134
137 continue;
138 }
139
142 cells.push_back(makeUbTupleFromArray(relativePosInPart));
143 }
144 }
145
146 std::string fileName = WbWriterVtkXmlBinary::getInstance()->writeOctsWithNodeData(fileNames[part], nodes, cells,
147 nodeDataNames, nodeData);
148 VF_LOG_DEBUG("DistributionDebugWriter wrote to {} ", fileName);
149 }
150}
151
153{
154 cudaMemoryManager.cudaAllocFsForAllLevelsOnHost();
155}
156
158{
159 cudaMemoryManager.cudaAllocFsForCheckPointAndRestart(level);
160}
161
163{
164 for (int level = 0; level <= para.getMaxLevel(); level++)
165 DistributionDebugWriter::copyDistributionsToHost(para, cudaMemoryManager, level);
166}
167
169 uint level)
170{
171
172 if (para.getParHostAsReference(level).distributions.f[0] == nullptr)
173 throw std::runtime_error("Distributions (distributions.f[0]) at level " + std::to_string(level) +
174 " are not allocated on the host. Can't copy distributions to host");
175 cudaMemoryManager.cudaCopyFsForCheckPoint(level);
176}
177
178}
179
#define VF_LOG_DEBUG(...)
Definition Logger.h:49
static WbWriterVtkXmlBinary * getInstance()
void cudaCopyFsForCheckPoint(int lev) const
copy distributions from device to host
void cudaAllocFsForCheckPointAndRestart(int lev) const
static void copyDistributionsToHost(const Parameter &para, const CudaMemoryManager &cudaMemoryManager)
Copy distributions from device to host. Call this function before writing data.
static void writeDistributions(const Parameter &para, uint timestep)
write the distributions for all levels
static void writeDistributionsForLevel(const Parameter &para, uint level, uint timestep)
write the distributions for one level
static void allocateDistributionsOnHost(const CudaMemoryManager &cudaMemoryManager)
allocate memory for the distributions on the host
static uint calculateNumberOfParts(uint numberOfNodes)
calculate how many output vtk-files are created for one timestep of the given grid level
static uint calculateStartingPostionOfPart(uint indexOfPart)
static uint calculateNumberOfNodesInPart(uint numberOfNodes, uint indexOfFilePart)
calculate how many grid nodes are written to the file with the given index
Class for LBM-parameter management.
Definition Parameter.h:359
int getMyProcessID() const
int getCoarse() const
int getMaxLevel() const
int getFine() const
std::string getFName() const
LBMSimulationParameter & getParHostAsReference(int level) const
static void getIndicesOfAllNodesInOct(std::array< uint, 8 > &nodeIndices, uint baseNodeOfOct, const LBMSimulationParameter &parH)
use the neighbor relations to find the indices of all nodes in an oct cell
static bool isPeriodicCell(const LBMSimulationParameter &parH, unsigned int baseNodeOfCell, unsigned int otherNodeInCell)
check whether a grid cell is part of a periodic boundary condition
static std::string makePartFileNameEnding(uint level, int processID, int part, int timestep)
create the ending of the file name for a file part
static void calculateRelativeNodeIndexInPart(std::array< uint, 8 > &relativePositionInPart, const std::array< uint, 8 > &indicesOfOct, uint startPositionOfPart)
calculate the node index relative to the start position of the part
static bool areAllNodesInOctValidForWriting(const std::array< uint, 8 > &indicesOfOct, const LBMSimulationParameter &parH, uint endPositionOfPart)
check if all nodes in an oct are valid to be written into an output file
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
unsigned int uint
Definition DataTypes.h:47
#define GEO_FLUID
Definition Calculation.h:45
UbTuple< T, T, T, T, T, T, T, T > makeUbTupleFromArray(const std::array< T, 8 > &array)
Definition UbTuple.h:613
UbTuple< T1 > makeUbTuple(T1 const &a1)
Definition UbTuple.h:554
std::string writeOctsWithNodeData(const std::string &filename, std::vector< UbTupleFloat3 > &nodes, std::vector< UbTupleUInt8 > &cells, std::vector< std::string > &datanames, std::vector< std::vector< double > > &nodedata) override
constexpr void getPreCollisionDistribution(real *local, const Distributions27 &global, const ListIndices &indices)
void createNodeDataNames(std::vector< std::string > &nodeDataNames)
constexpr void getPointersToDistributions(Distributions27 &dist, real *distributionArray, const unsigned long long numberOfLBnodes, const bool isEvenTimestep)
void createFileNames(std::vector< std::string > &fileNames, uint numberOfParts, uint level, uint timestep, const Parameter &para)
real * f[vf::lbm::dir::NUMBER_Of_DIRECTIONS]
Definition Calculation.h:95
struct holds and manages the LB-parameter of the simulation
Definition Parameter.h:74
uint * typeOfGridNode
stores the type for every lattice node (f.e. fluid node)
Definition Parameter.h:86
Distributions27 distributions
store all distribution functions for the D3Q27
Definition Parameter.h:83
real * coordinateX
store the coordinates for every lattice node
Definition Parameter.h:96
unsigned long long numberOfNodes
stores the number of nodes (based on indirect addressing scheme)
Definition Parameter.h:117
uint * neighborX
store the neighbors in +X, +Y, +Z, and in diagonal negative direction
Definition Parameter.h:93