SIRIUS 7.5.0
Electronic structure library and applications
env.hpp
Go to the documentation of this file.
1// Copyright (c) 2013-2018 Anton Kozhevnikov, Thomas Schulthess
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without modification, are permitted provided that
5// the following conditions are met:
6//
7// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
8// following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10// and the following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
13// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
14// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
15// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
17// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
18// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20/** \file env.hpp
21 *
22 * \brief Get the environment variables
23 */
24
25#ifndef __ENV_HPP__
26#define __ENV_HPP__
27
28#include <cstdlib>
29#include <string>
30#include <algorithm>
31#include <map>
32#include <memory>
33#include <sstream>
34
35namespace sirius {
36
37/// Get environment variables.
38namespace env {
39
40/// Check for environment variable and return a pointer to a stored value if found or a null-pointer if not.
41template <typename T>
42inline T const* get_value_ptr(std::string const& name__)
43{
44 static std::map<std::string, std::unique_ptr<T>> map_name;
45 if (map_name.count(name__) == 0) {
46 /* first time the function is called */
47 const char* raw_str = std::getenv(name__.c_str());
48 if (raw_str == NULL) {
49 map_name[name__] = nullptr;
50 } else {
51 map_name[name__] = std::make_unique<T>();
52 std::istringstream(std::string(raw_str)) >> (*map_name[name__]);
53 }
54 }
55 return map_name[name__].get();
56}
57
58inline bool
59print_performance()
60{
61 auto val = get_value_ptr<int>("SIRIUS_PRINT_PERFORMANCE");
62 return val && *val;
63}
64
65inline bool
66print_checksum()
67{
68 auto val = get_value_ptr<int>("SIRIUS_PRINT_CHECKSUM");
69 return val && *val;
70}
71
72inline bool
73print_hash()
74{
75 auto val = get_value_ptr<int>("SIRIUS_PRINT_HASH");
76 return val && *val;
77}
78
79inline bool
80print_mpi_layout()
81{
82 auto val = get_value_ptr<int>("SIRIUS_PRINT_MPI_LAYOUT");
83 return val && *val;
84}
85
86inline bool
87print_memory_usage()
88{
89 auto val = get_value_ptr<int>("SIRIUS_PRINT_MEMORY_USAGE");
90 return val && *val;
91}
92
93inline int
94print_timing()
95{
96 auto val = get_value_ptr<int>("SIRIUS_PRINT_TIMING");
97 if (val) {
98 return *val;
99 } else {
100 return 0;
101 }
102}
103
104inline std::string
105save_config()
106{
107 auto val = get_value_ptr<std::string>("SIRIUS_SAVE_CONFIG");
108 if (val) {
109 return *val;
110 } else {
111 return "";
112 }
113}
114
115inline std::string
116config_file()
117{
118 auto val = get_value_ptr<std::string>("SIRIUS_CONFIG");
119 if (val) {
120 return *val;
121 } else {
122 return "";
123 }
124}
125
126inline std::string
127get_ev_solver()
128{
129 auto val = get_value_ptr<std::string>("SIRIUS_EV_SOLVER");
130 if (val) {
131 return *val;
132 } else {
133 return "";
134 }
135}
136
137inline int
138get_verbosity()
139{
140 auto verb_lvl = env::get_value_ptr<int>("SIRIUS_VERBOSITY");
141 if (verb_lvl) {
142 return *verb_lvl;
143 } else {
144 return 0;
145 }
146}
147
148inline bool
149check_scf_density()
150{
151 auto val = get_value_ptr<int>("SIRIUS_CHECK_SCF_DENSITY");
152 return val && *val;
153}
154
155} // namespace env
156
157} // namespace sirius
158#endif
T const * get_value_ptr(std::string const &name__)
Check for environment variable and return a pointer to a stored value if found or a null-pointer if n...
Definition: env.hpp:42
Namespace of the SIRIUS library.
Definition: sirius.f90:5