Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add one dim cubic spline with linear grid #3839

Merged
merged 7 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions src/Numerics/OneDimCubicSplineLinearGrid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2022 QMCPACK developers.
//
// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//
// File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//////////////////////////////////////////////////////////////////////////////////////


#ifndef QMCPLUSPLUS_LINEAR_GRID_CUBIC_SPLINE_H
#define QMCPLUSPLUS_LINEAR_GRID_CUBIC_SPLINE_H

#include "OneDimCubicSpline.h"
#include "OneDimCubicSpline.h"
#include "OMPTarget/OffloadAlignedAllocators.hpp"

namespace qmcplusplus
{
/** combined OneDimCubicSpline and LinearGrid
* OneDimCubicSpline contains OneDimGridBase pointer and calls its virtual function members. This doesn't work well on a GPU.
* Since the use case is OneDimCubicSpline with LinearGrid. We fuse both classes and avoid any virtual functions.
* There are two splint functions. The one with one paramemter r is intended for testing or being called on the CPU.
* The static one with many parameters is intended to be used(inlined) inside a GPU kernel.
*/
template<class T>
class OneDimCubicSplineLinearGrid
{
public:
OneDimCubicSplineLinearGrid(const OneDimCubicSpline<T>& cublis_spliner)
{
auto& grid = dynamic_cast<const LinearGrid<T>&>(cublis_spliner.grid());
r_min_ = grid.rmin();
r_max_ = grid.rmax();
delta_inv_ = grid.DeltaInv;

const size_t num_points = grid.size();
X_.resize(num_points);
for (size_t i = 0; i < num_points; i++)
X_[i] = *(grid.data() + i);

first_deriv_ = cublis_spliner.first_deriv;
const_value_ = cublis_spliner.ConstValue;
m_Y_.resize(num_points);
m_Y2_.resize(num_points);
for (size_t i = 0; i < num_points; i++)
{
m_Y_[i] = cublis_spliner.m_Y[i];
m_Y2_[i] = cublis_spliner.m_Y2[i];
}
X_.updateTo();
m_Y_.updateTo();
m_Y2_.updateTo();
}

/** compute the function value at r
*/
T splint(T r) const
{
return splint(r_min_, r_max_, X_.data(), delta_inv_, m_Y_.data(), m_Y2_.data(), first_deriv_, const_value_, r);
}

/** compute the function value at r.
* Need to pass in all the parameters.
*/
static T splint(T r_min,
T r_max,
const T* X,
T delta_inv,
const T* m_Y,
const T* m_Y2,
T first_deriv,
T const_value,
T r)
{
if (r < r_min)
{
return m_Y[0] + first_deriv * (r - r_min);
}
else if (r >= r_max)
{
return const_value;
}

const size_t loc = std::floor((r - r_min) * delta_inv);
const T dist = r - X[loc];
const T delta = X[loc + 1] - X[loc];
CubicSplineEvaluator<T> eval(dist, delta);
return eval.cubicInterpolate(m_Y[loc], m_Y[loc + 1], m_Y2[loc], m_Y2[loc + 1]);
}

const auto& get_m_Y() const { return m_Y_; }
const auto& get_m_Y2() const { return m_Y2_; }
T get_first_deriv() const { return first_deriv_; }
T get_const_value() const { return const_value_; }

T get_r_min() const { return r_min_; }
T get_r_max() const { return r_max_; }
const auto& get_X() const { return X_; }
double get_delta_inv() const { return delta_inv_; }

private:
// spline related
/// data for the function on the grid
Vector<T, OffloadAllocator<T>> m_Y_;
/// data for the function on the grid
Vector<T, OffloadAllocator<T>> m_Y2_;
/// first derivative for handling r < r_min_
T first_deriv_;
/// const value for handling r > r_max_
T const_value_;

// grid related info
/// use spline above r_min_. If below, use first deriv extrapolation
T r_min_;
/// use spline below r_min_. If above, use const value
T r_max_;
/// the location of grid points
Vector<T, OffloadAllocator<T>> X_;
/// 1/grid space
double delta_inv_;
};

} // namespace qmcplusplus
#endif
1 change: 1 addition & 0 deletions src/Numerics/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(UTEST_SRCS
test_soa_cartesian_tensor.cpp
test_transform.cpp
test_min_oned.cpp
test_OneDimCubicSplineLinearGrid.cpp
test_one_dim_cubic_spline.cpp)

