votca 2024.1-dev
Loading...
Searching...
No Matches
xtp_calculators.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2023 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
19#include <iostream>
20#include <pybind11/embed.h>
21#include <pybind11/stl.h>
23
24namespace py = pybind11;
25using namespace votca;
26
27namespace pyxtp {
28
29void call_calculator(const std::string& calculatorName,
30 const std::map<std::string, std::string>& dict) {
31
32 // Retrieve arguments from Python dictionary
33 const std::string& xmlFile = dict.at("xmlFile");
34 const std::string& stateFile = dict.at("stateFile");
35 Index nThreads = std::stol(dict.at("nThreads"));
36 Index firstFrame = std::stol(dict.at("firstFrame"));
37 Index nFrames = std::stol(dict.at("nFrames"));
38 bool save = boost::lexical_cast<bool>("0");
39 // bool save = boost::lexical_cast<bool>(std::stol(dict.at("save")));
40
41 // Load properties
43 prop.LoadFromXML(xmlFile);
44
47 handler.ProcessUserInput(prop, calculatorName)
48 .get("options." + calculatorName);
49
50 // Call calculator
52 calc.Initialize(calculatorName, nThreads, options);
53 calc.Run(stateFile, nFrames, firstFrame, save);
54}
55
56void XTPCalculators::Initialize(const std::string& name, Index nThreads,
59 calculator_->setnThreads(nThreads);
61 std::cout << "Calculator has been Initialized\n";
62}
63
64void XTPCalculators::Run(const std::string& stateFile, Index nFrames,
65 Index firstFrame, bool save) {
66 // STATESAVER & PROGRESS OBSERVER
67 xtp::StateSaver statsav(stateFile);
68 std::vector<Index> frames = statsav.getFrames();
69 if (frames.empty()) {
70 throw std::runtime_error("stateFile " + stateFile + " not found.");
71 }
72 // INITIALIZE & RUN CALCULATORS
73 std::cout << "Initializing calculator\n";
74 std::cout << frames.size() << " frames in stateFile, Ids are: ";
75 for (Index frame : frames) {
76 std::cout << frame << " ";
77 }
78 std::cout << "\n";
79 if (firstFrame < Index(frames.size())) {
80 std::cout << "Starting at frame " << frames[firstFrame] << std::endl;
81 } else {
82 std::cout << "First frame:" << firstFrame
83 << " is larger than number of frames:" << Index(frames.size())
84 << std::endl;
85 return;
86 }
87
88 if ((firstFrame + nFrames) > Index(frames.size())) {
89 nFrames = Index(frames.size()) - firstFrame;
90 }
91
92 for (Index i = firstFrame; i < nFrames; i++) {
93 std::cout << "Evaluating frame " << frames[i] << std::endl;
94 xtp::Topology top = statsav.ReadFrame(frames[i]);
96 if (save && calculator_->WriteToStateFile()) {
97 statsav.WriteFrame(top);
98 } else {
99 std::cout << "Changes have not been written to state file." << std::endl;
100 }
101 }
102}
103
104} // namespace pyxtp
void Run(const std::string &statefile, Index nFrames, Index firstFrame, bool save)
void Initialize(const std::string &calculatorName, Index nThreads, votca::tools::Property prop)
std::unique_ptr< xtp::QMCalculator > calculator_
void setnThreads(Index nThreads)
Sets number of threads to use.
Definition calculator.h:83
virtual std::unique_ptr< T > Create(const key_t &key, args_t &&...arguments)
Property ProcessUserInput(const Property &user_input, const std::string &calcname) const
Load the default options and merge them with the user input.
class to manage program options with xml serialization functionality
Definition property.h:55
Property & get(const std::string &key)
get existing property
Definition property.cc:79
void LoadFromXML(std::string filename)
Definition property.cc:238
void Initialize(const tools::Property &opt) final
Initializes a calculator from an XML file with options.
bool EvaluateFrame(Topology &top)
virtual bool WriteToStateFile() const =0
Topology ReadFrame(Index frameid) const
Definition statesaver.cc:76
void WriteFrame(const Topology &top)
Definition statesaver.cc:44
std::vector< Index > getFrames() const
Definition statesaver.cc:33
Container for segments and box and atoms.
Definition topology.h:41
void call_calculator(const std::string &calculatorName, const std::map< std::string, std::string > &dict)
Construct a new pybind11 module object to invoke a votca-xtp Calculator.
std::string GetVotcaShare()
Retrieves VOTCASHARE environment variable. Throws std::runtime_error if not set.
Definition globals.cc:27
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26