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
35#include "votca/xtp/version.h"
37
38namespace votca {
39namespace xtp {
42
51
52 namespace propt = boost::program_options;
54 "options,o", propt::value<std::string>(),
55 std::string(" " + CalculatorType() + " user options.").c_str());
56
57 AddProgramOptions()("nthreads,t", propt::value<Index>()->default_value(1),
58 " number of threads to create");
60 "memory,M", propt::value<double>()->default_value(0.0),
61 " memory in GB for the whole process, used to choose caches and batch "
62 "sizes (shared equally by concurrent jobs; 0: built-in defaults)");
63
65 "execute,e", propt::value<std::string>(),
66 std::string("Name of " + CalculatorType() + " to run").c_str());
68 "list,l",
69 std::string("Lists all available " + CalculatorType() + "s").c_str());
71 "description,d", propt::value<std::string>(),
72 std::string("Short description of a " + CalculatorType() + "s").c_str());
73
75 "cmdoptions,c", propt::value<std::vector<std::string>>()->multitoken(),
76 "Modify options via command line by e.g. '-c xmltag.subtag=value'. "
77 "Use whitespace to separate multiple options");
78
80 "printoptions,p", propt::value<std::string>(),
81 std::string("Prints xml options of a " + CalculatorType()).c_str());
82
84}
85
87 MemoryBudget::Set(OptionsMap()["memory"].as<double>());
88
89 if (OptionsMap().count("list")) {
90 std::cout << "Available XTP" + CalculatorType() + "s: \n";
91 for (const auto &name : CalculatorNames()) {
92 PrintShortHelp(std::cout, name);
93 }
95 return true;
96 }
97
98 if (OptionsMap().count("description")) {
99 std::string calcname = OptionsMap()["description"].as<std::string>();
100 if (CalcExists(calcname)) {
101 PrintLongHelp(std::cout, calcname,
103 } else {
104 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
105 }
106
108 return true;
109 }
110 if (OptionsMap().count("printoptions")) {
111 std::string calcname = OptionsMap()["printoptions"].as<std::string>();
112 if (CalcExists(calcname)) {
113 std::string optionsFile;
114 if (OptionsMap().count("options")) {
115 optionsFile = OptionsMap()["options"].as<std::string>();
116 } else {
117 throw std::runtime_error(
118 "Please specify a --options file to print options for calculator "
119 "to.");
120 }
121 std::ofstream out;
122 out.open(optionsFile);
123 std::cout << "Writing options for calculator " << calcname << " to "
124 << optionsFile << std::endl;
125 out << "<options>\n";
127 out << "</options>\n" << std::endl;
128 } else {
129 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
130 }
131
133 return true;
134 }
135
136 CheckRequired("execute", "Nothing to do here: Abort.");
137 std::string calcname = OptionsMap()["execute"].as<std::string>();
138
139 if (CalcExists(calcname)) {
140 CreateCalculator(calcname);
141 } else {
142 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
144 }
145
147
148 tools::Property useroptions;
149 if (OptionsMap().count("options")) {
150 std::string optionsFile = OptionsMap()["options"].as<std::string>();
151 useroptions.LoadFromXML(optionsFile);
152 } else {
153 // Empty user options
154 tools::Property &opts = useroptions.add("options", "");
155 opts.add(calcname, "");
156 }
157
158 if (OptionsMap().count("cmdoptions")) {
159 for (const std::string &opt :
160 OptionsMap()["cmdoptions"].as<std::vector<std::string>>()) {
161 std::vector<std::string> entries = tools::Tokenizer(opt, "=").ToVector();
162 if (entries.size() != 2) {
163 throw std::runtime_error(opt + " is not well formated!");
164 } else {
165 useroptions.getOradd("options." + calcname + "." + entries[0]).value() =
166 entries[1];
167 }
168 }
169 }
170
171 tools::OptionsHandler handler(tools::GetVotcaShare() + "/xtp/xml/");
172
173 // This is a terrible low level hack, but I do not want to refactor all of
174 // xtp_calculators now as well
175 if (calcname == "qmmm") {
176 handler.setAdditionalChoices({"jobfile"});
177 }
178 options_ = handler.ProcessUserInput(useroptions, calcname)
179 .get("options." + calcname);
180 return true;
181}
182
184
185 std::string name = ProgramName();
186 if (VersionString() != "") {
187 name = name + ", version " + VersionString();
188 }
190
191 execute();
192}
193
194void XtpApplication::ShowHelpText(std::ostream &out) {
195 std::string name = ProgramName();
196 if (VersionString() != "") {
197 name = name + ", version " + VersionString();
198 }
200 HelpText(out);
201 out << "\n\n" << VisibleOptions() << std::endl;
202}
203
204void XtpApplication::PrintShortHelp(std::ostream &out,
205 const std::string &calculator_name) const {
206 std::string xmlfile =
207 tools::GetVotcaShare() + "/xtp/xml/" + calculator_name + ".xml";
208
209 tools::Property options;
210 options.LoadFromXML(xmlfile);
211 std::string help_string = options.get("options." + calculator_name)
212 .getAttribute<std::string>("help");
213 boost::format format("%|3t|%1% %|20t|%2% \n");
214 out << format % calculator_name % help_string;
215}
216
218 std::ostream &out, const std::string &calculator_name,
220 tools::OptionsHandler handler(tools::GetVotcaShare() + "/xtp/xml/");
221 tools::Property options = handler.CalculatorOptions(calculator_name);
222 tools::PropertyIOManipulator iom(format, 2, "");
223 out << iom << options;
224}
225
226} // namespace xtp
227} // 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
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