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 // Local VOTCA includes
24 #include "votca/xtp/calculatorfactory.h"
25 #include "votca/xtp/stateapplication.h"
26 #include "votca/xtp/statesaver.h"
27 #include "votca/xtp/version.h"
28 
29 namespace votca {
30 namespace xtp {
31 
AddCommandLineOptions()32 void StateApplication::AddCommandLineOptions() {
33 
34   namespace propt = boost::program_options;
35 
36   AddProgramOptions()("file,f", propt::value<std::string>(),
37                       "  hdf5 state file, *.hdf5");
38   AddProgramOptions()("first-frame,i", propt::value<Index>()->default_value(0),
39                       "  start from this frame");
40   AddProgramOptions()("nframes,n", propt::value<Index>()->default_value(1),
41                       "  number of frames to process");
42   AddProgramOptions()("save,s", propt::value<bool>()->default_value(true),
43                       "  whether or not to save changes to state file");
44 
45   AddCommandLineOpt();
46 }
47 
EvaluateSpecificOptions()48 void StateApplication::EvaluateSpecificOptions() {
49   CheckRequired("file", "Please provide the state file");
50   CheckOptions();
51 }
52 
execute()53 void StateApplication::execute() {
54 
55   Index nframes = OptionsMap()["nframes"].as<Index>();
56   Index fframe = OptionsMap()["first-frame"].as<Index>();
57   bool save = OptionsMap()["save"].as<bool>();
58 
59   // STATESAVER & PROGRESS OBSERVER
60   std::string statefile = OptionsMap()["file"].as<std::string>();
61   StateSaver statsav(statefile);
62   std::vector<Index> frames = statsav.getFrames();
63   if (frames.empty()) {
64     throw std::runtime_error("Statefile " + statefile + " not found.");
65   }
66   // INITIALIZE & RUN CALCULATORS
67   std::cout << "Initializing calculator" << std::endl;
68   ConfigCalculator();
69   std::cout << frames.size() << " frames in statefile, Ids are: ";
70   for (Index frame : frames) {
71     std::cout << frame << " ";
72   }
73   std::cout << std::endl;
74   if (fframe < Index(frames.size())) {
75     std::cout << "Starting at frame " << frames[fframe] << std::endl;
76   } else {
77     std::cout << "First frame:" << fframe
78               << " is larger than number of frames:" << Index(frames.size())
79               << std::endl;
80     return;
81   }
82 
83   if ((fframe + nframes) > Index(frames.size())) {
84     nframes = Index(frames.size()) - fframe;
85   }
86 
87   for (Index i = fframe; i < nframes; i++) {
88     std::cout << "Evaluating frame " << frames[i] << std::endl;
89     Topology top = statsav.ReadFrame(frames[i]);
90     EvaluateFrame(top);
91     if (save && savetoStateFile()) {
92       statsav.WriteFrame(top);
93     } else {
94       std::cout << "Changes have not been written to state file." << std::endl;
95     }
96   }
97 }
98 
99 }  // namespace xtp
100 }  // namespace votca
101