Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
qmom_field_function.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
5
6#include <deal.II/base/exceptions.h>
7
8#include <string>
9#include <vector>
10
11#ifdef LETHE_WITH_QMOMKIT
12# include <qmomkit/distributions.hpp>
13#endif
14
15DeclException3(QMOMScalarExpressionsCountError,
16 std::string,
17 unsigned int,
18 unsigned int,
19 << "The scalars function of the QMOM " << arg1 << " has " << arg2
20 << " expressions, but the QMOM physics transports " << arg3
21 << " scalars (the \"number of scalars\" of the QMOM subsection)."
22 << std::endl
23 << "Give one expression per scalar, separated by semicolons.");
24
25DeclException1(QMOMNegativeNumberDensityError,
26 double,
27 << "The number density of the particle size distribution of "
28 << "the QMOM physics must be positive or zero, but it is "
29 << arg1 << " particles/m^3.");
30
31DeclException1(QMOMKitMomentsError,
32 std::string,
33 << "QMOMKit could not compute the moments of the particle size "
34 << "distribution of the QMOM physics: " << arg1 << std::endl
35 << "Check the parameters of the distribution.");
36
37template <int dim>
40 const unsigned int n_scalars,
41 const unsigned int n_moments,
42 const std::string &description)
43 : Function<dim>(n_scalars + n_moments)
47{
48 // A function has at least one component: without scalars, the scalars
49 // function is ignored
50 AssertThrow(n_scalars == 0 || functions.scalars->n_components == n_scalars,
51 QMOMScalarExpressionsCountError(description,
52 functions.scalars->n_components,
53 n_scalars));
54
55#ifndef LETHE_WITH_QMOMKIT
56 // The moments of a particle size distribution are computed by QMOMKit
57 AssertThrow(functions.distribution_type ==
59 QMOMWithoutQMOMKitError());
60#endif
61}
62
63template <int dim>
64double
66 const unsigned int component) const
67{
68 AssertIndexRange(component, this->n_components);
69
70 // Components 0 to n_scalars - 1 are the scalars, and component
71 // n_scalars + k is the moment M_k
72 if (component < n_scalars)
73 return functions.scalars->value(p, component);
74
75 return compute_moments(p)[component - n_scalars];
76}
77
78template <int dim>
79void
81 Vector<double> &values) const
82{
83 AssertDimension(values.size(), this->n_components);
84
85 for (unsigned int i_scalar = 0; i_scalar < n_scalars; ++i_scalar)
86 values[i_scalar] = functions.scalars->value(p, i_scalar);
87
88 const std::vector<double> moments = compute_moments(p);
89 for (unsigned int i_moment = 0; i_moment < n_moments; ++i_moment)
90 values[n_scalars + i_moment] = moments[i_moment];
91}
92
93template <int dim>
94void
96 Vector<double> &values) const
97{
98 AssertDimension(values.size(), n_scalars);
99
100 for (unsigned int i_scalar = 0; i_scalar < n_scalars; ++i_scalar)
101 values[i_scalar] = functions.scalars->value(p, i_scalar);
102}
103
104template <int dim>
105void
107 Vector<double> &values) const
108{
109 AssertDimension(values.size(), n_moments);
110
111 const std::vector<double> moments = compute_moments(p);
112 for (unsigned int i_moment = 0; i_moment < n_moments; ++i_moment)
113 values[i_moment] = moments[i_moment];
114}
115
116template <int dim>
117void
119{
120 Function<dim>::set_time(new_time);
121 functions.set_time(new_time);
122}
123
124template <int dim>
125std::vector<double>
127 [[maybe_unused]] const Point<dim> &p) const
128{
129 using DistributionType = Parameters::QMOMDistributionType;
130
131 std::vector<double> moments(n_moments, 0.);
132 if (functions.distribution_type == DistributionType::none)
133 return moments;
134
135#ifdef LETHE_WITH_QMOMKIT
136 // Number density followed by the parameters of the distribution
137 Vector<double> distribution_values(functions.distribution->n_components);
138 functions.distribution->vector_value(p, distribution_values);
139 const double number_density = distribution_values[0];
140
141 AssertThrow(number_density >= 0.,
142 QMOMNegativeNumberDensityError(number_density));
143
144 // Without particles, all the moments are zero whatever the distribution
145 if (number_density <= 0.)
146 return moments;
147
148 qmomkit::DistributionSpecification distribution;
149 switch (functions.distribution_type)
150 {
151 case DistributionType::normal:
152 distribution =
153 qmomkit::NormalDistribution{.mean = distribution_values[1],
154 .standard_deviation =
155 distribution_values[2]};
156 break;
157 case DistributionType::lognormal:
158 distribution =
159 qmomkit::LogNormalDistribution{.normal_mean = distribution_values[1],
160 .normal_standard_deviation =
161 distribution_values[2]};
162 break;
163 case DistributionType::gamma:
164 distribution =
165 qmomkit::GammaDistribution{.shape = distribution_values[1],
166 .scale = distribution_values[2]};
167 break;
168 case DistributionType::weibull:
169 distribution =
170 qmomkit::WeibullDistribution{.shape = distribution_values[1],
171 .scale = distribution_values[2]};
172 break;
173 case DistributionType::beta:
174 distribution =
175 qmomkit::BetaDistribution{.alpha = distribution_values[1],
176 .beta = distribution_values[2]};
177 break;
178 case DistributionType::chi:
179 distribution = qmomkit::ChiDistribution{.degrees_of_freedom =
180 distribution_values[1]};
181 break;
182 case DistributionType::chi_squared:
183 distribution = qmomkit::ChiSquaredDistribution{
184 .degrees_of_freedom = distribution_values[1]};
185 break;
186 case DistributionType::none:
187 Assert(false, ExcInternalError());
188 break;
189 }
190
191 const qmomkit::RawMomentResult result =
192 qmomkit::compute_raw_moments(distribution, n_moments - 1, number_density);
193 AssertThrow(result.ok(), QMOMKitMomentsError(result.message));
194
195 for (unsigned int i_moment = 0; i_moment < n_moments; ++i_moment)
196 moments[i_moment] = result.moments[i_moment];
197#endif
198
199 return moments;
200}
201
202template class QMOMFieldFunction<2>;
203template class QMOMFieldFunction<3>;
Function of all the QMOM fields, built from the functions of the scalars and from the particle size d...
double value(const Point< dim > &p, const unsigned int component) const override
Evaluate one component of the function: scalar component if component < n_scalars,...
void vector_value(const Point< dim > &p, Vector< double > &values) const override
Evaluate all the components of the function. The moments are computed once for all of them.
Parameters::QMOMFunctions< dim > functions
Functions of the scalars and particle size distribution.
const unsigned int n_scalars
Number of scalars.
void moment_values(const Point< dim > &p, Vector< double > &values) const
Evaluate the moments only, indexed from 0 regardless of the scalars. The moments are computed once fo...
void set_time(const double new_time) override
Set the time of the function and of the underlying functions.
QMOMFieldFunction(const Parameters::QMOMFunctions< dim > &functions, const unsigned int n_scalars, const unsigned int n_moments, const std::string &description)
Constructor. Verifies that the scalars function has one component per scalar, unless there are no sca...
const unsigned int n_moments
Number of moments.
std::vector< double > compute_moments(const Point< dim > &p) const
Compute the raw moments of the particle size distribution at a point.
void scalar_values(const Point< dim > &p, Vector< double > &values) const
Evaluate the scalars only, indexed from 0 regardless of the moments.
QMOMDistributionType
Family of the particle size distribution from which the moments of the QMOM physics are computed....
DeclException3(QMOMScalarExpressionsCountError, std::string, unsigned int, unsigned int,<< "The scalars function of the QMOM "<< arg1<< " has "<< arg2<< " expressions, but the QMOM physics transports "<< arg3<< " scalars (the \"number of scalars\" of the QMOM subsection)."<< std::endl<< "Give one expression per scalar, separated by semicolons.")
DeclException1(QMOMNegativeNumberDensityError, double,<< "The number density of the particle size distribution of "<< "the QMOM physics must be positive or zero, but it is "<< arg1<< " particles/m^3.")
Vector-valued function of the fields of the quadrature method of moments (QMOM) physics: the scalars ...
Functions of space and time prescribing the fields of the QMOM physics. They are used for the initial...