Skip to content

Commit f206204

Browse files
Merge branch '210302_LF_CleanExamples' into 'master'
210302 lf clean examples See merge request multiscale-wdm/surrogate-models/fesl/fesl!40
2 parents a8a359a + 7a1fee1 commit f206204

22 files changed

+817
-953
lines changed

.gitlab-ci.yml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ setup-fesl:
4444
- *data_setup
4545
script:
4646
- python examples/ex00_verify_installation.py
47-
only:
48-
- master
49-
- conda-CI
50-
- pipelinetest
51-
- pipeline_fix_test-basic-functions
5247

5348
test-basic-functions:
5449
stage: test
@@ -61,11 +56,6 @@ test-basic-functions:
6156
- cd test
6257
- python fesl_tests.py
6358
needs: [setup-fesl]
64-
only:
65-
- master
66-
- conda-CI
67-
- pipelinetest
68-
- pipeline_fix_test-basic-functions
6959

7060
test-workflow:
7161
stage: test
@@ -77,10 +67,6 @@ test-workflow:
7767
- python ex99_verify_all_examples.py
7868
- cd ..
7969
needs: [setup-fesl]
80-
only:
81-
- master
82-
- conda-CI
83-
- pipeline_fix_test-basic-functions
8470

8571
#### documentation
8672

@@ -113,14 +99,6 @@ test-docstrings:
11399
script:
114100
- pydocstyle --convention=numpy fesl
115101
needs: [setup-docs]
116-
only:
117-
- master
118-
- conda-CI
119-
- documentation
120-
- fix_docstrings
121-
- 210222_LF_FixDocstrings
122-
- /^fix_docstrings_in_.*$/
123-
- pipeline_fix_test-basic-functions
124102

125103
pages:
126104
stage: deploy
@@ -147,11 +125,3 @@ pages:
147125
- public
148126
when:
149127
always
150-
only:
151-
- master
152-
- conda-CI
153-
- documentation
154-
- fix_docstrings
155-
- 210222_LF_FixDocstrings
156-
- /^fix_docstrings_in_.*$/
157-
- pipeline_fix_test-basic-functions

examples/ex01_run_singleshot.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
data_path = get_data_repo_path()+"Al256_reduced/"
55

66
"""
7-
ex01_run_singleshot.py: Shows how a neural network can be trained on material data using this framework.
8-
It uses preprocessed data, that is read in from *.npy files.
7+
ex01_run_singleshot.py: Shows how a neural network can be trained on material
8+
data using this framework. It uses preprocessed data, that is read in
9+
from *.npy files.
910
"""
1011

1112
printout("Welcome to FESL.")
@@ -18,48 +19,61 @@ def run_example01(desired_loss_improvement_factor=1):
1819

1920
####################
2021
# PARAMETERS
21-
# All parameters are handled from a central parameters class that contains subclasses.
22+
# All parameters are handled from a central parameters class that
23+
# contains subclasses.
2224
####################
25+
2326
test_parameters = fesl.Parameters()
27+
# Currently, the splitting in training, validation and test set are
28+
# done on a "by snapshot" basis. Specify how this is
29+
# done by providing a list containing entries of the form
30+
# "tr", "va" and "te".
2431
test_parameters.data.data_splitting_type = "by_snapshot"
2532
test_parameters.data.data_splitting_snapshots = ["tr", "va", "te"]
33+
34+
# Specify the data scaling.
2635
test_parameters.data.input_rescaling_type = "feature-wise-standard"
2736
test_parameters.data.output_rescaling_type = "normal"
28-
test_parameters.descriptors.twojmax = 11
29-
test_parameters.targets.ldos_gridsize = 10
37+
38+
# Specify the used activation function.
3039
test_parameters.network.layer_activations = ["ReLU"]
40+
41+
# Specify the training parameters.
3142
test_parameters.running.max_number_epochs = 20
3243
test_parameters.running.mini_batch_size = 40
3344
test_parameters.running.learning_rate = 0.00001
3445
test_parameters.running.trainingtype = "Adam"
35-
test_parameters.running.use_gpu = False
36-
test_parameters.running.use_horovod = False
37-
test_parameters.running.use_compression= False
3846

