26#include <boost/lexical_cast.hpp>
50 throw runtime_error(file +
": unknown topology format");
53 reader->ReadTopology(file, *
top_);
65 for (
auto &it : property) {
66 if (it.name() ==
"h5md_particle_group") {
68 }
else if (it.name() ==
"molecules") {
72 }
else if (it.name() ==
"bonded") {
74 }
else if (it.name() ==
"box") {
76 }
else if (it.name() ==
"beadtypes") {
79 throw runtime_error(
"unknown tag: topology." + it.name());
85 Eigen::Matrix3d m = Eigen::Matrix3d::Zero();
94 if (it.name() ==
"clear") {
96 }
else if (it.name() ==
"rename") {
97 string molname = it.getAttribute<
string>(
"name");
98 string range = it.getAttribute<
string>(
"range");
100 }
else if (it.name() ==
"define" || it.name() ==
"molecule") {
101 string molname = it.getAttribute<
string>(
"name");
103 if (it.name() ==
"define") {
104 first = it.getAttribute<
Index>(
"first");
106 Index nbeads = it.getAttribute<
Index>(
"nbeads");
107 Index nmols = it.getAttribute<
Index>(
"nmols");
108 if (it.name() ==
"define" && first < 1) {
109 throw std::runtime_error(
110 "Attribute first is suppose to be > 0, but found " +
111 boost::lexical_cast<string>(it.getAttribute<
string>(
"first")));
114 throw std::runtime_error(
115 "Attribute nbeads is suppose to be > 0, but found " +
116 boost::lexical_cast<string>(it.getAttribute<
string>(
"nbeads")));
119 throw std::runtime_error(
120 "Attribute nmols is suppose to be > 0, but found " +
121 boost::lexical_cast<string>(it.getAttribute<
string>(
"nmols")));
123 if (it.name() ==
"define") {
127 throw std::runtime_error(
128 "The defined list of beads only works for pure xml topology, "
129 "without 'base' attribute.");
139 vector<XMLBead *> xmlBeads;
140 vector<Index> xmlResidues;
142 if (it.name() ==
"bead") {
143 string atname = it.getAttribute<
string>(
"name");
144 string attype = it.getAttribute<
string>(
"type");
148 atmass = it.getAttribute<
double>(
"mass");
149 }
catch (runtime_error &) {
153 atq = it.getAttribute<
double>(
"q");
154 }
catch (runtime_error &) {
158 resid = it.getAttribute<
Index>(
"resid");
160 throw std::invalid_argument(
161 "Residue count for beads in topology.molecules.molecule has to "
162 "be greater than zero");
164 }
catch (runtime_error &) {
167 if (!xmlResidues.empty()) {
168 if (xmlResidues.back() != resid && xmlResidues.back() != resid - 1) {
169 throw std::invalid_argument(
170 "Residue count for beads in topology.molecules.molecule does not "
171 "increase monotonically in steps of 1");
173 if (xmlResidues.back() != -1 && resid == -1) {
174 throw std::invalid_argument(
175 "Residue count for beads in topology.molecules.molecule has to "
176 "be declared for all beads or for none");
179 xmlResidues.push_back(resid);
181 xmlBeads.push_back(xmlBead);
183 throw std::runtime_error(
184 "Wrong element under topology.molecules.molecule: " + it.name());
187 if (xmlResidues.size() != xmlBeads.size()) {
188 throw std::runtime_error(
189 "Number of elements in bead-vector and residue-vector are not "
194 if (!xmlResidues.empty()) {
195 if (xmlResidues.front() != resnr + 1 && xmlResidues.front() != -1) {
196 throw std::runtime_error(
197 "Residue count for beads in topology.molecules.molecule has to be "
198 "greater than the number of residues already in the topology");
201 for (
Index mn = 0; mn < nmols; mn++) {
205 xmlMolecule->
mi = mi;
206 molecules_.insert(make_pair(molname, xmlMolecule));
207 vector<Index>::iterator resit = xmlResidues.begin();
208 for (vector<XMLBead *>::iterator itb = xmlBeads.begin();
209 itb != xmlBeads.end(); ++itb, ++resit) {
227 mi->
AddBead(bead, bname.str());
233 throw std::runtime_error(
"Atom " + b.
name +
" in molecule " + molname +
243 for (
auto &xmlBead : xmlBeads) {
249 for (
auto &it : el) {
250 if (it.name() ==
"rename") {
251 string name = it.getAttribute<
string>(
"name");
252 string newname = it.getAttribute<
string>(
"newname");
253 if (name ==
"" || newname ==
"") {
254 throw runtime_error(
"invalid rename tag, name or newname are empty.");
257 }
else if (it.name() ==
"mass") {
258 string name = it.getAttribute<
string>(
"name");
259 double value = it.getAttribute<
double>(
"value");
262 throw std::runtime_error(
"Wrong element under beadtypes: " + it.name());
268 for (
auto &it : el) {
269 if (it.name() ==
"bond") {
271 }
else if (it.name() ==
"angle") {
273 }
else if (it.name() ==
"dihedral") {
276 throw std::runtime_error(
"Wrong element under bonded: " + it.name());
282 string name = p.
get(
"name").
as<
string>();
283 string beads = p.
get(
"beads").
as<
string>();
285 if (bead_list.size() % 2 == 1) {
286 throw runtime_error(
"Wrong number of beads in bond: " + name);
289 typedef pair<MoleculesMap::iterator, MoleculesMap::iterator> MRange;
291 for (vector<string>::iterator it = bead_list.begin();
292 it != bead_list.end();) {
298 for (MoleculesMap::iterator itm = mRange.first; itm != mRange.second;
312 throw std::runtime_error(
313 "Beads from different molecules, not supported!");
319 string name = p.
get(
"name").
as<
string>();
320 string beads = p.
get(
"beads").
as<
string>();
322 if (bead_list.size() % 3 == 1) {
323 throw runtime_error(
"Wrong number of beads in angle: " + name);
326 typedef pair<MoleculesMap::iterator, MoleculesMap::iterator> MRange;
328 for (vector<string>::iterator it = bead_list.begin();
329 it != bead_list.end();) {
336 for (MoleculesMap::iterator itm = mRange.first; itm != mRange.second;
351 throw std::runtime_error(
352 "Beads from different molecules, not supported!");
357 string name = p.
get(
"name").
as<
string>();
358 string beads = p.
get(
"beads").
as<
string>();
360 if (bead_list.size() % 4 == 1) {
361 throw runtime_error(
"Wrong number of beads in dihedral: " + name);
364 typedef pair<MoleculesMap::iterator, MoleculesMap::iterator> MRange;
366 for (vector<string>::iterator it = bead_list.begin();
367 it != bead_list.end();) {
376 for (MoleculesMap::iterator itm = mRange.first; itm != mRange.second;
393 throw std::runtime_error(
394 "Beads from different molecules, not supported!");
403 for (
auto &bead : xmlMolecule->
beads) {
406 xmlMolecule->
beads.clear();
base class for all interactions
void setGroup(const std::string &group)
void setIndex(const Index &index)
void setMolecule(const Index &mol)
information about molecules
void AddBead(Bead *bead, const std::string &name)
Add a bead to the molecule.
void AddInteraction(Interaction *ic)
Add an interaction to the molecule.
Index getId() const
get the molecule ID
topology of the whole system
Residue & CreateResidue(std::string name)
Create a new resiude.
void setBox(const Eigen::Matrix3d &box, BoundaryCondition::eBoxtype boxtype=BoundaryCondition::typeAuto)
void CreateMoleculesByRange(std::string name, Index first, Index nbeads, Index nmolecules)
create molecules based on blocks of atoms
void RenameMolecules(std::string range, std::string name)
rename all the molecules in range
void RenameBeadType(std::string name, std::string newname)
rename all the bead types
Index ResidueCount() const
bool BeadTypeExist(std::string type) const
Determine if a bead type exists.
Bead * CreateBead(Bead::Symmetry symmetry, std::string name, std::string type, Index resnr, double m, double q)
Creates a new Bead.
void AddBondedInteraction(Interaction *ic)
void RegisterBeadType(std::string type)
Register the bead type with the topology object.
void SetBeadTypeMass(std::string name, double value)
set the mass of all the beads of a certain type
Molecule * CreateMolecule(std::string name)
Creates a new molecule.
void setParticleGroup(std::string particle_group)
std::vector< XMLBead * > beads
std::map< std::string, XMLBead * > name2beads
void ParseBonded(tools::Property &el)
void ParseBeadTypes(tools::Property &el)
void ParseMolecule(tools::Property &p, std::string molname, Index nmols)
void ParseBond(tools::Property &p)
void ParseMolecules(tools::Property &p)
bool ReadTopology(std::string filename, Topology &top) override
read a topology file
void ParseAngle(tools::Property &p)
void ReadTopolFile(std::string file)
~XMLTopologyReader() override
void ParseDihedral(tools::Property &p)
void ParseBox(tools::Property &p)
void ParseRoot(tools::Property &property)
FileFormatFactory< TopologyReader > & TopReaderFactory()
base class for all analysis tools