-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '210302_LF_CleanExamples' into 'master'
210302 lf clean examples See merge request multiscale-wdm/surrogate-models/fesl/fesl!40
- Loading branch information
Showing
22 changed files
with
817 additions
and
953 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import fesl | ||
from fesl import printout | ||
from data_repo_path import get_data_repo_path | ||
import numpy as np | ||
data_path = get_data_repo_path()+"Al36/" | ||
|
||
""" | ||
ex02_preprocess_data.py: Shows how this framework can be used to preprocess | ||
data. Preprocessing here means converting raw DFT calculation output into | ||
numpy arrays of the correct size. For the input data, this means descriptor | ||
calculation. | ||
Further preprocessing steps (scaling, unit conversion) is done later. | ||
""" | ||
|
||
|
||
def run_example02(): | ||
|
||
#################### | ||
# PARAMETERS | ||
# All parameters are handled from a central parameters class that | ||
# contains subclasses. | ||
#################### | ||
|
||
test_parameters = fesl.Parameters() | ||
|
||
# Specify input data options, i.e. which descriptors are calculated | ||
# with which parameters. These are the standard parameters for | ||
# the calculation of SNAP descriptors. | ||
test_parameters.descriptors.descriptor_type = "SNAP" | ||
test_parameters.descriptors.twojmax = 10 | ||
test_parameters.descriptors.rcutfac = 4.67637 | ||
test_parameters.data.descriptors_contain_xyz = True | ||
|
||
# Specify output data options, i.e. how the LDOS is parsed. | ||
# The Al system used as an example here actually has 250 energy levels. | ||
# But for the convenience of the user, only 10 energy levels will be | ||
# used for this example. | ||
test_parameters.targets.target_type = "LDOS" | ||
test_parameters.targets.ldos_gridsize = 10 | ||
test_parameters.targets.ldos_gridspacing_ev = 0.1 | ||
test_parameters.targets.ldos_gridoffset_ev = -10 | ||
|
||
#################### | ||
# DATA | ||
# Create a DataConverter, and add snapshots to it. | ||
#################### | ||
|
||
data_converter = fesl.DataConverter(test_parameters) | ||
|
||
# Take care to choose the "add_snapshot" function correct for | ||
# the type of data you want to preprocess. | ||
data_converter.add_snapshot_qeout_cube("Al.pw.scf.out", data_path, | ||
"cubes/tmp.pp*Al_ldos.cube", | ||
data_path, output_units="1/Ry") | ||
|
||
# Convert all the snapshots and save them in the current directory. | ||
# data_converter.convert_snapshots("./", naming_scheme="Al_snapshot*") | ||
|
||
#################### | ||
# RESULTS. | ||
# Print the used parameters and check whether the preprocessed data | ||
# has the desired dimensions. | ||
#################### | ||
|
||
printout("Parameters used for this experiment:") | ||
test_parameters.show() | ||
|
||
input_data = np.load("Al_snapshot0.in.npy") | ||
input_data_shape = np.shape(input_data) | ||
if input_data_shape[0] != 108 or input_data_shape[1] != 108 or \ | ||
input_data_shape[2] != 100 or input_data_shape[3] != 94: | ||
return False | ||
|
||
output_data = np.load("Al_snapshot0.out.npy") | ||
output_data_shape = np.shape(output_data) | ||
if output_data_shape[0] != 108 or output_data_shape[1] != 108 or \ | ||
output_data_shape[2] != 100 or output_data_shape[3] != 10: | ||
return False | ||
|
||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
if run_example02(): | ||
printout("Successfully ran ex02_preprocess_data.") | ||
else: | ||
raise Exception("Ran ex02_preprocess_data but something was off." | ||
" If you haven't changed any parameters in " | ||
"the example, there might be a problem with your" | ||
" installation.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import fesl | ||
from fesl import printout | ||
import numpy as np | ||
from data_repo_path import get_data_repo_path | ||
data_path = get_data_repo_path()+"Al36/" | ||
|
||
|
||
""" | ||
ex03_postprocess_data.py: Shows how this framework can be used to | ||
postprocess data. Usually, this framework outputs LDOS data, thefore, | ||
post processing of LDOS data will be shown in the following. | ||
Set do_total_energy to False, if you don't have the QuantumEspresso | ||
Python module installed. | ||
""" | ||
|
||
|
||
def run_example03(do_total_energy=True, accuracy_electrons = 1e-11, | ||
accuracy_total_energy=50): | ||
|
||
#################### | ||
# PARAMETERS | ||
# All parameters are handled from a central parameters class that | ||
# contains subclasses. | ||
#################### | ||
test_parameters = fesl.Parameters() | ||
|
||
# Specify the correct LDOS parameters. | ||
test_parameters.targets.target_type = "LDOS" | ||
test_parameters.targets.ldos_gridsize = 250 | ||
test_parameters.targets.ldos_gridspacing_ev = 0.1 | ||
test_parameters.targets.ldos_gridoffset_ev = -10 | ||
|
||
#################### | ||
# TARGETS | ||
# Create a target calculator to postprocess data. | ||
# Use this calculator to perform various operations. | ||
#################### | ||
|
||
ldos = fesl.TargetInterface(test_parameters) | ||
|
||
# Read additional information about the calculation. | ||
# By doing this, the calculator is able to know e.g. the temperature | ||
# at which the calculation took place or the lattice constant used. | ||
ldos.read_additional_calculation_data("qe.out", | ||
data_path+"Al.pw.scf.out") | ||
|
||
# Read in LDOS data. For actual workflows, this part will come | ||
# from a network. | ||
ldos_data = np.load(data_path+"Al_ldos.npy") | ||
|
||
# Get quantities of interest. | ||
# For better values in the post processing, it is recommended to | ||
# calculate the "self-consistent Fermi energy", i.e. the Fermi energy | ||
# at which the (L)DOS reproduces the exact number of electrons. | ||
# This Fermi energy usually differs from the one outputted by the | ||
# QuantumEspresso calculation, due to numerical reasons. The difference | ||
# is usually very small. | ||
self_consistent_fermi_energy = ldos.\ | ||
get_self_consistent_fermi_energy_ev(ldos_data) | ||
number_of_electrons = ldos.\ | ||
get_number_of_electrons(ldos_data, fermi_energy_eV= | ||
self_consistent_fermi_energy) | ||
band_energy = ldos.get_band_energy(ldos_data, | ||
fermi_energy_eV= | ||
self_consistent_fermi_energy) | ||
if do_total_energy: | ||
# To perform a total energy calculation one also needs to provide | ||
# a pseudopotential(path). | ||
ldos.set_pseudopotential_path(data_path) | ||
total_energy = ldos.get_total_energy(ldos_data, | ||
fermi_energy_eV= | ||
self_consistent_fermi_energy) | ||
|
||
#################### | ||
# RESULTS. | ||
# Print the used parameters and check whether LDOS based results | ||
# are consistent with the actual DFT results. | ||
#################### | ||
|
||
printout("Parameters used for this experiment:") | ||
test_parameters.show() | ||
|
||
print("Number of electrons:", number_of_electrons) | ||
print("Band energy:", band_energy) | ||
if do_total_energy: | ||
print("Total energy:", total_energy) | ||
|
||
if np.abs(number_of_electrons - ldos.number_of_electrons) > \ | ||
accuracy_electrons: | ||
return False | ||
|
||
# FIXME: Add as soon as band_energy_dft_calculation is fixed. | ||
# if np.abs(number_of_electrons - ldos.number_of_electrons) > accuracy: | ||
# return True | ||
|
||
if do_total_energy: | ||
if np.abs(total_energy - ldos.total_energy_dft_calculation) > \ | ||
accuracy_total_energy: | ||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
if run_example03(): | ||
printout("Successfully ran ex03_postprocess_data.") | ||
else: | ||
raise Exception("Ran ex03_postprocess_data but something was off." | ||
" If you haven't changed any parameters in " | ||
"the example, there might be a problem with your" | ||
" installation.") |
Oops, something went wrong.