Skip to content

Commit

Permalink
Merge pull request #4 from mir-group/master
Browse files Browse the repository at this point in the history
Pulling master into my dev branch
  • Loading branch information
dmclark17 authored Mar 9, 2020
2 parents f431cfb + 8613c5c commit 6f2ee01
Show file tree
Hide file tree
Showing 24 changed files with 1,284 additions and 523 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ add_subdirectory(tests)
add_subdirectory(timing)
add_subdirectory(pybind11)
add_subdirectory(googletest)
add_subdirectory(parallel_tests)

# make python bindings
pybind11_add_module(ace src/ace_binding.cpp src/y_grad.cpp src/structure.cpp
src/local_environment.cpp src/descriptor.cpp
src/radial.cpp src/cutoffs.cpp src/single_bond.cpp
src/kernels.cpp src/sparse_gp.cpp)
find_package(OpenMP REQUIRED)
target_link_libraries(ace PRIVATE OpenMP::OpenMP_CXX)
6 changes: 6 additions & 0 deletions parallel_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# make library
add_executable(par_test par_test.cpp)

find_package(OpenMP REQUIRED)
target_link_libraries(par_test PRIVATE OpenMP::OpenMP_CXX)

27 changes: 27 additions & 0 deletions parallel_tests/par_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
#include <Eigen/Dense>

int main(){
auto t1 = std::chrono::high_resolution_clock::now();

int mat_size = 5;
Eigen::MatrixXd kern_mat = Eigen::MatrixXd::Zero(mat_size, mat_size);
Eigen::VectorXd vec_curr(5);

#pragma omp parallel for
for (int i = 0; i < mat_size; i ++){
std::this_thread::sleep_for(std::chrono::seconds(1));
vec_curr << i, i+1, i+2, i+3, i+4;
kern_mat.col(i) = vec_curr;
}

auto t2 = std::chrono::high_resolution_clock::now();
auto tot_time =
std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count();
std::cout << tot_time << std::endl;
std::cout << kern_mat << std::endl;

}
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ add_library(Ace y_grad.cpp radial.cpp cutoffs.cpp single_bond.cpp
structure.cpp local_environment.cpp descriptor.cpp
kernels.cpp sparse_gp.cpp)

find_package(OpenMP REQUIRED)
target_link_libraries(Ace PRIVATE OpenMP::OpenMP_CXX)

set(ACE_INCLUDE_DIR "${ACE_ROOT_DIR}/src" PARENT_SCOPE)
149 changes: 96 additions & 53 deletions src/ace_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "structure.h"
#include "local_environment.h"
#include "descriptor.h"
#include "sparse_gp.h"
#include "kernels.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand All @@ -12,49 +14,8 @@

namespace py = pybind11;

// Define spherical harmonics class.
class SphericalHarmonics{
public:
double x, y, z;
int lmax;
std::vector<double> Y, Yx, Yy, Yz;

SphericalHarmonics(double x, double y, double z, int lmax);
};

SphericalHarmonics :: SphericalHarmonics(double x, double y, double z,
int lmax){
int number_of_harmonics = (lmax + 1) * (lmax + 1);

this->x = x;
this->y = y;
this->z = z;
this->lmax = lmax;

// Initialize spherical harmonic vectors.
Y = std::vector<double>(number_of_harmonics, 0);
Yx = std::vector<double>(number_of_harmonics, 0);
Yy = std::vector<double>(number_of_harmonics, 0);
Yz = std::vector<double>(number_of_harmonics, 0);

get_Y(Y, Yx, Yy, Yz, x, y, z, lmax);
};

