Multi Cookie Generator 1 7 Tom T REPACK
Note that rand7_gen() returns a generator, since it has internal state involving the conversion of the number to base 7. The test harness calls next(r7) 10000 times to produce 10000 random numbers, and then it measures their distribution. Only integer math is used, so the results are exactly correct.
Multi Cookie Generator 1 7 Tom T
Note that this generator is not perfect (the number 0 has 0.0064% more chance than any other number), but for most practical purposes the guarantee of constant time probably outweighs this inaccuracy.
This solution is derived from the fact that the number 15,624 is divisible by 7 and thus if we can randomly and uniformly generate numbers from 0 to 15,624 and then take mod 7 we can get a near-uniform rand7 generator. Numbers from 0 to 15,624 can be uniformly generated by rolling rand5 6 times and using them to form the digits of a base 5 number as follows:
This is a number in base 5 form and thus we can see that this method can be used to go from any random number generator to any other random number generator. Though a small bias towards 0 is always introduced when using the exponent p-1.
The geometrical properties of multiphase materials are of central importance to a wide variety of engineering disciplines. For example, the distribution of precious metal catalysts on porous supports; the structure of metallic phases and defects in high-performance alloys; the arrangement of sand, organic matter, and water in soil science; and the distribution of calcium, collagen and blood vessels in bone1,2,3. In electrochemistry, whether we are considering batteries, fuel cells or supercapacitors, their electrodes are typically porous to maximise surface area but need to contain percolating paths for the transport of both electrons and ions, as well as maintaining sufficient mechanical integrity4,5. Thus the microstructure of these electrodes significantly impacts their performance and their morphological optimisation is vital for developing the next generation of energy storage technologies6.
Recent work by Mosser et al.38,39 introduces a deep learning approach for the stochastic generation of three-dimensional two-phase porous media. The authors implement a type of generative model called Generative Adversarial Networks (GANs)40 to reconstruct the three-dimensional microstructure of synthetic and natural granular microstructures. Li et al.41 extended this work to enable the generation of optimised sandstones, again using GANs. Compared to other common microstructure generation techniques, GANs are able to provide fast sampling of high-dimensional and intractable density functions without the need for an a priori model of the probability distribution function to be specified39. This work expands on the research of Mosser et al.38,39 and implements GANs for generating three-dimensional, three-phase microstructure for two types of electrode commonly used in electrochemical devices: a Li-ion battery cathode and an SOFC anode. A comparison between the statistical, morphological and transport properties of the generated images and the real tomographic data is performed. The two-point correlation function is further calculated for each of the three phases in the training and generated sets to investigate the long-range properties. Due to the fully convolutional nature of the GANs used, it is possible to generate arbitrarily large volumes of the electrodes based on the trained model. Lastly, by modifying the input of the generator, structures with periodic boundaries were generated.
Performing multiphysics simulations on representative 3D volumes is necessary for microstructural optimisation, but it is typically very computationally expensive. This is compounded by the fact that the regions near the boundaries can show unrealistic behaviour due to the arbitrary choice of boundary condition. However, synthetic periodic microstructures (with all the correct morphological properties) enable the use of periodic boundary conditions in the simulation, which will significantly reduce the simulated volume necessary to be considered representative. This has the potential to greatly accelerate these simulations and therefore the optimisation process as a whole.
The training process consists of a minimax game between two functions, the generator G(z) and the discriminator D(x). G(z) maps an d-dimensional latent vector \(\bfz \sim p_\rmz(\bfz)\in \mathbbR^d\) to a point in the space of real data as G(z; θ(G)), while D(x) represents the probability that x comes from pdata.40 The aim of the training is to make the implicit density learned by G(z) (i.e., pmodel) to be close to the distribution of real data (i.e., pdata). A more detailed introduction to GANs can be found in Section A of the supplementary material.
In this work, both the generator \(G_\theta ^(G)(\bfz)\) and the discriminator \(D_\theta ^(D)(\bfx)\) consist of deep convolutional neural networks.43 Each of these has a cost function to be optimised through stochastic gradient descent in a two-step training process. First, the discriminator is trained to maximise its loss function J(D):
These concepts are summarised in Fig. 1. Early in training, the discriminator significantly outperforms the generator, leading to a vanishing gradient in the generator. For this reason, in practice instead of minimising \(\log\,\left(1-D\left(G(\bfz)\right)\right)\), it is convenient to maximise the log-probability of the discriminator being mistaken, defined as \(\log\,\left(D(G(\bfz))\right)\)42.
The solution to this optimisation problem is a Nash equilibrium42 where each of the players achieves a local minimum. Throughout the learning process, the generator learns to represent the probability distribution function pmodel which is as close as possible to the distribution of the real data pdata(x). At the Nash equilibrium, the samples of x = G(z) pmodel(z) are indistinguishable from the real samples x pdata(x), thus pmodel(z) = pdata(x) and \(D(\bfx)=\frac12\) for all x since the discriminator can no longer distinguish between real and synthetic data.
The two GANs implemented in this work, one for each microstructure, were trained for a maximum of 72 epochs (i.e., 72 complete iterations of the training set). The stopping criteria was established through the manual inspection of the morphological properties every two epochs. Supplementary Figs S9 and S10 of the supplementary data shows the visual reconstruction of both microstructures, beginning with Gaussian noise at epoch 0, and ending with a visually equivalent microstructure at epoch 50. The image generation improves with the number of iterations; however, as pointed out by Mosser et al.38, this improvement cannot be observed directly from the loss function of the generator and so the morphological parameters described above are used instead.
Once the generator parameters have been trained, the generator can be used to create periodic microstructures of arbitrary size. This is simply achieved by applying circular spatial padding to the first transposed convolutional layer of the generator (although other approaches are possible). Figure 8 shows generated periodic microstructures for both the cathode and anode, arranged in an array to make their periodic nature easier to see. Additionally, local scalar flux maps resulting from steady-state diffusion simulations in TauFactor48 are shown for each microstructure. In both cases, the upper flux map shows the results of the simulation with mirror (i.e., zero flux) boundaries on the vertical edges, and the lower one shows the results of the simulation with periodic boundaries on the vertical edges. Comparing the results from the two boundary conditions, it is clear that using periodic boundaries opens up more paths that enable a larger flux due to the continuity of transport at the edges. Furthermore, this means that the flow effectively does not experience any edges in the horizontal direction, which means that, unlike the mirror boundary case, there are no unrealistic regions of the volume due to edge effects.
For both the Li-ion cathode (L) and the SOFC anode (R), periodic microstructures were generated by slightly changing the input to the generator. For each electrode, four instances are shown making the periodicity easier to observe. Also shown are local scalar flux maps generated from steady-state diffusion simulations in TauFactor with either mirror (top) or periodic (bottom) boundary conditions implemented on the vertical edges.
Figures 4 and 6 show some degree of mode collapse given by the small variance in the calculated properties of the generated data. Nevertheless, further analysis of the diversity of the generated samples is required to evaluate the existence of mode collapse based on the number of unique samples that can be generated50,51. Following the work of Radford et al.52, an interpolation between two points in the latent space is performed to test the absence of memorisation in the generator. The results shown in Supplementary Fig. S8 present a smooth transformation of the generated data as the latent vectors is progressed along a straight path. This indicates that the generator is not memorising the training data but has learned the meaningful features of the microstructure.
The minimum generated samples are the same size as the training data sub-volumes (i.e., 643 for both cases analysed in this work), but can be increased to any arbitrarily large size by increasing the size of the input z. Although the training process of the DC-GAN is computationally expensive, once a trained generator is obtained, it can produce image data inexpensively. The relation between computation time and generated image size is shown in Supplementary Fig. S7. 350c69d7ab