Skip to content

Commit

Permalink
Merge branch 'uf3:master' into lammps_implementation_v2
Browse files Browse the repository at this point in the history
  • Loading branch information
monk-04 authored Oct 11, 2023
2 parents be41ba0 + 311ade2 commit 39ed792
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 46 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ uf3/util/__pycache__/plotting.cpython-37.pyc
uf3/util/__pycache__/subsample.cpython-37.pyc

lammps_plugin/ML-UF3/.vscode/

.vscode/

*/__pycache__

*/*/__pycache__

*/*/*/__pycache__

85 changes: 52 additions & 33 deletions lammps_plugin/ML-UF3/pair_uf3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ void PairUF3::uf3_read_pot_file(int itype, int jtype, int ktype, char *potf_name
}

min_cut_3b[itype][jtype][ktype][0] = n3b_knot_matrix[itype][jtype][ktype][0][0];
min_cut_3b[itype][ktype][jtype][0] = min_cut_3b[itype][ktype][jtype][0];
min_cut_3b[itype][ktype][jtype][0] = n3b_knot_matrix[itype][ktype][jtype][0][0];
if (comm->me == 0)
utils::logmesg(lmp, "UF3: 3b min cutoff 0 {} {}\n", potf_name,
min_cut_3b[itype][jtype][ktype][0],
min_cut_3b[itype][ktype][jtype][0]);
utils::logmesg(lmp, "UF3: 3b min cutoff {} {}-{}-{}_0={} {}-{}-{}_0={}\n",
potf_name,itype,jtype,ktype,min_cut_3b[itype][jtype][ktype][0],
itype,ktype,jtype,min_cut_3b[itype][ktype][jtype][0]);

int num_knots_3b_ik = fp3rd_line.next_int();
temp_line = txtfilereader.next_line(num_knots_3b_ik);
Expand All @@ -473,18 +473,19 @@ void PairUF3::uf3_read_pot_file(int itype, int jtype, int ktype, char *potf_name
num_knots_3b_ik, fp5th_line.count());

n3b_knot_matrix[itype][jtype][ktype][1].resize(num_knots_3b_ik);
n3b_knot_matrix[itype][ktype][jtype][1].resize(num_knots_3b_ik);
n3b_knot_matrix[itype][ktype][jtype][2].resize(num_knots_3b_ik);
for (int i = 0; i < num_knots_3b_ik; i++) {
n3b_knot_matrix[itype][jtype][ktype][1][i] = fp5th_line.next_double();
n3b_knot_matrix[itype][ktype][jtype][1][i] =
n3b_knot_matrix[itype][ktype][jtype][2][i] =
n3b_knot_matrix[itype][jtype][ktype][1][i];
}

min_cut_3b[itype][jtype][ktype][1] = n3b_knot_matrix[itype][jtype][ktype][1][0];
min_cut_3b[itype][ktype][jtype][1] = min_cut_3b[itype][ktype][jtype][1];
min_cut_3b[itype][ktype][jtype][2] = n3b_knot_matrix[itype][ktype][jtype][2][0];
if (comm->me == 0)
utils::logmesg(lmp, "UF3: 3b min cutoff 1 {} {}\n", potf_name,
min_cut_3b[itype][jtype][ktype][1], min_cut_3b[itype][ktype][jtype][1]);
utils::logmesg(lmp, "UF3: 3b min cutoff {} {}-{}-{}_1={} {}-{}-{}_2={}\n",
potf_name,itype,jtype,ktype,min_cut_3b[itype][jtype][ktype][1],
itype,ktype,jtype,min_cut_3b[itype][ktype][jtype][2]);

int num_knots_3b_ij = fp3rd_line.next_int();
temp_line = txtfilereader.next_line(num_knots_3b_ij);
Expand All @@ -495,18 +496,19 @@ void PairUF3::uf3_read_pot_file(int itype, int jtype, int ktype, char *potf_name
num_knots_3b_ij, fp5th_line.count());

n3b_knot_matrix[itype][jtype][ktype][2].resize(num_knots_3b_ij);
n3b_knot_matrix[itype][ktype][jtype][2].resize(num_knots_3b_ij);
n3b_knot_matrix[itype][ktype][jtype][1].resize(num_knots_3b_ij);
for (int i = 0; i < num_knots_3b_ij; i++) {
n3b_knot_matrix[itype][jtype][ktype][2][i] = fp6th_line.next_double();
n3b_knot_matrix[itype][ktype][jtype][2][i] =
n3b_knot_matrix[itype][ktype][jtype][1][i] =
n3b_knot_matrix[itype][jtype][ktype][2][i];
}

min_cut_3b[itype][jtype][ktype][2] = n3b_knot_matrix[itype][jtype][ktype][2][0];
min_cut_3b[itype][ktype][jtype][2] = min_cut_3b[itype][ktype][jtype][2];
min_cut_3b[itype][ktype][jtype][1] = n3b_knot_matrix[itype][ktype][jtype][1][0];
if (comm->me == 0)
utils::logmesg(lmp, "UF3: 3b min cutoff 2 {} {}\n", potf_name,
min_cut_3b[itype][jtype][ktype][2], min_cut_3b[itype][ktype][jtype][2]);
utils::logmesg(lmp, "UF3: 3b min cutoff {} {}-{}-{}_2={} {}-{}-{}_1={}\n",
potf_name,itype,jtype,ktype,min_cut_3b[itype][jtype][ktype][2],
itype,ktype,jtype,min_cut_3b[itype][ktype][jtype][1]);

temp_line = txtfilereader.next_line(3);
ValueTokenizer fp7th_line(temp_line);
Expand Down Expand Up @@ -555,9 +557,22 @@ void PairUF3::uf3_read_pot_file(int itype, int jtype, int ktype, char *potf_name
}
}

key = std::to_string(itype) + std::to_string(ktype) + std::to_string(jtype);
n3b_coeff_matrix[key] = n3b_coeff_matrix[std::to_string(itype) +
std::to_string(jtype) + std::to_string(ktype)];
std::string key2 = std::to_string(itype) + std::to_string(ktype) + std::to_string(jtype);
n3b_coeff_matrix[key2].resize(coeff_matrix_dim2);
for (int j = 0; j < coeff_matrix_dim2; j++) {
n3b_coeff_matrix[key2][j].resize(coeff_matrix_dim1);
for (int i = 0; i < coeff_matrix_dim1; i++) {
n3b_coeff_matrix[key2][j][i].resize(coeff_matrix_dim3);
}
}

for (int i = 0; i < coeff_matrix_dim1; i++) {
for (int j = 0; j < coeff_matrix_dim2; j++) {
for (int k = 0; k < coeff_matrix_dim3; k++) {
n3b_coeff_matrix[key2][j][i][k] = n3b_coeff_matrix[key][i][j][k];
}
}
}

setflag_3b[itype][jtype][ktype] = 1;
setflag_3b[itype][ktype][jtype] = 1;
Expand Down Expand Up @@ -663,6 +678,7 @@ void PairUF3::uf3_read_pot_file(char *potf_name)
}
// cut_3b[temp_type1][temp_type3] = std::max(cut_3b[temp_type1][temp_type3],cut3b_rik);
cut_3b_list[temp_type1][temp_type3] = std::max(cut_3b_list[temp_type1][temp_type3], cut3b_rik);

cut_3b[temp_type1][temp_type2][temp_type3] = cut3b_rij;
cut_3b[temp_type1][temp_type3][temp_type2] = cut3b_rik;

Expand All @@ -682,44 +698,47 @@ void PairUF3::uf3_read_pot_file(char *potf_name)
}

min_cut_3b[temp_type1][temp_type2][temp_type3][0] = n3b_knot_matrix[temp_type1][temp_type2][temp_type3][0][0];
min_cut_3b[temp_type1][temp_type3][temp_type2][0] = min_cut_3b[temp_type1][temp_type3][temp_type2][0];
min_cut_3b[temp_type1][temp_type3][temp_type2][0] = n3b_knot_matrix[temp_type1][temp_type3][temp_type2][0][0];
if (comm->me == 0)
utils::logmesg(lmp, "UF3: 3b min cutoff 0 {} {}\n", potf_name,
min_cut_3b[temp_type1][temp_type2][temp_type3][0], min_cut_3b[temp_type1][temp_type3][temp_type2][0]);
utils::logmesg(lmp, "UF3: 3b min cutoff {} {}-{}-{}_0={} {}-{}-{}_0={}\n",
potf_name,temp_type1,temp_type2,temp_type3,min_cut_3b[temp_type1][temp_type2][temp_type3][0],
temp_type1,temp_type3,temp_type2,min_cut_3b[temp_type1][temp_type3][temp_type2][0]);

temp_line_len = fp3rd_line.next_int();
temp_line = txtfilereader.next_line(temp_line_len);
ValueTokenizer fp5th_line(temp_line);
n3b_knot_matrix[temp_type1][temp_type2][temp_type3][1].resize(temp_line_len);
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][1].resize(temp_line_len);
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][2].resize(temp_line_len);
for (int i = 0; i < temp_line_len; i++) {
n3b_knot_matrix[temp_type1][temp_type2][temp_type3][1][i] = fp5th_line.next_double();
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][1][i] =
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][2][i] =
n3b_knot_matrix[temp_type1][temp_type2][temp_type3][1][i];
}

min_cut_3b[temp_type1][temp_type2][temp_type3][1] = n3b_knot_matrix[temp_type1][temp_type2][temp_type3][1][0];
min_cut_3b[temp_type1][temp_type3][temp_type2][1] = min_cut_3b[temp_type1][temp_type3][temp_type2][1];
min_cut_3b[temp_type1][temp_type3][temp_type2][2] = n3b_knot_matrix[temp_type1][temp_type3][temp_type2][2][0];
if (comm->me == 0)
utils::logmesg(lmp, "UF3: 3b min cutoff 1 {} {}\n", potf_name,
min_cut_3b[temp_type1][temp_type2][temp_type3][1], min_cut_3b[temp_type1][temp_type3][temp_type2][1]);
utils::logmesg(lmp, "UF3: 3b min cutoff {} {}-{}-{}_1={} {}-{}-{}_2={}\n",
potf_name,temp_type1,temp_type2,temp_type3,min_cut_3b[temp_type1][temp_type2][temp_type3][1],
temp_type1,temp_type3,temp_type2,min_cut_3b[temp_type1][temp_type3][temp_type2][2]);

temp_line_len = fp3rd_line.next_int();
temp_line = txtfilereader.next_line(temp_line_len);
ValueTokenizer fp6th_line(temp_line);
n3b_knot_matrix[temp_type1][temp_type2][temp_type3][2].resize(temp_line_len);
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][2].resize(temp_line_len);
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][1].resize(temp_line_len);
for (int i = 0; i < temp_line_len; i++) {
n3b_knot_matrix[temp_type1][temp_type2][temp_type3][2][i] = fp6th_line.next_double();
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][2][i] =
n3b_knot_matrix[temp_type1][temp_type3][temp_type2][1][i] =
n3b_knot_matrix[temp_type1][temp_type2][temp_type3][2][i];
}

min_cut_3b[temp_type1][temp_type2][temp_type3][2] = n3b_knot_matrix[temp_type1][temp_type2][temp_type3][2][0];
min_cut_3b[temp_type1][temp_type3][temp_type2][2] = min_cut_3b[temp_type1][temp_type3][temp_type2][2];
min_cut_3b[temp_type1][temp_type3][temp_type2][1] = n3b_knot_matrix[temp_type1][temp_type3][temp_type2][1][0];
if (comm->me == 0)
utils::logmesg(lmp, "UF3: 3b min cutoff 2 {} {}\n", potf_name,
min_cut_3b[temp_type1][temp_type2][temp_type3][2], min_cut_3b[temp_type1][temp_type3][temp_type2][2]);
utils::logmesg(lmp, "UF3: 3b min cutoff {} {}-{}-{}_2={} {}-{}-{}_1={}\n",
potf_name,temp_type1,temp_type2,temp_type3,min_cut_3b[temp_type1][temp_type2][temp_type3][2],
temp_type1,temp_type3,temp_type2,min_cut_3b[temp_type1][temp_type3][temp_type2][2]);

temp_line = txtfilereader.next_line(3);
ValueTokenizer fp7th_line(temp_line);
Expand Down Expand Up @@ -838,7 +857,9 @@ void PairUF3::create_bsplines()
std::string key = std::to_string(i) + std::to_string(j) + std::to_string(k);
UFBS3b[i][j][k] =
uf3_triplet_bspline(lmp, n3b_knot_matrix[i][j][k], n3b_coeff_matrix[key]);
UFBS3b[i][k][j] = UFBS3b[i][j][k];
std::string key2 = std::to_string(i) + std::to_string(k) + std::to_string(j);
UFBS3b[i][k][j] =
uf3_triplet_bspline(lmp, n3b_knot_matrix[i][k][j], n3b_coeff_matrix[key2]);
}
}
}
Expand Down Expand Up @@ -1006,7 +1027,6 @@ void PairUF3::compute(int eflag, int vflag)
((del_rkj[0] * del_rkj[0]) + (del_rkj[1] * del_rkj[1]) + (del_rkj[2] * del_rkj[2])));

if (rjk >= min_cut_3b[itype][jtype][ktype][2]){
//utils::logmesg(lmp, "UF3: {} {} {}",itype,jtype,ktype);
double *triangle_eval = UFBS3b[itype][jtype][ktype].eval(rij, rik, rjk);

fij[0] = *(triangle_eval + 1) * (del_rji[0] / rij);
Expand Down Expand Up @@ -1127,7 +1147,6 @@ double PairUF3::single(int /*i*/, int /*j*/, int itype, int jtype, double rsq,

