VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
TransientBCSetter.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 TRANSIENTBCSETTER_H_
34#define TRANSIENTBCSETTER_H_
35
37#include <cmath>
38#include <future>
39#include <string>
40#include <vector>
41
42#include <basics/DataTypes.h>
45
46namespace vf::gpu {
47
48class Grid;
49namespace grid_generator
50{
51 class BoundaryCondition;
52}
53
55{
56 VTK
57};
58
60{
61 std::string name;
62 int offset;
63 std::vector<double> values;
64};
65
67{
68public:
69 explicit VTKFile(std::string fileName):
70 fileName(std::move(fileName))
71 {
72 readHeader();
73 this->loaded = false;
74 // printFileInfo();
75 };
76
77 void getData(real* data, uint numberOfNodes, const std::vector<uint>& readIndices, const std::vector<uint>& writeIndices, uint offsetRead, uint offsetWrite);
78 bool markNANs(const std::vector<uint>& readIndices) const;
80 bool inXBounds(real posX) const{ return posX<=maxX && posX>=minX; };
81 bool inYBounds(real posY) const{ return posY<=maxY && posY>=minY; };
82 bool inZBounds(real posZ) const{ return posZ<=maxZ && posZ>=minZ; };
83 int findNeighborMMM(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX) , getIdx0M0(posY) , getIdx00M(posZ) ); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
84 int findNeighborMMP(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX) , getIdx0M0(posY) , getIdx00M(posZ)+1); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
85 int findNeighborMPM(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX) , getIdx0M0(posY)+1, getIdx00M(posZ) ); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
86 int findNeighborMPP(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX) , getIdx0M0(posY)+1, getIdx00M(posZ)+1); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
87 int findNeighborPMM(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX)+1, getIdx0M0(posY) , getIdx00M(posZ) ); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
88 int findNeighborPMP(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX)+1, getIdx0M0(posY) , getIdx00M(posZ)+1); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
89 int findNeighborPPM(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX)+1, getIdx0M0(posY)+1, getIdx00M(posZ) ); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
90 int findNeighborPPP(real posX, real posY, real posZ) const{ const int idx = getLinearIndex(getIdxM00(posX)+1, getIdx0M0(posY)+1, getIdx00M(posZ)+1); return (idx>=0) && (idx<nx*ny*nz) ? idx : -1; };
91 int getIdxX(int linearIdx) const{ return linearIdx%nx;};
92 int getIdxY(int linearIdx) const{ return (linearIdx/nx)%ny;};
93 int getIdxZ(int linearIdx) const{ return linearIdx/(nx*ny); };
94 real getX(int linearIdx) const{ return getIdxX(linearIdx)*deltaX+minX; };
95 real getY(int linearIdx) const{ return getIdxY(linearIdx)*deltaY+minY; };
96 real getZ(int linearIdx) const{ return getIdxZ(linearIdx)*deltaZ+minZ; };
97 int getIdxM00(real posX) const{ return (posX-minX)/deltaX; };
98 int getIdx0M0(real posY) const{ return (posY-minY)/deltaY; };
99 int getIdx00M(real posZ) const{ return (posZ-minZ)/deltaZ; };
100 int getClosestIdxX(real posX) const{ const int x = round((posX-minX)/deltaX); return x>nx ? nx : (x<0 ? 0 : x);};
101 int getClosestIdxY(real posY) const{ const int y = round((posY-minY)/deltaY); return y>ny ? ny : (y<0 ? 0 : y);};
102 int getClosestIdxZ(real posZ) const{ const int z = round((posZ-minZ)/deltaZ); return z>nz ? nz : (z<0 ? 0 : z);};
103 int getLinearIndex(int idxX, int idxY, int idxZ) const{ return idxX + nx*(idxY+ny*idxZ); };
104 int getNumberOfPointsInXYPlane() const{ return nx*ny; }
105 int getNumberOfPointsInYZPlane() const{ return ny*nz; }
106 int getNumberOfPointsInXZPlane() const{ return nx*nz; }
107 int getNumberOfPoints() const{ return nx*ny*nz; }
108 size_t getNumberOfQuantities(){ return quantities.size(); }
109 void loadFile();
110 void unloadFile();
111 bool isLoaded() const{return loaded;};
112
113
114private:
115 void readHeader();
116 void printFileInfo();
117
118public:
119
120private:
121 std::string fileName;
122 real minX, maxX, minY, maxY, minZ, maxZ;
123 real deltaX, deltaY, deltaZ;
124 int nx, ny, nz;
125 std::vector<Quantity> quantities;
126 bool loaded;
127};
128
130{
131public:
132 FileCollection(std::string path, std::string prefix): path(fixPath(std::move(path))), prefix(std::move(prefix)){};
133
134 virtual ~FileCollection() = default;
135
136 virtual size_t getNumberOfQuantities() = 0;
137
139
140protected:
141 const std::string path, prefix;
142private:
143 static std::string fixPath(std::string path)
144 {
145 if(path.back() == '/')
146 return path;
147 return path + '/';
148 }
149};
150
151
153{
154public:
155 VTKFileCollection(const std::string& path, const std::string& prefix):
157 {
158 findFiles();
159 };
160
162 size_t getNumberOfQuantities() override{ return files[0][0][0].getNumberOfQuantities(); }
163
164
165private:
166 void findFiles();
167 std::string makeFileName(int level, int id, int part)
168 {
169 return prefix + "_lev_" + StringUtil::toString<int>(level)
170 + "_ID_" + StringUtil::toString<int>(id)
171 + "_File_" + StringUtil::toString<int>(part)
172 + ".bin." + suffix;
173 };
174
175
176public:
177 static const inline std::string suffix = "vti";
178 std::vector<std::vector<std::vector<VTKFile>>> files;
179};
180
181
183{
184public:
186 virtual ~TransientBCInputFileReader() = default;
187
188 virtual void getNextData(real* data, uint numberOfNodes, real time)=0;
189 virtual void fillArrays(std::vector<real>& coordsY, std::vector<real>& coordsZ)=0;
190 uint getNPoints() const{ return nPoints; };
191 uint getNPointsRead() const{ return nPointsRead; };
192 size_t getNumberOfQuantities() const{ return nQuantities; };
193 void setWritingOffset(uint offset){ this->writingOffset = offset; }
196
197public:
200
201protected:
202 uint nPoints { 0 }, nPointsRead { 0 }, writingOffset { 0 }, nReads { 0 };
203 const size_t nQuantities;
204 const bool cycleFiles;
205};
206
207
209{
210public:
211 VTKReader(const SPtr<VTKFileCollection>& fileCollection, uint readLevel, bool cycleFiles)
212 : fileCollection(fileCollection), readLevel(readLevel),
214 void getNextData(real* data, uint numberOfNodes, real time) override;
215 void fillArrays(std::vector<real>& coordsY, std::vector<real>& coordsZ) override;
216
217private:
218 uint getWriteIndex(int level, int id, int linearIndex);
219 void initializeIndexVectors();
220
221private:
222 std::vector<std::vector<std::vector<uint>>> readIndices, writeIndices;
223 std::vector<std::vector<size_t>> nFile;
224 SPtr<VTKFileCollection> fileCollection;
225 uint readLevel;
226 real startTime { vf::basics::constant::c0o1 };
227 std::future<void> read = std::async([]() {});
228};
229
230SPtr<FileCollection> createFileCollection(const std::string& path, const std::string& prefix, TransientBCFileType type);
232 bool cycleFiles);
233
234}
235
236#endif //TRANSIENTBCSETTER_H_
237
virtual size_t getNumberOfQuantities()=0
virtual TransientBCFileType getFileType()=0
FileCollection(std::string path, std::string prefix)
virtual ~FileCollection()=default
virtual ~TransientBCInputFileReader()=default
virtual void getNextData(real *data, uint numberOfNodes, real time)=0
void getNeighbors(uint *neighbor0PP, uint *neighbor0PM, uint *neighbor0MP, uint *neighbor0MM)
void getWeights(real *_weights0PP, real *_weights0PM, real *_weights0MP, real *_weights0MM)
virtual void fillArrays(std::vector< real > &coordsY, std::vector< real > &coordsZ)=0
TransientBCInputFileReader(size_t nQuantities, bool cycleFiles)
std::vector< std::vector< std::vector< VTKFile > > > files
static const std::string suffix
size_t getNumberOfQuantities() override
TransientBCFileType getFileType() override
VTKFileCollection(const std::string &path, const std::string &prefix)
int findNeighborPMM(real posX, real posY, real posZ) const
int getLinearIndex(int idxX, int idxY, int idxZ) const
bool inYBounds(real posY) const
real getX(int linearIdx) const
real getZ(int linearIdx) const
int getClosestIdxZ(real posZ) const
int getIdxX(int linearIdx) const
bool inXBounds(real posX) const
int getIdx0M0(real posY) const
int getNumberOfPointsInXZPlane() const
bool inZBounds(real posZ) const
int findNeighborMMP(real posX, real posY, real posZ) const
int findNeighborPMP(real posX, real posY, real posZ) const
int getClosestIdxY(real posY) const
int getIdx00M(real posZ) const
int findNeighborPPP(real posX, real posY, real posZ) const
int getClosestIdxX(real posX) const
bool inBoundingBox(real posX, real posY, real posZ) const
int getNumberOfPoints() const
int findNeighborMPM(real posX, real posY, real posZ) const
VTKFile(std::string fileName)
int findNeighborMPP(real posX, real posY, real posZ) const
int getIdxZ(int linearIdx) const
int getIdxM00(real posX) const
int getNumberOfPointsInYZPlane() const
bool markNANs(const std::vector< uint > &readIndices) const
void getData(real *data, uint numberOfNodes, const std::vector< uint > &readIndices, const std::vector< uint > &writeIndices, uint offsetRead, uint offsetWrite)
real getY(int linearIdx) const
int getIdxY(int linearIdx) const
int getNumberOfPointsInXYPlane() const
int findNeighborPPM(real posX, real posY, real posZ) const
int findNeighborMMM(real posX, real posY, real posZ) const
VTKReader(const SPtr< VTKFileCollection > &fileCollection, uint readLevel, bool cycleFiles)
void fillArrays(std::vector< real > &coordsY, std::vector< real > &coordsZ) override
void getNextData(real *data, uint numberOfNodes, real time) override
std::shared_ptr< T > SPtr
float real
Definition DataTypes.h:42
unsigned int uint
Definition DataTypes.h:47
@ z
Definition Axis.h:44
@ x
Definition Axis.h:42
@ y
Definition Axis.h:43
SPtr< TransientBCInputFileReader > createReaderForCollection(SPtr< FileCollection > fileCollection, uint readLevel, bool cycleFiles)
SPtr< FileCollection > createFileCollection(const std::string &path, const std::string &prefix, TransientBCFileType type)
std::vector< double > values