votca 2024-dev
Loading...
Searching...
No Matches
csg_map.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2021 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 <cstddef>
20#include <fstream>
21#include <stdexcept>
22#include <string>
23
24// Local VOTCA includes
26#include "votca/csg/topology.h"
28
29using namespace std;
30using namespace votca::csg;
31
32class CsgMapApp : public CsgApplication {
33 public:
34 string ProgramName() override { return "csg_map"; }
35 void HelpText(ostream &out) override {
36 out << "Convert a reference atomistic trajectory or configuration into a "
37 "coarse-grained one \n"
38 << "based on a mapping xml-file. The mapping can be applied to either "
39 "an entire trajectory \n"
40 << "or a selected set of frames only (see options).\n"
41 << "Examples:\n"
42 << "* csg_map --top FA-topol.tpr --trj FA-traj.trr --out CG-traj.xtc "
43 "--cg cg-map.xml\n"
44 << "* csg_map --top FA-topol.tpr --trj FA-conf.gro --out CG-conf.gro "
45 "--cg cg-map.xml\n"
46 << "* csg_map --top FA-topol.tpr --trj FA-traj.xtc --out "
47 "FA-history.dlph --no-map\n"
48 << "* csg_map --top FA-field.dlpf --trj FA-history.dlph --out "
49 "CG-history.dlph --cg cg-map.xml\n"
50 << "* csg_map --top .dlpf --trj .dlph --out .dlph --cg cg-map.xml "
51 "convert HISTORY to HISTORY_CGV\n";
52 }
53
54 bool DoTrajectory() override { return true; }
55 bool DoMapping() override { return true; }
56
57 void Initialize() override {
59 AddProgramOptions()("out", boost::program_options::value<string>(),
60 " output file for coarse-grained trajectory")(
61 "vel", " Write mapped velocities (if available)")(
62 "force",
63 " Write mapped forces (if "
64 "available)")("hybrid",
65 " Create "
66 "hybrid "
67 "trajectory "
68 "containing "
69 "both "
70 "atomistic "
71 "and "
72 "coarse-"
73 "grained");
74 }
75
76 bool EvaluateOptions() override {
78 CheckRequired("trj", "no trajectory file specified");
79 CheckRequired("out", "need to specify output trajectory");
80 return true;
81 }
82
83 void BeginEvaluate(Topology *top, Topology *top_ref) override;
84 void EvalConfiguration(Topology *top, Topology *top_ref) override {
85 if (!do_hybrid_) {
86 // simply write the topology mapped by csgapplication class
87 if (do_vel_) {
88 top->SetHasVel(true);
89 }
90 if (do_force_) {
91 top->SetHasForce(true);
92 }
93 writer_->Write(top);
94 } else {
95 // we want to combine atomistic and coarse-grained into one topology
96 Topology hybtol;
97
98 hybtol.setBox(top->getBox());
99 hybtol.setTime(top->getTime());
100 hybtol.setStep(top->getStep());
101
102 // copy all residues from both
103 for (const auto &residue : top_ref->Residues()) {
104 hybtol.CreateResidue(residue.getName());
105 }
106 for (const auto &residue : top->Residues()) {
107 hybtol.CreateResidue(residue.getName());
108 }
109
110 // copy all molecules and beads
111
112 for (const auto &molecule : top_ref->Molecules()) {
113 Molecule *mi = hybtol.CreateMolecule(molecule.getName());
114 for (votca::Index i = 0; i < molecule.BeadCount(); i++) {
115 // copy atomistic beads of molecule
116 votca::Index beadid = molecule.getBead(i)->getId();
117
118 const Bead *bi = molecule.getBead(i);
119 if (!hybtol.BeadTypeExist(bi->getType())) {
120 hybtol.RegisterBeadType(bi->getType());
121 }
122
123 Bead *bn =
124 hybtol.CreateBead(bi->getSymmetry(), bi->getName(), bi->getType(),
125 bi->getResnr(), bi->getMass(), bi->getQ());
126 bn->setPos(bi->getPos());
127 if (bi->HasVel()) {
128 bn->setVel(bi->getVel());
129 }
130 if (bi->HasF()) {
131 bn->setF(bi->getF());
132 }
133
134 mi->AddBead(&hybtol.Beads()[beadid], molecule.getBeadName(i));
135 }
136
137 if (mi->getId() < top->MoleculeCount()) {
138 // copy cg beads of molecule
139 Molecule *cgmol = top->getMolecule(mi->getId());
140 for (votca::Index i = 0; i < cgmol->BeadCount(); i++) {
141 Bead *bi = cgmol->getBead(i);
142 // todo: this is a bit dirty as a cg bead will always have the resid
143 // of its first parent
144 const Bead *bparent = molecule.getBead(0);
145 Bead *bn = hybtol.CreateBead(bi->getSymmetry(), bi->getName(),
146 bi->getType(), bparent->getResnr(),
147 bi->getMass(), bi->getQ());
148 bn->setPos(bi->getPos());
149 if (bi->HasVel()) {
150 bn->setVel(bi->getVel());
151 }
152 mi->AddBead(bi, bi->getName());
153 }
154 }
155 }
156 hybtol.setBox(top_ref->getBox());
157
158 writer_->Write(&hybtol);
159 }
160 }
161
162 void EndEvaluate() override { writer_->Close(); }
163
164 protected:
165 std::unique_ptr<TrajectoryWriter> writer_;
169};
170
172 string out = OptionsMap()["out"].as<string>();
173 cout << "writing coarse-grained trajectory to " << out << endl;
174 writer_ = TrjWriterFactory().Create(out);
175 if (writer_ == nullptr) {
176 throw runtime_error("output format not supported: " + out);
177 }
178
179 do_hybrid_ = false;
180 if (OptionsMap().count("hybrid")) {
181 if (!do_mapping_) {
182 throw runtime_error("options hybrid and no-map not compatible");
183 }
184 cout << "Doing hybrid mapping..." << endl;
185 do_hybrid_ = true;
186 }
187
188 do_vel_ = false;
189 if (OptionsMap().count("vel")) {
190 do_vel_ = true;
191 }
192
193 do_force_ = false;
194 if (OptionsMap().count("force")) {
195 do_force_ = true;
196 }
197
198 writer_->Open(out);
199}
200
201int main(int argc, char **argv) {
202 CsgMapApp app;
203 return app.Exec(argc, argv);
204}
string ProgramName() override
program name
Definition csg_map.cc:34
bool DoTrajectory() override
overload and return true to enable trajectory command line options
Definition csg_map.cc:54
void EndEvaluate() override
called after the last frame
Definition csg_map.cc:162
bool EvaluateOptions() override
Process command line options.
Definition csg_map.cc:76
void Initialize() override
Initialize application data.
Definition csg_map.cc:57
bool do_hybrid_
Definition csg_map.cc:166
std::unique_ptr< TrajectoryWriter > writer_
Definition csg_map.cc:165
void HelpText(ostream &out) override
help text of application without version information
Definition csg_map.cc:35
bool DoMapping() override
overload and return true to enable mapping command line options
Definition csg_map.cc:55
void EvalConfiguration(Topology *top, Topology *top_ref) override
Definition csg_map.cc:84
bool do_vel_
Definition csg_map.cc:167
void BeginEvaluate(Topology *top, Topology *top_ref) override
called before the first frame
Definition csg_map.cc:171
bool do_force_
Definition csg_map.cc:168
virtual const std::string getType() const noexcept
Definition basebead.h:84
virtual const Eigen::Vector3d & getPos() const
Definition basebead.h:166
std::string getName() const
Gets the name of the bead.
Definition basebead.h:58
virtual void setPos(const Eigen::Vector3d &bead_position)
Definition basebead.h:161
virtual const double & getMass() const noexcept
Definition basebead.h:104
Index getId() const noexcept
Gets the id of the bead.
Definition basebead.h:52
information about a bead
Definition bead.h:50
const Eigen::Vector3d & getF() const
get the force acting on the bead
Definition bead.h:361
const Index & getResnr() const
Definition bead.h:61
bool HasF() const noexcept
Definition bead.h:235
void setF(const Eigen::Vector3d &bead_force)
Definition bead.h:356
bool HasVel() const noexcept
Definition bead.h:232
void setVel(const Eigen::Vector3d &r)
Definition bead.h:315
const Eigen::Vector3d & getVel() const
Definition bead.h:320
virtual const double & getQ() const
Definition bead.h:67
Symmetry getSymmetry() const
Definition bead.h:86
bool EvaluateOptions() override
Process command line options.
void Initialize() override
Initialize application data.
information about molecules
Definition molecule.h:45
void AddBead(Bead *bead, const std::string &name)
Add a bead to the molecule.
Definition molecule.cc:29
Bead * getBead(Index bead)
get the id of a bead in the molecule
Definition molecule.h:59
Index BeadCount() const
get the number of beads in the molecule
Definition molecule.h:65
Index getId() const
get the molecule ID
Definition molecule.h:48
topology of the whole system
Definition topology.h:81
double getTime() const
Definition topology.h:317
Residue & CreateResidue(std::string name)
Create a new resiude.
Definition topology.h:462
void setBox(const Eigen::Matrix3d &box, BoundaryCondition::eBoxtype boxtype=BoundaryCondition::typeAuto)
Definition topology.h:272
Index MoleculeCount() const
number of molecules in the system
Definition topology.h:144
BeadContainer & Beads()
Definition topology.h:169
void setTime(double t)
Definition topology.h:311
bool BeadTypeExist(std::string type) const
Determine if a bead type exists.
Definition topology.cc:210
const Eigen::Matrix3d & getBox() const
Definition topology.h:298
Bead * CreateBead(Bead::Symmetry symmetry, std::string name, std::string type, Index resnr, double m, double q)
Creates a new Bead.
Definition topology.h:441
void SetHasVel(const bool v)
Definition topology.h:400
void SetHasForce(const bool v)
Definition topology.h:403
Index getStep() const
Definition topology.h:329
Molecule * getMolecule(const Index i)
Definition topology.h:231
void RegisterBeadType(std::string type)
Register the bead type with the topology object.
Definition topology.cc:214
Molecule * CreateMolecule(std::string name)
Creates a new molecule.
Definition topology.h:449
void setStep(Index s)
Definition topology.h:323
MoleculeContainer & Molecules()
Definition topology.h:182
ResidueContainer & Residues()
Definition topology.h:175
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)
Definition csg_map.cc:201
STL namespace.
FileFormatFactory< TrajectoryWriter > & TrjWriterFactory()
Eigen::Index Index
Definition types.h:26