hecss.core¶
The core functionality of the HECSS module is provided by the HECSS class which encapsulates the _sampler generator and exposes sample and generate as public API.
__all__ = ['HECSS']
module-attribute
¶
HECSS
¶
Class encapsulating the sampling and weight generation for the HECSS algorithm.
Create the HECSS sampling object. It is intended to be a single object
per crystal cryst used to run samplers for one or more temperatures.
The object holds data common to all samplers (structure, calculator etc.).
The other parameters are set per sampler. The set of samplers, indexed
by temperature is hold inside the HECSS object in the samplers dictionary.
Arguments¶
- cryst : crystal structure (ASE
Atomsobject) - calc : ASE calculator or a callable returning a fresh one. A single calculator instance is reused; a callable is invoked once per sampler (i.e. once per temperature).
- width : eta, displacement scaling parameter, approx 1.0. If None, it is
estimated from forces on the first
sample()call. - maxburn : max. number of initial burn-in samples
- w_search : use width/eta searching algorithm (default True)
- disp_dist : use different distribution instead of
stats.normas the displacement distribution. - directory : basic calculation directory used by directory based calculators
- pbar : show progress bar during calculations
Source code in hecss/core.py
Ep0 = None
instance-attribute
¶
Q = _disp_dists[disp_dist]
instance-attribute
¶
calc = calc
instance-attribute
¶
cryst = cryst
instance-attribute
¶
directory = f'calc'
instance-attribute
¶
eta = width
instance-attribute
¶
maxburn = maxburn
instance-attribute
¶
pbar = None
instance-attribute
¶
samplers = {}
instance-attribute
¶
w_scale = 0.001
instance-attribute
¶
w_search = w_search
instance-attribute
¶
xscale_init = np.ones((len(self.cryst), 3))
instance-attribute
¶
__init__(cryst, calc, width=None, maxburn=20, w_search=True, disp_dist='normal', directory=None, pbar=False)
¶
Source code in hecss/core.py
print_xs(c, s)
¶
smpl_print()
¶
Source code in hecss/core.py
__estimate_width_scale_aio(self, n=1, Tmin=0, Tmax=600, set_scale=True, pbar=None, nwork=5)
async
¶
Async/parallel version of w-estimator. Only supported for VASP.
Estimate coefficient between temperature and displacement scale (eta).
Calculate energy increase from the n temperatures uniformly
distributed between 0 and Tmax and calculate avarage \(\sqrt{E-E0/T}\)
which is a width scale for a given temperature:
$$
w = \eta\sqrt{T}
$$
which comes from the assumed approximate relationship:
$$
\frac{E(w(T))-E_0}{T} \approx \mathrm{const} = \eta^2.
$$
Input¶
n- number of sampling pointsTmin- min sampled temperatureTmax- max sampled temperatureset_scale- set scale parameter in the class after runpbar- show progress bar during calculationnwork- number of parallel workers; 0 or less uses all structures.
Output¶
This internal coroutine fills _eta_list and _eta_samples; it returns
nothing. Use estimate_width_scale for the public result.
Source code in hecss/core.py
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 | |
__get_calculator(self)
¶
Produce a new calculator each time it is called. If constructor produces new calculator, just call it. Otherwise pass the calc attribute.
Source code in hecss/core.py
__sampler_aio(self, T_goal, N=None, delta_sample=0.01, sigma=2, eqdelta=0.05, eqsigma=0.2, xi=1, chi=1, modify=None, modify_args=(), symprec=1e-05, width_list=None, dofmu_list=None, xscale_list=None, verb=True)
async
¶
The core functionality of the module is implemented in this generator function which yields samples of the prior distribution. This is an internal implementation mechanism. In principle, it can be used by outside code but it is strongly discouraged as the implementation details may change without any notice (even for minor revisions).
This is a generator and cannot be used as regular function.
It is intended to be used as a source of the sequence of
configurations in the for loop and must be closed after
finishing the iteration. On the other hand, the iteration
may be continued if additional samples are required.
The state is preserved until the .close() method is called.
The sampling loop may be run for N iterations or indefinitely (default).
INPUT¶
T_goal : Target temperature in Kelvin
N : Number of iterations. If None (default) the generator never stops.
delta_sample : Prior width adaptation rate. The default is sufficient in most cases.
sigma : Range around E0 in sigmas to stop w-search mode
eqdelta : Max. speed of amplitude correction from step to step (0.05=5%)
eqsigma : Half width of linear part of amplitude correction function.
xi : strength of the amplitude correction term [0-1]
chi : strength of the amplitude correction term mixing [0-1]
xscale_init : Initial values of the amplitude correction coefficients.
Array with shape cryst.get_positions().shape.
May be generated with calc_init_xscale function.
Ep0 : T=0 energy (base, no displacements), if None (default) calculate E0.
modify : pass your own pre-processing function to modify the structure
before calculation. The function must return a (e, f) tuple
with energy of the structure (e, scalar) and forces (f, array).
modify_args : tuple of extra positional arguments for modify function
symprec : symmetry detection treshold for spglib functions
directory : (only for VASP calculator) directory for calculations and generated samples.
If left as None, the calc/{T_goal:.1f}K/ will be used and the generated
samples will be stored in the smpl/{i:04d} subdirectories.
verb : print verbose progress messages for interactive use
Output parameters
width_list : Output parameter. If not None, store in passed list the sequence of widths.
dofmu_list : Output parameter. If not None, store in passed list the array of DOF virials
relative to temperature (T_goal).
xscale_list : Output parameter. If not None, store in passed list the array of amplitude
correction coefficients (normalized). May be used to generate xscale_init
values with the help of calc_init_xscale function.
OUTPUT¶
The generator yields samples from the prior distribution at T_goal temperature as tuples: (number, index, displacement, forces, energy):
- number : sample number, always increasing
- index : integer numbering the samples in the
smplsubdirectory. Index repeats if the sample is repeated in the sequence. - displacement : set of atomic displacements (in A) in the sample (numpy array)
- forces : set of forces (in eV/A) generated by the displacement
- energy : potential energy of the configuration
Source code in hecss/core.py
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 | |
estimate_width_scale(self, n=1, Tmin=0, Tmax=600, set_scale=True, pbar=None, nwork=None)
¶
Estimate coefficient between temperature and displacement scale (eta).
Calculate energy increase from the n temperatures uniformly
distributed between 0 and Tmax and calculate avarage \(\sqrt{E-E0/T}\)
which is a width scale for a given temperature:
$$
w = \eta\sqrt{T}
$$
which comes from the assumed approximate relationship:
$$
\frac{E(w(T))-E_0}{T} \approx \mathrm{const} = \eta^2.
$$
Input¶
n- number of sampling pointsTmin- min sampled temperatureTmax- max sampled temperatureset_scale- set scale parameter in the class after runpbar- show progress bar during calculationnwork- if not None, number of parallel workers to use. Only supported for VASP, 0 => unlimited.
Output¶
Returns (mean, std, xscale): the mean width scale eta, its standard
deviation over the sampled temperatures, and the per-element amplitude
correction matrix with the shape of the atomic positions.
Source code in hecss/core.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
generate(self, S, T=None, sigma_scale=1.0, border=False, probTH=0.25, Nmul=4, N=None, nonzero_w=True, debug=False)
¶
Generate new sample with normal energy probability distribution
corresponding to temperature T and size of the system inferred
from data. The output is generated by multiplying samples passed in S
proportionally to the weights generated by get_sample_weights
and assuming the final dataset will be Nmul times longer
(or the length N which takes precedence). If nonzero_w is True
the data multipliers in the range (probTH, 1) will be clamped to 1,
to avoid losing low-probability data. This will obviously deform the
distribution but may be beneficial for the interaction model constructed
from the data. The data on output will be ordered in increasing energy
and re-numbered. The configuration index is retained.
The work is directly delegated to the make_sampling function.
Input¶
S- list of samples generated by HECSS sampler ([n, i, x, f, e])T- temperature in kelvin, mean energy by defaultsigma_scale- scaling factor for variance. Defaults to 1.0.border- make border samples account for unrepresented part of domainprobTH- threshold for probability (N*weight) clamping.Nmul- data multiplication factor. The length of output data will be approximatelyNmul*len(data).N- approximate output data length. Takes precedence overNmulnonzero_w- prevent zero weights for data with weights in (probTH, 1) rangedebug- plot diagnostic plots
Output¶
Weighted, sorted by energy and re-numbered samples as a new list.
The data in the list is just reference-duplicated, not copied.
Thus the data elements should not be modified.
The format is the same as the data produced by HECSS sampler.
Source code in hecss/core.py
sample(self, T, N, sentinel=None, sentinel_args={}, nwork=None, **kwargs)
¶
Generate N samples using HECSS._sampler_(ser/aio) generator.
sentinel parameter is a call-back function
which is called on every sample to decide if the
iteration should be stopped early. If it returns
True the iteration will be stopped and the current
list of samples is returned. The sentinel is called
after generating each sample (i.e. first time after
first sample is produced). This may take considerable
time at the start since first initial and burn-in
samples must be produced.
The sampling loop may be run for N iterations or indefinitely.
INPUT¶
- T : Target temperature in Kelvin
- N : Number of iterations.
- sentinel : callback
sentinel(sample, samples, **sentinel_args); returning True stops the run early. - sentinel_args: keyword arguments passed to the sentinel callback.
- nwork : reserved for future parallel sampling. It does not affect sampling; -k only controls width estimation.
- delta_sample : Prior width adaptation rate. The default is sufficient in most cases.
- sigma : Range around E0 in sigmas to stop w-search mode
- eqdelta : Max. speed of amplitude correction from step to step (0.05=5%)
- eqsigma : Half width of linear part of amplitude correction function.
- xi : strength of the amplitude correction term [0-1]
- chi : strength of the amplitude correction term mixing [0-1]
Set
sampler.xscale_initbefore callingsample()to supply initial amplitude correction coefficients. The array must have shapecryst.get_positions().shape; usecalc_init_xscaleto generate it. - Ep0 : T=0 energy (base, no displacements), if None (default) calculate E0.
- modify : pass your own pre-processing function to modify the structure before calculation. The function must return a (e, f) tuple with energy of the structure (e, scalar) and forces (f, array).
- modify_args : tuple of extra positional arguments passed to
modify - symprec : symmetry detection treshold for spglib functions
- directory : (only for VASP calculator) directory for calculations and generated samples.
If left as None, the
calc/{T_goal:.1f}K/will be used and the generated samples will be stored in thesmpl/{i:04d}subdirectories. - reuse_base : None or the base calculator created by restarting the ground state config. If None the base will be recalculated at the start of the run. If the value is a calculator - the energy from this calculator will be used as ground state energy for the calculation. Be careful to have the same setup in calc and reuse_base, otherwise the ground state energy and distribution will be wrong.
- verb : print verbose progress messages for interactive use
Output parameters
- width_list : Output parameter. If not None, store in passed list the sequence of widths.
- dofmu_list : Output parameter. If not None, store in passed list the array of DOF virials relative to temperature (T_goal).
- xscale_list : Output parameter. If not None, store in passed list the array of amplitude
correction coefficients (normalized). May be used to generate
xscale_initvalues with the help ofcalc_init_xscalefunction.
OUTPUT¶
The returns a list of samples from the prior distribution at T=T_goal as list of tuples (number, index, displacement, forces, energy):
- number : sample number, always increasing
- index : integer numbering the samples in the
smplsubdirectory. Index repeats if the sample must be repeated in the sequence. - displacement : set of atomic displacements (in A) in the sample (numpy array)
- forces : set of forces (in eV/A) generated by the displacement
- energy : potential energy of the configuration
Source code in hecss/core.py
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | |