OpenLB 1.7
Loading...
Searching...
No Matches
analyticalPorosityVolumeF.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2023 Adrian Kummerländer, Dennis Teutscher
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef ANALYTICAL_POROSITY_VOLUME_F_H
25#define ANALYTICAL_POROSITY_VOLUME_F_H
26
27#ifdef FEATURE_VDB
28#include <openvdb/openvdb.h>
29#include <openvdb/tree/ValueAccessor.h>
30#include <openvdb/Grid.h>
31#endif
32
33#if defined(FEATURE_VTK)
34#include <vtkNew.h>
35#include <vtkStructuredPoints.h>
36#include <vtkStructuredPointsReader.h>
37#endif
38
39namespace olb {
40
41template <typename T>
42class AnalyticalPorosityVolumeF final : public AnalyticalF<3,T,T> {
43private:
44#if defined(FEATURE_VTK)
45 vtkNew<vtkStructuredPointsReader> _reader;
46 vtkStructuredPoints* _data;
47#endif
48#ifdef FEATURE_VDB
49 openvdb::GridPtrVecPtr grids;
50#endif
51 std::string _fileName;
52 Vector<int,3> _shape;
53 T _spacing;
54
55public:
56 AnalyticalPorosityVolumeF(std::string fileName, T spacing)
57 : AnalyticalF<3,T,T>(1), _spacing{spacing}
58 {
59 OstreamManager clout(std::cout, "PorosityVolumeImporter");
60 _fileName = fileName;
61 #ifdef FEATURE_VDB
62 if (isVDBFile(_fileName)) {
63 openvdb::initialize();
64 openvdb::io::File _vdbFile(_fileName);
65 // Read the OpenVDB file
66 _vdbFile.open();
67 grids = _vdbFile.getGrids();
68 auto grid = openvdb::gridPtrCast<openvdb::FloatGrid>(*grids->begin());
69 _vdbFile.close();
70 openvdb::CoordBBox bounds = grid->evalActiveVoxelBoundingBox();
71 _shape[0] = bounds.max().x() - bounds.min().x();
72 _shape[1] = bounds.max().y() - bounds.min().y();
73 _shape[2] = bounds.max().z() - bounds.min().z();
74 }
75 #endif
76 #if not defined(FEATURE_VTK)
77 if (isVTKFile(_fileName))
78 {
79 std::cerr << "To use the VTK format, add VTK to the FEATURES list in config.mk";
80 exit(1);
81 }
82 #endif
83 #if defined(FEATURE_VTK)
84 if (isVTKFile(_fileName)) {
85 _reader->SetFileName(fileName.c_str());
86 _reader->Update();
87 _reader->SetScalarsName(_reader->GetScalarsNameInFile(0));
88 auto* _data = _reader->GetOutput();
89 _data->GetDimensions(_shape.data());
90 }
91 #endif
92 #ifndef FEATURE_VDB
93 if (isVDBFile(_fileName))
94 {
95 std::cerr << "To use the VDB format, add VDB to the FEATURES list in config.mk";
96 exit(1);
97 }
98 #endif
99 }
100
102 return _shape * _spacing;
103 }
104
105 bool operator()(T output[], const T physR[]) override{
106 #ifdef FEATURE_VDB
107 if (isVDBFile(_fileName)) {
108 int iX;
109 int iY;
110 int iZ;
111 iX = util::floor(physR[0] / _spacing);
112 iY = util::floor(physR[1] / _spacing);
113 iZ = util::floor(physR[2] / _spacing);
114 if (iX >= 0 && iY >= 0 && iZ >= 0 && iX < _shape[0] && iY < _shape[1] && iZ < _shape[2]) {
115 auto grid = openvdb::gridPtrCast<openvdb::FloatGrid>(*grids->begin());
116 openvdb::Coord location(iX, iY, iZ);
117 openvdb::FloatGrid::Accessor accessor = grid->getAccessor();
118 output[0] =accessor.getValue(location);
119 } else {
120 output[0]=0;
121 }
122 return true;
123 }
124 #endif
125 #if defined(FEATURE_VTK)
126 if(isVTKFile(_fileName)) {
127 int iX;
128 int iY;
129 int iZ;
130 iX = util::floor(physR[0] / _spacing);
131 iY = util::floor(physR[1] / _spacing);
132 iZ = util::floor(physR[2] / _spacing);
133 if (iX >= 0 && iY >= 0 && iZ >= 0 && iX < _shape[0] && iY < _shape[1] && iZ < _shape[2]) {
134 auto* _data = _reader->GetOutput();
135 output[0] = *static_cast<T*>(_data->GetScalarPointer(iX, iY, iZ));
136 } else {
137 output[0] = 0;
138 }
139 return true;
140 }
141 #endif
142 return false;
143 }
144
145private:
146 bool isVDBFile(const std::string& fileName) {
147 return (fileName.size() >= 4 && fileName.substr(fileName.size() - 4) == ".vdb");
148 }
149 bool isVTKFile(const std::string& fileName) {
150 return (fileName.size() >= 4 && fileName.substr(fileName.size() - 4) == ".vtk");
151 }
152
153};
154
155}
156
157#endif
AnalyticalF are applications from DD to XD, where X is set by the constructor.
AnalyticalPorosityVolumeF(std::string fileName, T spacing)
bool operator()(T output[], const T physR[]) override
class for marking output with some text
Plain old scalar vector.
Definition vector.h:47
constexpr const T * data() const any_platform
Definition vector.h:161
ADf< T, DIM > floor(const ADf< T, DIM > &a)
Definition aDiff.h:869
Top level namespace for all of OpenLB.