version 0.4
controlvtkwriter.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10#include "observer.hh"
11#include "observermessages.hh"
12
13#include <string>
14
15#include <dune/functions/gridfunctions/discreteglobalbasisfunction.hh>
16#include <dune/grid/io/file/vtk/subsamplingvtkwriter.hh>
17
18#pragma GCC diagnostic push
19#pragma GCC diagnostic ignored "-Wswitch-enum"
20
21namespace Ikarus {
22
30 template <typename Basis>
31 class ControlSubsamplingVertexVTKWriter : public IObserver<ControlMessages> {
32 static constexpr int components = Basis::LocalView::Tree::degree() == 0 ? 1 : Basis::LocalView::Tree::degree();
33
34 public:
44 ControlSubsamplingVertexVTKWriter(const Basis& p_basis, const Eigen::VectorXd& sol, int refinementLevels = 0)
45 : basis{&p_basis}, vtkWriter(p_basis.gridView(), Dune::refinementLevels(refinementLevels)), solution{&sol} {}
46
56 auto setFieldInfo(std::string&& name, Dune::VTK::FieldInfo::Type type, std::size_t size,
57 Dune::VTK::Precision prec = Dune::VTK::Precision::float32) {
58 fieldInfo = Dune::VTK::FieldInfo(std::move(name), type, size, prec);
59 isFieldInfoSet = true;
60 }
61
67 auto setFileNamePrefix(std::string&& p_name) { prefixString = std::move(p_name); }
68
77 void updateImpl(ControlMessages message) final {
78 assert(isFieldInfoSet && "You need to call setFieldInfo first!");
79 switch (message) {
81 auto disp = Dune::Functions::makeDiscreteGlobalBasisFunction<Dune::FieldVector<double, components>>(
82 *basis, *solution);
83 vtkWriter.addVertexData(disp, fieldInfo);
84 vtkWriter.write(prefixString + std::to_string(step++));
85 } break;
86 default:
87 break; // default: do nothing when notified
88 }
89 }
90
91 private:
92 Basis const* basis;
93 Dune::SubsamplingVTKWriter<typename Basis::GridView> vtkWriter;
94 Eigen::VectorXd const* solution;
95 int step{0};
96 Dune::VTK::FieldInfo fieldInfo{"Default", Dune::VTK::FieldInfo::Type::scalar, 1};
97 std::string prefixString{};
98 bool isFieldInfoSet{false};
99 };
100} // namespace Ikarus
101#pragma GCC diagnostic pop
Enums for observer messages.
Implementation of the observer design pattern.
ControlMessages
Enum class defining control-routine-related messages.
Definition: observermessages.hh:16
Definition: simpleassemblers.hh:21
Definition: resultevaluators.hh:17
Wrapper class for a hierarchical basis constructed from a pre-basis.
Definition: utils/basis.hh:29
ControlSubsamplingVertexVTKWriter class for writing VTK files with subsampling based on control messa...
Definition: controlvtkwriter.hh:31
auto setFieldInfo(std::string &&name, Dune::VTK::FieldInfo::Type type, std::size_t size, Dune::VTK::Precision prec=Dune::VTK::Precision::float32)
Set field information for the VTK file.
Definition: controlvtkwriter.hh:56
void updateImpl(ControlMessages message) final
Implementation of the update method.
Definition: controlvtkwriter.hh:77
auto setFileNamePrefix(std::string &&p_name)
Set the file name prefix for VTK files.
Definition: controlvtkwriter.hh:67
ControlSubsamplingVertexVTKWriter(const Basis &p_basis, const Eigen::VectorXd &sol, int refinementLevels=0)
Constructor for ControlSubsamplingVertexVTKWriter.
Definition: controlvtkwriter.hh:44
Generic observer interface for the Observer design pattern. See for a description of the design patt...
Definition: observer.hh:25