OpenLB 1.8.1
Loading...
Searching...
No Matches
vtkSurfaceWriter.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2024 Adrian Kummerlaender
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 IO_VTK_SURFACE_WRITER_HH
25#define IO_VTK_SURFACE_WRITER_HH
26
27#ifdef FEATURE_VTK
28
29#include "vtkSurfaceWriter.h"
30
31#include <vtkPointData.h>
32#include <vtkTriangle.h>
33#include <vtkFloatArray.h>
34#include <vtkXMLUnstructuredGridWriter.h>
35#include <vtkXMLPUnstructuredGridWriter.h>
36
37namespace olb {
38
39template<typename T>
40void vtkSurfaceWriter<T>::init()
41{
42 _grid = vtkSmartPointer<vtkUnstructuredGrid>::New();
43 _points = vtkSmartPointer<vtkPoints>::New();
44 _cells = vtkSmartPointer<vtkCellArray>::New();
45 _localTriangles.clear();
46
47 std::vector<Cuboid3D<T>*> localCuboids;
48 for (int globC=0; globC < _cuboidDecomposition.cuboids().size(); ++globC) {
49 if (_loadBalancer.isLocal(globC)) {
50 localCuboids.emplace_back(&_cuboidDecomposition.cuboids()[globC]);
51 }
52 }
53
54 std::size_t pointId = 0;
55 for (STLtriangle<T>& triangle : _surfaceI.getMesh().getTriangles()) {
56 // Check whether triangle is fully contained in local cuboids (incl. overlap)
57 bool isTriangleContained = true;
58 for (int iD=0; iD < 3; ++iD) {
59 bool isPointContained = false;
60 for (auto* cuboid : localCuboids) {
61 if (cuboid->isInside(triangle.point[iD], 1)) {
62 isPointContained = true;
63 break;
64 }
65 }
66 isTriangleContained &= isPointContained;
67 }
68
69 if (isTriangleContained) {
70 _localTriangles.emplace_back(&triangle);
71 for (const auto& point : triangle.point) {
72 _points->InsertNextPoint(point[0], point[1], point[2]);
73 }
74
75 vtkSmartPointer<vtkTriangle> cell = vtkSmartPointer<vtkTriangle>::New();
76 cell->GetPointIds()->SetId(0, pointId++);
77 cell->GetPointIds()->SetId(1, pointId++);
78 cell->GetPointIds()->SetId(2, pointId++);
79 _cells->InsertNextCell(cell);
80 }
81 }
82
83 _grid->SetPoints(_points);
84 _grid->SetCells(VTK_TRIANGLE, _cells);
85}
86
87template<typename T>
88void vtkSurfaceWriter<T>::write(int iT)
89{
90 _grid->GetPointData()->Reset();
91 std::vector<vtkSmartPointer<vtkFloatArray>> data(_f.size());
92 for (std::size_t iF=0; iF < _f.size(); ++iF) {
93 data[iF] = vtkSmartPointer<vtkFloatArray>::New();
94 data[iF]->SetName(_f[iF]->getName().c_str());
95 data[iF]->SetNumberOfComponents(_f[iF]->getTargetDim());
96 _grid->GetPointData()->AddArray(data[iF]);
97 }
98
99 std::string filePath = singleton::directories().getVtkOutDir()
100 + createFileName(_fileName, iT)
101 + ".pvtu";
102
103 if (singleton::mpi().isMainProcessor()) {
104 vtkSmartPointer<vtkXMLPUnstructuredGridWriter> multiWriter = vtkSmartPointer<vtkXMLPUnstructuredGridWriter>::New();
105 multiWriter->SetInputData(_grid);
106 multiWriter->SetNumberOfPieces(singleton::mpi().getSize());
107 multiWriter->SetFileName(filePath.c_str());
108 multiWriter->SetUseSubdirectory(true);
109 multiWriter->SetStartPiece(0);
110 multiWriter->SetEndPiece(singleton::mpi().getSize()-1);
111 multiWriter->SetWriteSummaryFile(1);
112 multiWriter->Write();
113 }
114
115 for (const STLtriangle<T>* triangle : _localTriangles) {
116 for (const auto& point : triangle->point) {
117 for (std::size_t iF=0; iF < _f.size(); ++iF) {
118 std::vector<T> result(_f[iF]->getTargetDim(), 0);
119 _f[iF](result.data(), point.data());
120 data[iF]->InsertNextTuple(result.data());
121 }
122 }
123 }
124
125 vtkSmartPointer<vtkXMLPUnstructuredGridWriter> multiWriter = vtkSmartPointer<vtkXMLPUnstructuredGridWriter>::New();
126 multiWriter->SetWriteSummaryFile(0);
127 multiWriter->SetInputData(_grid);
128 multiWriter->SetNumberOfPieces(singleton::mpi().getSize());
129 multiWriter->SetFileName(filePath.c_str());
130 multiWriter->SetUseSubdirectory(true);
131 multiWriter->SetStartPiece(singleton::mpi().getRank());
132 multiWriter->SetEndPiece(singleton::mpi().getRank());
133 multiWriter->Write();
134
135 _f.clear();
136}
137
138}
139
140#endif
141
142#endif
std::string getVtkOutDir() const
Definition singleton.h:103
T triangle(Vector< T, 2 > p, Vector< T, 2 > a, Vector< T, 2 > b, Vector< T, 2 > c) any_platform
Exact signed distance to the surface of two-dimensional triangle.
Definition sdf.h:152
MpiManager & mpi()
Directories & directories()
Definition singleton.h:162
Top level namespace for all of OpenLB.
std::string createFileName(std::string name)
for .pvd masterFile
Definition fileName.hh:34
std::string getName(OperatorScope scope)
Returns human-readable name of scope.