Skip to content

Commit

Permalink
Small improvements in examples and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuellujan committed Jun 18, 2024
1 parent 407bdcf commit 560f4ed
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 76 deletions.
5 changes: 3 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pushfirst!(LOAD_PATH, joinpath(@__DIR__, "..")) # add PotentialLearning to environment stack
#pushfirst!(LOAD_PATH, joinpath(@__DIR__, "..")) # add PotentialLearning to environment stack

using PotentialLearning
using Documenter
Expand Down Expand Up @@ -32,11 +32,12 @@ function create_examples(examples, EXAMPLES_DIR, OUTPUT_DIR)
end
examples = [title => joinpath("generated", replace(example_path, ".jl" => ".md"))
for (title, example_path) in examples]
return examples
end

# Basic examples
examples = [
"Fit a-HfO2 dataset with ACE" => "ACE-aHfO2/fit-ace-aHfO2.jl",
"Fit a-HfO2 dataset with ACE" => "ACE-aHfO2/fit-ace-ahfo2.jl",
"Load Ar+Lennard-Jones dataset and postprocess" => "LJ-Ar/lennard-jones-ar.jl"
]
basic_examples = create_examples(examples, EXAMPLES_DIR, OUTPUT_DIR)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/how-to-run-the-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Clone `PotentialLearning.jl` repository in your working directory.
```
Access to any folder within `PotentialLearning.jl/examples`. E.g.
```shell
$ cd PotentialLearning.jl/examples/Na
$ cd PotentialLearning.jl/examples/DPP-ACE-aHfO2-1
```

## Run example
Expand All @@ -30,6 +30,6 @@ Type `]` to enter the Pkg REPL and instantiate.
```
Finally, include the example file.
```julia
julia> include("fit-dpp-ace-na.jl")
julia> include("fit-dpp-ace-ahfo2.jl")
```

Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@

# ## Load packages, define paths, and create experiment folder.

# Load packages
# Load packages.
using AtomsBase, InteratomicPotentials, PotentialLearning
using Unitful, UnitfulAtomic
using LinearAlgebra, Random, DisplayAs

# Define paths.
path = joinpath(dirname(pathof(PotentialLearning)), "../examples/ACE-aHfO2")
ds_path = "$path/../data/a-HfO2/a-HfO2-300K-NVT-6000.extxyz"
res_path = "$path/results/"
res_path = "$path/results/";

# Load utility functions.
include("$path/../utils/utils.jl")

# Create experiment folder.
run(`mkdir -p $res_path`)
run(`mkdir -p $res_path`);

# ## Load atomistic dataset and split it into training and test.

# Load atomistic dataset: atomistic configurations (atom positions, geometry, etc.) + DFT data (energies, forces, etc.)
ds = load_data(ds_path, uparse("eV"), uparse(""))
ds = load_data(ds_path, uparse("eV"), uparse(""))[1:1000] # Only the first 1K samples are used in this example.

# Split atomistic dataset into training and test
n_train, n_test = 50, 50 # only 50 samples per dataset are used in this example.
conf_train, conf_test = split(ds[1:1000], n_train, n_test)
n_train, n_test = 50, 50 # Only 50 samples per dataset are used in this example.
conf_train, conf_test = split(ds, n_train, n_test)

# ## Create ACE basis, compute descriptors and add them to the dataset.

Expand All @@ -37,7 +37,7 @@ basis = ACE(species = [:Hf, :O],
wL = 1.0,
csp = 1.0,
r0 = 1.0)
@save_var res_path basis
@save_var res_path basis;

# Compute ACE descriptors for energy and forces based on the atomistic training configurations.
println("Computing energy descriptors of training dataset...")
Expand Down Expand Up @@ -67,7 +67,7 @@ e_descr_test = compute_local_descriptors(conf_test, basis;
pbar = false)
println("Computing force descriptors of test dataset...")
f_descr_test = compute_force_descriptors(conf_test, basis;
pbar = false)
pbar = false);

# Update test dataset by adding energy and force descriptors.
ds_test = DataSet(conf_test .+ e_descr_test .+ f_descr_test)
Expand All @@ -92,9 +92,9 @@ f_test, f_test_pred = get_all_forces(ds_test),
@save_var res_path e_test
@save_var res_path e_test_pred
@save_var res_path f_test
@save_var res_path f_test_pred
@save_var res_path f_test_pred;

# Compute training metrics
# Compute training metrics.
e_train_metrics = get_metrics(e_train, e_train_pred,
metrics = [mae, rmse, rsq],
label = "e_train")
Expand All @@ -105,7 +105,7 @@ train_metrics = merge(e_train_metrics, f_train_metrics)
@save_dict res_path train_metrics
train_metrics

# Compute test metrics
# Compute test metrics.
e_test_metrics = get_metrics(e_test, e_test_pred,
metrics = [mae, rmse, rsq],
label = "e_test")
Expand All @@ -116,19 +116,19 @@ test_metrics = merge(e_test_metrics, f_test_metrics)
@save_dict res_path test_metrics
test_metrics

