PSD.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef MECURYDPM_PSD_H
27 #define MECURYDPM_PSD_H
28 
29 #include <fstream>
30 #include <Logger.h>
31 #include <vector>
32 #include <Tools/csvReader.h>
33 #include <random>
34 #include "iostream"
35 #include "ExtendedMath.h"
36 #include "RNG.h"
38 
39 using mathsFunc::square;
40 using mathsFunc::cubic;
41 
64 class PSD
65 {
66 
67 public:
73  enum class TYPE
74  {
83  };
84 
88  PSD();
89 
93  PSD(const PSD& other);
94 
98  ~PSD();
99 
103  PSD* copy() const;
104 
108  void printPSD() const;
109 
114 
120 
125 
130 
135  void setPSDFromVector(std::vector<DistributionElements> psd, TYPE PSDType);
136 
140  void setPSDFromCSV(const std::string& fileName, TYPE PSDType, bool headings = false, Mdouble
141  unitScalingFactorRadii = 1.0);
142 
146  void setDistributionUniform(Mdouble radMin, Mdouble radMax, int numberOfBins);
147 
151  void setDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins);
152 
153  static PSD getDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins) {
154  PSD psd;
155  psd.setDistributionNormal(mean, standardDeviation, numberOfBins);
156  return psd;
157  }
158 
162  void setDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins);
163 
164  static PSD getDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins) {
165  PSD psd;
166  psd.setDistributionLogNormal(mean, standardDeviation, numberOfBins);
167  return psd;
168  }
169 
174 
179 
184 
189 
194 
198  void cutoffCumulativeNumber(Mdouble quantileMin, Mdouble quantileMax, Mdouble minPolydispersity = 0.1);
199 
203  void cutoffAndSqueezeCumulative(Mdouble quantileMin, Mdouble quantileMax, Mdouble squeeze,
204  Mdouble minPolydispersity = 0.1);
205 
209  Mdouble getMinRadius() const;
210 
214  Mdouble getMaxRadius() const;
215 
219  std::vector<DistributionElements> getParticleSizeDistribution() const;
220 
224  void setParticleSizeDistribution(std::vector<DistributionElements>);
225 
229  int getInsertedParticleNumber() const;
230 
235 
239  void decrementVolumePerClass(Mdouble volume);
240 
244  Mdouble getRadius(int index) const;
245 
249  Mdouble getNumberDx(Mdouble x) const;
250 
254  Mdouble getVolumeDx(Mdouble x) const;
255 
259  Mdouble getRadiusByQuantile(Mdouble quantile) const;
260 
264  Mdouble getQuantileByRadius(Mdouble radius) const;
265 
270 
274  Mdouble getSizeRatio() const;
275 
279  void cutHighSizeRatio();
280 
281 // /*!
282 // * \brief compute raw momenta of the user defined PSD.
283 // */
284 // void computeRawMomenta();
285 //
286 // /*!
287 // * \brief compute central momenta of the user defined PSD.
288 // */
289 // void computeCentralMomenta();
290 //
291 // /*!
292 // * \brief compute standardised momenta of the user defined PSD.
293 // */
294 // void computeStandardisedMomenta();
295 //
296 // /*!
297 // * \brief get momenta of the user defined PSD.
298 // */
299 // std::array<Mdouble, 6> getMomenta() const;
300 
304  static std::vector<Mdouble> linspace(Mdouble Min, Mdouble Max, int numberOfBins);
305 
309  void setFixedSeed(int seed)
310  {
311 // random_.setLinearCongruentialGeneratorParmeters(0,0,1);
312  random_.setRandomSeed(seed);
313  }
314 
318  friend bool operator<(const DistributionElements& l, const DistributionElements& r);
319 
323  friend bool operator<(const DistributionElements& l, Mdouble r);
324 
328  friend std::ostream& operator<<(std::ostream& os, DistributionElements& p);
329 
333  friend std::istream& operator>>(std::istream& is, DistributionElements& p);
334 
339 
340 private:
344  std::vector<DistributionElements> particleSizeDistribution_;
345 
351  std::vector<int> nParticlesPerClass_;
352 
360  std::vector<Mdouble> volumePerClass_;
361 
365  int index_;
366 
371 };
372 
373 
374 #endif //MECURYDPM_PSD_H
double Mdouble
Definition: GeneralDefine.h:34
#define MERCURYDPM_DEPRECATED
Definition: GeneralDefine.h:37
class of DistributionElements which stores internalVariables and probabilities of a distribution....
Definition: DistributionElements.h:34
Contains a vector with radii and probabilities of a user defined particle size distribution (PSD)
Definition: PSD.h:65
Mdouble getRadius(int index) const
get a radius at a certain index of the particleSizeDistribution_
Definition: PSD.cc:987
void setDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
create a PSD vector for a normal distribution.
Definition: PSD.cc:323
Mdouble drawSample()
Draw a sample radius from a CUMULATIVE_NUMBER_DISTRIBUTION.
Definition: PSD.cc:114
Mdouble getVolumetricMeanRadius() const
get a volumetric mean radius of the PSD.
Definition: PSD.cc:826
Mdouble getQuantileByRadius(Mdouble radius) const
Calculates the quantile corresponding to a certain radius.
Definition: PSD.cc:810
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:1020
void convertCumulativeToCumulativeNumberDistribution(TYPE CDFType)
convert any other CDF to a CUMULATIVE_NUMBER_DISTRIBUTION.
Definition: PSD.cc:656
int getInsertedParticleNumber() const
Get the number of particles already inserted into the simulation.
Definition: PSD.cc:923
Mdouble getMinRadius() const
Get smallest radius of the PSD.
Definition: PSD.cc:886
void printPSD() const
Prints radii and probabilities of the PSD vector.
Definition: PSD.cc:76
void decrementVolumePerClass(Mdouble volume)
Decrement volumePerClass_ counter.
Definition: PSD.cc:852
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:777
void convertCumulativeToProbabilityDensity()
Converts a CDF to a PDF by derivation.
Definition: PSD.cc:546
static PSD getDistributionNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
Definition: PSD.h:153
void decrementNParticlesPerClass()
Decrement nParticlesPerClass_ counter.
Definition: PSD.cc:842
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:462
friend std::ostream & operator<<(std::ostream &os, DistributionElements &p)
Writes to output stream.
Definition: PSD.cc:1060
friend std::istream & operator>>(std::istream &is, DistributionElements &p)
Reads from input stream.
Definition: PSD.cc:1049
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:139
void setDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
create a PSD vector for a normal distribution.
Definition: PSD.cc:357
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:309
RNG random_
Definition: PSD.h:370
friend Mdouble operator==(DistributionElements l, Mdouble r)
Determines if a certain value of the PSD vector is equal to a double.
Definition: PSD.cc:1040
PSD * copy() const
Creates a copy on the heap and returns a pointer.
Definition: PSD.cc:64
void validateProbabilityDensityDistribution()
Validates if the integral of the PDF equals to unity.
Definition: PSD.cc:260
std::vector< Mdouble > volumePerClass_
Definition: PSD.h:360
~PSD()
Destructor; default destructor.
PSD()
Constructor; sets everything to 0 or default.
Definition: PSD.cc:32
void convertProbabilityDensityToCumulative()
Converts a PDF to a CDF by integration.
Definition: PSD.cc:531
std::vector< int > nParticlesPerClass_
Definition: PSD.h:351
void convertProbabilityDensityToProbabilityDensityNumberDistribution(TYPE PDFType)
convert any PDF to a PROBABILITYDENSITY_NUMBER_DISTRIBUTION.
Definition: PSD.cc:566
std::vector< DistributionElements > particleSizeDistribution_
Definition: PSD.h:344
std::vector< DistributionElements > getParticleSizeDistribution() const
Get the PSD vector.
Definition: PSD.cc:904
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:767
Mdouble getMaxRadius() const
Get largest radius of the PSD.
Definition: PSD.cc:895
TYPE
Enum class which stores the possible types of CDFs and PDFs. Particle size distributions can be repre...
Definition: PSD.h:74
@ 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:872
int index_
Definition: PSD.h:365
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.cc:749
static PSD getDistributionLogNormal(Mdouble mean, Mdouble standardDeviation, int numberOfBins)
Definition: PSD.h:164
Mdouble getSizeRatio() const
get the size ratio (width) of the PSD.
Definition: PSD.cc:861
void validateCumulativeDistribution()
Validates if a CDF starts with zero and adds up to unity.
Definition: PSD.cc:213
Mdouble getRadiusByQuantile(Mdouble quantile) const
Calculate the quantile of the PSD.
Definition: PSD.cc:791
void convertProbabilityDensityNumberDistributionToProbabilityDensityVolumeDistribution()
convert a PROBABILITYDENSITY_NUMBER_DISTRIBUTION to a PROBABILITYDENSITY_VOLUME_DISTRIBUTION.
Definition: PSD.cc:630
void cutoffCumulativeNumber(Mdouble quantileMin, Mdouble quantileMax, Mdouble minPolydispersity=0.1)
cutoff the PSD at given quantiles.
Definition: PSD.cc:715
MERCURYDPM_DEPRECATED void setPSDFromVector(std::vector< DistributionElements > psd, TYPE PSDType)
Deprecated version of reading in PSDs from a vector.
Definition: PSD.cc:396
static std::vector< Mdouble > linspace(Mdouble Min, Mdouble Max, int numberOfBins)
compute central momenta of the user defined PSD. ‍/ void computeCentralMomenta();
Definition: PSD.cc:999
void setDistributionUniform(Mdouble radMin, Mdouble radMax, int numberOfBins)
create a PSD vector for a uniform distribution.
Definition: PSD.cc:299
void setParticleSizeDistribution(std::vector< DistributionElements >)
set the PSD by a suitable vector.
Definition: PSD.cc:913
This is a class that generates random numbers i.e. named the Random Number Generator (RNG).
Definition: RNG.h:53
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:53
T square(const T val)
squares a number
Definition: ExtendedMath.h:106
T cubic(const T val)
calculates the cube of a number
Definition: ExtendedMath.h:115