8#include <deal.II/lac/lapack_full_matrix.h>
20 const double &d_average,
21 const double &d_standard_deviation,
22 const unsigned int &prn_seed,
23 const double min_cutoff,
24 const double max_cutoff,
48 const double mv = d_average;
49 const double mv2 = mv * mv;
51 const double sv = d_standard_deviation;
52 const double sv2 = sv * sv;
55 LAPACKFullMatrix<double> system_matrix;
56 Vector<double> system_rhs;
58 double residual_l2_norm = 1.;
59 unsigned int iteration_counter = 0;
63 double mu_n = d_average;
65 double sigma_n = d_standard_deviation;
67 while (residual_l2_norm > 1.e-12 && iteration_counter < 100)
69 system_matrix.reinit(2);
72 const double mn = mu_n;
73 const double mn2 = mn * mn;
74 const double mn3 = mn2 * mn;
75 const double mn4 = mn2 * mn2;
76 const double mn5 = mn4 * mn;
78 const double sn = sigma_n;
79 const double sn2 = sn * sn;
80 const double sn3 = sn2 * sn;
81 const double sn4 = sn2 * sn2;
84 const double dR1_sigma_n =
85 6. * (2. * (sn3 + sn * mn2) - mv * sn * mn);
87 const double dR1_mu_n =
88 12. * sn2 * mn + 4 * mn3 - 3. * mv * (sn2 + mn2);
90 const double dR2_sigma_n =
91 12. * sn3 * (5. * mn - 2. * mv) +
92 2. * sn * (10. * mn3 - 12. * mn2 * mv + 3. * mn * mv2) -
95 const double dR2_mu_n = 15. * sn4 +
96 sn2 * (30. * mn2 - 24. * mn * mv + 3. * mv2) +
97 5. * mn4 + 3. * mn2 * mv2 - 8. * mn3 * mv -
98 sv2 * (3. * sn2 + 3. * mn2);
100 system_matrix.set(0, 0, dR1_sigma_n);
101 system_matrix.set(0, 1, dR1_mu_n);
102 system_matrix.set(1, 0, dR2_sigma_n);
103 system_matrix.set(1, 1, dR2_mu_n);
107 3. * sn4 + 6. * sn2 * mn2 + mn4 - mv * (3. * sn2 * mn + mn3);
109 const double R2 = 3. * sn4 * (5. * mn - 2. * mv) +
110 sn2 * (10. * mn3 - 12. * mn2 * mv + 3. * mn * mv2) +
111 mn5 + mn3 * mv2 - 2. * mn4 * mv -
112 sv2 * (3. * sn2 * mn + mn3);
119 system_matrix.compute_lu_factorization();
120 system_matrix.solve(system_rhs);
123 sigma_n += system_rhs[0];
124 mu_n += system_rhs[1];
127 residual_l2_norm = system_rhs.l2_norm();
131 iteration_counter < 100.,
132 ExcMessage(
"The numbered weighted mean and standard deviation of "
133 "the normal distribution has not been found from the volume "
134 "weighted values provided in the parameter file. To solve "
135 "this issue, define the distribution as number-based."));
147 "The \"standard deviation\" parameter is too "
148 "high relative to the \"average diameter\" "
149 "parameter. This would results with frequent negative "
150 "diameter values. To fix this problem, please change "
151 "the value of those two parameters or define a "
152 "\"minimum diameter cutoff\" bigger than \"0.\""));
164 ExcMessage(
"The \"minimum diameter cutoff\" parameter needs to"
165 " be smaller than the \"maximum diameter cutoff\"."));
170 const unsigned int &number_of_particles)
172 std::vector<double> particle_sizes;
173 particle_sizes.reserve(number_of_particles);
177 unsigned int n_created_diameter = 0;
178 while (n_created_diameter < number_of_particles)
180 const double temp_diameter = distribution(
gen);
184 n_created_diameter++;
185 particle_sizes.push_back(temp_diameter);
188 return particle_sizes;
205 const unsigned int particle_type,
206 const ConditionalOStream &pcout)
214 const double q_min = 0.5 * (1.0 + std::erf(z_min / std::numbers::sqrt2));
215 const double q_max = 0.5 * (1.0 + std::erf(z_max / std::numbers::sqrt2));
217 pcout <<
"The particle size distribution of particle type " << particle_type
218 <<
" is normal." << std::endl;
220 if (q_min > 0.25 || q_max < 0.75)
222 <<
"Warning: The minimal and maximal cutoffs of the distribution are at "
223 << q_min * 100. <<
" and " << q_max * 100. <<
" respectively."
226 pcout <<
"The minimal and maximal cutoffs of the distribution are at "
227 << q_min * 100. <<
" and " << q_max * 100. <<
" respectively."
232 const double &d_average,
233 const double &d_standard_deviation,
234 const unsigned int &prn_seed,
238 &distribution_weighting_type)
241 1. + Utilities::fixed_power<2>(d_standard_deviation / d_average))))
248 mu_ln = std::log(d_average) - 0.5 * Utilities::fixed_power<2>(
sigma_ln);
255 mu_ln = std::log(d_average) - 3.5 * Utilities::fixed_power<2>(
sigma_ln);
264 "The \"standard deviation\" parameter is to "
265 "high relative to the \"average diameter\" "
266 "parameter. This would results with frequent negative "
267 "diameter values. To fix this problem, please change "
268 "the value of those two parameters or define a "
269 "\"minimum diameter cutoff\" bigger than \"0.\""));
282 "The \"minimum diameter cutoff\" parameter need to be smaller "
283 "than the \"maximum diameter cutoff\"."));
289 const unsigned int &number_of_particles)
291 std::vector<double> particle_sizes;
292 particle_sizes.reserve(number_of_particles);
296 unsigned int n_created_diameter = 0;
297 while (n_created_diameter < number_of_particles)
299 const double temp_diameter = distribution(
gen);
303 n_created_diameter++;
304 particle_sizes.push_back(temp_diameter);
307 return particle_sizes;
324 const unsigned int particle_type,
325 const ConditionalOStream &pcout)
331 const double q_min = 0.5 * (1.0 + std::erf(z_min / std::numbers::sqrt2));
332 const double q_max = 0.5 * (1.0 + std::erf(z_max / std::numbers::sqrt2));
334 pcout <<
"The particle size distribution of particle type " << particle_type
335 <<
" is lognormal." << std::endl;
337 if (q_min > 0.25 || q_max < 0.75)
339 <<
"Warning: The minimal and maximal cutoffs of the distribution are at "
340 << q_min * 100. <<
" and " << q_max * 100. <<
" percentiles respectively."
343 pcout <<
"The minimal and maximal cutoffs of the distribution are at "
344 << q_min * 100. <<
" and " << q_max * 100.
345 <<
" percentiles respectively." << std::endl;
356 const unsigned int &number_of_particles)
358 std::vector<double> particle_sizes;
359 particle_sizes.reserve(number_of_particles);
361 for (
unsigned int n = 0; n < number_of_particles; ++n)
362 particle_sizes.push_back(this->diameter_value);
364 return particle_sizes;
381 const unsigned int particle_type,
382 const ConditionalOStream &pcout)
384 pcout <<
"The particle size distribution of particle type " << particle_type
385 <<
" is uniform." << std::endl;
389 const std::vector<double> &d_list,
390 const std::vector<double> &d_probabilities,
391 const unsigned int &prn_seed,
392 const double min_cutoff,
393 const double max_cutoff,
395 &distribution_weighting_type,
397 const bool interpolate)
417 this->dia_max_cutoff = max_cutoff;
423 AssertThrow(number_d_values == d_probabilities.size(),
424 ExcMessage(
"The number of diameter values and "
425 "probability values need to be the same."));
428 ExcMessage(
"The \"custom distribution diameters values\" "
429 "parameter is not defined properly. Make sure to "
430 "define the diameter values in ascending order."));
435 std::ranges::is_sorted(d_probabilities) &&
436 d_probabilities.back() <= 1.0 + 1e-5,
437 ExcMessage(
"When using a custom distribution defined as a "
438 "CDF, the probability values need to be defined in"
439 " ascending order and the last probability value needs to be"
443 AssertThrow(d_probabilities[0] == 0.,
444 ExcMessage(
"When using a custom distribution defined"
445 " as a CDF with interpolation, the first "
446 "probability value needs to be equal to 0."));
453 AssertThrow(d_probabilities[0] == 0.,
454 ExcMessage(
"When using the custom distribution "
455 "defined as a PDF with interpolation, the "
456 "first probability value needs to be equal"
459 AssertThrow(d_probabilities.back() == 0.,
460 ExcMessage(
"When using the custom distribution defined"
461 " as a PDF with interpolation, the last "
462 "probability value needs to be equal to 0."));
466 AssertThrow(std::abs(std::accumulate(d_probabilities.begin(),
467 d_probabilities.end(),
470 ExcMessage(
"When using the custom distribution "
471 "defined as a PDF without interpolation, "
472 "probability values are used as fraction, "
473 "thus the sum of every probability value "
474 "should be equal to 1."));
502 for (
unsigned int i = 1; i < number_d_values; ++i)
505 0.5 * (d_probabilities[i - 1] + d_probabilities[i]) *
509 for (
unsigned int i = 0; i < number_d_values; ++i)
517 for (
unsigned int i = 1; i < number_d_values; ++i)
521 for (
unsigned int i = 0; i < number_d_values; ++i)
534 double fv_integral = 0.0;
535 for (
unsigned int i = 0; i < number_d_values - 1; ++i)
537 (d_probabilities[i] + d_probabilities[i + 1]) *
541 fv.resize(number_d_values);
542 for (
unsigned int i = 0; i < number_d_values; ++i)
543 fv[i] = d_probabilities[i] / fv_integral;
555 std::vector<double> segment_integral(number_d_values - 1, 0.0);
558 for (
unsigned int i = 0; i < number_d_values - 1; ++i)
566 const double b_i = (
fv[i + 1] -
fv[i]) / (d_high - d_low);
567 const double a_i =
fv[i] - b_i * d_low;
577 const double F_high =
578 -a_i / (2.0 * d_high * d_high) - b_i / d_high;
580 -a_i / (2.0 * d_low * d_low) - b_i / d_low;
582 segment_integral[i] = F_high - F_low;
589 for (
unsigned int i = 1; i < number_d_values; ++i)
598 std::vector<double> dfn(number_d_values - 1, 0.0);
599 double number_fraction_sum = 0.0;
603 for (
unsigned int i = 0; i < number_d_values - 1; ++i)
613 double this_bin_volume_fraction =
614 d_probabilities[i + 1] - d_probabilities[i];
638 const double weight =
639 (d_low + d_high) / (2.0 * std::pow(d_low * d_high, 2));
644 dfn[i] = this_bin_volume_fraction * weight;
649 number_fraction_sum += dfn[i];
655 for (
unsigned int i = 1; i < number_d_values - 1; ++i)
665 std::vector<double> volume_fraction(number_d_values, 0.);
671 volume_fraction[0] = d_probabilities[0];
672 for (
unsigned int i = 1; i < number_d_values; ++i)
674 d_probabilities[i] - d_probabilities[i - 1];
679 volume_fraction = d_probabilities;
681 std::vector<double> number_based_pdf(number_d_values);
683 for (
unsigned int i = 0; i < number_d_values; ++i)
685 number_based_pdf[i] =
689 n_tot += number_based_pdf[i];
694 for (
unsigned int i = 1; i < number_d_values; ++i)
703 const unsigned int &number_of_particles)
705 std::vector<double> particle_sizes;
706 particle_sizes.reserve(number_of_particles);
707 unsigned int n_created_diameter = 0;
710 std::uniform_real_distribution<> dis(0.0, 1 - 1e-12);
714 while (n_created_diameter < number_of_particles)
717 const double u_global = dis(
gen);
724 const unsigned int index_high =
726 const unsigned int index_low = index_high - 1;
733 const double delta_cdf = cdf_high - cdf_low;
736 const double u_local = (u_global - cdf_low) / delta_cdf;
738 double sampled_diameter;
755 (
fv[index_high] -
fv[index_low]) / (d_high - d_low);
756 const double a_i =
fv[index_low] - b_i * d_low;
758 auto antideriv = [a_i, b_i](
const double d) {
759 return -a_i / (2.0 * d * d) - b_i / d;
762 const double F_low = antideriv(d_low);
770 const double A = 2.0 * C;
771 const double B = 2.0 * b_i;
773 constexpr double tol = 1e-12;
775 if (std::abs(A) < tol)
784 "Degenerate analytical CDF inversion encountered."));
786 sampled_diameter = -a_i / B;
788 std::clamp(sampled_diameter, d_low, d_high);
792 const double discriminant =
793 std::max(B * B - 4.0 * A * a_i, 0.0);
794 const double sqrt_disc = std::sqrt(discriminant);
796 const double root1 = (-B + sqrt_disc) / (2.0 * A);
797 const double root2 = (-B - sqrt_disc) / (2.0 * A);
799 if (root1 >= d_low - tol && root1 <= d_high + tol)
800 sampled_diameter = std::clamp(root1, d_low, d_high);
801 else if (root2 >= d_low - tol && root2 <= d_high + tol)
802 sampled_diameter = std::clamp(root2, d_low, d_high);
807 "Failed to invert the analytical CDF "
808 "within the selected diameter interval."));
814 const double inv_d_low2 = 1.0 / (d_low * d_low);
815 const double inv_d_high2 = 1.0 / (d_high * d_high);
819 std::sqrt(inv_d_low2 - u_local * (inv_d_low2 - inv_d_high2));
828 n_created_diameter++;
829 particle_sizes.push_back(sampled_diameter);
835 while (n_created_diameter < number_of_particles)
838 const double u = dis(
gen);
845 const unsigned int index_high =
855 n_created_diameter++;
856 particle_sizes.push_back(sampled_diameter);
860 return particle_sizes;
877 const unsigned int particle_type,
878 const ConditionalOStream &pcout)
880 pcout <<
"The particle size distribution of particle type " << particle_type
881 <<
" is custom." << std::endl;
virtual std::vector< double > particle_size_sampling(const unsigned int &number_of_particles) override
Carries out the size sampling of each particle inserted at an insertion time step for the histogram d...
std::vector< double > number_based_cdf
Vector containing cumulative probabilities associated with the diameter_values vector....
std::mt19937 gen
Random number generator for the diameter selection.
double find_max_diameter() override
Find the maximum diameter of the custom distribution.
const std::vector< double > diameter_values
const bool interpolate_diameter_values
Indicates whether the diameter values are interpolated between the input values during sampling.
CustomDistribution(const std::vector< double > &d_list, const std::vector< double > &d_probabilities, const unsigned int &prn_seed, const double min_cutoff, const double max_cutoff, const Parameters::Lagrangian::DistributionWeightingType &distribution_weighting_type, const Parameters::Lagrangian::ProbabilityFunctionType &function_type_in, const bool interpolate)
The constructor stores the parameters necessary to define the histogram distribution.
void print_psd_declaration_string(const unsigned int particle_type, const ConditionalOStream &pcout) override
Print the declaration string relative to the particle size distribution used.
const Parameters::Lagrangian::ProbabilityFunctionType function_type
Defines if the custom distribution is a probability density function (PDF) or a cumulative density fu...
double find_min_diameter() override
Find the minimum diameter of the custom distribution.
Distribution(const Parameters::Lagrangian::DistributionWeightingType &distribution_weighting_type)
double dia_min_cutoff
Minimal cutoff diameters values.
double dia_max_cutoff
Maximum cutoff diameters values.
const Parameters::Lagrangian::DistributionWeightingType weighting_type
double mu_ln
Average diameter of the normal distribution.
double find_min_diameter() override
Find the minimum diameter a normal distribution.
double find_max_diameter() override
Find the maximum diameter of the normal distribution.
void print_psd_declaration_string(const unsigned int particle_type, const ConditionalOStream &pcout) override
Print the declaration string relative to the particle size distribution used.
virtual std::vector< double > particle_size_sampling(const unsigned int &number_of_particles) override
Carries out the size sampling of each particle inserted at an insertion time step for the normal dist...
const double sigma_ln
Standard deviation of the normal distribution.
LogNormalDistribution(const double &d_average, const double &d_standard_deviation, const unsigned int &prn_seed, double min_cutoff, double max_cutoff, const Parameters::Lagrangian::DistributionWeightingType &distribution_weighting_type)
The constructor stores the parameters necessary to define the normal distribution.
std::mt19937 gen
Random number generator for the diameter selection.
NormalDistribution(const double &d_average, const double &d_standard_deviation, const unsigned int &prn_seed, double min_cutoff, double max_cutoff, const Parameters::Lagrangian::DistributionWeightingType &distribution_weighting_type)
The constructor stores the parameters necessary to define the normal distribution.
double find_min_diameter() override
Find the minimum diameter a normal distribution.
double standard_deviation
Standard deviation of the normal distribution.
virtual std::vector< double > particle_size_sampling(const unsigned int &number_of_particles) override
Carries out the size sampling of each particle inserted at an insertion time step for the normal dist...
double find_max_diameter() override
Find the maximum diameter of the normal distribution.
std::mt19937 gen
Random number generator for the diameter selection.
double diameter_average
Average diameter of the normal distribution.
void print_psd_declaration_string(const unsigned int particle_type, const ConditionalOStream &pcout) override
Print the declaration string relative to the particle size distribution used.
DistributionWeightingType
Weighting basis for particle size distributions.
@ volume_based
Volume-based distribution weighting.
@ number_based
Number-based distribution weighting.
@ CDF
Cumulative density function.
@ PDF
Probability density function.