double PairUF3::memory_usage()
{
utils::logmesg(lmp, "\nUF3: memory_usage called");
double bytes = Pair::memory_usage();

bytes = 0;
Expand Down
2 changes: 2 additions & 0 deletions lammps_plugin/ML-UF3/uf3_pair_bspline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@ double uf3_pair_bspline::memory_usage()
bytes += (double)dnbspline_bases[i].memory_usage(); //bspline_basis2

bytes += (double)2*sizeof(double); //ret_val

return bytes;
}
2 changes: 2 additions & 0 deletions uf3/default_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ learning:
weight: 0.5
regularizer:
ridge_1b: 1.0e-8
ridge_2b: 0.0
ridge_3b: 1.0e-5
curvature_2b: 1.0e-8
curvature_3b: 1.0e-8
18 changes: 9 additions & 9 deletions uf3/regression/least_squares.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ def batched_predict(self,
Returns:
y_e (np.ndarray): target values for energies.
p_e (np.ndarray): prediction values for forces.
y_f (np.ndarray): target values for energies.
p_f (np.ndarray): target values for forces.
p_e (np.ndarray): prediction values for energies.
y_f (np.ndarray): target values for forces.
p_f (np.ndarray): prediction values for forces.
rmse_e (np.ndarray): RMSE across energy predictions.
rmse_e (np.ndarray): RMSE across force predictions.
"""
Expand Down Expand Up @@ -934,9 +934,9 @@ def subset_prediction(df: pd.DataFrame,
Returns:
y_e (np.ndarray): target values for energies.
p_e (np.ndarray): prediction values for forces.
y_f (np.ndarray): target values for energies.
p_f (np.ndarray): target values for forces.
p_e (np.ndarray): prediction values for energies.
y_f (np.ndarray): target values for forces.
p_f (np.ndarray): prediction values for forces.
"""
if subset_keys is not None:
idx = df.index.unique(level=0).intersection(subset_keys)
Expand Down Expand Up @@ -967,9 +967,9 @@ def batched_prediction(model: WeightedLinearModel,
Returns:
y_e (np.ndarray): target values for energies.
p_e (np.ndarray): prediction values for forces.
y_f (np.ndarray): target values for energies.
p_f (np.ndarray): target values for forces.
p_e (np.ndarray): prediction values for energies.
y_f (np.ndarray): target values for forces.
p_f (np.ndarray): prediction values for forces.
"""
if table_names is None:
_, _, table_names, _ = io.analyze_hdf_tables(filename)
Expand Down
4 changes: 2 additions & 2 deletions uf3/representation/bspline.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ def get_regularization_matrix(self,
Args:
ridge_map (dict): n-body term ridge regularizer strengths.
default: {1: 1e-4, 2: 1e-6, 3: 1e-5}
default: {1: 1e-8, 2: 0e0, 3: 1e-5}
curvature_map (dict): n-body term curvature regularizer strengths.
default: {1: 0.0, 2: 1e-5, 3: 1e-5}
default: {1: 0.0, 2: 1e-8, 3: 1e-8}
TODO: refactor to break up into smaller, reusable functions
Expand Down
5 changes: 3 additions & 2 deletions uf3/representation/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def evaluate_configuration(self,
{(name, 'e'), (name, 'fx'), ...{ instead of {'e', 'fx', ...}
energy (float): energy of configuration (optional).
forces (list, np.ndarray): array containing force components
fx, fy, fz for each atom. Expected shape is (n_atoms, 3).
fx, fy, fz for each atom. Expected shape is (3, n_atoms).
energy_key (str): column name for energies, default "energy".
Returns:
Expand Down Expand Up @@ -527,6 +527,7 @@ def get_training_tuples(self, df_features, kappa, data_coordinator):
y (np.ndarray): target vector.
w (np.ndarray): weight vector for machine learning.
"""
warnings.warn("get_training_tuples() is deprecated.", DeprecationWarning)
energy_key = data_coordinator.energy_key
x, y, w = dataframe_to_training_tuples(df_features,
kappa=kappa,
Expand Down Expand Up @@ -626,4 +627,4 @@ def flatten_by_interactions(vector_map, pair_tuples):
(np.ndarray): concatenated result of joining vector_map
in order of occurrence in pair_tuples.
"""
return np.concatenate([vector_map[pair] for pair in pair_tuples], axis=-1)
return np.concatenate([vector_map[pair] for pair in pair_tuples], axis=-1)

0 comments on commit 39ed792

Please sign in to comment.