|
| 1 | +.. visualisation: |
| 2 | +
|
| 3 | +============= |
| 4 | +Visualisation |
| 5 | +============= |
| 6 | + |
| 7 | +Here we detail the methods available for outputting the learned RAFFLE descriptor and RAFFLE fingerprints (distribution functions) for individual structures. |
| 8 | + |
| 9 | + |
| 10 | +Visualising the learned RAFFLE descriptor |
| 11 | +----------------------------------------- |
| 12 | + |
| 13 | +RAFFLE generates 2-body, 3-body, and 4-body distribution functions for each atomic species in the system (element pairs for the 2-body function). |
| 14 | +These are generated by combining formation energy-weighted (or convex hull-weighted) `n`-body distribution functions for each structure provided in the learning database. |
| 15 | + |
| 16 | +The ``raffle_generator`` object has a method ``get_descriptor()`` that returns the 2-, 3-, and 4-body forms of the learned RAFFLE generalised descriptor. |
| 17 | +The output is a list of three numpy arrays, with the first array containing the 2-body descriptor, the second array containing the 3-body descriptor, and the third array containing the 4-body descriptor. |
| 18 | +Each `n`-body descriptor is a 2D array, with the first column containing the species index (or element pair index for the 2-body descriptor) and the second column containing the binned descriptor value. |
| 19 | +The bin lengths are (``nbins`` component) set either explicitly or determined by the ``cutoff_min``, ``cutoff_max``, and ``width`` components of the generator. |
| 20 | + |
| 21 | +Here is an example of how to use the ``get_descriptor()`` method: |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + # Initialise RAFFLE generator |
| 26 | + from raffle.generator import raffle_generator |
| 27 | +
|
| 28 | + generator = raffle_generator() |
| 29 | +
|
| 30 | + # Set the host structure |
| 31 | + host = Atoms( |
| 32 | + # Host structure for the generator |
| 33 | + ) |
| 34 | + generator.set_host(host) |
| 35 | +
|
| 36 | + # Set the reference energies (i.e. chemical potential references) |
| 37 | + generator.distributions.set_element_energies( |
| 38 | + { |
| 39 | + # reference energies for all elements in the systems |
| 40 | + } |
| 41 | + ) |
| 42 | +
|
| 43 | + # Optional parameters |
| 44 | + generator.distributions.set_kBT(0.2) |
| 45 | + generator.distributions.set_width([0.04, np.pi/160.0, np.pi/160.0]) |
| 46 | + generator.distributions.set_cutoff_min([0.5, 0.0, 0.0]) |
| 47 | + generator.distributions.set_cutoff_max([6.0, np.pi, np.pi]) |
| 48 | +
|
| 49 | + # Set and learn from the initial database |
| 50 | + database = [ |
| 51 | + # List of structures in the learning database |
| 52 | + ] |
| 53 | + generator.distributions.create(database) |
| 54 | +
|
| 55 | + # Retrieve the descriptor |
| 56 | + descriptor_init = generator.get_descriptor() |
| 57 | +
|
| 58 | + # Print the 2-body descriptor |
| 59 | + print("2-body descriptor:") |
| 60 | + print(descriptor_init[0]) |
| 61 | +
|
| 62 | + # Print the 3-body descriptor |
| 63 | + print("3-body descriptor:") |
| 64 | + print(descriptor_init[1]) |
| 65 | +
|
| 66 | + # Print the 4-body descriptor |
| 67 | + print("4-body descriptor:") |
| 68 | + print(descriptor_init[2]) |
| 69 | +
|
| 70 | +With this, we can now plot the descriptor using any plotting library of your choice. |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + import matplotlib.pyplot as plt |
| 75 | + import numpy as np |
| 76 | +
|
| 77 | + # Create a figure with 3 subplots side by side |
| 78 | + fig, axes = plt.subplots(1, 3, figsize=(18, 5)) |
| 79 | +
|
| 80 | + # Plot for each n-body descriptor (2-body, 3-body, 4-body) |
| 81 | + for j in range(3): |
| 82 | + # Calculate x-axis values |
| 83 | + x = np.arange(generator.distributions.cutoff_min[j], |
| 84 | + generator.distributions.cutoff_max[j] + generator.distributions.width[j], |
| 85 | + generator.distributions.width[j]) |
| 86 | +
|
| 87 | + # Plot on the respective subplot |
| 88 | + for idx in range(len(descriptor_init[j])): |
| 89 | + axes[j].plot(x, descriptor_init[j][idx,:]) |
| 90 | +
|
| 91 | + # Set labels and title for each subplot |
| 92 | + axes[j].set_ylabel('Descriptor value') |
| 93 | + axes[j].set_title(f'{j+2}-body descriptor') |
| 94 | +
|
| 95 | + axes[0].set_xlabel('Distance (Å)') |
| 96 | + axes[1].set_xlabel('3-body angle (radians)') |
| 97 | + axes[2].set_xlabel('Improper dihedral angle (radians)') |
| 98 | + plt.tight_layout() |
| 99 | + plt.show() |
| 100 | +
|
| 101 | +An example python notebook is provided in :git:`examples/python_pkg/visualisation/descriptor.ipynb <examples/python_pkg/visualisation/descriptor.ipynb>` |
| 102 | + |
| 103 | +We can now use this to compare the initial descriptor with the updated descriptor after generating new structures. |
| 104 | + |
| 105 | +.. code-block:: python |
| 106 | +
|
| 107 | + # Generate new structures and update the descriptor |
| 108 | + structures = [ |
| 109 | + # List of structures to be generated |
| 110 | + ] |
| 111 | + generator.distributions.update(structures) |
| 112 | +
|
| 113 | + # Retrieve the updated descriptor |
| 114 | + descriptor_new = generator.get_descriptor() |
| 115 | +
|
| 116 | + # Print the updated descriptor on the plots and compare |
| 117 | + ... |
| 118 | +
|
| 119 | +
|
| 120 | +Visualising a RAFFLE fingerprint |
| 121 | +-------------------------------- |
| 122 | + |
| 123 | +RAFFLE fingerprints are the distribution functions for each structure in the learning database. |
| 124 | +These are then weighted by energy (formation or convex hull) to form the RAFFLE descriptor. |
| 125 | + |
| 126 | +However, the individual fingerprints can also be extracted and visualised. |
| 127 | + |
| 128 | +The `raffle_generator` object has a method `get_fingerprint()` that returns the distribution functions for a provided structure. |
| 129 | +The output is a list of three numpy arrays, with the first array containing the 2-body fingerprint, the second array containing the 3-body fingerprint, and the third array containing the 4-body fingerprint. |
| 130 | +Each `n`-body fingerprint is a 2D array, with the first column containing the species index (or element pair index for the 2-body fingerprint) and the second column containing the binned fingerprint value. |
| 131 | +Like above, the bin lengths are set either explicitly or determined by the `cutoff_min`, `cutoff_max`, and `width` components of the generator. |
| 132 | +Here is an example of how to use the `get_fingerprint()` method: |
| 133 | + |
| 134 | +.. code-block:: python |
| 135 | +
|
| 136 | + # Initialise RAFFLE generator |
| 137 | + from raffle.generator import raffle_generator |
| 138 | +
|
| 139 | + generator = raffle_generator() |
| 140 | +
|
| 141 | + # Optional parameters |
| 142 | + generator.distributions.set_width([0.04, np.pi/160.0, np.pi/160.0]) |
| 143 | + generator.distributions.set_cutoff_min([0.5, 0.0, 0.0]) |
| 144 | + generator.distributions.set_cutoff_max([6.0, np.pi, np.pi]) |
| 145 | +
|
| 146 | + # Structure to obtain the fingerprint for |
| 147 | + structure = Atoms( |
| 148 | + # Structure to be used for the fingerprint |
| 149 | + ) |
| 150 | +
|
| 151 | + fingerprint = generator.distributions.generate_fingerprint(structure) |
| 152 | +
|
| 153 | +This can then be visualised in a similar way to the descriptor. |
| 154 | + |
| 155 | +.. code-block:: python |
| 156 | +
|
| 157 | + # Create a figure with 3 subplots side by side |
| 158 | + fig, axes = plt.subplots(1, 3, figsize=(18, 5)) |
| 159 | +
|
| 160 | + # Plot for each n-body function (2-body, 3-body, 4-body) |
| 161 | + for j in range(3): |
| 162 | + # Calculate x-axis values |
| 163 | + x = np.arange(generator.distributions.cutoff_min[j], |
| 164 | + generator.distributions.cutoff_max[j] + generator.distributions.width[j], |
| 165 | + generator.distributions.width[j]) |
| 166 | +
|
| 167 | + # Plot on the respective subplot |
| 168 | + for idx in range(len(fingerprint[j])): |
| 169 | + axes[j].plot(x, fingerprint[j][idx,:]) |
| 170 | +
|
| 171 | + # Set labels and title for each subplot |
| 172 | + axes[j].set_ylabel('Fingerprint value') |
| 173 | + axes[j].set_title(f'{j+2}-body fingerprint') |
| 174 | +
|
| 175 | + axes[0].set_xlabel('Distance (Å)') |
| 176 | + axes[1].set_xlabel('3-body angle (radians)') |
| 177 | + axes[2].set_xlabel('Improper dihedral angle (radians)') |
| 178 | + plt.tight_layout() |
| 179 | + plt.show() |
| 180 | +
|
| 181 | +An example python notebook is provided in :git:`examples/python_pkg/visualisation/fingerprint.ipynb <examples/python_pkg/visualisation/fingerprint.ipynb>`. |
| 182 | + |
| 183 | + |
| 184 | +Visualising RAFFLE probability density |
| 185 | +-------------------------------------- |
| 186 | + |
| 187 | +RAFFLE probability density is the probability of finding a given element in a given position in the system. |
| 188 | +This is calculated by the RAFFLE generator and can be visualised using the `get_probability_density()` method. |
| 189 | +The output is a 2D array, with the first column containing the coordinates (spatial and species) the second column containing the binned probability density value. |
| 190 | + |
| 191 | +A structure is provided to the `get_probability_density()`, along with a list of elements to calculate the probability density for. |
| 192 | +These elements must be present in the RAFFLE descriptor. |
| 193 | + |
| 194 | +An example of how to use the `get_probability_density()` method is shown below: |
| 195 | + |
| 196 | +.. code-block:: python |
| 197 | +
|
| 198 | + # Initialise RAFFLE generator |
| 199 | + from raffle.generator import raffle_generator |
| 200 | +
|
| 201 | + generator = raffle_generator() |
| 202 | +
|
| 203 | + generator.distributions.set_element_energies( |
| 204 | + { |
| 205 | + # reference energies for all elements in the systems |
| 206 | + } |
| 207 | + ) |
| 208 | +
|
| 209 | + database = [ |
| 210 | + # List of structures in the learning database |
| 211 | + ] |
| 212 | + generator.distributions.create(database) |
| 213 | +
|
| 214 | + # Structure to obtain the probability density for |
| 215 | + structure = Atoms( |
| 216 | + # Structure to be used for the probability density |
| 217 | + ) |
| 218 | +
|
| 219 | + species = 'SiGe' |
| 220 | +
|
| 221 | + probability_density, grid = generator.get_probability_density(structure, species, return_grid=True) |
| 222 | +
|
| 223 | +The first index of the first column of `probability_density` is the x-coordinate, the second index is the y-coordinate, and the third index is the z-coordinate. |
| 224 | +The fourth index is the distance between the position and the nearest atom (i.e. the void value). |
| 225 | +The fifth index onwards is the species index (in order of the species list provided). |
| 226 | +The second column is the site index. |
| 227 | + |
| 228 | +For a more extensive example, see the `examples/python_pkg/visualisation/probability_density.ipynb` notebook. |
| 229 | +This also provides a visualisation of the probability density using the `matplotlib` library. |
0 commit comments