JDFTx  1.2.0
EnergyComponents.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_ENERGYCOMPONENTS_H
21 #define JDFTX_CORE_ENERGYCOMPONENTS_H
22 
25 
26 #include <map>
27 #include <string>
28 #include <core/string.h>
29 #include <cstdio>
30 
36 class EnergyComponents : public std::map<string,double>
37 {
38 public:
40  double& operator[](const char* key) { return std::map<string,double>::operator[](string(key)); }
42  double& operator[](const string& key) { return std::map<string,double>::operator[](key); }
43 
44  //const versions of above
46  double operator[](const char* key) const { auto iter = find(string(key)); return iter==end() ? 0. : iter->second; }
48  double operator[](const string& key) const { auto iter = find(key); return iter==end() ? 0. : iter->second; }
49 
51  void operator=(const double& value)
52  { clear(); //remove all named components
53  (*this)["default"] = value; //set the value to a default channel
54  }
55 
57  void operator+=(const double& value)
58  { (*this)["default"] += value; //accumulate to the default channel
59  }
60 
62  // Allows using templated CG without modification!
63  operator double() const
64  { double ret=0.0;
65  for(const_iterator i=begin(); i!=end(); i++)
66  { ret += i->second;
67  }
68  return ret;
69  }
70 
75  void print(FILE* fp, bool nonzeroOnly, const char* format="\t%s = %le\n") const
76  {
77  std::map<string,double> reducedComponents;
78  for(auto entry: *this)
79  if((not nonzeroOnly) or (entry.second != 0))
80  reducedComponents[entry.first.substr(0, entry.first.find_last_not_of("0123456789-")+1)] += entry.second;
81  for(auto entry: reducedComponents)
82  fprintf(fp, format, entry.first.c_str(), entry.second);
83  }
84 };
85 
86 #endif //JDFTX_CORE_ENERGYCOMPONENTS_H
double & operator[](const char *key)
Access by C-style string (need this to prevent ambiguous overload)
Definition: EnergyComponents.h:40
STL strings and streams with case insensitive comparison.
double operator[](const string &key) const
Expose base-class function hidden by the C-style string version above.
Definition: EnergyComponents.h:48
void print(FILE *fp, bool nonzeroOnly, const char *format="\t%s = %le\n") const
Definition: EnergyComponents.h:75
__hostanddev__ double value(const double *coeff, double x)
Compute value of quintic spline. Warning: x is not range-checked.
double & operator[](const string &key)
Expose base-class function hidden by the C-style string version above.
Definition: EnergyComponents.h:42
void operator=(const double &value)
Set to a simple scalar.
Definition: EnergyComponents.h:51
double operator[](const char *key) const
Access by C-style string (need this to prevent ambiguous overload)
Definition: EnergyComponents.h:46
void operator+=(const double &value)
Accumulate a simple scalar.
Definition: EnergyComponents.h:57
Definition: EnergyComponents.h:36