Enable parallel tests.
[hoomd-blue.git] / libhoomd / analyzers / MOL2DumpWriter.cc
blob15c3d492fdc2cbfbcafb2cbfe65c57dff5bb7270
1 /*
2 Highly Optimized Object-oriented Many-particle Dynamics -- Blue Edition
3 (HOOMD-blue) Open Source Software License Copyright 2009-2014 The Regents of
4 the University of Michigan All rights reserved.
6 HOOMD-blue may contain modifications ("Contributions") provided, and to which
7 copyright is held, by various Contributors who have granted The Regents of the
8 University of Michigan the right to modify and/or distribute such Contributions.
10 You may redistribute, use, and create derivate works of HOOMD-blue, in source
11 and binary forms, provided you abide by the following conditions:
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions, and the following disclaimer both in the code and
15 prominently in any materials provided with the distribution.
17 * Redistributions in binary form must reproduce the above copyright notice, this
18 list of conditions, and the following disclaimer in the documentation and/or
19 other materials provided with the distribution.
21 * All publications and presentations based on HOOMD-blue, including any reports
22 or published results obtained, in whole or in part, with HOOMD-blue, will
23 acknowledge its use according to the terms posted at the time of submission on:
24 http://codeblue.umich.edu/hoomd-blue/citations.html
26 * Any electronic documents citing HOOMD-Blue will link to the HOOMD-Blue website:
27 http://codeblue.umich.edu/hoomd-blue/
29 * Apart from the above required attributions, neither the name of the copyright
30 holder nor the names of HOOMD-blue's contributors may be used to endorse or
31 promote products derived from this software without specific prior written
32 permission.
34 Disclaimer
36 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' AND
37 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND/OR ANY
39 WARRANTIES THAT THIS SOFTWARE IS FREE OF INFRINGEMENT ARE DISCLAIMED.
41 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
43 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 // Maintainer: joaander
52 /*! \file MOL2DumpWriter.cc
53 \brief Defines the MOL2DumpWriter class
56 #ifdef WIN32
57 #pragma warning( push )
58 #pragma warning( disable : 4244 )
59 #endif
61 #include <boost/python.hpp>
62 using namespace boost::python;
64 #include <iomanip>
65 #include <fstream>
66 #include <stdexcept>
67 #include <boost/shared_ptr.hpp>
69 #include "MOL2DumpWriter.h"
70 #include "BondedGroupData.h"
72 using namespace std;
74 /*! \param sysdef SystemDefinition containing the ParticleData to dump
75 \param fname_base The base file name to write the output to
77 MOL2DumpWriter::MOL2DumpWriter(boost::shared_ptr<SystemDefinition> sysdef, std::string fname_base)
78 : Analyzer(sysdef), m_base_fname(fname_base)
80 m_exec_conf->msg->notice(5) << "Constructing MOL2DumpWriter: " << fname_base << endl;
83 MOL2DumpWriter::~MOL2DumpWriter()
85 m_exec_conf->msg->notice(5) << "Destroying PDBDumpWriter" << endl;
88 /*! \param timestep Current time step of the simulation
89 Writes a snapshot of the current state of the ParticleData to a mol2 file
91 void MOL2DumpWriter::analyze(unsigned int timestep)
93 if (m_prof)
94 m_prof->push("Dump MOL2");
96 ostringstream full_fname;
97 string filetype = ".mol2";
99 // Generate a filename with the timestep padded to ten zeros
100 full_fname << m_base_fname << "." << setfill('0') << setw(10) << timestep << filetype;
101 writeFile(full_fname.str());
103 if (m_prof)
104 m_prof->pop();
107 /*! \param fname File name to write
109 void MOL2DumpWriter::writeFile(std::string fname)
111 // open the file for writing
112 ofstream f(fname.c_str());
114 if (!f.good())
116 m_exec_conf->msg->error() << "dump.mol2: Unable to open dump file for writing: " << fname << endl;
117 throw runtime_error("Error writting mol2 dump file");
120 // acquire the particle data
121 ArrayHandle<Scalar4> h_pos(m_pdata->getPositions(), access_location::host, access_mode::read);
122 ArrayHandle<unsigned int> h_rtag(m_pdata->getRTags(), access_location::host, access_mode::read);
124 // write the header
125 f << "@<TRIPOS>MOLECULE" << "\n";
126 f << "Generated by HOOMD" << "\n";
127 int num_bonds = 1;
128 boost::shared_ptr<BondData> bond_data = m_sysdef->getBondData();
129 if (bond_data && bond_data->getN() > 0)
130 num_bonds = bond_data->getN();
132 f << m_pdata->getN() << " " << num_bonds << "\n";
133 f << "NO_CHARGES" << "\n";
135 f << "@<TRIPOS>ATOM" << "\n";
136 for (unsigned int j = 0; j < m_pdata->getN(); j++)
138 // use the rtag data to output the particles in the order they were read in
139 int i;
140 i= h_rtag.data[j];
142 // get the coordinates
143 Scalar x = (h_pos.data[i].x);
144 Scalar y = (h_pos.data[i].y);
145 Scalar z = (h_pos.data[i].z);
147 // get the type by name
148 unsigned int type_id = __scalar_as_int(h_pos.data[i].w);
149 string type_name = m_pdata->getNameByType(type_id);
151 // this is intended to go to VMD, so limit the type name to 15 characters
152 if (type_name.size() > 15)
154 m_exec_conf->msg->error() << "dump.mol2: Type name <" << type_name << "> too long: please limit to 15 characters" << endl;
155 throw runtime_error("Error writting mol2 dump file");
158 f << j+1 << " " << type_name << " " << x << " " << y << " "<< z << " " << type_name << "\n";
160 if (!f.good())
162 m_exec_conf->msg->error() << "dump.mol2: I/O error while writing MOL2 dump file" << endl;
163 throw runtime_error("Error writting mol2 dump file");
167 f << "@<TRIPOS>BOND" << "\n";
168 if (bond_data && bond_data->getN() > 0)
170 for (unsigned int i = 0; i < bond_data->getN(); i++)
172 BondData::members_t b = bond_data->getMembersByIndex(i);
173 f << i+1 << " " << b.tag[0]+1 << " " << b.tag[1]+1 << " 1" << "\n";
176 else
178 // write a dummy bond since VMD doesn't like loading mol2 files without bonds
179 f << "1 1 2 1" << "\n";
182 if (!f.good())
184 m_exec_conf->msg->error() << "dump.mol2: I/O error while writing file" << endl;
185 throw runtime_error("Error writting mol2 dump file");
188 f.close();
191 void export_MOL2DumpWriter()
193 class_<MOL2DumpWriter, boost::shared_ptr<MOL2DumpWriter>, bases<Analyzer>, boost::noncopyable>
194 ("MOL2DumpWriter", init< boost::shared_ptr<SystemDefinition>, std::string >())
195 .def("writeFile", &MOL2DumpWriter::writeFile)
200 #ifdef WIN32
201 #pragma warning( pop )
202 #endif