# Plot and save energy results
# Plot and save energy results.
e_plot = plot_energy(e_train, e_train_pred,
e_test, e_test_pred)
@save_fig res_path e_plot
DisplayAs.PNG(e_plot)

# Plot and save force results
# Plot and save force results.
f_plot = plot_forces(f_train, f_train_pred,
f_test, f_test_pred)
@save_fig res_path f_plot
DisplayAs.PNG(f_plot)

# Plot and save training force cosine
# Plot and save training force cosine.
e_train_plot = plot_energy(e_train, e_train_pred)
f_train_plot = plot_forces(f_train, f_train_pred)
f_train_cos = plot_cos(f_train, f_train_pred)
Expand All @@ -137,7 +137,7 @@ f_train_cos = plot_cos(f_train, f_train_pred)
@save_fig res_path f_train_cos
DisplayAs.PNG(f_train_cos)

# Plot and save test force cosine
# Plot and save test force cosine.
e_test_plot = plot_energy(e_test, e_test_pred)
f_test_plot = plot_forces(f_test, f_test_pred)
f_test_cos = plot_cos(f_test, f_test_pred)
Expand Down
4 changes: 2 additions & 2 deletions examples/DPP-ACE-Na/fit-dpp-ace-na.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

# ## Load packages and define paths.

# Load packages
# Load packages.
using Unitful, UnitfulAtomic
using AtomsBase, InteratomicPotentials, PotentialLearning
using LinearAlgebra, Plots

# Define paths.
path = joinpath(dirname(pathof(PotentialLearning)), "../examples/DPP-ACE-Na")
ds_path = "$path/../data/Na/liquify_sodium.yaml"
ds_path = "$path/../data/Na/liquify_sodium.yaml";

# ## Load atomistic dataset and split it into training and test.

Expand Down
14 changes: 7 additions & 7 deletions examples/DPP-ACE-Si/fit-dpp-ace-si.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

# ## Load packages, define paths, and create experiment folder.

# Load packages
# Load packages.
using LinearAlgebra, Random, InvertedIndices
using Statistics, StatsBase, Distributions, Determinantal
using Unitful, UnitfulAtomic
using AtomsBase, InteratomicPotentials, PotentialLearning
using CSV, JLD, DataFrames

# Define atomic type information
# Define atomic type information.
elname, elspec = "Si", [:Si]

# Define paths.
Expand All @@ -20,15 +20,15 @@ outpath = "$path/output/$elname/"
# Load utility functions.
include("$path/subsampling_utils.jl")

# ## Load atomistic datasets
# ## Load atomistic datasets.

# Load all atomistic datasets: atomistic configurations (atom positions, geometry, etc.) + DFT data (energies, forces, etc.)
file_arr = readext(inpath, "xyz")
nfile = length(file_arr)
confs_arr = [load_data(inpath*file, ExtXYZ(u"eV", u"")) for file in file_arr]
confs = concat_dataset(confs_arr)

# Id of configurations per file
# Id of configurations per file.
n = 0
confs_id = Vector{Vector{Int64}}(undef, nfile)
for k = 1:nfile
Expand All @@ -37,9 +37,9 @@ for k = 1:nfile
n += length(confs_arr[k])
end

# ## Subsampling by DPP
# ## Subsampling by DPP.

# Create ACE basis
# Create ACE basis.
nbody = 4
deg = 5
ace = ACE(species = elspec, # species
Expand All @@ -61,7 +61,7 @@ JLD.save(outpath*"$(elname)_force_descriptors.jld", "f_descr", f_descr)
ds = DataSet(confs .+ e_descr .+ f_descr)
ndata = length(ds)

# ## Compute cross validation error from training dataset
# ## Compute cross validation error from training dataset.
batch_size = [80, 40]
sel_ind = Dict{Int64, Vector}()
cond_num = Dict{Int64, Vector}()
Expand Down
24 changes: 12 additions & 12 deletions examples/DPP-ACE-aHfO2-1/fit-dpp-ace-ahfo2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

# ## Load packages, define paths, and create experiment folder.

# Load packages
# Load packages.
using AtomsBase, InteratomicPotentials, PotentialLearning
using Unitful, UnitfulAtomic
using LinearAlgebra, Random, DisplayAs

# Define paths.
path = joinpath(dirname(pathof(PotentialLearning)), "../examples/DPP-ACE-aHfO2-1")
ds_path = "$path/../data/a-HfO2/a-HfO2-300K-NVT-6000.extxyz"
res_path = "$path/results/"
res_path = "$path/results/";

# Load utility functions.
include("$path/../utils/utils.jl")

# Create experiment folder.
run(`mkdir -p $res_path`)
run(`mkdir -p $res_path`);

# ## Load atomistic dataset and split it into training and test.

Expand Down Expand Up @@ -66,7 +66,7 @@ basis = ACE(species = [:Hf, :O],
wL = 1.0,
csp = 1.0,
r0 = 1.0)
@save_var res_path basis
@save_var res_path basis;

# Compute ACE descriptors for energy and forces based on the atomistic training configurations.
println("Computing energy descriptors of training dataset...")
Expand Down Expand Up @@ -96,7 +96,7 @@ e_descr_test = compute_local_descriptors(conf_test, basis;
pbar = false)
println("Computing force descriptors of test dataset...")
f_descr_test = compute_force_descriptors(conf_test, basis;
pbar = false)
pbar = false);

