version 0.4.4
controlvtkwriter.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 The Ikarus Developers ikarus@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
10#pragma once
11#include <string>
12
13#include <dune/functions/gridfunctions/discreteglobalbasisfunction.hh>
14#include <dune/grid/io/file/vtk/subsamplingvtkwriter.hh>
15
16#include <Eigen/Core>
17
21
22namespace Ikarus {
23
31template <typename B>
33{
34 using Basis = B;
35 static constexpr int components = Basis::LocalView::Tree::degree() == 0 ? 1 : Basis::LocalView::Tree::degree();
36
37public:
46 ControlSubsamplingVertexVTKWriter(const Basis& basis, int refinementLevels = 0)
47 : basis_{basis},
48 vtkWriter_(basis.gridView(), Dune::refinementLevels(refinementLevels)) {}
49
50 template <typename BC>
52 this->subscribe(bc, [&](ControlMessages message, const BC::State& state) { this->update(message, state); });
53 return *this;
54 }
55
65 auto setFieldInfo(std::string&& name, Dune::VTK::FieldInfo::Type type, std::size_t size,
66 Dune::VTK::Precision prec = Dune::VTK::Precision::float32) {
67 fieldInfo_ = Dune::VTK::FieldInfo(std::move(name), type, size, prec);
68 isFieldInfoSet_ = true;
69 }
70
76 auto setFileNamePrefix(std::string&& name) { prefixString_ = std::move(name); }
77
87 void update(ControlMessages message, const Concepts::ControlRoutineState auto& state) {
88 assert(isFieldInfoSet_ && "You need to call setFieldInfo first!");
89 switch (message) {
91 auto disp = Dune::Functions::makeDiscreteGlobalBasisFunction<Dune::FieldVector<double, components>>(
92 basis_, state.domain.globalSolution());
93 vtkWriter_.addVertexData(disp, fieldInfo_);
94 vtkWriter_.write(prefixString_ + std::to_string(step_++));
95 } break;
96 default:
97 break; // default: do nothing when notified
98 }
99 }
100
101private:
102 const Basis& basis_;
103 Dune::SubsamplingVTKWriter<typename Basis::GridView> vtkWriter_;
104 int step_{0};
105 Dune::VTK::FieldInfo fieldInfo_{"Default", Dune::VTK::FieldInfo::Type::scalar, 1};
106 std::string prefixString_{};
107 bool isFieldInfoSet_{false};
108};
109
110#ifndef DOXYGEN
111template <typename Basis>
112ControlSubsamplingVertexVTKWriter(const Basis&, int) -> ControlSubsamplingVertexVTKWriter<Basis>;
113#endif
114
115} // namespace Ikarus
Enums for observer messages.
Implementation of the observer design pattern with broadcasters.
Definition: assemblermanipulatorbuildingblocks.hh:22
ControlMessages
Enum class defining control-routine-related messages.
Definition: broadcastermessages.hh:18
Definition: utils/dirichletvalues.hh:32
def basis(gv, tree)
Definition: basis.py:10
ControlSubsamplingVertexVTKWriter class for writing VTK files with subsampling based on control messa...
Definition: controlvtkwriter.hh:33
void update(ControlMessages message, const Concepts::ControlRoutineState auto &state)
Implementation of the update method.
Definition: controlvtkwriter.hh:87
ControlSubsamplingVertexVTKWriter & subscribeTo(BC &bc)
Definition: controlvtkwriter.hh:51
auto setFileNamePrefix(std::string &&name)
Set the file name prefix for VTK files.
Definition: controlvtkwriter.hh:76
ControlSubsamplingVertexVTKWriter(const Basis &basis, int refinementLevels=0)
Constructor for ControlSubsamplingVertexVTKWriter.
Definition: controlvtkwriter.hh:46
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:65
Implements a listener.
Definition: listener.hh:28
auto subscribe(Broadcaster &broadcaster, std::function< void(typename Broadcaster::MessageType, const typename Broadcaster::State &)> callback)
Function to subscribe to a broadcaster with a given function (either a lambda, std::function or funct...
Definition: listener.hh:41
Concept to check if a type represents a control routine state.
Definition: utils/concepts.hh:649
Several concepts.