3947
####################
4048
# DATA
41-
# Read data into RAM.
42-
# We have to specify the directories we want to read the snapshots from.
43-
# The Handlerinterface will also return input and output scaler objects. These are used internally to scale
44-
# the data. The objects can be used after successful training for inference or plotting.
49+
# Add and prepare snapshots for training.
4550
####################
4651

4752
data_handler = fesl.DataHandler(test_parameters)
4853

4954
# Add a snapshot we want to use in to the list.
50-
data_handler.add_snapshot("Al_debug_2k_nr0.in.npy", data_path, "Al_debug_2k_nr0.out.npy", data_path, output_units="1/Ry")
51-
data_handler.add_snapshot("Al_debug_2k_nr1.in.npy", data_path, "Al_debug_2k_nr1.out.npy", data_path, output_units="1/Ry")
52-
data_handler.add_snapshot("Al_debug_2k_nr2.in.npy", data_path, "Al_debug_2k_nr2.out.npy", data_path, output_units="1/Ry")
53-
55+
data_handler.add_snapshot("Al_debug_2k_nr0.in.npy", data_path,
56+
"Al_debug_2k_nr0.out.npy", data_path,
57+
output_units="1/Ry")
58+
data_handler.add_snapshot("Al_debug_2k_nr1.in.npy", data_path,
59+
"Al_debug_2k_nr1.out.npy", data_path,
60+
output_units="1/Ry")
61+
data_handler.add_snapshot("Al_debug_2k_nr2.in.npy", data_path,
62+
"Al_debug_2k_nr2.out.npy", data_path,
63+
output_units="1/Ry")
5464
data_handler.prepare_data()
5565
printout("Read data: DONE.")
5666

5767
####################
5868
# NETWORK SETUP
5969
# Set up the network and trainer we want to use.
70+
# The layer sizes can be specified before reading data,
71+
# but it is safer this way.
6072
####################
6173

62-
test_parameters.network.layer_sizes = [data_handler.get_input_dimension(), 100, data_handler.get_output_dimension()]
74+
test_parameters.network.layer_sizes = [data_handler.get_input_dimension(),
75+
100,
76+
data_handler.get_output_dimension()]
6377

6478
# Setup network and trainer.
6579
test_network = fesl.Network(test_parameters)
@@ -76,10 +90,15 @@ def run_example01(desired_loss_improvement_factor=1):
7690
printout("Training: DONE.")
7791

7892
####################
93+
# RESULTS.
94+
# Print the used parameters and check whether the loss decreased enough.
95+
####################
96+
7997
printout("Parameters used for this experiment:")
8098
test_parameters.show()
8199

82-
if desired_loss_improvement_factor*test_trainer.initial_test_loss < test_trainer.final_test_loss:
100+
if desired_loss_improvement_factor*test_trainer.initial_test_loss\
101+
< test_trainer.final_test_loss:
83102
return False
84103
else:
85104
return True
@@ -89,5 +108,7 @@ def run_example01(desired_loss_improvement_factor=1):
89108
if run_example01():
90109
printout("Successfully ran ex01_run_singleshot.")
91110
else:
92-
raise Exception("Ran ex01_run_singleshot but something was off. If you haven't changed any parameters in "
93-
"the example, there might be a problem with your installation.")
111+
raise Exception("Ran ex01_run_singleshot but something was off."
112+
" If you haven't changed any parameters in "
113+
"the example, there might be a problem with your"
114+
" installation.")