# Update test dataset by adding energy and force descriptors.
ds_test = DataSet(conf_test .+ e_descr_test .+ f_descr_test)
Expand All @@ -121,9 +121,9 @@ f_test, f_test_pred = get_all_forces(ds_test),
@save_var res_path e_test
@save_var res_path e_test_pred
@save_var res_path f_test
@save_var res_path f_test_pred
@save_var res_path f_test_pred;

# Compute training metrics
# Compute training metrics.
e_train_metrics = get_metrics(e_train, e_train_pred,
metrics = [mae, rmse, rsq],
label = "e_train")
Expand All @@ -134,7 +134,7 @@ train_metrics = merge(e_train_metrics, f_train_metrics)
@save_dict res_path train_metrics
train_metrics

# Compute test metrics
# Compute test metrics.
e_test_metrics = get_metrics(e_test, e_test_pred,
metrics = [mae, rmse, rsq],
label = "e_test")
Expand All @@ -145,19 +145,19 @@ test_metrics = merge(e_test_metrics, f_test_metrics)
@save_dict res_path test_metrics
test_metrics

# Plot and save energy results
# Plot and save energy results.
e_plot = plot_energy(e_train, e_train_pred,
e_test, e_test_pred)
@save_fig res_path e_plot
DisplayAs.PNG(e_plot)

# Plot and save force results
# Plot and save force results.
f_plot = plot_forces(f_train, f_train_pred,
f_test, f_test_pred)
@save_fig res_path f_plot
DisplayAs.PNG(f_plot)

# Plot and save training force cosine
# Plot and save training force cosine.
e_train_plot = plot_energy(e_train, e_train_pred)
f_train_plot = plot_forces(f_train, f_train_pred)
f_train_cos = plot_cos(f_train, f_train_pred)
Expand All @@ -166,7 +166,7 @@ f_train_cos = plot_cos(f_train, f_train_pred)
@save_fig res_path f_train_cos
DisplayAs.PNG(f_train_cos)

# Plot and save test force cosine
# Plot and save test force cosine.
e_test_plot = plot_energy(e_test, e_test_pred)
f_test_plot = plot_forces(f_test, f_test_pred)
f_test_cos = plot_cos(f_test, f_test_pred)
Expand Down
16 changes: 8 additions & 8 deletions examples/LJ-Ar/lennard-jones-ar.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# # Load Ar dataset with energies computed by Lennard-Jones and postprocess

# ## Load packages and define paths
# ## Load packages and define paths.

# Load packages
# Load packages.
using Unitful, UnitfulAtomic
using AtomsBase, InteratomicPotentials, PotentialLearning
using LinearAlgebra, Plots, DisplayAs

# Define paths.
path = joinpath(dirname(pathof(PotentialLearning)), "../examples/LJ-Ar")
ds_path = "$path/../data/LJ-AR/lj-ar.yaml"
ds_path = "$path/../data/LJ-AR/lj-ar.yaml";

# ## Load atomistic dataset
# ## Load atomistic dataset.
ds, thermo = load_data(ds_path, YAML(:Ar, u"eV", u""))
ds = @views ds[2:end] # Filter first configuration (zero energy)

Expand All @@ -23,15 +23,15 @@ n_atoms = length(first(systems)) # Note: in this dataset all systems contain the
positions = position.(systems)
dists_origin = map(x->ustrip.(norm.(x)), positions)

# Extract LJ energies from dataset
# Extract LJ energies from dataset.
energies = get_values.(get_energy.(ds))

# Define time range
# Define time range.
time_range = 0.5:0.5:5000

# ## Post-process data.

# Plot distance from origin vs time
# Plot distance from origin vs time.
p = plot(xlabel = "τ | ps",
ylabel = "Distance from origin | Å",
dpi = 300, fontsize = 12)
Expand All @@ -40,7 +40,7 @@ for i = 1:n_atoms
end
DisplayAs.PNG(p)

# Plot LJ energies vs time
# Plot LJ energies vs time.
p = plot(time_range, energies,
xlabel = "τ | ps",
ylabel = "Lennard Jones energy | eV",
Expand Down
Loading

0 comments on commit 560f4ed

Please sign in to comment.