Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
flow_control.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2020, 2023-2024, 2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
4#include <core/utilities.h>
5
7
8#include <deal.II/base/exceptions.h>
9
10#include <cmath>
11#include <fstream>
12#include <iomanip>
13#include <limits>
14#include <string>
15#include <utility>
16
17template <int dim>
19 const Parameters::DynamicFlowControl &flow_control)
20 : target_average_velocity(flow_control.average_velocity_0)
21 , initial_beta(flow_control.beta_0)
22 , alpha(flow_control.alpha)
23 , beta_threshold(flow_control.beta_threshold)
24 , flow_direction(flow_control.flow_direction)
25{
26 // The beta force is applied to the component flow_direction of a tensor of
27 // dimension dim. Tensor::operator[] only checks its index in Debug mode, so
28 // an invalid flow direction would write out of bounds in Release mode.
29 AssertThrow(
30 !flow_control.enable_flow_control || std::cmp_less(flow_direction, dim),
31 ExcMessage("The 'flow direction' of the 'flow control' subsection is " +
32 std::to_string(flow_direction) +
33 ", but it must be smaller than the dimension of the problem (" +
34 std::to_string(dim) + "). Set it to 0 for x, 1 for y" +
35 (dim == 3 ? " or 2 for z." : ".")));
36}
37
38template <int dim>
39double
40FlowControl<dim>::proportional_flow_controller(const double &average_velocity_n,
41 const double &dt) const
42{
43 return 0.5 * alpha * (target_average_velocity - average_velocity_n) / dt;
44}
45
46template <int dim>
47double
48FlowControl<dim>::main_flow_controller(const double &average_velocity_n,
49 const double &dt) const
50{
51 const double new_beta =
53 (target_average_velocity - 2 * average_velocity_n +
55 dt;
56
57 // If desired average velocity is reached, new beta only maintains the force
58 // to keep the flow at the desired value. If calculated beta is
59 // negative it is set to 0 to avoid +/- force.
60 if (target_average_velocity * new_beta < 0 &&
62 return 0.0;
63
64 return new_beta;
65}
66
67template <int dim>
68void
69FlowControl<dim>::calculate_beta(const double &average_velocity_n,
70 const double &dt,
71 const unsigned int &step_number)
72{
73 // Check if disabling the force-free deceleration and modify the force-free
74 // velocity band (> step time 1):
75 // If average velocity target is reached without any beta force applied,
76 // the force-free deceleration is disabled. It means that the average
77 // velocity has now reached the target value without force OR that slowing
78 // down the flow with the only pressure drop (no force applied) is
79 // ineffective (too big pressure drop). In case the average velocity is in
80 // the initial band but the pressure drop is too small to slow down the
81 // flow, the band factor is halved towards 1. Otherwise, the average velocity
82 // may take a lot of time to reach the target value only with pressure drop.
83 if (std::abs(previous_beta) < zero_beta_tolerance && step_number > 1)
84 {
85 if (std::abs(average_velocity_n) < std::abs(target_average_velocity))
87 else if (std::abs(previous_average_velocity - average_velocity_n) <
88 std::abs(average_velocity_n - target_average_velocity) &&
93 }
94
95 double new_beta = 0.0;
96 if (step_number == 0)
97 {
98 // No force applied at first time step.
99 // At this moment, calculate_beta is not called at this time step but
100 // the condition prevents a beta calculation if the way it's called
101 // changes.
102 new_beta = 0.0;
103 }
104 else if (step_number == 1)
105 {
106 // Apply initial beta force if user defined (> than 0)
107 // otherwise, it is calculated by the proportional controller
108 new_beta = (std::abs(initial_beta) > zero_beta_tolerance ?
110 proportional_flow_controller(average_velocity_n, dt));
111 }
112 else if (std::abs(average_velocity_n) > std::abs(target_average_velocity) &&
113 std::abs(average_velocity_n) < force_free_velocity_band_factor *
114 std::abs(target_average_velocity) &&
116 {
117 // If the average velocity is between targeted value and the upper limit
118 // of the band (meaning it is slightly over the value) and it has not
119 // reached once the value during the simulation (force-free deceleration
120 // is allowed), it decreases by itself (pressure drop).
121 new_beta = 0.0;
122 }
123 else if (step_number == 2)
124 {
125 // Since the main controller takes the previous beta value, it is better
126 // to use the proportional controller for this second time step since
127 // it won't be affected by the first beta value that may cause overshoot
128 // or undershoot.
129 new_beta = proportional_flow_controller(average_velocity_n, dt);
130 }
131 else
132 {
133 // Main controller
134 new_beta = main_flow_controller(average_velocity_n, dt);
135 }
136
137 // If the new beta is in the user defined threshold of the old beta, the new
138 // beta is kept as the previous beta. This could allow avoiding unnecessary
139 // reassembly of the matrix because of the force term when reuse matrix option
140 // is enabled for the non-linear solver
141 if (std::abs(new_beta - previous_beta) <
142 std::abs(beta_threshold * previous_beta))
143 new_beta = previous_beta;
144
145 // Setting beta coefficient to the tensor according to the flow direction.
146 beta[flow_direction] = new_beta;
147
148 // Assigning values of this time step as previous values prior next
149 // calculation.
150 previous_beta = new_beta;
151 previous_average_velocity = average_velocity_n;
152}
153
154template <int dim>
155void
156FlowControl<dim>::save(const std::string &prefix) const
157{
158 const std::string filename = prefix + ".flowcontrol";
159 std::ofstream output(filename);
160
161 // The state is written with enough digits to be read back exactly, so that a
162 // restarted simulation continues as if it had not been interrupted.
163 output << std::setprecision(std::numeric_limits<double>::max_digits10);
164 output << "Flow control" << std::endl;
165 output << "Previous_beta " << previous_beta << std::endl;
166 output << "Previous_average_velocity " << previous_average_velocity
167 << std::endl;
168 output << "No_force " << force_free_deceleration_is_allowed << std::endl;
169 output << "Threshold_factor " << force_free_velocity_band_factor << std::endl;
170}
171
172template <int dim>
173void
174FlowControl<dim>::read(const std::string &prefix)
175{
176 const std::string filename = prefix + ".flowcontrol";
177 check_file_exists(filename,
178 "checkpoint file of the dynamic flow control, given by "
179 "'subsection restart' - 'set filename'");
180 std::ifstream input(filename);
181
182 std::string buffer;
183 std::getline(input, buffer);
184 input >> buffer >> previous_beta;
185 input >> buffer >> previous_average_velocity;
186 input >> buffer >> force_free_deceleration_is_allowed;
187 input >> buffer >> force_free_velocity_band_factor;
188
189 AssertThrow(!input.fail(),
190 ExcMessage(
191 "The checkpoint file of the dynamic flow control <" + filename +
192 "> could not be read: it is truncated or does not have the "
193 "expected format. Restart from a complete checkpoint written "
194 "by a simulation with the flow control enabled."));
195}
196
197template class FlowControl<2>;
198template class FlowControl<3>;
Dynamic flow controller that drives the average velocity of the flow towards a target value.
double alpha
Relaxation coefficient of the controllers.
double force_free_velocity_band_factor
double previous_average_velocity
Average velocity of the previous time step.
double beta_threshold
void read(const std::string &prefix)
Read the state of the flow controller from a checkpoint file.
unsigned int flow_direction
Direction of the flow (0 for x, 1 for y and 2 for z).
bool force_free_deceleration_is_allowed
double main_flow_controller(const double &average_velocity_n, const double &dt) const
Calculate a beta force from the previous beta force and the average velocities of the last two time s...
static constexpr double zero_beta_tolerance
Below this magnitude, a beta force is considered to be zero.
double proportional_flow_controller(const double &average_velocity_n, const double &dt) const
Calculate a beta force from the last average velocity only. It is used for the first and second time ...
double target_average_velocity
Target average velocity.
void calculate_beta(const double &average_velocity_n, const double &dt, const unsigned int &step_number)
Calculate the beta force coefficient applied during the current time step.
double previous_beta
Beta force applied during the previous time step.
Tensor< 1, dim > beta
void save(const std::string &prefix) const
Save the state of the flow controller to a checkpoint file.
double initial_beta
static constexpr double minimum_force_free_velocity_band_factor
FlowControl(const Parameters::DynamicFlowControl &flow_control)
Constructor of the flow controller.
void check_file_exists(const std::string &file_name, const std::string &file_description)
Verify that a file that Lethe must read exists and can be opened, and raise an error stated in the us...
Definition utilities.cc:225
FlowControl - Set average velocity on a boundary (CFD) or the domain (CFD-DEM).