PYBIND11_MODULE(ace, m){
// Bind the spherical harmonics class.
py::class_<SphericalHarmonics>(m, "SphericalHarmonics")
.def(py::init<double, double, double, int>())
// Make attributes accessible.
.def_readwrite("x", &SphericalHarmonics::x)
.def_readwrite("y", &SphericalHarmonics::y)
.def_readwrite("z", &SphericalHarmonics::z)
.def_readwrite("lmax", &SphericalHarmonics::lmax)
.def_readwrite("Y", &SphericalHarmonics::Y)
.def_readwrite("Yx", &SphericalHarmonics::Yx)
.def_readwrite("Yy", &SphericalHarmonics::Yy)
.def_readwrite("Yz", &SphericalHarmonics::Yz);

// Bind the structure class.
// Structure
py::class_<Structure>(m, "Structure")
.def(py::init<const Eigen::MatrixXd &,
const std::vector<int> &,
Expand All @@ -67,6 +28,32 @@ PYBIND11_MODULE(ace, m){
.def_readwrite("volume", &Structure::volume)
.def("wrap_positions", &Structure::wrap_positions);

py::class_<StructureDescriptor, Structure>(m, "StructureDescriptor")
.def(py::init<const Eigen::MatrixXd &, const std::vector<int> &,
const Eigen::MatrixXd &, double>())
// n-body
.def(py::init<const Eigen::MatrixXd &, const std::vector<int> &,
const Eigen::MatrixXd &, double, std::vector<double>>())
// many-body
.def(py::init<const Eigen::MatrixXd &, const std::vector<int> &,
const Eigen::MatrixXd &, double, std::vector<double>,
std::vector<DescriptorCalculator *>>())
// n-body + many-body
.def(py::init<const Eigen::MatrixXd &, const std::vector<int> &,
const Eigen::MatrixXd &, double, std::vector<double>,
std::vector<double>,
std::vector<DescriptorCalculator *>>())
.def_readwrite("local_environments",
&StructureDescriptor::local_environments)
.def_readwrite("energy", &StructureDescriptor::energy)
.def_readwrite("forces", &StructureDescriptor::forces)
.def_readwrite("stresses", &StructureDescriptor::stresses)
.def_readwrite("cutoff", &StructureDescriptor::cutoff)
.def_readwrite("n_body_cutoffs", &StructureDescriptor::n_body_cutoffs)
.def_readwrite("many_body_cutoffs",
&StructureDescriptor::many_body_cutoffs);

// Local environment
py::class_<LocalEnvironment>(m, "LocalEnvironment")
.def(py::init<const Structure &, int, double>())
.def_readwrite("sweep", &LocalEnvironment::sweep)
Expand All @@ -82,18 +69,74 @@ PYBIND11_MODULE(ace, m){
.def_readwrite("xs", &LocalEnvironment::xs)
.def_readwrite("ys", &LocalEnvironment::ys)
.def_readwrite("zs", &LocalEnvironment::zs)
.def_readwrite("cutoff", &LocalEnvironment::cutoff);
.def_readwrite("cutoff", &LocalEnvironment::cutoff)
.def_readwrite("descriptor_vals", &LocalEnvironment::descriptor_vals)
.def_readwrite("descriptor_force_dervs",
&LocalEnvironment::descriptor_force_dervs)
.def_readwrite("descriptor_stress_dervs",
&LocalEnvironment::descriptor_stress_dervs);

py::class_<B2_Calculator>(m, "B2_Calculator")
.def(py::init<const std::string &, const std::string &,
const std::vector<double> &, const std::vector<double> &,
const std::vector<int> &>())
.def_readwrite("radial_basis", &B2_Calculator::radial_basis)
.def_readwrite("cutoff_function", &B2_Calculator::cutoff_function)
.def_readwrite("descriptor_vals", &B2_Calculator::descriptor_vals)
// Eigen::VectorXd descriptor_vals;
// Eigen::MatrixXd descriptor_force_dervs, descriptor_stress_dervs,
// force_dot, stress_dot;
// Descriptor calculators
py::class_<DescriptorCalculator>(m, "DescriptorCalculator")
.def("compute", &DescriptorCalculator::compute)
.def_readwrite("radial_basis", &DescriptorCalculator::radial_basis)
.def_readwrite("cutoff_function",
&DescriptorCalculator::cutoff_function)
.def_readwrite("descriptor_vals",
&DescriptorCalculator::descriptor_vals)
.def_readwrite("descriptor_force_dervs",
&B2_Calculator::descriptor_force_dervs)
&DescriptorCalculator::descriptor_force_dervs)
.def_readwrite("descriptor_stress_dervs",
&B2_Calculator::descriptor_stress_dervs)
.def("compute", &B2_Calculator::compute);
&DescriptorCalculator::descriptor_stress_dervs)
.def_readwrite("radial_hyps",
&DescriptorCalculator::radial_hyps)
.def_readwrite("cutoff_hyps",
&DescriptorCalculator::cutoff_hyps)
.def_readwrite("descriptor_settings",
&DescriptorCalculator::descriptor_settings);

py::class_<B2_Calculator, DescriptorCalculator>(m, "B2_Calculator")
.def(py::init<const std::string &, const std::string &,
const std::vector<double> &, const std::vector<double> &,
const std::vector<int> &, int>());

// Kernel functions
py::class_<Kernel>(m, "Kernel")
.def("env_env", &Kernel::env_env)
.def("env_struc", &Kernel::env_struc);

py::class_<TwoBodyKernel, Kernel>(m, "TwoBodyKernel")
.def(py::init<double, double, std::string, std::vector<double>>());

py::class_<ThreeBodyKernel, Kernel>(m, "ThreeBodyKernel")
.def(py::init<double, double, std::string, std::vector<double>>());

py::class_<DotProductKernel, Kernel>(m, "DotProductKernel")
.def(py::init<double, double, int>());

// Sparse GP
py::class_<SparseGP>(m, "SparseGP")
.def(py::init<std::vector<Kernel *>, double, double, double>())
.def_readwrite("Kuu", &SparseGP::Kuu)
.def_readwrite("Kuf", &SparseGP::Kuf)
.def_readwrite("Sigma", &SparseGP::Sigma)
.def_readwrite("noise", &SparseGP::noise)
.def_readwrite("noise_matrix", &SparseGP::noise_matrix)
.def_readwrite("kernels", &SparseGP::kernels)
.def_readwrite("alpha", &SparseGP::alpha)
.def_readwrite("y", &SparseGP::y)
.def_readwrite("hyperparameters", &SparseGP::hyperparameters)
.def("add_sparse_environment",
&SparseGP::add_sparse_environment)
.def("add_sparse_environment_serial",
&SparseGP::add_sparse_environment_serial)
.def("add_training_structure", &SparseGP::add_training_structure)
.def("add_training_structure_serial",
&SparseGP::add_training_structure_serial)
.def("update_alpha", &SparseGP::update_alpha)
.def("predict", &SparseGP::predict)
.def("predict_serial", &SparseGP::predict_serial);
}
19 changes: 12 additions & 7 deletions src/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ DescriptorCalculator::DescriptorCalculator(
const std::string & radial_basis, const std::string & cutoff_function,
const std::vector<double> & radial_hyps,
const std::vector<double> & cutoff_hyps,
const std::vector<int> & descriptor_settings){
const std::vector<int> & descriptor_settings,
int descriptor_index){

this->radial_basis = radial_basis;
this->cutoff_function = cutoff_function;
this->radial_hyps = radial_hyps;
this->cutoff_hyps = cutoff_hyps;
this->descriptor_settings = descriptor_settings;
this->descriptor_index = descriptor_index;

if (radial_basis == "chebyshev"){
this->radial_pointer = chebyshev;
Expand Down Expand Up @@ -81,6 +83,7 @@ for (int n1 = no_radial-1; n1 >= 0; n1 --){
single_bond_vals(n2_l);

// Store force derivatives.
// TODO: loop over many body indices, not entire neighbor list
for (int atom_index = 0; atom_index < neigh_size;
atom_index ++){
env_ind = env.neighbor_list[atom_index];
Expand Down Expand Up @@ -116,9 +119,10 @@ B1_Calculator :: B1_Calculator(const std::string & radial_basis,
const std::string & cutoff_function,
const std::vector<double> & radial_hyps,
const std::vector<double> & cutoff_hyps,
const std::vector<int> & descriptor_settings)
const std::vector<int> & descriptor_settings,
int descriptor_index)
: DescriptorCalculator(radial_basis, cutoff_function, radial_hyps,
cutoff_hyps, descriptor_settings){}
cutoff_hyps, descriptor_settings, descriptor_index){}

void B1_Calculator :: compute(const LocalEnvironment & env){
// Initialize single bond vectors.
Expand All @@ -136,7 +140,7 @@ void B1_Calculator :: compute(const LocalEnvironment & env){
// Compute single bond vector.
single_bond_sum_env(single_bond_vals, single_bond_force_dervs,
single_bond_stress_dervs, radial_pointer,
cutoff_pointer, env, env.cutoff, N,
cutoff_pointer, env, descriptor_index, N,
lmax, radial_hyps, cutoff_hyps);

// Set B1 values.
Expand All @@ -151,9 +155,10 @@ B2_Calculator :: B2_Calculator(const std::string & radial_basis,
const std::string & cutoff_function,
const std::vector<double> & radial_hyps,
const std::vector<double> & cutoff_hyps,
const std::vector<int> & descriptor_settings)
const std::vector<int> & descriptor_settings,
int descriptor_index)
: DescriptorCalculator(radial_basis, cutoff_function, radial_hyps,
cutoff_hyps, descriptor_settings){}
cutoff_hyps, descriptor_settings, descriptor_index){}

void B2_Calculator :: compute(const LocalEnvironment & env){
// Initialize single bond vectors.
Expand All @@ -176,7 +181,7 @@ void B2_Calculator :: compute(const LocalEnvironment & env){
// Compute single bond vector.
single_bond_sum_env(single_bond_vals, single_bond_force_dervs,
single_bond_stress_dervs, radial_pointer,
cutoff_pointer, env, env.cutoff, N,
cutoff_pointer, env, descriptor_index, N,
lmax, radial_hyps, cutoff_hyps);

// Initialize B2 vectors.
Expand Down
13 changes: 9 additions & 4 deletions src/descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <vector>
#include <Eigen/Dense>

// Descriptor calculator.
class LocalEnvironment;

// Descriptor calculator.
class DescriptorCalculator{
protected:
void (*radial_pointer)(double *, double *, double, int,
Expand All @@ -19,16 +19,19 @@ class DescriptorCalculator{
std::string radial_basis, cutoff_function;
std::vector<double> radial_hyps, cutoff_hyps;
std::vector<int> descriptor_settings;
int descriptor_index;

DescriptorCalculator();

DescriptorCalculator(
const std::string & radial_basis, const std::string & cutoff_function,
const std::vector<double> & radial_hyps,
const std::vector<double> & cutoff_hyps,
const std::vector<int> & descriptor_settings);
const std::vector<int> & descriptor_settings, int descriptor_index);

virtual void compute(const LocalEnvironment & env) = 0;

virtual ~DescriptorCalculator() = default;
};

void B2_descriptor(
Expand All @@ -49,7 +52,8 @@ class B1_Calculator : public DescriptorCalculator{
const std::string & cutoff_function,
const std::vector<double> & radial_hyps,
const std::vector<double> & cutoff_hyps,
const std::vector<int> & descriptor_settings);
const std::vector<int> & descriptor_settings,
int descriptor_index);

void compute(const LocalEnvironment & env);
};
Expand All @@ -63,7 +67,8 @@ class B2_Calculator : public DescriptorCalculator{
const std::string & cutoff_function,
const std::vector<double> & radial_hyps,
const std::vector<double> & cutoff_hyps,
const std::vector<int> & descriptor_settings);
const std::vector<int> & descriptor_settings,
int descriptor_index);

void compute(const LocalEnvironment & env);
};
Expand Down
Loading

0 comments on commit 6f2ee01

Please sign in to comment.