QMCPACK
SpeciesSet.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Raymond Clay III, j.k.rofling@gmail.com, Lawrence Livermore National Laboratory
10 //
11 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
12 //////////////////////////////////////////////////////////////////////////////////////
13 
14 
15 #ifndef QMCPLUSPLUS_SPECIES_SET_H
16 #define QMCPLUSPLUS_SPECIES_SET_H
17 
18 #include <string>
19 #include <vector>
20 
21 namespace qmcplusplus
22 {
23 /** \class SpeciesSet
24  * \brief Custom container for set of attributes for a set of species.
25  *
26  * A confusingly equivalent of std::map<std::string, <std::map<std::string, Scalar>>
27  * implemented as two sets of key vectors and a single vector of Scalars. It leaks its implementation
28  * details i.e. the indexing to the vectors. Reduces readability and increases semantic load.
29  * Looks like premature optimization.
30  *
31  * \todo prove this helps overall performance, if not remove it. Else document it and it's use
32 */
34 {
35 public:
36  using Scalar_t = double;
37  using SpeciesAttrib_t = std::vector<Scalar_t>;
38  using AttribList_t = std::vector<SpeciesAttrib_t>;
39 
40  //! The number of species
41  unsigned TotalNum = 0;
42 
43  //! Species name list
44  std::vector<std::string> speciesName;
45 
46  //! attribute name list
47  std::vector<std::string> attribName;
48 
49  //! List of species attributes
51 
52  ///return the number of species
53  inline int size() const { return TotalNum; }
54  ///return the number of species
55  inline int getTotalNum() const { return TotalNum; }
56  ///set the number of species
57  inline void setTotalNum(const unsigned n) { TotalNum = n; }
58  //! return the number of attributes in our list
59  inline int numAttributes() const { return d_attrib.size(); }
60 
61  /**
62  * @param aname Unique name of the species be added.
63  * @return the index of the species
64  * @brief When a name species does not exist, add a new species
65  */
66  int addSpecies(const std::string& aname);
67 
68  /** for a new attribute, allocate the data, !More often used to get the index of a species
69  * @param aname a unique name of an attribute
70  * @return the index of a new attribute
71  */
72  int addAttribute(const std::string& aname);
73 
74  /**
75  * @param aname Unique name of the species to be looked up.
76  * @return the index of the species
77  * @brief When a name species does not exist, return attribute.size()
78  */
79  int getAttribute(const std::string& aname) const;
80 
81  /** Check for attribute presence
82  * This replaces code that gets numAttributes then tries to addAttribute for
83  * a particular name and compares the numAttributes and index of the new Attribute
84  * @param aname Unique name of the species to be looked up.
85  * @return is an attribute of that name present
86  */
87  bool hasAttribute(const std::string& aname) const;
88 
89  /**
90  * @param i attribute index
91  * @param j species index
92  * @return the value of i-th attribute for the j-th species
93  */
94  inline double operator()(int i, int j) const { return d_attrib[i][j]; }
95 
96  /**
97  * assignment operator
98  * @param i attribute index
99  * @param j species index
100  * @return the value of i-th attribute for the j-th species
101  */
102  inline double& operator()(int i, int j) { return d_attrib[i][j]; }
103 
104  /**
105  * @param m the number of species to be added
106  */
107  void create(unsigned m);
108 
109  /**
110  * @param name a name of species
111  * @return an ID for the species with name.
112  * @brief if the input species is not found, add a new species
113  */
114  inline int findSpecies(const std::string& name) const
115  {
116  int i = 0;
117  while (i < speciesName.size())
118  {
119  if (speciesName[i] == name)
120  return i;
121  i++;
122  }
123  return i;
124  }
125 
126  /** almost all code ignores this and just uses addAttribute for the same purpose.
127  */
128  inline int findAttribute(const std::string& name) const { return findIndex(name, attribName); }
129 
130  inline int findIndex(const std::string& name, const std::vector<std::string>& alist) const
131  {
132  int i = 0;
133  while (i < alist.size())
134  {
135  if (alist[i] == name)
136  return i;
137  i++;
138  }
139  return -1;
140  }
141 
142  inline const std::string& getSpeciesName(int index) const { return speciesName[index]; }
143 };
144 } // namespace qmcplusplus
145 #endif
int numAttributes() const
return the number of attributes in our list
Definition: SpeciesSet.h:59
int findIndex(const std::string &name, const std::vector< std::string > &alist) const
Definition: SpeciesSet.h:130
std::vector< std::string > attribName
attribute name list
Definition: SpeciesSet.h:47
int addSpecies(const std::string &aname)
When a name species does not exist, add a new species.
Definition: SpeciesSet.cpp:33
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
bool hasAttribute(const std::string &aname) const
Check for attribute presence This replaces code that gets numAttributes then tries to addAttribute fo...
Definition: SpeciesSet.cpp:70
double & operator()(int i, int j)
assignment operator
Definition: SpeciesSet.h:102
void create(unsigned m)
Definition: SpeciesSet.cpp:20
int addAttribute(const std::string &aname)
for a new attribute, allocate the data, !More often used to get the index of a species ...
Definition: SpeciesSet.cpp:45
int size() const
return the number of species
Definition: SpeciesSet.h:53
int getTotalNum() const
return the number of species
Definition: SpeciesSet.h:55
AttribList_t d_attrib
List of species attributes.
Definition: SpeciesSet.h:50
const std::string & getSpeciesName(int index) const
Definition: SpeciesSet.h:142
void setTotalNum(const unsigned n)
set the number of species
Definition: SpeciesSet.h:57
std::vector< std::string > speciesName
Species name list.
Definition: SpeciesSet.h:44
int getAttribute(const std::string &aname) const
When a name species does not exist, return attribute.size()
Definition: SpeciesSet.cpp:60
double operator()(int i, int j) const
Definition: SpeciesSet.h:94
Custom container for set of attributes for a set of species.
Definition: SpeciesSet.h:33
std::vector< SpeciesAttrib_t > AttribList_t
Definition: SpeciesSet.h:38
int findSpecies(const std::string &name) const
if the input species is not found, add a new species
Definition: SpeciesSet.h:114
int findAttribute(const std::string &name) const
almost all code ignores this and just uses addAttribute for the same purpose.
Definition: SpeciesSet.h:128
std::vector< Scalar_t > SpeciesAttrib_t
Definition: SpeciesSet.h:37
unsigned TotalNum
The number of species.
Definition: SpeciesSet.h:41