examples/ex02_preprocess_data.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import fesl
2+
from fesl import printout
3+
from data_repo_path import get_data_repo_path
4+
import numpy as np
5+
data_path = get_data_repo_path()+"Al36/"
6+
7+
"""
8+
ex02_preprocess_data.py: Shows how this framework can be used to preprocess
9+
data. Preprocessing here means converting raw DFT calculation output into
10+
numpy arrays of the correct size. For the input data, this means descriptor
11+
calculation.
12+
13+
Further preprocessing steps (scaling, unit conversion) is done later.
14+
"""
15+
16+
17+
def run_example02():
18+
19+
####################
20+
# PARAMETERS
21+
# All parameters are handled from a central parameters class that
22+
# contains subclasses.
23+
####################
24+
25+
test_parameters = fesl.Parameters()
26+
27+
# Specify input data options, i.e. which descriptors are calculated
28+
# with which parameters. These are the standard parameters for
29+
# the calculation of SNAP descriptors.
30+
test_parameters.descriptors.descriptor_type = "SNAP"
31+
test_parameters.descriptors.twojmax = 10
32+
test_parameters.descriptors.rcutfac = 4.67637
33+
test_parameters.data.descriptors_contain_xyz = True
34+
35+
# Specify output data options, i.e. how the LDOS is parsed.
36+
# The Al system used as an example here actually has 250 energy levels.
37+
# But for the convenience of the user, only 10 energy levels will be
38+
# used for this example.
39+
test_parameters.targets.target_type = "LDOS"
40+
test_parameters.targets.ldos_gridsize = 10
41+
test_parameters.targets.ldos_gridspacing_ev = 0.1
42+
test_parameters.targets.ldos_gridoffset_ev = -10
43+
44+
####################
45+
# DATA
46+
# Create a DataConverter, and add snapshots to it.
47+
####################
48+
49+
data_converter = fesl.DataConverter(test_parameters)
50+
51+
# Take care to choose the "add_snapshot" function correct for
52+
# the type of data you want to preprocess.
53+
data_converter.add_snapshot_qeout_cube("Al.pw.scf.out", data_path,
54+
"cubes/tmp.pp*Al_ldos.cube",
55+
data_path, output_units="1/Ry")
56+
57+
# Convert all the snapshots and save them in the current directory.
58+
# data_converter.convert_snapshots("./", naming_scheme="Al_snapshot*")
59+
60+
####################
61+
# RESULTS.
62+
# Print the used parameters and check whether the preprocessed data
63+
# has the desired dimensions.
64+
####################
65+
66+
printout("Parameters used for this experiment:")
67+
test_parameters.show()
68+
69+
input_data = np.load("Al_snapshot0.in.npy")
70+
input_data_shape = np.shape(input_data)
71+
if input_data_shape[0] != 108 or input_data_shape[1] != 108 or \
72+
input_data_shape[2] != 100 or input_data_shape[3] != 94:
73+
return False
74+
75+
output_data = np.load("Al_snapshot0.out.npy")
76+
output_data_shape = np.shape(output_data)
77+
if output_data_shape[0] != 108 or output_data_shape[1] != 108 or \
78+
output_data_shape[2] != 100 or output_data_shape[3] != 10:
79+
return False
80+
81+
return True
82+
83+
84+
if __name__ == "__main__":
85+
if run_example02():
86+
printout("Successfully ran ex02_preprocess_data.")
87+
else:
88+
raise Exception("Ran ex02_preprocess_data but something was off."
89+
" If you haven't changed any parameters in "
90+
"the example, there might be a problem with your"
91+
" installation.")

