votca 2024-dev
Loading...
Searching...
No Matches
csg_dlptopol.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 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <fstream>
20#include <iostream>
21
22// Third party includes
23#include <boost/format.hpp>
24
25// Local VOTCA includes
27
28using namespace votca::csg;
29using namespace std;
30using boost::format;
31
40 public:
41 string ProgramName() override { return "csg_dlptopol"; }
42 void HelpText(ostream &out) override {
43 out << "Create a dlpoly topology template based on an existing (atomistic) "
44 "topology and \n"
45 << "a mapping xml-file. The created template file needs to be "
46 "inspected and amended by the user!\n\n"
47 << "Examples:\n"
48 << "* csg_dlptopol --top .dlpf --out .dlpf --cg cg-map.xml\n convert "
49 "FIELD to FIELD_CGV using cg-map.xml\n"
50 << "* csg_dlptopol --top FA-dlpoly.dlpf --out CG-dlpoly.dlpf --cg "
51 "cg-map.xml\n"
52 << "* csg_dlptopol --top FA-gromacs.tpr --out FA-dlpoly.dlpf "
53 "--no-map\n";
54 }
55
56 bool DoMapping(void) override { return true; }
57
58 void Initialize(void) override;
59 bool EvaluateOptions(void) override {
61 CheckRequired("out", "no output topology specified");
62 return true;
63 }
64 bool EvaluateTopology(Topology *top, Topology *top_ref) override;
65
66 protected:
67 void WriteMoleculeAtoms(ostream &out, const Molecule &cg);
68 void WriteMoleculeInteractions(ostream &out, const Molecule &cg);
69 void WriteVDWInteractions(ostream &out, const Molecule &cg);
70 void WriteMolecularType(ostream &out, const Molecule &cg,
71 votca::Index nummol);
72};
73
76 // AddProgramOptions()
77 //("top", boost::program_options::value<string>(),
78 //" input topology in any known format:\n <name>.dlpf for dlpoly, <name>.tpr
79 // for gromacs\n (convention: '.dlpf'='use FIELD')");
80 AddProgramOptions()("out", boost::program_options::value<string>(),
81 " output topology in dlpoly format");
82}
83
85 // check the file names from options
86
87 string fname = OptionsMap()["top"].as<string>();
88
89 if (fname == ".dlpf") {
90 fname = "FIELD";
91 }
92
93 cout << "input file-name: " << fname << endl;
94
95 fname = OptionsMap()["out"].as<string>();
96
97#ifndef NDEBUG
98 cout << "output file-name given: " << fname << endl;
99#endif
100
101 if (fname == ".dlpf") {
102 fname = "FIELD_CGV";
103 }
104
105#ifndef NDEBUG
106 cout << "output file-name actual: " << fname << endl;
107#else
108 cout << "output file-name: " << fname << endl;
109#endif
110
111 // do CG mapping
112
113 MoleculeContainer &mols = top->Molecules();
114 std::vector<const Molecule *> MolecularTypes;
115
116 votca::Index prv_mol_number = 1;
117 string prv_mol_name;
118 vector<votca::Index> nummols;
119
120 vector<string> vdw_pairs;
121
122 // find all unique molecular types
123
124 for (const auto &mol : mols) {
125 // molecules are ignored during the mapping stage
126 // i.e. the ignored ones do not enter the CG topology (*top) - ?
127 // if( IsIgnored(mol->getName()) ) continue;
128
129 if (mol.getName() == prv_mol_name) {
130 prv_mol_number++;
131 continue;
132 }
133
134 nummols.push_back(prv_mol_number);
135 prv_mol_number = 1;
136 prv_mol_name = mol.getName();
137
138 cout << "'" << mol.getName() << "' added to CG molecular types" << endl;
139
140 MolecularTypes.push_back(&mol);
141
142 // collect unique bead pairs over all molecular types found
143
144 for (votca::Index ib1 = 0; ib1 < mol.BeadCount(); ib1++) {
145 string bead_name1 = mol.getBead(ib1)->getType();
146 bead_name1 = bead_name1.substr(
147 0,
148 bead_name1.find_first_of("#")); // skip #index of atom from its name
149
150 for (const auto &MolecularType : MolecularTypes) {
151
152 for (votca::Index ib2 = 0; ib2 < MolecularType->BeadCount(); ib2++) {
153
154 string bead_name2 = MolecularType->getBead(ib2)->getType();
155 bead_name2 = bead_name2.substr(
156 0, bead_name2.find_first_of("#")); // skip #index of atom from
157 // its name
158
159 stringstream ss_bp1, ss_bp2;
160
161 ss_bp1 << format("%8s%8s") % bead_name1 % bead_name2;
162 ss_bp2 << format("%8s%8s") % bead_name2 % bead_name1;
163
164 bool is_new_pair = true;
165
166 for (const auto &vdw_pair : vdw_pairs) {
167 if (ss_bp1.str() == vdw_pair || ss_bp2.str() == vdw_pair) {
168 is_new_pair = false;
169 break;
170 }
171 }
172 if (is_new_pair) {
173 vdw_pairs.push_back(ss_bp1.str());
174#ifndef NDEBUG
175 cout << "'" << ss_bp1.str() << "' added to CG vdw pairs" << endl;
176#endif
177 }
178 }
179 }
180 }
181 }
182 nummols.push_back(prv_mol_number);
183
184 if (MolecularTypes.size() > 1) {
185 cout << "WARNING: creation of topology for multiple molecular types "
186 "is experimental at this stage\n";
187 }
188
189 ofstream fl;
190 fl.open(fname);
191
192 fl << "From VOTCA with love"
193 << " # check/amend this file if needed!\n";
194 fl << "units kJ\n";
195 fl << "molecular types " << MolecularTypes.size() << endl;
196
197 for (votca::Index i = 0; i < votca::Index(MolecularTypes.size()); i++) {
198 WriteMolecularType(fl, *(MolecularTypes[i]), nummols[i + 1]);
199 }
200
201 // vdw seciton (pairwise vdw/CG interactions)
202
203 if (vdw_pairs.size() > 0) {
204
205 fl << "vdw " << vdw_pairs.size() << endl;
206
207 for (const auto &vdw_pair : vdw_pairs) {
208 fl << vdw_pair << " tab 1.00000 0.00000\n";
209 }
210 }
211
212 fl << "close" << endl;
213
214 cout << "Created template for dlpoly topology - please, check & amend if "
215 "needed!"
216 << endl;
217
218 fl.close();
219 return true;
220}
221
222void DLPTopolApp::WriteMoleculeAtoms(ostream &out, const Molecule &cg) {
223 out << "atoms " << cg.BeadCount() << endl;
224 out << "# name mass charge nrept ifrozen (optional: ngroup, index, "
225 "name/type, type/residue, index/res-ID) \n";
226 for (votca::Index i = 0; i < cg.BeadCount(); ++i) {
227 const Bead *b = cg.getBead(i);
228
229 string bname = b->getName();
230 string btype = b->getType();
231
232 bname = bname.substr(
233 0, bname.find_first_of("#")); // skip #index of atom from its name
234 btype = btype.substr(
235 0, btype.find_first_of("#")); // skip #index of atom from its type
236
237 out << format("%8s %10f %10f 1 0 1 %10d %8s %8s %10d \n") %
238 btype % b->getMass() % b->getQ() % (i + 1) % btype % bname %
239 (i + 1);
240 //% b->getType()->getName() % b->getMass() % b->getQ() % (i+1) %
241 // b->getType()->getName() % b->getName() % (i+1);
242 }
243}
244
246
247 stringstream sout;
248
249 votca::Index n_entries = 0;
250 votca::Index nb = -1;
251
252 for (const Interaction *ic : cg.Interactions()) {
253 if (nb != ic->BeadCount()) {
254
255 if (sout.str() != "") {
256 out << n_entries << endl << sout.str();
257 }
258
259 sout.str("");
260 n_entries = 0;
261
262 nb = ic->BeadCount();
263 switch (nb) {
264 case 2:
265 out << "bonds ";
266 break;
267 case 3:
268 out << "angles ";
269 break;
270 case 4:
271 out << "dihedrals ";
272 break;
273 default:
274 string err = "cannot handle number of beads in interaction:";
275 err += to_string(ic->getMolecule() + 1) + ":" + ic->getGroup();
276 err += ":" + to_string(ic->getIndex() + 1);
277 throw runtime_error(err);
278 }
279 }
280 n_entries++;
281 // to do: is it possible to use bond/angle/dihedral function types for 1:1
282 // mapping? (CG map overwrites ic->Group anyway)
283 // sout << ic->getInteractionFunc(); // something like that (only for 1:1
284 // mapping!)
285 sout << " tab ";
286 for (votca::Index i = 0; i < nb; ++i) {
287 sout << ic->getBeadId(i) + 1 << " ";
288 }
289 sout << " 1.00000 0.00000"
290 << " # ";
291 sout << to_string(ic->getMolecule() + 1);
292 sout << ":" + ic->getGroup();
293 sout << ":" + to_string(ic->getIndex() + 1) << endl;
294 }
295 if (sout.str() != "") {
296 out << n_entries << endl << sout.str();
297 }
298}
299
300void DLPTopolApp::WriteMolecularType(ostream &out, const Molecule &cg,
301 votca::Index nummol) {
302 out << cg.getName() << endl;
303 out << "nummols " << nummol << endl;
304
305 WriteMoleculeAtoms(out, cg);
307
308 out << "finish" << endl;
309}
310
311int main(int argc, char **argv) {
312 DLPTopolApp app;
313 return app.Exec(argc, argv);
314}
class for writing dlpoly topology files
void HelpText(ostream &out) override
help text of application without version information
void WriteMolecularType(ostream &out, const Molecule &cg, votca::Index nummol)
bool DoMapping(void) override
overload and return true to enable mapping command line options
string ProgramName() override
program name
bool EvaluateOptions(void) override
Process command line options.
void WriteMoleculeAtoms(ostream &out, const Molecule &cg)
bool EvaluateTopology(Topology *top, Topology *top_ref) override
called after topology was loaded
void WriteMoleculeInteractions(ostream &out, const Molecule &cg)
void WriteVDWInteractions(ostream &out, const Molecule &cg)
void Initialize(void) override
Initialize application data.
virtual const std::string getType() const noexcept
Definition basebead.h:84
std::string getName() const
Gets the name of the bead.
Definition basebead.h:58
virtual const double & getMass() const noexcept
Definition basebead.h:104
information about a bead
Definition bead.h:50
virtual const double & getQ() const
Definition bead.h:67
bool EvaluateOptions() override
Process command line options.
void Initialize() override
Initialize application data.
base class for all interactions
Definition interaction.h:40
information about molecules
Definition molecule.h:45
Bead * getBead(Index bead)
get the id of a bead in the molecule
Definition molecule.h:59
std::vector< Interaction * > Interactions()
Definition molecule.h:79
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
topology of the whole system
Definition topology.h:81
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_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.
int main(int argc, char **argv)
STL namespace.
boost::container::deque< Molecule, void, block_molecule_4x_t > MoleculeContainer
Definition topology.h:68
Eigen::Index Index
Definition types.h:26