PSD.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef MECURYDPM_PSD_H
6 #define MECURYDPM_PSD_H
7 
8 #include <fstream>
9 #include <Logger.h>
10 #include <vector>
11 #include <Tools/csvReader.h>
12 #include <random>
13 #include <utility>
14 #include <algorithm>
15 #include "iostream"
16 #include "ExtendedMath.h"
17 #include "RNG.h"
19 #include "Helpers/Helpers.h"
20 
21 using mathsFunc::square;
22 using mathsFunc::cubic;
23 
46 class PSD
47 {
48 
49 public:
55  enum class TYPE
56  {
65  };
66 
70  PSD();
71 
77  PSD(std::unique_ptr<PSD> psdPtr) : particleSizeDistribution_(psdPtr->particleSizeDistribution_),
80  index_(psdPtr->index_),
81  random_(psdPtr->random_) {}
82 
86  PSD(const PSD& other);
87 
91  ~PSD();
92 
96  PSD* copy() const;
97 
101  void printPSD() const;
102 
107 
113 
118 
123 
127  void setPSDFromVector(std::vector<DistributionElements> psd, TYPE PSDType);
128 
132  void setPSDFromCSV(const std::string& fileName, TYPE PSDType, bool headings = false, Mdouble
133  unitScalingFactorRadii = 1.0);
134 
138  void setDistributionUniform(Mdouble radMin, Mdouble radMax, int numberOfBins);
139 
143  void setDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins);
144 
145  static PSD getDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins) {
146  PSD psd;
147  psd.setDistributionNormal(mean, standardDeviation, numberOfBins);
148  return psd;
149  }
150 
154  void setDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins);
155 
156  static PSD getDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins) {
157  PSD psd;
158  psd.setDistributionLogNormal(mean, standardDeviation, numberOfBins);
159  return psd;
160  }
161 
165  void setDistributionPhiNormal(Mdouble D50, Mdouble standardDeviationinPhi, int numberOfBins);
166 
171 
176 
181 
186 
193 
198 
203 
209  void cutoffCumulativeNumber(Mdouble quantileMin, Mdouble quantileMax, Mdouble minPolydispersity = 0.1)
210  {
211  cutoffCumulativeDistribution(TYPE::CUMULATIVE_NUMBER_DISTRIBUTION, quantileMin, quantileMax, minPolydispersity);
212  }
213 
217  void cutoffCumulativeDistribution(TYPE CDFType, Mdouble quantileMin, Mdouble quantileMax, Mdouble minPolydispersity = 0.1);
218 
224  void cutoffAndSqueezeCumulative(Mdouble quantileMin, Mdouble quantileMax, Mdouble squeeze,
225  Mdouble minPolydispersity = 0.1)
226  {
227  cutoffAndSqueezeCumulativeDistribution(TYPE::CUMULATIVE_NUMBER_DISTRIBUTION, quantileMin, quantileMax, squeeze, minPolydispersity);
228  }
229 
233  void cutoffAndSqueezeCumulativeDistribution(TYPE CDFType, Mdouble quantileMin, Mdouble quantileMax, Mdouble squeeze, Mdouble minPolydispersity = 0.1);
234 
238  Mdouble getMinRadius() const;
239 
243  Mdouble getMaxRadius() const;
244 
248  std::vector<DistributionElements> getParticleSizeDistribution() const;
249 
253  std::vector<DistributionElements> getParticleSizeDistributionByType(TYPE psdType, Mdouble scalingFactor = 1.0) const;
254 
260  void setParticleSizeDistribution(std::vector<DistributionElements>);
261 
265  void scaleParticleSize(double scale);
266 
274  double scaleParticleSizeAuto(int numberOfParticles, double targetVolume, bool allowScaleDown = false);
275 
279  int getInsertedParticleNumber() const;
280 
285 
289  void decrementVolumePerClass(Mdouble volume);
290 
294  Mdouble getRadius(int index) const;
295 
299  Mdouble getNumberDx(Mdouble x) const;
300 
304  Mdouble getLengthDx(Mdouble x) const;
305 
309  Mdouble getAreaDx(Mdouble x) const;
310 
314  Mdouble getVolumeDx(Mdouble x) const;
315 
319  Mdouble getRadiusByQuantile(Mdouble quantile) const;
320 
325 
330 
334  Mdouble getSizeRatio() const;
335 
339  void cutHighSizeRatio();
340 
341 // /*!
342 // * \brief compute raw momenta of the user defined PSD.
343 // */
344 // void computeRawMomenta();
345 //
346 // /*!
347 // * \brief compute central momenta of the user defined PSD.
348 // */
349 // void computeCentralMomenta();
350 //
351 // /*!
352 // * \brief compute standardised momenta of the user defined PSD.
353 // */
354 // void computeStandardisedMomenta();
355 //
356 // /*!
357 // * \brief get momenta of the user defined PSD.
358 // */
359 // std::array<Mdouble, 6> getMomenta() const;
360 
361 
365  std::vector< std::pair <Mdouble,Mdouble> > convertPdfPhiToPdfMeter(Mdouble meaninPhi, Mdouble standardDeviationinPhi, int numberOfBins);
366 
370  Mdouble computeD50(std::vector< std::pair <Mdouble,Mdouble> > vectorOfPair);
371 
375  static std::vector<Mdouble> linspace(Mdouble Min, Mdouble Max, int numberOfBins);
376 
380  void setFixedSeed(int seed)
381  {
382 // random_.setLinearCongruentialGeneratorParmeters(0,0,1);
383  random_.setRandomSeed(seed);
384  }
385 
389  friend bool operator<(const DistributionElements& l, const DistributionElements& r);
390 
394  friend bool operator<(const DistributionElements& l, Mdouble r);
395 
399  friend std::ostream& operator<<(std::ostream& os, DistributionElements& p);
400 
404  friend std::istream& operator>>(std::istream& is, DistributionElements& p);
405 
410 
411 private:
415  std::vector<DistributionElements> particleSizeDistribution_;
416 
422  std::vector<int> nParticlesPerClass_;
423 
431  std::vector<Mdouble> volumePerClass_;
432 
436  int index_;
437 
442 };
443 
444 
445 #endif //MECURYDPM_PSD_H
#define MERCURYDPM_DEPRECATED
Definition: GeneralDefine.h:16
float * p
Definition: Tutorial_Map_using.cpp:9
class of DistributionElements which stores internalVariables and probabilities of a distribution....
Definition: DistributionElements.h:13
Contains a vector with radii and probabilities of a user defined particle size distribution (PSD)
Definition: PSD.h:47
Mdouble getRadius(int index) const
get a radius at a certain index of the particleSizeDistribution_
Definition: PSD.cc:1254
void setDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
create a PSD vector for a normal distribution.
Definition: PSD.cc:318
void cutoffCumulativeDistribution(TYPE CDFType, Mdouble quantileMin, Mdouble quantileMax, Mdouble minPolydispersity=0.1)
Cut off the PSD at the given quantiles, with the PSD in the provided CDF form.
Definition: PSD.cc:821
Mdouble drawSample()
Draw a sample radius from a CUMULATIVE_NUMBER_DISTRIBUTION.
Definition: PSD.cc:92
MERCURYDPM_DEPRECATED void convertProbabilityDensityNumberDistributionToProbabilityDensityVolumeDistribution()
convert a PROBABILITYDENSITY_NUMBER_DISTRIBUTION to a PROBABILITYDENSITY_VOLUME_DISTRIBUTION.
Definition: PSD.cc:762
Mdouble getVolumetricMeanRadius() const
get a volumetric mean radius of the PSD.
Definition: PSD.cc:977
Mdouble getQuantileByRadius(Mdouble radius) const
Calculates the quantile corresponding to a certain radius.
Definition: PSD.cc:961
friend bool operator<(const DistributionElements &l, const DistributionElements &r)
determines if a certain value of the PSD vector is lower than another one. Used for std::lower_bound(...
Definition: PSD.cc:1264
void convertCumulativeToCumulativeNumberDistribution(TYPE CDFType)
Convert any CDF to a CUMULATIVE_NUMBER_DISTRIBUTION.
Definition: PSD.cc:771
std::vector< DistributionElements > getParticleSizeDistributionByType(TYPE psdType, Mdouble scalingFactor=1.0) const
Gets the PSD vector, converted to the preferred type.
Definition: PSD.cc:1067
int getInsertedParticleNumber() const
Get the number of particles already inserted into the simulation.
Definition: PSD.cc:1190
Mdouble getMinRadius() const
Get smallest radius of the PSD.
Definition: PSD.cc:1037
void printPSD() const
Prints radii and probabilities of the PSD vector.
Definition: PSD.cc:54
void decrementVolumePerClass(Mdouble volume)
Decrement volumePerClass_ counter.
Definition: PSD.cc:1003
void convertProbabilityDensityNumberDistributionToProbabilityDensity(TYPE PDFType)
Convert a PROBABILITYDENSITY_NUMBER_DISTRIBUTION to any PDF.
Definition: PSD.cc:706
Mdouble getVolumeDx(Mdouble x) const
Calculate a certain diameter (e.g. D10, D50, D90, etc.) from a percentile x of the volume based PSD.
Definition: PSD.cc:929
void scaleParticleSize(double scale)
Scales all particle sizes by a factor.
Definition: PSD.cc:1124
void convertCumulativeToProbabilityDensity()
Converts a CDF to a PDF by derivation.
Definition: PSD.cc:630
static PSD getDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
Definition: PSD.h:145
void decrementNParticlesPerClass()
Decrement nParticlesPerClass_ counter.
Definition: PSD.cc:993
Mdouble computeD50(std::vector< std::pair< Mdouble, Mdouble > > vectorOfPair)
compute the mass-based D_50 of the paired diameter and probabilityDensity.
Definition: PSD.cc:478
void setPSDFromCSV(const std::string &fileName, TYPE PSDType, bool headings=false, Mdouble unitScalingFactorRadii=1.0)
read in the PSD vector with probabilities and radii saved in a .csv file.
Definition: PSD.cc:590
friend std::ostream & operator<<(std::ostream &os, DistributionElements &p)
Writes to output stream.
Definition: PSD.cc:1304
friend std::istream & operator>>(std::istream &is, DistributionElements &p)
Reads from input stream.
Definition: PSD.cc:1293
Mdouble insertManuallyByVolume(Mdouble volume)
Draw sample radius manually per size class and check the volumeAllowed of each size class to insert t...
Definition: PSD.cc:117
void setDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
create a PSD vector for a normal distribution.
Definition: PSD.cc:353
void setFixedSeed(int seed)
set a fixed seed for the random number generator; this is used for the PSDSelfTest to reproduce resul...
Definition: PSD.h:380
double scaleParticleSizeAuto(int numberOfParticles, double targetVolume, bool allowScaleDown=false)
Scales all particle sizes, such that the total volume of N particles approximately equals the target ...
Definition: PSD.cc:1131
RNG random_
Definition: PSD.h:441
friend Mdouble operator==(DistributionElements l, Mdouble r)
Determines if a certain value of the PSD vector is equal to a double.
Definition: PSD.cc:1284
PSD * copy() const
Creates a copy on the heap and returns a pointer.
Definition: PSD.cc:42
PSD(std::unique_ptr< PSD > psdPtr)
Constructor; sets everything to 0 or default. This constructor is mainly used in MercuryPBM where sma...
Definition: PSD.h:77
void validateProbabilityDensityDistribution()
Validates if the integral of the PDF equals to unity.
Definition: PSD.cc:246
std::vector< Mdouble > volumePerClass_
Definition: PSD.h:431
~PSD()
Destructor; default destructor.
void convertCumulativeNumberDistributionToCumulative(TYPE CDFType)
Convert a CUMULATIVE_NUMBER_DISTRIBUTION to any CDF.
Definition: PSD.cc:794
PSD()
Constructor; sets everything to 0 or default.
Definition: PSD.cc:10
void convertProbabilityDensityToCumulative()
Converts a PDF to a CDF by integration.
Definition: PSD.cc:615
std::vector< int > nParticlesPerClass_
Definition: PSD.h:422
void convertProbabilityDensityToProbabilityDensityNumberDistribution(TYPE PDFType)
Convert any PDF to a PROBABILITYDENSITY_NUMBER_DISTRIBUTION.
Definition: PSD.cc:649
std::vector< DistributionElements > particleSizeDistribution_
Definition: PSD.h:415
std::vector< DistributionElements > getParticleSizeDistribution() const
Get the PSD vector.
Definition: PSD.cc:1055
std::vector< std::pair< Mdouble, Mdouble > > convertPdfPhiToPdfMeter(Mdouble meaninPhi, Mdouble standardDeviationinPhi, int numberOfBins)
compute central momenta of the user defined PSD. ‍/ void computeCentralMomenta();
Definition: PSD.cc:446
void cutoffAndSqueezeCumulativeDistribution(TYPE CDFType, Mdouble quantileMin, Mdouble quantileMax, Mdouble squeeze, Mdouble minPolydispersity=0.1)
Cut off the PSD at the given quantiles, with the PSD in the provided CDF form, and make it less polyd...
Definition: PSD.cc:863
Mdouble getNumberDx(Mdouble x) const
Calculate a certain diameter (e.g. D10, D50, D90, etc.) from a percentile x of the number based PSD.
Definition: PSD.cc:892
Mdouble getMaxRadius() const
Get largest radius of the PSD.
Definition: PSD.cc:1046
TYPE
Enum class which stores the possible types of CDFs and PDFs. Particle size distributions can be repre...
Definition: PSD.h:56
@ PROBABILITYDENSITY_VOLUME_DISTRIBUTION
@ CUMULATIVE_AREA_DISTRIBUTION
@ CUMULATIVE_VOLUME_DISTRIBUTION
@ PROBABILITYDENSITY_NUMBER_DISTRIBUTION
@ PROBABILITYDENSITY_LENGTH_DISTRIBUTION
@ PROBABILITYDENSITY_AREA_DISTRIBUTION
@ CUMULATIVE_LENGTH_DISTRIBUTION
@ CUMULATIVE_NUMBER_DISTRIBUTION
void cutHighSizeRatio()
Check if the size ratio is too high and cut it.
Definition: PSD.cc:1023
int index_
Definition: PSD.h:436
void setPSDFromVector(std::vector< DistributionElements > psd, TYPE PSDType)
Sets the PSD from a vector with DistributionElements.
Definition: PSD.cc:513
Mdouble getAreaDx(Mdouble x) const
Calculate a certain diameter (e.g. D10, D50, D90, etc.) from a percentile x of the area based PSD.
Definition: PSD.cc:916
Mdouble getLengthDx(Mdouble x) const
Calculate a certain diameter (e.g. D10, D50, D90, etc.) from a percentile x of the length based PSD.
Definition: PSD.cc:903
static PSD getDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
Definition: PSD.h:156
Mdouble getSizeRatio() const
get the size ratio (width) of the PSD.
Definition: PSD.cc:1012
static std::vector< Mdouble > linspace(Mdouble Min, Mdouble Max, int numberOfBins)
create a vector of linearly spaced values.
MERCURYDPM_DEPRECATED void cutoffCumulativeNumber(Mdouble quantileMin, Mdouble quantileMax, Mdouble minPolydispersity=0.1)
cutoff the PSD at given quantiles.
Definition: PSD.h:209
void validateCumulativeDistribution()
Validates if a CDF starts with zero and adds up to unity.
Definition: PSD.cc:191
MERCURYDPM_DEPRECATED void cutoffAndSqueezeCumulative(Mdouble quantileMin, Mdouble quantileMax, Mdouble squeeze, Mdouble minPolydispersity=0.1)
cutoff the PSD at given quantiles and make it less polydisperse by squeezing it.
Definition: PSD.h:224
Mdouble getRadiusByQuantile(Mdouble quantile) const
Calculate the quantile of the PSD.
Definition: PSD.cc:942
void setDistributionPhiNormal(Mdouble D50, Mdouble standardDeviationinPhi, int numberOfBins)
create a PSD vector for a normal distribution in Phi Units that has the demanded D50.
Definition: PSD.cc:393
void setDistributionUniform(Mdouble radMin, Mdouble radMax, int numberOfBins)
create a PSD vector for a uniform distribution.
Definition: PSD.cc:294
MERCURYDPM_DEPRECATED void setParticleSizeDistribution(std::vector< DistributionElements >)
set the PSD by a suitable vector.
Definition: PSD.cc:1119
This is a class that generates random numbers i.e. named the Random Number Generator (RNG).
Definition: RNG.h:32
void setRandomSeed(unsigned long int new_seed)
This is the seed for the random number generator (note the call to seed_LFG is only required really i...
Definition: RNG.cc:32
r
Definition: UniformPSDSelfTest.py:20
radius
Definition: UniformPSDSelfTest.py:15
string fileName
Definition: UniformPSDSelfTest.py:10
T square(const T val)
squares a number
Definition: ExtendedMath.h:86
T cubic(const T val)
calculates the cube of a number
Definition: ExtendedMath.h:95
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
list x
Definition: plotDoE.py:28