OpenLB 1.7
Loading...
Searching...
No Matches
consoleWriter.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2022 Nicolas Hafen, Mathias J. Krause
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
25
26#ifndef CONSOLE_WRITER_H
27#define CONSOLE_WRITER_H
28
29namespace olb {
30
31
32template<typename T, unsigned D, typename DATA>
34 using loc = int;
35 using Pos = Vector<loc,D>; //Has dimension of source data
36private:
37 DATA& _data;
38 Pos _extent = Pos(0);
39 std::ostream& os;
40 const char* _charEmpty=" ";
41 Vector<unsigned,2> _spacing = {2,0};
42
43 //Make sequence by using multiplier n
44 std::string sequence(std::string a, unsigned n){
45 std::string seq="";
46 for (unsigned i=0; i<n; ++i){
47 seq+=a;
48 }
49 return seq;
50 }
51
52public:
53
54 //Construct from DATA and ostream
55 ConsoleWriter(std::ostream& str, DATA& data)
56 : _data(data), os(str)
57 {
58 _extent = data.getExtent();
59 }
60
61 //Print Writer info
62 void print(){
63 os << "DataType: " << typeid(DATA).name() << std::endl;
64 os << "Extent=" << _extent << std::endl;
65 }
66
68 {
69 _spacing = spacing;
70 }
71
72 //Write method with function f
73 template<typename F>
74 void write(F f, unsigned sliceAxis=2, int slicePosOnAxis=0){
75 for (loc iY=0; iY < _extent[1]; ++iY) {
76 for (loc iX=0; iX < _extent[0]; ++iX) {
77 if constexpr (D == 3) {
78 int iZ = slicePosOnAxis;
79 loc iXeval;
80 loc iYeval;
81 loc iZeval;
82 if (sliceAxis==0){ //YZ-Slice
83 iXeval = iZ;
84 iYeval = iX;
85 iZeval = _extent[1]-iY-1;
86 } else if (sliceAxis==1){ //XZ-Slice
87 iXeval = iX;
88 iYeval = iZ;
89 iZeval = _extent[1]-iY-1;
90 } else { //XY-Slice
91 iXeval = iX;
92 iYeval = _extent[1]-iY-1;
93 iZeval = iZ;
94 }
95 //Evaluate function f
96 if constexpr (std::is_invocable_v<F, DATA&, LatticeR<D>>) {
97 os << f(_data,{iXeval,iYeval,iZeval}); //Version B: call with braced enclosed dimensions
98 } else {
99 os << f(_data,iXeval,iYeval,iZeval); //Version A: call with dimensions separat
100 }
101 } else {
102 //Project coordinates
103 loc iXeval = iX;
104 loc iYeval = _extent[1]-iY-1;
105 //Evaluate function f
106 if constexpr (std::is_invocable_v<F, DATA&, LatticeR<D>>) {
107 os << f(_data,{iXeval,iYeval}); //Version B: call with braced enclosed dimensions
108 } else {
109 os << f(_data,iXeval,iYeval); //Version A: call with dimensions separat
110 }
111 }
112 os << (iX==_extent[0]-1? "" : sequence(_charEmpty,_spacing[0]));
113 }
114 os << std::endl;
115 //Add _spacing (if needed)
116 if (iY<_extent[1]-1){
117 for (unsigned i=0; i<_spacing[1]; ++i){
118 for (loc iX=0; iX < _extent[0]; ++iX) {
119 os << (iX==_extent[0]-1? _charEmpty : sequence(_charEmpty,_spacing[0]+1));
120 }
121 os << std::endl;
122 }
123 }
124 }
125 }
126
128
129 //Write method for separate GETABLE (e.g. SuperGeometry)
130 template<typename GETABLE>
131 void writeGetable(GETABLE& getable, unsigned sliceAxis=2, int slicePosOnAxis=0){
132 write([&](auto& data, LatticeR<D> pos){
133 auto res = getable.get(pos);
134 return res;
135 });
136 }
137
138 //Write method for separate indicator (e.g. BlockIndicator)
139 template<typename INDICATOR>
140 void writeIndicator(INDICATOR& indicator, unsigned sliceAxis=2, int slicePosOnAxis=0){
141 write([&](auto& data, LatticeR<D> pos){
142 bool out[1];
143 indicator( out, pos.data() );
144 return out[0];
145 });
146 }
147
148 //Write method for separate phys indicator (e.g. IndicatorCircle)
149 template<typename INDICATOR>
150 void writeIndicator(BlockGeometry<T,D>& blockGeometry, INDICATOR& indicator, unsigned sliceAxis=2, int slicePosOnAxis=0){
151 write([&](auto& data, LatticeR<D> pos){
152 bool out[1];
153 Vector<T,D> physPos;
154 blockGeometry.getPhysR( physPos.data(), pos );
155 indicator( out, physPos.data() );
156 return out[0];
157 });
158 }
159
160 //Write method for separate functor (e.g. BlockLatticeVelocity)
161 template<unsigned DIM, typename FUNCTOR, typename S=T>
162 void writeFunctor(FUNCTOR& functor, unsigned sliceAxis=2, int slicePosOnAxis=0){
163 write([&](auto& data, LatticeR<D> pos){
164 Vector<S,DIM> out;
165 functor( out.data(), pos.data() );
166 if constexpr (DIM==1){
167 return out[0];
168 } else {
169 return out;
170 }
171 });
172 }
173
174};
175
176
177} // namespace olb
178
179#endif
Representation of a block geometry.
Vector< T, D > getPhysR(LatticeR< D > latticeR)
void write(F f, unsigned sliceAxis=2, int slicePosOnAxis=0)
ConsoleWriter(std::ostream &str, DATA &data)
void writeIndicator(INDICATOR &indicator, unsigned sliceAxis=2, int slicePosOnAxis=0)
void writeFunctor(FUNCTOR &functor, unsigned sliceAxis=2, int slicePosOnAxis=0)
void writeGetable(GETABLE &getable, unsigned sliceAxis=2, int slicePosOnAxis=0)
Non-generic common functions.
void adjustSettings(Vector< unsigned, 2 > spacing)
void writeIndicator(BlockGeometry< T, D > &blockGeometry, INDICATOR &indicator, unsigned sliceAxis=2, int slicePosOnAxis=0)
constexpr const T * data() const any_platform
Definition vector.h:161
Top level namespace for all of OpenLB.