MADNESS  0.10.1
basis.h
Go to the documentation of this file.
1 /* This file is a part of Slymer, which is distributed under the Creative
2  Commons Attribution-NonCommercial 4.0 International Public License.
3 
4  (c) 2017 Stony Brook University. */
5 
6 /**
7  * \file Basis/basis.h
8  * \brief Basis function API and routines.
9  *
10  * Sets up the interface for general basis functions and related calculations.
11  */
12 
13 #ifndef __Basis_basis_h__
14 #define __Basis_basis_h__
15 
16 #include <array>
17 #include <functional>
18 #include <memory>
19 #include <stdexcept>
20 #include <vector>
21 
22 namespace slymer {
23 
24 /// Abstract base class for generic basis functions.
26 public:
27  virtual ~BasisFunction() = default;
28 
29  /**
30  * \brief Evaluate the basis function at the specified point.
31  *
32  * \param[in] x The point.
33  * \return The basis function evaluated at the point x.
34  */
35  virtual double operator() (const std::array<double, 3> &x) const = 0;
36 
37 };
38 
39 /// Type for a basis set (collection of basis functions).
40 using BasisSet = std::vector<std::reference_wrapper<BasisFunction>>;
41 
42 /**
43  * \brief Convert a generic basis set to basis functions with the specific
44  * type.
45  *
46  * \throw std::runtime_error if a basis function in the basis set is not
47  * of the specified type.
48  *
49  * \tparam T The intended type of each basis function.
50  * \param[in] bset The basis set.
51  * \return A vector with cast references to the basis functions.
52  */
53 template<typename T>
54 std::vector<std::reference_wrapper<const T>> cast_basis(const BasisSet &bset) {
55  const unsigned size = bset.size();
56  std::vector<std::reference_wrapper<const T>> casts;
57 
58  for(unsigned j = 0; j < size; ++j) {
59  const T *ptr = dynamic_cast<const T*>(&bset[j].get());
60  if(ptr == nullptr)
61  throw std::runtime_error("A basis function is not of the specified type.");
62  casts.emplace_back(std::cref(*ptr));
63  }
64 
65  return casts;
66 }
67 
68 } // namespace slymer
69 
70 #endif
Abstract base class for generic basis functions.
Definition: basis.h:25
virtual ~BasisFunction()=default
virtual double operator()(const std::array< double, 3 > &x) const =0
Evaluate the basis function at the specified point.
auto T(World &world, response_space &f) -> response_space
Definition: global_functions.cc:34
Definition: basis.h:22
std::vector< std::reference_wrapper< BasisFunction > > BasisSet
Type for a basis set (collection of basis functions).
Definition: basis.h:40
std::vector< std::reference_wrapper< const T > > cast_basis(const BasisSet &bset)
Convert a generic basis set to basis functions with the specific type.
Definition: basis.h:54