# Run gen_gto.py to create these files. They may take a long time to compile.
Expand Down
60 changes: 60 additions & 0 deletions src/Numerics/tests/test_OneDimCubicSplineLinearGrid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//////////////////////////////////////////////////////////////////////////////////////
// This file is distributed under the University of Illinois/NCSA Open Source License.
// See LICENSE file in top directory for details.
//
// Copyright (c) 2022 QMCPACK developers.
//
// File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//
// File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
//////////////////////////////////////////////////////////////////////////////////////

#include "catch.hpp"
#include "Numerics/OneDimCubicSplineLinearGrid.h"

namespace qmcplusplus
{

// Copy and modified from one_dim_cubic_spline_1
TEST_CASE("test oneDimCubicSplineLinearGrid", "[numerics]")
{
const int n = 3;
std::vector<double> yvals = {1.0, 2.0, 1.5};

auto grid = std::make_unique<LinearGrid<double>>();
grid->set(0.5, 2.0, n);

OneDimCubicSpline<double> cubic_spline(std::move(grid), yvals);

int imin = 0;
int imax = 3 - 1;

double yp0 = 1.0;
double ypn = 2.0;

cubic_spline.spline(imin, yp0, imax, ypn);

OneDimCubicSplineLinearGrid linear_grid_cubic_spline(cubic_spline);

std::vector<double> check_xvals = {0.0, 0.39999999998, 0.79999999996, 1.19999999994, 1.59999999992, 1.9999999999, 2.5};
std::vector<double> check_yvals;

for (int i = 0; i < check_xvals.size(); i++)
{
double r = check_xvals[i];
double val = cubic_spline.splint(r);
check_yvals.push_back(val);

//std::cout << i << " r = " << r << " val = " << val << " " << check_yvals[i] << std::endl;
}

CHECK(check_yvals[0] == Approx(0.5));
CHECK(check_yvals[1] == Approx(0.9));
CHECK(check_yvals[2] == Approx(1.4779999999));
CHECK(check_yvals[3] == Approx(2.0012592592));
CHECK(check_yvals[4] == Approx(1.575851852));
CHECK(check_yvals[5] == Approx(1.5));
CHECK(check_yvals[6] == Approx(1.5));
}

} // namespace qmcplusplus
7 changes: 5 additions & 2 deletions src/QMCHamiltonians/CoulombPBCAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
//////////////////////////////////////////////////////////////////////////////////////


#include "EwaldRef.h"
#include "CoulombPBCAA.h"
#include <numeric>
#include "EwaldRef.h"
#include "Particle/DistanceTable.h"
#include "Utilities/ProgressReportEngine.h"
#include <numeric>
#include "Numerics/OneDimCubicSplineLinearGrid.h"

namespace qmcplusplus
{
Expand Down Expand Up @@ -302,6 +303,8 @@ void CoulombPBCAA::initBreakup(ParticleSet& P)
if (rVs == nullptr)
rVs = LRCoulombSingleton::createSpline4RbyVs(AA.get(), myRcut);

rVs_offload = std::make_shared<const OffloadSpline>(*rVs);

if (ComputeForces)
{
dAA = LRCoulombSingleton::getDerivHandler(P);
Expand Down
7 changes: 7 additions & 0 deletions src/QMCHamiltonians/CoulombPBCAA.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

namespace qmcplusplus
{

template<class T>
class OneDimCubicSplineLinearGrid;

/** @ingroup hamiltonian
*\brief Calculates the AA Coulomb potential using PBCs
*
Expand All @@ -35,11 +39,14 @@ struct CoulombPBCAA : public OperatorBase, public ForceBase
using GridType = LRCoulombSingleton::GridType;
using RadFunctorType = LRCoulombSingleton::RadFunctorType;
using mRealType = LRHandlerType::mRealType;
using OffloadSpline = OneDimCubicSplineLinearGrid<LRCoulombSingleton::pRealType>;

/// energy-optimized long range handle. Should be const LRHandlerType eventually
std::shared_ptr<LRHandlerType> AA;
/// energy-optimized short range pair potential
std::shared_ptr<const RadFunctorType> rVs;
/// the same as rVs but can be used inside OpenMP offload regions
std::shared_ptr<const OffloadSpline> rVs_offload;
/// force-optimized long range handle
std::shared_ptr<const LRHandlerType> dAA;
/// force-optimized short range pair potential
Expand Down