votca 2026-dev
Loading...
Searching...
No Matches
xtpapplication.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team
3 * (http://www.votca.org)
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License")
6 *
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20// Third party includes
21#include <boost/format.hpp>
22
23// VOTCA includes
24#include <boost/format.hpp>
25#include <ostream>
26#include <stdexcept>
27#include <votca/tools/globals.h>
30
31// Local VOTCA includes
34#include "votca/xtp/citations.h"
36#include "votca/xtp/version.h"
38
39namespace votca {
40namespace xtp {
43
52
53 namespace propt = boost::program_options;
55 "options,o", propt::value<std::string>(),
56 std::string(" " + CalculatorType() + " user options.").c_str());
57
58 AddProgramOptions()("nthreads,t", propt::value<Index>()->default_value(1),
59 " number of threads to create");
61 "memory,M", propt::value<double>()->default_value(0.0),
62 " memory in GB for the whole process, used to choose caches and batch "
63 "sizes (shared equally by concurrent jobs; 0: built-in defaults)");
64
66 "execute,e", propt::value<std::string>(),
67 std::string("Name of " + CalculatorType() + " to run").c_str());
69 "list,l",
70 std::string("Lists all available " + CalculatorType() + "s").c_str());
72 "description,d", propt::value<std::string>(),
73 std::string("Short description of a " + CalculatorType() + "s").c_str());
74
76 "cite", propt::value<std::string>(),
77 std::string("References to cite for a " + CalculatorType() +
78 " (the end of each run lists those actually used)")
79 .c_str());
80
82 "cmdoptions,c", propt::value<std::vector<std::string>>()->multitoken(),
83 "Modify options via command line by e.g. '-c xmltag.subtag=value'. "
84 "Use whitespace to separate multiple options");
85
87 "printoptions,p", propt::value<std::string>(),
88 std::string("Prints xml options of a " + CalculatorType()).c_str());
89
91}
92
94 MemoryBudget::Set(OptionsMap()["memory"].as<double>());
95
96 if (OptionsMap().count("list")) {
97 std::cout << "Available XTP" + CalculatorType() + "s: \n";
98 for (const auto &name : CalculatorNames()) {
99 PrintShortHelp(std::cout, name);
100 }
102 return true;
103 }
104
105 if (OptionsMap().count("description")) {
106 std::string calcname = OptionsMap()["description"].as<std::string>();
107 if (CalcExists(calcname)) {
108 PrintLongHelp(std::cout, calcname,
110 } else {
111 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
112 }
113
115 return true;
116 }
117 if (OptionsMap().count("cite")) {
118 std::string calcname = OptionsMap()["cite"].as<std::string>();
119 if (CalcExists(calcname)) {
120 std::cout << citations::CalculatorReport(calcname);
121 } else {
122 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
123 }
125 return true;
126 }
127 if (OptionsMap().count("printoptions")) {
128 std::string calcname = OptionsMap()["printoptions"].as<std::string>();
129 if (CalcExists(calcname)) {
130 std::string optionsFile;
131 if (OptionsMap().count("options")) {
132 optionsFile = OptionsMap()["options"].as<std::string>();
133 } else {
134 throw std::runtime_error(
135 "Please specify a --options file to print options for calculator "
136 "to.");
137 }
138 std::ofstream out;
139 out.open(optionsFile);
140 std::cout << "Writing options for calculator " << calcname << " to "
141 << optionsFile << std::endl;
142 out << "<options>\n";
144 out << "</options>\n" << std::endl;
145 } else {
146 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
147 }
148
150 return true;
151 }
152
153 CheckRequired("execute", "Nothing to do here: Abort.");
154 std::string calcname = OptionsMap()["execute"].as<std::string>();
155
156 if (CalcExists(calcname)) {
157 CreateCalculator(calcname);
158 } else {
159 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
161 }
162
164
165 tools::Property useroptions;
166 if (OptionsMap().count("options")) {
167 std::string optionsFile = OptionsMap()["options"].as<std::string>();
168 useroptions.LoadFromXML(optionsFile);
169 } else {
170 // Empty user options
171 tools::Property &opts = useroptions.add("options", "");
172 opts.add(calcname, "");
173 }
174
175 if (OptionsMap().count("cmdoptions")) {
176 for (const std::string &opt :
177 OptionsMap()["cmdoptions"].as<std::vector<std::string>>()) {
178 std::vector<std::string> entries = tools::Tokenizer(opt, "=").ToVector();
179 if (entries.size() != 2) {
180 throw std::runtime_error(opt + " is not well formated!");
181 } else {
182 useroptions.getOradd("options." + calcname + "." + entries[0]).value() =
183 entries[1];
184 }
185 }
186 }
187
188 tools::OptionsHandler handler(tools::GetVotcaShare() + "/xtp/xml/");
189
190 // This is a terrible low level hack, but I do not want to refactor all of
191 // xtp_calculators now as well
192 if (calcname == "qmmm") {
193 handler.setAdditionalChoices({"jobfile"});
194 }
195 options_ = handler.ProcessUserInput(useroptions, calcname)
196 .get("options." + calcname);
197 return true;
198}
199
201
202 std::string name = ProgramName();
203 if (VersionString() != "") {
204 name = name + ", version " + VersionString();
205 }
207
209 citations::Cite("Baumeier:2024");
210 if (OptionsMap().count("execute")) {
211 const std::string calcname = OptionsMap()["execute"].as<std::string>();
212 for (const std::string &key : citations::ForCalculator(calcname).keys) {
213 citations::Cite(key);
214 }
215 }
216
217 execute();
218
219 std::cout << "\n" << citations::Report() << std::flush;
220}
221
222void XtpApplication::ShowHelpText(std::ostream &out) {
223 std::string name = ProgramName();
224 if (VersionString() != "") {
225 name = name + ", version " + VersionString();
226 }
228 HelpText(out);
229 out << "\n\n" << VisibleOptions() << std::endl;
230}
231
232void XtpApplication::PrintShortHelp(std::ostream &out,
233 const std::string &calculator_name) const {
234 std::string xmlfile =
235 tools::GetVotcaShare() + "/xtp/xml/" + calculator_name + ".xml";
236
237 tools::Property options;
238 options.LoadFromXML(xmlfile);
239 std::string help_string = options.get("options." + calculator_name)
240 .getAttribute<std::string>("help");
241 boost::format format("%|3t|%1% %|20t|%2% \n");
242 out << format % calculator_name % help_string;
243}
244
246 std::ostream &out, const std::string &calculator_name,
248 tools::OptionsHandler handler(tools::GetVotcaShare() + "/xtp/xml/");
249 tools::Property options = handler.CalculatorOptions(calculator_name);
250 tools::PropertyIOManipulator iom(format, 2, "");
251 out << iom << options;
252}
253
254} // namespace xtp
255} // namespace votca
boost::program_options::variables_map & OptionsMap()
get available program options & descriptions
virtual void HelpText(std::ostream &out)=0
help text of application without version information
boost::program_options::options_description & VisibleOptions()
filters out the Hidden group from the options descriptions
virtual std::string ProgramName()=0
program name
virtual std::string VersionString()
version string of application
Definition application.h:55
void StopExecution()
call StopExecution after EvaluateOptions
boost::program_options::options_description_easy_init AddProgramOptions(const std::string &group="")
add option for command line
void CheckRequired(const std::string &option_name, const std::string &error_msg="")
Check weather required option is set.
Property ProcessUserInput(const Property &user_input, const std::string &calcname) const
Load the default options and merge them with the user input.
Property CalculatorOptions(const std::string &calcname) const
Resolve links and return all the options of a calculator.
void setAdditionalChoices(std::vector< std::string > choices)
Allows to bypass the choice evaluation for a specific calculator.
Manipulates the format state of the output stream.
class to manage program options with xml serialization functionality
Definition property.h:55
Property & add(const std::string &key, const std::string &value)
add a new property to structure
Definition property.cc:108
Property & get(const std::string &key)
get existing property
Definition property.cc:79
std::string & value()
reference to value of property
Definition property.h:153
Property & getOradd(const std::string &key)
adds new or gets existing property
Definition property.cc:156
T getAttribute(const std::string &attribute) const
return attribute as type
Definition property.h:319
void LoadFromXML(std::string filename)
Definition property.cc:238
break string into words
Definition tokenizer.h:72
std::vector< T > ToVector()
store all words in a vector of type T, does type conversion.
Definition tokenizer.h:109
static void Set(double total_gb, Index concurrent_jobs=1)
total_gb <= 0: no budget given
virtual void CreateCalculator(const std::string &name)=0
virtual std::vector< std::string > CalculatorNames() const =0
virtual void execute()=0
void PrintLongHelp(std::ostream &out, const std::string &calculator_name, tools::PropertyIOManipulator::Type format) const
void Initialize() final
Adds program options to the executable.
bool CalcExists(const std::string &name) const
virtual std::string CalculatorType() const =0
votca::tools::Property options_
void ShowHelpText(std::ostream &out) final
void Run() final
Main body of application.
virtual void EvaluateSpecificOptions()=0
virtual void AddCommandLineOptions()=0
void PrintShortHelp(std::ostream &out, const std::string &calculator_name) const
bool EvaluateOptions() final
Process command line options.
std::string GetVotcaShare()
Retrieves VOTCASHARE environment variable. Throws std::runtime_error if not set.
Definition globals.cc:27
CalculatorEntry ForCalculator(const std::string &name)
the references of a calculator or tool (empty for unknown names)
Definition citations.cc:324
std::string CalculatorReport(const std::string &name)
the text printed by –cite
Definition citations.cc:339
void Cite(const std::string &key)
marks a reference as used (thread safe; unknown keys are printed as such)
Definition citations.cc:289
std::string Report()
the block printed at the end of a run (empty if nothing was cited)
Definition citations.cc:307
Charge transport classes.
Definition ERIs.h:28
void HelpTextHeader(const std::string &tool_name)
Definition version.cc:34
Provides a means for comparing floating point numbers.
Definition basebead.h:33