examples/ex03_postprocess_data.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import fesl
2+
from fesl import printout
3+
import numpy as np
4+
from data_repo_path import get_data_repo_path
5+
data_path = get_data_repo_path()+"Al36/"
6+
7+
8+
"""
9+
ex03_postprocess_data.py: Shows how this framework can be used to
10+
postprocess data. Usually, this framework outputs LDOS data, thefore,
11+
post processing of LDOS data will be shown in the following.
12+
Set do_total_energy to False, if you don't have the QuantumEspresso
13+
Python module installed.
14+
"""
15+
16+
17+
def run_example03(do_total_energy=True, accuracy_electrons = 1e-11,
18+
accuracy_total_energy=50):
19+
20+
####################
21+
# PARAMETERS
22+
# All parameters are handled from a central parameters class that
23+
# contains subclasses.
24+
####################
25+
test_parameters = fesl.Parameters()
26+
27+
# Specify the correct LDOS parameters.
28+
test_parameters.targets.target_type = "LDOS"
29+
test_parameters.targets.ldos_gridsize = 250
30+
test_parameters.targets.ldos_gridspacing_ev = 0.1
31+
test_parameters.targets.ldos_gridoffset_ev = -10
32+
33+
####################
34+
# TARGETS
35+
# Create a target calculator to postprocess data.
36+
# Use this calculator to perform various operations.
37+
####################
38+
39+
ldos = fesl.TargetInterface(test_parameters)
40+
41+
# Read additional information about the calculation.
42+
# By doing this, the calculator is able to know e.g. the temperature
43+
# at which the calculation took place or the lattice constant used.
44+
ldos.read_additional_calculation_data("qe.out",
45+
data_path+"Al.pw.scf.out")
46+
47+
# Read in LDOS data. For actual workflows, this part will come
48+
# from a network.
49+
ldos_data = np.load(data_path+"Al_ldos.npy")
50+
51+
# Get quantities of interest.
52+
# For better values in the post processing, it is recommended to
53+
# calculate the "self-consistent Fermi energy", i.e. the Fermi energy
54+
# at which the (L)DOS reproduces the exact number of electrons.
55+
# This Fermi energy usually differs from the one outputted by the
56+
# QuantumEspresso calculation, due to numerical reasons. The difference
57+
# is usually very small.
58+
self_consistent_fermi_energy = ldos.\
59+
get_self_consistent_fermi_energy_ev(ldos_data)
60+
number_of_electrons = ldos.\
61+
get_number_of_electrons(ldos_data, fermi_energy_eV=
62+
self_consistent_fermi_energy)
63+
band_energy = ldos.get_band_energy(ldos_data,
64+
fermi_energy_eV=
65+
self_consistent_fermi_energy)
66+
if do_total_energy:
67+
# To perform a total energy calculation one also needs to provide
68+
# a pseudopotential(path).
69+
ldos.set_pseudopotential_path(data_path)
70+
total_energy = ldos.get_total_energy(ldos_data,
71+
fermi_energy_eV=
72+
self_consistent_fermi_energy)
73+
74+
####################
75+
# RESULTS.
76+
# Print the used parameters and check whether LDOS based results
77+
# are consistent with the actual DFT results.
78+
####################
79+
80+
printout("Parameters used for this experiment:")
81+
test_parameters.show()
82+
83+
print("Number of electrons:", number_of_electrons)
84+
print("Band energy:", band_energy)
85+
if do_total_energy:
86+
print("Total energy:", total_energy)
87+
88+
if np.abs(number_of_electrons - ldos.number_of_electrons) > \
89+
accuracy_electrons:
90+
return False
91+
92+
# FIXME: Add as soon as band_energy_dft_calculation is fixed.
93+
# if np.abs(number_of_electrons - ldos.number_of_electrons) > accuracy:
94+
# return True
95+
96+
if do_total_energy:
97+
if np.abs(total_energy - ldos.total_energy_dft_calculation) > \
98+
accuracy_total_energy:
99+
return False
100+
return True
101+
102+
103+
if __name__ == "__main__":
104+
if run_example03():
105+
printout("Successfully ran ex03_postprocess_data.")
106+
else:
107+
raise Exception("Ran ex03_postprocess_data but something was off."
108+
" If you haven't changed any parameters in "
109+
"the example, there might be a problem with your"
110+
" installation.")

0 commit comments

Comments
 (0)