OpenLB 1.8.1
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
33namespace olb {
34
35template <typename T>
36class AnalyticalPorosityVolumeF final : public AnalyticalF<3,T,T> {
37private:
38#ifdef FEATURE_VDB
39 openvdb::GridPtrVecPtr _grids;
40 openvdb::FloatGrid::Ptr _grid;
41 openvdb::math::Transform::Ptr _rotation;
42#endif
43
44 Vector<int,3> _shape;
45 Vector<T, 3> _origin;
46 T _volumeDeltaX;
47
48public:
50 AnalyticalPorosityVolumeF(std::string fileName, T volumeDeltaX, T rotation=0)
51 : AnalyticalF<3,T,T>(1), _volumeDeltaX{volumeDeltaX}
52 {
53 #ifdef FEATURE_VDB
54 openvdb::io::File _vdbFile(fileName);
55 // Read the OpenVDB file
56 _vdbFile.open();
57 _grids = _vdbFile.getGrids();
58
59 _grid = openvdb::gridPtrCast<openvdb::FloatGrid>(*_grids->begin());
60 auto bbox = _grid->evalActiveVoxelBoundingBox();
61
62 if (rotation != 0) {
63 openvdb::Vec3d minIdx(bbox.min().x(), bbox.min().y(), bbox.min().z());
64 openvdb::Vec3d extents(bbox.extents().x(), bbox.extents().y(), bbox.extents().z());
65 openvdb::Vec3d centerIdx = minIdx + extents * 0.5;
66
67 _rotation = openvdb::math::Transform::createLinearTransform(
68 openvdb::math::Mat4d::identity());
69 _rotation->preTranslate( centerIdx);
70 _rotation->preRotate(rotation, openvdb::math::Z_AXIS);
71 _rotation->preTranslate(-centerIdx);
72 _grid->setTransform(_rotation);
73 } else {
74 _rotation = openvdb::math::Transform::createLinearTransform(
75 openvdb::math::Mat4d::identity());
76 _grid->setTransform(_rotation);
77 }
78
79 bbox = _grid->evalActiveVoxelBoundingBox();
80 _shape[0] = bbox.max().x() - bbox.min().x();
81 _shape[1] = bbox.max().y() - bbox.min().y();
82 _shape[2] = bbox.max().z() - bbox.min().z();
83 _origin[0] = bbox.min().x();
84 _origin[1] = bbox.min().y();
85 _origin[2] = bbox.min().z();
86
87 _vdbFile.close();
88 #else
89 throw std::runtime_error("VDB support not enabled, set FEATURE := VDB");
90 #endif
91 }
92
94 return _shape * _volumeDeltaX;
95 }
97 return _origin * _volumeDeltaX;
98 }
99
100 bool operator()(T output[], const T physR[]) override{
101 #ifdef FEATURE_VDB
102 openvdb::Vec3d worldPos(
103 util::floor(physR[0] / _volumeDeltaX),
104 util::floor(physR[1] / _volumeDeltaX),
105 util::floor(physR[2] / _volumeDeltaX)
106 );
107
108 openvdb::Vec3d indexPos = _grid->transform().worldToIndex(worldPos);
109 openvdb::Coord location(
110 static_cast<int>(std::round(indexPos.x())),
111 static_cast<int>(std::round(indexPos.y())),
112 static_cast<int>(std::round(indexPos.z()))
113 );
114
115 openvdb::FloatGrid::Accessor accessor = _grid->getAccessor();
116 if(accessor.isValueOn(location)){
117 output[0] = _grid->tree().getValue(location);
118 } else {
119 output[0] = 1;
120 }
121 return true;
122 #endif
123 return false;
124 }
125
126};
127
128}
129
130#endif
AnalyticalF are applications from DD to XD, where X is set by the constructor.
AnalyticalPorosityVolumeF(std::string fileName, T volumeDeltaX, T rotation=0)
Reads first grid of VDB file with spacing volumeDeltaX and applies rotation [rad].
bool operator()(T output[], const T physR[]) override
has to be implemented for 'every' derived class
Plain old scalar vector.
ADf< T, DIM > floor(const ADf< T, DIM > &a)
Definition aDiff.h:869
Top level namespace for all of OpenLB.