|
| 1 | +r""" |
| 2 | +=============================================================================== |
| 3 | +Submodule -- Miscillaneous functions |
| 4 | +=============================================================================== |
| 5 | +
|
| 6 | +""" |
| 7 | +import scipy as _sp |
| 8 | +import scipy.stats as _spts |
| 9 | + |
| 10 | + |
| 11 | +def random(N, seed=None, num_range=[0, 1], **kwargs): |
| 12 | + r""" |
| 13 | + Create an array of random numbers of a specified size. |
| 14 | +
|
| 15 | + Parameters |
| 16 | + ---------- |
| 17 | + N : int |
| 18 | + The length of the random array to be generated. This should correspond |
| 19 | + to the number of pores or throats on the Geometry object. For instance |
| 20 | + N = geom.Nt or N = geom.Np. |
| 21 | +
|
| 22 | + seed : int |
| 23 | + The starting seed value to send to Scipy's random number generator. |
| 24 | + The default is None, which means different distribution is returned |
| 25 | + each time the model is run. |
| 26 | +
|
| 27 | + num_range : list |
| 28 | + A two element list indicating the low and high end of the returned |
| 29 | + numbers. |
| 30 | +
|
| 31 | + """ |
| 32 | + range_size = num_range[1] - num_range[0] |
| 33 | + range_min = num_range[0] |
| 34 | + _sp.random.seed(seed) |
| 35 | + value = _sp.random.rand(N,) |
| 36 | + value = value*range_size + range_min |
| 37 | + return value |
| 38 | + |
| 39 | + |
| 40 | +def scaled(geometry, prop, factor, **kwargs): |
| 41 | + r""" |
| 42 | + Scales an existing value by a factor. Useful for constricting some throat |
| 43 | + property. |
| 44 | +
|
| 45 | + Parameters |
| 46 | + ---------- |
| 47 | + geometry : OpenPNM Geometry Object |
| 48 | + The object with which this function as associated. This argument |
| 49 | + is required to (1) set number of values to generate (geom.Np or |
| 50 | + geom.Nt) and (2) provide access to other necessary values |
| 51 | + (i.e. geom['pore.seed']). |
| 52 | +
|
| 53 | + prop : string |
| 54 | + The dictionary key of the array containing the values to be scaled. |
| 55 | +
|
| 56 | + factor : scalar |
| 57 | + The factor by which the values should be scaled. |
| 58 | + """ |
| 59 | + value = geometry[prop]*factor |
| 60 | + return value |
| 61 | + |
| 62 | + |
| 63 | +def weibull(geometry, shape, scale, loc, seeds, **kwargs): |
| 64 | + r""" |
| 65 | + Produces values from a Weibull distribution given a set of random numbers. |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + geometry : OpenPNM Geometry Object |
| 70 | + The object with which this function as associated. This argument |
| 71 | + is required to (1) set number of values to generate (geom.Np or |
| 72 | + geom.Nt) and (2) provide access to other necessary values |
| 73 | + (i.e. geom['pore.seed']). |
| 74 | +
|
| 75 | + shape : float |
| 76 | + This controls the skewness of the distribution, with 'shape' < 1 giving |
| 77 | + values clustered on the low end of the range with a long tail, and |
| 78 | + 'shape' > 1 giving a more symmetrical distribution. |
| 79 | +
|
| 80 | + scale : float |
| 81 | + This controls the width of the distribution with most of values falling |
| 82 | + below this number. |
| 83 | +
|
| 84 | + loc : float |
| 85 | + Applies an offset to the distribution such that the smallest values are |
| 86 | + above this number. |
| 87 | +
|
| 88 | + seeds : string, optional |
| 89 | + The dictionary key on the Geometry object containing random seed values |
| 90 | + (between 0 and 1) to use in the statistical distribution. If none is |
| 91 | + specified, then an array of random numbers will be automatically |
| 92 | + generated and stored on the Geometry object. |
| 93 | +
|
| 94 | + Examples |
| 95 | + -------- |
| 96 | + The following code illustrates the inner workings of this function, |
| 97 | + which uses the 'weibull_min' method of the scipy.stats module. This can |
| 98 | + be used to find suitable values of 'shape', 'scale'` and 'loc'. Note that |
| 99 | + 'shape' is represented by 'c' in the actual function call. |
| 100 | +
|
| 101 | + .. code-block:: |
| 102 | +
|
| 103 | + import scipy.stats as spst |
| 104 | + import matplotlib.pyplot as plt |
| 105 | + x = spst.weibull_min.ppf(q=sp.rand(10000), c=1.5, scale=0.0001, loc=0) |
| 106 | + plt.hist(x, bins=50) |
| 107 | +
|
| 108 | + """ |
| 109 | + seeds = geometry[seeds] |
| 110 | + value = _spts.weibull_min.ppf(q=seeds, c=shape, scale=scale, loc=loc) |
| 111 | + return value |
| 112 | + |
| 113 | + |
| 114 | +def normal(geometry, scale, loc, seeds, **kwargs): |
| 115 | + r""" |
| 116 | + Produces values from a Weibull distribution given a set of random numbers. |
| 117 | +
|
| 118 | + Parameters |
| 119 | + ---------- |
| 120 | + geometry : OpenPNM Geometry Object |
| 121 | + The object with which this function as associated. This argument |
| 122 | + is required to (1) set number of values to generate (geom.Np or |
| 123 | + geom.Nt) and (2) provide access to other necessary values |
| 124 | + (i.e. geom['pore.seed']). |
| 125 | +
|
| 126 | + scale : float |
| 127 | + The standard deviation of the Normal distribution |
| 128 | +
|
| 129 | + loc : float |
| 130 | + The mean of the Normal distribution |
| 131 | +
|
| 132 | + seeds : string, optional |
| 133 | + The dictionary key on the Geometry object containing random seed values |
| 134 | + (between 0 and 1) to use in the statistical distribution. If none is |
| 135 | + specified, then an array of random numbers will be automatically |
| 136 | + generated and stored on teh Geometry object. |
| 137 | +
|
| 138 | + Examples |
| 139 | + -------- |
| 140 | + The following code illustrates the inner workings of this function, |
| 141 | + which uses the 'norm' method of the scipy.stats module. This can |
| 142 | + be used to find suitable values of 'scale' and 'loc'. |
| 143 | +
|
| 144 | + .. code-block:: |
| 145 | +
|
| 146 | + import scipy.stats as spst |
| 147 | + import matplotlib.pyplot as plt |
| 148 | + x = spst.norm.ppf(q=sp.rand(10000), scale=.0001, loc=0.001) |
| 149 | + plt.hist(x, bins=50) |
| 150 | +
|
| 151 | + """ |
| 152 | + seeds = geometry[seeds] |
| 153 | + value = _spts.norm.ppf(q=seeds, scale=scale, loc=loc) |
| 154 | + return value |
| 155 | + |
| 156 | + |
| 157 | +def generic(geometry, func, seeds, **kwargs): |
| 158 | + r""" |
| 159 | + Accepts an 'rv_frozen' object from the Scipy.stats submodule and returns |
| 160 | + values from the distribution for the given seeds using the ``ppf`` method. |
| 161 | +
|
| 162 | + Parameters |
| 163 | + ---------- |
| 164 | + func : object |
| 165 | + An 'rv_frozen' object from the Scipy.stats library with all of the |
| 166 | + parameters pre-specified. |
| 167 | +
|
| 168 | + seeds : string, optional |
| 169 | + The dictionary key on the Geometry object containing random seed values |
| 170 | + (between 0 and 1) to use in the statistical distribution. If none is |
| 171 | + specified, then an array of random numbers will be automatically |
| 172 | + generated and stored on teh Geometry object. |
| 173 | +
|
| 174 | + Examples |
| 175 | + -------- |
| 176 | + The following code illustrates the process of obtaining a 'frozen' Scipy |
| 177 | + stats object, and visualizes the corresponding distribution using a |
| 178 | + Matplotlib histogram: |
| 179 | +
|
| 180 | + .. code-block:: python |
| 181 | +
|
| 182 | + import scipy |
| 183 | + func = scipy.stats.weibull_min(c=2, scale=.0001, loc=0) |
| 184 | + import matplotlib.pylot as plt |
| 185 | + plt.hist(func.ppf(q=scipy.rand(1000), bins=50)) |
| 186 | +
|
| 187 | + """ |
| 188 | + seeds = geometry[seeds] |
| 189 | + value = func.ppf(seeds) |
| 190 | + return value |
0 commit comments