votca 2024.2-dev
Loading...
Searching...
No Matches
xtp_map.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2021 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// Standard includes
21#include <fstream>
22#include <iostream>
23#include <stdexcept>
24
25// VOTCA includes
31#include <votca/tools/globals.h>
32
33// Local VOTCA includes
36#include "votca/xtp/topology.h"
37#include "votca/xtp/version.h"
38
39using namespace std;
40
41namespace CSG = votca::csg;
42namespace XTP = votca::xtp;
43namespace TOOLS = votca::tools;
44
45class XtpMap : public TOOLS::Application {
46
47 public:
48 string ProgramName() override { return "xtp_map"; }
49 void HelpText(ostream& out) override {
50 out << "Generates QM|MD topology" << endl;
51 }
52 void ShowHelpText(std::ostream& out) override;
53
54 void Initialize() override;
55 bool EvaluateOptions() override;
56 void Run() override;
57
58 protected:
59};
60
61namespace propt = boost::program_options;
62
64
65 CSG::TrajectoryWriter::RegisterPlugins();
66 CSG::TrajectoryReader::RegisterPlugins();
67 CSG::TopologyReader::RegisterPlugins();
68
69 AddProgramOptions()("topology,t", propt::value<string>(), " topology");
70 AddProgramOptions()("coordinates,c", propt::value<string>(),
71 " coordinates or trajectory");
72 AddProgramOptions()("segments,s", propt::value<string>(),
73 " definition of segments and fragments");
74 AddProgramOptions()("makesegments,m", " write out a skeleton segments file");
75 AddProgramOptions()("file,f", propt::value<string>(), " state file");
76 AddProgramOptions()("first-frame,i",
77 propt::value<votca::Index>()->default_value(0),
78 " start from this frame");
79 AddProgramOptions()("begin,b", propt::value<double>()->default_value(0.0),
80 " start time in simulation");
81 AddProgramOptions()("nframes,n",
82 propt::value<votca::Index>()->default_value(1),
83 " number of frames to process");
84}
85
87
88 CheckRequired("topology", "Missing topology file");
89 CheckRequired("segments", "Missing segment definition file");
90 CheckRequired("coordinates", "Missing trajectory input");
91 if (!(OptionsMap().count("makesegments"))) {
92 CheckRequired("file", "Missing state file");
93 }
94 return 1;
95}
96
98
99 std::string name = ProgramName();
100 if (VersionString() != "") {
101 name = name + ", version " + VersionString();
102 }
104
105 // ++++++++++++++++++++++++++++ //
106 // Create MD topology from file //
107 // ++++++++++++++++++++++++++++ //
108
109 // Create topology reader
110 string topfile = OptionsMap()["topology"].as<string>();
111 std::unique_ptr<CSG::TopologyReader> topread =
112 CSG::TopReaderFactory().Create(topfile);
113
114 if (topread == nullptr) {
115 throw runtime_error(string("Input format not supported: ") +
116 OptionsMap()["topology"].as<string>());
117 }
118 CSG::Topology mdtopol;
119 topread->ReadTopology(topfile, mdtopol);
120 if (votca::Log::verbose()) {
121 cout << "Read MD topology from " << topfile << ": Found "
122 << mdtopol.BeadCount() << " atoms in " << mdtopol.MoleculeCount()
123 << " molecules. " << endl;
124 }
125
126 // ++++++++++++++++++++++++++++++ //
127 // Create MD trajectory from file //
128 // ++++++++++++++++++++++++++++++ //
129
130 // Create trajectory reader and initialize
131 string trjfile = OptionsMap()["coordinates"].as<string>();
132 std::unique_ptr<CSG::TrajectoryReader> trjread =
133 CSG::TrjReaderFactory().Create(trjfile);
134
135 if (trjread == nullptr) {
136 throw runtime_error(string("Input format not supported: ") +
137 OptionsMap()["coordinates"].as<string>());
138 }
139 trjread->Open(trjfile);
140 trjread->FirstFrame(mdtopol);
141
142 string mapfile = OptionsMap()["segments"].as<string>();
143 if (OptionsMap().count("makesegments")) {
144 if (TOOLS::filesystem::FileExists(mapfile)) {
145 cout << endl
146 << "xtp_map : map file '" << mapfile
147 << "' already in use. Delete the current mapfile or specify a "
148 "different name."
149 << endl;
150 return;
151 }
152
153 cout << " Writing template mapfile to " << mapfile << std::endl;
154
155 TOOLS::Property mapfile_prop("topology", "", "");
156 TOOLS::Property& molecules = mapfile_prop.add("molecules", "");
157
158 std::map<std::string, const CSG::Molecule*> firstmolecule;
159
160 std::map<std::string, votca::Index> molecule_names;
161 for (const CSG::Molecule& mol : mdtopol.Molecules()) {
162 if (!molecule_names.count(mol.getName())) {
163 firstmolecule[mol.getName()] = &mol;
164 }
165 molecule_names[mol.getName()]++;
166 }
167 for (const auto& mol : molecule_names) {
168 std::cout << "Found " << mol.second << " with name " << mol.first
169 << std::endl;
170 }
171 for (const auto& mol : molecule_names) {
172 TOOLS::Property& molecule = molecules.add("molecule", "");
173 molecule.add("mdname", mol.first);
174 TOOLS::Property& segments = molecule.add("segments", "");
175 TOOLS::Property& segment = segments.add("segment", "");
176 segment.add("name", "UPTOYOU_BUTUNIQUE");
177 segment.add("qmcoords_n", "XYZFILE_GROUNDSTATE");
178 segment.add("multipoles_n", "MPSFILE_GROUNDSTATE");
179 segment.add("map2md", "WANTTOMAPTOMDGEOMETRY");
180 segment.add("U_xX_nN_h", "REORG1_hole");
181 segment.add("U_nX_nN_h", "REORG2_hole");
182 segment.add("U_xN_xX_h", "REORG3_hole");
183 TOOLS::Property& fragments = segment.add("fragments", "");
184 TOOLS::Property& fragment = fragments.add("fragment", "");
185 std::string atomnames = "";
186 const CSG::Molecule* csgmol = firstmolecule[mol.first];
187 std::vector<const CSG::Bead*> sortedbeads;
188 sortedbeads.reserve(csgmol->BeadCount());
189 for (const CSG::Bead* bead : csgmol->Beads()) {
190 sortedbeads.push_back(bead);
191 }
192 std::sort(sortedbeads.begin(), sortedbeads.end(),
193 [&](const CSG::Bead* b1, const CSG::Bead* b2) {
194 return b1->getId() < b2->getId();
195 });
196
197 for (const CSG::Bead* bead : sortedbeads) {
198 atomnames += " " + std::to_string(bead->getResnr()) + ":" +
199 bead->getName() + ":" + std::to_string(bead->getId());
200 }
201 fragment.add("name", "UPTOYOU_BUTUNIQUE");
202 fragment.add("mdatoms", atomnames);
203 fragment.add("qmatoms", "IDS of QMATOMS i.e 0:C 1:H 2:C");
204 fragment.add("mpoles", "IDS of MPOLES i.e 0:C 1:H 2:C");
205 fragment.add("weights",
206 "weights for mapping(often atomic mass) i.e. 12 1 12");
207 fragment.add("localframe", "IDs of up to 3 qmatoms or mpoles i.e. 0 1 2");
208 std::ofstream template_mapfile(mapfile);
209 template_mapfile << mapfile_prop << std::flush;
210 template_mapfile.close();
211
212 std::cout << "MOLECULETYPE " << csgmol->getName() << std::endl;
213 std::cout << "SAMPLECOORDINATES" << std::endl;
214 std::cout << "ID NAME COORDINATES[Angstroem] " << std::endl;
215 for (const CSG::Bead* bead : sortedbeads) {
216 Eigen::Vector3d pos = bead->getPos() * votca::tools::conv::nm2ang;
217 std::string output =
218 (boost::format("%1$i %2$s %3$+1.4f %4$+1.4f %5$+1.4f\n") %
219 bead->getId() % bead->getName() % pos[0] % pos[1] % pos[2])
220 .str();
221 std::cout << output;
222 }
223 }
224 std::cout << std::flush;
225 return;
226 }
227
228 if (!TOOLS::filesystem::FileExists(mapfile)) {
229 cout << endl
230 << "xtp_map : map file '" << mapfile << "' could not be found."
231 << endl;
232 return;
233 }
234 XTP::Md2QmEngine md2qm(mapfile);
235
236 votca::Index firstFrame = OptionsMap()["first-frame"].as<votca::Index>();
237 votca::Index nFrames = OptionsMap()["nframes"].as<votca::Index>();
238 bool beginAt = false;
239 double time = OptionsMap()["begin"].as<double>();
240 double startTime = mdtopol.getTime();
241 if (time > 0.0) {
242 beginAt = true;
243 startTime = time;
244 }
245
246 // Extract first frame specified
247 bool hasFrame;
248 votca::Index frames_found = 0;
249 votca::Index firstframecounter = firstFrame;
250 for (hasFrame = true; hasFrame == true;
251 hasFrame = trjread->NextFrame(mdtopol)) {
252 frames_found++;
253 if (((mdtopol.getTime() < startTime) && beginAt) || firstframecounter > 0) {
254 firstframecounter--;
255 continue;
256 }
257 break;
258 }
259 if (!hasFrame) {
260 trjread->Close();
261
262 throw runtime_error("Time or frame number exceeds trajectory length");
263 }
264 if (votca::Log::verbose()) {
265 cout << "Read MD trajectory from " << trjfile << ": found " << frames_found
266 << " frames, starting from frame " << firstFrame << endl;
267 }
268 // +++++++++++++++++++++++++ //
269 // Convert MD to QM Topology //
270 // +++++++++++++++++++++++++ //
271
272 string statefile = OptionsMap()["file"].as<string>();
273 if (TOOLS::filesystem::FileExists(statefile)) {
274 cout << endl
275 << "xtp_map : state file '" << statefile
276 << "' already in use. Delete the current statefile or specify a "
277 "different name."
278 << endl;
279 return;
280 }
281
282 XTP::StateSaver statsav(statefile);
283 votca::Index laststep =
284 -1; // for some formats no step is given out so we check if the step
285 for (votca::Index saved = 0; hasFrame && saved < nFrames;
286 hasFrame = trjread->NextFrame(mdtopol), saved++) {
287 if (mdtopol.getStep() == laststep) {
288 mdtopol.setStep(laststep + 1);
289 }
290 laststep = mdtopol.getStep();
291 XTP::Topology qmtopol = md2qm.map(mdtopol);
292 statsav.WriteFrame(qmtopol);
293 }
294}
295
296void XtpMap::ShowHelpText(std::ostream& out) {
297 string name = ProgramName();
298 if (VersionString() != "") {
299 name = name + ", version " + VersionString();
300 }
302 HelpText(out);
303 out << "\n\n" << VisibleOptions() << endl;
304}
305
306int main(int argc, char** argv) {
307 XtpMap xtpmap;
308 return xtpmap.Exec(argc, argv);
309}
void Run() override
Main body of application.
Definition xtp_map.cc:97
string ProgramName() override
program name
Definition xtp_map.cc:48
void ShowHelpText(std::ostream &out) override
Definition xtp_map.cc:296
void HelpText(ostream &out) override
help text of application without version information
Definition xtp_map.cc:49
void Initialize() override
Initialize application data.
Definition xtp_map.cc:63
bool EvaluateOptions() override
Process command line options.
Definition xtp_map.cc:86
information about a bead
Definition bead.h:50
information about molecules
Definition molecule.h:45
const std::string & getName() const
get the name of the molecule
Definition molecule.h:51
Index BeadCount() const
get the number of beads in the molecule
Definition molecule.h:65
const std::vector< Bead * > & Beads() const
Definition molecule.h:67
topology of the whole system
Definition topology.h:81
double getTime() const
Definition topology.h:317
Index MoleculeCount() const
number of molecules in the system
Definition topology.h:144
Index BeadCount() const
Definition topology.h:150
Index getStep() const
Definition topology.h:329
void setStep(Index s)
Definition topology.h:323
MoleculeContainer & Molecules()
Definition topology.h:182
int Exec(int argc, char **argv)
executes the program
boost::program_options::variables_map & OptionsMap()
get available program options & descriptions
boost::program_options::options_description & VisibleOptions()
filters out the Hidden group from the options descriptions
virtual std::string VersionString()
version string of application
Definition application.h:55
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.
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
Topology map(const csg::Topology &top) const
void WriteFrame(const Topology &top)
Definition statesaver.cc:44
Container for segments and box and atoms.
Definition topology.h:41
STL namespace.
FileFormatFactory< TopologyReader > & TopReaderFactory()
FileFormatFactory< TrajectoryReader > & TrjReaderFactory()
const double nm2ang
Definition constants.h:50
Charge transport classes.
Definition ERIs.h:28
void HelpTextHeader(const std::string &tool_name)
Definition version.cc:34
Eigen::Index Index
Definition types.h:26
static bool verbose()
Definition globals.h:32
int main(int argc, char **argv)
Definition xtp_map.cc:306