votca 2024-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/filesystem.hpp>
25#include <boost/format.hpp>
26#include <ostream>
27#include <stdexcept>
28#include <votca/tools/globals.h>
31
32// Local VOTCA includes
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");
60
62 "execute,e", propt::value<std::string>(),
63 std::string("Name of " + CalculatorType() + " to run").c_str());
65 "list,l",
66 std::string("Lists all available " + CalculatorType() + "s").c_str());
68 "description,d", propt::value<std::string>(),
69 std::string("Short description of a " + CalculatorType() + "s").c_str());
70
72 "cmdoptions,c", propt::value<std::vector<std::string>>()->multitoken(),
73 "Modify options via command line by e.g. '-c xmltag.subtag=value'. "
74 "Use whitespace to separate multiple options");
75
77 "printoptions,p", propt::value<std::string>(),
78 std::string("Prints xml options of a " + CalculatorType()).c_str());
79
80#ifdef USE_CUDA
81 AddProgramOptions()("gpus,g", propt::value<Index>()->default_value(-1),
82 " Number of gpus to use");
83#endif
84
86}
87
89
90 if (OptionsMap().count("list")) {
91 std::cout << "Available XTP" + CalculatorType() + "s: \n";
92 for (const auto &name : CalculatorNames()) {
93 PrintShortHelp(std::cout, name);
94 }
96 return true;
97 }
98#ifdef USE_CUDA
99 OpenMP_CUDA::SetNoGPUs(OptionsMap()["gpus"].as<Index>());
100#endif
101
102 if (OptionsMap().count("description")) {
103 std::string calcname = OptionsMap()["description"].as<std::string>();
104 if (CalcExists(calcname)) {
105 PrintLongHelp(std::cout, calcname,
107 } else {
108 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
109 }
110
112 return true;
113 }
114 if (OptionsMap().count("printoptions")) {
115 std::string calcname = OptionsMap()["printoptions"].as<std::string>();
116 if (CalcExists(calcname)) {
117 std::string optionsFile;
118 if (OptionsMap().count("options")) {
119 optionsFile = OptionsMap()["options"].as<std::string>();
120 } else {
121 throw std::runtime_error(
122 "Please specify a --options file to print options for calculator "
123 "to.");
124 }
125 std::ofstream out;
126 out.open(optionsFile);
127 std::cout << "Writing options for calculator " << calcname << " to "
128 << optionsFile << std::endl;
129 out << "<options>\n";
131 out << "</options>\n" << std::endl;
132 } else {
133 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
134 }
135
137 return true;
138 }
139
140 CheckRequired("execute", "Nothing to do here: Abort.");
141 std::string calcname = OptionsMap()["execute"].as<std::string>();
142
143 if (CalcExists(calcname)) {
144 CreateCalculator(calcname);
145 } else {
146 std::cout << CalculatorType() << " " << calcname << " does not exist\n";
148 }
149
151
152 tools::Property useroptions;
153 if (OptionsMap().count("options")) {
154 std::string optionsFile = OptionsMap()["options"].as<std::string>();
155 useroptions.LoadFromXML(optionsFile);
156 } else {
157 // Empty user options
158 tools::Property &opts = useroptions.add("options", "");
159 opts.add(calcname, "");
160 }
161
162 if (OptionsMap().count("cmdoptions")) {
163 for (const std::string &opt :
164 OptionsMap()["cmdoptions"].as<std::vector<std::string>>()) {
165 std::vector<std::string> entries = tools::Tokenizer(opt, "=").ToVector();
166 if (entries.size() != 2) {
167 throw std::runtime_error(opt + " is not well formated!");
168 } else {
169 useroptions.getOradd("options." + calcname + "." + entries[0]).value() =
170 entries[1];
171 }
172 }
173 }
174
175 tools::OptionsHandler handler(tools::GetVotcaShare() + "/xtp/xml/");
176
177 // This is a terrible low level hack, but I do not want to refactor all of
178 // xtp_calculators now as well
179 if (calcname == "qmmm") {
180 handler.setAdditionalChoices({"jobfile"});
181 }
182 options_ = handler.ProcessUserInput(useroptions, calcname)
183 .get("options." + calcname);
184 return true;
185}
186
188
189 std::string name = ProgramName();
190 if (VersionString() != "") {
191 name = name + ", version " + VersionString();
192 }
194
195 execute();
196}
197
198void XtpApplication::ShowHelpText(std::ostream &out) {
199 std::string name = ProgramName();
200 if (VersionString() != "") {
201 name = name + ", version " + VersionString();
202 }
204 HelpText(out);
205 out << "\n\n" << VisibleOptions() << std::endl;
206}
207
208void XtpApplication::PrintShortHelp(std::ostream &out,
209 const std::string &calculator_name) const {
210 std::string xmlfile =
211 tools::GetVotcaShare() + "/xtp/xml/" + calculator_name + ".xml";
212
213 tools::Property options;
214 options.LoadFromXML(xmlfile);
215 std::string help_string = options.get("options." + calculator_name)
216 .getAttribute<std::string>("help");
217 boost::format format("%|3t|%1% %|20t|%2% \n");
218 out << format % calculator_name % help_string;
219}
220
222 std::ostream &out, const std::string &calculator_name,
224 tools::OptionsHandler handler(tools::GetVotcaShare() + "/xtp/xml/");
225 tools::Property options = handler.CalculatorOptions(calculator_name);
226 tools::PropertyIOManipulator iom(format, 2, "");
227 out << iom << options;
228}
229
230} // namespace xtp
231} // 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:308
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 SetNoGPUs(Index number)
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
void HelpTextHeader(const std::string &tool_name)
Definition version.cc:33
base class for all analysis tools
Definition basebead.h:33