ryujin 2.5.0 revision c64172211cf21344cb9b9b0f3a1266aa6400d392
Loading...
Searching...
No Matches
equation.h
Go to the documentation of this file.
1//
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3// Copyright (C) 2023 - 2026 by the ryujin authors
4//
5
6#pragma once
7
8#include <ryujin/base/compile_time_options.h>
12
13#include <deal.II/base/exceptions.h>
14#include <deal.II/base/mpi.h>
15#include <deal.II/base/parameter_acceptor.h>
16#include <deal.II/base/utilities.h>
17
18#include <algorithm>
19#include <functional>
20#include <map>
21#include <memory>
22#include <string>
23
24namespace ryujin
25{
45 template <typename Setup>
46 void write_parameter_files(const std::string &name,
47 bool write_detailed_description,
48 const Setup &setup)
49 {
50 {
51 auto &prm = dealii::ParameterAcceptor::prm;
52
53 /*
54 * Workaround: The Scheduler is constructed last and declares the
55 * "A - Scheduler" section last. Add an entry to the section so that it
56 * shows up first.
57 */
58 prm.enter_subsection("A - Scheduler");
59 prm.declare_entry("basename", "test");
60 prm.leave_subsection();
61
62 [[maybe_unused]] const auto objects = setup();
63
64 if (dealii::Utilities::MPI::this_mpi_process(MPI_COMM_SELF) == 0) {
65 if (write_detailed_description)
66 prm.print_parameters(
67 "default_parameters-" + name + "-description.prm",
68 dealii::ParameterHandler::OutputStyle::KeepDeclarationOrder);
69
70 prm.print_parameters(
71 "default_parameters-" + name + ".prm",
72 dealii::ParameterHandler::OutputStyle::Short |
73 dealii::ParameterHandler::OutputStyle::KeepDeclarationOrder);
74 }
75
76 // all objects have to go out of scope, see
77 // https://github.com/dealii/dealii/issues/15111
78 }
79
80 dealii::ParameterAcceptor::clear();
81 }
82
83
91 struct Equation {
96 std::function<void(
97 int dim, const std::string &parameter_file, const MPI_Comm &mpi_comm)>
99
104 std::function<void()> create_parameter_files;
105 };
106
107
114 std::map<std::string, Equation> &equations();
115
116
127 template <typename HD, typename PD = StubParabolicDescription>
129 {
130 public:
134 EquationRegistration(const std::string &name)
135 {
136 const auto run = [](int dim,
137 const std::string &parameter_file,
138 const MPI_Comm &mpi_communicator) {
139 switch (dim) {
140 case 1:
141 run_simulation<1>(parameter_file, mpi_communicator);
142 break;
143 case 2:
144 run_simulation<2>(parameter_file, mpi_communicator);
145 break;
146 case 3:
147 run_simulation<3>(parameter_file, mpi_communicator);
148 break;
149 default:
150 AssertThrow(false, dealii::ExcInternalError());
151 }
152 };
153
154 const auto create_parameter_files = [name] {
155 create_parameter_file<1>(name, false);
156 create_parameter_file<2>(name, true);
157 create_parameter_file<3>(name, false);
158 };
159
160 const Equation equation{run, create_parameter_files};
161
162 const bool inserted = equations().emplace(name, equation).second;
163 AssertThrow(inserted,
164 dealii::ExcMessage("The equation »" + name +
165 "« is registered more than once."));
166 }
167
168 private:
173 template <int dim>
174 static void run_simulation(const std::string &parameter_file,
175 const MPI_Comm &mpi_comm)
176 {
177 Simulation<HD, PD, dim, NUMBER> simulation(mpi_comm);
178 TaskComposition<dim, NUMBER> task_composition(simulation);
179
180 dealii::ParameterAcceptor::initialize(parameter_file);
181
182 task_composition.run();
183 }
184
188 template <int dim>
189 static void create_parameter_file(const std::string &name,
190 bool write_detailed_description)
191 {
192 std::string base_name = name;
193 std::ranges::replace(base_name, ' ', '_');
194 base_name += "-" + std::to_string(dim) + "d";
195
196 write_parameter_files(base_name, write_detailed_description, [&name] {
197 /*
198 * The parameters "dimension" and "equation" (see source/main.cc), set
199 * to this equation:
200 */
201 auto &prm = dealii::ParameterAcceptor::prm;
202 prm.enter_subsection("B - Equation");
203 prm.declare_entry("dimension",
204 std::to_string(dim),
205 dealii::Patterns::Integer(),
206 "The spatial dimension");
207 prm.declare_entry(
208 "equation", name, dealii::Patterns::Anything(), "The PDE system");
209 prm.leave_subsection();
210
211 /*
212 * Create temporary objects for the sole purpose of populating the
213 * ParameterAcceptor::prm object.
214 */
215 struct Objects {
216 Simulation<HD, PD, dim, NUMBER> simulation{MPI_COMM_SELF};
217 TaskComposition<dim, NUMBER> task_composition{simulation};
218 };
219 return std::make_unique<Objects>();
220 });
221 }
222 };
223} // namespace ryujin
EquationRegistration(const std::string &name)
Definition equation.h:134
std::map< std::string, Equation > & equations()
void write_parameter_files(const std::string &name, bool write_detailed_description, const Setup &setup)
Definition equation.h:46
std::function< void(int dim, const std::string &parameter_file, const MPI_Comm &mpi_comm)> run
Definition equation.h:98
std::function< void()> create_parameter_files
Definition equation.h:104